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
ntp.cpp@0:faa09bd4e6bf, 2017-01-09 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon Jan 09 13:29:41 2017 +0000
- Revision:
- 0:faa09bd4e6bf
- Child:
- 1:5f70c361db20
arp, ping udp and ntp are working. TCP started.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:faa09bd4e6bf | 1 | #include "mbed.h" |
andrewboyson | 0:faa09bd4e6bf | 2 | #include "net.h" |
andrewboyson | 0:faa09bd4e6bf | 3 | #include "gps.h" |
andrewboyson | 0:faa09bd4e6bf | 4 | |
andrewboyson | 0:faa09bd4e6bf | 5 | #define HEADER_SIZE 48 |
andrewboyson | 0:faa09bd4e6bf | 6 | |
andrewboyson | 0:faa09bd4e6bf | 7 | #define ERA_BASE 0 |
andrewboyson | 0:faa09bd4e6bf | 8 | #define ERA_PIVOT 2016 |
andrewboyson | 0:faa09bd4e6bf | 9 | |
andrewboyson | 0:faa09bd4e6bf | 10 | #define CLIENT 3 |
andrewboyson | 0:faa09bd4e6bf | 11 | #define SERVER 4 |
andrewboyson | 0:faa09bd4e6bf | 12 | |
andrewboyson | 0:faa09bd4e6bf | 13 | __packed struct header { |
andrewboyson | 0:faa09bd4e6bf | 14 | unsigned Mode : 3; |
andrewboyson | 0:faa09bd4e6bf | 15 | unsigned VN : 3; |
andrewboyson | 0:faa09bd4e6bf | 16 | unsigned LI : 2; |
andrewboyson | 0:faa09bd4e6bf | 17 | uint8_t Stratum; |
andrewboyson | 0:faa09bd4e6bf | 18 | int8_t Poll; |
andrewboyson | 0:faa09bd4e6bf | 19 | int8_t Precision; |
andrewboyson | 0:faa09bd4e6bf | 20 | uint32_t RootDelay; |
andrewboyson | 0:faa09bd4e6bf | 21 | uint32_t Dispersion; |
andrewboyson | 0:faa09bd4e6bf | 22 | char RefIdentifier[4]; |
andrewboyson | 0:faa09bd4e6bf | 23 | |
andrewboyson | 0:faa09bd4e6bf | 24 | uint64_t RefTimeStamp; |
andrewboyson | 0:faa09bd4e6bf | 25 | uint64_t OriTimeStamp; |
andrewboyson | 0:faa09bd4e6bf | 26 | uint64_t RecTimeStamp; |
andrewboyson | 0:faa09bd4e6bf | 27 | uint64_t TraTimeStamp; |
andrewboyson | 0:faa09bd4e6bf | 28 | }; |
andrewboyson | 0:faa09bd4e6bf | 29 | uint64_t makeNtpTimeStamp(time_t seconds, int ms) |
andrewboyson | 0:faa09bd4e6bf | 30 | { |
andrewboyson | 0:faa09bd4e6bf | 31 | uint64_t timestampMs = ms; |
andrewboyson | 0:faa09bd4e6bf | 32 | timestampMs <<= 32; |
andrewboyson | 0:faa09bd4e6bf | 33 | timestampMs /= 1000; |
andrewboyson | 0:faa09bd4e6bf | 34 | |
andrewboyson | 0:faa09bd4e6bf | 35 | uint64_t timestampS = seconds; |
andrewboyson | 0:faa09bd4e6bf | 36 | timestampS += 2208988800ULL; //Add 1970 - 1900 to align the epochs |
andrewboyson | 0:faa09bd4e6bf | 37 | timestampS <<= 32; //put the seconds into the upper 32 bits |
andrewboyson | 0:faa09bd4e6bf | 38 | |
andrewboyson | 0:faa09bd4e6bf | 39 | return NetToHost64(timestampS + timestampMs); |
andrewboyson | 0:faa09bd4e6bf | 40 | } |
andrewboyson | 0:faa09bd4e6bf | 41 | int NtpHandleRequest(int size, void * pPacket) |
andrewboyson | 0:faa09bd4e6bf | 42 | { |
andrewboyson | 0:faa09bd4e6bf | 43 | if (size < HEADER_SIZE) return 0; |
andrewboyson | 0:faa09bd4e6bf | 44 | |
andrewboyson | 0:faa09bd4e6bf | 45 | struct header* pHeader = (struct header*)pPacket; |
andrewboyson | 0:faa09bd4e6bf | 46 | |
andrewboyson | 0:faa09bd4e6bf | 47 | switch (pHeader->Mode) |
andrewboyson | 0:faa09bd4e6bf | 48 | { |
andrewboyson | 0:faa09bd4e6bf | 49 | case CLIENT: |
andrewboyson | 0:faa09bd4e6bf | 50 | pHeader->Mode = SERVER; |
andrewboyson | 0:faa09bd4e6bf | 51 | pHeader->LI = 0; |
andrewboyson | 0:faa09bd4e6bf | 52 | if (GpsStartTime) pHeader->Stratum = 1; |
andrewboyson | 0:faa09bd4e6bf | 53 | else pHeader->Stratum = 0; |
andrewboyson | 0:faa09bd4e6bf | 54 | pHeader->Poll = 0; |
andrewboyson | 0:faa09bd4e6bf | 55 | pHeader->Precision = 0; |
andrewboyson | 0:faa09bd4e6bf | 56 | pHeader->RootDelay = 0; |
andrewboyson | 0:faa09bd4e6bf | 57 | pHeader->Dispersion = 0; |
andrewboyson | 0:faa09bd4e6bf | 58 | if (GpsStartTime) |
andrewboyson | 0:faa09bd4e6bf | 59 | { |
andrewboyson | 0:faa09bd4e6bf | 60 | pHeader->RefIdentifier[0] = 'G'; //For stratum 1 (reference clock), this is a four-octet, left-justified, zero-padded ASCII string. |
andrewboyson | 0:faa09bd4e6bf | 61 | pHeader->RefIdentifier[1] = 'P'; |
andrewboyson | 0:faa09bd4e6bf | 62 | pHeader->RefIdentifier[2] = 'S'; |
andrewboyson | 0:faa09bd4e6bf | 63 | pHeader->RefIdentifier[3] = 0 ; |
andrewboyson | 0:faa09bd4e6bf | 64 | } |
andrewboyson | 0:faa09bd4e6bf | 65 | else |
andrewboyson | 0:faa09bd4e6bf | 66 | { |
andrewboyson | 0:faa09bd4e6bf | 67 | pHeader->RefIdentifier[0] = 'I'; //For stratum 1 (reference clock), this is a four-octet, left-justified, zero-padded ASCII string. |
andrewboyson | 0:faa09bd4e6bf | 68 | pHeader->RefIdentifier[1] = 'N'; |
andrewboyson | 0:faa09bd4e6bf | 69 | pHeader->RefIdentifier[2] = 'I'; |
andrewboyson | 0:faa09bd4e6bf | 70 | pHeader->RefIdentifier[3] = 'T' ; |
andrewboyson | 0:faa09bd4e6bf | 71 | } |
andrewboyson | 0:faa09bd4e6bf | 72 | |
andrewboyson | 0:faa09bd4e6bf | 73 | pHeader->RefTimeStamp = makeNtpTimeStamp(GpsStartTime, GpsStartMs); |
andrewboyson | 0:faa09bd4e6bf | 74 | pHeader->OriTimeStamp = pHeader->TraTimeStamp; |
andrewboyson | 0:faa09bd4e6bf | 75 | pHeader->RecTimeStamp = makeNtpTimeStamp(GpsTime, GpsMs); |
andrewboyson | 0:faa09bd4e6bf | 76 | pHeader->TraTimeStamp = makeNtpTimeStamp(GpsTime, GpsMs); |
andrewboyson | 0:faa09bd4e6bf | 77 | return HEADER_SIZE; |
andrewboyson | 0:faa09bd4e6bf | 78 | default: |
andrewboyson | 0:faa09bd4e6bf | 79 | printf("\r\nNTP packet unknown\r\n"); |
andrewboyson | 0:faa09bd4e6bf | 80 | printf("Mode %d\r\n", pHeader->Mode); |
andrewboyson | 0:faa09bd4e6bf | 81 | printf("Version %d\r\n", pHeader->VN); |
andrewboyson | 0:faa09bd4e6bf | 82 | printf("Stratum %d\r\n", pHeader->Stratum); |
andrewboyson | 0:faa09bd4e6bf | 83 | printf("Reference %4s\r\n", pHeader->RefIdentifier); |
andrewboyson | 0:faa09bd4e6bf | 84 | } |
andrewboyson | 0:faa09bd4e6bf | 85 | return 0; |
andrewboyson | 0:faa09bd4e6bf | 86 | } |