Skip to content

Commit

Permalink
Fix ESP32 network:stop/0
Browse files Browse the repository at this point in the history
Removes all of the resitered event handlers when the driver is stopped.

Fixes the incorrect deletion on the default event loop when the driver is stopped.

Adds a network:stop/0 and a second start to the test_wifi_example esp32-simtest to help catch
future problems with stopping and restarting the network.

Closes #1449

Signed-off-by: Winford <winford@object.stream>
  • Loading branch information
UncleGrumpy committed Jan 6, 2025
1 parent dfc3bf1 commit e75aa80
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ certain VM instructions are used.
- Fixed an issue where a timeout would occur immediately in a race condition
- Fixed SPI close command
- Added missing lock on socket structure
- Fix `network:stop/0` on ESP32 so the network can be started again

## [0.6.5] - 2024-10-15

Expand Down
9 changes: 3 additions & 6 deletions src/platforms/esp32/components/avm_builtins/network_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,9 @@ static void stop_network(Context *ctx)
{
// Stop unregister event callbacks so they dont trigger during shutdown.
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler);
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler);
esp_event_handler_unregister(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &event_handler);
esp_event_handler_unregister(sntp_event_base, SNTP_EVENT_BASE_SYNC, &event_handler);

esp_netif_t *sta_wifi_interface = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
esp_netif_t *ap_wifi_interface = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
Expand All @@ -830,12 +833,6 @@ static void stop_network(Context *ctx)
// Stop sntp (ignore OK, or not configured error)
esp_sntp_stop();

// Delete network event loop
esp_err_t err = esp_event_loop_delete_default();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Invalid state error while deleting event loop, continuing network shutdown...");
}

// Destroy existing netif interfaces
if (ap_wifi_interface != NULL) {
esp_netif_destroy_default_wifi(ap_wifi_interface);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
start() ->
case verify_platform(atomvm:platform()) of
ok ->
ok = start_network(),
ok = network:stop(),
start_network(),
loop(0);
Error ->
Expand Down

0 comments on commit e75aa80

Please sign in to comment.