1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Generated using zcbor version 0.7.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 
18 static bool encode_repeated_record_bn(zcbor_state_t *state, const struct record_bn *input);
19 static bool encode_repeated_record_bt(zcbor_state_t *state, const struct record_bt *input);
20 static bool encode_repeated_record_n(zcbor_state_t *state, const struct record_n *input);
21 static bool encode_repeated_record_t(zcbor_state_t *state, const struct record_t *input);
22 static bool encode_repeated_record_union(zcbor_state_t *state, const struct record_union_ *input);
23 static bool encode_value(zcbor_state_t *state, const struct value_ *input);
24 static bool encode_key_value_pair(zcbor_state_t *state, const struct key_value_pair *input);
25 static bool encode_repeated_record__key_value_pair(zcbor_state_t *state,
26 						   const struct record__key_value_pair *input);
27 static bool encode_record(zcbor_state_t *state, const struct record *input);
28 static bool encode_lwm2m_senml(zcbor_state_t *state, const struct lwm2m_senml *input);
29 
encode_repeated_record_bn(zcbor_state_t * state,const struct record_bn * input)30 static bool encode_repeated_record_bn(zcbor_state_t *state, const struct record_bn *input)
31 {
32 	zcbor_print("%s\r\n", __func__);
33 
34 	bool tmp_result = ((((zcbor_int32_put(state, (-2)))) &&
35 			    (zcbor_tstr_encode(state, (&(*input)._record_bn)))));
36 
37 	if (!tmp_result) {
38 		zcbor_trace();
39 	}
40 
41 	return tmp_result;
42 }
43 
encode_repeated_record_bt(zcbor_state_t * state,const struct record_bt * input)44 static bool encode_repeated_record_bt(zcbor_state_t *state, const struct record_bt *input)
45 {
46 	zcbor_print("%s\r\n", __func__);
47 
48 	bool tmp_result =
49 		((((zcbor_int32_put(state, (-3)))) &&
50 		  ((((*input)._record_bt >= INT64_MIN) && ((*input)._record_bt <= INT64_MAX)) ||
51 		   (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)) &&
52 		  (zcbor_int64_encode(state, (&(*input)._record_bt)))));
53 
54 	if (!tmp_result) {
55 		zcbor_trace();
56 	}
57 
58 	return tmp_result;
59 }
60 
encode_repeated_record_n(zcbor_state_t * state,const struct record_n * input)61 static bool encode_repeated_record_n(zcbor_state_t *state, const struct record_n *input)
62 {
63 	zcbor_print("%s\r\n", __func__);
64 
65 	bool tmp_result = ((((zcbor_uint32_put(state, (0)))) &&
66 			    (zcbor_tstr_encode(state, (&(*input)._record_n)))));
67 
68 	if (!tmp_result) {
69 		zcbor_trace();
70 	}
71 
72 	return tmp_result;
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_print("%s\r\n", __func__);
78 
79 	bool tmp_result =
80 		((((zcbor_uint32_put(state, (6)))) &&
81 		  ((((*input)._record_t >= INT64_MIN) && ((*input)._record_t <= INT64_MAX)) ||
82 		   (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)) &&
83 		  (zcbor_int64_encode(state, (&(*input)._record_t)))));
84 
85 	if (!tmp_result) {
86 		zcbor_trace();
87 	}
88 
89 	return tmp_result;
90 }
91 
encode_repeated_record_union(zcbor_state_t * state,const struct record_union_ * input)92 static bool encode_repeated_record_union(zcbor_state_t *state, const struct record_union_ *input)
93 {
94 	zcbor_print("%s\r\n", __func__);
95 	struct zcbor_string tmp_str;
96 
97 	bool tmp_result = (((
98 		((*input)._record_union_choice == _union_vi)
99 			? (((zcbor_uint32_put(state, (2)))) &&
100 			   ((((*input)._union_vi >= INT64_MIN) &&
101 			     ((*input)._union_vi <= INT64_MAX)) ||
102 			    (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)) &&
103 			   (zcbor_int64_encode(state, (&(*input)._union_vi))))
104 		: (((*input)._record_union_choice == _union_vf)
105 			? (((zcbor_uint32_put(state, (2)))) &&
106 				(zcbor_float64_encode(state, (&(*input)._union_vf))))
107 		: (((*input)._record_union_choice == _union_vs)
108 			? (((zcbor_uint32_put(state, (3)))) &&
109 				(zcbor_tstr_encode(state, (&(*input)._union_vs))))
110 		: (((*input)._record_union_choice == _union_vb)
111 			? (((zcbor_uint32_put(state, (4)))) &&
112 				(zcbor_bool_encode(state, (&(*input)._union_vb))))
113 		: (((*input)._record_union_choice == _union_vd)
114 			? (((zcbor_uint32_put(state, (8)))) &&
115 				(zcbor_bstr_encode(state, (&(*input)._union_vd))))
116 		: (((*input)._record_union_choice == _union_vlo)
117 			? (((zcbor_tstr_encode(state, ((tmp_str.value =	(uint8_t *)"vlo",
118 							tmp_str.len = sizeof("vlo") - 1,
119 							&tmp_str))))) &&
120 				(zcbor_tstr_encode(state, (&(*input)._union_vlo))))
121 		: false))))))));
122 
123 	if (!tmp_result) {
124 		zcbor_trace();
125 	}
126 
127 	return tmp_result;
128 }
129 
encode_value(zcbor_state_t * state,const struct value_ * input)130 static bool encode_value(zcbor_state_t *state, const struct value_ *input)
131 {
132 	zcbor_print("%s\r\n", __func__);
133 
134 	bool tmp_result = (((
135 		((*input)._value_choice == _value_tstr)
136 			? ((zcbor_tstr_encode(state, (&(*input)._value_tstr))))
137 		: (((*input)._value_choice == _value_bstr)
138 			? ((zcbor_bstr_encode(state, (&(*input)._value_bstr))))
139 		: (((*input)._value_choice == _value_int)
140 			? (((((*input)._value_int >= INT64_MIN) &&
141 				((*input)._value_int <= INT64_MAX)) ||
142 				(zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)) &&
143 				(zcbor_int64_encode(state, (&(*input)._value_int))))
144 		: (((*input)._value_choice == _value_float)
145 			? ((zcbor_float64_encode(state, (&(*input)._value_float))))
146 		: (((*input)._value_choice == _value_bool)
147 			? ((zcbor_bool_encode(state, (&(*input)._value_bool))))
148 		: false)))))));
149 
150 	if (!tmp_result) {
151 		zcbor_trace();
152 	}
153 
154 	return tmp_result;
155 }
156 
encode_key_value_pair(zcbor_state_t * state,const struct key_value_pair * input)157 static bool encode_key_value_pair(zcbor_state_t *state, const struct key_value_pair *input)
158 {
159 	zcbor_print("%s\r\n", __func__);
160 
161 	bool tmp_result = ((((zcbor_int32_encode(state, (&(*input)._key_value_pair_key)))) &&
162 			    (encode_value(state, (&(*input)._key_value_pair)))));
163 
164 	if (!tmp_result) {
165 		zcbor_trace();
166 	}
167 
168 	return tmp_result;
169 }
170 
encode_repeated_record__key_value_pair(zcbor_state_t * state,const struct record__key_value_pair * input)171 static bool encode_repeated_record__key_value_pair(zcbor_state_t *state,
172 						   const struct record__key_value_pair *input)
173 {
174 	zcbor_print("%s\r\n", __func__);
175 
176 	bool tmp_result = (((encode_key_value_pair(state, (&(*input)._record__key_value_pair)))));
177 
178 	if (!tmp_result) {
179 		zcbor_trace();
180 	}
181 
182 	return tmp_result;
183 }
184 
encode_record(zcbor_state_t * state,const struct record * input)185 static bool encode_record(zcbor_state_t *state, const struct record *input)
186 {
187 	zcbor_print("%s\r\n", __func__);
188 
189 	bool tmp_result = ((
190 		(zcbor_map_start_encode(state, ZCBOR_ARRAY_SIZE(input->_record__key_value_pair)) &&
191 		 ((zcbor_present_encode(&((*input)._record_bn_present),
192 					(zcbor_encoder_t *)encode_repeated_record_bn, state,
193 					(&(*input)._record_bn)) &&
194 		   zcbor_present_encode(&((*input)._record_bt_present),
195 					(zcbor_encoder_t *)encode_repeated_record_bt, state,
196 					(&(*input)._record_bt)) &&
197 		   zcbor_present_encode(&((*input)._record_n_present),
198 					(zcbor_encoder_t *)encode_repeated_record_n, state,
199 					(&(*input)._record_n)) &&
200 		   zcbor_present_encode(&((*input)._record_t_present),
201 					(zcbor_encoder_t *)encode_repeated_record_t, state,
202 					(&(*input)._record_t)) &&
203 		   zcbor_present_encode(&((*input)._record_union_present),
204 					(zcbor_encoder_t *)encode_repeated_record_union, state,
205 					(&(*input)._record_union)) &&
206 		   zcbor_multi_encode_minmax(
207 			   0, ZCBOR_ARRAY_SIZE(input->_record__key_value_pair),
208 			   &(*input)._record__key_value_pair_count,
209 			   (zcbor_encoder_t *)encode_repeated_record__key_value_pair, state,
210 			   (&(*input)._record__key_value_pair),
211 			   sizeof(struct record__key_value_pair))) ||
212 		  (zcbor_list_map_end_force_encode(state), false)) &&
213 		 zcbor_map_end_encode(state, ZCBOR_ARRAY_SIZE(input->_record__key_value_pair)))));
214 
215 	if (!tmp_result) {
216 		zcbor_trace();
217 	}
218 
219 	return tmp_result;
220 }
221 
encode_lwm2m_senml(zcbor_state_t * state,const struct lwm2m_senml * input)222 static bool encode_lwm2m_senml(zcbor_state_t *state, const struct lwm2m_senml *input)
223 {
224 	zcbor_print("%s\r\n", __func__);
225 
226 	bool tmp_result =
227 		(((zcbor_list_start_encode(state, ZCBOR_ARRAY_SIZE(input->_lwm2m_senml__record)) &&
228 		   ((zcbor_multi_encode_minmax(
229 			    1, ZCBOR_ARRAY_SIZE(input->_lwm2m_senml__record),
230 			    &(*input)._lwm2m_senml__record_count, (zcbor_encoder_t *)encode_record,
231 			    state, (&(*input)._lwm2m_senml__record), sizeof(struct record))) ||
232 		    (zcbor_list_map_end_force_encode(state), false)) &&
233 		   zcbor_list_end_encode(state, ZCBOR_ARRAY_SIZE(input->_lwm2m_senml__record)))));
234 
235 	if (!tmp_result) {
236 		zcbor_trace();
237 	}
238 
239 	return tmp_result;
240 }
241 
cbor_encode_lwm2m_senml(uint8_t * payload,size_t payload_len,const struct lwm2m_senml * input,size_t * payload_len_out)242 int cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len, const struct lwm2m_senml *input,
243 			    size_t *payload_len_out)
244 {
245 	zcbor_state_t states[5];
246 
247 	zcbor_new_state(states, sizeof(states) / sizeof(zcbor_state_t), payload, payload_len, 1);
248 
249 	bool ret = encode_lwm2m_senml(states, input);
250 
251 	if (ret && (payload_len_out != NULL)) {
252 		*payload_len_out = MIN(payload_len, (size_t)states[0].payload - (size_t)payload);
253 	}
254 
255 	if (!ret) {
256 		int err = zcbor_pop_error(states);
257 
258 		zcbor_print("Return error: %d\r\n", err);
259 		return (err == ZCBOR_SUCCESS) ? ZCBOR_ERR_UNKNOWN : err;
260 	}
261 	return ZCBOR_SUCCESS;
262 }
263