Improve repeater reliability and build tooling

This commit is contained in:
mikecarper committed 2026-07-13 01:03:27 -07:00
1 parent 2b4aed4ef1
commit da00a00436
31 files changed
+1079 -229

No files matched your search

+49 -10
View File
@@ -1179,6 +1179,11 @@ void MyMesh::startInterface(BaseSerialInterface &serial) {
}
void MyMesh::handleCmdFrame(size_t len) {
if (len == 0) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
if (cmd_frame[0] == CMD_DEVICE_QUERY && len >= 2) { // sent when app establishes connection
app_target_ver = cmd_frame[1]; // which version of protocol does app understand
@@ -1288,6 +1293,10 @@ void MyMesh::handleCmdFrame(size_t len) {
: ERR_CODE_UNSUPPORTED_CMD); // unknown recipient, or unsupported TXT_TYPE_*
}
} else if (cmd_frame[0] == CMD_SEND_CHANNEL_TXT_MSG) { // send GroupChannel text msg
if (len < 7) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
int i = 1;
uint8_t txt_type = cmd_frame[i++]; // should be TXT_TYPE_PLAIN
uint8_t channel_idx = cmd_frame[i++];
@@ -1308,7 +1317,7 @@ void MyMesh::handleCmdFrame(size_t len) {
}
}
} else if (cmd_frame[0] == CMD_SEND_CHANNEL_DATA) { // send GroupChannel datagram
if (len < 4) {
if (len < 3) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
@@ -1325,6 +1334,14 @@ void MyMesh::handleCmdFrame(size_t len) {
// parse provided path if not flood
uint8_t path[MAX_PATH_SIZE];
size_t path_bytes = 0;
if (path_len != OUT_PATH_UNKNOWN) {
path_bytes = (size_t)(path_len & 63) * (size_t)((path_len >> 6) + 1);
}
if ((size_t)i + path_bytes + 2 > len) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
if (path_len != OUT_PATH_UNKNOWN) {
i += mesh::Packet::writePath(path, &cmd_frame[i], path_len);
}
@@ -1460,7 +1477,7 @@ void MyMesh::handleCmdFrame(size_t len) {
writeErrFrame(ERR_CODE_TABLE_FULL);
}
}
} else if (cmd_frame[0] == CMD_REMOVE_CONTACT) {
} else if (cmd_frame[0] == CMD_REMOVE_CONTACT && len >= 1 + PUB_KEY_SIZE) {
uint8_t *pub_key = &cmd_frame[1];
ContactInfo *recipient = lookupContactByPubKey(pub_key, PUB_KEY_SIZE);
if (recipient && removeContact(*recipient)) {
@@ -1471,7 +1488,7 @@ void MyMesh::handleCmdFrame(size_t len) {
} else {
writeErrFrame(ERR_CODE_NOT_FOUND); // not found, or unable to remove
}
} else if (cmd_frame[0] == CMD_SHARE_CONTACT) {
} else if (cmd_frame[0] == CMD_SHARE_CONTACT && len >= 1 + PUB_KEY_SIZE) {
uint8_t *pub_key = &cmd_frame[1];
ContactInfo *recipient = lookupContactByPubKey(pub_key, PUB_KEY_SIZE);
if (recipient) {
@@ -1483,7 +1500,7 @@ void MyMesh::handleCmdFrame(size_t len) {
} else {
writeErrFrame(ERR_CODE_NOT_FOUND);
}
} else if (cmd_frame[0] == CMD_GET_CONTACT_BY_KEY) {
} else if (cmd_frame[0] == CMD_GET_CONTACT_BY_KEY && len >= 1 + PUB_KEY_SIZE) {
uint8_t *pub_key = &cmd_frame[1];
ContactInfo *contact = lookupContactByPubKey(pub_key, PUB_KEY_SIZE);
if (contact) {
@@ -1540,6 +1557,10 @@ void MyMesh::handleCmdFrame(size_t len) {
_serial->writeFrame(out_frame, 1);
}
} else if (cmd_frame[0] == CMD_SET_RADIO_PARAMS) {
if (len < 11) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
int i = 1;
uint32_t freq;
memcpy(&freq, &cmd_frame[i], 4);
@@ -1579,6 +1600,10 @@ void MyMesh::handleCmdFrame(size_t len) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
}
} else if (cmd_frame[0] == CMD_SET_RADIO_TX_POWER) {
if (len < 2) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
int8_t power = (int8_t)cmd_frame[1];
if (power < -9 || power > MAX_LORA_TX_POWER) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
@@ -1589,6 +1614,10 @@ void MyMesh::handleCmdFrame(size_t len) {
writeOKFrame();
}
} else if (cmd_frame[0] == CMD_SET_TUNING_PARAMS) {
if (len < 9) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
int i = 1;
uint32_t rx, af;
memcpy(&rx, &cmd_frame[i], 4);
@@ -1607,6 +1636,10 @@ void MyMesh::handleCmdFrame(size_t len) {
memcpy(&out_frame[i], &af, 4); i += 4;
_serial->writeFrame(out_frame, i);
} else if (cmd_frame[0] == CMD_SET_OTHER_PARAMS) {
if (len < 2) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
_prefs.manual_add_contacts = cmd_frame[1];
if (len >= 3) {
_prefs.telemetry_mode_base = cmd_frame[2] & 0x03; // v5+
@@ -1623,7 +1656,7 @@ void MyMesh::handleCmdFrame(size_t len) {
updateGpsTelemetryPolicy();
savePrefs();
writeOKFrame();
} else if (cmd_frame[0] == CMD_SET_PATH_HASH_MODE && cmd_frame[1] == 0 && len >= 3) {
} else if (cmd_frame[0] == CMD_SET_PATH_HASH_MODE && len >= 3 && cmd_frame[1] == 0) {
if (cmd_frame[2] >= 3) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
} else {
@@ -1631,7 +1664,7 @@ void MyMesh::handleCmdFrame(size_t len) {
savePrefs();
writeOKFrame();
}
} else if (cmd_frame[0] == CMD_REBOOT && memcmp(&cmd_frame[1], "reboot", 6) == 0) {
} else if (cmd_frame[0] == CMD_REBOOT && len >= 7 && memcmp(&cmd_frame[1], "reboot", 6) == 0) {
if (dirty_contacts_expiry) { // is there are pending dirty contacts write needed?
saveContacts();
}
@@ -1767,7 +1800,7 @@ void MyMesh::handleCmdFrame(size_t len) {
} else {
writeErrFrame(ERR_CODE_NOT_FOUND); // contact not found
}
} else if (cmd_frame[0] == CMD_SEND_PATH_DISCOVERY_REQ && cmd_frame[1] == 0 && len >= 2 + PUB_KEY_SIZE) {
} else if (cmd_frame[0] == CMD_SEND_PATH_DISCOVERY_REQ && len >= 2 + PUB_KEY_SIZE && cmd_frame[1] == 0) {
uint8_t *pub_key = &cmd_frame[2];
ContactInfo *recipient = lookupContactByPubKey(pub_key, PUB_KEY_SIZE);
if (recipient) {
@@ -2100,7 +2133,7 @@ void MyMesh::handleCmdFrame(size_t len) {
} else {
writeErrFrame(ERR_CODE_ILLEGAL_ARG); // invalid stats sub-type
}
} else if (cmd_frame[0] == CMD_FACTORY_RESET && memcmp(&cmd_frame[1], "reset", 5) == 0) {
} else if (cmd_frame[0] == CMD_FACTORY_RESET && len >= 6 && memcmp(&cmd_frame[1], "reset", 5) == 0) {
if (_serial) {
MESH_DEBUG_PRINTLN("Factory reset: disabling serial interface to prevent reconnects (BLE/WiFi)");
_serial->disable(); // Phone app disconnects before we can send OK frame so it's safe here
@@ -2126,9 +2159,11 @@ void MyMesh::handleCmdFrame(size_t len) {
writeOKFrame();
} else if (cmd_frame[0] == CMD_SET_DEFAULT_FLOOD_SCOPE && len >= 1) {
if (len >= 1+31+16) {
int n = strlen((char *) &cmd_frame[1]);
const void* terminator = memchr(&cmd_frame[1], 0, 31);
size_t n = terminator == NULL ? 31 : (const uint8_t*)terminator - &cmd_frame[1];
if (n > 0 && n < 31) {
strcpy(_prefs.default_scope_name, (char *) &cmd_frame[1]);
memcpy(_prefs.default_scope_name, &cmd_frame[1], n);
_prefs.default_scope_name[n] = 0;
memcpy(_prefs.default_scope_key, &cmd_frame[1+31], 16);
savePrefs();
writeOKFrame();
@@ -2159,6 +2194,10 @@ void MyMesh::handleCmdFrame(size_t len) {
writeErrFrame(ERR_CODE_TABLE_FULL);
}
} else if (cmd_frame[0] == CMD_SET_AUTOADD_CONFIG) {
if (len < 2) {
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
return;
}
_prefs.autoadd_config = cmd_frame[1];
if (len >= 3) {
_prefs.autoadd_max_hops = min(cmd_frame[2], (uint8_t)64);
+249 -57
View File
@@ -101,7 +101,8 @@
#endif
#define LOW_BATTERY_MIN_VALID_MV 1000
#define LOW_BATTERY_CHECK_INTERVAL (60UL * 1000UL)
#define LOW_BATTERY_STARTUP_DELAY (30ULL * 60ULL * 1000ULL)
#define LOW_BATTERY_CHECK_INTERVAL (30UL * 60UL * 1000UL)
#define LOW_BATTERY_WARN_INTERVAL (24UL * 60UL * 60UL * 1000UL)
#define LOW_BATTERY_CRITICAL_INTERVAL (12UL * 60UL * 60UL * 1000UL)
@@ -315,6 +316,10 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr
}
client = acl.putClient(sender, 0); // add to contacts (if not already known)
if (client == NULL) {
MESH_DEBUG_PRINTLN("Login rejected: ACL is full of protected contacts");
return 0;
}
if (sender_timestamp <= client->last_timestamp) {
MESH_DEBUG_PRINTLN("Possible login replay attack!");
return 0; // FATAL: client table is full -OR- replay attack
@@ -1391,6 +1396,61 @@ uint8_t MyMesh::floodRetrySourceMask(const mesh::Packet* packet) const {
return floodRetryBucketMaskForPrefix(source_prefix, hash_size, true);
}
bool MyMesh::floodRetryBridgeBucketFresh(uint8_t bucket) const {
if (bucket > FLOOD_RETRY_BRIDGE_OTHER_BUCKET) {
return false;
}
const uint8_t (*prefixes)[FLOOD_RETRY_PREFIX_LEN];
uint8_t prefix_count;
if (bucket == FLOOD_RETRY_BRIDGE_OTHER_BUCKET) {
prefixes = _prefs.flood_retry_prefixes;
prefix_count = FLOOD_RETRY_PREFIX_SLOTS;
} else {
prefixes = _prefs.flood_retry_bridge_buckets[bucket];
prefix_count = FLOOD_RETRY_BUCKET_PREFIXES;
}
for (uint8_t i = 0; i < prefix_count; i++) {
const uint8_t* configured = prefixes[i];
if ((configured[0] != 0 || configured[1] != 0 || configured[2] != 0)
&& !floodRetryPrefixIgnored(configured, FLOOD_RETRY_PREFIX_LEN)
&& floodRetryPrefixFresh(configured, FLOOD_RETRY_PREFIX_LEN)) {
return true;
}
}
const FloodRetryBridgeReachability& reachable = flood_retry_bridge_reachability[bucket];
if (reachable.prefix_len == 0 || reachable.last_heard_millis == 0
|| (uint32_t)(millis() - reachable.last_heard_millis) > 3600000UL) {
return false;
}
return (floodRetryBucketMaskForPrefix(reachable.prefix, reachable.prefix_len, false)
& floodRetryBucketMask(bucket)) != 0;
}
void MyMesh::recordFloodRetryBridgeReachability(const uint8_t* prefix, uint8_t prefix_len,
uint8_t bucket_mask) {
if (prefix == NULL || prefix_len == 0 || prefix_len > MAX_ROUTE_HASH_BYTES) {
return;
}
uint32_t now = millis();
if (now == 0) {
now = 1; // zero means unused
}
for (uint8_t bucket = 0; bucket <= FLOOD_RETRY_BRIDGE_OTHER_BUCKET; bucket++) {
if ((bucket_mask & floodRetryBucketMask(bucket)) == 0) {
continue;
}
FloodRetryBridgeReachability& reachable = flood_retry_bridge_reachability[bucket];
memset(reachable.prefix, 0, sizeof(reachable.prefix));
memcpy(reachable.prefix, prefix, prefix_len);
reachable.prefix_len = prefix_len;
reachable.last_heard_millis = now;
}
}
uint8_t MyMesh::floodRetryBridgeTargetMask(uint8_t source_mask) const {
uint8_t mask = 0;
for (int bucket = 0; bucket < FLOOD_RETRY_BRIDGE_BUCKETS; bucket++) {
@@ -1398,27 +1458,14 @@ uint8_t MyMesh::floodRetryBridgeTargetMask(uint8_t source_mask) const {
if ((source_mask & bucket_mask) != 0) {
continue;
}
for (int i = 0; i < FLOOD_RETRY_BUCKET_PREFIXES; i++) {
const uint8_t* configured = _prefs.flood_retry_bridge_buckets[bucket][i];
if ((configured[0] != 0 || configured[1] != 0 || configured[2] != 0)
&& !floodRetryPrefixIgnored(configured, FLOOD_RETRY_PREFIX_LEN)
&& floodRetryPrefixFresh(configured, FLOOD_RETRY_PREFIX_LEN)) {
mask |= bucket_mask;
break;
}
if (floodRetryBridgeBucketFresh((uint8_t)bucket)) {
mask |= bucket_mask;
}
}
uint8_t other_mask = floodRetryBucketMask(FLOOD_RETRY_BRIDGE_OTHER_BUCKET);
if ((source_mask & other_mask) == 0) {
for (int i = 0; i < FLOOD_RETRY_PREFIX_SLOTS; i++) {
const uint8_t* configured = _prefs.flood_retry_prefixes[i];
if ((configured[0] != 0 || configured[1] != 0 || configured[2] != 0)
&& !floodRetryPrefixIgnored(configured, FLOOD_RETRY_PREFIX_LEN)
&& floodRetryPrefixFresh(configured, FLOOD_RETRY_PREFIX_LEN)) {
mask |= other_mask;
break;
}
}
if ((source_mask & other_mask) == 0
&& floodRetryBridgeBucketFresh(FLOOD_RETRY_BRIDGE_OTHER_BUCKET)) {
mask |= other_mask;
}
return mask;
}
@@ -1515,14 +1562,20 @@ bool MyMesh::allowFloodRetry(const mesh::Packet* packet) const {
return true;
}
void MyMesh::clearFloodRetryBridgeState(const mesh::Packet* packet) {
FloodRetryBridgeState* state = floodRetryBridgeStateFor(packet, false);
if (state != NULL) {
state->active = false;
void MyMesh::clearFloodRetryBridgeStateByKey(const uint8_t* retry_key) {
if (retry_key == NULL) {
return;
}
for (int i = 0; i < MAX_FLOOD_RETRY_SLOTS; i++) {
if (flood_retry_bridge_states[i].active
&& memcmp(flood_retry_bridge_states[i].key, retry_key, MAX_HASH_SIZE) == 0) {
flood_retry_bridge_states[i].active = false;
return;
}
}
}
void MyMesh::refreshFloodRetryHeardRecent(const mesh::Packet* packet) {
void MyMesh::refreshFloodRetryReachability(const mesh::Packet* packet) {
if (packet == NULL || !packet->isRouteFlood() || packet->getPathHashCount() == 0) {
return;
}
@@ -1536,11 +1589,16 @@ void MyMesh::refreshFloodRetryHeardRecent(const mesh::Packet* packet) {
if (tables == NULL) {
return;
}
uint8_t path_count = packet->getPathHashCount();
const uint8_t* last_hop = &packet->path[(path_count - 1) * hash_size];
tables->setRecentRepeater(last_hop, hash_size, packet->_snr, false, true);
const uint8_t* path = packet->path;
if (_prefs.flood_retry_bridge_enabled) {
FloodRetryBridgeState* state = floodRetryBridgeStateFor(packet, false);
if (state != NULL) {
for (int hop = 0; hop < packet->getPathHashCount(); hop++) {
for (uint8_t hop = 0; hop < path_count; hop++) {
if (state->progress_marker > 0 && hop == state->progress_marker - 1) {
path += hash_size;
continue;
@@ -1548,17 +1606,13 @@ void MyMesh::refreshFloodRetryHeardRecent(const mesh::Packet* packet) {
uint8_t bucket_mask = floodRetryBucketMaskForPathHop(path, hash_size, (uint8_t)hop,
state->progress_marker);
bucket_mask &= state->target_mask & (uint8_t)~state->source_mask;
if (bucket_mask != 0) {
tables->setRecentRepeater(path, hash_size, packet->_snr, false, true);
if (bucket_mask != 0 && hop != path_count - 1) {
recordFloodRetryBridgeReachability(path, hash_size, bucket_mask);
}
path += hash_size;
}
return;
}
}
const uint8_t* heard_prefix = &packet->path[(packet->getPathHashCount() - 1) * hash_size];
tables->setRecentRepeater(heard_prefix, hash_size, packet->_snr, false, true);
}
void MyMesh::formatFloodRetryPath(char* dest, size_t dest_len, const mesh::Packet* packet) const {
@@ -1676,19 +1730,26 @@ bool MyMesh::formatFloodRetryHeard(char* dest, size_t dest_len, const mesh::Pack
}
void MyMesh::onFloodRetryEvent(const char* event, const mesh::Packet* packet, uint32_t delay_millis, uint8_t retry_attempt) {
if (event == NULL || packet == NULL) {
if (event == NULL) {
return;
}
bool clear_bridge_state = _prefs.flood_retry_bridge_enabled
&& (strcmp(event, "good") == 0 || strcmp(event, "failure") == 0 || strcmp(event, "failed_all_tries") == 0
|| strncmp(event, "dropped_", 8) == 0);
if (clear_bridge_state && strcmp(event, "failure") == 0) {
clearFloodRetryBridgeState(packet);
if (strcmp(event, "failure") == 0) {
return;
}
if (strcmp(event, "failure") == 0) {
if (packet == NULL) {
MESH_DEBUG_PRINTLN("flood retry %s (retry=%u, elapsed_ms=%lu, packet=released)",
event, (unsigned int)retry_attempt, (unsigned long)delay_millis);
if (_logging) {
File f = openAppend(PACKET_LOG_FILE);
if (f) {
f.print(getLogDateTime());
f.printf(": FLOOD RETRY %s (retry=%u, elapsed_ms=%lu, packet=released)\n",
event, (unsigned int)retry_attempt, (unsigned long)delay_millis);
f.close();
}
}
return;
}
@@ -1708,7 +1769,7 @@ void MyMesh::onFloodRetryEvent(const char* event, const mesh::Packet* packet, ui
formatFloodRetryPath(path_log, sizeof(path_log), packet);
heard_suffix[0] = 0;
if (strcmp(event, "good") == 0 && formatFloodRetryHeard(heard_log, sizeof(heard_log), packet)) {
refreshFloodRetryHeardRecent(packet);
refreshFloodRetryReachability(packet);
snprintf(heard_suffix, sizeof(heard_suffix), ", heard=%s", heard_log);
}
uint8_t log_cr = getRetryLogCodingRate(packet, getDefaultTxCodingRate());
@@ -1749,9 +1810,10 @@ void MyMesh::onFloodRetryEvent(const char* event, const mesh::Packet* packet, ui
}
}
if (clear_bridge_state) {
clearFloodRetryBridgeState(packet);
}
}
void MyMesh::onFloodRetrySlotReleased(const uint8_t* retry_key) {
clearFloodRetryBridgeStateByKey(retry_key);
}
bool MyMesh::hasFloodRetryTargetPrefix(const mesh::Packet* packet) const {
@@ -2247,6 +2309,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
_logging = false;
region_load_active = false;
memset(flood_retry_bridge_states, 0, sizeof(flood_retry_bridge_states));
memset(flood_retry_bridge_reachability, 0, sizeof(flood_retry_bridge_reachability));
recv_pkt_region = NULL;
memset(flood_channel_blocks, 0, sizeof(flood_channel_blocks));
@@ -2445,14 +2508,14 @@ void MyMesh::begin(FILESYSTEM *fs) {
#endif
}
void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) {
bool MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) {
if (scope.isNull()) {
sendFlood(pkt, delay_millis, path_hash_size);
return sendFlood(pkt, delay_millis, path_hash_size);
} else {
uint16_t codes[2];
codes[0] = scope.calcTransportCode(pkt);
codes[1] = 0; // REVISIT: set to 'home' Region, for sender/return region?
sendFlood(pkt, codes, delay_millis, path_hash_size);
return sendFlood(pkt, codes, delay_millis, path_hash_size);
}
}
@@ -2477,7 +2540,66 @@ bool MyMesh::resolveAlertScope(TransportKey& dest) {
return false;
}
bool MyMesh::sendRepeatersFloodText(const char* text) {
const RegionEntry* MyMesh::findNarrowestBatteryAlertRegion(bool& ambiguous) {
ambiguous = false;
const RegionEntry* narrowest = NULL;
uint8_t narrowest_depth = 0;
const int region_count = region_map.getCount();
for (int i = 0; i < region_count; i++) {
const RegionEntry* candidate = region_map.getByIdx(i);
uint8_t depth = 1;
uint16_t parent_id = candidate->parent;
bool valid = true;
// Region files are validated when loaded, but the live map can be edited
// before it is saved. Bound the walk so a temporary cycle cannot hang the
// repeater while selecting the default battery-alert scope.
for (int hops = 0; parent_id != 0; hops++) {
if (hops >= region_count) {
valid = false;
break;
}
const RegionEntry* parent = region_map.findById(parent_id);
if (parent == NULL || parent->isWildcard()) {
valid = false;
break;
}
depth++;
parent_id = parent->parent;
}
if (!valid) continue;
// A named region is not necessarily a usable transport scope. In
// particular, private regions need a stored key; do not select one merely
// because it happens to be the deepest entry in the hierarchy.
TransportKey candidate_scope;
if (!getBatteryAlertScopeForRegion(*candidate, candidate_scope)) continue;
if (depth > narrowest_depth) {
narrowest = candidate;
narrowest_depth = depth;
ambiguous = false;
} else if (depth == narrowest_depth) {
ambiguous = true;
}
}
return narrowest;
}
bool MyMesh::getBatteryAlertScopeForRegion(const RegionEntry& region, TransportKey& scope) {
if (region.isWildcard()) return false;
return region_map.getTransportKeysFor(region, &scope, 1) > 0 && !scope.isNull();
}
bool MyMesh::resolveBatteryAlertScope(TransportKey& scope) {
if (_prefs.battery_alert_region[0] == 0) return false;
const RegionEntry* region = region_map.findByName(_prefs.battery_alert_region);
return region != NULL && getBatteryAlertScopeForRegion(*region, scope);
}
bool MyMesh::sendRepeatersFloodText(const char* text, const TransportKey* scope) {
if (text == NULL || *text == 0) return false;
mesh::GroupChannel channel;
@@ -2521,8 +2643,8 @@ bool MyMesh::sendRepeatersFloodText(const char* text) {
return false;
}
sendFloodScoped(default_scope, pkt, 0, _prefs.path_hash_mode + 1);
return true;
const TransportKey& send_scope = scope == NULL ? default_scope : *scope;
return sendFloodScoped(send_scope, pkt, 0, _prefs.path_hash_mode + 1);
}
void MyMesh::checkBatteryAlert() {
@@ -2531,11 +2653,26 @@ void MyMesh::checkBatteryAlert() {
return;
}
// Ignore startup voltage sag and give solar/charger hardware time to settle.
// uptime_millis is 64-bit and includes time spent in the platform's light or
// event sleep, so this guard remains reliable across millis() wraparound.
if (uptime_millis < LOW_BATTERY_STARTUP_DELAY) {
return;
}
if (next_battery_alert_check && !millisHasNowPassed(next_battery_alert_check)) {
return;
}
next_battery_alert_check = futureMillis(LOW_BATTERY_CHECK_INTERVAL);
// Check the cheap configuration path first. This avoids powering the ADC or
// battery-divider circuitry when the selected region has been removed or no
// longer has a usable transport key.
TransportKey alert_scope;
if (!resolveBatteryAlertScope(alert_scope)) {
return; // low-battery alerts are never sent as an unscoped flood
}
uint16_t batt_mv = board.getBattMilliVolts();
uint8_t batt_pct = batteryPercentFromMilliVolts(batt_mv);
if (batt_mv <= LOW_BATTERY_MIN_VALID_MV || batt_pct >= _prefs.battery_alert_low_percent) {
@@ -2552,7 +2689,7 @@ void MyMesh::checkBatteryAlert() {
char text[96];
snprintf(text, sizeof(text), "LOW BATTERY %u%% (%u mV)", (uint32_t)batt_pct, (uint32_t)batt_mv);
if (sendRepeatersFloodText(text)) {
if (sendRepeatersFloodText(text, &alert_scope)) {
battery_alert_sent = true;
last_battery_alert_sent = millis();
}
@@ -2579,6 +2716,19 @@ bool MyMesh::hasStartedScheduledTempRadio() const {
return false;
}
#if defined(ENABLE_OTA)
bool MyMesh::isTempRadioActive() const {
const uint32_t now = getRTCClock()->getCurrentTime();
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
const ScheduledRadioSetting& setting = scheduled_radio_settings[i];
if (setting.active && setting.temporary && setting.started && now < setting.end_time) {
return true;
}
}
return false;
}
#endif
int MyMesh::findFreeScheduledRadioSlot() const {
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
if (!scheduled_radio_settings[i].active) {
@@ -2921,7 +3071,8 @@ void MyMesh::processScheduledRadioSettings() {
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
ScheduledRadioSetting& setting = scheduled_radio_settings[i];
if (setting.active && setting.temporary && setting.started && now >= setting.end_time) {
if (setting.active && setting.temporary && setting.started && now >= setting.end_time
&& !hasOutbound()) {
setting.active = false;
setting.started = false;
temp_ended = true;
@@ -3180,6 +3331,7 @@ void MyMesh::formatNeighborsReply(char *reply) {
void MyMesh::removeNeighbor(const uint8_t *pubkey, int key_len) {
#if MAX_NEIGHBOURS
if (pubkey == NULL || key_len <= 0 || key_len > PUB_KEY_SIZE) return;
for (int i = 0; i < MAX_NEIGHBOURS; i++) {
NeighbourInfo *neighbour = &neighbours[i];
if (memcmp(neighbour->id.pub_key, pubkey, key_len) == 0) {
@@ -3768,13 +3920,14 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
if (sp == NULL) {
strcpy(reply, "Err - bad params");
} else {
size_t hex_len = (size_t)(sp - hex);
*sp++ = 0; // replace space with null terminator
uint8_t pubkey[PUB_KEY_SIZE];
int hex_len = min(sp - hex, PUB_KEY_SIZE*2);
if (mesh::Utils::fromHex(pubkey, hex_len / 2, hex)) {
if (hex_len > 0 && hex_len <= PUB_KEY_SIZE * 2 && (hex_len & 1) == 0
&& mesh::Utils::fromHex(pubkey, (int)(hex_len / 2), hex)) {
uint8_t perms = atoi(sp);
if (acl.applyPermissions(self_id, pubkey, hex_len / 2, perms)) {
if (acl.applyPermissions(self_id, pubkey, (int)(hex_len / 2), perms)) {
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger acl.save()
strcpy(reply, "OK");
} else {
@@ -3849,24 +4002,63 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
}
} else if (strcmp(command, "get battery.alert") == 0) {
sprintf(reply, "> %s", _prefs.battery_alert_enabled ? "on" : "off");
} else if (strcmp(command, "get battery.alert.region") == 0) {
sprintf(reply, "> %s", _prefs.battery_alert_region[0]
? _prefs.battery_alert_region : "<unset>");
} else if (strcmp(command, "get battery.alert.low") == 0) {
sprintf(reply, "> %u", (uint32_t)_prefs.battery_alert_low_percent);
} else if (strcmp(command, "get battery.alert.critical") == 0) {
sprintf(reply, "> %u", (uint32_t)_prefs.battery_alert_critical_percent);
} else if (strncmp(command, "set battery.alert ", 18) == 0) {
const char* value = command + 18;
if (strcmp(value, "on") == 0) {
if (strncmp(value, "on", 2) == 0 && (value[2] == 0 || value[2] == ' ')) {
const char* region_name = skipLocalSpaces(value + 2);
const RegionEntry* region = NULL;
bool ambiguous = false;
if (*region_name) {
if (strchr(region_name, ' ') != NULL) {
strcpy(reply, "Err - region names cannot contain spaces");
return;
}
region = region_map.findByName(region_name);
if (region == NULL || region->isWildcard()) {
strcpy(reply, "Err - unknown or invalid alert region");
return;
}
} else {
region = findNarrowestBatteryAlertRegion(ambiguous);
if (region == NULL) {
strcpy(reply, "Err - define a usable region before enabling battery alerts");
return;
}
if (ambiguous) {
strcpy(reply, "Err - multiple narrowest regions; specify one");
return;
}
}
TransportKey region_scope;
if (!getBatteryAlertScopeForRegion(*region, region_scope)) {
strcpy(reply, "Err - alert region has no usable transport key");
return;
}
StrHelper::strncpy(_prefs.battery_alert_region, region->name,
sizeof(_prefs.battery_alert_region));
_prefs.battery_alert_enabled = 1;
next_battery_alert_check = 0;
battery_alert_sent = false;
savePrefs();
strcpy(reply, "OK");
sprintf(reply, "OK - battery alerts scoped to %s", _prefs.battery_alert_region);
} else if (strcmp(value, "off") == 0) {
_prefs.battery_alert_enabled = 0;
next_battery_alert_check = 0;
battery_alert_sent = false;
savePrefs();
strcpy(reply, "OK");
} else {
strcpy(reply, "Err - usage: set battery.alert <on|off>");
strcpy(reply, "Err - usage: set battery.alert <on [region]|off>");
}
} else if (strncmp(command, "set battery.alert.low ", 22) == 0) {
uint8_t percent;
+17 -5
View File
@@ -143,6 +143,11 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
uint8_t progress_marker;
bool active;
};
struct FloodRetryBridgeReachability {
uint8_t prefix[MAX_ROUTE_HASH_BYTES];
uint8_t prefix_len;
uint32_t last_heard_millis;
};
struct FloodChannelBlockEntry {
bool active;
uint8_t key_len;
@@ -152,6 +157,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
char name[FLOOD_CHANNEL_BLOCK_NAME_LEN];
};
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];
uint32_t pending_discover_tag;
unsigned long pending_discover_until;
@@ -212,12 +218,14 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
uint8_t floodRetryBucketMaskForPathHop(const uint8_t* prefix, uint8_t prefix_len, uint8_t hop,
uint8_t progress_marker) const;
uint8_t floodRetrySourceMask(const mesh::Packet* packet) const;
bool floodRetryBridgeBucketFresh(uint8_t bucket) const;
void recordFloodRetryBridgeReachability(const uint8_t* prefix, uint8_t prefix_len, uint8_t bucket_mask);
uint8_t floodRetryBridgeTargetMask(uint8_t source_mask) const;
uint8_t floodRetryBridgeHeardMask(const mesh::Packet* packet, uint8_t source_mask,
uint8_t progress_marker) const;
FloodRetryBridgeState* floodRetryBridgeStateFor(const mesh::Packet* packet, bool create) const;
void clearFloodRetryBridgeState(const mesh::Packet* packet);
void refreshFloodRetryHeardRecent(const mesh::Packet* packet);
void clearFloodRetryBridgeStateByKey(const uint8_t* retry_key);
void refreshFloodRetryReachability(const mesh::Packet* packet);
void formatFloodRetryPath(char* dest, size_t dest_len, const mesh::Packet* packet) const;
bool formatFloodRetryHeard(char* dest, size_t dest_len, const mesh::Packet* packet) const;
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
@@ -227,7 +235,10 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
uint8_t handleAnonClockReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len);
mesh::Packet* createSelfAdvert();
bool sendRepeatersFloodText(const char* text);
bool sendRepeatersFloodText(const char* text, const TransportKey* scope = nullptr);
const RegionEntry* findNarrowestBatteryAlertRegion(bool& ambiguous);
bool getBatteryAlertScopeForRegion(const RegionEntry& region, TransportKey& scope);
bool resolveBatteryAlertScope(TransportKey& scope);
void checkBatteryAlert();
void printRecentRepeatersSerial();
@@ -268,7 +279,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
protected:
#if defined(ENABLE_OTA)
bool isTempRadioActive() const override { return hasStartedScheduledTempRadio(); }
bool isTempRadioActive() const override;
#endif
float getAirtimeBudgetFactor() const override {
return _prefs.airtime_factor;
@@ -299,6 +310,7 @@ protected:
void onDirectRetrySucceeded(const uint8_t* next_hop_hash, uint8_t next_hop_hash_len, int8_t snr_x4) override;
bool allowFloodRetry(const mesh::Packet* packet) const override;
void onFloodRetryEvent(const char* event, const mesh::Packet* packet, uint32_t delay_millis, uint8_t retry_attempt) override;
void onFloodRetrySlotReleased(const uint8_t* retry_key) override;
bool hasFloodRetryTargetPrefix(const mesh::Packet* packet) const override;
uint8_t getFloodRetryMaxPathLength(const mesh::Packet* packet) const override;
uint8_t getFloodRetryMaxAttempts(const mesh::Packet* packet) const override;
@@ -359,7 +371,7 @@ public:
_cli.savePrefs(_fs);
}
void sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size);
bool sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size);
// CommonCLICallbacks
void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override;
+9 -4
View File
@@ -360,6 +360,10 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
}
client = acl.putClient(sender, 0); // add to known clients (if not already known)
if (client == NULL) {
MESH_DEBUG_PRINTLN("Login rejected: ACL is full of protected contacts");
return;
}
if (sender_timestamp <= client->last_timestamp) {
MESH_DEBUG_PRINTLN("possible replay attack!");
return;
@@ -1013,13 +1017,14 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
if (sp == NULL) {
strcpy(reply, "Err - bad params");
} else {
size_t hex_len = (size_t)(sp - hex);
*sp++ = 0; // replace space with null terminator
uint8_t pubkey[PUB_KEY_SIZE];
int hex_len = min(sp - hex, PUB_KEY_SIZE*2);
if (mesh::Utils::fromHex(pubkey, hex_len / 2, hex)) {
if (hex_len > 0 && hex_len <= PUB_KEY_SIZE * 2 && (hex_len & 1) == 0
&& mesh::Utils::fromHex(pubkey, (int)(hex_len / 2), hex)) {
uint8_t perms = atoi(sp);
if (acl.applyPermissions(self_id, pubkey, hex_len / 2, perms)) {
if (acl.applyPermissions(self_id, pubkey, (int)(hex_len / 2), perms)) {
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger acl.save()
strcpy(reply, "OK");
} else {
@@ -1120,7 +1125,7 @@ void MyMesh::loop() {
MESH_DEBUG_PRINTLN("Temp radio params");
}
if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig
if (revert_radio_at && millisHasNowPassed(revert_radio_at) && !hasOutbound()) { // revert radio params to orig
revert_radio_at = 0; // clear timer
radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
MESH_DEBUG_PRINTLN("Radio params restored");
+3 -1
View File
@@ -140,7 +140,9 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
protected:
#if defined(ENABLE_OTA)
bool isTempRadioActive() const override { return set_radio_at == 0 && revert_radio_at != 0; }
bool isTempRadioActive() const override {
return set_radio_at == 0 && revert_radio_at != 0 && !millisHasNowPassed(revert_radio_at);
}
#endif
float getAirtimeBudgetFactor() const override {
return _prefs.airtime_factor;
+3
View File
@@ -127,6 +127,9 @@ void loop() {
#endif
if (the_mesh.getNodePrefs()->powersaving_enabled && !board.isUsbDataConnected()) {
uint32_t sleep_secs = the_mesh.getPowerSaveSleepSeconds(30);
#ifdef HAS_EXTERNAL_WATCHDOG
if (sleep_secs > 0) external_watchdog.feed();
#endif
#if defined(NRF52_PLATFORM)
if (sleep_secs > 0) {
board.sleep(0); // nrf ignores seconds param, sleeps whenever possible
+9 -4
View File
@@ -383,6 +383,10 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t*
}
client = acl.putClient(sender, PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO); // add to contacts (if not already known)
if (client == NULL) {
MESH_DEBUG_PRINTLN("Login rejected: ACL is full of protected contacts");
return 0;
}
if (sender_timestamp <= client->last_timestamp) {
MESH_DEBUG_PRINTLN("Possible login replay attack!");
return 0; // FATAL: client table is full -OR- replay attack
@@ -435,13 +439,14 @@ void SensorMesh::handleCommand(uint32_t sender_timestamp, char* command, char* r
if (sp == NULL) {
strcpy(reply, "Err - bad params");
} else {
size_t hex_len = (size_t)(sp - hex);
*sp++ = 0; // replace space with null terminator
uint8_t pubkey[PUB_KEY_SIZE];
int hex_len = min(sp - hex, PUB_KEY_SIZE*2);
if (mesh::Utils::fromHex(pubkey, hex_len / 2, hex)) {
if (hex_len > 0 && hex_len <= PUB_KEY_SIZE * 2 && (hex_len & 1) == 0
&& mesh::Utils::fromHex(pubkey, (int)(hex_len / 2), hex)) {
uint8_t perms = atoi(sp);
if (acl.applyPermissions(self_id, pubkey, hex_len / 2, perms)) {
if (acl.applyPermissions(self_id, pubkey, (int)(hex_len / 2), perms)) {
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger acl.save()
updateGpsTelemetryPolicy();
strcpy(reply, "OK");
@@ -970,7 +975,7 @@ void SensorMesh::loop() {
MESH_DEBUG_PRINTLN("Temp radio params");
}
if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig
if (revert_radio_at && millisHasNowPassed(revert_radio_at) && !hasOutbound()) { // revert radio params to orig
revert_radio_at = 0; // clear timer
radio_driver.setParams(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
MESH_DEBUG_PRINTLN("Radio params restored");
+3 -1
View File
@@ -85,7 +85,9 @@ public:
protected:
#if defined(ENABLE_OTA)
bool isTempRadioActive() const override { return set_radio_at == 0 && revert_radio_at != 0; }
bool isTempRadioActive() const override {
return set_radio_at == 0 && revert_radio_at != 0 && !millisHasNowPassed(revert_radio_at);
}
#endif
// current telemetry data queries
float getVoltage(uint8_t channel) { return getTelemValue(channel, LPP_VOLTAGE); }