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 <stdio.h>
10 #include <pet_decode.h>
11 
main(void)12 int main(void)
13 {
14 	uint8_t payload[100] = {0};
15 	int32_t five = 5;
16 	int64_t six = 6;
17 	uint32_t seven = 7;
18 	uint64_t eight = 8;
19 	size_t nine = 9;
20 	uint32_t tag_nine = 9;
21 	bool false_ = false;
22 	float eleven_six = 11.6;
23 	double thirteen_eight = 13.8;
24 	size_t one = 1;
25 	bool one_b = 1;
26 	struct zcbor_string dummy_string;
27 
28 	ZCBOR_STATE_E(state_e, 3, payload, sizeof(payload), 0);
29 	ZCBOR_STATE_D(state_d, 3, payload, sizeof(payload), 30, 0);
30 
31 	state_e->constant_state->stop_on_error = true;
32 	state_d->constant_state->stop_on_error = true;
33 
34 	/* All succeed since the error has been popped. */
35 	zcbor_int32_put(state_e, 1);
36 	zcbor_int64_put(state_e, 2);
37 	zcbor_uint32_put(state_e, 3);
38 	zcbor_uint64_put(state_e, 4);
39 	zcbor_size_put(state_e, 10);
40 	zcbor_int32_encode(state_e, &five);
41 	zcbor_int64_encode(state_e, &six);
42 	zcbor_uint32_encode(state_e, &seven);
43 	zcbor_uint64_encode(state_e, &eight);
44 	zcbor_size_encode(state_e, &nine);
45 	zcbor_bstr_put_lit(state_e, "Hello");
46 	zcbor_tstr_put_lit(state_e, "World");
47 	zcbor_tag_put(state_e, 9);
48 	zcbor_tag_put(state_e, 10);
49 	zcbor_bool_put(state_e, true);
50 	zcbor_bool_encode(state_e, &false_);
51 	zcbor_float32_put(state_e, 10.5);
52 	zcbor_float32_encode(state_e, &eleven_six);
53 	zcbor_float64_put(state_e, 12.7);
54 	zcbor_float64_encode(state_e, &thirteen_eight);
55 	zcbor_nil_put(state_e, NULL);
56 	zcbor_undefined_put(state_e, NULL);
57 	zcbor_bstr_start_encode(state_e);
58 	zcbor_bstr_end_encode(state_e, NULL);
59 	zcbor_list_start_encode(state_e, 1);
60 	zcbor_map_start_encode(state_e, 0);
61 	zcbor_map_end_encode(state_e, 0);
62 	zcbor_list_end_encode(state_e, 1);
63 	zcbor_multi_encode(1, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)14, 0);
64 	bool ret = zcbor_multi_encode_minmax(1, 1, &one, (zcbor_encoder_t *)zcbor_int32_put, state_e, (void*)15, 0);
65 
66 	if (!ret) {
67 		printf("Encode error: %d\r\n", zcbor_peek_error(state_e));
68 		return 1;
69 	}
70 
71 	/* All succeed since the error has been popped. */
72 	zcbor_int32_expect(state_d, 1);
73 	zcbor_int64_expect(state_d, 2);
74 	zcbor_uint32_expect(state_d, 3);
75 	zcbor_uint64_expect(state_d, 4);
76 	zcbor_size_expect(state_d, 10);
77 	zcbor_int32_decode(state_d, &five);
78 	zcbor_int64_decode(state_d, &six);
79 	zcbor_uint32_decode(state_d, &seven);
80 	zcbor_uint64_decode(state_d, &eight);
81 	zcbor_size_decode(state_d, &nine);
82 	zcbor_bstr_expect_lit(state_d, "Hello");
83 	zcbor_tstr_expect_lit(state_d, "World");
84 	zcbor_tag_decode(state_d, &tag_nine);
85 	zcbor_tag_expect(state_d, 10);
86 	zcbor_bool_expect(state_d, true);
87 	zcbor_bool_decode(state_d, &false_);
88 	zcbor_float32_expect(state_d, 10.5);
89 	zcbor_float32_decode(state_d, &eleven_six);
90 	zcbor_float64_expect(state_d, 12.7);
91 	zcbor_float64_decode(state_d, &thirteen_eight);
92 	zcbor_nil_expect(state_d, NULL);
93 	zcbor_undefined_expect(state_d, NULL);
94 	zcbor_bstr_start_decode(state_d, &dummy_string);
95 	zcbor_bstr_end_decode(state_d);
96 	zcbor_list_start_decode(state_d);
97 	zcbor_map_start_decode(state_d);
98 	zcbor_map_end_decode(state_d);
99 	zcbor_list_end_decode(state_d);
100 	zcbor_multi_decode(1, 1, &one, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)14, 0);
101 	ret = zcbor_present_decode(&one_b, (zcbor_decoder_t *)zcbor_int32_expect, state_d, (void*)15);
102 
103 	if (!ret) {
104 		printf("Decode error: %d\r\n", zcbor_peek_error(state_d));
105 		return 1;
106 	}
107 
108 
109 	struct Pet pet;
110 	uint8_t input[] = {
111 		0x83, 0x82, 0x63, 0x66, 0x6f, 0x6f, 0x63, 0x62, 0x61, 0x72,
112 		0x48, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
113 		0x02};
114 	int int_ret = cbor_decode_Pet(input, sizeof(input), &pet, NULL);
115 
116 	if (int_ret != ZCBOR_SUCCESS) {
117 		printf("Decode error: %d\r\n", int_ret);
118 		return 1;
119 	}
120 
121 	printf("Success!\r\n");
122 
123 	return 0;
124 }
125