Harden marathon-tested OTA and hardware paths

Add durable TempRadio, ACL, clock, bridge, GPS/I2C, USB logging, and Indicator recovery safeguards together with expanded automation, build/profile contracts, release metadata, and the hardware validation ledger.
This commit is contained in:
mikecarper committed 2026-08-30 19:26:15 -07:00
1 parent e26d48e4ca
commit 9fd580c89e
109 files changed
+18697 -1094

No files matched your search

+40 -20
View File
@@ -2233,6 +2233,29 @@ bool MyMesh::handleLocalControlCommand(const char* command, char* reply,
return true;
}
if (strncmp(command, "set pin", 7) == 0
&& (command[7] == 0 || command[7] == ' '
|| command[7] == '\t')) {
const char* value = command + 7;
while (*value == ' ' || *value == '\t') value++;
int32_t parsed = 0;
if (!mesh::cli::parseIntegerStrict(value, parsed)
|| parsed < 0 || parsed > 999999) {
snprintf(reply, reply_size, "Error: pin must be 0-999999");
} else {
const uint32_t previous = _prefs.ble_pin;
_prefs.ble_pin = static_cast<uint32_t>(parsed);
if (!savePrefs()) {
_prefs.ble_pin = previous;
snprintf(reply, reply_size, "Error: pin changed but save failed");
} else {
snprintf(reply, reply_size, "> pin is now %06lu",
(unsigned long)_prefs.ble_pin);
}
}
return true;
}
#if defined(MESH_PRIMARY_ESPNOW) && MESH_PRIMARY_ESPNOW
if (strcmp(command, "get espnow.channel") == 0) {
formatEspNowChannel(reply, reply_size);
@@ -5999,7 +6022,17 @@ void MyMesh::handleTerminalCommand(char* command) {
terminalOutput().printf(" OK - FEM TX gain %s\r\n", value);
}
} else {
terminalOutput().printf(" ERROR: unknown setting: %s\r\n", config);
// The terminal has a few presentation-specific setters above, while
// handleCommand() owns the shared radio-pref and board-specific command
// surface used by framed/rescue clients. Use it only as the fallback so
// Full Companion does not develop holes such as `set radio ...` without
// changing the established terminal behavior of `set tx`, `set name`,
// or the WiFi commands.
if (handleCommand(command, 0, local_reply)) {
terminalOutput().printf(" %s\r\n", local_reply);
} else {
terminalOutput().printf(" ERROR: unknown setting: %s\r\n", config);
}
}
} else if (strcmp(command, "reboot") == 0) {
terminalOutput().print(" OK - rebooting in 1 second\r\n");
@@ -6018,6 +6051,7 @@ void MyMesh::handleTerminalCommand(char* command) {
terminalOutput().print(" set {name|lat|lon|freq|tx|af} {value}\r\n");
terminalOutput().print(" get bluetooth.name\r\n");
terminalOutput().print(" set bluetooth.name <name|default>\r\n");
terminalOutput().print(" set pin <0-999999>\r\n");
terminalOutput().print(" powersaving [on|off]\r\n");
#if MESH_USB_LOGGING_AVAILABLE
terminalOutput().print(" get usb.logging\r\n");
@@ -6086,6 +6120,11 @@ void MyMesh::handleTerminalCommand(char* command) {
} else {
terminalOutput().print(" disconnect (closes the TCP terminal)\r\n");
}
} else if (handleCommand(command, 0, local_reply)) {
// Fill the same safe shared-command surface for getters (`get name`,
// `get radio`, `get tx`, and variant commands). Terminal-only commands
// above still win, including the richer `ver` response and reboot flow.
terminalOutput().printf(" %s\r\n", local_reply);
} else {
terminalOutput().printf(" ERROR: unknown command: %s\r\n", command);
}
@@ -6235,25 +6274,6 @@ bool MyMesh::handleCommand(const char* command, uint32_t sender_timestamp,
return true;
}
if (strncmp(command, "set pin ", 8) == 0) {
int32_t parsed = 0;
if (!mesh::cli::parseIntegerStrict(command + 8, parsed)
|| parsed < 0 || parsed > 999999) {
strcpy(reply, "Error: pin must be 0-999999");
} else {
const uint32_t previous = _prefs.ble_pin;
_prefs.ble_pin = static_cast<uint32_t>(parsed);
if (!savePrefs()) {
_prefs.ble_pin = previous;
strcpy(reply, "Error: pin changed but save failed");
} else {
snprintf(reply, reply_capacity, "> pin is now %06lu",
(unsigned long)_prefs.ble_pin);
}
}
return true;
}
if (strcmp(command, "ver") == 0) {
snprintf(reply, reply_capacity, "%s (Build: %s)", FIRMWARE_VERSION,
FIRMWARE_BUILD_DATE);
+94 -28
View File
@@ -67,6 +67,7 @@ MultiSerialInterface interface_manager;
#include <helpers/CLICommandUtils.h>
#include <helpers/WiFiReconnectPolicy.h>
#include <helpers/WiFiPowerSave.h>
#include <helpers/ui/DisplayTextLayout.h>
#include <helpers/esp32/WiFiRadioPolicy.h>
#include <helpers/esp32/WiFiStationPolicy.h>
#include <Preferences.h>
@@ -946,6 +947,47 @@ void halt() {
static DisplayDriver* companion_setup_display = nullptr;
static unsigned long companion_setup_display_refresh = 0;
static bool hasLargeCompanionSetupDisplay() {
return companion_setup_display->width() >= 128
&& companion_setup_display->height() >= 128;
}
static int drawCompanionSetupValue(int y, int max_lines,
const char* value) {
static constexpr int margin = 6;
static constexpr int line_height = 13;
return mesh::ui::drawTextWrapped(
*companion_setup_display, margin, y,
companion_setup_display->width() - margin * 2, line_height,
max_lines, value != nullptr && value[0] != 0 ? value : "(not set)");
}
static void renderLargeCompanionSetupDisplay(const char* title,
const char* wifi_label,
const char* wifi_name,
const char* address,
bool connecting) {
static constexpr int line_height = 13;
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 4, title);
companion_setup_display->setCursor(6, 24);
companion_setup_display->print(wifi_label);
const int wifi_lines = drawCompanionSetupValue(38, 3, wifi_name);
int next_y = 38 + (wifi_lines > 0 ? wifi_lines : 1) * line_height + 8;
if (connecting) {
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, next_y, "Please wait...");
return;
}
companion_setup_display->setCursor(6, next_y);
companion_setup_display->print("Open in browser:");
char url[32];
snprintf(url, sizeof(url), "http://%s/", address ? address : "");
drawCompanionSetupValue(next_y + 14, 2, url);
}
static void renderCompanionSetupDisplay() {
if (!companion_setup_display
|| static_cast<int32_t>(millis() - companion_setup_display_refresh) < 0) return;
@@ -960,37 +1002,54 @@ void halt() {
char setup_ip[16] = {0};
if (WebConfigServer::getSetupInfo(setup_ssid, sizeof(setup_ssid),
setup_ip, sizeof(setup_ip))) {
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 0, "WebUI setup");
companion_setup_display->setCursor(0, 14);
companion_setup_display->print("Join open WiFi:");
companion_setup_display->drawTextEllipsized(
0, 25, companion_setup_display->width(), setup_ssid);
companion_setup_display->setCursor(0, 39);
companion_setup_display->print("Open in browser:");
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 51, setup_ip);
if (hasLargeCompanionSetupDisplay()) {
renderLargeCompanionSetupDisplay(
"WebUI setup", "Join open WiFi:", setup_ssid, setup_ip, false);
} else {
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 0, "WebUI setup");
companion_setup_display->setCursor(0, 14);
companion_setup_display->print("Join open WiFi:");
companion_setup_display->drawTextEllipsized(
0, 25, companion_setup_display->width(), setup_ssid);
companion_setup_display->setCursor(0, 39);
companion_setup_display->print("Open in browser:");
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 51, setup_ip);
}
} else if (WiFi.status() == WL_CONNECTED) {
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 0, "WebUI");
companion_setup_display->setCursor(0, 14);
companion_setup_display->print("Join WiFi:");
companion_setup_display->drawTextEllipsized(
0, 25, companion_setup_display->width(), configured_wifi_ssid);
companion_setup_display->setCursor(0, 39);
companion_setup_display->print("Open in browser:");
const String ip = WiFi.localIP().toString();
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 51, ip.c_str());
if (hasLargeCompanionSetupDisplay()) {
renderLargeCompanionSetupDisplay(
"WebUI ready", "Join WiFi:", configured_wifi_ssid, ip.c_str(),
false);
} else {
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 0, "WebUI");
companion_setup_display->setCursor(0, 14);
companion_setup_display->print("Join WiFi:");
companion_setup_display->drawTextEllipsized(
0, 25, companion_setup_display->width(), configured_wifi_ssid);
companion_setup_display->setCursor(0, 39);
companion_setup_display->print("Open in browser:");
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 51, ip.c_str());
}
} else {
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 8, "WiFi connecting");
companion_setup_display->setCursor(0, 25);
companion_setup_display->print("SSID:");
companion_setup_display->drawTextEllipsized(
0, 38, companion_setup_display->width(), configured_wifi_ssid);
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 52, "Please wait...");
if (hasLargeCompanionSetupDisplay()) {
renderLargeCompanionSetupDisplay(
"WiFi connecting", "WiFi name:", configured_wifi_ssid, nullptr,
true);
} else {
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 8, "WiFi connecting");
companion_setup_display->setCursor(0, 25);
companion_setup_display->print("SSID:");
companion_setup_display->drawTextEllipsized(
0, 38, companion_setup_display->width(), configured_wifi_ssid);
companion_setup_display->drawTextCentered(
companion_setup_display->width() / 2, 52, "Please wait...");
}
}
companion_setup_display->endFrame();
}
@@ -1698,6 +1757,13 @@ void loop() {
#endif
sensors.loop();
#ifdef DISPLAY_CLASS
#ifdef INDICATOR_WIFI_FONT_RECOVERY
// The Indicator keeps rendering with its built-in fallback while a missing
// SD font is recovered by a background TLS task. This poll only launches
// work after station Wi-Fi connects and installs a completed font on the
// main/UI task.
display.serviceFontRecovery();
#endif
#if defined(ESP32) && defined(WIFI_SSID) && defined(WITH_WEBCONFIG)
if (isCompanionWiFiEnabled() && (the_mesh.isWebConfigSetupActive()
#ifdef WITH_MQTT_BRIDGE
+311 -67
View File
@@ -5,8 +5,11 @@
#include <stdlib.h> // for qsort()
#include <helpers/CLICommandUtils.h>
#include <helpers/ClockSyncUtils.h>
#include <helpers/ClientLoginPersistence.h>
#include <helpers/ClientPathPersistence.h>
#include <helpers/DatagramPayloadLimits.h>
#include <helpers/FloodFilterPolicy.h>
#include <helpers/LazyPersistence.h>
#include <helpers/RegionNameUtils.h>
#if MESH_PACKET_LOGGING
#include <helpers/SerialPacketLog.h>
@@ -525,18 +528,23 @@ void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float sn
}
uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood) {
ClientInfo* client = NULL;
if (strcmp((char *)data, _prefs.password) != 0) { // admin pw bypasses ACL (allows upgrade)
client = acl.getClient(sender.pub_key, PUB_KEY_SIZE);
if (client == NULL) {
ClientInfo* existing_client =
acl.getClient(sender.pub_key, PUB_KEY_SIZE);
const bool admin_password =
strcmp((char *)data, _prefs.password) == 0;
ClientInfo* client = admin_password ? NULL : existing_client;
uint8_t perms;
if (client != NULL) {
// A preauthorized ACL identity is authenticated by the anonymous packet's
// ECDH MAC and does not need to repeat a password. Preserve its role.
perms = client->permissions & PERM_ACL_ROLE_MASK;
} else {
if (!admin_password && existing_client == NULL) {
#if MESH_DEBUG
MESH_DEBUG_PRINTLN("Login, sender not in ACL");
#endif
}
}
if (client == NULL) {
uint8_t perms;
if (strcmp((char *)data, _prefs.password) == 0) { // check for valid admin password
if (admin_password) { // valid admin password bypasses ACL (allows upgrade)
perms = PERM_ACL_ADMIN;
} else if (strcmp((char *)data, _prefs.guest_password) == 0) { // check guest password
perms = PERM_ACL_GUEST;
@@ -546,31 +554,46 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr
#endif
return 0;
}
}
const bool client_existed = existing_client != NULL;
const uint32_t previous_timestamp =
client_existed ? existing_client->last_timestamp : 0;
if (!acl.authorizeLoginTimestamp(
sender.pub_key, sender_timestamp, previous_timestamp, perms)) {
MESH_DEBUG_PRINTLN(
"Login rejected: replayed timestamp or replay state unavailable");
return 0;
}
if (client == NULL) {
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
}
MESH_DEBUG_PRINTLN("Login success!");
client->last_timestamp = sender_timestamp;
client->last_activity = getRTCClock()->getCurrentTime();
client->permissions &= ~PERM_ACL_ROLE_MASK;
client->permissions |= perms;
memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
if (perms != PERM_ACL_GUEST) { // keep number of FS writes to a minimum
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
}
}
if (is_flood) {
client->out_path_len = OUT_PATH_UNKNOWN; // need to rediscover out_path
MESH_DEBUG_PRINTLN("Login success!");
const uint8_t previous_permissions = client->permissions;
const bool reset_out_path = is_flood
&& client->out_path_len != OUT_PATH_FORCE_FLOOD;
const bool persisted_changed = mesh::applySuccessfulClientLogin(
*client, client_existed, perms, PERM_ACL_ROLE_MASK,
secret, sender_timestamp,
getRTCClock()->getCurrentTime(), reset_out_path, OUT_PATH_UNKNOWN);
const bool persistence_needed =
mesh::successfulClientLoginNeedsPersistence(
client_existed, previous_permissions, client->permissions,
PERM_ACL_ROLE_MASK, PERM_ACL_GUEST,
persisted_changed);
if (persistence_needed) {
// Keep writes bounded: replay/activity timestamps are transient, so an
// unchanged preauthorized/admin login does not rewrite the complete ACL.
mesh::scheduleLazyPersistenceMutation(
dirty_contacts_expiry, contacts_save_failures,
futureMillis(LAZY_CONTACTS_WRITE_DELAY));
}
uint32_t now = getRTCClock()->getCurrentTimeUnique();
@@ -671,9 +694,9 @@ uint8_t MyMesh::handleAnonClockReq(const mesh::Identity& sender, uint32_t sender
memcpy(&reply_data[4], &now, 4); // include our clock (for easy clock sync, and packet hash uniqueness)
reply_data[8] = 0; // features
#ifdef WITH_RS232_BRIDGE
if (_prefs.bridge_enabled) reply_data[8] |= 0x01; // is bridge, type UART
if (isBridgeRunning()) reply_data[8] |= 0x01; // is bridge, type UART
#elif WITH_ESPNOW_BRIDGE
reply_data[8] |= 0x03; // is bridge, type ESP-NOW
if (isBridgeRunning()) reply_data[8] |= 0x03; // is bridge, type ESP-NOW
#endif
if (_prefs.disable_fwd) { // is this repeater currently disabled
reply_data[8] |= 0x80; // is disabled
@@ -933,23 +956,30 @@ void MyMesh::sendClientReply(ClientInfo* client, mesh::Packet* packet, unsigned
fallback_scope_ptr);
}
void MyMesh::sendClientReplyWithFallbackScope(ClientInfo* client, mesh::Packet* packet,
bool MyMesh::sendClientReplyWithFallbackScope(ClientInfo* client, mesh::Packet* packet,
unsigned long delay_millis, uint8_t path_hash_size,
const TransportKey* fallback_scope) {
const TransportKey* fallback_scope,
bool allow_redundant_copies) {
if (packet == NULL) {
return;
return false;
}
if (client == NULL || !mesh::Packet::isValidPathLen(client->out_path_len)) {
const uint8_t flood_retry_attempts = _prefs.flood_retry_attempts;
if (!allow_redundant_copies) _prefs.flood_retry_attempts = 0;
bool queued;
if (fallback_scope != NULL) {
sendFloodScoped(*fallback_scope, packet, delay_millis, path_hash_size);
queued = sendFloodScoped(*fallback_scope, packet, delay_millis,
path_hash_size);
} else {
sendFlood(packet, delay_millis, path_hash_size);
queued = sendFlood(packet, delay_millis, path_hash_size);
}
return;
_prefs.flood_retry_attempts = flood_retry_attempts;
return queued;
}
mesh::Packet* alt = NULL;
if (mesh::Packet::isValidPathLen(client->alt_path_len)
if (allow_redundant_copies
&& mesh::Packet::isValidPathLen(client->alt_path_len)
&& !directPathsEqual(client->out_path, client->out_path_len, client->alt_path, client->alt_path_len)) {
alt = obtainNewPacket();
if (alt != NULL) {
@@ -959,13 +989,17 @@ void MyMesh::sendClientReplyWithFallbackScope(ClientInfo* client, mesh::Packet*
}
}
sendDirect(packet, client->out_path, client->out_path_len, delay_millis);
const uint8_t direct_retry_enabled = _prefs.direct_retry_enabled;
if (!allow_redundant_copies) _prefs.direct_retry_enabled = 0;
const bool primary_queued =
sendDirect(packet, client->out_path, client->out_path_len, delay_millis);
_prefs.direct_retry_enabled = direct_retry_enabled;
if (alt != NULL) {
uint8_t direct_retry_enabled = _prefs.direct_retry_enabled;
_prefs.direct_retry_enabled = 0;
sendDirect(alt, client->alt_path, client->alt_path_len, delay_millis);
_prefs.direct_retry_enabled = direct_retry_enabled;
}
return primary_queued;
}
bool MyMesh::floodChannelDataHopApplies(const mesh::Packet* packet) const {
@@ -1088,8 +1122,10 @@ void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
if (mqtt_bridge) mqtt_bridge->onPacketReceived(pkt);
#elif defined(WITH_BRIDGE)
// Non-MQTT bridge: use bridge.source setting
if (_prefs.bridge_pkt_src == 1) {
activeBridge()->sendPacket(pkt);
AbstractBridge* active_bridge = activeBridge();
if (_prefs.bridge_pkt_src == 1 && active_bridge
&& active_bridge->isRunning()) {
active_bridge->sendPacket(pkt);
}
#endif
@@ -1131,8 +1167,10 @@ void MyMesh::logTx(mesh::Packet *pkt, int len) {
if (mqtt_bridge) mqtt_bridge->sendPacket(pkt);
#elif defined(WITH_BRIDGE)
// Non-MQTT bridge: use bridge.source setting
if (_prefs.bridge_pkt_src == 0) {
activeBridge()->sendPacket(pkt);
AbstractBridge* active_bridge = activeBridge();
if (_prefs.bridge_pkt_src == 0 && active_bridge
&& active_bridge->isRunning()) {
active_bridge->sendPacket(pkt);
}
#endif
@@ -2503,6 +2541,10 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
uint8_t *data, size_t len) {
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin
// client (unknown at this stage)
if (len < 4) {
MESH_DEBUG_PRINTLN("Rejected incomplete anonymous request");
return;
}
uint32_t timestamp;
memcpy(&timestamp, data, 4);
@@ -2750,10 +2792,22 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
&& region_map.getTransportKeysFor(*recv_pkt_region, &reply_scope, 1) > 0;
if (cached_retry) {
MESH_DEBUG_PRINTLN("onPeerDataRecv: replaying cached remote CLI reply");
sendRemoteCliReply(client, secret, packet->getPathHashSize(),
sender_timestamp, cached_response,
reply_scoped ? &reply_scope : NULL);
const bool cached_temp_radio_success =
strncmp(command, "tempradio ", 10) == 0
&& strncmp(cached_response, "OK - temp params for ", 21) == 0;
if (cached_temp_radio_success) {
// The original acknowledgement is the only packet allowed to
// release its radio-mutation barrier. Replaying this success would
// create another untracked copy, including after a TX failure
// cancelled the handoff.
MESH_DEBUG_PRINTLN(
"onPeerDataRecv: TempRadio acknowledgement is already authoritative");
} else {
MESH_DEBUG_PRINTLN("onPeerDataRecv: replaying cached remote CLI reply");
sendRemoteCliReply(client, secret, packet->getPathHashSize(),
sender_timestamp, cached_response,
reply_scoped ? &reply_scope : NULL);
}
} else if (deferred_cli_command.matches(i, request_id, command,
command_len)) {
// The original request is already queued. Let it produce the one
@@ -2787,10 +2841,12 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx,
}
}
void MyMesh::sendRemoteCliReply(ClientInfo* client, const uint8_t* secret,
bool MyMesh::sendRemoteCliReply(ClientInfo* client, const uint8_t* secret,
uint8_t path_hash_size, uint32_t sender_timestamp,
const char* reply, const TransportKey* fallback_scope) {
if (client == NULL || secret == NULL || reply == NULL) return;
const char* reply, const TransportKey* fallback_scope,
mesh::Packet** queued_packet) {
if (queued_packet != NULL) *queued_packet = NULL;
if (client == NULL || secret == NULL || reply == NULL) return false;
if (reply[0] == 0) reply = "OK";
size_t text_len = strlen(reply);
@@ -2811,8 +2867,17 @@ void MyMesh::sendRemoteCliReply(ClientInfo* client, const uint8_t* secret,
mesh::Packet* packet = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, secret,
reply_data, 5 + text_len);
sendClientReplyWithFallbackScope(client, packet, CLI_REPLY_DELAY_MILLIS,
path_hash_size, fallback_scope);
// A caller which requests the exact queued packet is going to make a state
// transition depend on that packet's TX result. Keep one authoritative
// copy: an untracked alternate or retry could report success after primary
// failure cancelled the transition, or still be queued when primary success
// lets the radio tuple change. Ordinary replies retain redundant delivery.
const bool allow_redundant_copies = queued_packet == NULL;
const bool queued = sendClientReplyWithFallbackScope(
client, packet, CLI_REPLY_DELAY_MILLIS, path_hash_size, fallback_scope,
allow_redundant_copies);
if (queued && queued_packet != NULL) *queued_packet = packet;
return queued;
}
#if defined(ESP32_PLATFORM) || defined(USER_GPIO_CONTROL)
@@ -3059,13 +3124,31 @@ void __attribute__((noinline)) MyMesh::processDeferredCliCommand() {
mesh::RemoteCliReplyCache::fingerprint(
deferred_cli_command.command,
strlen(deferred_cli_command.command));
const bool arms_temp_radio =
strncmp(deferred_cli_command.command, "tempradio ", 10) == 0
&& strncmp(reply, "OK - temp params for ", 21) == 0;
remote_cli_reply_cache.remember(client->id.pub_key,
deferred_cli_command.request_id,
command_fingerprint, reply);
sendRemoteCliReply(client, deferred_cli_command.secret,
deferred_cli_command.path_hash_size,
deferred_cli_command.sender_timestamp, reply,
deferred_cli_reply_scoped ? &deferred_cli_reply_scope : NULL);
mesh::Packet* queued_reply = NULL;
const bool reply_queued = sendRemoteCliReply(
client, deferred_cli_command.secret,
deferred_cli_command.path_hash_size,
deferred_cli_command.sender_timestamp, reply,
deferred_cli_reply_scoped ? &deferred_cli_reply_scope : NULL,
arms_temp_radio ? &queued_reply : NULL);
if (arms_temp_radio) {
if (reply_queued && queued_reply != NULL) {
// The old two-RTC-second delay could expire while this exact packet was
// still parked behind CAD/duty/queue work. Do not change modulation
// until Dispatcher confirms that the reply physically transmitted.
temp_radio_reply_barrier.arm(queued_reply);
} else {
// A command which cannot queue its acknowledgement must never strand
// the administrator on an unconfirmed tuple.
scheduleNormalRadio();
}
}
clearDeferredCliCommand();
}
@@ -3078,10 +3161,16 @@ bool MyMesh::onPeerPathRecv(mesh::Packet *packet, int sender_idx, const uint8_t
MESH_DEBUG_PRINTLN("PATH to client, path_len=%d", (uint32_t)path_len);
auto client = acl.getClientByIdx(i);
// store a copy of path, for sendDirect()
if (client->out_path_len != OUT_PATH_FORCE_FLOOD) {
client->out_path_len = mesh::Packet::copyPath(client->out_path, path, path_len);
}
// PATH packets authenticate the sender and are de-duplicated in RAM, but
// the protocol carries no timestamp or request nonce. Keep a learned route
// useful for this boot without making an arbitrarily old replay durable.
const bool persistence_allowed = mesh::clientPathPersistenceAllowed(
client->permissions != 0, false /* no durable replay proof */);
const mesh::ClientPathUpdateResult path_update =
mesh::applyReceivedClientPath(
*client, path, path_len, persistence_allowed,
OUT_PATH_FORCE_FLOOD);
(void)path_update;
client->last_activity = getRTCClock()->getCurrentTime();
} else {
MESH_DEBUG_PRINTLN("onPeerPathRecv: invalid peer idx: %d", i);
@@ -3207,6 +3296,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
pending_battery_alert_packet = NULL;
battery_alert_sent = false;
dirty_contacts_expiry = 0;
contacts_save_failures = 0;
active_bw = 0.0f;
active_sf = 0;
active_cr = 0;
@@ -3501,7 +3591,17 @@ void MyMesh::begin(FILESYSTEM *fs) {
if (!bridge) {
bridge = createRS232Bridge();
}
#endif
if (!bridge || !beginRS232Bridge()) {
// Keep configured intent for a later explicit retry, but clean any
// partial object/GPS ownership. Advertisements and bridge.running report
// the actual stopped state rather than the saved preference.
MESH_DEBUG_PRINTLN("RS232 bridge configured on but failed to start");
if (!setBridgeState(false)) {
MESH_DEBUG_PRINTLN(
"RS232 bridge cleanup failed; UART/GPS ownership remains tracked");
}
}
#else
AbstractBridge* active_bridge = activeBridge();
if (active_bridge) {
#ifdef WITH_MQTT_BRIDGE
@@ -3534,6 +3634,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
active_bridge->begin();
}
#endif
}
#endif
@@ -3754,6 +3855,7 @@ bool MyMesh::sendRepeatersFloodText(const char* text, const TransportKey* scope,
void MyMesh::onSendComplete(mesh::Packet* packet) {
mesh::Mesh::onSendComplete(packet);
temp_radio_reply_barrier.complete(packet);
if (packet == pending_battery_alert_packet) {
pending_battery_alert_packet = NULL;
battery_alert_sent = true;
@@ -3763,6 +3865,12 @@ void MyMesh::onSendComplete(mesh::Packet* packet) {
void MyMesh::onSendFail(mesh::Packet* packet) {
mesh::Mesh::onSendFail(packet);
if (temp_radio_reply_barrier.fail(packet)) {
// Failure of the exact acknowledgement packet cancels the unconfirmed
// handoff. A later cached-command retry may replay the reply, but it does
// not re-run the mutation.
scheduleNormalRadio();
}
if (packet == pending_battery_alert_packet) {
pending_battery_alert_packet = NULL;
}
@@ -4075,6 +4183,7 @@ void MyMesh::clearScheduledRadioSetting(int idx, bool restore_if_started) {
&& scheduled_radio_settings[idx].started;
scheduled_radio_settings[idx].active = false;
scheduled_radio_settings[idx].started = false;
scheduled_radio_settings[idx].hard_end_uptime_millis = 0;
refreshScheduledRadioState();
if (scheduled_radio_settings[idx].temporary && temp_radio_handoff_pending
&& countScheduledRadioSettings(true) == 0) {
@@ -4197,6 +4306,12 @@ void MyMesh::addScheduledRadioParams(bool temporary, float freq, float bw, uint8
scheduled_radio_settings[slot].cr = cr;
scheduled_radio_settings[slot].start_time = start_time;
scheduled_radio_settings[slot].end_time = temporary ? end_time : 0;
const uint64_t current_uptime_millis =
uptime_millis + (uint32_t)(millis() - last_millis);
scheduled_radio_settings[slot].hard_end_uptime_millis = temporary
? mesh::TempRadioLeaseDeadline::fromEpochEnd(
current_uptime_millis, now, end_time)
: 0;
// A newly requested schedule must not inherit the backoff of an older radio
// apply failure, especially when its deadline is sooner than that retry.
scheduled_radio_retry_at = 0;
@@ -4275,6 +4390,7 @@ void MyMesh::deleteScheduledRadioParams(bool temporary, const char* selector, ch
restore_radio = restore_radio || (setting.temporary && setting.started);
setting.active = false;
setting.started = false;
setting.hard_end_uptime_millis = 0;
deleted++;
}
}
@@ -4306,7 +4422,40 @@ void MyMesh::deleteScheduledRadioParams(bool temporary, const char* selector, ch
}
void MyMesh::processScheduledRadioSettings() {
if (scheduled_radio_retry_at && !millisHasNowPassed(scheduled_radio_retry_at)) return;
const uint64_t current_uptime_millis =
uptime_millis + (uint32_t)(millis() - last_millis);
bool hard_temp_end_due = false;
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
const ScheduledRadioSetting& setting = scheduled_radio_settings[i];
if (setting.active && setting.temporary
&& mesh::TempRadioLeaseDeadline::expired(
current_uptime_millis, setting.hard_end_uptime_millis)) {
hard_temp_end_due = true;
break;
}
}
if (temp_radio_reply_barrier.waiting() && !hard_temp_end_due) {
// The exact parameterized-TempRadio reply is queued or on air. Unlike a
// fixed RTC delay, this remains correct under CAD, duty throttling, and
// unrelated queue pressure. TX completion/failure releases the barrier.
scheduled_radio_retry_at = futureMillis(RADIO_APPLY_RETRY_INTERVAL_MILLIS);
return;
}
if (hard_temp_end_due) {
// Expiry is stronger than reply delivery. This matters when a new
// immediate TempRadio command arrives while an older temporary tuple is
// already active: a permanently queued acknowledgement must not preserve
// that old tuple beyond the newly accepted hard lease. The late reply can
// still leave on the restored channel, but it can no longer cause a switch.
temp_radio_reply_barrier.clear();
}
// A radio-apply backoff must not turn into a lease extension. Bypass it
// only for an expired temporary window; ordinary starts and permanent
// changes retain their existing bounded retry behavior.
if (scheduled_radio_retry_at
&& !millisHasNowPassed(scheduled_radio_retry_at)
&& !hard_temp_end_due) return;
const bool schedule_check_due = next_scheduled_radio_time != 0
&& (next_scheduled_radio_check_at == 0
@@ -4320,7 +4469,7 @@ void MyMesh::processScheduledRadioSettings() {
}
bool saved_apply_due = saved_radio_apply_pending && !temp_radio_handoff_pending
&& !scheduled_temp_radio_started;
if (!schedule_due && !saved_apply_due) return;
if (!schedule_due && !saved_apply_due && !hard_temp_end_due) return;
// Never touch modulation registers while a packet is still on air. Back off
// this check too; a long packet should not make the scheduler poll every loop.
@@ -4332,6 +4481,28 @@ void MyMesh::processScheduledRadioSettings() {
bool apply_failed = false;
bool saved_params_changed = false;
if (hard_temp_end_due) {
// This applies to pending as well as active windows. A backward wall-clock
// correction can therefore neither postpone activation into the future nor
// keep an already-active node on the temporary tuple past the accepted
// monotonic lease bound.
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
ScheduledRadioSetting& setting = scheduled_radio_settings[i];
if (setting.active && setting.temporary
&& mesh::TempRadioLeaseDeadline::expired(
current_uptime_millis, setting.hard_end_uptime_millis)) {
const bool was_started = setting.started;
setting.active = false;
setting.started = false;
setting.hard_end_uptime_millis = 0;
if (was_started || temp_radio_handoff_pending) {
temp_radio_handoff_pending = false;
queueSavedRadioApply();
}
}
}
}
while (schedule_due) {
int due_idx = -1;
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
@@ -4374,6 +4545,7 @@ void MyMesh::processScheduledRadioSettings() {
if (setting.active && setting.temporary && setting.started && now >= setting.end_time) {
setting.active = false;
setting.started = false;
setting.hard_end_uptime_millis = 0;
queueSavedRadioApply();
}
}
@@ -4383,6 +4555,7 @@ void MyMesh::processScheduledRadioSettings() {
if (setting.active && setting.temporary && !setting.started && now >= setting.start_time) {
if (now >= setting.end_time) {
setting.active = false;
setting.hard_end_uptime_millis = 0;
if (temp_radio_handoff_pending) {
temp_radio_handoff_pending = false;
queueSavedRadioApply();
@@ -4431,6 +4604,18 @@ bool MyMesh::isMillisTimerDue(unsigned long timestamp) const {
}
bool MyMesh::hasScheduledRadioWorkDue() const {
const uint64_t current_uptime_millis =
uptime_millis + (uint32_t)(millis() - last_millis);
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
const ScheduledRadioSetting& setting = scheduled_radio_settings[i];
if (setting.active && setting.temporary
&& mesh::TempRadioLeaseDeadline::expired(
current_uptime_millis, setting.hard_end_uptime_millis)) return true;
}
// The monotonic lease end above is authoritative even while an ordinary
// radio-apply retry is backed off. Checking the retry first could let the
// power manager sleep through the hard end and extend TempRadio by up to a
// complete retry interval.
if (scheduled_radio_retry_at && !millisHasNowPassed(scheduled_radio_retry_at)) return false;
if (saved_radio_apply_pending && !temp_radio_handoff_pending
&& !scheduled_temp_radio_started) return true;
@@ -4453,10 +4638,21 @@ uint32_t MyMesh::limitSleepToMillisTimer(unsigned long timestamp, uint32_t sleep
}
uint32_t MyMesh::limitSleepToScheduledRadioWork(uint32_t sleep_secs) const {
if (scheduled_radio_retry_at && !millisHasNowPassed(scheduled_radio_retry_at)) {
return limitSleepToMillisTimer(scheduled_radio_retry_at, sleep_secs);
sleep_secs = limitSleepToMillisTimer(next_scheduled_radio_check_at, sleep_secs);
const uint64_t current_uptime_millis =
uptime_millis + (uint32_t)(millis() - last_millis);
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
const ScheduledRadioSetting& setting = scheduled_radio_settings[i];
if (!setting.active || !setting.temporary) continue;
const uint32_t hard_end_seconds =
mesh::TempRadioLeaseDeadline::secondsUntil(
current_uptime_millis, setting.hard_end_uptime_millis);
if (hard_end_seconds < sleep_secs) sleep_secs = hard_end_seconds;
}
return limitSleepToMillisTimer(next_scheduled_radio_check_at, sleep_secs);
if (scheduled_radio_retry_at && !millisHasNowPassed(scheduled_radio_retry_at)) {
sleep_secs = limitSleepToMillisTimer(scheduled_radio_retry_at, sleep_secs);
}
return sleep_secs;
}
uint32_t MyMesh::getPowerSaveSleepSeconds(uint32_t max_secs) const {
@@ -4487,6 +4683,10 @@ uint32_t MyMesh::getPowerSaveSleepSeconds(uint32_t max_secs) const {
}
void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) {
// A newer TempRadio command supersedes the reply barrier belonging to the
// old schedule. processDeferredCliCommand() arms the new exact reply after
// it has been composed and successfully queued.
temp_radio_reply_barrier.clear();
scheduled_radio_retry_at = 0;
scheduled_radio_retry_failures = 0;
bool cancelled_started_temp = false;
@@ -4495,6 +4695,7 @@ void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr,
cancelled_started_temp = cancelled_started_temp || scheduled_radio_settings[i].started;
scheduled_radio_settings[i].active = false;
scheduled_radio_settings[i].started = false;
scheduled_radio_settings[i].hard_end_uptime_millis = 0;
}
}
if (cancelled_started_temp) {
@@ -4524,6 +4725,12 @@ void MyMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr,
scheduled_radio_settings[slot].cr = cr;
scheduled_radio_settings[slot].start_time = start_time;
scheduled_radio_settings[slot].end_time = start_time + ((uint32_t)timeout_mins * 60);
const uint64_t current_uptime_millis =
uptime_millis + (uint32_t)(millis() - last_millis);
scheduled_radio_settings[slot].hard_end_uptime_millis =
mesh::TempRadioLeaseDeadline::fromEpochEnd(
current_uptime_millis, getRTCClock()->getCurrentTime(),
scheduled_radio_settings[slot].end_time);
refreshScheduledRadioState();
}
@@ -4531,10 +4738,12 @@ bool MyMesh::scheduleNormalRadio() {
// Cancel every pending/active temporary entry, but leave permanent radioat
// changes intact. The saved apply waits for the CLI reply to leave the
// outbound queue before changing modulation parameters.
temp_radio_reply_barrier.clear();
for (int i = 0; i < MAX_SCHEDULED_RADIO_SETTINGS; i++) {
if (!scheduled_radio_settings[i].temporary) continue;
scheduled_radio_settings[i].active = false;
scheduled_radio_settings[i].started = false;
scheduled_radio_settings[i].hard_end_uptime_millis = 0;
}
temp_radio_handoff_pending = false;
refreshScheduledRadioState();
@@ -10435,13 +10644,33 @@ bool MyMesh::handleClientPathCommand(ClientInfo* sender, char* command, char* re
return true;
}
const uint8_t previous_path_len = *stored_path_len;
uint8_t previous_path[MAX_PATH_SIZE];
memcpy(previous_path, stored_path, sizeof(previous_path));
const bool previous_out_path_persistable =
sender->out_path_is_persistable;
if (path_len == OUT_PATH_UNKNOWN || path_len == OUT_PATH_FORCE_FLOOD) {
memset(stored_path, 0, MAX_PATH_SIZE);
*stored_path_len = path_len;
} else {
*stored_path_len = mesh::Packet::copyPath(stored_path, path, path_len);
}
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
if (!is_alt) sender->out_path_is_persistable = true;
// Explicit operator routes are rare and must become durable before the CLI
// acknowledges them. Otherwise a replay-unproven PATH arriving during the
// lazy-write window could replace the only RAM copy. ClientACL::save also
// preserves durable routes for any *other* client currently using a newer
// transient route.
if (!acl.save(_fs)) {
*stored_path_len = previous_path_len;
memcpy(stored_path, previous_path, sizeof(previous_path));
sender->out_path_is_persistable = previous_out_path_persistable;
strcpy(reply, "Err - path save failed");
return true;
}
mesh::resetLazyPersistenceAfterSuccess(
dirty_contacts_expiry, contacts_save_failures);
formatPathReply(stored_path, *stored_path_len, reply, 160);
return true;
}
@@ -11173,7 +11402,9 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, ClientInfo* sender, char *
&& mesh::Utils::fromHex(pubkey, (int)(hex_len / 2), hex)) {
uint8_t perms = atoi(sp);
if (acl.applyPermissions(self_id, pubkey, (int)(hex_len / 2), perms)) {
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger acl.save()
mesh::scheduleLazyPersistenceMutation(
dirty_contacts_expiry, contacts_save_failures,
futureMillis(LAZY_CONTACTS_WRITE_DELAY));
strcpy(reply, "OK");
} else {
strcpy(reply, "Err - invalid params");
@@ -11751,8 +11982,21 @@ void __attribute__((noinline)) MyMesh::servicePostMeshLoop() {
// is pending dirty contacts write needed?
if (dirty_contacts_expiry && millisHasNowPassed(dirty_contacts_expiry)) {
acl.save(_fs);
dirty_contacts_expiry = 0;
const bool saved = acl.save(_fs);
if (saved) {
mesh::resetLazyPersistenceAfterSuccess(
dirty_contacts_expiry, contacts_save_failures);
} else {
const uint32_t retry_delay =
mesh::recordLazyPersistenceSaveFailure(
contacts_save_failures, LAZY_CONTACTS_WRITE_DELAY,
mesh::LAZY_PERSISTENCE_MAX_RETRY_DELAY_MILLIS);
mesh::completeLazyPersistenceSave(
dirty_contacts_expiry, false, futureMillis(retry_delay));
MESH_DEBUG_PRINTLN(
"ERROR: contacts save failed; retry in %lu ms",
(unsigned long)retry_delay);
}
}
// update uptime
+109 -19
View File
@@ -95,6 +95,8 @@
#endif
#include <helpers/RemoteCliReplyCache.h>
#include <helpers/RemoteCliRequest.h>
#include <helpers/TempRadioReplyBarrier.h>
#include <helpers/TempRadioLeaseDeadline.h>
#if defined(ESP32_PLATFORM) || defined(USER_GPIO_CONTROL)
#include <helpers/UserGpioReplyTracker.h>
#endif
@@ -281,6 +283,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
uint8_t cr;
uint32_t start_time;
uint32_t end_time;
uint64_t hard_end_uptime_millis;
};
FILESYSTEM* _fs;
@@ -289,6 +292,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
unsigned long next_local_advert, next_flood_advert;
mesh::DeferredCliCommand deferred_cli_command;
mesh::RemoteCliReplyCache remote_cli_reply_cache;
mesh::TempRadioReplyBarrier temp_radio_reply_barrier;
TransportKey deferred_cli_reply_scope;
bool deferred_cli_reply_scoped;
#if MESH_ENABLE_HOST_CLI
@@ -462,6 +466,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
unsigned long pending_discover_until;
bool region_load_active;
unsigned long dirty_contacts_expiry;
uint8_t contacts_save_failures;
#if MAX_NEIGHBOURS
NeighbourInfo neighbours[MAX_NEIGHBOURS];
#endif
@@ -501,6 +506,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
MQTTBridge* mqtt_bridge;
#elif defined(WITH_RS232_BRIDGE)
RS232Bridge* bridge;
uint8_t active_rs232_bridge_uart = 0;
#elif defined(WITH_ESPNOW_BRIDGE)
ESPNowBridge bridge;
#endif
@@ -636,12 +642,14 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
#if MESH_ENABLE_HOST_CLI
bool completeHostCliRequest(const char* service_reply);
#endif
void sendRemoteCliReply(ClientInfo* client, const uint8_t* secret,
bool sendRemoteCliReply(ClientInfo* client, const uint8_t* secret,
uint8_t path_hash_size, uint32_t sender_timestamp,
const char* reply, const TransportKey* fallback_scope);
void sendClientReplyWithFallbackScope(ClientInfo* client, mesh::Packet* packet,
const char* reply, const TransportKey* fallback_scope,
mesh::Packet** queued_packet = NULL);
bool sendClientReplyWithFallbackScope(ClientInfo* client, mesh::Packet* packet,
unsigned long delay_millis, uint8_t path_hash_size,
const TransportKey* fallback_scope);
const TransportKey* fallback_scope,
bool allow_redundant_copies = true);
void servicePostMeshLoop();
#if MESH_ENABLE_TELEMETRY_HISTORY
void sampleTelemetryHistory();
@@ -1007,7 +1015,73 @@ public:
WITH_RS232_BRIDGE_RX, WITH_RS232_BRIDGE_TX,
_mgr, getRTCClock());
}
bool beginRS232Bridge() {
if (!bridge) return false;
const uint8_t selected_uart = _prefs.bridge_uart;
bool gps_uart_blocked = false;
#if ENV_INCLUDE_GPS == 1
if (sensors.gpsSerialTransportMayConflict(selected_uart)) {
if (!sensors.gpsUsesSerialUart(selected_uart)
|| !sensors.gpsSerialTransportCanYield(selected_uart)) {
return false;
}
if (!sensors.setGpsSerialTransportBlocked(selected_uart, true)) {
return false;
}
gps_uart_blocked = true;
// Record ownership as soon as the GPS yields. If bridge startup then
// fails and the first release attempt also fails, normal bridge cleanup
// still knows which UART must be released and can retry safely.
active_rs232_bridge_uart = selected_uart;
}
#endif
bridge->begin();
if (!bridge->isRunning()) {
#if ENV_INCLUDE_GPS == 1
if (gps_uart_blocked) {
if (sensors.setGpsSerialTransportBlocked(selected_uart, false)) {
active_rs232_bridge_uart = 0;
}
}
#else
(void)gps_uart_blocked;
#endif
return false;
}
active_rs232_bridge_uart = selected_uart;
return true;
}
bool endRS232Bridge() {
const uint8_t released_uart = active_rs232_bridge_uart;
bool stopped = true;
// A fail-closed preflight can reject this object before begin() touches
// its UART. Adafruit nRF Uart::end() is not safe as a generic cleanup for
// an unstarted peripheral: it can stop a GPS-owned Serial1 and wait on
// hardware events which will never arrive. End only a bridge which proved
// that it reached running state.
if (bridge && bridge->isRunning()) {
bridge->end();
stopped = !bridge->isRunning();
}
bool gps_released = true;
#if ENV_INCLUDE_GPS == 1
// Release the bridge first, then restore a UART GPS. I2C GPS providers do
// not claim any UART and therefore never enter this path.
if (released_uart != 0 && sensors.gpsUsesSerialUart(released_uart)) {
gps_released = sensors.setGpsSerialTransportBlocked(released_uart, false);
}
#endif
if (stopped && gps_released) active_rs232_bridge_uart = 0;
return stopped && gps_released;
}
#endif
bool isBridgeRunning() const override {
const AbstractBridge* active_bridge = activeBridge();
return active_bridge != nullptr && active_bridge->isRunning();
}
bool setBridgeState(bool enable) override {
// Disabling an already-absent heap-backed bridge is successful and must
@@ -1034,16 +1108,18 @@ public:
bridge = createRS232Bridge();
if (!bridge) return false;
}
if (!enable) {
const bool stopped = endRS232Bridge();
if (stopped) {
delete bridge;
bridge = nullptr;
}
return stopped;
}
#endif
AbstractBridge* active_bridge = activeBridge();
if (!active_bridge) return false;
if (enable == active_bridge->isRunning()) {
#ifdef WITH_RS232_BRIDGE
if (!enable) {
delete bridge;
bridge = nullptr;
}
#endif
return true;
}
if (enable)
@@ -1059,28 +1135,47 @@ public:
mqtt_bridge->setBuildDate(getBuildDate());
mqtt_bridge->setStatsSources(this, _radio, _cli.getBoard(), _ms);
#endif
#ifdef WITH_RS232_BRIDGE
const bool started = beginRS232Bridge();
#else
active_bridge->begin();
const bool started = active_bridge->isRunning();
#endif
#ifdef WITH_MQTT_BRIDGE
_alerter.setBridge(mqtt_bridge);
#endif
return started;
}
else
{
active_bridge->end();
const bool stopped = !active_bridge->isRunning();
#ifdef WITH_RS232_BRIDGE
const bool stopped = endRS232Bridge();
delete bridge;
bridge = nullptr;
#else
active_bridge->end();
const bool stopped = !active_bridge->isRunning();
#endif
#ifdef WITH_MQTT_BRIDGE
_alerter.setBridge(nullptr);
#endif
return stopped;
}
return active_bridge->isRunning();
}
bool restartBridge() override {
#ifdef WITH_RS232_BRIDGE
// RS-232 changes must be applied synchronously so the CLI can commit or
// roll back the selected pins/baud. This branch also reconstructs a bridge
// after a prior allocation/start failure and is not WebConfig-coalesced.
if (bridge && (bridge->isRunning() || active_rs232_bridge_uart != 0)
&& !endRS232Bridge()) {
return false;
}
delete bridge;
bridge = createRS232Bridge();
return bridge && beginRS232Bridge();
#else
AbstractBridge* active_bridge = activeBridge();
if (!active_bridge) return false;
#ifdef WITH_WEBCONFIG
@@ -1090,12 +1185,6 @@ public:
}
#endif
if (active_bridge->isRunning()) active_bridge->end();
#ifdef WITH_RS232_BRIDGE
delete bridge;
bridge = createRS232Bridge();
if (bridge) bridge->begin();
return bridge && bridge->isRunning();
#endif
#ifdef WITH_MQTT_BRIDGE
// Set device metadata before restarting bridge (same as in begin())
char device_id[65];
@@ -1109,6 +1198,7 @@ public:
#endif
active_bridge->begin();
return active_bridge->isRunning();
#endif
}
void restartBridgeSlot(int slot) override {
+77 -29
View File
@@ -1,6 +1,9 @@
#include "MyMesh.h"
#include <helpers/radiolib/RxBoostedGainDefaults.h>
#include <helpers/CLICommandUtils.h>
#include <helpers/ClientLoginPersistence.h>
#include <helpers/ClientPathPersistence.h>
#include <helpers/LazyPersistence.h>
#if MESH_PACKET_LOGGING
#include <helpers/SerialPacketLog.h>
#endif
@@ -642,23 +645,29 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
uint8_t *data, size_t len) {
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin
// client (unknown at this stage)
if (len < 8) {
MESH_DEBUG_PRINTLN("Rejected incomplete anonymous request");
return;
}
uint32_t sender_timestamp, sender_sync_since;
memcpy(&sender_timestamp, data, 4);
memcpy(&sender_sync_since, &data[4], 4); // sender's "sync messags SINCE x" timestamp
data[len] = 0; // ensure null terminator
ClientInfo* client = NULL;
ClientInfo* existing_client =
acl.getClient(sender.pub_key, PUB_KEY_SIZE);
ClientInfo* client = existing_client;
uint8_t perm;
if (data[8] == 0) { // blank password, just check if sender is in ACL
client = acl.getClient(sender.pub_key, PUB_KEY_SIZE);
if (client == NULL) {
#if MESH_DEBUG
MESH_DEBUG_PRINTLN("Login, sender not in ACL");
#endif
return;
}
}
if (client == NULL) {
uint8_t perm;
perm = client->permissions & PERM_ACL_ROLE_MASK;
} else {
if (strcmp((char *)&data[8], _prefs.password) == 0) { // check for valid admin password
perm = PERM_ACL_ADMIN;
} else {
@@ -671,33 +680,46 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
return; // no response. Client will timeout
}
}
}
const bool client_existed = existing_client != NULL;
const uint32_t previous_timestamp =
client_existed ? existing_client->last_timestamp : 0;
if (!acl.authorizeLoginTimestamp(
sender.pub_key, sender_timestamp, previous_timestamp, perm)) {
MESH_DEBUG_PRINTLN(
"Login rejected: replayed timestamp or replay state unavailable");
return;
}
if (client == NULL) {
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;
}
MESH_DEBUG_PRINTLN("Login success!");
client->last_timestamp = sender_timestamp;
client->extra.room.sync_since = sender_sync_since;
client->extra.room.pending_ack = 0;
client->extra.room.push_failures = 0;
client->last_activity = getRTCClock()->getCurrentTime();
client->permissions &= ~0x03;
client->permissions |= perm;
memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
}
if (packet->isRouteFlood()) {
client->out_path_len = OUT_PATH_UNKNOWN; // need to rediscover out_path
MESH_DEBUG_PRINTLN("Login success!");
const uint8_t previous_permissions = client->permissions;
const uint32_t previous_sync_since = client->extra.room.sync_since;
const bool reset_out_path = packet->isRouteFlood()
&& client->out_path_len != OUT_PATH_FORCE_FLOOD;
bool persisted_changed = mesh::applySuccessfulClientLogin(
*client, client_existed, perm, PERM_ACL_ROLE_MASK,
secret, sender_timestamp, getRTCClock()->getCurrentTime(),
reset_out_path, OUT_PATH_UNKNOWN);
client->extra.room.sync_since = sender_sync_since;
client->extra.room.pending_ack = 0;
client->extra.room.push_failures = 0;
persisted_changed = persisted_changed
|| previous_sync_since != sender_sync_since;
if (mesh::successfulClientLoginNeedsPersistence(
client_existed, previous_permissions, client->permissions,
PERM_ACL_ROLE_MASK, PERM_ACL_GUEST, persisted_changed)) {
mesh::scheduleLazyPersistenceMutation(
dirty_contacts_expiry, contacts_save_failures,
futureMillis(LAZY_CONTACTS_WRITE_DELAY));
}
uint32_t now = getRTCClock()->getCurrentTimeUnique();
@@ -705,7 +727,9 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
// TODO: maybe reply with count of messages waiting to be synced for THIS client?
reply_data[4] = RESP_SERVER_LOGIN_OK;
reply_data[5] = 0; // Legacy: was recommended keep-alive interval (secs / 16)
reply_data[6] = (client->isAdmin() ? 1 : (client->permissions == 0 ? 2 : 0));
reply_data[6] = (client->isAdmin() ? 1
: ((client->permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST
? 2 : 0));
// LEGACY: reply_data[7] = getUnsyncedCount(client);
reply_data[7] = client->permissions; // NEW
getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
@@ -1033,7 +1057,15 @@ bool MyMesh::onPeerPathRecv(mesh::Packet *packet, int sender_idx, const uint8_t
if (i >= 0 && i < acl.getNumClients()) { // get from our known_clients table (sender SHOULD already be known in this context)
MESH_DEBUG_PRINTLN("PATH to client, path_len=%d", (uint32_t)path_len);
auto client = acl.getClientByIdx(i);
client->out_path_len = mesh::Packet::copyPath(client->out_path, path, path_len); // store a copy of path, for sendDirect()
// PATH has no durable replay freshness signal. Keep it RAM-only and do not
// let a replay replace an operator-selected force-flood route.
const bool persistence_allowed = mesh::clientPathPersistenceAllowed(
MyMesh::saveFilter(client), false /* no durable replay proof */);
const mesh::ClientPathUpdateResult path_update =
mesh::applyReceivedClientPath(
*client, path, path_len, persistence_allowed,
OUT_PATH_FORCE_FLOOD);
(void)path_update;
client->last_activity = getRTCClock()->getCurrentTime();
} else {
MESH_DEBUG_PRINTLN("onPeerPathRecv: invalid peer idx: %d", i);
@@ -1175,6 +1207,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
uptime_millis = 0;
next_local_advert = next_flood_advert = 0;
dirty_contacts_expiry = 0;
contacts_save_failures = 0;
_logging = false;
region_load_active = false;
set_radio_at = revert_radio_at = 0;
@@ -2196,7 +2229,9 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply
&& mesh::Utils::fromHex(pubkey, (int)(hex_len / 2), hex)) {
uint8_t perms = atoi(sp);
if (acl.applyPermissions(self_id, pubkey, (int)(hex_len / 2), perms)) {
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger acl.save()
mesh::scheduleLazyPersistenceMutation(
dirty_contacts_expiry, contacts_save_failures,
futureMillis(LAZY_CONTACTS_WRITE_DELAY));
strcpy(reply, "OK");
} else {
strcpy(reply, "Err - invalid params");
@@ -2481,8 +2516,21 @@ void MyMesh::loop() {
// is pending dirty contacts write needed?
if (dirty_contacts_expiry && millisHasNowPassed(dirty_contacts_expiry)) {
acl.save(_fs, MyMesh::saveFilter);
dirty_contacts_expiry = 0;
const bool saved = acl.save(_fs, MyMesh::saveFilter);
if (saved) {
mesh::resetLazyPersistenceAfterSuccess(
dirty_contacts_expiry, contacts_save_failures);
} else {
const uint32_t retry_delay =
mesh::recordLazyPersistenceSaveFailure(
contacts_save_failures, LAZY_CONTACTS_WRITE_DELAY,
mesh::LAZY_PERSISTENCE_MAX_RETRY_DELAY_MILLIS);
mesh::completeLazyPersistenceSave(
dirty_contacts_expiry, false, futureMillis(retry_delay));
MESH_DEBUG_PRINTLN(
"ERROR: contacts save failed; retry in %lu ms",
(unsigned long)retry_delay);
}
}
// TODO: periodically check for OLD/inactive entries in known_clients[], and evict
+5
View File
@@ -183,6 +183,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks,
UserGpioReplyTracker _gpio_reply_tracker;
#endif
unsigned long dirty_contacts_expiry;
uint8_t contacts_save_failures;
uint8_t reply_data[MAX_PACKET_PAYLOAD];
unsigned long next_push;
uint16_t _num_posted, _num_post_pushes;
@@ -481,6 +482,10 @@ public:
void loop();
#if defined(WITH_BRIDGE)
bool isBridgeRunning() const override {
return bridge != nullptr && bridge->isRunning();
}
bool setBridgeState(bool enable) override {
// An absent MQTT bridge is already stopped. Do not allocate one solely to
// satisfy an idempotent disable request.
+69 -30
View File
@@ -1,5 +1,8 @@
#include "SensorMesh.h"
#include <helpers/CLICommandUtils.h>
#include <helpers/ClientLoginPersistence.h>
#include <helpers/ClientPathPersistence.h>
#include <helpers/LazyPersistence.h>
#include <helpers/radiolib/RXPowerSaving.h>
static uint32_t nextRadioApplyRetryDelay(uint8_t& failure_count) {
@@ -444,15 +447,17 @@ int SensorMesh::getAGCResetInterval() const {
}
uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood) {
ClientInfo* client;
ClientInfo* existing_client = acl.getClient(sender.pub_key, PUB_KEY_SIZE);
ClientInfo* client = existing_client;
uint8_t role_permissions;
if (data[0] == 0) { // blank password, just check if sender is in ACL
client = acl.getClient(sender.pub_key, PUB_KEY_SIZE);
if (client == NULL) {
#if MESH_DEBUG
MESH_DEBUG_PRINTLN("Login, sender not in ACL");
#endif
return 0;
}
role_permissions = client->permissions & PERM_ACL_ROLE_MASK;
} else {
if (strcmp((char *) data, _prefs.password) != 0) { // check for valid admin password
#if MESH_DEBUG
@@ -460,30 +465,45 @@ uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t*
#endif
return 0;
}
role_permissions = PERM_ACL_ADMIN;
}
client = acl.putClient(sender, PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO); // add to contacts (if not already known)
const bool client_existed = existing_client != NULL;
const uint32_t previous_timestamp =
client_existed ? existing_client->last_timestamp : 0;
if (!acl.authorizeLoginTimestamp(
sender.pub_key, sender_timestamp, previous_timestamp,
role_permissions)) {
MESH_DEBUG_PRINTLN(
"Login rejected: replayed timestamp or replay state unavailable");
return 0;
}
if (client == NULL) {
client = acl.putClient(
sender, PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO);
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
}
MESH_DEBUG_PRINTLN("Login success!");
client->last_timestamp = sender_timestamp;
client->last_activity = getRTCClock()->getCurrentTime();
client->permissions |= PERM_ACL_ADMIN;
memcpy(client->shared_secret, secret, PUB_KEY_SIZE);
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
updateGpsTelemetryPolicy();
}
if (is_flood) {
client->out_path_len = OUT_PATH_UNKNOWN; // need to rediscover out_path
MESH_DEBUG_PRINTLN("Login success!");
const uint8_t previous_permissions = client->permissions;
const bool reset_out_path = is_flood
&& client->out_path_len != OUT_PATH_FORCE_FLOOD;
const bool persisted_changed = mesh::applySuccessfulClientLogin(
*client, client_existed, role_permissions, PERM_ACL_ROLE_MASK,
secret, sender_timestamp, getRTCClock()->getCurrentTime(),
reset_out_path, OUT_PATH_UNKNOWN);
if (mesh::successfulClientLoginNeedsPersistence(
client_existed, previous_permissions, client->permissions,
PERM_ACL_ROLE_MASK, PERM_ACL_GUEST, persisted_changed)) {
mesh::scheduleLazyPersistenceMutation(
dirty_contacts_expiry, contacts_save_failures,
futureMillis(LAZY_CONTACTS_WRITE_DELAY));
}
updateGpsTelemetryPolicy();
uint32_t now = getRTCClock()->getCurrentTimeUnique();
memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
@@ -590,7 +610,9 @@ void SensorMesh::handleCommand(uint32_t sender_timestamp, char* command, char* r
&& mesh::Utils::fromHex(pubkey, (int)(hex_len / 2), hex)) {
uint8_t perms = atoi(sp);
if (acl.applyPermissions(self_id, pubkey, (int)(hex_len / 2), perms)) {
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger acl.save()
mesh::scheduleLazyPersistenceMutation(
dirty_contacts_expiry, contacts_save_failures,
futureMillis(LAZY_CONTACTS_WRITE_DELAY));
updateGpsTelemetryPolicy();
strcpy(reply, "OK");
} else {
@@ -651,6 +673,10 @@ void SensorMesh::onGroupPacketRecv(mesh::Packet* packet) {
void SensorMesh::onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) {
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
if (len < 4) {
MESH_DEBUG_PRINTLN("Rejected incomplete anonymous request");
return;
}
uint32_t timestamp;
memcpy(&timestamp, data, 4);
@@ -871,17 +897,16 @@ bool SensorMesh::onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint
ClientInfo* from = acl.getClientByIdx(i);
MESH_DEBUG_PRINTLN("PATH to contact, path_len=%d", (uint32_t) path_len);
// NOTE: for this impl, we just replace the current 'out_path' regardless, whenever sender sends us a new out_path.
// FUTURE: could store multiple out_paths per contact, and try to find which is the 'best'(?)
from->out_path_len = mesh::Packet::copyPath(from->out_path, path, path_len); // store a copy of path, for sendDirect()
// PATH has an authenticated sender but no timestamp/request nonce. Keep the
// learned route in RAM only so an old replay cannot become durable.
const bool persistence_allowed = mesh::clientPathPersistenceAllowed(
from->permissions != 0, false /* no durable replay proof */);
const mesh::ClientPathUpdateResult path_update =
mesh::applyReceivedClientPath(
*from, path, path_len, persistence_allowed, OUT_PATH_FORCE_FLOOD);
(void)path_update;
from->last_activity = getRTCClock()->getCurrentTime();
// REVISIT: maybe make ALL out_paths non-persisted to minimise flash writes??
if (from->isAdmin()) {
// only do saveContacts() (of this out_path change) if this is an admin
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY);
}
// NOTE: no reciprocal path send!!
return false;
}
@@ -909,6 +934,7 @@ SensorMesh::SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::Millise
{
next_local_advert = next_flood_advert = 0;
dirty_contacts_expiry = 0;
contacts_save_failures = 0;
last_read_time = 0;
num_alert_tasks = 0;
set_radio_at = revert_radio_at = 0;
@@ -1298,8 +1324,21 @@ void SensorMesh::loop() {
// is there are pending dirty contacts write needed?
if (dirty_contacts_expiry && millisHasNowPassed(dirty_contacts_expiry)) {
acl.save(_fs);
dirty_contacts_expiry = 0;
const bool saved = acl.save(_fs);
if (saved) {
mesh::resetLazyPersistenceAfterSuccess(
dirty_contacts_expiry, contacts_save_failures);
} else {
const uint32_t retry_delay =
mesh::recordLazyPersistenceSaveFailure(
contacts_save_failures, LAZY_CONTACTS_WRITE_DELAY,
mesh::LAZY_PERSISTENCE_MAX_RETRY_DELAY_MILLIS);
mesh::completeLazyPersistenceSave(
dirty_contacts_expiry, false, futureMillis(retry_delay));
MESH_DEBUG_PRINTLN(
"ERROR: contacts save failed; retry in %lu ms",
(unsigned long)retry_delay);
}
}
}
+1
View File
@@ -195,6 +195,7 @@ private:
#endif
uint8_t reply_data[MAX_PACKET_PAYLOAD];
unsigned long dirty_contacts_expiry;
uint8_t contacts_save_failures;
CayenneLPP telemetry;
TransportKeyStore key_store;
RegionMap region_map;