• Home
  • History
  • Annotate
Name
Date
Size
#Lines
LOC

..--

.github/18-Mar-2025-443380

include/18-Mar-2025-1,411616

samples/18-Mar-2025-599407

scripts/18-Mar-2025-163119

src/18-Mar-2025-2,7712,006

tests/18-Mar-2025-16,33513,658

zcbor/18-Mar-2025-3,4082,868

zephyr/18-Mar-2025-74

.gitignoreD18-Mar-2025120 109

ARCHITECTURE.mdD18-Mar-202511.8 KiB221159

LICENSED18-Mar-202511.1 KiB203169

MIGRATION_GUIDE.mdD18-Mar-20252.3 KiB4430

README.mdD18-Mar-202538 KiB666524

RELEASE_NOTES.mdD18-Mar-202525.9 KiB527426

__init__.pyD18-Mar-2025223 156

pypi_README.mdD18-Mar-20251.1 KiB2012

pyproject.tomlD18-Mar-20251.3 KiB4739

README.md

1zcbor
2=====
3
4zcbor is a low footprint [CBOR](https://en.wikipedia.org/wiki/CBOR) library in the C language (C++ compatible), tailored for use in microcontrollers.
5It comes with a schema-driven script tool that can validate your data, or even generate code.
6The schema language (CDDL) allows creating very advanced and detailed schemas.
7
8The validation and conversion part of the tool works with YAML and JSON data, in addition to CBOR.
9It can for example validate a YAML file against a schema and convert it into CBOR.
10
11The code generation part of the tool generates C code based on the given schema.
12The generated code performs CBOR encoding and decoding using the C library, while also validating the data against all the rules in the schema.
13
14The schema language used by zcbor is CDDL (Concise Data Definition Language) which is a powerful human-readable data description language defined in [IETF RFC 8610](https://datatracker.ietf.org/doc/rfc8610/).
15
16
17Features
18========
19
20Here are some possible ways zcbor can be used:
21
22 - C code:
23   - As a low-footprint CBOR decoding/encoding library similar to TinyCBOR/QCBOR/NanoCBOR. The library can be used independently of the Python script. ([More information](#cbor-decodingencoding-library))
24   - To generate C code (using the Python script) for validating and decoding or encoding CBOR, for use in optimized or constrained environments, such as microcontrollers. ([More information](#code-generation))
25 - Python script and module ([More information](#python-script-and-module)):
26   - Validate a YAML/JSON file and translate it into CBOR e.g. for transmission.
27   - Validate a YAML/JSON/CBOR file before processing it with some other tool
28   - Decode and validate incoming CBOR data into human-readable YAML/JSON.
29   - As part of a python script that processes YAML/JSON/CBOR files.
30     - Uses the same internal representation used by the PyYAML/json/cbor2 libraries.
31     - Do validation against a CDDL schema.
32     - Create a read-only representation via named tuples (with names taken from the CDDL schema).
33
34
35Getting started
36===============
37
38There are samples in the [samples](samples) directory that demonstrate different ways to use zcbor, both the script tool and the C code.
39
401. The [hello_world sample](samples/hello_world/README.md) is a minimum examples of encoding and decoding using the C library.
412. The [pet sample](samples/pet/README.md) shows a how to use the C library together with generated code, and how to use the script tool to do code generation and data conversion.
42
43The [tests](tests) also demonstrate how to use zcbor in different ways. The [encoding](tests/encode), [decoding](tests/decode), and [unit](tests/unit) tests run using [Zephyr](https://github.com/zephyrproject-rtos/zephyr) (the samples do not use Zephyr).
44
45Should I use code generation or the library directly?
46-----------------------------------------------------
47
48The benefit of using code generation is greater for decoding than encoding.
49This is because decoding is generally more complex than encoding, since when decoding you have to gracefully handle all possible payloads.
50The code generation will provide a number of checks that are tedious to write manually.
51These checks ensure that the payload is well-formed.
52
53
54CBOR decoding/encoding library
55==============================
56
57The CBOR library can be found in [include/](include) and [src/](src) and can be used directly, by including the files in your project.
58If using zcbor with Zephyr, the library will be available when the [CONFIG_ZCBOR](https://docs.zephyrproject.org/latest/kconfig.html#CONFIG_ZCBOR) config is enabled.
59
60The library is also used by generated code. See the [Code generation](#code-generation) section for more info about code generation.
61
62The C library is C++ compatible.
63
64The zcbor state object
65----------------------
66
67To do encoding or decoding with the library, instantiate a `zcbor_state_t` object, which is most easily done using the `ZCBOR_STATE_*()` macros, look below or in the [hello_world](samples/hello_world/src/main.c) sample for example code.
68
69The `elem_count` member refers to the number of encoded objects in the current list or map.
70`elem_count` starts again when entering a nested list or map, and is restored when exiting.
71
72`elem_count` is one reason for needing "backup" states (the other is to allow rollback of the payload).
73Backups are needed for _decoding_ if there are any lists, maps, or CBOR-encoded strings (`zcbor_bstr_*_decode`) in the data.
74Backups are needed for _encoding_ if there are any lists or maps *and* you are using canonical encoding (`ZCBOR_CANONICAL`), or when using the `zcbor_bstr_*_encode` functions.
75
76`n_flags` is used when decoding maps where the order is unknown.
77It allows using the `zcbor_unordered_map_search()` function to search for elements.
78
79See the header files for more information.
80
81```c
82/** Initialize a decoding state (could include an array of backup states).
83 *  After calling this, decode_state[0] is ready to be used with the decoding APIs. */
84ZCBOR_STATE_D(decode_state, n, payload, payload_len, elem_count, n_flags);
85
86/** Initialize an encoding state (could include an array of backup states).
87 *  After calling this, encode_state[0] is ready to be used with the encoding APIs. */
88ZCBOR_STATE_E(encode_state, n, payload, payload_len, 0);
89```
90
91Configuration
92-------------
93
94The C library has a few compile-time configuration options.
95These configuration options can be enabled by adding them as compile definitions to the build.
96If using zcbor with Zephyr, use the [Kconfig options](https://github.com/zephyrproject-rtos/zephyr/blob/main/modules/zcbor/Kconfig) instead.
97
98Name                      | Description
99------------------------- | -----------
100`ZCBOR_CANONICAL`         | Assume canonical encoding (AKA "deterministically encoded CBOR", ch 4.2.1 in RFC8949). When encoding lists and maps, do not use indefinite length encoding. Enabling `ZCBOR_CANONICAL` increases code size and makes the encoding library more often use state backups. When decoding, if `enforce_canonical` is true, ensure that the incoming data conforms to canonical encoding, i.e. no indefinite length encoding, and always using minimal length encoding (e.g. not using 16 bits to encode a value < 256). Enabling `ZCBOR_CANONICAL` changes the default of `enforce_canonical` from `false` to `true` Note: the map ordering constraint in canonical encoding is not checked.
101`ZCBOR_VERBOSE`           | Print log messages on encoding/decoding errors (`zcbor_log()`), and also a trace message (`zcbor_trace()`) for each decoded value, and in each generated function (when using code generation).
102`ZCBOR_ASSERTS`           | Enable asserts (`zcbor_assert()`). When they fail, the assert statements instruct the current function to return a `ZCBOR_ERR_ASSERTION` error. If `ZCBOR_VERBOSE` is enabled, a message is printed.
103`ZCBOR_STOP_ON_ERROR`     | Enable the `stop_on_error` functionality. Note that it also has to be enabled in the state variable (`state->constant_state->stop_on_error`). This makes all zcbor functions abort their execution if called when an error has already happened.
104`ZCBOR_BIG_ENDIAN`        | All decoded values are returned as big-endian. The default is little-endian.
105`ZCBOR_MAP_SMART_SEARCH`  | Applies to decoding of unordered maps. When enabled, a flag is kept for each element in an array, ensuring it is not processed twice. If disabled, a count is kept for map as a whole. Enabling increases code size and memory usage, and requires the state variable to possess the memory necessary for the flags.
106
107
108Python script and module
109========================
110
111The zcbor.py script can directly read CBOR, YAML, or JSON data and validate it against a CDDL description.
112It can also freely convert the data between CBOR/YAML/JSON.
113It can also output the data to a C file formatted as a byte array.
114
115Invoking zcbor.py from the command line
116---------------------------------------
117
118zcbor.py can be installed via [`pip`](https://pypi.org/project/zcbor/), or alternatively invoked directly from its location in this repo.
119
120Following are some generalized examples for validating, and for converting (which also validates) data from the command line.
121The script infers the data format from the file extension, but the format can also be specified explicitly.
122See `zcbor validate --help` and `zcbor convert --help` for more information.
123
124```sh
125zcbor validate -c <CDDL description file> -t <which CDDL type to expect> -i <input data file>
126zcbor convert -c <CDDL description file> -t <which CDDL type to expect> -i <input data file> -o <output data file>
127```
128
129Or directly from within the repo.
130
131```sh
132python3 <zcbor base>/zcbor/zcbor.py validate -c <CDDL description file> -t <which CDDL type to expect> -i <input data file>
133python3 <zcbor base>/zcbor/zcbor.py convert -c <CDDL description file> -t <which CDDL type to expect> -i <input data file> -o <output data file>
134```
135
136Importing zcbor in a Python script
137----------------------------------
138
139Importing zcbor gives access to the DataTranslator class which is used to implement the command line conversion features.
140DataTranslator can be used to programmatically perform the translations, or to manipulate the data.
141When accessing the data, you can choose between two internal formats:
142
143 1. The format provided by the [cbor2](https://pypi.org/project/cbor2/), [yaml (PyYAML)](https://pypi.org/project/PyYAML/), and [json](https://docs.python.org/3/library/json.html) packages.
144    This is a format where the serialization types (map, list, string, number etc.) are mapped directly to the corresponding Python types.
145    This format is common between these packages, which makes translation very simple.
146    When returning this format, DataTranslator hides the idiomatic representations for bytestrings, tags, and non-text keys described above.
147 2. A custom format which allows accessing the data via the names from the CDDL description file.
148    This format is implemented using named tuples, and is immutable, meaning that it can be used for inspecting data, but not for changing or creating data.
149
150Making CBOR YAML-/JSON-compatible
151---------------------------------
152
153Since CBOR supports more data types than YAML and JSON, zcbor can optionally use a bespoke format when converting to/from YAML/JSON.
154This is controlled with the `--yaml-compatibility` option to `convert` and `validate`.
155This is relevant when handling YAML/JSON conversions of data that uses the unsupported features.
156The following data types are supported by CBOR, but not by YAML (or JSON which is a subset of YAML):
157
158 1. bytestrings: YAML supports only text strings. In YAML, bytestrings are represented as `{"zcbor_bstr": "<hex-formatted bytestring>"}`, or as `{"zcbor_bstr": <any type>}` if the CBOR bytestring contains CBOR-formatted data, in which the data is decoded into `<any type>`.
159 2. map keys other than text string: In YAML, such key value pairs are represented as `{"zcbor_keyval<unique int>": {"key": <key, not text>, "val": <value>}}`.
160 3. tags: In cbor2, tags are represented by a special type, `cbor2.CBORTag`. In YAML, these are represented as `{"zcbor_tag": <tag number>, "zcbor_tag_val": <tagged data>}`.
161 4. undefined: In cbor2, undefined has its own value `cbor2.types.undefined`. In YAML, undefined is represented as: `["zcbor_undefined"]`.
162
163You can see an example of the conversions in [tests/cases/yaml_compatibility.yaml](tests/cases/yaml_compatibility.yaml) and its CDDL file [tests/cases/yaml_compatibility.cddl](tests/cases/yaml_compatibility.cddl).
164
165
166Code generation
167===============
168
169Code generation is invoked with the `zcbor code` command:
170
171```sh
172zcbor code <--decode or --encode or both> -c <CDDL description file(s)> -t <which CDDL type(s) to expose in the API> --output-cmake <path to place the generated CMake file at>
173zcbor code <--decode or --encode or both> -c <CDDL description file(s)> -t <which CDDL type(s) to expose in the API> --oc <path to the generated C file> --oh <path to the generated header file> --oht <path to the generated types header>
174```
175
176When you call this, zcbor reads the CDDL files and creates C struct types to match the types described in the CDDL.
177It then creates code that uses the C library to decode CBOR data into the structs, and/or encode CBOR from the data in the structs.
178Finally, it takes the "entry types" (`-t`) and creates a public API function for each of them.
179While doing these things, it will make a number of optimizations, e.g. inlining code for small types and removing unused functions.
180It outputs the generated code into header and source files and optionally creates a CMake file to build them.
181
182The `zcbor code` command reads one or more CDDL file(s) and generates some or all of these files:
183 - A header file with types (always)
184 - A header file with declarations for decoding functions (if `--decode`/`-d` is specified)
185 - A C file with decoding functions (if `--decode`/`-d` is specified)
186 - A header file with declarations for encoding functions (if `--encode`/`-e` is specified)
187 - A C file with encoding functions (if `--encode`/`-e` is specified)
188 - A CMake file that creates a library with the generated code and the C library (if `--output-cmake` is specified).
189
190CDDL allows placing restrictions on the members of your data.
191Restrictions can be on type (int/string/list/bool etc.), on content (e.g. values/sizes of ints or strings), and repetition (e.g. the number of members in a list).
192The generated code will validate the input, which means that it will check all the restriction set in the CDDL description, and fail if a restriction is broken.
193
194There are tests for the code generation in [tests/decode](tests/decode) and [tests/encode](tests/encode).
195The tests require [Zephyr](https://github.com/zephyrproject-rtos/zephyr) (if your system is set up to build Zephyr samples, the tests should also build).
196
197The generated C code is C++ compatible.
198
199Build system
200------------
201
202When calling zcbor with the argument `--output-cmake <file path>`, a CMake file will be created at that location.
203The generated CMake file creates a target library and adds the generated and non-generated source files as well as required include directories to it.
204This CMake file can then be included in your project's `CMakeLists.txt` file, and the target can be linked into your project.
205This is demonstrated in the tests, e.g. at [tests/decode/test3_simple/CMakeLists.txt](tests/decode/test3_simple/CMakeLists.txt).
206zcbor can be instructed to copy the non-generated sources to the same location as the generated sources with `--copy-sources`.
207
208
209Usage Example
210=============
211
212There are buildable examples in the [samples](samples) directory.
213
214To see how to use the C library directly, see the [hello_world](samples/hello_world/src/main.c) sample, or the [pet](samples/pet/src/main.c) sample (look for calls to functions prefixed with `zcbor_`).
215
216To see how to use code generation, see the [pet](samples/pet/src/main.c) sample.
217
218Look at the [CMakeLists.txt](samples/pet/CMakeLists.txt) file to see how zcbor is invoked for code generation (and for conversion).
219
220To see how to do conversion, see the [pet](samples/pet/CMakeLists.txt) sample.
221
222Below are some additional examples of how to invoke zcbor for code generation and for converting/validating
223
224Code generation
225---------------
226
227```sh
228python3 <zcbor base>/zcbor/zcbor.py code -c pet.cddl -d -t Pet --oc pet_decode.c --oh pet_decode.h
229# or
230zcbor code -c pet.cddl -d -t Pet --oc pet_decode.c --oh pet_decode.h
231```
232
233Converting
234----------
235
236Here is an example call for converting from YAML to CBOR:
237
238```sh
239python3 <zcbor base>/zcbor/zcbor.py convert -c pet.cddl -t Pet -i mypet.yaml -o mypet.cbor
240# or
241zcbor convert -c pet.cddl -t Pet -i mypet.yaml -o mypet.cbor
242```
243
244Which takes a yaml structure from mypet.yaml, validates it against the Pet type in the CDDL description in pet.cddl, and writes binary CBOR data to mypet.cbor.
245
246Validating
247----------
248
249Here is an example call for validating a JSON file:
250
251```sh
252python3 <zcbor base>/zcbor/zcbor.py validate -c pet.cddl -t Pet --yaml-compatibility -i mypet.json
253# or
254zcbor validate -c pet.cddl -t Pet --yaml-compatibility -i mypet.json
255```
256
257Which takes the json structure in mypet.json, converts any [yaml-compatible](#making-cbor-yaml-json-compatible) values to their original form, and validates that against the Pet type in the CDDL description in pet.cddl.
258
259
260Running tests
261=============
262
263The tests for the generated code are based on the Zephyr ztest library.
264These tests can be found in [tests/decode](tests/decode) and [tests/encode](tests/encode).
265To set up the environment to run the ztest tests, follow [Zephyr's Getting Started Guide](https://docs.zephyrproject.org/latest/getting_started/index.html), or see the workflow in the [`.github`](.github) directory.
266
267Tests for `convert` and `verify` are implemented with the unittest module.
268These tests can be found in [tests/scripts/test_zcbor.py](tests/scripts/test_zcbor.py).
269In this file there are also tests for code style of all python scripts, using the `pycodestyle` library.
270
271Tests for the docs, samples, etc. can be found in [tests/scripts/test_repo_files.py](tests/scripts/test_repo_files.py).
272
273For running the tests locally, there is [`tests/test.sh`](tests/test.sh) which runs all above tests.
274
275
276Introduction to CDDL
277====================
278
279In CDDL you define types from other types.
280Types can be defined from base types, or from other types you define.
281Types are declared with '`=`', e.g. `Foo = int` which declares the type `Foo` to be an integer, analogous to `typedef int Foo;` in C.
282CDDL defines the following base types (this is not an exhaustive list):
283
284 - `int`: Positive or negative integer
285 - `uint`: Positive integer
286 - `bstr`: Byte string
287 - `tstr`: Text string
288 - `bool`: Boolean
289 - `nil`: Nil/Null value
290 - `float`: Floating point value
291 - `any`: Any single element
292
293CDDL allows creating aggregate types:
294
295 - `[]`: List. Elements don't need to have the same type.
296 - `{}`: Map. Key/value pairs as are declared as `<key> => <value>` or `<key>: <value>`. Note that `:` is also used for labels.
297 - `()`: Groups. Grouping with no enclosing type, which means that e.g. `Foo = [(int, bstr)]` is equivalent to `Foo = [int, bstr]`.
298 - `/`: Unions. Analogous to unions in C. E.g. `Foo = int/bstr/Bar` where Foo is either an int, a bstr, or Bar (some custom type).
299
300Literals can be used instead of the base type names:
301
302 - Number: `Foo = 3`, where Foo is a uint with the additional requirement that it must have the value 3.
303 - Number range: `Foo = -100..100`, where Foo is an int with value between -100 and 100.
304 - Text string: `Foo = "hello"`, where Foo is a tstr with the requirement that it must be "hello".
305 - True/False: `Foo = false`, where Foo is a bool which is always false.
306
307Base types can also be restricted in other ways:
308
309 - `.size`: Works for integers and strings. E.g. `Foo = uint .size 4` where Foo is a uint exactly 4 bytes long.
310 - `.cbor`/`.cborseq`: E.g. `Foo = bstr .cbor Bar` where Foo is a bstr whose contents must be CBOR data decodable as the Bar type.
311
312An element can be repeated:
313
314 - `?`: 0 or 1 time. E.g. `Foo = [int, ?bstr]`, where Foo is a list with an int possibly followed by a bstr.
315 - `*`: 0 or more times. E.g. `Foo = [*tstr]`, where Foo is a list containing 0 or more tstrs.
316 - `+`: 1 or more times. E.g. `Foo = [+Bar]`.
317 - `x*y`: Between x and y times, inclusive. E.g. `Foo = {4*8(int => bstr)}` where Foo is a map with 4 to 8 key/value pairs where each key is an int and each value is a bstr.
318
319Note that in the zcbor script and its generated code, the number of entries supported via `*` and `+` is affected by the default_max_qty value.
320
321Any element can be labeled with `:`.
322The label is only for readability and does not impact the data structure in any way.
323E.g. `Foo = [name: tstr, age: uint]` is equivalent to `Foo = [tstr, uint]`.
324
325See [pet.cddl](tests/cases/pet.cddl) for CDDL example code.
326
327Unsupported CDDL features
328-------------------------
329
330Not all features outlined in the CDDL specs [RFC8610](https://datatracker.ietf.org/doc/html/rfc8610), [RFC9090](https://datatracker.ietf.org/doc/html/rfc9090), and [RFC9165](https://datatracker.ietf.org/doc/html/rfc9165) are supported by zcbor.
331The following is a list of limitations and missing features:
332
333 * Generated code does not support unordered maps.
334 * Using `&()` to turn groups into choices (unions). `&()` is supported when used with `.bits`.
335 * Representation Types (`#x.y`), except for tags (`#6.y(foo)`) which are supported.
336 * Unwrapping (`~`)
337 * The control operators `.regexp`, `.ne`, `.default`, and `.within` from RFC8610.
338 * The control operators `.sdnv`, `.sdnvseq`, and `.oid` from RFC9090.
339 * The control operators `.plus`, `.cat`, `.det`, `.abnf`, `.abnfb`, and `.feature` from RFC9165.
340 * Generics (`foo<a, b>`).
341 * Using `:` for map keys.
342 * Cuts, either via `^` or implicitly via `:`.
343 * Most of the "Extended Diagnostic Notation" is unsupported.
344
345
346Introduction to CBOR
347====================
348
349CBOR's format is described well on [Wikipedia](https://en.wikipedia.org/wiki/CBOR), but here's a synopsis:
350
351Encoded CBOR data elements look like this.
352
353```
354| Header                       | Value                  | Payload                   |
355| 1 byte                       | 0, 1, 2, 4, or 8 bytes | 0 - 2^64-1 bytes/elements |
356| 3 bits     | 5 bits          |
357| Major Type | Additional Info |
358```
359
360The available major types can be seen in `zcbor_major_type_t`.
361
362For all major types, Values 0-23 are encoded directly in the _Additional info_, meaning that the _Value_ field is 0 bytes long.
363If _Additional info_ is 24, 25, 26, or 27, the _Value_ field is 1, 2, 4, or 8 bytes long, respectively.
364
365Major types `pint` (0), `nint` (1), `tag` (6), and `simple` (7) elements have no payload, only _Value_.
366
367 * `pint`: Interpret the _Value_ as a positive integer.
368 * `nint`: Interpret the _Value_ as a positive integer, then multiply by -1 and subtract 1.
369 * `tag`: The _Value_ says something about the next non-tag element.
370   See the [CBOR tag documentation](https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml) for details.
371 * `simple`: Different _Additional info_ mean different things:
372    * 0-19: Unassigned simple values.
373    * 20: `false` simple value
374    * 21: `true` simple value
375    * 22: `null` simple value
376    * 23: `undefined` simple value
377    * 24: Interpret the _Value_ as a 1 byte simple value. These simple values are currently unassigned.
378    * 25: Interpret the _Value_ as an IEEE 754 float16.
379    * 26: Interpret the _Value_ as an IEEE 754 float32.
380    * 27: Interpret the _Value_ as an IEEE 754 float64.
381    * 31: End of an indefinite-length `list` or `map`.
382
383For `bstr` (2), `tstr` (3), `list` (4), and `map` (5), the _Value_ describes the length of the _Payload_.
384For `bstr` and `tstr`, the length is in bytes, for `list`, the length is in number of elements, and for `map`, the length is in number of key/value element pairs.
385
386For `list` and `map`, sub elements are regular CBOR elements with their own _Header_, _Value_ and _Payload_. `list`s and `map`s can be recursively encoded.
387If a `list` or `map` has _Additional info_ 31, it is "indefinite-length", which means it has an "unknown" number of elements.
388Instead, its end is marked by a `simple` with _Additional info_ 31 (byte value 0xFF).
389
390
391History
392=======
393
394zcbor (then "cddl-gen") was initially conceived as a code generation project.
395It was inspired by the need to securely decode the complex manifest data structures in the [IETF SUIT specification](https://datatracker.ietf.org/doc/draft-ietf-suit-manifest/).
396This is reflected in the fact that there are multiple zcbor tests that use the CDDL and examples from various revisions of that specification.
397Decoding/deserializing data securely requires doing some quite repetitive checks on each data element, to be sure that you are not decoding gibberish.
398This is where code generation could pull a lot of weight.
399Later it was discovered that the CBOR library that was designed to used by generated code could be useful by itself.
400The script was also expanded so it could directly manipulate CBOR data.
401Since CBOR, YAML, and JSON are all represented in roughly the same way internally in Python, it was easy to expand that data manipulation to support YAML and JSON.
402
403Some places where zcbor is currently used:
404- [MCUboot's serial recovery mechanism](https://github.com/mcu-tools/mcuboot/blob/main/boot/boot_serial/src/boot_serial.c)
405- [Zephyr's mcumgr](https://github.com/zephyrproject-rtos/zephyr/blob/main/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c)
406- [Zephyr's LwM2M SenML](https://github.com/zephyrproject-rtos/zephyr/blob/main/subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c)
407- [nRF Connect SDK's full modem update mechanism](https://github.com/nrfconnect/sdk-nrf/blob/main/subsys/mgmt/fmfu/src/fmfu_mgmt.c)
408- [nRF Connect SDK's nrf_rpc](https://github.com/nrfconnect/sdk-nrfxlib/blob/main/nrf_rpc/nrf_rpc_cbor.c)
409
410
411Command line documentation
412==========================
413
414Added via `add_helptext.py`
415
416zcbor --help
417------------
418
419```
420usage: zcbor [-h] [--version] {code,validate,convert} ...
421
422Parse a CDDL file and validate/convert between YAML, JSON, and CBOR. Can also
423generate C code for validation/encoding/decoding of CBOR.
424
425positional arguments:
426  {code,validate,convert}
427
428options:
429  -h, --help            show this help message and exit
430  --version             show program's version number and exit
431
432```
433
434zcbor code --help
435-----------------
436
437```
438usage: zcbor code [-h] -c CDDL [--no-prelude] [-v]
439                  [--default-max-qty DEFAULT_MAX_QTY] [--output-c OUTPUT_C]
440                  [--output-h OUTPUT_H] [--output-h-types OUTPUT_H_TYPES]
441                  [--copy-sources] [--output-cmake OUTPUT_CMAKE] -t
442                  ENTRY_TYPES [ENTRY_TYPES ...] [-d] [-e] [--time-header]
443                  [--git-sha-header] [-b {32,64}]
444                  [--include-prefix INCLUDE_PREFIX] [-s]
445                  [--file-header FILE_HEADER]
446
447Parse a CDDL file and produce C code that validates and xcodes CBOR.
448The output from this script is a C file and a header file. The header file
449contains typedefs for all the types specified in the cddl input file, as well
450as declarations to xcode functions for the types designated as entry types when
451running the script. The c file contains all the code for decoding and validating
452the types in the CDDL input file. All types are validated as they are xcoded.
453
454Where a `bstr .cbor <Type>` is specified in the CDDL, AND the Type is an entry
455type, the xcoder will not xcode the string, only provide a pointer into the
456payload buffer. This is useful to reduce the size of typedefs, or to break up
457decoding. Using this mechanism is necessary when the CDDL contains self-
458referencing types, since the C type cannot be self referencing.
459
460This script requires 'regex' for lookaround functionality not present in 're'.
461
462options:
463  -h, --help            show this help message and exit
464  -c CDDL, --cddl CDDL  Path to one or more input CDDL file(s). Passing
465                        multiple files is equivalent to concatenating them.
466  --no-prelude          Exclude the standard CDDL prelude from the build. The
467                        prelude can be viewed at zcbor/prelude.cddl in the
468                        repo, or together with the script.
469  -v, --verbose         Print more information while parsing CDDL and
470                        generating code.
471  --default-max-qty DEFAULT_MAX_QTY, --dq DEFAULT_MAX_QTY
472                        Default maximum number of repetitions when no maximum
473                        is specified. This is needed to construct complete C
474                        types. The default_max_qty can usually be set to a
475                        text symbol if desired, to allow it to be configurable
476                        when building the code. This is not always possible,
477                        as sometimes the value is needed for internal
478                        computations. If so, the script will raise an
479                        exception.
480  --output-c OUTPUT_C, --oc OUTPUT_C
481                        Path to output C file. If both --decode and --encode
482                        are specified, _decode and _encode will be appended to
483                        the filename when creating the two files. If not
484                        specified, the path and name will be based on the
485                        --output-cmake file. A 'src' directory will be created
486                        next to the cmake file, and the C file will be placed
487                        there with the same name (except the file extension)
488                        as the cmake file.
489  --output-h OUTPUT_H, --oh OUTPUT_H
490                        Path to output header file. If both --decode and
491                        --encode are specified, _decode and _encode will be
492                        appended to the filename when creating the two files.
493                        If not specified, the path and name will be based on
494                        the --output-cmake file. An 'include' directory will
495                        be created next to the cmake file, and the C file will
496                        be placed there with the same name (except the file
497                        extension) as the cmake file.
498  --output-h-types OUTPUT_H_TYPES, --oht OUTPUT_H_TYPES
499                        Path to output header file with typedefs (shared
500                        between decode and encode). If not specified, the path
501                        and name will be taken from the output header file
502                        (--output-h), with '_types' added to the file name.
503  --copy-sources        Copy the non-generated source files (zcbor_*.c/h) into
504                        the same directories as the generated files.
505  --output-cmake OUTPUT_CMAKE
506                        Path to output CMake file. The filename of the CMake
507                        file without '.cmake' is used as the name of the CMake
508                        target in the file. The CMake file defines a CMake
509                        target with the zcbor source files and the generated
510                        file as sources, and the zcbor header files' and
511                        generated header files' folders as
512                        include_directories. Add it to your project via
513                        include() in your CMakeLists.txt file, and link the
514                        target to your program. This option works with or
515                        without the --copy-sources option.
516  -t ENTRY_TYPES [ENTRY_TYPES ...], --entry-types ENTRY_TYPES [ENTRY_TYPES ...]
517                        Names of the types which should have their xcode
518                        functions exposed.
519  -d, --decode          Generate decoding code. Either --decode or --encode or
520                        both must be specified.
521  -e, --encode          Generate encoding code. Either --decode or --encode or
522                        both must be specified.
523  --time-header         Put the current time in a comment in the generated
524                        files.
525  --git-sha-header      Put the current git sha of zcbor in a comment in the
526                        generated files.
527  -b {32,64}, --default-bit-size {32,64}
528                        Default bit size of integers in code. When integers
529                        have no explicit bounds, assume they have this bit
530                        width. Should follow the bit width of the architecture
531                        the code will be running on.
532  --include-prefix INCLUDE_PREFIX
533                        When #include'ing generated files, add this path
534                        prefix to the filename.
535  -s, --short-names     Attempt to make most generated struct member names
536                        shorter. This might make some names identical which
537                        will cause a compile error. If so, tweak the CDDL
538                        labels or layout, or disable this option. This might
539                        also make enum names different from the corresponding
540                        union members.
541  --file-header FILE_HEADER
542                        Header to be included in the comment at the top of
543                        generated files, e.g. copyright. Can be a string or a
544                        path to a file. If interpreted as a path to an
545                        existing file, the file's contents will be used.
546
547```
548
549zcbor validate --help
550---------------------
551
552```
553usage: zcbor validate [-h] -c CDDL [--no-prelude] [-v] -i INPUT
554                      [--input-as {yaml,json,cbor,cborhex}] -t ENTRY_TYPE
555                      [--default-max-qty DEFAULT_MAX_QTY]
556                      [--yaml-compatibility]
557
558Read CBOR, YAML, or JSON data from file or stdin and validate it against a
559CDDL schema file.
560
561options:
562  -h, --help            show this help message and exit
563  -c CDDL, --cddl CDDL  Path to one or more input CDDL file(s). Passing
564                        multiple files is equivalent to concatenating them.
565  --no-prelude          Exclude the standard CDDL prelude from the build. The
566                        prelude can be viewed at zcbor/prelude.cddl in the
567                        repo, or together with the script.
568  -v, --verbose         Print more information while parsing CDDL and
569                        generating code.
570  -i INPUT, --input INPUT
571                        Input data file. The option --input-as specifies how
572                        to interpret the contents. Use "-" to indicate stdin.
573  --input-as {yaml,json,cbor,cborhex}
574                        Which format to interpret the input file as. If
575                        omitted, the format is inferred from the file name.
576                        .yaml, .yml => YAML, .json => JSON, .cborhex => CBOR
577                        as hex string, everything else => CBOR
578  -t ENTRY_TYPE, --entry-type ENTRY_TYPE
579                        Name of the type (from the CDDL) to interpret the data
580                        as.
581  --default-max-qty DEFAULT_MAX_QTY, --dq DEFAULT_MAX_QTY
582                        Default maximum number of repetitions when no maximum
583                        is specified. It is only relevant when handling data
584                        that will be decoded by generated code. If omitted, a
585                        large number will be used.
586  --yaml-compatibility  Whether to convert CBOR-only values to YAML-compatible
587                        ones (when converting from CBOR), or vice versa (when
588                        converting to CBOR). When this is enabled, all CBOR
589                        data is guaranteed to convert into YAML/JSON. JSON and
590                        YAML do not support all data types that CBOR/CDDL
591                        supports. bytestrings (BSTR), tags, undefined, and
592                        maps with non-text keys need special handling. See the
593                        zcbor README for more information.
594
595```
596
597zcbor convert --help
598--------------------
599
600```
601usage: zcbor convert [-h] -c CDDL [--no-prelude] [-v] -i INPUT
602                     [--input-as {yaml,json,cbor,cborhex}] -t ENTRY_TYPE
603                     [--default-max-qty DEFAULT_MAX_QTY]
604                     [--yaml-compatibility] -o OUTPUT
605                     [--output-as {yaml,json,cbor,cborhex,c_code}]
606                     [--c-code-var-name C_CODE_VAR_NAME]
607                     [--c-code-columns C_CODE_COLUMNS]
608
609Parse a CDDL file and validate/convert between CBOR and YAML/JSON. The script
610decodes the CBOR/YAML/JSON data from a file or stdin and verifies that it
611conforms to the CDDL description. The script fails if the data does not
612conform. 'zcbor validate' can be used if only validate is needed.
613
614options:
615  -h, --help            show this help message and exit
616  -c CDDL, --cddl CDDL  Path to one or more input CDDL file(s). Passing
617                        multiple files is equivalent to concatenating them.
618  --no-prelude          Exclude the standard CDDL prelude from the build. The
619                        prelude can be viewed at zcbor/prelude.cddl in the
620                        repo, or together with the script.
621  -v, --verbose         Print more information while parsing CDDL and
622                        generating code.
623  -i INPUT, --input INPUT
624                        Input data file. The option --input-as specifies how
625                        to interpret the contents. Use "-" to indicate stdin.
626  --input-as {yaml,json,cbor,cborhex}
627                        Which format to interpret the input file as. If
628                        omitted, the format is inferred from the file name.
629                        .yaml, .yml => YAML, .json => JSON, .cborhex => CBOR
630                        as hex string, everything else => CBOR
631  -t ENTRY_TYPE, --entry-type ENTRY_TYPE
632                        Name of the type (from the CDDL) to interpret the data
633                        as.
634  --default-max-qty DEFAULT_MAX_QTY, --dq DEFAULT_MAX_QTY
635                        Default maximum number of repetitions when no maximum
636                        is specified. It is only relevant when handling data
637                        that will be decoded by generated code. If omitted, a
638                        large number will be used.
639  --yaml-compatibility  Whether to convert CBOR-only values to YAML-compatible
640                        ones (when converting from CBOR), or vice versa (when
641                        converting to CBOR). When this is enabled, all CBOR
642                        data is guaranteed to convert into YAML/JSON. JSON and
643                        YAML do not support all data types that CBOR/CDDL
644                        supports. bytestrings (BSTR), tags, undefined, and
645                        maps with non-text keys need special handling. See the
646                        zcbor README for more information.
647  -o OUTPUT, --output OUTPUT
648                        Output data file. The option --output-as specifies how
649                        to interpret the contents. Use "-" to indicate stdout.
650  --output-as {yaml,json,cbor,cborhex,c_code}
651                        Which format to interpret the output file as. If
652                        omitted, the format is inferred from the file name.
653                        .yaml, .yml => YAML, .json => JSON, .c, .h => C code,
654                        .cborhex => CBOR as hex string, everything else =>
655                        CBOR
656  --c-code-var-name C_CODE_VAR_NAME
657                        Only relevant together with '--output-as c_code' or .c
658                        files.
659  --c-code-columns C_CODE_COLUMNS
660                        Only relevant together with '--output-as c_code' or .c
661                        files. The number of bytes per line in the variable
662                        instantiation. If omitted, the entire declaration is a
663                        single line.
664
665```
666