2025-09-04 23:43:05 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "helpers/AbstractBridge.h"
|
2025-09-04 23:50:13 +01:00
|
|
|
#include "helpers/SimpleMeshTables.h"
|
2025-09-04 23:43:05 +01:00
|
|
|
#include <Stream.h>
|
|
|
|
|
|
2025-09-05 11:28:40 +01:00
|
|
|
#ifdef WITH_RS232_BRIDGE
|
2025-09-04 23:43:05 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 {
|
2025-09-04 23:43:05 +01:00
|
|
|
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-04 23:43:05 +01:00
|
|
|
*/
|
2025-09-05 11:28:40 +01:00
|
|
|
RS232Bridge(Stream& serial, mesh::PacketManager* mgr, mesh::RTCClock* rtc);
|
2025-09-04 23:43:05 +01:00
|
|
|
void begin() override;
|
|
|
|
|
void loop() override;
|
|
|
|
|
void onPacketTransmitted(mesh::Packet* packet) override;
|
2025-09-05 01:50:50 +01:00
|
|
|
void onPacketReceived(mesh::Packet* packet) override;
|
2025-09-04 23:43:05 +01:00
|
|
|
|
|
|
|
|
private:
|
2025-09-05 02:07:26 +01:00
|
|
|
const char* getLogDateTime();
|
2025-09-05 01:50:50 +01:00
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
|
2025-09-04 23:43:05 +01:00
|
|
|
Stream* _serial;
|
|
|
|
|
mesh::PacketManager* _mgr;
|
2025-09-05 02:07:26 +01:00
|
|
|
mesh::RTCClock* _rtc;
|
2025-09-04 23:50:13 +01:00
|
|
|
SimpleMeshTables _seen_packets;
|
2025-09-05 01:50:50 +01:00
|
|
|
uint8_t _rx_buffer[MAX_SERIAL_PACKET_SIZE]; // Buffer for serial data
|
|
|
|
|
uint16_t _rx_buffer_pos = 0;
|
2025-09-04 23:43:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|