Make ESP32 OTA startup handle WiFi and WebConfig automatically
Report the upload address and required network, honor AP switches on an existing uploader, and keep delayed WebConfig cleanup from disabling OTA WiFi. Preserve actionable startup errors and account for OTA in wireless state controls. Exercise the production OTA lifecycle and stock website console, including power-saving idle sessions, in host regression tests.
This commit is contained in:
1 parent
22e1e405f4
commit
47135e081c
23 files changed
+965
-46
No files matched your search
+75
@@ -0,0 +1,75 @@
|
||||
// Only the SerialPort device boundary is simulated. The console, CRLF parser,
|
||||
// encoder, streams, open/close and command writes come from the pinned website.
|
||||
import assert from 'node:assert/strict';
|
||||
import {spawn} from 'node:child_process';
|
||||
import {readFileSync} from 'node:fs';
|
||||
import {createInterface} from 'node:readline';
|
||||
import {setTimeout as delay} from 'node:timers/promises';
|
||||
|
||||
const {SerialConsole} = await import('data:text/javascript;base64,' +
|
||||
readFileSync(process.argv[2]).toString('base64'));
|
||||
const firmware = spawn(process.argv[3], [], {stdio:['pipe','pipe','inherit']});
|
||||
let stream, pending, state;
|
||||
let writes = 0;
|
||||
createInterface({input:firmware.stdout}).on('line', line => {
|
||||
if (line.startsWith('DATA ')) {
|
||||
const bytes = Buffer.from(line.slice(5), 'hex');
|
||||
// Delimiters must work when CR and LF arrive in separate USB transfers.
|
||||
for (let n=0; n<bytes.length; n++) stream.enqueue(new Uint8Array(bytes.subarray(n,n+1)));
|
||||
} else if (line.startsWith('STATE ')) state=line.slice(6).split(' ').map(Number);
|
||||
else if (line==='DONE') {const done=pending;pending=null;done.resolve();}
|
||||
});
|
||||
firmware.on('exit', code => {if(pending) pending.reject(Error('Firmware exited: '+code));});
|
||||
function command(text) {
|
||||
assert(!pending, 'Peripheral calls must be serialized');
|
||||
return new Promise((resolve,reject)=>{pending={resolve,reject};firmware.stdin.write(text+'\n');});
|
||||
}
|
||||
class Port {
|
||||
async open(options) {
|
||||
assert.deepEqual(options,{baudRate:115200});
|
||||
this.readable=new ReadableStream({start(controller){stream=controller;},cancel(){stream=null;}});
|
||||
this.writable=new WritableStream({write(bytes){++writes;return command('WRITE '+Buffer.from(bytes).toString('hex'));}});
|
||||
}
|
||||
async close(){await command('CLOSE');}
|
||||
async setSignals(){throw Error('The stock console must work without a reset or explicit DTR');}
|
||||
}
|
||||
let client, lines;
|
||||
async function connect() {
|
||||
lines=[];client=new SerialConsole(new Port());client.onOutput=line=>lines.push(line);
|
||||
client.connect(); await delay(0); assert(client.connected);
|
||||
}
|
||||
async function query(text, expected, label) {
|
||||
const start=lines.length;
|
||||
await client.sendCommand(text);
|
||||
// All device work is synchronous at the pipe boundary, but Web Streams
|
||||
// delivery is asynchronous. Bound it rather than making a wall-time sleep.
|
||||
for(let n=0;n<50;n++) {
|
||||
if(lines.slice(start).join('').includes(expected)) return;
|
||||
await delay(2);
|
||||
}
|
||||
assert.fail(label+': stock console did not display the firmware reply');
|
||||
}
|
||||
try {
|
||||
await connect();
|
||||
await query('set powersaving on',' -> OK - powersaving on\n','power-saving setter');
|
||||
await query('get powersaving',' -> > on\n','power-saving getter');
|
||||
for(const elapsed of [121000,180000,360000,1800000,10800000,0xfffffffe]) {
|
||||
const before=writes;
|
||||
await command('TIME '+elapsed);
|
||||
assert.equal(writes,before,'An idle web console gained an artificial keepalive');
|
||||
await query('get powersaving',' -> > on\n','idle query');
|
||||
assert.equal(state[1],0,'An open web console entered light sleep');
|
||||
}
|
||||
await client.disconnect();await connect();
|
||||
await query('get powersaving',' -> > on\n','reopen query');
|
||||
await query('set powersaving off',' -> OK - powersaving off\n','disable power saving');
|
||||
await command('HOST_OFF');await command('TIME 121000');
|
||||
assert.equal(state[1],0,'Disabled power saving slept');
|
||||
await query('set powersaving on',' -> OK - powersaving on\n','enable battery power saving');
|
||||
await command('TIME 242000');
|
||||
assert.equal(state[1],1,'USB protection permanently disabled battery power saving');
|
||||
console.log('Official console: CRLF, no-DTR idle, power saving, reopen and battery sleep passed');
|
||||
} finally {
|
||||
if(client?.connected) await client.disconnect();
|
||||
firmware.stdin.end();
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
// Inserted after the ESP32Board peripheral harness. Peripheral queues/time are
|
||||
// simulated. @-marked methods below are copied verbatim from production.
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
struct Stream {
|
||||
std::deque<uint8_t> input;
|
||||
std::string output;
|
||||
int available() { return input.size(); }
|
||||
int read() { int c = input.front(); input.pop_front(); return c; }
|
||||
void print(char c) { output += c; }
|
||||
void print(const char* text) { output += text; }
|
||||
void println(const char* text) { output += text; output += "\r\n"; }
|
||||
void printf(const char* format, ...) {
|
||||
char buffer[256]; va_list args; va_start(args, format);
|
||||
int n = vsnprintf(buffer, sizeof(buffer), format, args); va_end(args);
|
||||
assert(n >= 0 && size_t(n) < sizeof(buffer)); output.append(buffer, n);
|
||||
}
|
||||
} console;
|
||||
namespace mesh {
|
||||
void serviceUsbLoggingPort() {}
|
||||
void serviceUsbTerminalPort() {}
|
||||
bool takeUsbTerminalSessionReset() { return false; }
|
||||
bool tryCompleteUsbTerminalSessionReset() { return true; }
|
||||
bool canAcceptUsbConsoleCommand() { return true; }
|
||||
Stream& usbConsolePort() { return console; }
|
||||
void noteUsbLoggingStatsCommand(const char*) {}
|
||||
bool isUsbLoggingWatchdogArmed() { return false; }
|
||||
namespace wireless {
|
||||
struct Control { bool pending() { return false; } };
|
||||
Control& control() { static Control value; return value; }
|
||||
}
|
||||
}
|
||||
struct Prefs { uint8_t powersaving_enabled = 0; } prefs;
|
||||
struct Sensors { void setPowerSavingEnabled(bool) {} } sensors;
|
||||
struct MyMesh {
|
||||
Prefs* _prefs = &prefs;
|
||||
Sensors* _sensors = &sensors;
|
||||
unsigned saves = 0;
|
||||
void savePrefs() { ++saves; }
|
||||
void set(const char* config, char* reply) { @SETTER@ }
|
||||
void get(const char* config, char* reply) { @GETTER@ }
|
||||
void handleUsbCommand(const char* command, char* reply) {
|
||||
if (!strncmp(command, "set ", 4)) set(command + 4, reply);
|
||||
else if (!strncmp(command, "get ", 4)) get(command + 4, reply);
|
||||
else strcpy(reply, "Unknown");
|
||||
}
|
||||
void cancelPendingSerialOutput() {}
|
||||
bool hasPendingSerialOutput() { return false; }
|
||||
Prefs* getNodePrefs() { return _prefs; }
|
||||
unsigned getPowerSaveSleepSeconds(unsigned limit) { return limit; }
|
||||
bool millisHasNowPassed(unsigned deadline) { return millis() > deadline; }
|
||||
} the_mesh;
|
||||
static ESP32Board board;
|
||||
static constexpr unsigned POWERSAVING_FIRSTSLEEP_SECS = 120;
|
||||
static constexpr size_t LOCAL_SERIAL_COMMAND_MAX = 159;
|
||||
static char command[LOCAL_SERIAL_COMMAND_MAX + 2] = {};
|
||||
static bool command_overflow = false;
|
||||
@COMMAND_PUMP@
|
||||
static void power_loop() { @POWER_LOOP@ }
|
||||
|
||||
static void emit() {
|
||||
if (!console.output.empty()) {
|
||||
std::cout << "DATA ";
|
||||
for (uint8_t c : console.output) printf("%02x", c);
|
||||
std::cout << '\n'; console.output.clear();
|
||||
}
|
||||
std::cout << "STATE " << unsigned(prefs.powersaving_enabled) << ' '
|
||||
<< sleep_calls << ' ' << millis() << '\n';
|
||||
std::cout << "DONE\n" << std::flush;
|
||||
}
|
||||
int main() {
|
||||
attachHost(true); // The Web Serial port is open, with no DTR/keepalive.
|
||||
Serial.terminal_open = false;
|
||||
mesh::logging_enabled = false;
|
||||
std::string line;
|
||||
while (std::getline(std::cin, line)) {
|
||||
if (line.rfind("WRITE ", 0) == 0) {
|
||||
// A USB peripheral suspended in light sleep cannot deliver new bytes.
|
||||
if (sleep_calls == 0) {
|
||||
for (size_t n = 6; n < line.size(); n += 2) {
|
||||
unsigned byte; assert(n + 1 < line.size());
|
||||
assert(sscanf(line.c_str() + n, "%2x", &byte) == 1);
|
||||
console.input.push_back(uint8_t(byte));
|
||||
}
|
||||
while (console.available()) serviceCommandInterfaces();
|
||||
}
|
||||
} else if (line.rfind("TIME ", 0) == 0) {
|
||||
mock_millis = std::stoul(line.substr(5)); power_loop();
|
||||
} else if (line == "CLOSE") {
|
||||
console.input.clear(); console.output.clear(); command[0] = 0;
|
||||
} else if (line == "HOST_OFF") {
|
||||
attachHost(false);
|
||||
} else { assert(false && "Unknown peripheral operation"); }
|
||||
emit();
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
// Compile the production WebConfig stop/finalize methods with peripheral
|
||||
// doubles. Delayed HTTP request destruction must never turn OTA WiFi off.
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
|
||||
static uint32_t millis() { return 123; }
|
||||
struct WebConfigBatch {
|
||||
static uint32_t scheduleAt(uint32_t now, uint32_t delay) { return now + delay; }
|
||||
};
|
||||
constexpr int WIFI_STA = 1;
|
||||
constexpr int WIFI_AP = 2;
|
||||
struct WiFiFake {
|
||||
bool sta = true, ap = false, connected = true, allow_sta = true, allow_ap_stop = true;
|
||||
unsigned stops = 0, ap_stops = 0;
|
||||
bool enableSTA(bool on) { if (!allow_sta) return false; sta = on; return true; }
|
||||
bool softAPdisconnect(bool) { ++ap_stops; if (allow_ap_stop) ap = false; return false; }
|
||||
int getMode() const { return (sta ? WIFI_STA : 0) | (ap ? WIFI_AP : 0); }
|
||||
void mode(int value) { assert(value == WIFI_STA); sta = true; ap = false; }
|
||||
} WiFi;
|
||||
void stopOwnedWiFiRadio() {
|
||||
WiFi.sta = WiFi.ap = WiFi.connected = false;
|
||||
++WiFi.stops;
|
||||
}
|
||||
struct Server { bool listening = true; void end() { listening = false; } };
|
||||
struct DNS { void stop() {} };
|
||||
struct Callbacks { unsigned stops = 0; void onWebConfigStopped() { ++stops; } };
|
||||
struct WebConfigServer {
|
||||
enum Mode { MODE_OFF, MODE_LAN, MODE_SETUP };
|
||||
enum { STOP_WARN_MS = 1000, BATCH_IDLE = 0, BATCH_CONFIG = 0 };
|
||||
Server server;
|
||||
Server* _server = &server;
|
||||
DNS* _dns = nullptr;
|
||||
Callbacks callbacks;
|
||||
Callbacks* _cb = &callbacks;
|
||||
Mode _mode = MODE_LAN;
|
||||
bool _stopping = false, _keep_wifi_on_stop = false, _was_setup_ap = false;
|
||||
bool _owns_wifi = true, _stop_warned = false, _initial_setup = false;
|
||||
bool _retry_saved_wifi_in_setup = false, _setup_reconnect_in_progress = false;
|
||||
bool _batch_reboot_armed = false, _setup_wifi_handoff_pending = false;
|
||||
bool _admin_pwd_set = false, _board_cmds_probed = false;
|
||||
uint32_t _stop_warn_at = 0, _setup_started_at = 0, _connect_deadline = 0;
|
||||
uint32_t _setup_reconnect_deadline = 0, _reboot_at = 0, _batch_state = 0;
|
||||
uint32_t _batch_next = 0, _setup_wifi_handoff_deadline = 0, _batch_kind = 0;
|
||||
uint32_t _board_cmds = 0;
|
||||
char _wifi_ssid[32] = {}, _setup_wifi_handoff_ip[16] = {};
|
||||
char _session_token[16] = {}, _stats_json[16] = {};
|
||||
bool routes = true;
|
||||
void detachRoutes() { routes = false; }
|
||||
void closeTerminal() {}
|
||||
bool isRunning() const { return _mode != MODE_OFF; }
|
||||
bool isStopping() const { return _stopping; }
|
||||
void requestStop();
|
||||
bool stopForOTA(char*);
|
||||
void finalizeTeardown();
|
||||
};
|
||||
@METHODS@
|
||||
|
||||
int main() {
|
||||
for (bool setup : {false, true}) {
|
||||
for (bool owns_wifi : {false, true}) {
|
||||
WebConfigServer portal;
|
||||
portal._owns_wifi = owns_wifi;
|
||||
portal._was_setup_ap = setup;
|
||||
portal._mode = setup ? WebConfigServer::MODE_SETUP : WebConfigServer::MODE_LAN;
|
||||
WiFi = WiFiFake{};
|
||||
WiFi.ap = setup;
|
||||
WiFi.sta = !setup;
|
||||
WiFi.connected = !setup;
|
||||
char reply[160] = {};
|
||||
assert(portal.stopForOTA(reply));
|
||||
assert(strcmp(reply, "WebConfig stopped") == 0);
|
||||
assert(!portal.routes && !portal.server.listening && portal.isStopping());
|
||||
assert(WiFi.sta && !WiFi.ap && WiFi.stops == 0);
|
||||
assert(WiFi.connected == !setup);
|
||||
// OTA can now bind port 80 and create its own AP, while HTTP request
|
||||
// objects on the old server still have outstanding references.
|
||||
WiFi.ap = true;
|
||||
portal.requestStop(); // A duplicate stop cannot revoke the handoff.
|
||||
portal.finalizeTeardown(); // Existing requests have finally drained.
|
||||
assert(WiFi.ap && WiFi.sta && WiFi.stops == 0);
|
||||
assert(WiFi.ap_stops == unsigned(setup));
|
||||
assert(portal.callbacks.stops == 1 && !portal._keep_wifi_on_stop);
|
||||
}
|
||||
}
|
||||
WebConfigServer ordinary;
|
||||
WiFi = WiFiFake{};
|
||||
ordinary.requestStop();
|
||||
ordinary.finalizeTeardown();
|
||||
assert(WiFi.stops == 1 && !WiFi.sta); // Normal stop keeps its old behavior.
|
||||
WebConfigServer failed;
|
||||
failed._was_setup_ap = true;
|
||||
WiFi = WiFiFake{};
|
||||
WiFi.allow_sta = false;
|
||||
char reply[160] = {};
|
||||
assert(!failed.stopForOTA(reply));
|
||||
assert(strstr(reply, "ERR:") && failed.server.listening && !failed.isStopping());
|
||||
WiFi.allow_sta = true;
|
||||
WiFi.ap = true;
|
||||
WiFi.allow_ap_stop = false;
|
||||
assert(!failed.stopForOTA(reply));
|
||||
assert(strstr(reply, "WebConfig stopped; WiFi AP handoff failed"));
|
||||
assert(!failed.server.listening);
|
||||
}
|
||||
Vendored
+164
@@ -0,0 +1,164 @@
|
||||
// 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 <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <helpers/WirelessControl.h>
|
||||
|
||||
#define MESH_DEBUG_PRINTLN(...) ((void)0)
|
||||
constexpr int WL_CONNECTED = 3;
|
||||
constexpr int HTTP_GET = 0;
|
||||
struct IPAddress {
|
||||
std::string value;
|
||||
IPAddress() = default;
|
||||
IPAddress(unsigned a, unsigned b, unsigned c, unsigned d) {
|
||||
value = std::to_string(a) + "." + std::to_string(b) + "."
|
||||
+ std::to_string(c) + "." + std::to_string(d);
|
||||
}
|
||||
std::string toString() const { return value; }
|
||||
};
|
||||
struct WiFiFake {
|
||||
bool connected = true, ap = false, allow_ap = true;
|
||||
unsigned starts = 0;
|
||||
int status() const { return connected ? WL_CONNECTED : 0; }
|
||||
IPAddress localIP() const { return IPAddress(10, 20, 30, 40); }
|
||||
IPAddress softAPIP() const { return 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");
|
||||
return allow_ap;
|
||||
}
|
||||
bool softAP(const char* ssid, const char* password) {
|
||||
assert(strcmp(ssid, "MeshCore-OTA") == 0 && password == nullptr);
|
||||
++starts; ap = allow_ap; return ap;
|
||||
}
|
||||
void softAPdisconnect(bool) { ap = false; }
|
||||
} WiFi;
|
||||
static unsigned server_starts = 0;
|
||||
struct AsyncWebServerRequest {
|
||||
void send(int, const char*, const char*) {}
|
||||
void send(int, const char*, const char*, const char*) {}
|
||||
};
|
||||
struct AsyncWebServer {
|
||||
explicit AsyncWebServer(int) {}
|
||||
template<class F> void on(const char*, int, F) {}
|
||||
void begin() { ++server_starts; }
|
||||
void end() {}
|
||||
};
|
||||
struct Elegant {
|
||||
void setID(const char*) {}
|
||||
void begin(AsyncWebServer*) {}
|
||||
} AsyncElegantOTA;
|
||||
static int SPIFFS;
|
||||
struct ESP32Board {
|
||||
bool inhibit_sleep = false;
|
||||
#ifdef LIGHTWEIGHT_WIFI_OTA
|
||||
void* ota_server = nullptr;
|
||||
#else
|
||||
AsyncWebServer* ota_server = nullptr;
|
||||
#endif
|
||||
const char* getManufacturerName() const { return "test"; }
|
||||
bool startOTAUpdate(const char*, char*, bool);
|
||||
bool stopOTAUpdate(char*);
|
||||
};
|
||||
struct LightweightServer {
|
||||
bool allow = true;
|
||||
bool begin(ESP32Board*) { ++server_starts; return allow; }
|
||||
void end() {}
|
||||
} lightweight_ota_server;
|
||||
static bool lightweight_ota_started_ap;
|
||||
#ifdef COMPANION_RADIO_FULL
|
||||
static constexpr uint16_t LIGHTWEIGHT_OTA_PORT = 8080;
|
||||
#else
|
||||
static constexpr uint16_t LIGHTWEIGHT_OTA_PORT = 80;
|
||||
#endif
|
||||
struct esp_partition_t { unsigned address; };
|
||||
static bool valid_partitions = true;
|
||||
const esp_partition_t* esp_ota_get_running_partition() {
|
||||
static esp_partition_t value{0x10000}; return &value;
|
||||
}
|
||||
const esp_partition_t* esp_ota_get_next_update_partition(void*) {
|
||||
static esp_partition_t value{0x340000}; return valid_partitions ? &value : nullptr;
|
||||
}
|
||||
@METHODS@
|
||||
|
||||
int main() {
|
||||
ESP32Board board;
|
||||
struct { char text[160] = {}; uint32_t guard = 0x12345678; } reply;
|
||||
auto start = [&](bool ap, const char* ip, const char* instruction) {
|
||||
assert(board.startOTAUpdate("test node", reply.text, ap));
|
||||
assert(board.inhibit_sleep && board.ota_server);
|
||||
const std::string url = std::string("Started: http://") + ip
|
||||
#ifdef COMPANION_RADIO_FULL
|
||||
+ ":8080"
|
||||
#endif
|
||||
+ "/update";
|
||||
assert(strncmp(reply.text, url.c_str(), url.size()) == 0);
|
||||
assert(strstr(reply.text, instruction));
|
||||
assert(strlen(reply.text) < 160 && reply.guard == 0x12345678);
|
||||
};
|
||||
// Normal start reports the actual LAN address and does not disconnect it.
|
||||
start(false, "10.20.30.40", "same WiFi/LAN");
|
||||
assert(WiFi.connected && !WiFi.ap && WiFi.starts == 0 && server_starts == 1);
|
||||
start(false, "10.20.30.40", "same WiFi/LAN");
|
||||
assert(server_starts == 1);
|
||||
|
||||
// A failed switch must leave the existing uploader protected from sleep.
|
||||
WiFi.allow_ap = false;
|
||||
assert(!board.startOTAUpdate("test node", reply.text, true));
|
||||
assert(strcmp(reply.text, "ERR: OTA WiFi failed") == 0);
|
||||
assert(board.inhibit_sleep && board.ota_server && WiFi.connected);
|
||||
WiFi.allow_ap = true;
|
||||
// In particular, an already-running LAN server must honor a later `ap`.
|
||||
start(true, "192.168.4.1", "Join WiFi MeshCore-OTA");
|
||||
assert(WiFi.ap && WiFi.connected && server_starts == 1);
|
||||
assert(board.stopOTAUpdate(reply.text));
|
||||
assert(!board.inhibit_sleep && !board.ota_server && !WiFi.ap && WiFi.connected);
|
||||
|
||||
WiFi.connected = false;
|
||||
start(false, "192.168.4.1", "Join WiFi MeshCore-OTA");
|
||||
assert(WiFi.ap && !WiFi.connected);
|
||||
assert(board.stopOTAUpdate(reply.text));
|
||||
WiFi.allow_ap = false;
|
||||
assert(!board.startOTAUpdate("test node", reply.text, false));
|
||||
assert(strcmp(reply.text, "ERR: OTA WiFi failed") == 0);
|
||||
assert(!board.inhibit_sleep && !board.ota_server);
|
||||
// A pending wireless shutdown must not race a newly started uploader.
|
||||
struct Backend : mesh::wireless::Backend {
|
||||
uint8_t active = mesh::wireless::All;
|
||||
uint8_t available() const override { return mesh::wireless::All; }
|
||||
uint8_t enabled() const override { return active; }
|
||||
uint8_t clients() const override { return 0; }
|
||||
mesh::wireless::Result set(uint8_t service, bool on) override {
|
||||
if (on) active |= service; else active &= ~service;
|
||||
return mesh::wireless::Result::Done;
|
||||
}
|
||||
} backend;
|
||||
auto& control = mesh::wireless::control();
|
||||
control.begin(backend);
|
||||
assert(control.handle("set 2.4ghz off", reply.text, 160, 100,
|
||||
mesh::wireless::Independent));
|
||||
assert(!board.startOTAUpdate("test node", reply.text, false));
|
||||
assert(strstr(reply.text, "wireless change pending"));
|
||||
control.service(350);
|
||||
assert(control.masterOff() && control.blocked(mesh::wireless::WiFi));
|
||||
WiFi.allow_ap = true;
|
||||
start(false, "192.168.4.1", "Join WiFi MeshCore-OTA");
|
||||
assert(!control.masterOff() && !control.blocked(mesh::wireless::WiFi));
|
||||
assert(control.blocked(mesh::wireless::Bluetooth) && control.blocked(mesh::wireless::EspNow));
|
||||
assert(board.stopOTAUpdate(reply.text));
|
||||
#ifdef LIGHTWEIGHT_WIFI_OTA
|
||||
WiFi.allow_ap = true;
|
||||
lightweight_ota_server.allow = false;
|
||||
assert(!board.startOTAUpdate("test node", reply.text, true));
|
||||
assert(strcmp(reply.text, "ERR: OTA server failed") == 0);
|
||||
assert(!board.inhibit_sleep && !board.ota_server && !WiFi.ap);
|
||||
#ifdef COMPANION_RADIO_FULL
|
||||
valid_partitions = false;
|
||||
assert(!board.startOTAUpdate("test node", reply.text, false));
|
||||
assert(strstr(reply.text, "partition layout requires a USB firmware update"));
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run the stock website console against production CLI and sleep code.
|
||||
|
||||
USB/RTOS peripherals and time are simulated; the website's Web Streams client,
|
||||
repeater command pump, power-saving setters/getters, and ESP32Board methods are
|
||||
real. Physical browser/HIL testing remains necessary for USB enumeration.
|
||||
"""
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import urllib.request
|
||||
|
||||
from test_esp32_usb_sleep import HARNESS, board_method
|
||||
from test_hwcdc_tx_backport import body
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
COMMIT = "45a5257bf792b11a83f4c51c18d4f312addfb677"
|
||||
URL = f"https://raw.githubusercontent.com/meshcore-dev/flasher.meshcore.io/{COMMIT}/lib/console.js"
|
||||
SHA256 = "107d3ad867915aea8b2454c324327397a0f9e2f0469656046054011bc64475f9"
|
||||
SIZE = 2455
|
||||
|
||||
|
||||
def official_console():
|
||||
explicit = os.environ.get("MESHCORE_OFFICIAL_WEB_CONSOLE")
|
||||
path = Path(explicit) if explicit else Path.home() / ".cache/meshcore-tests" / SHA256 / "console.js"
|
||||
if not path.exists():
|
||||
if explicit:
|
||||
raise AssertionError(f"Missing explicit web console source: {path}")
|
||||
with urllib.request.urlopen(URL, timeout=45) as response:
|
||||
data = response.read(SIZE + 1)
|
||||
if len(data) != SIZE or hashlib.sha256(data).hexdigest() != SHA256:
|
||||
raise AssertionError("Pinned official console download failed its source gate")
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_bytes(data)
|
||||
data = path.read_bytes()
|
||||
if len(data) != SIZE or hashlib.sha256(data).hexdigest() != SHA256:
|
||||
raise AssertionError(f"Official web console SHA/size mismatch: {path}")
|
||||
return path.resolve()
|
||||
|
||||
|
||||
def firmware_harness(*, lose_sleep_guard=False, lose_crlf=False):
|
||||
# Retain the existing host peripheral boundary; use the production methods
|
||||
# and a virtual clock to exercise minutes/hours without slowing CI.
|
||||
source = HARNESS.split("int main()", 1)[0]
|
||||
methods = "\n".join(board_method(signature) for signature in (
|
||||
"void sleep(uint32_t secs) override", "bool isUsbDataConnected() override",
|
||||
"bool isUsbHostConnected() override"))
|
||||
if lose_sleep_guard:
|
||||
# A negative control must fail at the client-visible reply boundary,
|
||||
# rather than only inspecting whether an implementation string exists.
|
||||
methods = methods.replace("const bool usb_host_connected = isUsbHostConnected();",
|
||||
"const bool usb_host_connected = false;")
|
||||
methods = methods.replace("usb_host_sleep_policy.shouldKeepAwake(\n"
|
||||
" millis(), MESH_ESP32_USB_HOST_LOSS_SLEEP_GRACE_MS)",
|
||||
"false")
|
||||
source = source.replace("@METHODS@", methods)
|
||||
source = source.replace(" uint32_t irq = 48;", " void loop() {}\n uint32_t irq = 48;")
|
||||
cli = (ROOT / "src/helpers/CommonCLI.cpp").read_text()
|
||||
setter = body(cli, 'if (strcmp(config, "powersaving on") == 0')
|
||||
getter = body(cli, 'if (strcmp(config, "powersaving") == 0)')
|
||||
main = (ROOT / "examples/simple_repeater/main.cpp").read_text()
|
||||
pump = body(main, "static void __attribute__((noinline)) serviceCommandInterfaces()")
|
||||
if lose_crlf:
|
||||
pump = pump.replace('console.printf(" -> %s\\r\\n", reply);',
|
||||
'console.printf(" -> %s\\n", reply);')
|
||||
start = main.index(" bool can_power_save =")
|
||||
end = main.index(" if (the_mesh.getNodePrefs()->reboot_interval", start)
|
||||
power_loop = main[start:end]
|
||||
fixture = (ROOT / "test/fixtures/official_web_console/firmware.cpp").read_text()
|
||||
return source + (fixture.replace("@SETTER@", setter).replace("@GETTER@", getter)
|
||||
.replace("@COMMAND_PUMP@", pump).replace("@POWER_LOOP@", power_loop))
|
||||
|
||||
|
||||
class OfficialWebConsoleTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.node = shutil.which("node")
|
||||
cls.compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++")
|
||||
if not cls.node or not cls.compiler:
|
||||
raise unittest.SkipTest("Node 18+ and a host C++17 compiler are required")
|
||||
cls.console = official_console()
|
||||
|
||||
def run_contract(self, *, lose_sleep_guard=False, lose_crlf=False):
|
||||
with tempfile.TemporaryDirectory(prefix="official-web-console-") as tmp:
|
||||
tmp = Path(tmp)
|
||||
source = tmp / "firmware.cpp"
|
||||
source.write_text(firmware_harness(lose_sleep_guard=lose_sleep_guard,
|
||||
lose_crlf=lose_crlf), encoding="ascii")
|
||||
binary = tmp / "firmware"
|
||||
sanitizers = (["-fsanitize=address,undefined", "-fno-sanitize-recover=all",
|
||||
"-fno-pie", "-no-pie"] if sys.platform.startswith("linux") else [])
|
||||
result = subprocess.run([self.compiler, "-std=c++17", "-Wall", "-Wextra",
|
||||
"-DARDUINO_USB_MODE=1", "-DARDUINO_USB_CDC_ON_BOOT=1",
|
||||
"-DESP32_PLATFORM=1", "-DMESH_ESP32_USB_CONSOLE_COOPERATIVE=1",
|
||||
"-DMESH_USB_CONSOLE_COOPERATIVE=1", "-DMESH_USB_LOGGING_AVAILABLE=1",
|
||||
"-DCONFIG_TINYUSB_ENABLED=0", "-I", str(ROOT / "src"),
|
||||
*sanitizers, str(source), "-o", str(binary)],
|
||||
text=True, capture_output=True, timeout=60)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
result = subprocess.run([self.node,
|
||||
str(ROOT / "test/fixtures/official_web_console/contract.mjs"),
|
||||
str(self.console), str(binary)], text=True, capture_output=True, timeout=20)
|
||||
return result
|
||||
|
||||
def test_stock_console_crlf_idle_power_saving_and_reopen(self):
|
||||
result = self.run_contract()
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_missing_usb_sleep_guard_breaks_stock_console(self):
|
||||
result = self.run_contract(lose_sleep_guard=True)
|
||||
self.assertNotEqual(result.returncode, 0, "Negative control did not lose the console")
|
||||
self.assertIn("idle query", result.stderr)
|
||||
|
||||
def test_lf_only_reply_is_not_visible_in_stock_console(self):
|
||||
result = self.run_contract(lose_crlf=True)
|
||||
self.assertNotEqual(result.returncode, 0, "Negative control did not detect buffered replies")
|
||||
self.assertIn("power-saving setter", result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,221 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Exercise the actual browser OTA start/stop methods with fake WiFi and servers."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from test_replay_reset_integration import extract_braced
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
class WiFiOtaStartTest(unittest.TestCase):
|
||||
def compile_and_run(self, source, *flags):
|
||||
compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++")
|
||||
self.assertIsNotNone(compiler, "A host C++17 compiler is required")
|
||||
with tempfile.TemporaryDirectory(prefix="wifi-ota-start-") as directory:
|
||||
path = Path(directory)
|
||||
(path / "test.cpp").write_text(source, encoding="ascii")
|
||||
sanitizer = (["-fsanitize=address,undefined", "-fno-sanitize-recover=all",
|
||||
"-fno-pie", "-no-pie"] if sys.platform.startswith("linux") else [])
|
||||
result = subprocess.run([compiler, "-std=c++17", "-Wall", "-Wextra",
|
||||
*sanitizer, *flags, "-I", str(ROOT / "src"), str(path / "test.cpp"), "-o", str(path / "test")],
|
||||
capture_output=True, text=True, timeout=60)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
result = subprocess.run([str(path / "test")], capture_output=True,
|
||||
text=True, timeout=15)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
def test_network_instructions_and_switching_on_all_esp32_uploaders(self):
|
||||
source = (ROOT / "src/helpers/ESP32Board.cpp").read_text()
|
||||
lightweight, other = source.split("#elif defined(ADMIN_PASSWORD) && !defined(DISABLE_WIFI_OTA)", 1)
|
||||
fixture = (ROOT / "test/fixtures/wifi_ota_start.cpp").read_text()
|
||||
for name, implementation, flags in (
|
||||
("lightweight infrastructure", lightweight, ["-DLIGHTWEIGHT_WIFI_OTA=1"]),
|
||||
("lightweight companion", lightweight, ["-DLIGHTWEIGHT_WIFI_OTA=1", "-DCOMPANION_RADIO_FULL=1"]),
|
||||
("AsyncElegantOTA infrastructure", other, []),
|
||||
):
|
||||
with self.subTest(uploader=name):
|
||||
methods = "\n".join(extract_braced(implementation, signature) for signature in (
|
||||
"bool ESP32Board::startOTAUpdate(", "bool ESP32Board::stopOTAUpdate("))
|
||||
self.compile_and_run(fixture.replace("@METHODS@", methods), *flags)
|
||||
|
||||
def test_common_cli_preserves_actionable_ota_errors(self):
|
||||
cli = (ROOT / "src/helpers/CommonCLI.cpp").read_text()
|
||||
start = cli.index(' } else if (memcmp(command, "start ota", 9)')
|
||||
end = cli.index(' } else if (memcmp(command, "clock", 5)', start)
|
||||
fixture = r'''
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
struct Board {
|
||||
const char* error = "ERR: OTA WiFi failed";
|
||||
bool startOTAUpdate(const char*, char* reply, bool) {
|
||||
strcpy(reply, error); return false;
|
||||
}
|
||||
bool stopOTAUpdate(char* reply) { strcpy(reply, error); return false; }
|
||||
} board;
|
||||
struct Prefs { const char* node_name = "test"; } prefs;
|
||||
struct CLI {
|
||||
Board* _board = &board;
|
||||
Prefs* _prefs = &prefs;
|
||||
void run(const char* command, char* reply) {
|
||||
if (false) {
|
||||
@CLI@
|
||||
}
|
||||
}
|
||||
};
|
||||
int main() {
|
||||
CLI cli;
|
||||
char reply[160] = {};
|
||||
for (const char* command : {"start ota", "start ota ap", "stop ota"}) {
|
||||
cli.run(command, reply);
|
||||
assert(strcmp(reply, "ERR: OTA WiFi failed") == 0);
|
||||
board.error = "ERR: OTA server failed";
|
||||
cli.run(command, reply);
|
||||
assert(strcmp(reply, "ERR: OTA server failed") == 0);
|
||||
board.error = "";
|
||||
cli.run(command, reply);
|
||||
assert(strcmp(reply, "Error") == 0);
|
||||
board.error = "ERR: OTA WiFi failed";
|
||||
}
|
||||
}
|
||||
'''
|
||||
self.compile_and_run('#include <initializer_list>\n' + fixture.replace("@CLI@", cli[start:end]))
|
||||
|
||||
def test_common_cli_stops_webconfig_and_reports_the_handoff(self):
|
||||
cli = (ROOT / "src/helpers/CommonCLI.cpp").read_text()
|
||||
start = cli.index(' } else if (memcmp(command, "start ota", 9)')
|
||||
end = cli.index(' } else if (memcmp(command, "clock", 5)', start)
|
||||
fixture = r'''
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
struct Callbacks {
|
||||
bool active = true, allow_stop = true;
|
||||
int stops = 0, bridge_stops = 0;
|
||||
bool isWebConfigActive() const { return active; }
|
||||
bool stopWebConfigForOTA(char* reply) {
|
||||
++stops;
|
||||
if (!allow_stop) { strcpy(reply, "ERR: handoff failed"); return false; }
|
||||
active = false; strcpy(reply, "WebConfig stopped"); return true;
|
||||
}
|
||||
void setBridgeState(bool enabled) { if (!enabled) ++bridge_stops; }
|
||||
} callbacks;
|
||||
struct Board {
|
||||
bool allow_start = true;
|
||||
int starts = 0;
|
||||
bool startOTAUpdate(const char*, char* reply, bool) {
|
||||
assert(!callbacks.active); ++starts;
|
||||
strcpy(reply, allow_start ? "Started: http://10.20.30.40/update - Use same WiFi/LAN"
|
||||
: "ERR: OTA WiFi failed");
|
||||
return allow_start;
|
||||
}
|
||||
bool stopOTAUpdate(char*) { return true; }
|
||||
} board;
|
||||
struct Prefs { const char* node_name = "test"; bool bridge_enabled = false; } prefs;
|
||||
struct CLI {
|
||||
Board* _board = &board;
|
||||
Prefs* _prefs = &prefs;
|
||||
Callbacks* _callbacks = &callbacks;
|
||||
void run(const char* command, char* reply) {
|
||||
if (false) {
|
||||
@CLI@
|
||||
}
|
||||
}
|
||||
};
|
||||
int main() {
|
||||
CLI cli;
|
||||
struct { char text[160] = {}; unsigned guard = 123; } reply;
|
||||
cli.run("start ota invalid", reply.text);
|
||||
assert(strstr(reply.text, "usage") && callbacks.stops == 0 && board.starts == 0);
|
||||
callbacks.allow_stop = false;
|
||||
cli.run("start ota", reply.text);
|
||||
assert(strcmp(reply.text, "ERR: handoff failed") == 0 && board.starts == 0);
|
||||
callbacks.allow_stop = true;
|
||||
cli.run("start ota", reply.text);
|
||||
assert(strstr(reply.text, "http://10.20.30.40/update") && strstr(reply.text, "; WebConfig stopped"));
|
||||
assert(callbacks.stops == 2 && board.starts == 1 && callbacks.bridge_stops == 1);
|
||||
cli.run("start ota ap", reply.text);
|
||||
assert(!strstr(reply.text, "WebConfig stopped") && callbacks.stops == 2 && board.starts == 2);
|
||||
callbacks.active = true;
|
||||
board.allow_start = false;
|
||||
cli.run("start ota", reply.text);
|
||||
assert(strcmp(reply.text, "ERR: OTA WiFi failed; WebConfig stopped") == 0);
|
||||
assert(callbacks.stops == 3 && board.starts == 3 && callbacks.bridge_stops == 2);
|
||||
assert(reply.guard == 123);
|
||||
}
|
||||
'''
|
||||
self.compile_and_run(fixture.replace("@CLI@", cli[start:end]),
|
||||
"-DESP_PLATFORM=1", "-DADMIN_PASSWORD=1",
|
||||
"-DWITH_MQTT_BRIDGE=1", "-DLIGHTWEIGHT_WIFI_OTA=1")
|
||||
|
||||
def test_webconfig_teardown_cannot_turn_ota_wifi_off(self):
|
||||
source = (ROOT / "src/helpers/esp32/WebConfigServer.cpp").read_text()
|
||||
methods = "\n".join(extract_braced(source, signature) for signature in (
|
||||
"void WebConfigServer::requestStop(", "bool WebConfigServer::stopForOTA(",
|
||||
"void WebConfigServer::finalizeTeardown("))
|
||||
fixture = (ROOT / "test/fixtures/webconfig_ota_handoff.cpp").read_text()
|
||||
self.compile_and_run(fixture.replace("@METHODS@", methods))
|
||||
|
||||
def test_full_companion_cli_reports_webconfig_stop_and_hardware_errors(self):
|
||||
source = (ROOT / "examples/companion_radio/MyMesh.cpp").read_text()
|
||||
command = extract_braced(source, 'if (strcmp(command, "start ota") == 0')
|
||||
fixture = r'''
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
struct WebConfig {
|
||||
bool active = true, allowed = true;
|
||||
bool stopForOTA(char* reply) {
|
||||
if (!allowed) { strcpy(reply, "ERR: handoff failed"); return false; }
|
||||
active = false; return true;
|
||||
}
|
||||
} portal;
|
||||
struct Board {
|
||||
bool allowed = true;
|
||||
int starts = 0;
|
||||
bool startOTAUpdate(const char*, char* reply, bool) {
|
||||
assert(!portal.active); ++starts;
|
||||
strcpy(reply, allowed ? "Started: http://192.168.4.1:8080/update - Join WiFi MeshCore-OTA"
|
||||
: "ERR: this partition layout requires a USB firmware update");
|
||||
return allowed;
|
||||
}
|
||||
} board;
|
||||
struct Mesh {
|
||||
struct { const char* node_name = "test"; } _prefs;
|
||||
WebConfig* _webconfig = &portal;
|
||||
bool isWebConfigActiveOrStopping() { return portal.active; }
|
||||
bool run(const char* command, char* reply, size_t reply_size) {
|
||||
@COMMAND@
|
||||
return false;
|
||||
}
|
||||
};
|
||||
int main() {
|
||||
Mesh mesh;
|
||||
char reply[160] = {};
|
||||
portal.allowed = false;
|
||||
assert(mesh.run("start ota", reply, sizeof(reply)));
|
||||
assert(strcmp(reply, "ERR: handoff failed") == 0 && board.starts == 0);
|
||||
portal.allowed = true;
|
||||
assert(mesh.run("start ota", reply, sizeof(reply)));
|
||||
assert(strstr(reply, ":8080/update") && strstr(reply, "; WebConfig stopped"));
|
||||
assert(board.starts == 1);
|
||||
portal.active = true;
|
||||
board.allowed = false;
|
||||
assert(mesh.run("start ota ap", reply, sizeof(reply)));
|
||||
assert(strstr(reply, "partition layout") && strstr(reply, "; WebConfig stopped"));
|
||||
struct { char text[20]; unsigned guard = 42; } short_reply;
|
||||
assert(mesh.run("start ota", short_reply.text, sizeof(short_reply.text)));
|
||||
assert(short_reply.guard == 42 && short_reply.text[19] == 0);
|
||||
}
|
||||
'''
|
||||
self.compile_and_run(fixture.replace("@COMMAND@", command), "-DWITH_WEBCONFIG=1")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -319,7 +319,12 @@ int main(){
|
||||
#include <helpers/WirelessControl.h>
|
||||
#include <cassert>
|
||||
enum {WIFI_STA=1,WIFI_OFF=0,WIFI_IF_STA=0};
|
||||
struct {bool usb=false;bool isUsbDataConnected(){return usb;}} board;
|
||||
struct {
|
||||
bool usb=false, ota=false;
|
||||
bool isUsbDataConnected(){return usb;}
|
||||
bool isOTAUpdateRunning(){return ota;}
|
||||
bool stopOTAUpdate(char*){ota=false;return true;}
|
||||
} board;
|
||||
struct {
|
||||
int mode_value=WIFI_STA;bool autoreconnect=true;
|
||||
void setAutoReconnect(bool value){autoreconnect=value;}
|
||||
@@ -360,6 +365,13 @@ int main(){
|
||||
assert(!c.pending()&&!radio_driver.active&&::WiFi.mode_value==WIFI_OFF);
|
||||
c.handle("set 2.4ghz on",reply,sizeof(reply),300,Independent);c.service(550);
|
||||
assert(the_mesh.bridge&&the_mesh.web&&radio_driver.active);
|
||||
// OTA alone remains visible as an active WiFi owner after WebConfig drains.
|
||||
the_mesh.web=the_mesh.bridge=false;board.ota=true;
|
||||
assert(b.enabled()&mesh::wireless::WiFi);
|
||||
InfrastructureWirelessBackend ota_backend;
|
||||
assert(ota_backend.set(mesh::wireless::WiFi,false)==Result::Done);
|
||||
assert(!board.ota);
|
||||
the_mesh.web=the_mesh.bridge=true;
|
||||
// Individual WiFi off retains the ESP-NOW driver and stops both its own services.
|
||||
c.handle("set wifi off",reply,sizeof(reply),600,Independent);c.service(850);
|
||||
the_mesh.stopping=false;c.service(851);
|
||||
|
||||
Reference in new issue
Block a user