40inline constexpr uint8_t
FLAG = 0x7E;
41inline constexpr uint8_t
ESCAPE = 0x7D;
51 return 2 * payloadLen + 3;
63inline std::size_t
EncodeFrame(
const uint8_t* shtp, std::size_t len, uint8_t* out,
64 std::size_t cap)
noexcept {
66 auto put = [&](uint8_t b) {
74 for (std::size_t i = 0; i < len; ++i) {
75 const uint8_t b = shtp[i];
84 return n <= cap ? n : 0;
98template <std::
size_t Capacity>
119 state_ = State::Inside;
125 state_ = State::Escaped;
133 const Event ev = finishFrame();
139 const uint8_t*
Data() const noexcept {
144 std::size_t
Size() const noexcept {
160 state_ = State::Outside;
168 enum class State : uint8_t { Outside, Inside, Escaped };
170 void startFrame() noexcept {
171 state_ = State::Inside;
176 void store(uint8_t c)
noexcept {
177 if (len_ < Capacity) {
184 Event finishFrame() noexcept {
196 bufferSpace_ =
static_cast<uint16_t
>(frame_[1] | (frame_[2] << 8));
202 static_assert(Capacity >= 3,
"a frame needs room for the protocol ID and a buffer status");
204 uint8_t frame_[Capacity]{};
206 std::size_t shtpLen_{0};
207 uint16_t bufferSpace_{0};
208 bool overflow_{
false};
209 State state_{State::Outside};
Byte-at-a-time decoder for frames received from the hub.
Definition bno08x_shtp_uart.hpp:99
std::size_t Size() const noexcept
Length of the last SHTP transfer.
Definition bno08x_shtp_uart.hpp:144
Event Push(uint8_t c) noexcept
Feed one received byte.
Definition bno08x_shtp_uart.hpp:110
const uint8_t * Data() const noexcept
SHTP transfer of the last Event::ShtpFrame (header first).
Definition bno08x_shtp_uart.hpp:139
void ClearBufferSpace() noexcept
Forget the reported buffer space (the next frame needs a new query).
Definition bno08x_shtp_uart.hpp:154
void Reset() noexcept
Drop any partial frame and wait for the next flag.
Definition bno08x_shtp_uart.hpp:159
uint16_t BufferSpace() const noexcept
Free space the hub reported in its last Buffer Status Notification.
Definition bno08x_shtp_uart.hpp:149
Event
What the last byte completed.
Definition bno08x_shtp_uart.hpp:102
@ BufferStatus
A Buffer Status Notification updated BufferSpace().
@ None
Nothing complete yet.
@ Dropped
A frame was discarded (overflow or unknown protocol ID).
@ ShtpFrame
An SHTP transfer is in Data() / Size().
Definition bno08x_shtp_uart.hpp:33
constexpr uint32_t BAUD_RATE
UART-SHTP line rate: 3 Mbit/s, 8 data bits, no parity, 1 stop bit (datasheet 1.2.3....
Definition bno08x_shtp_uart.hpp:36
constexpr uint8_t PROTOCOL_SHTP
Protocol ID of SHTP transfers.
Definition bno08x_shtp_uart.hpp:44
constexpr std::size_t MaxEncodedSize(std::size_t payloadLen) noexcept
Largest frame EncodeFrame() can produce for payloadLen bytes (every byte escaped).
Definition bno08x_shtp_uart.hpp:50
constexpr uint8_t FLAG
Frame delimiter.
Definition bno08x_shtp_uart.hpp:40
constexpr uint32_t HOST_BYTE_GAP_US
Minimum spacing between bytes sent from the host to the hub, in µs (datasheet 1.2....
Definition bno08x_shtp_uart.hpp:38
constexpr uint8_t BUFFER_STATUS_QUERY[3]
Buffer Status Query, ready to send.
Definition bno08x_shtp_uart.hpp:47
std::size_t EncodeFrame(const uint8_t *shtp, std::size_t len, uint8_t *out, std::size_t cap) noexcept
Wrap one SHTP transfer (header included) in a UART-SHTP frame.
Definition bno08x_shtp_uart.hpp:63
constexpr uint8_t ESCAPE
Escape: the next byte is XOR ESCAPE_XOR.
Definition bno08x_shtp_uart.hpp:41
constexpr uint8_t ESCAPE_XOR
Value XORed into an escaped byte.
Definition bno08x_shtp_uart.hpp:42
constexpr uint8_t PROTOCOL_CONTROL
Protocol ID of control frames (BSQ / BSN).
Definition bno08x_shtp_uart.hpp:43