@@ -124,7 +124,7 @@ int DhcpClass::request_DHCP_lease(){
124
124
_dhcpUdpSocket.stop ();
125
125
_dhcpTransactionId++;
126
126
127
- _secTimeout = millis () + 1000 ;
127
+ _lastCheckLeaseMillis = millis ();
128
128
return result;
129
129
}
130
130
@@ -392,45 +392,41 @@ uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& tr
392
392
4/DHCP_CHECK_REBIND_OK: rebind success
393
393
*/
394
394
int DhcpClass::checkLease (){
395
- // this uses a signed / unsigned trick to deal with millis overflow
395
+ int rc = DHCP_CHECK_NONE;
396
+
396
397
unsigned long now = millis ();
397
- signed long snow = (long )now;
398
- int rc=DHCP_CHECK_NONE;
399
-
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 )
398
+ unsigned long elapsed = now - _lastCheckLeaseMillis;
399
+
400
+ // if more then one sec passed, reduce the counters accordingly
401
+ if (elapsed >= 1000 ) {
402
+ // set the new timestamps
403
+ _lastCheckLeaseMillis = now - (elapsed % 1000 );
404
+ elapsed = elapsed / 1000 ;
405
+
406
+ // decrease the counters by elapsed seconds
407
+ // we assume that the cycle time (elapsed) is fairly constant
408
+ // if the remainder is less than cycle time * 2
409
+ // do it early instead of late
410
+ if (_renewInSec < elapsed * 2 )
415
411
_renewInSec = 0 ;
416
412
else
417
- _renewInSec -= factor ;
413
+ _renewInSec -= elapsed ;
418
414
419
- if (_rebindInSec < factor* 2 )
415
+ if (_rebindInSec < elapsed * 2 )
420
416
_rebindInSec = 0 ;
421
417
else
422
- _rebindInSec -= factor ;
418
+ _rebindInSec -= elapsed ;
423
419
}
424
420
425
- // if we have a lease but should renew, do it
426
- if (_dhcp_state == STATE_DHCP_LEASED && _renewInSec <= 0 ) {
421
+ // if we have a lease but should renew, do it
422
+ if (_renewInSec == 0 &&_dhcp_state == STATE_DHCP_LEASED) {
427
423
_dhcp_state = STATE_DHCP_REREQUEST;
428
424
rc = 1 + request_DHCP_lease ();
429
425
}
430
426
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
427
+ // if we have a lease or is renewing but should bind, do it
428
+ if (_rebindInSec == 0 && (_dhcp_state == STATE_DHCP_LEASED || _dhcp_state == STATE_DHCP_START)) {
429
+ // this should basically restart completely
434
430
_dhcp_state = STATE_DHCP_START;
435
431
reset_DHCP_lease ();
436
432
rc = 3 + request_DHCP_lease ();
0 commit comments