Merge pull request #5001 from anttiylitokola/master

Update mbed-coap to version 4.0.10
pull/4776/head
Martin Kojtal 2017-09-04 16:47:10 +01:00 committed by GitHub
commit 0fec52de94
6 changed files with 121 additions and 33 deletions

View File

@ -1,5 +1,12 @@
# Change Log # Change Log
## [v4.0.10](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.0.10)
-[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.0.9...v4.0.10)
**Closed issues:**
- IOTMAC-615 Node mDS registration failure during OTA transfer
## [v4.0.9](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.0.9) ## [v4.0.9](https://github.com/ARMmbed/mbed-coap/releases/tag/v4.0.9)
-[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.0.8...v4.0.9) -[Full Changelog](https://github.com/ARMmbed/mbed-coap/compare/v4.0.8...v4.0.9)

View File

@ -1,6 +1,6 @@
{ {
"name": "mbed-coap", "name": "mbed-coap",
"version": "4.0.9", "version": "4.0.10",
"description": "COAP library", "description": "COAP library",
"keywords": [ "keywords": [
"coap", "coap",

View File

@ -59,6 +59,7 @@ sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap
coap_res_ptr = sn_coap_parser_alloc_message(handle); coap_res_ptr = sn_coap_parser_alloc_message(handle);
if (!coap_res_ptr) { if (!coap_res_ptr) {
tr_error("sn_coap_build_response - failed to allocate message!");
return NULL; return NULL;
} }
@ -83,6 +84,7 @@ sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap
coap_res_ptr->token_len = coap_packet_ptr->token_len; 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 = handle->sn_coap_protocol_malloc(coap_res_ptr->token_len);
if (!coap_res_ptr->token_ptr) { if (!coap_res_ptr->token_ptr) {
tr_error("sn_coap_build_response - failed to allocate token!");
handle->sn_coap_protocol_free(coap_res_ptr); handle->sn_coap_protocol_free(coap_res_ptr);
return NULL; return NULL;
} }
@ -98,7 +100,6 @@ int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_ms
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, sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size)
{ {
tr_debug("sn_coap_builder_2");
uint8_t *base_packet_data_ptr = NULL; uint8_t *base_packet_data_ptr = NULL;
/* * * * Check given pointers * * * */ /* * * * Check given pointers * * * */
@ -108,8 +109,8 @@ int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_
/* Initialize given Packet data memory area with zero values */ /* Initialize given Packet data memory area with zero values */
uint16_t dst_byte_count_to_be_built = sn_coap_builder_calc_needed_packet_data_size_2(src_coap_msg_ptr, blockwise_payload_size); uint16_t dst_byte_count_to_be_built = sn_coap_builder_calc_needed_packet_data_size_2(src_coap_msg_ptr, blockwise_payload_size);
tr_debug("sn_coap_builder_2 - message len: [%d]", dst_byte_count_to_be_built);
if (!dst_byte_count_to_be_built) { if (!dst_byte_count_to_be_built) {
tr_error("sn_coap_builder_2 - failed to allocate message!");
return -1; return -1;
} }
@ -123,6 +124,7 @@ int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_
/* * * * * * * * * * * * * * * * * * */ /* * * * * * * * * * * * * * * * * * */
if (sn_coap_builder_header_build(&dst_packet_data_ptr, src_coap_msg_ptr) != 0) { if (sn_coap_builder_header_build(&dst_packet_data_ptr, src_coap_msg_ptr) != 0) {
/* Header building failed */ /* Header building failed */
tr_error("sn_coap_builder_2 - header building failed!");
return -1; return -1;
} }
@ -149,7 +151,6 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_ms
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(sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size)
{ {
(void)blockwise_payload_size; (void)blockwise_payload_size;
tr_debug("sn_coap_builder_calc_needed_packet_data_size_2");
uint16_t returned_byte_count = 0; uint16_t returned_byte_count = 0;
if (!src_coap_msg_ptr) { if (!src_coap_msg_ptr) {
@ -168,6 +169,7 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
/* TOKEN - Length is 1-8 bytes */ /* TOKEN - Length is 1-8 bytes */
if (src_coap_msg_ptr->token_ptr != NULL) { if (src_coap_msg_ptr->token_ptr != NULL) {
if (src_coap_msg_ptr->token_len > 8 || src_coap_msg_ptr->token_len < 1) { /* Check that option is not longer than defined */ if (src_coap_msg_ptr->token_len > 8 || src_coap_msg_ptr->token_len < 1) { /* Check that option is not longer than defined */
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - token too large!");
return 0; return 0;
} }
@ -180,6 +182,7 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
if (repeatable_option_size) { if (repeatable_option_size) {
returned_byte_count += repeatable_option_size; returned_byte_count += repeatable_option_size;
} else { } else {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - uri path size failed!");
return 0; return 0;
} }
} }
@ -188,6 +191,7 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
/* CONTENT FORMAT - An integer option, up to 2 bytes */ /* CONTENT FORMAT - An integer option, up to 2 bytes */
if (src_coap_msg_ptr->content_format != COAP_CT_NONE) { if (src_coap_msg_ptr->content_format != COAP_CT_NONE) {
if ((uint32_t) src_coap_msg_ptr->content_format > 0xffff) { if ((uint32_t) src_coap_msg_ptr->content_format > 0xffff) {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - content format too large!");
return 0; return 0;
} }
@ -198,6 +202,7 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
/* ACCEPT - An integer option, up to 2 bytes */ /* ACCEPT - An integer option, up to 2 bytes */
if (src_coap_msg_ptr->options_list_ptr->accept != COAP_CT_NONE) { 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 ((uint32_t) src_coap_msg_ptr->options_list_ptr->accept > 0xffff) {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - accept too large!");
return 0; return 0;
} }
@ -222,6 +227,7 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
} }
else { else {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - proxy uri too large!");
return 0; return 0;
} }
@ -235,6 +241,7 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
if (repeatable_option_size) { if (repeatable_option_size) {
returned_byte_count += repeatable_option_size; returned_byte_count += repeatable_option_size;
} else { } else {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - etag too large!");
return 0; return 0;
} }
} }
@ -249,6 +256,7 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
} }
else { else {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - uri host too large!");
return 0; return 0;
} }
@ -261,12 +269,14 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
if (repeatable_option_size) { if (repeatable_option_size) {
returned_byte_count += repeatable_option_size; returned_byte_count += repeatable_option_size;
} else { } else {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - location path too large!");
return 0; return 0;
} }
} }
/* URI PORT - An integer option, up to 2 bytes */ /* 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 (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 ((uint32_t) src_coap_msg_ptr->options_list_ptr->uri_port > 0xffff) {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - uri port too large!");
return 0; 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_coap_msg_ptr->options_list_ptr->uri_port, COAP_OPTION_URI_PORT, &tempInt);
@ -278,6 +288,7 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
if (repeatable_option_size) { if (repeatable_option_size) {
returned_byte_count += repeatable_option_size; returned_byte_count += repeatable_option_size;
} else { } else {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - location query too large!");
return 0; return 0;
} }
} }
@ -295,6 +306,7 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
if (repeatable_option_size) { if (repeatable_option_size) {
returned_byte_count += repeatable_option_size; returned_byte_count += repeatable_option_size;
} else { } else {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - observe too large!");
return 0; return 0;
} }
} }
@ -302,6 +314,7 @@ 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 */ /* BLOCK 1 - An integer option, up to 3 bytes */
if (src_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) { 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 ((uint32_t) src_coap_msg_ptr->options_list_ptr->block1 > 0xffffff) {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - block1 too large!");
return 0; 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_coap_msg_ptr->options_list_ptr->block1, COAP_OPTION_BLOCK1, &tempInt);
@ -313,6 +326,7 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_
/* BLOCK 2 - An integer option, up to 3 bytes */ /* BLOCK 2 - An integer option, up to 3 bytes */
if (src_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) { 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 ((uint32_t) src_coap_msg_ptr->options_list_ptr->block2 > 0xffffff) {
tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - block2 too large!");
return 0; 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_coap_msg_ptr->options_list_ptr->block2, COAP_OPTION_BLOCK2, &tempInt);
@ -483,6 +497,7 @@ static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, sn_co
{ {
/* * * * Check validity of Header values * * * */ /* * * * Check validity of Header values * * * */
if (sn_coap_header_validity_check(src_coap_msg_ptr, COAP_VERSION) != 0) { if (sn_coap_header_validity_check(src_coap_msg_ptr, COAP_VERSION) != 0) {
tr_error("sn_coap_builder_header_build - header build failed!");
return -1; return -1;
} }
@ -526,6 +541,7 @@ static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_c
/* * * * Check if Options are used at all * * * */ /* * * * Check if Options are used at all * * * */
if (src_coap_msg_ptr->uri_path_ptr == NULL && src_coap_msg_ptr->token_ptr == NULL && if (src_coap_msg_ptr->uri_path_ptr == NULL && src_coap_msg_ptr->token_ptr == NULL &&
src_coap_msg_ptr->content_format == COAP_CT_NONE && src_coap_msg_ptr->options_list_ptr == NULL) { src_coap_msg_ptr->content_format == COAP_CT_NONE && src_coap_msg_ptr->options_list_ptr == NULL) {
tr_error("sn_coap_builder_options_build - options not used!");
return 0; return 0;
} }

View File

@ -29,6 +29,9 @@
#include "mbed-coap/sn_coap_protocol.h" #include "mbed-coap/sn_coap_protocol.h"
#include "sn_coap_header_internal.h" #include "sn_coap_header_internal.h"
#include "sn_coap_protocol_internal.h" #include "sn_coap_protocol_internal.h"
#include "mbed-trace/mbed_trace.h"
#define TRACE_GROUP "coap"
/** /**
* \fn int8_t sn_coap_header_validity_check(sn_coap_hdr_s *src_coap_msg_ptr, coap_version_e coap_version) * \fn int8_t sn_coap_header_validity_check(sn_coap_hdr_s *src_coap_msg_ptr, coap_version_e coap_version)
@ -56,6 +59,7 @@ int8_t sn_coap_header_validity_check(sn_coap_hdr_s *src_coap_msg_ptr, coap_versi
case COAP_MSG_TYPE_RESET: case COAP_MSG_TYPE_RESET:
break; /* Ok cases */ break; /* Ok cases */
default: default:
tr_error("sn_coap_header_validity_check - unknown message type!");
return -1; /* Failed case */ return -1; /* Failed case */
} }
@ -91,6 +95,7 @@ int8_t sn_coap_header_validity_check(sn_coap_hdr_s *src_coap_msg_ptr, coap_versi
case COAP_MSG_CODE_RESPONSE_CONTINUE: case COAP_MSG_CODE_RESPONSE_CONTINUE:
break; /* Ok cases */ break; /* Ok cases */
default: default:
tr_error("sn_coap_header_validity_check - unknown message code!");
return -1; /* Failed case */ return -1; /* Failed case */
} }

View File

@ -35,6 +35,9 @@
#include "mbed-coap/sn_coap_protocol.h" #include "mbed-coap/sn_coap_protocol.h"
#include "sn_coap_header_internal.h" #include "sn_coap_header_internal.h"
#include "sn_coap_protocol_internal.h" #include "sn_coap_protocol_internal.h"
#include "mbed-trace/mbed_trace.h"
#define TRACE_GROUP "coap"
/* * * * * * * * * * * * * * * * * * * * */ /* * * * * * * * * * * * * * * * * * * * */
/* * * * LOCAL FUNCTION PROTOTYPES * * * */ /* * * * LOCAL FUNCTION PROTOTYPES * * * */
/* * * * * * * * * * * * * * * * * * * * */ /* * * * * * * * * * * * * * * * * * * * */
@ -49,6 +52,7 @@ sn_coap_hdr_s *sn_coap_parser_init_message(sn_coap_hdr_s *coap_msg_ptr)
{ {
/* * * * Check given pointer * * * */ /* * * * Check given pointer * * * */
if (coap_msg_ptr == NULL) { if (coap_msg_ptr == NULL) {
tr_error("sn_coap_parser_init_message - message null!");
return NULL; return NULL;
} }
@ -91,6 +95,7 @@ sn_coap_options_list_s *sn_coap_parser_alloc_options(struct coap_s *handle, sn_c
coap_msg_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s)); coap_msg_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
if (coap_msg_ptr->options_list_ptr == NULL) { if (coap_msg_ptr->options_list_ptr == NULL) {
tr_error("sn_coap_parser_alloc_options - failed to allocate options list!");
return NULL; return NULL;
} }
@ -121,6 +126,7 @@ sn_coap_hdr_s *sn_coap_parser(struct coap_s *handle, uint16_t packet_data_len, u
parsed_and_returned_coap_msg_ptr = sn_coap_parser_alloc_message(handle); parsed_and_returned_coap_msg_ptr = sn_coap_parser_alloc_message(handle);
if (parsed_and_returned_coap_msg_ptr == NULL) { if (parsed_and_returned_coap_msg_ptr == NULL) {
tr_error("sn_coap_parser - failed to allocate message!");
return NULL; return NULL;
} }
@ -259,12 +265,14 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
if (dst_coap_msg_ptr->token_len) { if (dst_coap_msg_ptr->token_len) {
if ((dst_coap_msg_ptr->token_len > 8) || dst_coap_msg_ptr->token_ptr) { if ((dst_coap_msg_ptr->token_len > 8) || dst_coap_msg_ptr->token_ptr) {
tr_error("sn_coap_parser_options_parse - token not valid!");
return -1; 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 = handle->sn_coap_protocol_malloc(dst_coap_msg_ptr->token_len);
if (dst_coap_msg_ptr->token_ptr == NULL) { if (dst_coap_msg_ptr->token_ptr == NULL) {
tr_error("sn_coap_parser_options_parse - failed to allocate token!");
return -1; return -1;
} }
@ -293,6 +301,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
} }
/* Option number 15 reserved for payload marker. This is handled as a error! */ /* Option number 15 reserved for payload marker. This is handled as a error! */
else if (option_number == 15) { else if (option_number == 15) {
tr_error("sn_coap_parser_options_parse - invalid option number(15)!");
return -1; return -1;
} }
@ -310,6 +319,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
} }
/* Option number length 15 is reserved for the future use - ERROR */ /* Option number length 15 is reserved for the future use - ERROR */
else if (option_len == 15) { else if (option_len == 15) {
tr_error("sn_coap_parser_options_parse - invalid option len(15)!");
return -1; return -1;
} }
@ -335,6 +345,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_SIZE1: case COAP_OPTION_SIZE1:
case COAP_OPTION_SIZE2: case COAP_OPTION_SIZE2:
if (sn_coap_parser_alloc_options(handle, dst_coap_msg_ptr) == NULL) { if (sn_coap_parser_alloc_options(handle, dst_coap_msg_ptr) == NULL) {
tr_error("sn_coap_parser_options_parse - failed to allocate options!");
return -1; return -1;
} }
break; break;
@ -344,6 +355,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
switch (option_number) { switch (option_number) {
case COAP_OPTION_CONTENT_FORMAT: case COAP_OPTION_CONTENT_FORMAT:
if ((option_len > 2) || (dst_coap_msg_ptr->content_format != COAP_CT_NONE)) { if ((option_len > 2) || (dst_coap_msg_ptr->content_format != COAP_CT_NONE)) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_CONTENT_FORMAT not valid!");
return -1; return -1;
} }
(*packet_data_pptr)++; (*packet_data_pptr)++;
@ -352,6 +364,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_MAX_AGE: case COAP_OPTION_MAX_AGE:
if (option_len > 4) { if (option_len > 4) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_MAX_AGE not valid!");
return -1; return -1;
} }
(*packet_data_pptr)++; (*packet_data_pptr)++;
@ -360,6 +373,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_PROXY_URI: case COAP_OPTION_PROXY_URI:
if ((option_len > 1034) || (option_len < 1) || dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr) { if ((option_len > 1034) || (option_len < 1) || dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_PROXY_URI not valid!");
return -1; return -1;
} }
dst_coap_msg_ptr->options_list_ptr->proxy_uri_len = option_len; dst_coap_msg_ptr->options_list_ptr->proxy_uri_len = option_len;
@ -368,8 +382,10 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
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 = handle->sn_coap_protocol_malloc(option_len);
if (dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr == NULL) { 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; return -1;
} }
memcpy(dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr, *packet_data_pptr, option_len); memcpy(dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr, *packet_data_pptr, option_len);
(*packet_data_pptr) += option_len; (*packet_data_pptr) += option_len;
@ -386,12 +402,14 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
if (ret_status >= 0) { if (ret_status >= 0) {
i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
} else { } else {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_ETAG not valid!");
return -1; return -1;
} }
break; break;
case COAP_OPTION_URI_HOST: case COAP_OPTION_URI_HOST:
if ((option_len > 255) || (option_len < 1) || dst_coap_msg_ptr->options_list_ptr->uri_host_ptr) { if ((option_len > 255) || (option_len < 1) || dst_coap_msg_ptr->options_list_ptr->uri_host_ptr) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_URI_HOST not valid!");
return -1; return -1;
} }
dst_coap_msg_ptr->options_list_ptr->uri_host_len = option_len; dst_coap_msg_ptr->options_list_ptr->uri_host_len = option_len;
@ -400,6 +418,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
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 = handle->sn_coap_protocol_malloc(option_len);
if (dst_coap_msg_ptr->options_list_ptr->uri_host_ptr == NULL) { 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; return -1;
} }
memcpy(dst_coap_msg_ptr->options_list_ptr->uri_host_ptr, *packet_data_pptr, option_len); memcpy(dst_coap_msg_ptr->options_list_ptr->uri_host_ptr, *packet_data_pptr, option_len);
@ -409,6 +428,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_LOCATION_PATH: case COAP_OPTION_LOCATION_PATH:
if (dst_coap_msg_ptr->options_list_ptr->location_path_ptr) { if (dst_coap_msg_ptr->options_list_ptr->location_path_ptr) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_LOCATION_PATH exists!");
return -1; return -1;
} }
/* This is managed independently because User gives this option in one character table */ /* This is managed independently because User gives this option in one character table */
@ -418,6 +438,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
if (ret_status >= 0) { if (ret_status >= 0) {
i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
} else { } else {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_LOCATION_PATH not valid!");
return -1; return -1;
} }
@ -426,6 +447,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_URI_PORT: case COAP_OPTION_URI_PORT:
if ((option_len > 2) || dst_coap_msg_ptr->options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) { if ((option_len > 2) || dst_coap_msg_ptr->options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_URI_PORT not valid!");
return -1; return -1;
} }
(*packet_data_pptr)++; (*packet_data_pptr)++;
@ -440,6 +462,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
if (ret_status >= 0) { if (ret_status >= 0) {
i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
} else { } else {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_LOCATION_QUERY not valid!");
return -1; return -1;
} }
@ -452,6 +475,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
if (ret_status >= 0) { if (ret_status >= 0) {
i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
} else { } else {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_URI_PATH not valid!");
return -1; return -1;
} }
@ -459,6 +483,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_OBSERVE: case COAP_OPTION_OBSERVE:
if ((option_len > 2) || dst_coap_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) { if ((option_len > 2) || dst_coap_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_OBSERVE not valid!");
return -1; return -1;
} }
@ -475,6 +500,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
if (ret_status >= 0) { if (ret_status >= 0) {
i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
} else { } else {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_URI_QUERY not valid!");
return -1; return -1;
} }
@ -482,6 +508,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_BLOCK2: case COAP_OPTION_BLOCK2:
if ((option_len > 3) || dst_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) { if ((option_len > 3) || dst_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_BLOCK2 not valid!");
return -1; return -1;
} }
(*packet_data_pptr)++; (*packet_data_pptr)++;
@ -492,6 +519,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_BLOCK1: case COAP_OPTION_BLOCK1:
if ((option_len > 3) || dst_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) { if ((option_len > 3) || dst_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_BLOCK1 not valid!");
return -1; return -1;
} }
(*packet_data_pptr)++; (*packet_data_pptr)++;
@ -502,6 +530,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_ACCEPT: case COAP_OPTION_ACCEPT:
if ((option_len > 2) || (dst_coap_msg_ptr->options_list_ptr->accept != COAP_CT_NONE)) { if ((option_len > 2) || (dst_coap_msg_ptr->options_list_ptr->accept != COAP_CT_NONE)) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_ACCEPT not valid!");
return -1; return -1;
} }
@ -512,6 +541,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_SIZE1: case COAP_OPTION_SIZE1:
if ((option_len > 4) || dst_coap_msg_ptr->options_list_ptr->use_size1) { if ((option_len > 4) || dst_coap_msg_ptr->options_list_ptr->use_size1) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_SIZE1 not valid!");
return -1; return -1;
} }
dst_coap_msg_ptr->options_list_ptr->use_size1 = true; dst_coap_msg_ptr->options_list_ptr->use_size1 = true;
@ -521,6 +551,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
case COAP_OPTION_SIZE2: case COAP_OPTION_SIZE2:
if ((option_len > 4) || dst_coap_msg_ptr->options_list_ptr->use_size2) { if ((option_len > 4) || dst_coap_msg_ptr->options_list_ptr->use_size2) {
tr_error("sn_coap_parser_options_parse - COAP_OPTION_SIZE2 not valid!");
return -1; return -1;
} }
dst_coap_msg_ptr->options_list_ptr->use_size2 = true; dst_coap_msg_ptr->options_list_ptr->use_size2 = true;
@ -529,6 +560,7 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
break; break;
default: default:
tr_error("sn_coap_parser_options_parse - unknown option!");
return -1; return -1;
} }
@ -539,7 +571,6 @@ static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **pack
message_left = packet_len - (*packet_data_pptr - packet_data_start_ptr); message_left = packet_len - (*packet_data_pptr - packet_data_start_ptr);
} }
return 0; return 0;
@ -576,6 +607,7 @@ static int8_t sn_coap_parser_options_parse_multiple_options(struct coap_s *handl
*dst_pptr = (uint8_t *) handle->sn_coap_protocol_malloc(uri_query_needed_heap); *dst_pptr = (uint8_t *) handle->sn_coap_protocol_malloc(uri_query_needed_heap);
if (*dst_pptr == NULL) { if (*dst_pptr == NULL) {
tr_error("sn_coap_parser_options_parse_multiple_options - failed to allocate options!");
return -1; return -1;
} }
} }
@ -753,6 +785,7 @@ static int8_t sn_coap_parser_payload_parse(uint16_t packet_data_len, uint8_t *pa
} }
/* No payload marker.. */ /* No payload marker.. */
else { else {
tr_error("sn_coap_parser_payload_parse - payload marker not found!");
return -1; return -1;
} }
} }

View File

@ -216,8 +216,6 @@ struct coap_s *sn_coap_protocol_init(void *(*used_malloc_func_ptr)(uint16_t), vo
if (message_id == 0) { if (message_id == 0) {
message_id = 1; message_id = 1;
} }
tr_debug("Coap random msg ID: %d", message_id);
tr_debug("Coap BLOCKWISE_MAX_TIME_DATA_STORED: %d", SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED);
return handle; return handle;
} }
@ -350,24 +348,22 @@ int8_t prepare_blockwise_message(struct coap_s *handle, sn_coap_hdr_s *src_coap_
/* Allocate memory for less used options */ /* Allocate memory for less used options */
if (sn_coap_parser_alloc_options(handle, src_coap_msg_ptr) == NULL) { if (sn_coap_parser_alloc_options(handle, src_coap_msg_ptr) == NULL) {
tr_error("prepare_blockwise_message - failed to allocate options!");
return -2; return -2;
} }
/* Check if Request message */ /* Check if Request message */
if (src_coap_msg_ptr->msg_code < COAP_MSG_CODE_RESPONSE_CREATED) { if (src_coap_msg_ptr->msg_code < COAP_MSG_CODE_RESPONSE_CREATED) {
tr_debug("prepare_blockwise_message - block1 request");
/* Add Blockwise option, use Block1 because Request payload */ /* Add Blockwise option, use Block1 because Request payload */
src_coap_msg_ptr->options_list_ptr->block1 = 0x08; /* First block (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */ src_coap_msg_ptr->options_list_ptr->block1 = 0x08; /* First block (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */
src_coap_msg_ptr->options_list_ptr->block1 |= sn_coap_convert_block_size(handle->sn_coap_block_data_size); src_coap_msg_ptr->options_list_ptr->block1 |= sn_coap_convert_block_size(handle->sn_coap_block_data_size);
/* Add size1 parameter */ /* Add size1 parameter */
tr_debug("prepare_blockwise_message block1 request - payload len %d", src_coap_msg_ptr->payload_len);
src_coap_msg_ptr->options_list_ptr->use_size1 = true; src_coap_msg_ptr->options_list_ptr->use_size1 = true;
src_coap_msg_ptr->options_list_ptr->use_size2 = false; src_coap_msg_ptr->options_list_ptr->use_size2 = false;
src_coap_msg_ptr->options_list_ptr->size1 = src_coap_msg_ptr->payload_len; src_coap_msg_ptr->options_list_ptr->size1 = src_coap_msg_ptr->payload_len;
} else { /* Response message */ } else { /* Response message */
tr_debug("prepare_blockwise_message - block2 response");
/* Add Blockwise option, use Block2 because Response payload */ /* Add Blockwise option, use Block2 because Response payload */
src_coap_msg_ptr->options_list_ptr->block2 = 0x08; /* First block (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */ src_coap_msg_ptr->options_list_ptr->block2 = 0x08; /* First block (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */
src_coap_msg_ptr->options_list_ptr->block2 |= sn_coap_convert_block_size(handle->sn_coap_block_data_size); src_coap_msg_ptr->options_list_ptr->block2 |= sn_coap_convert_block_size(handle->sn_coap_block_data_size);
@ -384,7 +380,6 @@ int8_t prepare_blockwise_message(struct coap_s *handle, sn_coap_hdr_s *src_coap_
int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, 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) uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, void *param)
{ {
tr_debug("sn_coap_protocol_build - payload len %d", src_coap_msg_ptr->payload_len);
int16_t byte_count_built = 0; int16_t byte_count_built = 0;
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */ #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
uint16_t original_payload_len = 0; uint16_t original_payload_len = 0;
@ -429,6 +424,7 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p
byte_count_built = sn_coap_builder_2(dst_packet_data_ptr, src_coap_msg_ptr, handle->sn_coap_block_data_size); byte_count_built = sn_coap_builder_2(dst_packet_data_ptr, src_coap_msg_ptr, handle->sn_coap_block_data_size);
if (byte_count_built < 0) { if (byte_count_built < 0) {
tr_error("sn_coap_protocol_build - failed to build message!");
return byte_count_built; return byte_count_built;
} }
@ -460,6 +456,7 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p
memcpy(info->packet_ptr, dst_packet_data_ptr, byte_count_built); memcpy(info->packet_ptr, dst_packet_data_ptr, byte_count_built);
info->packet_len = byte_count_built; info->packet_len = byte_count_built;
} else { } else {
tr_error("sn_coap_protocol_build - failed to allocate duplication info!");
return -4; return -4;
} }
} }
@ -480,6 +477,7 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p
stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s)); stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
if (!stored_blockwise_msg_ptr) { if (!stored_blockwise_msg_ptr) {
//block paylaod save failed, only first block can be build. Perhaps we should return error. //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; return byte_count_built;
} }
memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s)); memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
@ -491,6 +489,7 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p
if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){ if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){
handle->sn_coap_protocol_free(stored_blockwise_msg_ptr); handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
stored_blockwise_msg_ptr = 0; stored_blockwise_msg_ptr = 0;
tr_error("sn_coap_protocol_build - block header copy failed!");
return -2; return -2;
} }
@ -502,6 +501,7 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p
sn_coap_parser_release_allocated_coap_msg_mem(handle, stored_blockwise_msg_ptr->coap_msg_ptr); 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); handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
stored_blockwise_msg_ptr = 0; stored_blockwise_msg_ptr = 0;
tr_error("sn_coap_protocol_build - block payload allocation failed!");
return byte_count_built; 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); 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);
@ -516,6 +516,7 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p
stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s)); stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
if (!stored_blockwise_msg_ptr) { if (!stored_blockwise_msg_ptr) {
tr_error("sn_coap_protocol_build - blockwise (GET) allocation failed!");
return byte_count_built; return byte_count_built;
} }
memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s)); memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
@ -527,6 +528,7 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p
if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){ if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){
handle->sn_coap_protocol_free(stored_blockwise_msg_ptr); handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
stored_blockwise_msg_ptr = 0; stored_blockwise_msg_ptr = 0;
tr_error("sn_coap_protocol_build - blockwise (GET) copy header failed!");
return -2; return -2;
} }
@ -536,15 +538,12 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p
#endif /* SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */ #endif /* SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */
tr_debug("sn_coap_protocol_build - msg id: [%d], bytes: [%d]", src_coap_msg_ptr->msg_id, byte_count_built);
/* * * * Return built CoAP message Packet data length * * * */ /* * * * Return built CoAP message Packet data length * * * */
return byte_count_built; return byte_count_built;
} }
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 *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)
{ {
tr_debug("sn_coap_protocol_parse");
sn_coap_hdr_s *returned_dst_coap_msg_ptr = NULL; sn_coap_hdr_s *returned_dst_coap_msg_ptr = NULL;
coap_version_e coap_version = COAP_VERSION_UNKNOWN; coap_version_e coap_version = COAP_VERSION_UNKNOWN;
@ -560,12 +559,14 @@ sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src
/* Check status of returned pointer */ /* Check status of returned pointer */
if (returned_dst_coap_msg_ptr == NULL) { if (returned_dst_coap_msg_ptr == NULL) {
/* Memory allocation error in parser */ /* Memory allocation error in parser */
tr_error("sn_coap_protocol_parse - allocation fail in parser!");
return NULL; return NULL;
} }
/* * * * Send bad request response if parsing fails * * * */ /* * * * Send bad request response if parsing fails * * * */
if (returned_dst_coap_msg_ptr->coap_status == COAP_STATUS_PARSER_ERROR_IN_HEADER) { if (returned_dst_coap_msg_ptr->coap_status == COAP_STATUS_PARSER_ERROR_IN_HEADER) {
sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param); sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr); sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr);
tr_error("sn_coap_protocol_parse - COAP_STATUS_PARSER_ERROR_IN_HEADER");
return NULL; return NULL;
} }
@ -575,6 +576,7 @@ sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src
if (((returned_dst_coap_msg_ptr->msg_code >> 5) == 1) || // if class == 1 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) == 6) || // if class == 6
((returned_dst_coap_msg_ptr->msg_code >> 5) == 7)) { // if class == 7 ((returned_dst_coap_msg_ptr->msg_code >> 5) == 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); sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
} }
@ -613,6 +615,7 @@ sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src
returned_dst_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE)) { returned_dst_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE)) {
/* Set returned status to User */ /* Set returned status to User */
returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED; returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED;
tr_error("sn_coap_protocol_parse - COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED!");
//todo: send response -> not implemented //todo: send response -> not implemented
return returned_dst_coap_msg_ptr; return returned_dst_coap_msg_ptr;
} }
@ -714,6 +717,7 @@ sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src
} }
if (!returned_dst_coap_msg_ptr) { if (!returned_dst_coap_msg_ptr) {
tr_error("sn_coap_protocol_parse - returned_dst_coap_msg_ptr null!");
return NULL; return NULL;
} }
@ -770,7 +774,9 @@ int8_t sn_coap_protocol_exec(struct coap_s *handle, uint32_t current_time)
#if ENABLE_RESENDINGS #if ENABLE_RESENDINGS
/* Check if there is ongoing active message sendings */ /* Check if there is ongoing active message sendings */
ns_list_foreach_safe(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) { /* foreach_safe isn't sufficient because callback routine could cancel messages. */
rescan:
ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) {
// First check that msg belongs to handle // First check that msg belongs to handle
if( stored_msg_ptr->coap == handle ){ if( stored_msg_ptr->coap == handle ){
/* Check if it is time to send this message */ /* Check if it is time to send this message */
@ -816,7 +822,9 @@ int8_t sn_coap_protocol_exec(struct coap_s *handle, uint32_t current_time)
handle->sn_coap_resending_intervall, handle->sn_coap_resending_intervall,
stored_msg_ptr->resending_counter); 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;
} }
} }
} }
@ -859,6 +867,7 @@ static uint8_t sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle
if (handle->sn_coap_resending_queue_msgs > 0) { if (handle->sn_coap_resending_queue_msgs > 0) {
if (handle->count_resent_msgs >= handle->sn_coap_resending_queue_msgs) { if (handle->count_resent_msgs >= handle->sn_coap_resending_queue_msgs) {
tr_error("sn_coap_protocol_linked_list_send_msg_store - resend queue full!");
return 0; return 0;
} }
} }
@ -866,6 +875,7 @@ static uint8_t sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle
/* Count resending queue size, if buffer size is defined */ /* Count resending queue size, if buffer size is defined */
if (handle->sn_coap_resending_queue_bytes > 0) { if (handle->sn_coap_resending_queue_bytes > 0) {
if ((sn_coap_count_linked_list_size(&handle->linked_list_resent_msgs) + send_packet_data_len) > handle->sn_coap_resending_queue_bytes) { if ((sn_coap_count_linked_list_size(&handle->linked_list_resent_msgs) + send_packet_data_len) > handle->sn_coap_resending_queue_bytes) {
tr_error("sn_coap_protocol_linked_list_send_msg_store - resend buffer size reached!");
return 0; return 0;
} }
} }
@ -874,6 +884,7 @@ static uint8_t sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle
stored_msg_ptr = sn_coap_protocol_allocate_mem_for_msg(handle, dst_addr_ptr, send_packet_data_len); stored_msg_ptr = sn_coap_protocol_allocate_mem_for_msg(handle, dst_addr_ptr, send_packet_data_len);
if (stored_msg_ptr == 0) { if (stored_msg_ptr == 0) {
tr_error("sn_coap_protocol_linked_list_send_msg_store - failed to allocate message!");
return 0; return 0;
} }
@ -1028,6 +1039,7 @@ static void sn_coap_protocol_linked_list_duplication_info_store(struct coap_s *h
stored_duplication_info_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_duplication_info_s)); stored_duplication_info_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_duplication_info_s));
if (stored_duplication_info_ptr == NULL) { if (stored_duplication_info_ptr == NULL) {
tr_error("sn_coap_protocol_linked_list_duplication_info_store - failed to allocate duplication info!");
return; return;
} }
memset(stored_duplication_info_ptr, 0, sizeof(coap_duplication_info_s)); memset(stored_duplication_info_ptr, 0, sizeof(coap_duplication_info_s));
@ -1035,6 +1047,7 @@ static void sn_coap_protocol_linked_list_duplication_info_store(struct coap_s *h
/* Allocate memory for stored Duplication info's address */ /* 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 = handle->sn_coap_protocol_malloc(sizeof(sn_nsdl_addr_s));
if (stored_duplication_info_ptr->address == NULL) { 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); handle->sn_coap_protocol_free(stored_duplication_info_ptr);
stored_duplication_info_ptr = 0; stored_duplication_info_ptr = 0;
return; return;
@ -1044,6 +1057,7 @@ static void sn_coap_protocol_linked_list_duplication_info_store(struct coap_s *h
stored_duplication_info_ptr->address->addr_ptr = handle->sn_coap_protocol_malloc(addr_ptr->addr_len); 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) { 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); handle->sn_coap_protocol_free(stored_duplication_info_ptr->address);
stored_duplication_info_ptr->address = 0; stored_duplication_info_ptr->address = 0;
handle->sn_coap_protocol_free(stored_duplication_info_ptr); handle->sn_coap_protocol_free(stored_duplication_info_ptr);
@ -1227,6 +1241,7 @@ static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s *
stored_blockwise_payload_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_payload_s)); stored_blockwise_payload_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_payload_s));
if (stored_blockwise_payload_ptr == NULL) { if (stored_blockwise_payload_ptr == NULL) {
tr_error("sn_coap_protocol_linked_list_blockwise_payload_store - failed to allocate blockwise!");
return; return;
} }
@ -1235,6 +1250,7 @@ static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s *
stored_blockwise_payload_ptr->payload_ptr = handle->sn_coap_protocol_malloc(stored_payload_len); stored_blockwise_payload_ptr->payload_ptr = handle->sn_coap_protocol_malloc(stored_payload_len);
if (stored_blockwise_payload_ptr->payload_ptr == NULL) { 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); handle->sn_coap_protocol_free(stored_blockwise_payload_ptr);
stored_blockwise_payload_ptr = 0; stored_blockwise_payload_ptr = 0;
return; return;
@ -1244,6 +1260,7 @@ static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s *
stored_blockwise_payload_ptr->addr_ptr = handle->sn_coap_protocol_malloc(addr_ptr->addr_len); stored_blockwise_payload_ptr->addr_ptr = handle->sn_coap_protocol_malloc(addr_ptr->addr_len);
if (stored_blockwise_payload_ptr->addr_ptr == NULL) { 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); handle->sn_coap_protocol_free(stored_blockwise_payload_ptr->payload_ptr);
stored_blockwise_payload_ptr->payload_ptr = 0; stored_blockwise_payload_ptr->payload_ptr = 0;
handle->sn_coap_protocol_free(stored_blockwise_payload_ptr); handle->sn_coap_protocol_free(stored_blockwise_payload_ptr);
@ -1313,7 +1330,6 @@ static bool sn_coap_protocol_linked_list_blockwise_payload_compare_block_number(
if (stored_payload_info_ptr->port == src_addr_ptr->port) { if (stored_payload_info_ptr->port == src_addr_ptr->port) {
// Check that incoming block number matches to last received one // Check that incoming block number matches to last received one
if (block_number - 1 == stored_payload_info_ptr->block_number) { if (block_number - 1 == stored_payload_info_ptr->block_number) {
tr_debug("sn_coap_protocol_linked_list_blockwise_payload_search_block_number - found %d", stored_payload_info_ptr->block_number);
return true; return true;
} }
} }
@ -1569,7 +1585,6 @@ void sn_coap_protocol_block_remove(struct coap_s *handle, sn_nsdl_addr_s *source
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_handle_blockwise_message(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, sn_coap_hdr_s *received_coap_msg_ptr, void *param)
{ {
tr_debug("sn_coap_handle_blockwise_message");
sn_coap_hdr_s *src_coap_blockwise_ack_msg_ptr = NULL; sn_coap_hdr_s *src_coap_blockwise_ack_msg_ptr = NULL;
uint16_t dst_packed_data_needed_mem = 0; uint16_t dst_packed_data_needed_mem = 0;
uint8_t *dst_ack_packet_data_ptr = NULL; uint8_t *dst_ack_packet_data_ptr = NULL;
@ -1581,9 +1596,7 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
/* Block1 Option in a request (e.g., PUT or POST) */ /* Block1 Option in a request (e.g., PUT or POST) */
// Blocked request sending, received ACK, sending next block.. // Blocked request sending, received ACK, sending next block..
if (received_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) { if (received_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) {
tr_debug("sn_coap_handle_blockwise_message - block1, message code: [%d]", received_coap_msg_ptr->msg_code);
if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) { if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) {
tr_debug("sn_coap_handle_blockwise_message - send block1 request");
if (received_coap_msg_ptr->options_list_ptr->block1 & 0x08) { 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 = NULL;
@ -1614,6 +1627,7 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE; src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE;
} else { } else {
if (!sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr)) { 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); sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
return 0; return 0;
} }
@ -1642,6 +1656,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); dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
if (!dst_ack_packet_data_ptr) { 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); handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0; src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
handle->sn_coap_protocol_free(original_payload_ptr); handle->sn_coap_protocol_free(original_payload_ptr);
@ -1658,7 +1673,6 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
} }
sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size); sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
tr_debug("sn_coap_handle_blockwise_message - block1 request, send block msg id: [%d]", src_coap_blockwise_ack_msg_ptr->msg_id);
handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param); handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
handle->sn_coap_protocol_free(dst_ack_packet_data_ptr); handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
@ -1671,7 +1685,6 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
} }
} else { } else {
// XXX what was this trying to free? // XXX what was this trying to free?
tr_debug("sn_coap_handle_blockwise_message - block1 request - last block sent");
received_coap_msg_ptr->coap_status = COAP_STATUS_OK; received_coap_msg_ptr->coap_status = COAP_STATUS_OK;
} }
@ -1679,7 +1692,6 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
// Blocked request receiving // Blocked request receiving
else { else {
tr_debug("sn_coap_handle_blockwise_message - block1 received");
if (received_coap_msg_ptr->payload_len > handle->sn_coap_block_data_size) { if (received_coap_msg_ptr->payload_len > handle->sn_coap_block_data_size) {
received_coap_msg_ptr->payload_len = handle->sn_coap_block_data_size; received_coap_msg_ptr->payload_len = handle->sn_coap_block_data_size;
} }
@ -1703,14 +1715,15 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
/* If not last block (more value is set) */ /* 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. */ /* 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) { if (received_coap_msg_ptr->options_list_ptr->block1 & 0x08) {
tr_debug("sn_coap_handle_blockwise_message - block1 received, send ack");
src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message(handle); src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message(handle);
if (src_coap_blockwise_ack_msg_ptr == NULL) { 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); sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
return NULL; return NULL;
} }
if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == 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); handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
src_coap_blockwise_ack_msg_ptr = 0; src_coap_blockwise_ack_msg_ptr = 0;
sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
@ -1718,13 +1731,14 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
} }
// Response with COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE if the payload size is more than we can handle // Response with COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE if the payload size is more than we can handle
tr_debug("sn_coap_handle_blockwise_message - block1 received - incoming size: [%d]", received_coap_msg_ptr->options_list_ptr->size1);
uint32_t max_size = SN_COAP_MAX_INCOMING_BLOCK_MESSAGE_SIZE; uint32_t max_size = SN_COAP_MAX_INCOMING_BLOCK_MESSAGE_SIZE;
if (!blocks_in_order) { if (!blocks_in_order) {
tr_error("sn_coap_handle_blockwise_message - (recv block1) COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE!");
src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE; src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE;
} else if (received_coap_msg_ptr->options_list_ptr->size1 > max_size) { } else if (received_coap_msg_ptr->options_list_ptr->size1 > max_size) {
// Include maximum size that stack can handle into response // Include maximum size that stack can handle into response
tr_error("sn_coap_handle_blockwise_message - (recv block1) COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE!");
src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE; src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE;
src_coap_blockwise_ack_msg_ptr->options_list_ptr->size1 = max_size; src_coap_blockwise_ack_msg_ptr->options_list_ptr->size1 = max_size;
} else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) { } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
@ -1760,6 +1774,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); dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
if (!dst_ack_packet_data_ptr) { 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); 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); handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0; src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
@ -1769,7 +1784,6 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
} }
sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size); sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
tr_debug("sn_coap_handle_blockwise_message - block1 received - send msg id [%d]", src_coap_blockwise_ack_msg_ptr->msg_id);
handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param); handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr); sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr);
@ -1779,7 +1793,6 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING; received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING;
} else { } else {
tr_debug("sn_coap_handle_blockwise_message - block1 received, last block received");
/* * * This is the last block when whole Blockwise payload from received * * */ /* * * This is the last block when whole Blockwise payload from received * * */
/* * * blockwise messages is gathered and returned to User * * */ /* * * blockwise messages is gathered and returned to User * * */
@ -1789,10 +1802,9 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
uint32_t whole_payload_len = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr); uint32_t whole_payload_len = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr);
uint8_t *temp_whole_payload_ptr = NULL; uint8_t *temp_whole_payload_ptr = NULL;
tr_debug("sn_coap_handle_blockwise_message - block1 received, whole_payload_len %d", whole_payload_len);
temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len); temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len);
if (temp_whole_payload_ptr == NULL || whole_payload_len > UINT16_MAX) { if (temp_whole_payload_ptr == NULL || whole_payload_len > UINT16_MAX) {
tr_debug("sn_coap_handle_blockwise_message - block1 received, last block received alloc fails"); 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); sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
handle->sn_coap_protocol_free(temp_whole_payload_ptr); handle->sn_coap_protocol_free(temp_whole_payload_ptr);
return 0; return 0;
@ -1818,10 +1830,8 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
/* Block2 Option in a response (e.g., a 2.05 response for GET) */ /* Block2 Option in a response (e.g., a 2.05 response for GET) */
/* Message ID must be same than in received message */ /* Message ID must be same than in received message */
else { else {
tr_debug("sn_coap_handle_blockwise_message - block2 - message code: [%d]", received_coap_msg_ptr->msg_code);
//This is response to request we made //This is response to request we made
if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) { if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) {
tr_debug("sn_coap_handle_blockwise_message - send block2 request");
uint32_t block_number = 0; uint32_t block_number = 0;
/* Store blockwise payload to Linked list */ /* Store blockwise payload to Linked list */
@ -1846,12 +1856,14 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
} }
if (!previous_blockwise_msg_ptr || !previous_blockwise_msg_ptr->coap_msg_ptr) { 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); sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
return 0; return 0;
} }
src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message(handle); src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message(handle);
if (src_coap_blockwise_ack_msg_ptr == NULL) { if (src_coap_blockwise_ack_msg_ptr == NULL) {
tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate message!");
return 0; return 0;
} }
@ -1870,6 +1882,7 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
/* * * Then build CoAP Acknowledgement message * * */ /* * * Then build CoAP Acknowledgement message * * */
if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) { 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); handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
src_coap_blockwise_ack_msg_ptr = 0; src_coap_blockwise_ack_msg_ptr = 0;
sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr); sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
@ -1896,6 +1909,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); dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
if (dst_ack_packet_data_ptr == NULL) { if (dst_ack_packet_data_ptr == NULL) {
tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate packet!");
handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_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; src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr); handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
@ -1907,6 +1921,7 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
/* * * Then build Acknowledgement message to Packed data * * */ /* * * 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) { 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); handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
dst_ack_packet_data_ptr = 0; dst_ack_packet_data_ptr = 0;
handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr); handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
@ -1922,6 +1937,7 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s)); stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
if (!stored_blockwise_msg_ptr) { 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); handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
dst_ack_packet_data_ptr = 0; dst_ack_packet_data_ptr = 0;
handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr); handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
@ -1967,6 +1983,7 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len); temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len);
if (!temp_whole_payload_ptr) { if (!temp_whole_payload_ptr) {
tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate whole payload!");
return 0; return 0;
} }
@ -1991,7 +2008,6 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
//Now we send data to request //Now we send data to request
else { else {
tr_debug("sn_coap_handle_blockwise_message - block2 received");
//Get message by using block number //Get message by using block number
//NOTE: Getting the first from list might not be correct one //NOTE: Getting the first from list might not be correct one
coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = ns_list_get_first(&handle->linked_list_blockwise_sent_msgs); coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = ns_list_get_first(&handle->linked_list_blockwise_sent_msgs);
@ -2012,6 +2028,7 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE; src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE;
} else { } else {
if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) { if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) {
tr_error("sn_coap_handle_blockwise_message - (recv block2) failed to allocate options!");
return 0; return 0;
} }
} }
@ -2059,6 +2076,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); dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
if (!dst_ack_packet_data_ptr) { if (!dst_ack_packet_data_ptr) {
tr_error("sn_coap_handle_blockwise_message - (recv block2) failed to allocate packet!");
if(original_payload_ptr){ if(original_payload_ptr){
handle->sn_coap_protocol_free(original_payload_ptr); handle->sn_coap_protocol_free(original_payload_ptr);
original_payload_ptr = NULL; original_payload_ptr = NULL;
@ -2071,7 +2089,6 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
} }
sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size); sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
tr_debug("sn_coap_handle_blockwise_message - block2 received, send message: [%d]", src_coap_blockwise_ack_msg_ptr->msg_id);
handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param); handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
handle->sn_coap_protocol_free(dst_ack_packet_data_ptr); handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
@ -2118,6 +2135,7 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa
destination_header_ptr = sn_coap_parser_alloc_message(handle); destination_header_ptr = sn_coap_parser_alloc_message(handle);
if (!destination_header_ptr) { if (!destination_header_ptr) {
tr_error("sn_coap_protocol_copy_header - failed to allocate message!");
return 0; return 0;
} }
@ -2130,6 +2148,7 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa
destination_header_ptr->uri_path_len = source_header_ptr->uri_path_len; 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 = handle->sn_coap_protocol_malloc(source_header_ptr->uri_path_len);
if (!destination_header_ptr->uri_path_ptr) { 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); sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
return 0; return 0;
} }
@ -2141,6 +2160,7 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa
destination_header_ptr->token_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->token_len); destination_header_ptr->token_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->token_len);
if (!destination_header_ptr->token_ptr) { if (!destination_header_ptr->token_ptr) {
sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_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; return 0;
} }
memcpy(destination_header_ptr->token_ptr, source_header_ptr->token_ptr, source_header_ptr->token_len); memcpy(destination_header_ptr->token_ptr, source_header_ptr->token_ptr, source_header_ptr->token_len);
@ -2152,6 +2172,7 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa
if (source_header_ptr->options_list_ptr) { if (source_header_ptr->options_list_ptr) {
if (sn_coap_parser_alloc_options(handle, destination_header_ptr) == NULL) { if (sn_coap_parser_alloc_options(handle, destination_header_ptr) == NULL) {
sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr); sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
tr_error("sn_coap_protocol_copy_header - failed to allocate options!");
return 0; return 0;
} }
@ -2162,6 +2183,7 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa
destination_header_ptr->options_list_ptr->proxy_uri_ptr = handle->sn_coap_protocol_malloc(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) { if (!destination_header_ptr->options_list_ptr->proxy_uri_ptr) {
sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_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; 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); 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);
@ -2172,6 +2194,7 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa
destination_header_ptr->options_list_ptr->etag_ptr = handle->sn_coap_protocol_malloc(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 (!destination_header_ptr->options_list_ptr->etag_ptr) {
sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_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; 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); 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);
@ -2182,6 +2205,7 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa
destination_header_ptr->options_list_ptr->uri_host_ptr = handle->sn_coap_protocol_malloc(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 (!destination_header_ptr->options_list_ptr->uri_host_ptr) {
sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_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; 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); 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);
@ -2191,6 +2215,7 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa
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_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); 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 (!destination_header_ptr->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); sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
return 0; return 0;
} }
@ -2204,6 +2229,7 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa
destination_header_ptr->options_list_ptr->location_query_ptr = handle->sn_coap_protocol_malloc(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 (!destination_header_ptr->options_list_ptr->location_query_ptr) {
sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_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; 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); 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);
@ -2217,6 +2243,7 @@ static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coa
destination_header_ptr->options_list_ptr->uri_query_ptr = handle->sn_coap_protocol_malloc(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 (!destination_header_ptr->options_list_ptr->uri_query_ptr) {
sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_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; 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); 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);