2025-09-05 11:28:40 +01:00
|
|
|
#include "RS232Bridge.h"
|
2025-09-08 02:03:08 +01:00
|
|
|
|
2025-09-04 23:43:05 +01:00
|
|
|
#include <HardwareSerial.h>
|
|
|
|
|
|
2025-09-05 11:28:40 +01:00
|
|
|
#ifdef WITH_RS232_BRIDGE
|
2025-09-04 23:43:05 +01:00
|
|
|
|
2025-09-08 02:03:08 +01:00
|
|
|
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() {
|
|
|
|
|
#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
|
2025-09-04 23:43:05 +01:00
|
|
|
|
|
|
|
|
#if defined(ESP32)
|
2025-09-05 11:28:40 +01:00
|
|
|
((HardwareSerial *)_serial)->setPins(WITH_RS232_BRIDGE_RX, WITH_RS232_BRIDGE_TX);
|
2025-09-05 09:22:06 +01:00
|
|
|
#elif defined(NRF52_PLATFORM)
|
2025-09-05 11:28:40 +01:00
|
|
|
((HardwareSerial *)_serial)->setPins(WITH_RS232_BRIDGE_RX, WITH_RS232_BRIDGE_TX);
|
2025-09-04 23:43:05 +01:00
|
|
|
#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);
|
2025-09-04 23:43:05 +01:00
|
|
|
#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);
|
2025-09-04 23:43:05 +01:00
|
|
|
#else
|
2025-09-05 11:28:40 +01:00
|
|
|
#error RS232Bridge was not tested on the current platform
|
2025-09-04 23:43:05 +01:00
|
|
|
#endif
|
2025-09-08 02:03:08 +01:00
|
|
|
((HardwareSerial *)_serial)->begin(115200);
|
2025-09-04 23:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-08 02:03:08 +01:00
|
|
|
void RS232Bridge::onPacketTransmitted(mesh::Packet *packet) {
|
|
|
|
|
// First validate the packet pointer
|
|
|
|
|
if (!packet) {
|
|
|
|
|
#if MESH_PACKET_LOGGING
|
|
|
|
|
Serial.printf("%s: RS232 BRIDGE: TX invalid packet pointer\n", getLogDateTime());
|
|
|
|
|
#endif
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 23:50:13 +01:00
|
|
|
if (!_seen_packets.hasSeen(packet)) {
|
2025-09-08 02:03:08 +01:00
|
|
|
|
2025-09-05 01:50:50 +01:00
|
|
|
uint8_t buffer[MAX_SERIAL_PACKET_SIZE];
|
|
|
|
|
uint16_t len = packet->writeTo(buffer + 4);
|
|
|
|
|
|
2025-09-08 02:03:08 +01:00
|
|
|
// 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;
|
|
|
|
|
}
|
2025-09-05 01:50:50 +01:00
|
|
|
|
2025-09-08 02:03:08 +01:00
|
|
|
// Build packet header
|
2025-09-08 11:15:28 +01:00
|
|
|
buffer[0] = (BRIDGE_PACKET_MAGIC >> 8) & 0xFF; // Magic high byte
|
|
|
|
|
buffer[1] = BRIDGE_PACKET_MAGIC & 0xFF; // Magic low byte
|
2025-09-08 20:21:33 +01:00
|
|
|
buffer[2] = (len >> 8) & 0xFF; // Length high byte
|
|
|
|
|
buffer[3] = len & 0xFF; // Length low byte
|
2025-09-08 02:03:08 +01:00
|
|
|
|
|
|
|
|
// Calculate checksum over the payload
|
2025-09-05 01:50:50 +01:00
|
|
|
uint16_t checksum = fletcher16(buffer + 4, len);
|
2025-09-08 02:03:08 +01:00
|
|
|
buffer[4 + len] = (checksum >> 8) & 0xFF; // Checksum high byte
|
|
|
|
|
buffer[5 + len] = checksum & 0xFF; // Checksum low byte
|
2025-09-05 01:50:50 +01:00
|
|
|
|
2025-09-08 02:03:08 +01:00
|
|
|
// Send complete packet
|
2025-09-05 01:50:50 +01:00
|
|
|
_serial->write(buffer, len + SERIAL_OVERHEAD);
|
2025-09-05 02:07:26 +01:00
|
|
|
|
|
|
|
|
#if MESH_PACKET_LOGGING
|
2025-09-08 02:03:08 +01:00
|
|
|
Serial.printf("%s: RS232 BRIDGE: TX, len=%d crc=0x%04x\n", getLogDateTime(), len, checksum);
|
2025-09-05 02:07:26 +01:00
|
|
|
#endif
|
2025-09-04 23:50:13 +01:00
|
|
|
}
|
2025-09-04 23:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-05 11:28:40 +01:00
|
|
|
void RS232Bridge::loop() {
|
2025-09-04 23:43:05 +01:00
|
|
|
while (_serial->available()) {
|
2025-09-05 01:50:50 +01:00
|
|
|
uint8_t b = _serial->read();
|
|
|
|
|
|
|
|
|
|
if (_rx_buffer_pos < 2) {
|
|
|
|
|
// Waiting for magic word
|
2025-09-08 11:15:28 +01:00
|
|
|
if ((_rx_buffer_pos == 0 && b == ((BRIDGE_PACKET_MAGIC >> 8) & 0xFF)) ||
|
|
|
|
|
(_rx_buffer_pos == 1 && b == (BRIDGE_PACKET_MAGIC & 0xFF))) {
|
2025-09-05 01:50:50 +01:00
|
|
|
_rx_buffer[_rx_buffer_pos++] = b;
|
|
|
|
|
} else {
|
2025-09-08 02:03:08 +01:00
|
|
|
// Invalid magic byte, reset and start over
|
2025-09-05 01:50:50 +01:00
|
|
|
_rx_buffer_pos = 0;
|
2025-09-08 02:03:08 +01:00
|
|
|
// Check if this byte could be the start of a new magic word
|
2025-09-08 11:15:28 +01:00
|
|
|
if (b == ((BRIDGE_PACKET_MAGIC >> 8) & 0xFF)) {
|
2025-09-08 02:03:08 +01:00
|
|
|
_rx_buffer[_rx_buffer_pos++] = b;
|
|
|
|
|
}
|
2025-09-05 01:50:50 +01:00
|
|
|
}
|
|
|
|
|
} 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];
|
2025-09-08 02:03:08 +01:00
|
|
|
|
|
|
|
|
// Validate length field
|
2025-09-05 09:22:06 +01:00
|
|
|
if (len > (MAX_TRANS_UNIT + 1)) {
|
2025-09-08 02:03:08 +01:00
|
|
|
#if MESH_PACKET_LOGGING
|
|
|
|
|
Serial.printf("%s: RS232 BRIDGE: RX invalid length %d, resetting\n", getLogDateTime(), len);
|
|
|
|
|
#endif
|
2025-09-05 01:50:50 +01:00
|
|
|
_rx_buffer_pos = 0; // Invalid length, reset
|
2025-09-08 02:03:08 +01:00
|
|
|
continue;
|
2025-09-05 01:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-05 09:22:06 +01:00
|
|
|
if (_rx_buffer_pos == len + SERIAL_OVERHEAD) { // Full packet received
|
2025-09-08 02:03:08 +01:00
|
|
|
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
|
2025-09-08 02:03:08 +01:00
|
|
|
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
|
2025-09-08 02:03:08 +01:00
|
|
|
mesh::Packet *pkt = _mgr->allocNew();
|
2025-09-05 01:50:50 +01:00
|
|
|
if (pkt) {
|
2025-09-08 02:03:08 +01:00
|
|
|
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
|
2025-09-05 01:50:50 +01:00
|
|
|
}
|
2025-09-08 02:03:08 +01:00
|
|
|
} else {
|
|
|
|
|
#if MESH_PACKET_LOGGING
|
|
|
|
|
Serial.printf("%s: RS232 BRIDGE: RX checksum mismatch, rcv=0x%04x\n", getLogDateTime(),
|
|
|
|
|
received_checksum);
|
|
|
|
|
#endif
|
2025-09-05 01:50:50 +01:00
|
|
|
}
|
|
|
|
|
_rx_buffer_pos = 0; // Reset for next packet
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-04 23:43:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-08 02:03:08 +01:00
|
|
|
void RS232Bridge::onPacketReceived(mesh::Packet *packet) {
|
|
|
|
|
handleReceivedPacket(packet);
|
2025-09-04 23:43:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|