From 1454e4cac12cf35860cab6f08c9d01585bebf10a Mon Sep 17 00:00:00 2001 From: agessaman Date: Thu, 16 Jul 2026 17:28:33 -0700 Subject: [PATCH] fix(mqtt): update neighbors interval range in documentation and code Adjusted the valid range for the `mqtt.neighbors.interval` setting from 12-8760 hours to 12-336 hours to reflect new constraints. Updated related documentation and CLI commands to ensure consistency across the codebase. This change improves clarity and prevents potential misconfigurations in neighbor publishing intervals. --- .gitignore | 3 +- MQTT_IMPLEMENTATION.md | 2 +- docs/cli_commands.md | 2 +- examples/simple_repeater/MyMesh.cpp | 54 ++++++++++++++++++++++++++--- examples/simple_repeater/MyMesh.h | 2 +- src/helpers/CommonCLI.cpp | 10 ++++++ src/helpers/CommonCLI.h | 12 ++++++- src/helpers/CommonCLI_Observer.cpp | 11 +++--- src/helpers/MQTTDefaults.h | 2 +- src/helpers/MQTTMessageBuilder.cpp | 14 +++++++- src/helpers/bridges/MQTTBridge.cpp | 7 ++-- src/helpers/bridges/MQTTBridge.h | 5 ++- 12 files changed, 103 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 29fa3674a..bee820484 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,5 @@ ssl_certs/cacert.pem platformio.local.ini .cursor/* .claude/* -.cursorrules \ No newline at end of file +.cursorrules +src/helpers/esp32/WebConfigHtml.h diff --git a/MQTT_IMPLEMENTATION.md b/MQTT_IMPLEMENTATION.md index cb1d0153f..f52fed702 100644 --- a/MQTT_IMPLEMENTATION.md +++ b/MQTT_IMPLEMENTATION.md @@ -416,7 +416,7 @@ These settings apply across all MQTT slots: - `off` - Disable TX packet uplinking - `set mqtt.interval ` - Set status publish interval (1-60 minutes) - `set mqtt.neighbors on|off` - Enable/disable periodic neighbors/scopes publish (PSRAM builds only) -- `set mqtt.neighbors.interval ` - Set neighbors publish interval (12-8760 hours, default 24) +- `set mqtt.neighbors.interval ` - Set neighbors publish interval (12-336 hours, default 24) - `set mqtt.ntp ` - Set custom NTP server (validated with immediate sync); `none` reverts to default - `set mqtt.owner <64-hex-char-public-key>` - Set owner public key - `set mqtt.email ` - Set owner email address diff --git a/docs/cli_commands.md b/docs/cli_commands.md index def7da172..c740ce1ff 100644 --- a/docs/cli_commands.md +++ b/docs/cli_commands.md @@ -1123,7 +1123,7 @@ Requires WiFi connected and the MQTT bridge running. - `set mqtt.neighbors.interval ` **Parameters:** -- `hours`: Interval between automatic `discover.scopes` runs (minimum **12**, default **24**) +- `hours`: Interval between automatic `discover.scopes` runs (**12-336**, default **24**) **Note:** Requires PSRAM. Publishes to `meshcore/{IATA}/{device}/neighbors` (retained, QoS 1). Use `discover.scopes` for a one-shot publish. diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 72810b796..1ca5adba7 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -670,13 +670,17 @@ int MyMesh::searchPeersByHash(const uint8_t *hash) { if (neighbor_discover_active) { for (int i = 0; i < neighbor_discover_count && n < MAX_CLIENTS; i++) { auto& nb = neighbours[neighbor_discover[i].neighbour_idx]; + // ACL clients already have a matching peer entry and shared secret. Adding + // a second overlay entry would decrypt first and intercept their normal + // CLI/request traffic for the duration of discovery. + if (acl.getClient(nb.id.pub_key, PUB_KEY_SIZE) != nullptr) continue; if (nb.heard_timestamp > 0 && nb.id.isHashMatch(hash)) { matching_peer_indexes[n++] = NEIGHBOR_DISCOVER_PEER_BASE + i; } } } #endif - for (int i = 0; i < acl.getNumClients(); i++) { + for (int i = 0; i < acl.getNumClients() && n < MAX_CLIENTS; i++) { if (acl.getClientByIdx(i)->id.isHashMatch(hash)) { matching_peer_indexes[n++] = i; // store the INDEXES of matching contacts (for subsequent 'peer' methods) } @@ -741,6 +745,20 @@ void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, } ClientInfo* client = acl.getClientByIdx(i); +#if defined(WITH_MQTT_NEIGHBORS) + // Neighbors that are already ACL clients are intentionally not added to the + // temporary peer overlay above. Consume only a matching discovery response + // here, then leave every other payload on the normal ACL path. + if (neighbor_discover_active && type == PAYLOAD_TYPE_RESPONSE) { + for (int oi = 0; oi < neighbor_discover_count; oi++) { + auto& nb = neighbours[neighbor_discover[oi].neighbour_idx]; + if (client->id.matches(nb.id) && handleNeighborDiscoverResponse(oi, data, len)) { + return; + } + } + } +#endif + if (type == PAYLOAD_TYPE_REQ) { // request (from a Known admin client!) uint32_t timestamp; memcpy(×tamp, data, 4); @@ -1574,14 +1592,14 @@ bool MyMesh::sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag) { return true; } -void MyMesh::handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data, size_t len) { - if (overlay_idx < 0 || overlay_idx >= neighbor_discover_count) return; +bool MyMesh::handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data, size_t len) { + if (overlay_idx < 0 || overlay_idx >= neighbor_discover_count) return false; NeighborDiscoverEntry& entry = neighbor_discover[overlay_idx]; - if (entry.status != ND_PENDING || len < 8) return; + if (entry.status != ND_PENDING || len < 8) return false; uint32_t tag; memcpy(&tag, data, 4); - if (tag != entry.tag) return; + if (tag != entry.tag) return false; size_t scope_len = len - 8; if (scope_len >= sizeof(entry.scopes)) { @@ -1590,6 +1608,19 @@ void MyMesh::handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data memcpy(entry.scopes, &data[8], scope_len); entry.scopes[scope_len] = 0; entry.status = ND_RESPONDED; + return true; +} + +static bool neighborPublishEntryComesBefore( + const MQTTMessageBuilder::NeighborsMessageEntry& lhs, + const MQTTMessageBuilder::NeighborsMessageEntry& rhs) { + if (lhs.heard_secs_ago != rhs.heard_secs_ago) { + return lhs.heard_secs_ago < rhs.heard_secs_ago; // newer first + } + if (lhs.snr != rhs.snr) { + return lhs.snr > rhs.snr; // stronger first when equally recent + } + return strcmp(lhs.pubkey_hex, rhs.pubkey_hex) < 0; } void MyMesh::finishNeighborDiscover() { @@ -1623,6 +1654,19 @@ void MyMesh::finishNeighborDiscover() { } } + // The JSON builder drops entries from the tail when the fixed MQTT buffer is + // full, so order the useful/recent neighbors first. Insertion sort is small, + // allocation-free, and sufficient for MAX_NEIGHBOURS (currently at most 50). + for (int i = 1; i < neighbor_discover_count; i++) { + MQTTMessageBuilder::NeighborsMessageEntry entry = entries[i]; + int j = i; + while (j > 0 && neighborPublishEntryComesBefore(entry, entries[j - 1])) { + entries[j] = entries[j - 1]; + j--; + } + entries[j] = entry; + } + #if defined(ESP_PLATFORM) char* json_buf = (char*)heap_caps_malloc(MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE, MALLOC_CAP_SPIRAM); #else diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index c30c31208..a2cfc1622 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -160,7 +160,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { bool startNeighborDiscover(char* reply); void loopNeighborDiscover(); void finishNeighborDiscover(); - void handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data, size_t len); + bool handleNeighborDiscoverResponse(int overlay_idx, const uint8_t* data, size_t len); void getLocalScopes(char* buf, size_t len); static const int NEIGHBOR_DISCOVER_PEER_BASE = 1000; static const unsigned long NEIGHBOR_DISCOVER_TIMEOUT_MS = 30000; diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index b9ad2eea1..b941d1799 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -633,6 +633,16 @@ void CommonCLI::loadMQTTPrefs(FILESYSTEM* fs) { memcpy(_mqtt_prefs.alert_region, _legacy_tail.alert_region, sizeof(_mqtt_prefs.alert_region)); MESH_DEBUG_PRINTLN("MQTT: Migrated observer settings from legacy /com_prefs trailing block"); } + + // Keep persisted values inside the signed-delta millis() scheduling window. + // This also repairs any manually-written or experimental value from firmware + // that briefly accepted intervals longer than the supported two-week cap. + if (_mqtt_prefs.mqtt_neighbors_interval < MQTT_NEIGHBORS_MIN_INTERVAL_MS || + _mqtt_prefs.mqtt_neighbors_interval > MQTT_NEIGHBORS_MAX_INTERVAL_MS) { + _mqtt_prefs.mqtt_neighbors_interval = MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS; + MESH_DEBUG_PRINTLN("MQTT: invalid neighbors interval reset to %u hours", + (unsigned)MQTT_NEIGHBORS_DEFAULT_INTERVAL_HOURS); + } _legacy_tail.valid = false; } diff --git a/src/helpers/CommonCLI.h b/src/helpers/CommonCLI.h index d470e7b16..255ea0bc1 100644 --- a/src/helpers/CommonCLI.h +++ b/src/helpers/CommonCLI.h @@ -161,9 +161,19 @@ struct MQTTPrefs { // tail so a shorter /mqtt_prefs payload from earlier firmware still loads with // these defaulting off (see applyMQTTDefaults); keeps the format at VERSION 1. uint8_t mqtt_neighbors_enabled; // Periodic neighbors/scopes MQTT publish (PSRAM boards only) - uint32_t mqtt_neighbors_interval; // Neighbors publish interval (ms), default 24h, min 12h + uint32_t mqtt_neighbors_interval; // Neighbors publish interval (ms), default 24h, range 12-336h }; +// Neighbor discovery is scheduled with the wrap-safe millis() helpers, whose +// signed-delta comparison requires intervals below INT32_MAX milliseconds. +// Two weeks stays comfortably inside that range. +static const uint32_t MQTT_NEIGHBORS_MIN_INTERVAL_HOURS = 12; +static const uint32_t MQTT_NEIGHBORS_MAX_INTERVAL_HOURS = 336; +static const uint32_t MQTT_NEIGHBORS_DEFAULT_INTERVAL_HOURS = 24; +static const uint32_t MQTT_NEIGHBORS_MIN_INTERVAL_MS = MQTT_NEIGHBORS_MIN_INTERVAL_HOURS * 3600000UL; +static const uint32_t MQTT_NEIGHBORS_MAX_INTERVAL_MS = MQTT_NEIGHBORS_MAX_INTERVAL_HOURS * 3600000UL; +static const uint32_t MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS = MQTT_NEIGHBORS_DEFAULT_INTERVAL_HOURS * 3600000UL; + // /mqtt_prefs is written with an 8-byte header so the format is self-describing. // Files with no header are legacy (versionless) and detected by size in loadMQTTPrefs. // The magic leads with a non-ASCII byte so it can never collide with the first diff --git a/src/helpers/CommonCLI_Observer.cpp b/src/helpers/CommonCLI_Observer.cpp index 1166553ed..3f597107f 100644 --- a/src/helpers/CommonCLI_Observer.cpp +++ b/src/helpers/CommonCLI_Observer.cpp @@ -247,22 +247,23 @@ bool CommonCLI::handleObserverSetCmd(uint32_t sender_timestamp, const char* conf strcpy(reply, "Error: interval must be between 1-60 minutes"); } #if defined(BOARD_HAS_PSRAM) && defined(MAX_NEIGHBOURS) && MAX_NEIGHBOURS > 0 - } else if (memcmp(config, "mqtt.neighbors ", 14) == 0) { - _mqtt_prefs.mqtt_neighbors_enabled = memcmp(&config[14], "on", 2) == 0; + } else if (memcmp(config, "mqtt.neighbors ", 15) == 0) { + _mqtt_prefs.mqtt_neighbors_enabled = memcmp(&config[15], "on", 2) == 0; savePrefs(); strcpy(reply, "OK"); } else if (memcmp(config, "mqtt.neighbors.interval ", 24) == 0) { uint32_t hours = _atoi(&config[24]); - if (hours >= 12 && hours <= 8760) { + if (hours >= MQTT_NEIGHBORS_MIN_INTERVAL_HOURS && + hours <= MQTT_NEIGHBORS_MAX_INTERVAL_HOURS) { _mqtt_prefs.mqtt_neighbors_interval = hours * 3600000UL; savePrefs(); sprintf(reply, "OK - neighbors interval set to %u hours (%lu ms)", hours, (unsigned long)_mqtt_prefs.mqtt_neighbors_interval); } else { - strcpy(reply, "Error: neighbors interval must be between 12-8760 hours"); + strcpy(reply, "Error: neighbors interval must be between 12-336 hours"); } #elif defined(WITH_MQTT_BRIDGE) - } else if (memcmp(config, "mqtt.neighbors ", 14) == 0 || + } else if (memcmp(config, "mqtt.neighbors ", 15) == 0 || memcmp(config, "mqtt.neighbors.interval ", 24) == 0) { strcpy(reply, "Err - not supported (requires PSRAM)"); #endif diff --git a/src/helpers/MQTTDefaults.h b/src/helpers/MQTTDefaults.h index 2617ef7ce..72c58b1ae 100644 --- a/src/helpers/MQTTDefaults.h +++ b/src/helpers/MQTTDefaults.h @@ -69,7 +69,7 @@ static inline void applyMQTTDefaults(MQTTPrefs* prefs) { prefs->mqtt_rx_enabled = 1; prefs->mqtt_status_interval = 300000; prefs->mqtt_neighbors_enabled = 0; - prefs->mqtt_neighbors_interval = 24UL * 3600UL * 1000UL; + prefs->mqtt_neighbors_interval = MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS; prefs->wifi_power_save = 1; mqttDefaultSlotPreset(prefs->mqtt_slot_preset[0], sizeof(prefs->mqtt_slot_preset[0]), diff --git a/src/helpers/MQTTMessageBuilder.cpp b/src/helpers/MQTTMessageBuilder.cpp index 75be1613b..a49f7f06f 100644 --- a/src/helpers/MQTTMessageBuilder.cpp +++ b/src/helpers/MQTTMessageBuilder.cpp @@ -238,6 +238,8 @@ int MQTTMessageBuilder::buildNeighborsMessage( char* buffer, size_t buffer_size ) { + if (!buffer || buffer_size == 0) return 0; + doc.clear(); JsonObject root = doc.to(); @@ -249,6 +251,8 @@ int MQTTMessageBuilder::buildNeighborsMessage( self["scopes"] = self_scopes ? self_scopes : ""; JsonArray arr = root.createNestedArray("neighbors"); + if (measureJson(root) >= buffer_size) return 0; + for (int i = 0; i < neighbor_count; i++) { JsonObject nb = arr.createNestedObject(); nb["pubkey"] = neighbors[i].pubkey_hex; @@ -256,6 +260,14 @@ int MQTTMessageBuilder::buildNeighborsMessage( nb["heard_secs_ago"] = neighbors[i].heard_secs_ago; nb["scopes"] = neighbors[i].scopes ? neighbors[i].scopes : ""; nb["status"] = neighbors[i].status; + + // Callers order neighbors from most to least useful (newest, then + // strongest). Stop as soon as the next entry would fill the fixed publish + // buffer, keeping document growth bounded and dropping the remaining tail. + if (measureJson(root) >= buffer_size) { + arr.remove(arr.size() - 1); + break; + } } size_t len = serializeJson(root, buffer, buffer_size); @@ -470,4 +482,4 @@ void MQTTMessageBuilder::packetToHex(mesh::Packet* packet, char* hex, size_t hex // Convert serialized packet to hex bytesToHex(raw_buf, raw_len, hex, hex_size); -} \ No newline at end of file +} diff --git a/src/helpers/bridges/MQTTBridge.cpp b/src/helpers/bridges/MQTTBridge.cpp index fe09024b3..aac22561a 100644 --- a/src/helpers/bridges/MQTTBridge.cpp +++ b/src/helpers/bridges/MQTTBridge.cpp @@ -1034,13 +1034,13 @@ void MQTTBridge::mqttTaskLoop() { processPacketQueue(); #if defined(WITH_MQTT_NEIGHBORS) - if (_neighbors_publish_pending) { - _neighbors_publish_pending = false; + if (_neighbors_publish_pending.load(std::memory_order_acquire)) { if (publishNeighbors()) { MQTT_DEBUG_PRINTLN("Neighbors published"); } else { MQTT_DEBUG_PRINTLN("Neighbors publish failed"); } + _neighbors_publish_pending.store(false, std::memory_order_release); } #endif @@ -2749,13 +2749,14 @@ bool MQTTBridge::publishStatus() { #if defined(WITH_MQTT_NEIGHBORS) void MQTTBridge::requestPublishNeighbors(const char* json, size_t len) { if (!_neighbors_json_buffer || !json || len == 0) return; + if (_neighbors_publish_pending.load(std::memory_order_acquire)) return; if (len >= NEIGHBORS_JSON_BUFFER_SIZE) { len = NEIGHBORS_JSON_BUFFER_SIZE - 1; } memcpy(_neighbors_json_buffer, json, len); _neighbors_json_buffer[len] = '\0'; _neighbors_publish_len = len; - _neighbors_publish_pending = true; + _neighbors_publish_pending.store(true, std::memory_order_release); } bool MQTTBridge::publishNeighbors() { diff --git a/src/helpers/bridges/MQTTBridge.h b/src/helpers/bridges/MQTTBridge.h index 70de88ad2..0623a17ef 100644 --- a/src/helpers/bridges/MQTTBridge.h +++ b/src/helpers/bridges/MQTTBridge.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "helpers/JWTHelper.h" #include "helpers/MQTTPresets.h" @@ -264,7 +265,9 @@ private: #if defined(WITH_MQTT_NEIGHBORS) char* _neighbors_json_buffer; size_t _neighbors_publish_len; - bool _neighbors_publish_pending; + // Release/acquire handoff from the Arduino loop (Core 1) to the MQTT task + // (Core 0). A second snapshot is dropped while the current one is publishing. + std::atomic _neighbors_publish_pending; #endif #if defined(BOARD_HAS_PSRAM) char* _publish_json_buffer;