summaryrefslogtreecommitdiff
path: root/addons/ptp/nxd_ptp_client.h
blob: d8ef3c790820fa29cc2f2ae8dfc1601b99a56215 (plain)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX PTP Client Component                                             */
/**                                                                       */
/**   Precision Time Protocol (PTP)                                       */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

/**************************************************************************/
/*                                                                        */
/*  APPLICATION INTERFACE DEFINITION                       RELEASE        */
/*                                                                        */
/*    nxd_ptp_client.h                                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This file defines the NetX Precision Time Protocol (PTP)            */
/*    Client component, including all data types and external references. */
/*                                                                        */
/**************************************************************************/

#ifndef NXD_PTP_CLIENT_H
#define NXD_PTP_CLIENT_H


/* Include NetX and ThreadX definitions */

#include "nx_api.h"


/* Determine if a C++ compiler is being used.  If so, ensure that standard
   C is used to process the API information.  */

#ifdef   __cplusplus

/* Yes, C++ compiler is present.  Use standard C.  */
extern   "C" {

#endif


/* Define the PTP Client ID */

#define NX_PTP_CLIENT_ID 0x50545001UL

/* PTP Client configurable options.  */
/* Enable GPTP configuration as per IEEE 802.1AS. PTP messages are transmitted over Ethernet with peer delay request mechanism.
   If not enabled by default, PTP messages are transmitted over UDP with delay request mechanism. */
/*
#define NX_ENABLE_GPTP
*/

/* Enable PTP master feature. The role can be selected at runtime. Supported roles,
   * NX_PTP_CLIENT_ROLE_SLAVE_ONLY
   * NX_PTP_CLIENT_ROLE_MASTER_ONLY
   * NX_PTP_CLIENT_ROLE_SLAVE_AND_MASTER */
/*
#define NX_PTP_ENABLE_MASTER
*/

/* Disable PTP slave feature. Only master can be supported.  */
/*
#define NX_PTP_DISABLE_SLAVE
*/

/* Enable sending reverse sync when the role of PTP clock becomes slave. This will allow master clock
   to be aware of the offset from slave to master. Reverse SYNC may be ignored by master clock.  */
/*
#define NX_PTP_ENABLE_REVERSE_SYNC
*/

/* PTP Client packet type. By default, UDP is used as transport.  */
#ifndef NX_PTP_CLIENT_TRANSPORT_UDP
#ifdef NX_ENABLE_GPTP
#define NX_PTP_CLIENT_TRANSPORT_UDP      0
#else
#define NX_PTP_CLIENT_TRANSPORT_UDP      1
#endif
#endif /* NX_PTP_CLIENT_TRANSPORT_UDP  */

#ifndef NX_PTP_CLIENT_TRANSPORT_ETHERNET
#ifdef NX_ENABLE_GPTP
#define NX_PTP_CLIENT_TRANSPORT_ETHERNET 1
#else
#define NX_PTP_CLIENT_TRANSPORT_ETHERNET 0
#endif
#endif /* NX_PTP_CLIENT_TRANSPORT_ETHERNET  */

#if NX_PTP_CLIENT_TRANSPORT_UDP && NX_PTP_CLIENT_TRANSPORT_ETHERNET
#error "Only one of NX_PTP_CLIENT_TRANSPORT_UDP and NX_PTP_CLIENT_TRANSPORT_ETHERNET can be enabled"
#endif

#if !NX_PTP_CLIENT_TRANSPORT_UDP && !NX_PTP_CLIENT_TRANSPORT_ETHERNET
#error "At least one of NX_PTP_CLIENT_TRANSPORT_UDP or NX_PTP_CLIENT_TRANSPORT_ETHERNET must be enabled"
#endif

#if (NX_PTP_CLIENT_TRANSPORT_ETHERNET == 1) && !defined(NX_ENABLE_VLAN)
#error "NX_ENABLE_VLAN must be enabled for NX_PTP_CLIENT_TRANSPORT_ETHERNET"
#endif

#if NX_PTP_CLIENT_TRANSPORT_ETHERNET
#include "nx_link.h"
#endif /* NX_PTP_CLIENT_TRANSPORT_ETHERNET  */

/* Set the client thread time slice.  */

#ifndef NX_PTP_CLIENT_THREAD_TIME_SLICE
#define NX_PTP_CLIENT_THREAD_TIME_SLICE          TX_NO_TIME_SLICE
#endif



/* Define the PTP client internal timer frequency */

#ifndef NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND
#define NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND     100
#endif


/* Define the maximum number of missing Announce packets before timeout */

#ifndef NX_PTP_CLIENT_ANNOUNCE_RECEIPT_TIMEOUT
#define NX_PTP_CLIENT_ANNOUNCE_RECEIPT_TIMEOUT   3
#endif


/* Define the time interval between successive Announce packet, expressed as log 2.
   This value should be uniform throughout a domain. The default value is 1=2s. */

#ifndef NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL
#define NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL      1
#endif

/* Define the time interval between successive Sync packet, expressed as log 2.
   This value should be uniform throughout a domain. The default value is -3=125ms. */

#ifndef NX_PTP_CLIENT_LOG_SYNC_INTERVAL
#define NX_PTP_CLIENT_LOG_SYNC_INTERVAL          -3
#endif

/* Define the time interval between successive Pdelay_Req packet, expressed as log 2.
   This value should be uniform throughout a domain. The default value is 0=1s. */

#ifndef NX_PTP_CLIENT_LOG_PDELAY_REQ_INTERVAL
#define NX_PTP_CLIENT_LOG_PDELAY_REQ_INTERVAL    0
#endif

/* Define the interval for sending Delay request packets */

#ifndef NX_PTP_CLIENT_DELAY_REQ_INTERVAL
#define NX_PTP_CLIENT_DELAY_REQ_INTERVAL         (2 * NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND)
#endif

/* Set maximum queue depth for client socket.*/

#ifndef NX_PTP_CLIENT_MAX_QUEUE_DEPTH    
#define NX_PTP_CLIENT_MAX_QUEUE_DEPTH            5
#endif

/* Define the meanLinkDelayThresh as per 802.1AS-2020, 11.2.2.  The default value is 800ns for 100BASE-TX, 1000BASE-T. */
#ifndef NX_PTP_CLIENT_DELAY_THRESH
#define NX_PTP_CLIENT_DELAY_THRESH               800
#endif /* NX_PTP_CLIENT_DELAY_THRESH */


/* Define the interval for sending Sync packets */
#if (NX_PTP_CLIENT_LOG_SYNC_INTERVAL >= 0)
#define NX_PTP_CLIENT_SYNC_INTERVAL              ((1 << NX_PTP_CLIENT_LOG_SYNC_INTERVAL) * \
                                                NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND)
#else
#define NX_PTP_CLIENT_SYNC_INTERVAL              (NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND >> \
                                                (-(NX_PTP_CLIENT_LOG_SYNC_INTERVAL)))
#endif

/* Define the interval for sending Announce packets */
#if (NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL >= 0)
#define NX_PTP_CLIENT_ANNOUNCE_INTERVAL          ((1 << NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL) * \
                                                NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND)
#else
#define NX_PTP_CLIENT_ANNOUNCE_INTERVAL          (NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND >> \
                                                (-(NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL)))
#endif

/* Define the interval for sending Peer Delay request packets */

#if (NX_PTP_CLIENT_LOG_PDELAY_REQ_INTERVAL >= 0)
#define NX_PTP_CLIENT_PDELAY_REQ_INTERVAL        ((1 << NX_PTP_CLIENT_LOG_PDELAY_REQ_INTERVAL) * \
                                                 NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND)
#else
#define NX_PTP_CLIENT_PDELAY_REQ_INTERVAL        (NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND >> \
                                                 (-(NX_PTP_CLIENT_LOG_PDELAY_REQ_INTERVAL)))
#endif


/* Define the maximum size of a PTP message */

#define NX_PTP_CLIENT_PACKET_DATA_SIZE           64

/* Define Announce receipt timeout expire value */
#define NX_PTP_CLIENT_ANNOUNCE_EXPIRATION        (NX_PTP_CLIENT_ANNOUNCE_RECEIPT_TIMEOUT *     \
                                                (1 << NX_PTP_CLIENT_LOG_ANNOUNCE_INTERVAL) * \
                                                NX_PTP_CLIENT_TIMER_TICKS_PER_SECOND)


/* Define the default priority for local PTP clock running as master mode. */
#define NX_PTP_CLIENT_MASTER_PRIORITY            248

/* Define the default clock class for local PTP clock running as master mode. */
#define NX_PTP_CLIENT_MASTER_CLOCK_CLASS         248

/* Define the default accuracy for local PTP clock running as master mode.
   The default value 0x22 means the time is accurate to within 250ns.  */
#define NX_PTP_CLIENT_MASTER_ACCURACY            0x22

/* Define  the default clock variance for local PTP clock running as master mode. */
#define NX_PTP_CLIENT_MASTER_CLOCK_VARIANCE      17258

/* Define  the default clock steps removed for local PTP clock running as master mode. */
#define NX_PTP_CLIENT_MASTER_CLOCK_STEPS_REMOVED 0

/* Implementations based on IEEE Std 1588-2008 and any implementations based on this edition
   where the expanded profile isolation capabilities of option 16.5 are not required */
#define NX_PTP_TRANSPORT_SPECIFIC_NON_802        0

/* Implementations based on PTP Profiles developed by the IEEE 802.1 Working Group */
#define NX_PTP_TRANSPORT_SPECIFIC_802            1


/* Define a PTP Time */

typedef struct NX_PTP_TIME_STRUCT
{
    /* The MSB of the number of seconds */
    LONG  second_high;

    /* The LSB of the number of seconds */
    ULONG second_low;

    /* The number of nanoseconds */
    LONG  nanosecond;
} NX_PTP_TIME;


/* Define the PTP Date Time structure.  */

typedef struct NX_PTP_DATE_TIME_STRUCT
{
    UINT  year;
    UCHAR month;
    UCHAR day;
    UCHAR hour;
    UCHAR minute;
    UCHAR second;
    UCHAR weekday;
    UCHAR reserved[2];
    ULONG nanosecond;
} NX_PTP_DATE_TIME;


/* Internal PTP error processing codes.  */

#define NX_PTP_ERROR_CONSTANT                      0xD00
/* Client side errors.  */
#define NX_PTP_CLIENT_NOT_STARTED                  (NX_PTP_ERROR_CONSTANT | 0x01) /* PTP Client task is not running */
#define NX_PTP_CLIENT_ALREADY_STARTED              (NX_PTP_ERROR_CONSTANT | 0x02) /* PTP Client task is already running */
#define NX_PTP_PARAM_ERROR                         (NX_PTP_ERROR_CONSTANT | 0x03) /* Invalid non pointer parameter.  */
#define NX_PTP_CLIENT_INSUFFICIENT_PACKET_PAYLOAD  (NX_PTP_ERROR_CONSTANT | 0x04) /* Client not properly initialized to receive time data.  */
#define NX_PTP_CLIENT_CLOCK_CALLBACK_FAILURE       (NX_PTP_ERROR_CONSTANT | 0x05) /* PTP clock callback returns error.  */


/* PTP Protocol Definitions */

/* Define the size of the PTP Clock Identity field */
#define NX_PTP_CLOCK_IDENTITY_SIZE                 8

/* Define the size of the PTP Clock Port and Identity field */
#define NX_PTP_CLOCK_PORT_IDENTITY_SIZE            (NX_PTP_CLOCK_IDENTITY_SIZE + 2)

/* PTP Master Definitions */

#define NX_PTP_MASTER_CLOCK_ANNOUNCE_RATE          10 //(every 10 ptp ticks)

/* Master time sources */
#define NX_PTP_MASTER_TIME_SRC_ATOMIC_CLOCK        0x10
#define NX_PTP_MASTER_TIME_SRC_GPS                 0x20
#define NX_PTP_MASTER_TIME_SRC_TERRESTRIAL_RADIO   0x30
#define NX_PTP_MASTER_TIME_SRC_PTP                 0x40
#define NX_PTP_MASTER_TIME_SRC_NTP                 0x50
#define NX_PTP_MASTER_TIME_SRC_HAND_SET            0x60
#define NX_PTP_MASTER_TIME_SRC_OTHER               0x90
#define NX_PTP_MASTER_TIME_SRC_INTERNAL_OSCILLATOR 0xa0


/* PTP event callback */

struct NX_PTP_CLIENT_STRUCT;

typedef UINT (*NX_PTP_CLIENT_EVENT_CALLBACK)(struct NX_PTP_CLIENT_STRUCT *client_ptr, UINT event,
                                             VOID *event_data, VOID *callback_data);

#define NX_PTP_CLIENT_EVENT_MASTER                 0
#define NX_PTP_CLIENT_EVENT_SYNC                   1
#define NX_PTP_CLIENT_EVENT_TIMEOUT                2


/* PTP clock callback operations  */

typedef UINT (*NX_PTP_CLIENT_CLOCK_CALLBACK)(struct NX_PTP_CLIENT_STRUCT *client_ptr, UINT operation,
                                             NX_PTP_TIME *time_ptr, NX_PACKET *packet_ptr, VOID *callback_data);

#define NX_PTP_CLIENT_CLOCK_INIT                   0                                     /* Initialization */
#define NX_PTP_CLIENT_CLOCK_SET                    1                                     /* Set the PTP clock */
#define NX_PTP_CLIENT_CLOCK_GET                    2                                     /* Get the PTP clock */
#define NX_PTP_CLIENT_CLOCK_ADJUST                 3                                     /* Adjust the PTP clock */
#define NX_PTP_CLIENT_CLOCK_PACKET_TS_EXTRACT      4                                     /* Extract timestamp from packet */
#define NX_PTP_CLIENT_CLOCK_PACKET_TS_PREPARE      5                                     /* Prepare timestamp for packet */
#define NX_PTP_CLIENT_CLOCK_SOFT_TIMER_UPDATE      6                                     /* Update timer for soft implementation */
#define NX_PTP_CLIENT_CLOCK_TS_EXTRACT_ETHERNET    NX_PTP_CLIENT_CLOCK_PACKET_TS_EXTRACT /* Record timestamp of received packet from Ethernet layer */


/* Master messages data */
typedef struct NX_PTP_CLIENT_MASTER_STRUCT
{
    NXD_ADDRESS *nx_ptp_client_master_address;
    UCHAR       *nx_ptp_client_master_port_identity;
    UCHAR        nx_ptp_client_master_priority1;
    UCHAR        nx_ptp_client_master_priority2;
    UCHAR        nx_ptp_client_master_clock_class;
    UCHAR        nx_ptp_client_master_clock_accuracy;
    USHORT       nx_ptp_client_master_offset_scaled_log_variance;
    USHORT       nx_ptp_client_master_steps_removed;
    UCHAR       *nx_ptp_client_master_grandmaster_identity;
    UCHAR        nx_ptp_client_master_time_source;
    UCHAR        nx_ptp_client_master_reserved[3];
#ifdef NX_PTP_ENABLE_MASTER
    USHORT       nx_ptp_client_master_sequenceId;
    USHORT       nx_ptp_client_master_sync_sequenceId;
    /* The announce interval timer */
    INT          nx_ptp_client_master_announce_timer;
#endif
} NX_PTP_CLIENT_MASTER;

/* Sync flags */
#define NX_PTP_CLIENT_SYNC_CALIBRATED     (1 << 0)
#define NX_PTP_CLIENT_SYNC_UTC_REASONABLE (1 << 1)
#define NX_PTP_CLIENT_SYNC_LEAP59         (1 << 2)
#define NX_PTP_CLIENT_SYNC_LEAP61         (1 << 3)

/* Sync message data */
typedef struct NX_PTP_CLIENT_SYNC_STRUCT
{
    USHORT nx_ptp_client_sync_flags;
    SHORT  nx_ptp_client_sync_utc_offset;
} NX_PTP_CLIENT_SYNC;


/* Define the Type of messages */

#define NX_PTP_CLIENT_ALL_EVENTS                 0xFFFFFFFF /* all events of PTP client */
#define NX_PTP_CLIENT_STOP_EVENT                 0x00000001 /* stop the PTP client */
#define NX_PTP_CLIENT_RX_EVENT                   0x00000002 /* received UDP packet */
#define NX_PTP_CLIENT_TIMER_EVENT                0x00000004 /* timer tick */
#define NX_PTP_CLIENT_SYNC_FOLLOW_EVENT          0x00000008 /* send Sync follow up packet */
#define NX_PTP_CLIENT_PDELAY_FOLLOW_EVENT        0x00000010 /* send pdelay response follow up packet */

/* Define the size of the PTP client message queue */

#define NX_PTP_CLIENT_MESSAGE_QUEUE_SIZE         16


/* Define the state of the PTP Client thread */

#define NX_PTP_CLIENT_THREAD_IDLE                0
#define NX_PTP_CLIENT_THREAD_RUNNING             1
#define NX_PTP_CLIENT_THREAD_STOPPING            2
#define NX_PTP_CLIENT_THREAD_STOPPED             3


/* Define the state of the PTP Client clock */

#define NX_PTP_CLIENT_STATE_LISTENING            0
#define NX_PTP_CLIENT_STATE_WAIT_SYNC            1
#define NX_PTP_CLIENT_STATE_WAIT_FOLLOW_UP       2
#define NX_PTP_CLIENT_STATE_SLAVE                3
#define NX_PTP_CLIENT_STATE_MASTER               4


/* Define the state of the delay measurement process */

#define NX_PTP_CLIENT_DELAY_IDLE                 0
#define NX_PTP_CLIENT_DELAY_WAIT_REQ_TS          1
#define NX_PTP_CLIENT_DELAY_WAIT_RESP            2


/* Define the state of the peer delay initiator process */

#define NX_PTP_CLIENT_PDELAY_IDLE                0
#define NX_PTP_CLIENT_PDELAY_WAIT_REQ_TS         1
#define NX_PTP_CLIENT_PDELAY_WAIT_RESP           2
#define NX_PTP_CLIENT_PDELAY_WAIT_RESP_FOLLOW_UP 3

/* Define the state of the peer delay receiver process */

#define NX_PTP_CLIENT_PDELAY_WAIT_SYNC           0
#define NX_PTP_CLIENT_PDELAY_WAIT_REQ            1
#define NX_PTP_CLIENT_PDELAY_SEND_RESP_ONESTEP   2
#define NX_PTP_CLIENT_PDELAY_SEND_RESP_TWOSTEP   3
#define NX_PTP_CLIENT_PDELAY_SEND_FOLLOW_UP      4

/* Define PTP roles.  */
#define NX_PTP_CLIENT_ROLE_SLAVE_ONLY            0
#define NX_PTP_CLIENT_ROLE_MASTER_ONLY           1
#define NX_PTP_CLIENT_ROLE_SLAVE_AND_MASTER      2

/* Define the structure of a PTP Client */

typedef struct NX_PTP_CLIENT_STRUCT
{
    /* PTP Client ID */
    ULONG                        nx_ptp_client_id;

    /* Pointer to the Client IP instance.  */
    NX_IP                       *nx_ptp_client_ip_ptr;

    /* Index to PTP network interface  */
    UINT                         nx_ptp_client_interface_index;

    /* Pointer to the Client packet pool.  */
    NX_PACKET_POOL              *nx_ptp_client_packet_pool_ptr;

    /* PTP Domain Number */
    UCHAR                        nx_ptp_client_domain;

    /* PTP Transport Specific */
    UCHAR                        nx_ptp_client_transport_specific;

    /* PTP Client Port and Identity */
    UCHAR                        nx_ptp_client_port_identity[NX_PTP_CLOCK_PORT_IDENTITY_SIZE];

    /* PTP event handler callback */
    NX_PTP_CLIENT_EVENT_CALLBACK nx_ptp_client_event_callback;
    VOID                        *nx_ptp_client_event_callback_data;

    /* PTP clock callback */
    NX_PTP_CLIENT_CLOCK_CALLBACK nx_ptp_client_clock_callback;
    VOID                        *nx_ptp_client_clock_callback_data;

    /* The message queue */
    TX_EVENT_FLAGS_GROUP         nx_ptp_client_events;

#if NX_PTP_CLIENT_TRANSPORT_UDP
    /* PTP General Messages UDP Socket */
    NX_UDP_SOCKET                nx_ptp_client_general_socket;

    /* PTP Event Messages UDP Socket */
    NX_UDP_SOCKET                nx_ptp_client_event_socket;

    /* Set if IPv4 multicast group has been joined */
    UCHAR                        nx_ptp_client_ipv4_group_joined;

#if defined(NX_ENABLE_IPV6_MULTICAST) && defined(FEATURE_NX_IPV6)
    /* Set if IPv6 multicast group has been joined */
    UCHAR                        nx_ptp_client_ipv6_group_joined;

    /* Reserved filed.  */
    UCHAR                        nx_ptp_client_reserved1[2];
#else

    /* Reserved filed.  */
    UCHAR                        nx_ptp_client_reserved1[3];
#endif
#endif /* NX_PTP_CLIENT_TRANSPORT_UDP */

#if NX_PTP_CLIENT_TRANSPORT_ETHERNET
    /* PTP Ethernet receive queue */
    NX_PACKET                   *nx_ptp_client_received_packet_head;
    NX_PACKET                   *nx_ptp_client_received_packet_tail;

    /* Link layer receive queue */
    NX_LINK_RECEIVE_QUEUE        nx_ptp_client_link_queue;
#endif /* NX_PTP_CLIENT_TRANSPORT_ETHERNET */

    /* The software clock value */
    NX_PTP_TIME                  nx_ptp_client_soft_clock;

    /* The state of the PTP client */
    UCHAR                        nx_ptp_client_state;

    /* The state of the delay measurement */
    UCHAR                        nx_ptp_client_delay_state;

    /* Define supported PTP roles: slave-only, master-only or slave-master. */
    UCHAR                        nx_ptp_client_role;

    /* Reserved filed.  */
    UCHAR                        nx_ptp_client_reserved2;

    /* The current UTC offset flags */
    USHORT                       nx_ptp_client_sync_flags;

    /* The current UTC offset */
    SHORT                        nx_ptp_client_utc_offset;

    /* The address of the master clock */
    NXD_ADDRESS                  nx_ptp_client_master_addr;

    /* The identity of the master clock */
    UCHAR                        nx_ptp_client_master_port_identity[NX_PTP_CLOCK_PORT_IDENTITY_SIZE];

    /* The previous sync master timestamp */
    NX_PTP_TIME                  nx_ptp_client_prev_sync;

    /* The previous sync client timestamp */
    NX_PTP_TIME                  nx_ptp_client_prev_sync_ts;

    /* The current sync master timestamp */
    NX_PTP_TIME                  nx_ptp_client_sync;

    /* The current sync client timestamp */
    NX_PTP_TIME                  nx_ptp_client_sync_ts;

    /* Delay between slave and master */
    NX_PTP_TIME                  nx_ptp_client_delay;

    /* The id of the sync message */
    USHORT                       nx_ptp_client_sync_id;

    /* The id of the last delay_req message */
    USHORT                       nx_ptp_client_delay_req_id;

    /* The delay request interval timer */
    INT                          nx_ptp_client_delay_req_timer;

    /* The Announce timeout */
    INT                          nx_ptp_client_announce_timeout;

    /* The delay request flag */
    UINT                         nx_ptp_client_delay_req_flag;

    /* The delay request client timestamp */
    NX_PTP_TIME                  nx_ptp_client_delay_ts;

    /* The delay request packet pointer */
    NX_PACKET                   *nx_ptp_client_delay_req_packet_ptr;

#if defined(NX_PTP_ENABLE_MASTER) || defined(NX_PTP_ENABLE_REVERSE_SYNC)
    /* The local Sync timestamp */
    NX_PTP_TIME                 nx_ptp_client_sync_ts_local;

    /* The sync packet pointer */
    NX_PACKET                   *nx_ptp_client_sync_packet_ptr;

    /* The sequence ID of Sync */
    USHORT                       nx_ptp_client_sync_sequenceId;
    USHORT                       nx_ptp_client_reserved3;

    /* The sync interval timer */
    INT                         nx_ptp_client_sync_timer;
#endif /* defined(NX_PTP_ENABLE_MASTER) || defined(NX_PTP_ENABLE_REVERSE_SYNC) */

    /* The PTP client timer */
    TX_TIMER                     nx_ptp_client_timer;

    /* The current state of the PTP Client thread */
    UINT                         nx_ptp_client_thread_state;

    /* The PTP client processing thread */
    TX_THREAD                    nx_ptp_client_thread;

#if defined (NX_PTP_ENABLE_MASTER)

    NX_PTP_CLIENT_MASTER         ptp_master;
#endif /* NX_PTP_ENABLE_MASTER */

#if defined(NX_ENABLE_GPTP)

    /* The peer delay request interval timer */
    INT                          nx_ptp_client_pdelay_req_timer;

    /* The current pdelay_req nodeA timestamp (t1) */
    NX_PTP_TIME                  nx_ptp_client_pdelay_req_ts;

    /* The current pdelay_resp from nodeB timestamp (t4) */
    NX_PTP_TIME                  nx_ptp_client_pdelay_resp_ts;

    /* The request receipt timestamp (t2) */
    NX_PTP_TIME                  nx_ptp_client_pdelay_req_receipt_ts;

    /* The response origin timestamp (t3) may not need */
    NX_PTP_TIME                  nx_ptp_client_pdelay_resp_origin_ts;

    /* The request receipt timestamp (t2) used for pdelay_resp */
    NX_PTP_TIME                  nx_ptp_client_pdelay_req_receipt;

    /* The response origin timestamp (t3) used for pdelay_resp_follow_up */
    NX_PTP_TIME                  nx_ptp_client_pdelay_resp_origin;

    /* responder sequenceID from received pdelay_req */
    USHORT                       nx_ptp_client_pdelay_responder_id;

    /* initiator sequence ID to send in pdelay_req */
    USHORT                       nx_ptp_client_pdelay_req_id;

    /* The delay request packet pointer */
    NX_PACKET                   *nx_ptp_client_pdelay_req_packet_ptr;
    
    /* The delay response packet pointer */
    NX_PACKET                   *nx_ptp_client_pdelay_resp_packet_ptr;

    /* The state of the peer delay mechanism in initiator mode */
    UCHAR                        nx_ptp_client_pdelay_initiator_state;

    /* The state of the peer delay mechanism in responder mode */
    UCHAR                        nx_ptp_client_pdelay_responder_state;

    /* PTP Client Port and Identity */
    UCHAR                        nx_ptp_client_request_port_identity[NX_PTP_CLOCK_PORT_IDENTITY_SIZE];
#endif /* NX_ENABLE_GPTP */
} NX_PTP_CLIENT;


#ifndef NX_PTP_SOURCE_CODE

/* Define the system API mappings based on the error checking selected by the user.   */

/* Determine if error checking is desired.  If so, map API functions
   to the appropriate error checking front-ends.  Otherwise, map API
   functions to the core functions that actually perform the work.
   Note: error checking is enabled by default.  */


#ifdef NX_PTP_DISABLE_ERROR_CHECKING

/* Services without error checking.  */

#define nx_ptp_client_create                       _nx_ptp_client_create
#define nx_ptp_client_delete                       _nx_ptp_client_delete
#define nx_ptp_client_start                        _nx_ptp_client_start
#define nx_ptp_client_stop                         _nx_ptp_client_stop
#define nx_ptp_client_time_get                     _nx_ptp_client_time_get
#define nx_ptp_client_time_set                     _nx_ptp_client_time_set
#define nx_ptp_client_master_info_get              _nx_ptp_client_master_info_get
#define nx_ptp_client_sync_info_get                _nx_ptp_client_sync_info_get
#define nx_ptp_client_packet_timestamp_notify      _nx_ptp_client_packet_timestamp_notify
#define nx_ptp_client_soft_clock_callback          _nx_ptp_client_soft_clock_callback
#define nx_ptp_client_utility_time_diff            _nx_ptp_client_utility_time_diff
#define nx_ptp_client_utility_convert_time_to_date _nx_ptp_client_utility_convert_time_to_date

#ifdef NX_PTP_ENABLE_MASTER
#define nx_ptp_client_master_enable                _nx_ptp_client_master_enable
#endif /* NX_PTP_ENABLE_MASTER */

#else

/* Services with error checking.  */

#define nx_ptp_client_create                       _nxe_ptp_client_create
#define nx_ptp_client_delete                       _nxe_ptp_client_delete
#define nx_ptp_client_start                        _nxe_ptp_client_start
#define nx_ptp_client_stop                         _nxe_ptp_client_stop
#define nx_ptp_client_time_get                     _nxe_ptp_client_time_get
#define nx_ptp_client_time_set                     _nxe_ptp_client_time_set
#define nx_ptp_client_master_info_get              _nxe_ptp_client_master_info_get
#define nx_ptp_client_sync_info_get                _nxe_ptp_client_sync_info_get
#define nx_ptp_client_packet_timestamp_notify      _nx_ptp_client_packet_timestamp_notify
#define nx_ptp_client_soft_clock_callback          _nx_ptp_client_soft_clock_callback
#define nx_ptp_client_utility_time_diff            _nxe_ptp_client_utility_time_diff
#define nx_ptp_client_utility_convert_time_to_date _nxe_ptp_client_utility_convert_time_to_date

#ifdef NX_PTP_ENABLE_MASTER
#define nx_ptp_client_master_enable                _nxe_ptp_client_master_enable
#endif /* NX_PTP_ENABLE_MASTER */

#endif

#endif


/* Define the function prototypes of the PTP Client API */

UINT _nx_ptp_client_create(NX_PTP_CLIENT *client_ptr, NX_IP *ip_ptr, UINT interface_index,
                           NX_PACKET_POOL *packet_pool_ptr, UINT thread_priority, UCHAR *thread_stack, UINT stack_size,
                           NX_PTP_CLIENT_CLOCK_CALLBACK clock_callback, VOID *clock_callback_data);
UINT _nx_ptp_client_delete(NX_PTP_CLIENT *client_ptr);
UINT _nx_ptp_client_start(NX_PTP_CLIENT *client_ptr, UCHAR *client_port_identity_ptr, UINT client_port_identity_length,
                          UINT domain, UINT transport_specific, NX_PTP_CLIENT_EVENT_CALLBACK event_callback,
                          VOID *event_callback_data);
UINT _nx_ptp_client_stop(NX_PTP_CLIENT *client_ptr);
UINT _nx_ptp_client_time_get(NX_PTP_CLIENT *client_ptr, NX_PTP_TIME *time_ptr);
UINT _nx_ptp_client_time_set(NX_PTP_CLIENT *client_ptr, NX_PTP_TIME *time_ptr);
UINT _nx_ptp_client_master_info_get(NX_PTP_CLIENT_MASTER *master_ptr, NXD_ADDRESS *address, UCHAR **port_identity,
                                    UINT *port_identity_length, UCHAR *priority1, UCHAR *priority2, UCHAR *clock_class,
                                    UCHAR *clock_accuracy, USHORT *clock_variance, UCHAR **grandmaster_identity,
                                    UINT *grandmaster_identity_length, USHORT *steps_removed, UCHAR *time_source);
UINT _nx_ptp_client_sync_info_get(NX_PTP_CLIENT_SYNC *sync_ptr, USHORT *flags, SHORT *utc_offset);
UINT _nx_ptp_client_utility_time_diff(NX_PTP_TIME *time1_ptr, NX_PTP_TIME *time2_ptr, NX_PTP_TIME *result_ptr);
UINT _nx_ptp_client_utility_time_sum(NX_PTP_TIME *time1_ptr, NX_PTP_TIME *time2_ptr, NX_PTP_TIME *result_ptr);
UINT _nx_ptp_client_utility_convert_time_to_date(NX_PTP_TIME *time_ptr, LONG offset, NX_PTP_DATE_TIME *date_time_ptr);
VOID _nx_ptp_client_packet_timestamp_notify(NX_PTP_CLIENT *client_ptr, NX_PACKET *packet_ptr, NX_PTP_TIME *timestamp_ptr);
UINT _nx_ptp_client_soft_clock_callback(NX_PTP_CLIENT *client_ptr, UINT operation,
                                        NX_PTP_TIME *time_ptr, NX_PACKET *packet_ptr,
                                        VOID *callback_data);
#ifdef NX_PTP_ENABLE_MASTER
UINT _nx_ptp_client_master_enable(NX_PTP_CLIENT *client_ptr, UCHAR role, UCHAR priority1, UCHAR priority2,
                                  UCHAR clock_class, UCHAR clock_accuracy, USHORT clock_variance,
                                  USHORT steps_removed, UCHAR time_source);
#endif /* NX_PTP_ENABLE_MASTER */

UINT _nxe_ptp_client_create(NX_PTP_CLIENT *client_ptr, NX_IP *ip_ptr, UINT interface_index,
                            NX_PACKET_POOL *packet_pool_ptr, UINT thread_priority, UCHAR *thread_stack, UINT stack_size,
                            NX_PTP_CLIENT_CLOCK_CALLBACK clock_callback, VOID *clock_callback_data);
UINT _nxe_ptp_client_delete(NX_PTP_CLIENT *client_ptr);
UINT _nxe_ptp_client_start(NX_PTP_CLIENT *client_ptr, UCHAR *client_port_identity_ptr, UINT client_port_identity_length,
                           UINT domain, UINT transport_specific, NX_PTP_CLIENT_EVENT_CALLBACK event_callback,
                           VOID *event_callback_data);
UINT _nxe_ptp_client_stop(NX_PTP_CLIENT *client_ptr);
UINT _nxe_ptp_client_time_get(NX_PTP_CLIENT *client_ptr, NX_PTP_TIME *time_ptr);
UINT _nxe_ptp_client_time_set(NX_PTP_CLIENT *client_ptr, NX_PTP_TIME *time_ptr);
UINT _nxe_ptp_client_master_info_get(NX_PTP_CLIENT_MASTER *master_ptr, NXD_ADDRESS *address, UCHAR **port_identity,
                                     UINT *port_identity_length, UCHAR *priority1, UCHAR *priority2, UCHAR *clock_class,
                                     UCHAR *clock_accuracy, USHORT *clock_variance, UCHAR **grandmaster_identity,
                                     UINT *grandmaster_identity_length, USHORT *steps_removed, UCHAR *time_source);
UINT _nxe_ptp_client_sync_info_get(NX_PTP_CLIENT_SYNC *sync_ptr, USHORT *flags, SHORT *utc_offset);
UINT _nxe_ptp_client_utility_time_diff(NX_PTP_TIME *time1_ptr, NX_PTP_TIME *time2_ptr, NX_PTP_TIME *result_ptr);
UINT _nxe_ptp_client_utility_time_sum(NX_PTP_TIME *time1_ptr, NX_PTP_TIME *time2_ptr, NX_PTP_TIME *result_ptr);
UINT _nxe_ptp_client_utility_convert_time_to_date(NX_PTP_TIME *time_ptr, LONG offset, NX_PTP_DATE_TIME *date_time_ptr);
#ifdef NX_PTP_ENABLE_MASTER
UINT _nxe_ptp_client_master_enable(NX_PTP_CLIENT *client_ptr, UCHAR role, UCHAR priority1, UCHAR priority2,
                                   UCHAR clock_class, UCHAR clock_accuracy, USHORT clock_variance,
                                   USHORT steps_removed, UCHAR time_source);
#endif /* NX_PTP_ENABLE_MASTER */


/* Define the function prototypes of the private utility functions */

VOID _nx_ptp_client_utility_time_div_by_2(NX_PTP_TIME *time_ptr);
VOID _nx_ptp_client_utility_add64(LONG *a_hi, ULONG *a_lo, LONG b_hi, ULONG b_lo);
VOID _nx_ptp_client_utility_sub64(LONG *a_hi, ULONG *a_lo, LONG b_hi, ULONG b_lo);
VOID _nx_ptp_client_utility_inc64(LONG *a_hi, ULONG *a_lo);
VOID _nx_ptp_client_utility_dec64(LONG *a_hi, ULONG *a_lo);
VOID _nx_ptp_client_utility_neg64(LONG *a_hi, ULONG *a_lo);


/* Determine if a C++ compiler is being used.  If so, complete the standard
   C conditional started above.  */
#ifdef __cplusplus
}
#endif

#endif  /* NX_PTP_CLIENT_H */