Refactor bridge implementations to inherit from BridgeBase

This commit is contained in:
João Brázio
2025-09-08 02:03:08 +01:00
parent 537449e6af
commit 0051ccef26
7 changed files with 282 additions and 155 deletions

View File

@@ -1,7 +1,7 @@
#pragma once
#include "helpers/AbstractBridge.h"
#include "helpers/SimpleMeshTables.h"
#include "helpers/bridges/BridgeBase.h"
#include <Stream.h>
#ifdef WITH_RS232_BRIDGE
@@ -15,21 +15,22 @@
*
* Features:
* - Point-to-point communication over hardware UART
* - Fletcher-16 checksum for data integrity verification
* - Magic header for packet synchronization
* - Fletcher-16 checksum for data integrity verification
* - Magic header for packet synchronization and frame alignment
* - Duplicate packet detection using SimpleMeshTables tracking
* - Configurable RX/TX pins via build defines
* - Baud rate fixed at 115200
* - Fixed baud rate at 115200 for consistent timing
*
* Packet Structure:
* [2 bytes] Magic Header - Used to identify start of packet
* [2 bytes] Fletcher-16 checksum - Data integrity check
* [1 byte] Payload length
* [n bytes] Packet payload
* [2 bytes] Magic Header (0xC03E) - Used to identify start of RS232Bridge packets
* [2 bytes] Payload Length - Length of the mesh packet payload
* [n bytes] Mesh Packet Payload - The actual mesh packet data
* [2 bytes] Fletcher-16 Checksum - Calculated over the payload for integrity verification
*
* The Fletcher-16 checksum is used to validate packet integrity and detect
* corrupted or malformed packets. It provides error detection capabilities
* suitable for serial communication where noise or timing issues could
* corrupt data.
* The Fletcher-16 checksum is calculated over the mesh packet payload and provides
* error detection capabilities suitable for serial communication where electrical
* noise, timing issues, or hardware problems could corrupt data. The checksum
* validation ensures only valid packets are forwarded to the mesh.
*
* Configuration:
* - Define WITH_RS232_BRIDGE to enable this bridge
@@ -37,12 +38,13 @@
* - Define WITH_RS232_BRIDGE_TX with the TX pin number
*
* Platform Support:
* - ESP32 targets
* - NRF52 targets
* - RP2040 targets
* - STM32 targets
* Different platforms require different pin configuration methods:
* - ESP32: Uses HardwareSerial::setPins(rx, tx)
* - NRF52: Uses HardwareSerial::setPins(rx, tx)
* - RP2040: Uses SerialUART::setRX(rx) and SerialUART::setTX(tx)
* - STM32: Uses HardwareSerial::setRx(rx) and HardwareSerial::setTx(tx)
*/
class RS232Bridge : public AbstractBridge {
class RS232Bridge : public BridgeBase {
public:
/**
* @brief Constructs an RS232Bridge instance
@@ -51,69 +53,98 @@ public:
* @param mgr PacketManager for allocating and queuing packets
* @param rtc RTCClock for timestamping debug messages
*/
RS232Bridge(Stream& serial, mesh::PacketManager* mgr, mesh::RTCClock* rtc);
RS232Bridge(Stream &serial, mesh::PacketManager *mgr, mesh::RTCClock *rtc);
/**
* Initializes the RS232 bridge
*
* - Configures UART pins based on platform
* - Sets baud rate to 115200
*
* - Validates that RX/TX pins are defined
* - Configures UART pins based on target platform
* - Sets baud rate to 115200 for consistent communication
* - Platform-specific pin configuration methods are used
*/
void begin() override;
/**
* @brief Main loop handler
* Processes incoming serial data and builds packets
* @brief Main loop handler for processing incoming serial data
*
* Implements a state machine for packet reception:
* 1. Searches for magic header bytes for packet synchronization
* 2. Reads length field to determine expected packet size
* 3. Validates packet length against maximum allowed size
* 4. Receives complete packet payload and checksum
* 5. Validates Fletcher-16 checksum for data integrity
* 6. Creates mesh packet and forwards if valid
*/
void loop() override;
/**
* @brief Called when a packet needs to be transmitted over serial
* Formats and sends the packet with proper framing
*
* Formats the mesh packet with RS232 framing protocol:
* - Adds magic header for synchronization
* - Includes payload length field
* - Calculates Fletcher-16 checksum over payload
* - Transmits complete framed packet
* - Uses duplicate detection to prevent retransmission
*
* @param packet The mesh packet to transmit
*/
void onPacketTransmitted(mesh::Packet* packet) override;
void onPacketTransmitted(mesh::Packet *packet) override;
/**
* @brief Called when a complete packet has been received from serial
* Queues the packet for mesh processing if checksum is valid
*
* @param packet The received mesh packet
* @brief Called when a complete valid packet has been received from serial
*
* Forwards the received packet to the mesh for processing.
* The packet has already been validated for checksum integrity
* and parsed successfully at this point.
*
* @param packet The received mesh packet ready for processing
*/
void onPacketReceived(mesh::Packet* packet) override;
void onPacketReceived(mesh::Packet *packet) override;
private:
/** Helper function to get formatted timestamp for logging */
const char* getLogDateTime();
/**
* RS232 Protocol Structure:
* - Magic header: 2 bytes (packet identification)
* - Length field: 2 bytes (payload length)
* - Payload: variable bytes (mesh packet data)
* - Checksum: 2 bytes (Fletcher-16 over payload)
* Total overhead: 6 bytes
*/
/** Magic number to identify start of RS232 packets */
/** Magic number to identify start of RS232Bridge packets */
static constexpr uint16_t SERIAL_PKT_MAGIC = 0xC03E;
/**
* @brief The total overhead of the serial protocol in bytes.
* [MAGIC_WORD (2 bytes)] [LENGTH (2 bytes)] [PAYLOAD (variable)] [CHECKSUM (2 bytes)]
* Size constants for packet parsing and construction
*/
static constexpr uint16_t SERIAL_OVERHEAD = 6;
static constexpr uint16_t MAGIC_HEADER_SIZE = 2;
static constexpr uint16_t LENGTH_FIELD_SIZE = 2;
static constexpr uint16_t CHECKSUM_SIZE = 2;
/**
* @brief The maximum size of a packet on the serial line.
* @brief The total overhead of the serial protocol in bytes.
* Includes: MAGIC_WORD (2) + LENGTH (2) + CHECKSUM (2) = 6 bytes
*/
static constexpr uint16_t SERIAL_OVERHEAD = MAGIC_HEADER_SIZE + LENGTH_FIELD_SIZE + CHECKSUM_SIZE;
/**
* @brief The maximum size of a complete packet on the serial line.
*
* This is calculated as the sum of:
* - 1 byte for the packet header (from mesh::Packet)
* - 4 bytes for transport codes (from mesh::Packet)
* - 1 byte for the path length (from mesh::Packet)
* - MAX_PATH_SIZE for the path itself (from MeshCore.h)
* - MAX_PACKET_PAYLOAD for the payload (from MeshCore.h)
* - SERIAL_OVERHEAD for the serial framing
* - MAX_TRANS_UNIT + 1 for the maximum mesh packet size
* - SERIAL_OVERHEAD for the framing (magic + length + checksum)
*/
static constexpr uint16_t MAX_SERIAL_PACKET_SIZE = (MAX_TRANS_UNIT + 1) + SERIAL_OVERHEAD;
Stream* _serial;
mesh::PacketManager* _mgr;
mesh::RTCClock* _rtc;
SimpleMeshTables _seen_packets;
uint8_t _rx_buffer[MAX_SERIAL_PACKET_SIZE]; // Buffer for serial data
/** Hardware serial port interface */
Stream *_serial;
/** Buffer for building received packets */
uint8_t _rx_buffer[MAX_SERIAL_PACKET_SIZE];
/** Current position in the receive buffer */
uint16_t _rx_buffer_pos = 0;
};