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
net.cpp@46:40d33e9037e4, 2017-10-24 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue Oct 24 07:01:35 2017 +0000
- Revision:
- 46:40d33e9037e4
- Parent:
- 44:83ce5ace337b
- Child:
- 47:73af5c0b0dc2
Tidied up the RA module
Who changed what in which revision?
User | Revision | Line number | New 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 | 46:40d33e9037e4 | 10 | #include "rs.h" |
andrewboyson | 22:914b970356f0 | 11 | #include "ntp.h" |
andrewboyson | 22:914b970356f0 | 12 | #include "io.h" |
andrewboyson | 2:849103b5a16d | 13 | |
andrewboyson | 37:793b39683406 | 14 | |
andrewboyson | 43:bc028d5a6424 | 15 | bool NetTraceStack = false; |
andrewboyson | 43:bc028d5a6424 | 16 | bool NetTraceNewLine = false; |
andrewboyson | 43:bc028d5a6424 | 17 | bool NetTraceVerbose = false; |
andrewboyson | 42:222a4f45f916 | 18 | bool NetPreferIp4Polled = false; |
andrewboyson | 42:222a4f45f916 | 19 | |
andrewboyson | 44:83ce5ace337b | 20 | char* NetName4; |
andrewboyson | 44:83ce5ace337b | 21 | char* NetName6; |
andrewboyson | 10:f0854784e960 | 22 | |
andrewboyson | 0:faa09bd4e6bf | 23 | int16_t NetToHost16(int16_t n) |
andrewboyson | 0:faa09bd4e6bf | 24 | { |
andrewboyson | 0:faa09bd4e6bf | 25 | int16_t h; |
andrewboyson | 0:faa09bd4e6bf | 26 | |
andrewboyson | 0:faa09bd4e6bf | 27 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 28 | char* ph = (char*)&h + 1; |
andrewboyson | 0:faa09bd4e6bf | 29 | |
andrewboyson | 0:faa09bd4e6bf | 30 | *ph = *pn; ph--; pn++; // 1<-0 |
andrewboyson | 0:faa09bd4e6bf | 31 | *ph = *pn; // 0<-1 |
andrewboyson | 0:faa09bd4e6bf | 32 | |
andrewboyson | 0:faa09bd4e6bf | 33 | return h; |
andrewboyson | 0:faa09bd4e6bf | 34 | } |
andrewboyson | 0:faa09bd4e6bf | 35 | |
andrewboyson | 0:faa09bd4e6bf | 36 | int32_t NetToHost32(int32_t n) |
andrewboyson | 0:faa09bd4e6bf | 37 | { |
andrewboyson | 0:faa09bd4e6bf | 38 | int32_t h; |
andrewboyson | 0:faa09bd4e6bf | 39 | |
andrewboyson | 0:faa09bd4e6bf | 40 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 41 | char* ph = (char*)&h + 3; |
andrewboyson | 0:faa09bd4e6bf | 42 | |
andrewboyson | 0:faa09bd4e6bf | 43 | *ph = *pn; ph--; pn++; // 3<-0 |
andrewboyson | 0:faa09bd4e6bf | 44 | *ph = *pn; ph--; pn++; // 2<-1 |
andrewboyson | 0:faa09bd4e6bf | 45 | *ph = *pn; ph--; pn++; // 1<-2 |
andrewboyson | 0:faa09bd4e6bf | 46 | *ph = *pn; // 0<-3 |
andrewboyson | 0:faa09bd4e6bf | 47 | |
andrewboyson | 0:faa09bd4e6bf | 48 | return h; |
andrewboyson | 0:faa09bd4e6bf | 49 | } |
andrewboyson | 0:faa09bd4e6bf | 50 | int64_t NetToHost64(int64_t n) |
andrewboyson | 0:faa09bd4e6bf | 51 | { |
andrewboyson | 0:faa09bd4e6bf | 52 | int64_t h; |
andrewboyson | 0:faa09bd4e6bf | 53 | |
andrewboyson | 0:faa09bd4e6bf | 54 | char* pn = (char*)&n; |
andrewboyson | 0:faa09bd4e6bf | 55 | char* ph = (char*)&h + 7; |
andrewboyson | 0:faa09bd4e6bf | 56 | |
andrewboyson | 0:faa09bd4e6bf | 57 | *ph = *pn; ph--; pn++; // 7<-0 |
andrewboyson | 0:faa09bd4e6bf | 58 | *ph = *pn; ph--; pn++; // 6<-1 |
andrewboyson | 0:faa09bd4e6bf | 59 | *ph = *pn; ph--; pn++; // 5<-2 |
andrewboyson | 0:faa09bd4e6bf | 60 | *ph = *pn; ph--; pn++; // 4<-3 |
andrewboyson | 0:faa09bd4e6bf | 61 | *ph = *pn; ph--; pn++; // 3<-4 |
andrewboyson | 0:faa09bd4e6bf | 62 | *ph = *pn; ph--; pn++; // 2<-5 |
andrewboyson | 0:faa09bd4e6bf | 63 | *ph = *pn; ph--; pn++; // 1<-6 |
andrewboyson | 0:faa09bd4e6bf | 64 | *ph = *pn; // 0<-7 |
andrewboyson | 0:faa09bd4e6bf | 65 | |
andrewboyson | 0:faa09bd4e6bf | 66 | return h; |
andrewboyson | 0:faa09bd4e6bf | 67 | } |
andrewboyson | 0:faa09bd4e6bf | 68 | uint16_t onesComplement(uint16_t start, int count, void* pData) |
andrewboyson | 0:faa09bd4e6bf | 69 | { |
andrewboyson | 0:faa09bd4e6bf | 70 | uint32_t sum = start; //Initialise the 32 bit accumulator with the last sum |
andrewboyson | 0:faa09bd4e6bf | 71 | uint16_t* p = (uint16_t*)pData; //Set up a 16 bit pointer for the data |
andrewboyson | 0:faa09bd4e6bf | 72 | |
andrewboyson | 0:faa09bd4e6bf | 73 | while(count > 1) |
andrewboyson | 0:faa09bd4e6bf | 74 | { |
andrewboyson | 0:faa09bd4e6bf | 75 | sum += *p++; // Add each pair of bytes into 32 bit accumulator |
andrewboyson | 0:faa09bd4e6bf | 76 | count -= 2; |
andrewboyson | 0:faa09bd4e6bf | 77 | } |
andrewboyson | 0:faa09bd4e6bf | 78 | if(count) sum += * (uint8_t*) p; // Add left-over byte, if any |
andrewboyson | 0:faa09bd4e6bf | 79 | 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 | 80 | return sum; |
andrewboyson | 0:faa09bd4e6bf | 81 | } |
andrewboyson | 0:faa09bd4e6bf | 82 | uint16_t NetCheckSumTwo(int count1, void* pData1, int count2, void* pData2) |
andrewboyson | 0:faa09bd4e6bf | 83 | { |
andrewboyson | 0:faa09bd4e6bf | 84 | uint16_t sum = onesComplement(0, count1, pData1); |
andrewboyson | 0:faa09bd4e6bf | 85 | return ~onesComplement(sum, count2, pData2); |
andrewboyson | 0:faa09bd4e6bf | 86 | } |
andrewboyson | 0:faa09bd4e6bf | 87 | uint16_t NetCheckSum(int count, void* pData) |
andrewboyson | 0:faa09bd4e6bf | 88 | { |
andrewboyson | 0:faa09bd4e6bf | 89 | return ~onesComplement(0, count, pData); |
andrewboyson | 0:faa09bd4e6bf | 90 | } |
andrewboyson | 2:849103b5a16d | 91 | |
andrewboyson | 44:83ce5ace337b | 92 | int NetInit(char* name4, char* name6) |
andrewboyson | 2:849103b5a16d | 93 | { |
andrewboyson | 44:83ce5ace337b | 94 | NetName4 = name4; |
andrewboyson | 44:83ce5ace337b | 95 | NetName6 = name6; |
andrewboyson | 30:e34173b7585c | 96 | PhyInit(); |
andrewboyson | 30:e34173b7585c | 97 | TcpInit(); |
andrewboyson | 30:e34173b7585c | 98 | ArInit(); |
andrewboyson | 35:93c39d260a83 | 99 | NrInit(); |
andrewboyson | 30:e34173b7585c | 100 | SlaacInit(); |
andrewboyson | 2:849103b5a16d | 101 | return 0; |
andrewboyson | 2:849103b5a16d | 102 | } |
andrewboyson | 2:849103b5a16d | 103 | int NetMain() |
andrewboyson | 2:849103b5a16d | 104 | { |
andrewboyson | 35:93c39d260a83 | 105 | PhyMain(); |
andrewboyson | 35:93c39d260a83 | 106 | ArMain(); |
andrewboyson | 35:93c39d260a83 | 107 | NrMain(); |
andrewboyson | 43:bc028d5a6424 | 108 | DhcpMain(); |
andrewboyson | 43:bc028d5a6424 | 109 | DnsMain(); |
andrewboyson | 46:40d33e9037e4 | 110 | RsMain(); |
andrewboyson | 2:849103b5a16d | 111 | return 0; |
andrewboyson | 2:849103b5a16d | 112 | } |