Skip to content

Commit 4f76b19

Browse files
Veijo Pesonencarlescufi
authored andcommitted
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 <[email protected]>
1 parent aa42e3e commit 4f76b19

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,29 @@ static int put_basename(struct lwm2m_output_context *out, struct lwm2m_obj_path
141141
return 0;
142142
}
143143

144+
static int put_empty_array(struct lwm2m_output_context *out)
145+
{
146+
int len = 1;
147+
148+
memset(CPKT_BUF_W_PTR(out->out_cpkt), 0x80, len); /* 80 # array(0) */
149+
out->out_cpkt->offset += len;
150+
151+
return len;
152+
}
153+
144154
static int put_end(struct lwm2m_output_context *out, struct lwm2m_obj_path *path)
145155
{
146156
size_t len;
157+
struct lwm2m_senml *input = &(LWM2M_OFD_CBOR(out)->input);
158+
159+
if (!input->_lwm2m_senml__record_count) {
160+
len = put_empty_array(out);
161+
162+
return len;
163+
}
147164

148165
uint_fast8_t ret =
149-
cbor_encode_lwm2m_senml(CPKT_BUF_W_REGION(out->out_cpkt),
150-
(struct lwm2m_senml *)&(LWM2M_OFD_CBOR(out)->input), &len);
166+
cbor_encode_lwm2m_senml(CPKT_BUF_W_REGION(out->out_cpkt), input, &len);
151167

152168
if (ret != ZCBOR_SUCCESS) {
153169
LOG_ERR("unable to encode senml cbor msg");

0 commit comments

Comments
 (0)