1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 /**************************************************************************/
12 /**************************************************************************/
13 /**                                                                       */
14 /** NetX PTP Client Component                                             */
15 /**                                                                       */
16 /**   Precision Time Protocol (PTP)                                       */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 /**************************************************************************/
22 /*                                                                        */
23 /*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
24 /*                                                                        */
25 /*    nxd_ptp_client.h                                    PORTABLE C      */
26 /*                                                           6.4.0        */
27 /*  AUTHOR                                                                */
28 /*                                                                        */
29 /*    Yuxin Zhou, Microsoft Corporation                                   */
30 /*                                                                        */
31 /*  DESCRIPTION                                                           */
32 /*                                                                        */
33 /*    This file defines the NetX Precision Time Protocol (PTP)            */
34 /*    Client component, including all data types and external references. */
35 /*                                                                        */
36 /*  RELEASE HISTORY                                                       */
37 /*                                                                        */
38 /*    DATE              NAME                      DESCRIPTION             */
39 /*                                                                        */
40 /*  12-31-2020     Yuxin Zhou               Initial Version 6.1.3         */
41 /*  12-31-2023     Tiejun Zhou              Modified comment(s), and      */
42 /*                                            supported gPTP profile,     */
43 /*                                            supported master clock,     */
44 /*                                            resulting in version 6.4.0  */
45 /*                                                                        */
46 /**************************************************************************/
47 
48 #ifndef NXD_PTP_CLIENT_H
49 #define NXD_PTP_CLIENT_H
50 
51 
52 /* Include NetX and ThreadX definitions */
53 
54 #include "nx_api.h"
55 
56 
57 /* Determine if a C++ compiler is being used.  If so, ensure that standard
58    C is used to process the API information.  */
59 
60 #ifdef   __cplusplus
61 
62 /* Yes, C++ compiler is present.  Use standard C.  */
63 extern   "C" {
64 
65 #endif
66 
67 
68 /* Define the PTP Client ID */
69 
70 #define NX_PTP_CLIENT_ID 0x50545001UL
71 
72 /* PTP Client configurable options.  */
73 /* Enable GPTP configuration as per IEEE 802.1AS. PTP messages are transmitted over Ethernet with peer delay request mechanism.
74    If not enabled by default, PTP messages are transmitted over UDP with delay request mechanism. */
75 /*
76 #define NX_ENABLE_GPTP
77 */
78 
79 /* Enable PTP master feature. The role can be selected at runtime. Supported roles,
80    * NX_PTP_CLIENT_ROLE_SLAVE_ONLY
81    * NX_PTP_CLIENT_ROLE_MASTER_ONLY
82    * NX_PTP_CLIENT_ROLE_SLAVE_AND_MASTER */
83 /*
84 #define NX_PTP_ENABLE_MASTER
85 */
86 
87 /* Disable PTP slave feature. Only master can be supported.  */
88 /*
89 #define NX_PTP_DISABLE_SLAVE
90 */
91 
92 /* Enable sending reverse sync when the role of PTP clock becomes slave. This will allow master clock
93    to be aware of the offset from slave to master. Reverse SYNC may be ignored by master clock.  */
94 /*
95 #define NX_PTP_ENABLE_REVERSE_SYNC
96 */
97 
98 /* PTP Client packet type. By default, UDP is used as transport.  */
99 #ifndef NX_PTP_CLIENT_TRANSPORT_UDP
100 #ifdef NX_ENABLE_GPTP
101 #define NX_PTP_CLIENT_TRANSPORT_UDP      0
102 #else
103 #define NX_PTP_CLIENT_TRANSPORT_UDP      1
104 #endif
105 #endif /* NX_PTP_CLIENT_TRANSPORT_UDP  */
106 
107 #ifndef NX_PTP_CLIENT_TRANSPORT_ETHERNET
108 #ifdef NX_ENABLE_GPTP
109 #define NX_PTP_CLIENT_TRANSPORT_ETHERNET 1
110 #else
111 #define NX_PTP_CLIENT_TRANSPORT_ETHERNET 0
112 #endif
113 #endif /* NX_PTP_CLIENT_TRANSPORT_ETHERNET  */
114 
115 #if NX_PTP_CLIENT_TRANSPORT_UDP && NX_PTP_CLIENT_TRANSPORT_ETHERNET
116 #error "Only one of NX_PTP_CLIENT_TRANSPORT_UDP and NX_PTP_CLIENT_TRANSPORT_ETHERNET can be enabled"
117 #endif
118 
119 #if !NX_PTP_CLIENT_TRANSPORT_UDP && !NX_PTP_CLIENT_TRANSPORT_ETHERNET
120 #error "At least one of NX_PTP_CLIENT_TRANSPORT_UDP or NX_PTP_CLIENT_TRANSPORT_ETHERNET must be enabled"
121 #endif
122 
123 #if (NX_PTP_CLIENT_TRANSPORT_ETHERNET == 1) && !defined(NX_ENABLE_VLAN)
124 #error "NX_ENABLE_VLAN must be enabled for NX_PTP_CLIENT_TRANSPORT_ETHERNET"
125 #endif
126 
127 #if NX_PTP_CLIENT_TRANSPORT_ETHERNET
128 #include "nx_link.h"
129 #endif /* NX_PTP_CLIENT_TRANSPORT_ETHERNET  */
130 
131 /* Set the client thread time slice.  */
132 
133 #ifndef NX_PTP_CLIENT_THREAD_TIME_SLICE
134 #define NX_PTP_CLIENT_THREAD_TIME_SLICE          TX_NO_TIME_SLICE
135 #endif
136 
137 
138 
139 /* Define the PTP client internal timer frequency */
140 
141 #ifndef NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND
142 #define NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND     100
143 #endif
144 
145 
146 /* Define the maximum number of missing Announce packets before timeout */
147 
148 #ifndef NX_PTP_CLIENT_ANNOUNCE_RECEIPT_TIMEOUT
149 #define NX_PTP_CLIENT_ANNOUNCE_RECEIPT_TIMEOUT   3
150 #endif
151 
152 
153 /* Define the time interval between successive Announce packet, expressed as log 2.
154    This value should be uniform throughout a domain. The default value is 1=2s. */
155 
156 #ifndef NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL
157 #define NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL      1
158 #endif
159 
160 /* Define the time interval between successive Sync packet, expressed as log 2.
161    This value should be uniform throughout a domain. The default value is -3=125ms. */
162 
163 #ifndef NX_PTP_CLIENT_LOG_SYNC_INTERVAL
164 #define NX_PTP_CLIENT_LOG_SYNC_INTERVAL          -3
165 #endif
166 
167 /* Define the time interval between successive Pdelay_Req packet, expressed as log 2.
168    This value should be uniform throughout a domain. The default value is 0=1s. */
169 
170 #ifndef NX_PTP_CLIENT_LOG_PDELAY_REQ_INTERVAL
171 #define NX_PTP_CLIENT_LOG_PDELAY_REQ_INTERVAL    0
172 #endif
173 
174 /* Define the interval for sending Delay request packets */
175 
176 #ifndef NX_PTP_CLIENT_DELAY_REQ_INTERVAL
177 #define NX_PTP_CLIENT_DELAY_REQ_INTERVAL         (2 * NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND)
178 #endif
179 
180 /* Set maximum queue depth for client socket.*/
181 
182 #ifndef NX_PTP_CLIENT_MAX_QUEUE_DEPTH
183 #define NX_PTP_CLIENT_MAX_QUEUE_DEPTH            5
184 #endif
185 
186 /* Define the meanLinkDelayThresh as per 802.1AS-2020, 11.2.2.  The default value is 800ns for 100BASE-TX, 1000BASE-T. */
187 #ifndef NX_PTP_CLIENT_DELAY_THRESH
188 #define NX_PTP_CLIENT_DELAY_THRESH               800
189 #endif /* NX_PTP_CLIENT_DELAY_THRESH */
190 
191 
192 /* Define the interval for sending Sync packets */
193 #if (NX_PTP_CLIENT_LOG_SYNC_INTERVAL >= 0)
194 #define NX_PTP_CLIENT_SYNC_INTERVAL              ((1 << NX_PTP_CLIENT_LOG_SYNC_INTERVAL) * \
195                                                 NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND)
196 #else
197 #define NX_PTP_CLIENT_SYNC_INTERVAL              (NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND >> \
198                                                 (-(NX_PTP_CLIENT_LOG_SYNC_INTERVAL)))
199 #endif
200 
201 /* Define the interval for sending Announce packets */
202 #if (NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL >= 0)
203 #define NX_PTP_CLIENT_ANNOUNCE_INTERVAL          ((1 << NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL) * \
204                                                 NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND)
205 #else
206 #define NX_PTP_CLIENT_ANNOUNCE_INTERVAL          (NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND >> \
207                                                 (-(NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL)))
208 #endif
209 
210 /* Define the interval for sending Peer Delay request packets */
211 
212 #if (NX_PTP_CLIENT_LOG_PDELAY_REQ_INTERVAL >= 0)
213 #define NX_PTP_CLIENT_PDELAY_REQ_INTERVAL        ((1 << NX_PTP_CLIENT_LOG_PDELAY_REQ_INTERVAL) * \
214                                                  NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND)
215 #else
216 #define NX_PTP_CLIENT_PDELAY_REQ_INTERVAL        (NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND >> \
217                                                  (-(NX_PTP_CLIENT_LOG_PDELAY_REQ_INTERVAL)))
218 #endif
219 
220 
221 /* Define the maximum size of a PTP message */
222 
223 #define NX_PTP_CLIENT_PACKET_DATA_SIZE           64
224 
225 /* Define Announce receipt timeout expire value */
226 #define NX_PTP_CLIENT_ANNOUNCE_EXPIRATION        (NX_PTP_CLIENT_ANNOUNCE_RECEIPT_TIMEOUT *     \
227                                                 (1 << NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL) * \
228                                                 NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND)
229 
230 
231 /* Define the default priority for local PTP clock running as master mode. */
232 #define NX_PTP_CLIENT_MASTER_PRIORITY            248
233 
234 /* Define the default clock class for local PTP clock running as master mode. */
235 #define NX_PTP_CLIENT_MASTER_CLOCK_CLASS         248
236 
237 /* Define the default accuracy for local PTP clock running as master mode.
238    The default value 0x22 means the time is accurate to within 250ns.  */
239 #define NX_PTP_CLIENT_MASTER_ACCURACY            0x22
240 
241 /* Define  the default clock variance for local PTP clock running as master mode. */
242 #define NX_PTP_CLIENT_MASTER_CLOCK_VARIANCE      17258
243 
244 /* Define  the default clock steps removed for local PTP clock running as master mode. */
245 #define NX_PTP_CLIENT_MASTER_CLOCK_STEPS_REMOVED 0
246 
247 /* Implementations based on IEEE Std 1588-2008 and any implementations based on this edition
248    where the expanded profile isolation capabilities of option 16.5 are not required */
249 #define NX_PTP_TRANSPORT_SPECIFIC_NON_802        0
250 
251 /* Implementations based on PTP Profiles developed by the IEEE 802.1 Working Group */
252 #define NX_PTP_TRANSPORT_SPECIFIC_802            1
253 
254 
255 /* Define a PTP Time */
256 
257 typedef struct NX_PTP_TIME_STRUCT
258 {
259     /* The MSB of the number of seconds */
260     LONG  second_high;
261 
262     /* The LSB of the number of seconds */
263     ULONG second_low;
264 
265     /* The number of nanoseconds */
266     LONG  nanosecond;
267 } NX_PTP_TIME;
268 
269 
270 /* Define the PTP Date Time structure.  */
271 
272 typedef struct NX_PTP_DATE_TIME_STRUCT
273 {
274     UINT  year;
275     UCHAR month;
276     UCHAR day;
277     UCHAR hour;
278     UCHAR minute;
279     UCHAR second;
280     UCHAR weekday;
281     UCHAR reserved[2];
282     ULONG nanosecond;
283 } NX_PTP_DATE_TIME;
284 
285 
286 /* Internal PTP error processing codes.  */
287 
288 #define NX_PTP_ERROR_CONSTANT                      0xD00
289 /* Client side errors.  */
290 #define NX_PTP_CLIENT_NOT_STARTED                  (NX_PTP_ERROR_CONSTANT | 0x01) /* PTP Client task is not running */
291 #define NX_PTP_CLIENT_ALREADY_STARTED              (NX_PTP_ERROR_CONSTANT | 0x02) /* PTP Client task is already running */
292 #define NX_PTP_PARAM_ERROR                         (NX_PTP_ERROR_CONSTANT | 0x03) /* Invalid non pointer parameter.  */
293 #define NX_PTP_CLIENT_INSUFFICIENT_PACKET_PAYLOAD  (NX_PTP_ERROR_CONSTANT | 0x04) /* Client not properly initialized to receive time data.  */
294 #define NX_PTP_CLIENT_CLOCK_CALLBACK_FAILURE       (NX_PTP_ERROR_CONSTANT | 0x05) /* PTP clock callback returns error.  */
295 
296 
297 /* PTP Protocol Definitions */
298 
299 /* Define the size of the PTP Clock Identity field */
300 #define NX_PTP_CLOCK_IDENTITY_SIZE                 8
301 
302 /* Define the size of the PTP Clock Port and Identity field */
303 #define NX_PTP_CLOCK_PORT_IDENTITY_SIZE            (NX_PTP_CLOCK_IDENTITY_SIZE + 2)
304 
305 /* PTP Master Definitions */
306 
307 #define NX_PTP_MASTER_CLOCK_ANNOUNCE_RATE          10 //(every 10 ptp ticks)
308 
309 /* Master time sources */
310 #define NX_PTP_MASTER_TIME_SRC_ATOMIC_CLOCK        0x10
311 #define NX_PTP_MASTER_TIME_SRC_GPS                 0x20
312 #define NX_PTP_MASTER_TIME_SRC_TERRESTRIAL_RADIO   0x30
313 #define NX_PTP_MASTER_TIME_SRC_PTP                 0x40
314 #define NX_PTP_MASTER_TIME_SRC_NTP                 0x50
315 #define NX_PTP_MASTER_TIME_SRC_HAND_SET            0x60
316 #define NX_PTP_MASTER_TIME_SRC_OTHER               0x90
317 #define NX_PTP_MASTER_TIME_SRC_INTERNAL_OSCILLATOR 0xa0
318 
319 
320 /* PTP event callback */
321 
322 struct NX_PTP_CLIENT_STRUCT;
323 
324 typedef UINT (*NX_PTP_CLIENT_EVENT_CALLBACK)(struct NX_PTP_CLIENT_STRUCT *client_ptr, UINT event,
325                                              VOID *event_data, VOID *callback_data);
326 
327 #define NX_PTP_CLIENT_EVENT_MASTER                 0
328 #define NX_PTP_CLIENT_EVENT_SYNC                   1
329 #define NX_PTP_CLIENT_EVENT_TIMEOUT                2
330 
331 
332 /* PTP clock callback operations  */
333 
334 typedef UINT (*NX_PTP_CLIENT_CLOCK_CALLBACK)(struct NX_PTP_CLIENT_STRUCT *client_ptr, UINT operation,
335                                              NX_PTP_TIME *time_ptr, NX_PACKET *packet_ptr, VOID *callback_data);
336 
337 #define NX_PTP_CLIENT_CLOCK_INIT                   0                                     /* Initialization */
338 #define NX_PTP_CLIENT_CLOCK_SET                    1                                     /* Set the PTP clock */
339 #define NX_PTP_CLIENT_CLOCK_GET                    2                                     /* Get the PTP clock */
340 #define NX_PTP_CLIENT_CLOCK_ADJUST                 3                                     /* Adjust the PTP clock */
341 #define NX_PTP_CLIENT_CLOCK_PACKET_TS_EXTRACT      4                                     /* Extract timestamp from packet */
342 #define NX_PTP_CLIENT_CLOCK_PACKET_TS_PREPARE      5                                     /* Prepare timestamp for packet */
343 #define NX_PTP_CLIENT_CLOCK_SOFT_TIMER_UPDATE      6                                     /* Update timer for soft implementation */
344 #define NX_PTP_CLIENT_CLOCK_TS_EXTRACT_ETHERNET    NX_PTP_CLIENT_CLOCK_PACKET_TS_EXTRACT /* Record timestamp of received packet from Ethernet layer */
345 
346 
347 /* Master messages data */
348 typedef struct NX_PTP_CLIENT_MASTER_STRUCT
349 {
350     NXD_ADDRESS *nx_ptp_client_master_address;
351     UCHAR       *nx_ptp_client_master_port_identity;
352     UCHAR        nx_ptp_client_master_priority1;
353     UCHAR        nx_ptp_client_master_priority2;
354     UCHAR        nx_ptp_client_master_clock_class;
355     UCHAR        nx_ptp_client_master_clock_accuracy;
356     USHORT       nx_ptp_client_master_offset_scaled_log_variance;
357     USHORT       nx_ptp_client_master_steps_removed;
358     UCHAR       *nx_ptp_client_master_grandmaster_identity;
359     UCHAR        nx_ptp_client_master_time_source;
360     UCHAR        nx_ptp_client_master_reserved[3];
361 #ifdef NX_PTP_ENABLE_MASTER
362     USHORT       nx_ptp_client_master_sequenceId;
363     USHORT       nx_ptp_client_master_sync_sequenceId;
364     /* The announce interval timer */
365     INT          nx_ptp_client_master_announce_timer;
366 #endif
367 } NX_PTP_CLIENT_MASTER;
368 
369 /* Sync flags */
370 #define NX_PTP_CLIENT_SYNC_CALIBRATED     (1 << 0)
371 #define NX_PTP_CLIENT_SYNC_UTC_REASONABLE (1 << 1)
372 #define NX_PTP_CLIENT_SYNC_LEAP59         (1 << 2)
373 #define NX_PTP_CLIENT_SYNC_LEAP61         (1 << 3)
374 
375 /* Sync message data */
376 typedef struct NX_PTP_CLIENT_SYNC_STRUCT
377 {
378     USHORT nx_ptp_client_sync_flags;
379     SHORT  nx_ptp_client_sync_utc_offset;
380 } NX_PTP_CLIENT_SYNC;
381 
382 
383 /* Define the Type of messages */
384 
385 #define NX_PTP_CLIENT_ALL_EVENTS                 0xFFFFFFFF /* all events of PTP client */
386 #define NX_PTP_CLIENT_STOP_EVENT                 0x00000001 /* stop the PTP client */
387 #define NX_PTP_CLIENT_RX_EVENT                   0x00000002 /* received UDP packet */
388 #define NX_PTP_CLIENT_TIMER_EVENT                0x00000004 /* timer tick */
389 #define NX_PTP_CLIENT_SYNC_FOLLOW_EVENT          0x00000008 /* send Sync follow up packet */
390 #define NX_PTP_CLIENT_PDELAY_FOLLOW_EVENT        0x00000010 /* send pdelay response follow up packet */
391 
392 /* Define the size of the PTP client message queue */
393 
394 #define NX_PTP_CLIENT_MESSAGE_QUEUE_SIZE         16
395 
396 
397 /* Define the state of the PTP Client thread */
398 
399 #define NX_PTP_CLIENT_THREAD_IDLE                0
400 #define NX_PTP_CLIENT_THREAD_RUNNING             1
401 #define NX_PTP_CLIENT_THREAD_STOPPING            2
402 #define NX_PTP_CLIENT_THREAD_STOPPED             3
403 
404 
405 /* Define the state of the PTP Client clock */
406 
407 #define NX_PTP_CLIENT_STATE_LISTENING            0
408 #define NX_PTP_CLIENT_STATE_WAIT_SYNC            1
409 #define NX_PTP_CLIENT_STATE_WAIT_FOLLOW_UP       2
410 #define NX_PTP_CLIENT_STATE_SLAVE                3
411 #define NX_PTP_CLIENT_STATE_MASTER               4
412 
413 
414 /* Define the state of the delay measurement process */
415 
416 #define NX_PTP_CLIENT_DELAY_IDLE                 0
417 #define NX_PTP_CLIENT_DELAY_WAIT_REQ_TS          1
418 #define NX_PTP_CLIENT_DELAY_WAIT_RESP            2
419 
420 
421 /* Define the state of the peer delay initiator process */
422 
423 #define NX_PTP_CLIENT_PDELAY_IDLE                0
424 #define NX_PTP_CLIENT_PDELAY_WAIT_REQ_TS         1
425 #define NX_PTP_CLIENT_PDELAY_WAIT_RESP           2
426 #define NX_PTP_CLIENT_PDELAY_WAIT_RESP_FOLLOW_UP 3
427 
428 /* Define the state of the peer delay receiver process */
429 
430 #define NX_PTP_CLIENT_PDELAY_WAIT_SYNC           0
431 #define NX_PTP_CLIENT_PDELAY_WAIT_REQ            1
432 #define NX_PTP_CLIENT_PDELAY_SEND_RESP_ONESTEP   2
433 #define NX_PTP_CLIENT_PDELAY_SEND_RESP_TWOSTEP   3
434 #define NX_PTP_CLIENT_PDELAY_SEND_FOLLOW_UP      4
435 
436 /* Define PTP roles.  */
437 #define NX_PTP_CLIENT_ROLE_SLAVE_ONLY            0
438 #define NX_PTP_CLIENT_ROLE_MASTER_ONLY           1
439 #define NX_PTP_CLIENT_ROLE_SLAVE_AND_MASTER      2
440 
441 /* Define the structure of a PTP Client */
442 
443 typedef struct NX_PTP_CLIENT_STRUCT
444 {
445     /* PTP Client ID */
446     ULONG                        nx_ptp_client_id;
447 
448     /* Pointer to the Client IP instance.  */
449     NX_IP                       *nx_ptp_client_ip_ptr;
450 
451     /* Index to PTP network interface  */
452     UINT                         nx_ptp_client_interface_index;
453 
454     /* Pointer to the Client packet pool.  */
455     NX_PACKET_POOL              *nx_ptp_client_packet_pool_ptr;
456 
457     /* PTP Domain Number */
458     UCHAR                        nx_ptp_client_domain;
459 
460     /* PTP Transport Specific */
461     UCHAR                        nx_ptp_client_transport_specific;
462 
463     /* PTP Client Port and Identity */
464     UCHAR                        nx_ptp_client_port_identity[NX_PTP_CLOCK_PORT_IDENTITY_SIZE];
465 
466     /* PTP event handler callback */
467     NX_PTP_CLIENT_EVENT_CALLBACK nx_ptp_client_event_callback;
468     VOID                        *nx_ptp_client_event_callback_data;
469 
470     /* PTP clock callback */
471     NX_PTP_CLIENT_CLOCK_CALLBACK nx_ptp_client_clock_callback;
472     VOID                        *nx_ptp_client_clock_callback_data;
473 
474     /* The message queue */
475     TX_EVENT_FLAGS_GROUP         nx_ptp_client_events;
476 
477 #if NX_PTP_CLIENT_TRANSPORT_UDP
478     /* PTP General Messages UDP Socket */
479     NX_UDP_SOCKET                nx_ptp_client_general_socket;
480 
481     /* PTP Event Messages UDP Socket */
482     NX_UDP_SOCKET                nx_ptp_client_event_socket;
483 
484     /* Set if IPv4 multicast group has been joined */
485     UCHAR                        nx_ptp_client_ipv4_group_joined;
486 
487 #if defined(NX_ENABLE_IPV6_MULTICAST) && defined(FEATURE_NX_IPV6)
488     /* Set if IPv6 multicast group has been joined */
489     UCHAR                        nx_ptp_client_ipv6_group_joined;
490 
491     /* Reserved filed.  */
492     UCHAR                        nx_ptp_client_reserved1[2];
493 #else
494 
495     /* Reserved filed.  */
496     UCHAR                        nx_ptp_client_reserved1[3];
497 #endif
498 #endif /* NX_PTP_CLIENT_TRANSPORT_UDP */
499 
500 #if NX_PTP_CLIENT_TRANSPORT_ETHERNET
501     /* PTP Ethernet receive queue */
502     NX_PACKET                   *nx_ptp_client_received_packet_head;
503     NX_PACKET                   *nx_ptp_client_received_packet_tail;
504 
505     /* Link layer receive queue */
506     NX_LINK_RECEIVE_QUEUE        nx_ptp_client_link_queue;
507 #endif /* NX_PTP_CLIENT_TRANSPORT_ETHERNET */
508 
509     /* The software clock value */
510     NX_PTP_TIME                  nx_ptp_client_soft_clock;
511 
512     /* The state of the PTP client */
513     UCHAR                        nx_ptp_client_state;
514 
515     /* The state of the delay measurement */
516     UCHAR                        nx_ptp_client_delay_state;
517 
518     /* Define supported PTP roles: slave-only, master-only or slave-master. */
519     UCHAR                        nx_ptp_client_role;
520 
521     /* Reserved filed.  */
522     UCHAR                        nx_ptp_client_reserved2;
523 
524     /* The current UTC offset flags */
525     USHORT                       nx_ptp_client_sync_flags;
526 
527     /* The current UTC offset */
528     SHORT                        nx_ptp_client_utc_offset;
529 
530     /* The address of the master clock */
531     NXD_ADDRESS                  nx_ptp_client_master_addr;
532 
533     /* The identity of the master clock */
534     UCHAR                        nx_ptp_client_master_port_identity[NX_PTP_CLOCK_PORT_IDENTITY_SIZE];
535 
536     /* The previous sync master timestamp */
537     NX_PTP_TIME                  nx_ptp_client_prev_sync;
538 
539     /* The previous sync client timestamp */
540     NX_PTP_TIME                  nx_ptp_client_prev_sync_ts;
541 
542     /* The current sync master timestamp */
543     NX_PTP_TIME                  nx_ptp_client_sync;
544 
545     /* The current sync client timestamp */
546     NX_PTP_TIME                  nx_ptp_client_sync_ts;
547 
548     /* Delay between slave and master */
549     NX_PTP_TIME                  nx_ptp_client_delay;
550 
551     /* The id of the sync message */
552     USHORT                       nx_ptp_client_sync_id;
553 
554     /* The id of the last delay_req message */
555     USHORT                       nx_ptp_client_delay_req_id;
556 
557     /* The delay request interval timer */
558     INT                          nx_ptp_client_delay_req_timer;
559 
560     /* The Announce timeout */
561     INT                          nx_ptp_client_announce_timeout;
562 
563     /* The delay request flag */
564     UINT                         nx_ptp_client_delay_req_flag;
565 
566     /* The delay request client timestamp */
567     NX_PTP_TIME                  nx_ptp_client_delay_ts;
568 
569     /* The delay request packet pointer */
570     NX_PACKET                   *nx_ptp_client_delay_req_packet_ptr;
571 
572 #if defined(NX_PTP_ENABLE_MASTER) || defined(NX_PTP_ENABLE_REVERSE_SYNC)
573     /* The local Sync timestamp */
574     NX_PTP_TIME                 nx_ptp_client_sync_ts_local;
575 
576     /* The sync packet pointer */
577     NX_PACKET                   *nx_ptp_client_sync_packet_ptr;
578 
579     /* The sequence ID of Sync */
580     USHORT                       nx_ptp_client_sync_sequenceId;
581     USHORT                       nx_ptp_client_reserved3;
582 
583     /* The sync interval timer */
584     INT                         nx_ptp_client_sync_timer;
585 #endif /* defined(NX_PTP_ENABLE_MASTER) || defined(NX_PTP_ENABLE_REVERSE_SYNC) */
586 
587     /* The PTP client timer */
588     TX_TIMER                     nx_ptp_client_timer;
589 
590     /* The current state of the PTP Client thread */
591     UINT                         nx_ptp_client_thread_state;
592 
593     /* The PTP client processing thread */
594     TX_THREAD                    nx_ptp_client_thread;
595 
596 #if defined (NX_PTP_ENABLE_MASTER)
597 
598     NX_PTP_CLIENT_MASTER         ptp_master;
599 #endif /* NX_PTP_ENABLE_MASTER */
600 
601 #if defined(NX_ENABLE_GPTP)
602 
603     /* The peer delay request interval timer */
604     INT                          nx_ptp_client_pdelay_req_timer;
605 
606     /* The current pdelay_req nodeA timestamp (t1) */
607     NX_PTP_TIME                  nx_ptp_client_pdelay_req_ts;
608 
609     /* The current pdelay_resp from nodeB timestamp (t4) */
610     NX_PTP_TIME                  nx_ptp_client_pdelay_resp_ts;
611 
612     /* The request receipt timestamp (t2) */
613     NX_PTP_TIME                  nx_ptp_client_pdelay_req_receipt_ts;
614 
615     /* The response origin timestamp (t3) may not need */
616     NX_PTP_TIME                  nx_ptp_client_pdelay_resp_origin_ts;
617 
618     /* The request receipt timestamp (t2) used for pdelay_resp */
619     NX_PTP_TIME                  nx_ptp_client_pdelay_req_receipt;
620 
621     /* The response origin timestamp (t3) used for pdelay_resp_follow_up */
622     NX_PTP_TIME                  nx_ptp_client_pdelay_resp_origin;
623 
624     /* responder sequenceID from received pdelay_req */
625     USHORT                       nx_ptp_client_pdelay_responder_id;
626 
627     /* initiator sequence ID to send in pdelay_req */
628     USHORT                       nx_ptp_client_pdelay_req_id;
629 
630     /* The delay request packet pointer */
631     NX_PACKET                   *nx_ptp_client_pdelay_req_packet_ptr;
632 
633     /* The delay response packet pointer */
634     NX_PACKET                   *nx_ptp_client_pdelay_resp_packet_ptr;
635 
636     /* The state of the peer delay mechanism in initiator mode */
637     UCHAR                        nx_ptp_client_pdelay_initiator_state;
638 
639     /* The state of the peer delay mechanism in responder mode */
640     UCHAR                        nx_ptp_client_pdelay_responder_state;
641 
642     /* PTP Client Port and Identity */
643     UCHAR                        nx_ptp_client_request_port_identity[NX_PTP_CLOCK_PORT_IDENTITY_SIZE];
644 #endif /* NX_ENABLE_GPTP */
645 } NX_PTP_CLIENT;
646 
647 
648 #ifndef NX_PTP_SOURCE_CODE
649 
650 /* Define the system API mappings based on the error checking selected by the user.   */
651 
652 /* Determine if error checking is desired.  If so, map API functions
653    to the appropriate error checking front-ends.  Otherwise, map API
654    functions to the core functions that actually perform the work.
655    Note: error checking is enabled by default.  */
656 
657 
658 #ifdef NX_PTP_DISABLE_ERROR_CHECKING
659 
660 /* Services without error checking.  */
661 
662 #define nx_ptp_client_create                       _nx_ptp_client_create
663 #define nx_ptp_client_delete                       _nx_ptp_client_delete
664 #define nx_ptp_client_start                        _nx_ptp_client_start
665 #define nx_ptp_client_stop                         _nx_ptp_client_stop
666 #define nx_ptp_client_time_get                     _nx_ptp_client_time_get
667 #define nx_ptp_client_time_set                     _nx_ptp_client_time_set
668 #define nx_ptp_client_master_info_get              _nx_ptp_client_master_info_get
669 #define nx_ptp_client_sync_info_get                _nx_ptp_client_sync_info_get
670 #define nx_ptp_client_packet_timestamp_notify      _nx_ptp_client_packet_timestamp_notify
671 #define nx_ptp_client_soft_clock_callback          _nx_ptp_client_soft_clock_callback
672 #define nx_ptp_client_utility_time_diff            _nx_ptp_client_utility_time_diff
673 #define nx_ptp_client_utility_convert_time_to_date _nx_ptp_client_utility_convert_time_to_date
674 
675 #ifdef NX_PTP_ENABLE_MASTER
676 #define nx_ptp_client_master_enable                _nx_ptp_client_master_enable
677 #endif /* NX_PTP_ENABLE_MASTER */
678 
679 #else
680 
681 /* Services with error checking.  */
682 
683 #define nx_ptp_client_create                       _nxe_ptp_client_create
684 #define nx_ptp_client_delete                       _nxe_ptp_client_delete
685 #define nx_ptp_client_start                        _nxe_ptp_client_start
686 #define nx_ptp_client_stop                         _nxe_ptp_client_stop
687 #define nx_ptp_client_time_get                     _nxe_ptp_client_time_get
688 #define nx_ptp_client_time_set                     _nxe_ptp_client_time_set
689 #define nx_ptp_client_master_info_get              _nxe_ptp_client_master_info_get
690 #define nx_ptp_client_sync_info_get                _nxe_ptp_client_sync_info_get
691 #define nx_ptp_client_packet_timestamp_notify      _nx_ptp_client_packet_timestamp_notify
692 #define nx_ptp_client_soft_clock_callback          _nx_ptp_client_soft_clock_callback
693 #define nx_ptp_client_utility_time_diff            _nxe_ptp_client_utility_time_diff
694 #define nx_ptp_client_utility_convert_time_to_date _nxe_ptp_client_utility_convert_time_to_date
695 
696 #ifdef NX_PTP_ENABLE_MASTER
697 #define nx_ptp_client_master_enable                _nxe_ptp_client_master_enable
698 #endif /* NX_PTP_ENABLE_MASTER */
699 
700 #endif
701 
702 #endif
703 
704 
705 /* Define the function prototypes of the PTP Client API */
706 
707 UINT _nx_ptp_client_create(NX_PTP_CLIENT *client_ptr, NX_IP *ip_ptr, UINT interface_index,
708                            NX_PACKET_POOL *packet_pool_ptr, UINT thread_priority, UCHAR *thread_stack, UINT stack_size,
709                            NX_PTP_CLIENT_CLOCK_CALLBACK clock_callback, VOID *clock_callback_data);
710 UINT _nx_ptp_client_delete(NX_PTP_CLIENT *client_ptr);
711 UINT _nx_ptp_client_start(NX_PTP_CLIENT *client_ptr, UCHAR *client_port_identity_ptr, UINT client_port_identity_length,
712                           UINT domain, UINT transport_specific, NX_PTP_CLIENT_EVENT_CALLBACK event_callback,
713                           VOID *event_callback_data);
714 UINT _nx_ptp_client_stop(NX_PTP_CLIENT *client_ptr);
715 UINT _nx_ptp_client_time_get(NX_PTP_CLIENT *client_ptr, NX_PTP_TIME *time_ptr);
716 UINT _nx_ptp_client_time_set(NX_PTP_CLIENT *client_ptr, NX_PTP_TIME *time_ptr);
717 UINT _nx_ptp_client_master_info_get(NX_PTP_CLIENT_MASTER *master_ptr, NXD_ADDRESS *address, UCHAR **port_identity,
718                                     UINT *port_identity_length, UCHAR *priority1, UCHAR *priority2, UCHAR *clock_class,
719                                     UCHAR *clock_accuracy, USHORT *clock_variance, UCHAR **grandmaster_identity,
720                                     UINT *grandmaster_identity_length, USHORT *steps_removed, UCHAR *time_source);
721 UINT _nx_ptp_client_sync_info_get(NX_PTP_CLIENT_SYNC *sync_ptr, USHORT *flags, SHORT *utc_offset);
722 UINT _nx_ptp_client_utility_time_diff(NX_PTP_TIME *time1_ptr, NX_PTP_TIME *time2_ptr, NX_PTP_TIME *result_ptr);
723 UINT _nx_ptp_client_utility_time_sum(NX_PTP_TIME *time1_ptr, NX_PTP_TIME *time2_ptr, NX_PTP_TIME *result_ptr);
724 UINT _nx_ptp_client_utility_convert_time_to_date(NX_PTP_TIME *time_ptr, LONG offset, NX_PTP_DATE_TIME *date_time_ptr);
725 VOID _nx_ptp_client_packet_timestamp_notify(NX_PTP_CLIENT *client_ptr, NX_PACKET *packet_ptr, NX_PTP_TIME *timestamp_ptr);
726 UINT _nx_ptp_client_soft_clock_callback(NX_PTP_CLIENT *client_ptr, UINT operation,
727                                         NX_PTP_TIME *time_ptr, NX_PACKET *packet_ptr,
728                                         VOID *callback_data);
729 #ifdef NX_PTP_ENABLE_MASTER
730 UINT _nx_ptp_client_master_enable(NX_PTP_CLIENT *client_ptr, UCHAR role, UCHAR priority1, UCHAR priority2,
731                                   UCHAR clock_class, UCHAR clock_accuracy, USHORT clock_variance,
732                                   USHORT steps_removed, UCHAR time_source);
733 #endif /* NX_PTP_ENABLE_MASTER */
734 
735 UINT _nxe_ptp_client_create(NX_PTP_CLIENT *client_ptr, NX_IP *ip_ptr, UINT interface_index,
736                             NX_PACKET_POOL *packet_pool_ptr, UINT thread_priority, UCHAR *thread_stack, UINT stack_size,
737                             NX_PTP_CLIENT_CLOCK_CALLBACK clock_callback, VOID *clock_callback_data);
738 UINT _nxe_ptp_client_delete(NX_PTP_CLIENT *client_ptr);
739 UINT _nxe_ptp_client_start(NX_PTP_CLIENT *client_ptr, UCHAR *client_port_identity_ptr, UINT client_port_identity_length,
740                            UINT domain, UINT transport_specific, NX_PTP_CLIENT_EVENT_CALLBACK event_callback,
741                            VOID *event_callback_data);
742 UINT _nxe_ptp_client_stop(NX_PTP_CLIENT *client_ptr);
743 UINT _nxe_ptp_client_time_get(NX_PTP_CLIENT *client_ptr, NX_PTP_TIME *time_ptr);
744 UINT _nxe_ptp_client_time_set(NX_PTP_CLIENT *client_ptr, NX_PTP_TIME *time_ptr);
745 UINT _nxe_ptp_client_master_info_get(NX_PTP_CLIENT_MASTER *master_ptr, NXD_ADDRESS *address, UCHAR **port_identity,
746                                      UINT *port_identity_length, UCHAR *priority1, UCHAR *priority2, UCHAR *clock_class,
747                                      UCHAR *clock_accuracy, USHORT *clock_variance, UCHAR **grandmaster_identity,
748                                      UINT *grandmaster_identity_length, USHORT *steps_removed, UCHAR *time_source);
749 UINT _nxe_ptp_client_sync_info_get(NX_PTP_CLIENT_SYNC *sync_ptr, USHORT *flags, SHORT *utc_offset);
750 UINT _nxe_ptp_client_utility_time_diff(NX_PTP_TIME *time1_ptr, NX_PTP_TIME *time2_ptr, NX_PTP_TIME *result_ptr);
751 UINT _nxe_ptp_client_utility_time_sum(NX_PTP_TIME *time1_ptr, NX_PTP_TIME *time2_ptr, NX_PTP_TIME *result_ptr);
752 UINT _nxe_ptp_client_utility_convert_time_to_date(NX_PTP_TIME *time_ptr, LONG offset, NX_PTP_DATE_TIME *date_time_ptr);
753 #ifdef NX_PTP_ENABLE_MASTER
754 UINT _nxe_ptp_client_master_enable(NX_PTP_CLIENT *client_ptr, UCHAR role, UCHAR priority1, UCHAR priority2,
755                                    UCHAR clock_class, UCHAR clock_accuracy, USHORT clock_variance,
756                                    USHORT steps_removed, UCHAR time_source);
757 #endif /* NX_PTP_ENABLE_MASTER */
758 
759 
760 /* Define the function prototypes of the private utility functions */
761 
762 VOID _nx_ptp_client_utility_time_div_by_2(NX_PTP_TIME *time_ptr);
763 VOID _nx_ptp_client_utility_add64(LONG *a_hi, ULONG *a_lo, LONG b_hi, ULONG b_lo);
764 VOID _nx_ptp_client_utility_sub64(LONG *a_hi, ULONG *a_lo, LONG b_hi, ULONG b_lo);
765 VOID _nx_ptp_client_utility_inc64(LONG *a_hi, ULONG *a_lo);
766 VOID _nx_ptp_client_utility_dec64(LONG *a_hi, ULONG *a_lo);
767 VOID _nx_ptp_client_utility_neg64(LONG *a_hi, ULONG *a_lo);
768 
769 
770 /* Determine if a C++ compiler is being used.  If so, complete the standard
771    C conditional started above.  */
772 #ifdef __cplusplus
773 }
774 #endif
775 
776 #endif  /* NX_PTP_CLIENT_H */
777 
778