diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index e3ab3f2b..55f1e608 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -182,10 +182,10 @@ void MyMesh::putHeardNode(const mesh::Identity& id, uint32_t advert_timestamp, u // = join key: group `rxlog` records by it to get EVERY path this // node's advert arrived by (multi-path). is a single representative // (direct/0-hop preferred). Trailer line: E,, -void MyMesh::formatNodesReply(char* reply, uint16_t offset) { +void MyMesh::formatNodesReply(char* reply, int max_len, uint16_t offset) { char* dp = reply; int total = 0; - HeardNode* sorted[MAX_HEARD_NODES]; + static HeardNode* sorted[MAX_HEARD_NODES]; // BSS, ne stack (128 ptrů; volá se jen z loop tasku) for (int i = 0; i < MAX_HEARD_NODES; i++) { if (heard_nodes[i].heard_timestamp > 0) sorted[total++] = &heard_nodes[i]; } @@ -207,7 +207,7 @@ void MyMesh::formatNodesReply(char* reply, uint16_t offset) { (int)n->snr, (int)n->rssi, (unsigned long)(now - n->heard_timestamp), (unsigned long)n->pkt_hash, pathhex, n->name); int ll = strlen(line); - if (i > offset && (dp - reply) + ll + 24 > 150) break; // room for trailer; always emit >=1 + if (i > offset && (dp - reply) + ll + 24 > max_len) break; // room for trailer; always emit >=1 memcpy(dp, line, ll); dp += ll; } dp += sprintf(dp, "E,%d,%d", i, total); @@ -237,9 +237,9 @@ void MyMesh::captureRx(const mesh::Packet* pkt) { // `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) { +void MyMesh::formatRxLogReply(char* reply, int max_len, uint32_t cursor) { char* dp = reply; - RxLogRec* recs[RXLOG_SIZE]; + static RxLogRec* recs[RXLOG_SIZE]; // BSS, ne stack (512 ptrů = 2 KB; volá se jen z loop tasku) int n = 0; uint32_t oldest = 0, newest = 0; for (int i = 0; i < RXLOG_SIZE; i++) { @@ -264,7 +264,7 @@ void MyMesh::formatRxLogReply(char* reply, uint32_t cursor) { (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 + if (i > 0 && (dp - reply) + ll + 40 > max_len) break; // room for trailer; always emit >=1 memcpy(dp, line, ll); dp += ll; last = r->seq; } @@ -1390,7 +1390,7 @@ void MyMesh::clearStats() { ((SimpleMeshTables *)getTables())->resetStats(); } -void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply) { +void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply, int reply_max) { if (region_load_active) { if (StrHelper::isBlank(command)) { // empty/blank line, signal to terminate 'load' operation region_map = temp_map; // copy over the temp instance as new current map @@ -1478,15 +1478,17 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply } #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) + // analyzer: paged dump of heard repeaters/room servers (for a map/bridge). + // Page size = caller's reply capacity: local console passes ~1000 (fewer BLE + // round trips), the mesh admin CLI keeps the default 150 (one LoRa packet). uint16_t offset = (command[5] == ' ') ? (uint16_t) atoi(&command[6]) : 0; - formatNodesReply(reply, offset); + formatNodesReply(reply, reply_max, 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); + formatRxLogReply(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 ddbcbd83..5f835ab5 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -199,11 +199,11 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks { 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); + void formatNodesReply(char* reply, int max_len, uint16_t offset); #endif #if RXLOG_SIZE void captureRx(const mesh::Packet* pkt); - void formatRxLogReply(char* reply, uint32_t cursor); + void formatRxLogReply(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); @@ -308,7 +308,11 @@ public: void saveIdentity(const mesh::LocalIdentity& new_id) override; void clearStats() override; - void handleCommand(uint32_t sender_timestamp, char* command, char* reply); + // reply_max = usable reply capacity. Default 150 keeps the mesh admin CLI reply + // inside one LoRa packet; the local console (USB/BLE, big buffer) passes more so + // the paged analyzer replies (nodes/rxlog) can use big pages. Explicit parameter + // on purpose — a forged sender_timestamp must never be able to grow the reply. + void handleCommand(uint32_t sender_timestamp, char* command, char* reply, int reply_max = 150); void loop(); #if defined(WITH_BRIDGE) diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 2b0d01c7..f2ffb193 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -28,8 +28,13 @@ // no flush needed. static void bleWriteAll(const char* s) { size_t n = strlen(s), i = 0; uint32_t stall = 0; + // chunk = ATT payload (MTU-3); bridge negotiates MTU 247 -> 244 B chunks. + // Fallback 20 B when MTU stayed at the 23 B default (unpaired phone etc.). + size_t maxchunk = 20; + BLEConnection* conn = Bluefruit.Connection(Bluefruit.connHandle()); + if (conn && conn->getMtu() > 23) maxchunk = conn->getMtu() - 3; while (i < n && Bluefruit.connected() && stall < 300) { // max ~0.9 s stall → bail - size_t chunk = n - i; if (chunk > 20) chunk = 20; + size_t chunk = n - i; if (chunk > maxchunk) chunk = maxchunk; size_t w = bleuart.write((const uint8_t*)(s + i), chunk); if (w > 0) { i += w; stall = 0; } else { stall++; delay(3); } // queue full -> wait a moment and retry @@ -342,8 +347,8 @@ void loop() { if (len > 0 && command[len - 1] == '\r') { // received complete line Serial.print('\n'); command[len - 1] = 0; // replace newline with C string null terminator - char reply[160]; - the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial! + static char reply[1032]; // big pages for paged analyzer replies (nodes/rxlog); BSS, ne stack + the_mesh.handleCommand(0, command, reply, (int)sizeof(reply) - 32); // NOTE: there is no sender_timestamp via serial! if (reply[0]) { Serial.print(" -> "); Serial.println(reply); #if defined(BLE_PIN_CODE) && defined(NRF52_PLATFORM) diff --git a/variants/sensecap_solar/platformio.ini b/variants/sensecap_solar/platformio.ini index b7e821bc..78c1a41b 100644 --- a/variants/sensecap_solar/platformio.ini +++ b/variants/sensecap_solar/platformio.ini @@ -84,10 +84,16 @@ build_flags = ; frame firehose over the text console for a BLE bridge/map. RAM-only, ~15 KB. ; nodes [offset] -> heard repeater/room registry (identity, geo, SNR, path) ; rxlog [cursor] -> circular log of every frame heard (type, path, SNR/RSSI) - ; RXLOG_SIZE 96 -> 256 (~32 B/rec): daytime advert bursts overflowed the ring - ; between BLE drains (~250 drops/h, meshcore_rxlog_dropped_total) - -D MAX_HEARD_NODES=48 - -D RXLOG_SIZE=256 + ; Sized from channel physics, not last week's traffic (so we stop re-raising): + ; RXLOG_SIZE 512 (~32 B/rec, 16 KB): ~2 min of a fully saturated SF8/62.5k + ; channel (~2-4 frames/s) or ~35 min of typical traffic -- covers BLE + ; outages; the fast big-page drain (~40+ rec/s) outruns the radio itself. + ; MAX_HEARD_NODES 128 (~68 B/entry, 8.7 KB): the 48-slot registry filled in + ; <2 h and churn evicted real neighbours (Kladno, ABC_10W_MKT) into the + ; poorer `neighbors`-table fallback; 128 > distinct nodes heard per + ; neighbour advert period even on inversion evenings. + -D MAX_HEARD_NODES=128 + -D RXLOG_SIZE=512 ; -D MESH_PACKET_LOGGING=1 build_src_filter = ${SenseCap_Solar.build_src_filter} +<../examples/simple_repeater/*.cpp>