Skip to content

[lightning-ln882h] Fix static IP ignored after WiFi connect#375

Open
Bl00d-B0b wants to merge 5 commits intolibretiny-eu:masterfrom
Bl00d-B0b:ln882h-static-ip-fix
Open

[lightning-ln882h] Fix static IP ignored after WiFi connect#375
Bl00d-B0b wants to merge 5 commits intolibretiny-eu:masterfrom
Bl00d-B0b:ln882h-static-ip-fix

Conversation

@Bl00d-B0b
Copy link
Copy Markdown

Fixes static IP being ignored on LN882H after WiFi connect, and ensures DHCP is fully stopped across all reconnect and roaming paths.

Changes

  1. Fix static IP ignored after WiFi connect — apply static IP and stop DHCP in wifiEventStaConnected() before the SDK starts DHCP. wifi_sta_connect() always starts DHCP internally with no way to disable it.

  2. Fix forward declaration of wifiEventIpReceived — required for the above fix.

  3. Fix DHCP leaking through static IP on reconnect/roam — two additional bugs in the previous fix:

    • Wrong order: netif_set_addr() was called before netifapi_dhcp_release_and_stop(). When DHCP is already BOUND, this triggers the netif status callback with dhcp_supplied_address==true before DHCP is stopped. Fixed: stop DHCP first.
    • Missing fallback: wifiEventIpReceived() is called from inside dhcp_recv() in the LwIP tcpip_thread. If DHCP completes despite the early stop (roaming, SDK-internal reconnect), the lease stays active and sends periodic renewal requests. Cannot call netifapi_dhcp_release_and_stop() (deadlock) or dhcp_release_and_stop() directly (use-after-free). Fixed: schedule wifiApplyStaticIpCallback via tcpip_callback() to run after dhcp_recv() returns.

Tested on

LN882H-based 2-gang light switch (DS-101JL) running ESPHome

Bl00d-B0b and others added 3 commits April 2, 2026 22:12
…y-eu#375)

wifi_sta_connect() always starts DHCP internally; there is no way to
pass a dhcp_mode parameter to it like BK72XX's bk_wlan_start_sta_adv_fix.

Apply static IP and stop DHCP in wifiEventStaConnected(), immediately
after the L2 association event fires, before the SDK-initiated DHCP
exchange completes. Emit GOT_IP manually since the netdev callback
won't fire when DHCP is stopped.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…am (libretiny-eu#375)

Two bugs in the previous static-IP fix (e2d501b + f49e18f):

1. Wrong order in wifiEventStaConnected(): netif_set_addr() was called
   before netifapi_dhcp_release_and_stop(). When DHCP is already BOUND
   at the time the connected event fires, netif_set_addr() triggers the
   LwIP netif status callback with dhcp_supplied_address==true, emitting
   a spurious GOT_IP before DHCP was stopped.
   Fix: stop DHCP first, then set the address.

2. Missing fallback in wifiEventIpReceived(): this callback is invoked by
   the SDK's sta_netif_status_changed_cb() from inside dhcp_recv() in the
   LwIP tcpip_thread. If DHCP completes before or after the connected
   event (e.g. roaming, SDK-internal reconnect), the DHCP lease stays
   active and the client sends periodic renewal requests.
   Cannot call netifapi_dhcp_release_and_stop() here (deadlock: we ARE
   the tcpip_thread waiting on itself) and cannot call dhcp_release_and_stop()
   directly (use-after-free: dhcp_recv() hasn't returned yet).
   Fix: schedule wifiApplyStaticIpCallback via tcpip_callback(). It runs
   after dhcp_recv() unwinds, where it is safe to stop DHCP, apply the
   static address, and emit GOT_IP.
@Bl00d-B0b Bl00d-B0b force-pushed the ln882h-static-ip-fix branch 3 times, most recently from 5760e15 to 6686568 Compare April 2, 2026 19:54
LN882H Fix and others added 2 commits April 2, 2026 22:54
…libretiny-eu#376)

The LN882H SDK's CmBacktrace library (cmb_def.h) uses the 'register'
storage class specifier in inline assembly helpers (cmb_get_msp/psp/sp).
This keyword was removed in C++17 (P0001R1) and produces -Wregister
warnings when compiling with -std=c++17.

Add a fixup header that wraps the original cmb_def.h with GCC diagnostic
pragmas to suppress the warning without modifying SDK sources.
…ct (libretiny-eu#375)

When the reconnect/roam fallback path fires (wifiApplyStaticIpCallback),
LwIP's dhcp_recv() has already updated the DNS table via DHCP option 6
before we could stop DHCP. Restore info.dns1/dns2 after netif_set_addr()
to ensure the user-configured DNS servers survive reconnects.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Bl00d-B0b
Copy link
Copy Markdown
Author

Bl00d-B0b commented Apr 5, 2026

Added one more fix: on the reconnect/roam fallback path (wifiApplyStaticIpCallback), LwIP's dhcp_recv() processes DHCP option 6 and overwrites the DNS table before we get to stop DHCP. The static IP and gateway were already being restored, but DNS was not. Added DNS restoration (dns_setserver) after netif_set_addr() using the stored info.dns1/info.dns2 values.

Currently testing on a DS-101JL / BSEED WiFi Touch Light Switch 2 Gang with an LN-CB3S V1.0 module (WL2H-U-2 / LN882H).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant