Skip to content

Commit 98b22a1

Browse files
Veijo Pesonencarlescufi
authored andcommitted
net: lwm2m: SenML CBOR optimisations
Regenerates the encoder and decoder. Treats integers and floating-point values as separate entities instead of saying that those are numerical values. Brings some memory savings. Signed-off-by: Veijo Pesonen <[email protected]>
1 parent b78dd24 commit 98b22a1

File tree

6 files changed

+104
-135
lines changed

6 files changed

+104
-135
lines changed

subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,8 @@ static int put_value(struct lwm2m_output_context *out, struct lwm2m_obj_path *pa
304304
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
305305

306306
/* Write the value */
307-
record->_record_union._record_union_choice = _union_v;
308-
record->_record_union._union_v._numeric_choice = _numeric_int;
309-
record->_record_union._union_v._numeric_int = value;
307+
record->_record_union._record_union_choice = _union_vi;
308+
record->_record_union._union_vi = value;
310309
record->_record_union_present = 1;
311310

312311
return 0;
@@ -343,9 +342,8 @@ static int put_time(struct lwm2m_output_context *out, struct lwm2m_obj_path *pat
343342
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
344343

345344
/* Write the value */
346-
record->_record_union._record_union_choice = _union_v;
347-
record->_record_union._union_v._numeric_choice = _numeric_int;
348-
record->_record_union._union_v._numeric_int = value;
345+
record->_record_union._record_union_choice = _union_vi;
346+
record->_record_union._union_vi = value;
349347
record->_record_union_present = 1;
350348

351349
return 0;
@@ -362,9 +360,8 @@ static int put_float(struct lwm2m_output_context *out, struct lwm2m_obj_path *pa
362360
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
363361

364362
/* Write the value */
365-
record->_record_union._record_union_choice = _union_v;
366-
record->_record_union._union_v._numeric_choice = _numeric_float;
367-
record->_record_union._union_v._numeric_float = *value;
363+
record->_record_union._record_union_choice = _union_vf;
364+
record->_record_union._union_vf = *value;
368365
record->_record_union_present = 1;
369366

370367
return 0;
@@ -477,7 +474,7 @@ static int get_s32(struct lwm2m_input_context *in, int32_t *value)
477474
return -EINVAL;
478475
}
479476

480-
*value = fd->current->_record_union._union_v._numeric_int;
477+
*value = fd->current->_record_union._union_vi;
481478
fd->current = NULL;
482479

483480
return 0;
@@ -492,7 +489,7 @@ static int get_s64(struct lwm2m_input_context *in, int64_t *value)
492489
return -EINVAL;
493490
}
494491

495-
*value = fd->current->_record_union._union_v._numeric_int;
492+
*value = fd->current->_record_union._union_vi;
496493
fd->current = NULL;
497494

498495
return 0;
@@ -507,7 +504,7 @@ static int get_float(struct lwm2m_input_context *in, double *value)
507504
return -EINVAL;
508505
}
509506

510-
*value = fd->current->_record_union._union_v._numeric_float;
507+
*value = fd->current->_record_union._union_vf;
511508
fd->current = NULL;
512509

513510
return 0;

subsys/net/lib/lwm2m/lwm2m_senml_cbor_decode.c

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
/*
7-
* Generated using zcbor version 0.3.99
7+
* Generated using zcbor version 0.4.0
88
* https://github.com/zephyrproject-rtos/zcbor
99
* Generated with a --default-max-qty of 99
1010
*/
@@ -43,47 +43,31 @@ static bool decode_repeated_record_n(zcbor_state_t *state, struct record_n *resu
4343
return tmp_result;
4444
}
4545

46-
static bool decode_numeric(zcbor_state_t *state, struct numeric_ *result)
47-
{
48-
zcbor_print("%s\r\n", __func__);
49-
bool int_res;
50-
51-
bool tmp_result =
52-
(((zcbor_union_start_code(state) &&
53-
(int_res = ((((zcbor_int64_decode(state, (&(*result)._numeric_int))) &&
54-
((((*result)._numeric_int >= -9223372036854775807LL) &&
55-
((*result)._numeric_int <= 9223372036854775807LL)) ||
56-
(zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))) &&
57-
(((*result)._numeric_choice = _numeric_int) || 1)) ||
58-
(((zcbor_float_decode(state, (&(*result)._numeric_float)))) &&
59-
(((*result)._numeric_choice = _numeric_float) || 1))),
60-
zcbor_union_end_code(state), int_res))));
61-
62-
if (!tmp_result)
63-
zcbor_trace();
64-
65-
return tmp_result;
66-
}
67-
6846
static bool decode_repeated_record_union(zcbor_state_t *state, struct record_union_ *result)
6947
{
7048
zcbor_print("%s\r\n", __func__);
7149
bool int_res;
7250

73-
bool tmp_result = (((zcbor_union_start_code(state) &&
74-
(int_res = (((((zcbor_uint32_expect_union(state, (2)))) &&
75-
(decode_numeric(state, (&(*result)._union_v)))) &&
76-
(((*result)._record_union_choice = _union_v) || 1)) ||
77-
((((zcbor_uint32_expect_union(state, (3)))) &&
78-
(zcbor_tstr_decode(state, (&(*result)._union_vs)))) &&
79-
(((*result)._record_union_choice = _union_vs) || 1)) ||
80-
((((zcbor_uint32_expect_union(state, (4)))) &&
81-
(zcbor_bool_decode(state, (&(*result)._union_vb)))) &&
82-
(((*result)._record_union_choice = _union_vb) || 1)) ||
83-
((((zcbor_uint32_expect_union(state, (8)))) &&
84-
(zcbor_bstr_decode(state, (&(*result)._union_vd)))) &&
85-
(((*result)._record_union_choice = _union_vd) || 1))),
86-
zcbor_union_end_code(state), int_res))));
51+
bool tmp_result = (((zcbor_union_start_code(state) && (int_res = (
52+
((((zcbor_uint32_expect_union(state, (2)))) &&
53+
(zcbor_int64_decode(state, (&(*result)._union_vi))) &&
54+
((((*result)._union_vi >= -9223372036854775807LL) &&
55+
((*result)._union_vi <= 9223372036854775807LL)) ||
56+
(zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))) &&
57+
(((*result)._record_union_choice = _union_vi) || 1)) ||
58+
((((zcbor_uint32_expect_union(state, (2)))) &&
59+
(zcbor_float_decode(state, (&(*result)._union_vf)))) &&
60+
(((*result)._record_union_choice = _union_vf) || 1)) ||
61+
((((zcbor_uint32_expect_union(state, (3)))) &&
62+
(zcbor_tstr_decode(state, (&(*result)._union_vs)))) &&
63+
(((*result)._record_union_choice = _union_vs) || 1)) ||
64+
((((zcbor_uint32_expect_union(state, (4)))) &&
65+
(zcbor_bool_decode(state, (&(*result)._union_vb)))) &&
66+
(((*result)._record_union_choice = _union_vb) || 1)) ||
67+
((((zcbor_uint32_expect_union(state, (8)))) &&
68+
(zcbor_bstr_decode(state, (&(*result)._union_vd)))) &&
69+
(((*result)._record_union_choice = _union_vd) || 1))),
70+
zcbor_union_end_code(state), int_res))));
8771

8872
if (!tmp_result)
8973
zcbor_trace();
@@ -97,18 +81,21 @@ static bool decode_value(zcbor_state_t *state, struct value_ *result)
9781
bool int_res;
9882

9983
bool tmp_result =
100-
(((zcbor_union_start_code(state) &&
101-
(int_res = ((((zcbor_tstr_decode(state, (&(*result)._value_tstr)))) &&
84+
(((zcbor_union_start_code(state) && (int_res = (
85+
(((zcbor_tstr_decode(state, (&(*result)._value_tstr)))) &&
10286
(((*result)._value_choice = _value_tstr) || 1)) ||
103-
(((zcbor_bstr_decode(state, (&(*result)._value_bstr)))) &&
87+
(((zcbor_bstr_decode(state, (&(*result)._value_bstr)))) &&
10488
(((*result)._value_choice = _value_bstr) || 1)) ||
105-
(zcbor_union_elem_code(state) &&
106-
(((decode_numeric(state, (&(*result)._value__numeric)))) &&
107-
(((*result)._value_choice = _value__numeric) || 1))) ||
108-
(zcbor_union_elem_code(state) &&
109-
(((zcbor_bool_decode(state, (&(*result)._value_bool)))) &&
110-
(((*result)._value_choice = _value_bool) || 1)))),
111-
zcbor_union_end_code(state), int_res))));
89+
(((zcbor_int64_decode(state, (&(*result)._value_int))) &&
90+
((((*result)._value_int >= -9223372036854775807LL) &&
91+
((*result)._value_int <= 9223372036854775807LL)) ||
92+
(zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))) &&
93+
(((*result)._value_choice = _value_int) || 1)) ||
94+
(((zcbor_float_decode(state, (&(*result)._value_float)))) &&
95+
(((*result)._value_choice = _value_float) || 1)) ||
96+
(((zcbor_bool_decode(state, (&(*result)._value_bool)))) &&
97+
(((*result)._value_choice = _value_bool) || 1))),
98+
zcbor_union_end_code(state), int_res))));
11299

113100
if (!tmp_result)
114101
zcbor_trace();
@@ -163,7 +150,7 @@ static bool decode_record(zcbor_state_t *state, struct record *result)
163150
state, (&(*result)._record__key_value_pair),
164151
sizeof(struct record__key_value_pair))) ||
165152
(zcbor_list_map_end_force_decode(state), false)) &&
166-
zcbor_map_end_decode(state))));
153+
zcbor_map_end_decode(state))));
167154

168155
if (!tmp_result)
169156
zcbor_trace();
@@ -190,10 +177,10 @@ static bool decode_lwm2m_senml(zcbor_state_t *state, struct lwm2m_senml *result)
190177
return tmp_result;
191178
}
192179

193-
uint_fast8_t cbor_decode_lwm2m_senml(const uint8_t *payload, size_t payload_len,
180+
int cbor_decode_lwm2m_senml(const uint8_t *payload, size_t payload_len,
194181
struct lwm2m_senml *result, size_t *payload_len_out)
195182
{
196-
zcbor_state_t states[6];
183+
zcbor_state_t states[5];
197184

198185
zcbor_new_state(states, sizeof(states) / sizeof(zcbor_state_t), payload, payload_len, 1);
199186

@@ -204,7 +191,7 @@ uint_fast8_t cbor_decode_lwm2m_senml(const uint8_t *payload, size_t payload_len,
204191
}
205192

206193
if (!ret) {
207-
uint_fast8_t ret = zcbor_pop_error(states);
194+
int ret = zcbor_pop_error(states);
208195

209196
return (ret == ZCBOR_SUCCESS) ? ZCBOR_ERR_UNKNOWN : ret;
210197
}

subsys/net/lib/lwm2m/lwm2m_senml_cbor_decode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
/*
7-
* Generated using zcbor version 0.3.99
7+
* Generated using zcbor version 0.4.0
88
* https://github.com/zephyrproject-rtos/zcbor
99
* Generated with a --default-max-qty of 99
1010
*/
@@ -19,7 +19,7 @@
1919
#include "zcbor_decode.h"
2020
#include "lwm2m_senml_cbor_types.h"
2121

22-
uint_fast8_t cbor_decode_lwm2m_senml(const uint8_t *payload, size_t payload_len,
22+
int cbor_decode_lwm2m_senml(const uint8_t *payload, size_t payload_len,
2323
struct lwm2m_senml *result, size_t *payload_len_out);
2424

2525
#endif /* LWM2M_SENML_CBOR_DECODE_H__ */

subsys/net/lib/lwm2m/lwm2m_senml_cbor_encode.c

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
/*
7-
* Generated using zcbor version 0.3.99
7+
* Generated using zcbor version 0.4.0
88
* https://github.com/zephyrproject-rtos/zcbor
99
* Generated with a --default-max-qty of 99
1010
*/
@@ -43,45 +43,29 @@ static bool encode_repeated_record_n(zcbor_state_t *state, const struct record_n
4343
return tmp_result;
4444
}
4545

46-
static bool encode_numeric(zcbor_state_t *state, const struct numeric_ *input)
47-
{
48-
zcbor_print("%s\r\n", __func__);
49-
50-
bool tmp_result =
51-
(((((*input)._numeric_choice == _numeric_int) ?
52-
(((((*input)._numeric_int >= -9223372036854775807LL) &&
53-
((*input)._numeric_int <= 9223372036854775807LL)) ||
54-
(zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)) &&
55-
(zcbor_int64_encode(state, (&(*input)._numeric_int)))) :
56-
(((*input)._numeric_choice == _numeric_float) ?
57-
((zcbor_float64_encode(state, (&(*input)._numeric_float)))) :
58-
false))));
59-
60-
if (!tmp_result)
61-
zcbor_trace();
62-
63-
return tmp_result;
64-
}
65-
6646
static bool encode_repeated_record_union(zcbor_state_t *state, const struct record_union_ *input)
6747
{
6848
zcbor_print("%s\r\n", __func__);
6949

7050
bool tmp_result =
71-
(((((*input)._record_union_choice == _union_v) ?
72-
(((zcbor_uint32_put(state, (2)))) &&
73-
(encode_numeric(state, (&(*input)._union_v)))) :
74-
(((*input)._record_union_choice == _union_vs) ?
75-
(((zcbor_uint32_put(state, (3)))) &&
76-
(zcbor_tstr_encode(state, (&(*input)._union_vs)))) :
77-
(((*input)._record_union_choice == _union_vb) ?
78-
(((zcbor_uint32_put(state, (4)))) &&
79-
(zcbor_bool_encode(state, (&(*input)._union_vb)))) :
80-
(((*input)._record_union_choice == _union_vd) ?
81-
(((zcbor_uint32_put(state, (8)))) &&
82-
(zcbor_bstr_encode(state,
83-
(&(*input)._union_vd)))) :
84-
false))))));
51+
(((((*input)._record_union_choice == _union_vi) ?
52+
(((zcbor_uint32_put(state, (2)))) &&
53+
((((*input)._union_vi >= -9223372036854775807LL) &&
54+
((*input)._union_vi <= 9223372036854775807LL)) ||
55+
(zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)) &&
56+
(zcbor_int64_encode(state, (&(*input)._union_vi)))) :
57+
(((*input)._record_union_choice == _union_vf) ?
58+
(((zcbor_uint32_put(state, (2)))) &&
59+
(zcbor_float64_encode(state, (&(*input)._union_vf)))) :
60+
(((*input)._record_union_choice == _union_vs) ?
61+
(((zcbor_uint32_put(state, (3)))) &&
62+
(zcbor_tstr_encode(state, (&(*input)._union_vs)))) :
63+
(((*input)._record_union_choice == _union_vb) ?
64+
(((zcbor_uint32_put(state, (4)))) &&
65+
(zcbor_bool_encode(state, (&(*input)._union_vb)))) :
66+
(((*input)._record_union_choice == _union_vd) ?
67+
(((zcbor_uint32_put(state, (8)))) &&
68+
(zcbor_bstr_encode(state, (&(*input)._union_vd)))) : false)))))));
8569

8670
if (!tmp_result)
8771
zcbor_trace();
@@ -95,15 +79,18 @@ static bool encode_value(zcbor_state_t *state, const struct value_ *input)
9579

9680
bool tmp_result = ((
9781
(((*input)._value_choice == _value_tstr) ?
98-
((zcbor_tstr_encode(state, (&(*input)._value_tstr)))) :
99-
(((*input)._value_choice == _value_bstr) ?
100-
((zcbor_bstr_encode(state, (&(*input)._value_bstr)))) :
101-
(((*input)._value_choice == _value__numeric) ?
102-
((encode_numeric(state, (&(*input)._value__numeric)))) :
103-
(((*input)._value_choice == _value_bool) ?
104-
((zcbor_bool_encode(state,
105-
(&(*input)._value_bool)))) :
106-
false))))));
82+
((zcbor_tstr_encode(state, (&(*input)._value_tstr)))) :
83+
(((*input)._value_choice == _value_bstr) ?
84+
((zcbor_bstr_encode(state, (&(*input)._value_bstr)))) :
85+
(((*input)._value_choice == _value_int) ?
86+
(((((*input)._value_int >= -9223372036854775807LL) &&
87+
((*input)._value_int <= 9223372036854775807LL)) ||
88+
(zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)) &&
89+
(zcbor_int64_encode(state, (&(*input)._value_int)))) :
90+
(((*input)._value_choice == _value_float) ?
91+
((zcbor_float64_encode(state, (&(*input)._value_float)))) :
92+
(((*input)._value_choice == _value_bool) ?
93+
((zcbor_bool_encode(state, (&(*input)._value_bool)))) : false)))))));
10794

10895
if (!tmp_result)
10996
zcbor_trace();
@@ -188,10 +175,10 @@ static bool encode_lwm2m_senml(zcbor_state_t *state, const struct lwm2m_senml *i
188175
return tmp_result;
189176
}
190177

191-
uint_fast8_t cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len,
178+
int cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len,
192179
const struct lwm2m_senml *input, size_t *payload_len_out)
193180
{
194-
zcbor_state_t states[6];
181+
zcbor_state_t states[5];
195182

196183
zcbor_new_state(states, sizeof(states) / sizeof(zcbor_state_t), payload, payload_len, 1);
197184

@@ -202,7 +189,7 @@ uint_fast8_t cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len,
202189
}
203190

204191
if (!ret) {
205-
uint_fast8_t ret = zcbor_pop_error(states);
192+
int ret = zcbor_pop_error(states);
206193

207194
return (ret == ZCBOR_SUCCESS) ? ZCBOR_ERR_UNKNOWN : ret;
208195
}

subsys/net/lib/lwm2m/lwm2m_senml_cbor_encode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
/*
7-
* Generated using zcbor version 0.3.99
7+
* Generated using zcbor version 0.4.0
88
* https://github.com/zephyrproject-rtos/zcbor
99
* Generated with a --default-max-qty of 99
1010
*/
@@ -19,7 +19,7 @@
1919
#include "zcbor_encode.h"
2020
#include "lwm2m_senml_cbor_types.h"
2121

22-
uint_fast8_t cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len,
22+
int cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len,
2323
const struct lwm2m_senml *input, size_t *payload_len_out);
2424

2525
#endif /* LWM2M_SENML_CBOR_ENCODE_H__ */

0 commit comments

Comments
 (0)