Monado OpenXR Runtime
Loading...
Searching...
No Matches
u_cobs.h
Go to the documentation of this file.
1// Copyright 2026, Beyley Cardellio
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Code to decode and encode COBS (Consistent Overhead Byte Stuffing) packet data.
6 * @author Beyley Cardellio <ep1cm1n10n123@gmail.com>
7 * @ingroup aux_util
8 */
9
10#pragma once
11
12#include <xrt/xrt_compiler.h>
13
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19
20// Callback for when packet is finished
21typedef void (*cobs_callback_t)(void *user_data, const uint8_t *data, size_t length);
22
23/*!
24 * A streaming COBS (Consistent Overhead Byte Stuffing) decoder with automatic error recovery.
25 * Prefers to drop incomplete packets instead of attempting to parse them.
26 *
27 * https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
28 *
29 * @ingroup aux_util
30 */
32{
33 cobs_callback_t callback;
34 void *user_data;
35
36 uint8_t *buffer;
37 size_t buffer_size;
38
39 size_t read_bytes;
40 size_t bytes_until_next_code;
41
42 //! Whether we're in error recovery mode (trying to reach a 0x00 delimiter)
44};
45
46/*!
47 * Creates a new COBS decoder.
48 *
49 * @param buffer_size The size of the internal buffer for decoding packets into.
50 * @param callback The callback to fire when a packet is completely received.
51 * @param user_data The user data pointer to pass into the callback.
52 * @param out_decoder A pointer to store the created COBS decoder.
53 * @return 0 on success, -ENOMEM if memory allocation fails.
54 *
55 * @ingroup aux_util
56 */
57int
58u_cobs_decoder_create(size_t buffer_size,
59 cobs_callback_t callback,
60 void *user_data,
61 struct u_cobs_decoder *out_decoder);
62
63/*!
64 * Destroys a COBS decoder previously created by @ref u_cobs_decoder_create
65 *
66 * @param cobs The COBS decoder to destroy.
67 *
68 * @ingroup aux_util
69 */
70void
72
73/*!
74 * Pushes bytes into the COBS decoder. This function will synchronously fire the callback when a packet is completed.
75 *
76 * @param cobs The COBS decoder.
77 * @param data The data to decode.
78 * @param length The length of the data pointer.
79 * @return 0 on success, -EINVAL on invalid packet, -ENOSPC if we run out of space in the packet buffer.
80 *
81 * @ingroup aux_util
82 */
83int
84u_cobs_push_bytes(struct u_cobs_decoder *cobs, const uint8_t *data, size_t length);
85
86/*!
87 * Encodes a single packet using COBS.
88 *
89 * @param input The input data buffer to encode.
90 * @param input_length The length of the input data buffer.
91 * @param output The output data buffer, where the packet is encoded to.
92 * @param output_size The output size of the data buffer.
93 * @return The amount of bytes written to the output buffer. -ENOSPC if we run out of space in the output buffer.
94 *
95 * @ingroup aux_util
96 */
97int
98u_cobs_encode(const uint8_t *input, size_t input_length, uint8_t *output, size_t output_size);
99
100
101#ifdef __cplusplus
102}
103#endif
int u_cobs_encode(const uint8_t *input, size_t input_length, uint8_t *output, size_t output_size)
Encodes a single packet using COBS.
Definition u_cobs.c:123
int u_cobs_push_bytes(struct u_cobs_decoder *cobs, const uint8_t *data, size_t length)
Pushes bytes into the COBS decoder.
Definition u_cobs.c:49
int u_cobs_decoder_create(size_t buffer_size, cobs_callback_t callback, void *user_data, struct u_cobs_decoder *out_decoder)
Creates a new COBS decoder.
Definition u_cobs.c:21
void u_cobs_decoder_destroy(struct u_cobs_decoder *cobs)
Destroys a COBS decoder previously created by u_cobs_decoder_create.
Definition u_cobs.c:41
A streaming COBS (Consistent Overhead Byte Stuffing) decoder with automatic error recovery.
Definition u_cobs.h:32
bool error_recovery
Whether we're in error recovery mode (trying to reach a 0x00 delimiter)
Definition u_cobs.h:43
Header holding common defines.