Skip to content

Commit 110661a

Browse files
committed
Add streaming_chunks sample
Introduce a small sample that demonstrates chunked streaming encode/decode for tstr and bstr. The program streams a long name and payload via chunk callbacks, then decodes using chunk-in callbacks.
1 parent 7b562c9 commit 110661a

10 files changed

Lines changed: 725 additions & 0 deletions
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) 2026
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
cmake_minimum_required(VERSION 3.13.3)
6+
project(sample_streaming_chunks)
7+
8+
# Regenerate zcbor-generated files when requested.
9+
if (REGENERATE_ZCBOR)
10+
set(zcbor_command
11+
zcbor code
12+
--decode --encode
13+
--stream-encode
14+
--stream-decode
15+
-c ${CMAKE_CURRENT_LIST_DIR}/streaming_chunks.cddl
16+
-t StreamItem
17+
--output-cmake ${CMAKE_CURRENT_LIST_DIR}/streaming_chunks.cmake
18+
--file-header "Copyright (c) 2026\n\nSPDX-License-Identifier: Apache-2.0"
19+
)
20+
execute_process(COMMAND ${zcbor_command})
21+
endif()
22+
23+
include(${CMAKE_CURRENT_LIST_DIR}/streaming_chunks.cmake)
24+
25+
add_executable(app src/main.c)
26+
27+
# Link the library from streaming_chunks.cmake
28+
target_link_libraries(app PRIVATE streaming_chunks)

samples/streaming_chunks/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Streaming chunks sample
2+
3+
This sample demonstrates streaming encode and decode with chunk callbacks for
4+
both `tstr` and `bstr` fields using zcbor-generated code.
5+
6+
## To build
7+
8+
```sh
9+
cmake -S . -B build -DREGENERATE_ZCBOR=Y
10+
cmake --build build
11+
```
12+
13+
## To run
14+
15+
```sh
16+
build/app
17+
```
18+
19+
## Expected output
20+
21+
```
22+
Decoded name: Chunky Name
23+
Decoded payload: de ad be ef ca fe ba be
24+
```
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2026
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Generated using zcbor version 0.9.1
7+
* https://github.com/NordicSemiconductor/zcbor
8+
* Generated with a --default-max-qty of 3
9+
*/
10+
11+
#ifndef STREAMING_CHUNKS_DECODE_H__
12+
#define STREAMING_CHUNKS_DECODE_H__
13+
14+
#include <stdint.h>
15+
#include <stdbool.h>
16+
#include <stddef.h>
17+
#include <string.h>
18+
#include "zcbor_encode.h"
19+
#include "zcbor_decode.h"
20+
#include "streaming_chunks_types.h"
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
#if ZCBOR_GENERATED_DEFAULT_MAX_QTY != 3
27+
#error "The type file was generated with a different default_max_qty than this file"
28+
#endif
29+
30+
int cbor_decode_StreamItem(
31+
const uint8_t *payload, size_t payload_len,
32+
struct StreamItem *result,
33+
size_t *payload_len_out);
34+
35+
36+
37+
38+
39+
/* Streaming decode helpers */
40+
#ifndef ZCBOR_CHUNK_OUT_DEFINED
41+
#define ZCBOR_CHUNK_OUT_DEFINED
42+
struct zcbor_chunk_out {
43+
void *ctx;
44+
zcbor_stream_chunk_out call;
45+
};
46+
#endif
47+
#ifndef ZCBOR_CHUNK_IN_DEFINED
48+
#define ZCBOR_CHUNK_IN_DEFINED
49+
struct zcbor_chunk_in {
50+
void *ctx;
51+
zcbor_stream_chunk_in call;
52+
};
53+
#endif
54+
55+
#ifndef CBOR_STREAM_IO_STREAMITEM_DEFINED
56+
#define CBOR_STREAM_IO_STREAMITEM_DEFINED
57+
struct cbor_stream_io_StreamItem {
58+
/* Text string fields (chunk_out) */
59+
struct zcbor_chunk_out chunks_out_StreamItem_name;
60+
61+
/* Byte string fields (chunk_out) */
62+
struct zcbor_chunk_out chunks_out_StreamItem_payload;
63+
64+
/* Text string fields (chunk_in) */
65+
struct zcbor_chunk_in chunks_in_StreamItem_name;
66+
67+
/* Byte string fields (chunk_in) */
68+
struct zcbor_chunk_in chunks_in_StreamItem_payload;
69+
};
70+
#endif
71+
typedef struct cbor_stream_io_StreamItem cbor_stream_io_StreamItem;
72+
73+
int cbor_stream_decode_StreamItem(
74+
const uint8_t *payload, size_t payload_len,
75+
struct StreamItem *result,
76+
const cbor_stream_io_StreamItem *io,
77+
size_t *payload_len_out);
78+
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif /* STREAMING_CHUNKS_DECODE_H__ */
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2026
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Generated using zcbor version 0.9.1
7+
* https://github.com/NordicSemiconductor/zcbor
8+
* Generated with a --default-max-qty of 3
9+
*/
10+
11+
#ifndef STREAMING_CHUNKS_ENCODE_H__
12+
#define STREAMING_CHUNKS_ENCODE_H__
13+
14+
#include <stdint.h>
15+
#include <stdbool.h>
16+
#include <stddef.h>
17+
#include <string.h>
18+
#include "zcbor_encode.h"
19+
#include "zcbor_decode.h"
20+
#include "streaming_chunks_types.h"
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
#if ZCBOR_GENERATED_DEFAULT_MAX_QTY != 3
27+
#error "The type file was generated with a different default_max_qty than this file"
28+
#endif
29+
30+
int cbor_encode_StreamItem(
31+
uint8_t *payload, size_t payload_len,
32+
const struct StreamItem *input,
33+
size_t *payload_len_out);
34+
35+
/* Streaming encode helpers */
36+
struct zcbor_stream_iter_io {
37+
void *ctx;
38+
zcbor_stream_iter next;
39+
};
40+
#ifndef ZCBOR_CHUNK_OUT_DEFINED
41+
#define ZCBOR_CHUNK_OUT_DEFINED
42+
struct zcbor_chunk_out {
43+
void *ctx;
44+
zcbor_stream_chunk_out call;
45+
};
46+
#endif
47+
#ifndef ZCBOR_CHUNK_IN_DEFINED
48+
#define ZCBOR_CHUNK_IN_DEFINED
49+
struct zcbor_chunk_in {
50+
void *ctx;
51+
zcbor_stream_chunk_in call;
52+
};
53+
#endif
54+
55+
#ifndef CBOR_STREAM_IO_STREAMITEM_DEFINED
56+
#define CBOR_STREAM_IO_STREAMITEM_DEFINED
57+
struct cbor_stream_io_StreamItem {
58+
/* Repeated fields (iterator) */
59+
/* no repeated iters */
60+
61+
/* Text string fields (chunk_out) */
62+
struct zcbor_chunk_out chunks_out_StreamItem_name;
63+
64+
/* Byte string fields (chunk_out) */
65+
struct zcbor_chunk_out chunks_out_StreamItem_payload;
66+
67+
/* Text string fields (chunk_in) */
68+
struct zcbor_chunk_in chunks_in_StreamItem_name;
69+
70+
/* Byte string fields (chunk_in) */
71+
struct zcbor_chunk_in chunks_in_StreamItem_payload;
72+
};
73+
#endif
74+
typedef struct cbor_stream_io_StreamItem cbor_stream_io_StreamItem;
75+
76+
int cbor_stream_encode_StreamItem(
77+
zcbor_stream_write_fn stream_write, void *stream_user_data,
78+
const struct StreamItem *input,
79+
const cbor_stream_io_StreamItem *io,
80+
size_t *bytes_written_out);
81+
82+
83+
84+
85+
86+
87+
#ifdef __cplusplus
88+
}
89+
#endif
90+
91+
#endif /* STREAMING_CHUNKS_ENCODE_H__ */
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2026
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Generated using zcbor version 0.9.1
7+
* https://github.com/NordicSemiconductor/zcbor
8+
* Generated with a --default-max-qty of 3
9+
*/
10+
11+
#ifndef STREAMING_CHUNKS_TYPES_H__
12+
#define STREAMING_CHUNKS_TYPES_H__
13+
14+
#include <stdint.h>
15+
#include <stdbool.h>
16+
#include <stddef.h>
17+
#include <zcbor_common.h>
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
/** Which value for --default-max-qty this file was created with.
24+
*
25+
* The define is used in the other generated file to do a build-time
26+
* compatibility check.
27+
*
28+
* See `zcbor --help` for more information about --default-max-qty
29+
*/
30+
#define ZCBOR_GENERATED_DEFAULT_MAX_QTY 3
31+
32+
/* Allow build-system override. */
33+
#ifndef DEFAULT_MAX_QTY
34+
#define DEFAULT_MAX_QTY ZCBOR_GENERATED_DEFAULT_MAX_QTY
35+
#endif
36+
37+
/* Allow build-system override for streaming state array size. */
38+
#ifndef ZCBOR_STREAM_STATE_ARRAY_SIZE
39+
#define ZCBOR_STREAM_STATE_ARRAY_SIZE 8
40+
#endif
41+
42+
struct StreamItem {
43+
struct zcbor_string StreamItem_name;
44+
struct zcbor_string StreamItem_payload;
45+
};
46+
47+
#ifdef __cplusplus
48+
}
49+
#endif
50+
51+
#endif /* STREAMING_CHUNKS_TYPES_H__ */

0 commit comments

Comments
 (0)