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 gPTP clock data.
35  */
36 struct gptp_clock_data {
37 	/** gptp_domain pointer */
38 	struct gptp_domain *domain;
39 	/** pi control drift value */
40 	double pi_drift;
41 };
42 
43 extern struct gptp_clock_data gptp_clock;
44 
45 /**
46  * @brief Is a slave acting as a slave.
47  *
48  * Utility to check if a port is configured as a slave.
49  *
50  * @param port Port to check.
51  *
52  * @return True if this is a slave port.
53  */
54 bool gptp_is_slave_port(int port);
55 
56 /**
57  * @brief Convert the network interface to the correct port number.
58  *
59  * @param iface Network Interface acting as a ptp port.
60  *
61  * @return Number of the port if found, ENODEV otherwise.
62  */
63 int gptp_get_port_number(struct net_if *iface);
64 
65 /**
66  * @brief Calculate a logInterval and store in Uscaled ns structure.
67  *
68  * @param interval Result of calculation.
69  *
70  * @param seconds Seconds of interval.
71  *
72  * @param log_msg_interval Logarithm 2 to apply to this interval.
73  */
74 void gptp_set_time_itv(struct gptp_uscaled_ns *interval,
75 		       uint16_t seconds,
76 		       int8_t log_msg_interval);
77 
78 /**
79  * @brief Convert uscaled ns to ms for timer use.
80  *
81  * @param usns Pointer to uscaled nanoseconds to convert.
82  *
83  * @return INT32_MAX if value exceed timer max value, 0 if the result of the
84  *	    conversion is less 1ms, the converted value otherwise.
85  */
86 int32_t gptp_uscaled_ns_to_timer_ms(struct gptp_uscaled_ns *usns);
87 
88 /**
89  * @brief Update pDelay request interval and its timer.
90  *
91  * @param port Port number.
92  *
93  * @param log_val New logarithm 2 to apply to this interval.
94  */
95 void gptp_update_pdelay_req_interval(int port, int8_t log_val);
96 
97 /**
98  * @brief Update sync interval and its timer.
99  *
100  * @param port Port number.
101  *
102  * @param log_val New logarithm 2 to apply to this interval.
103  */
104 void gptp_update_sync_interval(int port, int8_t log_val);
105 
106 /**
107  * @brief Update announce interval and its timer.
108  *
109  * @param port Port number.
110  *
111  * @param log_val New logarithm 2 to apply to this interval.
112  */
113 
114 void gptp_update_announce_interval(int port, int8_t log_val);
115 
116 /**
117  * @brief Convert a ptp timestamp to nanoseconds.
118  *
119  * @param ts A PTP timestamp.
120  *
121  * @return Number of nanoseconds.
122  */
gptp_timestamp_to_nsec(struct net_ptp_time * ts)123 static inline uint64_t gptp_timestamp_to_nsec(struct net_ptp_time *ts)
124 {
125 	if (!ts) {
126 		return 0;
127 	}
128 
129 	return (ts->second * NSEC_PER_SEC) + ts->nanosecond;
130 }
131 
132 /**
133  * @brief gPTP PI servo.
134  *
135  * @param nanosecond_diff nanosecond offset.
136  *
137  * @return ppb value to adjust.
138  */
139 double gptp_servo_pi(int64_t nanosecond_diff);
140 
141 /**
142  * @brief Change the port state
143  *
144  * @param port Port number of the clock to use.
145  * @param state New state
146  */
147 #if CONFIG_NET_GPTP_LOG_LEVEL < LOG_LEVEL_DBG
148 void gptp_change_port_state(int port, enum gptp_port_state state);
149 #else
150 #define gptp_change_port_state(port, state)			\
151 	gptp_change_port_state_debug(port, state, __func__, __LINE__)
152 
153 void gptp_change_port_state_debug(int port, enum gptp_port_state state,
154 				  const char *caller, int line);
155 #endif
156 
157 #if CONFIG_NET_GPTP_LOG_LEVEL < LOG_LEVEL_DBG
158 void gptp_change_pa_info_state(
159 	int port,
160 	struct gptp_port_announce_information_state *pa_info_state,
161 	enum gptp_pa_info_states state);
162 #else
163 #define gptp_change_pa_info_state(port, pa_info_state, state)		\
164 	gptp_change_pa_info_state_debug(port, pa_info_state, state,	\
165 					__func__, __LINE__)
166 
167 void gptp_change_pa_info_state_debug(
168 	int port,
169 	struct gptp_port_announce_information_state *pa_info_state,
170 	enum gptp_pa_info_states state,
171 	const char *caller, int line);
172 #endif
173 
174 #ifdef __cplusplus
175 }
176 #endif
177 
178 #endif /* __GPTP_PRIVATE_H */
179