Skip to content

Commit 2b089e6

Browse files
committed
Ehternet: removed useless _lastCheck field in DHCP
it seems that _lastCheck, besides the name, was simply used as flag to detect if _secTimeout is initialized. Now the check is removed and _secTimeout is initalized in the proper place.
1 parent ce595ce commit 2b089e6

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

libraries/Ethernet/src/Dhcp.cpp

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long
1414
_dhcpLeaseTime=0;
1515
_dhcpT1=0;
1616
_dhcpT2=0;
17-
_lastCheck=0;
1817
_timeout = timeout;
1918
_responseTimeout = responseTimeout;
2019

@@ -125,6 +124,7 @@ int DhcpClass::request_DHCP_lease(){
125124
_dhcpUdpSocket.stop();
126125
_dhcpTransactionId++;
127126

127+
_secTimeout = millis() + 1000;
128128
return result;
129129
}
130130

@@ -396,51 +396,45 @@ int DhcpClass::checkLease(){
396396
unsigned long now = millis();
397397
signed long snow = (long)now;
398398
int rc=DHCP_CHECK_NONE;
399-
if (_lastCheck != 0){
400-
signed long factor;
401-
//calc how many ms past the timeout we are
402-
factor = snow - (long)_secTimeout;
403-
//if on or passed the timeout, reduce the counters
404-
if ( factor >= 0 ){
405-
//next timeout should be now plus 1000 ms minus parts of second in factor
406-
_secTimeout = snow + 1000 - factor % 1000;
407-
//how many seconds late are we, minimum 1
408-
factor = factor / 1000 +1;
409-
410-
//reduce the counters by that mouch
411-
//if we can assume that the cycle time (factor) is fairly constant
412-
//and if the remainder is less than cycle time * 2
413-
//do it early instead of late
414-
if(_renewInSec < factor*2 )
415-
_renewInSec = 0;
416-
else
417-
_renewInSec -= factor;
418-
419-
if(_rebindInSec < factor*2 )
420-
_rebindInSec = 0;
421-
else
422-
_rebindInSec -= factor;
423-
}
424399

425-
//if we have a lease but should renew, do it
426-
if (_dhcp_state == STATE_DHCP_LEASED && _renewInSec <=0){
427-
_dhcp_state = STATE_DHCP_REREQUEST;
428-
rc = 1 + request_DHCP_lease();
429-
}
430-
431-
//if we have a lease or is renewing but should bind, do it
432-
if( (_dhcp_state == STATE_DHCP_LEASED || _dhcp_state == STATE_DHCP_START) && _rebindInSec <=0){
433-
//this should basically restart completely
434-
_dhcp_state = STATE_DHCP_START;
435-
reset_DHCP_lease();
436-
rc = 3 + request_DHCP_lease();
437-
}
400+
signed long factor;
401+
//calc how many ms past the timeout we are
402+
factor = snow - (long)_secTimeout;
403+
//if on or passed the timeout, reduce the counters
404+
if ( factor >= 0 ){
405+
//next timeout should be now plus 1000 ms minus parts of second in factor
406+
_secTimeout = snow + 1000 - factor % 1000;
407+
//how many seconds late are we, minimum 1
408+
factor = factor / 1000 +1;
409+
410+
//reduce the counters by that mouch
411+
//if we can assume that the cycle time (factor) is fairly constant
412+
//and if the remainder is less than cycle time * 2
413+
//do it early instead of late
414+
if(_renewInSec < factor*2 )
415+
_renewInSec = 0;
416+
else
417+
_renewInSec -= factor;
418+
419+
if(_rebindInSec < factor*2 )
420+
_rebindInSec = 0;
421+
else
422+
_rebindInSec -= factor;
438423
}
439-
else{
440-
_secTimeout = snow + 1000;
424+
425+
//if we have a lease but should renew, do it
426+
if (_dhcp_state == STATE_DHCP_LEASED && _renewInSec <=0){
427+
_dhcp_state = STATE_DHCP_REREQUEST;
428+
rc = 1 + request_DHCP_lease();
441429
}
442430

443-
_lastCheck = now;
431+
//if we have a lease or is renewing but should bind, do it
432+
if( (_dhcp_state == STATE_DHCP_LEASED || _dhcp_state == STATE_DHCP_START) && _rebindInSec <=0){
433+
//this should basically restart completely
434+
_dhcp_state = STATE_DHCP_START;
435+
reset_DHCP_lease();
436+
rc = 3 + request_DHCP_lease();
437+
}
444438
return rc;
445439
}
446440

libraries/Ethernet/src/Dhcp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ class DhcpClass {
150150
uint32_t _dhcpT1, _dhcpT2;
151151
signed long _renewInSec;
152152
signed long _rebindInSec;
153-
signed long _lastCheck;
154153
unsigned long _timeout;
155154
unsigned long _responseTimeout;
156155
unsigned long _secTimeout;

0 commit comments

Comments
 (0)