Harden storage transactions and deferred radio settings
This commit is contained in:
1 parent
8f09daaad8
commit
b4315dabdb
26 files changed
+1770
-153
No files matched your search
+102
-56
@@ -10,8 +10,10 @@
|
||||
#include "AdvertDataHelpers.h"
|
||||
#include "AlertReporter.h" // for alertReporterBannedChannelMatch()
|
||||
#include "sensors/EnvironmentI2CConfig.h"
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
#include "AtomicFileWriter.h"
|
||||
#elif defined(ESP32_PLATFORM) || defined(RP2040_PLATFORM)
|
||||
#include "ContactFileTransaction.h"
|
||||
#endif
|
||||
#include <RTClib.h>
|
||||
#include <ctype.h>
|
||||
@@ -25,6 +27,7 @@
|
||||
#if defined(ENABLE_OTA)
|
||||
#include "ota/OtaCli.h"
|
||||
#include "ota/OtaContext.h" // persist/sync OTA policy + signer allowlist with NodePrefs
|
||||
#include "ota/OtaConfigState.h"
|
||||
#include "ota/OtaSpeedConfig.h"
|
||||
#endif
|
||||
|
||||
@@ -735,6 +738,10 @@ static void formatSnrDbX4Short(char* dest, size_t dest_len, int16_t snr_x4) {
|
||||
|
||||
void CommonCLI::loadPrefs(FILESYSTEM* fs) {
|
||||
_radio_profiles.begin(fs, _callbacks->getProfileRadio(), _rtc, true);
|
||||
_prefs->primary_radio_preamble = _radio_profiles.primaryPreamble();
|
||||
#if !defined(WITH_MQTT_BRIDGE) && (defined(ESP32_PLATFORM) || defined(RP2040_PLATFORM))
|
||||
mesh::ContactFileTransaction::recover(fs, "/com_prefs");
|
||||
#endif
|
||||
#if defined(ENABLE_OTA)
|
||||
mesh::ota::beginSpeedConfig(fs);
|
||||
#endif
|
||||
@@ -863,6 +870,8 @@ void CommonCLI::loadPrefs(FILESYSTEM* fs) {
|
||||
#if MESH_USB_LOGGING_AVAILABLE
|
||||
mesh::setUsbLoggingEnabled(_prefs->usb_logging_enabled != 0);
|
||||
#endif
|
||||
_radio_profiles.adoptPrimaryPreamble(_prefs->primary_radio_preamble);
|
||||
_radio_profiles.stagePrimary(_prefs->primary_radio_preamble, false);
|
||||
}
|
||||
|
||||
#if defined(ENABLE_OTA)
|
||||
@@ -1261,6 +1270,14 @@ void CommonCLI::loadPrefsInt(FILESYSTEM* fs, const char* filename) {
|
||||
if (file.available() >= (int)sizeof(_prefs->bridge_format)) {
|
||||
file.read((uint8_t *)&_prefs->bridge_format,
|
||||
sizeof(_prefs->bridge_format));
|
||||
if (file.available() >= (int)sizeof(_prefs->primary_radio_preamble)) {
|
||||
uint16_t preamble = 0;
|
||||
if (file.read((uint8_t *)&preamble, sizeof(preamble)) == sizeof(preamble)
|
||||
&& (preamble == 0 || (preamble >= 8
|
||||
&& preamble <= mesh::RadioProfiles::MaxPreamble))) {
|
||||
_prefs->primary_radio_preamble = preamble;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1580,6 +1597,7 @@ static bool writeCommonPrefsImage(Writer& writer, NodePrefs* prefs) {
|
||||
WRITE_COMMON_PREFS(&prefs->usb_logging_enabled); // 861
|
||||
WRITE_COMMON_PREFS(&prefs->bridge_uart); // 862
|
||||
WRITE_COMMON_PREFS(&prefs->bridge_format); // 863
|
||||
WRITE_COMMON_PREFS(&prefs->primary_radio_preamble); // appended primary tuple field
|
||||
|
||||
#undef WRITE_COMMON_PREFS_BYTES
|
||||
#undef WRITE_COMMON_PREFS
|
||||
@@ -1589,10 +1607,14 @@ static bool writeCommonPrefsImage(Writer& writer, NodePrefs* prefs) {
|
||||
|
||||
void CommonCLI::savePrefs(FILESYSTEM* fs, PrefsSaveRouting::Scope scope) {
|
||||
const PrefsSaveRouting::Plan plan = PrefsSaveRouting::planFor(scope);
|
||||
if (plan.common) {
|
||||
_common_save_result_known = true;
|
||||
_common_save_succeeded = false;
|
||||
}
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// Observer builds use a verified temp/backup transaction for common prefs.
|
||||
// Radio and bridge changes must never leave a truncated boot-time image.
|
||||
if (plan.common) saveCommonPrefsImageAtomically(fs);
|
||||
if (plan.common) _common_save_succeeded = saveCommonPrefsImageAtomically(fs);
|
||||
if (plan.observer) {
|
||||
_observer_save_result_known = true;
|
||||
_observer_save_succeeded = saveMQTTPrefs(fs);
|
||||
@@ -1601,15 +1623,10 @@ void CommonCLI::savePrefs(FILESYSTEM* fs, PrefsSaveRouting::Scope scope) {
|
||||
#else
|
||||
// Observer-only saves are a no-op on roles with no observer preference image.
|
||||
if (!plan.common) return;
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
mesh::AtomicFileWriter file(fs, "/com_prefs");
|
||||
#elif defined(STM32_PLATFORM)
|
||||
fs->remove("/com_prefs");
|
||||
File file = fs->open("/com_prefs", FILE_O_WRITE);
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
File file = fs->open("/com_prefs", "w");
|
||||
#else
|
||||
File file = fs->open("/com_prefs", "w", true);
|
||||
mesh::ContactFileTransaction file(fs, "/com_prefs");
|
||||
#endif
|
||||
if (file) {
|
||||
uint8_t pad[8];
|
||||
@@ -1737,15 +1754,12 @@ void CommonCLI::savePrefs(FILESYSTEM* fs, PrefsSaveRouting::Scope scope) {
|
||||
file.write((uint8_t *)&_prefs->usb_logging_enabled, sizeof(_prefs->usb_logging_enabled)); // 861
|
||||
file.write((uint8_t *)&_prefs->bridge_uart, sizeof(_prefs->bridge_uart)); // 862
|
||||
file.write((uint8_t *)&_prefs->bridge_format, sizeof(_prefs->bridge_format)); // 863
|
||||
// next: 864
|
||||
file.write((uint8_t *)&_prefs->primary_radio_preamble, sizeof(_prefs->primary_radio_preamble)); // appended
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
if (!file.commit()) {
|
||||
_common_save_succeeded = file.commit();
|
||||
if (!_common_save_succeeded) {
|
||||
MESH_DEBUG_PRINTLN("ERROR: savePrefs atomic commit failed");
|
||||
}
|
||||
#else
|
||||
file.close();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -2028,7 +2042,14 @@ public:
|
||||
&& !_fs->rename("/com_prefs", "/com_prefs.bak")) {
|
||||
return false;
|
||||
}
|
||||
if (!_fs->rename("/com_prefs.tmp", "/com_prefs")) return false;
|
||||
if (!_fs->rename("/com_prefs.tmp", "/com_prefs")) {
|
||||
// An explicit failure must retain the previous durable configuration.
|
||||
// If restoration also fails, recovery keeps the backup authoritative.
|
||||
if (_fs->exists("/com_prefs.bak")) {
|
||||
_fs->rename("/com_prefs.bak", "/com_prefs");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (_fs->exists("/com_prefs.bak")) _fs->remove("/com_prefs.bak");
|
||||
return true;
|
||||
}
|
||||
@@ -2036,7 +2057,9 @@ public:
|
||||
void abort() {
|
||||
if (_open) _file.close();
|
||||
_open = false;
|
||||
if (_owns_temp && !_finished && _fs->exists("/com_prefs.tmp")) {
|
||||
// abort() is an explicit rejected write, unlike a power loss. Never leave
|
||||
// its candidate eligible to become the committed primary on a later boot.
|
||||
if (_owns_temp && _fs->exists("/com_prefs.tmp")) {
|
||||
_fs->remove("/com_prefs.tmp");
|
||||
}
|
||||
_finished = false;
|
||||
@@ -2076,21 +2099,12 @@ bool CommonCLI::recoverCommonPrefsFiles(FILESYSTEM* fs) {
|
||||
if (fs->exists("/com_prefs.bak")) fs->remove("/com_prefs.bak");
|
||||
return !fs->exists("/com_prefs.tmp") && !fs->exists("/com_prefs.bak");
|
||||
|
||||
case Action::PromoteTemp:
|
||||
if (fs->rename("/com_prefs.tmp", "/com_prefs")) {
|
||||
if (fs->exists("/com_prefs.bak")) fs->remove("/com_prefs.bak");
|
||||
return fs->exists("/com_prefs");
|
||||
}
|
||||
// The verified new image could not be published. Restore the previous
|
||||
// image so boot can continue with the last committed radio settings.
|
||||
if (fs->rename("/com_prefs.bak", "/com_prefs")) {
|
||||
if (fs->exists("/com_prefs.tmp")) fs->remove("/com_prefs.tmp");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
case Action::PromoteBackup:
|
||||
return fs->rename("/com_prefs.bak", "/com_prefs");
|
||||
// A complete temp was not necessarily accepted by the caller. Roll back
|
||||
// an interrupted or rejected commit to the last published image.
|
||||
if (!fs->rename("/com_prefs.bak", "/com_prefs")) return false;
|
||||
if (fs->exists("/com_prefs.tmp")) fs->remove("/com_prefs.tmp");
|
||||
return !fs->exists("/com_prefs.tmp");
|
||||
|
||||
case Action::DiscardTemp:
|
||||
// With no backup, a reset may have interrupted the very first write
|
||||
@@ -2455,6 +2469,39 @@ bool CommonCLI::saveObserverPrefs() {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool CommonCLI::saveCommonPrefs() {
|
||||
_common_save_result_known = false;
|
||||
_common_save_succeeded = false;
|
||||
_callbacks->savePrefs(PrefsSaveRouting::Scope::Common);
|
||||
return _common_save_result_known && _common_save_succeeded;
|
||||
}
|
||||
|
||||
bool CommonCLI::savePrimaryRadioParams(float freq, float bw, uint8_t sf,
|
||||
uint8_t cr, uint16_t preamble) {
|
||||
if (!isfinite(freq) || !isfinite(bw) || freq < 150.0f || freq > 2500.0f
|
||||
|| sf < 5 || sf > 12 || cr < 5 || cr > 8 || !isValidLoRaBandwidth(bw)
|
||||
|| !_radio_profiles.acceptsPrimary(freq, bw, sf, cr, preamble)) return false;
|
||||
const float old_freq = _prefs->freq, old_bw = _prefs->bw;
|
||||
const uint8_t old_sf = _prefs->sf, old_cr = _prefs->cr;
|
||||
const int8_t old_power = _prefs->tx_power_dbm;
|
||||
const uint32_t old_rx = _prefs->rx_ps_rx_us, old_sleep = _prefs->rx_ps_sleep_us;
|
||||
const uint16_t old_preamble = _prefs->primary_radio_preamble;
|
||||
_prefs->freq = freq; _prefs->bw = bw; _prefs->sf = sf; _prefs->cr = cr;
|
||||
_prefs->primary_radio_preamble = preamble;
|
||||
_prefs->tx_power_dbm = mesh::clampLoRaTxPower(old_power, freq);
|
||||
recalculateRxPowerSavingFromLevel(_prefs);
|
||||
if (!saveCommonPrefs()) {
|
||||
_prefs->freq = old_freq; _prefs->bw = old_bw;
|
||||
_prefs->sf = old_sf; _prefs->cr = old_cr;
|
||||
_prefs->tx_power_dbm = old_power;
|
||||
_prefs->rx_ps_rx_us = old_rx; _prefs->rx_ps_sleep_us = old_sleep;
|
||||
_prefs->primary_radio_preamble = old_preamble;
|
||||
return false;
|
||||
}
|
||||
_radio_profiles.adoptPrimaryPreamble(preamble);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t CommonCLI::buildAdvertData(uint8_t node_type, uint8_t* app_data) {
|
||||
if (_prefs->advert_loc_policy == ADVERT_LOC_NONE) {
|
||||
AdvertDataBuilder builder(node_type, _prefs->node_name);
|
||||
@@ -2695,20 +2742,31 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
&& otaCommandNeedsTempRadio(command)) {
|
||||
strcpy(reply, "LoRa OTA needs temp radio on every node. Run: tempradio 909.950,250,5,5,120");
|
||||
} else {
|
||||
if (!mesh::ota::ota_acquire_context(reply, 160)) return;
|
||||
const auto previous = mesh::ota::OtaConfigState::capture(mesh::ota::ota_ctx());
|
||||
mesh::ota::handle_ota_command(command, reply, *_board);
|
||||
if (mesh::ota::ota_context_if_active()
|
||||
&& mesh::ota::ota_ctx().config_dirty) { // a policy/key changed via the CLI -> persist it
|
||||
mesh::ota::OtaContext& c = mesh::ota::ota_ctx();
|
||||
_prefs->ota_autofetch = c.manager.autofetch();
|
||||
_prefs->ota_checkpoint_blocks = c.manager.checkpoint_blocks();
|
||||
_prefs->ota_advert_interval = c.manager.advert_mins();
|
||||
_prefs->ota_max_hops = c.manager.max_hops();
|
||||
_prefs->ota_autoinstall = c.autoinstall;
|
||||
_prefs->ota_signer_count = c.allow.count();
|
||||
for (uint8_t i = 0; i < c.allow.count() && i < MAX_OTA_SIGNERS; i++)
|
||||
memcpy(_prefs->ota_signers[i], c.allow.get(i), 32);
|
||||
_callbacks->savePrefs();
|
||||
c.config_dirty = false;
|
||||
const auto copy_policy = [this, &c]() {
|
||||
_prefs->ota_autofetch = c.manager.autofetch();
|
||||
_prefs->ota_checkpoint_blocks = c.manager.checkpoint_blocks();
|
||||
_prefs->ota_advert_interval = c.manager.advert_mins();
|
||||
_prefs->ota_max_hops = c.manager.max_hops();
|
||||
_prefs->ota_autoinstall = c.autoinstall;
|
||||
_prefs->ota_signer_count = c.allow.count();
|
||||
memset(_prefs->ota_signers, 0, sizeof(_prefs->ota_signers));
|
||||
for (uint8_t i = 0; i < c.allow.count() && i < MAX_OTA_SIGNERS; i++)
|
||||
memcpy(_prefs->ota_signers[i], c.allow.get(i), 32);
|
||||
};
|
||||
copy_policy();
|
||||
if (saveCommonPrefs()) {
|
||||
c.config_dirty = false;
|
||||
} else {
|
||||
previous.apply(c);
|
||||
copy_policy();
|
||||
strcpy(reply, "ERR OTA configuration could not be saved");
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
@@ -3657,22 +3715,10 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
&& sf >= 5 && sf <= 12 && cr >= 5 && cr <= 8
|
||||
&& isValidLoRaBandwidth(bw)
|
||||
&& _radio_profiles.acceptsPrimary(freq, bw, sf, cr, preamble)) {
|
||||
if (!_radio_profiles.savePrimaryPreamble(preamble)) {
|
||||
strcpy(reply, "Error: preamble could not be saved"); return;
|
||||
}
|
||||
_prefs->sf = sf;
|
||||
_prefs->cr = cr;
|
||||
_prefs->freq = freq;
|
||||
_prefs->bw = bw;
|
||||
const int8_t previous_power = _prefs->tx_power_dbm;
|
||||
_prefs->tx_power_dbm = mesh::clampLoRaTxPower(
|
||||
_prefs->tx_power_dbm, _prefs->freq);
|
||||
// Retune level-based RX powersaving to the new SF/BW. Persist only; the
|
||||
// radio itself is "reboot to apply", and begin() re-arms the timings then.
|
||||
recalcRxPowerSavingFromLevel(
|
||||
_prefs->rx_ps_level, _prefs->sf, _prefs->bw, _prefs->rx_ps_preamble, &_prefs->rx_ps_rx_us,
|
||||
&_prefs->rx_ps_sleep_us); // retune level-based timings to the loaded SF/BW
|
||||
_callbacks->savePrefs();
|
||||
if (!savePrimaryRadioParams(freq, bw, sf, cr, preamble)) {
|
||||
strcpy(reply, "Error: radio settings could not be saved"); return;
|
||||
}
|
||||
if (_prefs->tx_power_dbm != previous_power) {
|
||||
sprintf(reply, "OK - reboot to apply; TX power limited to %d dBm",
|
||||
(int)_prefs->tx_power_dbm);
|
||||
|
||||
@@ -188,6 +188,9 @@ public:
|
||||
// ESP-NOW bridge wire format. Appended at /com_prefs offset 863; zero keeps
|
||||
// existing installations on the original wrapped/checksummed/XOR format.
|
||||
uint8_t bridge_format = mesh::bridge::ESPNOW_FORMAT_WRAPPED;
|
||||
// Appended after the existing /com_prefs tail. Keep the preamble in the same
|
||||
// transaction as frequency/modulation; old images adopt /radio_profiles.
|
||||
uint16_t primary_radio_preamble = 0;
|
||||
uint8_t retry_preset = 0;
|
||||
uint8_t direct_retry_attempts = 0;
|
||||
uint16_t direct_retry_base_ms = 0;
|
||||
@@ -756,6 +759,8 @@ class CommonCLI {
|
||||
bool _observer_save_succeeded = false;
|
||||
#endif
|
||||
bool _com_prefs_needs_upgrade = false; // old-format legacy prefs detected; rewrite once after load
|
||||
bool _common_save_result_known = false;
|
||||
bool _common_save_succeeded = false;
|
||||
mesh::RadioProfileCLI _radio_profiles;
|
||||
|
||||
mesh::RTCClock* getRTCClock() { return _rtc; }
|
||||
@@ -793,6 +798,9 @@ class CommonCLI {
|
||||
bool handleObserverCommand(uint32_t sender_timestamp, char* command, char* reply);
|
||||
|
||||
public:
|
||||
bool saveCommonPrefs();
|
||||
bool savePrimaryRadioParams(float freq, float bw, uint8_t sf, uint8_t cr,
|
||||
uint16_t preamble);
|
||||
mesh::RadioProfileCLI& radioProfiles() { return _radio_profiles; }
|
||||
const mesh::RadioProfileCLI& radioProfiles() const { return _radio_profiles; }
|
||||
static bool calculateRxPowerSavingLevel(uint32_t level, uint8_t sf, float bw, uint32_t preamble,
|
||||
|
||||
@@ -4,19 +4,18 @@
|
||||
|
||||
// Recovery policy for a verified temp/backup preference transaction.
|
||||
// The writer moves the old primary to backup only after temp is complete.
|
||||
// Until the new primary is published, the backup remains authoritative.
|
||||
namespace CommonPrefsRecovery {
|
||||
|
||||
enum class Action : uint8_t {
|
||||
None,
|
||||
KeepPrimary,
|
||||
PromoteTemp,
|
||||
PromoteBackup,
|
||||
DiscardTemp,
|
||||
};
|
||||
|
||||
inline Action select(bool primary_exists, bool temp_exists, bool backup_exists) {
|
||||
if (primary_exists) return Action::KeepPrimary;
|
||||
if (backup_exists && temp_exists) return Action::PromoteTemp;
|
||||
if (backup_exists) return Action::PromoteBackup;
|
||||
if (temp_exists) return Action::DiscardTemp;
|
||||
return Action::None;
|
||||
|
||||
@@ -69,6 +69,9 @@ class RadioProfileCLI {
|
||||
uint32_t replyMutationGeneration() const { return remote_generation_; }
|
||||
bool finishReplyMutation(bool delivered);
|
||||
uint16_t primaryPreamble() const { return primary_preamble_; }
|
||||
// Infrastructure owns a newer, atomic primary tuple in /com_prefs. Adopting
|
||||
// its committed value changes saved intent only, never the running radio.
|
||||
void adoptPrimaryPreamble(uint16_t symbols) { primary_preamble_ = symbols; }
|
||||
bool savePrimaryPreamble(uint16_t symbols);
|
||||
bool acceptsPrimary(float freq, float bw, uint8_t sf, uint8_t cr, uint16_t preamble) const;
|
||||
RadioParamApplyResult applyPrimary(float freq, float bw, uint8_t sf, uint8_t cr,
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
#if defined(ENABLE_OTA) && (defined(COMPANION_RADIO_FULL) || COMPANION_FEATURE_OTA_CLI)
|
||||
#include "CompanionOtaConfig.h"
|
||||
#include <helpers/PersistentStoreFormat.h>
|
||||
#include <stdio.h>
|
||||
#if defined(ESP32_PLATFORM)
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
namespace mesh { namespace ota {
|
||||
namespace {
|
||||
FILESYSTEM* settings_fs = nullptr;
|
||||
bool held = false;
|
||||
constexpr size_t ImageSize = 16 + MAX_OTA_SIGNERS * 32;
|
||||
constexpr size_t CrcOffset = ImageSize - 4;
|
||||
const char* const Path = "/ota_config";
|
||||
const char* const Temp = "/ota_config.tmp";
|
||||
const char* const Backup = "/ota_config.bak";
|
||||
|
||||
// Some FS::exists implementations open the file and report an unreadable
|
||||
// existing image as absent. Metadata errors must not authorize replacement.
|
||||
bool pathPresence(const char* path, bool& present) {
|
||||
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
||||
struct lfs_info info;
|
||||
settings_fs->_lockFS();
|
||||
const int result = lfs_stat(settings_fs->_getFS(), path, &info);
|
||||
settings_fs->_unlockFS();
|
||||
if (result != 0 && result != LFS_ERR_NOENT) return false;
|
||||
present = result == 0;
|
||||
#elif defined(ESP32_PLATFORM)
|
||||
char vfs_path[48];
|
||||
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;
|
||||
#else
|
||||
// Arduino-Pico exposes only a boolean metadata result. It can distinguish
|
||||
// file-open failures below, but not missing metadata from metadata I/O.
|
||||
present = settings_fs->exists(path);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
bool removeIfPresent(const char* path) {
|
||||
bool present;
|
||||
return pathPresence(path, present) && (!present || settings_fs->remove(path));
|
||||
}
|
||||
|
||||
bool validState(const OtaConfigState& state) {
|
||||
return state.autofetch <= 2 && state.autoinstall <= 1 && state.hops <= 8
|
||||
&& state.checkpoint <= 4096 && state.advert <= 10080;
|
||||
}
|
||||
|
||||
bool decode(const uint8_t* image, OtaConfigState& state) {
|
||||
if (memcmp(image, "OC\1\0", 4) || image[7] > MAX_OTA_SIGNERS
|
||||
|| storage::readLE32(image + CrcOffset)
|
||||
!= storage::updateCRC32(0xffffffffU, image, CrcOffset)) return false;
|
||||
OtaConfigState candidate;
|
||||
candidate.autofetch = image[4]; candidate.autoinstall = image[5];
|
||||
candidate.hops = image[6];
|
||||
candidate.checkpoint = storage::readLE16(image + 8);
|
||||
candidate.advert = storage::readLE16(image + 10);
|
||||
if (!validState(candidate)) return false;
|
||||
for (uint8_t i = 0; i < image[7]; ++i) candidate.allow.add(image + 12 + i * 32);
|
||||
if (candidate.allow.count() != image[7]) return false;
|
||||
state = candidate;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool readImage(const char* path, uint8_t* image, OtaConfigState& state) {
|
||||
#if defined(NRF52_PLATFORM)
|
||||
File file(*settings_fs);
|
||||
if (!file.open(path, FILE_O_READ)) return false;
|
||||
#elif defined(STM32_PLATFORM)
|
||||
File file = settings_fs->open(path, FILE_O_READ);
|
||||
#else
|
||||
File file = settings_fs->open(path, "r");
|
||||
#endif
|
||||
if (!file) return false;
|
||||
const bool complete = file.size() == ImageSize
|
||||
&& file.read(image, ImageSize) == (int)ImageSize;
|
||||
file.close();
|
||||
return complete && decode(image, state);
|
||||
}
|
||||
}
|
||||
|
||||
void beginCompanionOtaConfig(FILESYSTEM* fs) {
|
||||
settings_fs = fs;
|
||||
held = false;
|
||||
}
|
||||
|
||||
bool loadCompanionOtaConfig(OtaConfigState& state) {
|
||||
if (!settings_fs) return false;
|
||||
uint8_t image[ImageSize];
|
||||
bool primary = false, backup = false;
|
||||
if (!pathPresence(Path, primary)) { held = true; return false; }
|
||||
if (primary && readImage(Path, image, state)) return true;
|
||||
if (primary) held = true;
|
||||
if (!pathPresence(Backup, backup)) { held = true; return false; }
|
||||
if (backup && readImage(Backup, image, state)) {
|
||||
// Keep the verified previous settings even if recovery cannot rename them.
|
||||
// An unreadable primary may only have suffered a transient read failure;
|
||||
// do not remove it to promote the fallback.
|
||||
if (primary || !settings_fs->rename(Backup, Path)) held = true;
|
||||
return true;
|
||||
}
|
||||
if (primary || backup) {
|
||||
held = true; // unreadable is not permission to overwrite with defaults
|
||||
return false;
|
||||
}
|
||||
state = OtaConfigState();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool saveCompanionOtaConfig(const OtaConfigState& state) {
|
||||
if (!settings_fs || held || !validState(state)) return false;
|
||||
uint8_t image[ImageSize] = {'O', 'C', 1, 0}, verify[ImageSize];
|
||||
OtaConfigState prior;
|
||||
bool had_primary = false;
|
||||
if (!pathPresence(Path, had_primary)) { held = true; return false; }
|
||||
if (had_primary && !readImage(Path, verify, prior)) {
|
||||
held = true;
|
||||
return false;
|
||||
}
|
||||
image[4] = state.autofetch; image[5] = state.autoinstall; image[6] = state.hops;
|
||||
image[7] = state.allow.count();
|
||||
storage::writeLE16(image + 8, state.checkpoint);
|
||||
storage::writeLE16(image + 10, state.advert);
|
||||
for (uint8_t i = 0; i < state.allow.count(); ++i)
|
||||
memcpy(image + 12 + i * 32, state.allow.get(i), 32);
|
||||
storage::writeLE32(image + CrcOffset,
|
||||
storage::updateCRC32(0xffffffffU, image, CrcOffset));
|
||||
if (!removeIfPresent(Temp)) return false;
|
||||
#if defined(NRF52_PLATFORM)
|
||||
File file(*settings_fs);
|
||||
if (!file.open(Temp, FILE_O_WRITE)) return false;
|
||||
#elif defined(STM32_PLATFORM)
|
||||
File file = settings_fs->open(Temp, FILE_O_WRITE);
|
||||
#else
|
||||
File file = settings_fs->open(Temp, "w");
|
||||
#endif
|
||||
if (!file) return false;
|
||||
const bool wrote = file.write(image, sizeof(image)) == sizeof(image);
|
||||
file.flush(); file.close();
|
||||
if (!wrote || !readImage(Temp, verify, prior)
|
||||
|| memcmp(image, verify, sizeof(image))) return false;
|
||||
if (!removeIfPresent(Backup)) return false;
|
||||
if (had_primary && !settings_fs->rename(Path, Backup)) return false;
|
||||
if (!settings_fs->rename(Temp, Path)) {
|
||||
if (had_primary && !settings_fs->rename(Backup, Path)) held = true;
|
||||
return false;
|
||||
}
|
||||
if (had_primary) settings_fs->remove(Backup);
|
||||
return true;
|
||||
}
|
||||
|
||||
} }
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <helpers/IdentityStore.h>
|
||||
#include "OtaConfigState.h"
|
||||
|
||||
namespace mesh { namespace ota {
|
||||
|
||||
// Separate from /new_prefs: preserve compatibility with existing Companions.
|
||||
// Initializing does not allocate an OTA context. Dynamic contexts load on use.
|
||||
void beginCompanionOtaConfig(FILESYSTEM* fs);
|
||||
bool loadCompanionOtaConfig(OtaConfigState& state);
|
||||
bool saveCompanionOtaConfig(const OtaConfigState& state);
|
||||
|
||||
} }
|
||||
+24
-23
@@ -950,9 +950,10 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
strcpy(reply, "ERR this target has no SD-backed OTA archive");
|
||||
#endif
|
||||
|
||||
// ---- policy config (persisted via NodePrefs). conservative defaults: autofetch/autoinstall off ----
|
||||
// ---- policy config (persisted by the caller). conservative defaults: autofetch/autoinstall off ----
|
||||
} else if (is_cmd(a, "config|cfg|set", &rest)) {
|
||||
const char* p = rest;
|
||||
const char* value = nullptr;
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
|
||||
if (strncmp(p, "cache ", 6) == 0 || strncmp(p, "sdseed ", 7) == 0) {
|
||||
const char* v = p + (p[0] == 'c' ? 6 : 7);
|
||||
@@ -967,38 +968,38 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
} else
|
||||
#endif
|
||||
#if defined(OTA_SEEDER_ONLY)
|
||||
if (strncmp(p, "autofetch ", 10) == 0 || strncmp(p, "autoinstall ", 12) == 0) {
|
||||
if (is_cmd(p, "autofetch|autoinstall", &value)) {
|
||||
strcpy(reply, "ERR seeder-only build keeps autofetch and autoinstall off");
|
||||
} else
|
||||
#endif
|
||||
if (strncmp(p, "autofetch ", 10) == 0) {
|
||||
const char* v = p + 10;
|
||||
uint8_t pol = strncmp(v, "any", 3) == 0 ? OtaManager::AUTOFETCH_ANY
|
||||
: strncmp(v, "signed", 6) == 0 ? OtaManager::AUTOFETCH_SIGNED
|
||||
: strncmp(v, "off", 3) == 0 ? OtaManager::AUTOFETCH_OFF : 0xFF;
|
||||
if (is_cmd(p, "autofetch", &value)) {
|
||||
uint8_t pol = strcmp(value, "any") == 0 ? OtaManager::AUTOFETCH_ANY
|
||||
: strcmp(value, "signed") == 0 ? OtaManager::AUTOFETCH_SIGNED
|
||||
: strcmp(value, "off") == 0 ? OtaManager::AUTOFETCH_OFF : 0xFF;
|
||||
if (pol == 0xFF) { strcpy(reply, "ERR usage: ota config autofetch <off|any|signed>"); return true; }
|
||||
c.manager.set_autofetch(pol); c.config_dirty = true; strcpy(reply, "OK autofetch updated (saved)");
|
||||
} else if (strncmp(p, "autoinstall ", 12) == 0) {
|
||||
const char* v = p + 12;
|
||||
uint8_t pol = strncmp(v, "trusted", 7) == 0 ? OtaContext::AUTOINSTALL_TRUSTED
|
||||
: strncmp(v, "off", 3) == 0 ? OtaContext::AUTOINSTALL_OFF : 0xFF;
|
||||
} else if (is_cmd(p, "autoinstall", &value)) {
|
||||
uint8_t pol = strcmp(value, "trusted") == 0 ? OtaContext::AUTOINSTALL_TRUSTED
|
||||
: strcmp(value, "off") == 0 ? OtaContext::AUTOINSTALL_OFF : 0xFF;
|
||||
if (pol == 0xFF) { strcpy(reply, "ERR usage: ota config autoinstall <off|trusted>"); return true; }
|
||||
c.autoinstall = pol; c.config_dirty = true; strcpy(reply, "OK autoinstall updated (saved)");
|
||||
} else if (strncmp(p, "checkpoint ", 11) == 0) { // resume checkpoint cadence (blocks; 0=never)
|
||||
long n = atol(p + 11);
|
||||
if (n < 0 || n > 4096) { strcpy(reply, "ERR usage: ota config checkpoint <0..4096> (blocks; 0=never)"); return true; }
|
||||
} else if (is_cmd(p, "checkpoint", &value)) { // resume checkpoint cadence (blocks; 0=never)
|
||||
uint32_t n;
|
||||
if (!mesh::cli::parseUnsignedIntegerStrict(value, n) || n > 4096) { strcpy(reply, "ERR usage: ota config checkpoint <0..4096> (blocks; 0=never)"); return true; }
|
||||
c.manager.set_checkpoint_blocks((uint16_t)n); c.config_dirty = true;
|
||||
sprintf(reply, "OK checkpoint every %ld blocks (saved)%s", n, n == 0 ? " - periodic resume disabled" : "");
|
||||
} else if (strncmp(p, "advert ", 7) == 0) { // beacon re-advertise cadence (minutes; 0=disable)
|
||||
long m = atol(p + 7);
|
||||
if (m < 0 || m > 10080) { strcpy(reply, "ERR usage: ota config advert <0..10080> (minutes; 0=disable)"); return true; }
|
||||
sprintf(reply, "OK checkpoint every %lu blocks (saved)%s", (unsigned long)n, n == 0 ? " - periodic resume disabled" : "");
|
||||
} else if (is_cmd(p, "advert", &value)) { // beacon re-advertise cadence (minutes; 0=disable)
|
||||
uint32_t m;
|
||||
if (!mesh::cli::parseUnsignedIntegerStrict(value, m) || m > 10080) { strcpy(reply, "ERR usage: ota config advert <0..10080> (minutes; 0=disable)"); return true; }
|
||||
c.manager.set_advert_mins((uint16_t)m); c.config_dirty = true;
|
||||
sprintf(reply, "OK re-advertise every %ld min (saved)%s", m, m == 0 ? " - periodic advert disabled" : "");
|
||||
} else if (strncmp(p, "hops ", 5) == 0) { // OTA flood reach in hops (0 = direct only)
|
||||
long h = atol(p + 5);
|
||||
if (h < 0 || h > 8) { strcpy(reply, "ERR usage: ota config hops <0..8> (hops; 0 = direct only)"); return true; }
|
||||
sprintf(reply, "OK re-advertise every %lu min (saved)%s", (unsigned long)m, m == 0 ? " - periodic advert disabled" : "");
|
||||
} else if (is_cmd(p, "hops", &value)) { // OTA flood reach in hops (0 = direct only)
|
||||
uint32_t h;
|
||||
if (!mesh::cli::parseUnsignedIntegerStrict(value, h) || h > 8) { strcpy(reply, "ERR usage: ota config hops <0..8> (hops; 0 = direct only)"); return true; }
|
||||
c.manager.set_max_hops((uint8_t)h); c.config_dirty = true;
|
||||
sprintf(reply, "OK OTA reach = %ld hop%s (saved)%s", h, h == 1 ? "" : "s", h == 0 ? " - direct only" : "");
|
||||
sprintf(reply, "OK OTA reach = %lu hop%s (saved)%s", (unsigned long)h, h == 1 ? "" : "s", h == 0 ? " - direct only" : "");
|
||||
} else if (*p) {
|
||||
strcpy(reply, "ERR unknown OTA config setting");
|
||||
} else { // show current policy
|
||||
uint8_t af = c.manager.autofetch();
|
||||
char speed[16]; formatSpeed(speed, sizeof(speed));
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "OtaManager.h"
|
||||
#include "SignerAllowlist.h"
|
||||
|
||||
namespace mesh { namespace ota {
|
||||
|
||||
// Policy only: copying this must never copy an active transfer/workspace.
|
||||
struct OtaConfigState {
|
||||
uint8_t autofetch = 0, autoinstall = 0, hops = OTA_HOP_LIMIT_DEFAULT;
|
||||
uint16_t checkpoint = OTA_CHECKPOINT_BLOCKS, advert = OTA_ADVERT_INTERVAL_MINS;
|
||||
SignerAllowlist allow;
|
||||
|
||||
template <typename Context> static OtaConfigState capture(const Context& context) {
|
||||
OtaConfigState state;
|
||||
state.autofetch = context.manager.autofetch();
|
||||
state.autoinstall = context.autoinstall;
|
||||
state.hops = context.manager.max_hops();
|
||||
state.checkpoint = context.manager.checkpoint_blocks();
|
||||
state.advert = context.manager.advert_mins();
|
||||
state.allow = context.allow;
|
||||
return state;
|
||||
}
|
||||
|
||||
template <typename Context> void apply(Context& context) const {
|
||||
#if defined(OTA_SEEDER_ONLY)
|
||||
context.manager.set_autofetch(0);
|
||||
context.autoinstall = 0;
|
||||
#else
|
||||
context.manager.set_autofetch(autofetch);
|
||||
context.autoinstall = autoinstall;
|
||||
#endif
|
||||
context.manager.set_max_hops(hops);
|
||||
context.manager.set_checkpoint_blocks(checkpoint);
|
||||
context.manager.set_advert_mins(advert);
|
||||
context.allow = allow;
|
||||
context.config_dirty = false;
|
||||
}
|
||||
};
|
||||
|
||||
} }
|
||||
@@ -7,6 +7,18 @@
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
namespace {
|
||||
bool (*context_config_loader)(OtaConfigState&) = nullptr;
|
||||
}
|
||||
|
||||
void ota_set_context_config_loader(bool (*load)(OtaConfigState&)) {
|
||||
context_config_loader = load;
|
||||
if (auto* context = ota_context_if_active()) {
|
||||
OtaConfigState restored;
|
||||
if (load && load(restored)) restored.apply(*context);
|
||||
}
|
||||
}
|
||||
|
||||
#if OTA_DYNAMIC_CONTEXT
|
||||
namespace {
|
||||
OtaContext* active_context = nullptr;
|
||||
@@ -101,6 +113,8 @@ bool ota_acquire_context(char* reply, size_t cap) {
|
||||
c.manager.set_max_hops(saved_hops);
|
||||
c.autoinstall = saved_autoinstall;
|
||||
c.allow = saved_allow;
|
||||
OtaConfigState restored;
|
||||
if (context_config_loader && context_config_loader(restored)) restored.apply(c);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "OtaDeflate.h"
|
||||
#include "OtaStore.h"
|
||||
#include "SignerAllowlist.h"
|
||||
#include "OtaConfigState.h"
|
||||
#include "OtaApply.h"
|
||||
#include "OtaFormat.h"
|
||||
#include "OtaByteIO.h"
|
||||
@@ -705,6 +706,8 @@ OtaContext& ota_ctx(); // process-wide context
|
||||
// Dynamic builds return null outside an acquired workspace. Static builds
|
||||
// always return their process-wide context.
|
||||
OtaContext* ota_context_if_active();
|
||||
// Optional role-specific policy reload; registration never allocates a context.
|
||||
void ota_set_context_config_loader(bool (*load)(OtaConfigState&));
|
||||
bool ota_acquire_context(char* reply, size_t cap);
|
||||
void ota_begin_context(uint32_t target, OtaSend send, void* ctx,
|
||||
const char* hw, const uint8_t* seeder_id);
|
||||
|
||||
Reference in new issue
Block a user