1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zcbor_decode.h>
8 #include <zcbor_encode.h>
9 #include <zcbor_common.h>
10 #include <stdio.h>
11
main(void)12 void main(void)
13 {
14 uint8_t cbor_payload[15];
15 bool success;
16 struct zcbor_string decoded_string;
17
18 /* Create zcbor state variable for encoding. */
19 ZCBOR_STATE_E(encoding_state, 0, cbor_payload, sizeof(cbor_payload), 0);
20
21 /* Encode a text string into the cbor_payload buffer */
22 success = zcbor_tstr_put_lit(encoding_state, "Hello World");
23
24 if (!success) {
25 printf("Encoding failed: %d\r\n", zcbor_peek_error(encoding_state));
26 return;
27 }
28
29 /* Create zcbor state variable for decoding. */
30 ZCBOR_STATE_D(decoding_state, 0, cbor_payload, sizeof(cbor_payload), 1, 0);
31
32 /* Decode the text string into the cbor_payload buffer */
33 success = zcbor_tstr_decode(decoding_state, &decoded_string);
34
35 if (!success) {
36 printf("Decoding failed: %d\r\n", zcbor_peek_error(decoding_state));
37 return;
38 }
39
40 printf("Decoded string: '%.*s'\r\n", (int)decoded_string.len, decoded_string.value);
41 }
42