feat(mqtt): enhance packet filter functionality with named types
Add support for named packet types in per-slot filters, allowing users to specify packet types using descriptive names alongside numeric values. This improves usability and clarity in configuring MQTT slot filters. Updates include modifications to the parsing logic, WebConfig interface, and related documentation to reflect the new naming conventions.
This commit is contained in:
1 parent
e00b29b4b8
commit
52bd27190f
17 files changed
+830
-87
No files matched your search
+2
-2
@@ -30,9 +30,9 @@ does not reflect the GoogleTest count — run the built binary directly
|
||||
| `test_mqtt_topic_router` | `src/helpers/MQTTTopicRouter.h` | complete preset/custom topic-routing contract; MeshRank packets-only behavior; required identifiers; invalid inputs/slots; exact buffer boundaries |
|
||||
| `test_mqtt_connection_policy` | `src/helpers/MQTTConnectionPolicy.h` | reconnect guard/backoff/stagger and breaker transitions; stable reset; JWT lifetime/renewal policy; exact timing boundaries and 32-bit `millis()` rollover |
|
||||
| `test_mqtt_packet_queue_policy` | `src/helpers/MQTTPacketQueuePolicy.h` | queue-full eviction; stale-disconnect flush; adaptive drain limits; bounded QoS0 retries; exact timing boundaries and 32-bit `millis()` rollover |
|
||||
| `test_mqtt_packet_filter` | `src/helpers/MQTTPacketFilter.h` | per-slot 0-15 allowlist parsing/formatting; exact bounds; membership; eligible-target and retry-completion policy |
|
||||
| `test_mqtt_packet_filter` | `src/helpers/MQTTPacketFilter.h` | per-slot 0-15 allowlist parsing/formatting, numeric and named spellings; exact bounds; membership; candidate/eligible split and retry-completion policy; pre-queue union gate; default-mask detection |
|
||||
| `test_mqtt_runtime_buffer_lifecycle` | `src/helpers/MQTTRuntimeBufferLifecycle.h` | idempotent allocation/release; partial-allocation degradation; retry of only missing buffers |
|
||||
| `test_mqtt_prefs_codec` | `src/helpers/MQTTPrefsStorage.h`, `src/helpers/MQTTPrefsCodec.h` | binary pre-slot/3-slot/6-slot migration fixtures; v1 header integrity; downgrade preservation |
|
||||
| `test_mqtt_prefs_codec` | `src/helpers/MQTTPrefsStorage.h`, `src/helpers/MQTTPrefsCodec.h` | binary pre-slot/3-slot/6-slot migration fixtures; v1 header integrity; downgrade preservation; shortest-payload write policy (default filters stay downgrade-readable) |
|
||||
| `test_mqtt_prefs_atomic_store` | `src/helpers/MQTTPrefsAtomicStore.h` | transactional MQTT writes and legacy `/node_prefs` handoff; exact short-write detection; begin/finish/rename failure cleanup; original-file preservation |
|
||||
| `test_mqtt_payload_builder` | `src/helpers/MQTTPayloadBuilder.cpp` | status/packet/raw JSON contracts; optional fields; escaping; RX metrics and path; score handling; exact buffer bounds; maximum representative payloads |
|
||||
| `test_utils` | `src/Utils.cpp` | `Utils::toHex` (upstream) |
|
||||
|
||||
@@ -33,10 +33,52 @@ TEST(MQTTPacketFilter, FullNumericListCanonicalizesToAll) {
|
||||
EXPECT_STREQ("all", output);
|
||||
}
|
||||
|
||||
TEST(MQTTPacketFilter, ParsesPayloadTypeNames) {
|
||||
uint16_t mask = 0;
|
||||
ASSERT_TRUE(Filter::parse("advert", &mask));
|
||||
EXPECT_EQ(static_cast<uint16_t>(1u << 4), mask);
|
||||
|
||||
ASSERT_TRUE(Filter::parse(" txt_msg , grp_txt ", &mask));
|
||||
EXPECT_EQ(static_cast<uint16_t>((1u << 2) | (1u << 5)), mask);
|
||||
|
||||
// Names and numbers are interchangeable within one list.
|
||||
ASSERT_TRUE(Filter::parse("advert,2,raw_custom", &mask));
|
||||
EXPECT_EQ(static_cast<uint16_t>((1u << 4) | (1u << 2) | (1u << 15)), mask);
|
||||
|
||||
// Output stays numeric regardless of how the value was spelled.
|
||||
char output[Filter::kFilterTextSize];
|
||||
ASSERT_TRUE(Filter::format(mask, output, sizeof(output)));
|
||||
EXPECT_STREQ("2,4,15", output);
|
||||
}
|
||||
|
||||
TEST(MQTTPacketFilter, EveryNamedTypeResolvesToItsPacketHeaderValue) {
|
||||
// Pins the table against src/Packet.h's PAYLOAD_TYPE_* values.
|
||||
const struct { const char* name; uint8_t type; } expected[] = {
|
||||
{"req", 0}, {"response", 1}, {"txt_msg", 2}, {"ack", 3}, {"advert", 4},
|
||||
{"grp_txt", 5}, {"grp_data", 6}, {"anon_req", 7}, {"path", 8},
|
||||
{"trace", 9}, {"multipart", 10}, {"control", 11}, {"raw_custom", 15},
|
||||
};
|
||||
for (const auto& e : expected) {
|
||||
uint16_t mask = 0;
|
||||
ASSERT_TRUE(Filter::parse(e.name, &mask)) << e.name;
|
||||
EXPECT_EQ(static_cast<uint16_t>(1u << e.type), mask) << e.name;
|
||||
}
|
||||
// Reserved types have no name and stay reachable by number only.
|
||||
size_t count = 0;
|
||||
const Filter::NamedPacketType* names = Filter::namedPacketTypes(&count);
|
||||
EXPECT_EQ(sizeof(expected) / sizeof(expected[0]), count);
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
EXPECT_TRUE(names[i].type < 12 || names[i].type == 15) << names[i].name;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MQTTPacketFilter, RejectsMalformedOrOutOfRangeValuesWithoutChangingOutput) {
|
||||
const char* invalid[] = {
|
||||
",", "1,", ",1", "1,,2", "1, ,2", "-1", "+1", "0x2", "16",
|
||||
"999999999999999999999", "ALL", "All", "all,2", "none,2", "2-4", "2x",
|
||||
// Names are lowercase, exact, and are not whole-value keywords.
|
||||
"ADVERT", "Advert", "advert_", "adver", "advertx", "reserved", "12_reserved",
|
||||
"2,all", "advert,none", "advert advert", "advert;2",
|
||||
};
|
||||
for (const char* value : invalid) {
|
||||
uint16_t mask = 0x1234;
|
||||
@@ -137,6 +179,124 @@ TEST(MQTTPacketFilter, PacketAndRawCanShareTheSameMaskDecision) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MQTTPacketFilter, CandidateIsTheTopicFreePartOfEligibility) {
|
||||
const uint16_t only_advert = static_cast<uint16_t>(1u << 4);
|
||||
EXPECT_TRUE(Filter::slotCandidate(true, only_advert, 4));
|
||||
EXPECT_FALSE(Filter::slotCandidate(false, only_advert, 4));
|
||||
EXPECT_FALSE(Filter::slotCandidate(true, only_advert, 2));
|
||||
// A candidate is exactly an eligible slot minus the topic-support term. The
|
||||
// bridge evaluates the cheap half first and only builds a topic for slots
|
||||
// that pass it, so the two must stay in this exact relationship.
|
||||
for (int enabled = 0; enabled <= 1; ++enabled) {
|
||||
for (int topic = 0; topic <= 1; ++topic) {
|
||||
for (uint8_t type = 0; type <= 15; ++type) {
|
||||
EXPECT_EQ(Filter::slotCandidate(enabled != 0, only_advert, type) && topic != 0,
|
||||
Filter::slotEligible(enabled != 0, topic != 0, only_advert, type));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The gate has to resolve topic support before anything expensive runs: a
|
||||
// connected slot that cannot form a topic (MeshRank raw, or a meshcore preset
|
||||
// with no IATA) must reach "intentionally complete" without a serialisation.
|
||||
TEST(MQTTPacketFilter, TopicIncompatibleTargetsAreCompleteWithoutPublishing) {
|
||||
const uint16_t all = Filter::kAllPacketTypes;
|
||||
for (uint8_t type = 0; type <= 15; ++type) {
|
||||
const bool eligible = Filter::slotEligible(true, false, all, type);
|
||||
EXPECT_FALSE(eligible) << unsigned(type);
|
||||
EXPECT_TRUE(Filter::publishComplete(eligible, false)) << unsigned(type);
|
||||
}
|
||||
// With topic support restored the same slot becomes a retry target again.
|
||||
EXPECT_TRUE(Filter::slotEligible(true, true, all, 4));
|
||||
EXPECT_FALSE(Filter::publishComplete(Filter::slotEligible(true, true, all, 4), false));
|
||||
}
|
||||
|
||||
TEST(MQTTPacketFilter, EnabledUnionIgnoresDisabledSlotsAndGatesTheQueue) {
|
||||
const uint16_t masks[3] = {
|
||||
static_cast<uint16_t>(1u << 2), // text only
|
||||
static_cast<uint16_t>(1u << 4), // adverts only
|
||||
Filter::kAllPacketTypes, // everything, but disabled below
|
||||
};
|
||||
const bool enabled[3] = {true, true, false};
|
||||
|
||||
const uint16_t combined = Filter::enabledUnion(masks, enabled, 3);
|
||||
EXPECT_EQ(static_cast<uint16_t>((1u << 2) | (1u << 4)), combined);
|
||||
EXPECT_TRUE(Filter::allows(combined, 2));
|
||||
EXPECT_TRUE(Filter::allows(combined, 4));
|
||||
EXPECT_FALSE(Filter::allows(combined, 3)); // no enabled slot wants ACKs
|
||||
|
||||
// A null `enabled` array counts every slot; no slots means no destination.
|
||||
EXPECT_EQ(Filter::kAllPacketTypes, Filter::enabledUnion(masks, nullptr, 3));
|
||||
EXPECT_EQ(0u, Filter::enabledUnion(masks, enabled, 0));
|
||||
EXPECT_EQ(0u, Filter::enabledUnion(nullptr, enabled, 3));
|
||||
|
||||
// The union must never reject a type any single slot would have published.
|
||||
for (uint8_t type = 0; type <= 15; ++type) {
|
||||
const bool any_slot_allows =
|
||||
(enabled[0] && Filter::allows(masks[0], type)) ||
|
||||
(enabled[1] && Filter::allows(masks[1], type)) ||
|
||||
(enabled[2] && Filter::allows(masks[2], type));
|
||||
EXPECT_EQ(any_slot_allows, Filter::allows(combined, type)) << unsigned(type);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MQTTPacketFilter, AllMasksDefaultDrivesTheShortPrefsPayload) {
|
||||
uint16_t masks[6];
|
||||
for (int i = 0; i < 6; ++i) masks[i] = Filter::kAllPacketTypes;
|
||||
EXPECT_TRUE(Filter::allMasksDefault(masks, 6));
|
||||
EXPECT_TRUE(Filter::allMasksDefault(nullptr, 6));
|
||||
EXPECT_TRUE(Filter::allMasksDefault(masks, 0));
|
||||
|
||||
// Any slot off the default — including "none" — opts into the longer payload.
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
masks[i] = 0;
|
||||
EXPECT_FALSE(Filter::allMasksDefault(masks, 6)) << i;
|
||||
masks[i] = static_cast<uint16_t>(Filter::kAllPacketTypes & ~1u);
|
||||
EXPECT_FALSE(Filter::allMasksDefault(masks, 6)) << i;
|
||||
masks[i] = Filter::kAllPacketTypes;
|
||||
}
|
||||
EXPECT_TRUE(Filter::allMasksDefault(masks, 6));
|
||||
}
|
||||
|
||||
// countTypes() backs the diag line's fallback when the exact list won't fit,
|
||||
// so it has to agree with what format() would have emitted.
|
||||
TEST(MQTTPacketFilter, CountTypesMatchesTheFormattedList) {
|
||||
EXPECT_EQ(0, Filter::countTypes(0));
|
||||
EXPECT_EQ(16, Filter::countTypes(Filter::kAllPacketTypes));
|
||||
EXPECT_EQ(1, Filter::countTypes(static_cast<uint16_t>(1u << 15)));
|
||||
|
||||
for (uint32_t mask = 0; mask <= 0xFFFFu; ++mask) {
|
||||
uint8_t expected = 0;
|
||||
for (uint8_t type = 0; type <= 15; ++type) {
|
||||
if ((mask & (1u << type)) != 0) expected++;
|
||||
}
|
||||
ASSERT_EQ(expected, Filter::countTypes(static_cast<uint16_t>(mask))) << mask;
|
||||
}
|
||||
}
|
||||
|
||||
// The diag fallback exists because a clipped list parses as a real, different
|
||||
// allowlist. Pin that hazard so the fallback is never "simplified" away.
|
||||
TEST(MQTTPacketFilter, ClippedListStillParsesAsADifferentAllowlist) {
|
||||
const uint16_t configured = static_cast<uint16_t>(Filter::kAllPacketTypes & ~1u);
|
||||
char text[Filter::kFilterTextSize];
|
||||
ASSERT_TRUE(Filter::format(configured, text, sizeof(text)));
|
||||
ASSERT_STREQ("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15", text);
|
||||
|
||||
// Drop the trailing "15" the way a bounded reply buffer would.
|
||||
char clipped[Filter::kFilterTextSize];
|
||||
memcpy(clipped, text, sizeof(clipped));
|
||||
clipped[strlen("1,2,3,4,5,6,7,8,9,10,11,12,13,14,")] = '\0';
|
||||
|
||||
uint16_t misread = 0;
|
||||
ASSERT_FALSE(Filter::parse(clipped, &misread)); // trailing comma is rejected
|
||||
clipped[strlen("1,2,3,4,5,6,7,8,9,10,11,12,13,14")] = '\0';
|
||||
ASSERT_TRUE(Filter::parse(clipped, &misread)); // but one char earlier reads clean
|
||||
EXPECT_NE(configured, misread) << "a clipped list must not be mistaken for the real one";
|
||||
EXPECT_EQ(14, Filter::countTypes(misread));
|
||||
EXPECT_EQ(15, Filter::countTypes(configured));
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
|
||||
@@ -249,6 +249,83 @@ TEST(MQTTPrefsCodec, CurrentVersionedPayloadRoundTripsExactly) {
|
||||
EXPECT_EQ(0, memcmp(&source, &loaded, sizeof(source)));
|
||||
}
|
||||
|
||||
// /mqtt_prefs also carries the WiFi credentials, so a payload older firmware
|
||||
// rejects strands a downgraded node with no network and no way to save. The
|
||||
// filter tail is only written once it actually holds something.
|
||||
TEST(MQTTPrefsCodec, DefaultFiltersKeepTheDowngradeReadablePayloadLength) {
|
||||
MQTTPrefs source = defaults();
|
||||
strncpy(source.wifi_ssid, "home-net", sizeof(source.wifi_ssid) - 1);
|
||||
strncpy(source.mqtt_slot_preset[0], "analyzer-us", sizeof(source.mqtt_slot_preset[0]) - 1);
|
||||
|
||||
ASSERT_EQ(Codec::kV1PreFilterPayloadSize, Codec::payloadLenFor(source));
|
||||
std::vector<uint8_t> bytes(Codec::kEncodedSize, 0xEE);
|
||||
const size_t written = Codec::encode(source, bytes.data(), bytes.size());
|
||||
ASSERT_EQ(sizeof(MQTTPrefsHeader) + Codec::kV1PreFilterPayloadSize, written);
|
||||
EXPECT_LT(written, Codec::kEncodedSize);
|
||||
|
||||
MQTTPrefsHeader header;
|
||||
memcpy(&header, bytes.data(), sizeof(header));
|
||||
EXPECT_EQ(Codec::kV1PreFilterPayloadSize, header.payload_len);
|
||||
// Nothing past the boundary was touched, so the file really is the short one.
|
||||
for (size_t i = written; i < bytes.size(); ++i) {
|
||||
EXPECT_EQ(0xEE, bytes[i]) << i;
|
||||
}
|
||||
|
||||
bytes.resize(written);
|
||||
const Codec::DecodePlan plan = classify(bytes);
|
||||
ASSERT_EQ(Codec::Source::Current, plan.source);
|
||||
ASSERT_EQ(Codec::kV1PreFilterPayloadSize, plan.payload_len);
|
||||
ASSERT_FALSE(plan.preserve_file);
|
||||
|
||||
MQTTPrefs loaded = defaults();
|
||||
memcpy(&loaded, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
|
||||
EXPECT_EQ(0, memcmp(&source, &loaded, sizeof(source)));
|
||||
}
|
||||
|
||||
TEST(MQTTPrefsCodec, AnyNonDefaultFilterOptsIntoTheLongerPayload) {
|
||||
MQTTPrefs source = defaults();
|
||||
strncpy(source.wifi_ssid, "home-net", sizeof(source.wifi_ssid) - 1);
|
||||
|
||||
// "none" is as much a real setting as a subset, and must survive a reload.
|
||||
for (int slot = 0; slot < MQTT_PREFS_SLOT_COUNT; ++slot) {
|
||||
for (uint16_t mask : {static_cast<uint16_t>(0),
|
||||
static_cast<uint16_t>(1u << 4),
|
||||
static_cast<uint16_t>(MQTTPacketFilter::kAllPacketTypes & ~1u)}) {
|
||||
source.mqtt_slot_packet_filter[slot] = mask;
|
||||
ASSERT_EQ(Codec::kV1BaselinePayloadSize, Codec::payloadLenFor(source)) << slot;
|
||||
|
||||
std::vector<uint8_t> bytes(Codec::kEncodedSize);
|
||||
ASSERT_EQ(Codec::kEncodedSize, Codec::encode(source, bytes.data(), bytes.size()));
|
||||
const Codec::DecodePlan plan = classify(bytes);
|
||||
ASSERT_EQ(Codec::kV1BaselinePayloadSize, plan.payload_len);
|
||||
|
||||
MQTTPrefs loaded = defaults();
|
||||
memcpy(&loaded, bytes.data() + sizeof(MQTTPrefsHeader), plan.payload_len);
|
||||
EXPECT_EQ(mask, loaded.mqtt_slot_packet_filter[slot]) << slot;
|
||||
EXPECT_EQ(0, memcmp(&source, &loaded, sizeof(source)));
|
||||
}
|
||||
source.mqtt_slot_packet_filter[slot] = MQTTPacketFilter::kAllPacketTypes;
|
||||
}
|
||||
// Clearing the last non-default filter returns the node to the short payload.
|
||||
EXPECT_EQ(Codec::kV1PreFilterPayloadSize, Codec::payloadLenFor(source));
|
||||
}
|
||||
|
||||
TEST(MQTTPrefsCodec, EncodeRefusesAnOutputBufferShorterThanItsChosenPayload) {
|
||||
MQTTPrefs shortest = defaults();
|
||||
const size_t short_size = sizeof(MQTTPrefsHeader) + Codec::kV1PreFilterPayloadSize;
|
||||
std::vector<uint8_t> bytes(Codec::kEncodedSize);
|
||||
|
||||
EXPECT_EQ(0u, Codec::encode(shortest, bytes.data(), short_size - 1));
|
||||
EXPECT_EQ(short_size, Codec::encode(shortest, bytes.data(), short_size));
|
||||
EXPECT_EQ(0u, Codec::encode(shortest, nullptr, bytes.size()));
|
||||
|
||||
// A buffer that fits the short payload is not enough for the long one.
|
||||
MQTTPrefs longest = defaults();
|
||||
longest.mqtt_slot_packet_filter[0] = 0;
|
||||
EXPECT_EQ(0u, Codec::encode(longest, bytes.data(), short_size));
|
||||
EXPECT_EQ(Codec::kEncodedSize, Codec::encode(longest, bytes.data(), bytes.size()));
|
||||
}
|
||||
|
||||
TEST(MQTTPrefsCodec, PreFilterV1PayloadDefaultsEverySlotToAllTypes) {
|
||||
MQTTPrefs source = defaults();
|
||||
strncpy(source.mqtt_origin, "pre-filter-node", sizeof(source.mqtt_origin) - 1);
|
||||
|
||||
Reference in new issue
Block a user