Skip to content

Commit

Permalink
Fix shutdown crash in AP mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiw committed Jul 29, 2024
1 parent 9d55f63 commit ba1c0fc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
2 changes: 2 additions & 0 deletions firmware/main/network/interfaces/EthernetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,13 @@ void EthernetInterface::tearDown()
{
if (ethDeviceHandle_ != nullptr)
{
ESP_LOGI(CURRENT_LOG_TAG, "Requesting stop of Ethernet driver");
status_ = INTERFACE_SHUTTING_DOWN;
esp_eth_stop(ethDeviceHandle_);
}
else
{
ESP_LOGI(CURRENT_LOG_TAG, "Ethernet driver already shut down");
status_ = INTERFACE_DOWN;
}
}
Expand Down
45 changes: 18 additions & 27 deletions firmware/main/network/interfaces/WirelessInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,28 +219,10 @@ void WirelessInterface::bringUp()
void WirelessInterface::tearDown()
{
status_ = INTERFACE_SHUTTING_DOWN;

esp_event_handler_instance_unregister(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifiEventHandle_);
esp_event_handler_instance_unregister(IP_EVENT,
ESP_EVENT_ANY_ID,
&ipEventHandle_);

if (hasStaConfig_)
{
// Disconnect from AP
ESP_LOGI(CURRENT_LOG_TAG, "Final disconnect from Wi-Fi");
ESP_ERROR_CHECK(esp_wifi_disconnect());
}
else
{
// Immediate shutdown Wi-Fi.
ESP_LOGI(CURRENT_LOG_TAG, "Directly stopping Wi-Fi service");

status_ = INTERFACE_DOWN;
ESP_ERROR_CHECK(esp_wifi_stop());
}
// Immediately shutdown Wi-Fi.
ESP_LOGI(CURRENT_LOG_TAG, "Stopping Wi-Fi service");
ESP_ERROR_CHECK(esp_wifi_stop());
}

void WirelessInterface::getMacAddress(uint8_t* mac)
Expand Down Expand Up @@ -349,6 +331,19 @@ void WirelessInterface::WiFiEventHandler_(void *event_handler_arg, esp_event_bas
break;
}
case WIFI_EVENT_AP_STOP:
case WIFI_EVENT_STA_STOP:
{
esp_event_handler_instance_unregister(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&obj->wifiEventHandle_);
esp_event_handler_instance_unregister(IP_EVENT,
ESP_EVENT_ANY_ID,
&obj->ipEventHandle_);


obj->status_ = INTERFACE_DOWN;
break;
}
case WIFI_EVENT_STA_DISCONNECTED:
{
bool networkIsDown = true;
Expand All @@ -374,12 +369,8 @@ void WirelessInterface::WiFiEventHandler_(void *event_handler_arg, esp_event_bas

// Reattempt connection to access point if we couldn't find
// it the first time around.
if (obj->status_ == INTERFACE_SHUTTING_DOWN)
{
obj->status_ = INTERFACE_DOWN;
esp_wifi_stop();
}
else if (event_id == WIFI_EVENT_STA_DISCONNECTED)
if (obj->status_ != INTERFACE_SHUTTING_DOWN &&
event_id == WIFI_EVENT_STA_DISCONNECTED)
{
esp_wifi_disconnect();
ESP_ERROR_CHECK(esp_wifi_connect());
Expand Down

0 comments on commit ba1c0fc

Please sign in to comment.