Files
MeshCore-Evo/src/helpers/bridges/RS232Bridge.h

62 lines
1.9 KiB
C
Raw Normal View History

#pragma once
#include "helpers/AbstractBridge.h"
#include "helpers/SimpleMeshTables.h"
#include <Stream.h>
2025-09-05 11:28:40 +01:00
#ifdef WITH_RS232_BRIDGE
/**
* @brief A bridge implementation that uses a serial port to connect two mesh networks.
*/
2025-09-05 11:28:40 +01:00
class RS232Bridge : public AbstractBridge {
public:
/**
* @brief Construct a new Serial Bridge object
*
* @param serial The serial port to use for the bridge.
* @param mgr A pointer to the packet manager.
2025-09-05 02:07:26 +01:00
* @param rtc A pointer to the RTC clock.
*/
2025-09-05 11:28:40 +01:00
RS232Bridge(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:
2025-09-05 02:07:26 +01:00
const char* getLogDateTime();
/**
* @brief The 2-byte magic word used to signify the start of a packet.
*/
static constexpr uint16_t SERIAL_PKT_MAGIC = 0xCAFE;
/**
* @brief The total overhead of the serial protocol in bytes.
* [MAGIC_WORD (2 bytes)] [LENGTH (2 bytes)] [PAYLOAD (variable)] [CHECKSUM (2 bytes)]
*/
static constexpr uint16_t SERIAL_OVERHEAD = 6;
/**
* @brief The maximum size of a 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
*/
static constexpr uint16_t MAX_SERIAL_PACKET_SIZE = (MAX_TRANS_UNIT + 1) + SERIAL_OVERHEAD;
Stream* _serial;
mesh::PacketManager* _mgr;
2025-09-05 02:07:26 +01:00
mesh::RTCClock* _rtc;
SimpleMeshTables _seen_packets;
uint8_t _rx_buffer[MAX_SERIAL_PACKET_SIZE]; // Buffer for serial data
uint16_t _rx_buffer_pos = 0;
};
#endif