From 48a680359d171543d2fb3faa721f852cfbd6f5ae Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Wed, 10 Mar 2021 13:12:45 +0000 Subject: [PATCH] Block2: Fix ordering issue when calling coap_add_data_large_response() If the response code was set after calling coap_add_data_large_response(), the continuing blocks were sent with code 0.00. Now check following call to response handler that the correct code is held within the lg_xmit information. --- include/coap2/coap_block_internal.h | 12 ++++++++++ src/block.c | 37 +++++++++++++++++++---------- src/net.c | 3 +++ src/resource.c | 2 ++ 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/include/coap2/coap_block_internal.h b/include/coap2/coap_block_internal.h index 8ba2d19f..5ece4038 100644 --- a/include/coap2/coap_block_internal.h +++ b/include/coap2/coap_block_internal.h @@ -220,6 +220,18 @@ int coap_add_data_large_internal(struct coap_session_t *session, coap_release_large_data_t release_func, void *app_ptr); +/** + * The function checks that the code in a newly formed lg_xmit created by + * coap_add_data_large_response() is updated. + * + * @param session The session + * @param response The response PDU to to check + * @param resource The requested resource + * @param query The requested query + */ +void coap_check_code_lg_xmit(coap_session_t *session, coap_pdu_t *response, + coap_resource_t *resource, coap_string_t *query); + /** @} */ #endif /* COAP_BLOCK_INTERNAL_H_ */ diff --git a/src/block.c b/src/block.c index 0482d530..4f94af27 100644 --- a/src/block.c +++ b/src/block.c @@ -1265,8 +1265,6 @@ coap_handle_request_put_block(coap_context_t *context, } if (block_option) { coap_lg_srcv_t *p; - coap_lg_xmit_t *lg_xmit; - coap_string_t empty = { 0, NULL}; coap_opt_t *size_opt = coap_check_option(pdu, COAP_OPTION_SIZE1, &opt_iter); @@ -1393,16 +1391,7 @@ coap_handle_request_put_block(coap_context_t *context, /* Need to do this here as we need to free off p */ h(context, resource, session, pdu, token, query, response); /* Check if lg_xmit generated and update PDU code if so */ - LL_FOREACH(session->lg_xmit, lg_xmit) { - if (!COAP_PDU_IS_REQUEST(&lg_xmit->pdu) && - lg_xmit->b.b2.resource == resource && - coap_string_equal(query ? query : &empty, - lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty)) { - /* lg_xmit found */ - lg_xmit->pdu.code = response->code; - break; - } - } + coap_check_code_lg_xmit(session, response, resource, query); /* Last chunk - free off shortly */ coap_ticks(&p->last_used); goto skip_app_handler; @@ -1426,6 +1415,8 @@ coap_handle_request_put_block(coap_context_t *context, block.szx), buf); h(context, resource, session, pdu, token, query, response); + /* Check if lg_xmit generated and update PDU code if so */ + coap_check_code_lg_xmit(session, response, resource, query); if (COAP_RESPONSE_CLASS(response->code) == 2) { /* Just in case, as there are more to go */ response->code = COAP_RESPONSE_CODE(231); @@ -1974,3 +1965,25 @@ skip_app_handler: return 1; } +/* Check if lg_xmit generated and update PDU code if so */ +void +coap_check_code_lg_xmit(coap_session_t *session, coap_pdu_t *response, + coap_resource_t *resource, coap_string_t *query) { + coap_lg_xmit_t *lg_xmit; + coap_string_t empty = { 0, NULL}; + + if (response->code == 0) + return; + LL_FOREACH(session->lg_xmit, lg_xmit) { + if (!COAP_PDU_IS_REQUEST(&lg_xmit->pdu) && + lg_xmit->b.b2.resource == resource && + coap_string_equal(query ? query : &empty, + lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty)) { + /* lg_xmit found */ + if (lg_xmit->pdu.code == 0) { + lg_xmit->pdu.code = response->code; + return; + } + } + } +} diff --git a/src/net.c b/src/net.c index 312f93a7..59ddd3a1 100644 --- a/src/net.c +++ b/src/net.c @@ -2726,6 +2726,9 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu */ h(context, resource, session, pdu, &token, query, response); + /* Check if lg_xmit generated and update PDU code if so */ + coap_check_code_lg_xmit(session, response, resource, query); + skip_handler: respond = no_response(pdu, response, session); if (respond != RESPONSE_DROP) { diff --git a/src/resource.c b/src/resource.c index 49555a3b..abf03456 100644 --- a/src/resource.c +++ b/src/resource.c @@ -915,6 +915,8 @@ coap_notify_observers(coap_context_t *context, coap_resource_t *r, assert(h); /* we do not allow subscriptions if no * GET/FETCH handler is defined */ h(context, r, obs->session, NULL, &token, obs->query, response); + /* Check if lg_xmit generated and update PDU code if so */ + coap_check_code_lg_xmit(obs->session, response, r, obs->query); if (COAP_RESPONSE_CLASS(response->code) > 2) { coap_delete_observer(r, obs->session, &token); }