From 7ba7f7e8d2bed08c9294a85765f17affaaf534cb Mon Sep 17 00:00:00 2001 From: Antti Yli-Tokola Date: Wed, 21 Aug 2019 09:30:09 +0300 Subject: [PATCH] Update mbed-coap to version 4.8.1 - Store ACK's also into duplicate info list. - ROM size optimization. Flash size has gone down ~1100 bytes. **Closed issues:** - IOTCLT-3592 - Client does not handle Duplicate ACK messages during blockwise registration correctly --- features/frameworks/mbed-coap/CHANGELOG.md | 9 + features/frameworks/mbed-coap/Makefile.test | 2 +- .../mbed-coap/mbed-coap/sn_coap_header.h | 12 +- .../mbed-coap/mbed-coap/sn_coap_protocol.h | 5 +- .../mbed-coap/mbed-coap/sn_config.h | 10 + .../source/include/sn_coap_header_internal.h | 4 +- .../include/sn_coap_protocol_internal.h | 12 +- .../mbed-coap/source/sn_coap_builder.c | 349 +++--- .../mbed-coap/source/sn_coap_header_check.c | 2 +- .../mbed-coap/source/sn_coap_parser.c | 125 +- .../mbed-coap/source/sn_coap_protocol.c | 1023 +++++++---------- 11 files changed, 717 insertions(+), 836 deletions(-) diff --git a/features/frameworks/mbed-coap/CHANGELOG.md b/features/frameworks/mbed-coap/CHANGELOG.md index faa56179a0..1b01fc89fb 100644 --- a/features/frameworks/mbed-coap/CHANGELOG.md +++ b/features/frameworks/mbed-coap/CHANGELOG.md @@ -1,5 +1,14 @@ # Change Log +## [v4.8.1(https://github.com/ARMmbed/mbed-coap/releases/tag/v4.8.1) +- Store ACK's also into duplicate info list. +- ROM size optimization. Flash size has gone down ~1100 bytes. + +**Closed issues:** +- IOTCLT-3592 - Client does not handle Duplicate ACK messages during blockwise registration correctly + +-[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.8.0...v4.8.1) + ## [v4.8.0](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.8.0) - Make `sn_coap_protocol_linked_list_duplication_info_remove` API to public. User might want to delete some messages from the duplicate list. - Enable support for unified client configuration. diff --git a/features/frameworks/mbed-coap/Makefile.test b/features/frameworks/mbed-coap/Makefile.test index 78cf60fbbc..37f8b5fbf3 100644 --- a/features/frameworks/mbed-coap/Makefile.test +++ b/features/frameworks/mbed-coap/Makefile.test @@ -49,7 +49,7 @@ test: $(TESTDIRS) @rm -f lcov/index.xml @find ./ -name '*.gcno' | xargs cp --backup=numbered -t ./coverage/ @find ./ -name '*.gcda' | xargs cp --backup=numbered -t ./coverage/ - @gcovr --object-directory ./coverage --exclude-unreachable-branches -e '.*/builds/.*' -e '.*/test/.*' -e '.*/stubs/.*' -e '.*/mbed-coap/.*' -x -o ./lcov/gcovr.xml + @gcovr --object-directory ./coverage --exclude-unreachable-branches -e '.*/builds/.*' -e '.*/CppUTest/.*' -e '.*/mbed-client-libservice/.*' -e '.*/test/.*' -e '.*/stubs/.*' -e '.*/mbed-coap/.*' -x -o ./lcov/gcovr.xml @lcov -d test/. -c -o $(COVERAGEFILE) @lcov -q -r $(COVERAGEFILE) "/usr*" -o $(COVERAGEFILE) @lcov -q -r $(COVERAGEFILE) "/test*" -o $(COVERAGEFILE) diff --git a/features/frameworks/mbed-coap/mbed-coap/sn_coap_header.h b/features/frameworks/mbed-coap/mbed-coap/sn_coap_header.h index ed1e67bceb..4617bc789b 100644 --- a/features/frameworks/mbed-coap/mbed-coap/sn_coap_header.h +++ b/features/frameworks/mbed-coap/mbed-coap/sn_coap_header.h @@ -338,7 +338,7 @@ extern void sn_coap_parser_release_allocated_coap_msg_mem(struct coap_s *handle, * -1 = Failure in given CoAP header structure\n * -2 = Failure in given pointer (= NULL) */ -extern int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr); +extern int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, const sn_coap_hdr_s *src_coap_msg_ptr); /** * \fn uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr) @@ -351,7 +351,7 @@ extern int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_ * \return Return value is count of needed memory as bytes for build Packet data * Null if failed */ -extern uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr); +extern uint16_t sn_coap_builder_calc_needed_packet_data_size(const sn_coap_hdr_s *src_coap_msg_ptr); /** * \fn int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_size) @@ -368,7 +368,7 @@ extern uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_ * -1 = Failure in given CoAP header structure\n * -2 = Failure in given pointer (= NULL) */ -extern int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size); +extern int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, const sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size); /** * \fn uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size) @@ -382,7 +382,7 @@ extern int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *sr * \return Return value is count of needed memory as bytes for build Packet data * Null if failed */ -extern uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size); +extern uint16_t sn_coap_builder_calc_needed_packet_data_size_2(const sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size); /** * \fn sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code) @@ -397,7 +397,7 @@ extern uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *sr * NULL Error in parsing the request * */ -extern sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code); +extern sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, const sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code); /** * \brief Initialise a message structure to empty @@ -436,6 +436,8 @@ extern sn_coap_hdr_s *sn_coap_parser_alloc_message(struct coap_s *handle); */ extern sn_coap_options_list_s *sn_coap_parser_alloc_options(struct coap_s *handle, sn_coap_hdr_s *coap_msg_ptr); +extern sn_coap_hdr_s *sn_coap_parser_alloc_message_with_options(struct coap_s *handle); + #ifdef __cplusplus } #endif diff --git a/features/frameworks/mbed-coap/mbed-coap/sn_coap_protocol.h b/features/frameworks/mbed-coap/mbed-coap/sn_coap_protocol.h index c3584cf7c8..456947b29f 100644 --- a/features/frameworks/mbed-coap/mbed-coap/sn_coap_protocol.h +++ b/features/frameworks/mbed-coap/mbed-coap/sn_coap_protocol.h @@ -38,7 +38,8 @@ extern "C" { * * \param *used_malloc_func_ptr is function pointer for used memory allocation function. * - * \param *used_free_func_ptr is function pointer for used memory free function. + * \param *used_free_func_ptr is function pointer for used memory free function. Note: the implementation + * must handle NULL parameter and ignore it just as typical libc's free() does. * * \param *used_tx_callback_ptr function callback pointer to tx function for sending coap messages * @@ -234,7 +235,7 @@ extern int8_t sn_coap_protocol_delete_retransmission(struct coap_s *handle, uint * * \brief If re-transmissions are enabled, this function removes message from retransmission buffer. */ -extern int8_t sn_coap_protocol_delete_retransmission_by_token(struct coap_s *handle, uint8_t *token, uint8_t token_len); +extern int8_t sn_coap_protocol_delete_retransmission_by_token(struct coap_s *handle, const uint8_t *token, uint8_t token_len); /** * \fn int8_t sn_coap_convert_block_size(uint16_t block_size) diff --git a/features/frameworks/mbed-coap/mbed-coap/sn_config.h b/features/frameworks/mbed-coap/mbed-coap/sn_config.h index 36fa59e3ba..a71f854544 100644 --- a/features/frameworks/mbed-coap/mbed-coap/sn_config.h +++ b/features/frameworks/mbed-coap/mbed-coap/sn_config.h @@ -250,4 +250,14 @@ #define SN_COAP_MAX_INCOMING_BLOCK_MESSAGE_SIZE UINT16_MAX #endif +/** + * \def SN_COAP_BLOCKWISE_INTERNAL_BLOCK_2_HANDLING_ENABLED + * \brief A size optimization switch, which removes the blockwise Block2 response if set to 0. + * handling code which is typically overridden by a call of "sn_coap_protocol_handle_block2_response_internally(coap, false);". + * By default the code is there, so the override can be reversed by "sn_coap_protocol_handle_block2_response_internally(coap, true)". + */ +#ifndef SN_COAP_BLOCKWISE_INTERNAL_BLOCK_2_HANDLING_ENABLED +#define SN_COAP_BLOCKWISE_INTERNAL_BLOCK_2_HANDLING_ENABLED 1 +#endif + #endif // SN_CONFIG_H diff --git a/features/frameworks/mbed-coap/source/include/sn_coap_header_internal.h b/features/frameworks/mbed-coap/source/include/sn_coap_header_internal.h index f00dd7d7cd..ef62e1742e 100644 --- a/features/frameworks/mbed-coap/source/include/sn_coap_header_internal.h +++ b/features/frameworks/mbed-coap/source/include/sn_coap_header_internal.h @@ -58,7 +58,7 @@ extern "C" { * \brief This structure is returned by sn_coap_exec() for sending */ typedef struct sn_nsdl_transmit_ { - sn_nsdl_addr_s *dst_addr_ptr; + sn_nsdl_addr_s dst_addr_ptr; sn_nsdl_capab_e protocol; @@ -69,7 +69,7 @@ typedef struct sn_nsdl_transmit_ { /* * * * * * * * * * * * * * * * * * * * * * */ /* * * * EXTERNAL FUNCTION PROTOTYPES * * * */ /* * * * * * * * * * * * * * * * * * * * * * */ -extern int8_t sn_coap_header_validity_check(sn_coap_hdr_s *src_coap_msg_ptr, coap_version_e coap_version); +extern int8_t sn_coap_header_validity_check(const sn_coap_hdr_s *src_coap_msg_ptr, coap_version_e coap_version); #endif /* SN_COAP_HEADER_INTERNAL_H_ */ diff --git a/features/frameworks/mbed-coap/source/include/sn_coap_protocol_internal.h b/features/frameworks/mbed-coap/source/include/sn_coap_protocol_internal.h index 3654684517..1592df55b4 100644 --- a/features/frameworks/mbed-coap/source/include/sn_coap_protocol_internal.h +++ b/features/frameworks/mbed-coap/source/include/sn_coap_protocol_internal.h @@ -48,9 +48,8 @@ typedef struct coap_send_msg_ { uint8_t resending_counter; /* Tells how many times message is still tried to resend */ uint32_t resending_time; /* Tells next resending time */ - sn_nsdl_transmit_s *send_msg_ptr; + sn_nsdl_transmit_s send_msg_ptr; - struct coap_s *coap; /* CoAP library handle */ void *param; /* Extra parameter that will be passed to TX/RX callback functions */ ns_list_link_t link; @@ -64,7 +63,6 @@ typedef struct coap_duplication_info_ { uint16_t msg_id; uint16_t packet_len; uint8_t *packet_ptr; - struct coap_s *coap; /* CoAP library handle */ sn_nsdl_addr_s *address; void *param; ns_list_link_t link; @@ -77,7 +75,6 @@ typedef struct coap_blockwise_msg_ { uint32_t timestamp; /* Tells when Blockwise message is stored to Linked list */ sn_coap_hdr_s *coap_msg_ptr; - struct coap_s *coap; /* CoAP library handle */ void *param; uint16_t msg_id; @@ -100,7 +97,6 @@ typedef struct coap_blockwise_payload_ { uint16_t payload_len; uint8_t *payload_ptr; - struct coap_s *coap; /* CoAP library handle */ ns_list_link_t link; } coap_blockwise_payload_s; @@ -139,6 +135,12 @@ struct coap_s { uint8_t sn_coap_internal_block2_resp_handling; /* If this is set then coap itself sends a next GET request automatically */ }; +/* Utility function which performs a call to sn_coap_protocol_malloc() and memset's the result to zero. */ +void *sn_coap_protocol_calloc(struct coap_s *handle, uint16_t length); + +/* Utility function which performs a call to sn_coap_protocol_malloc() and memcopy's the source to result buffer. */ +void *sn_coap_protocol_malloc_copy(struct coap_s *handle, const void *source, uint16_t length); + #ifdef __cplusplus } #endif diff --git a/features/frameworks/mbed-coap/source/sn_coap_builder.c b/features/frameworks/mbed-coap/source/sn_coap_builder.c index d663efb40f..d34edb0478 100644 --- a/features/frameworks/mbed-coap/source/sn_coap_builder.c +++ b/features/frameworks/mbed-coap/source/sn_coap_builder.c @@ -37,19 +37,19 @@ #define TRACE_GROUP "coap" /* * * * LOCAL FUNCTION PROTOTYPES * * * */ -static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr); -static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr); -static uint16_t sn_coap_builder_options_calc_option_size(uint16_t query_len, uint8_t *query_ptr, sn_coap_option_numbers_e option); -static int16_t sn_coap_builder_options_build_add_one_option(uint8_t **dst_packet_data_pptr, uint16_t option_len, uint8_t *option_ptr, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number); -static int16_t sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_packet_data_pptr, uint8_t **src_pptr, uint16_t *src_len_ptr, sn_coap_option_numbers_e option, uint16_t *previous_option_number); +static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr); +static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr); +static uint16_t sn_coap_builder_options_calc_option_size(uint16_t query_len, const uint8_t *query_ptr, sn_coap_option_numbers_e option); +static int16_t sn_coap_builder_options_build_add_one_option(uint8_t **dst_packet_data_pptr, uint16_t option_len, const uint8_t *option_ptr, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number); +static void sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_packet_data_pptr, const uint8_t *src_pptr, uint16_t src_len_ptr, sn_coap_option_numbers_e option, uint16_t *previous_option_number); static uint8_t sn_coap_builder_options_build_add_uint_option(uint8_t **dst_packet_data_pptr, uint32_t value, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number); -static uint8_t sn_coap_builder_options_get_option_part_count(uint16_t query_len, uint8_t *query_ptr, sn_coap_option_numbers_e option); -static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option_string(uint16_t query_len, uint8_t *query_ptr, uint8_t query_index, sn_coap_option_numbers_e option); -static int16_t sn_coap_builder_options_get_option_part_position(uint16_t query_len, uint8_t *query_ptr, uint8_t query_index, sn_coap_option_numbers_e option); -static void sn_coap_builder_payload_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr); -static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_coap_msg_ptr/*, uint8_t block_option*/); +static uint8_t sn_coap_builder_options_get_option_part_count(uint16_t query_len, const uint8_t *query_ptr, sn_coap_option_numbers_e option); +static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option_string(uint16_t query_len, const uint8_t *query_ptr, uint8_t query_index, sn_coap_option_numbers_e option); +static int16_t sn_coap_builder_options_get_option_part_position(uint16_t query_len, const uint8_t *query_ptr, uint8_t query_index, sn_coap_option_numbers_e option); +static void sn_coap_builder_payload_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr); +static uint8_t sn_coap_builder_options_calculate_jump_need(const sn_coap_hdr_s *src_coap_msg_ptr); -sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code) +sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, const sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code) { sn_coap_hdr_s *coap_res_ptr; @@ -86,25 +86,24 @@ sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap if (coap_packet_ptr->token_ptr) { coap_res_ptr->token_len = coap_packet_ptr->token_len; - coap_res_ptr->token_ptr = handle->sn_coap_protocol_malloc(coap_res_ptr->token_len); + coap_res_ptr->token_ptr = sn_coap_protocol_malloc_copy(handle, coap_packet_ptr->token_ptr, coap_res_ptr->token_len); if (!coap_res_ptr->token_ptr) { tr_error("sn_coap_build_response - failed to allocate token!"); handle->sn_coap_protocol_free(coap_res_ptr); return NULL; } - memcpy(coap_res_ptr->token_ptr, coap_packet_ptr->token_ptr, coap_res_ptr->token_len); } return coap_res_ptr; } -int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr) +int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, const sn_coap_hdr_s *src_coap_msg_ptr) { return sn_coap_builder_2(dst_packet_data_ptr, src_coap_msg_ptr, SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE); } -int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size) +int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, const sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size) { - uint8_t *base_packet_data_ptr = NULL; + uint8_t *base_packet_data_ptr; /* * * * Check given pointers * * * */ if (dst_packet_data_ptr == NULL || src_coap_msg_ptr == NULL) { @@ -118,6 +117,7 @@ int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_ return -1; } + // XXX: this should not be needed anymore but I have no courage to remove it yet. memset(dst_packet_data_ptr, 0, dst_byte_count_to_be_built); /* * * * Store base (= original) destination Packet data pointer for later usage * * * */ @@ -147,12 +147,12 @@ int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_ /* * * * Return built Packet data length * * * */ return (dst_packet_data_ptr - base_packet_data_ptr); } -uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr) +uint16_t sn_coap_builder_calc_needed_packet_data_size(const sn_coap_hdr_s *src_coap_msg_ptr) { return sn_coap_builder_calc_needed_packet_data_size_2(src_coap_msg_ptr, SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE); } -uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size) +uint16_t sn_coap_builder_calc_needed_packet_data_size_2(const sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size) { (void)blockwise_payload_size; uint16_t returned_byte_count = 0; @@ -203,30 +203,33 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_ } /* If options list pointer exists */ if (src_coap_msg_ptr->options_list_ptr != NULL) { + + const sn_coap_options_list_s *src_options_list_ptr = src_coap_msg_ptr->options_list_ptr; + /* ACCEPT - An integer option, up to 2 bytes */ - if (src_coap_msg_ptr->options_list_ptr->accept != COAP_CT_NONE) { - if ((uint32_t) src_coap_msg_ptr->options_list_ptr->accept > 0xffff) { + if (src_options_list_ptr->accept != COAP_CT_NONE) { + if ((uint32_t) src_options_list_ptr->accept > 0xffff) { tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - accept too large!"); return 0; } - returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->accept, COAP_OPTION_ACCEPT, &tempInt); + returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->accept, COAP_OPTION_ACCEPT, &tempInt); } /* MAX AGE - An integer option, omitted for default. Up to 4 bytes */ - if (src_coap_msg_ptr->options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) { - returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->max_age, COAP_OPTION_MAX_AGE, &tempInt); + if (src_options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) { + returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->max_age, COAP_OPTION_MAX_AGE, &tempInt); } /* PROXY URI - Length of this option is 1-1034 bytes */ - if (src_coap_msg_ptr->options_list_ptr->proxy_uri_ptr != NULL) { - if (src_coap_msg_ptr->options_list_ptr->proxy_uri_len >= 1 && src_coap_msg_ptr->options_list_ptr->proxy_uri_len <= 12) { /* Add option header byte(s) - depending of option length */ + if (src_options_list_ptr->proxy_uri_ptr != NULL) { + if (src_options_list_ptr->proxy_uri_len >= 1 && src_options_list_ptr->proxy_uri_len <= 12) { /* Add option header byte(s) - depending of option length */ returned_byte_count++; } - else if (src_coap_msg_ptr->options_list_ptr->proxy_uri_len >= 13 && src_coap_msg_ptr->options_list_ptr->proxy_uri_len <= 269) { + else if (src_options_list_ptr->proxy_uri_len >= 13 && src_options_list_ptr->proxy_uri_len <= 269) { returned_byte_count += 2; } - else if (src_coap_msg_ptr->options_list_ptr->proxy_uri_len >= 270 && src_coap_msg_ptr->options_list_ptr->proxy_uri_len <= 1034) { + else if (src_options_list_ptr->proxy_uri_len >= 270 && src_options_list_ptr->proxy_uri_len <= 1034) { returned_byte_count += 3; } @@ -236,12 +239,12 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_ } /* Add needed memory for Option value */ - returned_byte_count += src_coap_msg_ptr->options_list_ptr->proxy_uri_len; + returned_byte_count += src_options_list_ptr->proxy_uri_len; } /* ETAG - Repeatable option. Length of this option is 1-8 bytes*/ - if (src_coap_msg_ptr->options_list_ptr->etag_ptr != NULL) { - repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->options_list_ptr->etag_len, - src_coap_msg_ptr->options_list_ptr->etag_ptr, COAP_OPTION_ETAG); + if (src_options_list_ptr->etag_ptr != NULL) { + repeatable_option_size = sn_coap_builder_options_calc_option_size(src_options_list_ptr->etag_len, + src_options_list_ptr->etag_ptr, COAP_OPTION_ETAG); if (repeatable_option_size) { returned_byte_count += repeatable_option_size; } else { @@ -250,12 +253,12 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_ } } /* URI HOST - Length of this option is 1-255 bytes */ - if (src_coap_msg_ptr->options_list_ptr->uri_host_ptr != NULL) { - if (src_coap_msg_ptr->options_list_ptr->uri_host_len > 0 && src_coap_msg_ptr->options_list_ptr->uri_host_len <= 12) { + if (src_options_list_ptr->uri_host_ptr != NULL) { + if (src_options_list_ptr->uri_host_len > 0 && src_options_list_ptr->uri_host_len <= 12) { returned_byte_count++; } - else if (src_coap_msg_ptr->options_list_ptr->uri_host_len >= 13 && src_coap_msg_ptr->options_list_ptr->uri_host_len <= 255) { + else if (src_options_list_ptr->uri_host_len >= 13 && src_options_list_ptr->uri_host_len <= 255) { returned_byte_count += 2; } @@ -264,12 +267,12 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_ return 0; } - returned_byte_count += src_coap_msg_ptr->options_list_ptr->uri_host_len; + returned_byte_count += src_options_list_ptr->uri_host_len; } /* LOCATION PATH - Repeatable option. Length of this option is 0-255 bytes*/ - if (src_coap_msg_ptr->options_list_ptr->location_path_ptr != NULL) { - repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->options_list_ptr->location_path_len, - src_coap_msg_ptr->options_list_ptr->location_path_ptr, COAP_OPTION_LOCATION_PATH); + if (src_options_list_ptr->location_path_ptr != NULL) { + repeatable_option_size = sn_coap_builder_options_calc_option_size(src_options_list_ptr->location_path_len, + src_options_list_ptr->location_path_ptr, COAP_OPTION_LOCATION_PATH); if (repeatable_option_size) { returned_byte_count += repeatable_option_size; } else { @@ -278,17 +281,17 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_ } } /* URI PORT - An integer option, up to 2 bytes */ - if (src_coap_msg_ptr->options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) { - if ((uint32_t) src_coap_msg_ptr->options_list_ptr->uri_port > 0xffff) { + if (src_options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) { + if ((uint32_t) src_options_list_ptr->uri_port > 0xffff) { tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - uri port too large!"); return 0; } - returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->uri_port, COAP_OPTION_URI_PORT, &tempInt); + returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->uri_port, COAP_OPTION_URI_PORT, &tempInt); } /* lOCATION QUERY - Repeatable option. Length of this option is 0-255 bytes */ - if (src_coap_msg_ptr->options_list_ptr->location_query_ptr != NULL) { - repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->options_list_ptr->location_query_len, - src_coap_msg_ptr->options_list_ptr->location_query_ptr, COAP_OPTION_LOCATION_QUERY); + if (src_options_list_ptr->location_query_ptr != NULL) { + repeatable_option_size = sn_coap_builder_options_calc_option_size(src_options_list_ptr->location_query_len, + src_options_list_ptr->location_query_ptr, COAP_OPTION_LOCATION_QUERY); if (repeatable_option_size) { returned_byte_count += repeatable_option_size; } else { @@ -297,16 +300,16 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_ } } /* OBSERVE - An integer option, up to 3 bytes */ - if (src_coap_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) { - if ((uint32_t) src_coap_msg_ptr->options_list_ptr->observe > 0xffffff) { + if (src_options_list_ptr->observe != COAP_OBSERVE_NONE) { + if ((uint32_t) src_options_list_ptr->observe > 0xffffff) { return 0; } - returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->observe, COAP_OPTION_OBSERVE, &tempInt); + returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->observe, COAP_OPTION_OBSERVE, &tempInt); } /* URI QUERY - Repeatable option. Length of this option is 1-255 */ - if (src_coap_msg_ptr->options_list_ptr->uri_query_ptr != NULL) { - repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->options_list_ptr->uri_query_len, - src_coap_msg_ptr->options_list_ptr->uri_query_ptr, COAP_OPTION_URI_QUERY); + if (src_options_list_ptr->uri_query_ptr != NULL) { + repeatable_option_size = sn_coap_builder_options_calc_option_size(src_options_list_ptr->uri_query_len, + src_options_list_ptr->uri_query_ptr, COAP_OPTION_URI_QUERY); if (repeatable_option_size) { returned_byte_count += repeatable_option_size; } else { @@ -316,28 +319,28 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_ } /* BLOCK 1 - An integer option, up to 3 bytes */ - if (src_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) { - if ((uint32_t) src_coap_msg_ptr->options_list_ptr->block1 > 0xffffff) { + if (src_options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) { + if ((uint32_t) src_options_list_ptr->block1 > 0xffffff) { tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - block1 too large!"); return 0; } - returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->block1, COAP_OPTION_BLOCK1, &tempInt); + returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->block1, COAP_OPTION_BLOCK1, &tempInt); } /* SIZE1 - Length of this option is 0-4 bytes */ - if (src_coap_msg_ptr->options_list_ptr->use_size1) { - returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->size1, COAP_OPTION_SIZE1, &tempInt); + if (src_options_list_ptr->use_size1) { + returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->size1, COAP_OPTION_SIZE1, &tempInt); } /* BLOCK 2 - An integer option, up to 3 bytes */ - if (src_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) { - if ((uint32_t) src_coap_msg_ptr->options_list_ptr->block2 > 0xffffff) { + if (src_options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) { + if ((uint32_t) src_options_list_ptr->block2 > 0xffffff) { tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - block2 too large!"); return 0; } - returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->block2, COAP_OPTION_BLOCK2, &tempInt); + returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->block2, COAP_OPTION_BLOCK2, &tempInt); } /* SIZE2 - Length of this option is 0-4 bytes */ if (src_coap_msg_ptr->options_list_ptr->use_size2) { - returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->size2, COAP_OPTION_SIZE2, &tempInt); + returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->size2, COAP_OPTION_SIZE2, &tempInt); } } #if SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE @@ -354,12 +357,12 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_ if (src_coap_msg_ptr->payload_len) { returned_byte_count ++; /* For payload marker */ } - returned_byte_count += sn_coap_builder_options_calculate_jump_need(src_coap_msg_ptr/*, 0*/); + returned_byte_count += sn_coap_builder_options_calculate_jump_need(src_coap_msg_ptr); } return returned_byte_count; } /** - * \fn static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_coap_msg_ptr, uint8_t block_option) + * \fn static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_coap_msg_ptr) * * \brief Checks if there is need for option jump * @@ -370,43 +373,47 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_ * \return Returns bytes needed for jumping */ -static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_coap_msg_ptr/*, uint8_t block_option*/) +static uint8_t sn_coap_builder_options_calculate_jump_need(const sn_coap_hdr_s *src_coap_msg_ptr) { uint8_t previous_option_number = 0; uint8_t needed_space = 0; - if (src_coap_msg_ptr->options_list_ptr != NULL) { + const sn_coap_options_list_s* options_list_ptr = src_coap_msg_ptr->options_list_ptr; + + if (options_list_ptr != NULL) { + + /* If option numbers greater than 12 is not used, then jumping is not needed */ //TODO: Check if this is really needed! Does it enhance perf? If not -> remove - if (!src_coap_msg_ptr->options_list_ptr->uri_query_ptr && - src_coap_msg_ptr->options_list_ptr->accept == COAP_CT_NONE && - !src_coap_msg_ptr->options_list_ptr->location_query_ptr && - src_coap_msg_ptr->options_list_ptr->block2 == COAP_OPTION_BLOCK_NONE && - src_coap_msg_ptr->options_list_ptr->block1 == COAP_OPTION_BLOCK_NONE && - !src_coap_msg_ptr->options_list_ptr->proxy_uri_ptr && - src_coap_msg_ptr->options_list_ptr->max_age == COAP_OPTION_MAX_AGE_DEFAULT && - !src_coap_msg_ptr->options_list_ptr->use_size1 && - !src_coap_msg_ptr->options_list_ptr->use_size2) { + if (!options_list_ptr->uri_query_ptr && + options_list_ptr->accept == COAP_CT_NONE && + !options_list_ptr->location_query_ptr && + options_list_ptr->block2 == COAP_OPTION_BLOCK_NONE && + options_list_ptr->block1 == COAP_OPTION_BLOCK_NONE && + !options_list_ptr->proxy_uri_ptr && + options_list_ptr->max_age == COAP_OPTION_MAX_AGE_DEFAULT && + !options_list_ptr->use_size1 && + !options_list_ptr->use_size2) { return 0; } - if (src_coap_msg_ptr->options_list_ptr->uri_host_ptr != NULL) { + if (options_list_ptr->uri_host_ptr != NULL) { previous_option_number = (COAP_OPTION_URI_HOST); } - if (src_coap_msg_ptr->options_list_ptr->etag_ptr != NULL) { + if (options_list_ptr->etag_ptr != NULL) { previous_option_number = (COAP_OPTION_ETAG); } - if (src_coap_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) { + if (options_list_ptr->observe != COAP_OBSERVE_NONE) { previous_option_number = (COAP_OPTION_OBSERVE); } - if (src_coap_msg_ptr->options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) { + if (options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) { previous_option_number = (COAP_OPTION_URI_PORT); } - if (src_coap_msg_ptr->options_list_ptr->location_path_ptr != NULL) { + if (options_list_ptr->location_path_ptr != NULL) { previous_option_number = (COAP_OPTION_LOCATION_PATH); } @@ -416,50 +423,51 @@ static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_co if (src_coap_msg_ptr->content_format != COAP_CT_NONE) { previous_option_number = (COAP_OPTION_CONTENT_FORMAT); } - if (src_coap_msg_ptr->options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) { + + if (options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) { if ((COAP_OPTION_MAX_AGE - previous_option_number) > 12) { needed_space += 1; } previous_option_number = (COAP_OPTION_MAX_AGE); } - if (src_coap_msg_ptr->options_list_ptr->uri_query_ptr != NULL) { + if (options_list_ptr->uri_query_ptr != NULL) { if ((COAP_OPTION_URI_QUERY - previous_option_number) > 12) { needed_space += 1; } previous_option_number = (COAP_OPTION_URI_QUERY); } - if (src_coap_msg_ptr->options_list_ptr->accept != COAP_CT_NONE) { + if (options_list_ptr->accept != COAP_CT_NONE) { if ((COAP_OPTION_ACCEPT - previous_option_number) > 12) { needed_space += 1; } previous_option_number = (COAP_OPTION_ACCEPT); } - if (src_coap_msg_ptr->options_list_ptr->location_query_ptr != NULL) { + if (options_list_ptr->location_query_ptr != NULL) { if ((COAP_OPTION_LOCATION_QUERY - previous_option_number) > 12) { needed_space += 1; } previous_option_number = (COAP_OPTION_LOCATION_QUERY); } - if (src_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) { + if (options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) { if ((COAP_OPTION_BLOCK2 - previous_option_number) > 12 ){ needed_space += 1; } previous_option_number = (COAP_OPTION_BLOCK2); } - if (src_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) { + if (options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) { if ((COAP_OPTION_BLOCK1 - previous_option_number) > 12 ){ needed_space += 1; } previous_option_number = (COAP_OPTION_BLOCK1); } - if (src_coap_msg_ptr->options_list_ptr->use_size2) { + if (options_list_ptr->use_size2) { if ((COAP_OPTION_SIZE2 - previous_option_number) > 12) { needed_space += 1; } previous_option_number = (COAP_OPTION_SIZE2); } - if (src_coap_msg_ptr->options_list_ptr->proxy_uri_ptr != NULL) { + if (options_list_ptr->proxy_uri_ptr != NULL) { if ((COAP_OPTION_PROXY_URI - previous_option_number) > 12) { needed_space += 1; } @@ -468,7 +476,7 @@ static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_co } previous_option_number = (COAP_OPTION_PROXY_URI); } - if (src_coap_msg_ptr->options_list_ptr->use_size1 ) { + if (options_list_ptr->use_size1) { if ((COAP_OPTION_SIZE1 - previous_option_number) > 12) { needed_space += 1; } @@ -499,7 +507,7 @@ static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_co * * \return Return value is 0 in ok case and -1 in failure case **************************************************************************** */ -static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr) +static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr) { /* * * * Check validity of Header values * * * */ if (sn_coap_header_validity_check(src_coap_msg_ptr, COAP_VERSION) != 0) { @@ -507,25 +515,19 @@ static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, sn_co return -1; } - /* * * Add CoAP Version * * */ - **dst_packet_data_pptr += COAP_VERSION; + uint8_t* dest_packet = *dst_packet_data_pptr; - /* * * Add Message type * * */ - **dst_packet_data_pptr += src_coap_msg_ptr->msg_type; + /* Set CoAP Version, Message type and Token length */ + dest_packet[0] = COAP_VERSION | src_coap_msg_ptr->msg_type | src_coap_msg_ptr->token_len; - /* * * Add Token length * * */ - **dst_packet_data_pptr += (src_coap_msg_ptr->token_len); - - (*dst_packet_data_pptr) ++; /* * * Add Message code * * */ - **dst_packet_data_pptr = src_coap_msg_ptr->msg_code; - (*dst_packet_data_pptr) ++; + dest_packet[1] = src_coap_msg_ptr->msg_code; /* * * Add Message ID * * */ - **dst_packet_data_pptr = (uint8_t)(src_coap_msg_ptr->msg_id >> COAP_HEADER_MSG_ID_MSB_SHIFT); /* MSB part */ - (*dst_packet_data_pptr) ++; - **dst_packet_data_pptr = (uint8_t)src_coap_msg_ptr->msg_id; /* LSB part */ - (*dst_packet_data_pptr) ++; + dest_packet[2] = (uint8_t)(src_coap_msg_ptr->msg_id >> COAP_HEADER_MSG_ID_MSB_SHIFT); /* MSB part */ + dest_packet[3] = (uint8_t)src_coap_msg_ptr->msg_id; /* LSB part */ + + *dst_packet_data_pptr = dest_packet + 4; /* Success */ return 0; @@ -542,7 +544,7 @@ static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, sn_co * * \return Return value is 0 in every case */ -static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr) +static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr) { /* * * * Check if Options are used at all * * * */ if (src_coap_msg_ptr->uri_path_ptr == NULL && src_coap_msg_ptr->token_ptr == NULL && @@ -567,35 +569,37 @@ static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_c //missing: COAP_OPTION_IF_MATCH, COAP_OPTION_IF_NONE_MATCH, COAP_OPTION_SIZE + const sn_coap_options_list_s *src_options_list_ptr = src_coap_msg_ptr->options_list_ptr; + /* Check if less used options are used at all */ - if (src_coap_msg_ptr->options_list_ptr != NULL) { + if (src_options_list_ptr != NULL) { /* * * * Build Uri-Host option * * * */ - sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->uri_host_len, - src_coap_msg_ptr->options_list_ptr->uri_host_ptr, COAP_OPTION_URI_HOST, &previous_option_number); + sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, src_options_list_ptr->uri_host_len, + src_options_list_ptr->uri_host_ptr, COAP_OPTION_URI_HOST, &previous_option_number); /* * * * Build ETag option * * * */ - sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->options_list_ptr->etag_ptr, - (uint16_t *)&src_coap_msg_ptr->options_list_ptr->etag_len, COAP_OPTION_ETAG, &previous_option_number); + sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, src_options_list_ptr->etag_ptr, + src_options_list_ptr->etag_len, COAP_OPTION_ETAG, &previous_option_number); /* * * * Build Observe option * * * * */ - if (src_coap_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) { - sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->observe, + if (src_options_list_ptr->observe != COAP_OBSERVE_NONE) { + sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->observe, COAP_OPTION_OBSERVE, &previous_option_number); } /* * * * Build Uri-Port option * * * */ - if (src_coap_msg_ptr->options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) { - sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->uri_port, + if (src_options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) { + sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->uri_port, COAP_OPTION_URI_PORT, &previous_option_number); } /* * * * Build Location-Path option * * * */ - sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->options_list_ptr->location_path_ptr, - &src_coap_msg_ptr->options_list_ptr->location_path_len, COAP_OPTION_LOCATION_PATH, &previous_option_number); + sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, src_options_list_ptr->location_path_ptr, + src_options_list_ptr->location_path_len, COAP_OPTION_LOCATION_PATH, &previous_option_number); } /* * * * Build Uri-Path option * * * */ - sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->uri_path_ptr, - &src_coap_msg_ptr->uri_path_len, COAP_OPTION_URI_PATH, &previous_option_number); + sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, src_coap_msg_ptr->uri_path_ptr, + src_coap_msg_ptr->uri_path_len, COAP_OPTION_URI_PATH, &previous_option_number); /* * * * Build Content-Type option * * * */ if (src_coap_msg_ptr->content_format != COAP_CT_NONE) { @@ -603,55 +607,53 @@ static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_c COAP_OPTION_CONTENT_FORMAT, &previous_option_number); } - if (src_coap_msg_ptr->options_list_ptr != NULL) { + if (src_options_list_ptr != NULL) { /* * * * Build Max-Age option * * * */ - if (src_coap_msg_ptr->options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) { - sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->max_age, + if (src_options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) { + sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->max_age, COAP_OPTION_MAX_AGE, &previous_option_number); } /* * * * Build Uri-Query option * * * * */ - sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->options_list_ptr->uri_query_ptr, - &src_coap_msg_ptr->options_list_ptr->uri_query_len, COAP_OPTION_URI_QUERY, &previous_option_number); + sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, src_options_list_ptr->uri_query_ptr, + src_options_list_ptr->uri_query_len, COAP_OPTION_URI_QUERY, &previous_option_number); /* * * * Build Accept option * * * * */ if (src_coap_msg_ptr->options_list_ptr->accept != COAP_CT_NONE) { - sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->accept, + sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->accept, COAP_OPTION_ACCEPT, &previous_option_number); } - } - if (src_coap_msg_ptr->options_list_ptr != NULL) { /* * * * Build Location-Query option * * * */ - sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->options_list_ptr->location_query_ptr, - &src_coap_msg_ptr->options_list_ptr->location_query_len, COAP_OPTION_LOCATION_QUERY, &previous_option_number); + sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, src_options_list_ptr->location_query_ptr, + src_options_list_ptr->location_query_len, COAP_OPTION_LOCATION_QUERY, &previous_option_number); /* * * * Build Block2 option * * * * */ if (src_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) { - sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->block2, + sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->block2, COAP_OPTION_BLOCK2, &previous_option_number); } /* * * * Build Block1 option * * * * */ if (src_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) { - sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->block1, + sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->block1, COAP_OPTION_BLOCK1, &previous_option_number); } /* * * * Build Size2 option * * * */ if (src_coap_msg_ptr->options_list_ptr->use_size2) { - sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->size2, + sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->size2, COAP_OPTION_SIZE2, &previous_option_number); } /* * * * Build Proxy-Uri option * * * */ - sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->proxy_uri_len, - src_coap_msg_ptr->options_list_ptr->proxy_uri_ptr, COAP_OPTION_PROXY_URI, &previous_option_number); + sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, src_options_list_ptr->proxy_uri_len, + src_options_list_ptr->proxy_uri_ptr, COAP_OPTION_PROXY_URI, &previous_option_number); /* * * * Build Size1 option * * * */ - if (src_coap_msg_ptr->options_list_ptr->use_size1) { - sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->size1, + if (src_options_list_ptr->use_size1) { + sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->size1, COAP_OPTION_SIZE1, &previous_option_number); } } @@ -676,7 +678,7 @@ static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_c * \return Return value is 0 if option was not added, 1 if added */ static int16_t sn_coap_builder_options_build_add_one_option(uint8_t **dst_packet_data_pptr, uint16_t option_len, - uint8_t *option_ptr, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number) + const uint8_t *option_ptr, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number) { /* Check if there is option at all */ if (option_ptr != NULL) { @@ -686,61 +688,67 @@ static int16_t sn_coap_builder_options_build_add_one_option(uint8_t **dst_packet /* * * Build option header * * */ + uint8_t first_byte; + /* First option length without extended part */ if (option_len <= 12) { - **dst_packet_data_pptr = option_len; + first_byte = option_len; } else if (option_len > 12 && option_len < 269) { - **dst_packet_data_pptr = 0x0D; + first_byte = 0x0D; } - else if (option_len >= 269) { - **dst_packet_data_pptr = 0x0E; + else /*if (option_len >= 269)*/ { + first_byte = 0x0E; } + uint8_t *dest_packet = *dst_packet_data_pptr; + /* Then option delta with extensions, and move pointer */ if (option_delta <= 12) { - **dst_packet_data_pptr += (option_delta << 4); - *dst_packet_data_pptr += 1; + dest_packet[0] = first_byte + (option_delta << 4); + dest_packet += 1; } else if (option_delta > 12 && option_delta < 269) { - **dst_packet_data_pptr += 0xD0; + dest_packet[0] = first_byte + 0xD0; option_delta -= 13; - *(*dst_packet_data_pptr + 1) = (uint8_t)option_delta; - *dst_packet_data_pptr += 2; + dest_packet[1] = (uint8_t)option_delta; + dest_packet += 2; } //This is currently dead code (but possibly needed in future) - else if (option_delta >= 269) { - **dst_packet_data_pptr += 0xE0; + else /*if (option_delta >= 269)*/ { + dest_packet[0] = first_byte + 0xE0; option_delta -= 269; - *(*dst_packet_data_pptr + 2) = (uint8_t)option_delta; - *(*dst_packet_data_pptr + 1) = (option_delta >> 8); - *dst_packet_data_pptr += 3; + dest_packet[1] = (option_delta >> 8); + dest_packet[2] = (uint8_t)option_delta; + dest_packet += 3; } /* Now option length extensions, if needed */ if (option_len > 12 && option_len < 269) { - **dst_packet_data_pptr = (uint8_t)(option_len - 13); - *dst_packet_data_pptr += 1; + dest_packet[0] = (uint8_t)(option_len - 13); + dest_packet += 1; } else if (option_len >= 269) { - *(*dst_packet_data_pptr + 1) = (uint8_t)(option_len - 269); - **dst_packet_data_pptr = ((option_len - 269) >> 8); - *dst_packet_data_pptr += 2; + dest_packet[0] = ((option_len - 269) >> 8); + dest_packet[1] = (uint8_t)(option_len - 269); + dest_packet += 2; } *previous_option_number = option_number; /* Write Option value */ - memcpy(*dst_packet_data_pptr, option_ptr, option_len); + memcpy(dest_packet, option_ptr, option_len); /* Increase destination Packet data pointer */ - (*dst_packet_data_pptr) += option_len; + dest_packet += option_len; + + *dst_packet_data_pptr = dest_packet; return 1; } @@ -775,11 +783,9 @@ static uint8_t sn_coap_builder_options_build_add_uint_option(uint8_t **dst_packe /* If output pointer isn't NULL, write it out */ if (dst_packet_data_pptr) { - int16_t ret = sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, len, payload, option_number, previous_option_number); - /* Allow for failure returns when writing (why even permit failure returns?) */ - if (ret < 0) { - return ret; - } + // No need to check & handle return value, as the function returns failure only if the option pointer is zero + // and it is pointing to a local variable here. + sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, len, payload, option_number, previous_option_number); } /* Return the total option size */ @@ -793,21 +799,21 @@ static uint8_t sn_coap_builder_options_build_add_uint_option(uint8_t **dst_packe * * \param **dst_packet_data_pptr is destination for built Packet data * - * \param uint8_t **src_pptr + * \param uint8_t **src_ptr * - * \param uint16_t *src_len_ptr + * \param uint16_t src_len * * \paramsn_coap_option_numbers_e option option to be added * * \return Return value is 0 always */ -static int16_t sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_packet_data_pptr, uint8_t **src_pptr, uint16_t *src_len_ptr, sn_coap_option_numbers_e option, uint16_t *previous_option_number) +static void sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_packet_data_pptr, const uint8_t *src_pptr, uint16_t src_len, sn_coap_option_numbers_e option, uint16_t *previous_option_number) { /* Check if there is option at all */ - if (*src_pptr != NULL) { - uint8_t *query_ptr = *src_pptr; + if (src_pptr != NULL) { + const uint8_t *query_ptr = src_pptr; uint8_t query_part_count = 0; - uint16_t query_len = *src_len_ptr; + uint16_t query_len = src_len; uint8_t i = 0; uint16_t query_part_offset = 0; @@ -823,11 +829,10 @@ static int16_t sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_p query_part_offset = sn_coap_builder_options_get_option_part_position(query_len, query_ptr, i, option); /* Add Uri-query's one part to Options */ - sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, one_query_part_len, *src_pptr + query_part_offset, option, previous_option_number); + sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, one_query_part_len, src_pptr + query_part_offset, option, previous_option_number); } } /* Success */ - return 0; } @@ -842,7 +847,7 @@ static int16_t sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_p * * \return Return value is count of needed memory as bytes for Uri-query option */ -static uint16_t sn_coap_builder_options_calc_option_size(uint16_t query_len, uint8_t *query_ptr, sn_coap_option_numbers_e option) +static uint16_t sn_coap_builder_options_calc_option_size(uint16_t query_len, const uint8_t *query_ptr, sn_coap_option_numbers_e option) { uint8_t query_part_count = sn_coap_builder_options_get_option_part_count(query_len, query_ptr, option); uint8_t i = 0; @@ -923,7 +928,7 @@ static uint16_t sn_coap_builder_options_calc_option_size(uint16_t query_len, uin * * \return Return value is count of query parts */ -static uint8_t sn_coap_builder_options_get_option_part_count(uint16_t query_len, uint8_t *query_ptr, sn_coap_option_numbers_e option) +static uint8_t sn_coap_builder_options_get_option_part_count(uint16_t query_len, const uint8_t *query_ptr, sn_coap_option_numbers_e option) { uint8_t returned_query_count = 0; uint16_t query_len_index = 0; @@ -963,7 +968,7 @@ static uint8_t sn_coap_builder_options_get_option_part_count(uint16_t query_len, * * \return Return value is length of query part */ -static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option_string(uint16_t query_len, uint8_t *query_ptr, +static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option_string(uint16_t query_len, const uint8_t *query_ptr, uint8_t query_index, sn_coap_option_numbers_e option) { uint16_t returned_query_part_len = 0; @@ -1021,7 +1026,7 @@ static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option * \return Return value is position (= offset) of query part in whole query. In * fail cases -1 is returned. */ -static int16_t sn_coap_builder_options_get_option_part_position(uint16_t query_len, uint8_t *query_ptr, +static int16_t sn_coap_builder_options_get_option_part_position(uint16_t query_len, const uint8_t *query_ptr, uint8_t query_index, sn_coap_option_numbers_e option) { uint16_t returned_query_part_offset = 0; @@ -1076,7 +1081,7 @@ static int16_t sn_coap_builder_options_get_option_part_position(uint16_t query_l * * \param *src_coap_msg_ptr is source for building Packet data */ -static void sn_coap_builder_payload_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr) +static void sn_coap_builder_payload_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr) { /* Check if Payload is used at all */ if (src_coap_msg_ptr->payload_len && src_coap_msg_ptr->payload_ptr != NULL) { diff --git a/features/frameworks/mbed-coap/source/sn_coap_header_check.c b/features/frameworks/mbed-coap/source/sn_coap_header_check.c index 0d06e83910..e0c4869569 100644 --- a/features/frameworks/mbed-coap/source/sn_coap_header_check.c +++ b/features/frameworks/mbed-coap/source/sn_coap_header_check.c @@ -44,7 +44,7 @@ * \return Return value is status of validity check. In ok cases 0 and in * failure cases -1 */ -int8_t sn_coap_header_validity_check(sn_coap_hdr_s *src_coap_msg_ptr, coap_version_e coap_version) +int8_t sn_coap_header_validity_check(const sn_coap_hdr_s *src_coap_msg_ptr, coap_version_e coap_version) { /* * Check validity of CoAP Version * */ if (coap_version != COAP_VERSION_1) { diff --git a/features/frameworks/mbed-coap/source/sn_coap_parser.c b/features/frameworks/mbed-coap/source/sn_coap_parser.c index 68dd47d8a2..36ab20d8dc 100644 --- a/features/frameworks/mbed-coap/source/sn_coap_parser.c +++ b/features/frameworks/mbed-coap/source/sn_coap_parser.c @@ -45,7 +45,7 @@ static void sn_coap_parser_header_parse(uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr, coap_version_e *coap_version_ptr); static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr, uint8_t *packet_data_start_ptr, uint16_t packet_len); static int8_t sn_coap_parser_options_parse_multiple_options(struct coap_s *handle, uint8_t **packet_data_pptr, uint16_t packet_left_len, uint8_t **dst_pptr, uint16_t *dst_len_ptr, sn_coap_option_numbers_e option, uint16_t option_number_len); -static int16_t sn_coap_parser_options_count_needed_memory_multiple_option(uint8_t *packet_data_ptr, uint16_t packet_left_len, sn_coap_option_numbers_e option, uint16_t option_number_len); +static int16_t sn_coap_parser_options_count_needed_memory_multiple_option(const uint8_t *packet_data_ptr, uint16_t packet_left_len, sn_coap_option_numbers_e option, uint16_t option_number_len); static int8_t sn_coap_parser_payload_parse(uint16_t packet_data_len, uint8_t *packet_data_start_ptr, uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr); sn_coap_hdr_s *sn_coap_parser_init_message(sn_coap_hdr_s *coap_msg_ptr) @@ -64,6 +64,29 @@ sn_coap_hdr_s *sn_coap_parser_init_message(sn_coap_hdr_s *coap_msg_ptr) return coap_msg_ptr; } +sn_coap_hdr_s *sn_coap_parser_alloc_message_with_options(struct coap_s *handle) +{ + // check the handle just as in any other place + if (handle == NULL) { + return NULL; + } + + sn_coap_hdr_s *coap_msg_ptr = sn_coap_parser_alloc_message(handle); + + sn_coap_options_list_s *options_list_ptr = sn_coap_parser_alloc_options(handle, coap_msg_ptr); + + if ((coap_msg_ptr == NULL) || (options_list_ptr == NULL)) { + + // oops, out of memory free if got already any + handle->sn_coap_protocol_free(coap_msg_ptr); + handle->sn_coap_protocol_free(options_list_ptr); + + coap_msg_ptr = NULL; + } + + return coap_msg_ptr; +} + sn_coap_hdr_s *sn_coap_parser_alloc_message(struct coap_s *handle) { sn_coap_hdr_s *returned_coap_msg_ptr; @@ -81,6 +104,8 @@ sn_coap_hdr_s *sn_coap_parser_alloc_message(struct coap_s *handle) sn_coap_options_list_s *sn_coap_parser_alloc_options(struct coap_s *handle, sn_coap_hdr_s *coap_msg_ptr) { + sn_coap_options_list_s *options_list_ptr; + /* * * * Check given pointers * * * */ if (handle == NULL || coap_msg_ptr == NULL) { return NULL; @@ -92,24 +117,23 @@ sn_coap_options_list_s *sn_coap_parser_alloc_options(struct coap_s *handle, sn_c } /* * * * Allocate memory for options and initialize allocated memory with with default values * * * */ - coap_msg_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s)); + /* XXX not technically legal to memset pointers to 0 */ + options_list_ptr = sn_coap_protocol_calloc(handle, sizeof(sn_coap_options_list_s)); - if (coap_msg_ptr->options_list_ptr == NULL) { + if (options_list_ptr == NULL) { tr_error("sn_coap_parser_alloc_options - failed to allocate options list!"); return NULL; } - /* XXX not technically legal to memset pointers to 0 */ - memset(coap_msg_ptr->options_list_ptr, 0x00, sizeof(sn_coap_options_list_s)); + coap_msg_ptr->options_list_ptr = options_list_ptr; - coap_msg_ptr->options_list_ptr->max_age = 0; - coap_msg_ptr->options_list_ptr->uri_port = COAP_OPTION_URI_PORT_NONE; - coap_msg_ptr->options_list_ptr->observe = COAP_OBSERVE_NONE; - coap_msg_ptr->options_list_ptr->accept = COAP_CT_NONE; - coap_msg_ptr->options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE; - coap_msg_ptr->options_list_ptr->block1 = COAP_OPTION_BLOCK_NONE; + options_list_ptr->uri_port = COAP_OPTION_URI_PORT_NONE; + options_list_ptr->observe = COAP_OBSERVE_NONE; + options_list_ptr->accept = COAP_CT_NONE; + options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE; + options_list_ptr->block1 = COAP_OPTION_BLOCK_NONE; - return coap_msg_ptr->options_list_ptr; + return options_list_ptr; } sn_coap_hdr_s *sn_coap_parser(struct coap_s *handle, uint16_t packet_data_len, uint8_t *packet_data_ptr, coap_version_e *coap_version_ptr) @@ -156,43 +180,35 @@ void sn_coap_parser_release_allocated_coap_msg_mem(struct coap_s *handle, sn_coa } if (freed_coap_msg_ptr != NULL) { - if (freed_coap_msg_ptr->uri_path_ptr != NULL) { - handle->sn_coap_protocol_free(freed_coap_msg_ptr->uri_path_ptr); + + // As there are multiple sequential calls to the protocol_free, caching pointer to it + // saves one instruction per call. + void (*local_free)(void *) = handle->sn_coap_protocol_free; + + local_free(freed_coap_msg_ptr->uri_path_ptr); + local_free(freed_coap_msg_ptr->token_ptr); + + // same here, caching the struct start saves a bit. + sn_coap_options_list_s *options_list_ptr = freed_coap_msg_ptr->options_list_ptr; + + if (options_list_ptr != NULL) { + + local_free(options_list_ptr->proxy_uri_ptr); + + local_free(options_list_ptr->etag_ptr); + + local_free(options_list_ptr->uri_host_ptr); + + local_free(options_list_ptr->location_path_ptr); + + local_free(options_list_ptr->location_query_ptr); + + local_free(options_list_ptr->uri_query_ptr); + + local_free(options_list_ptr); } - if (freed_coap_msg_ptr->token_ptr != NULL) { - handle->sn_coap_protocol_free(freed_coap_msg_ptr->token_ptr); - } - - if (freed_coap_msg_ptr->options_list_ptr != NULL) { - if (freed_coap_msg_ptr->options_list_ptr->proxy_uri_ptr != NULL) { - handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->proxy_uri_ptr); - } - - if (freed_coap_msg_ptr->options_list_ptr->etag_ptr != NULL) { - handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->etag_ptr); - } - - if (freed_coap_msg_ptr->options_list_ptr->uri_host_ptr != NULL) { - handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->uri_host_ptr); - } - - if (freed_coap_msg_ptr->options_list_ptr->location_path_ptr != NULL) { - handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->location_path_ptr); - } - - if (freed_coap_msg_ptr->options_list_ptr->location_query_ptr != NULL) { - handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->location_query_ptr); - } - - if (freed_coap_msg_ptr->options_list_ptr->uri_query_ptr != NULL) { - handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->uri_query_ptr); - } - - handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr); - } - - handle->sn_coap_protocol_free(freed_coap_msg_ptr); + local_free(freed_coap_msg_ptr); } } @@ -269,14 +285,13 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack return -1; } - dst_coap_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(dst_coap_msg_ptr->token_len); + dst_coap_msg_ptr->token_ptr = sn_coap_protocol_malloc_copy(handle, *packet_data_pptr, dst_coap_msg_ptr->token_len); if (dst_coap_msg_ptr->token_ptr == NULL) { tr_error("sn_coap_parser_options_parse - failed to allocate token!"); return -1; } - memcpy(dst_coap_msg_ptr->token_ptr, *packet_data_pptr, dst_coap_msg_ptr->token_len); (*packet_data_pptr) += dst_coap_msg_ptr->token_len; } @@ -379,14 +394,13 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack dst_coap_msg_ptr->options_list_ptr->proxy_uri_len = option_len; (*packet_data_pptr)++; - dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr = handle->sn_coap_protocol_malloc(option_len); + dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr = sn_coap_protocol_malloc_copy(handle, *packet_data_pptr, option_len); if (dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr == NULL) { tr_error("sn_coap_parser_options_parse - COAP_OPTION_PROXY_URI allocation failed!"); return -1; } - memcpy(dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr, *packet_data_pptr, option_len); (*packet_data_pptr) += option_len; break; @@ -415,13 +429,12 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack dst_coap_msg_ptr->options_list_ptr->uri_host_len = option_len; (*packet_data_pptr)++; - dst_coap_msg_ptr->options_list_ptr->uri_host_ptr = handle->sn_coap_protocol_malloc(option_len); + dst_coap_msg_ptr->options_list_ptr->uri_host_ptr = sn_coap_protocol_malloc_copy(handle, *packet_data_pptr, option_len); if (dst_coap_msg_ptr->options_list_ptr->uri_host_ptr == NULL) { tr_error("sn_coap_parser_options_parse - COAP_OPTION_URI_HOST allocation failed!"); return -1; } - memcpy(dst_coap_msg_ptr->options_list_ptr->uri_host_ptr, *packet_data_pptr, option_len); (*packet_data_pptr) += option_len; break; @@ -623,9 +636,9 @@ static int8_t sn_coap_parser_options_parse_multiple_options(struct coap_s *handl /* Uri-Query is modified to following format: temp1'\0'temp2'\0'temp3 i.e. */ /* Uri-Path is modified to following format: temp1\temp2\temp3 i.e. */ if (option == COAP_OPTION_URI_QUERY || option == COAP_OPTION_LOCATION_QUERY || option == COAP_OPTION_ETAG || option == COAP_OPTION_ACCEPT) { - memset(temp_parsed_uri_query_ptr, '&', 1); + *temp_parsed_uri_query_ptr = '&'; } else if (option == COAP_OPTION_URI_PATH || option == COAP_OPTION_LOCATION_PATH) { - memset(temp_parsed_uri_query_ptr, '/', 1); + *temp_parsed_uri_query_ptr = '/'; } temp_parsed_uri_query_ptr++; @@ -680,7 +693,7 @@ static int8_t sn_coap_parser_options_parse_multiple_options(struct coap_s *handl * * \param uint16_t option_number_len length of the first option part */ -static int16_t sn_coap_parser_options_count_needed_memory_multiple_option(uint8_t *packet_data_ptr, uint16_t packet_left_len, sn_coap_option_numbers_e option, uint16_t option_number_len) +static int16_t sn_coap_parser_options_count_needed_memory_multiple_option(const uint8_t *packet_data_ptr, uint16_t packet_left_len, sn_coap_option_numbers_e option, uint16_t option_number_len) { uint16_t ret_value = 0; uint16_t i = 1; diff --git a/features/frameworks/mbed-coap/source/sn_coap_protocol.c b/features/frameworks/mbed-coap/source/sn_coap_protocol.c index 9924b0e8c2..b4a1bafa79 100644 --- a/features/frameworks/mbed-coap/source/sn_coap_protocol.c +++ b/features/frameworks/mbed-coap/source/sn_coap_protocol.c @@ -51,32 +51,40 @@ static void sn_coap_protocol_linked_list_duplication_info_store(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id, void *param); static coap_duplication_info_s *sn_coap_protocol_linked_list_duplication_info_search(const struct coap_s *handle, const sn_nsdl_addr_s *scr_addr_ptr, const uint16_t msg_id); static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(struct coap_s *handle); +static void sn_coap_protocol_duplication_info_free(struct coap_s *handle, coap_duplication_info_s *duplication_info_ptr); static bool sn_coap_protocol_update_duplicate_package_data(const struct coap_s *handle, const sn_nsdl_addr_s *dst_addr_ptr, const sn_coap_hdr_s *coap_msg_ptr, const int16_t data_size, const uint8_t *dst_packet_data_ptr); #endif #if SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not enabled, this part of code will not be compiled */ static void sn_coap_protocol_linked_list_blockwise_msg_remove(struct coap_s *handle, coap_blockwise_msg_s *removed_msg_ptr); static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s *handle, sn_nsdl_addr_s *addr_ptr, uint16_t stored_payload_len, uint8_t *stored_payload_ptr, uint8_t *token_ptr, uint8_t token_len, uint32_t block_number); -static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length, uint8_t *token_ptr, uint8_t token_len); -static bool sn_coap_protocol_linked_list_blockwise_payload_compare_block_number(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint8_t *token_ptr, uint8_t token_len, uint32_t block_number); +static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(struct coap_s *handle, const sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length, const uint8_t *token_ptr, uint8_t token_len); +static bool sn_coap_protocol_linked_list_blockwise_payload_search_compare_block_number(struct coap_s *handle, const sn_nsdl_addr_s *src_addr_ptr, const uint8_t *token_ptr, uint8_t token_len, uint32_t block_number); static void sn_coap_protocol_linked_list_blockwise_payload_remove(struct coap_s *handle, coap_blockwise_payload_s *removed_payload_ptr); static void sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(struct coap_s *handle, uint8_t *token_ptr, uint8_t token_len); -static uint32_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint8_t *token_ptr, uint8_t token_len); +static uint32_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(struct coap_s *handle, const sn_nsdl_addr_s *src_addr_ptr, const uint8_t *token_ptr, uint8_t token_len); static void sn_coap_protocol_handle_blockwise_timout(struct coap_s *handle); static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, sn_coap_hdr_s *received_coap_msg_ptr, void *param); -static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coap_hdr_s *source_header_ptr); +static bool sn_coap_handle_last_blockwise(struct coap_s *handle, const sn_nsdl_addr_s *src_addr_ptr, sn_coap_hdr_s *received_coap_msg_ptr); +static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, const sn_coap_hdr_s *source_header_ptr); +static coap_blockwise_msg_s *search_sent_blockwise_message(struct coap_s *handle, uint16_t msg_id); +static int16_t store_blockwise_copy(struct coap_s *handle, const sn_coap_hdr_s *src_coap_msg_ptr, void *param, uint16_t original_payload_len, bool copy_payload); #endif #if ENABLE_RESENDINGS static uint8_t sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t send_packet_data_len, uint8_t *send_packet_data_ptr, uint32_t sending_time, void *param); -static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(struct coap_s *handle,sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id); -static void sn_coap_protocol_linked_list_send_msg_remove(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id); +static void sn_coap_protocol_linked_list_send_msg_remove(struct coap_s *handle, const sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id); static coap_send_msg_s *sn_coap_protocol_allocate_mem_for_msg(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t packet_data_len); static void sn_coap_protocol_release_allocated_send_msg_mem(struct coap_s *handle, coap_send_msg_s *freed_send_msg_ptr); static uint16_t sn_coap_count_linked_list_size(const coap_send_msg_list_t *linked_list_ptr); static uint32_t sn_coap_calculate_new_resend_time(const uint32_t current_time, const uint8_t interval, const uint8_t counter); #endif +static uint16_t read_packet_msg_id(const coap_send_msg_s *stored_msg); +static uint16_t get_new_message_id(void); + +static bool compare_address_and_port(const sn_nsdl_addr_s* left, const sn_nsdl_addr_s* right); + /* * * * * * * * * * * * * * * * * */ /* * * * GLOBAL DECLARATIONS * * * */ /* * * * * * * * * * * * * * * * * */ @@ -95,50 +103,23 @@ int8_t sn_coap_protocol_destroy(struct coap_s *handle) #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */ ns_list_foreach_safe(coap_duplication_info_s, tmp, &handle->linked_list_duplication_msgs) { - if (tmp->coap == handle) { - if (tmp->address) { - if (tmp->address->addr_ptr) { - handle->sn_coap_protocol_free(tmp->address->addr_ptr); - tmp->address->addr_ptr = 0; - } - handle->sn_coap_protocol_free(tmp->address); - tmp->address = 0; - } - if (tmp->packet_ptr) { - handle->sn_coap_protocol_free(tmp->packet_ptr); - tmp->packet_ptr = 0; - } - ns_list_remove(&handle->linked_list_duplication_msgs, tmp); - handle->count_duplication_msgs--; - handle->sn_coap_protocol_free(tmp); - tmp = 0; - } + + ns_list_remove(&handle->linked_list_duplication_msgs, tmp); + + sn_coap_protocol_duplication_info_free(handle, tmp); } #endif #if SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwise is not enabled, this part of code will not be compiled */ - ns_list_foreach_safe(coap_blockwise_msg_s, tmp, &handle->linked_list_blockwise_sent_msgs) { - if (tmp->coap == handle) { - if (tmp->coap_msg_ptr) { - handle->sn_coap_protocol_free(tmp->coap_msg_ptr->payload_ptr); - sn_coap_parser_release_allocated_coap_msg_mem(tmp->coap, tmp->coap_msg_ptr); - } - ns_list_remove(&handle->linked_list_blockwise_sent_msgs, tmp); - handle->sn_coap_protocol_free(tmp); - } - } - ns_list_foreach_safe(coap_blockwise_payload_s, tmp, &handle->linked_list_blockwise_received_payloads) { - if (tmp->coap == handle) { - sn_coap_protocol_linked_list_blockwise_payload_remove(handle, tmp); - } - } + sn_coap_protocol_clear_sent_blockwise_messages(handle); + + sn_coap_protocol_clear_received_blockwise_messages(handle); #endif handle->sn_coap_protocol_free(handle); - handle = 0; return 0; } @@ -352,10 +333,9 @@ int8_t sn_coap_protocol_delete_retransmission(struct coap_s *handle, uint16_t ms return -1; } ns_list_foreach_safe(coap_send_msg_s, tmp, &handle->linked_list_resent_msgs) { - if (tmp->send_msg_ptr && tmp->send_msg_ptr->packet_ptr ) { - uint16_t temp_msg_id = (tmp->send_msg_ptr->packet_ptr[2] << 8); - temp_msg_id += (uint16_t)tmp->send_msg_ptr->packet_ptr[3]; - if(temp_msg_id == msg_id){ + if (tmp->send_msg_ptr.packet_ptr) { + uint16_t temp_msg_id = read_packet_msg_id(tmp); + if (temp_msg_id == msg_id) { ns_list_remove(&handle->linked_list_resent_msgs, tmp); --handle->count_resent_msgs; sn_coap_protocol_release_allocated_send_msg_mem(handle, tmp); @@ -367,7 +347,7 @@ int8_t sn_coap_protocol_delete_retransmission(struct coap_s *handle, uint16_t ms return -2; } -int8_t sn_coap_protocol_delete_retransmission_by_token(struct coap_s *handle, uint8_t *token, uint8_t token_len) +int8_t sn_coap_protocol_delete_retransmission_by_token(struct coap_s *handle, const uint8_t *token, uint8_t token_len) { #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */ if (handle == NULL || token == NULL || token_len == 0) { @@ -376,14 +356,11 @@ int8_t sn_coap_protocol_delete_retransmission_by_token(struct coap_s *handle, ui } ns_list_foreach(coap_send_msg_s, stored_msg, &handle->linked_list_resent_msgs) { - uint8_t stored_token_len = (stored_msg->send_msg_ptr->packet_ptr[0] & 0x0F); + uint8_t stored_token_len = (stored_msg->send_msg_ptr.packet_ptr[0] & 0x0F); if (stored_token_len == token_len) { - uint8_t stored_token[8]; - memcpy(stored_token, &stored_msg->send_msg_ptr->packet_ptr[4], stored_token_len); - if (memcmp(stored_token, token, stored_token_len) == 0) { - uint16_t temp_msg_id = (stored_msg->send_msg_ptr->packet_ptr[2] << 8); - temp_msg_id += (uint16_t)stored_msg->send_msg_ptr->packet_ptr[3]; - tr_debug("sn_coap_protocol_delete_retransmission_by_token - removed msg_id: %d", temp_msg_id); + if (memcmp(&stored_msg->send_msg_ptr.packet_ptr[4], token, stored_token_len) == 0) { + + tr_debug("sn_coap_protocol_delete_retransmission_by_token - removed msg_id: %" PRIu16, read_packet_msg_id(stored_msg)); ns_list_remove(&handle->linked_list_resent_msgs, stored_msg); --handle->count_resent_msgs; @@ -397,6 +374,12 @@ int8_t sn_coap_protocol_delete_retransmission_by_token(struct coap_s *handle, ui return -2; } +static uint16_t read_packet_msg_id(const coap_send_msg_s *stored_msg) +{ + uint16_t msg_id = (stored_msg->send_msg_ptr.packet_ptr[2] << 8); + msg_id += (uint16_t)stored_msg->send_msg_ptr.packet_ptr[3]; + return msg_id; +} int8_t prepare_blockwise_message(struct coap_s *handle, sn_coap_hdr_s *src_coap_msg_ptr) { @@ -440,7 +423,6 @@ int8_t prepare_blockwise_message(struct coap_s *handle, sn_coap_hdr_s *src_coap_ return 0; } - int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, void *param) { @@ -462,12 +444,9 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_ACKNOWLEDGEMENT && src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_RESET && src_coap_msg_ptr->msg_id == 0) { - /* * * * Generate new Message ID and increase it by one * * * */ - src_coap_msg_ptr->msg_id = message_id; - message_id++; - if (message_id == 0) { - message_id = 1; - } + + /* * * * Generate new Message ID * * * */ + src_coap_msg_ptr->msg_id = get_new_message_id(); } #if SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not enabled, this part of code will not be compiled */ @@ -525,71 +504,18 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p /* * * * Manage rest blockwise messages sending by storing them to Linked list * * * */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - coap_blockwise_msg_s *stored_blockwise_msg_ptr; - - stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s)); - if (!stored_blockwise_msg_ptr) { - //block paylaod save failed, only first block can be build. Perhaps we should return error. - tr_error("sn_coap_protocol_build - blockwise message allocation failed!"); - return byte_count_built; - } - memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s)); - - /* Fill struct */ - stored_blockwise_msg_ptr->timestamp = handle->system_time; - - stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(handle, src_coap_msg_ptr); - if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){ - handle->sn_coap_protocol_free(stored_blockwise_msg_ptr); - stored_blockwise_msg_ptr = 0; - tr_error("sn_coap_protocol_build - block header copy failed!"); - return -2; + int status = store_blockwise_copy(handle, src_coap_msg_ptr, param, original_payload_len, true); + if (status < 0) { + return status; } - stored_blockwise_msg_ptr->coap_msg_ptr->payload_len = original_payload_len; - stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr = handle->sn_coap_protocol_malloc(stored_blockwise_msg_ptr->coap_msg_ptr->payload_len); - - if (!stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr) { - //block payload save failed, only first block can be build. Perhaps we should return error. - sn_coap_parser_release_allocated_coap_msg_mem(handle, stored_blockwise_msg_ptr->coap_msg_ptr); - handle->sn_coap_protocol_free(stored_blockwise_msg_ptr); - stored_blockwise_msg_ptr = 0; - tr_error("sn_coap_protocol_build - block payload allocation failed!"); - return byte_count_built; - } - memcpy(stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr, src_coap_msg_ptr->payload_ptr, stored_blockwise_msg_ptr->coap_msg_ptr->payload_len); - - stored_blockwise_msg_ptr->coap = handle; - stored_blockwise_msg_ptr->param = param; - stored_blockwise_msg_ptr->msg_id = stored_blockwise_msg_ptr->coap_msg_ptr->msg_id; - ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr); } else if (src_coap_msg_ptr->msg_code <= COAP_MSG_CODE_REQUEST_DELETE && src_coap_msg_ptr->msg_code != COAP_MSG_CODE_EMPTY) { - /* Add message to linked list - response can be in blocks and we need header to build response.. */ - coap_blockwise_msg_s *stored_blockwise_msg_ptr; - stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s)); - if (!stored_blockwise_msg_ptr) { - tr_error("sn_coap_protocol_build - blockwise (GET) allocation failed!"); - return byte_count_built; + int status = store_blockwise_copy(handle, src_coap_msg_ptr, param, original_payload_len, false); + if (status < 0) { + return status; } - memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s)); - - /* Fill struct */ - stored_blockwise_msg_ptr->timestamp = handle->system_time; - - stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(handle, src_coap_msg_ptr); - if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){ - handle->sn_coap_protocol_free(stored_blockwise_msg_ptr); - stored_blockwise_msg_ptr = 0; - tr_error("sn_coap_protocol_build - blockwise (GET) copy header failed!"); - return -2; - } - - stored_blockwise_msg_ptr->coap = handle; - stored_blockwise_msg_ptr->param = param; - stored_blockwise_msg_ptr->msg_id = stored_blockwise_msg_ptr->coap_msg_ptr->msg_id; - ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr); } #endif /* SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */ @@ -598,6 +524,49 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p return byte_count_built; } +#if SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE +static int16_t store_blockwise_copy(struct coap_s *handle, const sn_coap_hdr_s *src_coap_msg_ptr, void *param, uint16_t original_payload_len, bool copy_payload) +{ + coap_blockwise_msg_s *stored_blockwise_msg_ptr; + + stored_blockwise_msg_ptr = sn_coap_protocol_calloc(handle, sizeof(coap_blockwise_msg_s)); + if (!stored_blockwise_msg_ptr) { + //block payload save failed, only first block can be build. Perhaps we should return error. + tr_error("sn_coap_protocol_build - blockwise message allocation failed!"); + return -2; + } + + /* Fill struct */ + stored_blockwise_msg_ptr->timestamp = handle->system_time; + + stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(handle, src_coap_msg_ptr); + if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){ + handle->sn_coap_protocol_free(stored_blockwise_msg_ptr); + tr_error("sn_coap_protocol_build - block header copy failed!"); + return -2; + } + + if (copy_payload) { + stored_blockwise_msg_ptr->coap_msg_ptr->payload_len = original_payload_len; + stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr = sn_coap_protocol_malloc_copy(handle, src_coap_msg_ptr->payload_ptr, stored_blockwise_msg_ptr->coap_msg_ptr->payload_len); + + if (!stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr) { + //block payload save failed, only first block can be build. Perhaps we should return error. + sn_coap_parser_release_allocated_coap_msg_mem(handle, stored_blockwise_msg_ptr->coap_msg_ptr); + handle->sn_coap_protocol_free(stored_blockwise_msg_ptr); + tr_error("sn_coap_protocol_build - block payload allocation failed!"); + return -2; + } + } + + stored_blockwise_msg_ptr->param = param; + stored_blockwise_msg_ptr->msg_id = stored_blockwise_msg_ptr->coap_msg_ptr->msg_id; + ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr); + + return 0; +} +#endif + sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t packet_data_len, uint8_t *packet_data_ptr, void *param) { sn_coap_hdr_s *returned_dst_coap_msg_ptr = NULL; @@ -629,9 +598,11 @@ sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src /* * * * Check validity of parsed Header values * * * */ if (sn_coap_header_validity_check(returned_dst_coap_msg_ptr, coap_version) != 0) { /* If message code is in a reserved class (1, 6 or 7), send reset. Message code class is 3 MSB of the message code byte */ - if (((returned_dst_coap_msg_ptr->msg_code >> 5) == 1) || // if class == 1 - ((returned_dst_coap_msg_ptr->msg_code >> 5) == 6) || // if class == 6 - ((returned_dst_coap_msg_ptr->msg_code >> 5) == 7)) { // if class == 7 + + const uint8_t msg_code_class = returned_dst_coap_msg_ptr->msg_code >> 5; + if ((msg_code_class == 1) || // if class == 1 + (msg_code_class == 6) || // if class == 6 + (msg_code_class == 7)) { // if class == 7 tr_error("sn_coap_protocol_parse - message code not valid!"); sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param); } @@ -679,46 +650,50 @@ sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT/* If Message duplication is used, this part of code will not be compiled */ - /* * * * Manage received CoAP message duplicate detection * * * */ + // Manage received CoAP message duplicate detection - /* If no message duplication detected */ + // If no message duplication detected if ((returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE || - returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_NON_CONFIRMABLE) && - handle->sn_coap_duplication_buffer_size != 0) { - if (sn_coap_protocol_linked_list_duplication_info_search(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id) == NULL) { - /* * * No Message duplication: Store received message for detecting later duplication * * */ + returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_NON_CONFIRMABLE || + (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT && + returned_dst_coap_msg_ptr->msg_code != COAP_MSG_CODE_EMPTY)) && + handle->sn_coap_duplication_buffer_size != 0) { - /* Get count of stored duplication messages */ + coap_duplication_info_s* response = sn_coap_protocol_linked_list_duplication_info_search(handle, + src_addr_ptr, + returned_dst_coap_msg_ptr->msg_id); + if (response == NULL) { + // No Message duplication: Store received message for detecting later duplication + + // Get count of stored duplication messages uint16_t stored_duplication_msgs_count = handle->count_duplication_msgs; - /* Check if there is no room to store message for duplication detection purposes */ + // Check if there is no room to store message for duplication detection purposes if (stored_duplication_msgs_count >= handle->sn_coap_duplication_buffer_size) { tr_debug("sn_coap_protocol_parse - duplicate list full, dropping oldest"); - /* Get oldest stored duplication message */ + // Get oldest stored duplication message coap_duplication_info_s *stored_duplication_info_ptr = ns_list_get_first(&handle->linked_list_duplication_msgs); - /* Remove oldest stored duplication message for getting room for new duplication message */ + // Remove oldest stored duplication message for getting room for new duplication message sn_coap_protocol_linked_list_duplication_info_remove(handle, stored_duplication_info_ptr->address->addr_ptr, stored_duplication_info_ptr->address->port, stored_duplication_info_ptr->msg_id); } - /* Store Duplication info to Linked list */ + // Store Duplication info to Linked list sn_coap_protocol_linked_list_duplication_info_store(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id, param); - } else { /* * * Message duplication detected * * */ - /* Set returned status to User */ + } else { // Message duplication detected + // Set returned status to User returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_DUPLICATED_MSG; - coap_duplication_info_s* response = sn_coap_protocol_linked_list_duplication_info_search(handle, - src_addr_ptr, - returned_dst_coap_msg_ptr->msg_id); - /* Send ACK response */ - if (response) { - /* Check that response has been created */ + + // Send ACK response + if (response && returned_dst_coap_msg_ptr->msg_type != COAP_MSG_TYPE_ACKNOWLEDGEMENT) { + // Check that response has been created if (response->packet_ptr) { tr_debug("sn_coap_protocol_parse - send ack for duplicate message"); - response->coap->sn_coap_tx_callback(response->packet_ptr, + handle->sn_coap_tx_callback(response->packet_ptr, response->packet_len, response->address, response->param); } } @@ -736,20 +711,24 @@ sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src if (returned_dst_coap_msg_ptr->options_list_ptr != NULL && (returned_dst_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE || returned_dst_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE)) { - returned_dst_coap_msg_ptr = sn_coap_handle_blockwise_message(handle, src_addr_ptr, returned_dst_coap_msg_ptr, param); + + // the sn_coap_handle_blockwise_message() will return the given message on success or NULL on error + if (sn_coap_handle_blockwise_message(handle, src_addr_ptr, returned_dst_coap_msg_ptr, param) == NULL) { + + tr_error("sn_coap_protocol_parse - handle blockwise returns null!"); + + // the message is freed just here, not in every other path in sn_coap_handle_blockwise_message(). + sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr); + returned_dst_coap_msg_ptr = NULL; + } + } else if (returned_dst_coap_msg_ptr->msg_code != COAP_MSG_CODE_EMPTY) { // Do not clean stored blockwise message when empty ack is received. // Stored message is mandatory when building a next (GET) blockwise message. // This will happen when non piggybacked response mode is selected. /* Get ... */ - coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = NULL; + coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = search_sent_blockwise_message(handle, returned_dst_coap_msg_ptr->msg_id); - ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) { - if (returned_dst_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) { - stored_blockwise_msg_temp_ptr = msg; - break; - } - } /* Remove from the list if not an notification message. * Initial notification message is needed for sending rest of the blocks (GET request). */ @@ -764,17 +743,8 @@ sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src } } if (remove_from_the_list) { - ns_list_remove(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_temp_ptr); - if (stored_blockwise_msg_temp_ptr->coap_msg_ptr) { - if(stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr){ - handle->sn_coap_protocol_free(stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr); - stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = 0; - } - sn_coap_parser_release_allocated_coap_msg_mem(stored_blockwise_msg_temp_ptr->coap, stored_blockwise_msg_temp_ptr->coap_msg_ptr); - } - handle->sn_coap_protocol_free(stored_blockwise_msg_temp_ptr); - stored_blockwise_msg_temp_ptr = 0; + sn_coap_protocol_linked_list_blockwise_msg_remove(handle, stored_blockwise_msg_temp_ptr); } } @@ -797,15 +767,9 @@ sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src /* Check if there is ongoing active message resendings */ if (stored_resending_msgs_count > 0) { - sn_nsdl_transmit_s *removed_msg_ptr = NULL; - /* Check if received message was confirmation for some active resending message */ - removed_msg_ptr = sn_coap_protocol_linked_list_send_msg_search(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id); - - if (removed_msg_ptr != NULL) { - /* Remove resending message from active message resending Linked list */ - sn_coap_protocol_linked_list_send_msg_remove(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id); - } + /* Remove resending message from active message resending Linked list, if any exists */ + sn_coap_protocol_linked_list_send_msg_remove(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id); } } #endif /* ENABLE_RESENDINGS */ @@ -840,54 +804,49 @@ int8_t sn_coap_protocol_exec(struct coap_s *handle, uint32_t current_time) rescan: ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) { // First check that msg belongs to handle - if( stored_msg_ptr->coap == handle ){ - /* Check if it is time to send this message */ - if (current_time >= stored_msg_ptr->resending_time) { - /* * * Increase Resending counter * * */ - stored_msg_ptr->resending_counter++; + /* Check if it is time to send this message */ + if (current_time >= stored_msg_ptr->resending_time) { + /* * * Increase Resending counter * * */ + stored_msg_ptr->resending_counter++; - /* Check if all re-sendings have been done */ - if (stored_msg_ptr->resending_counter > handle->sn_coap_resending_count) { - coap_version_e coap_version = COAP_VERSION_UNKNOWN; + /* Check if all re-sendings have been done */ + if (stored_msg_ptr->resending_counter > handle->sn_coap_resending_count) { + coap_version_e coap_version = COAP_VERSION_UNKNOWN; - /* Get message ID from stored sending message */ - uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8); - temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3]; - /* Remove message from Linked list */ - ns_list_remove(&handle->linked_list_resent_msgs, stored_msg_ptr); - --handle->count_resent_msgs; + /* Remove message from Linked list */ + ns_list_remove(&handle->linked_list_resent_msgs, stored_msg_ptr); + --handle->count_resent_msgs; - /* If RX callback have been defined.. */ - if (stored_msg_ptr->coap->sn_coap_rx_callback != 0) { - sn_coap_hdr_s *tmp_coap_hdr_ptr; - /* Parse CoAP message, set status and call RX callback */ - tmp_coap_hdr_ptr = sn_coap_parser(stored_msg_ptr->coap, stored_msg_ptr->send_msg_ptr->packet_len, stored_msg_ptr->send_msg_ptr->packet_ptr, &coap_version); + /* If RX callback have been defined.. */ + if (handle->sn_coap_rx_callback != 0) { + sn_coap_hdr_s *tmp_coap_hdr_ptr; + /* Parse CoAP message, set status and call RX callback */ + tmp_coap_hdr_ptr = sn_coap_parser(handle, stored_msg_ptr->send_msg_ptr.packet_len, stored_msg_ptr->send_msg_ptr.packet_ptr, &coap_version); - if (tmp_coap_hdr_ptr != 0) { - tmp_coap_hdr_ptr->coap_status = COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED; - stored_msg_ptr->coap->sn_coap_rx_callback(tmp_coap_hdr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr, stored_msg_ptr->param); + if (tmp_coap_hdr_ptr != 0) { + tmp_coap_hdr_ptr->coap_status = COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED; + handle->sn_coap_rx_callback(tmp_coap_hdr_ptr, &stored_msg_ptr->send_msg_ptr.dst_addr_ptr, stored_msg_ptr->param); - sn_coap_parser_release_allocated_coap_msg_mem(stored_msg_ptr->coap, tmp_coap_hdr_ptr); - } + sn_coap_parser_release_allocated_coap_msg_mem(handle, tmp_coap_hdr_ptr); } - - /* Free memory of stored message */ - sn_coap_protocol_release_allocated_send_msg_mem(handle, stored_msg_ptr); - } else { - /* Send message */ - stored_msg_ptr->coap->sn_coap_tx_callback(stored_msg_ptr->send_msg_ptr->packet_ptr, - stored_msg_ptr->send_msg_ptr->packet_len, stored_msg_ptr->send_msg_ptr->dst_addr_ptr, stored_msg_ptr->param); - - /* * * Count new Resending time * * */ - stored_msg_ptr->resending_time = sn_coap_calculate_new_resend_time(current_time, - handle->sn_coap_resending_intervall, - stored_msg_ptr->resending_counter); } - /* Callback routine could have wiped the list (eg as a response to sending failed) */ - /* Be super cautious and rescan from the start */ - goto rescan; + + /* Free memory of stored message */ + sn_coap_protocol_release_allocated_send_msg_mem(handle, stored_msg_ptr); + } else { + /* Send message */ + handle->sn_coap_tx_callback(stored_msg_ptr->send_msg_ptr.packet_ptr, + stored_msg_ptr->send_msg_ptr.packet_len, &stored_msg_ptr->send_msg_ptr.dst_addr_ptr, stored_msg_ptr->param); + + /* * * Count new Resending time * * */ + stored_msg_ptr->resending_time = sn_coap_calculate_new_resend_time(current_time, + handle->sn_coap_resending_intervall, + stored_msg_ptr->resending_counter); } + /* Callback routine could have wiped the list (eg as a response to sending failed) */ + /* Be super cautious and rescan from the start */ + goto rescan; } } @@ -920,7 +879,7 @@ static uint8_t sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle uint8_t *send_packet_data_ptr, uint32_t sending_time, void *param) { - coap_send_msg_s *stored_msg_ptr = NULL; + coap_send_msg_s *stored_msg_ptr; /* If both queue parameters are "0" or resending count is "0", then re-sending is disabled */ if (((handle->sn_coap_resending_queue_msgs == 0) && (handle->sn_coap_resending_queue_bytes == 0)) || (handle->sn_coap_resending_count == 0)) { @@ -955,17 +914,16 @@ static uint8_t sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle stored_msg_ptr->resending_time = sending_time; /* Filling of sn_nsdl_transmit_s */ - stored_msg_ptr->send_msg_ptr->protocol = SN_NSDL_PROTOCOL_COAP; - stored_msg_ptr->send_msg_ptr->packet_len = send_packet_data_len; - memcpy(stored_msg_ptr->send_msg_ptr->packet_ptr, send_packet_data_ptr, send_packet_data_len); + stored_msg_ptr->send_msg_ptr.protocol = SN_NSDL_PROTOCOL_COAP; + stored_msg_ptr->send_msg_ptr.packet_len = send_packet_data_len; + memcpy(stored_msg_ptr->send_msg_ptr.packet_ptr, send_packet_data_ptr, send_packet_data_len); /* Filling of sn_nsdl_addr_s */ - stored_msg_ptr->send_msg_ptr->dst_addr_ptr->type = dst_addr_ptr->type; - stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_len = dst_addr_ptr->addr_len; - memcpy(stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_len); - stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port = dst_addr_ptr->port; + stored_msg_ptr->send_msg_ptr.dst_addr_ptr.type = dst_addr_ptr->type; + stored_msg_ptr->send_msg_ptr.dst_addr_ptr.addr_len = dst_addr_ptr->addr_len; + memcpy(stored_msg_ptr->send_msg_ptr.dst_addr_ptr.addr_ptr, dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_len); + stored_msg_ptr->send_msg_ptr.dst_addr_ptr.port = dst_addr_ptr->port; - stored_msg_ptr->coap = handle; stored_msg_ptr->param = param; /* Storing Resending message to Linked list */ @@ -974,44 +932,7 @@ static uint8_t sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle return 1; } -/**************************************************************************//** - * \fn static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id) - * - * \brief Searches stored resending message from Linked list - * - * \param *src_addr_ptr is searching key for searched message - * - * \param msg_id is searching key for searched message - * - * \return Return value is pointer to found stored resending message in Linked - * list or NULL if message not found - *****************************************************************************/ -static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(struct coap_s *handle, - sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id) -{ - /* Loop all stored resending messages Linked list */ - ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) { - /* Get message ID from stored resending message */ - uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8); - temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3]; - - /* If message's Message ID is same than is searched */ - if (temp_msg_id == msg_id) { - /* If message's Source address is same than is searched */ - if (0 == memcmp(src_addr_ptr->addr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, src_addr_ptr->addr_len)) { - /* If message's Source address port is same than is searched */ - if (stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port == src_addr_ptr->port) { - /* * * Message found, return pointer to that stored resending message * * * */ - return stored_msg_ptr->send_msg_ptr; - } - } - } - } - - /* Message not found */ - return NULL; -} /**************************************************************************//** * \fn static void sn_coap_protocol_linked_list_send_msg_remove(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id) * @@ -1021,31 +942,27 @@ static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(struct c * \param msg_id is searching key for removed message *****************************************************************************/ -static void sn_coap_protocol_linked_list_send_msg_remove(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id) +static void sn_coap_protocol_linked_list_send_msg_remove(struct coap_s *handle, const sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id) { /* Loop all stored resending messages in Linked list */ ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) { /* Get message ID from stored resending message */ - uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8); - temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3]; + uint16_t temp_msg_id = read_packet_msg_id(stored_msg_ptr); /* If message's Message ID is same than is searched */ if (temp_msg_id == msg_id) { - /* If message's Source address is same than is searched */ - if (0 == memcmp(src_addr_ptr->addr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, src_addr_ptr->addr_len)) { - /* If message's Source address port is same than is searched */ - if (stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port == src_addr_ptr->port) { - /* * * Message found * * */ + /* If message's Source address and port is same than is searched */ + if (compare_address_and_port(src_addr_ptr, &stored_msg_ptr->send_msg_ptr.dst_addr_ptr)) { + /* * * Message found * * */ - /* Remove message from Linked list */ - ns_list_remove(&handle->linked_list_resent_msgs, stored_msg_ptr); - --handle->count_resent_msgs; + /* Remove message from Linked list */ + ns_list_remove(&handle->linked_list_resent_msgs, stored_msg_ptr); + --handle->count_resent_msgs; - /* Free memory of stored message */ - sn_coap_protocol_release_allocated_send_msg_mem(handle, stored_msg_ptr); + /* Free memory of stored message */ + sn_coap_protocol_release_allocated_send_msg_mem(handle, stored_msg_ptr); - return; - } + return; } } } @@ -1104,32 +1021,26 @@ static void sn_coap_protocol_linked_list_duplication_info_store(struct coap_s *h /* * * * Allocating memory for stored Duplication info * * * */ /* Allocate memory for stored Duplication info's structure */ - stored_duplication_info_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_duplication_info_s)); + stored_duplication_info_ptr = sn_coap_protocol_calloc(handle, sizeof(coap_duplication_info_s)); if (stored_duplication_info_ptr == NULL) { tr_error("sn_coap_protocol_linked_list_duplication_info_store - failed to allocate duplication info!"); return; } - memset(stored_duplication_info_ptr, 0, sizeof(coap_duplication_info_s)); /* Allocate memory for stored Duplication info's address */ - stored_duplication_info_ptr->address = handle->sn_coap_protocol_malloc(sizeof(sn_nsdl_addr_s)); + stored_duplication_info_ptr->address = sn_coap_protocol_calloc(handle, sizeof(sn_nsdl_addr_s)); if (stored_duplication_info_ptr->address == NULL) { tr_error("sn_coap_protocol_linked_list_duplication_info_store - failed to allocate address!"); - handle->sn_coap_protocol_free(stored_duplication_info_ptr); - stored_duplication_info_ptr = 0; + sn_coap_protocol_duplication_info_free(handle, stored_duplication_info_ptr); return; } - memset(stored_duplication_info_ptr->address, 0, sizeof(sn_nsdl_addr_s)); stored_duplication_info_ptr->address->addr_ptr = handle->sn_coap_protocol_malloc(addr_ptr->addr_len); if (stored_duplication_info_ptr->address->addr_ptr == NULL) { tr_error("sn_coap_protocol_linked_list_duplication_info_store - failed to allocate address pointer!"); - handle->sn_coap_protocol_free(stored_duplication_info_ptr->address); - stored_duplication_info_ptr->address = 0; - handle->sn_coap_protocol_free(stored_duplication_info_ptr); - stored_duplication_info_ptr = 0; + sn_coap_protocol_duplication_info_free(handle, stored_duplication_info_ptr); return; } @@ -1140,8 +1051,6 @@ static void sn_coap_protocol_linked_list_duplication_info_store(struct coap_s *h stored_duplication_info_ptr->address->port = addr_ptr->port; stored_duplication_info_ptr->msg_id = msg_id; - stored_duplication_info_ptr->coap = handle; - stored_duplication_info_ptr->param = param; /* * * * Storing Duplication info to Linked list * * * */ @@ -1167,13 +1076,10 @@ static coap_duplication_info_s* sn_coap_protocol_linked_list_duplication_info_se ns_list_foreach(coap_duplication_info_s, stored_duplication_info_ptr, &handle->linked_list_duplication_msgs) { /* If message's Message ID is same than is searched */ if (stored_duplication_info_ptr->msg_id == msg_id) { - /* If message's Source address is same than is searched */ - if (0 == memcmp(addr_ptr->addr_ptr, stored_duplication_info_ptr->address->addr_ptr, addr_ptr->addr_len)) { - /* If message's Source address port is same than is searched */ - if (stored_duplication_info_ptr->address->port == addr_ptr->port) { - /* * * Correct Duplication info found * * * */ - return stored_duplication_info_ptr; - } + /* If message's Source address & port is same than is searched */ + if (compare_address_and_port(addr_ptr, stored_duplication_info_ptr->address)) { + /* * * Correct Duplication info found * * * */ + return stored_duplication_info_ptr; } } } @@ -1190,7 +1096,7 @@ static coap_duplication_info_s* sn_coap_protocol_linked_list_duplication_info_se static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(struct coap_s *handle) { - /* Loop all stored duplication messages in Linked list */ + ns_list_foreach_safe(coap_duplication_info_s, removed_duplication_info_ptr, &handle->linked_list_duplication_msgs) { if ((handle->system_time - removed_duplication_info_ptr->timestamp) > SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED) { /* * * * Old Duplication info found, remove it from Linked list * * * */ @@ -1198,14 +1104,7 @@ static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(struct --handle->count_duplication_msgs; /* Free memory of stored Duplication info */ - handle->sn_coap_protocol_free(removed_duplication_info_ptr->address->addr_ptr); - removed_duplication_info_ptr->address->addr_ptr = 0; - handle->sn_coap_protocol_free(removed_duplication_info_ptr->address); - removed_duplication_info_ptr->address = 0; - handle->sn_coap_protocol_free(removed_duplication_info_ptr->packet_ptr); - removed_duplication_info_ptr->packet_ptr = 0; - handle->sn_coap_protocol_free(removed_duplication_info_ptr); - removed_duplication_info_ptr = 0; + sn_coap_protocol_duplication_info_free(handle, removed_duplication_info_ptr); } } } @@ -1218,9 +1117,9 @@ void sn_coap_protocol_linked_list_duplication_info_remove(struct coap_s *handle, /* Loop all stored duplication messages in Linked list */ ns_list_foreach(coap_duplication_info_s, removed_duplication_info_ptr, &handle->linked_list_duplication_msgs) { /* If message's Address is same than is searched */ - if (handle == removed_duplication_info_ptr->coap && 0 == memcmp(scr_addr_ptr, - removed_duplication_info_ptr->address->addr_ptr, - removed_duplication_info_ptr->address->addr_len)) { + if (0 == memcmp(scr_addr_ptr, + removed_duplication_info_ptr->address->addr_ptr, + removed_duplication_info_ptr->address->addr_len)) { /* If message's Address prt is same than is searched */ if (removed_duplication_info_ptr->address->port == port) { /* If Message ID is same than is searched */ @@ -1231,14 +1130,7 @@ void sn_coap_protocol_linked_list_duplication_info_remove(struct coap_s *handle, --handle->count_duplication_msgs; /* Free memory of stored Duplication info */ - handle->sn_coap_protocol_free(removed_duplication_info_ptr->address->addr_ptr); - removed_duplication_info_ptr->address->addr_ptr = 0; - handle->sn_coap_protocol_free(removed_duplication_info_ptr->address); - removed_duplication_info_ptr->address = 0; - handle->sn_coap_protocol_free(removed_duplication_info_ptr->packet_ptr); - removed_duplication_info_ptr->packet_ptr = 0; - handle->sn_coap_protocol_free(removed_duplication_info_ptr); - removed_duplication_info_ptr = 0; + sn_coap_protocol_duplication_info_free(handle, removed_duplication_info_ptr); return; } } @@ -1251,6 +1143,18 @@ void sn_coap_protocol_linked_list_duplication_info_remove(struct coap_s *handle, (void)msg_id; #endif //SN_COAP_DUPLICATION_MAX_MSGS_COUNT } +static void sn_coap_protocol_duplication_info_free(struct coap_s *handle, coap_duplication_info_s *duplication_info_ptr) +{ + if (duplication_info_ptr) { + if (duplication_info_ptr->address) { + handle->sn_coap_protocol_free(duplication_info_ptr->address->addr_ptr); + handle->sn_coap_protocol_free(duplication_info_ptr->address); + } + handle->sn_coap_protocol_free(duplication_info_ptr->packet_ptr); + handle->sn_coap_protocol_free(duplication_info_ptr); + } +} + #if SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /**************************************************************************//** @@ -1263,21 +1167,15 @@ void sn_coap_protocol_linked_list_duplication_info_remove(struct coap_s *handle, static void sn_coap_protocol_linked_list_blockwise_msg_remove(struct coap_s *handle, coap_blockwise_msg_s *removed_msg_ptr) { - if( removed_msg_ptr->coap == handle ){ - ns_list_remove(&handle->linked_list_blockwise_sent_msgs, removed_msg_ptr); + ns_list_remove(&handle->linked_list_blockwise_sent_msgs, removed_msg_ptr); - if( removed_msg_ptr->coap_msg_ptr ){ - if (removed_msg_ptr->coap_msg_ptr->payload_ptr) { - handle->sn_coap_protocol_free(removed_msg_ptr->coap_msg_ptr->payload_ptr); - removed_msg_ptr->coap_msg_ptr->payload_ptr = 0; - } + if (removed_msg_ptr->coap_msg_ptr) { + handle->sn_coap_protocol_free(removed_msg_ptr->coap_msg_ptr->payload_ptr); - sn_coap_parser_release_allocated_coap_msg_mem(handle, removed_msg_ptr->coap_msg_ptr); - } - - handle->sn_coap_protocol_free(removed_msg_ptr); - removed_msg_ptr = 0; + sn_coap_parser_release_allocated_coap_msg_mem(handle, removed_msg_ptr->coap_msg_ptr); } + + handle->sn_coap_protocol_free(removed_msg_ptr); } /**************************************************************************//** @@ -1302,19 +1200,13 @@ static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s * } // Do not add duplicates to list, this could happen if server needs to retransmit block message again - ns_list_foreach(coap_blockwise_payload_s, payload_info_ptr, &handle->linked_list_blockwise_received_payloads) { - if ((0 == memcmp(addr_ptr->addr_ptr, payload_info_ptr->addr_ptr, addr_ptr->addr_len)) && (payload_info_ptr->port == addr_ptr->port)) { - if (token_ptr) { - if (!payload_info_ptr->token_ptr || (payload_info_ptr->token_len != token_len) || (memcmp(payload_info_ptr->token_ptr, token_ptr, token_len))) { - continue; - } - } else if (payload_info_ptr->token_ptr) { - continue; - } - if (payload_info_ptr->block_number == block_number) { - return; - } - } + + if (sn_coap_protocol_linked_list_blockwise_payload_search_compare_block_number(handle, + addr_ptr, + token_ptr, + token_len, + block_number)) { + return; } coap_blockwise_payload_s *stored_blockwise_payload_ptr = NULL; @@ -1331,33 +1223,29 @@ static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s * /* Allocate memory for stored Payload's data */ - stored_blockwise_payload_ptr->payload_ptr = handle->sn_coap_protocol_malloc(stored_payload_len); + stored_blockwise_payload_ptr->payload_ptr = sn_coap_protocol_malloc_copy(handle, stored_payload_ptr, stored_payload_len); if (stored_blockwise_payload_ptr->payload_ptr == NULL) { tr_error("sn_coap_protocol_linked_list_blockwise_payload_store - failed to allocate payload!"); handle->sn_coap_protocol_free(stored_blockwise_payload_ptr); - stored_blockwise_payload_ptr = 0; return; } /* Allocate memory for stored Payload's address */ - stored_blockwise_payload_ptr->addr_ptr = handle->sn_coap_protocol_malloc(addr_ptr->addr_len); + stored_blockwise_payload_ptr->addr_ptr = sn_coap_protocol_malloc_copy(handle, addr_ptr->addr_ptr, addr_ptr->addr_len); if (stored_blockwise_payload_ptr->addr_ptr == NULL) { tr_error("sn_coap_protocol_linked_list_blockwise_payload_store - failed to allocate address pointer!"); handle->sn_coap_protocol_free(stored_blockwise_payload_ptr->payload_ptr); - stored_blockwise_payload_ptr->payload_ptr = 0; handle->sn_coap_protocol_free(stored_blockwise_payload_ptr); - stored_blockwise_payload_ptr = 0; - return; } /* Allocate & copy token number */ if (token_ptr && token_len) { - stored_blockwise_payload_ptr->token_ptr = handle->sn_coap_protocol_malloc(token_len); + stored_blockwise_payload_ptr->token_ptr = sn_coap_protocol_malloc_copy(handle, token_ptr, token_len); - if(!stored_blockwise_payload_ptr->token_ptr) { + if (!stored_blockwise_payload_ptr->token_ptr) { tr_error("sn_coap_protocol_linked_list_blockwise_payload_store - failed to allocate token pointer!"); handle->sn_coap_protocol_free(stored_blockwise_payload_ptr->addr_ptr); handle->sn_coap_protocol_free(stored_blockwise_payload_ptr->payload_ptr); @@ -1365,7 +1253,6 @@ static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s * return; } - memcpy(stored_blockwise_payload_ptr->token_ptr, token_ptr, token_len); stored_blockwise_payload_ptr->token_len = token_len; } else { stored_blockwise_payload_ptr->token_ptr = NULL; @@ -1376,13 +1263,9 @@ static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s * stored_blockwise_payload_ptr->timestamp = handle->system_time; - memcpy(stored_blockwise_payload_ptr->addr_ptr, addr_ptr->addr_ptr, addr_ptr->addr_len); stored_blockwise_payload_ptr->port = addr_ptr->port; - memcpy(stored_blockwise_payload_ptr->payload_ptr, stored_payload_ptr, stored_payload_len); stored_blockwise_payload_ptr->payload_len = stored_payload_len; - stored_blockwise_payload_ptr->coap = handle; - stored_blockwise_payload_ptr->block_number = block_number; /* * * * Storing Payload to Linked list * * * */ @@ -1401,7 +1284,7 @@ static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s * * list or NULL if payload not found *****************************************************************************/ -static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length, uint8_t *token_ptr, uint8_t token_len) +static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(struct coap_s *handle, const sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length, const uint8_t *token_ptr, uint8_t token_len) { /* Loop all stored blockwise payloads in Linked list */ ns_list_foreach(coap_blockwise_payload_s, stored_payload_info_ptr, &handle->linked_list_blockwise_received_payloads) { @@ -1424,9 +1307,9 @@ static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(struct coa return NULL; } -static bool sn_coap_protocol_linked_list_blockwise_payload_compare_block_number(struct coap_s *handle, - sn_nsdl_addr_s *src_addr_ptr, - uint8_t *token_ptr, +static bool sn_coap_protocol_linked_list_blockwise_payload_search_compare_block_number(struct coap_s *handle, + const sn_nsdl_addr_s *src_addr_ptr, + const uint8_t *token_ptr, uint8_t token_len, uint32_t block_number) { @@ -1442,8 +1325,8 @@ static bool sn_coap_protocol_linked_list_blockwise_payload_compare_block_number( } else if (stored_payload_info_ptr->token_ptr) { continue; } - // Check that incoming block number matches to last received one - if (block_number - 1 == stored_payload_info_ptr->block_number) { + // Check that stored block number matches to given one + if (block_number == stored_payload_info_ptr->block_number) { return true; } } @@ -1492,23 +1375,11 @@ static void sn_coap_protocol_linked_list_blockwise_payload_remove(struct coap_s { ns_list_remove(&handle->linked_list_blockwise_received_payloads, removed_payload_ptr); /* Free memory of stored payload */ - if (removed_payload_ptr->addr_ptr != NULL) { - handle->sn_coap_protocol_free(removed_payload_ptr->addr_ptr); - removed_payload_ptr->addr_ptr = 0; - } - - if (removed_payload_ptr->payload_ptr != NULL) { - handle->sn_coap_protocol_free(removed_payload_ptr->payload_ptr); - removed_payload_ptr->payload_ptr = 0; - } - - if (removed_payload_ptr->token_ptr != NULL) { - handle->sn_coap_protocol_free(removed_payload_ptr->token_ptr); - removed_payload_ptr->token_ptr = 0; - } + handle->sn_coap_protocol_free(removed_payload_ptr->addr_ptr); + handle->sn_coap_protocol_free(removed_payload_ptr->payload_ptr); + handle->sn_coap_protocol_free(removed_payload_ptr->token_ptr); handle->sn_coap_protocol_free(removed_payload_ptr); - removed_payload_ptr = 0; } /**************************************************************************//** @@ -1521,7 +1392,7 @@ static void sn_coap_protocol_linked_list_blockwise_payload_remove(struct coap_s * \return Return value is length of Payloads as bytes *****************************************************************************/ -static uint32_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint8_t *token_ptr, uint8_t token_len) +static uint32_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(struct coap_s *handle, const sn_nsdl_addr_s *src_addr_ptr, const uint8_t *token_ptr, uint8_t token_len) { uint32_t ret_whole_payload_len = 0; /* Loop all stored blockwise payloads in Linked list */ @@ -1574,7 +1445,6 @@ rescan: handle->sn_coap_rx_callback(removed_blocwise_msg_ptr->coap_msg_ptr, NULL, removed_blocwise_msg_ptr->param); callback_called = true; } - handle->sn_coap_protocol_free(removed_blocwise_msg_ptr->coap_msg_ptr->payload_ptr); sn_coap_parser_release_allocated_coap_msg_mem(handle, removed_blocwise_msg_ptr->coap_msg_ptr); } @@ -1618,36 +1488,21 @@ rescan: coap_send_msg_s *sn_coap_protocol_allocate_mem_for_msg(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t packet_data_len) { - coap_send_msg_s *msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_send_msg_s)); + coap_send_msg_s *msg_ptr = sn_coap_protocol_calloc(handle, sizeof(coap_send_msg_s)); if (msg_ptr == NULL) { - return NULL; + return 0; } - //Locall structure for 1 malloc for send msg - struct - { - sn_nsdl_transmit_s transmit; - sn_nsdl_addr_s addr; - uint8_t trail_data[]; - } *m; - int trail_size = dst_addr_ptr->addr_len + packet_data_len; + msg_ptr->send_msg_ptr.packet_ptr = handle->sn_coap_protocol_malloc(packet_data_len); - m = handle->sn_coap_protocol_malloc(sizeof *m + trail_size); - if (!m) { - handle->sn_coap_protocol_free(msg_ptr); - return NULL; - } - //Init data - memset(m, 0, sizeof(*m) + trail_size); - memset(msg_ptr, 0, sizeof(coap_send_msg_s)); + msg_ptr->send_msg_ptr.dst_addr_ptr.addr_ptr = sn_coap_protocol_calloc(handle, dst_addr_ptr->addr_len); - msg_ptr->send_msg_ptr = &m->transmit; - msg_ptr->send_msg_ptr->dst_addr_ptr = &m->addr; + if ((msg_ptr->send_msg_ptr.dst_addr_ptr.addr_ptr == NULL) || + (msg_ptr->send_msg_ptr.packet_ptr == NULL)) { - msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr = m->trail_data; - if (packet_data_len) { - msg_ptr->send_msg_ptr->packet_ptr = m->trail_data + dst_addr_ptr->addr_len; + sn_coap_protocol_release_allocated_send_msg_mem(handle, msg_ptr); + return 0; } return msg_ptr; @@ -1665,10 +1520,12 @@ coap_send_msg_s *sn_coap_protocol_allocate_mem_for_msg(struct coap_s *handle, sn static void sn_coap_protocol_release_allocated_send_msg_mem(struct coap_s *handle, coap_send_msg_s *freed_send_msg_ptr) { if (freed_send_msg_ptr != NULL) { - handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr); - freed_send_msg_ptr->send_msg_ptr = NULL; + + handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr.dst_addr_ptr.addr_ptr); + + handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr.packet_ptr); + handle->sn_coap_protocol_free(freed_send_msg_ptr); - freed_send_msg_ptr = NULL; } } @@ -1684,9 +1541,7 @@ static uint16_t sn_coap_count_linked_list_size(const coap_send_msg_list_t *linke uint16_t total_size = 0; ns_list_foreach(coap_send_msg_s, stored_msg_ptr, linked_list_ptr) { - if (stored_msg_ptr->send_msg_ptr) { - total_size += stored_msg_ptr->send_msg_ptr->packet_len; - } + total_size += stored_msg_ptr->send_msg_ptr.packet_len; } return total_size; @@ -1695,20 +1550,31 @@ static uint16_t sn_coap_count_linked_list_size(const coap_send_msg_list_t *linke #endif #if SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE + +static coap_blockwise_msg_s* search_sent_blockwise_message(struct coap_s *handle, uint16_t msg_id) +{ + ns_list_foreach(coap_blockwise_msg_s, tmp, &handle->linked_list_blockwise_sent_msgs) { + if (tmp->coap_msg_ptr && tmp->coap_msg_ptr->msg_id == msg_id) { + return tmp; + } + } + + return NULL; +} + void sn_coap_protocol_remove_sent_blockwise_message(struct coap_s *handle, uint16_t msg_id) { if (!handle) { return; } - ns_list_foreach_safe(coap_blockwise_msg_s, tmp, &handle->linked_list_blockwise_sent_msgs) { - if (tmp->coap == handle && tmp->coap_msg_ptr && tmp->coap_msg_ptr->msg_id == msg_id) { - handle->sn_coap_protocol_free(tmp->coap_msg_ptr->payload_ptr); - sn_coap_parser_release_allocated_coap_msg_mem(tmp->coap, tmp->coap_msg_ptr); - ns_list_remove(&handle->linked_list_blockwise_sent_msgs, tmp); - handle->sn_coap_protocol_free(tmp); - break; - } + coap_blockwise_msg_s *tmp = search_sent_blockwise_message(handle, msg_id); + + if (tmp) { + handle->sn_coap_protocol_free(tmp->coap_msg_ptr->payload_ptr); + sn_coap_parser_release_allocated_coap_msg_mem(handle, tmp->coap_msg_ptr); + ns_list_remove(&handle->linked_list_blockwise_sent_msgs, tmp); + handle->sn_coap_protocol_free(tmp); } } @@ -1789,15 +1655,10 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn if (received_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) { if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) { if (received_coap_msg_ptr->options_list_ptr->block1 & 0x08) { - coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = NULL; + coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr; /* Get */ - ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) { - if (msg->coap_msg_ptr && received_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) { - stored_blockwise_msg_temp_ptr = msg; - break; - } - } + stored_blockwise_msg_temp_ptr = search_sent_blockwise_message(handle, received_coap_msg_ptr->msg_id); if (stored_blockwise_msg_temp_ptr) { /* Build response message */ @@ -1820,7 +1681,6 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn } else { if (!sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr)) { tr_error("sn_coap_handle_blockwise_message - (send block1) failed to allocate ack message!"); - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); return 0; } } @@ -1851,19 +1711,12 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn if (!dst_ack_packet_data_ptr) { tr_error("sn_coap_handle_blockwise_message - (send block1) failed to allocate ack message!"); handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr); - src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0; handle->sn_coap_protocol_free(original_payload_ptr); - original_payload_ptr = 0; handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr); - src_coap_blockwise_ack_msg_ptr = 0; stored_blockwise_msg_temp_ptr->coap_msg_ptr = NULL; - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); return NULL; } - src_coap_blockwise_ack_msg_ptr->msg_id = message_id++; - if (message_id == 0) { - message_id = 1; - } + src_coap_blockwise_ack_msg_ptr->msg_id = get_new_message_id(); sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size); @@ -1895,7 +1748,6 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn } else { // XXX what was this trying to free? received_coap_msg_ptr->coap_status = COAP_STATUS_OK; - } } @@ -1906,15 +1758,15 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn } // Check that incoming block number is in order. - uint32_t block_number = received_coap_msg_ptr->options_list_ptr->block1 >> 4; + const uint32_t block_number = received_coap_msg_ptr->options_list_ptr->block1 >> 4; bool blocks_in_order = true; if (block_number > 0 && - !sn_coap_protocol_linked_list_blockwise_payload_compare_block_number(handle, + !sn_coap_protocol_linked_list_blockwise_payload_search_compare_block_number(handle, src_addr_ptr, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len, - block_number)) { + block_number - 1)) { blocks_in_order = false; } @@ -1929,18 +1781,10 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn /* If not last block (more value is set) */ /* Block option length can be 1-3 bytes. First 4-20 bits are for block number. Last 4 bits are ALWAYS more bit + block size. */ if (received_coap_msg_ptr->options_list_ptr->block1 & 0x08) { - src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message(handle); + + src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message_with_options(handle); if (src_coap_blockwise_ack_msg_ptr == NULL) { tr_error("sn_coap_handle_blockwise_message - (recv block1) failed to allocate ack message!"); - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); - return NULL; - } - - if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) { - tr_error("sn_coap_handle_blockwise_message - (recv block1) failed to allocate options!"); - handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr); - src_coap_blockwise_ack_msg_ptr = 0; - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); return NULL; } @@ -1987,9 +1831,8 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn src_coap_blockwise_ack_msg_ptr->msg_id = received_coap_msg_ptr->msg_id; // Copy token to response - src_coap_blockwise_ack_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(received_coap_msg_ptr->token_len); + src_coap_blockwise_ack_msg_ptr->token_ptr = sn_coap_protocol_malloc_copy(handle, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); if (src_coap_blockwise_ack_msg_ptr->token_ptr) { - memcpy(src_coap_blockwise_ack_msg_ptr->token_ptr, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); src_coap_blockwise_ack_msg_ptr->token_len = received_coap_msg_ptr->token_len; } @@ -1998,11 +1841,8 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem); if (!dst_ack_packet_data_ptr) { tr_error("sn_coap_handle_blockwise_message - (recv block1) message allocation failed!"); - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr); - src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0; handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr); - src_coap_blockwise_ack_msg_ptr = 0; return NULL; } @@ -2032,32 +1872,10 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn /* * * This is the last block when whole Blockwise payload from received * * */ /* * * blockwise messages is gathered and returned to User * * */ - /* Store last Blockwise payload to Linked list */ - uint16_t payload_len = 0; - uint8_t *payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); - uint32_t whole_payload_len = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); - uint8_t *temp_whole_payload_ptr = NULL; + if (!sn_coap_handle_last_blockwise(handle, src_addr_ptr, received_coap_msg_ptr)) { - temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len); - if (temp_whole_payload_ptr == NULL || whole_payload_len > UINT16_MAX) { - tr_error("sn_coap_handle_blockwise_message - (recv block1) failed to allocate all blocks!"); - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); - handle->sn_coap_protocol_free(temp_whole_payload_ptr); - return 0; + return NULL; } - - // In block message case, payload_ptr freeing must be done in application level - received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr; - received_coap_msg_ptr->payload_len = whole_payload_len; - - /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */ - while (payload_ptr != NULL) { - memcpy(temp_whole_payload_ptr, payload_ptr, payload_len); - temp_whole_payload_ptr += payload_len; - sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); - payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); - } - received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED; } } } @@ -2068,9 +1886,9 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn else { //This is response to request we made if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) { +#if SN_COAP_BLOCKWISE_INTERNAL_BLOCK_2_HANDLING_ENABLED if (handle->sn_coap_internal_block2_resp_handling) { uint32_t block_number = 0; - /* Store blockwise payload to Linked list */ //todo: add block number to stored values - just to make sure all packets are in order sn_coap_protocol_linked_list_blockwise_payload_store(handle, @@ -2082,43 +1900,25 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn received_coap_msg_ptr->options_list_ptr->block2 >> 4); /* If not last block (more value is set) */ if (received_coap_msg_ptr->options_list_ptr->block2 & 0x08) { - coap_blockwise_msg_s *previous_blockwise_msg_ptr = NULL; + coap_blockwise_msg_s *previous_blockwise_msg_ptr; //build and send ack received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING; - ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) { - if (received_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) { - previous_blockwise_msg_ptr = msg; - break; - } - } + previous_blockwise_msg_ptr = search_sent_blockwise_message(handle, received_coap_msg_ptr->msg_id); if (!previous_blockwise_msg_ptr || !previous_blockwise_msg_ptr->coap_msg_ptr) { tr_error("sn_coap_handle_blockwise_message - (send block2) previous message null!"); - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); - return 0; - } - - src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message(handle); - if (src_coap_blockwise_ack_msg_ptr == NULL) { - tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate message!"); return 0; } /* * * Then build CoAP Acknowledgement message * * */ - - if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) { - tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate options!"); - handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr); - src_coap_blockwise_ack_msg_ptr = 0; - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); + src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message_with_options(handle); + if (src_coap_blockwise_ack_msg_ptr == NULL) { + tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate message!"); return NULL; } - src_coap_blockwise_ack_msg_ptr->msg_id = message_id++; - if (message_id == 0) { - message_id = 1; - } + src_coap_blockwise_ack_msg_ptr->msg_id = get_new_message_id(); /* Update block option */ block_temp = received_coap_msg_ptr->options_list_ptr->block2 & 0x07; @@ -2132,80 +1932,64 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn /* Set BLOCK2 (subsequent) GET msg code and copy uri path from previous msg*/ src_coap_blockwise_ack_msg_ptr->msg_code = previous_blockwise_msg_ptr->coap_msg_ptr->msg_code; if (previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_ptr) { - src_coap_blockwise_ack_msg_ptr->uri_path_len = previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_len; - src_coap_blockwise_ack_msg_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_len); + src_coap_blockwise_ack_msg_ptr->uri_path_ptr = sn_coap_protocol_malloc_copy(handle, previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_ptr, previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_len); + if (!src_coap_blockwise_ack_msg_ptr->uri_path_ptr) { sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr); tr_error("sn_coap_handle_blockwise_message - failed to allocate for uri path ptr!"); return NULL; } - memcpy(src_coap_blockwise_ack_msg_ptr->uri_path_ptr, previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_ptr, previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_len); + src_coap_blockwise_ack_msg_ptr->uri_path_len = previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_len; } if (previous_blockwise_msg_ptr->coap_msg_ptr->token_ptr) { - src_coap_blockwise_ack_msg_ptr->token_len = previous_blockwise_msg_ptr->coap_msg_ptr->token_len; - src_coap_blockwise_ack_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(previous_blockwise_msg_ptr->coap_msg_ptr->token_len); + + src_coap_blockwise_ack_msg_ptr->token_ptr = sn_coap_protocol_malloc_copy(handle, previous_blockwise_msg_ptr->coap_msg_ptr->token_ptr, previous_blockwise_msg_ptr->coap_msg_ptr->token_len); if (!src_coap_blockwise_ack_msg_ptr->token_ptr) { sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr); tr_error("sn_coap_handle_blockwise_message - failed to allocate for token ptr!"); return NULL; } - memcpy(src_coap_blockwise_ack_msg_ptr->token_ptr, previous_blockwise_msg_ptr->coap_msg_ptr->token_ptr, previous_blockwise_msg_ptr->coap_msg_ptr->token_len); + src_coap_blockwise_ack_msg_ptr->token_len = received_coap_msg_ptr->token_len; } - ns_list_remove(&handle->linked_list_blockwise_sent_msgs, previous_blockwise_msg_ptr); - if (previous_blockwise_msg_ptr->coap_msg_ptr) { - if (previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr) { - handle->sn_coap_protocol_free(previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr); - previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr = 0; - } - sn_coap_parser_release_allocated_coap_msg_mem(handle, previous_blockwise_msg_ptr->coap_msg_ptr); - previous_blockwise_msg_ptr->coap_msg_ptr = 0; - } - handle->sn_coap_protocol_free(previous_blockwise_msg_ptr); + sn_coap_protocol_linked_list_blockwise_msg_remove(handle, previous_blockwise_msg_ptr); + previous_blockwise_msg_ptr = 0; /* Then get needed memory count for Packet data */ - dst_packed_data_needed_mem = sn_coap_builder_calc_needed_packet_data_size_2(src_coap_blockwise_ack_msg_ptr ,handle->sn_coap_block_data_size); + dst_packed_data_needed_mem = sn_coap_builder_calc_needed_packet_data_size_2(src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size); /* Then allocate memory for Packet data */ - dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem); + dst_ack_packet_data_ptr = sn_coap_protocol_calloc(handle, dst_packed_data_needed_mem); if (dst_ack_packet_data_ptr == NULL) { tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate packet!"); sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr); - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); return NULL; } - memset(dst_ack_packet_data_ptr, 0, dst_packed_data_needed_mem); /* * * Then build Acknowledgement message to Packed data * * */ if ((sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size)) < 0) { tr_error("sn_coap_handle_blockwise_message - (send block2) builder failed!"); handle->sn_coap_protocol_free(dst_ack_packet_data_ptr); - dst_ack_packet_data_ptr = 0; sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr); - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); return NULL; } /* * * Save to linked list * * */ coap_blockwise_msg_s *stored_blockwise_msg_ptr; - stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s)); + stored_blockwise_msg_ptr = sn_coap_protocol_calloc(handle, sizeof(coap_blockwise_msg_s)); if (!stored_blockwise_msg_ptr) { tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate blockwise message!"); handle->sn_coap_protocol_free(dst_ack_packet_data_ptr); - dst_ack_packet_data_ptr = 0; sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr); - sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); return 0; } - memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s)); stored_blockwise_msg_ptr->timestamp = handle->system_time; stored_blockwise_msg_ptr->coap_msg_ptr = src_coap_blockwise_ack_msg_ptr; - stored_blockwise_msg_ptr->coap = handle; stored_blockwise_msg_ptr->param = param; stored_blockwise_msg_ptr->msg_id = stored_blockwise_msg_ptr->coap_msg_ptr->msg_id; ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr); @@ -2230,35 +2014,20 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn /* * * This is the last block when whole Blockwise payload from received * * */ /* * * blockwise messages is gathered and returned to User * * */ - /* Store last Blockwise payload to Linked list */ - uint16_t payload_len = 0; - uint8_t *payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); - uint16_t whole_payload_len = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); - uint8_t *temp_whole_payload_ptr = NULL; + if (!sn_coap_handle_last_blockwise(handle, src_addr_ptr, received_coap_msg_ptr)) { - temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len); - if (!temp_whole_payload_ptr) { - tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate whole payload!"); - return 0; + return NULL; } - received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr; - received_coap_msg_ptr->payload_len = whole_payload_len; - - /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */ - while (payload_ptr != NULL) { - memcpy(temp_whole_payload_ptr, payload_ptr, payload_len); - - temp_whole_payload_ptr += payload_len; - - sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); - payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); - } - received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED; - //todo: remove previous msg from list } } +#else + // If the internal handling is disabled, this code should not be ran unless + // the client messed up in its own handler. + tr_error("sn_coap_handle_blockwise_message - (send block2) - callback not set"); + return NULL; +#endif } //Now we send data to request @@ -2267,13 +2036,14 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn //NOTE: Getting the first from list might not be correct one coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = sn_coap_stored_blockwise_msg_get(handle, received_coap_msg_ptr); if (stored_blockwise_msg_temp_ptr) { - uint16_t block_size; - uint32_t block_number; + + block_temp = received_coap_msg_ptr->options_list_ptr->block2 & 0x07; /* Resolve block parameters */ - block_number = received_coap_msg_ptr->options_list_ptr->block2 >> 4; - block_temp = received_coap_msg_ptr->options_list_ptr->block2 & 0x07; - block_size = 1u << (block_temp + 4); + const uint16_t block_size = 1u << (block_temp + 4); + const uint32_t block_number = received_coap_msg_ptr->options_list_ptr->block2 >> 4; + + /* Build response message */ src_coap_blockwise_ack_msg_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr; @@ -2318,9 +2088,8 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn src_coap_blockwise_ack_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) { if (received_coap_msg_ptr->token_len && src_coap_blockwise_ack_msg_ptr->token_ptr) { handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->token_ptr); - src_coap_blockwise_ack_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(received_coap_msg_ptr->token_len); + src_coap_blockwise_ack_msg_ptr->token_ptr = sn_coap_protocol_malloc_copy(handle, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); if (src_coap_blockwise_ack_msg_ptr->token_ptr) { - memcpy(src_coap_blockwise_ack_msg_ptr->token_ptr, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); src_coap_blockwise_ack_msg_ptr->token_len = received_coap_msg_ptr->token_len; } } @@ -2332,10 +2101,7 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem); if (!dst_ack_packet_data_ptr) { tr_error("sn_coap_handle_blockwise_message - (recv block2) failed to allocate packet!"); - if(original_payload_ptr){ - handle->sn_coap_protocol_free(original_payload_ptr); - original_payload_ptr = NULL; - } + handle->sn_coap_protocol_free(original_payload_ptr); sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr); stored_blockwise_msg_temp_ptr->coap_msg_ptr = NULL; return NULL; @@ -2368,6 +2134,37 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn return received_coap_msg_ptr; } +static bool sn_coap_handle_last_blockwise(struct coap_s *handle, const sn_nsdl_addr_s *src_addr_ptr, sn_coap_hdr_s *received_coap_msg_ptr) +{ + /* Store last Blockwise payload to Linked list */ + uint16_t payload_len = 0; + uint8_t *payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); + uint32_t whole_payload_len = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); + uint8_t *temp_whole_payload_ptr = NULL; + + temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len); + if (temp_whole_payload_ptr == NULL || whole_payload_len > UINT16_MAX) { + tr_error("sn_coap_handle_blockwise_message - (recv block) failed to allocate all blocks, len: %" PRIu32, whole_payload_len); + handle->sn_coap_protocol_free(temp_whole_payload_ptr); + return false; + } + + // In block message case, payload_ptr freeing must be done in application level + received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr; + received_coap_msg_ptr->payload_len = whole_payload_len; + + /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */ + while (payload_ptr != NULL) { + memcpy(temp_whole_payload_ptr, payload_ptr, payload_len); + temp_whole_payload_ptr += payload_len; + sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); + payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len); + } + received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED; + + return true; +} + int8_t sn_coap_convert_block_size(uint16_t block_size) { if (block_size == 16) { @@ -2384,12 +2181,12 @@ int8_t sn_coap_convert_block_size(uint16_t block_size) return 5; } else if (block_size == 1024) { return 6; + } else { + return 0; } - - return 0; } -static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coap_hdr_s *source_header_ptr) +static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, const sn_coap_hdr_s *source_header_ptr) { sn_coap_hdr_s *destination_header_ptr; @@ -2406,24 +2203,22 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa if (source_header_ptr->uri_path_ptr) { destination_header_ptr->uri_path_len = source_header_ptr->uri_path_len; - destination_header_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->uri_path_len); + destination_header_ptr->uri_path_ptr = sn_coap_protocol_malloc_copy(handle, source_header_ptr->uri_path_ptr, source_header_ptr->uri_path_len); if (!destination_header_ptr->uri_path_ptr) { tr_error("sn_coap_protocol_copy_header - failed to allocate uri path!"); sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr); return 0; } - memcpy(destination_header_ptr->uri_path_ptr, source_header_ptr->uri_path_ptr, source_header_ptr->uri_path_len); } if (source_header_ptr->token_ptr) { destination_header_ptr->token_len = source_header_ptr->token_len; - destination_header_ptr->token_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->token_len); + destination_header_ptr->token_ptr = sn_coap_protocol_malloc_copy(handle, source_header_ptr->token_ptr, source_header_ptr->token_len); if (!destination_header_ptr->token_ptr) { sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr); tr_error("sn_coap_protocol_copy_header - failed to allocate token!"); return 0; } - memcpy(destination_header_ptr->token_ptr, source_header_ptr->token_ptr, source_header_ptr->token_len); } destination_header_ptr->content_format = source_header_ptr->content_format; @@ -2436,81 +2231,78 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa return 0; } - destination_header_ptr->options_list_ptr->max_age = source_header_ptr->options_list_ptr->max_age; + const sn_coap_options_list_s *source_options_list_ptr = source_header_ptr->options_list_ptr; + sn_coap_options_list_s *destination_options_list_ptr = destination_header_ptr->options_list_ptr; - if (source_header_ptr->options_list_ptr->proxy_uri_ptr) { - destination_header_ptr->options_list_ptr->proxy_uri_len = source_header_ptr->options_list_ptr->proxy_uri_len; - destination_header_ptr->options_list_ptr->proxy_uri_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->proxy_uri_len); - if (!destination_header_ptr->options_list_ptr->proxy_uri_ptr) { + destination_options_list_ptr->max_age = source_options_list_ptr->max_age; + + if (source_options_list_ptr->proxy_uri_ptr) { + destination_options_list_ptr->proxy_uri_len = source_options_list_ptr->proxy_uri_len; + destination_options_list_ptr->proxy_uri_ptr = sn_coap_protocol_malloc_copy(handle, source_options_list_ptr->proxy_uri_ptr, source_options_list_ptr->proxy_uri_len); + if (!destination_options_list_ptr->proxy_uri_ptr) { sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr); tr_error("sn_coap_protocol_copy_header - failed to allocate proxy uri!"); return 0; } - memcpy(destination_header_ptr->options_list_ptr->proxy_uri_ptr, source_header_ptr->options_list_ptr->proxy_uri_ptr, source_header_ptr->options_list_ptr->proxy_uri_len); } - if (source_header_ptr->options_list_ptr->etag_ptr) { - destination_header_ptr->options_list_ptr->etag_len = source_header_ptr->options_list_ptr->etag_len; - destination_header_ptr->options_list_ptr->etag_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->etag_len); - if (!destination_header_ptr->options_list_ptr->etag_ptr) { + if (source_options_list_ptr->etag_ptr) { + destination_options_list_ptr->etag_len = source_options_list_ptr->etag_len; + destination_options_list_ptr->etag_ptr = sn_coap_protocol_malloc_copy(handle, source_options_list_ptr->etag_ptr, source_options_list_ptr->etag_len); + if (!destination_options_list_ptr->etag_ptr) { sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr); tr_error("sn_coap_protocol_copy_header - failed to allocate etag!"); return 0; } - memcpy(destination_header_ptr->options_list_ptr->etag_ptr, source_header_ptr->options_list_ptr->etag_ptr, source_header_ptr->options_list_ptr->etag_len); } - if (source_header_ptr->options_list_ptr->uri_host_ptr) { - destination_header_ptr->options_list_ptr->uri_host_len = source_header_ptr->options_list_ptr->uri_host_len; - destination_header_ptr->options_list_ptr->uri_host_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_host_len); - if (!destination_header_ptr->options_list_ptr->uri_host_ptr) { + if (source_options_list_ptr->uri_host_ptr) { + destination_options_list_ptr->uri_host_len = source_options_list_ptr->uri_host_len; + destination_options_list_ptr->uri_host_ptr = sn_coap_protocol_malloc_copy(handle, source_options_list_ptr->uri_host_ptr, source_options_list_ptr->uri_host_len); + if (!destination_options_list_ptr->uri_host_ptr) { sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr); tr_error("sn_coap_protocol_copy_header - failed to allocate uri host!"); return 0; } - memcpy(destination_header_ptr->options_list_ptr->uri_host_ptr, source_header_ptr->options_list_ptr->uri_host_ptr, source_header_ptr->options_list_ptr->uri_host_len); } - if (source_header_ptr->options_list_ptr->location_path_ptr) { - destination_header_ptr->options_list_ptr->location_path_len = source_header_ptr->options_list_ptr->location_path_len; - destination_header_ptr->options_list_ptr->location_path_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->location_path_len); - if (!destination_header_ptr->options_list_ptr->location_path_ptr) { + if (source_options_list_ptr->location_path_ptr) { + destination_options_list_ptr->location_path_len = source_options_list_ptr->location_path_len; + destination_options_list_ptr->location_path_ptr = sn_coap_protocol_malloc_copy(handle, source_options_list_ptr->location_path_ptr, source_options_list_ptr->location_path_len); + if (!destination_options_list_ptr->location_path_ptr) { tr_error("sn_coap_protocol_copy_header - failed to allocate location path!"); sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr); return 0; } - memcpy(destination_header_ptr->options_list_ptr->location_path_ptr, source_header_ptr->options_list_ptr->location_path_ptr, source_header_ptr->options_list_ptr->location_path_len); } - destination_header_ptr->options_list_ptr->uri_port = source_header_ptr->options_list_ptr->uri_port; + destination_options_list_ptr->uri_port = source_options_list_ptr->uri_port; - if (source_header_ptr->options_list_ptr->location_query_ptr) { - destination_header_ptr->options_list_ptr->location_query_len = source_header_ptr->options_list_ptr->location_query_len; - destination_header_ptr->options_list_ptr->location_query_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->location_query_len); - if (!destination_header_ptr->options_list_ptr->location_query_ptr) { + if (source_options_list_ptr->location_query_ptr) { + destination_options_list_ptr->location_query_len = source_options_list_ptr->location_query_len; + destination_options_list_ptr->location_query_ptr = sn_coap_protocol_malloc_copy(handle, source_options_list_ptr->location_query_ptr, source_options_list_ptr->location_query_len); + if (!destination_options_list_ptr->location_query_ptr) { sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr); tr_error("sn_coap_protocol_copy_header - failed to allocate location query!"); return 0; } - memcpy(destination_header_ptr->options_list_ptr->location_query_ptr, source_header_ptr->options_list_ptr->location_query_ptr, source_header_ptr->options_list_ptr->location_query_len); } - destination_header_ptr->options_list_ptr->observe = source_header_ptr->options_list_ptr->observe; - destination_header_ptr->options_list_ptr->accept = source_header_ptr->options_list_ptr->accept; + destination_options_list_ptr->observe = source_options_list_ptr->observe; + destination_options_list_ptr->accept = source_options_list_ptr->accept; - if (source_header_ptr->options_list_ptr->uri_query_ptr) { - destination_header_ptr->options_list_ptr->uri_query_len = source_header_ptr->options_list_ptr->uri_query_len; - destination_header_ptr->options_list_ptr->uri_query_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_query_len); - if (!destination_header_ptr->options_list_ptr->uri_query_ptr) { + if (source_options_list_ptr->uri_query_ptr) { + destination_options_list_ptr->uri_query_len = source_options_list_ptr->uri_query_len; + destination_options_list_ptr->uri_query_ptr = sn_coap_protocol_malloc_copy(handle, source_options_list_ptr->uri_query_ptr, source_options_list_ptr->uri_query_len); + if (!destination_options_list_ptr->uri_query_ptr) { sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr); tr_error("sn_coap_protocol_copy_header - failed to allocate uri query!"); return 0; } - memcpy(destination_header_ptr->options_list_ptr->uri_query_ptr, source_header_ptr->options_list_ptr->uri_query_ptr, source_header_ptr->options_list_ptr->uri_query_len); } - destination_header_ptr->options_list_ptr->block1 = source_header_ptr->options_list_ptr->block1; - destination_header_ptr->options_list_ptr->block2 = source_header_ptr->options_list_ptr->block2; + destination_options_list_ptr->block1 = source_options_list_ptr->block1; + destination_options_list_ptr->block2 = source_options_list_ptr->block2; } return destination_header_ptr; @@ -2545,3 +2337,50 @@ static bool sn_coap_protocol_update_duplicate_package_data(const struct coap_s * return true; } #endif + +void *sn_coap_protocol_malloc_copy(struct coap_s *handle, const void *source, uint16_t length) +{ + void *dest = handle->sn_coap_protocol_malloc(length); + + if ((dest) && (source)) { + memcpy(dest, source, length); + } + return dest; +} + +/* + * This should logically be part and accessed via of the coap_s just as malloc() & free() + * are, but that would require the client to fill one up, as a wrapper filled from this + * class would need access to the handle itself. + */ +void *sn_coap_protocol_calloc(struct coap_s *handle, uint16_t length) +{ + void *result = handle->sn_coap_protocol_malloc(length); + + if (result) { + memset(result, 0, length); + } + return result; +} + +static bool compare_address_and_port(const sn_nsdl_addr_s* left, const sn_nsdl_addr_s* right) +{ + bool match = false; + + if (left->port == right->port) { + if (0 == memcmp(left->addr_ptr, right->addr_ptr, left->addr_len)) { + match = true; + } + } + + return match; +} + +static uint16_t get_new_message_id(void) +{ + message_id++; + if (message_id == 0) { + message_id = 1; + } + return message_id; +}