fix(esp32): defer station maintenance while an AP owns WiFi
Starting or restarting MQTT during browser OTA could remove the active AP by forcing STA mode. Defer shared MQTT startup and recovery, and Companion WiFi startup, pending credential reloads and saved-SSID retries, until the live SDK AP releases the driver. Preserve the setup owner's deliberate credential handoff and cooperative stop acknowledgement. Validated 91 source host tests, including 20 new AP ownership cases and nine reverted controls under ASan/UBSan. Hardware and firmware build qualification remain separate; the Arduino mode snapshot is not atomic.
This commit is contained in:
1 parent
8c7d7d8839
commit
df4cbb75eb
13 files changed
+596
-9
No files matched your search
@@ -153,7 +153,12 @@ const char* MQTTBridge::repeatStatus() const {
|
||||
}
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
static bool mqttStationWiFiMutationAllowed() {
|
||||
return mesh::wifi::stationMutationAllowed();
|
||||
}
|
||||
|
||||
void MQTTBridge::beginWiFiStation() {
|
||||
if (!mqttStationWiFiMutationAllowed()) return;
|
||||
mesh::wifi::beginStation(_wifi_ssid, _wifi_password);
|
||||
}
|
||||
|
||||
@@ -1408,11 +1413,19 @@ void MQTTBridge::mqttTask(void* parameter) {
|
||||
vTaskDelete(nullptr);
|
||||
}
|
||||
|
||||
void MQTTBridge::initializeWiFiInTask() {
|
||||
MQTT_DEBUG_PRINTLN("Initializing WiFi in MQTT task...");
|
||||
bool MQTTBridge::initializeWiFiInTask() {
|
||||
// OTA and setup APs own the shared driver. Starting/restarting MQTT must
|
||||
// wait for their teardown instead of changing AP+STA to STA and dropping
|
||||
// the operator's connection. Consult the SDK even if Arduino reports OFF.
|
||||
if (!mqttStationWiFiMutationAllowed()) return false;
|
||||
|
||||
// Initialize WiFi
|
||||
WiFi.mode(WIFI_STA);
|
||||
// Use Arduino's additive STA API instead of deliberately removing AP.
|
||||
// Its mode snapshot is not atomic; recheck before reconnect/event/NTP work.
|
||||
if (!WiFi.enableSTA(true)) return false;
|
||||
wifi_mode_t mode = WIFI_MODE_NULL;
|
||||
if (esp_wifi_get_mode(&mode) != ESP_OK || (mode & WIFI_MODE_AP)) return false;
|
||||
|
||||
MQTT_DEBUG_PRINTLN("Initializing WiFi in MQTT task...");
|
||||
|
||||
// A primary ESP-NOW radio must reconnect through the fixed-channel policy;
|
||||
// other ESP32 targets retain the driver's normal automatic reconnect.
|
||||
@@ -1480,6 +1493,7 @@ void MQTTBridge::initializeWiFiInTask() {
|
||||
// before NTP sync just wastes heap on TLS handshakes that will be rejected.
|
||||
|
||||
MQTT_DEBUG_PRINTLN("WiFi initialization started in task");
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -1503,8 +1517,11 @@ bool MQTTBridge::waitUnlessStopping(uint32_t delay_ms) {
|
||||
// mqttTaskLoop() - main loop running on Core 0
|
||||
// ---------------------------------------------------------------------------
|
||||
void MQTTBridge::mqttTaskLoop() {
|
||||
// Initialize WiFi first
|
||||
initializeWiFiInTask();
|
||||
// An active OTA/setup AP can outlive MQTT's start request. Stay stoppable
|
||||
// while waiting, and leave slot/TLS work until station initialization ran.
|
||||
while (!_stop_requested && !initializeWiFiInTask()) {
|
||||
if (!waitUnlessStopping(100)) break;
|
||||
}
|
||||
|
||||
// Wait a bit for WiFi to start connecting
|
||||
waitUnlessStopping(1000);
|
||||
@@ -3069,6 +3086,11 @@ void MQTTBridge::checkConfigurationMismatch() {
|
||||
}
|
||||
|
||||
bool MQTTBridge::handleWiFiConnection(unsigned long now) {
|
||||
#ifdef ESP_PLATFORM
|
||||
// An AP can start after this worker initialized. Defer channel enforcement,
|
||||
// reconnect/disconnect and radio power changes until that owner releases it.
|
||||
if (!mqttStationWiFiMutationAllowed()) return false;
|
||||
#endif
|
||||
wl_status_t current_wifi_status = WiFi.status();
|
||||
#ifdef ESP_PLATFORM
|
||||
if (current_wifi_status == WL_CONNECTED
|
||||
|
||||
@@ -517,7 +517,7 @@ private:
|
||||
static void mqttTask(void* parameter);
|
||||
void mqttTaskLoop(); // Main loop for MQTT task
|
||||
bool waitUnlessStopping(uint32_t delay_ms);
|
||||
void initializeWiFiInTask(); // WiFi initialization moved to task
|
||||
bool initializeWiFiInTask(); // False while an AP owns the shared driver.
|
||||
#endif
|
||||
bool publishPacket(mesh::Packet* packet, bool is_tx, bool& has_eligible_target,
|
||||
const uint8_t* raw_data = nullptr, int raw_len = 0,
|
||||
|
||||
@@ -140,6 +140,15 @@ inline esp_err_t checkLongRangeRadioStart() {
|
||||
return (mode & WIFI_MODE_AP) ? ESP_ERR_INVALID_STATE : ESP_OK;
|
||||
}
|
||||
|
||||
// Background station owners must defer while an OTA/setup AP owns WiFi.
|
||||
// Only the setup owner's deliberate AP+STA credential handoff may opt in.
|
||||
inline bool stationMutationAllowed(bool setup_handoff = false) {
|
||||
wifi_mode_t mode = WIFI_MODE_NULL;
|
||||
const esp_err_t result = esp_wifi_get_mode(&mode);
|
||||
return result == ESP_ERR_WIFI_NOT_INIT
|
||||
|| (result == ESP_OK && (!(mode & WIFI_MODE_AP) || setup_handoff));
|
||||
}
|
||||
|
||||
inline esp_err_t applyAccessPointProtocolMask() {
|
||||
return esp_wifi_set_protocol(WIFI_IF_AP, kAccessPointProtocolMask);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user