Merge feat/stats-bundle-env into main
This commit is contained in:
commit
a2a2b67ee3
7 files changed
+997
-16
No files matched your search
@@ -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++;
|
||||
|
||||
Reference in new issue
Block a user