From 44aa778e97fa2ab11e3c8a2ced2197f14238cac3 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Sun, 12 Jul 2026 00:19:32 +0200 Subject: [PATCH] feat(repeater): passive mesh analyzer -- `nodes` + `rxlog` over the console Two RAM-only, opt-in tables fed from the normal RX path and pulled out over the text console (serial / BLE) for a BLE bridge + map to consume. Both are compiled out unless the build sets a non-zero size; enabled on SenseCap_Solar_repeater_ble (-D MAX_HEARD_NODES=48 -D RXLOG_SIZE=96, ~6 KB RAM). nodes [offset] -- registry of heard REPEATERS and ROOM SERVERS (chat clients are deliberately skipped: personal/mobile devices, privacy). Per node: pubkey prefix, name, type, lat/lon, last SNR/RSSI, seconds since heard, and the path the advert arrived by. Fed from onAdvertRecv (adverts are signature-verified upstream). -> map dots + topology edges + link quality. rxlog [cursor] -- circular firehose of EVERY parsed frame heard, fed from logRx() (synchronous, so SNR/RSSI belong to that frame). Per record: monotonic seq (paging cursor), time, packet hash, header (route|type), SNR/RSSI, hop path. -> live traffic / analyzer view. Both replies are paged to stay within the ~160 B console reply cap and carry a trailer (next cursor + totals) so the bridge can drain and detect if it fell behind the ring. Reachable over serial, the PIN-paired BLE console and the admin mesh CLI, same as `neighbors`. Co-Authored-By: Claude Opus 4.8 --- examples/simple_repeater/MyMesh.cpp | 178 ++++++++++++++++++++++++- examples/simple_repeater/MyMesh.h | 78 +++++++++++ variants/sensecap_solar/platformio.ini | 6 + 3 files changed, 261 insertions(+), 1 deletion(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index f243e02d..43477699 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -87,6 +87,137 @@ void MyMesh::putNeighbour(const mesh::Identity &id, uint32_t timestamp, float sn #endif } +#if MAX_HEARD_NODES +// CUSTOM (TeTeHacko): record/update one heard node in the analyzer registry. +void MyMesh::putHeardNode(const mesh::Identity& id, uint32_t advert_timestamp, uint8_t type, + int32_t lat, int32_t lon, const char* name, + int8_t snr, int8_t rssi, const mesh::Packet* pkt) { + uint32_t now = getRTCClock()->getCurrentTime(); + uint32_t oldest = 0xFFFFFFFF; + HeardNode* slot = &heard_nodes[0]; + for (int i = 0; i < MAX_HEARD_NODES; i++) { + HeardNode* n = &heard_nodes[i]; + if (n->heard_timestamp > 0 && memcmp(n->pub_prefix, id.pub_key, HEARD_NODE_PREFIX) == 0) { + slot = n; break; // update existing entry + } + if (n->heard_timestamp < oldest) { oldest = n->heard_timestamp; slot = n; } // else evict LRU + } + memcpy(slot->pub_prefix, id.pub_key, HEARD_NODE_PREFIX); + slot->advert_timestamp = advert_timestamp; + slot->heard_timestamp = now; + slot->type = type; + slot->lat = lat; + slot->lon = lon; + slot->snr = snr; + slot->rssi = rssi; + StrHelper::strncpy(slot->name, name ? name : "", sizeof(slot->name)); + slot->path_len = (uint8_t) pkt->path_len; + uint8_t pb = pkt->getPathByteLen(); + if (pb > ANALYZER_PATH_LEN) pb = ANALYZER_PATH_LEN; + memset(slot->path, 0, sizeof(slot->path)); + if (pb) memcpy(slot->path, pkt->path, pb); +} + +// `nodes [offset]` -> paged dump of the heard-node registry (newest first). +// One text line per node: N,,,,,,,,, +// Trailer line: E,, +void MyMesh::formatNodesReply(char* reply, uint16_t offset) { + char* dp = reply; + int total = 0; + HeardNode* sorted[MAX_HEARD_NODES]; + for (int i = 0; i < MAX_HEARD_NODES; i++) { + if (heard_nodes[i].heard_timestamp > 0) sorted[total++] = &heard_nodes[i]; + } + std::sort(sorted, sorted + total, [](const HeardNode* a, const HeardNode* b) { + return a->heard_timestamp > b->heard_timestamp; // newest first + }); + + uint32_t now = getRTCClock()->getCurrentTime(); + int i = offset; + char line[160]; + for (; i < total; i++) { + HeardNode* n = sorted[i]; + char prefix[HEARD_NODE_PREFIX * 2 + 1]; + mesh::Utils::toHex(prefix, n->pub_prefix, HEARD_NODE_PREFIX); + char pathhex[ANALYZER_PATH_LEN * 2 + 1]; + uint8_t cnt = n->path_len & 63, sz = (n->path_len >> 6) + 1; + uint8_t pb = cnt * sz; if (pb > ANALYZER_PATH_LEN) pb = ANALYZER_PATH_LEN; + if (pb) mesh::Utils::toHex(pathhex, n->path, pb); else pathhex[0] = 0; + snprintf(line, sizeof(line), "N,%s,%u,%ld,%ld,%d,%d,%lu,%s,%s\n", + prefix, (unsigned)n->type, (long)n->lat, (long)n->lon, + (int)n->snr, (int)n->rssi, (unsigned long)(now - n->heard_timestamp), + pathhex, n->name); + int ll = strlen(line); + if (i > offset && (dp - reply) + ll + 24 > 150) break; // room for trailer; always emit >=1 + memcpy(dp, line, ll); dp += ll; + } + dp += sprintf(dp, "E,%d,%d", i, total); + *dp = 0; +} +#endif + +#if RXLOG_SIZE +// CUSTOM (TeTeHacko): append one heard frame to the analyzer firehose ring. +void MyMesh::captureRx(const mesh::Packet* pkt) { + RxLogRec* r = &rxlog_ring[rxlog_head]; + rxlog_head = (rxlog_head + 1) % RXLOG_SIZE; + r->seq = rxlog_next_seq++; + if (rxlog_next_seq == 0) rxlog_next_seq = 1; // wrap guard (seq 0 reserved for empty) + r->when = getRTCClock()->getCurrentTime(); + uint8_t h[MAX_HASH_SIZE]; + pkt->calculatePacketHash(h); + memcpy(&r->pkt_hash, h, 4); + r->snr = pkt->_snr; + r->rssi = (int8_t) pkt->getRSSI(); + r->header = pkt->header; + r->path_len = (uint8_t) pkt->path_len; + uint8_t pb = pkt->getPathByteLen(); + if (pb > ANALYZER_PATH_LEN) pb = ANALYZER_PATH_LEN; + memset(r->path, 0, sizeof(r->path)); + if (pb) memcpy(r->path, pkt->path, pb); +} + +// `rxlog [cursor]` -> paged dump of frames with seq > cursor (oldest first). +// One text line per record: R,,,,,,, +// Trailer line: E,,, +void MyMesh::formatRxLogReply(char* reply, uint32_t cursor) { + char* dp = reply; + RxLogRec* recs[RXLOG_SIZE]; + int n = 0; + uint32_t oldest = 0, newest = 0; + for (int i = 0; i < RXLOG_SIZE; i++) { + RxLogRec* r = &rxlog_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 RxLogRec* a, const RxLogRec* b) { + return a->seq < b->seq; // oldest unread first + }); + + uint32_t last = cursor; + char line[128]; + for (int i = 0; i < n; i++) { + RxLogRec* r = recs[i]; + char pathhex[ANALYZER_PATH_LEN * 2 + 1]; + uint8_t cnt = r->path_len & 63, sz = (r->path_len >> 6) + 1; + uint8_t pb = cnt * sz; if (pb > ANALYZER_PATH_LEN) pb = ANALYZER_PATH_LEN; + if (pb) mesh::Utils::toHex(pathhex, r->path, pb); else pathhex[0] = 0; + snprintf(line, sizeof(line), "R,%lu,%lu,%08lX,%02X,%d,%d,%s\n", + (unsigned long)r->seq, (unsigned long)r->when, + (unsigned long)r->pkt_hash, (unsigned)r->header, + (int)r->snr, (int)r->rssi, pathhex); + int ll = strlen(line); + if (i > 0 && (dp - reply) + ll + 40 > 150) break; // room for trailer; always emit >=1 + 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 @@ -473,6 +604,12 @@ void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) { } void MyMesh::logRx(mesh::Packet *pkt, int len, float score) { +#if RXLOG_SIZE + // analyzer firehose: every parsed frame we hear. Called synchronously from + // Dispatcher::checkRecv (before any score-delay queueing), so getRSSI()/SNR + // on the packet still belong to THIS frame. + captureRx(pkt); +#endif #ifdef WITH_BRIDGE if (_prefs.bridge_pkt_src == 1) { bridge.sendPacket(pkt); @@ -638,13 +775,32 @@ void MyMesh::onAdvertRecv(mesh::Packet *packet, const mesh::Identity &id, uint32 const uint8_t *app_data, size_t app_data_len) { mesh::Mesh::onAdvertRecv(packet, id, timestamp, app_data, app_data_len); // chain to super impl + AdvertDataParser parser(app_data, app_data_len); + // if this a zero hop advert (and not via 'Share'), add it to neighbours if (packet->getPathHashCount() == 0 && !isShare(packet)) { - AdvertDataParser parser(app_data, app_data_len); if (parser.isValid() && parser.getType() == ADV_TYPE_REPEATER) { // just keep neigbouring Repeaters putNeighbour(id, timestamp, packet->getSNR()); } } + +#if MAX_HEARD_NODES + // analyzer: record infrastructure nodes only -- REPEATERS and ROOM SERVERS, + // any hop count. Chat clients (and sensors) are deliberately NOT recorded: + // they are personal/mobile devices, so mapping them is a privacy concern and + // just churns the registry. The signature is already verified upstream + // (mesh::Mesh::onAdvertRecv only reaches here for valid adverts), so the + // identities/positions kept here are trustworthy. + uint8_t adv_type = parser.getType(); + if (parser.isValid() && !isShare(packet) + && (adv_type == ADV_TYPE_REPEATER || adv_type == ADV_TYPE_ROOM)) { + putHeardNode(id, timestamp, adv_type, + parser.hasLatLon() ? parser.getIntLat() : 0, + parser.hasLatLon() ? parser.getIntLon() : 0, + parser.hasName() ? parser.getName() : "", + packet->_snr, (int8_t)packet->getRSSI(), packet); + } +#endif } void MyMesh::onPeerDataRecv(mesh::Packet *packet, uint8_t type, int sender_idx, const uint8_t *secret, @@ -871,6 +1027,14 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc #if MAX_NEIGHBOURS memset(neighbours, 0, sizeof(neighbours)); #endif +#if MAX_HEARD_NODES + memset(heard_nodes, 0, sizeof(heard_nodes)); +#endif +#if RXLOG_SIZE + memset(rxlog_ring, 0, sizeof(rxlog_ring)); + rxlog_head = 0; + rxlog_next_seq = 1; // seq 0 is reserved for "empty slot" / initial cursor +#endif // defaults memset(&_prefs, 0, sizeof(_prefs)); @@ -1257,6 +1421,18 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply sendNodeDiscoverReq(); strcpy(reply, "OK - Discover sent"); } +#if MAX_HEARD_NODES + } else if (memcmp(command, "nodes", 5) == 0 && (command[5] == 0 || command[5] == ' ')) { + // analyzer: paged dump of heard repeaters/room servers (for a map/bridge) + uint16_t offset = (command[5] == ' ') ? (uint16_t) atoi(&command[6]) : 0; + formatNodesReply(reply, offset); +#endif +#if RXLOG_SIZE + } else if (memcmp(command, "rxlog", 5) == 0 && (command[5] == 0 || command[5] == ' ')) { + // 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, cursor); +#endif } else{ #if defined(BLE_PIN_CODE) && defined(NRF52_PLATFORM) // CUSTOM (TeTeHacko): `ble on|off|ble` — implemented in main.cpp (BLE console). diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index 7597c6c6..b7618bfb 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -68,6 +68,66 @@ struct NeighbourInfo { int8_t snr; // multiplied by 4, user should divide to get float value }; +// --------------------------------------------------------------------------- +// CUSTOM (TeTeHacko): passive "mesh analyzer" data. Two independent, RAM-only +// tables (both compiled out unless the build sets a non-zero size), fed from +// the normal RX path and pulled out over the text console (serial / BLE) for a +// map/bridge to consume: +// * heard_nodes[] -- one row per DISTINCT REPEATER / ROOM SERVER whose +// (verified) advert we heard (chat clients are intentionally skipped for +// privacy): pubkey prefix, name, type, position, last SNR/RSSI and the path +// the advert arrived by. -> map dots + topology edges + link quality. +// Exposed by the `nodes [offset]` CLI command. +// * rxlog_ring[] -- a circular firehose of EVERY parsed frame we hear: +// 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 +// --------------------------------------------------------------------------- +#ifndef MAX_HEARD_NODES + #define MAX_HEARD_NODES 0 +#endif +#ifndef RXLOG_SIZE + #define RXLOG_SIZE 0 +#endif +#ifndef HEARD_NODE_NAME_LEN + #define HEARD_NODE_NAME_LEN 24 +#endif +#ifndef HEARD_NODE_PREFIX + #define HEARD_NODE_PREFIX 6 // bytes of pub_key kept (resolve path hashes + registry cross-ref) +#endif +#ifndef ANALYZER_PATH_LEN + #define ANALYZER_PATH_LEN 16 // max hop-hash bytes kept per record +#endif + +#if MAX_HEARD_NODES +struct HeardNode { + uint8_t pub_prefix[HEARD_NODE_PREFIX]; + char name[HEARD_NODE_NAME_LEN]; + int32_t lat, lon; // degrees x 1e6, 0 = unknown + uint32_t heard_timestamp; // epoch when last heard (0 = empty slot) + uint32_t advert_timestamp; // advert's own timestamp (newest wins) + int8_t snr; // x4 + int8_t rssi; // dBm + uint8_t type; // ADV_TYPE_* + uint8_t path_len; // bit-packed (hash size|count) as heard + uint8_t path[ANALYZER_PATH_LEN]; +}; +#endif + +#if RXLOG_SIZE +struct RxLogRec { + uint32_t seq; // monotonic capture sequence (0 = empty slot; also the paging cursor) + uint32_t when; // epoch + uint32_t pkt_hash; // low 4 bytes of the packet hash (dedup / cross-observer correlate) + int8_t snr; // x4 + int8_t rssi; // dBm + uint8_t header; // raw header byte (route|type|ver) + uint8_t path_len; // bit-packed (hash size|count) + uint8_t path[ANALYZER_PATH_LEN]; +}; +#endif + #ifndef FIRMWARE_BUILD_DATE #define FIRMWARE_BUILD_DATE "6 Jun 2026" #endif @@ -105,6 +165,14 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { unsigned long dirty_contacts_expiry; #if MAX_NEIGHBOURS NeighbourInfo neighbours[MAX_NEIGHBOURS]; +#endif +#if MAX_HEARD_NODES + HeardNode heard_nodes[MAX_HEARD_NODES]; +#endif +#if RXLOG_SIZE + 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 CayenneLPP telemetry; unsigned long set_radio_at, revert_radio_at; @@ -120,6 +188,16 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { #endif void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr); +#if MAX_HEARD_NODES + void putHeardNode(const mesh::Identity& id, uint32_t advert_timestamp, uint8_t type, + int32_t lat, int32_t lon, const char* name, + int8_t snr, int8_t rssi, const mesh::Packet* pkt); + void formatNodesReply(char* reply, uint16_t offset); +#endif +#if RXLOG_SIZE + void captureRx(const mesh::Packet* pkt); + void formatRxLogReply(char* reply, 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); uint8_t handleAnonOwnerReq(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 f3d6c72a..b6924446 100644 --- a/variants/sensecap_solar/platformio.ini +++ b/variants/sensecap_solar/platformio.ini @@ -80,6 +80,12 @@ build_flags = ; GPS on/off itself persists via the `gps on|off` CLI command (NodePrefs). ; Note: the L76K draws ~25-35 mA — plan for the extra power on a solar node. -D ENV_SKIP_GPS_DETECT=1 + ; Passive mesh analyzer (TeTeHacko): expose heard repeaters/room servers and a + ; frame firehose over the text console for a BLE bridge/map. RAM-only, ~10 KB. + ; nodes [offset] -> heard repeater/room registry (identity, geo, SNR, path) + ; rxlog [cursor] -> circular log of every frame heard (type, path, SNR/RSSI) + -D MAX_HEARD_NODES=48 + -D RXLOG_SIZE=96 ; -D MESH_PACKET_LOGGING=1 build_src_filter = ${SenseCap_Solar.build_src_filter} +<../examples/simple_repeater/*.cpp>