Add serial logging for TX/RX packets

This commit is contained in:
João Brázio 2025-09-05 02:07:26 +01:00
parent 2b920dfed3
commit 77ab19153e
No known key found for this signature in database
GPG key ID: 56A1490716A324DD
3 changed files with 22 additions and 3 deletions

View file

@ -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;

View file

@ -1,5 +1,6 @@
#include "SerialBridge.h"
#include <HardwareSerial.h>
#include <RTClib.h>
#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);

View file

@ -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;