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
udp/ntp/ntpclientreply.c@138:5ff0c7069300, 2019-04-07 (annotated)
- Committer:
- andrewboyson
- Date:
- Sun Apr 07 18:36:42 2019 +0000
- Revision:
- 138:5ff0c7069300
- Parent:
- 125:8c84daac38ab
- Child:
- 139:1f3641abc07f
Removed all __packed structs
Who changed what in which revision?
User | Revision | Line number | New 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 | 138:5ff0c7069300 | 5 | #include "ntphdr.h" |
andrewboyson | 113:904b40231907 | 6 | #include "clktime.h" |
andrewboyson | 113:904b40231907 | 7 | #include "clktm.h" |
andrewboyson | 113:904b40231907 | 8 | #include "clkutc.h" |
andrewboyson | 113:904b40231907 | 9 | #include "clkgov.h" |
andrewboyson | 113:904b40231907 | 10 | #include "log.h" |
andrewboyson | 113:904b40231907 | 11 | #include "net.h" |
andrewboyson | 113:904b40231907 | 12 | |
andrewboyson | 113:904b40231907 | 13 | int32_t NtpClientReplyOffsetMs; |
andrewboyson | 113:904b40231907 | 14 | int32_t NtpClientReplyMaxDelayMs; |
andrewboyson | 113:904b40231907 | 15 | |
andrewboyson | 138:5ff0c7069300 | 16 | void NtpClientReply(void (*traceback)(void), char* pPacket) |
andrewboyson | 113:904b40231907 | 17 | { |
andrewboyson | 113:904b40231907 | 18 | if (NtpTrace) |
andrewboyson | 113:904b40231907 | 19 | { |
andrewboyson | 113:904b40231907 | 20 | if (NetTraceNewLine) Log("\r\n"); |
andrewboyson | 113:904b40231907 | 21 | LogTimeF("NTP received reply\r\n"); |
andrewboyson | 113:904b40231907 | 22 | if (NetTraceStack) traceback(); |
andrewboyson | 138:5ff0c7069300 | 23 | NtpLogHeader(pPacket); |
andrewboyson | 113:904b40231907 | 24 | } |
andrewboyson | 113:904b40231907 | 25 | |
andrewboyson | 138:5ff0c7069300 | 26 | uint64_t ori = NtpHdrGetOriTimeStamp(pPacket); |
andrewboyson | 138:5ff0c7069300 | 27 | uint64_t rec = NtpHdrGetRecTimeStamp(pPacket); |
andrewboyson | 138:5ff0c7069300 | 28 | int li = NtpHdrGetLI (pPacket); |
andrewboyson | 113:904b40231907 | 29 | |
andrewboyson | 113:904b40231907 | 30 | //Check the received timestamp delay |
andrewboyson | 124:6e558721ec1c | 31 | clktime oriTicks = NtpTimeStampToClkTime(ori); |
andrewboyson | 124:6e558721ec1c | 32 | clktime ntpTicks = NtpTimeStampToClkTime(rec); |
andrewboyson | 125:8c84daac38ab | 33 | clktime clkTicks = ClkTimeGet(); //Use the costly time this instant |
andrewboyson | 113:904b40231907 | 34 | |
andrewboyson | 124:6e558721ec1c | 35 | clktime roundTripTicks = clkTicks - oriTicks; |
andrewboyson | 124:6e558721ec1c | 36 | clktime delayMs = roundTripTicks >> CLK_TIME_ONE_MS_ISH_SHIFT; |
andrewboyson | 124:6e558721ec1c | 37 | clktime limit = NtpClientReplyMaxDelayMs; |
andrewboyson | 113:904b40231907 | 38 | if (delayMs > limit) |
andrewboyson | 113:904b40231907 | 39 | { |
andrewboyson | 113:904b40231907 | 40 | LogTimeF("NtpClient error: delay %lld ms is greater than limit %lld ms\r\n", delayMs, limit); |
andrewboyson | 113:904b40231907 | 41 | return; |
andrewboyson | 113:904b40231907 | 42 | } |
andrewboyson | 113:904b40231907 | 43 | |
andrewboyson | 113:904b40231907 | 44 | if (NtpClientTrace) |
andrewboyson | 113:904b40231907 | 45 | { |
andrewboyson | 124:6e558721ec1c | 46 | int64_t diffMs = (ntpTicks - clkTicks) >> CLK_TIME_ONE_MS_ISH_SHIFT; |
andrewboyson | 113:904b40231907 | 47 | LogTimeF("NtpClient difference (ext-int) is %lld ms\r\n", diffMs); |
andrewboyson | 113:904b40231907 | 48 | } |
andrewboyson | 113:904b40231907 | 49 | |
andrewboyson | 113:904b40231907 | 50 | //Handle the LI |
andrewboyson | 113:904b40231907 | 51 | if (li == 3) |
andrewboyson | 113:904b40231907 | 52 | { |
andrewboyson | 113:904b40231907 | 53 | LogTimeF("NtpClient error: NTP server is not synchronised (LI = 3)\r\n"); |
andrewboyson | 113:904b40231907 | 54 | return; |
andrewboyson | 113:904b40231907 | 55 | } |
andrewboyson | 113:904b40231907 | 56 | if (li == 1 || li == 2) |
andrewboyson | 113:904b40231907 | 57 | { |
andrewboyson | 113:904b40231907 | 58 | struct tm tm; |
andrewboyson | 113:904b40231907 | 59 | ClkTimeToTmUtc(clkTicks, &tm); |
andrewboyson | 113:904b40231907 | 60 | int year1970 = tm.tm_year - 70; //1900 |
andrewboyson | 113:904b40231907 | 61 | int month = tm.tm_mon; //0 to 11 |
andrewboyson | 113:904b40231907 | 62 | ClkUtcSetNextEpochMonth1970(year1970 * 12 + month + 1); //+1 as new UTC epoch is at the start of next month |
andrewboyson | 113:904b40231907 | 63 | ClkUtcSetNextLeapForward(li == 1); |
andrewboyson | 113:904b40231907 | 64 | ClkUtcSetNextLeapEnable(true); |
andrewboyson | 113:904b40231907 | 65 | } |
andrewboyson | 113:904b40231907 | 66 | if (li == 0) |
andrewboyson | 113:904b40231907 | 67 | { |
andrewboyson | 113:904b40231907 | 68 | ClkUtcSetNextLeapEnable(false); |
andrewboyson | 113:904b40231907 | 69 | } |
andrewboyson | 113:904b40231907 | 70 | |
andrewboyson | 113:904b40231907 | 71 | //Set the clock |
andrewboyson | 124:6e558721ec1c | 72 | clktime offsetTime = NtpClientReplyOffsetMs << CLK_TIME_ONE_MS_ISH_SHIFT; |
andrewboyson | 113:904b40231907 | 73 | ClkGovSyncTime(ntpTicks + offsetTime); |
andrewboyson | 113:904b40231907 | 74 | |
andrewboyson | 113:904b40231907 | 75 | //Tell the query service that the time has been updated |
andrewboyson | 113:904b40231907 | 76 | NtpClientTimeUpdateSuccessful(); |
andrewboyson | 113:904b40231907 | 77 | } |