Avoid repeated missing SPIFFS scans during ESP32 startup

This commit is contained in:
mikecarper committed 2026-10-05 14:12:44 -07:00
1 parent 47f10c0735
commit a0de63ab5e
16 files changed
+1158

No files matched your search

+27
View File
@@ -1,5 +1,9 @@
#include "MyMesh.h"
#if defined(ESP32_PLATFORM)
#include <helpers/esp32/BootFileSystem.h>
#endif
#include <helpers/UsbLogging.h>
#include <helpers/HilStartupTrace.h>
#include <helpers/FileRead.h>
#if MESH_ENABLE_TELEMETRY_HISTORY
#include <helpers/FilePresence.h>
@@ -3706,14 +3710,20 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
// OTA mesh-integration (receive/begin/loop) is centralized in mesh::Mesh - no per-example wiring.
void MyMesh::begin(FILESYSTEM *fs) {
mesh::hilStartupTrace("mesh_dispatcher_begin");
mesh::Mesh::begin(); // also starts OTA (ota_ctx().begin) for all roles
mesh::hilStartupTrace("mesh_dispatcher_ready");
_fs = fs;
// load persisted prefs
mesh::hilStartupTrace("mesh_prefs_begin");
_cli.loadPrefs(_fs);
mesh::hilStartupTrace("mesh_prefs_ready");
mesh::hilStartupTrace("mesh_management_begin");
_cli.beginManagement(*this, _fs);
#if MESH_ENABLE_TELEMETRY_HISTORY
loadTelemetryHistoryTxPrefs();
#endif
mesh::hilStartupTrace("mesh_management_ready");
#if defined(SIM_WIFI_SSID) && defined(WITH_MQTT_BRIDGE)
// Emulator builds (Wokwi) boot with fresh NVS every run. Seed WiFi so the
@@ -3734,9 +3744,12 @@ void MyMesh::begin(FILESYSTEM *fs) {
}
#endif
mesh::hilStartupTrace("mesh_acl_begin");
acl.load(_fs, self_id);
mesh::hilStartupTrace("mesh_acl_ready");
// TODO: key_store.begin();
region_map.load(_fs);
mesh::hilStartupTrace("mesh_regions_ready");
#if !defined(PORTABLE_MQTT_OBSERVER)
#if MESH_ENABLE_FLOOD_RULE_ENGINE
bool flood_filters_loaded = loadFloodPacketFilters();
@@ -3755,6 +3768,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
loadClockSyncPrefs();
#endif
#endif
mesh::hilStartupTrace("mesh_filters_ready");
// establish default-scope
{
@@ -3776,6 +3790,11 @@ void MyMesh::begin(FILESYSTEM *fs) {
}
}
#if defined(ESP32_PLATFORM)
// Bridge/WebConfig startup can create tasks that read or write this FS.
mesh::endEsp32BootFileInventory();
#endif
mesh::hilStartupTrace("mesh_bridge_begin");
#if defined(WITH_BRIDGE)
if (_prefs.bridge_enabled) {
#ifdef WITH_MQTT_BRIDGE
@@ -3849,6 +3868,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
#if defined(WITH_MQTT_BRIDGE) && defined(WITH_ESPNOW_BRIDGE)
if (_prefs.espnow_bridge_enabled) setEspNowBridgeState(true);
#endif
mesh::hilStartupTrace("mesh_bridge_ready");
// Wire fault-alert reporter. begin() is safe regardless of bridge state.
// Passing `this` as the callbacks lets the reporter resolve a TransportKey
@@ -3859,6 +3879,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
_alerter.setBridge(mqtt_bridge);
#endif
mesh::hilStartupTrace("mesh_webconfig_begin");
#if defined(WITH_WEBCONFIG) && !defined(WEBCONFIG_NO_AUTO_AP)
bool start_webui = WebConfigServer::loadEnabled(false);
#ifdef WITH_MQTT_BRIDGE
@@ -3880,6 +3901,8 @@ void MyMesh::begin(FILESYSTEM *fs) {
}
#endif
mesh::hilStartupTrace("mesh_webconfig_ready");
mesh::hilStartupTrace("mesh_radio_begin");
saved_radio_apply_pending = !applySavedRadioParams();
MESH_DEBUG_PRINTLN("RX Boosted Gain Mode: %s",
radio_driver.getRxBoostedGainMode() ? "Enabled" : "Disabled");
@@ -3890,6 +3913,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
}
board.setLoRaFemPaGainEnabled(_prefs.radio_fem_txgain);
setRxPowerSaving(_prefs.rx_powersaving_enabled, _prefs.rx_ps_rx_us, _prefs.rx_ps_sleep_us);
mesh::hilStartupTrace("mesh_radio_ready");
board.attachDynamicPrefs(_prefs.getCustom());
@@ -3898,6 +3922,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
next_recent_repeater_sweep = futureMillis(RECENT_REPEATER_SWEEP_INTERVAL_MILLIS);
#if ENV_INCLUDE_GPS == 1
mesh::hilStartupTrace("mesh_gps_begin");
applyGpsPrefs();
#if MESH_ENABLE_TELEMETRY_HISTORY
if (sensors.getLocationProvider() != NULL) {
@@ -3905,6 +3930,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
MESH_DEBUG_PRINTLN("Telemetry GPS retention: %u days", (unsigned)gps_days);
}
#endif
mesh::hilStartupTrace("mesh_gps_ready");
#endif
#if MESH_ENABLE_TELEMETRY_HISTORY
@@ -3920,6 +3946,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
(unsigned)external_voltage_history.storageBytes());
}
#endif
mesh::hilStartupTrace("mesh_history_ready");
}
bool MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) {
+57
View File
@@ -5,6 +5,7 @@
#include <helpers/ui/DisplayPowerSettings.h>
#include <helpers/UsbLoggingWatchdog.h>
#include <helpers/UsbLoggingClientActivity.h>
#include <helpers/HilStartupTrace.h>
#if MESH_PACKET_LOGGING
#include <helpers/SerialPacketLog.h>
#endif
@@ -15,6 +16,7 @@
#endif
#if defined(ESP32_PLATFORM)
#include <helpers/ESP32TrueRandom.h>
#include <helpers/esp32/BootFileSystem.h>
#endif
#if defined(NRF52_PLATFORM)
#include <helpers/nrf52/InternalPrimaryFsBoot.h>
@@ -105,12 +107,16 @@ void setup() {
#if MESH_PACKET_LOGGING
mesh::serialLogBegin();
#endif
mesh::hilStartupTrace("serial_ready");
mesh::hilStartupTrace("board_begin");
board.begin();
mesh::hilStartupTrace("board_ready");
#ifdef HAS_EXTERNAL_WATCHDOG
external_watchdog.begin();
#endif
mesh::hilStartupTrace("fs_begin");
FILESYSTEM* fs;
#if defined(NRF52_PLATFORM)
bool volatile_primary_fs = false;
@@ -146,7 +152,9 @@ void setup() {
#else
#error "need to define filesystem"
#endif
mesh::hilStartupTrace("fs_ready");
#ifdef DISPLAY_CLASS
mesh::hilStartupTrace("display_begin");
mesh::ui::loadDisplayPowerSettings(fs, false);
mesh::ui::StartupScreen startup_screen;
display_ready = display.begin();
@@ -154,6 +162,7 @@ void setup() {
startup_screen.begin(&display,
board.isExternalPowered() || board.isUsbHostConnected());
}
mesh::hilStartupTrace("display_ready");
#endif
// Let serial settle after the display is already showing startup.
@@ -165,6 +174,7 @@ void setup() {
delay(5000);
#endif
mesh::hilStartupTrace("radio_begin");
int radioinit_attempts = 0;
while (!radio_init()) {
++radioinit_attempts;
@@ -198,9 +208,11 @@ void setup() {
}
delay(500);
}
mesh::hilStartupTrace("radio_ready");
fast_rng.begin(radio_driver.getRngSeed());
mesh::hilStartupTrace("identity_begin");
#if defined(NRF52_PLATFORM)
IdentityLoadResult identity_load = volatile_primary_fs
? store.loadResult("_main", the_mesh.self_id)
@@ -250,6 +262,15 @@ void setup() {
board.reboot();
return;
}
mesh::hilStartupTrace("identity_ready");
#if defined(ESP32_PLATFORM)
// Identity recovery/persistence has finished before taking the inventory.
// The mesh retains this FS view after setup, so the view has static lifetime.
static mesh::Esp32BootFileSystem boot_fs(*fs);
fs = &boot_fs;
boot_fs.beginInventory();
#endif
// Print the running firmware version at boot so it's visible after an OTA
// reboot without having to issue `ver` manually.
@@ -276,10 +297,15 @@ void setup() {
}
#endif
mesh::hilStartupTrace("sensors_begin");
sensors.begin();
mesh::hilStartupTrace("sensors_ready");
mesh::hilStartupTrace("mesh_begin");
the_mesh.begin(fs);
mesh::hilStartupTrace("mesh_ready");
mesh::hilStartupTrace("watchdog_begin");
#if defined(NRF52_PLATFORM)
mesh::loadUsbLoggingWatchdog(fs, !volatile_primary_fs,
[]() -> uint32_t { return rtc_clock.getCurrentTime(); });
@@ -287,6 +313,7 @@ void setup() {
mesh::loadUsbLoggingWatchdog(fs, true,
[]() -> uint32_t { return rtc_clock.getCurrentTime(); });
#endif
mesh::hilStartupTrace("watchdog_ready");
#if defined(NRF52_PLATFORM)
if (volatile_primary_fs) {
@@ -300,6 +327,7 @@ void setup() {
#ifdef DISPLAY_CLASS
if (display_ready) {
mesh::hilStartupTrace("display_task_begin");
#ifdef WITH_MQTT_BRIDGE
ui_task.setObserverPrefs(the_mesh.getObserverPrefs());
#endif
@@ -307,6 +335,7 @@ void setup() {
ui_task.setActivityWindow(the_mesh.getActivityWindow());
#endif
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
mesh::hilStartupTrace("display_task_ready");
}
#endif
@@ -319,10 +348,25 @@ void setup() {
the_mesh.sendSelfAdvertisement(16000, false);
#endif
mesh::hilStartupTrace("boot_complete_begin");
#if defined(ESP32_PLATFORM)
mesh::endEsp32BootFileInventory();
#endif
board.onBootComplete();
mesh::hilStartupTrace("boot_complete_ready");
}
static void __attribute__((noinline)) serviceCommandInterfaces() {
#if defined(MESH_HIL_STARTUP_TRACE) && MESH_HIL_STARTUP_TRACE
static bool first_cli_entry = true;
static bool first_cli_ready = true;
static bool first_cli_blocked = true;
static bool first_cli_rx = true;
if (first_cli_entry) {
first_cli_entry = false;
mesh::hilStartupTrace("cli_first_entry");
}
#endif
bool usb_ready = true;
#if MESH_USB_CONSOLE_COOPERATIVE
mesh::serviceUsbLoggingPort();
@@ -339,6 +383,19 @@ static void __attribute__((noinline)) serviceCommandInterfaces() {
&& mesh::canAcceptUsbConsoleCommand();
#endif
Stream& console = mesh::usbConsolePort();
#if defined(MESH_HIL_STARTUP_TRACE) && MESH_HIL_STARTUP_TRACE
if (usb_ready && first_cli_ready) {
first_cli_ready = false;
mesh::hilStartupTrace("cli_ready");
} else if (!usb_ready && first_cli_blocked) {
first_cli_blocked = false;
mesh::hilStartupTrace("cli_gate_blocked");
}
if (usb_ready && first_cli_rx && console.available() > 0) {
first_cli_rx = false;
mesh::hilStartupTrace("cli_rx_seen");
}
#endif
// Handle Serial CLI
int len = strlen(command);
bool line_complete = false;
+7
View File
@@ -1,4 +1,7 @@
#include "MyMesh.h"
#if defined(ESP32_PLATFORM)
#include <helpers/esp32/BootFileSystem.h>
#endif
#include <helpers/UsbLogging.h>
#if defined(ENABLE_OTA)
#include <helpers/ota/OtaContext.h>
@@ -1418,6 +1421,10 @@ void MyMesh::begin(FILESYSTEM *fs) {
#if ENV_INCLUDE_GPS == 1
applyGpsPrefs();
#endif
#if defined(ESP32_PLATFORM)
// MQTT/WebConfig startup below may create tasks that access persisted state.
mesh::endEsp32BootFileInventory();
#endif
#ifdef WITH_MQTT_BRIDGE
if (_prefs.bridge_enabled) {
// Defer construction to avoid static init crashes on ESP32 classic
+11
View File
@@ -13,6 +13,7 @@
#if defined(ESP32_PLATFORM)
#include <helpers/ESP32TrueRandom.h>
#include <helpers/esp32/BootFileSystem.h>
#endif
#if defined(NRF52_PLATFORM)
#include <helpers/nrf52/InternalPrimaryFsBoot.h>
@@ -173,6 +174,13 @@ void setup() {
return;
}
#if defined(ESP32_PLATFORM)
// Identity writes have finished; this FS view remains alive after setup.
static mesh::Esp32BootFileSystem boot_fs(*fs);
fs = &boot_fs;
boot_fs.beginInventory();
#endif
Stream& console = mesh::usbConsolePort();
console.print("Room ID: ");
mesh::Utils::printHex(console, the_mesh.self_id.pub_key, PUB_KEY_SIZE); console.println();
@@ -225,6 +233,9 @@ void setup() {
the_mesh.sendSelfAdvertisement(16000, false);
#endif
#if defined(ESP32_PLATFORM)
mesh::endEsp32BootFileInventory();
#endif
board.onBootComplete();
}