-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.c
128 lines (109 loc) · 3.11 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zcbor_encode.h>
#include <stdio.h>
#include <pet_decode.h>
#include <pet_encode.h>
#include <pet1.h>
static void print_pet(const struct Pet *pet)
{
printf("Name:");
for (int i = 0; i < pet->names_count; i++) {
printf(" %.*s", (int)pet->names[i].len, pet->names[i].value);
}
printf("\nBirthday: 0x");
for (int i = 0; i < pet->birthday.len; i++) {
printf("%02x", pet->birthday.value[i]);
}
switch (pet->species_choice) {
case Pet_species_cat_c:
printf("\nSpecies: Cat\n\n");
return;
case Pet_species_dog_c:
printf("\nSpecies: Dog\n\n");
return;
case Pet_species_other_c:
printf("\nSpecies: Other\n\n");
return;
}
}
/** First pet - from var in pet1.h. */
static void get_pet1(void)
{
struct Pet decoded_pet;
int err;
err = cbor_decode_Pet(pet1, sizeof(pet1), &decoded_pet, NULL);
if (err != ZCBOR_SUCCESS) {
printf("Decoding failed for pet1: %d\r\n", err);
return;
}
print_pet(&decoded_pet);
}
/** Second pet - encoded with zcbor C library. */
static void get_pet2(void)
{
struct Pet decoded_pet;
int err;
uint8_t pet2[30];
ZCBOR_STATE_E(encoding_state, 0, pet2, sizeof(pet2), 0);
bool r = true;
const uint8_t timestamp2[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
r = r && zcbor_list_start_encode(encoding_state, 3);
r = r && zcbor_list_start_encode(encoding_state, 3);
r = r && zcbor_tstr_put_lit(encoding_state, "Danny");
r = r && zcbor_tstr_put_lit(encoding_state, "the");
r = r && zcbor_tstr_put_lit(encoding_state, "Dog");
r = r && zcbor_list_end_encode(encoding_state, 3);
r = r && zcbor_bstr_put_arr(encoding_state, timestamp2);
r = r && zcbor_uint64_put(encoding_state, 2);
r = r && zcbor_list_end_encode(encoding_state, 3);
if (!r) {
printf("Encoding failed for pet2: %d\r\n", zcbor_peek_error(encoding_state));
return;
}
err = cbor_decode_Pet(pet2, sizeof(pet2), &decoded_pet, NULL);
if (err != ZCBOR_SUCCESS) {
printf("Decoding failed for pet2: %d\r\n", err);
return;
}
print_pet(&decoded_pet);
}
/** Third pet - encoded with zcbor-generated code. */
static void get_pet3(void)
{
struct Pet decoded_pet;
struct Pet encoded_pet;
int err;
uint8_t pet3[30];
const uint8_t first_name[] = "Gary";
const uint8_t last_name[] = "Giraffe";
const uint8_t timestamp3[] = { 0x01, 0x02, 0x03, 0x04, 0x0a, 0x0b, 0x0c, 0x0d };
encoded_pet.names[0].value = first_name;
encoded_pet.names[0].len = sizeof(first_name) - 1;
encoded_pet.names[1].value = last_name;
encoded_pet.names[1].len = sizeof(last_name) - 1;
encoded_pet.names_count = 2;
encoded_pet.birthday.value = timestamp3;
encoded_pet.birthday.len = sizeof(timestamp3);
encoded_pet.species_choice = Pet_species_other_c;
err = cbor_encode_Pet(pet3, sizeof(pet3), &encoded_pet, NULL);
if (err != ZCBOR_SUCCESS) {
printf("Encoding failed for pet3: %d\r\n", err);
return;
}
err = cbor_decode_Pet(pet3, sizeof(pet3), &decoded_pet, NULL);
if (err != ZCBOR_SUCCESS) {
printf("Decoding failed for pet3: %d\r\n", err);
return;
}
print_pet(&decoded_pet);
}
void main(void)
{
get_pet1();
get_pet2();
get_pet3();
}