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 Feb 04 15:27:33 2019 +0000
Revision:
121:bc048b65a630
Parent:
113:904b40231907
Child:
124:6e558721ec1c
Added fault codes to UDP and NTP modules following a hard fault.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdint.h>
andrewboyson 61:aad055f1b0d1 2 #include <stdbool.h>
andrewboyson 61:aad055f1b0d1 3
andrewboyson 121:bc048b65a630 4 #include "log.h"
andrewboyson 121:bc048b65a630 5 #include "net.h"
andrewboyson 37:793b39683406 6 #include "action.h"
andrewboyson 121:bc048b65a630 7 #include "udp.h"
andrewboyson 112:f8694d0b8858 8 #include "ntpclient.h"
andrewboyson 113:904b40231907 9 #include "ntpserver.h"
andrewboyson 112:f8694d0b8858 10 #include "clkntp.h"
andrewboyson 112:f8694d0b8858 11 #include "clktime.h"
andrewboyson 113:904b40231907 12 #include "ntp.h"
andrewboyson 121:bc048b65a630 13 #include "fault.h"
andrewboyson 22:914b970356f0 14
andrewboyson 83:08c983006a6e 15
andrewboyson 37:793b39683406 16 bool NtpTrace = false;
andrewboyson 0:faa09bd4e6bf 17
andrewboyson 113:904b40231907 18 void NtpInit()
andrewboyson 61:aad055f1b0d1 19 {
andrewboyson 113:904b40231907 20 NtpClientInit();
andrewboyson 113:904b40231907 21 }
andrewboyson 113:904b40231907 22
andrewboyson 113:904b40231907 23 void NtpLogHeader(struct NtpHeader* pHeader)
andrewboyson 37:793b39683406 24 {
andrewboyson 61:aad055f1b0d1 25 if (NetTraceVerbose) Log ("NTP header\r\n ");
andrewboyson 61:aad055f1b0d1 26 else Log ("NTP header: ");
andrewboyson 61:aad055f1b0d1 27
andrewboyson 61:aad055f1b0d1 28 LogF("Mode %d", pHeader->Mode);
andrewboyson 61:aad055f1b0d1 29 LogF(", Version %d", pHeader->VN);
andrewboyson 61:aad055f1b0d1 30 LogF(", LI %d", pHeader->LI);
andrewboyson 61:aad055f1b0d1 31 LogF(", Stratum %d", pHeader->Stratum);
andrewboyson 61:aad055f1b0d1 32 LogF(", Poll %d", pHeader->Poll);
andrewboyson 61:aad055f1b0d1 33 LogF(", Precision %d", pHeader->Precision);
andrewboyson 61:aad055f1b0d1 34 LogF(", Root delay %d", NetToHost32(pHeader->RootDelay));
andrewboyson 61:aad055f1b0d1 35 LogF(", Dispersion %d", NetToHost32(pHeader->Dispersion));
andrewboyson 61:aad055f1b0d1 36 Log (", Ident ");
andrewboyson 121:bc048b65a630 37 for (int i = 0; i < 4; i++) if (pHeader->RefIdentifier[i]) LogChar(pHeader->RefIdentifier[i]);
andrewboyson 61:aad055f1b0d1 38 Log ("\r\n");
andrewboyson 61:aad055f1b0d1 39
andrewboyson 43:bc028d5a6424 40 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 41 {
andrewboyson 43:bc028d5a6424 42 LogF(" REF %llu\r\n", NetToHost64(pHeader->RefTimeStamp));
andrewboyson 43:bc028d5a6424 43 LogF(" ORI %llu\r\n", NetToHost64(pHeader->OriTimeStamp));
andrewboyson 43:bc028d5a6424 44 LogF(" REC %llu\r\n", NetToHost64(pHeader->RecTimeStamp));
andrewboyson 43:bc028d5a6424 45 LogF(" TRA %llu\r\n", NetToHost64(pHeader->TraTimeStamp));
andrewboyson 43:bc028d5a6424 46 }
andrewboyson 37:793b39683406 47 }
andrewboyson 112:f8694d0b8858 48
andrewboyson 59:e0e556c8bd46 49 int NtpHandlePacketReceived(void (*traceback)(void), int sizeRx, void * pPacketRx, int* pSizeTx, void* pPacketTx)
andrewboyson 22:914b970356f0 50 {
andrewboyson 121:bc048b65a630 51 int lastFaultPoint = FaultPoint;
andrewboyson 121:bc048b65a630 52 FaultPoint = FAULT_POINT_NtpHandlePacketReceived;
andrewboyson 121:bc048b65a630 53
andrewboyson 113:904b40231907 54 if (sizeRx != sizeof(struct NtpHeader))
andrewboyson 22:914b970356f0 55 {
andrewboyson 59:e0e556c8bd46 56 LogTimeF("\r\nNTP packet wrong size %d\r\n", sizeRx);
andrewboyson 22:914b970356f0 57 return DO_NOTHING;
andrewboyson 22:914b970356f0 58 }
andrewboyson 113:904b40231907 59 struct NtpHeader* pHeaderRx = (struct NtpHeader*)pPacketRx;
andrewboyson 113:904b40231907 60 struct NtpHeader* pHeaderTx = (struct NtpHeader*)pPacketTx;
andrewboyson 17:e475ab861365 61
andrewboyson 59:e0e556c8bd46 62 int dest = DO_NOTHING;
andrewboyson 59:e0e556c8bd46 63 switch (pHeaderRx->Mode)
andrewboyson 0:faa09bd4e6bf 64 {
andrewboyson 121:bc048b65a630 65 case NTP_CLIENT: dest = NtpServerRequest(traceback, pHeaderRx, pHeaderTx); break;
andrewboyson 121:bc048b65a630 66 case NTP_SERVER: NtpClientReply (traceback, pHeaderRx); break;
andrewboyson 121:bc048b65a630 67 default: LogTimeF("\r\nNTP packet unknown mode %d\r\n", pHeaderRx->Mode); break;
andrewboyson 0:faa09bd4e6bf 68 }
andrewboyson 59:e0e556c8bd46 69
andrewboyson 121:bc048b65a630 70 FaultPoint = lastFaultPoint;
andrewboyson 121:bc048b65a630 71 if (dest == DO_NOTHING)
andrewboyson 121:bc048b65a630 72 {
andrewboyson 121:bc048b65a630 73 return DO_NOTHING;
andrewboyson 121:bc048b65a630 74 }
andrewboyson 121:bc048b65a630 75 else
andrewboyson 121:bc048b65a630 76 {
andrewboyson 121:bc048b65a630 77 *pSizeTx = sizeof(struct NtpHeader);
andrewboyson 121:bc048b65a630 78 return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack);
andrewboyson 121:bc048b65a630 79 }
andrewboyson 22:914b970356f0 80 }
andrewboyson 112:f8694d0b8858 81 int NtpPollForPacketToSend(int type, void* pPacket, int* pSize)
andrewboyson 112:f8694d0b8858 82 {
andrewboyson 113:904b40231907 83 int dest = NtpClientQueryPoll(type, pPacket, pSize);
andrewboyson 112:f8694d0b8858 84 if (!dest) return DO_NOTHING;
andrewboyson 36:900e24b27bfb 85
andrewboyson 42:222a4f45f916 86 if (NtpTrace)
andrewboyson 37:793b39683406 87 {
andrewboyson 43:bc028d5a6424 88 if (NetTraceNewLine) Log("\r\n");
andrewboyson 37:793b39683406 89 LogTimeF("Sending NTP request\r\n");
andrewboyson 113:904b40231907 90 NtpLogHeader((struct NtpHeader*)pPacket);
andrewboyson 37:793b39683406 91 }
andrewboyson 43:bc028d5a6424 92 return ActionMakeFromDestAndTrace(dest, NtpTrace && NetTraceStack);
andrewboyson 112:f8694d0b8858 93
andrewboyson 112:f8694d0b8858 94
andrewboyson 22:914b970356f0 95 }
andrewboyson 22:914b970356f0 96