22#include "pw_assert/assert.h"
23#include "pw_bytes/span.h"
24#include "pw_checksum/crc32.h"
25#include "pw_hdlc/internal/protocol.h"
26#include "pw_result/result.h"
27#include "pw_status/status.h"
39 static constexpr size_t kMinContentSizeBytes =
40 kMinAddressSize + kControlSize + kFcsSize;
44 constexpr uint64_t address()
const {
return address_; }
46 constexpr std::byte control()
const {
return control_; }
54 : data_(data), address_(address), control_(control) {}
71 last_read_bytes_index_(0),
72 current_frame_size_(0),
73 state_(State::kInterFrame) {}
96 size_t max_frame_size) {
99 return max_frame_size < Frame::kMinContentSizeBytes
100 ? Frame::kMinContentSizeBytes
101 : max_frame_size - 2;
106 template <
typename F,
typename... Args>
108 for (std::byte b : data) {
111 callback(std::forward<Args>(args)..., result);
121 state_ = State::kInterFrame;
134 current_frame_size_ = 0;
135 last_read_bytes_index_ = 0;
139 void AppendByte(std::byte new_byte);
141 Status CheckFrame()
const;
143 bool VerifyFrameCheckSequence()
const;
151 std::array<std::byte,
sizeof(uint32_t)> last_read_bytes_;
152 size_t last_read_bytes_index_;
155 checksum::Crc32 fcs_;
157 size_t current_frame_size_;
163template <
size_t kSizeBytes>
178 static constexpr size_t max_size() {
return kSizeBytes; }
181 static_assert(kSizeBytes >= Frame::kMinContentSizeBytes);
183 std::array<std::byte, kSizeBytes> frame_buffer_;
static constexpr Status Unavailable()
Definition: status.h:304
Declares a buffer along with a Decoder.
Definition: decoder.h:164
DecoderBuffer & operator=(DecoderBuffer &&)=delete
static constexpr size_t max_size()
Definition: decoder.h:178
DecoderBuffer(DecoderBuffer &&)=delete
Result< Frame > Process(std::byte new_byte)
Parses a single byte of an HDLC stream.
void Process(ConstByteSpan data, F &&callback, Args &&... args)
Processes a span of data and calls the provided callback with each frame or error.
Definition: decoder.h:107
size_t max_size() const
Definition: decoder.h:117
static constexpr size_t RequiredBufferSizeForFrameSize(size_t max_frame_size)
Definition: decoder.h:95
void Clear()
Clears and resets the decoder.
Definition: decoder.h:120