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 21:37:14 2017 +0000
Revision:
20:23f2b29b48ea
Parent:
19:43c70b424fc1
Child:
22:914b970356f0
Moved translation from clock ns to ntp timestamp from the net module to the ntp server module.

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 7:b794780e33b4 37 int NtpHandleRequest(int* pSize, void * pPacket)
andrewboyson 0:faa09bd4e6bf 38 {
andrewboyson 17:e475ab861365 39 if (!NtpEnableServer) return DO_NOTHING;
andrewboyson 17:e475ab861365 40
andrewboyson 17:e475ab861365 41 if (!NtpGetClockRefFunction || !NtpGetClockNowFunction || !NtpGetClockStratumFunction || !NtpGetClockIdentFunction)
andrewboyson 17:e475ab861365 42 {
andrewboyson 17:e475ab861365 43 LogTimeF("NtpHandleRequest - NTP server is enabled but has not been plumbed into a clock\r\n");
andrewboyson 17:e475ab861365 44 return DO_NOTHING;
andrewboyson 17:e475ab861365 45 }
andrewboyson 17:e475ab861365 46
andrewboyson 9:91dae5300a4d 47 if (*pSize < HEADER_SIZE) return DO_NOTHING;
andrewboyson 0:faa09bd4e6bf 48
andrewboyson 0:faa09bd4e6bf 49 struct header* pHeader = (struct header*)pPacket;
andrewboyson 0:faa09bd4e6bf 50
andrewboyson 20:23f2b29b48ea 51 uint64_t refNtp = NtpGetClockRefFunction();
andrewboyson 20:23f2b29b48ea 52 uint64_t nowNtp = NtpGetClockNowFunction();
andrewboyson 20:23f2b29b48ea 53 int stratum = NtpGetClockStratumFunction();
andrewboyson 20:23f2b29b48ea 54 char* ident = NtpGetClockIdentFunction();
andrewboyson 17:e475ab861365 55
andrewboyson 0:faa09bd4e6bf 56 switch (pHeader->Mode)
andrewboyson 0:faa09bd4e6bf 57 {
andrewboyson 0:faa09bd4e6bf 58 case CLIENT:
andrewboyson 0:faa09bd4e6bf 59 pHeader->Mode = SERVER;
andrewboyson 0:faa09bd4e6bf 60 pHeader->LI = 0;
andrewboyson 17:e475ab861365 61 pHeader->Stratum = stratum;
andrewboyson 0:faa09bd4e6bf 62 pHeader->Poll = 0;
andrewboyson 0:faa09bd4e6bf 63 pHeader->Precision = 0;
andrewboyson 0:faa09bd4e6bf 64 pHeader->RootDelay = 0;
andrewboyson 0:faa09bd4e6bf 65 pHeader->Dispersion = 0;
andrewboyson 17:e475ab861365 66 pHeader->RefIdentifier[0] = ident[0]; //For stratum 1 (reference clock), this is a four-octet, left-justified, zero-padded ASCII string.
andrewboyson 17:e475ab861365 67 pHeader->RefIdentifier[1] = ident[1];
andrewboyson 17:e475ab861365 68 pHeader->RefIdentifier[2] = ident[2];
andrewboyson 17:e475ab861365 69 pHeader->RefIdentifier[3] = ident[3];
andrewboyson 0:faa09bd4e6bf 70
andrewboyson 20:23f2b29b48ea 71 pHeader->RefTimeStamp = NetToHost64(refNtp);
andrewboyson 0:faa09bd4e6bf 72 pHeader->OriTimeStamp = pHeader->TraTimeStamp;
andrewboyson 20:23f2b29b48ea 73 pHeader->RecTimeStamp = NetToHost64(nowNtp);
andrewboyson 20:23f2b29b48ea 74 pHeader->TraTimeStamp = NetToHost64(nowNtp);
andrewboyson 7:b794780e33b4 75 *pSize = HEADER_SIZE;
andrewboyson 9:91dae5300a4d 76 return UNICAST;
andrewboyson 0:faa09bd4e6bf 77 default:
andrewboyson 4:31fa7d50722c 78 LogTimeF("\r\nNTP packet unknown\r\n");
andrewboyson 4:31fa7d50722c 79 LogTimeF("Mode %d\r\n", pHeader->Mode);
andrewboyson 4:31fa7d50722c 80 LogTimeF("Version %d\r\n", pHeader->VN);
andrewboyson 4:31fa7d50722c 81 LogTimeF("Stratum %d\r\n", pHeader->Stratum);
andrewboyson 4:31fa7d50722c 82 LogTimeF("Reference %4s\r\n", pHeader->RefIdentifier);
andrewboyson 0:faa09bd4e6bf 83 }
andrewboyson 9:91dae5300a4d 84 return DO_NOTHING;
andrewboyson 0:faa09bd4e6bf 85 }