MeshCore/src/helpers/bridges/RS232Bridge.cpp

171 lines
5.4 KiB
C++
Raw Normal View History

2025-09-05 11:28:40 +01:00
#include "RS232Bridge.h"
#include <HardwareSerial.h>
2025-09-05 11:28:40 +01:00
#ifdef WITH_RS232_BRIDGE
RS232Bridge::RS232Bridge(Stream &serial, mesh::PacketManager *mgr, mesh::RTCClock *rtc)
: BridgeBase(mgr, rtc), _serial(&serial) {}
2025-09-05 11:28:40 +01:00
void RS232Bridge::begin() {
2025-09-24 16:30:00 +01:00
Serial.printf("%s: RS232 BRIDGE: Initializing...\n", getLogDateTime());
2025-09-05 11:28:40 +01:00
#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)
2025-09-05 11:28:40 +01:00
((HardwareSerial *)_serial)->setPins(WITH_RS232_BRIDGE_RX, WITH_RS232_BRIDGE_TX);
#elif defined(NRF52_PLATFORM)
2025-09-05 11:28:40 +01:00
((HardwareSerial *)_serial)->setPins(WITH_RS232_BRIDGE_RX, WITH_RS232_BRIDGE_TX);
#elif defined(RP2040_PLATFORM)
2025-09-05 11:28:40 +01:00
((SerialUART *)_serial)->setRX(WITH_RS232_BRIDGE_RX);
((SerialUART *)_serial)->setTX(WITH_RS232_BRIDGE_TX);
#elif defined(STM32_PLATFORM)
2025-09-05 11:28:40 +01:00
((HardwareSerial *)_serial)->setRx(WITH_RS232_BRIDGE_RX);
((HardwareSerial *)_serial)->setTx(WITH_RS232_BRIDGE_TX);
#else
2025-09-05 11:28:40 +01:00
#error RS232Bridge was not tested on the current platform
#endif
((HardwareSerial *)_serial)->begin(115200);
2025-09-24 16:30:00 +01:00
// Update bridge state
_initialized = true;
}
2025-09-24 16:30:00 +01:00
void RS232Bridge::end() {
Serial.printf("%s: RS232 BRIDGE: Stopping...\n", getLogDateTime());
((HardwareSerial *)_serial)->end();
2025-09-05 02:07:26 +01:00
2025-09-24 16:30:00 +01:00
// Update bridge state
_initialized = false;
}
2025-09-05 11:28:40 +01:00
void RS232Bridge::loop() {
2025-09-24 16:30:00 +01:00
// Guard against uninitialized state
if (_initialized == false) {
return;
}
while (_serial->available()) {
uint8_t b = _serial->read();
if (_rx_buffer_pos < 2) {
// Waiting for magic word
if ((_rx_buffer_pos == 0 && b == ((BRIDGE_PACKET_MAGIC >> 8) & 0xFF)) ||
(_rx_buffer_pos == 1 && b == (BRIDGE_PACKET_MAGIC & 0xFF))) {
_rx_buffer[_rx_buffer_pos++] = b;
} else {
// Invalid magic byte, reset and start over
_rx_buffer_pos = 0;
// Check if this byte could be the start of a new magic word
if (b == ((BRIDGE_PACKET_MAGIC >> 8) & 0xFF)) {
_rx_buffer[_rx_buffer_pos++] = b;
}
}
} 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];
// Validate length field
if (len > (MAX_TRANS_UNIT + 1)) {
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: RX invalid length %d, resetting\n", getLogDateTime(), len);
#endif
_rx_buffer_pos = 0; // Invalid length, reset
continue;
}
if (_rx_buffer_pos == len + SERIAL_OVERHEAD) { // Full packet received
uint16_t received_checksum = (_rx_buffer[4 + len] << 8) | _rx_buffer[5 + len];
if (validateChecksum(_rx_buffer + 4, len, received_checksum)) {
2025-09-05 02:07:26 +01:00
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: RX, len=%d crc=0x%04x\n", getLogDateTime(), len,
received_checksum);
2025-09-05 02:07:26 +01:00
#endif
mesh::Packet *pkt = _mgr->allocNew();
if (pkt) {
if (pkt->readFrom(_rx_buffer + 4, len)) {
onPacketReceived(pkt);
} else {
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: RX failed to parse packet\n", getLogDateTime());
#endif
_mgr->free(pkt);
}
} else {
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: RX failed to allocate packet\n", getLogDateTime());
#endif
}
} else {
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: RX checksum mismatch, rcv=0x%04x\n", getLogDateTime(),
received_checksum);
#endif
}
_rx_buffer_pos = 0; // Reset for next packet
}
}
}
}
}
2025-09-24 16:30:00 +01:00
void RS232Bridge::onPacketTransmitted(mesh::Packet *packet) {
// Guard against uninitialized state
if (_initialized == false) {
Serial.printf("%s: ESPNOW BRIDGE: TX packet attempted before initialization\n", getLogDateTime());
return;
}
// First validate the packet pointer
if (!packet) {
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: TX invalid packet pointer\n", getLogDateTime());
#endif
return;
}
if (!_seen_packets.hasSeen(packet)) {
uint8_t buffer[MAX_SERIAL_PACKET_SIZE];
uint16_t len = packet->writeTo(buffer + 4);
// Check if packet fits within our maximum payload size
if (len > (MAX_TRANS_UNIT + 1)) {
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: TX packet too large (payload=%d, max=%d)\n", getLogDateTime(), len,
MAX_TRANS_UNIT + 1);
#endif
return;
}
// Build packet header
buffer[0] = (BRIDGE_PACKET_MAGIC >> 8) & 0xFF; // Magic high byte
buffer[1] = BRIDGE_PACKET_MAGIC & 0xFF; // Magic low byte
buffer[2] = (len >> 8) & 0xFF; // Length high byte
buffer[3] = len & 0xFF; // Length low byte
// Calculate checksum over the payload
uint16_t checksum = fletcher16(buffer + 4, len);
buffer[4 + len] = (checksum >> 8) & 0xFF; // Checksum high byte
buffer[5 + len] = checksum & 0xFF; // Checksum low byte
// Send complete packet
_serial->write(buffer, len + SERIAL_OVERHEAD);
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: TX, len=%d crc=0x%04x\n", getLogDateTime(), len, checksum);
#endif
}
}
void RS232Bridge::onPacketReceived(mesh::Packet *packet) {
handleReceivedPacket(packet);
}
#endif