A time interface class. This class replicates the normal time functions, but goes a couple of steps further. mbed library 82 and prior has a defective gmtime function. Also, this class enables access to setting the time, and adjusting the accuracy of the RTC.

Dependencies:   CalendarPage

Dependents:   CI-data-logger-server WattEye X10Svr SSDP_Server

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Sun Jan 12 23:02:30 2020 +0000
Parent:
27:67e4e2ab048a
Child:
29:28180961841f
Child:
31:c692c48e79bb
Commit message:
More minor changes for NTP sync.

Changed in this revision

NTPClient/NTPClient.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/NTPClient/NTPClient.cpp	Sun Jan 12 19:04:47 2020 +0000
+++ b/NTPClient/NTPClient.cpp	Sun Jan 12 23:02:30 2020 +0000
@@ -24,7 +24,7 @@
 #include "NTPClient.h"
 
 
-//#define DEBUG "NTPc"
+#define DEBUG "NTPc"
 
 #if (defined(DEBUG))
 #include <cstdio>
@@ -256,7 +256,7 @@
 
     // Send the query
     int ret = m_sock.sendTo(nist, (char*)&pkt, sizeof(NTPPacket));
-    INFO("m_sock.sendto(...) returned %d", ret_send);
+    INFO("m_sock.sendto(...) returned %d", ret);
     if (ret < 0 ) {
         ERR("Could not send packet");
         m_sock.close();
@@ -306,6 +306,16 @@
     pkt.txTm_s = ntohl( pkt.txTm_s );
     pkt.txTm_f = ntohl( pkt.txTm_f );
 
+    //Compute offset, see RFC 4330 p.13
+    uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
+
+    // Modification by David Smart
+    // The setTime function was computing the offset incorrectly as the value was promoted to 64-bit.
+    // The side effect was that a negative offset ended up as a very large positive (e.g. jump from 
+    // 2016 to 2084). This change revises that computation.
+    int64_t offset = (((int64_t)pkt.rxTm_s - pkt.origTm_s) + ((int64_t)pkt.txTm_s - destTm_s)) / 2; //Avoid overflow
+    set_time(time(NULL) + offset);
+
     #ifdef DEBUG
     const char *ModeList[] = {
         "reserved", "symmetric active", "symmetric passive", "client",
@@ -325,19 +335,9 @@
     INFO("  pkt.rxTm_s         %08X, %u, time rcvd at server", pkt.rxTm_s, pkt.rxTm_s);
     INFO("  pkt.txTm_s         %08X, %u, time sent from server", pkt.txTm_s, pkt.txTm_s);
     INFO("  pkt.refTm_f        %08X, %u, fraction", pkt.refTm_f, pkt.refTm_f);
+    INFO("  offset             %08X, %u", destTm_s, destTm_s);
     #endif
 
-    //Compute offset, see RFC 4330 p.13
-    uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
-
-
-    // Modification by David Smart
-    // The setTime function was computing the offset incorrectly as the value was promoted to 64-bit.
-    // The side effect was that a negative offset ended up as a very large positive (e.g. jump from 
-    // 2016 to 2084). This change revises that computation.
-    int64_t offset = ( ((int64_t)pkt.rxTm_s - pkt.origTm_s ) + ((int64_t) pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow
-    set_time( time(NULL) + offset );
-
     m_sock.close();
 
 #endif // OS version