294 lines
15 KiB
Markdown
294 lines
15 KiB
Markdown
# Stats bundle -- one request per scrape (repeater)
|
|
|
|
> [!WARNING]
|
|
> Part of an experimental fork, not meant for regular users. The request is
|
|
> not part of upstream's protocol.
|
|
|
|
A monitoring scrape used to cost a login (when the session had expired),
|
|
`REQ_TYPE_GET_STATUS`, `REQ_TYPE_GET_TELEMETRY_DATA` (the node's sensors) and, for the filter
|
|
counters, more CLI round trips -- up to 6 packets over several hops. The
|
|
**stats bundle** answers all of it in **one request and one reply**.
|
|
|
|
Code: `src/helpers/StatsBundle.h` (encoder, decoder, prefs CRC; native tests in
|
|
`test/test_stats_bundle`), glue in `examples/simple_repeater/MyMesh.cpp`
|
|
(`handleRequest`). Reference implementation in Python and test vectors:
|
|
`test/vectors/gen_stats_bundle_vectors.py` -> `test/vectors/stats_bundle_vectors.json`
|
|
(`--check` verifies the JSON). Python 3 stdlib only, meant to be lifted into
|
|
the exporter and the Ansible collection as it is.
|
|
|
|
## Request
|
|
|
|
A normal logged-in `REQ` (like `GET_STATUS`); the first payload byte is the type.
|
|
|
|
| byte | meaning |
|
|
|---|---|
|
|
| 0 | `REQ_TYPE_STATS_BUNDLE = 0xF1` (our private range next to `REQ_TYPE_FW_OTA = 0xF0`; both outside upstream's 0x01..0x09) |
|
|
| 1 | `page`, optional, default 0 |
|
|
|
|
An unknown page gets no reply. A build without the bundle ignores the type
|
|
(upstream firmware answers nothing to an unknown type): a reader that gets no
|
|
reply to page 0 falls back to `GET_STATUS` + `GET_TELEMETRY_DATA`.
|
|
|
|
**Who may read what.** An **admin** gets everything. A guest or read-only login
|
|
gets exactly what `GET_STATUS` and the telemetry give it today -- status
|
|
and telemetry records (ids 1..21) and the sensor record (id 42, below), nothing
|
|
else. Sensors follow the telemetry rules: a **guest** is answered as telemetry
|
|
answers a guest (base set, so no sensors and no id 42); any other login gets
|
|
all of them, as a `GET_TELEMETRY_DATA` with the default (all permissions) mask
|
|
does.
|
|
|
|
## Reply
|
|
|
|
The reply carries the 4-byte tag of the request, then at most **150 B** of
|
|
bundle (the same budget as the OTA reply: tag + 150 B pads to 160 B and the
|
|
companion's push frame must stay within `MAX_FRAME_SIZE` 176).
|
|
|
|
```
|
|
[ver = 1][page << 4 | pages] record record ...
|
|
```
|
|
|
|
`pages` is the total number of pages (1..15), `page` the one in this reply.
|
|
|
|
**Record** = key byte `(id << 2) | wire_type`, id 0..63:
|
|
|
|
| wire type | value |
|
|
|---|---|
|
|
| 0 | varint: LEB128, unsigned 32 bit (low 7 bits first, bit 7 = more). Signed values are **zigzag** `(x << 1) ^ (x >> 31)` |
|
|
| 1 | length (varint) + that many bytes |
|
|
| 2, 3 | reserved; a reader that meets one drops the page |
|
|
|
|
A reader **skips ids it does not know** (wt 0: read the varint; wt 1: skip the
|
|
length), so ids can be added without a version bump. The version byte changes
|
|
only when an existing id changes meaning. Only **non-zero** counters are sent;
|
|
an absent counter is 0. Always sent for an admin: ids 32, 33, 34 (see below).
|
|
|
|
**A zero byte where a record key is expected ends the page.** Id 0 is never
|
|
used; the reply arrives padded with zeros to the cipher block (AES, 16 B --
|
|
the companion hands the padding over too: 139 B of bundle came as 140 B, a
|
|
page 1 of 30 B as 44 B). A reader that takes `0x00` as a record with id 0
|
|
reads past the data and fails the page.
|
|
|
|
### Ids
|
|
|
|
| id | name | value |
|
|
|---|---|---|
|
|
| 1 | `batt_mv` | mV |
|
|
| 2 | `temp_dc` | MCU temperature, 0.1 C, zigzag (absent when the board has none; sent even when 0) |
|
|
| 3 | `uptime` | s |
|
|
| 4 / 5 | `air_tx` / `air_rx` | total TX / RX airtime, s |
|
|
| 6 / 7 | `noise` / `rssi` | dBm, zigzag |
|
|
| 8 | `snr4` | SNR x 4, zigzag |
|
|
| 9 | `tx_queue` | packets in the outbound queue |
|
|
| 10 / 11 | `recv` / `sent` | packets |
|
|
| 12 / 13 | `sent_flood` / `sent_direct` | |
|
|
| 14 / 15 | `recv_flood` / `recv_direct` | |
|
|
| 16 | `err_events` | the error flag **bits** of `RepeaterStats.err_events` (a bit set, not a count of errors) |
|
|
| 17 / 18 | `direct_dups` / `flood_dups` | |
|
|
| 19 | `recv_errors` | |
|
|
| 20 / 21 | `tx_start_fail` / `tx_timeout` | |
|
|
| | *the fields of `RepeaterStats` plus telemetry; ids 1..21 are all a guest sees* | |
|
|
| 24 | `fair_group` | FairnessLimiter drops, channel (group) bucket |
|
|
| 25 | `fair_sender` | sender bucket |
|
|
| 26 | `fair_advert` | advert bucket |
|
|
| 27 | `fair_window` | per-origin advert window |
|
|
| 32 | `rules_auto` | `count << 16 \| crc16` of the `auto` `filter flow` rules |
|
|
| 33 | `rules_static` | the same for the static ones |
|
|
| 34 | `prefs_crc` | `prefs_crc16`, below |
|
|
| 35 | `pwr_flags` | adaptive battery power save (`set pwr.rxgain` / `set pwr.rxps`): bit 0 rxgain stage active, bit 1 rxps stage active, bit 2 rxgain configured, bit 3 rxps configured |
|
|
| 36 | `n_counters` | number of flow slots with a non-zero counter (see "Pages") |
|
|
| 40 (wt 1) | `counters` | a run of triples `slot, dropped, passed`, each a varint |
|
|
| 41 (wt 1) | `rule` | one rule, below |
|
|
| 42 (wt 1) | `env_lpp` | the node's sensors, below |
|
|
|
|
`rules_auto` / `rules_static` are the `crc16` values of `filter flow`
|
|
(CRC-16/CCITT-FALSE over the canonical serialization, see
|
|
[tth-commands.md](tth-commands.md#forward-deny-rules----filter-flow-every-repeater)
|
|
and `test/vectors/flow_rules_vectors.json`), with the entry count above them.
|
|
An empty set is `0x0000FFFF`.
|
|
|
|
**Sensor record** (id 42): the **CayenneLPP bytes** (`[channel][type][data]`
|
|
repeated, the format of a `GET_TELEMETRY_DATA` reply) of the node's sensors
|
|
**without channel 1**. Channel 1 is the node itself -- battery and MCU
|
|
temperature are already ids 1 and 2 (GPS, where a build reports it, is also on
|
|
channel 1 and so not here). It is what a telemetry request would add for the
|
|
same login: environment sensors (temperature, humidity, pressure, INA, ...) on
|
|
channels 2.., in the order the node initialised them. The record is **omitted**
|
|
when there is nothing to send (no sensors, a guest); readings are whole, and
|
|
those that would not fit in 128 B (`SB_ENV_MAX`) are left out, never cut. A
|
|
reader decodes it like a telemetry reply; types and scaling are in
|
|
`lpp_decode()` of the Python reference (e.g. `02 67 00E1` = channel 2,
|
|
temperature 22.5 C; `02 68 3C` = humidity 30 %; `02 73 271C` = pressure
|
|
1001.2 hPa; 11 B for the three).
|
|
|
|
**Rule record** (id 41): `slot` (varint) `kind` (1 B, ASCII) `klen` (1 B)
|
|
`key` (klen B) `mf` (1 B) then, in this order and only when flagged,
|
|
`secs` (varint, mode throttle), `expires` (varint, absolute unix),
|
|
`left` (varint, seconds to expiry by the two clocks of `filter flow`).
|
|
`mf`: bits 0-1 mode (0 block, 1 throttle, 2 dry), bit 2 `auto`, bit 3 has
|
|
`expires`, bit 4 has `left`.
|
|
|
|
### Pages
|
|
|
|
`slot_id` is the index in the **sorted** rule table of `filter flow`
|
|
((kind, key) order) and **moves on `add` / `del` / expiry**.
|
|
|
|
* **Page 0** is what is read every scrape: ids 1..21, fairness, ids 32..36, the
|
|
**sensor record** (id 42) and as many `(slot, dropped, passed)` as fit in the
|
|
150 B -- for every slot with a non-zero counter, in slot order -- and, **when
|
|
they fit, the rule records (id 41)** after them: a record is never split, and
|
|
what does not fit moves to page 1 and on. The rule table holds **24 rules**
|
|
(`FLOW_MAX`). A realistic node (2 rules, a few fairness drops, the full
|
|
status, 3 sensors) needs 129 B including the rule records; 16 slots with
|
|
counters do not fit, so the rest of the triples spills to page 1. The sensor
|
|
record comes before the counters and rules, so it takes page 0 ahead of
|
|
them; one too big for what is left of page 0 starts page 1 (whole), after
|
|
the CRC records.
|
|
* **Pages 1..** hold, in this order, a sensor record that did not fit page 0,
|
|
the **spilled counter triples** (id 40, the slots that did not fit on page 0,
|
|
still in slot order) and then the **rule records** (id 41) that did not fit
|
|
page 0, one record per slot in slot order. A record is never
|
|
split; a page is closed when the next record does not fit. Every page after
|
|
the first starts with the records `rules_auto` and `rules_static` (8 B).
|
|
* `n_counters` (id 36) tells how many slots have counters. When the triples on
|
|
page 0 are fewer than `n_counters`, the rest is on pages 1.. -- read them.
|
|
|
|
### Reader rule (pairing)
|
|
|
|
`(slot_id, dropped, passed)` means nothing without the table that numbered the
|
|
slots. So:
|
|
|
|
1. Fetch page 0. Compare `rules_auto` and `rules_static` with the values of the
|
|
previous scrape (the reader keeps its `slot_id -> rule` map and the CRCs it
|
|
was built from).
|
|
2. **If either CRC changed** (or the reader has no map yet): fetch pages 1.. **first**,
|
|
build the new map from the rule records, and only then pair the page 0
|
|
triples with it. Spilled counter triples (id 40 on pages 1..) are read in the
|
|
same pass.
|
|
3. If `n_counters` is greater than the triples on page 0 but the CRCs did not
|
|
change, fetch the pages 1.. for the spilled counters as well.
|
|
4. **Every page 1.. opens with the two CRC records (ids 32, 33) of page 0.** If
|
|
the pair on any later page differs from page 0, the table changed between the
|
|
requests (a concurrent `add`, an expiry): **drop this scrape's counters** and
|
|
try the next scrape. Never assign numbers to old keys.
|
|
|
|
When the CRCs are unchanged and nothing spilled, the reader needs no page 1.. at
|
|
all: one request, one reply. The drift check against the signed inventory (#44) compares
|
|
`rules_static` and `prefs_crc` with the values computed from it; the agent
|
|
(#43) does the same for `rules_auto`.
|
|
|
|
## `prefs_crc16`
|
|
|
|
CRC-16/CCITT-FALSE (poly 0x1021, init 0xFFFF, no reflection, no final xor;
|
|
check value of "123456789" is 0x29B1) over the canonical serialization of the
|
|
prefs that Ansible manages. All integers big-endian.
|
|
|
|
| # | bytes | field | encoding |
|
|
|---|---|---|---|
|
|
| 1 | 1 | serialization version | `2` (since regions; `1` had none) |
|
|
| 2 | 4 | radio frequency | MHz x 1000 rounded: `round(869.432 * 1000)` = 869432 |
|
|
| 3 | 4 | bandwidth | kHz x 1000 rounded (62.5 -> 62500) |
|
|
| 4 | 1 | `sf` | |
|
|
| 5 | 1 | `cr` | |
|
|
| 6 | 1 | TX power | dBm, two's complement |
|
|
| 7 | 1 + n | node name | length (<= 31) + the UTF-8 bytes as stored |
|
|
| 8 | 1 | advert interval | the stored value (`advert_interval`, minutes / 2) |
|
|
| 9 | 1 | flood advert interval | hours |
|
|
| 10 | 1 | `flood.max` | |
|
|
| 11 | 1 | `flood.max` unscoped | |
|
|
| 12 | 1 | `flood.max` advert | |
|
|
| 13 | 1 | path hash mode | |
|
|
| 14 | 1 | `repeat` | the stored `disable_fwd` (1 = repeating off) |
|
|
| 15 | 1 | `powersaving` | 0 / 1 |
|
|
| 16 | 1 | `radio.rxgain` | `rx_boosted_gain` 0 / 1 |
|
|
| 17 | 1 | `rxps` enabled | 0 / 1 (all `rxps` bytes 0 in a build without `WITH_RX_POWERSAVING`) |
|
|
| 18 | 4 | `rxps` RX time | microseconds |
|
|
| 19 | 4 | `rxps` sleep time | microseconds |
|
|
| 20 | 1 | `rxps` level | |
|
|
| 21 | 1 | `rxps` preamble | |
|
|
| 22 | 1 | bridge | `bridge.enabled` 0 / 1 |
|
|
| 23 | 1 + n | home region | name, see "Regions" below (empty = none) |
|
|
| 24 | 1 | region count | `N`, number of regions (the root `*` is not counted) |
|
|
| 25.. | N x (1 + n, 1 + m, 1) | regions | `name`, `parent`, `flood`, sorted, see below |
|
|
|
|
### Regions (rows 23-25)
|
|
|
|
Appended after the bridge byte, so the CRC of a node and of the inventory can
|
|
be compared without any RF traffic. Everything is **length-prefixed UTF-8**
|
|
(1 length byte, then the bytes, like the node name):
|
|
|
|
```
|
|
home_name N { name parent flood } x N
|
|
```
|
|
|
|
* **Name** (`home`, `name`, `parent`): one leading `#` dropped (the CLI and
|
|
`meshcore_region` ignore it too), at most 31 bytes; the root `*`, an unset
|
|
value and "no parent" are all the **empty** string (length byte 0).
|
|
* **home_name**: the home region (`region home`); empty when there is none
|
|
(stored home id 0, which the CLI shows as `*`).
|
|
* **N**: one byte, the number of regions.
|
|
* **Region**: `name`, `parent` (the name of the parent; empty = child of the
|
|
root), `flood` (1 byte: 1 = flood allowed, 0 = denied, `region allowf` /
|
|
`region denyf`).
|
|
* **Order**: sorted by **bytes** of `name` (memcmp, a shorter prefix first), then
|
|
of `parent`. Insertion order, internal ids, and the order of `region` output
|
|
do not matter. A parent id that no region has (a corrupt map) is written as
|
|
the name `?`, which no real region can have, so it shows up as drift.
|
|
|
|
Not in it: the root's own flood flag, the default flood scope
|
|
(`region default`; the module sets it only when `mc_default_scope` is given,
|
|
so the inventory does not always have a value), internal ids. The CRC reads the
|
|
**live** map, so a `region` change that was not yet saved (`region save`) is
|
|
already in it; after a reboot it is gone again.
|
|
|
|
**From the inventory alone** (role `repeater-state`, module `meshcore_region`):
|
|
|
|
| inventory | in the serialization |
|
|
|---|---|
|
|
| `mc_region_home` (default `"*"` when absent) | `home_name`; `"*"` -> empty |
|
|
| `mc_regions` list | one region per item; `N` = its length |
|
|
| item `name` | `name`, without `#` |
|
|
| item `parent` (default `"*"`) | `parent`; `"*"` -> empty |
|
|
| item `flood` (default `true`) | `flood` 1 / 0 |
|
|
|
|
Example, `mc_regions: [{name: cz}, {name: cz-ulk, parent: cz}, {name: cz-sudety,
|
|
parent: cz}, {name: cs}]` with `mc_region_home: cz-ulk` gives (hex)
|
|
`06 637a2d756c6b | 04 | 02 6373 00 01 | 02 637a 00 01 | 09 637a2d7375646574
|
|
79 02 637a 01 | 06 637a2d756c6b 02 637a 01` (spaces and bars only for reading);
|
|
the whole serialization has CRC `D5FD` (vector `cz_repeater`, and the same
|
|
map in another order, `cz_repeater_reordered`). `regions_canonical()` in the
|
|
Python reference is the whole recipe: lift it as it is.
|
|
|
|
Reference: `prefs_canonical()` in `test/vectors/gen_stats_bundle_vectors.py`;
|
|
test vectors (canonical bytes and CRC for three nodes, one with a denied-flood region) in
|
|
`test/vectors/stats_bundle_vectors.json`.
|
|
|
|
**Not in it:** the `filter flow` rules (they have `rules_auto` /
|
|
`rules_static`), `filter` age and dry-run, FairnessLimiter caps, passwords,
|
|
guest and ACL entries, the adaptive battery thresholds (`pwr.*`, their own files) and GPS settings.
|
|
Adding a field means a new serialization version byte (and a new vector).
|
|
|
|
## Version and compatibility
|
|
|
|
The sensor record is a **new id** (42), not a new meaning of an old one, so the
|
|
version byte stays `1`: a reader that does not know id 42 skips it by its length
|
|
(`wt 1`), as the format requires of every reader. A reader that knows it and
|
|
talks to a node without it (a release from before this record) finds no id 42 --
|
|
the same as a node with no sensors; telling the two apart takes the firmware
|
|
version, not the bundle.
|
|
|
|
## Sizes
|
|
|
|
| case | page 0 |
|
|
|---|---|
|
|
| guest (status + telemetry) | 60 B |
|
|
| admin, empty table, short status | 18 B |
|
|
| admin, 2 rules, a few fairness drops, full status | 116 B (one page) |
|
|
| the same + 3 sensors (temperature, humidity, pressure: 11 B LPP + 2 B header) | 129 B (one page) |
|
|
| admin, empty table, full status, 3 sensors | 93 B |
|
|
| read-only, full status, 3 sensors | 73 B |
|
|
| admin, 2 rules, a 124 B sensor record | 82 B + 145 B + 35 B (3 pages; the sensors on page 1) |
|
|
| admin, 16 rules with counters | 149 B + 145 B + 150 B + 70 B (4 pages) |
|
|
|
|
The boundary cases (149 / 150 B fit, 151 B moves the last record to page 1)
|
|
are in the vectors.
|