Support signed fleet radio schedules and clock updates
This commit is contained in:
1 parent
a91ec9407a
commit
5979280156
22 files changed
+1520
-53
No files matched your search
@@ -2916,7 +2916,7 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
strcpy(reply, "OK - Advert sent");
|
||||
} else if (memcmp(command, "clock sync", 10) == 0) {
|
||||
uint32_t curr = getRTCClock()->getCurrentTime();
|
||||
if (sender_timestamp > curr) {
|
||||
if (sender_timestamp != UINT32_MAX && sender_timestamp > curr) {
|
||||
getRTCClock()->setCurrentTime(sender_timestamp + 1);
|
||||
_callbacks->onManualClockSet();
|
||||
uint32_t now = getRTCClock()->getCurrentTime();
|
||||
|
||||
@@ -480,7 +480,11 @@ struct LegacyObserverTail {
|
||||
};
|
||||
#endif
|
||||
|
||||
class CommonCLICallbacks {
|
||||
class CommonCLICallbacks
|
||||
#if MESH_ENABLE_FLEET_CONTROL
|
||||
: public mesh::FleetReplyHooks
|
||||
#endif
|
||||
{
|
||||
public:
|
||||
virtual bool supportsFleetControl() const { return false; }
|
||||
// True only after an actual NTP response this boot, not a plausible RTC.
|
||||
|
||||
@@ -248,7 +248,7 @@ void CommonCLI::beginManagement(mesh::Mesh& mesh, FILESYSTEM* fs) {
|
||||
#if MESH_ENABLE_FLEET_CONTROL
|
||||
if (_callbacks->supportsFleetControl()
|
||||
&& (fs->exists("/fleet_channel") || fs->exists("/fleet_channel.bak"))) {
|
||||
_fleet_channel = new (std::nothrow) mesh::FleetChannel(fs);
|
||||
_fleet_channel = new (std::nothrow) mesh::FleetChannel(fs, _callbacks);
|
||||
}
|
||||
#endif
|
||||
if (!_data_route) {
|
||||
@@ -279,7 +279,7 @@ bool CommonCLI::handleFleetCommand(const char* command, char* reply) {
|
||||
if (!_callbacks->supportsFleetControl()) {
|
||||
strcpy(reply, "Err - fleet control is available on infrastructure roles"); return true;
|
||||
}
|
||||
if (!_fleet_channel) _fleet_channel = new (std::nothrow) mesh::FleetChannel(_management_fs);
|
||||
if (!_fleet_channel) _fleet_channel = new (std::nothrow) mesh::FleetChannel(_management_fs, _callbacks);
|
||||
if (!_fleet_channel) {
|
||||
strcpy(reply, "Err - fleet control memory unavailable"); return true;
|
||||
}
|
||||
|
||||
@@ -215,7 +215,11 @@ bool FleetChannel::acknowledge(Mesh& mesh, const char* name, uint32_t sequence,
|
||||
|
||||
void FleetChannel::finish(RadioProfileCLI& profiles, bool delivered) {
|
||||
barrier_.clear(); ack_deadline_ = 0;
|
||||
profiles.finishReplyMutation(delivered);
|
||||
if (ack_profile_mutation_
|
||||
&& profiles.replyMutationGeneration() == ack_profile_generation_)
|
||||
profiles.finishReplyMutation(delivered);
|
||||
ack_profile_mutation_ = false;
|
||||
if (hooks_) hooks_->finishFleetReplyMutation(delivered);
|
||||
}
|
||||
void FleetChannel::complete(Packet* packet, RadioProfileCLI& profiles) {
|
||||
if (barrier_.complete(packet) && !barrier_.waiting()) finish(profiles, true);
|
||||
|
||||
@@ -9,6 +9,15 @@
|
||||
|
||||
namespace mesh {
|
||||
|
||||
// Role-owned mutations must complete after the exact fleet reply, just like
|
||||
// shared secondary-profile changes. Completion hooks stage work for loop().
|
||||
struct FleetReplyHooks {
|
||||
virtual void beginFleetCommand() {}
|
||||
virtual void endFleetCommand() {}
|
||||
virtual bool hasFleetReplyMutation() const { return false; }
|
||||
virtual void finishFleetReplyMutation(bool) {}
|
||||
};
|
||||
|
||||
// Scope codes authenticate the original packet's payload. A new reply needs
|
||||
// fresh codes from the exact matching region key, including rotated keys.
|
||||
template <typename Regions, typename Region>
|
||||
@@ -31,16 +40,19 @@ bool captureFleetReplyScope(Regions& regions, const Region* region,
|
||||
// the deliberately narrower fleet capability.
|
||||
class FleetChannel {
|
||||
FILESYSTEM* fs_;
|
||||
FleetReplyHooks* hooks_;
|
||||
GroupChannel channel_{};
|
||||
Identity controller_;
|
||||
uint32_t last_sequence_ = 0;
|
||||
uint32_t ack_deadline_ = 0;
|
||||
uint32_t ack_profile_generation_ = 0;
|
||||
uint32_t verify_window_ = 0;
|
||||
uint32_t accepted_ = 0, rejected_ = 0, busy_ = 0;
|
||||
uint8_t verify_attempts_ = 0;
|
||||
bool configured_ = false, controller_set_ = false, healthy_ = false;
|
||||
bool pending_ = false;
|
||||
bool broadcast_ = false;
|
||||
bool ack_profile_mutation_ = false;
|
||||
uint8_t incoming_[MAX_PACKET_PAYLOAD]{};
|
||||
uint8_t incoming_len_ = 0, rx_profile_ = 0, path_hash_size_ = 1;
|
||||
uint32_t rx_generation_ = 0;
|
||||
@@ -58,7 +70,8 @@ class FleetChannel {
|
||||
void serviceDeadline(Mesh& mesh, RadioProfileCLI& profiles);
|
||||
|
||||
public:
|
||||
explicit FleetChannel(FILESYSTEM* fs) : fs_(fs) { healthy_ = load(); }
|
||||
explicit FleetChannel(FILESYSTEM* fs, FleetReplyHooks* hooks = nullptr)
|
||||
: fs_(fs), hooks_(hooks) { healthy_ = load(); }
|
||||
bool handleConfig(const char* command, char* reply, size_t capacity);
|
||||
void receive(Packet* packet, Mesh& mesh, const TransportKey* scope = nullptr);
|
||||
void complete(Packet* packet, RadioProfileCLI& profiles);
|
||||
@@ -72,7 +85,8 @@ class FleetChannel {
|
||||
serviceDeadline(mesh, profiles);
|
||||
if (!pending_) return;
|
||||
pending_ = false;
|
||||
if (barrier_.waiting() || profiles.hasReplyMutation()) { ++busy_; return; }
|
||||
if (barrier_.waiting() || profiles.hasReplyMutation()
|
||||
|| (hooks_ && hooks_->hasFleetReplyMutation())) { ++busy_; return; }
|
||||
FleetCommand::Decoded command;
|
||||
if (!decode(mesh, command)) { ++rejected_; return; }
|
||||
// Reserve before dispatch, including failed commands. A lost reply, reboot,
|
||||
@@ -87,10 +101,18 @@ class FleetChannel {
|
||||
Dispatcher::ReceiveProfileScope receive_scope(mesh, rx_profile_, rx_generation_);
|
||||
char reply[160] = {};
|
||||
const uint32_t generation = profiles.replyMutationGeneration();
|
||||
if (hooks_) hooks_->beginFleetCommand();
|
||||
profiles.beginReplyCommand();
|
||||
handle(command.sequence, command.command, reply);
|
||||
profiles.endReplyCommand();
|
||||
const bool changed = profiles.replyMutationGeneration() != generation;
|
||||
if (hooks_) hooks_->endFleetCommand();
|
||||
// Only this acknowledgement may release the secondary mutation it staged.
|
||||
// Direct recovery can cancel it and accept a newer independent command.
|
||||
ack_profile_generation_ = profiles.replyMutationGeneration();
|
||||
ack_profile_mutation_ = profiles.hasReplyMutation()
|
||||
&& ack_profile_generation_ != generation;
|
||||
const bool changed = ack_profile_mutation_
|
||||
|| (hooks_ && hooks_->hasFleetReplyMutation());
|
||||
if (!acknowledge(mesh, name, command.sequence, reply, changed) && changed) {
|
||||
finish(profiles, false);
|
||||
}
|
||||
|
||||
@@ -136,6 +136,69 @@ bool integerToken(const char* text, size_t length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool boundedIntegerToken(const char* text, size_t length, uint32_t maximum,
|
||||
uint32_t& value) {
|
||||
if (!length) return false;
|
||||
value = 0;
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (text[i] < '0' || text[i] > '9') return false;
|
||||
const uint32_t digit = uint32_t(text[i] - '0');
|
||||
if (digit > maximum || value > (maximum - digit) / 10U) return false;
|
||||
value = value * 10U + digit;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool scheduleTimeToken(const char* text, size_t length) {
|
||||
if (!length) return false;
|
||||
const bool relative = text[0] == '+';
|
||||
uint32_t value;
|
||||
// The scheduler resolves +N whole minutes against its current RTC and
|
||||
// performs future/overflow/endpoint-order checks using that one snapshot.
|
||||
return boundedIntegerToken(text + relative, length - relative,
|
||||
relative ? UINT32_MAX / 60U : UINT32_MAX, value)
|
||||
&& value != 0;
|
||||
}
|
||||
|
||||
bool preambleToken(const char* text, size_t length) {
|
||||
if (keyEquals(text, length, "auto")) return true;
|
||||
uint32_t value;
|
||||
return boundedIntegerToken(text, length, 65528U, value)
|
||||
&& (value == 0 || value >= 8);
|
||||
}
|
||||
|
||||
bool scheduleSelector(const char* arguments, bool secondary) {
|
||||
if (!*arguments || !strcmp(arguments, "all")) return true;
|
||||
if (*arguments < '1' || *arguments > '9') return false;
|
||||
uint32_t value;
|
||||
// Primary capacity is role/build configurable. Match the bounded row
|
||||
// authorization policy and let the role reject nonexistent slots.
|
||||
return boundedIntegerToken(arguments, strlen(arguments), secondary ? 4U : 255U, value);
|
||||
}
|
||||
|
||||
bool scheduledRadioTuple(const char* arguments, bool temporary, bool secondary) {
|
||||
unsigned fields = 0;
|
||||
const unsigned time_index = secondary ? 5 : 4;
|
||||
const unsigned expected = time_index + (temporary ? 2 : 1);
|
||||
const char* cursor = arguments;
|
||||
for (;;) {
|
||||
const char* end = strchr(cursor, ',');
|
||||
const size_t length = end ? size_t(end - cursor) : strlen(cursor);
|
||||
bool valid = false;
|
||||
if (fields < 2) valid = decimalToken(cursor, length);
|
||||
else if (fields < 4) {
|
||||
uint32_t value;
|
||||
valid = boundedIntegerToken(cursor, length, 255U, value);
|
||||
} else if (secondary && fields == 4) {
|
||||
valid = keyEquals(cursor, length, "rx") || keyEquals(cursor, length, "rxtx");
|
||||
} else if (fields < expected) valid = scheduleTimeToken(cursor, length);
|
||||
else if (fields == expected) valid = preambleToken(cursor, length);
|
||||
if (!valid || ++fields > expected + 1) return false;
|
||||
if (!end) return fields == expected || fields == expected + 1;
|
||||
cursor = end + 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool radioTuple(const char* arguments, bool temporary) {
|
||||
if (!strcmp(arguments, "off")) return true;
|
||||
unsigned fields = 0;
|
||||
@@ -231,6 +294,12 @@ bool FleetCommand::commandAllowed(const char* command) {
|
||||
|| byte == '\\') return false;
|
||||
}
|
||||
if (!length || command[0] == ' ' || command[length - 1] == ' ') return false;
|
||||
if (!strcmp(command, "clock") || !strcmp(command, "clock sync")) return true;
|
||||
if (!strncmp(command, "time ", 5)) {
|
||||
uint32_t value;
|
||||
return boundedIntegerToken(command + 5, length - 5, UINT32_MAX, value)
|
||||
&& value >= MinEpoch;
|
||||
}
|
||||
bool get = false, set = false, del = false;
|
||||
if (!strncmp(command, "get ", 4)) get = true;
|
||||
else if (!strncmp(command, "set ", 4)) set = true;
|
||||
@@ -242,6 +311,14 @@ bool FleetCommand::commandAllowed(const char* command) {
|
||||
const char* arguments = separator ? separator + 1 : "";
|
||||
if (!key_length || (separator && (!*arguments || *arguments == ' '))) return false;
|
||||
|
||||
const bool scheduled_primary = keyEquals(key, key_length, "radioat")
|
||||
|| keyEquals(key, key_length, "tempradioat");
|
||||
const bool scheduled_secondary = keyEquals(key, key_length, "radioat2")
|
||||
|| keyEquals(key, key_length, "tempradioat2");
|
||||
if (scheduled_primary || scheduled_secondary) {
|
||||
return set ? scheduledRadioTuple(arguments, key[0] == 't', scheduled_secondary)
|
||||
: (get || del) && scheduleSelector(arguments, scheduled_secondary);
|
||||
}
|
||||
if (keyEquals(key, key_length, "radio2")
|
||||
|| keyEquals(key, key_length, "tempradio2")) {
|
||||
return get ? !*arguments : set && radioTuple(arguments, key_length == 10);
|
||||
|
||||
@@ -3,15 +3,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "../Identity.h"
|
||||
|
||||
#ifndef MESH_ENABLE_FLEET_CONTROL
|
||||
// Fixed 240 KiB STM32 images have no room for fleet control.
|
||||
#if defined(STM32_PLATFORM)
|
||||
#define MESH_ENABLE_FLEET_CONTROL 0
|
||||
#else
|
||||
#define MESH_ENABLE_FLEET_CONTROL 1
|
||||
#endif
|
||||
#endif
|
||||
#include "FleetControlConfig.h"
|
||||
|
||||
namespace mesh {
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef MESH_ENABLE_FLEET_CONTROL
|
||||
// Fixed 240 KiB STM32 images have no room for fleet control.
|
||||
#if defined(STM32_PLATFORM)
|
||||
#define MESH_ENABLE_FLEET_CONTROL 0
|
||||
#else
|
||||
#define MESH_ENABLE_FLEET_CONTROL 1
|
||||
#endif
|
||||
#endif
|
||||
@@ -287,6 +287,20 @@ bool RadioProfileCLI::finishReplyMutation(bool delivered) {
|
||||
|| !fs_ || !fs_->exists(ReplyPath) || fs_->remove(ReplyPath);
|
||||
}
|
||||
|
||||
#if MESH_ENABLE_FLEET_CONTROL
|
||||
bool RadioProfileCLI::stageRemoteSchedule(unsigned slot, const Schedule& schedule) {
|
||||
if (slot >= 4 || !stageRemoteMutation(schedule.temporary
|
||||
? RemoteMutation::AddTemporarySchedule : RemoteMutation::AddSavedSchedule,
|
||||
schedule.config, schedule.end, uint8_t(1U << slot))) return false;
|
||||
// Reuse the pending mutation's two time fields for the accepted UTC start
|
||||
// and end. Keep the separate monotonic end anchored at command admission;
|
||||
// neither a late reply nor a wall-clock correction may extend the lease.
|
||||
remote_start_ms_ = schedule.start;
|
||||
remote_schedule_end_ms_ = schedule.end_ms;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool RadioProfileCLI::applyReplyMutation() {
|
||||
const auto kind = remote_mutation_;
|
||||
if ((kind == RemoteMutation::Saved || kind == RemoteMutation::Off)
|
||||
@@ -302,11 +316,37 @@ bool RadioProfileCLI::applyReplyMutation() {
|
||||
} else if (kind == RemoteMutation::Saved) {
|
||||
saved_ = remote_config_;
|
||||
publish_pending_ = true; publish_after_ms_ = millis();
|
||||
#if MESH_ENABLE_FLEET_CONTROL
|
||||
} else if (kind == RemoteMutation::AddSavedSchedule
|
||||
|| kind == RemoteMutation::AddTemporarySchedule) {
|
||||
const bool temporary = kind == RemoteMutation::AddTemporarySchedule;
|
||||
const uint32_t now = millis(), epoch = rtc_ ? rtc_->getCurrentTime() : 0;
|
||||
if (temporary && (!remainingMillis(remote_schedule_end_ms_, now)
|
||||
|| epoch >= remote_duration_ms_)) return true;
|
||||
for (unsigned i = 0; i < 4; ++i) if (remote_delete_mask_ & (1U << i)) {
|
||||
Schedule& scheduled = schedules_[(temporary ? 4 : 0) + i];
|
||||
scheduled = {};
|
||||
scheduled.config = remote_config_;
|
||||
scheduled.start = remote_start_ms_;
|
||||
scheduled.end = remote_duration_ms_;
|
||||
scheduled.end_ms = remote_schedule_end_ms_;
|
||||
scheduled.temporary = temporary;
|
||||
scheduled.active = true;
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
} else if (kind == RemoteMutation::DeleteTemp) {
|
||||
for (unsigned i = 0; i < 4; ++i) if (remote_delete_mask_ & (1U << i)) {
|
||||
if (schedules_[4+i].started) { temp_active_ = false; temp_remaining_ms_ = 0; }
|
||||
schedules_[4+i] = {};
|
||||
}
|
||||
#if MESH_ENABLE_FLEET_CONTROL
|
||||
} else if (kind == RemoteMutation::DeleteSavedSchedule) {
|
||||
for (unsigned i = 0; i < 4; ++i) if (remote_delete_mask_ & (1U << i)) {
|
||||
schedules_[i] = {};
|
||||
}
|
||||
schedule_retry_ms_ = 0;
|
||||
#endif
|
||||
} else {
|
||||
if (kind == RemoteMutation::Off) { saved_ = {}; schedule_retry_ms_ = 0; }
|
||||
temp_pending_ = temp_active_ = false; temp_remaining_ms_ = 0;
|
||||
@@ -633,8 +673,14 @@ bool RadioProfileCLI::handle(const char* command, char* reply, size_t capacity,
|
||||
}
|
||||
const unsigned first = scheduled_temp ? 4 : 0;
|
||||
if (verb == Delete) {
|
||||
if (remote_command_ && scheduled_temp) {
|
||||
if (remote_command_ && (scheduled_temp || MESH_ENABLE_FLEET_CONTROL)) {
|
||||
#if MESH_ENABLE_FLEET_CONTROL
|
||||
stageRemoteMutation(scheduled_temp ? RemoteMutation::DeleteTemp
|
||||
: RemoteMutation::DeleteSavedSchedule,
|
||||
{}, 0, index ? 1U << (index - 1) : 15);
|
||||
#else
|
||||
stageRemoteMutation(RemoteMutation::DeleteTemp, {}, 0, index ? 1U << (index - 1) : 15);
|
||||
#endif
|
||||
snprintf(reply, capacity, "OK - schedule cleared after reply"); return true;
|
||||
}
|
||||
if (!scheduled_temp) schedule_retry_ms_ = 0;
|
||||
@@ -709,9 +755,20 @@ bool RadioProfileCLI::handle(const char* command, char* reply, size_t capacity,
|
||||
}
|
||||
const unsigned first = scheduled_temp ? 4 : 0;
|
||||
for (unsigned i = 0; i < 4; ++i) if (!schedules_[first+i].active) {
|
||||
#if MESH_ENABLE_FLEET_CONTROL
|
||||
Schedule s;
|
||||
#else
|
||||
auto& s = schedules_[first+i]; s = {};
|
||||
#endif
|
||||
s.config = config; s.start = start; s.end = end; s.temporary = scheduled_temp;
|
||||
s.end_ms = scheduled_temp ? millis() + (end-epoch)*1000UL : 0; s.active = true;
|
||||
#if MESH_ENABLE_FLEET_CONTROL
|
||||
if (remote_command_) {
|
||||
if (!stageRemoteSchedule(i, s)) {
|
||||
snprintf(reply, capacity, "Error: radio acknowledgement pending"); return true;
|
||||
}
|
||||
} else schedules_[first+i] = s;
|
||||
#endif
|
||||
snprintf(reply, capacity, "OK - %s %u queued, preamble=%u", key, i+1, preview.preamble(1, 32));
|
||||
appendChirpWarning(reply, capacity, preview); return true;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <Dispatcher.h>
|
||||
#include <helpers/IdentityStore.h>
|
||||
#include <helpers/CLICommandUtils.h>
|
||||
#include <helpers/FleetControlConfig.h>
|
||||
|
||||
namespace mesh {
|
||||
|
||||
@@ -41,16 +42,27 @@ class RadioProfileCLI {
|
||||
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 };
|
||||
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);
|
||||
bool prepareSavedImage(const char* path, const RadioProfileConfig& config,
|
||||
uint16_t preamble, RadioCrossMode cross);
|
||||
|
||||
Reference in new issue
Block a user