From 8d7a47abf7510c62d8e2472daf69f6d30ace83d9 Mon Sep 17 00:00:00 2001 From: agessaman Date: Sun, 19 Jul 2026 20:41:20 -0700 Subject: [PATCH] feat(mqtt): add neighbors prefs fields (flex-compatible v1 layout) Append mqtt_neighbors_enabled(u8) + mqtt_neighbors_interval(u32) to the observer tail of MQTTPrefs. The layout is kept byte-identical to the flex neighbors build: the enable flag lands in the old struct's zeroed trailing padding (offset 2857) and the interval begins exactly at the former baseline (2860), so sizeof grows 2860 -> 2864 (net +4 bytes). offsetof static_asserts lock the layout so a mismatch fails the build. The codec now accepts three v1 payload sizes: register 2860 as a "pre-neighbors" Current payload so an in-lineage upgrade reads its existing /mqtt_prefs and defaults the neighbors tail (off / 24h). Because 2864 is the shared Current baseline, a /mqtt_prefs written by either the flex build or this firmware is interchangeable. Add the 12/24/336h interval constants, neighbors defaults, and a load-time interval clamp that keeps persisted values inside the signed-delta millis() scheduling window. Extend the host codec suite with a pre-neighbors migration case and neighbors round-trip coverage. --- src/helpers/CommonCLI.cpp | 10 +++++ src/helpers/MQTTDefaults.h | 5 +++ src/helpers/MQTTPrefsCodec.h | 7 ++++ src/helpers/MQTTPrefsStorage.h | 36 ++++++++++++++++- .../test_mqtt_prefs_codec.cpp | 40 +++++++++++++++++++ 5 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index 02569e4c5..b430374b4 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -807,6 +807,16 @@ void CommonCLI::loadMQTTPrefs( mqtt_rewrite_pending = true; 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; if (mqtt_rewrite_pending) { diff --git a/src/helpers/MQTTDefaults.h b/src/helpers/MQTTDefaults.h index f3c117eb6..6b4c9ef15 100644 --- a/src/helpers/MQTTDefaults.h +++ b/src/helpers/MQTTDefaults.h @@ -100,6 +100,11 @@ static inline void applyMQTTDefaults(MQTTPrefs* prefs) { prefs->alert_wifi_minutes = 30; prefs->alert_mqtt_minutes = 240; prefs->alert_min_interval_min = 60; + + // Neighbors publishing defaults off; a defaulted tail is a valid 24h interval + // (not 0) so an in-lineage upgrade from a pre-neighbors payload is sane. + prefs->mqtt_neighbors_enabled = 0; + prefs->mqtt_neighbors_interval = MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS; } #endif // WITH_MQTT_BRIDGE diff --git a/src/helpers/MQTTPrefsCodec.h b/src/helpers/MQTTPrefsCodec.h index 85629dffd..715d33f26 100644 --- a/src/helpers/MQTTPrefsCodec.h +++ b/src/helpers/MQTTPrefsCodec.h @@ -36,6 +36,7 @@ struct DecodePlan { }; static const size_t kV1PreObserverPayloadSize = MQTT_PREFS_V1_PRE_OBSERVER_PAYLOAD_SIZE; +static const size_t kV1PreNeighborsPayloadSize = MQTT_PREFS_V1_PRE_NEIGHBORS_PAYLOAD_SIZE; static const size_t kV1BaselinePayloadSize = MQTT_PREFS_V1_FULL_PAYLOAD_SIZE; static const size_t kEncodedSize = sizeof(MQTTPrefsHeader) + kV1BaselinePayloadSize; @@ -99,6 +100,12 @@ inline DecodePlan classify(const uint8_t* prefix, size_t prefix_read, size_t fil if (header.payload_len == kV1BaselinePayloadSize) { return {Source::Current, false, false, true, kV1BaselinePayloadSize}; } + if (header.payload_len == kV1PreNeighborsPayloadSize) { + // Written by observer/webconfig firmware before the neighbors tail + // existed. The observer fields ARE present; only the neighbors tail is + // missing, so it loads and keeps its defaults (off / 24h). + return {Source::Current, false, false, true, kV1PreNeighborsPayloadSize}; + } if (header.payload_len == kV1PreObserverPayloadSize) { return {Source::Current, false, false, false, kV1PreObserverPayloadSize}; } diff --git a/src/helpers/MQTTPrefsStorage.h b/src/helpers/MQTTPrefsStorage.h index fd898694a..ffa90c58b 100644 --- a/src/helpers/MQTTPrefsStorage.h +++ b/src/helpers/MQTTPrefsStorage.h @@ -108,12 +108,35 @@ struct MQTTPrefs { uint16_t alert_min_interval_min; char alert_hashtag[24]; char alert_region[31]; + + // Neighbors publishing (PSRAM boards only). Appended at the end of the + // observer tail so a shorter (pre-neighbors) /mqtt_prefs payload from earlier + // firmware still loads with these defaulting off/24h; keeps the format at + // VERSION 1. Field order and sizes are kept byte-identical to the flex + // neighbors build so a /mqtt_prefs written by either firmware is + // interchangeable (see the offsetof static_asserts below). + uint8_t mqtt_neighbors_enabled; + uint32_t mqtt_neighbors_interval; }; -// Version-1 has exactly two layouts this firmware can decode. Never infer a +// Neighbor discovery is scheduled with the wrap-safe millis() helpers, whose +// signed-delta comparison requires intervals below INT32_MAX ms. The 336h +// (two-week) cap 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; + +// Version-1 has three payload layouts this firmware can decode. Never infer a // compatible payload from an arbitrary shorter size: raw prefs have no checksum. +// - PRE_OBSERVER (2736): stops before the observer tail (snmp_*/alert_*). +// - PRE_NEIGHBORS (2860): full observer tail, no neighbors fields yet. +// - FULL (2864): current baseline, with the neighbors tail. static const size_t MQTT_PREFS_V1_PRE_OBSERVER_PAYLOAD_SIZE = 2736; -static const size_t MQTT_PREFS_V1_FULL_PAYLOAD_SIZE = 2860; +static const size_t MQTT_PREFS_V1_PRE_NEIGHBORS_PAYLOAD_SIZE = 2860; +static const size_t MQTT_PREFS_V1_FULL_PAYLOAD_SIZE = 2864; // /mqtt_prefs starts with a self-describing 8-byte header. Headerless files // are deployed legacy layouts and continue to be distinguished by size. @@ -234,6 +257,15 @@ static_assert(offsetof(MQTTPrefs, snmp_enabled) == MQTT_PREFS_V1_PRE_OBSERVER_PA "v1 pre-observer /mqtt_prefs boundary changed"); static_assert(sizeof(MQTTPrefs) == MQTT_PREFS_V1_FULL_PAYLOAD_SIZE, "v1 /mqtt_prefs payload layout changed"); +// Lock the neighbors tail to the flex neighbors build's layout so a /mqtt_prefs +// written by either firmware is byte-for-byte interchangeable. The enable flag +// lands in the old struct's zeroed trailing padding (offset 2857), and the +// interval begins exactly at the pre-neighbors payload size (2860) so a +// pre-neighbors read stops right before it and the interval keeps its default. +static_assert(offsetof(MQTTPrefs, mqtt_neighbors_enabled) == 2857, + "neighbors enable flag must sit at the flex-compatible offset"); +static_assert(offsetof(MQTTPrefs, mqtt_neighbors_interval) == MQTT_PREFS_V1_PRE_NEIGHBORS_PAYLOAD_SIZE, + "neighbors interval offset must equal the pre-neighbors payload size"); static_assert(sizeof(OldMQTTPrefs) == 472, "frozen pre-slot /mqtt_prefs layout changed"); static_assert(sizeof(PreWifiPowerOldMQTTPrefs) == 472, "frozen pre-WiFi-power /mqtt_prefs layout changed"); static_assert(offsetof(OldMQTTPrefs, wifi_power_save) == 144, diff --git a/test/test_mqtt_prefs_codec/test_mqtt_prefs_codec.cpp b/test/test_mqtt_prefs_codec/test_mqtt_prefs_codec.cpp index 2909fa5fd..6d1fba4ba 100644 --- a/test/test_mqtt_prefs_codec/test_mqtt_prefs_codec.cpp +++ b/test/test_mqtt_prefs_codec/test_mqtt_prefs_codec.cpp @@ -226,6 +226,8 @@ TEST(MQTTPrefsCodec, CurrentVersionedPayloadRoundTripsExactly) { strncpy(source.mqtt_origin, "current-node", sizeof(source.mqtt_origin) - 1); strncpy(source.mqtt_slot_password[2], "preserve-me", sizeof(source.mqtt_slot_password[2]) - 1); strncpy(source.alert_region, "PNW", sizeof(source.alert_region) - 1); + source.mqtt_neighbors_enabled = 1; + source.mqtt_neighbors_interval = MQTT_NEIGHBORS_MAX_INTERVAL_MS; std::vector bytes(Codec::kEncodedSize); ASSERT_EQ(Codec::kEncodedSize, Codec::encode(source, bytes.data(), bytes.size())); @@ -266,6 +268,44 @@ TEST(MQTTPrefsCodec, CompatibleShortV1PayloadPreservesDefaultsBeyondObserverBoun EXPECT_EQ(5, loaded.radio_watchdog_minutes); } +TEST(MQTTPrefsCodec, PreNeighborsV1PayloadLoadsObserverFieldsAndDefaultsNeighborsTail) { + // A /mqtt_prefs written by observer/webconfig firmware before the neighbors + // tail existed: full observer fields, 2860-byte v1 payload. It must still load + // as Current (observer fields present) with the neighbors tail defaulted. + MQTTPrefs source = defaults(); + strncpy(source.mqtt_origin, "pre-neighbors-node", sizeof(source.mqtt_origin) - 1); + strncpy(source.alert_region, "PNW", sizeof(source.alert_region) - 1); + source.snmp_enabled = 1; + source.alert_enabled = 1; + source.mqtt_neighbors_enabled = 0; // old struct's byte 2857 was zero padding + source.mqtt_neighbors_interval = 0x11223344; // must NOT survive a 2860-byte read + + std::vector bytes(sizeof(MQTTPrefsHeader) + Codec::kV1PreNeighborsPayloadSize, 0); + writeHeader(&bytes, MQTT_PREFS_VERSION, + static_cast(Codec::kV1PreNeighborsPayloadSize)); + memcpy(bytes.data() + sizeof(MQTTPrefsHeader), &source, Codec::kV1PreNeighborsPayloadSize); + + const Codec::DecodePlan plan = classify(bytes); + ASSERT_EQ(Codec::Source::Current, plan.source); + ASSERT_EQ(Codec::kV1PreNeighborsPayloadSize, plan.payload_len); + ASSERT_FALSE(plan.preserve_file); + ASSERT_TRUE(plan.observer_fields_present); + + MQTTPrefs loaded = defaults(); + loaded.mqtt_neighbors_enabled = 1; // pretend stale + loaded.mqtt_neighbors_interval = MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS; // caller's defaulted tail + memcpy(&loaded, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len); + + EXPECT_STREQ("pre-neighbors-node", loaded.mqtt_origin); + EXPECT_STREQ("PNW", loaded.alert_region); + EXPECT_EQ(1, loaded.snmp_enabled); + EXPECT_EQ(1, loaded.alert_enabled); + // Enable flag sits at offset 2857 (inside the 2860 read) -> takes the file's 0. + // Interval begins at 2860 (beyond the read) -> keeps the caller's default. + EXPECT_EQ(0u, loaded.mqtt_neighbors_enabled); + EXPECT_EQ(MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS, loaded.mqtt_neighbors_interval); +} + TEST(MQTTPrefsCodec, CorruptOrShortVersionedInputsArePreserved) { Codec::DecodePlan plan = Codec::classify(nullptr, 0, 0); EXPECT_EQ(Codec::Source::Corrupt, plan.source);