Files
MeshCore/src/helpers/RadioProfileCLI.h
T

154 lines
7.4 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;
RadioCrossMode cross_ = RadioCrossMode::Auto;
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;
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);
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;
public:
void begin(FILESYSTEM* fs, Radio* radio, RTCClock* rtc, bool infrastructure_replies = false);
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);
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