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"
37 static constexpr size_t kMinContentSizeBytes =
38 kMinAddressSize + kControlSize + kFcsSize;
40 static Result<Frame> Parse(ConstByteSpan frame);
42 constexpr uint64_t address()
const {
return address_; }
44 constexpr std::byte control()
const {
return control_; }
46 constexpr ConstByteSpan data()
const {
return data_; }
51 constexpr Frame(uint64_t address, std::byte control, ConstByteSpan data)
52 : data_(data), address_(address), control_(control) {}
68 constexpr Decoder(ByteSpan buffer)
71 last_read_bytes_index_(0),
72 current_frame_size_(0),
73 state_(State::kInterFrame) {}
104 static constexpr size_t RequiredBufferSizeForFrameSize(
105 size_t max_frame_size) {
108 return max_frame_size < Frame::kMinContentSizeBytes
109 ? Frame::kMinContentSizeBytes
110 : max_frame_size - 2;
115 template <
typename F,
typename... Args>
116 void Process(ConstByteSpan data, F&& callback, Args&&... args) {
117 for (std::byte b : data) {
119 if (result.status() != Status::Unavailable()) {
120 callback(std::forward<Args>(args)..., result);
126 size_t max_size()
const {
return buffer_.size(); }
130 state_ = State::kInterFrame;
143 current_frame_size_ = 0;
144 last_read_bytes_index_ = 0;
148 void AppendByte(std::byte new_byte);
150 Status CheckFrame()
const;
152 bool VerifyFrameCheckSequence()
const;
160 std::array<std::byte,
sizeof(uint32_t)> last_read_bytes_;
161 size_t last_read_bytes_index_;
164 checksum::Crc32 fcs_;
166 size_t current_frame_size_;
172template <
size_t kSizeBytes>
187 static constexpr size_t max_size() {
return kSizeBytes; }
190 static_assert(kSizeBytes >= Frame::kMinContentSizeBytes);
192 std::array<std::byte, kSizeBytes> frame_buffer_;
Definition: decoder.h:173
DecoderBuffer & operator=(DecoderBuffer &&)=delete
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:116