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 Oct 04 07:51:02 2017 +0000
Revision:
37:793b39683406
Parent:
35:93c39d260a83
Child:
42:222a4f45f916
Added trace back and trace forward to log messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 7:b794780e33b4 1 #include "mbed.h"
andrewboyson 7:b794780e33b4 2 #include "phy.h"
andrewboyson 7:b794780e33b4 3 #include "net.h"
andrewboyson 7:b794780e33b4 4 #include "tcp.h"
andrewboyson 7:b794780e33b4 5 #include "dhcp.h"
andrewboyson 9:91dae5300a4d 6 #include "ar.h"
andrewboyson 35:93c39d260a83 7 #include "nr.h"
andrewboyson 7:b794780e33b4 8 #include "dns.h"
andrewboyson 7:b794780e33b4 9 #include "slaac.h"
andrewboyson 29:39277bf2003d 10 #include "ra.h"
andrewboyson 22:914b970356f0 11 #include "ntp.h"
andrewboyson 22:914b970356f0 12 #include "io.h"
andrewboyson 22:914b970356f0 13 #include "clock.h"
andrewboyson 2:849103b5a16d 14
andrewboyson 37:793b39683406 15
andrewboyson 37:793b39683406 16 bool NetTraceBack = false;
andrewboyson 37:793b39683406 17 bool NetTraceForward = false;
andrewboyson 37:793b39683406 18
andrewboyson 10:f0854784e960 19 char* NetName;
andrewboyson 10:f0854784e960 20
andrewboyson 0:faa09bd4e6bf 21 int16_t NetToHost16(int16_t n)
andrewboyson 0:faa09bd4e6bf 22 {
andrewboyson 0:faa09bd4e6bf 23 int16_t h;
andrewboyson 0:faa09bd4e6bf 24
andrewboyson 0:faa09bd4e6bf 25 char* pn = (char*)&n;
andrewboyson 0:faa09bd4e6bf 26 char* ph = (char*)&h + 1;
andrewboyson 0:faa09bd4e6bf 27
andrewboyson 0:faa09bd4e6bf 28 *ph = *pn; ph--; pn++; // 1<-0
andrewboyson 0:faa09bd4e6bf 29 *ph = *pn; // 0<-1
andrewboyson 0:faa09bd4e6bf 30
andrewboyson 0:faa09bd4e6bf 31 return h;
andrewboyson 0:faa09bd4e6bf 32 }
andrewboyson 0:faa09bd4e6bf 33
andrewboyson 0:faa09bd4e6bf 34 int32_t NetToHost32(int32_t n)
andrewboyson 0:faa09bd4e6bf 35 {
andrewboyson 0:faa09bd4e6bf 36 int32_t h;
andrewboyson 0:faa09bd4e6bf 37
andrewboyson 0:faa09bd4e6bf 38 char* pn = (char*)&n;
andrewboyson 0:faa09bd4e6bf 39 char* ph = (char*)&h + 3;
andrewboyson 0:faa09bd4e6bf 40
andrewboyson 0:faa09bd4e6bf 41 *ph = *pn; ph--; pn++; // 3<-0
andrewboyson 0:faa09bd4e6bf 42 *ph = *pn; ph--; pn++; // 2<-1
andrewboyson 0:faa09bd4e6bf 43 *ph = *pn; ph--; pn++; // 1<-2
andrewboyson 0:faa09bd4e6bf 44 *ph = *pn; // 0<-3
andrewboyson 0:faa09bd4e6bf 45
andrewboyson 0:faa09bd4e6bf 46 return h;
andrewboyson 0:faa09bd4e6bf 47 }
andrewboyson 0:faa09bd4e6bf 48 int64_t NetToHost64(int64_t n)
andrewboyson 0:faa09bd4e6bf 49 {
andrewboyson 0:faa09bd4e6bf 50 int64_t h;
andrewboyson 0:faa09bd4e6bf 51
andrewboyson 0:faa09bd4e6bf 52 char* pn = (char*)&n;
andrewboyson 0:faa09bd4e6bf 53 char* ph = (char*)&h + 7;
andrewboyson 0:faa09bd4e6bf 54
andrewboyson 0:faa09bd4e6bf 55 *ph = *pn; ph--; pn++; // 7<-0
andrewboyson 0:faa09bd4e6bf 56 *ph = *pn; ph--; pn++; // 6<-1
andrewboyson 0:faa09bd4e6bf 57 *ph = *pn; ph--; pn++; // 5<-2
andrewboyson 0:faa09bd4e6bf 58 *ph = *pn; ph--; pn++; // 4<-3
andrewboyson 0:faa09bd4e6bf 59 *ph = *pn; ph--; pn++; // 3<-4
andrewboyson 0:faa09bd4e6bf 60 *ph = *pn; ph--; pn++; // 2<-5
andrewboyson 0:faa09bd4e6bf 61 *ph = *pn; ph--; pn++; // 1<-6
andrewboyson 0:faa09bd4e6bf 62 *ph = *pn; // 0<-7
andrewboyson 0:faa09bd4e6bf 63
andrewboyson 0:faa09bd4e6bf 64 return h;
andrewboyson 0:faa09bd4e6bf 65 }
andrewboyson 0:faa09bd4e6bf 66 uint16_t onesComplement(uint16_t start, int count, void* pData)
andrewboyson 0:faa09bd4e6bf 67 {
andrewboyson 0:faa09bd4e6bf 68 uint32_t sum = start; //Initialise the 32 bit accumulator with the last sum
andrewboyson 0:faa09bd4e6bf 69 uint16_t* p = (uint16_t*)pData; //Set up a 16 bit pointer for the data
andrewboyson 0:faa09bd4e6bf 70
andrewboyson 0:faa09bd4e6bf 71 while(count > 1)
andrewboyson 0:faa09bd4e6bf 72 {
andrewboyson 0:faa09bd4e6bf 73 sum += *p++; // Add each pair of bytes into 32 bit accumulator
andrewboyson 0:faa09bd4e6bf 74 count -= 2;
andrewboyson 0:faa09bd4e6bf 75 }
andrewboyson 0:faa09bd4e6bf 76 if(count) sum += * (uint8_t*) p; // Add left-over byte, if any
andrewboyson 0:faa09bd4e6bf 77 while (sum>>16) sum = (sum & 0xffff) + (sum >> 16); // Add any carries from the sum back into the sum to make it ones complement
andrewboyson 0:faa09bd4e6bf 78 return sum;
andrewboyson 0:faa09bd4e6bf 79 }
andrewboyson 0:faa09bd4e6bf 80 uint16_t NetCheckSumTwo(int count1, void* pData1, int count2, void* pData2)
andrewboyson 0:faa09bd4e6bf 81 {
andrewboyson 0:faa09bd4e6bf 82 uint16_t sum = onesComplement(0, count1, pData1);
andrewboyson 0:faa09bd4e6bf 83 return ~onesComplement(sum, count2, pData2);
andrewboyson 0:faa09bd4e6bf 84 }
andrewboyson 0:faa09bd4e6bf 85 uint16_t NetCheckSum(int count, void* pData)
andrewboyson 0:faa09bd4e6bf 86 {
andrewboyson 0:faa09bd4e6bf 87 return ~onesComplement(0, count, pData);
andrewboyson 0:faa09bd4e6bf 88 }
andrewboyson 2:849103b5a16d 89
andrewboyson 10:f0854784e960 90 int NetInit(char* name)
andrewboyson 2:849103b5a16d 91 {
andrewboyson 10:f0854784e960 92 NetName = name;
andrewboyson 30:e34173b7585c 93 PhyInit();
andrewboyson 30:e34173b7585c 94 TcpInit();
andrewboyson 30:e34173b7585c 95 ArInit();
andrewboyson 35:93c39d260a83 96 NrInit();
andrewboyson 30:e34173b7585c 97 SlaacInit();
andrewboyson 2:849103b5a16d 98 return 0;
andrewboyson 2:849103b5a16d 99 }
andrewboyson 2:849103b5a16d 100 int NetMain()
andrewboyson 2:849103b5a16d 101 {
andrewboyson 35:93c39d260a83 102 PhyMain();
andrewboyson 35:93c39d260a83 103 ArMain();
andrewboyson 35:93c39d260a83 104 NrMain();
andrewboyson 23:b641979389b2 105 if (ClockTicked)
andrewboyson 2:849103b5a16d 106 {
andrewboyson 35:93c39d260a83 107 DhcpTick();
andrewboyson 35:93c39d260a83 108 ArTick();
andrewboyson 35:93c39d260a83 109 NrTick();
andrewboyson 35:93c39d260a83 110 DnsTick();
andrewboyson 35:93c39d260a83 111 RaTick();
andrewboyson 2:849103b5a16d 112 }
andrewboyson 2:849103b5a16d 113 return 0;
andrewboyson 2:849103b5a16d 114 }