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.
This commit is contained in:
1 parent
6b3c543cba
commit
1454e4cac1
12 files changed
+103
-21
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+11
-1
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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]),
|
||||
|
||||
@@ -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<JsonObject>();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <Timezone.h>
|
||||
#include <atomic>
|
||||
#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<bool> _neighbors_publish_pending;
|
||||
#endif
|
||||
#if defined(BOARD_HAS_PSRAM)
|
||||
char* _publish_json_buffer;
|
||||
|
||||
Reference in new issue
Block a user