1 /*
2 * Copyright (c) 2017 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file
9 * @brief Private functions for the Precision Time Protocol Stack.
10 *
11 * This is not to be included by the application.
12 */
13
14 #ifndef __GPTP_PRIVATE_H
15 #define __GPTP_PRIVATE_H
16
17 #include <zephyr/net/gptp.h>
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 /* Common defines for the gPTP stack. */
24 #define GPTP_THREAD_WAIT_TIMEOUT_MS 1
25 #define GPTP_MULTIPLE_PDELAY_RESP_WAIT (5 * 60 * MSEC_PER_SEC)
26
27 #if defined(CONFIG_NET_GPTP_STATISTICS)
28 #define GPTP_STATS_INC(port, var) (GPTP_PORT_PARAM_DS(port)->var++)
29 #else
30 #define GPTP_STATS_INC(port, var)
31 #endif
32
33 /**
34 * @brief Is a slave acting as a slave.
35 *
36 * Utility to check if a port is configured as a slave.
37 *
38 * @param port Port to check.
39 *
40 * @return True if this is a slave port.
41 */
42 bool gptp_is_slave_port(int port);
43
44 /**
45 * @brief Convert the network interface to the correct port number.
46 *
47 * @param iface Network Interface acting as a ptp port.
48 *
49 * @return Number of the port if found, ENODEV otherwise.
50 */
51 int gptp_get_port_number(struct net_if *iface);
52
53 /**
54 * @brief Calculate a logInterval and store in Uscaled ns structure.
55 *
56 * @param interval Result of calculation.
57 *
58 * @param seconds Seconds of interval.
59 *
60 * @param log_msg_interval Logarithm 2 to apply to this interval.
61 */
62 void gptp_set_time_itv(struct gptp_uscaled_ns *interval,
63 uint16_t seconds,
64 int8_t log_msg_interval);
65
66 /**
67 * @brief Convert uscaled ns to ms for timer use.
68 *
69 * @param usns Pointer to uscaled nanoseconds to convert.
70 *
71 * @return INT32_MAX if value exceed timer max value, 0 if the result of the
72 * conversion is less 1ms, the converted value otherwise.
73 */
74 int32_t gptp_uscaled_ns_to_timer_ms(struct gptp_uscaled_ns *usns);
75
76 /**
77 * @brief Update pDelay request interval and its timer.
78 *
79 * @param port Port number.
80 *
81 * @param log_val New logarithm 2 to apply to this interval.
82 */
83 void gptp_update_pdelay_req_interval(int port, int8_t log_val);
84
85 /**
86 * @brief Update sync interval and its timer.
87 *
88 * @param port Port number.
89 *
90 * @param log_val New logarithm 2 to apply to this interval.
91 */
92 void gptp_update_sync_interval(int port, int8_t log_val);
93
94 /**
95 * @brief Update announce interval and its timer.
96 *
97 * @param port Port number.
98 *
99 * @param log_val New logarithm 2 to apply to this interval.
100 */
101
102 void gptp_update_announce_interval(int port, int8_t log_val);
103
104 /**
105 * @brief Convert a ptp timestamp to nanoseconds.
106 *
107 * @param ts A PTP timestamp.
108 *
109 * @return Number of nanoseconds.
110 */
gptp_timestamp_to_nsec(struct net_ptp_time * ts)111 static inline uint64_t gptp_timestamp_to_nsec(struct net_ptp_time *ts)
112 {
113 if (!ts) {
114 return 0;
115 }
116
117 return (ts->second * NSEC_PER_SEC) + ts->nanosecond;
118 }
119
120 /**
121 * @brief Change the port state
122 *
123 * @param port Port number of the clock to use.
124 * @param state New state
125 */
126 #if CONFIG_NET_GPTP_LOG_LEVEL < LOG_LEVEL_DBG
127 void gptp_change_port_state(int port, enum gptp_port_state state);
128 #else
129 #define gptp_change_port_state(port, state) \
130 gptp_change_port_state_debug(port, state, __func__, __LINE__)
131
132 void gptp_change_port_state_debug(int port, enum gptp_port_state state,
133 const char *caller, int line);
134 #endif
135
136 #if CONFIG_NET_GPTP_LOG_LEVEL < LOG_LEVEL_DBG
137 void gptp_change_pa_info_state(
138 int port,
139 struct gptp_port_announce_information_state *pa_info_state,
140 enum gptp_pa_info_states state);
141 #else
142 #define gptp_change_pa_info_state(port, pa_info_state, state) \
143 gptp_change_pa_info_state_debug(port, pa_info_state, state, \
144 __func__, __LINE__)
145
146 void gptp_change_pa_info_state_debug(
147 int port,
148 struct gptp_port_announce_information_state *pa_info_state,
149 enum gptp_pa_info_states state,
150 const char *caller, int line);
151 #endif
152
153 #ifdef __cplusplus
154 }
155 #endif
156
157 #endif /* __GPTP_PRIVATE_H */
158