From 3896930be617fa3263a778486fcfdea46d70525c Mon Sep 17 00:00:00 2001 From: Ricardo Salveti Date: Fri, 28 Jul 2017 19:21:58 -0300 Subject: [PATCH] net: lwm2m: engine: add flag for separate response Separate response can happen when handling block transfer for firmware updates, and to avoid duplicating the lwm2m_udp_receive function, create and additional flag to allow handling CoAP separate response messages. This is required to avoid removing the reply callback, since a new message (with a valid token) will be received later from the server. Signed-off-by: Ricardo Salveti --- subsys/net/lib/lwm2m/lwm2m_engine.c | 22 ++++++++++++++++--- subsys/net/lib/lwm2m/lwm2m_engine.h | 1 + .../net/lib/lwm2m/lwm2m_obj_firmware_pull.c | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index 07146085328..b3b2bf0bb11 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -2154,6 +2154,7 @@ int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr) void lwm2m_udp_receive(struct net_context *ctx, struct net_pkt *pkt, struct zoap_pending *zpendings, int num_zpendings, struct zoap_reply *zreplies, int num_zreplies, + bool handle_separate_response, int (*udp_request_handler)(struct zoap_packet *, struct zoap_packet *, struct sockaddr *)) @@ -2256,8 +2257,23 @@ void lwm2m_udp_receive(struct net_context *ctx, struct net_pkt *pkt, SYS_LOG_ERR("No handler for response"); } } else { - SYS_LOG_DBG("reply handled reply:%p", reply); - zoap_reply_clear(reply); + /* + * Separate response is composed of 2 messages, empty ACK with + * no token and an additional message with a matching token id + * (based on the token used by the CON request). + * + * Since the ACK received by the notify CON messages are also + * empty with no token (consequence of always using the same + * token id for all notifications), we have to use an + * additional flag to decide when to clear the reply callback. + */ + if (handle_separate_response && !tkl && + zoap_header_get_type(&response) == ZOAP_TYPE_ACK) { + SYS_LOG_DBG("separated response, not removing reply"); + } else { + SYS_LOG_DBG("reply %p handled and removed", reply); + zoap_reply_clear(reply); + } } cleanup: @@ -2270,7 +2286,7 @@ static void udp_receive(struct net_context *ctx, struct net_pkt *pkt, int status, void *user_data) { lwm2m_udp_receive(ctx, pkt, pendings, NUM_PENDINGS, - replies, NUM_REPLIES, handle_request); + replies, NUM_REPLIES, false, handle_request); } static void retransmit_request(struct k_work *work) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.h b/subsys/net/lib/lwm2m/lwm2m_engine.h index c3a7fc7f1f9..7304e418294 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.h +++ b/subsys/net/lib/lwm2m/lwm2m_engine.h @@ -54,6 +54,7 @@ int lwm2m_udp_sendto(struct net_pkt *pkt, const struct sockaddr *dst_addr); void lwm2m_udp_receive(struct net_context *ctx, struct net_pkt *pkt, struct zoap_pending *zpendings, int num_zpendings, struct zoap_reply *zreplies, int num_zreplies, + bool handle_separate_response, int (*udp_request_handler)(struct zoap_packet *request, struct zoap_packet *response, struct sockaddr *from_addr)); diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c index 4d1d3f559ae..cf697efcb14 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_firmware_pull.c @@ -47,7 +47,7 @@ firmware_udp_receive(struct net_context *ctx, struct net_pkt *pkt, int status, void *user_data) { lwm2m_udp_receive(ctx, pkt, pendings, NUM_PENDINGS, - replies, NUM_REPLIES, NULL); + replies, NUM_REPLIES, true, NULL); } static void retransmit_request(struct k_work *work)