aboutsummaryrefslogtreecommitdiff
path: root/pw_hdlc/public/pw_hdlc/decoder.h
diff options
context:
space:
mode:
Diffstat (limited to 'pw_hdlc/public/pw_hdlc/decoder.h')
-rw-r--r--pw_hdlc/public/pw_hdlc/decoder.h28
1 files changed, 16 insertions, 12 deletions
diff --git a/pw_hdlc/public/pw_hdlc/decoder.h b/pw_hdlc/public/pw_hdlc/decoder.h
index 94caf17b0..0290a2ad8 100644
--- a/pw_hdlc/public/pw_hdlc/decoder.h
+++ b/pw_hdlc/public/pw_hdlc/decoder.h
@@ -22,6 +22,7 @@
#include "pw_assert/assert.h"
#include "pw_bytes/span.h"
#include "pw_checksum/crc32.h"
+#include "pw_hdlc/internal/protocol.h"
#include "pw_result/result.h"
#include "pw_status/status.h"
@@ -30,19 +31,11 @@ namespace pw::hdlc {
// Represents the contents of an HDLC frame -- the unescaped data between two
// flag bytes. Instances of Frame are only created when a full, valid frame has
// been read.
-//
-// For now, the Frame class assumes a single-byte control field and a 32-bit
-// frame check sequence (FCS).
class Frame {
- private:
- static constexpr size_t kMinimumAddressSize = 1;
- static constexpr size_t kControlSize = 1;
- static constexpr size_t kFcsSize = sizeof(uint32_t);
-
public:
// The minimum size of a frame, excluding control bytes (flag or escape).
- static constexpr size_t kMinSizeBytes =
- kMinimumAddressSize + kControlSize + kFcsSize;
+ static constexpr size_t kMinContentSizeBytes =
+ kMinAddressSize + kControlSize + kFcsSize;
static Result<Frame> Parse(ConstByteSpan frame);
@@ -95,6 +88,17 @@ class Decoder {
//
Result<Frame> Process(std::byte new_byte);
+ // Returns the buffer space required for a `Decoder` to successfully decode a
+ // frame whose on-the-wire HDLC encoded size does not exceed `max_frame_size`.
+ static constexpr size_t RequiredBufferSizeForFrameSize(
+ size_t max_frame_size) {
+ // Flag bytes aren't stored in the internal buffer, so we can save a couple
+ // bytes.
+ return max_frame_size < Frame::kMinContentSizeBytes
+ ? Frame::kMinContentSizeBytes
+ : max_frame_size - 2;
+ }
+
// Processes a span of data and calls the provided callback with each frame or
// error.
template <typename F, typename... Args>
@@ -115,7 +119,7 @@ class Decoder {
void Clear() {
state_ = State::kInterFrame;
Reset();
- };
+ }
private:
// State enum class is used to make the Decoder a finite state machine.
@@ -165,7 +169,7 @@ class DecoderBuffer : public Decoder {
static constexpr size_t max_size() { return kSizeBytes; }
private:
- static_assert(kSizeBytes >= Frame::kMinSizeBytes);
+ static_assert(kSizeBytes >= Frame::kMinContentSizeBytes);
std::array<std::byte, kSizeBytes> frame_buffer_;
};