From 5f92db3c12ea17bb0585ffb398cf28f2e05c9c76 Mon Sep 17 00:00:00 2001 From: Antti Yli-Tokola Date: Thu, 29 Aug 2019 13:34:24 +0300 Subject: [PATCH] Add a flag to maintain backward compatibility By default CoAP will create a copy of the whole data to be passed to application and it keeps the backward compatibility. If enabled, application must NOT free the payload when it gets the COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED status. And application must call sn_coap_protocol_block_remove() instead. --- features/frameworks/mbed-coap/CHANGELOG.md | 27 +++++++------------ .../mbed-coap/mbed-coap/sn_config.h | 10 +++++++ .../mbed-coap/source/sn_coap_protocol.c | 10 +++++++ 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/features/frameworks/mbed-coap/CHANGELOG.md b/features/frameworks/mbed-coap/CHANGELOG.md index d3735bc5eb..a7d2d68ddc 100644 --- a/features/frameworks/mbed-coap/CHANGELOG.md +++ b/features/frameworks/mbed-coap/CHANGELOG.md @@ -1,24 +1,18 @@ # Change Log +## [v5.1.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.1.0) + +- Introduce SN_COAP_REDUCE_BLOCKWISE_HEAP_FOOTPRINT configuration flag. + Flag is disabled by default to keep the backward compatibility in place. + If flag is enabled, application must NOT free the payload when it gets the COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED status. + And application must call sn_coap_protocol_block_remove() instead. + +-[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v5.0.0...v5.1.0) + ## [v5.0.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v5.0.0) - **NOTE! Blockwise functionality has changed and it is not backward compatible. User is now responsible of freeing the data by calling sn_coap_protocol_block_remove() and must NOT free the payload anymore separately.** - -Here is the change needed on application side: - -``` -if (received_coap_message->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED) { - free(received_coap_header->payload_ptr); -} - ---> - -if (received_coap_message->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED) { - // Free the block message from the CoAP list - sn_nsdl_remove_coap_block(_nsdl_handle, address, received_coap_header->payload_len, received_coap_header->payload_ptr); -} -``` - Reduce heap footprint by storing only single block when receiving a blockwise message. + * User is now responsible of freeing the data by calling sn_coap_protocol_block_remove() and must not free the payload separately. - Bug fix: Request blockwise transfer if incoming payload length is too large and when it comes without block indication. -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.8.1...v5.0.0) @@ -250,4 +244,3 @@ Extend blockwise message transfer status to have states for sending as well. **New feature** - Initial release of mbed-coap separated from mbed-client-c - diff --git a/features/frameworks/mbed-coap/mbed-coap/sn_config.h b/features/frameworks/mbed-coap/mbed-coap/sn_config.h index a71f854544..f04ffe80c7 100644 --- a/features/frameworks/mbed-coap/mbed-coap/sn_config.h +++ b/features/frameworks/mbed-coap/mbed-coap/sn_config.h @@ -260,4 +260,14 @@ #define SN_COAP_BLOCKWISE_INTERNAL_BLOCK_2_HANDLING_ENABLED 1 #endif +/** + * \def SN_COAP_REDUCE_BLOCKWISE_HEAP_FOOTPRINT + * \brief A heap optimization switch, which removes unnecessary copy of the blockwise data. + * If enabled, application must NOT free the payload when it gets the COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED status. + * Application must call sn_coap_protocol_block_remove() instead. + */ +#ifndef SN_COAP_REDUCE_BLOCKWISE_HEAP_FOOTPRINT +#define SN_COAP_REDUCE_BLOCKWISE_HEAP_FOOTPRINT 0 /**< Disabled by default */ +#endif + #endif // SN_CONFIG_H diff --git a/features/frameworks/mbed-coap/source/sn_coap_protocol.c b/features/frameworks/mbed-coap/source/sn_coap_protocol.c index e095fa4df4..aff7fff444 100644 --- a/features/frameworks/mbed-coap/source/sn_coap_protocol.c +++ b/features/frameworks/mbed-coap/source/sn_coap_protocol.c @@ -2293,8 +2293,18 @@ static bool sn_coap_handle_last_blockwise(struct coap_s *handle, const sn_nsdl_a if (!whole_payload_len) { return false; } + +#if SN_COAP_REDUCE_BLOCKWISE_HEAP_FOOTPRINT received_coap_msg_ptr->payload_ptr = payload_ptr; received_coap_msg_ptr->payload_len = whole_payload_len; +#else + received_coap_msg_ptr->payload_ptr = sn_coap_protocol_malloc_copy(handle, payload_ptr, whole_payload_len); + if (received_coap_msg_ptr->payload_ptr == NULL) { + tr_error("sn_coap_handle_last_blockwise - failed to allocate whole package!"); + return false; + } + received_coap_msg_ptr->payload_len = whole_payload_len; +#endif received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED; return true;