From 4f76b194a4b7f349964ae63b5ea8011ca3b0af80 Mon Sep 17 00:00:00 2001 From: Veijo Pesonen Date: Tue, 5 Apr 2022 14:31:51 +0300 Subject: [PATCH] net: lwm2m: empty CBOR array support Makes possible to write an empty CBOR array if there are no SenML CBOR records that needs to be written. This came up when trying to delete a portfolio object instance. Signed-off-by: Veijo Pesonen --- subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c b/subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c index 54436e48370..e8bdeadbaf3 100644 --- a/subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c +++ b/subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c @@ -141,13 +141,29 @@ static int put_basename(struct lwm2m_output_context *out, struct lwm2m_obj_path return 0; } +static int put_empty_array(struct lwm2m_output_context *out) +{ + int len = 1; + + memset(CPKT_BUF_W_PTR(out->out_cpkt), 0x80, len); /* 80 # array(0) */ + out->out_cpkt->offset += len; + + return len; +} + static int put_end(struct lwm2m_output_context *out, struct lwm2m_obj_path *path) { size_t len; + struct lwm2m_senml *input = &(LWM2M_OFD_CBOR(out)->input); + + if (!input->_lwm2m_senml__record_count) { + len = put_empty_array(out); + + return len; + } uint_fast8_t ret = - cbor_encode_lwm2m_senml(CPKT_BUF_W_REGION(out->out_cpkt), - (struct lwm2m_senml *)&(LWM2M_OFD_CBOR(out)->input), &len); + cbor_encode_lwm2m_senml(CPKT_BUF_W_REGION(out->out_cpkt), input, &len); if (ret != ZCBOR_SUCCESS) { LOG_ERR("unable to encode senml cbor msg");