Add repeater filtering, clock sync, and companion fixes

This commit is contained in:
mikecarper committed 2026-07-15 17:40:20 -07:00
1 parent 0d2eb2420c
commit 63776482e1
30 files changed
+2757 -65

No files matched your search

+124 -29
View File
@@ -14,8 +14,10 @@ static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
return delay_ms > 30000UL ? 30000UL : delay_ms;
}
static const uint32_t COMMAND_RADIO_APPLY_TIMEOUT_MS = 5000UL;
#ifndef RXPS_FIXED_ENABLED
#define RXPS_FIXED_ENABLED 1
#define RXPS_FIXED_ENABLED 0
#endif
#ifndef RXPS_FIXED_LEVEL
#define RXPS_FIXED_LEVEL 2
@@ -350,11 +352,11 @@ static bool calcFixedRxPowerSaving(uint8_t sf, float bw, uint32_t* rx_us, uint32
return true;
}
static bool applyFixedRadioParams(float freq, float bw, uint8_t sf, uint8_t cr) {
static mesh::RadioParamApplyResult applyFixedRadioParams(float freq, float bw, uint8_t sf, uint8_t cr) {
uint32_t rx_us, sleep_us;
if (!calcFixedRxPowerSaving(sf, bw, &rx_us, &sleep_us)) {
MESH_DEBUG_PRINTLN("RX Power Saving fixed profile invalid");
return false;
return mesh::RadioParamApplyResult::FAILED;
}
uint32_t timings[2] = {rx_us, sleep_us};
@@ -363,14 +365,17 @@ static bool applyFixedRadioParams(float freq, float bw, uint8_t sf, uint8_t cr)
// radio does not implement RX duty cycling. This matches the former
// setParams()+setRxPowerSaving() behavior while retaining one atomic
// transition on radios that do support it.
bool ok = radio_driver.setParams(freq, bw, sf, cr, supports_rxps ? timings : NULL);
mesh::RadioParamApplyResult result =
radio_driver.trySetParams(freq, bw, sf, cr, supports_rxps ? timings : NULL);
MESH_DEBUG_PRINTLN("RX Power Saving fixed level %d p%d: %s (%lu/%lu us)",
RXPS_FIXED_LEVEL,
RXPS_FIXED_PREAMBLE,
ok && supports_rxps ? "Enabled" : (ok ? "Unsupported" : "Apply failed"),
result == mesh::RadioParamApplyResult::APPLIED
? (supports_rxps ? "Enabled" : "Unsupported")
: (result == mesh::RadioParamApplyResult::BUSY ? "Busy" : "Apply failed"),
(unsigned long)rx_us,
(unsigned long)sleep_us);
return ok;
return result;
}
#endif
int MyMesh::calcRxDelay(float score, uint32_t air_time) const {
@@ -1115,6 +1120,13 @@ MyMesh::MyMesh(mesh::Radio &radio, mesh::RNG &rng, mesh::RTCClock &rtc, SimpleMe
saved_radio_apply_pending = false;
radio_apply_retry_at = 0;
radio_apply_failures = 0;
command_radio_apply_pending = false;
command_radio_freq = 0.0f;
command_radio_bw = 0.0f;
command_radio_sf = 0;
command_radio_cr = 0;
command_radio_repeat = 0;
command_radio_apply_deadline = 0;
offline_queue_len = 0;
app_target_ver = 0;
clearPendingReqs();
@@ -1278,14 +1290,80 @@ void MyMesh::begin(bool has_display) {
#endif
}
bool MyMesh::applySavedRadioParams() {
mesh::RadioParamApplyResult MyMesh::tryApplyRadioParams(float freq, float bw, uint8_t sf, uint8_t cr) {
#if RXPS_FIXED_ENABLED
return applyFixedRadioParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
return applyFixedRadioParams(freq, bw, sf, cr);
#else
return radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
return radio_driver.trySetParams(freq, bw, sf, cr);
#endif
}
bool MyMesh::applySavedRadioParams() {
return tryApplyRadioParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr)
== mesh::RadioParamApplyResult::APPLIED;
}
void MyMesh::finishRadioParamApply(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t repeat) {
_prefs.sf = sf;
_prefs.cr = cr;
_prefs.freq = freq;
_prefs.bw = bw;
_prefs.client_repeat = repeat;
savePrefs();
saved_radio_apply_pending = false;
radio_apply_retry_at = 0;
radio_apply_failures = 0;
MESH_DEBUG_PRINTLN("OK: CMD_SET_RADIO_PARAMS: f=%d, bw=%d, sf=%d, cr=%d",
(uint32_t)(freq * 1000.0f), (uint32_t)(bw * 1000.0f),
(uint32_t)sf, (uint32_t)cr);
writeOKFrame();
}
void MyMesh::cancelPendingRadioParamApply() {
if (!command_radio_apply_pending) return;
command_radio_apply_pending = false;
command_radio_apply_deadline = 0;
// The requested tuple was not committed. Reassert the persisted tuple in
// case a failed hardware apply only restored part of the old configuration.
saved_radio_apply_pending = true;
radio_apply_retry_at = 0;
radio_apply_failures = 0;
}
void MyMesh::servicePendingRadioParamApply() {
if (!command_radio_apply_pending) return;
if (!_serial || !_serial->isConnected()) {
cancelPendingRadioParamApply();
return;
}
// Leave enough queue headroom for the eventual command response.
if (_serial->isWriteBusy()) return;
mesh::RadioParamApplyResult result = tryApplyRadioParams(
command_radio_freq, command_radio_bw, command_radio_sf, command_radio_cr);
if (result == mesh::RadioParamApplyResult::APPLIED) {
float freq = command_radio_freq;
float bw = command_radio_bw;
uint8_t sf = command_radio_sf;
uint8_t cr = command_radio_cr;
uint8_t repeat = command_radio_repeat;
command_radio_apply_pending = false;
command_radio_apply_deadline = 0;
finishRadioParamApply(freq, bw, sf, cr, repeat);
} else if (result == mesh::RadioParamApplyResult::FAILED) {
cancelPendingRadioParamApply();
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
} else if (command_radio_apply_deadline == _ms->getMillis()
|| millisHasNowPassed(command_radio_apply_deadline)) {
cancelPendingRadioParamApply();
writeErrFrame(ERR_CODE_BAD_STATE);
}
}
const char *MyMesh::getNodeName() {
return _prefs.node_name;
}
@@ -1408,6 +1486,7 @@ void MyMesh::handleCmdFrame(size_t len) {
MESH_DEBUG_PRINTLN("App %s connected", app_name);
_iter_started = false; // stop any left-over ContactsIterator
cancelPendingRadioParamApply();
int i = 0;
out_frame[i++] = RESP_CODE_SELF_INFO;
out_frame[i++] = ADV_TYPE_CHAT; // what this node Advert identifies as (maybe node's pronouns too?? :-)
@@ -1809,29 +1888,34 @@ void MyMesh::handleCmdFrame(size_t len) {
bw <= 500000) {
float new_freq = (float)freq / 1000.0;
float new_bw = (float)bw / 1000.0;
#if RXPS_FIXED_ENABLED
bool applied = applyFixedRadioParams(new_freq, new_bw, sf, cr);
#else
bool applied = radio_driver.setParams(new_freq, new_bw, sf, cr);
#endif
if (!applied) {
// Persisted settings remain authoritative after a rejected change.
saved_radio_apply_pending = true;
radio_apply_retry_at = 0;
if (command_radio_apply_pending) {
writeErrFrame(ERR_CODE_BAD_STATE);
return;
}
_prefs.sf = sf;
_prefs.cr = cr;
_prefs.freq = new_freq;
_prefs.bw = new_bw;
_prefs.client_repeat = repeat;
savePrefs();
MESH_DEBUG_PRINTLN("OK: CMD_SET_RADIO_PARAMS: f=%d, bw=%d, sf=%d, cr=%d", freq, bw, (uint32_t)sf,
(uint32_t)cr);
mesh::RadioParamApplyResult result = tryApplyRadioParams(new_freq, new_bw, sf, cr);
if (result == mesh::RadioParamApplyResult::BUSY) {
command_radio_apply_pending = true;
command_radio_freq = new_freq;
command_radio_bw = new_bw;
command_radio_sf = sf;
command_radio_cr = cr;
command_radio_repeat = repeat;
command_radio_apply_deadline = futureMillis(COMMAND_RADIO_APPLY_TIMEOUT_MS);
MESH_DEBUG_PRINTLN("Deferred CMD_SET_RADIO_PARAMS while radio is busy");
return;
}
if (result == mesh::RadioParamApplyResult::FAILED) {
// Persisted settings remain authoritative after a rejected change or
// a hardware apply that had to be rolled back.
saved_radio_apply_pending = true;
radio_apply_retry_at = 0;
radio_apply_failures = 0;
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
writeOKFrame();
finishRadioParamApply(new_freq, new_bw, sf, cr, repeat);
} else {
MESH_DEBUG_PRINTLN("Error: CMD_SET_RADIO_PARAMS: f=%d, bw=%d, sf=%d, cr=%d", freq, bw, (uint32_t)sf,
(uint32_t)cr);
@@ -1938,6 +2022,7 @@ void MyMesh::handleCmdFrame(size_t len) {
self_id = identity;
writeOKFrame();
// re-load contacts, to invalidate ecdh shared_secrets
_iter_started = false;
resetContacts();
_store->loadContacts(this);
updateGpsTelemetryPolicy();
@@ -2668,6 +2753,12 @@ void MyMesh::checkCLIRescueCmd() {
void MyMesh::checkSerialInterface() {
size_t len = _serial->checkRecvFrame(cmd_frame);
if (!_serial->isConnected()) {
_iter_started = false;
cancelPendingRadioParamApply();
return;
}
if (len > 0) {
handleCmdFrame(len);
} else if (_iter_started // check if our ContactsIterator is 'running'
@@ -2695,7 +2786,7 @@ void MyMesh::checkSerialInterface() {
void MyMesh::loop() {
BaseChatMesh::loop();
if (saved_radio_apply_pending && !hasOutbound()
if (!command_radio_apply_pending && saved_radio_apply_pending && !hasOutbound()
&& (!radio_apply_retry_at || millisHasNowPassed(radio_apply_retry_at))) {
// A power-saving wake can enter begin() with a complete packet already
// waiting. Preserve that packet, then apply the persisted radio settings
@@ -2725,6 +2816,7 @@ void MyMesh::loop() {
} else {
checkSerialInterface();
}
servicePendingRadioParamApply();
// is there are pending dirty contacts write needed?
if (dirty_contacts_expiry && millisHasNowPassed(dirty_contacts_expiry)) {
@@ -2755,7 +2847,10 @@ bool MyMesh::advert() {
// To check if there is pending work
bool MyMesh::hasPendingWork() const {
if (radio_driver.isWatchdogObserving() || radio_driver.isCalibratingNoiseFloor()) return true;
return hasQueuedWorkDue() || hasRetryWorkDue()
return (_serial != NULL && _serial->hasPendingIO())
|| (_iter_started && _serial != NULL && _serial->isConnected())
|| command_radio_apply_pending
|| hasQueuedWorkDue() || hasRetryWorkDue()
|| (saved_radio_apply_pending
&& (!radio_apply_retry_at || millisHasNowPassed(radio_apply_retry_at)))
|| (dirty_contacts_expiry != 0 && millisHasNowPassed(dirty_contacts_expiry))
+11
View File
@@ -222,7 +222,11 @@ private:
bool isValidClientRepeatFreq(uint32_t f) const;
bool hasLocationTelemetryRecipient();
void updateGpsTelemetryPolicy();
mesh::RadioParamApplyResult tryApplyRadioParams(float freq, float bw, uint8_t sf, uint8_t cr);
bool applySavedRadioParams();
void finishRadioParamApply(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t repeat);
void cancelPendingRadioParamApply();
void servicePendingRadioParamApply();
// helpers, short-cuts
void saveChannels() { _store->saveChannels(this); }
@@ -253,6 +257,13 @@ private:
bool saved_radio_apply_pending;
unsigned long radio_apply_retry_at;
uint8_t radio_apply_failures;
bool command_radio_apply_pending;
float command_radio_freq;
float command_radio_bw;
uint8_t command_radio_sf;
uint8_t command_radio_cr;
uint8_t command_radio_repeat;
unsigned long command_radio_apply_deadline;
bool send_unscoped; // force un-scoped flood (instead of using send_scope)
char cli_command[80];
uint8_t app_target_ver;
File diff suppressed because it is too large. Load diff
+119
View File
@@ -98,6 +98,9 @@ struct NeighbourInfo {
#ifndef FIRMWARE_BUILD_DATE
#define FIRMWARE_BUILD_DATE "6 Jun 2026"
#endif
#ifndef FIRMWARE_BUILD_EPOCH
#define FIRMWARE_BUILD_EPOCH 0UL
#endif
#ifndef FIRMWARE_VERSION
#define FIRMWARE_VERSION "v1.17.0"
@@ -113,6 +116,40 @@ struct NeighbourInfo {
#define MAX_SCHEDULED_RADIO_SETTINGS (MAX_SCHEDULED_RADIO_SETTINGS_PER_TYPE * 2)
#ifndef FLOOD_PACKET_FILTER_SLOTS
#define FLOOD_PACKET_FILTER_SLOTS 16
#endif
#define FLOOD_PACKET_FILTER_ANY_TYPE 0xFF
#define FLOOD_PACKET_FILTER_MAX_HOPS 63
#ifndef FLOOD_GROUP_MODERATION_SLOTS
#define FLOOD_GROUP_MODERATION_SLOTS 16
#endif
#define FLOOD_GROUP_MODERATION_NAME_LEN 32
#define FLOOD_GROUP_MODERATION_PATH_HOPS_MAX 3
#define FLOOD_GROUP_MODERATION_PATH_BYTES_MAX (FLOOD_GROUP_MODERATION_PATH_HOPS_MAX * 3)
#define FLOOD_GROUP_MODERATION_HOPS_ALL 0xFF
#define FLOOD_GROUP_MODERATION_RATE_UNLIMITED 0xFFFF
#ifndef CLOCK_SYNC_SAMPLE_SLOTS
#define CLOCK_SYNC_SAMPLE_SLOTS 16
#endif
#define CLOCK_SYNC_REQUIRED_SAMPLES_MIN 3
#define CLOCK_SYNC_REQUIRED_SAMPLES_MAX CLOCK_SYNC_SAMPLE_SLOTS
#define CLOCK_SYNC_REQUIRED_SAMPLES_DEFAULT 9
#define CLOCK_SYNC_PATH_ID_SIZE 8
#define CLOCK_SYNC_STARTUP_DELAY_MILLIS (30ULL * 60ULL * 1000ULL)
#define CLOCK_SYNC_RETRY_INTERVAL_MILLIS (30ULL * 60ULL * 1000ULL)
#define CLOCK_SYNC_SAMPLE_MAX_AGE_MILLIS (2UL * 60UL * 60UL * 1000UL)
#define CLOCK_SYNC_CONSENSUS_WINDOW_SECONDS 600UL
#define CLOCK_SYNC_DRIFT_MIN_SECONDS 30UL
#define CLOCK_SYNC_DRIFT_MAX_SECONDS 86400UL
#define CLOCK_SYNC_DRIFT_DEFAULT_SECONDS 3600UL
#define CLOCK_SYNC_MESH_SUPPRESS_NONE 0
#define CLOCK_SYNC_MESH_SUPPRESS_CLI 1
#define CLOCK_SYNC_MESH_SUPPRESS_GPS 2
#define CLOCK_SYNC_MESH_SUPPRESS_INTERNET 3
#define RECENT_REPEATER_MAX_AGE_MILLIS (24UL * 60UL * 60UL * 1000UL)
#define RECENT_REPEATER_SWEEP_INTERVAL_MILLIS (3UL * 60UL * 60UL * 1000UL)
#define RADIO_APPLY_RETRY_INTERVAL_MILLIS 1000UL
@@ -176,9 +213,58 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
uint8_t secret[PUB_KEY_SIZE];
char name[FLOOD_CHANNEL_BLOCK_NAME_LEN];
};
struct FloodPacketFilterEntry {
bool active;
uint8_t payload_type;
uint8_t min_hops;
uint8_t max_hops;
};
struct FloodGroupModerationEntry {
bool active;
uint8_t key_len;
uint8_t hash_prefix[FLOOD_CHANNEL_BLOCK_PREFIX_LEN];
uint8_t secret[PUB_KEY_SIZE];
char channel_name[FLOOD_GROUP_MODERATION_NAME_LEN];
char sender[FLOOD_GROUP_MODERATION_NAME_LEN];
uint8_t path_hash_size;
uint8_t path_hops;
uint8_t path[FLOOD_GROUP_MODERATION_PATH_BYTES_MAX];
uint8_t max_hops;
uint16_t rate_per_minute;
uint32_t rate_window_started;
uint16_t rate_window_count;
bool rate_window_active;
};
struct ClockSyncSample {
bool active;
uint8_t source_kind;
uint8_t source_id[4];
uint8_t path_id[CLOCK_SYNC_PATH_ID_SIZE];
uint32_t epoch;
uint32_t received_millis;
};
mutable FloodRetryBridgeState flood_retry_bridge_states[MAX_FLOOD_RETRY_SLOTS];
FloodRetryBridgeReachability flood_retry_bridge_reachability[FLOOD_RETRY_BRIDGE_BUCKETS + 1];
FloodChannelBlockEntry flood_channel_blocks[FLOOD_CHANNEL_BLOCK_SLOTS];
FloodPacketFilterEntry flood_packet_filters[FLOOD_PACKET_FILTER_SLOTS];
FloodGroupModerationEntry flood_group_moderation[FLOOD_GROUP_MODERATION_SLOTS];
ClockSyncSample clock_sync_samples[CLOCK_SYNC_SAMPLE_SLOTS];
bool clock_sync_mesh_enabled;
bool clock_sync_internet_enabled;
bool clock_sync_complete;
bool clock_sync_internet_pending;
uint8_t clock_sync_mesh_suppressed_by;
uint8_t clock_sync_last_result;
uint8_t clock_sync_last_source;
uint8_t clock_sync_last_sample_count;
uint8_t clock_sync_last_fresh_count;
uint8_t clock_sync_last_required_count;
uint8_t clock_sync_required_samples;
uint32_t clock_sync_drift_seconds;
uint32_t clock_sync_last_estimate;
uint32_t clock_sync_last_abs_drift;
uint32_t clock_sync_internet_requested_millis;
uint64_t clock_sync_next_attempt_uptime;
uint32_t pending_discover_tag;
unsigned long pending_discover_until;
bool region_load_active;
@@ -294,6 +380,37 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
bool floodChannelDataHopApplies(const mesh::Packet* packet) const;
bool floodChannelBlockMatches(const FloodChannelBlockEntry& entry, const mesh::Packet* packet) const;
bool shouldBlockFloodChannelForward(const mesh::Packet* packet) const;
void loadFloodPacketFilters();
bool saveFloodPacketFilters();
bool shouldBlockFloodPacketForward(const mesh::Packet* packet) const;
void formatFloodPacketFilters(const char* args, char* reply) const;
void formatFloodPacketFilterDetail(int index, char* reply, size_t reply_len) const;
void setFloodPacketFilter(const char* args, char* reply);
void deleteFloodPacketFilter(const char* args, char* reply);
void loadFloodGroupModeration();
bool saveFloodGroupModeration();
bool shouldBlockFloodGroupTextForward(const mesh::Packet* packet);
void formatFloodGroupModeration(const char* args, char* reply) const;
void formatFloodGroupModerationDetail(int index, char* reply, size_t reply_len) const;
void setFloodGroupModeration(const char* args, char* reply);
void deleteFloodGroupModeration(const char* args, char* reply);
bool decodeFloodGroupPlainText(const mesh::Packet* packet, const uint8_t* secret, uint8_t key_len,
uint32_t& timestamp, char* sender, size_t sender_len) const;
void loadClockSyncPrefs();
bool saveClockSyncPrefs();
void resetClockSyncAttempt();
void suppressMeshClockSyncForBoot(uint8_t source);
void checkGpsClockSyncOverride();
uint32_t estimateClockTransitMillis(const mesh::Packet* packet) const;
void recordClockSyncSample(uint8_t source_kind, const uint8_t source_id[4], uint32_t epoch,
const mesh::Packet* packet);
void recordAcceptedFloodClockSample(const mesh::Packet* packet);
void recordPublicChannelClockSample(const mesh::Packet* packet);
bool estimateMeshClock(uint32_t& estimate, uint8_t& fresh_count,
uint8_t& agreeing_count, uint8_t& required_count) const;
bool applyClockEstimate(uint32_t estimate, uint8_t source, uint8_t sample_count);
void checkClockSync();
void formatClockSyncStatus(char* reply, size_t reply_len) const;
int findFloodChannelBlockBySelector(const char* selector) const;
int findFloodChannelBlockSlot(const uint8_t prefix[FLOOD_CHANNEL_BLOCK_PREFIX_LEN], const char* name) const;
void formatFloodChannelBlockDetail(char* reply, int idx) const;
@@ -410,6 +527,8 @@ public:
_cli.savePrefs(_fs);
}
void onManualClockSet() override;
bool sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size);
// CommonCLICallbacks
+6 -1
View File
@@ -10,6 +10,11 @@ class RateLimiter {
public:
RateLimiter(uint16_t maximum, uint32_t secs): _maximum(maximum), _secs(secs), _start_timestamp(0), _count(0) { }
void reset() {
_start_timestamp = 0;
_count = 0;
}
bool allow(uint32_t now) {
if (now < _start_timestamp + _secs) {
_count++;
@@ -20,4 +25,4 @@ public:
}
return true;
}
};
};