Rework packet serialization and parsing

This commit is contained in:
João Brázio 2025-09-05 01:50:50 +01:00
parent ee3c4baea5
commit 2b920dfed3
No known key found for this signature in database
GPG key ID: 56A1490716A324DD
3 changed files with 82 additions and 56 deletions

View file

@ -21,12 +21,38 @@ public:
void begin() override;
void loop() override;
void onPacketTransmitted(mesh::Packet* packet) override;
void onPacketReceived() override;
void onPacketReceived(mesh::Packet* packet) override;
private:
/**
* @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;
SimpleMeshTables _seen_packets;
uint8_t _rx_buffer[MAX_SERIAL_PACKET_SIZE]; // Buffer for serial data
uint16_t _rx_buffer_pos = 0;
};
#endif