@@ -87,7 +87,7 @@ namespace net::globals::ptp {
8787extern uint32_t timestamp[2 ];
8888} // namespace net::globals::ptp
8989
90- #define _NTPFRAC_ (x ) (4294U * (x) + ((1981U * (x)) >> 11 ) + ((2911U * (x)) >> 28 ))
90+ #define _NTPFRAC_ (x ) (( 4294U * (x) ) + ((1981U * (x)) >> 11 ) + ((2911U * (x)) >> 28 ))
9191#define NTPFRAC (x ) _NTPFRAC_(x / 1000 )
9292// The reverse of the above, needed if we want to set our microsecond
9393// clock (via clock_settime) based on the incoming time in NTP format.
@@ -130,7 +130,15 @@ static void Print([[maybe_unused]] const char* text, [[maybe_unused]] const stru
130130#ifndef NDEBUG
131131 const auto kSeconds = static_cast <time_t >(timestamp->seconds - ntp::kJan1970 );
132132 const auto * local_time = localtime (&kSeconds );
133- printf (" %s %02d:%02d:%02d.%06d %04d [%u][0x%.8x]\n " , text, local_time->tm_hour , local_time->tm_min , local_time->tm_sec , USEC (timestamp->fraction ), local_time->tm_year + 1900 , timestamp->seconds , timestamp->fraction );
133+ printf (" %s %02d:%02d:%02d.%06d %04d [%u][0x%.8x]\n " ,
134+ text,
135+ static_cast <int >(local_time->tm_hour ),
136+ static_cast <int >(local_time->tm_min ),
137+ static_cast <int >(local_time->tm_sec ),
138+ static_cast <int >(USEC (timestamp->fraction )),
139+ local_time->tm_year + 1900 ,
140+ static_cast <int >(timestamp->seconds ),
141+ static_cast <unsigned >(timestamp->fraction ));
134142#endif
135143}
136144
@@ -237,9 +245,13 @@ static void Send() {
237245 network::udp::SendWithTimestamp (s_ntp_client.handle , reinterpret_cast <const uint8_t *>(&s_ntp_client.request ), kRequestSize , s_ntp_client.server_ip , network::iana::Ports::kPortNtp );
238246
239247#ifndef NDEBUG
240- printf (" Request: org=%.8x%.8x rx=%.8x%.8x tx=%.8x%.8x\n " , __builtin_bswap32 (s_ntp_client.request .origin_timestamp_s ), __builtin_bswap32 (s_ntp_client.request .origin_timestamp_f ),
241- __builtin_bswap32 (s_ntp_client.request .receive_timestamp_s ), __builtin_bswap32 (s_ntp_client.request .receive_timestamp_f ), __builtin_bswap32 (s_ntp_client.request .transmit_timestamp_s ),
242- __builtin_bswap32 (s_ntp_client.request .transmit_timestamp_f ));
248+ printf (" Request: org=%.8x%.8x rx=%.8x%.8x tx=%.8x%.8x\n " ,
249+ static_cast <unsigned >(__builtin_bswap32 (s_ntp_client.request .origin_timestamp_s )), // NOLINT
250+ static_cast <unsigned >(__builtin_bswap32 (s_ntp_client.request .origin_timestamp_f )), // NOLINT
251+ static_cast <unsigned >(__builtin_bswap32 (s_ntp_client.request .receive_timestamp_s )), // NOLINT
252+ static_cast <unsigned >(__builtin_bswap32 (s_ntp_client.request .receive_timestamp_f )), // NOLINT
253+ static_cast <unsigned >(__builtin_bswap32 (s_ntp_client.request .transmit_timestamp_s )), // NOLINT
254+ static_cast <unsigned >(__builtin_bswap32 (s_ntp_client.request .transmit_timestamp_f ))); // NOLINT
243255#endif
244256
245257 if (s_ntp_client.state .x > 0 ) {
@@ -273,10 +285,12 @@ static inline int32_t AbsInt32(int32_t x) {
273285}
274286
275287static void UpdatePtpTime () {
276- int32_t diff_seconds1, diff_nano_seconds1;
288+ int32_t diff_seconds1;
289+ int32_t diff_nano_seconds1;
277290 Difference (s_ntp_client.t1 , s_ntp_client.t2 , diff_seconds1, diff_nano_seconds1);
278291
279- int32_t diff_seconds2, diff_nano_seconds2;
292+ int32_t diff_seconds2;
293+ int32_t diff_nano_seconds2;
280294 Difference (s_ntp_client.t4 , s_ntp_client.t3 , diff_seconds2, diff_nano_seconds2);
281295
282296 const auto kOffsetSeconds = static_cast <int64_t >(diff_seconds1) + static_cast <int64_t >(diff_seconds2);
@@ -292,7 +306,7 @@ static void UpdatePtpTime() {
292306 gd32::ptp::ptptime ptp_get;
293307 Gd32PtpGetTime (&ptp_get);
294308
295- s_ntp_client.request .reference_timestamp_s = __builtin_bswap32 (static_cast < uint32_t >( ptp_get.tv_sec ) + ntp::kJan1970 );
309+ s_ntp_client.request .reference_timestamp_s = __builtin_bswap32 (ptp_get.tv_sec + ntp::kJan1970 );
296310 s_ntp_client.request .reference_timestamp_f = __builtin_bswap32 (NTPFRAC (ptp_get.tv_nsec ));
297311
298312 if ((ptp_offset.tv_sec == 0 ) && (ptp_offset.tv_nsec > -999999 ) && (ptp_offset.tv_nsec < 999999 )) {
@@ -350,7 +364,13 @@ static void UpdatePtpTime() {
350364 sign = ' -' ;
351365 }
352366
353- printf (" %s : offset=%c%d.%09d delay=%d.%09d\n " , s_ntp_client.state .mode == ntp::Modes::kBasic ? " Basic" : " Interleaved" , sign, ptp_offset.tv_sec , ptp_offset.tv_nsec , ptp_delay.tv_sec , ptp_delay.tv_nsec );
367+ printf (" %s : offset=%c%d.%09d delay=%d.%09d\n " ,
368+ s_ntp_client.state .mode == ntp::Modes::kBasic ? " Basic" : " Interleaved" ,
369+ sign,
370+ static_cast <int >(ptp_offset.tv_sec ),
371+ static_cast <int >(ptp_offset.tv_nsec ),
372+ static_cast <int >(ptp_delay.tv_sec ),
373+ static_cast <int >(ptp_delay.tv_nsec ));
354374#endif
355375}
356376
@@ -369,8 +389,13 @@ static void UpdatePtpTime() {
369389static void Process () {
370390 const auto * const kReply = s_ntp_client.reply ;
371391#ifndef NDEBUG
372- printf (" Response: org=%.8x%.8x rx=%.8x%.8x tx=%.8x%.8x\n " , __builtin_bswap32 (kReply ->origin_timestamp_s ), __builtin_bswap32 (kReply ->origin_timestamp_f ), __builtin_bswap32 (kReply ->receive_timestamp_s ),
373- __builtin_bswap32 (kReply ->receive_timestamp_f ), __builtin_bswap32 (kReply ->transmit_timestamp_s ), __builtin_bswap32 (kReply ->transmit_timestamp_f ));
392+ printf (" Response: org=%.8x%.8x rx=%.8x%.8x tx=%.8x%.8x\n " ,
393+ static_cast <unsigned >(__builtin_bswap32 (kReply ->origin_timestamp_s )), // NOLINT
394+ static_cast <unsigned >(__builtin_bswap32 (kReply ->origin_timestamp_f )), // NOLINT
395+ static_cast <unsigned >(__builtin_bswap32 (kReply ->receive_timestamp_s )), // NOLINT
396+ static_cast <unsigned >(__builtin_bswap32 (kReply ->receive_timestamp_f )), // NOLINT
397+ static_cast <unsigned >(__builtin_bswap32 (kReply ->transmit_timestamp_s )), // NOLINT
398+ static_cast <unsigned >(__builtin_bswap32 (kReply ->transmit_timestamp_f ))); // NOLINT
374399#endif
375400 // If the origin timestamp is equal to the transmit timestamp, the response is in the basic mode.
376401 if ((kReply ->origin_timestamp_s == s_ntp_client.request .transmit_timestamp_s ) && (kReply ->origin_timestamp_f == s_ntp_client.request .transmit_timestamp_f )) {
0 commit comments