From this code, it is found that the lease time offered by the DHCP server will be divided by 2. Just want to understand why need to be divided by 2? Any potential issue if the stack renews the request just near or exactly at the lease time?
The issue with holding off to the very end for renewing is if anything slows down the renewal, the client will hit a condition where it doesn’t have an IP address, and its address might get assigned to another device, and any on going communication will get disrupted.
Thus, it is important to not start a communication that might take longer than the reaming lease expiration time to complete.
Thus, it is common for devices to attempt to renew their IP address significantly before it actually expires. Divide by two may be earlier than needed, but does say that if renew is prompt, then the second renew will be close to the time of the original expiration, so if you configure an intentional change, it will happen at about the expected point of time.
We are seeking to understand why division by 2 is necessary in this context. We use the hook vApplicationIPNetworkEventHook_Multi() to display the leased IP address from the DHCP server on the terminal. We have observed that this hook is triggered every time there is a renewal, occurring at half the duration of the lease.
Based on your explanation, this behavior is expected. We are considering adding logic to only display the new leased IP if there is a change, so that we do not see the message with every DHCP renewal request.
/* The DHCP parameter is in seconds, convert
* to host-endian format. */
EP_DHCPData.ulLeaseTime = FreeRTOS_ntohl( pxSet->ulParameter );
/* Divide the lease time by two to ensure a renew
* request is sent before the lease actually expires. */
EP_DHCPData.ulLeaseTime >>= 1U;
The above code was there since version 0.0, we wanted to make sure that we’re not too late. We could have taken 90% of the time. But for now the code must remain downward compatible.
When xApplicationDHCPHook() is called, please take notice of the parameter eDHCPPhase. Only when it is equal to eDHCPPhasePreRequest, the IP-address will be claimed / requested.