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 <veijo.pesonen@nordicsemi.no>
This commit is contained in:
Veijo Pesonen 2022-04-26 11:33:48 +03:00 committed by Carles Cufí
parent b78dd2498a
commit 98b22a1850
6 changed files with 104 additions and 135 deletions

View file

@ -304,9 +304,8 @@ static int put_value(struct lwm2m_output_context *out, struct lwm2m_obj_path *pa
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out)); struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
/* Write the value */ /* Write the value */
record->_record_union._record_union_choice = _union_v; record->_record_union._record_union_choice = _union_vi;
record->_record_union._union_v._numeric_choice = _numeric_int; record->_record_union._union_vi = value;
record->_record_union._union_v._numeric_int = value;
record->_record_union_present = 1; record->_record_union_present = 1;
return 0; return 0;
@ -343,9 +342,8 @@ static int put_time(struct lwm2m_output_context *out, struct lwm2m_obj_path *pat
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out)); struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
/* Write the value */ /* Write the value */
record->_record_union._record_union_choice = _union_v; record->_record_union._record_union_choice = _union_vi;
record->_record_union._union_v._numeric_choice = _numeric_int; record->_record_union._union_vi = value;
record->_record_union._union_v._numeric_int = value;
record->_record_union_present = 1; record->_record_union_present = 1;
return 0; return 0;
@ -362,9 +360,8 @@ static int put_float(struct lwm2m_output_context *out, struct lwm2m_obj_path *pa
struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out)); struct record *record = CONSUME_CBOR_FD_REC(LWM2M_OFD_CBOR(out));
/* Write the value */ /* Write the value */
record->_record_union._record_union_choice = _union_v; record->_record_union._record_union_choice = _union_vf;
record->_record_union._union_v._numeric_choice = _numeric_float; record->_record_union._union_vf = *value;
record->_record_union._union_v._numeric_float = *value;
record->_record_union_present = 1; record->_record_union_present = 1;
return 0; return 0;
@ -477,7 +474,7 @@ static int get_s32(struct lwm2m_input_context *in, int32_t *value)
return -EINVAL; return -EINVAL;
} }
*value = fd->current->_record_union._union_v._numeric_int; *value = fd->current->_record_union._union_vi;
fd->current = NULL; fd->current = NULL;
return 0; return 0;
@ -492,7 +489,7 @@ static int get_s64(struct lwm2m_input_context *in, int64_t *value)
return -EINVAL; return -EINVAL;
} }
*value = fd->current->_record_union._union_v._numeric_int; *value = fd->current->_record_union._union_vi;
fd->current = NULL; fd->current = NULL;
return 0; return 0;
@ -507,7 +504,7 @@ static int get_float(struct lwm2m_input_context *in, double *value)
return -EINVAL; return -EINVAL;
} }
*value = fd->current->_record_union._union_v._numeric_float; *value = fd->current->_record_union._union_vf;
fd->current = NULL; fd->current = NULL;
return 0; return 0;

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
/* /*
* Generated using zcbor version 0.3.99 * Generated using zcbor version 0.4.0
* https://github.com/zephyrproject-rtos/zcbor * https://github.com/zephyrproject-rtos/zcbor
* Generated with a --default-max-qty of 99 * Generated with a --default-max-qty of 99
*/ */
@ -43,47 +43,31 @@ static bool decode_repeated_record_n(zcbor_state_t *state, struct record_n *resu
return tmp_result; return tmp_result;
} }
static bool decode_numeric(zcbor_state_t *state, struct numeric_ *result)
{
zcbor_print("%s\r\n", __func__);
bool int_res;
bool tmp_result =
(((zcbor_union_start_code(state) &&
(int_res = ((((zcbor_int64_decode(state, (&(*result)._numeric_int))) &&
((((*result)._numeric_int >= -9223372036854775807LL) &&
((*result)._numeric_int <= 9223372036854775807LL)) ||
(zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))) &&
(((*result)._numeric_choice = _numeric_int) || 1)) ||
(((zcbor_float_decode(state, (&(*result)._numeric_float)))) &&
(((*result)._numeric_choice = _numeric_float) || 1))),
zcbor_union_end_code(state), int_res))));
if (!tmp_result)
zcbor_trace();
return tmp_result;
}
static bool decode_repeated_record_union(zcbor_state_t *state, struct record_union_ *result) static bool decode_repeated_record_union(zcbor_state_t *state, struct record_union_ *result)
{ {
zcbor_print("%s\r\n", __func__); zcbor_print("%s\r\n", __func__);
bool int_res; bool int_res;
bool tmp_result = (((zcbor_union_start_code(state) && bool tmp_result = (((zcbor_union_start_code(state) && (int_res = (
(int_res = (((((zcbor_uint32_expect_union(state, (2)))) && ((((zcbor_uint32_expect_union(state, (2)))) &&
(decode_numeric(state, (&(*result)._union_v)))) && (zcbor_int64_decode(state, (&(*result)._union_vi))) &&
(((*result)._record_union_choice = _union_v) || 1)) || ((((*result)._union_vi >= -9223372036854775807LL) &&
((((zcbor_uint32_expect_union(state, (3)))) && ((*result)._union_vi <= 9223372036854775807LL)) ||
(zcbor_tstr_decode(state, (&(*result)._union_vs)))) && (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))) &&
(((*result)._record_union_choice = _union_vs) || 1)) || (((*result)._record_union_choice = _union_vi) || 1)) ||
((((zcbor_uint32_expect_union(state, (4)))) && ((((zcbor_uint32_expect_union(state, (2)))) &&
(zcbor_bool_decode(state, (&(*result)._union_vb)))) && (zcbor_float_decode(state, (&(*result)._union_vf)))) &&
(((*result)._record_union_choice = _union_vb) || 1)) || (((*result)._record_union_choice = _union_vf) || 1)) ||
((((zcbor_uint32_expect_union(state, (8)))) && ((((zcbor_uint32_expect_union(state, (3)))) &&
(zcbor_bstr_decode(state, (&(*result)._union_vd)))) && (zcbor_tstr_decode(state, (&(*result)._union_vs)))) &&
(((*result)._record_union_choice = _union_vd) || 1))), (((*result)._record_union_choice = _union_vs) || 1)) ||
zcbor_union_end_code(state), int_res)))); ((((zcbor_uint32_expect_union(state, (4)))) &&
(zcbor_bool_decode(state, (&(*result)._union_vb)))) &&
(((*result)._record_union_choice = _union_vb) || 1)) ||
((((zcbor_uint32_expect_union(state, (8)))) &&
(zcbor_bstr_decode(state, (&(*result)._union_vd)))) &&
(((*result)._record_union_choice = _union_vd) || 1))),
zcbor_union_end_code(state), int_res))));
if (!tmp_result) if (!tmp_result)
zcbor_trace(); zcbor_trace();
@ -97,18 +81,21 @@ static bool decode_value(zcbor_state_t *state, struct value_ *result)
bool int_res; bool int_res;
bool tmp_result = bool tmp_result =
(((zcbor_union_start_code(state) && (((zcbor_union_start_code(state) && (int_res = (
(int_res = ((((zcbor_tstr_decode(state, (&(*result)._value_tstr)))) && (((zcbor_tstr_decode(state, (&(*result)._value_tstr)))) &&
(((*result)._value_choice = _value_tstr) || 1)) || (((*result)._value_choice = _value_tstr) || 1)) ||
(((zcbor_bstr_decode(state, (&(*result)._value_bstr)))) && (((zcbor_bstr_decode(state, (&(*result)._value_bstr)))) &&
(((*result)._value_choice = _value_bstr) || 1)) || (((*result)._value_choice = _value_bstr) || 1)) ||
(zcbor_union_elem_code(state) && (((zcbor_int64_decode(state, (&(*result)._value_int))) &&
(((decode_numeric(state, (&(*result)._value__numeric)))) && ((((*result)._value_int >= -9223372036854775807LL) &&
(((*result)._value_choice = _value__numeric) || 1))) || ((*result)._value_int <= 9223372036854775807LL)) ||
(zcbor_union_elem_code(state) && (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))) &&
(((zcbor_bool_decode(state, (&(*result)._value_bool)))) && (((*result)._value_choice = _value_int) || 1)) ||
(((*result)._value_choice = _value_bool) || 1)))), (((zcbor_float_decode(state, (&(*result)._value_float)))) &&
zcbor_union_end_code(state), int_res)))); (((*result)._value_choice = _value_float) || 1)) ||
(((zcbor_bool_decode(state, (&(*result)._value_bool)))) &&
(((*result)._value_choice = _value_bool) || 1))),
zcbor_union_end_code(state), int_res))));
if (!tmp_result) if (!tmp_result)
zcbor_trace(); zcbor_trace();
@ -163,7 +150,7 @@ static bool decode_record(zcbor_state_t *state, struct record *result)
state, (&(*result)._record__key_value_pair), state, (&(*result)._record__key_value_pair),
sizeof(struct record__key_value_pair))) || sizeof(struct record__key_value_pair))) ||
(zcbor_list_map_end_force_decode(state), false)) && (zcbor_list_map_end_force_decode(state), false)) &&
zcbor_map_end_decode(state)))); zcbor_map_end_decode(state))));
if (!tmp_result) if (!tmp_result)
zcbor_trace(); zcbor_trace();
@ -190,10 +177,10 @@ static bool decode_lwm2m_senml(zcbor_state_t *state, struct lwm2m_senml *result)
return tmp_result; return tmp_result;
} }
uint_fast8_t cbor_decode_lwm2m_senml(const uint8_t *payload, size_t payload_len, int cbor_decode_lwm2m_senml(const uint8_t *payload, size_t payload_len,
struct lwm2m_senml *result, size_t *payload_len_out) struct lwm2m_senml *result, size_t *payload_len_out)
{ {
zcbor_state_t states[6]; zcbor_state_t states[5];
zcbor_new_state(states, sizeof(states) / sizeof(zcbor_state_t), payload, payload_len, 1); zcbor_new_state(states, sizeof(states) / sizeof(zcbor_state_t), payload, payload_len, 1);
@ -204,7 +191,7 @@ uint_fast8_t cbor_decode_lwm2m_senml(const uint8_t *payload, size_t payload_len,
} }
if (!ret) { if (!ret) {
uint_fast8_t ret = zcbor_pop_error(states); int ret = zcbor_pop_error(states);
return (ret == ZCBOR_SUCCESS) ? ZCBOR_ERR_UNKNOWN : ret; return (ret == ZCBOR_SUCCESS) ? ZCBOR_ERR_UNKNOWN : ret;
} }

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
/* /*
* Generated using zcbor version 0.3.99 * Generated using zcbor version 0.4.0
* https://github.com/zephyrproject-rtos/zcbor * https://github.com/zephyrproject-rtos/zcbor
* Generated with a --default-max-qty of 99 * Generated with a --default-max-qty of 99
*/ */
@ -19,7 +19,7 @@
#include "zcbor_decode.h" #include "zcbor_decode.h"
#include "lwm2m_senml_cbor_types.h" #include "lwm2m_senml_cbor_types.h"
uint_fast8_t cbor_decode_lwm2m_senml(const uint8_t *payload, size_t payload_len, int cbor_decode_lwm2m_senml(const uint8_t *payload, size_t payload_len,
struct lwm2m_senml *result, size_t *payload_len_out); struct lwm2m_senml *result, size_t *payload_len_out);
#endif /* LWM2M_SENML_CBOR_DECODE_H__ */ #endif /* LWM2M_SENML_CBOR_DECODE_H__ */

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
/* /*
* Generated using zcbor version 0.3.99 * Generated using zcbor version 0.4.0
* https://github.com/zephyrproject-rtos/zcbor * https://github.com/zephyrproject-rtos/zcbor
* Generated with a --default-max-qty of 99 * Generated with a --default-max-qty of 99
*/ */
@ -43,45 +43,29 @@ static bool encode_repeated_record_n(zcbor_state_t *state, const struct record_n
return tmp_result; return tmp_result;
} }
static bool encode_numeric(zcbor_state_t *state, const struct numeric_ *input)
{
zcbor_print("%s\r\n", __func__);
bool tmp_result =
(((((*input)._numeric_choice == _numeric_int) ?
(((((*input)._numeric_int >= -9223372036854775807LL) &&
((*input)._numeric_int <= 9223372036854775807LL)) ||
(zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)) &&
(zcbor_int64_encode(state, (&(*input)._numeric_int)))) :
(((*input)._numeric_choice == _numeric_float) ?
((zcbor_float64_encode(state, (&(*input)._numeric_float)))) :
false))));
if (!tmp_result)
zcbor_trace();
return tmp_result;
}
static bool encode_repeated_record_union(zcbor_state_t *state, const struct record_union_ *input) static bool encode_repeated_record_union(zcbor_state_t *state, const struct record_union_ *input)
{ {
zcbor_print("%s\r\n", __func__); zcbor_print("%s\r\n", __func__);
bool tmp_result = bool tmp_result =
(((((*input)._record_union_choice == _union_v) ? (((((*input)._record_union_choice == _union_vi) ?
(((zcbor_uint32_put(state, (2)))) && (((zcbor_uint32_put(state, (2)))) &&
(encode_numeric(state, (&(*input)._union_v)))) : ((((*input)._union_vi >= -9223372036854775807LL) &&
(((*input)._record_union_choice == _union_vs) ? ((*input)._union_vi <= 9223372036854775807LL)) ||
(((zcbor_uint32_put(state, (3)))) && (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)) &&
(zcbor_tstr_encode(state, (&(*input)._union_vs)))) : (zcbor_int64_encode(state, (&(*input)._union_vi)))) :
(((*input)._record_union_choice == _union_vb) ? (((*input)._record_union_choice == _union_vf) ?
(((zcbor_uint32_put(state, (4)))) && (((zcbor_uint32_put(state, (2)))) &&
(zcbor_bool_encode(state, (&(*input)._union_vb)))) : (zcbor_float64_encode(state, (&(*input)._union_vf)))) :
(((*input)._record_union_choice == _union_vd) ? (((*input)._record_union_choice == _union_vs) ?
(((zcbor_uint32_put(state, (8)))) && (((zcbor_uint32_put(state, (3)))) &&
(zcbor_bstr_encode(state, (zcbor_tstr_encode(state, (&(*input)._union_vs)))) :
(&(*input)._union_vd)))) : (((*input)._record_union_choice == _union_vb) ?
false)))))); (((zcbor_uint32_put(state, (4)))) &&
(zcbor_bool_encode(state, (&(*input)._union_vb)))) :
(((*input)._record_union_choice == _union_vd) ?
(((zcbor_uint32_put(state, (8)))) &&
(zcbor_bstr_encode(state, (&(*input)._union_vd)))) : false)))))));
if (!tmp_result) if (!tmp_result)
zcbor_trace(); zcbor_trace();
@ -95,15 +79,18 @@ static bool encode_value(zcbor_state_t *state, const struct value_ *input)
bool tmp_result = (( bool tmp_result = ((
(((*input)._value_choice == _value_tstr) ? (((*input)._value_choice == _value_tstr) ?
((zcbor_tstr_encode(state, (&(*input)._value_tstr)))) : ((zcbor_tstr_encode(state, (&(*input)._value_tstr)))) :
(((*input)._value_choice == _value_bstr) ? (((*input)._value_choice == _value_bstr) ?
((zcbor_bstr_encode(state, (&(*input)._value_bstr)))) : ((zcbor_bstr_encode(state, (&(*input)._value_bstr)))) :
(((*input)._value_choice == _value__numeric) ? (((*input)._value_choice == _value_int) ?
((encode_numeric(state, (&(*input)._value__numeric)))) : (((((*input)._value_int >= -9223372036854775807LL) &&
(((*input)._value_choice == _value_bool) ? ((*input)._value_int <= 9223372036854775807LL)) ||
((zcbor_bool_encode(state, (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false)) &&
(&(*input)._value_bool)))) : (zcbor_int64_encode(state, (&(*input)._value_int)))) :
false)))))); (((*input)._value_choice == _value_float) ?
((zcbor_float64_encode(state, (&(*input)._value_float)))) :
(((*input)._value_choice == _value_bool) ?
((zcbor_bool_encode(state, (&(*input)._value_bool)))) : false)))))));
if (!tmp_result) if (!tmp_result)
zcbor_trace(); zcbor_trace();
@ -188,10 +175,10 @@ static bool encode_lwm2m_senml(zcbor_state_t *state, const struct lwm2m_senml *i
return tmp_result; return tmp_result;
} }
uint_fast8_t cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len, int cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len,
const struct lwm2m_senml *input, size_t *payload_len_out) const struct lwm2m_senml *input, size_t *payload_len_out)
{ {
zcbor_state_t states[6]; zcbor_state_t states[5];
zcbor_new_state(states, sizeof(states) / sizeof(zcbor_state_t), payload, payload_len, 1); zcbor_new_state(states, sizeof(states) / sizeof(zcbor_state_t), payload, payload_len, 1);
@ -202,7 +189,7 @@ uint_fast8_t cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len,
} }
if (!ret) { if (!ret) {
uint_fast8_t ret = zcbor_pop_error(states); int ret = zcbor_pop_error(states);
return (ret == ZCBOR_SUCCESS) ? ZCBOR_ERR_UNKNOWN : ret; return (ret == ZCBOR_SUCCESS) ? ZCBOR_ERR_UNKNOWN : ret;
} }

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
/* /*
* Generated using zcbor version 0.3.99 * Generated using zcbor version 0.4.0
* https://github.com/zephyrproject-rtos/zcbor * https://github.com/zephyrproject-rtos/zcbor
* Generated with a --default-max-qty of 99 * Generated with a --default-max-qty of 99
*/ */
@ -19,7 +19,7 @@
#include "zcbor_encode.h" #include "zcbor_encode.h"
#include "lwm2m_senml_cbor_types.h" #include "lwm2m_senml_cbor_types.h"
uint_fast8_t cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len, int cbor_encode_lwm2m_senml(uint8_t *payload, size_t payload_len,
const struct lwm2m_senml *input, size_t *payload_len_out); const struct lwm2m_senml *input, size_t *payload_len_out);
#endif /* LWM2M_SENML_CBOR_ENCODE_H__ */ #endif /* LWM2M_SENML_CBOR_ENCODE_H__ */

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
/* /*
* Generated using zcbor version 0.3.99 * Generated using zcbor version 0.4.0
* https://github.com/zephyrproject-rtos/zcbor * https://github.com/zephyrproject-rtos/zcbor
* Generated with a --default-max-qty of 99 * Generated with a --default-max-qty of 99
*/ */
@ -35,20 +35,13 @@ struct record_n {
struct zcbor_string _record_n; struct zcbor_string _record_n;
}; };
struct numeric_ {
union {
int64_t _numeric_int;
double _numeric_float;
};
enum { _numeric_int,
_numeric_float,
} _numeric_choice;
};
struct record_union_ { struct record_union_ {
union { union {
struct { struct {
struct numeric_ _union_v; int64_t _union_vi;
};
struct {
double _union_vf;
}; };
struct { struct {
struct zcbor_string _union_vs; struct zcbor_string _union_vs;
@ -60,10 +53,12 @@ struct record_union_ {
struct zcbor_string _union_vd; struct zcbor_string _union_vd;
}; };
}; };
enum { _union_v, enum {
_union_vs, _union_vi,
_union_vb, _union_vf,
_union_vd, _union_vs,
_union_vb,
_union_vd,
} _record_union_choice; } _record_union_choice;
}; };
@ -71,13 +66,16 @@ struct value_ {
union { union {
struct zcbor_string _value_tstr; struct zcbor_string _value_tstr;
struct zcbor_string _value_bstr; struct zcbor_string _value_bstr;
struct numeric_ _value__numeric; int64_t _value_int;
double _value_float;
bool _value_bool; bool _value_bool;
}; };
enum { _value_tstr, enum {
_value_bstr, _value_tstr,
_value__numeric, _value_bstr,
_value_bool, _value_int,
_value_float,
_value_bool,
} _value_choice; } _value_choice;
}; };