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/dhcp.c
- Revision:
- 93:580fc113d9e9
- Parent:
- 83:08c983006a6e
--- a/udp/dhcp.c Thu Nov 29 16:52:10 2018 +0000 +++ b/udp/dhcp.c Sun Dec 02 18:40:35 2018 +0000 @@ -3,7 +3,7 @@ #include <string.h> #include "log.h" -#include "clock.h" +#include "mstimer.h" #include "net.h" #include "action.h" #include "eth.h" @@ -53,16 +53,14 @@ uint32_t cookie; }; -#define REPEAT_DELAY_TIME 60 -static uint32_t delayTime = 10000000; //Reset whenever a message is sent and blocks another send until count exceeds REPEAT_DELAY_TIME -uint32_t DhcpElapsedTime = 0; //Reset whenever an IP address request has been acknowledged -void DhcpMain() +#define REPEAT_DELAY_TIME_MS 60 * 1000 +static uint32_t delayStartMs = 0; //Reset whenever a message is sent and blocks another send until count exceeds REPEAT_DELAY_TIME +static uint32_t lifeStartMs = 0; //Reset whenever an IP address request has been acknowledged + +uint32_t DhcpGetElapsedLife() { - if (ClockTicked) - { - DhcpElapsedTime++; - delayTime++; - } + if (!lifeStartMs) return 0; + return (MsTimerCount - lifeStartMs) / 1000; } static uint8_t dhcpMessageType = 0; @@ -226,7 +224,7 @@ *p++ = 255; //End of options - delayTime = 0; + delayStartMs = MsTimerCount; return HEADER_LENGTH + p - pOptions; } int DhcpHandleResponse(void (*traceback)(void), int sizeRx, void* pPacketRx, int* pSizeTx, void* pPacketTx) @@ -265,7 +263,7 @@ case DHCPACK: if (DhcpTrace) { LogTime("DHCP <- ack ip "); Ip4AddressLog(yiaddr); Log("\r\n"); } DhcpLocalIp = yiaddr; - DhcpElapsedTime = 0; + lifeStartMs = MsTimerCount; break; case DHCPNAK: if (DhcpTrace) { LogTime("DHCP <- nack ip "); Ip4AddressLog(yiaddr); Log("\r\n"); } @@ -279,13 +277,16 @@ int DhcpPollForRequestToSend(void* pPacket, int* pSize) { - if (delayTime < REPEAT_DELAY_TIME) return DO_NOTHING; //Don't retry within the delay time + if (delayStartMs && !MsTimerHasElapsed(delayStartMs, REPEAT_DELAY_TIME_MS)) return DO_NOTHING; //Don't retry within the delay time - if (DhcpLocalIp && DhcpElapsedTime < (DhcpLeaseTime >> 1)) return 0; //Do nothing if have address and within T1 + uint32_t elapsedTimeMs = MsTimerCount - lifeStartMs; + uint32_t leaseTimeMs = DhcpLeaseTime * 1000; + + if (DhcpLocalIp && elapsedTimeMs < (leaseTimeMs >> 1)) return 0; //Do nothing if have address and within T1 *pSize = 0; int dest = DO_NOTHING; - if (DhcpLocalIp && DhcpElapsedTime < DhcpLeaseTime) + if (DhcpLocalIp && elapsedTimeMs < leaseTimeMs) { *pSize = sendRequest(pPacket, DHCPREQUEST, DhcpServer, DhcpLocalIp); //if within T2 then send request to the server - not broadcast dest = UNICAST_DHCP;