Share Companion hardware diagnostics and protect RXEN reconfiguration

This commit is contained in:
mikecarper committed 2026-10-23 08:57:51 -07:00
1 parent 04c4fdb240
commit 807343de8d
28 files changed
+1393 -277

No files matched your search

+7
View File
@@ -195,6 +195,12 @@ public:
// internally bounded operations can use this while retaining their own
// timeout. Boards without an explicit watchdog need no implementation.
virtual void serviceWatchdog() { /* no op */ }
// Query the actual hardware state without starting, feeding or stopping a
// watchdog. False means this board has no shared runtime status reader.
virtual bool getSystemWatchdogRunning(bool& running) const {
(void)running;
return false;
}
virtual void enterDeepSleep(uint32_t secs) { (void)secs; }
// Called by example setup() functions to signal that boot is complete.
// Boards may override to stop a boot-indicator LED sequence or similar.
@@ -298,6 +304,7 @@ public:
#endif
// Power management interface (boards with power management override these)
virtual bool supportsPowerManagement() const { return false; }
virtual bool isPwrMgtInitialised() const { return false; }
virtual bool isExternalPowered() { return false; }
virtual bool isUsbDataConnected() { return false; }
+2 -215
View File
@@ -98,36 +98,6 @@
#define RECENT_REPEATER_PREFIX_MAX_BYTES 3
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
static const uint32_t SDCARD_ACTION_COOLDOWN_MILLIS = 5UL * 60UL * 1000UL;
static void formatSdCardElapsed(char* out, size_t out_len, bool recorded,
uint32_t ran_at, uint32_t now) {
if (!recorded) {
snprintf(out, out_len, "never");
return;
}
const uint32_t elapsed_secs = (now - ran_at) / 1000UL;
if (elapsed_secs < 60UL) {
snprintf(out, out_len, "%lus ago", (unsigned long)elapsed_secs);
} else if (elapsed_secs < 60UL * 60UL) {
snprintf(out, out_len, "%lum %lus ago",
(unsigned long)(elapsed_secs / 60UL),
(unsigned long)(elapsed_secs % 60UL));
} else if (elapsed_secs < 24UL * 60UL * 60UL) {
snprintf(out, out_len, "%luh %lum ago",
(unsigned long)(elapsed_secs / 3600UL),
(unsigned long)((elapsed_secs / 60UL) % 60UL));
} else {
snprintf(out, out_len, "%lud %luh ago",
(unsigned long)(elapsed_secs / 86400UL),
(unsigned long)((elapsed_secs / 3600UL) % 24UL));
}
}
#endif
#include <helpers/radiolib/RXPowerSaving.h>
// Believe it or not, this std C function is busted on some platforms!
@@ -4061,194 +4031,11 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
bool CommonCLI::handleSdCardSetCmd(const char* config, char* reply) {
if (strncmp(config, "sdcard", 6) != 0 ||
(config[6] != 0 && config[6] != ' ')) {
return false;
}
const char* action_args = skipSpacesConst(config + 6);
bool is_format;
bool force = false;
if (strcmp(action_args, "format") == 0) {
is_format = true;
} else if (strcmp(action_args, "format --force") == 0) {
is_format = true;
force = true;
} else if (strcmp(action_args, "erase") == 0) {
is_format = false;
} else if (strcmp(action_args, "erase --force") == 0) {
is_format = false;
force = true;
} else {
mesh::cli::formatError(reply, 160, "use set sdcard format|erase [--force]");
return true;
}
const bool recorded = is_format ? _sdcard_format_recorded : _sdcard_erase_recorded;
const uint32_t ran_at = is_format ? _sdcard_format_at : _sdcard_erase_at;
const char* action = is_format ? "format" : "erase";
const uint32_t now = millis();
if (!force && recorded && now - ran_at < SDCARD_ACTION_COOLDOWN_MILLIS) {
char age[32];
formatSdCardElapsed(age, sizeof(age), true, ran_at, now);
snprintf(reply, 160,
"Error: SD card %s ran %s; use set sdcard %s --force",
action, age, action);
return true;
}
// Heap-backed source-only builds may not have an OTA workspace yet. Never
// dereference it (or start card maintenance) after an allocation failure.
if (!mesh::ota::ota_acquire_context(reply, 160)) return true;
mesh::ota::OtaContext& context = mesh::ota::ota_ctx();
if (context.apply_pending) {
mesh::cli::formatError(reply, 160, "OTA apply is pending");
return true;
}
#if defined(OTA_TOWER_AUTO_STORE)
// Card maintenance must never abandon a fallback update stored internally.
if (context.fetch_store.usesInternal() &&
(context.manager.fetchState() != mesh::ota::OtaManager::IDLE ||
context.fetch_store.staged_size() != 0)) {
mesh::cli::formatError(reply, 160, "internal OTA slot is occupied; use ota cancel first");
return true;
}
#elif defined(OTA_RAK15002_SD_STORE) && !defined(OTA_SEEDER_ONLY)
// The optional card is independent of an internal/NOR application fetch
// or an internal bootloader package. Formatting it must not reset that
// unrelated receive session or strand its still-pinned staging bytes.
if (!context.fetch_store.usesSd() &&
(context.manager.fetchState() != mesh::ota::OtaManager::IDLE ||
context.fetch_store.staged_size() != 0)) {
mesh::cli::formatError(reply, 160, "non-SD OTA slot is occupied; use ota cancel first");
return true;
}
#endif
mesh::ota::OtaStoreSdNrf52& card = context.sdStagingStore();
// A card-wide destructive operation invalidates the staged SD fetch. First
// let an archive capture checkpoint and close its shared card handles.
context.prepareSdCardReset();
context.manager.reset_session();
context.manager.want(0);
context.manager.want_mid(nullptr);
context.session_started_ms = 0;
context.prev_fstate = mesh::ota::OtaManager::IDLE;
if (is_format) {
if (!card.formatCard(*_board)) {
char error[80];
strncpy(error, card.last_error(), sizeof(error) - 1);
error[sizeof(error) - 1] = 0;
context.finishSdCardReset(millis());
snprintf(reply, 160, "Error: SD card format failed: %s",
error);
return true;
}
context.finishSdCardReset(millis());
_sdcard_format_recorded = true;
_sdcard_format_at = millis();
strcpy(reply, "OK - SD card format complete");
return true;
}
if (!card.eraseCard(*_board)) {
char error[80];
strncpy(error, card.last_error(), sizeof(error) - 1);
error[sizeof(error) - 1] = 0;
context.finishSdCardReset(millis());
snprintf(reply, 160, "Error: SD card erase failed: %s",
error);
return true;
}
_sdcard_erase_recorded = true;
_sdcard_erase_at = millis();
if (!card.formatCard(*_board)) {
char error[80];
strncpy(error, card.last_error(), sizeof(error) - 1);
error[sizeof(error) - 1] = 0;
context.finishSdCardReset(millis());
snprintf(reply, 160, "Error: SD card erased but format failed: %s",
error);
return true;
}
_sdcard_format_recorded = true;
_sdcard_format_at = millis();
context.finishSdCardReset(millis());
strcpy(reply, "OK - SD card erase and format complete");
return true;
return mesh::cli::handleSdCardSet(_sdcard_cli, *_board, config, reply, 160);
}
bool CommonCLI::handleSdCardGetCmd(const char* config, char* reply) {
if (strncmp(config, "sdcard", 6) != 0 ||
(config[6] != 0 && config[6] != ' ')) {
return false;
}
const char* query = skipSpacesConst(config + 6);
const uint32_t now = millis();
if (*query == 0 || strcmp(query, "*") == 0) {
char format_age[32];
char erase_age[32];
formatSdCardElapsed(format_age, sizeof(format_age),
_sdcard_format_recorded, _sdcard_format_at, now);
formatSdCardElapsed(erase_age, sizeof(erase_age),
_sdcard_erase_recorded, _sdcard_erase_at, now);
snprintf(reply, 160, "> format: %s, erase: %s", format_age, erase_age);
} else if (strcmp(query, "format") == 0) {
char age[32];
formatSdCardElapsed(age, sizeof(age), _sdcard_format_recorded,
_sdcard_format_at, now);
snprintf(reply, 160, "> format: %s", age);
} else if (strcmp(query, "erase") == 0) {
char age[32];
formatSdCardElapsed(age, sizeof(age), _sdcard_erase_recorded,
_sdcard_erase_at, now);
snprintf(reply, 160, "> erase: %s", age);
} else if (strcmp(query, "free") == 0) {
if (!mesh::ota::ota_acquire_context(reply, 160)) return true;
uint64_t used_bytes = 0;
uint64_t free_bytes = 0;
mesh::ota::OtaStoreSdNrf52& store = mesh::ota::ota_ctx().sdStagingStore();
if (!store.getSpace(*_board, used_bytes, free_bytes)) {
snprintf(reply, 160, "Error: SD card space query failed: %s",
store.last_error());
} else {
char used[24];
char free[24];
mesh::cli::formatStorageBytes(used, sizeof(used), used_bytes);
mesh::cli::formatStorageBytes(free, sizeof(free), free_bytes);
snprintf(reply, 160, "> used: %s, free: %s", used, free);
}
} else if (strncmp(query, "ls", 2) == 0 || strncmp(query, "dir", 3) == 0) {
const bool is_ls = strncmp(query, "ls", 2) == 0;
const size_t word_len = is_ls ? 2 : 3;
if (query[word_len] != 0 && query[word_len] != ' ') {
mesh::cli::formatError(reply, 160, "use get sdcard ls|dir [page]");
return true;
}
const char* page_arg = skipSpacesConst(query + word_len);
uint32_t page = 1;
if (*page_arg) {
char* end = nullptr;
unsigned long parsed = strtoul(page_arg, &end, 10);
if (parsed == 0 || parsed > 65535 || !end || *skipSpacesConst(end) != 0) {
mesh::cli::formatError(reply, 160, "use get sdcard ls|dir [page]");
return true;
}
page = parsed;
}
if (!mesh::ota::ota_acquire_context(reply, 160)) return true;
mesh::ota::OtaStoreSdNrf52& store = mesh::ota::ota_ctx().sdStagingStore();
if (!store.listFiles(*_board, (uint16_t)page, reply, 160)) {
snprintf(reply, 160, "Error: SD card list failed: %s", store.last_error());
}
} else {
mesh::cli::formatError(reply, 160, "use get sdcard [format|erase|free|ls|dir [page]|*]");
}
return true;
return mesh::cli::handleSdCardGet(_sdcard_cli, *_board, config, reply, 160);
}
#endif
+4 -4
View File
@@ -18,6 +18,9 @@
#include <helpers/WirelessControl.h>
#include <helpers/FleetCommand.h>
#include <helpers/BatteryForwarding.h>
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
#include <helpers/SdCardCLI.h>
#endif
#if defined(WITH_UDP_BRIDGE)
#include <helpers/UDPBridgePrefs.h>
#endif
@@ -905,10 +908,7 @@ class CommonCLI {
ClientACL* _acl;
char tmp[COMMON_CLI_TMP_LEN];
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
bool _sdcard_format_recorded = false;
bool _sdcard_erase_recorded = false;
uint32_t _sdcard_format_at = 0;
uint32_t _sdcard_erase_at = 0;
mesh::cli::SdCardCLIState _sdcard_cli;
#endif
#ifdef WITH_MQTT_BRIDGE
MQTTPrefs _mqtt_prefs;
+69
View File
@@ -0,0 +1,69 @@
#pragma once
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <helpers/CLICommandUtils.h>
namespace mesh { namespace companion {
// Read-only board diagnostics shared by every Companion transport. A capability
// reply describes the board/build, independently of whether boot setup ran.
template<class Board>
bool handlePowerDiagnosticsCommand(Board& board, const char* command,
char* reply, size_t capacity) {
if (!command || !reply || !capacity) return false;
if (strcmp(command, "power") == 0) {
const char* usb = board.hasUsbPowerDetect()
? (board.isUsbPowered() ? "yes" : "no") : "n/a";
const char* solar = board.hasSolarChargerDetect()
? (board.isSolarChargerActive() ? "yes" : "no") : "n/a";
snprintf(reply, capacity,
"batt:%u mV usb:%s solar_chg:%s ext:%s charger:%s",
(unsigned)board.getBattMilliVolts(), usb, solar,
board.isExternalPowered() ? "yes" : "no",
board.isChargerActive() ? "yes" : "no");
return true;
}
if (strcmp(command, "get pwrmgt.support") == 0) {
snprintf(reply, capacity, "> %s",
board.supportsPowerManagement() ? "supported" : "unsupported");
return true;
}
const bool source = strcmp(command, "get pwrmgt.source") == 0;
const bool boot_voltage = strcmp(command, "get pwrmgt.bootmv") == 0;
if (source || boot_voltage) {
if (!board.supportsPowerManagement()) {
snprintf(reply, capacity, "ERROR: Power management not supported");
} else if (source) {
snprintf(reply, capacity, "> %s",
board.isExternalPowered() ? "external" : "battery");
} else {
snprintf(reply, capacity, "> %u mV", (unsigned)board.getBootVoltage());
}
return true;
}
if (strcmp(command, "get system.watchdog") == 0) {
bool running = false;
if (!board.getSystemWatchdogRunning(running)) {
cli::formatError(reply, capacity, "unsupported on this platform");
} else {
snprintf(reply, capacity, "> %s", running ? "on" : "off");
}
return true;
}
static const char setter[] = "set system.watchdog";
if (strncmp(command, setter, sizeof(setter) - 1) == 0
&& (command[sizeof(setter) - 1] == 0
|| command[sizeof(setter) - 1] == ' '
|| command[sizeof(setter) - 1] == '\t')) {
// Companion has no persisted watchdog preference/feed-loop policy. In
// particular, claiming "off" without that policy would cause reset loops.
cli::formatError(reply, capacity,
"system watchdog control is unavailable in Companion");
return true;
}
return false;
}
} } // namespace mesh::companion
+73
View File
@@ -0,0 +1,73 @@
#pragma once
#include "CLICommandUtils.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>
namespace mesh { namespace companion {
// The radio owns RXEN admission and receiver restoration. This command adapter
// does not stop RX, change preferences, or bypass an unread packet/other owner.
template<typename Driver, typename ClearStats>
bool handleRadioDiagnosticsCommand(Driver& driver, bool radio_available,
const char* command, char* reply,
size_t capacity, ClearStats clear_stats) {
if (!command || !reply || capacity == 0) return false;
if (strcmp(command, "get rxps.wd") == 0) {
snprintf(reply, capacity, "> soft=%lu,hard=%lu",
(unsigned long)driver.getRxPsWatchdogSoftCount(),
(unsigned long)driver.getRxPsWatchdogHardCount());
return true;
}
if (strcmp(command, "clear stats") == 0) {
// Match infrastructure: radio packet counts, Dispatcher packet counts and
// sticky error flags, and table duplicate counters. Watchdog history,
// receive timestamps, queues, and duplicate-detection entries are retained.
clear_stats();
snprintf(reply, capacity, "(OK - stats reset)");
return true;
}
if (strcmp(command, "get radio.rxps.rfrx_disabled") == 0) {
if (!driver.supportsRxPowerSavingRfRxDisable()) {
cli::formatError(reply, capacity, "unsupported");
} else if (!radio_available) {
cli::formatError(reply, capacity, "radio unavailable");
} else {
snprintf(reply, capacity, "> %s",
driver.isRxPowerSavingRfRxDisabled() ? "on" : "off");
}
return true;
}
static const char set_key[] = "set radio.rxps.rfrx_disabled";
if (strncmp(command, set_key, sizeof(set_key) - 1) != 0
|| (command[sizeof(set_key) - 1] != 0
&& command[sizeof(set_key) - 1] != ' '
&& command[sizeof(set_key) - 1] != '\t')) return false;
const char* value = command + sizeof(set_key) - 1;
if (*value == ' ') ++value;
if (strcmp(value, "on") != 0 && strcmp(value, "off") != 0) {
cli::formatError(reply, capacity, "use set radio.rxps.rfrx_disabled on|off");
} else if (!driver.supportsRxPowerSavingRfRxDisable()) {
cli::formatError(reply, capacity, "unsupported");
} else if (!radio_available) {
cli::formatError(reply, capacity, "radio unavailable");
} else if (!driver.setRxPowerSavingRfRxDisabled(strcmp(value, "on") == 0)) {
cli::formatError(reply, capacity, "radio busy or RXEN change failed");
} else {
snprintf(reply, capacity, "OK - radio.rxps.rfrx_disabled %s", value);
}
return true;
}
inline void formatCadDiagnostics(char* reply, size_t capacity, bool enabled,
const char* scan, const char* retry,
const char* maximum, uint32_t busy_count) {
if (!reply || capacity == 0) return;
snprintf(reply, capacity,
"radio.cad %s, scan=%s ms, retry=%s ms, max=%s ms, busy=%lu",
enabled ? "on" : "off", scan, retry, maximum,
(unsigned long)busy_count);
}
} } // namespace mesh::companion
+7
View File
@@ -370,6 +370,13 @@ void NRF52Board::serviceWatchdog() {
#endif
}
bool NRF52Board::getSystemWatchdogRunning(bool& running) const {
// Read even if this build's feed policy is disabled: a bootloader can leave
// a watchdog running. The query must describe the hardware, not a preference.
running = NRF_WDT->RUNSTATUS != 0;
return true;
}
#ifdef NRF52_POWER_MANAGEMENT
// Power Management global variables
uint32_t g_nrf52_reset_reason = 0; // Reset/Startup reason
+2
View File
@@ -68,6 +68,7 @@ public:
// and be reset automatically.
void feedWatchdog(bool enabled = true);
void serviceWatchdog() override;
bool getSystemWatchdogRunning(bool& running) const override;
virtual uint8_t getStartupReason() const override { return startup_reason; }
virtual float getMCUTemperature() override;
void reboot() override;
@@ -110,6 +111,7 @@ public:
void attachDynamicPrefs(KeyValueStore* prefs) { (void)prefs; } // no-op
#ifdef NRF52_POWER_MANAGEMENT
bool supportsPowerManagement() const override { return true; }
uint16_t getBootVoltage() override { return boot_voltage_mv; }
virtual uint32_t getResetReason() const override { return reset_reason; }
uint8_t getShutdownReason() const override { return shutdown_reason; }
+271
View File
@@ -0,0 +1,271 @@
#include "SdCardCLI.h"
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
#include <Arduino.h>
#include <MeshCore.h>
#include <stdlib.h>
#include <string.h>
#include <helpers/CLICommandUtils.h>
#include <helpers/StorageLayout.h>
#include <helpers/ota/OtaContext.h>
namespace mesh {
namespace cli {
static const uint32_t SDCARD_ACTION_COOLDOWN_MILLIS = 5UL * 60UL * 1000UL;
static const char* skipSpacesConst(const char* value) {
while (*value == ' ' || *value == '\t') ++value;
return value;
}
static bool sdCardBusAvailable(char* reply, size_t capacity) {
#if defined(OTA_RAK15002_SD_STORE)
if (mesh::ota::rak4631_ethernet_owns_spi()) {
mesh::cli::formatError(reply, capacity, "turn Ethernet off before accessing SD");
return false;
}
#else
(void)reply;
(void)capacity;
#endif
return true;
}
static void formatSdCardElapsed(char* out, size_t out_len, bool recorded,
uint32_t ran_at, uint32_t now) {
if (!recorded) {
snprintf(out, out_len, "never");
return;
}
const uint32_t elapsed_secs = (now - ran_at) / 1000UL;
if (elapsed_secs < 60UL) {
snprintf(out, out_len, "%lus ago", (unsigned long)elapsed_secs);
} else if (elapsed_secs < 60UL * 60UL) {
snprintf(out, out_len, "%lum %lus ago",
(unsigned long)(elapsed_secs / 60UL),
(unsigned long)(elapsed_secs % 60UL));
} else if (elapsed_secs < 24UL * 60UL * 60UL) {
snprintf(out, out_len, "%luh %lum ago",
(unsigned long)(elapsed_secs / 3600UL),
(unsigned long)((elapsed_secs / 60UL) % 60UL));
} else {
snprintf(out, out_len, "%lud %luh ago",
(unsigned long)(elapsed_secs / 86400UL),
(unsigned long)((elapsed_secs / 3600UL) % 24UL));
}
}
bool handleSdCardSet(SdCardCLIState& state, MainBoard& board,
const char* config, char* reply, size_t capacity) {
if (!config || !reply || capacity == 0
|| strncmp(config, "sdcard", 6) != 0 ||
(config[6] != 0 && config[6] != ' ')) {
return false;
}
const char* action_args = skipSpacesConst(config + 6);
bool is_format;
bool force = false;
if (strcmp(action_args, "format") == 0) {
is_format = true;
} else if (strcmp(action_args, "format --force") == 0) {
is_format = true;
force = true;
} else if (strcmp(action_args, "erase") == 0) {
is_format = false;
} else if (strcmp(action_args, "erase --force") == 0) {
is_format = false;
force = true;
} else {
mesh::cli::formatError(reply, capacity, "use set sdcard format|erase [--force]");
return true;
}
const bool recorded = is_format ? state.format_recorded : state.erase_recorded;
const uint32_t ran_at = is_format ? state.format_at : state.erase_at;
const char* action = is_format ? "format" : "erase";
const uint32_t now = millis();
if (!force && recorded && now - ran_at < SDCARD_ACTION_COOLDOWN_MILLIS) {
char age[32];
formatSdCardElapsed(age, sizeof(age), true, ran_at, now);
snprintf(reply, capacity,
"Error: SD card %s ran %s; use set sdcard %s --force",
action, age, action);
return true;
}
if (!sdCardBusAvailable(reply, capacity)) return true;
// Heap-backed source-only builds may not have an OTA workspace yet. Never
// dereference it (or start card maintenance) after an allocation failure.
if (!mesh::ota::ota_acquire_context(reply, capacity)) return true;
mesh::ota::OtaContext& context = mesh::ota::ota_ctx();
if (context.apply_pending) {
mesh::cli::formatError(reply, capacity, "OTA apply is pending");
return true;
}
#if defined(OTA_TOWER_AUTO_STORE)
// Card maintenance must never abandon a fallback update stored internally.
if (context.fetch_store.usesInternal() &&
(context.manager.fetchState() != mesh::ota::OtaManager::IDLE ||
context.fetch_store.staged_size() != 0)) {
mesh::cli::formatError(reply, capacity, "internal OTA slot is occupied; use ota cancel first");
return true;
}
#elif defined(OTA_RAK15002_SD_STORE) && !defined(OTA_SEEDER_ONLY)
// The optional card is independent of an internal/NOR application fetch
// or an internal bootloader package. Formatting it must not reset that
// unrelated receive session or strand its still-pinned staging bytes.
if (!context.fetch_store.usesSd() &&
(context.manager.fetchState() != mesh::ota::OtaManager::IDLE ||
context.fetch_store.staged_size() != 0)) {
mesh::cli::formatError(reply, capacity, "non-SD OTA slot is occupied; use ota cancel first");
return true;
}
#endif
mesh::ota::OtaStoreSdNrf52& card = context.sdStagingStore();
// A card-wide destructive operation invalidates the staged SD fetch. First
// let an archive capture checkpoint and close its shared card handles.
context.prepareSdCardReset();
context.manager.reset_session();
context.manager.want(0);
context.manager.want_mid(nullptr);
context.session_started_ms = 0;
context.prev_fstate = mesh::ota::OtaManager::IDLE;
if (is_format) {
if (!card.formatCard(board)) {
char error[80];
strncpy(error, card.last_error(), sizeof(error) - 1);
error[sizeof(error) - 1] = 0;
context.finishSdCardReset(millis());
snprintf(reply, capacity, "Error: SD card format failed: %s",
error);
return true;
}
context.finishSdCardReset(millis());
state.format_recorded = true;
state.format_at = millis();
snprintf(reply, capacity, "OK - SD card format complete");
return true;
}
if (!card.eraseCard(board)) {
char error[80];
strncpy(error, card.last_error(), sizeof(error) - 1);
error[sizeof(error) - 1] = 0;
context.finishSdCardReset(millis());
snprintf(reply, capacity, "Error: SD card erase failed: %s",
error);
return true;
}
state.erase_recorded = true;
state.erase_at = millis();
if (!card.formatCard(board)) {
char error[80];
strncpy(error, card.last_error(), sizeof(error) - 1);
error[sizeof(error) - 1] = 0;
context.finishSdCardReset(millis());
snprintf(reply, capacity, "Error: SD card erased but format failed: %s",
error);
return true;
}
state.format_recorded = true;
state.format_at = millis();
context.finishSdCardReset(millis());
snprintf(reply, capacity, "OK - SD card erase and format complete");
return true;
}
bool handleSdCardGet(SdCardCLIState& state, MainBoard& board,
const char* config, char* reply, size_t capacity) {
if (!config || !reply || capacity == 0
|| strncmp(config, "sdcard", 6) != 0 ||
(config[6] != 0 && config[6] != ' ')) {
return false;
}
const char* query = skipSpacesConst(config + 6);
const uint32_t now = millis();
if (*query == 0 || strcmp(query, "*") == 0) {
char format_age[32];
char erase_age[32];
formatSdCardElapsed(format_age, sizeof(format_age),
state.format_recorded, state.format_at, now);
formatSdCardElapsed(erase_age, sizeof(erase_age),
state.erase_recorded, state.erase_at, now);
snprintf(reply, capacity, "> format: %s, erase: %s", format_age, erase_age);
} else if (strcmp(query, "format") == 0) {
char age[32];
formatSdCardElapsed(age, sizeof(age), state.format_recorded,
state.format_at, now);
snprintf(reply, capacity, "> format: %s", age);
} else if (strcmp(query, "erase") == 0) {
char age[32];
formatSdCardElapsed(age, sizeof(age), state.erase_recorded,
state.erase_at, now);
snprintf(reply, capacity, "> erase: %s", age);
} else if (strcmp(query, "free") == 0) {
if (!sdCardBusAvailable(reply, capacity)) return true;
if (!mesh::ota::ota_acquire_context(reply, capacity)) return true;
uint64_t used_bytes = 0;
uint64_t free_bytes = 0;
mesh::ota::OtaStoreSdNrf52& store = mesh::ota::ota_ctx().sdStagingStore();
if (!store.getSpace(board, used_bytes, free_bytes)) {
snprintf(reply, capacity, "Error: SD card space query failed: %s",
store.last_error());
} else {
char used[24];
char free[24];
mesh::cli::formatStorageBytes(used, sizeof(used), used_bytes);
mesh::cli::formatStorageBytes(free, sizeof(free), free_bytes);
snprintf(reply, capacity, "> used: %s, free: %s", used, free);
}
} else if (strncmp(query, "ls", 2) == 0 || strncmp(query, "dir", 3) == 0) {
const bool is_ls = strncmp(query, "ls", 2) == 0;
const size_t word_len = is_ls ? 2 : 3;
if (query[word_len] != 0 && query[word_len] != ' ') {
mesh::cli::formatError(reply, capacity, "use get sdcard ls|dir [page]");
return true;
}
const char* page_arg = skipSpacesConst(query + word_len);
uint32_t page = 1;
if (*page_arg) {
char* end = nullptr;
unsigned long parsed = strtoul(page_arg, &end, 10);
if (parsed == 0 || parsed > 65535 || !end || *skipSpacesConst(end) != 0) {
mesh::cli::formatError(reply, capacity, "use get sdcard ls|dir [page]");
return true;
}
page = parsed;
}
if (!sdCardBusAvailable(reply, capacity)) return true;
if (!mesh::ota::ota_acquire_context(reply, capacity)) return true;
mesh::ota::OtaStoreSdNrf52& store = mesh::ota::ota_ctx().sdStagingStore();
if (!store.listFiles(board, (uint16_t)page, reply, capacity)) {
snprintf(reply, capacity, "Error: SD card list failed: %s", store.last_error());
}
} else {
mesh::cli::formatError(reply, capacity, "use get sdcard [format|erase|free|ls|dir [page]|*]");
}
return true;
}
bool handleSdCardCommand(SdCardCLIState& state, MainBoard& board,
const char* command, char* reply, size_t capacity) {
if (!command) return false;
if (strncmp(command, "get ", 4) == 0)
return handleSdCardGet(state, board, command + 4, reply, capacity);
if (strncmp(command, "set ", 4) == 0)
return handleSdCardSet(state, board, command + 4, reply, capacity);
return false;
}
} // namespace cli
} // namespace mesh
#endif
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
namespace mesh {
class MainBoard;
namespace cli {
// Per-CLI, per-boot history. It is not stored in node preferences or on SD.
struct SdCardCLIState {
bool format_recorded = false;
bool erase_recorded = false;
uint32_t format_at = 0;
uint32_t erase_at = 0;
};
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
// The role must perform its normal local/remote authorization before dispatch.
// config begins with "sdcard"; command begins with "get " or "set ".
bool handleSdCardSet(SdCardCLIState& state, MainBoard& board,
const char* config, char* reply, size_t capacity);
bool handleSdCardGet(SdCardCLIState& state, MainBoard& board,
const char* config, char* reply, size_t capacity);
bool handleSdCardCommand(SdCardCLIState& state, MainBoard& board,
const char* command, char* reply, size_t capacity);
#endif
} // namespace cli
} // namespace mesh
+14 -5
View File
@@ -202,12 +202,21 @@ public:
if (isCarrierWaveActive()) return false;
if (!supportsRxPowerSavingRfRxDisable()) return false;
if (_rx_ps_armed) stopReceiveDutyCycle();
((CustomSX1262 *)_radio)->setRxPowerSavingRfRxDisabled(disabled);
// Do not stop duty-cycle RX or change its policy before admission. A BUSY
// sleep window, TX, or unread RX completion still owns the current mode.
const uint8_t resume_rx = beginReconfigure();
if (resume_rx > 1) return false;
// Re-arm the configured receive mode immediately without disturbing an
// unread RX interrupt or an in-flight transmission.
return setRxPowerSaving(_rx_ps_enabled, _rx_ps_rx_us, _rx_ps_sleep_us);
const bool previous = isRxPowerSavingRfRxDisabled();
((CustomSX1262 *)_radio)->setRxPowerSavingRfRxDisabled(disabled);
endReconfigure(resume_rx);
if (resume_rx && !isInRecvMode()) {
// startRecv already retained its bounded retry/backoff. That retry must
// use the old RXEN policy; never report a failed RX restoration as OK.
((CustomSX1262 *)_radio)->setRxPowerSavingRfRxDisabled(previous);
return false;
}
return true;
}
bool isRxPowerSavingRfRxDisabled() const override {