11/* Copyright (c) Microsoft Corporation.
22 Licensed under the MIT License. */
3-
3+
44#include "sntp_client.h"
55
66#include <time.h>
1111
1212#include "networking.h"
1313
14- #define SNTP_THREAD_STACK_SIZE 2048
15- #define SNTP_THREAD_PRIORITY 9
16-
17- #define SNTP_SERVER "pool.ntp.org"
14+ #define SNTP_THREAD_STACK_SIZE 2048
15+ #define SNTP_THREAD_PRIORITY 9
1816
19- #define SNTP_UPDATE_EVENT 1
20- #define SNTP_NEW_TIME 2
17+ #define SNTP_UPDATE_EVENT 1
18+ #define SNTP_NEW_TIME 2
2119
2220// Seconds between Unix Epoch (1/1/1970) and NTP Epoch (1/1/1999)
23- #define UNIX_TO_NTP_EPOCH_SECS 0x83AA7E80
21+ #define UNIX_TO_NTP_EPOCH_SECS 0x83AA7E80
22+
23+ static const char * SNTP_SERVER [] = {
24+ "0.pool.ntp.org" ,
25+ "1.pool.ntp.org" ,
26+ "2.pool.ntp.org" ,
27+ "3.pool.ntp.org" ,
28+ };
29+ static UINT sntp_server_count = 0 ;
2430
2531static ULONG sntp_thread_stack [SNTP_THREAD_STACK_SIZE / sizeof (ULONG )];
2632static TX_THREAD mqtt_client_thread ;
@@ -31,17 +37,17 @@ static TX_EVENT_FLAGS_GROUP sntp_flags;
3137// Variables to keep track of time
3238static TX_MUTEX time_mutex ;
3339static ULONG sntp_last_time = 0 ;
34- static ULONG tx_last_ticks = 0 ;
35- static bool first_sync = false;
40+ static ULONG tx_last_ticks = 0 ;
41+ static bool first_sync = false;
3642
37- int _gettimeofday (struct timeval * tp , void * tzvp );
43+ int _gettimeofday (struct timeval * tp , void * tzvp );
3844
3945static void print_address (CHAR * preable , NXD_ADDRESS address )
4046{
4147 if (address .nxd_ip_version == NX_IP_VERSION_V4 )
4248 {
4349 ULONG ipv4 = address .nxd_ip_address .v4 ;
44-
50+
4551 printf ("\t%s: %d.%d.%d.%d\r\n" ,
4652 preable ,
4753 (u_int8_t )(ipv4 >> 24 ),
@@ -79,7 +85,7 @@ static void set_sntp_time()
7985
8086 // Stash the Unix and ThreadX times
8187 sntp_last_time = seconds - UNIX_TO_NTP_EPOCH_SECS ;
82- tx_last_ticks = tx_time_get ();
88+ tx_last_ticks = tx_time_get ();
8389
8490 tx_mutex_put (& time_mutex );
8591
@@ -105,13 +111,22 @@ static UINT sntp_client_run()
105111 UINT status ;
106112 NXD_ADDRESS sntp_address ;
107113
108- status = nxd_dns_host_by_name_get (& nx_dns_client , (UCHAR * )SNTP_SERVER , & sntp_address , 5 * NX_IP_PERIODIC_RATE , NX_IP_VERSION_V4 );
114+ printf ("\tSNTP server %s\r\n" , SNTP_SERVER [sntp_server_count ]);
115+
116+ status = nxd_dns_host_by_name_get (& nx_dns_client ,
117+ (UCHAR * )SNTP_SERVER [sntp_server_count ],
118+ & sntp_address ,
119+ 5 * NX_IP_PERIODIC_RATE ,
120+ NX_IP_VERSION_V4 );
109121 if (status != NX_SUCCESS )
110122 {
111- printf ("\tFAIL: Unable to resolve DNS for SNTP Server %s (0x%02x)\r\n" , SNTP_SERVER , status );
123+ printf ("\tFAIL: Unable to resolve DNS for SNTP Server %s (0x%02x)\r\n" , SNTP_SERVER [ sntp_server_count ] , status );
112124 return status ;
113125 }
114126
127+ // rotate to the next sntp dns service
128+ sntp_server_count = (sntp_server_count + 1 ) % (sizeof (SNTP_SERVER ) / sizeof (& SNTP_SERVER ));
129+
115130 print_address ("SNTP IP address" , sntp_address );
116131 status = nxd_sntp_client_initialize_unicast (& sntp_client , & sntp_address );
117132 if (status != NX_SUCCESS )
@@ -123,7 +138,7 @@ static UINT sntp_client_run()
123138
124139 // Run Unicast client
125140 status = nx_sntp_client_run_unicast (& sntp_client );
126- if (status != NX_SUCCESS )
141+ if (status != NX_SUCCESS )
127142 {
128143 printf ("\tFAIL: Unable to start unicast SNTP client (0x%02x)\r\n" , status );
129144 nx_sntp_client_stop (& sntp_client );
@@ -144,14 +159,14 @@ static void sntp_thread_entry(ULONG info)
144159
145160 // status = nx_sntp_client_create(&sntp_client, &nx_ip, 0, &nx_pool, NX_NULL, NX_NULL, NULL);
146161 status = nx_sntp_client_create (& sntp_client , & nx_ip , 0 , nx_ip .nx_ip_default_packet_pool , NX_NULL , NX_NULL , NULL );
147- if (status != NX_SUCCESS )
162+ if (status != NX_SUCCESS )
148163 {
149164 printf ("\tFAIL: SNTP client create failed (0x%02x)\r\n" , status );
150165 return ;
151166 }
152167
153168 status = nx_sntp_client_set_local_time (& sntp_client , 0 , 0 );
154- if (status != NX_SUCCESS )
169+ if (status != NX_SUCCESS )
155170 {
156171 printf ("\tFAIL: Unable to set local time for SNTP client (0x%02x)\r\n" , status );
157172 nx_sntp_client_delete (& sntp_client );
@@ -160,7 +175,7 @@ static void sntp_thread_entry(ULONG info)
160175
161176 // Setup time update callback function
162177 status = nx_sntp_client_set_time_update_notify (& sntp_client , time_update_callback );
163- if (status != NX_SUCCESS )
178+ if (status != NX_SUCCESS )
164179 {
165180 printf ("\tFAIL: Unable to set time update notify CB (0x%02x)\r\n" , status );
166181 nx_sntp_client_delete (& sntp_client );
@@ -256,11 +271,14 @@ UINT sntp_start()
256271
257272 status = tx_thread_create (& mqtt_client_thread ,
258273 "SNTP client thread" ,
259- sntp_thread_entry ,
274+ sntp_thread_entry ,
260275 (ULONG )NULL ,
261- & sntp_thread_stack , SNTP_THREAD_STACK_SIZE ,
262- SNTP_THREAD_PRIORITY , SNTP_THREAD_PRIORITY ,
263- TX_NO_TIME_SLICE , TX_AUTO_START );
276+ & sntp_thread_stack ,
277+ SNTP_THREAD_STACK_SIZE ,
278+ SNTP_THREAD_PRIORITY ,
279+ SNTP_THREAD_PRIORITY ,
280+ TX_NO_TIME_SLICE ,
281+ TX_AUTO_START );
264282 if (status != TX_SUCCESS )
265283 {
266284 printf ("Unable to create SNTP thread (0x%02x)\r\n" , status );
@@ -271,10 +289,10 @@ UINT sntp_start()
271289}
272290
273291// newlibc-nano stub
274- int _gettimeofday (struct timeval * tp , void * tzvp )
292+ int _gettimeofday (struct timeval * tp , void * tzvp )
275293{
276- tp -> tv_sec = sntp_time_get ();
294+ tp -> tv_sec = sntp_time_get ();
277295 tp -> tv_usec = 0 ;
278-
296+
279297 return 0 ;
280298}
0 commit comments