@@ -17,67 +17,59 @@ LOG_MODULE_REGISTER(sntp_client, LOG_LEVEL_DBG);
1717#include <stdlib.h>
1818#include <string.h>
1919
20-
2120static int resolve_dns (const char * host , char * ip_str , int * ipVer )
2221{
23- struct addrinfo hints ;
24- struct addrinfo * res ;
25- int errcode ;
26- char addrstr [100 ];
27- void * ptr = NULL ;
28-
29- memset (& hints , 0 , sizeof (hints ));
30- hints .ai_family = PF_UNSPEC ;
31- hints .ai_socktype = SOCK_STREAM ;
32- hints .ai_flags |= AI_CANONNAME ;
33-
34- errcode = getaddrinfo (host , NULL , & hints , & res );
35- if (errcode != 0 )
36- {
37- printf ("[sntp] getaddrinfo\n" );
38- return -1 ;
39- }
40-
22+ struct addrinfo hints ;
23+ struct addrinfo * res ;
24+ int errcode ;
25+ char addrstr [100 ];
26+ void * ptr = NULL ;
27+
28+ memset (& hints , 0 , sizeof (hints ));
29+ hints .ai_family = PF_UNSPEC ;
30+ hints .ai_socktype = SOCK_STREAM ;
31+ hints .ai_flags |= AI_CANONNAME ;
32+
33+ errcode = getaddrinfo (host , NULL , & hints , & res );
34+ if (errcode != 0 ) {
35+ printf ("[sntp] getaddrinfo\n" );
36+ return -1 ;
37+ }
38+
4139 if (!res ) {
4240 printf ("[sntp] result is null\n" );
4341 return -1 ;
4442 }
4543
46- printf ("[sntp] Host: %s\n" , host );
47- inet_ntop (res -> ai_family , res -> ai_addr -> data , addrstr , 100 );
48-
49- switch (res -> ai_family )
50- {
51- case AF_INET :
52- ptr = & ((struct sockaddr_in * ) res -> ai_addr )-> sin_addr ;
53- * ipVer = 4 ;
54- break ;
55- case AF_INET6 :
56- ptr = & ((struct sockaddr_in6 * ) res -> ai_addr )-> sin6_addr ;
57- * ipVer = 6 ;
58- break ;
59- }
60-
61- inet_ntop (res -> ai_family , ptr , addrstr , 100 );
44+ printf ("[sntp] Host: %s\n" , host );
45+ inet_ntop (res -> ai_family , res -> ai_addr -> data , addrstr , 100 );
46+
47+ switch (res -> ai_family ) {
48+ case AF_INET :
49+ ptr = & ((struct sockaddr_in * )res -> ai_addr )-> sin_addr ;
50+ * ipVer = 4 ;
51+ break ;
52+ case AF_INET6 :
53+ ptr = & ((struct sockaddr_in6 * )res -> ai_addr )-> sin6_addr ;
54+ * ipVer = 6 ;
55+ break ;
56+ }
57+
58+ inet_ntop (res -> ai_family , ptr , addrstr , 100 );
6259 memcpy (ip_str , addrstr , strlen (addrstr ));
6360 * ipVer = res -> ai_family == PF_INET6 ? 6 : 4 ;
64- printf ("[sntp] IPv%d address: %s\n" , * ipVer ,ip_str );
61+ printf ("[sntp] IPv%d address: %s\n" , * ipVer , ip_str );
6562 freeaddrinfo (res );
66- return 0 ;
63+ return 0 ;
6764}
6865
69-
7066static void date_print (const struct shell * shell , struct tm * tm )
7167{
7268 shell_print (shell ,
73- "%d-%02u-%02u "
74- "%02u:%02u:%02u UTC" ,
75- tm -> tm_year + 1900 ,
76- tm -> tm_mon + 1 ,
77- tm -> tm_mday ,
78- tm -> tm_hour ,
79- tm -> tm_min ,
80- tm -> tm_sec );
69+ "%d-%02u-%02u "
70+ "%02u:%02u:%02u UTC" ,
71+ tm -> tm_year + 1900 , tm -> tm_mon + 1 , tm -> tm_mday , tm -> tm_hour , tm -> tm_min ,
72+ tm -> tm_sec );
8173}
8274
8375static int time_date_set (const struct shell * shell , uint32_t epoch_sec )
@@ -87,14 +79,14 @@ static int time_date_set(const struct shell *shell, uint32_t epoch_sec)
8779 tp .tv_sec = (uint32_t )epoch_sec ;
8880
8981 gmtime_r (& tp .tv_sec , & tm );
90- date_print (shell ,& tm );
82+ date_print (shell , & tm );
9183
9284#if defined(CONFIG_COUNTER_GECKO_RTCC )
9385 RTCC_CounterSet (tp .tv_sec );
9486#endif
9587
96- if ( clock_settime ( CLOCK_REALTIME , & tp ) == -1 ) {
97- shell_error ( shell , "system clock set failed" );
88+ if ( clock_settime (CLOCK_REALTIME , & tp ) == -1 ) {
89+ shell_error (shell , "system clock set failed" );
9890 return - EINVAL ;
9991 }
10092 shell_print (shell , "successfully updated RTC" );
@@ -104,11 +96,10 @@ static int time_date_set(const struct shell *shell, uint32_t epoch_sec)
10496
10597int isValidIpAddress (char * ipAddress )
10698{
107- struct sockaddr_in sa ;
108- return inet_pton (AF_INET , ipAddress , & (sa .sin_addr ));
99+ struct sockaddr_in sa ;
100+ return inet_pton (AF_INET , ipAddress , & (sa .sin_addr ));
109101}
110102
111-
112103int tmo_update_time (const struct shell * shell , char * host , int iface_idx )
113104{
114105#if defined(CONFIG_NET_IPV6 )
@@ -120,14 +111,15 @@ int tmo_update_time(const struct shell *shell, char *host, int iface_idx)
120111 int ipVer = 4 ;
121112
122113 // Create and zero out the packet. All 48 bytes worth.
123- ntp_packet packet = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
124- memset ( & packet , 0 , sizeof ( ntp_packet ) );
114+ ntp_packet packet = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
115+ memset (& packet , 0 , sizeof (ntp_packet ) );
125116
126- // Set the first byte's bits to 00,011,011 for li = 0, vn = 3, and mode = 3. The rest will be left set to zero.
127- * ( ( char * ) & packet + 0 ) = 0x1b ; // Represents 27 in base 10 or 00011011 in base 2.
117+ // Set the first byte's bits to 00,011,011 for li = 0, vn = 3, and mode = 3. The rest will
118+ // be left set to zero.
119+ * ((char * )& packet + 0 ) = 0x1b ; // Represents 27 in base 10 or 00011011 in base 2.
128120
129121 struct net_if * iface = net_if_get_by_index (iface_idx );
130-
122+
131123 if (tmo_offload_init (iface_idx )) {
132124 return -1 ;
133125 }
@@ -139,23 +131,26 @@ int tmo_update_time(const struct shell *shell, char *host, int iface_idx)
139131
140132 int sd = zsock_socket_ext (AF_INET , SOCK_DGRAM , IPPROTO_UDP , iface );
141133 if (sd == -1 ) {
142- shell_error (shell , "Socket creation failed, errno = %d" , errno );
134+ if (errno == ENOMEM ) {
135+ shell_error (shell , "No sockets available, errno = %d" , errno );
136+ } else {
137+ shell_error (shell , "Socket creation failed, errno = %d" , errno );
138+ }
143139 return 0 ;
144140 }
145141
146142 memset (ip_addr , 0 , sizeof (char ) * 100 );
147- if (isValidIpAddress (host )) {
143+ if (isValidIpAddress (host )) {
148144#ifdef DEBUG
149- shell_print (shell , "ip" );
145+ shell_print (shell , "ip" );
150146#endif
151- strncpy (ip_addr ,host , strlen (host ));
152- }
153- else {
147+ strncpy (ip_addr , host , strlen (host ));
148+ } else {
154149#ifdef DEBUG
155- shell_print (shell , "dns" );
150+ shell_print (shell , "dns" );
156151#endif
157152 resolve_dns (host , ip_addr , & ipVer );
158- }
153+ }
159154
160155 struct sockaddr_in sin ;
161156 sin .sin_family = AF_INET ;
@@ -169,20 +164,21 @@ int tmo_update_time(const struct shell *shell, char *host, int iface_idx)
169164 shell_error (shell , "zsock_connect errno" );
170165 }
171166
172- int stat = zsock_send (sd , ( char * ) & packet , sizeof ( ntp_packet ), 0 );
167+ int stat = zsock_send (sd , (char * ) & packet , sizeof (ntp_packet ), 0 );
173168 if (stat == -1 ) {
174169 shell_error (shell , "Send failed, errno = %d" , errno );
175170 zsock_close (sd );
176171 return - EINVAL ;
177172 }
178173 k_msleep (2000 );
179174
180- int recvsize = sizeof ( ntp_packet );
175+ int recvsize = sizeof (ntp_packet );
181176
182- memset ( ( char * ) & packet , 0 , sizeof ( ntp_packet ));
177+ memset (( char * ) & packet , 0 , sizeof (ntp_packet ));
183178 int total = 0 ;
184179 while (total < recvsize || recvsize == 0 ) {
185- stat = zsock_recv (sd , (( char * ) & packet ) + total , recvsize - total , ZSOCK_MSG_DONTWAIT );
180+ stat = zsock_recv (sd , ((char * )& packet ) + total , recvsize - total ,
181+ ZSOCK_MSG_DONTWAIT );
186182 if (stat == -1 ) {
187183 shell_error (shell , "recv failed, errno = %d" , errno );
188184 zsock_close (sd );
@@ -196,10 +192,10 @@ int tmo_update_time(const struct shell *shell, char *host, int iface_idx)
196192 return total ;
197193 }
198194
199- packet .txTm_s = ntohl ( packet .txTm_s ); // Time-stamp seconds.
200- packet .txTm_f = ntohl ( packet .txTm_f ); // Time-stamp fraction of a second.
195+ packet .txTm_s = ntohl (packet .txTm_s ); // Time-stamp seconds.
196+ packet .txTm_f = ntohl (packet .txTm_f ); // Time-stamp fraction of a second.
201197
202- time_t txTm = ( time_t ) ( packet .txTm_s - NTP_TIMESTAMP_DELTA );
198+ time_t txTm = (time_t )( packet .txTm_s - NTP_TIMESTAMP_DELTA );
203199#ifdef DEBUG
204200 shell_print (shell , "epoch %lld" , txTm );
205201#endif
0 commit comments