Add nRF52 support and refactor packet handling

This commit introduces several improvements to the SerialBridge helper:

- Adds support for the nRF52 platform by implementing the `setPins` configuration.
- Corrects the type cast for the RP2040 platform from `HardwareSerial` to `SerialUART`.
- Refactors packet deserialization to use a new `Packet::readFrom()` method instead of a direct `memcpy`, improving encapsulation.
- Updates the packet length validation to use the more appropriate `MAX_TRANS_UNIT` constant.
This commit is contained in:
João Brázio
2025-09-05 09:22:06 +01:00
parent 77ab19153e
commit 375093f78d

View File

@@ -30,9 +30,11 @@ SerialBridge::SerialBridge(Stream& serial, mesh::PacketManager* mgr, mesh::RTCCl
void SerialBridge::begin() { void SerialBridge::begin() {
#if defined(ESP32) #if defined(ESP32)
((HardwareSerial *)_serial)->setPins(BRIDGE_OVER_SERIAL_RX, BRIDGE_OVER_SERIAL_TX); ((HardwareSerial *)_serial)->setPins(BRIDGE_OVER_SERIAL_RX, BRIDGE_OVER_SERIAL_TX);
#elif defined(NRF52_PLATFORM)
((HardwareSerial *)_serial)->setPins(BRIDGE_OVER_SERIAL_RX, BRIDGE_OVER_SERIAL_TX);
#elif defined(RP2040_PLATFORM) #elif defined(RP2040_PLATFORM)
((HardwareSerial*)_serial)->setRX(BRIDGE_OVER_SERIAL_RX); ((SerialUART *)_serial)->setRX(BRIDGE_OVER_SERIAL_RX);
((HardwareSerial*)_serial)->setTX(BRIDGE_OVER_SERIAL_TX); ((SerialUART *)_serial)->setTX(BRIDGE_OVER_SERIAL_TX);
#elif defined(STM32_PLATFORM) #elif defined(STM32_PLATFORM)
((HardwareSerial *)_serial)->setRx(BRIDGE_OVER_SERIAL_RX); ((HardwareSerial *)_serial)->setRx(BRIDGE_OVER_SERIAL_RX);
((HardwareSerial *)_serial)->setTx(BRIDGE_OVER_SERIAL_TX); ((HardwareSerial *)_serial)->setTx(BRIDGE_OVER_SERIAL_TX);
@@ -82,7 +84,7 @@ void SerialBridge::loop() {
if (_rx_buffer_pos >= 4) { if (_rx_buffer_pos >= 4) {
uint16_t len = (_rx_buffer[2] << 8) | _rx_buffer[3]; uint16_t len = (_rx_buffer[2] << 8) | _rx_buffer[3];
if (len > MAX_PACKET_PAYLOAD) { if (len > (MAX_TRANS_UNIT + 1)) {
_rx_buffer_pos = 0; // Invalid length, reset _rx_buffer_pos = 0; // Invalid length, reset
return; return;
} }
@@ -95,8 +97,7 @@ void SerialBridge::loop() {
#endif #endif
mesh::Packet* pkt = _mgr->allocNew(); mesh::Packet* pkt = _mgr->allocNew();
if (pkt) { if (pkt) {
memcpy(pkt->payload, _rx_buffer + 4, len); pkt->readFrom(_rx_buffer + 4, len);
pkt->payload_len = len;
onPacketReceived(pkt); onPacketReceived(pkt);
} }
} }