golioth / golioth-zephyr-sdk

Golioth SDK For Zephyr
https://www.golioth.io
Apache License 2.0
65 stars 20 forks source link

samples: implement WiFi state machine to handle reconnects #434

Closed mniestroj closed 8 months ago

mniestroj commented 9 months ago

Implement WiFi connectivity state machine running in a dedicated work queue (which runs in a dedicated thread). Network management events from Zephyr are handled there, as well as timeouts and retries when attempting to connect. As a result, WiFi connectivity is bringed back whenever there was a temporary problem with WiFi AP connection.

In case WiFi radio is not ready (L1 is down, which results in LOWER_DOWN interface management event/state) wait until it becomes ready (which is required by ESP32 WiFi driver, since it does not handle connect requests otherwise).

Initialize and launch WiFi work queue thread automatically by using SYS_INIT(), so that net_connect() function does not need to handle WiFi explicitly.

Since net_connect() handles mostly DHCPv4 setup and waiting to obtain IPv4 address, there is no need to check whether interace is UP at the beginning. DHCPv4 in Zephyr has its own state machine, with network events handling, all it requires is just calling net_dhcpv4_start() even for interfaces that are not operational at the time of function call.

Replace waiting for "DHCPv4 bound" event with waiting for "IPv4 address add" event. The latter allows to wait for WiFi to be connected to AP with assigned IP address, while still handling (implicitly) "DHCPv4 bound" event, which is required for ethernet cable connected devices.

github-actions[bot] commented 9 months ago

Visit the preview URL for this PR (updated for commit 1adde6d):

https://golioth-zephyr-sdk-doxygen-dev--pr434-wifi-conn-mgr-ky2vq2vi.web.app

(expires Mon, 30 Oct 2023 14:33:09 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: a389eefadf4b4b68a539327b3459dd66c142cf49