1 /*
2 * Copyright (c) 2024 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Generated using zcbor version 0.9.0
7 * https://github.com/zephyrproject-rtos/zcbor
8 * Generated with a --default-max-qty of 99
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 "lwm2m_senml_cbor_encode.h"
17 #include "zcbor_print.h"
18
19 #define log_result(state, result, func) \
20 do { \
21 if (!result) { \
22 zcbor_trace_file(state); \
23 zcbor_log("%s error: %s\r\n", func, \
24 zcbor_error_str(zcbor_peek_error(state))); \
25 } else { \
26 zcbor_log("%s success\r\n", func); \
27 } \
28 } while (0)
29
30 static bool encode_repeated_record_bn(zcbor_state_t *state, const struct record_bn *input);
31 static bool encode_repeated_record_bt(zcbor_state_t *state, const struct record_bt *input);
32 static bool encode_repeated_record_n(zcbor_state_t *state, const struct record_n *input);
33 static bool encode_repeated_record_t(zcbor_state_t *state, const struct record_t *input);
34 static bool encode_repeated_record_union(zcbor_state_t *state, const struct record_union_r *input);
35 static bool encode_value(zcbor_state_t *state, const struct value_r *input);
36 static bool encode_key_value_pair(zcbor_state_t *state, const struct key_value_pair *input);
37 static bool encode_repeated_record_key_value_pair_m(zcbor_state_t *state,
38 const struct record_key_value_pair_m *input);
39 static bool encode_record(zcbor_state_t *state, const struct record *input);
40 static bool encode_lwm2m_senml(zcbor_state_t *state, const struct lwm2m_senml *input);
41
encode_repeated_record_bn(zcbor_state_t * state,const struct record_bn * input)42 static bool encode_repeated_record_bn(zcbor_state_t *state, const struct record_bn *input)
43 {
44 zcbor_log("%s\r\n", __func__);
45
46 bool res = ((((zcbor_int32_put(state, (-2)))) &&
47 (zcbor_tstr_encode(state, (&(*input).record_bn)))));
48
49 log_result(state, res, __func__);
50 return res;
51 }
52
encode_repeated_record_bt(zcbor_state_t * state,const struct record_bt * input)53 static bool encode_repeated_record_bt(zcbor_state_t *state, const struct record_bt *input)
54 {
55 zcbor_log("%s\r\n", __func__);
56
57 bool res = ((((zcbor_int32_put(state, (-3)))) &&
58 (zcbor_int64_encode(state, (&(*input).record_bt)))));
59
60 log_result(state, res, __func__);
61 return res;
62 }
63
encode_repeated_record_n(zcbor_state_t * state,const struct record_n * input)64 static bool encode_repeated_record_n(zcbor_state_t *state, const struct record_n *input)
65 {
66 zcbor_log("%s\r\n", __func__);
67
68 bool res = ((((zcbor_uint32_put(state, (0)))) &&
69 (zcbor_tstr_encode(state, (&(*input).record_n)))));
70
71 log_result(state, res, __func__);
72 return res;
73 }
74
encode_repeated_record_t(zcbor_state_t * state,const struct record_t * input)75 static bool encode_repeated_record_t(zcbor_state_t *state, const struct record_t *input)
76 {
77 zcbor_log("%s\r\n", __func__);
78
79 bool res = ((((zcbor_uint32_put(state, (6)))) &&
80 (zcbor_int64_encode(state, (&(*input).record_t)))));
81
82 log_result(state, res, __func__);
83 return res;
84 }
85
encode_repeated_record_union(zcbor_state_t * state,const struct record_union_r * input)86 static bool encode_repeated_record_union(zcbor_state_t *state, const struct record_union_r *input)
87 {
88 zcbor_log("%s\r\n", __func__);
89 struct zcbor_string tmp_str;
90
91 bool res = (((
92 ((*input).record_union_choice == union_vi_c)
93 ? (((zcbor_uint32_put(state, (2)))) &&
94 (zcbor_int64_encode(state, (&(*input).union_vi))))
95 : (((*input).record_union_choice == union_vf_c)
96 ? (((zcbor_uint32_put(state, (2)))) &&
97 (zcbor_float64_encode(state, (&(*input).union_vf))))
98 : (((*input).record_union_choice == union_vs_c)
99 ? (((zcbor_uint32_put(state, (3)))) &&
100 (zcbor_tstr_encode(state, (&(*input).union_vs))))
101 : (((*input).record_union_choice == union_vb_c)
102 ? (((zcbor_uint32_put(state, (4)))) &&
103 (zcbor_bool_encode(
104 state, (&(*input).union_vb))))
105 : (((*input).record_union_choice ==
106 union_vd_c)
107 ? (((zcbor_uint32_put(state,
108 (8)))) &&
109 (zcbor_bstr_encode(
110 state,
111 (&(*input).union_vd))))
112 : (((*input).record_union_choice ==
113 union_vlo_c)
114 ? (((zcbor_tstr_encode(
115 state,
116 ((tmp_str.value =
117 (uint8_t *)"vlo",
118 tmp_str.len =
119 sizeof("vlo") -
120 1,
121 &tmp_str))))) &&
122 (zcbor_tstr_encode(
123 state,
124 (&(*input).union_vlo))))
125 : false))))))));
126
127 log_result(state, res, __func__);
128 return res;
129 }
130
encode_value(zcbor_state_t * state,const struct value_r * input)131 static bool encode_value(zcbor_state_t *state, const struct value_r *input)
132 {
133 zcbor_log("%s\r\n", __func__);
134
135 bool res = (((
136 ((*input).value_choice == value_tstr_c)
137 ? ((zcbor_tstr_encode(state, (&(*input).value_tstr))))
138 : (((*input).value_choice == value_bstr_c)
139 ? ((zcbor_bstr_encode(state, (&(*input).value_bstr))))
140 : (((*input).value_choice == value_int_c)
141 ? ((zcbor_int64_encode(state, (&(*input).value_int))))
142 : (((*input).value_choice == value_float_c)
143 ? ((zcbor_float64_encode(
144 state, (&(*input).value_float))))
145 : (((*input).value_choice == value_bool_c)
146 ? ((zcbor_bool_encode(
147 state,
148 (&(*input).value_bool))))
149 : false)))))));
150
151 log_result(state, res, __func__);
152 return res;
153 }
154
encode_key_value_pair(zcbor_state_t * state,const struct key_value_pair * input)155 static bool encode_key_value_pair(zcbor_state_t *state, const struct key_value_pair *input)
156 {
157 zcbor_log("%s\r\n", __func__);
158
159 bool res = ((((zcbor_int32_encode(state, (&(*input).key_value_pair_key)))) &&
160 (encode_value(state, (&(*input).key_value_pair)))));
161
162 log_result(state, res, __func__);
163 return res;
164 }
165
encode_repeated_record_key_value_pair_m(zcbor_state_t * state,const struct record_key_value_pair_m * input)166 static bool encode_repeated_record_key_value_pair_m(zcbor_state_t *state,
167 const struct record_key_value_pair_m *input)
168 {
169 zcbor_log("%s\r\n", __func__);
170
171 bool res = (((encode_key_value_pair(state, (&(*input).record_key_value_pair_m)))));
172
173 log_result(state, res, __func__);
174 return res;
175 }
176
encode_record(zcbor_state_t * state,const struct record * input)177 static bool encode_record(zcbor_state_t *state, const struct record *input)
178 {
179 zcbor_log("%s\r\n", __func__);
180
181 bool res = ((
182 (zcbor_map_start_encode(state, ZCBOR_ARRAY_SIZE(input->record_key_value_pair_m)) &&
183 (((!(*input).record_bn_present ||
184 encode_repeated_record_bn(state, (&(*input).record_bn))) &&
185 (!(*input).record_bt_present ||
186 encode_repeated_record_bt(state, (&(*input).record_bt))) &&
187 (!(*input).record_n_present ||
188 encode_repeated_record_n(state, (&(*input).record_n))) &&
189 (!(*input).record_t_present ||
190 encode_repeated_record_t(state, (&(*input).record_t))) &&
191 (!(*input).record_union_present ||
192 encode_repeated_record_union(state, (&(*input).record_union))) &&
193 zcbor_multi_encode_minmax(
194 0, ZCBOR_ARRAY_SIZE(input->record_key_value_pair_m),
195 &(*input).record_key_value_pair_m_count,
196 (zcbor_encoder_t *)encode_repeated_record_key_value_pair_m, state,
197 (*&(*input).record_key_value_pair_m),
198 sizeof(struct record_key_value_pair_m))) ||
199 (zcbor_list_map_end_force_encode(state), false)) &&
200 zcbor_map_end_encode(state, ZCBOR_ARRAY_SIZE(input->record_key_value_pair_m)))));
201
202 log_result(state, res, __func__);
203 return res;
204 }
205
encode_lwm2m_senml(zcbor_state_t * state,const struct lwm2m_senml * input)206 static bool encode_lwm2m_senml(zcbor_state_t *state, const struct lwm2m_senml *input)
207 {
208 zcbor_log("%s\r\n", __func__);
209
210 bool res =
211 (((zcbor_list_start_encode(state, ZCBOR_ARRAY_SIZE(input->lwm2m_senml_record_m)) &&
212 ((zcbor_multi_encode_minmax(
213 1, ZCBOR_ARRAY_SIZE(input->lwm2m_senml_record_m),
214 &(*input).lwm2m_senml_record_m_count, (zcbor_encoder_t *)encode_record,
215 state, (*&(*input).lwm2m_senml_record_m), sizeof(struct record))) ||
216 (zcbor_list_map_end_force_encode(state), false)) &&
217 zcbor_list_end_encode(state, ZCBOR_ARRAY_SIZE(input->lwm2m_senml_record_m)))));
218
219 log_result(state, res, __func__);
220 return res;
221 }
222
cbor_encode_lwm2m_senml(uint8_t * payload,size_t payload_len,const struct lwm2m_senml * input,size_t * payload_len_out)223 int cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len, const struct lwm2m_senml *input,
224 size_t *payload_len_out)
225 {
226 zcbor_state_t states[5];
227
228 return zcbor_entry_function(payload, payload_len, (void *)input, payload_len_out, states,
229 (zcbor_decoder_t *)encode_lwm2m_senml,
230 sizeof(states) / sizeof(zcbor_state_t), 1);
231 }
232