@@ -55,7 +55,7 @@ struct ptp_clock {
55
55
} timestamp ; /* latest timestamps in nanoseconds */
56
56
};
57
57
58
- static struct ptp_clock clock = { 0 };
58
+ __maybe_unused static struct ptp_clock ptp_clk = { 0 };
59
59
char str_clock_id [] = "FF:FF:FF:FF:FF:FF:FF:FF" ;
60
60
61
61
static int clock_generate_id (ptp_clk_id * clock_id , struct net_if * iface )
@@ -146,7 +146,7 @@ static void clock_forward_management_msg(struct ptp_port *port, struct ptp_msg *
146
146
length = msg -> header .msg_length ;
147
147
msg -> management .boundary_hops -- ;
148
148
149
- SYS_SLIST_FOR_EACH_CONTAINER (& clock .ports_list , iter , node ) {
149
+ SYS_SLIST_FOR_EACH_CONTAINER (& ptp_clk .ports_list , iter , node ) {
150
150
if (clock_forward_msg (port , iter , msg , & net_byte_ord )) {
151
151
LOG_ERR ("Failed to forward message to %d Port" ,
152
152
iter -> port_ds .id .port_number );
@@ -168,11 +168,11 @@ static int clock_management_set(struct ptp_port *port,
168
168
169
169
switch (tlv -> id ) {
170
170
case PTP_MGMT_PRIORITY1 :
171
- clock .default_ds .priority1 = * tlv -> data ;
171
+ ptp_clk .default_ds .priority1 = * tlv -> data ;
172
172
send_resp = true;
173
173
break ;
174
174
case PTP_MGMT_PRIORITY2 :
175
- clock .default_ds .priority2 = * tlv -> data ;
175
+ ptp_clk .default_ds .priority2 = * tlv -> data ;
176
176
send_resp = true;
177
177
break ;
178
178
default :
@@ -184,71 +184,71 @@ static int clock_management_set(struct ptp_port *port,
184
184
185
185
static void clock_update_grandmaster (void )
186
186
{
187
- memset (& clock .current_ds , 0 , sizeof (struct ptp_current_ds ));
187
+ memset (& ptp_clk .current_ds , 0 , sizeof (struct ptp_current_ds ));
188
188
189
- memcpy (& clock .parent_ds .port_id .clk_id ,
190
- & clock .default_ds .clk_id ,
189
+ memcpy (& ptp_clk .parent_ds .port_id .clk_id ,
190
+ & ptp_clk .default_ds .clk_id ,
191
191
sizeof (ptp_clk_id ));
192
- memcpy (& clock .parent_ds .gm_id ,
193
- & clock .default_ds .clk_id ,
192
+ memcpy (& ptp_clk .parent_ds .gm_id ,
193
+ & ptp_clk .default_ds .clk_id ,
194
194
sizeof (ptp_clk_id ));
195
- clock .parent_ds .port_id .port_number = 0 ;
196
- clock .parent_ds .gm_clk_quality = clock .default_ds .clk_quality ;
197
- clock .parent_ds .gm_priority1 = clock .default_ds .priority1 ;
198
- clock .parent_ds .gm_priority2 = clock .default_ds .priority2 ;
199
-
200
- clock .time_prop_ds .current_utc_offset = 37 ; /* IEEE 1588-2019 9.4 */
201
- clock .time_prop_ds .time_src = clock .time_src ;
202
- clock .time_prop_ds .flags = 0 ;
195
+ ptp_clk .parent_ds .port_id .port_number = 0 ;
196
+ ptp_clk .parent_ds .gm_clk_quality = ptp_clk .default_ds .clk_quality ;
197
+ ptp_clk .parent_ds .gm_priority1 = ptp_clk .default_ds .priority1 ;
198
+ ptp_clk .parent_ds .gm_priority2 = ptp_clk .default_ds .priority2 ;
199
+
200
+ ptp_clk .time_prop_ds .current_utc_offset = 37 ; /* IEEE 1588-2019 9.4 */
201
+ ptp_clk .time_prop_ds .time_src = ptp_clk .time_src ;
202
+ ptp_clk .time_prop_ds .flags = 0 ;
203
203
}
204
204
205
205
static void clock_update_time_receiver (void )
206
206
{
207
- struct ptp_msg * best_msg = (struct ptp_msg * )k_fifo_peek_tail (& clock .best -> messages );
207
+ struct ptp_msg * best_msg = (struct ptp_msg * )k_fifo_peek_tail (& ptp_clk .best -> messages );
208
208
209
- clock .current_ds .steps_rm = 1 + clock .best -> dataset .steps_rm ;
209
+ ptp_clk .current_ds .steps_rm = 1 + ptp_clk .best -> dataset .steps_rm ;
210
210
211
- memcpy (& clock .parent_ds .gm_id ,
211
+ memcpy (& ptp_clk .parent_ds .gm_id ,
212
212
& best_msg -> announce .gm_id ,
213
213
sizeof (best_msg -> announce .gm_id ));
214
- memcpy (& clock .parent_ds .port_id ,
215
- & clock .best -> dataset .sender ,
216
- sizeof (clock .best -> dataset .sender ));
217
- clock .parent_ds .gm_clk_quality = best_msg -> announce .gm_clk_quality ;
218
- clock .parent_ds .gm_priority1 = best_msg -> announce .gm_priority1 ;
219
- clock .parent_ds .gm_priority2 = best_msg -> announce .gm_priority2 ;
220
-
221
- clock .time_prop_ds .current_utc_offset = best_msg -> announce .current_utc_offset ;
222
- clock .time_prop_ds .flags = best_msg -> header .flags [1 ];
214
+ memcpy (& ptp_clk .parent_ds .port_id ,
215
+ & ptp_clk .best -> dataset .sender ,
216
+ sizeof (ptp_clk .best -> dataset .sender ));
217
+ ptp_clk .parent_ds .gm_clk_quality = best_msg -> announce .gm_clk_quality ;
218
+ ptp_clk .parent_ds .gm_priority1 = best_msg -> announce .gm_priority1 ;
219
+ ptp_clk .parent_ds .gm_priority2 = best_msg -> announce .gm_priority2 ;
220
+
221
+ ptp_clk .time_prop_ds .current_utc_offset = best_msg -> announce .current_utc_offset ;
222
+ ptp_clk .time_prop_ds .flags = best_msg -> header .flags [1 ];
223
223
}
224
224
225
225
static void clock_check_pollfd (void )
226
226
{
227
227
struct ptp_port * port ;
228
- struct zsock_pollfd * fd = & clock .pollfd [1 ];
228
+ struct zsock_pollfd * fd = & ptp_clk .pollfd [1 ];
229
229
230
- if (clock .pollfd_valid ) {
230
+ if (ptp_clk .pollfd_valid ) {
231
231
return ;
232
232
}
233
233
234
- SYS_SLIST_FOR_EACH_CONTAINER (& clock .ports_list , port , node ) {
234
+ SYS_SLIST_FOR_EACH_CONTAINER (& ptp_clk .ports_list , port , node ) {
235
235
for (int i = 0 ; i < PTP_SOCKET_CNT ; i ++ ) {
236
236
fd -> fd = port -> socket [i ];
237
237
fd -> events = ZSOCK_POLLIN | ZSOCK_POLLPRI ;
238
238
fd ++ ;
239
239
}
240
240
}
241
241
242
- clock .pollfd_valid = true;
242
+ ptp_clk .pollfd_valid = true;
243
243
}
244
244
245
245
const struct ptp_clock * ptp_clock_init (void )
246
246
{
247
- struct ptp_default_ds * dds = & clock .default_ds ;
248
- struct ptp_parent_ds * pds = & clock .parent_ds ;
247
+ struct ptp_default_ds * dds = & ptp_clk .default_ds ;
248
+ struct ptp_parent_ds * pds = & ptp_clk .parent_ds ;
249
249
struct net_if * iface = net_if_get_first_by_type (& NET_L2_GET_NAME (ETHERNET ));
250
250
251
- clock .time_src = (enum ptp_time_src )PTP_TIME_SRC_INTERNAL_OSC ;
251
+ ptp_clk .time_src = (enum ptp_time_src )PTP_TIME_SRC_INTERNAL_OSC ;
252
252
253
253
/* Initialize Default Dataset. */
254
254
int ret = clock_generate_id (& dds -> clk_id , iface );
@@ -279,33 +279,33 @@ const struct ptp_clock *ptp_clock_init(void)
279
279
/* Parent statistics haven't been measured - IEEE 1588-2019 7.6.4.2 */
280
280
pds -> stats = false;
281
281
282
- clock .phc = net_eth_get_ptp_clock (iface );
283
- if (!clock .phc ) {
282
+ ptp_clk .phc = net_eth_get_ptp_clock (iface );
283
+ if (!ptp_clk .phc ) {
284
284
LOG_ERR ("Couldn't get PTP HW Clock for the interface." );
285
285
return NULL ;
286
286
}
287
287
288
- clock .pollfd [0 ].fd = eventfd (0 , EFD_NONBLOCK );
289
- clock .pollfd [0 ].events = ZSOCK_POLLIN ;
288
+ ptp_clk .pollfd [0 ].fd = eventfd (0 , EFD_NONBLOCK );
289
+ ptp_clk .pollfd [0 ].events = ZSOCK_POLLIN ;
290
290
291
- sys_slist_init (& clock .ports_list );
291
+ sys_slist_init (& ptp_clk .ports_list );
292
292
LOG_DBG ("PTP Clock %s initialized" , clock_id_str (& dds -> clk_id ));
293
- return & clock ;
293
+ return & ptp_clk ;
294
294
}
295
295
296
296
struct zsock_pollfd * ptp_clock_poll_sockets (void )
297
297
{
298
298
int ret ;
299
299
300
300
clock_check_pollfd ();
301
- ret = zsock_poll (clock .pollfd , PTP_SOCKET_CNT * clock .default_ds .n_ports + 1 , -1 );
302
- if (ret > 0 && clock .pollfd [0 ].revents ) {
301
+ ret = zsock_poll (ptp_clk .pollfd , PTP_SOCKET_CNT * ptp_clk .default_ds .n_ports + 1 , -1 );
302
+ if (ret > 0 && ptp_clk .pollfd [0 ].revents ) {
303
303
eventfd_t value ;
304
304
305
- eventfd_read (clock .pollfd [0 ].fd , & value );
305
+ eventfd_read (ptp_clk .pollfd [0 ].fd , & value );
306
306
}
307
307
308
- return & clock .pollfd [1 ];
308
+ return & ptp_clk .pollfd [1 ];
309
309
}
310
310
311
311
void ptp_clock_handle_state_decision_evt (void )
@@ -314,11 +314,11 @@ void ptp_clock_handle_state_decision_evt(void)
314
314
struct ptp_port * port ;
315
315
bool tt_changed = false;
316
316
317
- if (!clock .state_decision_event ) {
317
+ if (!ptp_clk .state_decision_event ) {
318
318
return ;
319
319
}
320
320
321
- SYS_SLIST_FOR_EACH_CONTAINER (& clock .ports_list , port , node ) {
321
+ SYS_SLIST_FOR_EACH_CONTAINER (& ptp_clk .ports_list , port , node ) {
322
322
foreign = ptp_port_best_foreign (port );
323
323
if (!foreign ) {
324
324
continue ;
@@ -328,9 +328,9 @@ void ptp_clock_handle_state_decision_evt(void)
328
328
}
329
329
}
330
330
331
- clock .best = best ;
331
+ ptp_clk .best = best ;
332
332
333
- SYS_SLIST_FOR_EACH_CONTAINER (& clock .ports_list , port , node ) {
333
+ SYS_SLIST_FOR_EACH_CONTAINER (& ptp_clk .ports_list , port , node ) {
334
334
enum ptp_port_state state ;
335
335
enum ptp_port_event event ;
336
336
@@ -362,7 +362,7 @@ void ptp_clock_handle_state_decision_evt(void)
362
362
ptp_port_event_handle (port , event , tt_changed );
363
363
}
364
364
365
- clock .state_decision_event = false;
365
+ ptp_clk .state_decision_event = false;
366
366
}
367
367
368
368
int ptp_clock_management_msg_process (struct ptp_port * port , struct ptp_msg * msg )
@@ -494,7 +494,7 @@ int ptp_clock_management_msg_process(struct ptp_port *port, struct ptp_msg *msg)
494
494
if (target_port -> port_number == port -> port_ds .id .port_number ) {
495
495
ptp_port_management_msg_process (port , port , msg , mgmt );
496
496
} else if (target_port -> port_number == UINT16_MAX ) {
497
- SYS_SLIST_FOR_EACH_CONTAINER (& clock .ports_list , iter , node ) {
497
+ SYS_SLIST_FOR_EACH_CONTAINER (& ptp_clk .ports_list , iter , node ) {
498
498
if (ptp_port_management_msg_process (iter , port , msg , mgmt )) {
499
499
break ;
500
500
}
@@ -509,108 +509,108 @@ int ptp_clock_management_msg_process(struct ptp_port *port, struct ptp_msg *msg)
509
509
void ptp_clock_synchronize (uint64_t ingress , uint64_t egress )
510
510
{
511
511
int64_t offset ;
512
- uint64_t delay = clock .current_ds .mean_delay >> 16 ;
512
+ uint64_t delay = ptp_clk .current_ds .mean_delay >> 16 ;
513
513
514
- clock .timestamp .t1 = egress ;
515
- clock .timestamp .t2 = ingress ;
514
+ ptp_clk .timestamp .t1 = egress ;
515
+ ptp_clk .timestamp .t2 = ingress ;
516
516
517
- if (!clock .current_ds .mean_delay ) {
517
+ if (!ptp_clk .current_ds .mean_delay ) {
518
518
return ;
519
519
}
520
520
521
- offset = clock .timestamp .t2 - clock .timestamp .t1 - delay ;
521
+ offset = ptp_clk .timestamp .t2 - ptp_clk .timestamp .t1 - delay ;
522
522
523
- /* If diff is too big, clock needs to be set first. */
523
+ /* If diff is too big, ptp_clk needs to be set first. */
524
524
if (offset > NSEC_PER_SEC || offset < - NSEC_PER_SEC ) {
525
525
struct net_ptp_time current ;
526
526
527
527
LOG_WRN ("Clock offset exceeds 1 second." );
528
528
529
- ptp_clock_get (clock .phc , & current );
529
+ ptp_clock_get (ptp_clk .phc , & current );
530
530
531
531
current .second -= (uint64_t )(offset / NSEC_PER_SEC );
532
532
current .nanosecond -= (uint32_t )(offset % NSEC_PER_SEC );
533
533
534
- ptp_clock_set (clock .phc , & current );
534
+ ptp_clock_set (ptp_clk .phc , & current );
535
535
return ;
536
536
}
537
537
538
538
LOG_DBG ("Offset %lldns" , offset );
539
- clock .current_ds .offset_from_tt = clock_ns_to_timeinterval (offset );
539
+ ptp_clk .current_ds .offset_from_tt = clock_ns_to_timeinterval (offset );
540
540
541
- ptp_clock_adjust (clock .phc , offset );
541
+ ptp_clock_adjust (ptp_clk .phc , offset );
542
542
}
543
543
544
544
void ptp_clock_delay (uint64_t egress , uint64_t ingress )
545
545
{
546
546
int64_t delay ;
547
547
548
- clock .timestamp .t3 = egress ;
549
- clock .timestamp .t4 = ingress ;
548
+ ptp_clk .timestamp .t3 = egress ;
549
+ ptp_clk .timestamp .t4 = ingress ;
550
550
551
- delay = ((clock .timestamp .t2 - clock .timestamp .t3 ) +
552
- (clock .timestamp .t4 - clock .timestamp .t1 )) / 2 ;
551
+ delay = ((ptp_clk .timestamp .t2 - ptp_clk .timestamp .t3 ) +
552
+ (ptp_clk .timestamp .t4 - ptp_clk .timestamp .t1 )) / 2 ;
553
553
554
554
LOG_DBG ("Delay %lldns" , delay );
555
- clock .current_ds .mean_delay = clock_ns_to_timeinterval (delay );
555
+ ptp_clk .current_ds .mean_delay = clock_ns_to_timeinterval (delay );
556
556
}
557
557
558
558
sys_slist_t * ptp_clock_ports_list (void )
559
559
{
560
- return & clock .ports_list ;
560
+ return & ptp_clk .ports_list ;
561
561
}
562
562
563
563
enum ptp_clock_type ptp_clock_type (void )
564
564
{
565
- return (enum ptp_clock_type )clock .default_ds .type ;
565
+ return (enum ptp_clock_type )ptp_clk .default_ds .type ;
566
566
}
567
567
568
568
const struct ptp_default_ds * ptp_clock_default_ds (void )
569
569
{
570
- return & clock .default_ds ;
570
+ return & ptp_clk .default_ds ;
571
571
}
572
572
573
573
const struct ptp_parent_ds * ptp_clock_parent_ds (void )
574
574
{
575
- return & clock .parent_ds ;
575
+ return & ptp_clk .parent_ds ;
576
576
}
577
577
578
578
const struct ptp_current_ds * ptp_clock_current_ds (void )
579
579
{
580
- return & clock .current_ds ;
580
+ return & ptp_clk .current_ds ;
581
581
}
582
582
583
583
const struct ptp_time_prop_ds * ptp_clock_time_prop_ds (void )
584
584
{
585
- return & clock .time_prop_ds ;
585
+ return & ptp_clk .time_prop_ds ;
586
586
}
587
587
588
588
const struct ptp_dataset * ptp_clock_ds (void )
589
589
{
590
- struct ptp_dataset * ds = & clock .dataset ;
590
+ struct ptp_dataset * ds = & ptp_clk .dataset ;
591
591
592
- ds -> priority1 = clock .default_ds .priority1 ;
593
- ds -> clk_quality = clock .default_ds .clk_quality ;
594
- ds -> priority2 = clock .default_ds .priority2 ;
592
+ ds -> priority1 = ptp_clk .default_ds .priority1 ;
593
+ ds -> clk_quality = ptp_clk .default_ds .clk_quality ;
594
+ ds -> priority2 = ptp_clk .default_ds .priority2 ;
595
595
ds -> steps_rm = 0 ;
596
596
ds -> sender .port_number = 0 ;
597
597
ds -> receiver .port_number = 0 ;
598
- memcpy (& ds -> clk_id , & clock .default_ds .clk_id , sizeof (ptp_clk_id ));
599
- memcpy (& ds -> sender .clk_id , & clock .default_ds .clk_id , sizeof (ptp_clk_id ));
600
- memcpy (& ds -> receiver .clk_id , & clock .default_ds .clk_id , sizeof (ptp_clk_id ));
598
+ memcpy (& ds -> clk_id , & ptp_clk .default_ds .clk_id , sizeof (ptp_clk_id ));
599
+ memcpy (& ds -> sender .clk_id , & ptp_clk .default_ds .clk_id , sizeof (ptp_clk_id ));
600
+ memcpy (& ds -> receiver .clk_id , & ptp_clk .default_ds .clk_id , sizeof (ptp_clk_id ));
601
601
return ds ;
602
602
}
603
603
604
604
const struct ptp_dataset * ptp_clock_best_foreign_ds (void )
605
605
{
606
- return clock .best ? & clock .best -> dataset : NULL ;
606
+ return ptp_clk .best ? & ptp_clk .best -> dataset : NULL ;
607
607
}
608
608
609
609
struct ptp_port * ptp_clock_port_from_iface (struct net_if * iface )
610
610
{
611
611
struct ptp_port * port ;
612
612
613
- SYS_SLIST_FOR_EACH_CONTAINER (& clock .ports_list , port , node ) {
613
+ SYS_SLIST_FOR_EACH_CONTAINER (& ptp_clk .ports_list , port , node ) {
614
614
if (port -> iface == iface ) {
615
615
return port ;
616
616
}
@@ -621,28 +621,28 @@ struct ptp_port *ptp_clock_port_from_iface(struct net_if *iface)
621
621
622
622
void ptp_clock_pollfd_invalidate (void )
623
623
{
624
- clock .pollfd_valid = false;
624
+ ptp_clk .pollfd_valid = false;
625
625
}
626
626
627
627
void ptp_clock_signal_timeout (void )
628
628
{
629
- eventfd_write (clock .pollfd [0 ].fd , 1 );
629
+ eventfd_write (ptp_clk .pollfd [0 ].fd , 1 );
630
630
}
631
631
632
632
void ptp_clock_state_decision_req (void )
633
633
{
634
- clock .state_decision_event = true;
634
+ ptp_clk .state_decision_event = true;
635
635
}
636
636
637
637
void ptp_clock_port_add (struct ptp_port * port )
638
638
{
639
- clock .default_ds .n_ports ++ ;
640
- sys_slist_append (& clock .ports_list , & port -> node );
639
+ ptp_clk .default_ds .n_ports ++ ;
640
+ sys_slist_append (& ptp_clk .ports_list , & port -> node );
641
641
}
642
642
643
643
const struct ptp_foreign_tt_clock * ptp_clock_best_time_transmitter (void )
644
644
{
645
- return clock .best ;
645
+ return ptp_clk .best ;
646
646
}
647
647
648
648
bool ptp_clock_id_eq (const ptp_clk_id * c1 , const ptp_clk_id * c2 )
0 commit comments