Files
MeshCore/src/helpers/RadioProfileCLI.h
T
mikecarper 73c5f572eb Apply busy radio settings and recover service starts automatically
Save valid busy RX, fast-switch and Companion tuple changes once, then
apply the latest accepted intent at a safe radio gap. Preserve checked-save
rollback, runtime RXEN intent, TempRadio ownership and exact remote reply
delivery. Repair failed preamble compensation without hiding uncertain saves.

Retry WebConfig, MQTT and NimBLE startup failures automatically. Preserve
explicit off choices, OTA ownership and the latest WiFi credentials through
cooperative shutdown. Add regression coverage and update command guides.

Validation: focused host transaction, rollback, ownership and lifecycle suites;
Full RAK4631, Heltec V4 R8 TFT and RC32 packages; representative room, sensor,
LR1110, LR2021, SX1276, STM32WL and ESP-NOW builds. Linked RAK crypto and OTA
workspace checks pass; its complete OTA image uses 712644/712704 bytes.
2026-10-25 15:20:55 -07:00

190 lines
9.0 KiB
C++

#pragma once
#include <Dispatcher.h>
#include <helpers/IdentityStore.h>
#include <helpers/CLICommandUtils.h>
#include <helpers/FleetControlConfig.h>
#include <helpers/MeshtextConfig.h>
namespace mesh {
// Shared by infrastructure, Companion and standalone roles. Settings use a
// separate versioned image so existing preference offsets stay compatible.
class RadioProfileCLI {
public:
struct Schedule {
RadioProfileConfig config;
uint32_t start = 0, end = 0;
uint32_t end_ms = 0; // monotonic deadline anchored when the command is accepted
bool active = false, temporary = false, started = false;
};
private:
FILESYSTEM* fs_ = nullptr;
Radio* radio_ = nullptr;
RTCClock* rtc_ = nullptr;
RadioProfileConfig saved_;
RadioProfileConfig temporary_;
RadioProfileConfig pending_temporary_;
uint16_t primary_preamble_ = 0;
bool primary_preamble_dirty_ = false;
uint32_t primary_preamble_retry_ms_ = 0;
RadioCrossMode cross_ = RadioCrossMode::Off;
uint8_t reply_setting_ = 0; // reserved image byte: 0 = role default
bool infrastructure_replies_ = false;
// Saved Off can be an explicit veto, rather than an unused secondary slot.
bool manual_secondary_ = false;
uint32_t secondary_intent_generation_ = 0;
#if MESH_ENABLE_MESHTEXT_CHAT || MESH_ENABLE_ETHERNET_TUNNEL
RadioProfileConfig auto_secondary_;
bool auto_secondary_requested_ = false;
bool secondary_protocol_overlay_ = false;
RadioProfileProtocol secondary_protocol_ = RadioProfileProtocol::MeshCore;
RadioProfileConfig owned_secondary_snapshot_;
uint32_t owned_secondary_generation_ = 0;
bool owns_secondary_runtime_ = false, external_secondary_override_ = false;
bool runtimeSecondaryUnchanged() const;
void protectExternalSecondary();
#endif
Schedule schedules_[8];
uint32_t temp_remaining_ms_ = 0;
uint32_t temp_start_ms_ = 0;
uint32_t pending_duration_ms_ = 0;
uint32_t publish_after_ms_ = 0;
uint32_t last_ms_ = 0;
uint32_t schedule_retry_ms_ = 0;
bool temp_pending_ = false, temp_active_ = false;
bool hold_ = false;
// Set only after the active image is conclusively malformed and there is no
// readable backup. Read/I/O failures and an intact backup remain read-only.
bool recoverable_corrupt_store_ = false;
bool publish_pending_ = false;
#if MESH_RADIO_SWITCH_TUNING
RadioSwitchTuning fast_saved_;
uint32_t fast_retry_ms_ = 0;
bool fast_pending_ = false, fast_hold_ = false;
#endif
enum class ImageReadResult : uint8_t { Missing, Valid, Invalid, Unreadable };
enum class RemoteMutation : uint8_t {
None, Saved, Temporary, Off, TempOff, DeleteTemp
#if MESH_ENABLE_FLEET_CONTROL
, DeleteSavedSchedule, AddSavedSchedule, AddTemporarySchedule
#endif
};
RemoteMutation remote_mutation_ = RemoteMutation::None;
RadioProfileConfig remote_config_;
uint32_t remote_duration_ms_ = 0, remote_start_ms_ = 0;
uint32_t remote_commit_retry_ms_ = 0;
#if MESH_ENABLE_FLEET_CONTROL
uint32_t remote_schedule_end_ms_ = 0;
#endif
uint32_t remote_generation_ = 0;
uint8_t remote_delete_mask_ = 0;
bool remote_command_ = false, remote_delivered_ = false;
bool stageRemoteMutation(RemoteMutation kind, const RadioProfileConfig& config = {},
uint32_t duration_ms = 0, uint8_t delete_mask = 0);
#if MESH_ENABLE_FLEET_CONTROL
bool stageRemoteSchedule(unsigned slot, const Schedule& schedule);
#endif
bool save(const RadioProfileConfig& config, uint16_t preamble, RadioCrossMode cross,
int8_t manual_secondary = -1);
bool prepareSavedImage(const char* path, const RadioProfileConfig& config,
uint16_t preamble, RadioCrossMode cross, int8_t manual_secondary = -1);
bool commitSavedImage(const char* path);
#if MESH_RADIO_SWITCH_TUNING
bool commitImage(const char* staged, const char* active, const char* backup,
bool& hold, bool preserve_backup = false);
bool validFastImage(const uint8_t* bytes, RadioSwitchTuning& value) const;
void loadFastSettings(bool boot = false);
bool saveFastSettings(const RadioSwitchTuning& value);
static bool commitFastSettings(void* context, const RadioSwitchTuning& value);
bool handleFastSettings(const char* key, const char* args, int verb,
bool remote, char* reply, size_t capacity);
#endif
bool discardCorruptSavedImagesForWrite();
bool applyReplyMutation();
ImageReadResult readImage(const char* path, uint8_t* bytes, size_t size);
bool writeImage(const char* path, const uint8_t* bytes, size_t size);
void publish();
bool hasSecondarySchedule() const;
bool secondaryRuntimeBusy() const;
void noteSecondaryManualChange();
void formatConfig(char* reply, size_t capacity, const RadioProfileConfig& config,
bool temporary, uint32_t remaining_ms, bool automatic = false,
const Schedule* schedule = nullptr) const;
void appendSecondaryWarnings(char* reply, size_t capacity, const RadioProfiles& preview) const;
public:
// Call after the settings filesystem mounts and before radio_init().
#if MESH_RADIO_SWITCH_TUNING
static void configureBootTuning(FILESYSTEM* fs, Radio& radio);
#else
static void configureBootTuning(FILESYSTEM*, Radio&) {}
#endif
void begin(FILESYSTEM* fs, Radio* radio, RTCClock* rtc, bool infrastructure_replies = false);
void servicePendingTuning();
bool hasPendingTuning() const {
#if MESH_RADIO_SWITCH_TUNING
return fast_pending_ || primary_preamble_dirty_;
#else
return primary_preamble_dirty_;
#endif
}
void loop();
bool handle(const char* command, char* reply, size_t capacity = 160, bool remote_origin = false);
// Remote infrastructure commands must keep both RX-origin generations alive
// until their exact response packets finish, not merely wait a fixed delay.
void beginReplyCommand() { remote_command_ = true; }
void endReplyCommand() { remote_command_ = false; }
bool hasReplyMutation() const { return remote_mutation_ != RemoteMutation::None; }
uint32_t replyMutationGeneration() const { return remote_generation_; }
bool finishReplyMutation(bool delivered);
// Channel-derived RF settings are RAM-only and never replace explicit saved
// settings (including Off). Repeated identical requests do not retune.
bool requestAutoSecondary(const RadioProfileConfig& config);
void clearAutoSecondary();
bool autoSecondaryActive() const;
bool canAutoSecondary() const;
bool secondaryRuntimeAvailable() const { return !secondaryRuntimeBusy(); }
// Borrow a manually configured RF tuple without persisting its wire protocol
// or the foreign network's fixed preamble. Temporary sessions take priority.
bool useSecondaryProtocol(RadioProfileProtocol protocol);
void clearSecondaryProtocol();
bool manualSecondaryConfigured() const { return manual_secondary_ && saved_.mode != RadioProfileMode::Off; }
const RadioProfileConfig& savedSecondaryConfig() const { return saved_; }
uint32_t secondaryIntentGeneration() const { return secondary_intent_generation_; }
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);
// A failed compensating write still restores the old runtime target. Keep
// that target dirty until storage confirms it, even for an identical save.
bool restorePrimaryPreamble(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,
bool temporary, uint16_t preamble, const uint32_t* timings = nullptr) {
if (!radio_) return RadioParamApplyResult::FAILED;
RadioProfileParams p;
p.freq = freq; p.bw = bw; p.sf = sf; p.cr = cr; p.preamble = preamble;
return radio_->trySetPrimaryParams(p, temporary, timings);
}
void stagePrimary(uint16_t preamble, bool temporary) {
if (!radio_ || !radio_->profiles()) return;
auto p = radio_->profiles()->primary;
p.preamble = preamble;
radio_->profiles()->setPrimary(p, temporary);
}
bool secondaryTemporary() const { return temp_active_; }
uint32_t secondaryRemainingSeconds() const { return (temp_remaining_ms_ + 999) / 1000; }
void appendPreamble(char* reply, size_t capacity, uint8_t profile = 0) const;
void appendSavedPreamble(char* reply, size_t capacity, uint8_t sf, float bw) const;
static void appendChirpWarning(char* reply, size_t capacity, const RadioProfiles& preview);
void appendPrimaryChirpWarning(char* reply, size_t capacity, uint8_t sf, float bw,
uint16_t preamble) const;
// Strip exactly one optional trailing preamble before a legacy primary
// parser runs. Missing means auto. Reject extra fields and malformed input.
static bool parseSuffix(const char* input, unsigned fields, char* legacy,
size_t capacity, uint16_t& preamble);
};
} // namespace mesh