Comms CCF
This is a simple communication layer based on COBS, CBOR and FNV-1A
Loading...
Searching...
No Matches
cobs.hpp
Go to the documentation of this file.
1
33
34#pragma once
35
36#include <stddef.h>
37#include <stdint.h>
38
39#include <span>
40#include <iterator>
41
42namespace Cobs
43{
46 constexpr uint8_t maxRunLength = 254;
47
50 constexpr size_t maxEncodedSize(size_t dataSize)
51 {
52 size_t overhead = (dataSize + maxRunLength)/maxRunLength;
53 return dataSize + overhead;
54 }
55
60 class Encoder
61 {
62 public:
63 template<size_t Extent>
64 Encoder(std::span<const uint8_t, Extent> _data)
65 : data(_data), runLength(findRunLength()) { }
66
67 template<size_t Extent>
68 Encoder(std::span<uint8_t, Extent> span)
69 : Encoder(std::span{
70 reinterpret_cast<const uint8_t *>(span.data()),
71 span.size()})
72 {
73 }
74
75 Encoder begin() const { return *this; }
76 std::nullptr_t end() const { return {}; }
77
78 // Iterator functionality
79 using difference_type = ptrdiff_t;
80 using value_type = uint8_t;
81
82 value_type operator*() const;
83 Encoder & operator++();
84 Encoder operator++(int) { return ++*this; }
85 bool operator!=(std::nullptr_t) const;
86
87 private:
96 uint8_t findRunLength();
97
98 std::span<const uint8_t> data;
100 uint8_t runLength;
101 uint8_t runIndex = 0;
102 bool runHeaderOutput = false;
103 };
104 static_assert(std::input_iterator<Encoder>);
105
108 {
109 public:
112 bool feed(uint8_t byte);
113
117 uint8_t get(uint8_t byte) const;
118
119 private:
120 uint8_t runLength = 0;
121 bool runLengthWasMax = true;
122 };
123};
Decodes data as a state-machine.
Definition cobs.hpp:108
uint8_t get(uint8_t byte) const
Definition cobs.cpp:118
bool feed(uint8_t byte)
Definition cobs.cpp:94
constexpr size_t maxEncodedSize(size_t dataSize)
Definition cobs.hpp:50
constexpr uint8_t maxRunLength
Definition cobs.hpp:46