From db09596b1dba943938aef0e8f8733e9373f5a3e7 Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Tue, 30 Jan 2018 13:39:34 -0800 Subject: [PATCH] net: lwm2m: correct packet pending process in lwm2m_send_message() During the CoAP API change, the way packets were ref'd and then unref'd in order to stop the packet sending functions from releasing the net_pkts was changed and never updated in the LwM2M library. Let's use coap_pending_cycle() and coap_pending_clear() to do the ref/unref the same way as the coap-client samples in order to match the pending process with the current CoAP APIs. Signed-off-by: Michael Scott --- subsys/net/lib/lwm2m/lwm2m_engine.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index 4e259658590..60632afbe5a 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -1032,41 +1032,44 @@ cleanup: int lwm2m_send_message(struct lwm2m_message *msg) { int ret; - struct net_pkt *pkt; if (!msg || !msg->ctx) { SYS_LOG_ERR("LwM2M message is invalid."); return -EINVAL; } - /* protect the packet from being released inbetween net_app_send_pkt() - * to coap_pending_cycle() - */ - pkt = msg->cpkt.pkt; - net_pkt_ref(pkt); + if (msg->type == COAP_TYPE_CON) { + /* + * Increase packet ref count to avoid being unref after + * net_app_send_pkt() + */ + coap_pending_cycle(msg->pending); + } msg->send_attempts++; ret = net_app_send_pkt(&msg->ctx->net_app_ctx, msg->cpkt.pkt, &msg->ctx->net_app_ctx.default_ctx->remote, NET_SOCKADDR_MAX_SIZE, K_NO_WAIT, NULL); if (ret < 0) { - goto out; + if (msg->type == COAP_TYPE_CON) { + coap_pending_clear(msg->pending); + } + + return ret; } if (msg->type == COAP_TYPE_CON) { + /* don't re-queue the retransmit work on retransmits */ if (msg->send_attempts > 1) { - goto out; + return 0; } - coap_pending_cycle(msg->pending); k_delayed_work_submit(&msg->ctx->retransmit_work, msg->pending->timeout); } else { lwm2m_reset_message(msg, true); } -out: - net_pkt_unref(pkt); return ret; }