Merge feat/stats-bundle-env into main

This commit is contained in:
tth committed 2026-12-04 04:05:18 +01:00
commit a2a2b67ee3
7 files changed
+997 -16

No files matched your search

+51 -12
View File
@@ -5,7 +5,7 @@
> 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` and, for the filter
`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**.
@@ -30,8 +30,12 @@ An unknown page gets no reply. A build without the bundle ignores the type
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 base telemetry give it today -- status
and telemetry records (ids 1..21), one page, nothing else.
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
@@ -72,7 +76,7 @@ an absent counter is 0. Always sent for an admin: ids 32, 33, 34 (see below).
| 10 / 11 | `recv` / `sent` | packets |
| 12 / 13 | `sent_flood` / `sent_direct` | |
| 14 / 15 | `recv_flood` / `recv_direct` | |
| 16 | `err_events` | error flag bits |
| 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` | |
@@ -88,6 +92,7 @@ an absent counter is 0. Always sent for an admin: ids 32, 33, 34 (see below).
| 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
@@ -95,6 +100,20 @@ an absent counter is 0. Always sent for an admin: ids 32, 33, 34 (see below).
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),
@@ -107,14 +126,21 @@ An empty set is `0x0000FFFF`.
`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 and
as many `(slot, dropped, passed)` as fit in the 150 B -- for every slot with a
non-zero counter, in slot order. A realistic node (2 rules, a few fairness
drops, the full status) needs about 116 B including the rule records; all 16
slots with counters do not fit, so the rest of the triples spills to page 1.
* **Pages 1..** hold, in this order, the **spilled counter triples** (id 40, the
slots that did not fit on page 0, still in slot order) and then the **rule
metadata** (id 41), one record per slot in slot order. A record is never
* **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
@@ -235,6 +261,15 @@ test vectors (canonical bytes and CRC for three nodes, one with a denied-flood r
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 |
@@ -242,6 +277,10 @@ Adding a field means a new serialization version byte (and a new vector).
| 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)
+1 -1
View File
@@ -199,7 +199,7 @@ clock reads before 2026; a clock running ahead makes everything look old.
## Stats bundle -- one request per scrape (repeater)
A binary request (`0xF1`, not a console command) that returns status, telemetry,
A binary request (`0xF1`, not a console command) that returns status, telemetry (with the node's sensors),
FairnessLimiter drops, per-rule `filter flow` counters, the rule CRCs
(`rules_crc16`, auto and static), `prefs_crc16` and the adaptive power save
stage in one reply of at most 150 B, paged when it does not fit. Admin gets
+13
View File
@@ -608,6 +608,19 @@ int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t
in.v[SB_RECV_ERRORS] = radio_driver.getPacketsRecvErrors();
in.v[SB_TX_START_FAIL] = getNumTxStartFail();
in.v[SB_TX_TIMEOUT] = getNumTxTimeout();
// The node's sensors as a telemetry request would add them, without channel 1
// (battery and MCU temperature are scalars above). Same permissions as
// REQ_TYPE_GET_TELEMETRY_DATA: a guest gets the base set (no sensors), anyone
// else everything, as with the default (all-permissions) request.
uint8_t env_buf[SB_ENV_MAX];
{
uint8_t perm_mask = ((sender->permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST) ? 0x00 : 0xFF;
telemetry.reset();
sensors.querySensors(perm_mask, telemetry);
in.env_len = (uint8_t)sbLppExclude(telemetry.getBuffer(), telemetry.getSize(), TELEM_CHANNEL_SELF,
env_buf, sizeof(env_buf));
in.env = in.env_len ? env_buf : NULL;
}
if (in.admin) {
in.fair[0] = fairness_limiter.deniedGroup();
in.fair[1] = fairness_limiter.deniedSenderNormal();
+49 -1
View File
@@ -1,7 +1,7 @@
#pragma once
// CUSTOM (TeTeHacko): "stats bundle" -- ONE binary request that answers a whole
// monitoring scrape (status + telemetry + fairness drops + `filter flow`
// monitoring scrape (status + telemetry + sensors (CayenneLPP) + fairness drops + `filter flow`
// counters + rule/prefs CRCs) instead of login + req_status + req_telemetry +
// CLI round trips (tth #42, epic #45). Header-only and free of mesh/Arduino
// types so encoder and decoder run in the native tests; the glue (reading the
@@ -38,6 +38,7 @@ enum {
SB_PWR_FLAGS = 35, SB_N_COUNTERS = 36,
// wt 1
SB_COUNTERS = 40 /* run of (slot, dropped, passed) varints */, SB_RULE = 41,
SB_ENV_LPP = 42 /* CayenneLPP bytes of the node's sensors, channel 1 (the node itself) left out */,
};
#define SB_LAST_STATUS_ID 21
@@ -47,6 +48,9 @@ enum {
#define SB_PWR_RXPS_SET 0x08
#define SB_BODY_MAX (FLOW_MAX * 11)
// Longest env record body: what is left of a page after the 2 B header, the 2 rule
// CRC records that open every page after the first (<= 12 B) and the record header.
#define SB_ENV_MAX 128
struct StatsBundleIn {
bool admin;
@@ -55,6 +59,8 @@ struct StatsBundleIn {
uint32_t fair[4]; // group, sender, advert, window
uint8_t pwr_flags;
uint16_t prefs_crc;
const uint8_t* env; // SB_ENV_LPP body (see sbLppExclude), NULL / 0 = no sensors
uint8_t env_len;
const FlowTable* flow;
uint32_t now_ms, now_unix;
};
@@ -109,6 +115,38 @@ public:
int finish() { if (cur == target) tlen = used; return tlen; }
};
// Data size of a CayenneLPP type, 0 = unknown (a walk cannot continue past it).
inline uint8_t sbLppSize(uint8_t type) {
switch (type) {
case 0: case 1: case 102: case 104: case 120: case 142: return 1;
case 2: case 3: case 101: case 103: case 115: case 116: case 117: case 121: case 125:
case 128: case 132: return 2;
case 135: return 3;
case 100: case 118: case 130: case 131: case 133: return 4;
case 113: case 134: return 6;
case 136: return 9;
default: return 0;
}
}
// Copies the readings of a CayenneLPP buffer [channel][type][data] except those on
// channel `skip` into out (at most `max` bytes, whole readings only). Stops at an
// unknown type. Returns the length written.
inline int sbLppExclude(const uint8_t* in, int n, uint8_t skip, uint8_t* out, int max) {
int p = 0, o = 0;
while (p + 2 <= n) {
uint8_t sz = sbLppSize(in[p + 1]);
if (sz == 0 || p + 2 + sz > n) break;
if (in[p] != skip) {
if (o + 2 + sz > max) break;
memcpy(&out[o], &in[p], 2 + sz);
o += 2 + sz;
}
p += 2 + sz;
}
return o;
}
inline uint8_t sbRuleRecord(const FlowTable& t, int i, uint32_t now_ms, uint32_t now_unix, uint8_t* o) {
const FlowRule& r = t.e[i].r;
uint8_t n = 0;
@@ -146,6 +184,8 @@ inline int sbEncode(const StatsBundleIn& in, uint8_t page, uint8_t* out, int max
if (z) x = sbZigzag((int32_t)x);
if (x != 0 || (id == SB_TEMP_DC && in.has_temp)) w.scalar(id, x);
}
const bool env = in.env && in.env_len && in.env_len <= SB_ENV_MAX;
if (!in.admin && env) w.blob(SB_ENV_LPP, in.env, in.env_len);
if (in.admin) {
for (uint8_t k = 0; k < 4; k++) if (in.fair[k]) w.scalar(SB_FAIR_GROUP + k, in.fair[k]);
int ca = 0, cs = 0;
@@ -163,6 +203,8 @@ inline int sbEncode(const StatsBundleIn& in, uint8_t page, uint8_t* out, int max
int nc = 0;
for (uint8_t i = 0; i < in.flow->n; i++) if (in.flow->e[i].dropped || in.flow->e[i].passed) nc++;
if (nc) w.scalar(SB_N_COUNTERS, (uint32_t)nc);
// sensors ahead of the counters and rules: page 0 is read every scrape
if (env) w.blob(SB_ENV_LPP, in.env, in.env_len);
uint8_t body[SB_BODY_MAX];
int bl = 0;
for (uint8_t i = 0; i < in.flow->n; i++) {
@@ -211,6 +253,8 @@ struct StatsBundleOut {
uint8_t n_ctr;
SbRule rule[FLOW_MAX];
uint8_t n_rule;
uint8_t env[SB_ENV_MAX]; // SB_ENV_LPP of this page
uint8_t env_len;
// Value of scalar `id`, `dflt` when absent (an absent counter is 0).
uint32_t get(uint8_t id, uint32_t dflt = 0) const {
for (uint8_t i = 0; i < n_sc; i++) if (sc[i].id == id) return sc[i].v;
@@ -290,6 +334,10 @@ inline bool sbDecode(const uint8_t* b, int len, StatsBundleOut* o) {
o->ctr[o->n_ctr].passed = pa;
o->n_ctr++;
}
} else if (id == SB_ENV_LPP) {
if (bl > SB_ENV_MAX) return false;
memcpy(o->env, body, bl);
o->env_len = (uint8_t)bl;
} else if (id == SB_RULE) {
if (o->n_rule >= FLOW_MAX || !sbDecodeRule(body, (int)bl, &o->rule[o->n_rule])) return false;
o->n_rule++;
@@ -177,6 +177,99 @@ TEST(StatsBundle, BoundaryLengths149_150_151) {
}
}
// Sensors: SB_ENV_LPP carries the CayenneLPP bytes without channel 1 (docs/stats-bundle.md).
// Cases and sizes mirror admin_sensors*, readonly_sensors in the vectors.
static const uint8_t LPP_FULL_3[] = {0x01, 0x74, 0x01, 0x90, 0x01, 0x67, 0x00, 0x00, // ch 1: battery, MCU temp
0x02, 0x67, 0x00, 0xE1, 0x02, 0x68, 0x3C, 0x02, 0x73, 0x27, 0x1C};
static int envFrom(const uint8_t* lpp, int n, uint8_t* out) { return sbLppExclude(lpp, n, 1, out, SB_ENV_MAX); }
TEST(StatsBundleEnv, LppExcludeDropsChannelOneAndStopsOnUnknown) {
uint8_t e[SB_ENV_MAX];
int n = envFrom(LPP_FULL_3, sizeof(LPP_FULL_3), e);
EXPECT_EQ("026700e102683c0273271c", hex(e, n));
uint8_t junk[sizeof(LPP_FULL_3) + 3];
memcpy(junk, LPP_FULL_3, sizeof(LPP_FULL_3));
junk[sizeof(LPP_FULL_3)] = 9; junk[sizeof(LPP_FULL_3) + 1] = 0xFF; junk[sizeof(LPP_FULL_3) + 2] = 0;
EXPECT_EQ(n, envFrom(junk, sizeof(junk), e));
EXPECT_EQ(0, envFrom(LPP_FULL_3, 8, e)); // only channel 1: nothing
EXPECT_EQ(0, envFrom(LPP_FULL_3, 3, e)); // truncated reading
uint8_t many_[40 * 4];
for (int i = 0; i < 40; i++) { many_[i * 4] = (uint8_t)(2 + i); many_[i * 4 + 1] = 103; many_[i * 4 + 2] = 0; many_[i * 4 + 3] = 1; }
EXPECT_EQ(SB_ENV_MAX, envFrom(many_, sizeof(many_), e)); // whole readings, up to the cap
}
TEST(StatsBundleEnv, SensorsOnPage0) {
uint8_t e[SB_ENV_MAX], o[200];
int el = envFrom(LPP_FULL_3, sizeof(LPP_FULL_3), e);
StatsBundleIn in;
fillStatus(in);
FlowTable t; t.clearAll();
in.admin = true; in.flow = &t; in.prefs_crc = 0xBEEF; in.pwr_flags = SB_PWR_RXGAIN_ACTIVE;
in.fair[0] = 31; in.fair[1] = 4; in.fair[3] = 120;
in.pwr_flags = 2; in.env = e; in.env_len = (uint8_t)el;
int n = enc(in, 0, o);
EXPECT_EQ(93, n); EXPECT_EQ(0x6A05, crc(o, n)); // empty table: 11 B env + 2 B header
StatsBundleOut d; ASSERT_TRUE(sbDecode(o, n, &d));
EXPECT_EQ(1, d.pages);
ASSERT_EQ(11, d.env_len);
EXPECT_EQ("026700e102683c0273271c", hex(d.env, d.env_len));
twoRules(t);
n = enc(in, 0, o);
EXPECT_EQ(129, n); EXPECT_EQ(0xFF10, crc(o, n)); // status + 2 rules + 3 sensors, one page
ASSERT_TRUE(sbDecode(o, n, &d));
EXPECT_EQ(1, d.pages); EXPECT_EQ(2, d.n_rule); EXPECT_EQ(11, d.env_len);
in.env = NULL; in.env_len = 0; // no sensors: no record
n = enc(in, 0, o);
EXPECT_EQ(116, n);
ASSERT_TRUE(sbDecode(o, n, &d));
EXPECT_EQ(0, d.env_len);
}
TEST(StatsBundleEnv, BigRecordSpillsToPage1) {
uint8_t e[124], o[200];
for (int i = 0; i < 31; i++) { e[i * 4] = (uint8_t)(2 + i); e[i * 4 + 1] = 103; e[i * 4 + 2] = 0; e[i * 4 + 3] = (uint8_t)(202 + i); }
StatsBundleIn in;
fillStatus(in);
FlowTable t; twoRules(t);
in.admin = true; in.flow = &t; in.prefs_crc = 0xBEEF; in.pwr_flags = 2;
in.fair[0] = 31; in.fair[1] = 4; in.fair[3] = 120;
in.env = e; in.env_len = 124;
const int len[3] = {82, 145, 35};
const uint16_t c[3] = {0xA16B, 0xBDF8, 0x1083};
StatsBundleOut d[3];
for (int p = 0; p < 3; p++) {
int n = enc(in, p, o);
EXPECT_EQ(len[p], n); EXPECT_EQ(c[p], crc(o, n));
ASSERT_TRUE(sbDecode(o, n, &d[p]));
EXPECT_EQ(3, d[p].pages);
}
EXPECT_EQ(-1, enc(in, 3, o));
EXPECT_EQ(0, d[0].env_len); // page 0 keeps status + CRCs, the record is whole on page 1
EXPECT_EQ(124, d[1].env_len);
EXPECT_EQ(0, memcmp(e, d[1].env, 124));
EXPECT_EQ(2, d[1].n_ctr + d[2].n_ctr);
EXPECT_EQ(2, d[2].n_rule);
in.env_len = SB_ENV_MAX + 1; // over the cap: dropped, never truncated
int n = enc(in, 0, o);
ASSERT_TRUE(sbDecode(o, n, &d[0]));
EXPECT_EQ(0, d[0].env_len);
EXPECT_EQ(1, d[0].pages);
}
TEST(StatsBundleEnv, ReadOnlyGetsSensorsOnOnePage) {
uint8_t e[SB_ENV_MAX], o[200];
int el = envFrom(LPP_FULL_3, sizeof(LPP_FULL_3), e);
StatsBundleIn in;
fillStatus(in);
FlowTable t; twoRules(t);
in.admin = false; in.flow = &t; in.env = e; in.env_len = (uint8_t)el;
int n = enc(in, 0, o);
EXPECT_EQ(73, n); EXPECT_EQ(0x9166, crc(o, n));
StatsBundleOut d; ASSERT_TRUE(sbDecode(o, n, &d));
EXPECT_EQ(11, d.env_len); EXPECT_FALSE(d.has(SB_RULES_AUTO));
}
TEST(StatsBundle, NeverExceedsBudget) {
StatsBundleIn in;
fillStatus(in);
+86 -2
View File
@@ -26,7 +26,24 @@ STATUS_IDS = {
ZIGZAG = {"temp_dc", "noise", "rssi", "snr4"}
FAIR_IDS = {"group": 24, "sender": 25, "advert": 26, "window": 27}
ID_RULES_AUTO, ID_RULES_STATIC, ID_PREFS_CRC, ID_PWR, ID_N_COUNTERS = 32, 33, 34, 35, 36
ID_COUNTERS, ID_RULE = 40, 41
ID_COUNTERS, ID_RULE, ID_ENV_LPP = 40, 41, 42
ENV_MAX = 128 # longest env record body (SB_ENV_MAX)
SELF_CHANNEL = 1 # LPP channel of the node itself (battery, MCU temperature): not in the env record
# CayenneLPP: type -> (name, data size, signed, divisor); divisor 1 = raw integer
LPP_TYPES = {
0: ("digital_input", 1, False, 1), 1: ("digital_output", 1, False, 1),
2: ("analog_input", 2, True, 100), 3: ("analog_output", 2, True, 100),
100: ("generic", 4, False, 1), 101: ("luminosity", 2, False, 1), 102: ("presence", 1, False, 1),
103: ("temperature", 2, True, 10), 104: ("humidity", 1, False, 2), 115: ("pressure", 2, False, 10),
116: ("voltage", 2, False, 100), 117: ("current", 2, False, 1000), 118: ("frequency", 4, False, 1),
120: ("percentage", 1, False, 1), 121: ("altitude", 2, True, 1), 125: ("concentration", 2, False, 1),
128: ("power", 2, False, 1), 130: ("distance", 4, False, 1000), 131: ("energy", 4, False, 1000),
132: ("direction", 2, False, 1), 133: ("unixtime", 4, False, 1), 142: ("switch", 1, False, 1),
# multi-value types are kept as raw bytes
113: ("accelerometer", 6, None, 1), 134: ("gyrometer", 6, None, 1), 135: ("colour", 3, None, 1),
136: ("gps", 9, None, 1),
}
def crc16_ccitt_false(data: bytes) -> int:
@@ -68,6 +85,44 @@ def read_varint(b: bytes, pos: int):
raise ValueError("varint too long")
# ---- CayenneLPP (the env record) -------------------------------------------
def lpp_exclude(buf: bytes, skip: int = SELF_CHANNEL, max_len: int = ENV_MAX) -> bytes:
"""The readings of an LPP buffer except channel `skip`, whole readings, <= max_len bytes;
stops at an unknown type (what the firmware puts in the env record, sbLppExclude)."""
out, p = b"", 0
while p + 2 <= len(buf):
t = LPP_TYPES.get(buf[p + 1])
if t is None or p + 2 + t[1] > len(buf):
break
if buf[p] != skip:
if len(out) + 2 + t[1] > max_len:
break
out += buf[p:p + 2 + t[1]]
p += 2 + t[1]
return out
def lpp_decode(buf: bytes):
"""[{channel, type, name, value}] ; value is a number (scaled), or hex for multi-value types.
Stops at an unknown type. Same scaling as the Cayenne LPP / meshcore telemetry decoder."""
out, p = [], 0
while p + 2 <= len(buf):
t = LPP_TYPES.get(buf[p + 1])
if t is None or p + 2 + t[1] > len(buf):
break
name, size, signed, div = t
raw = buf[p + 2:p + 2 + size]
if signed is None:
val = raw.hex()
else:
n = int.from_bytes(raw, "big", signed=signed)
val = n / div if div != 1 else n
out.append({"channel": buf[p], "type": buf[p + 1], "name": name, "value": val})
p += 2 + size
return out
# ---- flow rules (same canonical order and CRC as flow_rules_vectors) -------
def sort_key(e):
@@ -121,7 +176,12 @@ def records(case):
v = case["status"].get(name, 0)
if v:
recs.append(scalar(id_, zigzag(v) if name in ZIGZAG else v))
env = bytes.fromhex(case.get("env", ""))
if env and len(env) > ENV_MAX:
env = b""
if not case["admin"]:
if env:
recs.append(blob(ID_ENV_LPP, env))
return recs, []
for name, id_ in FAIR_IDS.items():
v = case.get("fair", {}).get(name, 0)
@@ -139,6 +199,8 @@ def records(case):
for i, e in enumerate(entries) if e.get("dropped", 0) or e.get("passed", 0)]
if trip:
recs.append(scalar(ID_N_COUNTERS, len(trip)))
if env: # sensors ahead of the counters and rules: page 0 is read every scrape
recs.append(blob(ID_ENV_LPP, env))
return recs, (trip, [blob(ID_RULE, rule_record(i, e)) for i, e in enumerate(entries)])
@@ -209,6 +271,9 @@ def decode_page(b: bytes):
out["counters"].append((s, d, p))
elif id_ == ID_RULE:
out["rules"].append(decode_rule(body))
elif id_ == ID_ENV_LPP:
out["env"] = body.hex()
out["env_lpp"] = lpp_decode(body)
else:
raise ValueError("bad wire type")
return out
@@ -310,6 +375,13 @@ TWO_RULES = [
]
# LPP as the node builds it: ch 1 = battery + MCU temp (not in the record), ch 2 = BME280-like sensor
LPP_FULL_3 = bytes.fromhex("01740190" "01670000" "026700e1" "02683c" "0273271c")
LPP_ENV_3 = lpp_exclude(LPP_FULL_3) # temp 22.5 C, humidity 30 %, pressure 1001.2 hPa = 11 B
# a node with an external sensor, a power meter and many channels: 28 readings fill the record
LPP_ENV_BIG = b"".join(bytes([ch, 103]) + (200 + ch).to_bytes(2, "big") for ch in range(2, 2 + 31))
def many_rules(n, dropped, passed=0):
return [{"kind": "a", "key": "%08x" % (0x11000000 + i), "mode": "throttle", "secs": 1800, "auto": i % 2 == 0,
"expires": 1800000000 + i, "remain": 3600 * (i + 1), "dropped": dropped + i, "passed": passed}
@@ -339,6 +411,14 @@ def cases():
"rules": []}),
("admin_two_rules", {"admin": True, "status": STATUS, "fair": {"group": 31, "sender": 4, "window": 120},
"prefs_crc": 0xBEEF, "pwr": 2, "rules": TWO_RULES}),
("admin_sensors", {"admin": True, "status": STATUS, "fair": {"group": 31, "sender": 4, "window": 120},
"prefs_crc": 0xBEEF, "pwr": 2, "rules": [], "env": LPP_ENV_3.hex()}),
("admin_sensors_two_rules", {"admin": True, "status": STATUS, "fair": {"group": 31, "sender": 4, "window": 120},
"prefs_crc": 0xBEEF, "pwr": 2, "rules": TWO_RULES, "env": LPP_ENV_3.hex()}),
("admin_sensors_spill", {"admin": True, "status": STATUS, "fair": {"group": 31, "sender": 4, "window": 120},
"prefs_crc": 0xBEEF, "pwr": 2, "rules": TWO_RULES, "env": LPP_ENV_BIG[:124].hex()}),
("readonly_sensors", {"admin": False, "status": STATUS, "rules": TWO_RULES, "prefs_crc": 1,
"env": LPP_ENV_3.hex()}),
("admin_16_rules", {"admin": True, "status": STATUS, "fair": {"group": 31, "sender": 4, "window": 120},
"prefs_crc": 0xBEEF, "pwr": 3, "rules": many_rules(16, 100000, 7)}),
]
@@ -354,7 +434,7 @@ def build():
"format": {"version": VERSION, "page_max": PAGE_MAX, "request": "F1 <page>"},
"ids": {"status": STATUS_IDS, "fair": FAIR_IDS, "rules_auto": ID_RULES_AUTO, "rules_static": ID_RULES_STATIC,
"prefs_crc": ID_PREFS_CRC, "pwr": ID_PWR, "n_counters": ID_N_COUNTERS, "counters": ID_COUNTERS,
"rule": ID_RULE},
"rule": ID_RULE, "env_lpp": ID_ENV_LPP},
"prefs": [],
"cases": [],
}
@@ -373,6 +453,10 @@ def build():
def selftest():
assert LPP_ENV_3.hex() == "026700e102683c0273271c", LPP_ENV_3.hex()
assert [(r["name"], r["value"]) for r in lpp_decode(LPP_ENV_3)] == [
("temperature", 22.5), ("humidity", 30.0), ("pressure", 1001.2)]
assert lpp_exclude(LPP_FULL_3 + b"\x09\xff\x00") == LPP_ENV_3 # unknown type stops the walk
for name, case in cases():
pages = encode_pages(case)
assert all(len(p) <= PAGE_MAX for p in pages), name
+704
View File
@@ -245,6 +245,709 @@
}
]
},
{
"decoded": [
{
"counters": [],
"env": "026700e102683c0273271c",
"env_lpp": [
{
"channel": 2,
"name": "temperature",
"type": 103,
"value": 22.5
},
{
"channel": 2,
"name": "humidity",
"type": 104,
"value": 30.0
},
{
"channel": 2,
"name": "pressure",
"type": 115,
"value": 1001.2
}
],
"page": 0,
"pages": 1,
"rules": [],
"scalars": {
"1": 3987,
"10": 148233,
"11": 9120,
"12": 8011,
"13": 1109,
"14": 130011,
"15": 18222,
"17": 412,
"18": 20991,
"19": 3071,
"2": 430,
"21": 2,
"24": 31,
"25": 4,
"27": 120,
"3": 1234567,
"32": 65535,
"33": 65535,
"34": 48879,
"35": 2,
"4": 842,
"5": 51234,
"6": 225,
"7": 193,
"8": 43
}
}
],
"input": {
"admin": true,
"env": "026700e102683c0273271c",
"fair": {
"group": 31,
"sender": 4,
"window": 120
},
"prefs_crc": 48879,
"pwr": 2,
"rules": [],
"status": {
"air_rx": 51234,
"air_tx": 842,
"batt_mv": 3987,
"direct_dups": 412,
"err_events": 0,
"flood_dups": 20991,
"noise": -113,
"recv": 148233,
"recv_direct": 18222,
"recv_errors": 3071,
"recv_flood": 130011,
"rssi": -97,
"sent": 9120,
"sent_direct": 1109,
"sent_flood": 8011,
"snr4": -22,
"temp_dc": 215,
"tx_queue": 0,
"tx_start_fail": 0,
"tx_timeout": 2,
"uptime": 1234567
}
},
"name": "admin_sensors",
"pages": [
{
"crc16": "6A05",
"hex": "010104931f08ae030c87ad4b10ca0614a2900318e1011cc101202b288986092ca04730cb3e34d50838dbf7073cae8e01449c0348ffa3014cff175402601f64046c7880ffff0384ffff0388effd028c02a90b026700e102683c0273271c",
"len": 93
}
]
},
{
"decoded": [
{
"counters": [
[
0,
416,
14
],
[
1,
37,
0
]
],
"env": "026700e102683c0273271c",
"env_lpp": [
{
"channel": 2,
"name": "temperature",
"type": 103,
"value": 22.5
},
{
"channel": 2,
"name": "humidity",
"type": 104,
"value": 30.0
},
{
"channel": 2,
"name": "pressure",
"type": 115,
"value": 1001.2
}
],
"page": 0,
"pages": 1,
"rules": [
{
"auto": false,
"key": "2e",
"kind": "c",
"left": 604800,
"mode": "throttle",
"secs": 1800,
"slot": 0
},
{
"auto": true,
"expires": 1800000000,
"key": "8b2c",
"kind": "r",
"mode": "block",
"slot": 1
}
],
"scalars": {
"1": 3987,
"10": 148233,
"11": 9120,
"12": 8011,
"13": 1109,
"14": 130011,
"15": 18222,
"17": 412,
"18": 20991,
"19": 3071,
"2": 430,
"21": 2,
"24": 31,
"25": 4,
"27": 120,
"3": 1234567,
"32": 96198,
"33": 92086,
"34": 48879,
"35": 2,
"36": 2,
"4": 842,
"5": 51234,
"6": 225,
"7": 193,
"8": 43
}
}
],
"input": {
"admin": true,
"env": "026700e102683c0273271c",
"fair": {
"group": 31,
"sender": 4,
"window": 120
},
"prefs_crc": 48879,
"pwr": 2,
"rules": [
{
"dropped": 416,
"key": "2e",
"kind": "c",
"mode": "throttle",
"passed": 14,
"remain": 604800,
"secs": 1800
},
{
"auto": true,
"dropped": 37,
"expires": 1800000000,
"key": "8b2c",
"kind": "r",
"mode": "block"
}
],
"status": {
"air_rx": 51234,
"air_tx": 842,
"batt_mv": 3987,
"direct_dups": 412,
"err_events": 0,
"flood_dups": 20991,
"noise": -113,
"recv": 148233,
"recv_direct": 18222,
"recv_errors": 3071,
"recv_flood": 130011,
"rssi": -97,
"sent": 9120,
"sent_direct": 1109,
"sent_flood": 8011,
"snr4": -22,
"temp_dc": 215,
"tx_queue": 0,
"tx_start_fail": 0,
"tx_timeout": 2,
"uptime": 1234567
}
},
"name": "admin_sensors_two_rules",
"pages": [
{
"crc16": "FF10",
"hex": "010104931f08ae030c87ad4b10ca0614a2900318e1011cc101202b288986092ca04730cb3e34d50838dbf7073cae8e01449c0348ffa3014cff175402601f64046c7880c6ef0584b6cf0588effd028c029002a90b026700e102683c0273271ca10700a0030e012500a50a0063012e11880e80f524a50b0172028b2c0c80a4a7da06",
"len": 129
}
]
},
{
"decoded": [
{
"counters": [],
"page": 0,
"pages": 3,
"rules": [],
"scalars": {
"1": 3987,
"10": 148233,
"11": 9120,
"12": 8011,
"13": 1109,
"14": 130011,
"15": 18222,
"17": 412,
"18": 20991,
"19": 3071,
"2": 430,
"21": 2,
"24": 31,
"25": 4,
"27": 120,
"3": 1234567,
"32": 96198,
"33": 92086,
"34": 48879,
"35": 2,
"36": 2,
"4": 842,
"5": 51234,
"6": 225,
"7": 193,
"8": 43
}
},
{
"counters": [
[
0,
416,
14
],
[
1,
37,
0
]
],
"env": "026700ca036700cb046700cc056700cd066700ce076700cf086700d0096700d10a6700d20b6700d30c6700d40d6700d50e6700d60f6700d7106700d8116700d9126700da136700db146700dc156700dd166700de176700df186700e0196700e11a6700e21b6700e31c6700e41d6700e51e6700e61f6700e7206700e8",
"env_lpp": [
{
"channel": 2,
"name": "temperature",
"type": 103,
"value": 20.2
},
{
"channel": 3,
"name": "temperature",
"type": 103,
"value": 20.3
},
{
"channel": 4,
"name": "temperature",
"type": 103,
"value": 20.4
},
{
"channel": 5,
"name": "temperature",
"type": 103,
"value": 20.5
},
{
"channel": 6,
"name": "temperature",
"type": 103,
"value": 20.6
},
{
"channel": 7,
"name": "temperature",
"type": 103,
"value": 20.7
},
{
"channel": 8,
"name": "temperature",
"type": 103,
"value": 20.8
},
{
"channel": 9,
"name": "temperature",
"type": 103,
"value": 20.9
},
{
"channel": 10,
"name": "temperature",
"type": 103,
"value": 21.0
},
{
"channel": 11,
"name": "temperature",
"type": 103,
"value": 21.1
},
{
"channel": 12,
"name": "temperature",
"type": 103,
"value": 21.2
},
{
"channel": 13,
"name": "temperature",
"type": 103,
"value": 21.3
},
{
"channel": 14,
"name": "temperature",
"type": 103,
"value": 21.4
},
{
"channel": 15,
"name": "temperature",
"type": 103,
"value": 21.5
},
{
"channel": 16,
"name": "temperature",
"type": 103,
"value": 21.6
},
{
"channel": 17,
"name": "temperature",
"type": 103,
"value": 21.7
},
{
"channel": 18,
"name": "temperature",
"type": 103,
"value": 21.8
},
{
"channel": 19,
"name": "temperature",
"type": 103,
"value": 21.9
},
{
"channel": 20,
"name": "temperature",
"type": 103,
"value": 22.0
},
{
"channel": 21,
"name": "temperature",
"type": 103,
"value": 22.1
},
{
"channel": 22,
"name": "temperature",
"type": 103,
"value": 22.2
},
{
"channel": 23,
"name": "temperature",
"type": 103,
"value": 22.3
},
{
"channel": 24,
"name": "temperature",
"type": 103,
"value": 22.4
},
{
"channel": 25,
"name": "temperature",
"type": 103,
"value": 22.5
},
{
"channel": 26,
"name": "temperature",
"type": 103,
"value": 22.6
},
{
"channel": 27,
"name": "temperature",
"type": 103,
"value": 22.7
},
{
"channel": 28,
"name": "temperature",
"type": 103,
"value": 22.8
},
{
"channel": 29,
"name": "temperature",
"type": 103,
"value": 22.9
},
{
"channel": 30,
"name": "temperature",
"type": 103,
"value": 23.0
},
{
"channel": 31,
"name": "temperature",
"type": 103,
"value": 23.1
},
{
"channel": 32,
"name": "temperature",
"type": 103,
"value": 23.2
}
],
"page": 1,
"pages": 3,
"rules": [],
"scalars": {
"32": 96198,
"33": 92086
}
},
{
"counters": [],
"page": 2,
"pages": 3,
"rules": [
{
"auto": false,
"key": "2e",
"kind": "c",
"left": 604800,
"mode": "throttle",
"secs": 1800,
"slot": 0
},
{
"auto": true,
"expires": 1800000000,
"key": "8b2c",
"kind": "r",
"mode": "block",
"slot": 1
}
],
"scalars": {
"32": 96198,
"33": 92086
}
}
],
"input": {
"admin": true,
"env": "026700ca036700cb046700cc056700cd066700ce076700cf086700d0096700d10a6700d20b6700d30c6700d40d6700d50e6700d60f6700d7106700d8116700d9126700da136700db146700dc156700dd166700de176700df186700e0196700e11a6700e21b6700e31c6700e41d6700e51e6700e61f6700e7206700e8",
"fair": {
"group": 31,
"sender": 4,
"window": 120
},
"prefs_crc": 48879,
"pwr": 2,
"rules": [
{
"dropped": 416,
"key": "2e",
"kind": "c",
"mode": "throttle",
"passed": 14,
"remain": 604800,
"secs": 1800
},
{
"auto": true,
"dropped": 37,
"expires": 1800000000,
"key": "8b2c",
"kind": "r",
"mode": "block"
}
],
"status": {
"air_rx": 51234,
"air_tx": 842,
"batt_mv": 3987,
"direct_dups": 412,
"err_events": 0,
"flood_dups": 20991,
"noise": -113,
"recv": 148233,
"recv_direct": 18222,
"recv_errors": 3071,
"recv_flood": 130011,
"rssi": -97,
"sent": 9120,
"sent_direct": 1109,
"sent_flood": 8011,
"snr4": -22,
"temp_dc": 215,
"tx_queue": 0,
"tx_start_fail": 0,
"tx_timeout": 2,
"uptime": 1234567
}
},
"name": "admin_sensors_spill",
"pages": [
{
"crc16": "A16B",
"hex": "010304931f08ae030c87ad4b10ca0614a2900318e1011cc101202b288986092ca04730cb3e34d50838dbf7073cae8e01449c0348ffa3014cff175402601f64046c7880c6ef0584b6cf0588effd028c029002",
"len": 82
},
{
"crc16": "BDF8",
"hex": "011380c6ef0584b6cf05a97c026700ca036700cb046700cc056700cd066700ce076700cf086700d0096700d10a6700d20b6700d30c6700d40d6700d50e6700d60f6700d7106700d8116700d9126700da136700db146700dc156700dd166700de176700df186700e0196700e11a6700e21b6700e31c6700e41d6700e51e6700e61f6700e7206700e8a10700a0030e012500",
"len": 145
},
{
"crc16": "1083",
"hex": "012380c6ef0584b6cf05a50a0063012e11880e80f524a50b0172028b2c0c80a4a7da06",
"len": 35
}
]
},
{
"decoded": [
{
"counters": [],
"env": "026700e102683c0273271c",
"env_lpp": [
{
"channel": 2,
"name": "temperature",
"type": 103,
"value": 22.5
},
{
"channel": 2,
"name": "humidity",
"type": 104,
"value": 30.0
},
{
"channel": 2,
"name": "pressure",
"type": 115,
"value": 1001.2
}
],
"page": 0,
"pages": 1,
"rules": [],
"scalars": {
"1": 3987,
"10": 148233,
"11": 9120,
"12": 8011,
"13": 1109,
"14": 130011,
"15": 18222,
"17": 412,
"18": 20991,
"19": 3071,
"2": 430,
"21": 2,
"3": 1234567,
"4": 842,
"5": 51234,
"6": 225,
"7": 193,
"8": 43
}
}
],
"input": {
"admin": false,
"env": "026700e102683c0273271c",
"prefs_crc": 1,
"rules": [
{
"dropped": 416,
"key": "2e",
"kind": "c",
"mode": "throttle",
"passed": 14,
"remain": 604800,
"secs": 1800
},
{
"auto": true,
"dropped": 37,
"expires": 1800000000,
"key": "8b2c",
"kind": "r",
"mode": "block"
}
],
"status": {
"air_rx": 51234,
"air_tx": 842,
"batt_mv": 3987,
"direct_dups": 412,
"err_events": 0,
"flood_dups": 20991,
"noise": -113,
"recv": 148233,
"recv_direct": 18222,
"recv_errors": 3071,
"recv_flood": 130011,
"rssi": -97,
"sent": 9120,
"sent_direct": 1109,
"sent_flood": 8011,
"snr4": -22,
"temp_dc": 215,
"tx_queue": 0,
"tx_start_fail": 0,
"tx_timeout": 2,
"uptime": 1234567
}
},
"name": "readonly_sensors",
"pages": [
{
"crc16": "9166",
"hex": "010104931f08ae030c87ad4b10ca0614a2900318e1011cc101202b288986092ca04730cb3e34d50838dbf7073cae8e01449c0348ffa3014cff175402a90b026700e102683c0273271c",
"len": 73
}
]
},
{
"decoded": [
{
@@ -1296,6 +1999,7 @@
},
"ids": {
"counters": 40,
"env_lpp": 42,
"fair": {
"advert": 26,
"group": 24,