Fix wifi crashing because stack overflow and fix esp_ota cancelling when starting from factory
This commit is contained in:
@@ -70,6 +70,10 @@ void ethernet_event_handler(void *arg, esp_event_base_t event_base,
|
|||||||
{
|
{
|
||||||
ESP_LOGI(kLogEthernet, "Ethernet Link Down");
|
ESP_LOGI(kLogEthernet, "Ethernet Link Down");
|
||||||
s_eth_link_up = false;
|
s_eth_link_up = false;
|
||||||
|
if (s_semph_eth_link)
|
||||||
|
{
|
||||||
|
xSemaphoreGive(s_semph_eth_link);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,10 +186,18 @@ internal esp_err_t check_ethernet_connection(uint32_t timeoutSeconds)
|
|||||||
{
|
{
|
||||||
if (!xSemaphoreTake(s_semph_eth_link, pdMS_TO_TICKS(5000)))
|
if (!xSemaphoreTake(s_semph_eth_link, pdMS_TO_TICKS(5000)))
|
||||||
{
|
{
|
||||||
ESP_LOGE(kLogEthernet,
|
ESP_LOGE(
|
||||||
"No physical Ethernet link detected. Skipping DHCP wait.");
|
kLogEthernet,
|
||||||
|
"No physical Ethernet link detected (Timeout). Skipping DHCP wait.");
|
||||||
return ESP_ERR_INVALID_STATE; // Special error to skip retries
|
return ESP_ERR_INVALID_STATE; // Special error to skip retries
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!s_eth_link_up)
|
||||||
|
{
|
||||||
|
ESP_LOGE(kLogEthernet, "No physical Ethernet link detected "
|
||||||
|
"(Disconnected). Skipping DHCP wait.");
|
||||||
|
return ESP_ERR_INVALID_STATE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGI(kLogEthernet, "Waiting for IP address for %d seconds.",
|
ESP_LOGI(kLogEthernet, "Waiting for IP address for %d seconds.",
|
||||||
@@ -209,15 +221,11 @@ internal esp_netif_t *s_wifi_netif = nullptr;
|
|||||||
internal SemaphoreHandle_t s_semph_get_wifi_ip_addrs = nullptr;
|
internal SemaphoreHandle_t s_semph_get_wifi_ip_addrs = nullptr;
|
||||||
internal SemaphoreHandle_t s_semph_wifi_link = nullptr;
|
internal SemaphoreHandle_t s_semph_wifi_link = nullptr;
|
||||||
internal volatile bool s_wifi_link_up = false;
|
internal volatile bool s_wifi_link_up = false;
|
||||||
internal TimerHandle_t s_wifi_reconnect_timer = nullptr;
|
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
internal bool s_wifi_connected = false;
|
internal bool s_wifi_connected = false;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Forward declaration for timer callback
|
|
||||||
internal void wifi_reconnect_timer_cb(TimerHandle_t xTimer);
|
|
||||||
|
|
||||||
void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
||||||
int32_t event_id, void *event_data)
|
int32_t event_id, void *event_data)
|
||||||
{
|
{
|
||||||
@@ -232,11 +240,11 @@ void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
|||||||
}
|
}
|
||||||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
||||||
{
|
{
|
||||||
ESP_LOGI(kLogWifi, "WiFi disconnected. Scheduling reconnect...");
|
ESP_LOGI(kLogWifi, "WiFi disconnected.");
|
||||||
s_wifi_link_up = false;
|
s_wifi_link_up = false;
|
||||||
if (s_wifi_reconnect_timer != nullptr)
|
if (s_semph_wifi_link)
|
||||||
{
|
{
|
||||||
xTimerStart(s_wifi_reconnect_timer, 0);
|
xSemaphoreGive(s_semph_wifi_link);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
||||||
@@ -254,20 +262,8 @@ void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void wifi_reconnect_timer_cb(TimerHandle_t xTimer)
|
|
||||||
{
|
|
||||||
ESP_LOGI(kLogWifi, "Timer expired. Executing esp_wifi_connect()...");
|
|
||||||
esp_wifi_connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
void teardown_wifi()
|
void teardown_wifi()
|
||||||
{
|
{
|
||||||
if (s_wifi_reconnect_timer != nullptr)
|
|
||||||
{
|
|
||||||
xTimerStop(s_wifi_reconnect_timer, 0);
|
|
||||||
xTimerDelete(s_wifi_reconnect_timer, 0);
|
|
||||||
s_wifi_reconnect_timer = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s_semph_wifi_link != nullptr)
|
if (s_semph_wifi_link != nullptr)
|
||||||
{
|
{
|
||||||
@@ -313,11 +309,6 @@ internal esp_err_t connect_wifi(const char *ssid, const char *password,
|
|||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a 5-second timer to avoid spamming connect requests
|
|
||||||
s_wifi_reconnect_timer =
|
|
||||||
xTimerCreate("wifi_recon", pdMS_TO_TICKS(5000), pdFALSE, (void *)0,
|
|
||||||
wifi_reconnect_timer_cb);
|
|
||||||
|
|
||||||
// esp_netif_init() is already called by Ethernet, but safe to call multiple
|
// esp_netif_init() is already called by Ethernet, but safe to call multiple
|
||||||
// times or we can assume it's initialized.
|
// times or we can assume it's initialized.
|
||||||
s_wifi_netif = esp_netif_create_default_wifi_sta();
|
s_wifi_netif = esp_netif_create_default_wifi_sta();
|
||||||
@@ -373,24 +364,26 @@ internal esp_err_t check_wifi_connection(uint32_t timeoutSeconds)
|
|||||||
// Wait up to 10000ms for the physical link to associate with the AP
|
// Wait up to 10000ms for the physical link to associate with the AP
|
||||||
if (!s_wifi_link_up)
|
if (!s_wifi_link_up)
|
||||||
{
|
{
|
||||||
// If the timer isn't already running, kickstart a connection attempt now
|
ESP_LOGI(kLogWifi,
|
||||||
if (s_wifi_reconnect_timer != nullptr &&
|
"Physical link is down. Requesting immediate AP association...");
|
||||||
xTimerIsTimerActive(s_wifi_reconnect_timer) == pdFALSE)
|
esp_err_t err = esp_wifi_connect();
|
||||||
|
if (err != ESP_OK && err != ESP_ERR_WIFI_CONN)
|
||||||
{
|
{
|
||||||
ESP_LOGI(kLogWifi,
|
ESP_LOGE(kLogWifi, "esp_wifi_connect failed: %s", esp_err_to_name(err));
|
||||||
"Physical link is down. Requesting immediate AP association...");
|
|
||||||
esp_err_t err = esp_wifi_connect();
|
|
||||||
if (err != ESP_OK && err != ESP_ERR_WIFI_CONN)
|
|
||||||
{
|
|
||||||
ESP_LOGE(kLogWifi, "esp_wifi_connect failed: %s", esp_err_to_name(err));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!xSemaphoreTake(s_semph_wifi_link, pdMS_TO_TICKS(10000)))
|
if (!xSemaphoreTake(s_semph_wifi_link, pdMS_TO_TICKS(10000)))
|
||||||
{
|
{
|
||||||
ESP_LOGE(kLogWifi, "Failed to associate with WiFi AP.");
|
ESP_LOGE(kLogWifi, "Failed to associate with WiFi AP (Timeout).");
|
||||||
return ESP_ERR_TIMEOUT; // Return timeout so main.cpp triggers a retry
|
return ESP_ERR_TIMEOUT; // Return timeout so main.cpp triggers a retry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// After semaphore is taken, check if it was because of a disconnect
|
||||||
|
if (!s_wifi_link_up)
|
||||||
|
{
|
||||||
|
ESP_LOGE(kLogWifi, "Failed to associate with WiFi AP (Disconnected).");
|
||||||
|
return ESP_ERR_TIMEOUT;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGI(kLogWifi, "Waiting for IP address for %d seconds.", timeoutSeconds);
|
ESP_LOGI(kLogWifi, "Waiting for IP address for %d seconds.", timeoutSeconds);
|
||||||
|
|||||||
@@ -168,8 +168,16 @@ extern "C" void app_main()
|
|||||||
|
|
||||||
printf("Connected!\n");
|
printf("Connected!\n");
|
||||||
|
|
||||||
// Mark the current app as valid to cancel rollback
|
// Mark the current app as valid to cancel rollback, only if it's an OTA app
|
||||||
esp_ota_mark_app_valid_cancel_rollback();
|
{
|
||||||
|
const esp_partition_t *running = esp_ota_get_running_partition();
|
||||||
|
if (running != NULL &&
|
||||||
|
running->subtype >= ESP_PARTITION_SUBTYPE_APP_OTA_MIN &&
|
||||||
|
running->subtype < ESP_PARTITION_SUBTYPE_APP_OTA_MAX)
|
||||||
|
{
|
||||||
|
esp_ota_mark_app_valid_cancel_rollback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Start the webserver
|
// Start the webserver
|
||||||
web_server = start_webserver();
|
web_server = start_webserver();
|
||||||
|
|||||||
Reference in New Issue
Block a user