LSH-Core
Deterministic firmware core for Controllino-based Labo Smart Home nodes
 
Loading...
Searching...
No Matches
msgpack_serial_framing.hpp
Go to the documentation of this file.
1
21#ifndef LSH_CORE_COMMUNICATION_MSGPACK_SERIAL_FRAMING_HPP
22#define LSH_CORE_COMMUNICATION_MSGPACK_SERIAL_FRAMING_HPP
23
24#include <stdint.h>
25
26namespace lsh::core::transport
27{
28constexpr uint8_t MSGPACK_FRAME_END = 0xC0U;
29constexpr uint8_t MSGPACK_FRAME_ESCAPE = 0xDBU;
30constexpr uint8_t MSGPACK_FRAME_ESCAPED_END = 0xDCU;
31constexpr uint8_t MSGPACK_FRAME_ESCAPED_ESCAPE = 0xDDU;
32
36enum class MsgPackFrameConsumeResult : uint8_t
37{
41};
42
47{
48private:
49 char *const frameBuffer;
50 const uint16_t frameCapacity;
51 uint16_t frameLengthBytes = 0U;
52 uint32_t lastByteTimeMs = 0U;
53 bool escapePending = false;
54 bool discardUntilFrameEnd = false;
55
56 [[nodiscard]] auto appendByte(uint8_t byte) -> bool;
57 void startDiscarding() noexcept;
58
59public:
60 MsgPackFrameReceiver(char *buffer, uint16_t capacity) noexcept;
61
62 void reset() noexcept;
63 void resetIfIdle(uint32_t nowMs, uint32_t idleTimeoutMs) noexcept;
64 [[nodiscard]] auto consumeByte(uint8_t byte, uint32_t nowMs) -> MsgPackFrameConsumeResult;
65 [[nodiscard]] auto frameData() const -> const uint8_t *;
66 [[nodiscard]] auto frameLength() const -> uint16_t;
67};
68
69} // namespace lsh::core::transport
70
71#endif // LSH_CORE_COMMUNICATION_MSGPACK_SERIAL_FRAMING_HPP
Incremental receiver for the SLIP-like MsgPack framing used on the bridge serial link.
Definition msgpack_serial_framing.hpp:47
auto frameData() const -> const uint8_t *
Return the beginning of the completed payload buffer.
Definition msgpack_serial_framing.cpp:190
auto frameLength() const -> uint16_t
Return the current payload length assembled by the receiver.
Definition msgpack_serial_framing.cpp:200
auto consumeByte(uint8_t byte, uint32_t nowMs) -> MsgPackFrameConsumeResult
Feed one raw serial byte into the framed MsgPack receiver.
Definition msgpack_serial_framing.cpp:115
void resetIfIdle(uint32_t nowMs, uint32_t idleTimeoutMs) noexcept
Drop a partially received frame that has been silent for too long.
Definition msgpack_serial_framing.cpp:55
void reset() noexcept
Forget the current receive state and return to the idle state.
Definition msgpack_serial_framing.cpp:39
constexpr uint8_t MSGPACK_FRAME_END
Delimiter byte that separates adjacent framed MsgPack payloads.
Definition msgpack_serial_framing.hpp:28
constexpr uint8_t MSGPACK_FRAME_ESCAPED_END
Escaped representation of MSGPACK_FRAME_END.
Definition msgpack_serial_framing.hpp:30
constexpr uint8_t MSGPACK_FRAME_ESCAPE
Escape marker emitted before reserved payload bytes.
Definition msgpack_serial_framing.hpp:29
constexpr uint8_t MSGPACK_FRAME_ESCAPED_ESCAPE
Escaped representation of MSGPACK_FRAME_ESCAPE.
Definition msgpack_serial_framing.hpp:31
MsgPackFrameConsumeResult
Outcome of feeding one raw serial byte into the MsgPack frame receiver.
Definition msgpack_serial_framing.hpp:37
@ FrameDiscarded
One corrupted or oversized frame has just been dropped at its closing delimiter.
@ Incomplete
The byte has been consumed, but no full payload is ready yet.
@ FrameComplete
One complete deframed payload is ready in the destination buffer.