From c5fb0fb4ce0f73d1a35e1b053eb85af524754816 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Thu, 16 Jul 2026 20:50:25 +0200 Subject: [PATCH] =?UTF-8?q?feat(repeater/analyzer):=20pktfeed=20ring=20+?= =?UTF-8?q?=20`pktlog`=20cmd=20=E2=80=94=20raw=20frames=20for=20the=20comm?= =?UTF-8?q?unity=20observer=20feed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Small sibling ring to rxlog (PKTFEED_SIZE=32, ~8.6 KB) that keeps the FULL raw frame bytes: staged pre-parse in logRxRaw, committed post-parse in logRx (radio garbage never enters). Paged `pktlog [cursor]` mirrors rxlog paging; the bridge derives route/type/payload and the MeshCore SHA packet hash (analyzer correlation key) from raw, so the radio hot path stays SHA-free. No 'always emit >=1' here: a ~550 B record line would overflow the 150 B mesh-CLI budget — there the reply degrades to a bare trailer. Co-Authored-By: Claude Fable 5 --- examples/simple_repeater/MyMesh.cpp | 98 ++++++++++++++++++++++++++ examples/simple_repeater/MyMesh.h | 36 +++++++++- variants/sensecap_solar/platformio.ini | 5 ++ 3 files changed, 138 insertions(+), 1 deletion(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 55f1e608..ebc62bc3 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -273,6 +273,84 @@ void MyMesh::formatRxLogReply(char* reply, int max_len, uint32_t cursor) { } #endif +#if PKTFEED_SIZE +// CUSTOM (TeTeHacko): observer feed -- rxlog's sibling ring that keeps the FULL +// raw frame so the bridge can publish meshcoretomqtt-compatible packets (the +// SHA256 packet hash and type/route/payload decode all happen bridge-side). +void MyMesh::stagePktFeed(const uint8_t raw[], int len) { + if (len <= 0 || len > MAX_TRANS_UNIT) { + pktfeed_stage_len = 0; + return; + } + memcpy(pktfeed_stage, raw, len); + pktfeed_stage_len = (uint8_t) len; +} + +void MyMesh::commitPktFeed(const mesh::Packet* pkt) { + // stage/commit must be the same frame: both run in order inside one + // Dispatcher::checkRecv pass, but a parse-failed frame leaves a stale stage + // behind -- the length check discards such a mismatched pairing + if (pktfeed_stage_len == 0 || pktfeed_stage_len != pkt->getRawLength()) { + pktfeed_stage_len = 0; + return; + } + PktFeedRec* r = &pktfeed_ring[pktfeed_head]; + pktfeed_head = (pktfeed_head + 1) % PKTFEED_SIZE; + r->seq = pktfeed_next_seq++; + if (pktfeed_next_seq == 0) pktfeed_next_seq = 1; // wrap guard (seq 0 reserved for empty) + r->when = getRTCClock()->getCurrentTime(); + r->snr = pkt->_snr; + r->rssi = (int8_t) pkt->getRSSI(); + r->len = pktfeed_stage_len; + memcpy(r->raw, pktfeed_stage, pktfeed_stage_len); + pktfeed_stage_len = 0; +} + +// `pktlog [cursor]` -> paged dump of raw frames with seq > cursor (oldest first). +// One text line per record: P,,,,, +// Trailer line: E,,, +void MyMesh::formatPktFeedReply(char* reply, int max_len, uint32_t cursor) { + char* dp = reply; + PktFeedRec* recs[PKTFEED_SIZE]; // small ring -> stack is fine (32 ptrs) + int n = 0; + uint32_t oldest = 0, newest = 0; + for (int i = 0; i < PKTFEED_SIZE; i++) { + PktFeedRec* r = &pktfeed_ring[i]; + if (r->seq == 0) continue; + if (oldest == 0 || r->seq < oldest) oldest = r->seq; + if (r->seq > newest) newest = r->seq; + if (r->seq > cursor) recs[n++] = r; + } + std::sort(recs, recs + n, [](const PktFeedRec* a, const PktFeedRec* b) { + return a->seq < b->seq; // oldest unread first + }); + + uint32_t last = cursor; + static char line[40 + MAX_TRANS_UNIT * 2]; // BSS, ne stack (~550 B; volá se jen z loop tasku) + for (int i = 0; i < n; i++) { + PktFeedRec* r = recs[i]; + int ll = snprintf(line, sizeof(line), "P,%lu,%lu,%d,%d,", + (unsigned long)r->seq, (unsigned long)r->when, + (int)r->snr, (int)r->rssi); + for (int b = 0; b < r->len; b++) { + ll += sprintf(line + ll, "%02X", r->raw[b]); + } + line[ll++] = '\n'; + line[ll] = 0; + // Unlike rxlog there is NO "always emit >=1" here: a full-size record line + // (~550 B) exceeds the mesh admin CLI's 150 B budget and would overflow its + // reply buffer. There the reply degrades to a bare trailer (paging stalls); + // the intended consumer is the local console's ~1000 B pages, which always + // fit at least one record. + if ((dp - reply) + ll + 40 > max_len) break; // room for trailer + memcpy(dp, line, ll); dp += ll; + last = r->seq; + } + dp += sprintf(dp, "E,%lu,%lu,%lu", (unsigned long)last, (unsigned long)oldest, (unsigned long)newest); + *dp = 0; +} +#endif + uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood) { ClientInfo* client = NULL; if (data[0] == 0) { // blank password, just check if sender is in ACL @@ -650,6 +728,10 @@ const char *MyMesh::getLogDateTime() { } void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) { +#if PKTFEED_SIZE + // observer feed: raw bytes only exist here (pre-parse); committed in logRx + stagePktFeed(raw, len); +#endif #if MESH_PACKET_LOGGING Serial.print(getLogDateTime()); Serial.print(" RAW: "); @@ -665,6 +747,10 @@ void MyMesh::logRx(mesh::Packet *pkt, int len, float score) { // on the packet still belong to THIS frame. captureRx(pkt); #endif +#if PKTFEED_SIZE + // observer feed: pair the staged raw bytes (from logRxRaw) with this frame + commitPktFeed(pkt); +#endif #ifdef WITH_BRIDGE if (_prefs.bridge_pkt_src == 1) { bridge.sendPacket(pkt); @@ -1090,6 +1176,12 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc rxlog_head = 0; rxlog_next_seq = 1; // seq 0 is reserved for "empty slot" / initial cursor #endif +#if PKTFEED_SIZE + memset(pktfeed_ring, 0, sizeof(pktfeed_ring)); + pktfeed_head = 0; + pktfeed_next_seq = 1; // seq 0 is reserved for "empty slot" / initial cursor + pktfeed_stage_len = 0; +#endif // defaults memset(&_prefs, 0, sizeof(_prefs)); @@ -1489,6 +1581,12 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply // analyzer: paged firehose of every frame heard, since (0 = all) uint32_t cursor = (command[5] == ' ') ? (uint32_t) strtoul(&command[6], NULL, 10) : 0; formatRxLogReply(reply, reply_max, cursor); +#endif +#if PKTFEED_SIZE + } else if (memcmp(command, "pktlog", 6) == 0 && (command[6] == 0 || command[6] == ' ')) { + // observer feed: paged raw frames heard, since (0 = all) + uint32_t cursor = (command[6] == ' ') ? (uint32_t) strtoul(&command[7], NULL, 10) : 0; + formatPktFeedReply(reply, reply_max, cursor); #endif } else{ #if defined(BLE_PIN_CODE) && defined(NRF52_PLATFORM) diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 5f835ab5..93fe2f0f 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -82,7 +82,12 @@ struct NeighbourInfo { // capture seq, time, packet hash, header (route|type), SNR/RSSI and the hop // path. -> live traffic / analyzer view. // Exposed by the `rxlog [cursor]` CLI command. -// Enable per-variant, e.g.: -D MAX_HEARD_NODES=48 -D RXLOG_SIZE=96 +// * pktfeed_ring[] -- like rxlog but with the FULL RAW FRAME BYTES, for the +// community observer feed (meshcoretomqtt-compatible: the bridge derives +// type/route/payload and the SHA256 packet hash from the raw bytes). +// Smaller ring -- raw frames are fat (~268 B/record). +// Exposed by the `pktlog [cursor]` CLI command. +// Enable per-variant, e.g.: -D MAX_HEARD_NODES=48 -D RXLOG_SIZE=96 -D PKTFEED_SIZE=32 // --------------------------------------------------------------------------- #ifndef MAX_HEARD_NODES #define MAX_HEARD_NODES 0 @@ -90,6 +95,9 @@ struct NeighbourInfo { #ifndef RXLOG_SIZE #define RXLOG_SIZE 0 #endif +#ifndef PKTFEED_SIZE + #define PKTFEED_SIZE 0 +#endif #ifndef HEARD_NODE_NAME_LEN #define HEARD_NODE_NAME_LEN 24 #endif @@ -135,6 +143,17 @@ struct RxLogRec { }; #endif +#if PKTFEED_SIZE +struct PktFeedRec { + uint32_t seq; // monotonic capture sequence (0 = empty slot; also the paging cursor) + uint32_t when; // epoch + int8_t snr; // x4 + int8_t rssi; // dBm + uint8_t len; // raw frame length in bytes + uint8_t raw[MAX_TRANS_UNIT]; // full wire frame (header|[transport]|path_len|path|payload) +}; +#endif + #ifndef FIRMWARE_BUILD_DATE #define FIRMWARE_BUILD_DATE "6 Jun 2026" #endif @@ -180,6 +199,16 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { RxLogRec rxlog_ring[RXLOG_SIZE]; uint16_t rxlog_head; // next slot to write uint32_t rxlog_next_seq; // next capture sequence to assign (starts at 1) +#endif +#if PKTFEED_SIZE + PktFeedRec pktfeed_ring[PKTFEED_SIZE]; + uint16_t pktfeed_head; // next slot to write + uint32_t pktfeed_next_seq; // next capture sequence to assign (starts at 1) + // raw bytes staged in logRxRaw (pre-parse, the only place they exist) and + // committed to the ring in logRx (fires only for frames that parsed OK, so + // radio garbage never enters the feed) + uint8_t pktfeed_stage[MAX_TRANS_UNIT]; + uint8_t pktfeed_stage_len; // 0 = nothing staged #endif CayenneLPP telemetry; unsigned long set_radio_at, revert_radio_at; @@ -204,6 +233,11 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { #if RXLOG_SIZE void captureRx(const mesh::Packet* pkt); void formatRxLogReply(char* reply, int max_len, uint32_t cursor); +#endif +#if PKTFEED_SIZE + void stagePktFeed(const uint8_t raw[], int len); + void commitPktFeed(const mesh::Packet* pkt); + void formatPktFeedReply(char* reply, int max_len, uint32_t cursor); #endif uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood); uint8_t handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data); diff --git a/variants/sensecap_solar/platformio.ini b/variants/sensecap_solar/platformio.ini index 78c1a41b..9e9d9994 100644 --- a/variants/sensecap_solar/platformio.ini +++ b/variants/sensecap_solar/platformio.ini @@ -94,6 +94,11 @@ build_flags = ; neighbour advert period even on inversion evenings. -D MAX_HEARD_NODES=128 -D RXLOG_SIZE=512 + ; PKTFEED_SIZE 32 (~268 B/rec, 8.6 KB): observer feed ring -- like rxlog but + ; with full raw frame bytes (`pktlog` cmd) for the meshcoretomqtt-compatible + ; community analyzer feed. 32 records ≈ 4+ min of typical traffic between + ; 5 s bridge polls; drop detection via the E-trailer catches any overflow. + -D PKTFEED_SIZE=32 ; -D MESH_PACKET_LOGGING=1 build_src_filter = ${SenseCap_Solar.build_src_filter} +<../examples/simple_repeater/*.cpp>