From 3fcf6ba3ddb66a2db11960570cd84c29a768381e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 8 Apr 2026 21:25:02 +0000 Subject: [PATCH] Fix T-Beam WiFi crash: resilient watchdog init and null-safe WiFi event handler Agent-Logs-Url: https://github.com/meshtastic/firmware/sessions/5350cd7b-deee-49c1-b40b-8db0c5a6809c Co-authored-by: thebentern <9000580+thebentern@users.noreply.github.com> --- src/mesh/wifi/WiFiAPClient.cpp | 8 ++++-- src/platform/esp32/main-esp32.cpp | 42 +++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/mesh/wifi/WiFiAPClient.cpp b/src/mesh/wifi/WiFiAPClient.cpp index 5d05c7fc6..c785e6a3b 100644 --- a/src/mesh/wifi/WiFiAPClient.cpp +++ b/src/mesh/wifi/WiFiAPClient.cpp @@ -401,7 +401,9 @@ static void WiFiEvent(WiFiEvent_t event) WiFi.disconnect(false, true); syslog.disable(); needReconnect = true; - wifiReconnect->setIntervalFromNow(1000); + if (wifiReconnect) { + wifiReconnect->setIntervalFromNow(1000); + } } break; case ARDUINO_EVENT_WIFI_STA_AUTHMODE_CHANGE: @@ -431,7 +433,9 @@ static void WiFiEvent(WiFiEvent_t event) WiFi.disconnect(false, true); syslog.disable(); needReconnect = true; - wifiReconnect->setIntervalFromNow(1000); + if (wifiReconnect) { + wifiReconnect->setIntervalFromNow(1000); + } } break; case ARDUINO_EVENT_WPS_ER_SUCCESS: diff --git a/src/platform/esp32/main-esp32.cpp b/src/platform/esp32/main-esp32.cpp index 25cb30e96..3a40b09a7 100644 --- a/src/platform/esp32/main-esp32.cpp +++ b/src/platform/esp32/main-esp32.cpp @@ -1,6 +1,7 @@ #include "PowerFSM.h" #include "PowerMon.h" #include "configuration.h" +#include "esp_idf_version.h" #include "esp_task_wdt.h" #include "main.h" @@ -126,9 +127,12 @@ void esp32Setup() nvs_stats_t nvs_stats; auto res = nvs_get_stats(NULL, &nvs_stats); - assert(res == ESP_OK); - LOG_DEBUG("NVS: UsedEntries %d, FreeEntries %d, AllEntries %d, NameSpaces %d", nvs_stats.used_entries, nvs_stats.free_entries, - nvs_stats.total_entries, nvs_stats.namespace_count); + if (res != ESP_OK) { + LOG_WARN("nvs_get_stats failed: 0x%x", res); + } else { + LOG_DEBUG("NVS: UsedEntries %d, FreeEntries %d, AllEntries %d, NameSpaces %d", nvs_stats.used_entries, + nvs_stats.free_entries, nvs_stats.total_entries, nvs_stats.namespace_count); + } LOG_DEBUG("Setup Preferences in Flash Storage"); @@ -165,18 +169,34 @@ void esp32Setup() // #define APP_WATCHDOG_SECS 45 #define APP_WATCHDOG_SECS 90 -#ifdef CONFIG_IDF_TARGET_ESP32C6 - esp_task_wdt_config_t *wdt_config = (esp_task_wdt_config_t *)malloc(sizeof(esp_task_wdt_config_t)); - wdt_config->timeout_ms = APP_WATCHDOG_SECS * 1000; - wdt_config->trigger_panic = true; - res = esp_task_wdt_init(wdt_config); - assert(res == ESP_OK); +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + // IDF v5.x API: use esp_task_wdt_config_t for both init and reconfigure + { + esp_task_wdt_config_t wdt_config = { + .timeout_ms = APP_WATCHDOG_SECS * 1000, + .idle_core_mask = 0, + .trigger_panic = true, + }; + // Try reconfigure first (TWDT may already be initialized by the Arduino framework) + res = esp_task_wdt_reconfigure(&wdt_config); + if (res == ESP_ERR_INVALID_STATE) { + // TWDT not yet initialized, initialize it now + res = esp_task_wdt_init(&wdt_config); + } + if (res != ESP_OK) { + LOG_WARN("Failed to (re)configure watchdog timer: 0x%x", res); + } + } #else res = esp_task_wdt_init(APP_WATCHDOG_SECS, true); - assert(res == ESP_OK); + if (res != ESP_OK) { + LOG_WARN("Failed to initialize watchdog timer: 0x%x", res); + } #endif res = esp_task_wdt_add(NULL); - assert(res == ESP_OK); + if (res != ESP_OK && res != ESP_ERR_INVALID_ARG) { + LOG_WARN("Failed to add main task to watchdog: 0x%x", res); + } #if HAS_32768HZ enableSlowCLK();