From 0c9077a3cdcc84486fab641ad3487cb7b53c9514 Mon Sep 17 00:00:00 2001
From: Vincent Selosse <vincent.selosse@netatmo.com>
Date: Thu, 11 Feb 2021 15:03:45 +0100
Subject: [PATCH] lwip-2.1.2: Manage the lease time very low value

---
 lwip/lwip-2.1.2/src/core/ipv4/dhcp.c    | 22 ++++++++--------------
 lwip/lwip-2.1.2/src/include/lwip/dhcp.h | 14 +++++++-------
 2 files changed, 15 insertions(+), 21 deletions(-)

diff --git a/lwip/lwip-2.1.2/src/core/ipv4/dhcp.c b/lwip/lwip-2.1.2/src/core/ipv4/dhcp.c
index f6a548a8..1a827481 100644
--- a/lwip/lwip-2.1.2/src/core/ipv4/dhcp.c
+++ b/lwip/lwip-2.1.2/src/core/ipv4/dhcp.c
@@ -570,7 +570,7 @@ dhcp_t1_timeout(struct netif *netif)
     dhcp_renew(netif);
     /* Calculate next timeout */
     if (((dhcp->t2_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS)) {
-      dhcp->t1_renew_time = (u16_t)((dhcp->t2_timeout - dhcp->lease_used) / 2);
+      dhcp->t1_renew_time = ((dhcp->t2_timeout - dhcp->lease_used) / 2);
     }
   }
 }
@@ -596,7 +596,7 @@ dhcp_t2_timeout(struct netif *netif)
     dhcp_rebind(netif);
     /* Calculate next timeout */
     if (((dhcp->t0_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS)) {
-      dhcp->t2_rebind_time = (u16_t)((dhcp->t0_timeout - dhcp->lease_used) / 2);
+      dhcp->t2_rebind_time = ((dhcp->t0_timeout - dhcp->lease_used) / 2);
     }
   }
 }
@@ -1085,10 +1085,8 @@ dhcp_bind(struct netif *netif)
     /* set renewal period timer */
     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t0 renewal timer %"U32_F" secs\n", dhcp->offered_t0_lease));
     timeout = (dhcp->offered_t0_lease + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
-    if (timeout > 0xffff) {
-      timeout = 0xffff;
-    }
-    dhcp->t0_timeout = (u16_t)timeout;
+
+    dhcp->t0_timeout = timeout;
     if (dhcp->t0_timeout == 0) {
       dhcp->t0_timeout = 1;
     }
@@ -1100,10 +1098,8 @@ dhcp_bind(struct netif *netif)
     /* set renewal period timer */
     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew));
     timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
-    if (timeout > 0xffff) {
-      timeout = 0xffff;
-    }
-    dhcp->t1_timeout = (u16_t)timeout;
+
+    dhcp->t1_timeout = timeout;
     if (dhcp->t1_timeout == 0) {
       dhcp->t1_timeout = 1;
     }
@@ -1114,10 +1110,8 @@ dhcp_bind(struct netif *netif)
   if (dhcp->offered_t2_rebind != 0xffffffffUL) {
     LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind));
     timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
-    if (timeout > 0xffff) {
-      timeout = 0xffff;
-    }
-    dhcp->t2_timeout = (u16_t)timeout;
+
+    dhcp->t2_timeout = timeout;
     if (dhcp->t2_timeout == 0) {
       dhcp->t2_timeout = 1;
     }
diff --git a/lwip/lwip-2.1.2/src/include/lwip/dhcp.h b/lwip/lwip-2.1.2/src/include/lwip/dhcp.h
index db003925..74466c95 100644
--- a/lwip/lwip-2.1.2/src/include/lwip/dhcp.h
+++ b/lwip/lwip-2.1.2/src/include/lwip/dhcp.h
@@ -50,7 +50,7 @@ extern "C" {
 #endif
 
 /** period (in seconds) of the application calling dhcp_coarse_tmr() */
-#define DHCP_COARSE_TIMER_SECS  60
+#define DHCP_COARSE_TIMER_SECS  1
 /** period (in milliseconds) of the application calling dhcp_coarse_tmr() */
 #define DHCP_COARSE_TIMER_MSECS (DHCP_COARSE_TIMER_SECS * 1000UL)
 /** period (in milliseconds) of the application calling dhcp_fine_tmr() */
@@ -80,12 +80,12 @@ struct dhcp
   u8_t subnet_mask_given;
 
   u16_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
-  u16_t t1_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
-  u16_t t2_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
-  u16_t t1_renew_time;  /* #ticks with period DHCP_COARSE_TIMER_SECS until next renew try */
-  u16_t t2_rebind_time; /* #ticks with period DHCP_COARSE_TIMER_SECS until next rebind try */
-  u16_t lease_used; /* #ticks with period DHCP_COARSE_TIMER_SECS since last received DHCP ack */
-  u16_t t0_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for lease time */
+  u32_t t1_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
+  u32_t t2_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
+  u32_t t1_renew_time;  /* #ticks with period DHCP_COARSE_TIMER_SECS until next renew try */
+  u32_t t2_rebind_time; /* #ticks with period DHCP_COARSE_TIMER_SECS until next rebind try */
+  u32_t lease_used; /* #ticks with period DHCP_COARSE_TIMER_SECS since last received DHCP ack */
+  u32_t t0_timeout; /* #ticks with period DHCP_COARSE_TIMER_SECS for lease time */
   ip_addr_t server_ip_addr; /* dhcp server address that offered this lease (ip_addr_t because passed to UDP) */
   ip4_addr_t offered_ip_addr;
   ip4_addr_t offered_sn_mask;
-- 
2.25.1

