Skip to main content

Module cobs

Module cobs 

Source
Expand description

COBS framing, Consistent Overhead Byte Stuffing.

COBS (Cheshire and Baker, 1999) frames a packet by removing every zero byte from it, so a single zero byte, the DELIMITER, can mark the end of a frame and never be confused with data. It encodes each run of up to 254 non-zero bytes as a length code followed by the bytes themselves; a code of 0xFF marks a full run of 254 non-zero bytes with no zero after it, and a code from 0x01 to 0xFE marks a shorter run that ended at a zero.

Its appeal over SLIP is the overhead: where SLIP can double a worst-case payload, COBS adds at most one byte per 254 (see max_encoded_len), which is why motor-control and robotics links that care about predictable framing cost prefer it.

encode produces the encoded block followed by the trailing zero delimiter, ready for the wire; decode accepts a frame with or without that delimiter.

Structs§

CobsDecoder
A streaming COBS decoder that reassembles whole frames from a serial byte stream.

Constants§

DELIMITER
The COBS frame delimiter: a single zero byte, the one value the encoding removes from the payload so it can mark a frame boundary unambiguously.

Functions§

decode
Decodes a single COBS frame, recovering the original payload.
encode
Encodes a payload into a COBS frame, terminated by the DELIMITER byte.
max_encoded_len
Returns an output length that is always large enough to hold the COBS encoding of a payload of payload_len bytes, including the trailing DELIMITER.