Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Fri Feb 22 08:41:47 2019 +0000
Parent:
123:44fea08759af
Child:
125:8c84daac38ab
Commit message:
Moved NTP conversion routines from clock library

Changed in this revision

udp/ntp/ntp.c Show annotated file Show diff for this revision Revisions of this file
udp/ntp/ntpclient.h Show annotated file Show diff for this revision Revisions of this file
udp/ntp/ntpclientquery.c Show annotated file Show diff for this revision Revisions of this file
udp/ntp/ntpclientreply.c Show annotated file Show diff for this revision Revisions of this file
udp/ntp/ntpserver.c Show annotated file Show diff for this revision Revisions of this file
udp/ntp/ntptimestamp.c Show annotated file Show diff for this revision Revisions of this file
udp/ntp/ntptimestamp.h Show annotated file Show diff for this revision Revisions of this file
--- a/udp/ntp/ntp.c	Thu Feb 21 12:53:06 2019 +0000
+++ b/udp/ntp/ntp.c	Fri Feb 22 08:41:47 2019 +0000
@@ -7,7 +7,6 @@
 #include "udp.h"
 #include "ntpclient.h"
 #include "ntpserver.h"
-#include "clkntp.h"
 #include "clktime.h"
 #include "ntp.h"
 #include "fault.h"
--- a/udp/ntp/ntpclient.h	Thu Feb 21 12:53:06 2019 +0000
+++ b/udp/ntp/ntpclient.h	Fri Feb 22 08:41:47 2019 +0000
@@ -6,8 +6,8 @@
 extern void     NtpClientInit(void);
 extern void     NtpClientTimeUpdateSuccessful(void);
 
-extern int      NtpClientReplyOffsetMs;
-extern int      NtpClientReplyMaxDelayMs;
+extern int32_t  NtpClientReplyOffsetMs;
+extern int32_t  NtpClientReplyMaxDelayMs;
 extern void     NtpClientReply(void (*traceback)(void), struct NtpHeader* pHeader);
 
 extern char     NtpClientQueryServerName[];
--- a/udp/ntp/ntpclientquery.c	Thu Feb 21 12:53:06 2019 +0000
+++ b/udp/ntp/ntpclientquery.c	Fri Feb 22 08:41:47 2019 +0000
@@ -2,19 +2,19 @@
 #include <string.h>
 #include <stdbool.h>
 
-#include      "log.h"
-#include      "clk.h"
-#include  "mstimer.h"
-#include  "clktime.h"
-#include   "clkntp.h"
-#include   "clkutc.h"
-#include   "clkgov.h"
-#include    "clktm.h"
-#include      "net.h"
-#include      "ntp.h"
+#include "log.h"
+#include "clk.h"
+#include "mstimer.h"
+#include "clktime.h"
+#include "ntptimestamp.h"
+#include "clkutc.h"
+#include "clkgov.h"
+#include "clktm.h"
+#include "net.h"
+#include "ntp.h"
 #include "ntpclient.h"
-#include      "dns.h"
-#include      "ip4.h"
+#include "dns.h"
+#include "ip4.h"
 #include "resolve.h"
 #include "action.h"
 
@@ -68,7 +68,7 @@
     pHeader->RefTimeStamp     = 0;
     pHeader->OriTimeStamp     = 0;
     pHeader->RecTimeStamp     = 0;
-    pHeader->TraTimeStamp     = NetToHost64(ClkTimeToNtpTimeStamp(ClkTimeGet()));
+    pHeader->TraTimeStamp     = NetToHost64(NtpTimeStampFromClkTime(ClkTimeGet()));
 
     *pSize = sizeof(struct NtpHeader);
 }
--- a/udp/ntp/ntpclientreply.c	Thu Feb 21 12:53:06 2019 +0000
+++ b/udp/ntp/ntpclientreply.c	Fri Feb 22 08:41:47 2019 +0000
@@ -1,7 +1,7 @@
 #include <stdint.h>
 
 #include "ntpclient.h"
-#include "clkntp.h"
+#include "ntptimestamp.h"
 #include "clktime.h"
 #include "clktm.h"
 #include "clkutc.h"
@@ -27,13 +27,13 @@
     int      li  =             pHeader->LI;
     
     //Check the received timestamp delay
-    int64_t oriTicks = ClkTimeFromNtpTimeStamp(ori);
-    int64_t ntpTicks = ClkTimeFromNtpTimeStamp(rec);
-    int64_t clkTicks = ClkTimeGet();
+    clktime oriTicks = NtpTimeStampToClkTime(ori);
+    clktime ntpTicks = NtpTimeStampToClkTime(rec);
+    clktime clkTicks = ClkTimeGet();
     
-    int64_t roundTripTicks = clkTicks - oriTicks;
-    int64_t delayMs        = roundTripTicks >> CLK_TIME_ONE_MS_ISH_SHIFT;
-    int64_t limit          = NtpClientReplyMaxDelayMs;
+    clktime roundTripTicks = clkTicks - oriTicks;
+    clktime delayMs        = roundTripTicks >> CLK_TIME_ONE_MS_ISH_SHIFT;
+    clktime limit          = NtpClientReplyMaxDelayMs;
     if (delayMs > limit)
     {
         LogTimeF("NtpClient error: delay %lld ms is greater than limit %lld ms\r\n", delayMs, limit);
@@ -42,7 +42,7 @@
     
     if (NtpClientTrace)
     {
-        int64_t diffMs = ((int64_t)(ntpTicks - clkTicks)) >> CLK_TIME_ONE_MS_ISH_SHIFT;
+        int64_t diffMs = (ntpTicks - clkTicks) >> CLK_TIME_ONE_MS_ISH_SHIFT;
         LogTimeF("NtpClient difference (ext-int) is %lld ms\r\n", diffMs);
     }
     
@@ -68,7 +68,7 @@
     }
     
     //Set the clock
-    int64_t offsetTime = NtpClientReplyOffsetMs << CLK_TIME_ONE_MS_ISH_SHIFT;
+    clktime offsetTime = NtpClientReplyOffsetMs << CLK_TIME_ONE_MS_ISH_SHIFT;
     ClkGovSyncTime(ntpTicks + offsetTime);
     
     //Tell the query service that the time has been updated
--- a/udp/ntp/ntpserver.c	Thu Feb 21 12:53:06 2019 +0000
+++ b/udp/ntp/ntpserver.c	Fri Feb 22 08:41:47 2019 +0000
@@ -4,7 +4,7 @@
 #include "clkgov.h"
 #include "clktime.h"
 #include "clkutc.h"
-#include "clkntp.h"
+#include "ntptimestamp.h"
 #include "clkutc.h"
 #include "ntp.h"
 #include "action.h"
@@ -60,7 +60,7 @@
 }
 static uint64_t getRefNtp()
 {
-    return ClkTimeToNtpTimeStamp(ClkRefTai());
+    return NtpTimeStampFromClkTime(ClkRefTai());
 }
 int NtpServerRequest(void (*traceback)(void), struct NtpHeader* pHeaderRx, struct NtpHeader* pHeaderTx)
 {
@@ -75,7 +75,7 @@
     }
     
     uint64_t refNtp    = getRefNtp();
-    uint64_t nowNtp    = ClkTimeToNtpTimeStamp(ClkTimeGet());
+    uint64_t nowNtp    = NtpTimeStampFromClkTime(ClkTimeGet());
     int      stratum   = getStratum();
     char*    ident     = getIdent();
     int      li        = getLi();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/udp/ntp/ntptimestamp.c	Fri Feb 22 08:41:47 2019 +0000
@@ -0,0 +1,36 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "clktime.h"
+#include "clkutc.h"
+
+#define SECONDS_BETWEEN_1900_AND_1970 2208988800ULL
+
+#define ERA_BASE_LOW 1LL //Adjust this from 0 to 1 between 1968 and 2036; 1 to 2 between 2104 and 2168
+#define ERA_BASE_HGH 0LL //Adjust this from 0 to 1 between 2036 and 2104; 1 to 2 between 2172 and 2240
+
+uint64_t NtpTimeStampFromClkTime(clktime tai)
+{
+    clktime utc = ClkUtcFromTai(tai);
+    uint64_t timestamp = utc << (32 - CLK_TIME_ONE_SECOND_SHIFT);
+    
+    timestamp += SECONDS_BETWEEN_1900_AND_1970 << 32; //This should just wrap around the unsigned timestamp removing the unwanted era.
+    
+    return timestamp;
+}
+clktime NtpTimeStampToClkTime(uint64_t timestamp)
+{
+    bool isHigh = (timestamp & (1ULL << 63)) != 0;
+    
+    clktime utc = timestamp - (SECONDS_BETWEEN_1900_AND_1970 << 32);
+    utc >>= (32 - CLK_TIME_ONE_SECOND_SHIFT);
+    
+    //Correct for era
+    if (isHigh) utc += ERA_BASE_HGH << (32 + CLK_TIME_ONE_SECOND_SHIFT);
+    else        utc += ERA_BASE_LOW << (32 + CLK_TIME_ONE_SECOND_SHIFT);
+    
+    clktime tai = ClkUtcToTai(utc);
+    
+    return tai;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/udp/ntp/ntptimestamp.h	Fri Feb 22 08:41:47 2019 +0000
@@ -0,0 +1,5 @@
+#include <stdint.h>
+#include "clktime.h"
+
+extern uint64_t NtpTimeStampFromClkTime(clktime  tai);
+extern clktime  NtpTimeStampToClkTime  (uint64_t timestamp);