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