1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zcbor_encode.h>
8 #include <stdio.h>
9 #include <pet_decode.h>
10 #include <pet_encode.h>
11 #include <pet1.h>
12
print_pet(const struct Pet * pet)13 static void print_pet(const struct Pet *pet)
14 {
15 printf("Name:");
16 for (int i = 0; i < pet->names_count; i++) {
17 printf(" %.*s", (int)pet->names[i].len, pet->names[i].value);
18 }
19 printf("\nBirthday: 0x");
20 for (int i = 0; i < pet->birthday.len; i++) {
21 printf("%02x", pet->birthday.value[i]);
22 }
23 switch (pet->species_choice) {
24 case _Pet_species_cat:
25 printf("\nSpecies: Cat\n\n");
26 return;
27 case _Pet_species_dog:
28 printf("\nSpecies: Dog\n\n");
29 return;
30 case _Pet_species_other:
31 printf("\nSpecies: Other\n\n");
32 return;
33 }
34 }
35
36 /** First pet - from var in pet1.h. */
get_pet1(void)37 static void get_pet1(void)
38 {
39 struct Pet decoded_pet;
40 int err;
41
42 err = cbor_decode_Pet(pet1, sizeof(pet1), &decoded_pet, NULL);
43 if (err != ZCBOR_SUCCESS) {
44 printf("Decoding failed for pet1: %d\r\n", err);
45 return;
46 }
47
48 print_pet(&decoded_pet);
49 }
50
51 /** Second pet - encoded with zcbor C library. */
get_pet2(void)52 static void get_pet2(void)
53 {
54 struct Pet decoded_pet;
55 int err;
56 uint8_t pet2[30];
57 ZCBOR_STATE_E(encoding_state, 0, pet2, sizeof(pet2), 0);
58 bool r = true;
59 const uint8_t timestamp2[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
60
61 r = r && zcbor_list_start_encode(encoding_state, 3);
62 r = r && zcbor_list_start_encode(encoding_state, 3);
63 r = r && zcbor_tstr_put_lit(encoding_state, "Danny");
64 r = r && zcbor_tstr_put_lit(encoding_state, "the");
65 r = r && zcbor_tstr_put_lit(encoding_state, "Dog");
66 r = r && zcbor_list_end_encode(encoding_state, 3);
67 r = r && zcbor_bstr_put_arr(encoding_state, timestamp2);
68 r = r && zcbor_uint64_put(encoding_state, 2);
69 r = r && zcbor_list_end_encode(encoding_state, 3);
70
71 if (!r) {
72 printf("Encoding failed for pet2: %d\r\n", zcbor_peek_error(encoding_state));
73 return;
74 }
75
76 err = cbor_decode_Pet(pet2, sizeof(pet2), &decoded_pet, NULL);
77
78 if (err != ZCBOR_SUCCESS) {
79 printf("Decoding failed for pet2: %d\r\n", err);
80 return;
81 }
82
83 print_pet(&decoded_pet);
84 }
85
86 /** Third pet - encoded with zcbor-generated code. */
get_pet3(void)87 static void get_pet3(void)
88 {
89 struct Pet decoded_pet;
90 struct Pet encoded_pet;
91 int err;
92 uint8_t pet3[30];
93 const uint8_t first_name[] = "Gary";
94 const uint8_t last_name[] = "Giraffe";
95 const uint8_t timestamp3[] = { 0x01, 0x02, 0x03, 0x04, 0x0a, 0x0b, 0x0c, 0x0d };
96
97 encoded_pet.names[0].value = first_name;
98 encoded_pet.names[0].len = sizeof(first_name) - 1;
99 encoded_pet.names[1].value = last_name;
100 encoded_pet.names[1].len = sizeof(last_name) - 1;
101 encoded_pet.names_count = 2;
102 encoded_pet.birthday.value = timestamp3;
103 encoded_pet.birthday.len = sizeof(timestamp3);
104 encoded_pet.species_choice = _Pet_species_other;
105
106 err = cbor_encode_Pet(pet3, sizeof(pet3), &encoded_pet, NULL);
107
108 if (err != ZCBOR_SUCCESS) {
109 printf("Encoding failed for pet3: %d\r\n", err);
110 return;
111 }
112
113 err = cbor_decode_Pet(pet3, sizeof(pet3), &decoded_pet, NULL);
114
115 if (err != ZCBOR_SUCCESS) {
116 printf("Decoding failed for pet3: %d\r\n", err);
117 return;
118 }
119
120 print_pet(&decoded_pet);
121 }
122
main(void)123 void main(void)
124 {
125 get_pet1();
126 get_pet2();
127 get_pet3();
128 }
129