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/ntpclientquery.c@133:a37eb35a03f1, 2019-03-20 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Mar 20 11:20:44 2019 +0000
- Revision:
- 133:a37eb35a03f1
- Parent:
- 132:db2174b36a6d
- Child:
- 138:5ff0c7069300
Updated clock library
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 | #include <string.h> |
andrewboyson | 113:904b40231907 | 3 | #include <stdbool.h> |
andrewboyson | 113:904b40231907 | 4 | |
andrewboyson | 124:6e558721ec1c | 5 | #include "log.h" |
andrewboyson | 124:6e558721ec1c | 6 | #include "clk.h" |
andrewboyson | 124:6e558721ec1c | 7 | #include "mstimer.h" |
andrewboyson | 124:6e558721ec1c | 8 | #include "clktime.h" |
andrewboyson | 124:6e558721ec1c | 9 | #include "ntptimestamp.h" |
andrewboyson | 124:6e558721ec1c | 10 | #include "clkutc.h" |
andrewboyson | 124:6e558721ec1c | 11 | #include "clkgov.h" |
andrewboyson | 124:6e558721ec1c | 12 | #include "clktm.h" |
andrewboyson | 124:6e558721ec1c | 13 | #include "net.h" |
andrewboyson | 124:6e558721ec1c | 14 | #include "ntp.h" |
andrewboyson | 113:904b40231907 | 15 | #include "ntpclient.h" |
andrewboyson | 124:6e558721ec1c | 16 | #include "dns.h" |
andrewboyson | 124:6e558721ec1c | 17 | #include "ip4.h" |
andrewboyson | 116:60521b29e4c9 | 18 | #include "resolve.h" |
andrewboyson | 113:904b40231907 | 19 | #include "action.h" |
andrewboyson | 128:79052cb4a41c | 20 | #include "dnslabel.h" |
andrewboyson | 113:904b40231907 | 21 | |
andrewboyson | 113:904b40231907 | 22 | #define ONE_BILLION 1000000000ULL |
andrewboyson | 113:904b40231907 | 23 | #define ONE_MILLION 1000000LL |
andrewboyson | 113:904b40231907 | 24 | |
andrewboyson | 113:904b40231907 | 25 | char NtpClientQueryServerName[DNS_MAX_LABEL_LENGTH+1]; |
andrewboyson | 113:904b40231907 | 26 | int32_t NtpClientQueryInitialInterval; |
andrewboyson | 113:904b40231907 | 27 | int32_t NtpClientQueryNormalInterval; |
andrewboyson | 113:904b40231907 | 28 | int32_t NtpClientQueryRetryInterval; |
andrewboyson | 113:904b40231907 | 29 | |
andrewboyson | 113:904b40231907 | 30 | bool NtpClientQuerySendRequestsViaIp4 = false; |
andrewboyson | 113:904b40231907 | 31 | uint32_t NtpClientQueryServerIp4; |
andrewboyson | 113:904b40231907 | 32 | char NtpClientQueryServerIp6[16]; |
andrewboyson | 113:904b40231907 | 33 | |
andrewboyson | 113:904b40231907 | 34 | static uint64_t startNtpMs = 0; |
andrewboyson | 113:904b40231907 | 35 | static int intervalTypeNtp = NTP_QUERY_INTERVAL_INITIAL; |
andrewboyson | 113:904b40231907 | 36 | static bool intervalCompleteNtp() |
andrewboyson | 113:904b40231907 | 37 | { |
andrewboyson | 113:904b40231907 | 38 | uint32_t interval; |
andrewboyson | 113:904b40231907 | 39 | switch(intervalTypeNtp) |
andrewboyson | 113:904b40231907 | 40 | { |
andrewboyson | 113:904b40231907 | 41 | case NTP_QUERY_INTERVAL_INITIAL: interval = NtpClientQueryInitialInterval; break; |
andrewboyson | 113:904b40231907 | 42 | case NTP_QUERY_INTERVAL_NORMAL: interval = NtpClientQueryNormalInterval; break; |
andrewboyson | 113:904b40231907 | 43 | case NTP_QUERY_INTERVAL_RETRY: interval = NtpClientQueryRetryInterval; break; |
andrewboyson | 113:904b40231907 | 44 | } |
andrewboyson | 133:a37eb35a03f1 | 45 | return MsTimerRelative(startNtpMs, interval * 1000); |
andrewboyson | 113:904b40231907 | 46 | } |
andrewboyson | 113:904b40231907 | 47 | void NtpClientQueryStartInterval(int type) |
andrewboyson | 113:904b40231907 | 48 | { |
andrewboyson | 113:904b40231907 | 49 | startNtpMs = MsTimerCount; |
andrewboyson | 113:904b40231907 | 50 | intervalTypeNtp = type; |
andrewboyson | 113:904b40231907 | 51 | } |
andrewboyson | 113:904b40231907 | 52 | |
andrewboyson | 113:904b40231907 | 53 | void writeRequest(void* pPacket, int* pSize) |
andrewboyson | 113:904b40231907 | 54 | { |
andrewboyson | 113:904b40231907 | 55 | struct NtpHeader* pHeader = (struct NtpHeader*)pPacket; |
andrewboyson | 113:904b40231907 | 56 | |
andrewboyson | 113:904b40231907 | 57 | pHeader->Mode = NTP_CLIENT; |
andrewboyson | 113:904b40231907 | 58 | pHeader->VN = 3; |
andrewboyson | 113:904b40231907 | 59 | pHeader->LI = 0; |
andrewboyson | 113:904b40231907 | 60 | pHeader->Stratum = 0; |
andrewboyson | 113:904b40231907 | 61 | pHeader->Poll = 0; |
andrewboyson | 113:904b40231907 | 62 | pHeader->Precision = 0; |
andrewboyson | 113:904b40231907 | 63 | pHeader->RootDelay = 0; |
andrewboyson | 113:904b40231907 | 64 | pHeader->Dispersion = 0; |
andrewboyson | 113:904b40231907 | 65 | pHeader->RefIdentifier[0] = 0; |
andrewboyson | 113:904b40231907 | 66 | pHeader->RefIdentifier[1] = 0; |
andrewboyson | 113:904b40231907 | 67 | pHeader->RefIdentifier[2] = 0; |
andrewboyson | 113:904b40231907 | 68 | pHeader->RefIdentifier[3] = 0; |
andrewboyson | 113:904b40231907 | 69 | pHeader->RefTimeStamp = 0; |
andrewboyson | 113:904b40231907 | 70 | pHeader->OriTimeStamp = 0; |
andrewboyson | 113:904b40231907 | 71 | pHeader->RecTimeStamp = 0; |
andrewboyson | 125:8c84daac38ab | 72 | pHeader->TraTimeStamp = NetToHost64(NtpTimeStampFromClkTime(ClkTimeGet())); //use the costly time this instant |
andrewboyson | 113:904b40231907 | 73 | |
andrewboyson | 113:904b40231907 | 74 | *pSize = sizeof(struct NtpHeader); |
andrewboyson | 113:904b40231907 | 75 | } |
andrewboyson | 113:904b40231907 | 76 | |
andrewboyson | 113:904b40231907 | 77 | int NtpClientQueryPoll(int type, void* pPacket, int* pSize) |
andrewboyson | 113:904b40231907 | 78 | { |
andrewboyson | 113:904b40231907 | 79 | if (NtpClientQueryServerName[0]) //An empty name means ntp client is not enabled |
andrewboyson | 113:904b40231907 | 80 | { |
andrewboyson | 113:904b40231907 | 81 | if (intervalCompleteNtp()) //Wait for the time out |
andrewboyson | 113:904b40231907 | 82 | { |
andrewboyson | 113:904b40231907 | 83 | bool isMulticast = NtpClientQueryServerName[0] == '*'; |
andrewboyson | 116:60521b29e4c9 | 84 | if (isMulticast || Resolve(NtpClientQueryServerName, type, &NtpClientQueryServerIp4, NtpClientQueryServerIp6)) |
andrewboyson | 113:904b40231907 | 85 | { |
andrewboyson | 113:904b40231907 | 86 | ClkGovIsReceivingTime = false; |
andrewboyson | 113:904b40231907 | 87 | NtpClientQueryStartInterval(NTP_QUERY_INTERVAL_RETRY); |
andrewboyson | 113:904b40231907 | 88 | writeRequest(pPacket, pSize); |
andrewboyson | 113:904b40231907 | 89 | |
andrewboyson | 113:904b40231907 | 90 | if (isMulticast) return MULTICAST_NTP; |
andrewboyson | 113:904b40231907 | 91 | else return UNICAST_NTP; |
andrewboyson | 113:904b40231907 | 92 | } |
andrewboyson | 113:904b40231907 | 93 | } |
andrewboyson | 113:904b40231907 | 94 | } |
andrewboyson | 113:904b40231907 | 95 | return DO_NOTHING; |
andrewboyson | 113:904b40231907 | 96 | } |