forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptp.h
343 lines (276 loc) · 7.89 KB
/
gptp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (c) 2017 Intel Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Public functions for the Precision Time Protocol Stack.
*
*/
#ifndef ZEPHYR_INCLUDE_NET_GPTP_H_
#define ZEPHYR_INCLUDE_NET_GPTP_H_
/**
* @brief generic Precision Time Protocol (gPTP) support
* @defgroup gptp gPTP support
* @ingroup networking
* @{
*/
#include <net/net_core.h>
#include <net/ptp_time.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/** @cond INTERNAL_HIDDEN */
#define GPTP_OFFSET_SCALED_LOG_VAR_UNKNOWN 0x436A
#define GPTP_PRIORITY1_NON_GM_CAPABLE 255
#define GPTP_PRIORITY1_GM_CAPABLE 248
#define GPTP_PRIORITY2_DEFAULT 248
/** @endcond */
/**
* @brief Scaled Nanoseconds.
*/
struct gptp_scaled_ns {
/** High half. */
s32_t high;
/** Low half. */
s64_t low;
} __packed;
/**
* @brief UScaled Nanoseconds.
*/
struct gptp_uscaled_ns {
/** High half. */
u32_t high;
/** Low half. */
u64_t low;
} __packed;
/** @cond INTERNAL_HIDDEN */
#if defined(CONFIG_NEWLIB_LIBC)
#include <math.h>
#define GPTP_POW2(exp) pow(2, exp)
#else
static inline double gptp_pow2(int exp)
{
double res;
if (exp >= 0) {
res = 1 << exp;
} else {
res = 1.0;
while (exp++) {
res /= 2;
}
}
return res;
}
#define GPTP_POW2(exp) gptp_pow2(exp)
#endif
/* Pre-calculated constants */
/* 2^16 */
#define GPTP_POW2_16 65536.0
/* 2^41 */
#define GPTP_POW2_41 2199023255552.0
/* Message types. Event messages have BIT(3) set to 0, and general messages
* have that bit set to 1. IEEE 802.1AS chapter 10.5.2.2.2
*/
#define GPTP_SYNC_MESSAGE 0x00
#define GPTP_DELAY_REQ_MESSAGE 0x01
#define GPTP_PATH_DELAY_REQ_MESSAGE 0x02
#define GPTP_PATH_DELAY_RESP_MESSAGE 0x03
#define GPTP_FOLLOWUP_MESSAGE 0x08
#define GPTP_DELAY_RESP_MESSAGE 0x09
#define GPTP_PATH_DELAY_FOLLOWUP_MESSAGE 0x0a
#define GPTP_ANNOUNCE_MESSAGE 0x0b
#define GPTP_SIGNALING_MESSAGE 0x0c
#define GPTP_MANAGEMENT_MESSAGE 0x0d
#define GPTP_IS_EVENT_MSG(msg_type) (!((msg_type) & BIT(3)))
#define GPTP_CLOCK_ID_LEN 8
/** @endcond */
/**
* @brief Port Identity.
*/
struct gptp_port_identity {
/** Clock identity of the port. */
u8_t clk_id[GPTP_CLOCK_ID_LEN];
/** Number of the port. */
u16_t port_number;
} __packed;
struct gptp_flags {
union {
/** Byte access. */
u8_t octets[2];
/** Whole field access. */
u16_t all;
};
} __packed;
struct gptp_hdr {
/** Type of the message. */
u8_t message_type:4;
/** Transport specific, always 1. */
u8_t transport_specific:4;
/** Version of the PTP, always 2. */
u8_t ptp_version:4;
/** Reserved field. */
u8_t reserved0:4;
/** Total length of the message from the header to the last TLV. */
u16_t message_length;
/** Domain number, always 0. */
u8_t domain_number;
/** Reserved field. */
u8_t reserved1;
/** Message flags. */
struct gptp_flags flags;
/** Correction Field. The content depends of the message type. */
s64_t correction_field;
/** Reserved field. */
u32_t reserved2;
/** Port Identity of the sender. */
struct gptp_port_identity port_id;
/** Sequence Id. */
u16_t sequence_id;
/** Control value. Sync: 0, Follow-up: 2, Others: 5. */
u8_t control;
/** Message Interval in Log2 for Sync and Announce messages. */
s8_t log_msg_interval;
} __packed;
/** @cond INTERNAL_HIDDEN */
#define GPTP_GET_CURRENT_TIME_USCALED_NS(port, uscaled_ns_ptr) \
do { \
(uscaled_ns_ptr)->low = \
gptp_get_current_time_nanosecond(port) << 16; \
(uscaled_ns_ptr)->high = 0; \
} while (false)
/** @endcond */
/**
* @typedef gptp_phase_dis_callback_t
* @brief Define callback that is called after a phase discontinuity has been
* sent by the grandmaster.
* @param "u8_t *gm_identity" A pointer to first element of a
* ClockIdentity array. The size of the array is GPTP_CLOCK_ID_LEN.
* @param "u16_t *gm_time_base" A pointer to the value of timeBaseIndicator
* of the current grandmaster.
* @param "struct scaled_ns *last_gm_ph_change" A pointer to the value of
* lastGmPhaseChange received from grandmaster.
* @param "double *last_gm_freq_change" A pointer to the value of
* lastGmFreqChange received from the grandmaster.
*/
typedef void (*gptp_phase_dis_callback_t)(
u8_t *gm_identity,
u16_t *time_base,
struct gptp_scaled_ns *last_gm_ph_change,
double *last_gm_freq_change);
/**
* @brief Phase discontinuity callback structure.
*
* Stores the phase discontinuity callback information. Caller must make sure
* that the variable pointed by this is valid during the lifetime of
* registration. Typically this means that the variable cannot be
* allocated from stack.
*/
struct gptp_phase_dis_cb {
/** Node information for the slist. */
sys_snode_t node;
/** Phase discontinuity callback. */
gptp_phase_dis_callback_t cb;
};
/**
* @brief ClockSourceTime.invoke function parameters
*
* Parameters passed by ClockSourceTime.invoke function.
*/
struct gptp_clk_src_time_invoke_params {
/** Frequency change on the last Time Base Indicator Change. */
double last_gm_freq_change;
/** The time this function is invoked. */
struct net_ptp_extended_time src_time;
/** Phase change on the last Time Base Indicator Change. */
struct gptp_scaled_ns last_gm_phase_change;
/** Time Base - changed only if Phase or Frequency changes. */
u16_t time_base_indicator;
};
/**
* @brief Register a phase discontinuity callback.
*
* @param phase_dis Caller specified handler for the callback.
* @param cb Callback to register.
*/
void gptp_register_phase_dis_cb(struct gptp_phase_dis_cb *phase_dis,
gptp_phase_dis_callback_t cb);
/**
* @brief Unregister a phase discontinuity callback.
*
* @param phase_dis Caller specified handler for the callback.
*/
void gptp_unregister_phase_dis_cb(struct gptp_phase_dis_cb *phase_dis);
/**
* @brief Call a phase discontinuity callback function.
*/
void gptp_call_phase_dis_cb(void);
/**
* @brief Get gPTP time.
*
* @param slave_time A pointer to structure where timestamp will be saved.
* @param gm_present A pointer to a boolean where status of the
* presence of a grand master will be saved.
*
* @return Error code. 0 if no error.
*/
int gptp_event_capture(struct net_ptp_time *slave_time, bool *gm_present);
/**
* @brief Utility function to print clock id to a user supplied buffer.
*
* @param clk_id Clock id
* @param output Output buffer
* @param output_len Output buffer len
*
* @return Pointer to output buffer
*/
char *gptp_sprint_clock_id(const u8_t *clk_id, char *output,
size_t output_len);
/**
* @typedef gptp_port_cb_t
* @brief Callback used while iterating over gPTP ports
*
* @param port Port number
* @param iface Pointer to network interface
* @param user_data A valid pointer to user data or NULL
*/
typedef void (*gptp_port_cb_t)(int port, struct net_if *iface,
void *user_data);
/**
* @brief Go through all the gPTP ports and call callback for each of them.
*
* @param cb User-supplied callback function to call
* @param user_data User specified data
*/
void gptp_foreach_port(gptp_port_cb_t cb, void *user_data);
/**
* @brief Get gPTP domain.
* @details This contains all the configuration / status of the gPTP domain.
*
* @return Pointer to domain or NULL if not found.
*/
struct gptp_domain *gptp_get_domain(void);
/**
* @brief This interface is used by the ClockSource entity to provide time to
* the ClockMaster entity of a time-aware system.
*
* @param arg Current state and parameters of the ClockSource entity.
*/
void gptp_clk_src_time_invoke(struct gptp_clk_src_time_invoke_params *arg);
/**
* @brief Return pointer to gPTP packet header in network packet.
*
* @param pkt Network packet (received or sent)
*
* @return Pointer to gPTP header.
*/
struct gptp_hdr *gptp_get_hdr(struct net_pkt *pkt);
#ifdef __cplusplus
}
#endif
/**
* @}
*/
#endif /* ZEPHYR_INCLUDE_NET_GPTP_H_ */