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:
Mon Jun 26 15:42:06 2017 +0000
Revision:
19:43c70b424fc1
Parent:
17:e475ab861365
Child:
20:23f2b29b48ea
Added NtpEnableClient

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 1:5f70c361db20 1 #include "mbed.h"
andrewboyson 4:31fa7d50722c 2 #include "log.h"
andrewboyson 1:5f70c361db20 3 #include "net.h"
andrewboyson 7:b794780e33b4 4 #include "udp.h"
andrewboyson 0:faa09bd4e6bf 5
andrewboyson 0:faa09bd4e6bf 6 #define HEADER_SIZE 48
andrewboyson 0:faa09bd4e6bf 7
andrewboyson 0:faa09bd4e6bf 8 #define ERA_BASE 0
andrewboyson 0:faa09bd4e6bf 9 #define ERA_PIVOT 2016
andrewboyson 0:faa09bd4e6bf 10
andrewboyson 0:faa09bd4e6bf 11 #define CLIENT 3
andrewboyson 0:faa09bd4e6bf 12 #define SERVER 4
andrewboyson 0:faa09bd4e6bf 13
andrewboyson 17:e475ab861365 14 int64_t (*NtpGetClockRefFunction) ();
andrewboyson 17:e475ab861365 15 int64_t (*NtpGetClockNowFunction) ();
andrewboyson 17:e475ab861365 16 int (*NtpGetClockStratumFunction)();
andrewboyson 17:e475ab861365 17 char* (*NtpGetClockIdentFunction) ();
andrewboyson 17:e475ab861365 18 bool NtpEnableServer = false;
andrewboyson 19:43c70b424fc1 19 bool NtpEnableClient = false;
andrewboyson 17:e475ab861365 20
andrewboyson 0:faa09bd4e6bf 21 __packed struct header {
andrewboyson 0:faa09bd4e6bf 22 unsigned Mode : 3;
andrewboyson 0:faa09bd4e6bf 23 unsigned VN : 3;
andrewboyson 0:faa09bd4e6bf 24 unsigned LI : 2;
andrewboyson 0:faa09bd4e6bf 25 uint8_t Stratum;
andrewboyson 0:faa09bd4e6bf 26 int8_t Poll;
andrewboyson 0:faa09bd4e6bf 27 int8_t Precision;
andrewboyson 0:faa09bd4e6bf 28 uint32_t RootDelay;
andrewboyson 0:faa09bd4e6bf 29 uint32_t Dispersion;
andrewboyson 0:faa09bd4e6bf 30 char RefIdentifier[4];
andrewboyson 0:faa09bd4e6bf 31
andrewboyson 0:faa09bd4e6bf 32 uint64_t RefTimeStamp;
andrewboyson 0:faa09bd4e6bf 33 uint64_t OriTimeStamp;
andrewboyson 0:faa09bd4e6bf 34 uint64_t RecTimeStamp;
andrewboyson 0:faa09bd4e6bf 35 uint64_t TraTimeStamp;
andrewboyson 0:faa09bd4e6bf 36 };
andrewboyson 1:5f70c361db20 37 uint64_t makeNtpTimeStamp(int64_t ns)
andrewboyson 1:5f70c361db20 38 {
andrewboyson 1:5f70c361db20 39 uint64_t timestamp = ns << 2;
andrewboyson 1:5f70c361db20 40 timestamp /= 1000; timestamp <<= 10;
andrewboyson 1:5f70c361db20 41 timestamp /= 1000; timestamp <<= 10;
andrewboyson 1:5f70c361db20 42 timestamp /= 1000; timestamp <<= 10;
andrewboyson 0:faa09bd4e6bf 43
andrewboyson 1:5f70c361db20 44 timestamp += 2208988800ULL << 32;
andrewboyson 0:faa09bd4e6bf 45
andrewboyson 1:5f70c361db20 46 return NetToHost64(timestamp);
andrewboyson 0:faa09bd4e6bf 47 }
andrewboyson 7:b794780e33b4 48 int NtpHandleRequest(int* pSize, void * pPacket)
andrewboyson 0:faa09bd4e6bf 49 {
andrewboyson 17:e475ab861365 50 if (!NtpEnableServer) return DO_NOTHING;
andrewboyson 17:e475ab861365 51
andrewboyson 17:e475ab861365 52 if (!NtpGetClockRefFunction || !NtpGetClockNowFunction || !NtpGetClockStratumFunction || !NtpGetClockIdentFunction)
andrewboyson 17:e475ab861365 53 {
andrewboyson 17:e475ab861365 54 LogTimeF("NtpHandleRequest - NTP server is enabled but has not been plumbed into a clock\r\n");
andrewboyson 17:e475ab861365 55 return DO_NOTHING;
andrewboyson 17:e475ab861365 56 }
andrewboyson 17:e475ab861365 57
andrewboyson 9:91dae5300a4d 58 if (*pSize < HEADER_SIZE) return DO_NOTHING;
andrewboyson 0:faa09bd4e6bf 59
andrewboyson 0:faa09bd4e6bf 60 struct header* pHeader = (struct header*)pPacket;
andrewboyson 0:faa09bd4e6bf 61
andrewboyson 17:e475ab861365 62 int64_t refNs = NtpGetClockRefFunction();
andrewboyson 17:e475ab861365 63 int64_t nowNs = NtpGetClockNowFunction();
andrewboyson 17:e475ab861365 64 int stratum = NtpGetClockStratumFunction();
andrewboyson 17:e475ab861365 65 char* ident = NtpGetClockIdentFunction();
andrewboyson 17:e475ab861365 66
andrewboyson 0:faa09bd4e6bf 67 switch (pHeader->Mode)
andrewboyson 0:faa09bd4e6bf 68 {
andrewboyson 0:faa09bd4e6bf 69 case CLIENT:
andrewboyson 0:faa09bd4e6bf 70 pHeader->Mode = SERVER;
andrewboyson 0:faa09bd4e6bf 71 pHeader->LI = 0;
andrewboyson 17:e475ab861365 72 pHeader->Stratum = stratum;
andrewboyson 0:faa09bd4e6bf 73 pHeader->Poll = 0;
andrewboyson 0:faa09bd4e6bf 74 pHeader->Precision = 0;
andrewboyson 0:faa09bd4e6bf 75 pHeader->RootDelay = 0;
andrewboyson 0:faa09bd4e6bf 76 pHeader->Dispersion = 0;
andrewboyson 17:e475ab861365 77 pHeader->RefIdentifier[0] = ident[0]; //For stratum 1 (reference clock), this is a four-octet, left-justified, zero-padded ASCII string.
andrewboyson 17:e475ab861365 78 pHeader->RefIdentifier[1] = ident[1];
andrewboyson 17:e475ab861365 79 pHeader->RefIdentifier[2] = ident[2];
andrewboyson 17:e475ab861365 80 pHeader->RefIdentifier[3] = ident[3];
andrewboyson 0:faa09bd4e6bf 81
andrewboyson 17:e475ab861365 82 pHeader->RefTimeStamp = makeNtpTimeStamp(refNs);
andrewboyson 0:faa09bd4e6bf 83 pHeader->OriTimeStamp = pHeader->TraTimeStamp;
andrewboyson 17:e475ab861365 84 pHeader->RecTimeStamp = makeNtpTimeStamp(nowNs);
andrewboyson 17:e475ab861365 85 pHeader->TraTimeStamp = makeNtpTimeStamp(nowNs);
andrewboyson 7:b794780e33b4 86 *pSize = HEADER_SIZE;
andrewboyson 9:91dae5300a4d 87 return UNICAST;
andrewboyson 0:faa09bd4e6bf 88 default:
andrewboyson 4:31fa7d50722c 89 LogTimeF("\r\nNTP packet unknown\r\n");
andrewboyson 4:31fa7d50722c 90 LogTimeF("Mode %d\r\n", pHeader->Mode);
andrewboyson 4:31fa7d50722c 91 LogTimeF("Version %d\r\n", pHeader->VN);
andrewboyson 4:31fa7d50722c 92 LogTimeF("Stratum %d\r\n", pHeader->Stratum);
andrewboyson 4:31fa7d50722c 93 LogTimeF("Reference %4s\r\n", pHeader->RefIdentifier);
andrewboyson 0:faa09bd4e6bf 94 }
andrewboyson 9:91dae5300a4d 95 return DO_NOTHING;
andrewboyson 0:faa09bd4e6bf 96 }