A port and bug fix of Alix955/code/ntp-client. The socket would hang as it was defined both with a timeout and as blocking.

Files at this revision

API Documentation at this revision

Comitter:
sjalloq
Date:
Sun May 26 08:46:35 2019 +0100
Parent:
1:099750f42b02
Commit message:
ntp-client : fork from Alix955/code/ntp-client

- sockets should not be setup as blocking if you want to use a timeout. This
resulted in the socket hanging when packets went missing.

Changed in this revision

NTPClient.cpp Show annotated file Show diff for this revision Revisions of this file
NTPClient.h Show annotated file Show diff for this revision Revisions of this file
--- a/NTPClient.cpp	Fri Dec 07 13:07:56 2018 +0000
+++ b/NTPClient.cpp	Sun May 26 08:46:35 2019 +0100
@@ -1,58 +1,70 @@
 #include "ntp-client/NTPClient.h"
 #include "mbed.h"
 
-NTPClient::NTPClient(NetworkInterface *iface)
-: iface(iface), nist_server_address((char *)NTP_DEFULT_NIST_SERVER_ADDRESS), nist_server_port(NTP_DEFULT_NIST_SERVER_PORT) {
+NTPClient::NTPClient(NetworkInterface *iface) :	iface(iface), 
+												nist_server_address((char *)NTP_DEFULT_NIST_SERVER_ADDRESS), 
+												nist_server_port(NTP_DEFULT_NIST_SERVER_PORT) {
 }
 
-void NTPClient::set_server(char* server, int port){
+void NTPClient::set_server(char* server, int port) {
     nist_server_address = server;
-    nist_server_port = port;
+    nist_server_port    = port;
 }
 
+/* The timeout value is in uS.  A sensible positive value is >5K to 
+	avoid the timeout firing on slow responses.  Note that this is UDP
+	so there is the likely possibility that the packet will be lost. 
+	
+	A timeout value of 0 corresponds to set_blocking(false).
+	A timeout value of -1 corresponds to set_blocking(true).
+*/
 time_t NTPClient::get_timestamp(int timeout) {
-    const time_t TIME1970 = (time_t)2208988800UL;
-    int ntp_send_values[12] = {0};
-    int ntp_recv_values[12] = {0};
-
-    SocketAddress nist;
-    int ret_gethostbyname = iface->gethostbyname(nist_server_address, &nist);
+	const time_t TIME1970 = (time_t)2208988800UL;
+	int ntp_send_values[12] = {0};
+	int ntp_recv_values[12] = {0};
 
-    if (ret_gethostbyname < 0) {
-        // Network error on DNS lookup
-        printf("Error on DNS lookup : %d\n" , ret_gethostbyname);
-        return ret_gethostbyname;
-    }
-
-    nist.set_port(nist_server_port);
+	SocketAddress nist;
+	UDPSocket sock;
+	SocketAddress source;
+	
+	nsapi_size_or_error_t szerr;
 
-    memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
-    ntp_send_values[0] = '\x1b';
+	int ret_gethostbyname = iface->gethostbyname(nist_server_address, &nist);
+	if (ret_gethostbyname < 0) {
+		// Network error on DNS lookup
+		printf("Error on DNS lookup : %d\n" , ret_gethostbyname);
+		return ret_gethostbyname;
+	}
+	nist.set_port(nist_server_port);
 
-    memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values));
+	memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values));
+	memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
+	ntp_send_values[0] = '\x1b';
 
-    UDPSocket sock;
-    sock.open(iface);
-    sock.set_timeout(timeout);
+	sock.open(iface);
+	sock.set_timeout(timeout);
+	
+	szerr = sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values));
+	if(szerr < 0) {
+		sock.close();
+		printf("sock.sendto() error code = %d\n\r", szerr);
+		return szerr;
+	}
 
-    sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values));
+    szerr = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values));
+	if(szerr < 0) {
+		sock.close();
+		printf("sock.sendto() error code = %d\n\r", szerr);
+		return szerr;
+	}
 
-    SocketAddress source;
-    const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values));
-
-    if (n > 10) {
+    if (szerr > 10) {
         return ntohl(ntp_recv_values[10]) - TIME1970;
     } else {
-        if (n < 0) {
-            // Network error
-            printf("Network error: %d\n", n);
-            return n;
-        } else {
-            // No or partial data returned
-            printf("No or partial data returned: -1");
-            return -1;
-        }
-    }
+		// No or partial data returned
+		printf("No or partial data returned: bytes=%d\n\r", szerr);
+        return -1;
+	}
 }
 
 uint32_t NTPClient::ntohl(uint32_t x) {
@@ -61,4 +73,4 @@
     ret |= (x & 0xff0000UL) >> 8;
     ret |= (x & 0xff000000UL) >> 24;
     return ret;
-}
\ No newline at end of file
+}
--- a/NTPClient.h	Fri Dec 07 13:07:56 2018 +0000
+++ b/NTPClient.h	Sun May 26 08:46:35 2019 +0100
@@ -4,8 +4,7 @@
 #include "mbed.h"
 //use nslookup 2.pool.ntp.org in cmd to find ip address
 
-//#define NTP_DEFULT_NIST_SERVER_ADDRESS "2.pool.ntp.org"
-#define NTP_DEFULT_NIST_SERVER_ADDRESS "109.74.206.120"
+#define NTP_DEFULT_NIST_SERVER_ADDRESS "0.pool.ntp.org"
 #define NTP_DEFULT_NIST_SERVER_PORT 123
 
 class NTPClient {