Harden storage transactions and deferred radio settings

This commit is contained in:
mikecarper committed 2026-09-15 16:01:46 -07:00
1 parent 8f09daaad8
commit b4315dabdb
26 files changed
+1770 -153

No files matched your search

+134 -18
View File
@@ -6,6 +6,10 @@
#if defined(ESP32_PLATFORM) || defined(RP2040_PLATFORM)
#include <helpers/ContactFileTransaction.h>
#endif
#if defined(ESP32_PLATFORM)
#include <errno.h>
#include <sys/stat.h>
#endif
#if COMPANION_FEATURE_JOHN
#include <helpers/bible/JohnBookmarkFiles.h>
#endif
@@ -57,6 +61,37 @@ DataStore::DataStore(FILESYSTEM& fs, FILESYSTEM& fsExtra, mesh::RTCClock& clock)
}
#endif
#if !defined(NRF52_PLATFORM)
// Unlike ESP32 FS::exists(), a metadata probe must not treat failure to open
// an existing file as evidence that its durable contents are absent.
static bool companionPathPresence(FILESYSTEM* fs, const char* path,
bool& present) {
#if defined(ESP32_PLATFORM)
(void)fs; // Companion storage is mounted at the default SPIFFS VFS path.
char vfs_path[96];
const int length = snprintf(vfs_path, sizeof(vfs_path), "/spiffs%s", path);
if (length < 0 || static_cast<size_t>(length) >= sizeof(vfs_path)) return false;
struct stat info;
const int result = ::stat(vfs_path, &info);
if (result != 0 && errno != ENOENT) return false;
present = result == 0;
#elif defined(STM32_PLATFORM)
struct lfs_info info;
fs->_lockFS();
const int result = lfs_stat(fs->_getFS(), path, &info);
fs->_unlockFS();
if (result != 0 && result != LFS_ERR_NOENT) return false;
present = result == 0;
#else
// Arduino-Pico's exists() uses lfs_stat(), not an open operation.
// Its public API hides metadata errors; existing-file open/read failures
// are distinguishable below, but metadata I/O failure is not.
present = fs->exists(path);
#endif
return true;
}
#endif
static File openWrite(FILESYSTEM* fs, const char* filename) {
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
fs->remove(filename);
@@ -524,13 +559,39 @@ bool DataStore::formatFileSystem() {
_identity_creation_blocked = false;
_prefs_load_incomplete = false;
}
#else
if (success) {
_identity_creation_blocked = false;
_prefs_load_incomplete = false;
_channel_load_incomplete = false;
#if !MESH_CONTACT_CACHE
_uncached_contact_load_incomplete = false;
#endif
}
#endif
return success;
#elif defined(RP2040_PLATFORM)
return LittleFS.format();
const bool success = LittleFS.format();
if (success) {
_identity_creation_blocked = false;
_prefs_load_incomplete = false;
_channel_load_incomplete = false;
_uncached_contact_load_incomplete = false;
}
return success;
#elif defined(ESP32)
bool fs_success = ((fs::SPIFFSFS *)_fs)->format();
esp_err_t nvs_err = nvs_flash_erase(); // no need to reinit, will be done by reboot
if (fs_success && nvs_err == ESP_OK) {
_identity_creation_blocked = false;
_prefs_load_incomplete = false;
_channel_load_incomplete = false;
#if MESH_CONTACT_CACHE
_cache_load_incomplete = false;
#else
_uncached_contact_load_incomplete = false;
#endif
}
return fs_success && (nvs_err == ESP_OK);
#else
#error "need to implement format()"
@@ -588,22 +649,32 @@ bool DataStore::loadMainIdentity(mesh::LocalIdentity &identity) {
_identity_creation_blocked = false;
return true;
#else
return identity_store.load("_main", identity);
#endif
}
bool DataStore::canCreateMainIdentity() const {
#if defined(NRF52_PLATFORM)
return !_identity_creation_blocked;
bool identity_exists = false;
#if defined(STM32_PLATFORM)
const char* path = "/_main.id";
#else
const char* path = "/identity/_main.id";
#endif
if (!companionPathPresence(_fs, path, identity_exists)) {
_identity_creation_blocked = true;
return false;
}
if (!identity_exists) return false;
if (!identity_store.load("_main", identity)) {
_identity_creation_blocked = true;
return false;
}
_identity_creation_blocked = false;
return true;
#endif
}
bool DataStore::canCreateMainIdentity() const {
return !_identity_creation_blocked;
}
bool DataStore::saveMainIdentity(const mesh::LocalIdentity &identity) {
#if defined(NRF52_PLATFORM)
if (_identity_creation_blocked) return false;
#endif
return identity_store.save("_main", identity);
}
@@ -1399,6 +1470,9 @@ bool DataStore::writeContactPage(DataStoreHost* host, uint8_t page,
#endif
void DataStore::loadContacts(DataStoreHost* host) {
#if !defined(NRF52_PLATFORM) && !MESH_CONTACT_CACHE
if (_uncached_contact_load_incomplete) return;
#endif
#if MESH_CONTACT_CACHE
_cache_host = host;
mesh::contactPathStorage().attach(this);
@@ -1408,13 +1482,34 @@ void DataStore::loadContacts(DataStoreHost* host) {
if (_cache_load_incomplete) return;
#if defined(ESP32_PLATFORM)
_contact_path_reader.close();
if (!mesh::ContactFileTransaction::recover(_getContactsChannelsFS(), "/contacts3")) {
#endif
#endif
#if !defined(NRF52_PLATFORM)
bool contacts_exist = false;
bool contacts_metadata_ready = companionPathPresence(
_getContactsChannelsFS(), "/contacts3", contacts_exist);
#endif
#if defined(ESP32_PLATFORM) || defined(RP2040_PLATFORM)
bool backup_exists = false;
contacts_metadata_ready = contacts_metadata_ready && companionPathPresence(
_getContactsChannelsFS(), "/contacts3.bak", backup_exists);
const bool contacts_required = contacts_exist || backup_exists;
contacts_metadata_ready = contacts_metadata_ready
&& mesh::ContactFileTransaction::recover(_getContactsChannelsFS(), "/contacts3")
&& companionPathPresence(_getContactsChannelsFS(), "/contacts3", contacts_exist)
&& (!contacts_required || contacts_exist);
#endif
#if !defined(NRF52_PLATFORM)
if (!contacts_metadata_ready) {
MESH_DEBUG_PRINTLN("DataStore: contact transaction recovery failed");
#if MESH_CONTACT_CACHE
_cache_load_incomplete = true;
#else
_uncached_contact_load_incomplete = true;
#endif
return;
}
#endif
#endif
#if defined(NRF52_PLATFORM)
// loadContacts() is also used after an identity import. Rebuild runtime
// slot ownership from disk so stale pointers/slots from the previous in-RAM
@@ -1524,9 +1619,13 @@ void DataStore::loadContacts(DataStoreHost* host) {
#endif
File file = openRead(_getContactsChannelsFS(), "/contacts3");
#if MESH_CONTACT_CACHE && defined(ESP32_PLATFORM)
if (!file && _getContactsChannelsFS()->exists("/contacts3")) {
#if !defined(NRF52_PLATFORM)
if (!file && contacts_exist) {
#if MESH_CONTACT_CACHE
_cache_load_incomplete = true;
#else
_uncached_contact_load_incomplete = true;
#endif
return;
}
#endif
@@ -1553,9 +1652,13 @@ void DataStore::loadContacts(DataStoreHost* host) {
if (file) {
bool full = false;
uint16_t record_index = 0;
#if MESH_CONTACT_CACHE && defined(ESP32_PLATFORM)
#if !defined(NRF52_PLATFORM)
if (file.size() % mesh::storage::CONTACT_RECORD_SIZE != 0) {
#if MESH_CONTACT_CACHE
_cache_load_incomplete = true;
#else
_uncached_contact_load_incomplete = true;
#endif
file.close();
return;
}
@@ -1567,7 +1670,7 @@ void DataStore::loadContacts(DataStoreHost* host) {
bool legacy_host_refused = false;
#endif
while (!full
#if MESH_CONTACT_CACHE && defined(ESP32_PLATFORM)
#if !defined(NRF52_PLATFORM)
&& record_index < file.size() / mesh::storage::CONTACT_RECORD_SIZE
#endif
#if defined(NRF52_PLATFORM)
@@ -1578,6 +1681,8 @@ void DataStore::loadContacts(DataStoreHost* host) {
if (file.read(record, sizeof(record)) != sizeof(record)) {
#if MESH_CONTACT_CACHE
_cache_load_incomplete = true;
#elif !defined(NRF52_PLATFORM)
_uncached_contact_load_incomplete = true;
#endif
#if defined(NRF52_PLATFORM)
legacy_read_failed = true;
@@ -1591,6 +1696,8 @@ void DataStore::loadContacts(DataStoreHost* host) {
if (path_unavailable) {
#if MESH_CONTACT_CACHE
_cache_load_incomplete = true;
#elif !defined(NRF52_PLATFORM)
_uncached_contact_load_incomplete = true;
#endif
#if defined(NRF52_PLATFORM)
legacy_read_failed = true;
@@ -1613,6 +1720,8 @@ void DataStore::loadContacts(DataStoreHost* host) {
full = true;
#if MESH_CONTACT_CACHE
_cache_load_incomplete = true;
#elif !defined(NRF52_PLATFORM)
_uncached_contact_load_incomplete = true;
#endif
#if defined(NRF52_PLATFORM)
_contact_slots.release(slot);
@@ -1717,7 +1826,11 @@ bool DataStore::saveContacts(DataStoreHost* host, bool (*filter)(const ContactIn
paths.endCommit(success);
return success;
#else
File file = openWrite(_getContactsChannelsFS(), "/contacts3");
#if defined(STM32_PLATFORM)
mesh::AtomicFileWriter file(_getContactsChannelsFS(), "/contacts3");
#else
mesh::ContactFileTransaction file(_getContactsChannelsFS(), "/contacts3");
#endif
bool success = (bool)file;
if (file) {
uint32_t idx = 0;
@@ -1748,7 +1861,7 @@ bool DataStore::saveContacts(DataStoreHost* host, bool (*filter)(const ContactIn
idx++; // advance to next contact
}
file.close();
success = file.commit(success);
}
return success;
#endif
@@ -1929,6 +2042,9 @@ bool DataStore::hasPendingContactWrites() const {
bool DataStore::hasIncompleteContactLoad() const {
#if !defined(NRF52_PLATFORM)
if (_channel_load_incomplete) return true;
#if !MESH_CONTACT_CACHE
if (_uncached_contact_load_incomplete) return true;
#endif
#endif
#if MESH_CONTACT_CACHE
if (_cache_load_incomplete) return true;
+4 -1
View File
@@ -39,9 +39,13 @@ class DataStore
FILESYSTEM* _configuredFsExtra;
mesh::RTCClock* _clock;
IdentityStore identity_store;
bool _identity_creation_blocked = false;
bool _prefs_load_incomplete = false;
#if !defined(NRF52_PLATFORM)
bool _channel_load_incomplete = false;
#if !MESH_CONTACT_CACHE
bool _uncached_contact_load_incomplete = false;
#endif
#endif
#if MESH_CONTACT_CACHE
DataStoreHost* _cache_host = nullptr;
@@ -66,7 +70,6 @@ class DataStore
mesh::storage::DirtyPageSet _dirty_contact_pages;
mesh::storage::DirtyPageSet _unread_contact_pages;
bool _contact_load_incomplete = false;
bool _identity_creation_blocked = false;
bool _primary_storage_unavailable = false;
bool _secondary_authority_unknown = false;
uint32_t _contact_page_generations[mesh::storage::CONTACT_PAGE_COUNT];
+19
View File
@@ -62,6 +62,8 @@
#if COMPANION_FEATURE_OTA_CLI
#include <helpers/ota/OtaCli.h>
#include <helpers/ota/OtaContext.h>
#include <helpers/ota/CompanionOtaConfig.h>
#endif
#if defined(WITH_MQTT_BRIDGE) && defined(ESP32_PLATFORM) && defined(WIFI_SSID)
@@ -1714,6 +1716,10 @@ void MyMesh::begin(bool has_display, bool radio_available) {
#if defined(ENABLE_OTA)
mesh::ota::beginSpeedConfig(_store->getPrimaryFS());
#endif
#if COMPANION_FEATURE_OTA_CLI
mesh::ota::beginCompanionOtaConfig(_store->getPrimaryFS());
mesh::ota::ota_set_context_config_loader(mesh::ota::loadCompanionOtaConfig);
#endif
const bool identity_loaded = _store->loadMainIdentity(self_id);
const bool is_new_install = !identity_loaded
@@ -3267,7 +3273,20 @@ bool MyMesh::handleLocalControlCommand(const char* command, char* reply,
if (strncmp(command, "ota", 3) == 0
&& (command[3] == 0 || command[3] == ' ')) {
char ota_reply[160] = {0};
if (!mesh::ota::ota_acquire_context(ota_reply, sizeof(ota_reply))) {
snprintf(reply, reply_size, "%s", ota_reply);
return true;
}
auto& context = mesh::ota::ota_ctx();
const auto previous = mesh::ota::OtaConfigState::capture(context);
if (!mesh::ota::handle_ota_command(command, ota_reply, board)) return false;
if (context.config_dirty) {
if (!mesh::ota::saveCompanionOtaConfig(mesh::ota::OtaConfigState::capture(context))) {
previous.apply(context);
snprintf(ota_reply, sizeof(ota_reply), "ERR OTA settings save failed; settings unchanged");
}
context.config_dirty = false;
}
snprintf(reply, reply_size, "%s", ota_reply);
return true;
}
+10 -23
View File
@@ -3800,10 +3800,6 @@ void MyMesh::begin(FILESYSTEM *fs) {
#endif
saved_radio_apply_pending = !applySavedRadioParams();
if (!saved_radio_apply_pending) {
radio_driver.setTxPower(_prefs.tx_power_dbm);
radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain);
}
MESH_DEBUG_PRINTLN("RX Boosted Gain Mode: %s",
radio_driver.getRxBoostedGainMode() ? "Enabled" : "Disabled");
const bool fem_gain_changed = board.canControlLoRaFemLna()
@@ -4176,13 +4172,16 @@ bool MyMesh::applySavedRadioParams() {
}
#endif
// Each setter may independently defer while a packet is being received.
// Only complete recovery when the whole saved configuration was accepted.
if (radio_driver.supportsRxBoostedGainMode()
&& !radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain)) return false;
if (!applyRadioParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr, _cli.radioProfiles().primaryPreamble())) return false;
#if defined(USE_LR2021)
return radio_driver.configSideDetectors(_prefs.extra_sf, extra_sf_count, _prefs.bw);
#else
return true;
if (!radio_driver.configSideDetectors(_prefs.extra_sf, extra_sf_count, _prefs.bw)) return false;
#endif
return radio_driver.setTxPower(_prefs.tx_power_dbm);
}
void MyMesh::queueSavedRadioApply() {
@@ -4724,28 +4723,21 @@ void MyMesh::processScheduledRadioSettings() {
}
ScheduledRadioSetting& setting = scheduled_radio_settings[due_idx];
if (!_cli.radioProfiles().savePrimaryPreamble(setting.preamble)) {
if (!_cli.savePrimaryRadioParams(setting.freq, setting.bw, setting.sf,
setting.cr, setting.preamble)) {
scheduled_radio_save_retry_at = futureMillis(60000);
if (!scheduled_radio_save_retry_at) scheduled_radio_save_retry_at = 1;
break;
}
scheduled_radio_save_retry_at = 0;
_prefs.freq = setting.freq;
_prefs.bw = setting.bw;
_prefs.sf = setting.sf;
_prefs.cr = setting.cr;
setting.active = false;
setting.started = false;
saved_params_changed = true;
}
if (saved_params_changed) {
// Keep level-derived RX duty-cycle windows synchronized with the newly
// persisted SF/BW. Manual RX/sleep timings intentionally remain fixed.
CommonCLI::recalculateRxPowerSavingFromLevel(&_prefs);
_prefs.tx_power_dbm = mesh::clampLoRaTxPower(
_prefs.tx_power_dbm, _prefs.freq);
savePrefs();
// Only move the live radio after the complete tuple has committed. Failed
// entries remain queued and retain their previous durable configuration.
queueSavedRadioApply();
}
@@ -4790,12 +4782,7 @@ void MyMesh::processScheduledRadioSettings() {
refreshScheduledRadioState();
if (saved_radio_apply_pending && !temp_radio_handoff_pending
&& !scheduled_temp_radio_started && !apply_failed) {
// If begin() deferred the saved params to preserve a wake packet, its gain
// update was deferred for the same reason. Retry both at the first safe
// handoff; unsupported boosted-gain modes remain harmless here.
radio_driver.setRxBoostedGainMode(_prefs.rx_boosted_gain);
if (applySavedRadioParams()) {
radio_driver.setTxPower(_prefs.tx_power_dbm);
saved_radio_apply_pending = false;
temp_radio_applied = false;
if (radio_timing.isTemporary()) setTempRadioTiming(0);