Rename SerialBridge to RS232Bridge
This commit is contained in:
123
src/helpers/bridges/RS232Bridge.cpp
Normal file
123
src/helpers/bridges/RS232Bridge.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#include "RS232Bridge.h"
|
||||
#include <HardwareSerial.h>
|
||||
#include <RTClib.h>
|
||||
|
||||
#ifdef WITH_RS232_BRIDGE
|
||||
|
||||
// Fletcher-16
|
||||
// https://en.wikipedia.org/wiki/Fletcher%27s_checksum
|
||||
inline static uint16_t fletcher16(const uint8_t *bytes, const size_t len) {
|
||||
uint8_t sum1 = 0, sum2 = 0;
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
sum1 = (sum1 + bytes[i]) % 255;
|
||||
sum2 = (sum2 + sum1) % 255;
|
||||
}
|
||||
|
||||
return (sum2 << 8) | sum1;
|
||||
};
|
||||
|
||||
const char* RS232Bridge::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;
|
||||
}
|
||||
|
||||
RS232Bridge::RS232Bridge(Stream& serial, mesh::PacketManager* mgr, mesh::RTCClock* rtc) : _serial(&serial), _mgr(mgr), _rtc(rtc) {}
|
||||
|
||||
void RS232Bridge::begin() {
|
||||
#if !defined(WITH_RS232_BRIDGE_RX) || !defined(WITH_RS232_BRIDGE_TX)
|
||||
#error "WITH_RS232_BRIDGE_RX and WITH_RS232_BRIDGE_TX must be defined"
|
||||
#endif
|
||||
|
||||
#if defined(ESP32)
|
||||
((HardwareSerial *)_serial)->setPins(WITH_RS232_BRIDGE_RX, WITH_RS232_BRIDGE_TX);
|
||||
#elif defined(NRF52_PLATFORM)
|
||||
((HardwareSerial *)_serial)->setPins(WITH_RS232_BRIDGE_RX, WITH_RS232_BRIDGE_TX);
|
||||
#elif defined(RP2040_PLATFORM)
|
||||
((SerialUART *)_serial)->setRX(WITH_RS232_BRIDGE_RX);
|
||||
((SerialUART *)_serial)->setTX(WITH_RS232_BRIDGE_TX);
|
||||
#elif defined(STM32_PLATFORM)
|
||||
((HardwareSerial *)_serial)->setRx(WITH_RS232_BRIDGE_RX);
|
||||
((HardwareSerial *)_serial)->setTx(WITH_RS232_BRIDGE_TX);
|
||||
#else
|
||||
#error RS232Bridge was not tested on the current platform
|
||||
#endif
|
||||
((HardwareSerial*)_serial)->begin(115200);
|
||||
}
|
||||
|
||||
void RS232Bridge::onPacketTransmitted(mesh::Packet* packet) {
|
||||
if (!_seen_packets.hasSeen(packet)) {
|
||||
uint8_t buffer[MAX_SERIAL_PACKET_SIZE];
|
||||
uint16_t len = packet->writeTo(buffer + 4);
|
||||
|
||||
buffer[0] = (SERIAL_PKT_MAGIC >> 8) & 0xFF;
|
||||
buffer[1] = SERIAL_PKT_MAGIC & 0xFF;
|
||||
buffer[2] = (len >> 8) & 0xFF;
|
||||
buffer[3] = len & 0xFF;
|
||||
|
||||
uint16_t checksum = fletcher16(buffer + 4, len);
|
||||
buffer[4 + len] = (checksum >> 8) & 0xFF;
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
void RS232Bridge::loop() {
|
||||
while (_serial->available()) {
|
||||
uint8_t b = _serial->read();
|
||||
|
||||
if (_rx_buffer_pos < 2) {
|
||||
// Waiting for magic word
|
||||
if ((_rx_buffer_pos == 0 && b == ((SERIAL_PKT_MAGIC >> 8) & 0xFF)) ||
|
||||
(_rx_buffer_pos == 1 && b == (SERIAL_PKT_MAGIC & 0xFF))) {
|
||||
_rx_buffer[_rx_buffer_pos++] = b;
|
||||
} else {
|
||||
_rx_buffer_pos = 0;
|
||||
}
|
||||
} else {
|
||||
// Reading length, payload, and checksum
|
||||
_rx_buffer[_rx_buffer_pos++] = b;
|
||||
|
||||
if (_rx_buffer_pos >= 4) {
|
||||
uint16_t len = (_rx_buffer[2] << 8) | _rx_buffer[3];
|
||||
if (len > (MAX_TRANS_UNIT + 1)) {
|
||||
_rx_buffer_pos = 0; // Invalid length, reset
|
||||
return;
|
||||
}
|
||||
|
||||
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) {
|
||||
pkt->readFrom(_rx_buffer + 4, len);
|
||||
onPacketReceived(pkt);
|
||||
}
|
||||
}
|
||||
_rx_buffer_pos = 0; // Reset for next packet
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RS232Bridge::onPacketReceived(mesh::Packet* packet) {
|
||||
if (!_seen_packets.hasSeen(packet)) {
|
||||
_mgr->queueInbound(packet, 0);
|
||||
} else {
|
||||
_mgr->free(packet);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
61
src/helpers/bridges/RS232Bridge.h
Normal file
61
src/helpers/bridges/RS232Bridge.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include "helpers/AbstractBridge.h"
|
||||
#include "helpers/SimpleMeshTables.h"
|
||||
#include <Stream.h>
|
||||
|
||||
#ifdef WITH_RS232_BRIDGE
|
||||
|
||||
/**
|
||||
* @brief A bridge implementation that uses a serial port to connect two mesh networks.
|
||||
*/
|
||||
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.
|
||||
* @param rtc A pointer to the RTC clock.
|
||||
*/
|
||||
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:
|
||||
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;
|
||||
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
|
||||
Reference in New Issue
Block a user