1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Generated using zcbor version 0.7.0
7 * https://github.com/NordicSemiconductor/zcbor
8 * Generated with a --default-max-qty of 3
9 */
10
11 #include <stdint.h>
12 #include <stdbool.h>
13 #include <stddef.h>
14 #include <string.h>
15 #include "zcbor_encode.h"
16 #include "pet_encode.h"
17
18 #if DEFAULT_MAX_QTY != 3
19 #error "The type file was generated with a different default_max_qty than this file"
20 #endif
21
22 static bool encode_Pet(zcbor_state_t *state, const struct Pet *input);
23
24
encode_Pet(zcbor_state_t * state,const struct Pet * input)25 static bool encode_Pet(
26 zcbor_state_t *state, const struct Pet *input)
27 {
28 zcbor_print("%s\r\n", __func__);
29
30 bool tmp_result = (((zcbor_list_start_encode(state, 3) && ((((zcbor_list_start_encode(state, 3) && ((zcbor_multi_encode_minmax(1, 3, &(*input).names_count, (zcbor_encoder_t *)zcbor_tstr_encode, state, (&(*input).names), sizeof(struct zcbor_string))) || (zcbor_list_map_end_force_encode(state), false)) && zcbor_list_end_encode(state, 3)))
31 && (((((((*input).birthday.len >= 8)
32 && ((*input).birthday.len <= 8)) || (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))) || (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))
33 && (zcbor_bstr_encode(state, (&(*input).birthday))))
34 && ((((*input).species_choice == _Pet_species_cat) ? ((zcbor_uint32_put(state, (1))))
35 : (((*input).species_choice == _Pet_species_dog) ? ((zcbor_uint32_put(state, (2))))
36 : (((*input).species_choice == _Pet_species_other) ? ((zcbor_uint32_put(state, (3))))
37 : false))))) || (zcbor_list_map_end_force_encode(state), false)) && zcbor_list_end_encode(state, 3))));
38
39 if (!tmp_result)
40 zcbor_trace();
41
42 return tmp_result;
43 }
44
45
46
cbor_encode_Pet(uint8_t * payload,size_t payload_len,const struct Pet * input,size_t * payload_len_out)47 int cbor_encode_Pet(
48 uint8_t *payload, size_t payload_len,
49 const struct Pet *input,
50 size_t *payload_len_out)
51 {
52 zcbor_state_t states[4];
53
54 zcbor_new_state(states, sizeof(states) / sizeof(zcbor_state_t), payload, payload_len, 1);
55
56 bool ret = encode_Pet(states, input);
57
58 if (ret && (payload_len_out != NULL)) {
59 *payload_len_out = MIN(payload_len,
60 (size_t)states[0].payload - (size_t)payload);
61 }
62
63 if (!ret) {
64 int err = zcbor_pop_error(states);
65
66 zcbor_print("Return error: %d\r\n", err);
67 return (err == ZCBOR_SUCCESS) ? ZCBOR_ERR_UNKNOWN : err;
68 }
69 return ZCBOR_SUCCESS;
70 }
71