Consistent Overhead Byte Stuffing (COBS) codec and zero-copy frame streamer.
Classes | |
| class | pw::cobs::EncodeBuffer< kMaxPayloadSize > |
| class | pw::cobs::Streamer |
| class | pw::cobs::StreamerBuffer< kSizeBytes > |
| class | pw::cobs::Decoder |
| class | pw::cobs::DecoderBuffer< kSizeBytes > |
A Decoder with an inline decode buffer of kSizeBytes bytes. More... | |
Typedefs | |
| using | pw::cobs::EncodeBuffer< kMaxPayloadSize >::value_type = std::byte |
| using | pw::cobs::EncodeBuffer< kMaxPayloadSize >::size_type = size_t |
| using | pw::cobs::EncodeBuffer< kMaxPayloadSize >::difference_type = ptrdiff_t |
| using | pw::cobs::EncodeBuffer< kMaxPayloadSize >::reference = std::byte & |
| using | pw::cobs::EncodeBuffer< kMaxPayloadSize >::const_reference = const std::byte & |
| using | pw::cobs::EncodeBuffer< kMaxPayloadSize >::pointer = std::byte * |
| using | pw::cobs::EncodeBuffer< kMaxPayloadSize >::const_pointer = const std::byte * |
| using | pw::cobs::EncodeBuffer< kMaxPayloadSize >::iterator = containers::PtrIterator< EncodeBuffer > |
| using | pw::cobs::EncodeBuffer< kMaxPayloadSize >::const_iterator = containers::ConstPtrIterator< EncodeBuffer > |
Functions | |
| constexpr size_t | pw::cobs::MaxEncodedSize (size_t unencoded_size) |
| constexpr size_t | pw::cobs::MaxDecodedSize (size_t encoded_size) |
| Result< ByteSpan > | pw::cobs::Encode (ConstByteSpan input, ByteSpan output) |
| Result< ByteSpan > | pw::cobs::Decode (ConstByteSpan encoded, ByteSpan output) |
| Result< ByteSpan > | pw::cobs::EncodeInPlace (ByteSpan buffer, size_t payload_size) |
| pw::cobs::EncodeBuffer< kMaxPayloadSize >::EncodeBuffer (ConstByteSpan initial) | |
| constexpr std::byte * | pw::cobs::EncodeBuffer< kMaxPayloadSize >::data () |
| constexpr const std::byte * | pw::cobs::EncodeBuffer< kMaxPayloadSize >::data () const |
| constexpr size_t | pw::cobs::EncodeBuffer< kMaxPayloadSize >::size () const |
| Current unencoded payload length in bytes. | |
| static constexpr size_t | pw::cobs::EncodeBuffer< kMaxPayloadSize >::max_size () |
| Maximum unencoded payload capacity in bytes. | |
| static constexpr size_t | pw::cobs::EncodeBuffer< kMaxPayloadSize >::capacity () |
| constexpr bool | pw::cobs::EncodeBuffer< kMaxPayloadSize >::empty () const |
| constexpr iterator | pw::cobs::EncodeBuffer< kMaxPayloadSize >::begin () |
| constexpr const_iterator | pw::cobs::EncodeBuffer< kMaxPayloadSize >::begin () const |
| constexpr const_iterator | pw::cobs::EncodeBuffer< kMaxPayloadSize >::cbegin () const |
| constexpr iterator | pw::cobs::EncodeBuffer< kMaxPayloadSize >::end () |
| constexpr const_iterator | pw::cobs::EncodeBuffer< kMaxPayloadSize >::end () const |
| constexpr const_iterator | pw::cobs::EncodeBuffer< kMaxPayloadSize >::cend () const |
| constexpr std::byte & | pw::cobs::EncodeBuffer< kMaxPayloadSize >::operator[] (size_t index) |
| constexpr const std::byte & | pw::cobs::EncodeBuffer< kMaxPayloadSize >::operator[] (size_t index) const |
| constexpr void | pw::cobs::EncodeBuffer< kMaxPayloadSize >::clear () |
| Resets the unencoded payload length to 0 so a new payload can be built. | |
| void | pw::cobs::EncodeBuffer< kMaxPayloadSize >::resize (size_t new_size) |
| void | pw::cobs::EncodeBuffer< kMaxPayloadSize >::push_back (std::byte b) |
| void | pw::cobs::EncodeBuffer< kMaxPayloadSize >::append (ConstByteSpan bytes) |
| ConstByteSpan | pw::cobs::EncodeBuffer< kMaxPayloadSize >::Encode () |
| ConstByteSpan | pw::cobs::EncodeBuffer< kMaxPayloadSize >::Encode (size_t payload_size) |
| constexpr | pw::cobs::Streamer::Streamer (ByteSpan buffer, bool emit_leading_delimiter=false) |
| constexpr void | pw::cobs::Streamer::Reset (bool emit_leading_delimiter=false) |
| ConstByteSpan | pw::cobs::Streamer::Write (ConstByteSpan data) |
| ByteSpan | pw::cobs::Streamer::Flush () |
| ByteSpan | pw::cobs::Streamer::Finish () |
| constexpr ConstByteSpan | pw::cobs::Streamer::AbortSequence () const |
| constexpr | pw::cobs::StreamerBuffer< kSizeBytes >::StreamerBuffer (bool emit_leading_delimiter=false) |
| pw::cobs::StreamerBuffer< kSizeBytes >::StreamerBuffer (const StreamerBuffer &)=delete | |
| StreamerBuffer & | pw::cobs::StreamerBuffer< kSizeBytes >::operator= (const StreamerBuffer &)=delete |
| pw::cobs::StreamerBuffer< kSizeBytes >::StreamerBuffer (StreamerBuffer &&)=delete | |
| StreamerBuffer & | pw::cobs::StreamerBuffer< kSizeBytes >::operator= (StreamerBuffer &&)=delete |
| static constexpr size_t | pw::cobs::StreamerBuffer< kSizeBytes >::max_size () |
| constexpr | pw::cobs::Decoder::Decoder (ByteSpan decode_buffer) |
| void | pw::cobs::Decoder::Clear () |
| Discards any partially received frame and resets the decoder to idle. | |
| constexpr bool | pw::cobs::Decoder::in_progress () const |
| Result< ByteSpan > | pw::cobs::Decoder::Process (std::byte b) |
| template<typename Callback > | |
| void | pw::cobs::Decoder::Process (ConstByteSpan data, Callback &&callback) |
| pw::cobs::DecoderBuffer< kSizeBytes >::DecoderBuffer (const DecoderBuffer &)=delete | |
| DecoderBuffer & | pw::cobs::DecoderBuffer< kSizeBytes >::operator= (const DecoderBuffer &)=delete |
| pw::cobs::DecoderBuffer< kSizeBytes >::DecoderBuffer (DecoderBuffer &&)=delete | |
| DecoderBuffer & | pw::cobs::DecoderBuffer< kSizeBytes >::operator= (DecoderBuffer &&)=delete |
| static constexpr size_t | pw::cobs::DecoderBuffer< kSizeBytes >::max_size () |
| Returns the capacity of the decode buffer in bytes. | |
Variables | |
| constexpr size_t | pw::cobs::kMaxRunLength = 254 |
| Maximum number of contiguous non-zero bytes in a single COBS block. | |
| static constexpr size_t | pw::cobs::Streamer::kMinBufferSize = MaxEncodedSize(kMaxRunLength) + 1 |
| static constexpr std::array< std::byte, 2 > | pw::cobs::Streamer::kAbortSequence |
Friends | |
| Result< ByteSpan > | pw::cobs::Streamer::Encode (ConstByteSpan input, ByteSpan output) |
| Result< ByteSpan > | pw::cobs::Decoder::Decode (ConstByteSpan encoded, ByteSpan output) |
|
inlineconstexpr |
|
inline |
Appends bytes to the unencoded payload.
size() + bytes.size() <= max_size().
|
inlineconstexpr |
| Result< ByteSpan > pw::cobs::Decode | ( | ConstByteSpan | encoded, |
| ByteSpan | output | ||
| ) |
Decodes a single 0x00-terminated COBS frame encoded into output.
encoded and output must not overlap.output on success or one of the following error codes on failure:encoded is not a valid 0x00-terminated COBS frame.output is too small to hold the decoded payload. Sizing output to MaxDecodedSize(encoded.size()) guarantees success.
|
inlineexplicitconstexpr |
Constructs a Decoder that writes decoded payloads into decode_buffer.
Size decode_buffer to the maximum expected decoded payload length (or use DecoderBuffer<kMaxPayloadSize>).
|
inline |
Encodes the current size() payload bytes in-place and returns the resulting 0x00-terminated COBS frame.
Encode() call on this EncodeBuffer. Because Encode() overwrites the buffer in-place, call clear() or resize() before building a new payload. | Result< ByteSpan > pw::cobs::Encode | ( | ConstByteSpan | input, |
| ByteSpan | output | ||
| ) |
Encodes input into output as a 0x00-terminated COBS frame.
input and output must not overlap (use EncodeInPlace() or EncodeBuffer for in-place encoding).output (including trailing 0x00) on success or one of the following error codes on failure:output is too small to hold the encoded frame. Sizing output to MaxEncodedSize(input.size()) guarantees success.
|
inline |
Resizes the payload to payload_size bytes (for payloads written directly via data()) and encodes in-place.
payload_size <= max_size(). Encode(). Encodes a payload in-place inside buffer, appending a trailing 0x00 frame delimiter.
Prefer EncodeBuffer<kMaxPayloadSize> when allocating a fixed-capacity buffer on the stack or inside a class. Use EncodeInPlace directly when operating on an externally provided ByteSpan.
buffer.size() >= MaxEncodedSize(payload_size). buffer.subspan(1, payload_size) before calling EncodeInPlace (buffer[0] must be left reserved in front of the payload).buffer (including trailing 0x00) on success or one of the following error codes on failure:buffer.size() < MaxEncodedSize(payload_size). | ByteSpan pw::cobs::Streamer::Finish | ( | ) |
| ByteSpan pw::cobs::Streamer::Flush | ( | ) |
|
inlineconstexpr |
Returns true if a frame is currently being assembled (at least one non-zero byte has been received since the last 0x00 delimiter or Clear()).
|
constexpr |
|
constexpr |
Returns the worst-case encoded frame size in bytes for a payload of unencoded_size bytes, including the trailing 0x00 delimiter.
Use this to size output buffers passed to Encode() or EncodeInPlace().
|
inline |
Processes a chunk of incoming wire bytes and invokes callback with a Result<ByteSpan> (Ok, DataLoss, or ResourceExhausted) for each frame that terminates within data.
ByteSpan passed to callback points into decode_buffer and is only valid for the duration of that callback invocation. Processes a single incoming wire byte b.
The decoder automatically resets at every 0x00 delimiter (whether the frame succeeded or failed), so the next byte begins a new frame without calling Clear().
0x00 delimiter was ignored).0x00 delimiter. The returned ByteSpan points into decode_buffer and remains valid until the next Process() or Clear() call.0x00.decode_buffer.size() (reported when the frame's closing 0x00 delimiter arrives).
|
inline |
Appends a single byte to the unencoded payload.
size() < max_size().
|
inlineconstexpr |
Discards any buffered state and prepares to encode a new frame.
| emit_leading_delimiter | If true, prepends a leading 0x00 delimiter byte to the first chunk of the new frame. |
|
inline |
Sets the unencoded payload length to new_size bytes.
new_size <= max_size().
|
inlineexplicitconstexpr |
Constructs a Streamer that writes encoded chunks into buffer.
buffer.size() >= kMinBufferSize to stream arbitrary payloads (or use StreamerBuffer<>, which provides the buffer automatically). | emit_leading_delimiter | If true, prepends a leading 0x00 delimiter byte to the first emitted chunk to flush any prior line noise on the receiver. |
| ConstByteSpan pw::cobs::Streamer::Write | ( | ConstByteSpan | data | ) |
Encodes as much of data as fits into the output buffer and returns the unconsumed remainder of data.
data was consumed and you may call Write() with the next payload piece or Finish() to end the frame. Flush(), transmit that chunk, and pass the returned remainder back to Write().
|
staticconstexpr |
2-byte sequence (0xFF, 0x00) that aborts an in-progress frame on the wire so the receiver discards it with DATA_LOSS.
|
staticconstexpr |
Minimum buffer size (257 bytes) required to stream arbitrarily large frames across multiple Flush() calls.
|
friend |
Decodes a single 0x00-terminated COBS frame encoded into output.
encoded and output must not overlap.output on success or one of the following error codes on failure:encoded is not a valid 0x00-terminated COBS frame.output is too small to hold the decoded payload. Sizing output to MaxDecodedSize(encoded.size()) guarantees success.
|
friend |
Encodes input into output as a 0x00-terminated COBS frame.
input and output must not overlap (use EncodeInPlace() or EncodeBuffer for in-place encoding).output (including trailing 0x00) on success or one of the following error codes on failure:output is too small to hold the encoded frame. Sizing output to MaxEncodedSize(input.size()) guarantees success.