Group baton_protocol¶
CRC-16, incremental stream parser and frame encoder. More...
Classes¶
| Type | Name |
|---|---|
| struct | baton_frame One decoded frame (header fields and payload, without SYNC and CRC). |
| struct | baton_parser Incremental parser state for one byte stream. |
Public Functions¶
| Type | Name |
|---|---|
| uint16_t | baton_crc16 (const uint8_t * data, size_t length) Calculate the CRC-16/CCITT-FALSE of a buffer (poly 0x1021, init 0xFFFF). |
| uint16_t | baton_crc16_update (uint16_t crc, uint8_t byte) Add one byte to a CRC-16/CCITT-FALSE register. |
| int | baton_encode (uint8_t dst, uint8_t src, uint8_t ctrl, const uint8_t * payload, size_t payload_length, uint8_t * output, size_t output_capacity, size_t * output_length) Encode one frame: SYNC, header, payload and CRC. |
| int | baton_parser_feed (struct baton_parser * parser, uint8_t byte) Feed one byte to the incremental frame parser. |
| void | baton_parser_init (struct baton_parser * parser) Reset a parser before starting or restarting a byte stream. |
Macros¶
| Type | Name |
|---|---|
| define | BATON_CRC_INIT 0xFFFFUInitial value of the CRC-16/CCITT-FALSE register. |
| define | BATON_FRAME_MAX ([**BATON\_FRAME\_OVERHEAD**](group__baton__defs.md#define-baton_frame_overhead) + [**BATON\_MAX\_PAYLOAD**](group__baton__config.md#define-baton_max_payload))Largest complete frame on the wire, SYNC to CRC. |
| define | BATON_PARSER_IDLE 0UValue of baton_parser::state while the parser waits for SYNC (between frames). |
| define | BATON_PARSE_ERROR (-1)Error returned for invalid input, malformed frames, or insufficient capacity. |
Detailed Description¶
Pure functions over caller-owned buffers: no state outside the structures passed in, safe to use from several contexts on different structures.
** **
- CRC: baton_crc16, baton_crc16_update.
- Parser: baton_parser_init, baton_parser_feed.
- Encoder: baton_encode.
Public Functions Documentation¶
function baton_crc16¶
Calculate the CRC-16/CCITT-FALSE of a buffer (poly 0x1021, init 0xFFFF).
Parameters:
dataBytes covered by the CRC.lengthNumber of bytes to process.
Returns:
Calculated CRC; BATON_CRC_INIT when data is NULL.
function baton_crc16_update¶
Add one byte to a CRC-16/CCITT-FALSE register.
Parameters:
crcCurrent register value (BATON_CRC_INIT before the first byte).byteNext byte.
Returns:
Updated register value.
function baton_encode¶
Encode one frame: SYNC, header, payload and CRC.
int baton_encode (
uint8_t dst,
uint8_t src,
uint8_t ctrl,
const uint8_t * payload,
size_t payload_length,
uint8_t * output,
size_t output_capacity,
size_t * output_length
)
payload may point to &output[BATON_HEADER_SIZE]: a payload that was built in place is then not copied.
Parameters:
dstDestination address.srcSource address.ctrlControl byte, see baton_ctrl().payloadPayload bytes, or NULL whenpayload_lengthis zero.payload_lengthNumber of payload bytes (at most BATON_MAX_PAYLOAD).outputDestination buffer for the complete frame.output_capacityCapacity ofoutputin bytes.output_lengthReceives the frame length on success.
Return value:
0Frame encoded.BATON_PARSE_ERRORInvalid input or insufficient output capacity.
function baton_parser_feed¶
Feed one byte to the incremental frame parser.
The parser waits for SYNC, reads the header, rejects a LEN above BATON_MAX_PAYLOAD as soon as it is read, then reads the payload and the CRC and validates it. After a valid or invalid frame it waits for the next SYNC. Bytes of a rejected frame are not re-scanned for SYNC; the inter-byte timeout of the link resynchronizes a broken stream.
Parameters:
parserParser state.byteNext byte from the input stream.
Return value:
1A complete valid frame is available inparser->frameuntil the next call.0More bytes are required.BATON_PARSE_ERRORNULL parser, LEN out of range, CRC mismatch or corrupted state.
function baton_parser_init¶
Reset a parser before starting or restarting a byte stream.
Parameters:
parserParser state to initialize; NULL is ignored.
Macro Definition Documentation¶
define BATON_CRC_INIT¶
Initial value of the CRC-16/CCITT-FALSE register.
define BATON_FRAME_MAX¶
Largest complete frame on the wire, SYNC to CRC.
define BATON_PARSER_IDLE¶
Value of baton_parser::state while the parser waits for SYNC (between frames).
define BATON_PARSE_ERROR¶
Error returned for invalid input, malformed frames, or insufficient capacity.