A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Tue Jan 22 15:41:12 2019 +0000
Revision:
113:904b40231907
Child:
124:6e558721ec1c
Incorporated ntp server module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 113:904b40231907 1 #include <stdint.h>
andrewboyson 113:904b40231907 2
andrewboyson 113:904b40231907 3 #include "ntpclient.h"
andrewboyson 113:904b40231907 4 #include "clkntp.h"
andrewboyson 113:904b40231907 5 #include "clktime.h"
andrewboyson 113:904b40231907 6 #include "clktm.h"
andrewboyson 113:904b40231907 7 #include "clkutc.h"
andrewboyson 113:904b40231907 8 #include "clkgov.h"
andrewboyson 113:904b40231907 9 #include "log.h"
andrewboyson 113:904b40231907 10 #include "net.h"
andrewboyson 113:904b40231907 11
andrewboyson 113:904b40231907 12 int32_t NtpClientReplyOffsetMs;
andrewboyson 113:904b40231907 13 int32_t NtpClientReplyMaxDelayMs;
andrewboyson 113:904b40231907 14
andrewboyson 113:904b40231907 15 void NtpClientReply(void (*traceback)(void), struct NtpHeader* pHeader)
andrewboyson 113:904b40231907 16 {
andrewboyson 113:904b40231907 17 if (NtpTrace)
andrewboyson 113:904b40231907 18 {
andrewboyson 113:904b40231907 19 if (NetTraceNewLine) Log("\r\n");
andrewboyson 113:904b40231907 20 LogTimeF("NTP received reply\r\n");
andrewboyson 113:904b40231907 21 if (NetTraceStack) traceback();
andrewboyson 113:904b40231907 22 NtpLogHeader(pHeader);
andrewboyson 113:904b40231907 23 }
andrewboyson 113:904b40231907 24
andrewboyson 113:904b40231907 25 uint64_t ori = NetToHost64(pHeader->OriTimeStamp);
andrewboyson 113:904b40231907 26 uint64_t rec = NetToHost64(pHeader->RecTimeStamp);
andrewboyson 113:904b40231907 27 int li = pHeader->LI;
andrewboyson 113:904b40231907 28
andrewboyson 113:904b40231907 29 //Check the received timestamp delay
andrewboyson 113:904b40231907 30 int64_t oriTicks = ClkTimeFromNtpTimeStamp(ori);
andrewboyson 113:904b40231907 31 int64_t ntpTicks = ClkTimeFromNtpTimeStamp(rec);
andrewboyson 113:904b40231907 32 int64_t clkTicks = ClkTimeGet();
andrewboyson 113:904b40231907 33
andrewboyson 113:904b40231907 34 int64_t roundTripTicks = clkTicks - oriTicks;
andrewboyson 113:904b40231907 35 int64_t delayMs = roundTripTicks >> CLK_TIME_ONE_MS_ISH_SHIFT;
andrewboyson 113:904b40231907 36 int64_t limit = NtpClientReplyMaxDelayMs;
andrewboyson 113:904b40231907 37 if (delayMs > limit)
andrewboyson 113:904b40231907 38 {
andrewboyson 113:904b40231907 39 LogTimeF("NtpClient error: delay %lld ms is greater than limit %lld ms\r\n", delayMs, limit);
andrewboyson 113:904b40231907 40 return;
andrewboyson 113:904b40231907 41 }
andrewboyson 113:904b40231907 42
andrewboyson 113:904b40231907 43 if (NtpClientTrace)
andrewboyson 113:904b40231907 44 {
andrewboyson 113:904b40231907 45 int64_t diffMs = ((int64_t)(ntpTicks - clkTicks)) >> CLK_TIME_ONE_MS_ISH_SHIFT;
andrewboyson 113:904b40231907 46 LogTimeF("NtpClient difference (ext-int) is %lld ms\r\n", diffMs);
andrewboyson 113:904b40231907 47 }
andrewboyson 113:904b40231907 48
andrewboyson 113:904b40231907 49 //Handle the LI
andrewboyson 113:904b40231907 50 if (li == 3)
andrewboyson 113:904b40231907 51 {
andrewboyson 113:904b40231907 52 LogTimeF("NtpClient error: NTP server is not synchronised (LI = 3)\r\n");
andrewboyson 113:904b40231907 53 return;
andrewboyson 113:904b40231907 54 }
andrewboyson 113:904b40231907 55 if (li == 1 || li == 2)
andrewboyson 113:904b40231907 56 {
andrewboyson 113:904b40231907 57 struct tm tm;
andrewboyson 113:904b40231907 58 ClkTimeToTmUtc(clkTicks, &tm);
andrewboyson 113:904b40231907 59 int year1970 = tm.tm_year - 70; //1900
andrewboyson 113:904b40231907 60 int month = tm.tm_mon; //0 to 11
andrewboyson 113:904b40231907 61 ClkUtcSetNextEpochMonth1970(year1970 * 12 + month + 1); //+1 as new UTC epoch is at the start of next month
andrewboyson 113:904b40231907 62 ClkUtcSetNextLeapForward(li == 1);
andrewboyson 113:904b40231907 63 ClkUtcSetNextLeapEnable(true);
andrewboyson 113:904b40231907 64 }
andrewboyson 113:904b40231907 65 if (li == 0)
andrewboyson 113:904b40231907 66 {
andrewboyson 113:904b40231907 67 ClkUtcSetNextLeapEnable(false);
andrewboyson 113:904b40231907 68 }
andrewboyson 113:904b40231907 69
andrewboyson 113:904b40231907 70 //Set the clock
andrewboyson 113:904b40231907 71 int64_t offsetTime = NtpClientReplyOffsetMs << CLK_TIME_ONE_MS_ISH_SHIFT;
andrewboyson 113:904b40231907 72 ClkGovSyncTime(ntpTicks + offsetTime);
andrewboyson 113:904b40231907 73
andrewboyson 113:904b40231907 74 //Tell the query service that the time has been updated
andrewboyson 113:904b40231907 75 NtpClientTimeUpdateSuccessful();
andrewboyson 113:904b40231907 76 }