feat(mqtt): add default_scope to neighbor discovery JSON handling

Enhance the neighbor discovery JSON structure by introducing a
default_scope field, which indicates the region name this node
floods to by default. This change improves clarity in the
neighbor discovery process and aligns with the unscoped flood
behavior when no default region is set. Updates include
modifications to the MyMesh class and related message building
functions to accommodate the new field.
This commit is contained in:
agessaman committed 2026-07-29 15:00:32 -07:00
1 parent 971a3b1464
commit 8b16af3830
10 files changed
+57 -20

No files matched your search

+4 -1
View File
@@ -742,7 +742,7 @@ Like status/raw, this topic is **not** sent to MeshRank slots (packets-only cont
"timestamp": "2024-01-01T12:00:00.000000+00:00",
"origin": "MeshCore-HOWL",
"origin_id": "A1B2C3D4E5F67890...",
"self": { "scopes": "DEN,APRS" },
"self": { "scopes": "DEN,APRS", "default_scope": "*" },
"neighbors": [
{
"pubkey": "0011223344556677...",
@@ -757,6 +757,9 @@ Like status/raw, this topic is **not** sent to MeshRank slots (packets-only cont
Entries are ordered most- to least-useful (most recently heard, then stronger
SNR); the tail is dropped if the payload would exceed the 10 KB publish buffer.
`status` is `responded`, `timeout`, or `send_failed` per neighbor.
`self.default_scope` is the region name this node floods to by default (`region
default`); it is `*` when no default region is set, matching the unscoped flood
the radio actually performs in that case.
## Key Features
+11 -2
View File
@@ -1078,6 +1078,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
neighbor_discover_request = NULL;
next_neighbors_publish = 0;
self_scopes_buf[0] = 0;
self_default_scope_buf[0] = 0;
neighbor_discover_origin[0] = 0;
#endif
@@ -1892,6 +1893,14 @@ uint32_t MyMesh::neighborDiscoverQueryTimeoutMs() const {
void MyMesh::resetNeighborDiscoverJsonBudget() {
getLocalScopes(self_scopes_buf, sizeof(self_scopes_buf));
{
// No default region means this node floods unscoped, i.e. the wildcard.
RegionEntry* def = region_map.getDefaultRegion();
const char* def_name = (def && def->name[0]) ? def->name : "*";
if (*def_name == '#') def_name++; // match how self.scopes renders names
strncpy(self_default_scope_buf, def_name, sizeof(self_default_scope_buf) - 1);
self_default_scope_buf[sizeof(self_default_scope_buf) - 1] = 0;
}
MQTTBridge::getEffectiveMqttOrigin(
&_prefs, _cli.getObserverPrefs(),
neighbor_discover_origin, sizeof(neighbor_discover_origin));
@@ -1907,7 +1916,7 @@ void MyMesh::resetNeighborDiscoverJsonBudget() {
neighbor_discover_truncated = false;
neighbor_discover_json_size = MQTTMessageBuilder::measureNeighborsMessageBase(
neighbor_discover_origin, self_pubkey_hex, timestamp, self_scopes_buf,
neighbor_discover_count);
self_default_scope_buf, neighbor_discover_count);
}
// Account for one terminal result. The base measurement reserves maximum-width
@@ -2075,7 +2084,7 @@ void MyMesh::finishNeighborDiscover() {
#endif
int json_len = MQTTMessageBuilder::buildNeighborsMessage(
doc, neighbor_discover_origin, self_pubkey_hex, timestamp, self_scopes_buf,
entries, neighbor_discover_publish_count,
self_default_scope_buf, entries, neighbor_discover_publish_count,
json_buf, MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE,
neighbor_discover_count, neighbor_discover_queried_count,
neighbor_discover_truncated);
+1
View File
@@ -181,6 +181,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
mesh::Packet* neighbor_discover_request; // request awaiting TX completion
unsigned long next_neighbors_publish; // periodic publish deadline (0 = fire ASAP)
char self_scopes_buf[96];
char self_default_scope_buf[31];
char neighbor_discover_origin[32];
mesh::Packet* sendAnonRegionsReq(const mesh::Identity& target, uint32_t& tag);
+11 -2
View File
@@ -897,6 +897,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
neighbor_discover_request = NULL;
next_neighbors_publish = 0;
self_scopes_buf[0] = 0;
self_default_scope_buf[0] = 0;
neighbor_discover_origin[0] = 0;
memset(neighbours, 0, sizeof(neighbours));
#endif
@@ -1739,6 +1740,14 @@ uint32_t MyMesh::neighborDiscoverQueryTimeoutMs() const {
void MyMesh::resetNeighborDiscoverJsonBudget() {
getLocalScopes(self_scopes_buf, sizeof(self_scopes_buf));
{
// No default region means this node floods unscoped, i.e. the wildcard.
RegionEntry* def = region_map.getDefaultRegion();
const char* def_name = (def && def->name[0]) ? def->name : "*";
if (*def_name == '#') def_name++; // match how self.scopes renders names
strncpy(self_default_scope_buf, def_name, sizeof(self_default_scope_buf) - 1);
self_default_scope_buf[sizeof(self_default_scope_buf) - 1] = 0;
}
MQTTBridge::getEffectiveMqttOrigin(
&_prefs, _cli.getObserverPrefs(),
neighbor_discover_origin, sizeof(neighbor_discover_origin));
@@ -1754,7 +1763,7 @@ void MyMesh::resetNeighborDiscoverJsonBudget() {
neighbor_discover_truncated = false;
neighbor_discover_json_size = MQTTMessageBuilder::measureNeighborsMessageBase(
neighbor_discover_origin, self_pubkey_hex, timestamp, self_scopes_buf,
neighbor_discover_count);
self_default_scope_buf, neighbor_discover_count);
}
// Account for one terminal result. The base measurement reserves maximum-width
@@ -1922,7 +1931,7 @@ void MyMesh::finishNeighborDiscover() {
#endif
int json_len = MQTTMessageBuilder::buildNeighborsMessage(
doc, neighbor_discover_origin, self_pubkey_hex, timestamp, self_scopes_buf,
entries, neighbor_discover_publish_count,
self_default_scope_buf, entries, neighbor_discover_publish_count,
json_buf, MQTTBridge::NEIGHBORS_JSON_BUFFER_SIZE,
neighbor_discover_count, neighbor_discover_queried_count,
neighbor_discover_truncated);
+1
View File
@@ -174,6 +174,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks
mesh::Packet* neighbor_discover_request;
unsigned long next_neighbors_publish;
char self_scopes_buf[96];
char self_default_scope_buf[31];
char neighbor_discover_origin[32];
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
+7 -3
View File
@@ -108,6 +108,7 @@ int MQTTMessageBuilder::buildNeighborsMessage(
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const char* self_default_scope,
const NeighborsMessageEntry* neighbors,
int neighbor_count,
char* buffer,
@@ -117,8 +118,9 @@ int MQTTMessageBuilder::buildNeighborsMessage(
bool truncated
) {
return MQTTPayloadBuilder::buildNeighborsMessage(
doc, origin, origin_id, timestamp, self_scopes, neighbors, neighbor_count,
buffer, buffer_size, total_neighbors, queried_neighbors, truncated);
doc, origin, origin_id, timestamp, self_scopes, self_default_scope,
neighbors, neighbor_count, buffer, buffer_size, total_neighbors,
queried_neighbors, truncated);
}
size_t MQTTMessageBuilder::measureNeighborsMessageBase(
@@ -126,10 +128,12 @@ size_t MQTTMessageBuilder::measureNeighborsMessageBase(
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const char* self_default_scope,
int total_neighbors
) {
return MQTTPayloadBuilder::measureNeighborsMessageBase(
origin, origin_id, timestamp, self_scopes, total_neighbors);
origin, origin_id, timestamp, self_scopes, self_default_scope,
total_neighbors);
}
size_t MQTTMessageBuilder::measureNeighborsMessageEntry(
+2
View File
@@ -164,6 +164,7 @@ public:
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const char* self_default_scope,
const NeighborsMessageEntry* neighbors,
int neighbor_count,
char* buffer,
@@ -177,6 +178,7 @@ public:
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const char* self_default_scope,
int total_neighbors
);
static size_t measureNeighborsMessageEntry(const NeighborsMessageEntry& neighbor);
+6 -2
View File
@@ -191,6 +191,7 @@ static JsonArray buildNeighborsMessageBase(
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const char* self_default_scope,
int total_neighbors,
int queried_neighbors,
bool truncated
@@ -208,6 +209,7 @@ static JsonArray buildNeighborsMessageBase(
JsonObject self = root["self"].to<JsonObject>();
self["scopes"] = self_scopes ? self_scopes : "";
self["default_scope"] = self_default_scope ? self_default_scope : "";
return root["neighbors"].to<JsonArray>();
}
@@ -228,11 +230,12 @@ size_t MQTTPayloadBuilder::measureNeighborsMessageBase(
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const char* self_default_scope,
int total_neighbors
) {
JsonDocument doc;
buildNeighborsMessageBase(
doc, origin, origin_id, timestamp, self_scopes,
doc, origin, origin_id, timestamp, self_scopes, self_default_scope,
total_neighbors, total_neighbors, false);
return measureJson(doc);
}
@@ -253,6 +256,7 @@ int MQTTPayloadBuilder::buildNeighborsMessage(
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const char* self_default_scope,
const NeighborsMessageEntry* neighbors,
int neighbor_count,
char* buffer,
@@ -264,7 +268,7 @@ int MQTTPayloadBuilder::buildNeighborsMessage(
if (!buffer || buffer_size == 0) return 0;
JsonArray arr = buildNeighborsMessageBase(
doc, origin, origin_id, timestamp, self_scopes,
doc, origin, origin_id, timestamp, self_scopes, self_default_scope,
total_neighbors, queried_neighbors, truncated);
if (doc.overflowed() || arr.isNull() || measureJson(doc) >= buffer_size) return 0;
+2
View File
@@ -86,6 +86,7 @@ public:
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const char* self_default_scope,
const NeighborsMessageEntry* neighbors,
int neighbor_count,
char* buffer,
@@ -103,6 +104,7 @@ public:
const char* origin_id,
const char* timestamp,
const char* self_scopes,
const char* self_default_scope,
int total_neighbors
);
static size_t measureNeighborsMessageEntry(const NeighborsMessageEntry& neighbor);
@@ -242,7 +242,7 @@ TEST(MQTTPayloadBuilder, NeighborsMessageRoundTripsSelfAndEntries) {
JsonDocument scratch;
char buffer[1024];
int len = MQTTPayloadBuilder::buildNeighborsMessage(
scratch, "DEN Repeater", "0123456789ABCDEF", kTimestamp, "DEN,APRS",
scratch, "DEN Repeater", "0123456789ABCDEF", kTimestamp, "DEN,APRS", "*",
neighbors, 2, buffer, sizeof(buffer), 5, 2, true);
ASSERT_GT(len, 0);
@@ -253,6 +253,7 @@ TEST(MQTTPayloadBuilder, NeighborsMessageRoundTripsSelfAndEntries) {
EXPECT_STREQ("DEN Repeater", parsed["origin"].as<const char*>());
EXPECT_STREQ("0123456789ABCDEF", parsed["origin_id"].as<const char*>());
EXPECT_STREQ("DEN,APRS", parsed["self"]["scopes"].as<const char*>());
EXPECT_STREQ("*", parsed["self"]["default_scope"].as<const char*>());
EXPECT_EQ(5, parsed["total_neighbors"].as<int>());
EXPECT_EQ(2, parsed["queried_neighbors"].as<int>());
EXPECT_TRUE(parsed["truncated"].as<bool>());
@@ -277,7 +278,7 @@ TEST(MQTTPayloadBuilder, NeighborsMessageMeasurementsMatchCompletePayload) {
size_t measured =
MQTTPayloadBuilder::measureNeighborsMessageBase(
"DEN Repeater", "0123456789ABCDEF", kTimestamp, "DEN,APRS", 2)
"DEN Repeater", "0123456789ABCDEF", kTimestamp, "DEN,APRS", "*", 2)
+ MQTTPayloadBuilder::measureNeighborsMessageEntry(neighbors[0])
+ 1 // array comma
+ MQTTPayloadBuilder::measureNeighborsMessageEntry(neighbors[1]);
@@ -285,7 +286,7 @@ TEST(MQTTPayloadBuilder, NeighborsMessageMeasurementsMatchCompletePayload) {
JsonDocument scratch;
char buffer[1024];
int len = MQTTPayloadBuilder::buildNeighborsMessage(
scratch, "DEN Repeater", "0123456789ABCDEF", kTimestamp, "DEN,APRS",
scratch, "DEN Repeater", "0123456789ABCDEF", kTimestamp, "DEN,APRS", "*",
neighbors, 2, buffer, sizeof(buffer), 2, 2, false);
ASSERT_GT(len, 0);
@@ -307,7 +308,7 @@ TEST(MQTTPayloadBuilder, NeighborsMessageMeasuredPrefixStopsBeforeOverflow) {
constexpr size_t kBufferSize = 512;
size_t used = MQTTPayloadBuilder::measureNeighborsMessageBase(
"node", "id", kTimestamp, "DEN", 20);
"node", "id", kTimestamp, "DEN", "*", 20);
int published = 0;
int queried = 0;
while (queried < 20) {
@@ -327,7 +328,7 @@ TEST(MQTTPayloadBuilder, NeighborsMessageMeasuredPrefixStopsBeforeOverflow) {
JsonDocument scratch;
char buffer[kBufferSize];
int len = MQTTPayloadBuilder::buildNeighborsMessage(
scratch, "node", "id", kTimestamp, "DEN",
scratch, "node", "id", kTimestamp, "DEN", "*",
neighbors, published, buffer, sizeof(buffer),
20, queried, true);
@@ -349,13 +350,13 @@ TEST(MQTTPayloadBuilder, NeighborsMessageFallbackMarksTruncated) {
};
size_t first_only_size =
MQTTPayloadBuilder::measureNeighborsMessageBase(
"node", "id", kTimestamp, "DEN", 2)
"node", "id", kTimestamp, "DEN", "*", 2)
+ MQTTPayloadBuilder::measureNeighborsMessageEntry(neighbors[0]);
std::vector<char> buffer(first_only_size + 1);
JsonDocument scratch;
int len = MQTTPayloadBuilder::buildNeighborsMessage(
scratch, "node", "id", kTimestamp, "DEN",
scratch, "node", "id", kTimestamp, "DEN", "*",
neighbors, 2, buffer.data(), buffer.size(),
2, 2, false);
@@ -374,7 +375,7 @@ TEST(MQTTPayloadBuilder, NeighborsMessageFailsCleanlyOnAllocationFailure) {
char buffer[512];
EXPECT_EQ(0, MQTTPayloadBuilder::buildNeighborsMessage(
scratch, "node", "id", kTimestamp, "DEN",
scratch, "node", "id", kTimestamp, "DEN", "*",
&neighbor, 1, buffer, sizeof(buffer),
1, 1, false));
}
@@ -383,13 +384,14 @@ TEST(MQTTPayloadBuilder, NeighborsMessageHandlesEmptyTableAndNullScopes) {
JsonDocument scratch;
char buffer[256];
int len = MQTTPayloadBuilder::buildNeighborsMessage(
scratch, "node", "id", kTimestamp, nullptr, nullptr, 0,
scratch, "node", "id", kTimestamp, nullptr, nullptr, nullptr, 0,
buffer, sizeof(buffer));
ASSERT_GT(len, 0);
JsonDocument parsed;
ASSERT_FALSE(deserializeJson(parsed, buffer));
EXPECT_STREQ("", parsed["self"]["scopes"].as<const char*>());
EXPECT_STREQ("", parsed["self"]["default_scope"].as<const char*>());
JsonArray arr = parsed["neighbors"].as<JsonArray>();
ASSERT_TRUE(arr.isNull() == false);
EXPECT_EQ(0U, arr.size());
@@ -412,7 +414,7 @@ TEST(MQTTPayloadBuilder, NeighborsMessageDropsTailWhenBufferFills) {
JsonDocument scratch;
char buffer[512];
int len = MQTTPayloadBuilder::buildNeighborsMessage(
scratch, "node", "id", kTimestamp, "DEN", neighbors, 20,
scratch, "node", "id", kTimestamp, "DEN", "*", neighbors, 20,
buffer, sizeof(buffer));
ASSERT_GT(len, 0);