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:
Wed Feb 01 07:41:03 2017 +0000
Revision:
4:31fa7d50722c
Parent:
1:5f70c361db20
Child:
7:b794780e33b4
Added log library and removed printfs to the PC uart

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 1:5f70c361db20 4 #include "clock.h"
andrewboyson 1:5f70c361db20 5 #include "sync.h"
andrewboyson 0:faa09bd4e6bf 6
andrewboyson 0:faa09bd4e6bf 7 #define HEADER_SIZE 48
andrewboyson 0:faa09bd4e6bf 8
andrewboyson 0:faa09bd4e6bf 9 #define ERA_BASE 0
andrewboyson 0:faa09bd4e6bf 10 #define ERA_PIVOT 2016
andrewboyson 0:faa09bd4e6bf 11
andrewboyson 0:faa09bd4e6bf 12 #define CLIENT 3
andrewboyson 0:faa09bd4e6bf 13 #define SERVER 4
andrewboyson 0:faa09bd4e6bf 14
andrewboyson 0:faa09bd4e6bf 15 __packed struct header {
andrewboyson 0:faa09bd4e6bf 16 unsigned Mode : 3;
andrewboyson 0:faa09bd4e6bf 17 unsigned VN : 3;
andrewboyson 0:faa09bd4e6bf 18 unsigned LI : 2;
andrewboyson 0:faa09bd4e6bf 19 uint8_t Stratum;
andrewboyson 0:faa09bd4e6bf 20 int8_t Poll;
andrewboyson 0:faa09bd4e6bf 21 int8_t Precision;
andrewboyson 0:faa09bd4e6bf 22 uint32_t RootDelay;
andrewboyson 0:faa09bd4e6bf 23 uint32_t Dispersion;
andrewboyson 0:faa09bd4e6bf 24 char RefIdentifier[4];
andrewboyson 0:faa09bd4e6bf 25
andrewboyson 0:faa09bd4e6bf 26 uint64_t RefTimeStamp;
andrewboyson 0:faa09bd4e6bf 27 uint64_t OriTimeStamp;
andrewboyson 0:faa09bd4e6bf 28 uint64_t RecTimeStamp;
andrewboyson 0:faa09bd4e6bf 29 uint64_t TraTimeStamp;
andrewboyson 0:faa09bd4e6bf 30 };
andrewboyson 1:5f70c361db20 31 uint64_t makeNtpTimeStamp(int64_t ns)
andrewboyson 1:5f70c361db20 32 {
andrewboyson 1:5f70c361db20 33 uint64_t timestamp = ns << 2;
andrewboyson 1:5f70c361db20 34 timestamp /= 1000; timestamp <<= 10;
andrewboyson 1:5f70c361db20 35 timestamp /= 1000; timestamp <<= 10;
andrewboyson 1:5f70c361db20 36 timestamp /= 1000; timestamp <<= 10;
andrewboyson 0:faa09bd4e6bf 37
andrewboyson 1:5f70c361db20 38 timestamp += 2208988800ULL << 32;
andrewboyson 0:faa09bd4e6bf 39
andrewboyson 1:5f70c361db20 40 return NetToHost64(timestamp);
andrewboyson 0:faa09bd4e6bf 41 }
andrewboyson 0:faa09bd4e6bf 42 int NtpHandleRequest(int size, void * pPacket)
andrewboyson 0:faa09bd4e6bf 43 {
andrewboyson 0:faa09bd4e6bf 44 if (size < HEADER_SIZE) return 0;
andrewboyson 0:faa09bd4e6bf 45
andrewboyson 0:faa09bd4e6bf 46 struct header* pHeader = (struct header*)pPacket;
andrewboyson 0:faa09bd4e6bf 47
andrewboyson 0:faa09bd4e6bf 48 switch (pHeader->Mode)
andrewboyson 0:faa09bd4e6bf 49 {
andrewboyson 0:faa09bd4e6bf 50 case CLIENT:
andrewboyson 0:faa09bd4e6bf 51 pHeader->Mode = SERVER;
andrewboyson 0:faa09bd4e6bf 52 pHeader->LI = 0;
andrewboyson 1:5f70c361db20 53 if (ClockStartNs) pHeader->Stratum = 1;
andrewboyson 0:faa09bd4e6bf 54 else pHeader->Stratum = 0;
andrewboyson 0:faa09bd4e6bf 55 pHeader->Poll = 0;
andrewboyson 0:faa09bd4e6bf 56 pHeader->Precision = 0;
andrewboyson 0:faa09bd4e6bf 57 pHeader->RootDelay = 0;
andrewboyson 0:faa09bd4e6bf 58 pHeader->Dispersion = 0;
andrewboyson 1:5f70c361db20 59 char* pRef;
andrewboyson 1:5f70c361db20 60 if (ClockStartNs)
andrewboyson 0:faa09bd4e6bf 61 {
andrewboyson 1:5f70c361db20 62 if (SyncedRate && SyncedTime) pRef = "GPS";
andrewboyson 1:5f70c361db20 63 else pRef = "LOCL";
andrewboyson 0:faa09bd4e6bf 64 }
andrewboyson 0:faa09bd4e6bf 65 else
andrewboyson 0:faa09bd4e6bf 66 {
andrewboyson 1:5f70c361db20 67 pRef = "INIT";
andrewboyson 0:faa09bd4e6bf 68 }
andrewboyson 1:5f70c361db20 69 pHeader->RefIdentifier[0] = pRef[0]; //For stratum 1 (reference clock), this is a four-octet, left-justified, zero-padded ASCII string.
andrewboyson 1:5f70c361db20 70 pHeader->RefIdentifier[1] = pRef[1];
andrewboyson 1:5f70c361db20 71 pHeader->RefIdentifier[2] = pRef[2];
andrewboyson 1:5f70c361db20 72 pHeader->RefIdentifier[3] = pRef[3];
andrewboyson 0:faa09bd4e6bf 73
andrewboyson 1:5f70c361db20 74 pHeader->RefTimeStamp = makeNtpTimeStamp(ClockStartNs);
andrewboyson 0:faa09bd4e6bf 75 pHeader->OriTimeStamp = pHeader->TraTimeStamp;
andrewboyson 1:5f70c361db20 76 pHeader->RecTimeStamp = makeNtpTimeStamp(ClockGetNs());
andrewboyson 1:5f70c361db20 77 pHeader->TraTimeStamp = makeNtpTimeStamp(ClockGetNs());
andrewboyson 0:faa09bd4e6bf 78 return HEADER_SIZE;
andrewboyson 0:faa09bd4e6bf 79 default:
andrewboyson 4:31fa7d50722c 80 LogTimeF("\r\nNTP packet unknown\r\n");
andrewboyson 4:31fa7d50722c 81 LogTimeF("Mode %d\r\n", pHeader->Mode);
andrewboyson 4:31fa7d50722c 82 LogTimeF("Version %d\r\n", pHeader->VN);
andrewboyson 4:31fa7d50722c 83 LogTimeF("Stratum %d\r\n", pHeader->Stratum);
andrewboyson 4:31fa7d50722c 84 LogTimeF("Reference %4s\r\n", pHeader->RefIdentifier);
andrewboyson 0:faa09bd4e6bf 85 }
andrewboyson 0:faa09bd4e6bf 86 return 0;
andrewboyson 0:faa09bd4e6bf 87 }