Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
COBS (Consistent Overhead Byte Stuffing)

COBS encoding and decoding functions with custom delimiter support. More...

Data Structures

struct  cobs_encoder
 COBS streaming encoder state. More...
struct  cobs_decoder
 COBS streaming decoder state. More...

Typedefs

typedef int(* cobs_stream_cb) (const uint8_t *buf, size_t len, void *user_data)
 Callback function type for streaming COBS encoder/decoder.

Functions

static size_t cobs_max_encoded_len (size_t decoded_size, uint32_t flags)
 Calculate maximum encoded buffer size.
int cobs_encode (struct net_buf *src, struct net_buf *dst, uint32_t flags)
 COBS encoding.
int cobs_decode (struct net_buf *src, struct net_buf *dst, uint32_t flags)
 COBS decoding.
int cobs_encoder_init (struct cobs_encoder *enc, cobs_stream_cb cb, void *user_data, uint32_t flags)
 Initialize COBS streaming encoder.
int cobs_encoder_close (struct cobs_encoder *enc)
 Finalize COBS streaming encoder.
int cobs_encoder_write (struct cobs_encoder *enc, const uint8_t *buf, size_t len)
 Write data to COBS streaming encoder.
int cobs_decoder_init (struct cobs_decoder *dec, cobs_stream_cb cb, void *user_data, uint32_t flags)
 Initialize COBS streaming decoder.
int cobs_decoder_close (struct cobs_decoder *dec)
 Finalize COBS streaming decoder.
int cobs_decoder_write (struct cobs_decoder *dec, const uint8_t *buf, size_t len)
 Write data to COBS streaming decoder.

Detailed Description

COBS encoding and decoding functions with custom delimiter support.

Provides functions for COBS encoding/decoding with configurable delimiters. The implementation handles both standard zero-delimited COBS and custom delimiter variants.

Typedef Documentation

◆ cobs_stream_cb

typedef int(* cobs_stream_cb) (const uint8_t *buf, size_t len, void *user_data)

#include <zephyr/data/cobs.h>

Callback function type for streaming COBS encoder/decoder.

This callback is invoked by the streaming encoder/decoder to output processed data chunks. A decoder that allows trailing delimiters and encounters one will invoke this callback with a NULL pointer and zero length indicating a completed frame.

When this callback function returns a negative error value, the encoder or decoder stream is aborted and the error will be propagated.

Parameters
bufBuffer containing processed data
lenLength of data in buffer
user_dataUser-provided context pointer
Returns
0 on success, negative errno code on failure

Function Documentation

◆ cobs_decode()

int cobs_decode ( struct net_buf * src,
struct net_buf * dst,
uint32_t flags )

#include <zephyr/data/cobs.h>

COBS decoding.

Decodes COBS-encoded data from source buffer to destination buffer.

Parameters
srcSource buffer to decode
dstDestination buffer for decoded data
flagsDecoding flags COBS_FLAGS
Return values
0Success
-ENOMEMInsufficient destination space
-EINVALInvalid COBS structure or parameters

◆ cobs_decoder_close()

int cobs_decoder_close ( struct cobs_decoder * dec)

#include <zephyr/data/cobs.h>

Finalize COBS streaming decoder.

Completes the decoding process and verifies that the stream ended properly. Should be called after all data has been written to the decoder.

The decoder state will be reset.

Parameters
decPointer to decoder structure
Return values
0Success
-EINVALMore data was expected before closing

◆ cobs_decoder_init()

int cobs_decoder_init ( struct cobs_decoder * dec,
cobs_stream_cb cb,
void * user_data,
uint32_t flags )

#include <zephyr/data/cobs.h>

Initialize COBS streaming decoder.

Initializes a COBS decoder for streaming operation. The decoder will call the provided callback function to output decoded data chunks as they become available.

Parameters
decPointer to decoder structure to initialize
cbCallback function for output data
user_dataUser data pointer passed to callback
flagsDecoding flags COBS_FLAGS
Returns
0 on success, negative errno code on failure

◆ cobs_decoder_write()

int cobs_decoder_write ( struct cobs_decoder * dec,
const uint8_t * buf,
size_t len )

#include <zephyr/data/cobs.h>

Write data to COBS streaming decoder.

Decodes the provided encoded data and outputs decoded chunks via the registered callback function. This function can be called multiple times to decode data incrementally.

In case an error is returned, the decoder state will be reset.

Note
If a delimiter is encountered, and the COBS_FLAG_TRAILING_DELIMITER flag is set, the registered callback function will be called with a NULL pointer indicating a frame end.
Parameters
decPointer to decoder structure
bufBuffer containing encoded data
lenLength of data in buffer
Returns
Number of bytes used from buf on success, negative errno code on failure

◆ cobs_encode()

int cobs_encode ( struct net_buf * src,
struct net_buf * dst,
uint32_t flags )

#include <zephyr/data/cobs.h>

COBS encoding.

Encodes data from source buffer to destination buffer using COBS encoding.

Parameters
srcSource buffer to encode
dstDestination buffer for encoded data
flagsEncoding flags COBS_FLAGS
Return values
0Success
-ENOMEMInsufficient destination space
-EINVALInvalid COBS structure or parameters

◆ cobs_encoder_close()

int cobs_encoder_close ( struct cobs_encoder * enc)

#include <zephyr/data/cobs.h>

Finalize COBS streaming encoder.

Flushes any remaining data and optionally writes trailing delimiter if COBS_FLAG_TRAILING_DELIMITER was set during initialization.

The encoder state will be reset.

Parameters
encPointer to encoder structure
Returns
0 on success, negative errno code on failure

◆ cobs_encoder_init()

int cobs_encoder_init ( struct cobs_encoder * enc,
cobs_stream_cb cb,
void * user_data,
uint32_t flags )

#include <zephyr/data/cobs.h>

Initialize COBS streaming encoder.

Initializes a COBS encoder for streaming operation. The encoder will call the provided callback function to output encoded data chunks as they become available.

Parameters
encPointer to encoder structure to initialize
cbCallback function for output data
user_dataUser data pointer passed to callback
flagsEncoding flags COBS_FLAGS
Returns
0 on success, negative errno code on failure

◆ cobs_encoder_write()

int cobs_encoder_write ( struct cobs_encoder * enc,
const uint8_t * buf,
size_t len )

#include <zephyr/data/cobs.h>

Write data to COBS streaming encoder.

Encodes the provided data and outputs encoded chunks via the registered callback function. This function can be called multiple times to encode data incrementally.

In case an error is returned, the encoder state will be reset.

Parameters
encPointer to encoder structure
bufBuffer containing data to encode
lenLength of data in buffer
Returns
Number of bytes used from buf on success, negative errno code on failure

◆ cobs_max_encoded_len()

size_t cobs_max_encoded_len ( size_t decoded_size,
uint32_t flags )
inlinestatic

#include <zephyr/data/cobs.h>

Calculate maximum encoded buffer size.

Parameters
decoded_sizeSize of input data to be encoded
flagsEncoding flags COBS_FLAGS
Returns
Required buffer size for worst-case encoding scenario