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@200:5acbc41bf469, 2021-05-20 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu May 20 14:32:52 2021 +0000
- Revision:
- 200:5acbc41bf469
- Parent:
- 139:1f3641abc07f
Increased number of arp entries from 20 to 30 to accommodate the number of WIZ devices plus a few incoming port 80 calls from the internet.
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 | 139:1f3641abc07f | 25 | uint64_t ori = NtpHdrGetOriTimeStamp(pPacket); |
andrewboyson | 139:1f3641abc07f | 26 | if (NtpClientQueryTime == 0 || ori != NtpClientQueryTime) |
andrewboyson | 139:1f3641abc07f | 27 | { |
andrewboyson | 139:1f3641abc07f | 28 | LogTimeF("NtpClient error: unsolicited reply\r\n"); |
andrewboyson | 139:1f3641abc07f | 29 | return; |
andrewboyson | 139:1f3641abc07f | 30 | } |
andrewboyson | 113:904b40231907 | 31 | |
andrewboyson | 138:5ff0c7069300 | 32 | uint64_t rec = NtpHdrGetRecTimeStamp(pPacket); |
andrewboyson | 138:5ff0c7069300 | 33 | int li = NtpHdrGetLI (pPacket); |
andrewboyson | 113:904b40231907 | 34 | |
andrewboyson | 113:904b40231907 | 35 | //Check the received timestamp delay |
andrewboyson | 124:6e558721ec1c | 36 | clktime oriTicks = NtpTimeStampToClkTime(ori); |
andrewboyson | 124:6e558721ec1c | 37 | clktime ntpTicks = NtpTimeStampToClkTime(rec); |
andrewboyson | 125:8c84daac38ab | 38 | clktime clkTicks = ClkTimeGet(); //Use the costly time this instant |
andrewboyson | 113:904b40231907 | 39 | |
andrewboyson | 124:6e558721ec1c | 40 | clktime roundTripTicks = clkTicks - oriTicks; |
andrewboyson | 124:6e558721ec1c | 41 | clktime delayMs = roundTripTicks >> CLK_TIME_ONE_MS_ISH_SHIFT; |
andrewboyson | 124:6e558721ec1c | 42 | clktime limit = NtpClientReplyMaxDelayMs; |
andrewboyson | 113:904b40231907 | 43 | if (delayMs > limit) |
andrewboyson | 113:904b40231907 | 44 | { |
andrewboyson | 113:904b40231907 | 45 | LogTimeF("NtpClient error: delay %lld ms is greater than limit %lld ms\r\n", delayMs, limit); |
andrewboyson | 113:904b40231907 | 46 | return; |
andrewboyson | 113:904b40231907 | 47 | } |
andrewboyson | 113:904b40231907 | 48 | |
andrewboyson | 113:904b40231907 | 49 | if (NtpClientTrace) |
andrewboyson | 113:904b40231907 | 50 | { |
andrewboyson | 124:6e558721ec1c | 51 | int64_t diffMs = (ntpTicks - clkTicks) >> CLK_TIME_ONE_MS_ISH_SHIFT; |
andrewboyson | 113:904b40231907 | 52 | LogTimeF("NtpClient difference (ext-int) is %lld ms\r\n", diffMs); |
andrewboyson | 113:904b40231907 | 53 | } |
andrewboyson | 113:904b40231907 | 54 | |
andrewboyson | 113:904b40231907 | 55 | //Handle the LI |
andrewboyson | 113:904b40231907 | 56 | if (li == 3) |
andrewboyson | 113:904b40231907 | 57 | { |
andrewboyson | 113:904b40231907 | 58 | LogTimeF("NtpClient error: NTP server is not synchronised (LI = 3)\r\n"); |
andrewboyson | 113:904b40231907 | 59 | return; |
andrewboyson | 113:904b40231907 | 60 | } |
andrewboyson | 113:904b40231907 | 61 | if (li == 1 || li == 2) |
andrewboyson | 113:904b40231907 | 62 | { |
andrewboyson | 113:904b40231907 | 63 | struct tm tm; |
andrewboyson | 113:904b40231907 | 64 | ClkTimeToTmUtc(clkTicks, &tm); |
andrewboyson | 113:904b40231907 | 65 | int year1970 = tm.tm_year - 70; //1900 |
andrewboyson | 113:904b40231907 | 66 | int month = tm.tm_mon; //0 to 11 |
andrewboyson | 113:904b40231907 | 67 | ClkUtcSetNextEpochMonth1970(year1970 * 12 + month + 1); //+1 as new UTC epoch is at the start of next month |
andrewboyson | 113:904b40231907 | 68 | ClkUtcSetNextLeapForward(li == 1); |
andrewboyson | 113:904b40231907 | 69 | ClkUtcSetNextLeapEnable(true); |
andrewboyson | 113:904b40231907 | 70 | } |
andrewboyson | 113:904b40231907 | 71 | if (li == 0) |
andrewboyson | 113:904b40231907 | 72 | { |
andrewboyson | 113:904b40231907 | 73 | ClkUtcSetNextLeapEnable(false); |
andrewboyson | 113:904b40231907 | 74 | } |
andrewboyson | 113:904b40231907 | 75 | |
andrewboyson | 113:904b40231907 | 76 | //Set the clock |
andrewboyson | 124:6e558721ec1c | 77 | clktime offsetTime = NtpClientReplyOffsetMs << CLK_TIME_ONE_MS_ISH_SHIFT; |
andrewboyson | 113:904b40231907 | 78 | ClkGovSyncTime(ntpTicks + offsetTime); |
andrewboyson | 113:904b40231907 | 79 | |
andrewboyson | 113:904b40231907 | 80 | //Tell the query service that the time has been updated |
andrewboyson | 113:904b40231907 | 81 | NtpClientTimeUpdateSuccessful(); |
andrewboyson | 113:904b40231907 | 82 | } |