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
Diff: udp/ntp/ntp.c
- Revision:
- 113:904b40231907
- Parent:
- 112:f8694d0b8858
- Child:
- 121:bc048b65a630
--- a/udp/ntp/ntp.c Mon Jan 21 16:58:28 2019 +0000 +++ b/udp/ntp/ntp.c Tue Jan 22 15:41:12 2019 +0000 @@ -6,41 +6,20 @@ #include "action.h" #include "udp.h" #include "ntpclient.h" +#include "ntpserver.h" #include "clkntp.h" #include "clktime.h" +#include "ntp.h" bool NtpTrace = false; -#define CLIENT 3 -#define SERVER 4 - -bool NtpServerEnable = false; -uint64_t (*NtpFunctionGetClockRef) (); -int (*NtpFunctionGetClockStratum) (); -char * (*NtpFunctionGetClockIdent) (); -int (*NtpFunctionGetClockLi) (); -int (*NtpFunctionGetClockPrecision)(); - - -__packed struct header +void NtpInit() { - unsigned Mode : 3; - unsigned VN : 3; - unsigned LI : 2; - uint8_t Stratum; - int8_t Poll; - int8_t Precision; - uint32_t RootDelay; - uint32_t Dispersion; - char RefIdentifier[4]; - - uint64_t RefTimeStamp; - uint64_t OriTimeStamp; - uint64_t RecTimeStamp; - uint64_t TraTimeStamp; -}; -static void logHeader(struct header* pHeader) + NtpClientInit(); +} + +void NtpLogHeader(struct NtpHeader* pHeader) { if (NetTraceVerbose) Log ("NTP header\r\n "); else Log ("NTP header: "); @@ -65,133 +44,42 @@ LogF(" TRA %llu\r\n", NetToHost64(pHeader->TraTimeStamp)); } } -static void (*pTraceBack)(void); -static int handleRequest(struct header* pHeaderRx, struct header* pHeaderTx) -{ - if (!NtpServerEnable) return DO_NOTHING; - - if (!NtpFunctionGetClockRef || - !NtpFunctionGetClockStratum || - !NtpFunctionGetClockIdent || - !NtpFunctionGetClockLi || - !NtpFunctionGetClockPrecision) - { - LogTimeF("NtpHandleRequest - NTP server is enabled but has not been plumbed into a clock\r\n"); - return DO_NOTHING; - } - - if (NtpTrace) - { - if (NetTraceNewLine) Log("\r\n"); - LogTimeF("NTP received request\r\n"); - if (NetTraceStack) pTraceBack(); - logHeader(pHeaderRx); - } - - uint64_t refNtp = NtpFunctionGetClockRef(); - uint64_t nowNtp = ClkTimeToNtpTimeStamp(ClkTimeGet()); - int stratum = NtpFunctionGetClockStratum(); - char* ident = NtpFunctionGetClockIdent(); - int li = NtpFunctionGetClockLi(); - int precision = NtpFunctionGetClockPrecision(); - - pHeaderTx->Mode = SERVER; - pHeaderTx->VN = pHeaderRx->VN; - pHeaderTx->LI = li; - pHeaderTx->Stratum = stratum; - pHeaderTx->Poll = pHeaderRx->Poll; - pHeaderTx->Precision = precision; - pHeaderTx->RootDelay = 0; - pHeaderTx->Dispersion = 0; - pHeaderTx->RefIdentifier[0] = ident[0]; //For stratum 1 (reference clock), this is a four-octet, left-justified, zero-padded ASCII string. - pHeaderTx->RefIdentifier[1] = ident[1]; - pHeaderTx->RefIdentifier[2] = ident[2]; - pHeaderTx->RefIdentifier[3] = ident[3]; - pHeaderTx->RefTimeStamp = NetToHost64(refNtp); - pHeaderTx->OriTimeStamp = pHeaderRx->TraTimeStamp; - pHeaderTx->RecTimeStamp = NetToHost64(nowNtp); - pHeaderTx->TraTimeStamp = NetToHost64(nowNtp); - - if (NtpTrace) logHeader(pHeaderTx); - return UNICAST; -} -static void handleReply(struct header* pHeader) -{ - if (NtpTrace) - { - if (NetTraceNewLine) Log("\r\n"); - LogTimeF("NTP received reply\r\n"); - if (NetTraceStack) pTraceBack(); - logHeader(pHeader); - } - - uint64_t ori = NetToHost64(pHeader->OriTimeStamp); - uint64_t rec = NetToHost64(pHeader->RecTimeStamp); - int li = pHeader->LI; - - NtpClientSetClockTime(ori, rec, li); -} -void NtpHdrWriteRequest(void* pPacket, int* pSize) -{ - struct header* pHeader = (struct header*)pPacket; - - pHeader->Mode = CLIENT; - pHeader->VN = 3; - pHeader->LI = 0; - pHeader->Stratum = 0; - pHeader->Poll = 0; - pHeader->Precision = 0; - pHeader->RootDelay = 0; - pHeader->Dispersion = 0; - pHeader->RefIdentifier[0] = 0; - pHeader->RefIdentifier[1] = 0; - pHeader->RefIdentifier[2] = 0; - pHeader->RefIdentifier[3] = 0; - pHeader->RefTimeStamp = 0; - pHeader->OriTimeStamp = 0; - pHeader->RecTimeStamp = 0; - pHeader->TraTimeStamp = NetToHost64(ClkTimeToNtpTimeStamp(ClkTimeGet())); - - *pSize = sizeof(struct header); -} int NtpHandlePacketReceived(void (*traceback)(void), int sizeRx, void * pPacketRx, int* pSizeTx, void* pPacketTx) { - pTraceBack = traceback; - - if (sizeRx != sizeof(struct header)) + if (sizeRx != sizeof(struct NtpHeader)) { LogTimeF("\r\nNTP packet wrong size %d\r\n", sizeRx); return DO_NOTHING; } - struct header* pHeaderRx = (struct header*)pPacketRx; - struct header* pHeaderTx = (struct header*)pPacketTx; + struct NtpHeader* pHeaderRx = (struct NtpHeader*)pPacketRx; + struct NtpHeader* pHeaderTx = (struct NtpHeader*)pPacketTx; int dest = DO_NOTHING; switch (pHeaderRx->Mode) { - case CLIENT: dest = handleRequest(pHeaderRx, pHeaderTx); break; - case SERVER: handleReply (pHeaderRx); return DO_NOTHING; + case NTP_CLIENT: dest = NtpServerRequest(traceback, pHeaderRx, pHeaderTx); break; + case NTP_SERVER: NtpClientReply (traceback, pHeaderRx); return DO_NOTHING; default: LogTimeF("\r\nNTP packet unknown mode %d\r\n", pHeaderRx->Mode); return DO_NOTHING; } if (dest == DO_NOTHING) return DO_NOTHING; - *pSizeTx = sizeof(struct header); + *pSizeTx = sizeof(struct NtpHeader); return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack); } int NtpPollForPacketToSend(int type, void* pPacket, int* pSize) { - int dest = NtpClientPollForPacketToSend(type, pPacket, pSize); + int dest = NtpClientQueryPoll(type, pPacket, pSize); if (!dest) return DO_NOTHING; if (NtpTrace) { if (NetTraceNewLine) Log("\r\n"); LogTimeF("Sending NTP request\r\n"); - logHeader((struct header*)pPacket); + NtpLogHeader((struct NtpHeader*)pPacket); } return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack);