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