From 77ab19153e9c36eddf7f4c3546e1c5c6f0ec7396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Br=C3=A1zio?= Date: Fri, 5 Sep 2025 02:07:26 +0100 Subject: [PATCH] Add serial logging for TX/RX packets --- examples/simple_repeater/main.cpp | 2 +- src/helpers/SerialBridge.cpp | 18 +++++++++++++++++- src/helpers/SerialBridge.h | 5 ++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 7150f0b2..93d3656f 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -579,7 +579,7 @@ public: _cli(board, rtc, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4) { #ifdef BRIDGE_OVER_SERIAL - bridge = new SerialBridge(BRIDGE_OVER_SERIAL, _mgr); + bridge = new SerialBridge(BRIDGE_OVER_SERIAL, _mgr, &rtc); #endif memset(known_clients, 0, sizeof(known_clients)); next_local_advert = next_flood_advert = 0; diff --git a/src/helpers/SerialBridge.cpp b/src/helpers/SerialBridge.cpp index c56645b6..f971e39c 100644 --- a/src/helpers/SerialBridge.cpp +++ b/src/helpers/SerialBridge.cpp @@ -1,5 +1,6 @@ #include "SerialBridge.h" #include +#include #ifdef BRIDGE_OVER_SERIAL @@ -16,7 +17,15 @@ inline static uint16_t fletcher16(const uint8_t *bytes, const size_t len) { return (sum2 << 8) | sum1; }; -SerialBridge::SerialBridge(Stream& serial, mesh::PacketManager* mgr) : _serial(&serial), _mgr(mgr) {} +const char* SerialBridge::getLogDateTime() { + static char tmp[32]; + uint32_t now = _rtc->getCurrentTime(); + DateTime dt = DateTime(now); + sprintf(tmp, "%02d:%02d:%02d - %d/%d/%d U", dt.hour(), dt.minute(), dt.second(), dt.day(), dt.month(), dt.year()); + return tmp; +} + +SerialBridge::SerialBridge(Stream& serial, mesh::PacketManager* mgr, mesh::RTCClock* rtc) : _serial(&serial), _mgr(mgr), _rtc(rtc) {} void SerialBridge::begin() { #if defined(ESP32) @@ -48,6 +57,10 @@ void SerialBridge::onPacketTransmitted(mesh::Packet* packet) { buffer[5 + len] = checksum & 0xFF; _serial->write(buffer, len + SERIAL_OVERHEAD); + +#if MESH_PACKET_LOGGING + Serial.printf("%s: BRIDGE: TX, len=%d crc=0x%04x\n", getLogDateTime(), len, checksum); +#endif } } @@ -77,6 +90,9 @@ void SerialBridge::loop() { if (_rx_buffer_pos == len + SERIAL_OVERHEAD) { // Full packet received uint16_t checksum = (_rx_buffer[4 + len] << 8) | _rx_buffer[5 + len]; if (checksum == fletcher16(_rx_buffer + 4, len)) { +#if MESH_PACKET_LOGGING + Serial.printf("%s: BRIDGE: RX, len=%d crc=0x%04x\n", getLogDateTime(), len, checksum); +#endif mesh::Packet *pkt = _mgr->allocNew(); if (pkt) { memcpy(pkt->payload, _rx_buffer + 4, len); diff --git a/src/helpers/SerialBridge.h b/src/helpers/SerialBridge.h index 7b729870..cc837d5e 100644 --- a/src/helpers/SerialBridge.h +++ b/src/helpers/SerialBridge.h @@ -16,14 +16,16 @@ public: * * @param serial The serial port to use for the bridge. * @param mgr A pointer to the packet manager. + * @param rtc A pointer to the RTC clock. */ - SerialBridge(Stream& serial, mesh::PacketManager* mgr); + SerialBridge(Stream& serial, mesh::PacketManager* mgr, mesh::RTCClock* rtc); void begin() override; void loop() override; void onPacketTransmitted(mesh::Packet* packet) override; void onPacketReceived(mesh::Packet* packet) override; private: + const char* getLogDateTime(); /** * @brief The 2-byte magic word used to signify the start of a packet. */ @@ -50,6 +52,7 @@ private: Stream* _serial; mesh::PacketManager* _mgr; + mesh::RTCClock* _rtc; SimpleMeshTables _seen_packets; uint8_t _rx_buffer[MAX_SERIAL_PACKET_SIZE]; // Buffer for serial data uint16_t _rx_buffer_pos = 0;