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:
Fri Feb 22 11:26:55 2019 +0000
Revision:
125:8c84daac38ab
Parent:
124:6e558721ec1c
Child:
138:5ff0c7069300
tidied up ntp data types for calculating ms from clktime.

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 124:6e558721ec1c 4 #include "ntptimestamp.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 124:6e558721ec1c 30 clktime oriTicks = NtpTimeStampToClkTime(ori);
andrewboyson 124:6e558721ec1c 31 clktime ntpTicks = NtpTimeStampToClkTime(rec);
andrewboyson 125:8c84daac38ab 32 clktime clkTicks = ClkTimeGet(); //Use the costly time this instant
andrewboyson 113:904b40231907 33
andrewboyson 124:6e558721ec1c 34 clktime roundTripTicks = clkTicks - oriTicks;
andrewboyson 124:6e558721ec1c 35 clktime delayMs = roundTripTicks >> CLK_TIME_ONE_MS_ISH_SHIFT;
andrewboyson 124:6e558721ec1c 36 clktime 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 124:6e558721ec1c 45 int64_t diffMs = (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 124:6e558721ec1c 71 clktime 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 }