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
+41
-15
@@ -289,41 +289,67 @@ Each slot (1-6) supports the following commands:
|
||||
- `set mqttN.topic <template>` - Set custom topic template (custom preset only, see below)
|
||||
- `set mqttN.audience <audience>` - Set JWT audience for custom slot (enables Ed25519 JWT auth)
|
||||
- `set mqttN.audience` - Clear JWT audience (reverts to username/password auth)
|
||||
- `set mqttN.filter <all|none|CSV>` - Select payload types uploaded to this slot
|
||||
- `set mqttN.filter <all|none|list>` - Select payload types uploaded to this slot
|
||||
|
||||
**Note:** Custom server/port settings only apply when the slot's preset is `custom`. Username/password also apply to built-in presets that use per-slot credentials (e.g. `inwmesh`); other userpass presets (`tennmesh`, `nashmesh`, `ctmesh`) ship fixed credentials in firmware.
|
||||
|
||||
#### Per-broker packet filters
|
||||
|
||||
Each slot has an independent allowlist. For example, this sends only text
|
||||
messages and adverts to slot 1 while slot 2 keeps its default of all packet
|
||||
types:
|
||||
Each slot has an independent allowlist. List entries may be payload-type names
|
||||
or numbers, and the two can be mixed. These are all equivalent, sending only
|
||||
text messages and adverts to slot 1:
|
||||
|
||||
```bash
|
||||
set mqtt1.filter txt_msg,advert
|
||||
set mqtt1.filter 2,4
|
||||
set mqtt2.filter all
|
||||
set mqtt1.filter advert, 2
|
||||
```
|
||||
|
||||
Use `none` when a broker should remain connected for status/neighbors but
|
||||
receive no packet traffic. A bare `set mqttN.filter` or an empty WebConfig
|
||||
value resets the slot to `all`.
|
||||
receive no packet traffic. A bare `set mqttN.filter` resets the slot to `all`.
|
||||
`get mqttN.filter` always answers in the canonical numeric form (`all`, `none`,
|
||||
or an ascending CSV), whichever spelling was used to set it.
|
||||
|
||||
| Type | Name | Type | Name |
|
||||
|------|------|------|------|
|
||||
| 0 | REQ | 8 | PATH |
|
||||
| 1 | RESPONSE | 9 | TRACE |
|
||||
| 2 | TXT_MSG | 10 | MULTIPART |
|
||||
| 3 | ACK | 11 | CONTROL |
|
||||
| 4 | ADVERT | 12-14 | Reserved |
|
||||
| 5 | GRP_TXT | 15 | RAW_CUSTOM |
|
||||
| 6 | GRP_DATA | | |
|
||||
| 7 | ANON_REQ | | |
|
||||
| 0 | `req` | 8 | `path` |
|
||||
| 1 | `response` | 9 | `trace` |
|
||||
| 2 | `txt_msg` | 10 | `multipart` |
|
||||
| 3 | `ack` | 11 | `control` |
|
||||
| 4 | `advert` | 12-14 | reserved (number only) |
|
||||
| 5 | `grp_txt` | 15 | `raw_custom` |
|
||||
| 6 | `grp_data` | | |
|
||||
| 7 | `anon_req` | | |
|
||||
|
||||
Names are lowercase and exact; types 12-14 are reserved upstream and have no
|
||||
name, so they are selectable by number only.
|
||||
|
||||
The filter applies to both structured `packets` and `raw` publications for RX
|
||||
packets and for TX packets permitted by `mqtt.tx`. It does not affect local
|
||||
packet processing, forwarding, capture logs, status, or neighbors. Changes
|
||||
apply live without reconnecting the broker.
|
||||
|
||||
A rejected packet is dropped before it is copied into the publish queue, so a
|
||||
narrow filter saves the queue slot and the per-packet work, not just the
|
||||
upload. `get mqtt.stats` reports the running count as `filt=<n>`, and a slot
|
||||
whose filter is not `all` shows it in `get mqttN.diag` and on the WebConfig
|
||||
Stats tab — a filtered slot otherwise looks identical to an idle healthy one.
|
||||
|
||||
The diag reply is capped at 160 characters. When a slot is also reporting a
|
||||
long error tail, the filter is summarised as `filter:<n>/16` rather than
|
||||
listed, because a list clipped mid-way would read as a different, valid
|
||||
allowlist. `get mqttN.filter` always gives the exact value.
|
||||
|
||||
In WebConfig the allowlist is a checkbox per type under each configured slot,
|
||||
with **All** / **None** shortcuts. Clearing every box is `none` (nothing
|
||||
uploaded).
|
||||
|
||||
**Downgrade note:** slots left at the `all` default keep `/mqtt_prefs` in the
|
||||
layout older firmware can read. Setting any filter — including `none` — extends
|
||||
the file so that a downgrade to pre-filter firmware falls back to defaults and
|
||||
loses the stored WiFi credentials along with the broker config. Reset every
|
||||
slot to `all` before rolling a node back over the air.
|
||||
|
||||
#### Example: Configure MeshRank on Slot 3
|
||||
```bash
|
||||
set mqtt3.preset meshrank
|
||||
|
||||
+23
-2
@@ -106,8 +106,29 @@ Adding a field to the current version stays backward compatible: append it to th
|
||||
of `MQTTPrefs`, give the older exact payload length an explicit decoder boundary, and
|
||||
leave the missing tail at its default. The packet-filter addition follows that rule:
|
||||
the prior 2864-byte v1 payload loads with all six filters set to `all`, while the
|
||||
current payload is 2876 bytes. Older firmware treats that longer same-version payload
|
||||
as unsupported and preserves it rather than loading or overwriting it.
|
||||
full payload is 2876 bytes.
|
||||
|
||||
#### Why the written length is not always the full length
|
||||
|
||||
Reading a longer payload is the easy direction; writing one is a one-way door.
|
||||
Pre-filter firmware classifies a 2876-byte v1 payload as `UnsupportedVersion`, so
|
||||
after a downgrade the node boots on *defaults* and `_mqtt_prefs_hold` blocks every
|
||||
save. `/mqtt_prefs` holds `wifi_ssid`/`wifi_password` as well as the broker config, so
|
||||
that node comes up with no network at all: no portal, no OTA, no way back except
|
||||
serial or a re-flash forward. The same is true of every previous tail (observer,
|
||||
neighbors) — each append moved the cliff.
|
||||
|
||||
`MQTTPrefsCodec::payloadLenFor()` narrows the blast radius instead of widening it. It
|
||||
returns the *shortest* length that still round-trips the configuration, which for the
|
||||
packet-filter tail means: keep writing 2864 while all six masks are the all-types
|
||||
default, and only write 2876 once a filter actually holds something. A node that
|
||||
upgrades and never touches a filter therefore stays downgrade-readable, and clearing
|
||||
the last non-default filter puts it back. Opting in is an explicit operator action,
|
||||
not a side effect of installing a build.
|
||||
|
||||
The rule generalises to the next appended field: give it a default that a missing tail
|
||||
already implies, and extend `payloadLenFor()` so the longer payload is only written
|
||||
when the field is set. That keeps every append reversible for the fleet majority.
|
||||
|
||||
### Settings upgrade / migration
|
||||
|
||||
|
||||
@@ -1515,9 +1515,17 @@ void MyMesh::buildStatsJson(char* buf, size_t buf_size) {
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
MQTTBridge::SlotStatusSnapshot s;
|
||||
if (!MQTTBridge::getSlotStatusSnapshot(i, &s)) continue;
|
||||
// "filt" is omitted for the all-types default, so the portal only has to
|
||||
// render the exception and the JSON stays inside the stats buffer.
|
||||
char filt[24];
|
||||
filt[0] = '\0';
|
||||
if (s.filter_mask != MQTTPacketFilter::kAllPacketTypes) {
|
||||
snprintf(filt, sizeof(filt), ",\"filt\":%u", (unsigned)s.filter_mask);
|
||||
}
|
||||
int n = snprintf(buf + pos, buf_size - pos,
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\",\"ok\":%lu,\"err\":%lu}",
|
||||
first ? "" : ",", i + 1, s.name, s.state, s.publish_ok, s.publish_err);
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\",\"ok\":%lu,\"err\":%lu%s}",
|
||||
first ? "" : ",", i + 1, s.name, s.state, s.publish_ok, s.publish_err,
|
||||
filt);
|
||||
if (n < 0 || n >= (int)(buf_size - pos)) break;
|
||||
pos += n;
|
||||
first = false;
|
||||
|
||||
@@ -1310,9 +1310,17 @@ void MyMesh::buildStatsJson(char* buf, size_t buf_size) {
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
MQTTBridge::SlotStatusSnapshot s;
|
||||
if (!MQTTBridge::getSlotStatusSnapshot(i, &s)) continue;
|
||||
// "filt" is omitted for the all-types default, so the portal only has to
|
||||
// render the exception and the JSON stays inside the stats buffer.
|
||||
char filt[24];
|
||||
filt[0] = '\0';
|
||||
if (s.filter_mask != MQTTPacketFilter::kAllPacketTypes) {
|
||||
snprintf(filt, sizeof(filt), ",\"filt\":%u", (unsigned)s.filter_mask);
|
||||
}
|
||||
int n = snprintf(buf + pos, buf_size - pos,
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\",\"ok\":%lu,\"err\":%lu}",
|
||||
first ? "" : ",", i + 1, s.name, s.state, s.publish_ok, s.publish_err);
|
||||
"%s{\"n\":%d,\"name\":\"%s\",\"state\":\"%s\",\"ok\":%lu,\"err\":%lu%s}",
|
||||
first ? "" : ",", i + 1, s.name, s.state, s.publish_ok, s.publish_err,
|
||||
filt);
|
||||
if (n < 0 || n >= (int)(buf_size - pos)) break;
|
||||
pos += n;
|
||||
first = false;
|
||||
|
||||
@@ -47,8 +47,16 @@ LEN_LIMITS = {
|
||||
"name": 31, "wifi.ssid": 31, "wifi.pwd": 63, "mqtt.origin": 31,
|
||||
"mqtt.email": 63, "mqtt.ntp": 63, "timezone": 31, "snmp.community": 23,
|
||||
}
|
||||
# "filter" is absent on purpose: it is a bitmask, not a text buffer, so it has
|
||||
# no destination-buffer limit. It is still bounded by the shared CLI command
|
||||
# budget below, like every other key.
|
||||
SLOT_LEN_LIMITS = {"server": 63, "username": 31, "password": 63,
|
||||
"token": 47, "topic": 95, "audience": 63, "filter": 37}
|
||||
"token": 47, "topic": 95, "audience": 63}
|
||||
|
||||
# BatchEntry::cmd[160] in WebConfigServer.cpp holds "set <key> <value>" plus a
|
||||
# NUL. Over-long values are rejected there rather than truncated, because a
|
||||
# clipped value can still be valid and would persist as a different setting.
|
||||
BATCH_CMD_SIZE = 160
|
||||
|
||||
# Preset names + what the UI must collect (mirrors handlePresets()).
|
||||
PRESETS = (
|
||||
@@ -254,7 +262,30 @@ def apply_set(cfg, key, val):
|
||||
return True, "OK" # unknown-but-allowlisted: accept (mock is lenient here)
|
||||
|
||||
|
||||
# Payload-type names accepted alongside the decimal form. Mirrors
|
||||
# namedPacketTypes() in src/helpers/MQTTPacketFilter.h; 12-14 are reserved
|
||||
# upstream and stay reachable by number only.
|
||||
PACKET_TYPE_NAMES = {
|
||||
"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,
|
||||
}
|
||||
|
||||
|
||||
def packet_filter_mask(text):
|
||||
"""Canonical filter text -> bitmask, for the stats payload."""
|
||||
if text == "all":
|
||||
return 0xFFFF
|
||||
if text == "none":
|
||||
return 0
|
||||
mask = 0
|
||||
for token in text.split(","):
|
||||
mask |= 1 << int(token)
|
||||
return mask
|
||||
|
||||
|
||||
def canonical_packet_filter(val):
|
||||
"""Mirror of MQTTPacketFilter::parse + ::format. Returns None if invalid."""
|
||||
stripped = val.strip()
|
||||
if stripped == "" or stripped == "all":
|
||||
return "all"
|
||||
@@ -263,10 +294,13 @@ def canonical_packet_filter(val):
|
||||
mask = 0
|
||||
for part in stripped.split(","):
|
||||
token = part.strip()
|
||||
if not re.fullmatch(r"[0-9]+", token):
|
||||
return None
|
||||
packet_type = int(token)
|
||||
if packet_type > 15:
|
||||
if re.fullmatch(r"[0-9]+", token):
|
||||
packet_type = int(token)
|
||||
if packet_type > 15:
|
||||
return None
|
||||
elif token in PACKET_TYPE_NAMES:
|
||||
packet_type = PACKET_TYPE_NAMES[token]
|
||||
else:
|
||||
return None
|
||||
mask |= 1 << packet_type
|
||||
if mask == 0xFFFF:
|
||||
@@ -290,7 +324,8 @@ def apply_slot_set(cfg, idx, field, val):
|
||||
if field == "filter":
|
||||
canonical = canonical_packet_filter(val)
|
||||
if canonical is None:
|
||||
return False, "Error: filter must be all, none, or CSV packet types 0-15"
|
||||
return False, ("Error: filter must be all, none, or a CSV of "
|
||||
"types 0-15 / names (advert,txt_msg,...)")
|
||||
slot["filter"] = canonical
|
||||
return True, "OK - slot %d packet types: %s" % (idx + 1, canonical)
|
||||
if field in ("preset", "server", "username", "password", "token", "topic", "audience"):
|
||||
@@ -451,6 +486,13 @@ class Handler(BaseHTTPRequestHandler):
|
||||
# drop unchanged secrets (sentinel), like the firmware does
|
||||
entries = [(k, v) for k, v in setmap.items()
|
||||
if not (is_secret_key(k) and v == SENTINEL)]
|
||||
# Same command-budget rejection the firmware applies while building
|
||||
# BatchEntry::cmd; CR/LF are stripped there and don't count.
|
||||
for k, v in entries:
|
||||
prefix = "password " if k == "password" else "set %s " % k
|
||||
stripped = str(v).replace("\r", "").replace("\n", "")
|
||||
if len(prefix) + len(stripped) > BATCH_CMD_SIZE - 1:
|
||||
return self._json(400, {"error": "value too long", "key": k[:32]})
|
||||
if not entries and not reboot:
|
||||
return self._json(400, {"error": "no changes"})
|
||||
# apply now, but expose as pending->done to exercise polling
|
||||
@@ -500,8 +542,14 @@ class Handler(BaseHTTPRequestHandler):
|
||||
for i, s in enumerate(ST.cfg["mqtt"]["slots"]):
|
||||
if s["preset"] == "none":
|
||||
continue
|
||||
slots.append({"n": i + 1, "name": s["preset"], "state": "ok",
|
||||
"ok": 100 + up, "err": 0})
|
||||
row = {"n": i + 1, "name": s["preset"], "state": "ok",
|
||||
"ok": 100 + up, "err": 0}
|
||||
# Mirrors buildStatsJson(): "filt" carries the raw mask and is
|
||||
# omitted entirely for the all-types default.
|
||||
mask = packet_filter_mask(s.get("filter", "all"))
|
||||
if mask != 0xFFFF:
|
||||
row["filt"] = mask
|
||||
slots.append(row)
|
||||
return {
|
||||
"uptime_s": up, "batt_mv": 4020, "heap_free": 142000, "heap_min": 118000,
|
||||
"heap_max_alloc": 96000, "noise": -98, "rssi": -71, "snr": 9.5,
|
||||
|
||||
@@ -944,12 +944,15 @@ bool CommonCLI::saveMQTTPrefs(FILESYSTEM* fs) {
|
||||
}
|
||||
|
||||
// Write header and payload sequentially so the transaction needs no second
|
||||
// full-size (2.8 KiB) staging buffer on constrained targets.
|
||||
const MQTTPrefsHeader header = MQTTPrefsCodec::makeHeader();
|
||||
// full-size (2.8 KiB) staging buffer on constrained targets. The length is
|
||||
// the shortest that still round-trips this config, so a node with default
|
||||
// packet filters keeps writing a payload older firmware can read.
|
||||
const size_t payload_len = MQTTPrefsCodec::payloadLenFor(_mqtt_prefs);
|
||||
const MQTTPrefsHeader header = MQTTPrefsCodec::makeHeader(payload_len);
|
||||
MQTTPrefsFileStore store(fs);
|
||||
const MQTTPrefsAtomicStore::Result result = MQTTPrefsAtomicStore::write(
|
||||
store, (const uint8_t *)&header, sizeof(header),
|
||||
(const uint8_t *)&_mqtt_prefs, sizeof(_mqtt_prefs));
|
||||
(const uint8_t *)&_mqtt_prefs, payload_len);
|
||||
if (!MQTTPrefsAtomicStore::committed(result)) {
|
||||
MESH_DEBUG_PRINTLN("MQTT: atomic /mqtt_prefs save failed at %s; source preserved",
|
||||
mqttPrefsSaveResultName(result));
|
||||
|
||||
@@ -536,13 +536,25 @@ bool CommonCLI::handleObserverSetCmd(uint32_t sender_timestamp, const char* conf
|
||||
const char* filter_value = subcmd[6] == '\0' ? "" : &subcmd[7];
|
||||
uint16_t filter_mask = 0;
|
||||
if (!MQTTPacketFilter::parse(filter_value, &filter_mask)) {
|
||||
strcpy(reply, "Error: filter must be all, none, or CSV packet types 0-15");
|
||||
strcpy(reply, "Error: filter must be all, none, or a CSV of types 0-15 / names (advert,txt_msg,...)");
|
||||
} else {
|
||||
_mqtt_prefs.mqtt_slot_packet_filter[slot] = filter_mask;
|
||||
savePrefs();
|
||||
char filter_text[MQTTPacketFilter::kFilterTextSize];
|
||||
MQTTPacketFilter::format(filter_mask, filter_text, sizeof(filter_text));
|
||||
snprintf(reply, 160, "OK - slot %d packet types: %s", slot + 1, filter_text);
|
||||
// A non-default filter extends /mqtt_prefs past what pre-filter
|
||||
// firmware can read (see MQTTPrefsCodec::payloadLenFor), so say when
|
||||
// that cost buys nothing: slots beyond the runtime array are never
|
||||
// published to on this board, the same warning `preset` gives.
|
||||
if (slot >= RUNTIME_MQTT_SLOTS &&
|
||||
filter_mask != MQTTPacketFilter::kAllPacketTypes) {
|
||||
size_t used = strlen(reply);
|
||||
if (used < 158) {
|
||||
snprintf(reply + used, 160 - used,
|
||||
" (slot inactive on this hardware; blocks firmware rollback)");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sprintf(reply, "unknown config: %s", config);
|
||||
|
||||
+123
-23
@@ -23,11 +23,58 @@ inline bool tokenEquals(const char* begin, size_t len, const char* token) {
|
||||
return token != nullptr && strlen(token) == len && memcmp(begin, token, len) == 0;
|
||||
}
|
||||
|
||||
// Parse a canonical allowlist value. Empty input means "all" so WebConfig can
|
||||
// clear a field and retain the same backwards-compatible default as an older
|
||||
// /mqtt_prefs file. Keywords are deliberately lowercase and cannot be mixed
|
||||
// with numeric entries. Decimal entries may contain surrounding ASCII
|
||||
// whitespace, must be in 0..15, and may be repeated.
|
||||
// Payload-type spellings accepted alongside the decimal form, mirroring the
|
||||
// PAYLOAD_TYPE_* names in src/Packet.h. Types 12-14 are reserved upstream and
|
||||
// have no name, so they stay reachable only by number.
|
||||
struct NamedPacketType {
|
||||
const char* name;
|
||||
uint8_t type;
|
||||
};
|
||||
|
||||
inline const NamedPacketType* namedPacketTypes(size_t* count_out) {
|
||||
static const NamedPacketType kNames[] = {
|
||||
{"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},
|
||||
};
|
||||
if (count_out != nullptr) *count_out = sizeof(kNames) / sizeof(kNames[0]);
|
||||
return kNames;
|
||||
}
|
||||
|
||||
// Resolve one already-trimmed list entry to a payload type. Decimal and named
|
||||
// spellings are interchangeable within a list; "all"/"none" are whole-value
|
||||
// keywords and deliberately do not resolve here, so "all,2" stays invalid.
|
||||
inline bool resolveToken(const char* begin, size_t len, uint8_t* type_out) {
|
||||
if (begin == nullptr || type_out == nullptr || len == 0) return false;
|
||||
|
||||
if (*begin >= '0' && *begin <= '9') {
|
||||
unsigned value = 0;
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
if (begin[i] < '0' || begin[i] > '9') return false;
|
||||
value = value * 10u + static_cast<unsigned>(begin[i] - '0');
|
||||
if (value > kMaxPacketType) return false;
|
||||
}
|
||||
*type_out = static_cast<uint8_t>(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t name_count = 0;
|
||||
const NamedPacketType* names = namedPacketTypes(&name_count);
|
||||
for (size_t i = 0; i < name_count; ++i) {
|
||||
if (tokenEquals(begin, len, names[i].name)) {
|
||||
*type_out = names[i].type;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse an allowlist value. Empty input means "all" so WebConfig can clear a
|
||||
// field and retain the same backwards-compatible default as an older
|
||||
// /mqtt_prefs file. Keywords and type names are deliberately lowercase; "all"
|
||||
// and "none" cannot be mixed into a list. Entries may be decimal (0..15) or
|
||||
// named, may carry surrounding ASCII whitespace, and may be repeated.
|
||||
inline bool parse(const char* input, uint16_t* mask_out) {
|
||||
if (input == nullptr || mask_out == nullptr) return false;
|
||||
|
||||
@@ -48,23 +95,23 @@ inline bool parse(const char* input, uint16_t* mask_out) {
|
||||
|
||||
uint16_t parsed = 0;
|
||||
const char* cursor = begin;
|
||||
while (cursor < end) {
|
||||
while (cursor < end && isAsciiSpace(*cursor)) cursor++;
|
||||
if (cursor >= end || *cursor < '0' || *cursor > '9') return false;
|
||||
for (;;) {
|
||||
const char* comma = cursor;
|
||||
while (comma < end && *comma != ',') comma++;
|
||||
|
||||
unsigned value = 0;
|
||||
while (cursor < end && *cursor >= '0' && *cursor <= '9') {
|
||||
value = value * 10u + static_cast<unsigned>(*cursor - '0');
|
||||
if (value > kMaxPacketType) return false;
|
||||
cursor++;
|
||||
const char* token_begin = cursor;
|
||||
const char* token_end = comma;
|
||||
while (token_begin < token_end && isAsciiSpace(*token_begin)) token_begin++;
|
||||
while (token_end > token_begin && isAsciiSpace(token_end[-1])) token_end--;
|
||||
|
||||
uint8_t type = 0;
|
||||
if (!resolveToken(token_begin, static_cast<size_t>(token_end - token_begin), &type)) {
|
||||
return false;
|
||||
}
|
||||
while (cursor < end && isAsciiSpace(*cursor)) cursor++;
|
||||
parsed |= static_cast<uint16_t>(1u << type);
|
||||
|
||||
parsed |= static_cast<uint16_t>(1u << value);
|
||||
if (cursor == end) break;
|
||||
if (*cursor != ',') return false;
|
||||
cursor++;
|
||||
if (cursor == end) return false;
|
||||
if (comma >= end) break;
|
||||
cursor = comma + 1;
|
||||
}
|
||||
|
||||
*mask_out = parsed;
|
||||
@@ -104,17 +151,70 @@ inline bool format(uint16_t mask, char* output, size_t output_size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// The keyword shortcut above means format() never actually emits the whole
|
||||
// 0..15 list, but the emit loop is written for every type, so the internal
|
||||
// buffer must still hold it: 22 digits + 15 separators + NUL.
|
||||
static_assert(kFilterTextSize >= 38, "filter text buffer must hold the full 0..15 list");
|
||||
|
||||
inline bool allows(uint16_t mask, uint8_t packet_type) {
|
||||
return packet_type <= kMaxPacketType &&
|
||||
(mask & static_cast<uint16_t>(1u << packet_type)) != 0;
|
||||
}
|
||||
|
||||
// Eligibility deliberately excludes connection state. A temporarily
|
||||
// disconnected broker that is configured for this type still requires the
|
||||
// shared queue's existing bounded retry policy.
|
||||
// How many types a mask allows. Lets a length-constrained reply fall back to
|
||||
// "N/16" instead of a clipped list, which would read as a different allowlist.
|
||||
inline uint8_t countTypes(uint16_t mask) {
|
||||
uint8_t total = 0;
|
||||
for (uint8_t type = kMinPacketType; type <= kMaxPacketType; ++type) {
|
||||
if ((mask & static_cast<uint16_t>(1u << type)) != 0) total++;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
// Conservative "could any configured broker want this type?" mask, used to
|
||||
// reject a packet before it is copied into the publish queue. Disabled slots
|
||||
// contribute nothing; a slot whose topic style later turns out not to support
|
||||
// the publication is still counted here, so the gate never drops a packet the
|
||||
// per-slot pass would have published.
|
||||
inline uint16_t enabledUnion(const uint16_t* masks, const bool* enabled, size_t count) {
|
||||
if (masks == nullptr) return 0;
|
||||
uint16_t combined = 0;
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
if (enabled != nullptr && !enabled[i]) continue;
|
||||
combined = static_cast<uint16_t>(combined | masks[i]);
|
||||
}
|
||||
return combined;
|
||||
}
|
||||
|
||||
// True when every slot still carries the default all-types mask, i.e. nothing
|
||||
// depends on the packet-filter tail of /mqtt_prefs being written.
|
||||
inline bool allMasksDefault(const uint16_t* masks, size_t count) {
|
||||
if (masks == nullptr) return true;
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
if (masks[i] != kAllPacketTypes) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Stage one of the publish gate: everything decidable before a packet is
|
||||
// serialised or a topic is built. Cheap enough to run per packet per slot.
|
||||
inline bool slotCandidate(bool slot_enabled, uint16_t mask, uint8_t packet_type) {
|
||||
return slot_enabled && allows(mask, packet_type);
|
||||
}
|
||||
|
||||
// The complete gate, once the slot's topic support is known. Eligibility
|
||||
// deliberately excludes connection state: a temporarily disconnected broker
|
||||
// that is configured for this type still requires the shared queue's existing
|
||||
// bounded retry policy.
|
||||
//
|
||||
// eligiblePacketSlots() calls this directly, using slotCandidate() first only
|
||||
// to skip the topic build for slots the mask already rejects. A slot that
|
||||
// passes both therefore has its topic built again in the publish loop; that
|
||||
// second build is the accepted cost of not carrying six 128-byte topics on the
|
||||
// MQTT task stack.
|
||||
inline bool slotEligible(bool slot_enabled, bool topic_supported,
|
||||
uint16_t mask, uint8_t packet_type) {
|
||||
return slot_enabled && topic_supported && allows(mask, packet_type);
|
||||
return slotCandidate(slot_enabled, mask, packet_type) && topic_supported;
|
||||
}
|
||||
|
||||
// A fully filtered/topic-incompatible packet is intentionally complete. Once
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "MQTTPacketFilter.h"
|
||||
#include "MQTTPrefsStorage.h"
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
@@ -41,20 +42,40 @@ static const size_t kV1PreFilterPayloadSize = MQTT_PREFS_V1_PRE_FILTER_PAYLOAD_S
|
||||
static const size_t kV1BaselinePayloadSize = MQTT_PREFS_V1_FULL_PAYLOAD_SIZE;
|
||||
static const size_t kEncodedSize = sizeof(MQTTPrefsHeader) + kV1BaselinePayloadSize;
|
||||
|
||||
inline MQTTPrefsHeader makeHeader() {
|
||||
// Shortest payload length that still round-trips this configuration.
|
||||
//
|
||||
// The packet-filter tail is the only optional part of the current layout, and
|
||||
// its default (all types) is exactly what a pre-filter decoder supplies for a
|
||||
// missing tail. So a device whose filters are all default keeps writing the
|
||||
// 2864-byte payload that pre-filter firmware can still read. That matters
|
||||
// because /mqtt_prefs also carries the WiFi credentials: an unrecognised
|
||||
// longer payload sends older firmware to defaults with no network, and it
|
||||
// refuses to overwrite the file, so the node cannot be recovered over the air.
|
||||
// Touching any filter opts that node into the longer payload — a deliberate,
|
||||
// operator-initiated trade rather than a side effect of upgrading.
|
||||
inline size_t payloadLenFor(const MQTTPrefs& prefs) {
|
||||
return MQTTPacketFilter::allMasksDefault(prefs.mqtt_slot_packet_filter,
|
||||
MQTT_PREFS_SLOT_COUNT)
|
||||
? kV1PreFilterPayloadSize
|
||||
: kV1BaselinePayloadSize;
|
||||
}
|
||||
|
||||
inline MQTTPrefsHeader makeHeader(size_t payload_len) {
|
||||
MQTTPrefsHeader header;
|
||||
memcpy(header.magic, MQTT_PREFS_MAGIC, sizeof(header.magic));
|
||||
header.version = MQTT_PREFS_VERSION;
|
||||
header.payload_len = static_cast<uint16_t>(kV1BaselinePayloadSize);
|
||||
header.payload_len = static_cast<uint16_t>(payload_len);
|
||||
return header;
|
||||
}
|
||||
|
||||
inline size_t encode(const MQTTPrefs& prefs, uint8_t* output, size_t output_size) {
|
||||
if (output == nullptr || output_size < kEncodedSize) return 0;
|
||||
const MQTTPrefsHeader header = makeHeader();
|
||||
const size_t payload_len = payloadLenFor(prefs);
|
||||
const size_t encoded_size = sizeof(MQTTPrefsHeader) + payload_len;
|
||||
if (output == nullptr || output_size < encoded_size) return 0;
|
||||
const MQTTPrefsHeader header = makeHeader(payload_len);
|
||||
memcpy(output, &header, sizeof(header));
|
||||
memcpy(output + sizeof(header), &prefs, sizeof(prefs));
|
||||
return kEncodedSize;
|
||||
memcpy(output + sizeof(header), &prefs, payload_len);
|
||||
return encoded_size;
|
||||
}
|
||||
|
||||
inline bool isMagicPrefix(const uint8_t* input, size_t available) {
|
||||
|
||||
@@ -140,6 +140,11 @@ static const uint32_t MQTT_NEIGHBORS_DEFAULT_INTERVAL_MS = MQTT_NEIGHBORS_DEFAUL
|
||||
// - PRE_NEIGHBORS (2860): full observer tail, no neighbors fields yet.
|
||||
// - PRE_FILTER (2864): neighbors tail, no per-slot packet filters.
|
||||
// - FULL (2876): current baseline, with six uint16_t filter masks.
|
||||
//
|
||||
// FULL is the maximum written, not the default: MQTTPrefsCodec::payloadLenFor()
|
||||
// keeps emitting PRE_FILTER while every slot holds the all-types default, so a
|
||||
// node that never touches a filter stays readable by pre-filter firmware. See
|
||||
// the rollback note there — /mqtt_prefs also carries the WiFi credentials.
|
||||
static const size_t MQTT_PREFS_V1_PRE_OBSERVER_PAYLOAD_SIZE = 2736;
|
||||
static const size_t MQTT_PREFS_V1_PRE_NEIGHBORS_PAYLOAD_SIZE = 2860;
|
||||
static const size_t MQTT_PREFS_V1_PRE_FILTER_PAYLOAD_SIZE = 2864;
|
||||
|
||||
@@ -330,10 +330,17 @@ void MQTTBridge::formatMqttStatsReply(char* buf, size_t bufsize) {
|
||||
|
||||
// drops=<outbox>/<skipped>: outbox-cap drops vs. memory-pressure skips.
|
||||
int pos = 0;
|
||||
replyAppendf(buf, bufsize, &pos, "> Free=%d Max=%d q:%d/%d Outbox=%u drops=%lu/%d |",
|
||||
replyAppendf(buf, bufsize, &pos, "> Free=%d Max=%d q:%d/%d Outbox=%u drops=%lu/%d",
|
||||
(int)ESP.getFreeHeap(), (int)ESP.getMaxAllocHeap(),
|
||||
q, MAX_QUEUE_SIZE, (unsigned)outbox_total,
|
||||
outbox_drops, b->_skipped_publishes);
|
||||
// filt=<n>: packets the per-slot type filters rejected before the queue.
|
||||
// Omitted while zero so an unfiltered node's reply keeps its former length —
|
||||
// the per-slot list below is what usually gets clamped away first.
|
||||
if (b->_filtered_packets > 0) {
|
||||
replyAppendf(buf, bufsize, &pos, " filt=%lu", b->_filtered_packets);
|
||||
}
|
||||
replyAppendf(buf, bufsize, &pos, " |");
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
if (!b->_slots[i].enabled || !b->_slots[i].client) continue;
|
||||
replyAppendf(buf, bufsize, &pos, " s%d=%lu/%lu", i + 1,
|
||||
@@ -370,6 +377,9 @@ bool MQTTBridge::getSlotStatusSnapshot(int slot_index, SlotStatusSnapshot* out)
|
||||
}
|
||||
out->publish_ok = slot.client ? slot.client->getPublishOk() : 0;
|
||||
out->publish_err = slot.client ? slot.client->getPublishErr() : 0;
|
||||
// Lets the portal show why a healthy slot is quiet.
|
||||
out->filter_mask = b->_obs ? b->_obs->mqtt_slot_packet_filter[slot_index]
|
||||
: MQTTPacketFilter::kAllPacketTypes;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -476,6 +486,17 @@ void MQTTBridge::formatSlotDiagReply(char* buf, size_t bufsize, int slot_index)
|
||||
|
||||
// replyAppendf clamps pos on every call, so the chained appends below can't
|
||||
// walk past the reply buffer even if the accumulated text exceeds it (A1).
|
||||
// A non-default filter is the one healthy-looking reason for a slot to stop
|
||||
// publishing, so it has to appear — but appended last. replyAppendf clamps at
|
||||
// the 160-byte reply, and an error tail (TLS + mbedTLS + errno + age) can
|
||||
// already reach ~117 chars, so putting the filter first would push the
|
||||
// operator's diagnostic detail off the end of a failing slot's line.
|
||||
const uint16_t filter_mask = b->_obs ? b->_obs->mqtt_slot_packet_filter[slot_index]
|
||||
: MQTTPacketFilter::kAllPacketTypes;
|
||||
char filter_text[MQTTPacketFilter::kFilterTextSize];
|
||||
const bool show_filter = filter_mask != MQTTPacketFilter::kAllPacketTypes &&
|
||||
MQTTPacketFilter::format(filter_mask, filter_text, sizeof(filter_text));
|
||||
|
||||
int pos = 0;
|
||||
replyAppendf(buf, bufsize, &pos, "> mqtt%d: %s", slot_index + 1, state);
|
||||
if (slot.disconnect_count > 0) {
|
||||
@@ -486,14 +507,10 @@ void MQTTBridge::formatSlotDiagReply(char* buf, size_t bufsize, int slot_index)
|
||||
}
|
||||
}
|
||||
|
||||
// If connected with no errors, we're done
|
||||
// Connected with no errors: nothing more to say about the connection.
|
||||
if (slot.connected && slot.last_error_time == 0) {
|
||||
replyAppendf(buf, bufsize, &pos, ", no errors");
|
||||
return;
|
||||
}
|
||||
|
||||
// Show last error if we have one
|
||||
if (slot.last_error_time > 0) {
|
||||
} else if (slot.last_error_time > 0) {
|
||||
// TLS error with human-friendly description
|
||||
if (slot.last_tls_err != 0) {
|
||||
const char* desc = tlsErrorStr(slot.last_tls_err);
|
||||
@@ -523,6 +540,26 @@ void MQTTBridge::formatSlotDiagReply(char* buf, size_t bufsize, int slot_index)
|
||||
} else if (!slot.connected) {
|
||||
replyAppendf(buf, bufsize, &pos, ", no error info");
|
||||
}
|
||||
|
||||
// Appended last so it never displaces connection diagnostics. replyAppendf
|
||||
// clamps rather than overflows, but a clipped type list is worse than no
|
||||
// list: "…,13,14," parses as a real, different allowlist, and this is the
|
||||
// one line an operator reads to find out why a slot is quiet. So the exact
|
||||
// text is only emitted when it fits whole; otherwise fall back to a count,
|
||||
// which cannot be misread. `get mqttN.filter` always has the exact value.
|
||||
if (show_filter) {
|
||||
static const int kFilterLabelLen = (int)sizeof(", filter:") - 1;
|
||||
const int remaining = pos < (int)bufsize ? (int)bufsize - 1 - pos : 0;
|
||||
const uint8_t allowed = MQTTPacketFilter::countTypes(filter_mask);
|
||||
const int compact_len = kFilterLabelLen + (allowed >= 10 ? 5 : 4); // "N/16"
|
||||
if (kFilterLabelLen + (int)strlen(filter_text) <= remaining) {
|
||||
replyAppendf(buf, bufsize, &pos, ", filter:%s", filter_text);
|
||||
} else if (compact_len <= remaining) {
|
||||
replyAppendf(buf, bufsize, &pos, ", filter:%u/16", (unsigned)allowed);
|
||||
}
|
||||
// Neither fits: the slot is reporting so much error detail that the filter
|
||||
// is the least useful field on the line. Omit it rather than mislead.
|
||||
}
|
||||
}
|
||||
|
||||
// Bounded cooperative-stop timeout for end() (see MQTTLifecycle::Coordinator).
|
||||
@@ -2699,15 +2736,16 @@ void MQTTBridge::loop() {
|
||||
void MQTTBridge::onPacketReceived(mesh::Packet *packet) {
|
||||
if (!_initialized || !_obs->mqtt_packets_enabled || !_obs->mqtt_rx_enabled) return;
|
||||
|
||||
// Check if we have any enabled slots to send to
|
||||
bool has_valid_slots = false;
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
if (_slots[i].enabled && _slots[i].client) {
|
||||
has_valid_slots = true;
|
||||
break;
|
||||
}
|
||||
// Drop before the queue copy when no configured slot allows this payload
|
||||
// type. A QueuedPacket carries the packet plus up to 256 bytes of raw radio
|
||||
// data and crosses to Core 0, so filtering here (not at publish time) is
|
||||
// what actually saves the queue slot and the memcpy. This also subsumes the
|
||||
// older "is any slot configured at all?" pre-check.
|
||||
bool filtered = false;
|
||||
if (!shouldQueuePacketType(packet->getPayloadType(), filtered)) {
|
||||
if (filtered) _filtered_packets++;
|
||||
return;
|
||||
}
|
||||
if (!has_valid_slots) return;
|
||||
|
||||
// Queue packet for transmission
|
||||
queuePacket(packet, false);
|
||||
@@ -2725,6 +2763,13 @@ void MQTTBridge::sendPacket(mesh::Packet *packet) {
|
||||
if (!_identity || memcmp(_identity->pub_key, packet->payload, PUB_KEY_SIZE) != 0) return;
|
||||
}
|
||||
|
||||
// Same pre-queue filter gate as the RX path.
|
||||
bool filtered = false;
|
||||
if (!shouldQueuePacketType(packet->getPayloadType(), filtered)) {
|
||||
if (filtered) _filtered_packets++;
|
||||
return;
|
||||
}
|
||||
|
||||
// Queue packet for transmission
|
||||
queuePacket(packet, true);
|
||||
}
|
||||
@@ -3021,8 +3066,21 @@ void MQTTBridge::processPacketQueue() {
|
||||
// Publishing
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Which slots will actually take this payload type, resolved in two stages so
|
||||
// the cheap test runs first: the filter/enabled check costs a bit test, while
|
||||
// the topic build costs an snprintf. Both are far cheaper than the ~2 KB JSON
|
||||
// serialisation they gate, so this must complete before a caller builds a
|
||||
// message — a slot that cannot form a topic (MeshRank raw, or a meshcore
|
||||
// preset with no IATA set) would otherwise pay for a document nobody receives.
|
||||
//
|
||||
// A slot's connection state is deliberately not consulted: a configured but
|
||||
// disconnected broker is still a target, so the packet stays on the queue for
|
||||
// the existing bounded retry rather than being dropped as complete.
|
||||
uint8_t MQTTBridge::eligiblePacketSlots(uint8_t packet_type, MQTTMessageType type) {
|
||||
static_assert(RUNTIME_MQTT_SLOTS <= 8, "eligible slot mask must fit in uint8_t");
|
||||
// Per-slot filters cover packet traffic only. Status and neighbors are
|
||||
// documented as never filtered, so refuse those publication types here
|
||||
// rather than silently applying a packet mask if a caller is added later.
|
||||
if (!_obs || (type != MSG_PACKETS && type != MSG_RAW)) return 0;
|
||||
|
||||
uint8_t eligible_slots = 0;
|
||||
@@ -3030,9 +3088,9 @@ uint8_t MQTTBridge::eligiblePacketSlots(uint8_t packet_type, MQTTMessageType typ
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; ++i) {
|
||||
const bool slot_enabled = _slots[i].enabled && _slots[i].client != nullptr;
|
||||
// Load once so a live CLI/WebConfig update cannot split this packet's
|
||||
// eligibility decision across two different masks.
|
||||
// decision across two different masks.
|
||||
const uint16_t filter_mask = _obs->mqtt_slot_packet_filter[i];
|
||||
if (!slot_enabled || !MQTTPacketFilter::allows(filter_mask, packet_type)) continue;
|
||||
if (!MQTTPacketFilter::slotCandidate(slot_enabled, filter_mask, packet_type)) continue;
|
||||
|
||||
const bool topic_supported = buildTopicForSlot(i, type, topic, sizeof(topic));
|
||||
if (MQTTPacketFilter::slotEligible(slot_enabled, topic_supported,
|
||||
@@ -3043,6 +3101,34 @@ uint8_t MQTTBridge::eligiblePacketSlots(uint8_t packet_type, MQTTMessageType typ
|
||||
return eligible_slots;
|
||||
}
|
||||
|
||||
// Conservative pre-queue gate: the OR of every configured slot's mask. Runs on
|
||||
// Core 1 in the radio callback, so it must stay cheap — a packet no broker
|
||||
// wants is rejected before it is copied into the queue at all. `filtered`
|
||||
// separates "a broker is configured but none wants this type" (worth counting)
|
||||
// from "no broker configured at all" (the pre-existing silent drop).
|
||||
bool MQTTBridge::shouldQueuePacketType(uint8_t packet_type, bool& filtered) {
|
||||
filtered = false;
|
||||
if (!_obs) return false;
|
||||
|
||||
uint16_t masks[RUNTIME_MQTT_SLOTS];
|
||||
bool enabled[RUNTIME_MQTT_SLOTS];
|
||||
bool any_enabled = false;
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; ++i) {
|
||||
masks[i] = _obs->mqtt_slot_packet_filter[i];
|
||||
enabled[i] = _slots[i].enabled && _slots[i].client != nullptr;
|
||||
any_enabled = any_enabled || enabled[i];
|
||||
}
|
||||
if (!any_enabled) return false;
|
||||
|
||||
if (MQTTPacketFilter::allows(
|
||||
MQTTPacketFilter::enabledUnion(masks, enabled, RUNTIME_MQTT_SLOTS),
|
||||
packet_type)) {
|
||||
return true;
|
||||
}
|
||||
filtered = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MQTTBridge::publishStatus() {
|
||||
if (!_cached_has_connected_slots) {
|
||||
return false;
|
||||
@@ -3151,6 +3237,8 @@ bool MQTTBridge::publishPacket(mesh::Packet* packet, bool is_tx,
|
||||
const uint8_t packet_type = packet->getPayloadType();
|
||||
const uint8_t eligible_slots = eligiblePacketSlots(packet_type, MSG_PACKETS);
|
||||
has_eligible_target = eligible_slots != 0;
|
||||
// Filtered out everywhere, or no slot can form a packets topic: intentionally
|
||||
// complete, and nothing below (JSON build included) is worth paying for.
|
||||
if (!has_eligible_target) return false;
|
||||
|
||||
refreshOriginFromPrefs();
|
||||
@@ -3258,6 +3346,9 @@ bool MQTTBridge::publishPacket(mesh::Packet* packet, bool is_tx,
|
||||
bool published = false;
|
||||
char topic[128];
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
// Eligibility already proved this slot's topic builds; rebuilding it is
|
||||
// one snprintf, cheaper than carrying a RUNTIME_MQTT_SLOTS x 128 topic
|
||||
// cache on the MQTT task's 8 KB stack alongside the JSON buffer.
|
||||
if ((eligible_slots & static_cast<uint8_t>(1u << i)) != 0 &&
|
||||
_slots[i].enabled && _slots[i].client && _slots[i].connected) {
|
||||
if (buildTopicForSlot(i, MSG_PACKETS, topic, sizeof(topic))) {
|
||||
@@ -3283,6 +3374,8 @@ bool MQTTBridge::publishRaw(mesh::Packet* packet, bool& has_eligible_target) {
|
||||
const uint8_t packet_type = packet->getPayloadType();
|
||||
const uint8_t eligible_slots = eligiblePacketSlots(packet_type, MSG_RAW);
|
||||
has_eligible_target = eligible_slots != 0;
|
||||
// Filtered out everywhere, or no slot has a raw topic (MeshRank is
|
||||
// packets-only): intentionally complete, and no JSON is built.
|
||||
if (!has_eligible_target) return false;
|
||||
|
||||
refreshOriginFromPrefs();
|
||||
|
||||
@@ -326,6 +326,10 @@ private:
|
||||
unsigned long _last_memory_check;
|
||||
bool _memory_pressure = false; // Cached max-alloc verdict; re-sampled at most once per interval in publishPacket() so the heap walk isn't paid per-packet under pressure
|
||||
int _skipped_publishes; // Exposed via SNMP; count of publishes skipped when max_alloc is too low
|
||||
// Packets rejected by the per-slot filters before reaching the queue. Written
|
||||
// on Core 1 (radio callbacks), read on Core 0 for `mqtt.stats`; a torn read of
|
||||
// a diagnostic counter is harmless, so no atomic is warranted.
|
||||
unsigned long _filtered_packets = 0;
|
||||
|
||||
// Status publish retry tracking
|
||||
unsigned long _last_status_retry; // Track last retry attempt (separate from successful publish)
|
||||
@@ -371,6 +375,7 @@ private:
|
||||
bool buildTopicForSlot(int index, MQTTMessageType type, char* topic_buf, size_t buf_size);
|
||||
bool substituteTopicTemplate(const char* tmpl, MQTTMessageType type, int slot_index, char* buf, size_t buf_size);
|
||||
uint8_t eligiblePacketSlots(uint8_t packet_type, MQTTMessageType type);
|
||||
bool shouldQueuePacketType(uint8_t packet_type, bool& filtered);
|
||||
|
||||
// Internal methods - slot management
|
||||
// Lifetime model (Phase 1 of MQTT memory-defrag):
|
||||
@@ -593,6 +598,9 @@ public:
|
||||
const char* state; // "inactive" | "wait" | "ok" | "fail" | "disc"
|
||||
unsigned long publish_ok;
|
||||
unsigned long publish_err;
|
||||
// Raw packet-type allowlist. Kept as the mask rather than canonical text so
|
||||
// the 1 KB stats JSON stays compact; the portal renders it.
|
||||
uint16_t filter_mask;
|
||||
};
|
||||
static bool getSlotStatusSnapshot(int slot_index, SlotStatusSnapshot* out);
|
||||
/** True when WiFi is set and at least one MQTT slot can run (preset + custom host if needed). */
|
||||
|
||||
@@ -746,11 +746,32 @@ void WebConfigServer::handleConfigPost(AsyncWebServerRequest* req) {
|
||||
// top-level `password` command, so it persists exactly as the CLI does.
|
||||
int pos = admin_pwd ? snprintf(e.cmd, sizeof(e.cmd), "password ")
|
||||
: snprintf(e.cmd, sizeof(e.cmd), "set %s ", key);
|
||||
for (const char* p = val; *p && pos < (int)sizeof(e.cmd) - 1; p++) {
|
||||
bool overflow = false;
|
||||
for (const char* p = val; *p; p++) {
|
||||
if (*p == '\r' || *p == '\n') continue;
|
||||
if (pos >= (int)sizeof(e.cmd) - 1) { overflow = true; break; }
|
||||
e.cmd[pos++] = *p;
|
||||
}
|
||||
e.cmd[pos] = 0;
|
||||
// A value that doesn't fit used to be truncated here and applied anyway.
|
||||
// For length-checked keys the CLI would reject the remainder, but a key
|
||||
// whose grammar stays valid when clipped (mqttN.filter: "advert,2" cut to
|
||||
// "advert") would silently persist a *different* setting and still answer
|
||||
// OK. Refuse the batch instead — the caller can shorten and retry.
|
||||
if (overflow) {
|
||||
// Name the key, like the "bad key" rejection above: a 20-field batch is
|
||||
// rejected whole, so without it the operator has nothing to correct.
|
||||
char safe_key[33];
|
||||
strncpy(safe_key, key, sizeof(safe_key) - 1);
|
||||
safe_key[sizeof(safe_key) - 1] = 0;
|
||||
StaticJsonDocument<128> ed;
|
||||
ed["error"] = "value too long";
|
||||
ed["key"] = safe_key;
|
||||
String out;
|
||||
serializeJson(ed, out);
|
||||
req->send(400, "application/json", out);
|
||||
return;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
// Phase 2: the count is now known, so the remaining NoChanges/Accept arms
|
||||
|
||||
+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);
|
||||
|
||||
+137
-5
@@ -41,6 +41,18 @@ input[type=text],input[type=password],input[type=number],input[type=email],selec
|
||||
border:1px solid var(--in-line);border-radius:8px;outline:none} /* >=16px: stops iOS zoom-on-focus (sideways-scroll in captive sheet) */
|
||||
input:focus,select:focus{border-color:var(--acc);box-shadow:0 0 0 2px color-mix(in srgb,var(--acc) 25%,transparent)}
|
||||
input.dirty,select.dirty{border-color:var(--warn)}
|
||||
/* packet-type allowlist: auto-fit so six slots stay readable on a phone */
|
||||
.pkg{display:grid;grid-template-columns:repeat(auto-fill,minmax(122px,1fr));gap:2px 8px;
|
||||
border:1px solid var(--in-line);border-radius:8px;padding:8px;background:var(--in-bg)}
|
||||
.f.dirty .pkg{border-color:var(--warn)}
|
||||
.pk{display:flex;align-items:center;gap:6px;font-size:12.5px;font-weight:400;margin:0;
|
||||
min-height:26px;cursor:pointer}
|
||||
.pk input{width:15px;height:15px;margin:0;flex:none}
|
||||
.pk span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
||||
.pka{display:flex;align-items:center;gap:10px;margin-top:5px;font-size:11.5px}
|
||||
.lnk{background:none;border:none;padding:0;font:inherit;font-size:11.5px;color:var(--acc);
|
||||
cursor:pointer;text-decoration:underline}
|
||||
.pfsum{color:var(--mut);margin-left:auto}
|
||||
.row{display:flex;gap:10px}.row>.f{flex:1;min-width:0}
|
||||
.sw{display:flex;align-items:center;justify-content:space-between;gap:10px;padding:9px 0}
|
||||
.sw span b{display:block;font-size:13.5px;font-weight:600}
|
||||
@@ -468,7 +480,7 @@ function api(path,opts){
|
||||
if(!r.ok&&r.status!==202){
|
||||
// Carry the HTTP status and any batch reqid so callers can tell a
|
||||
// definite rejection (400/409/413) from an ambiguous network failure.
|
||||
var e=new Error(j.error||("HTTP "+r.status));e.status=r.status;e.reqid=j.reqid;throw e;
|
||||
var e=new Error(j.error||("HTTP "+r.status));e.status=r.status;e.reqid=j.reqid;e.key=j.key;throw e;
|
||||
}
|
||||
return j;
|
||||
});
|
||||
@@ -579,6 +591,7 @@ function loadConfig(){
|
||||
$$("[data-rg]").forEach(function(el){el.value=String(c.radio[el.dataset.rg]);el.classList.remove("dirty")});
|
||||
st.orig.radio=radioCombo();afHint();
|
||||
for(var i=1;i<=st.nslots;i++){refreshSlotFields($("#app-slots"),i);refreshSlotFields($("#wz-slots"),i)}
|
||||
syncPacketFilters();
|
||||
updateSaveBar();
|
||||
});
|
||||
}
|
||||
@@ -627,11 +640,99 @@ function revertAll(){loadConfig().then(function(){toast("Reverted")})}
|
||||
// maxlength per destination MQTTPrefs buffer minus the NUL (host[64],
|
||||
// username[32], password[64], token[48], topic[96], audience[64]). Bounds the
|
||||
// common browser case; the backend still owns authoritative length limits.
|
||||
var SLOT_MAXLEN={token:47,username:31,password:63,server:63,topic:95,audience:63,filter:37};
|
||||
// mqttN.filter is deliberately absent: it is a bitmask, not a text buffer, and
|
||||
// its accepted input (names, spaces) is longer than its canonical output — a
|
||||
// maxlength here would silently truncate a valid list into a different one.
|
||||
var SLOT_MAXLEN={token:47,username:31,password:63,server:63,topic:95,audience:63};
|
||||
|
||||
// Payload types, mirroring PAYLOAD_TYPE_* in src/Packet.h. 12-14 are reserved
|
||||
// upstream with no name, so they are offered by number only.
|
||||
var PKT_TYPES=[[0,"REQ"],[1,"RESPONSE"],[2,"TXT_MSG"],[3,"ACK"],[4,"ADVERT"],
|
||||
[5,"GRP_TXT"],[6,"GRP_DATA"],[7,"ANON_REQ"],[8,"PATH"],[9,"TRACE"],
|
||||
[10,"MULTIPART"],[11,"CONTROL"],[12,"reserved"],[13,"reserved"],[14,"reserved"],
|
||||
[15,"RAW_CUSTOM"]];
|
||||
// Name -> type, for the names MQTTPacketFilter::resolveToken actually accepts.
|
||||
// "reserved" is a shared display label for 12-14, not a spelling the firmware
|
||||
// resolves, so it is excluded — otherwise "reserved" would silently mean 14.
|
||||
// Null-prototype so a token like "constructor" can't match an inherited key.
|
||||
var PKT_NAMED=Object.create(null);
|
||||
PKT_TYPES.forEach(function(p){if(p[1]!=="reserved")PKT_NAMED[p[1].toLowerCase()]=p[0]});
|
||||
// Mirrors MQTTPacketFilter::parse/format. The text form must round-trip the
|
||||
// firmware's canonical output exactly, or an unedited slot reads as dirty.
|
||||
function filterMask(text){
|
||||
var t=String(text==null?"":text).trim();
|
||||
// Empty is "all" on purpose — it is what the firmware stores for a missing
|
||||
// or cleared value, so an absent `filter` in /api/config reads as unfiltered
|
||||
// rather than as a slot that uploads nothing.
|
||||
if(t===""||t==="all")return 0xFFFF;
|
||||
if(t==="none")return 0;
|
||||
var mask=0,ok=true;
|
||||
t.split(",").forEach(function(part){
|
||||
var tok=part.trim(),n=-1;
|
||||
if(/^[0-9]+$/.test(tok))n=parseInt(tok,10);
|
||||
else if(tok.toLowerCase() in PKT_NAMED)n=PKT_NAMED[tok.toLowerCase()];
|
||||
if(n<0||n>15){ok=false;return}
|
||||
mask|=(1<<n);
|
||||
});
|
||||
// The device only ever emits canonical text; anything else means a value we
|
||||
// did not write, so show it as unfiltered rather than inventing a subset.
|
||||
return ok?mask:0xFFFF;
|
||||
}
|
||||
function filterText(mask){
|
||||
if(mask===0xFFFF)return"all";
|
||||
if(mask===0)return"none";
|
||||
var out=[];
|
||||
for(var i=0;i<16;i++)if(mask&(1<<i))out.push(i);
|
||||
return out.join(",");
|
||||
}
|
||||
function slotFieldHtml(i,f,type,label,hint){
|
||||
var ml=SLOT_MAXLEN[f]?' maxlength="'+SLOT_MAXLEN[f]+'"':'';
|
||||
return'<div class="f sf-'+f+'"><label>'+label+'</label><input type="'+type+'" data-k="mqtt'+i+'.'+f+'"'+ml+' autocapitalize="off" autocorrect="off" autocomplete="off">'+(hint?'<div class="hint">'+hint+'</div>':'')+'</div>';
|
||||
}
|
||||
// Checkboxes rather than a text field: the accepted grammar is wider than the
|
||||
// canonical form, so free text invites a value that saves as something else.
|
||||
// The hidden input stays the single source of truth for the generic dirty /
|
||||
// save / result-chip machinery, which is keyed on [data-k].
|
||||
function packetFilterHtml(i){
|
||||
var boxes=PKT_TYPES.map(function(p){
|
||||
return'<label class="pk"><input type="checkbox" data-pf="'+i+'" value="'+p[0]+'"> <span>'+p[0]+' '+p[1]+'</span></label>';
|
||||
}).join("");
|
||||
return'<div class="f sf-filter"><label>Packet types</label>'
|
||||
+'<input type="hidden" data-k="mqtt'+i+'.filter">'
|
||||
+'<div class="pkg">'+boxes+'</div>'
|
||||
+'<div class="pka"><button type="button" class="lnk" data-pfset="'+i+':all">All</button>'
|
||||
+'<button type="button" class="lnk" data-pfset="'+i+':none">None</button>'
|
||||
+'<span class="pfsum"></span></div>'
|
||||
+'<div class="hint">Which payload types are uploaded to this broker. Applies to both packet and raw uploads; status and neighbors are never filtered. Clearing every box uploads nothing.</div></div>';
|
||||
}
|
||||
// Reflect each hidden filter value into its checkboxes and summary. Cheap
|
||||
// enough to re-run wholesale after any load or edit.
|
||||
function syncPacketFilters(){
|
||||
$$('[data-k$=".filter"]').forEach(function(h){
|
||||
var i=h.dataset.k.match(/^mqtt([1-9])\./)[1];
|
||||
var card=h.closest(".slot");if(!card)return;
|
||||
var mask=filterMask(h.value);
|
||||
card.querySelectorAll('[data-pf="'+i+'"]').forEach(function(cb){
|
||||
cb.checked=(mask&(1<<parseInt(cb.value,10)))!==0;
|
||||
});
|
||||
var sum=card.querySelector(".pfsum");
|
||||
if(sum)sum.textContent=mask===0xFFFF?"all types":(mask===0?"nothing uploaded":filterText(mask));
|
||||
var wrap=h.closest(".f");
|
||||
if(wrap)wrap.classList.toggle("dirty",h.dataset.k in st.dirty);
|
||||
});
|
||||
}
|
||||
// Recompute one slot's canonical value from its boxes and push it through the
|
||||
// same markDirty path a typed field would use.
|
||||
function packetFilterChanged(i,card){
|
||||
var mask=0;
|
||||
card.querySelectorAll('[data-pf="'+i+'"]').forEach(function(cb){
|
||||
if(cb.checked)mask|=(1<<parseInt(cb.value,10));
|
||||
});
|
||||
var k="mqtt"+i+".filter",v=filterText(mask);
|
||||
$$('[data-k="'+k+'"]').forEach(function(h){h.value=v});
|
||||
markDirty(k,v);
|
||||
syncPacketFilters();
|
||||
}
|
||||
function buildSlots(sel){
|
||||
var host=$(sel);if(!host)return;
|
||||
var html="";
|
||||
@@ -649,7 +750,7 @@ function buildSlots(sel){
|
||||
+slotFieldHtml(i,"port","number","Port","")
|
||||
+slotFieldHtml(i,"topic","text","Topic template","Placeholders: {iata} {device} {type} {token}")
|
||||
+slotFieldHtml(i,"audience","text","JWT audience","Optional — blank for user/pass auth")
|
||||
+slotFieldHtml(i,"filter","text","Packet types","all, none, or 0–15 CSV. 0 REQ · 1 RESPONSE · 2 TXT_MSG · 3 ACK · 4 ADVERT · 5 GRP_TXT · 6 GRP_DATA · 7 ANON_REQ · 8 PATH · 9 TRACE · 10 MULTIPART · 11 CONTROL · 12–14 reserved · 15 RAW_CUSTOM. Applies to packet and raw uploads.")
|
||||
+packetFilterHtml(i)
|
||||
+'</div>';
|
||||
}
|
||||
host.innerHTML=html;
|
||||
@@ -672,8 +773,21 @@ document.addEventListener("change",function(ev){
|
||||
var el=ev.target,m;
|
||||
if(el.dataset.k&&(m=el.dataset.k.match(/^mqtt([1-9])\.preset$/))){
|
||||
["#app-slots","#wz-slots"].forEach(function(sel){refreshSlotFields($(sel),+m[1])});
|
||||
}else if(el.dataset.pf){
|
||||
var card=el.closest(".slot");
|
||||
if(card)packetFilterChanged(el.dataset.pf,card);
|
||||
}
|
||||
});
|
||||
document.addEventListener("click",function(ev){
|
||||
var b=ev.target.closest?ev.target.closest("[data-pfset]"):null;
|
||||
if(!b)return;
|
||||
var parts=b.dataset.pfset.split(":"),i=parts[0],all=parts[1]==="all";
|
||||
// Write through the same path a checkbox edit takes, so both the app and
|
||||
// wizard copies of this slot stay in step.
|
||||
var card=b.closest(".slot");if(!card)return;
|
||||
card.querySelectorAll('[data-pf="'+i+'"]').forEach(function(cb){cb.checked=all});
|
||||
packetFilterChanged(i,card);
|
||||
});
|
||||
|
||||
/* ---------- save / results ---------- */
|
||||
function chipFor(el,ok,msg){
|
||||
@@ -702,7 +816,19 @@ function saveChanges(extra,cb){
|
||||
// result to poll for. Report it instead of chasing a phantom save.
|
||||
if(e.status===400||e.status===413){
|
||||
$("#save-btn").disabled=false;
|
||||
if(cb)cb(null,false);else toast(e.message||"Save rejected");
|
||||
// A whole-batch rejection names the offending key; chip it so the
|
||||
// operator doesn't have to guess which of N edited fields was at fault.
|
||||
// Every copy, not querySelector: the wizard's slot cards precede the
|
||||
// app's in document order, so one lookup always lands on the hidden one.
|
||||
// Comparing the property also keeps a device-echoed key — "bad key"
|
||||
// returns whatever was sent — out of the selector grammar, where a quote
|
||||
// would throw SyntaxError and swallow the rest of this handler.
|
||||
if(e.key){
|
||||
$$("[data-k]").forEach(function(el){
|
||||
if(el.dataset.k===e.key)chipFor(el,false,e.message||"rejected");
|
||||
});
|
||||
}
|
||||
if(cb)cb(null,false);else toast((e.message||"Save rejected")+(e.key?": "+e.key:""));
|
||||
return;
|
||||
}
|
||||
// 409 busy: a batch is in flight. If it's ours (same reqid — our POST
|
||||
@@ -826,6 +952,7 @@ function loadConfigSoft(){ // re-sync accepted values without clobbering chips/r
|
||||
el.value=radioParts[idx];el.classList.toggle("dirty",keepRadio);
|
||||
});
|
||||
for(var i=1;i<=st.nslots;i++){refreshSlotFields($("#app-slots"),i);refreshSlotFields($("#wz-slots"),i)}
|
||||
syncPacketFilters();
|
||||
afHint();
|
||||
updateSaveBar();
|
||||
}).catch(function(){});
|
||||
@@ -880,7 +1007,12 @@ function pollStats(){
|
||||
spark($("#spark-heap"),st.hist.heap);spark($("#spark-noise"),st.hist.noise);
|
||||
var sl=s.slots||[];
|
||||
$("#stat-slots").innerHTML=sl.length?sl.map(function(x){
|
||||
return'<div class="kv"><i>Slot '+x.n+" · "+esc(x.name)+'</i><b><span class="pill '+esc(x.state)+'">'+esc(x.state)+'</span> '+x.ok+' ok / '+x.err+' err</b></div>';
|
||||
// "filt" is sent only when the slot is not on the all-types default. A
|
||||
// healthy slot that has stopped publishing is otherwise indistinguishable
|
||||
// from an idle one.
|
||||
var f=(x.filt==null)?"":'<div class="kv"><i></i><b style="font-weight:400;color:var(--mut)">filter: '
|
||||
+esc(x.filt===0?"none (nothing uploaded)":filterText(x.filt))+'</b></div>';
|
||||
return'<div class="kv"><i>Slot '+x.n+" · "+esc(x.name)+'</i><b><span class="pill '+esc(x.state)+'">'+esc(x.state)+'</span> '+x.ok+' ok / '+x.err+' err</b></div>'+f;
|
||||
}).join(""):"No servers configured.";
|
||||
}).catch(function(){}).then(function(){
|
||||
// Runs whether the poll resolved or rejected: clear the guard, then schedule
|
||||
|
||||
Reference in new issue
Block a user