Fix ESP32 Full idle radio teardown and compact AP startup
This commit is contained in:
1 parent
f1ffe53e8a
commit
d0859f925d
29 files changed
+1724
-144
No files matched your search
Vendored
+1
@@ -233,6 +233,7 @@ public:
|
||||
MyMesh* _callbacks;
|
||||
FakeSensors* _sensors;
|
||||
FakeBoard* _board;
|
||||
@OTA_STATE@
|
||||
NodePrefs stored;
|
||||
unsigned saves = 0;
|
||||
bool fail_save = false;
|
||||
|
||||
+8
-2
@@ -1,6 +1,7 @@
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
#include <helpers/WirelessControl.h>
|
||||
struct Worker {
|
||||
bool running = false, stopping = false;
|
||||
bool isRunning() const { return running; }
|
||||
@@ -22,23 +23,28 @@ struct WebConfigServer {
|
||||
bool isRunning() const { return false; }
|
||||
bool isStopping() const { return false; }
|
||||
void updateWiFiOwnership(bool owner) { owns = owner; }
|
||||
void startAutoMode(char* reply) { strcpy(reply, "starting"); }
|
||||
void startSetupMode(char* reply) { strcpy(reply, "setup"); }
|
||||
bool startAutoMode(char* reply) { strcpy(reply, "starting"); return true; }
|
||||
bool startSetupMode(char* reply) { strcpy(reply, "setup"); return true; }
|
||||
};
|
||||
struct MyMesh {
|
||||
Worker* @WORKER@ = nullptr;
|
||||
CLI _cli;
|
||||
WebConfigServer* _webconfig = nullptr;
|
||||
bool _unconfigured_setup_espnow_suspended = false;
|
||||
bool isMqttBridgeRunning() const { return @WORKER@ && @WORKER@->isRunning(); }
|
||||
bool isMqttBridgeStopping() const { return @WORKER@ && @WORKER@->isStopping(); }
|
||||
struct { unsigned char pub_key[32] = {}; } self_id;
|
||||
const char* getFirmwareVer() const { return "test"; }
|
||||
const char* getBuildDate() const { return "test"; }
|
||||
const char* getRole() const { return "test"; }
|
||||
bool startWebConfig(bool force_ap, char* reply);
|
||||
void suspendUnconfiguredSetupBridges();
|
||||
void refresh() {
|
||||
@REFRESH@
|
||||
}
|
||||
};
|
||||
@START@
|
||||
@SUSPEND@
|
||||
int main() {
|
||||
MyMesh mesh;
|
||||
Worker worker;
|
||||
|
||||
Vendored
+168
-11
@@ -1,17 +1,23 @@
|
||||
// WiFi and the HTTP server are peripheral fakes; both OTA lifecycle methods
|
||||
// are inserted verbatim from ESP32Board.cpp by test_wifi_ota_start.py.
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <string>
|
||||
#include <helpers/WirelessControl.h>
|
||||
|
||||
#define MESH_DEBUG_PRINTLN(...) ((void)0)
|
||||
constexpr int WL_CONNECTED = 3;
|
||||
constexpr int HTTP_GET = 0;
|
||||
constexpr int WIFI_AP = 2;
|
||||
constexpr int ESP_OK = 0, WIFI_IF_AP = 1;
|
||||
using wifi_mode_t = int;
|
||||
constexpr int WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2;
|
||||
constexpr int WIFI_MODE_NULL = WIFI_OFF, WIFI_MODE_AP = WIFI_AP;
|
||||
constexpr int ESP_OK = 0, WIFI_IF_AP = 1, WIFI_IF_STA = 0;
|
||||
constexpr int WIFI_AUTH_OPEN = 0;
|
||||
constexpr int WIFI_SCAN_RUNNING = -1, WIFI_SCAN_FAILED = -2, AP_STARTED_BIT = 1;
|
||||
constexpr int LISTEN = 10;
|
||||
bool async_bind_allowed = true;
|
||||
constexpr uint8_t WIFI_PROTOCOL_11B = 1, WIFI_PROTOCOL_11G = 2;
|
||||
@@ -19,6 +25,19 @@ constexpr uint8_t WIFI_PROTOCOL_11N = 4, WIFI_PROTOCOL_LR = 8;
|
||||
using esp_err_t = int;
|
||||
bool allow_protocol_reset = true;
|
||||
bool constrain_espnow_channel = true;
|
||||
struct wifi_config_t {
|
||||
struct {
|
||||
uint8_t ssid[32] = {};
|
||||
uint8_t ssid_len = 0, ssid_hidden = 0, channel = 1;
|
||||
int authmode = WIFI_AUTH_OPEN;
|
||||
} ap;
|
||||
};
|
||||
enum class Fault { None, Ssid, Hidden, Channel, Protocol, Mode, StartEvent, ZeroIp,
|
||||
DelayedStart, ModeRead, ConfigRead, ProtocolRead };
|
||||
enum class ScanFault { None, FailedFacade, StopRejected, Timeout, DelayedStop };
|
||||
static unsigned clock_ms = 100;
|
||||
unsigned millis() { return clock_ms; }
|
||||
void delay(unsigned);
|
||||
struct IPAddress {
|
||||
std::string value;
|
||||
IPAddress() = default;
|
||||
@@ -30,37 +49,121 @@ struct IPAddress {
|
||||
explicit operator uint32_t() const { return value.empty() || value == "0.0.0.0" ? 0 : 1; }
|
||||
};
|
||||
struct WiFiFake {
|
||||
bool connected = true, ap = false, allow_ap = true;
|
||||
unsigned starts = 0;
|
||||
bool connected = true, ap = false, allow_ap = true, radio = true, ap_started = false;
|
||||
bool mode_allowed = true, sdk_scan_active = false;
|
||||
unsigned starts = 0, sta_preparations = 0, radio_stops = 0, scans = 0, scan_stops = 0;
|
||||
unsigned pending_start = 0, pending_stop = 0, scan_remaining = 0;
|
||||
int scan_result = WIFI_SCAN_FAILED;
|
||||
Fault fault = Fault::None;
|
||||
ScanFault scan_fault = ScanFault::None;
|
||||
wifi_config_t live_config;
|
||||
struct APStatus { bool started() const; } AP;
|
||||
int ap_channel = 0;
|
||||
uint8_t ap_protocol = 15; // Valid driver state, but LR beacons hide the AP from ordinary clients.
|
||||
int status() const { return connected ? WL_CONNECTED : 0; }
|
||||
int getMode() const { return ap ? WIFI_AP : 0; }
|
||||
int getMode() const { return (ap ? WIFI_AP : 0) | (radio ? WIFI_STA : 0); }
|
||||
int getStatusBits() const { return ap_started ? AP_STARTED_BIT : 0; }
|
||||
bool mode(int requested) {
|
||||
if (!mode_allowed) return false;
|
||||
radio = requested != WIFI_OFF;
|
||||
if (!radio) { ++radio_stops; connected = false; }
|
||||
if (requested == WIFI_STA) ++sta_preparations;
|
||||
if (!(requested & WIFI_AP)) {
|
||||
ap = false;
|
||||
if (ap_started) pending_stop = 60;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
IPAddress localIP() const { return IPAddress(10, 20, 30, 40); }
|
||||
IPAddress softAPIP() const { return ap ? IPAddress(192, 168, 4, 1) : IPAddress(0, 0, 0, 0); }
|
||||
// A configured netif IP is independent from the live SDK AP/event state.
|
||||
IPAddress softAPIP() const { return fault != Fault::ZeroIp && ap
|
||||
? IPAddress(192, 168, 4, 1) : IPAddress(0, 0, 0, 0); }
|
||||
bool softAPConfig(IPAddress ip, IPAddress gateway, IPAddress mask) {
|
||||
assert(ip.value == "192.168.4.1" && gateway.value == ip.value);
|
||||
assert(mask.value == "255.255.255.0");
|
||||
assert(!sdk_scan_active);
|
||||
return allow_ap;
|
||||
}
|
||||
bool softAP(const char* ssid, const char* password, int channel = 1) {
|
||||
assert(strcmp(ssid, "MeshCore-OTA") == 0 && password == nullptr);
|
||||
assert(ssid && strlen(ssid) <= 32 && password == nullptr);
|
||||
ap_channel = channel;
|
||||
++starts; ap = allow_ap; return ap;
|
||||
++starts; ap = allow_ap; radio = true;
|
||||
if (ap) {
|
||||
live_config = {};
|
||||
const char* live_ssid = fault == Fault::Ssid ? "old-hotspot" : ssid;
|
||||
live_config.ap.ssid_len = uint8_t(strlen(live_ssid));
|
||||
memcpy(live_config.ap.ssid, live_ssid, strlen(live_ssid));
|
||||
live_config.ap.ssid_hidden = fault == Fault::Hidden;
|
||||
live_config.ap.channel = fault == Fault::Channel ? 11 : channel;
|
||||
ap_started = fault != Fault::StartEvent && fault != Fault::DelayedStart;
|
||||
if (fault == Fault::DelayedStart) pending_start = 60;
|
||||
}
|
||||
return ap;
|
||||
}
|
||||
void softAPdisconnect(bool) { ap = false; ap_started = false; }
|
||||
void scanDelete() { if (!sdk_scan_active) scan_result = WIFI_SCAN_FAILED; }
|
||||
int scanComplete() const { return scan_result; }
|
||||
int scanNetworks(bool async, bool hidden, bool passive, unsigned maximum, uint8_t channel) {
|
||||
assert(async && !hidden && !passive && maximum == 200);
|
||||
assert(channel == (constrain_espnow_channel ? 6 : 0));
|
||||
assert(radio && !ap && !ap_started);
|
||||
++scans; sdk_scan_active = true; scan_result = WIFI_SCAN_RUNNING;
|
||||
if (scan_fault == ScanFault::None) scan_remaining = 100;
|
||||
else if (scan_fault == ScanFault::FailedFacade || scan_fault == ScanFault::StopRejected)
|
||||
scan_result = WIFI_SCAN_FAILED;
|
||||
return scan_result;
|
||||
}
|
||||
void softAPdisconnect(bool) { ap = false; }
|
||||
} WiFi;
|
||||
esp_err_t esp_wifi_set_protocol(int interface_id, uint8_t protocols) {
|
||||
bool WiFiFake::APStatus::started() const { return WiFi.ap_started; }
|
||||
void delay(unsigned amount) {
|
||||
clock_ms += amount;
|
||||
auto consume = [amount](unsigned& pending) {
|
||||
if (!pending) return false;
|
||||
pending -= std::min(pending, amount); return pending == 0;
|
||||
};
|
||||
if (consume(WiFi.pending_stop)) WiFi.ap_started = false;
|
||||
if (consume(WiFi.pending_start)) WiFi.ap_started = true;
|
||||
if (consume(WiFi.scan_remaining)) {
|
||||
WiFi.sdk_scan_active = false;
|
||||
WiFi.scan_result = WiFi.scan_fault == ScanFault::None ? 2 : WIFI_SCAN_FAILED;
|
||||
}
|
||||
}
|
||||
int esp_wifi_scan_stop() {
|
||||
++WiFi.scan_stops;
|
||||
if (WiFi.scan_fault == ScanFault::StopRejected) return -1;
|
||||
if (WiFi.scan_fault == ScanFault::DelayedStop) WiFi.scan_remaining = 60;
|
||||
else { WiFi.sdk_scan_active = false; WiFi.scan_result = WIFI_SCAN_FAILED; }
|
||||
return ESP_OK;
|
||||
}
|
||||
int esp_wifi_get_mode(wifi_mode_t* value) {
|
||||
if (WiFi.fault == Fault::ModeRead) return -1;
|
||||
*value = WiFi.fault == Fault::Mode ? WIFI_STA : WiFi.getMode(); return ESP_OK;
|
||||
}
|
||||
int esp_wifi_get_config(int interface_id, wifi_config_t* value) {
|
||||
assert(interface_id == WIFI_IF_AP);
|
||||
if (WiFi.fault == Fault::ConfigRead) return -1;
|
||||
*value = WiFi.live_config; return ESP_OK;
|
||||
}
|
||||
int esp_wifi_get_protocol(int interface_id, uint8_t* value) {
|
||||
assert(interface_id == WIFI_IF_AP);
|
||||
if (WiFi.fault == Fault::ProtocolRead) return -1;
|
||||
*value = WiFi.fault == Fault::Protocol ? WIFI_PROTOCOL_LR : WiFi.ap_protocol;
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_err_t esp_wifi_set_protocol(int interface_id, uint8_t protocols) {
|
||||
assert(interface_id == WIFI_IF_AP || interface_id == WIFI_IF_STA);
|
||||
if (!allow_protocol_reset) return -1;
|
||||
WiFi.ap_protocol = protocols;
|
||||
if (interface_id == WIFI_IF_AP) WiFi.ap_protocol = protocols;
|
||||
return ESP_OK;
|
||||
}
|
||||
namespace mesh { namespace wifi {
|
||||
bool espNowChannelConstrained() { return constrain_espnow_channel; }
|
||||
uint8_t activeEspNowChannel() { return 6; }
|
||||
uint8_t stationScanChannel() { return constrain_espnow_channel ? 6 : 0; }
|
||||
esp_err_t applyProtocolMask(int interface_id) { return esp_wifi_set_protocol(interface_id, 15); }
|
||||
@AP_PROTOCOL_POLICY@
|
||||
} }
|
||||
@AP_LIFECYCLE_POLICY@
|
||||
static unsigned server_starts = 0;
|
||||
struct AsyncWebServerRequest {
|
||||
void send(int, const char*, const char*) {}
|
||||
@@ -85,6 +188,7 @@ static int SPIFFS;
|
||||
struct ESP32Board {
|
||||
bool inhibit_sleep = false;
|
||||
bool ota_started_ap = false;
|
||||
bool ota_started_radio = false;
|
||||
#ifdef LIGHTWEIGHT_WIFI_OTA
|
||||
void* ota_server = nullptr;
|
||||
#else
|
||||
@@ -180,6 +284,7 @@ int main() {
|
||||
lightweight_ota_server.allow_stop = true;
|
||||
#endif
|
||||
assert(board.stopOTAUpdate(reply.text));
|
||||
assert(WiFi.scans == 0); // Raising an AP preserves the live LAN without scanning.
|
||||
assert(!board.inhibit_sleep && !board.ota_server && !WiFi.ap && WiFi.connected);
|
||||
|
||||
constrain_espnow_channel = false;
|
||||
@@ -204,6 +309,58 @@ int main() {
|
||||
assert(!board.inhibit_sleep && !board.ota_server && !WiFi.ap);
|
||||
async_bind_allowed = true;
|
||||
#endif
|
||||
// Cold start/stop must restore OFF without leaking the temporary STA radio.
|
||||
WiFi.allow_ap = true;
|
||||
WiFi.radio = false;
|
||||
WiFi.connected = false;
|
||||
start(true, "192.168.4.1", "Join WiFi MeshCore-OTA");
|
||||
assert(WiFi.scans > 0); // Offline OTA uses STA-only scan-first, including minimal.
|
||||
assert(board.ota_started_radio && WiFi.radio && WiFi.ap_started);
|
||||
assert(board.stopOTAUpdate(reply.text));
|
||||
assert(!WiFi.radio && !board.ota_started_radio);
|
||||
|
||||
// A facade-successful AP cannot report success with any unusable live state.
|
||||
for (Fault fault : {Fault::Ssid, Fault::Hidden, Fault::Channel, Fault::Protocol,
|
||||
Fault::Mode, Fault::StartEvent, Fault::ZeroIp,
|
||||
Fault::ModeRead, Fault::ConfigRead, Fault::ProtocolRead}) {
|
||||
WiFi.fault = fault;
|
||||
const unsigned started = millis();
|
||||
assert(!board.startOTAUpdate("test", reply.text, true));
|
||||
assert(strcmp(reply.text, "ERR: OTA WiFi failed") == 0);
|
||||
assert(!WiFi.ap && !WiFi.radio && !board.inhibit_sleep && !board.ota_server);
|
||||
assert(millis() - started < 5000);
|
||||
}
|
||||
WiFi.fault = Fault::DelayedStart;
|
||||
start(true, "192.168.4.1", "Join WiFi MeshCore-OTA");
|
||||
assert(WiFi.ap_started);
|
||||
assert(board.stopOTAUpdate(reply.text));
|
||||
WiFi.fault = Fault::None;
|
||||
// Failed/timeout scans must stop in the SDK before a hotspot can start.
|
||||
for (ScanFault fault : {ScanFault::FailedFacade, ScanFault::Timeout, ScanFault::DelayedStop}) {
|
||||
WiFi.scan_fault = fault;
|
||||
const unsigned started = millis();
|
||||
start(true, "192.168.4.1", "Join WiFi MeshCore-OTA");
|
||||
assert(!WiFi.sdk_scan_active && millis() - started < 5000);
|
||||
assert(board.stopOTAUpdate(reply.text));
|
||||
}
|
||||
WiFi.scan_fault = ScanFault::StopRejected;
|
||||
const unsigned ap_before_scan_failure = WiFi.starts;
|
||||
assert(!board.startOTAUpdate("test", reply.text, true));
|
||||
assert(WiFi.starts == ap_before_scan_failure && !WiFi.ap && !WiFi.radio);
|
||||
WiFi.sdk_scan_active = false;
|
||||
WiFi.scan_fault = ScanFault::None;
|
||||
// A later ESP-NOW owner or associated station protects its radio on stop.
|
||||
start(true, "192.168.4.1", "Join WiFi MeshCore-OTA");
|
||||
constrain_espnow_channel = true;
|
||||
assert(board.stopOTAUpdate(reply.text));
|
||||
assert(WiFi.radio && !WiFi.ap);
|
||||
constrain_espnow_channel = false;
|
||||
WiFi.radio = false;
|
||||
start(true, "192.168.4.1", "Join WiFi MeshCore-OTA");
|
||||
WiFi.connected = true;
|
||||
assert(board.stopOTAUpdate(reply.text));
|
||||
assert(WiFi.radio && WiFi.connected);
|
||||
WiFi.connected = false;
|
||||
// A pending wireless shutdown must not race a newly started uploader.
|
||||
struct Backend : mesh::wireless::Backend {
|
||||
uint8_t active = mesh::wireless::All;
|
||||
|
||||
Reference in new issue
Block a user