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: net.cpp
- Revision:
- 2:849103b5a16d
- Parent:
- 0:faa09bd4e6bf
- Child:
- 5:5fc4ca9fbf17
diff -r 5f70c361db20 -r 849103b5a16d net.cpp --- a/net.cpp Thu Jan 19 23:03:26 2017 +0000 +++ b/net.cpp Fri Jan 27 19:58:51 2017 +0000 @@ -1,4 +1,21 @@ #include "mbed.h" +#include "phy.h" +#include "tcp.h" +#include "dhcp.h" +#include "arp.h" + +int NetIpToString(uint32_t ip, char* text) +{ + int a0 = (ip & 0xFF000000) >> 24; + int a1 = (ip & 0x00FF0000) >> 16; + int a2 = (ip & 0x0000FF00) >> 8; + int a3 = (ip & 0x000000FF); + return sprintf(text, "%d.%d.%d.%d", a0, a1, a2, a3); +} +int NetMacToString(char* mac, char* text) +{ + return sprintf(text, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); +} int16_t NetToHost16(int16_t n) { @@ -68,3 +85,30 @@ { return ~onesComplement(0, count, pData); } + +static bool ticked() +{ + static time_t lastT = 0; + time_t thisT = time(NULL); + bool oneshot = lastT != thisT; + lastT = thisT; + return oneshot; +} + +int NetInit() +{ + PhyInit(); + TcpInit(); + ArpInit(); + return 0; +} +int NetMain() +{ + PhyMain(); + if (ticked()) + { + DhcpTick(); + ArpTick(); + } + return 0; +} \ No newline at end of file