Skip to content

Commit f301301

Browse files
committed
Fix DHCP timeout fallback to static IP
1 parent 2a606e1 commit f301301

6 files changed

Lines changed: 47 additions & 36 deletions

File tree

src/port/stm32h563/config.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@
5252
#define WOLFIP_ENABLE_DHCP 1
5353
#endif
5454

55-
#if WOLFIP_ENABLE_DHCP
56-
#define DHCP
57-
#else
55+
/* Static IP fallback (used when DHCP is disabled or times out) */
5856
#define WOLFIP_IP "192.168.12.11"
5957
#define WOLFIP_NETMASK "255.255.255.0"
6058
#define WOLFIP_GW "192.168.12.1"
6159
#define WOLFIP_STATIC_DNS_IP "9.9.9.9"
60+
61+
#if WOLFIP_ENABLE_DHCP
62+
#define DHCP
6263
#endif
6364

6465
#endif /* WOLF_CONFIG_H */

src/port/stm32h563/main.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -642,20 +642,18 @@ int main(void)
642642
uart_putip4(gw);
643643
uart_puts("\n");
644644
} else {
645-
uint32_t polls = 0, pkts = 0;
646-
stm32_eth_get_stats(&polls, &pkts);
647-
uart_puts(" DHCP timeout - no lease obtained\n");
648-
uart_puts(" ETH stats: polls=");
649-
uart_puthex(polls);
650-
uart_puts(" pkts=");
651-
uart_puthex(pkts);
652-
uart_puts("\n DMACSR=");
653-
uart_puthex(stm32_eth_get_dmacsr());
654-
uart_puts(" RX_DES3=");
655-
uart_puthex(stm32_eth_get_rx_des3());
656-
uart_puts("\n ticks=");
657-
uart_puthex((uint32_t)(tick - dhcp_start_tick));
645+
ip4 ip = atoip4(WOLFIP_IP);
646+
ip4 nm = atoip4(WOLFIP_NETMASK);
647+
ip4 gw = atoip4(WOLFIP_GW);
648+
uart_puts(" DHCP timeout - falling back to static IP\n");
649+
uart_puts(" IP: ");
650+
uart_putip4(ip);
651+
uart_puts("\n Mask: ");
652+
uart_putip4(nm);
653+
uart_puts("\n GW: ");
654+
uart_putip4(gw);
658655
uart_puts("\n");
656+
wolfIP_ipconfig_set(IPStack, ip, nm, gw);
659657
}
660658
}
661659
}

src/port/stm32h753/config.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@
5454
#define WOLFIP_ENABLE_DHCP 1
5555
#endif
5656

57-
#if WOLFIP_ENABLE_DHCP
58-
#define DHCP
59-
#else
60-
/* Static IP configuration (when DHCP disabled) */
57+
/* Static IP fallback (used when DHCP is disabled or times out) */
6158
#define WOLFIP_IP "192.168.1.100"
6259
#define WOLFIP_NETMASK "255.255.255.0"
6360
#define WOLFIP_GW "192.168.1.1"
6461
#define WOLFIP_STATIC_DNS_IP "8.8.8.8"
62+
63+
#if WOLFIP_ENABLE_DHCP
64+
#define DHCP
6565
#endif
6666

6767
/* Hardware debug: uncomment to enable verbose GPIO/ETH/MDIO/DHCP logging */

src/port/stm32h753/main.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,13 +1151,13 @@ int main(void)
11511151
#ifdef DHCP
11521152
{
11531153
uint32_t dhcp_start_tick;
1154-
uint32_t dhcp_timeout = 30000;
1154+
uint32_t dhcp_timeout = 5000;
11551155

11561156
ret = dhcp_client_init(IPStack);
11571157
if (ret >= 0) {
11581158
uart_puts("Waiting for DHCP...\n");
11591159
dhcp_start_tick = tick;
1160-
while (!dhcp_bound(IPStack)) {
1160+
while (!dhcp_bound(IPStack) && dhcp_client_is_running(IPStack)) {
11611161
uint32_t elapsed = (uint32_t)(tick - dhcp_start_tick);
11621162
(void)wolfIP_poll(IPStack, tick);
11631163
tick++;
@@ -1195,7 +1195,18 @@ int main(void)
11951195
uart_putip4(gw);
11961196
uart_puts("\n");
11971197
} else {
1198-
uart_puts("DHCP timeout!\n");
1198+
ip4 ip = atoip4(WOLFIP_IP);
1199+
ip4 nm = atoip4(WOLFIP_NETMASK);
1200+
ip4 gw = atoip4(WOLFIP_GW);
1201+
uart_puts(" DHCP timeout - falling back to static IP\n");
1202+
uart_puts(" IP: ");
1203+
uart_putip4(ip);
1204+
uart_puts("\n Mask: ");
1205+
uart_putip4(nm);
1206+
uart_puts("\n GW: ");
1207+
uart_putip4(gw);
1208+
uart_puts("\n");
1209+
wolfIP_ipconfig_set(IPStack, ip, nm, gw);
11991210
}
12001211
}
12011212
}

src/port/va416xx/config.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@
6262
#define WOLFIP_ENABLE_DHCP 1
6363
#endif
6464

65-
#if WOLFIP_ENABLE_DHCP
66-
#define DHCP
67-
#else
65+
/* Static IP fallback (used when DHCP is disabled or times out) */
6866
#define WOLFIP_IP "192.168.12.11"
6967
#define WOLFIP_NETMASK "255.255.255.0"
7068
#define WOLFIP_GW "192.168.12.1"
7169
#define WOLFIP_STATIC_DNS_IP "8.8.8.8"
70+
71+
#if WOLFIP_ENABLE_DHCP
72+
#define DHCP
7273
#endif
7374

7475
#endif /* WOLF_CONFIG_H */

src/port/va416xx/main.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ extern volatile uint64_t HAL_time_ms;
4444
#define RX_BUF_SIZE 1024
4545

4646
/* DHCP timeout: total time to wait for DHCP before static IP fallback.
47-
* wolfIP's internal DHCP state machine only retries for ~8 seconds
48-
* (DHCP_DISCOVER_RETRIES=3 × 2s timeout). After that it sets state to
47+
* wolfIP's internal DHCP state machine only retries for ~4 seconds
48+
* (DHCP_DISCOVER_RETRIES=1 x 2s timeout). After that it sets state to
4949
* DHCP_OFF and the UDP socket stops accepting unicast DHCP responses
5050
* (because DHCP_IS_RUNNING becomes false). We re-init periodically to
5151
* keep trying, but must space re-inits apart to avoid socket churn
5252
* (close/reopen loses in-flight responses). */
53-
#define DHCP_TIMEOUT_MS 120000U /* 120s total before static fallback */
54-
#define DHCP_REINIT_MS 15000U /* 15s between DHCP re-init attempts */
53+
#define DHCP_TIMEOUT_MS 30000U /* 30s total before static fallback */
54+
#define DHCP_REINIT_MS 10000U /* 10s between DHCP re-init attempts */
5555

5656
static struct wolfIP *IPStack;
5757
static uint8_t rx_buf[RX_BUF_SIZE];
@@ -703,12 +703,12 @@ int main(void)
703703
#ifdef DHCP
704704
/* Non-blocking DHCP handling.
705705
*
706-
* wolfIP's internal DHCP state machine gives up after ~8s
707-
* (3 retries × 2s). When state goes to DHCP_OFF, the UDP
706+
* wolfIP's internal DHCP state machine gives up after ~4s
707+
* (1 retry x 2s). When state goes to DHCP_OFF, the UDP
708708
* socket stops accepting unicast DHCP responses (the
709709
* DHCP_IS_RUNNING check in udp_process fails).
710710
*
711-
* We periodically re-init DHCP (every 15s) to restart the
711+
* We periodically re-init DHCP (every 10s) to restart the
712712
* state machine and keep the UDP socket accepting responses.
713713
* Must space re-inits apart to avoid socket churn (the
714714
* close/reopen cycle can lose in-flight responses). */
@@ -729,9 +729,9 @@ int main(void)
729729
printf("DHCP assigned IP:\n");
730730
} else {
731731
printf("DHCP timeout, using static IP\n");
732-
ip = atoip4("192.168.12.11");
733-
nm = atoip4("255.255.255.0");
734-
gw = atoip4("192.168.12.1");
732+
ip = atoip4(WOLFIP_IP);
733+
nm = atoip4(WOLFIP_NETMASK);
734+
gw = atoip4(WOLFIP_GW);
735735
wolfIP_ipconfig_set(IPStack, ip, nm, gw);
736736
}
737737
printf(" IP: "); uart_putip4(ip); printf("\n");

0 commit comments

Comments
 (0)