Nathan Yonkee / Mbed 2 deprecated Nucleo_sinewave_output_copy

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sn_coap_protocol.c Source File

sn_coap_protocol.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011-2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 /**
00018  * \file sn_coap_protocol.c
00019  *
00020  * \brief CoAP Protocol implementation
00021  *
00022  * Functionality: CoAP Protocol
00023  *
00024  */
00025 
00026 
00027 /* * * * * * * * * * * * * * */
00028 /* * * * INCLUDE FILES * * * */
00029 /* * * * * * * * * * * * * * */
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h> /* For libary malloc() */
00033 #include <string.h> /* For memset() and memcpy() */
00034 #if defined __linux__ || defined TARGET_LIKE_MBED
00035 #include <time.h>
00036 #endif
00037 
00038 #include "ns_types.h"
00039 #include "mbed-coap/sn_coap_protocol.h"
00040 #include "sn_coap_header_internal.h"
00041 #include "sn_coap_protocol_internal.h"
00042 #include "randLIB.h"
00043 #include "mbed-trace/mbed_trace.h"
00044 
00045 #define TRACE_GROUP "coap"
00046 /* * * * * * * * * * * * * * * * * * * * */
00047 /* * * * LOCAL FUNCTION PROTOTYPES * * * */
00048 /* * * * * * * * * * * * * * * * * * * * */
00049 
00050 static void                  sn_coap_protocol_send_rst(struct coap_s *handle, uint16_t msg_id, sn_nsdl_addr_s *addr_ptr, void *param);
00051 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT/* If Message duplication detection is not used at all, this part of code will not be compiled */
00052 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);
00053 static coap_duplication_info_s *sn_coap_protocol_linked_list_duplication_info_search(struct coap_s *handle, sn_nsdl_addr_s *scr_addr_ptr, uint16_t msg_id);
00054 static void                  sn_coap_protocol_linked_list_duplication_info_remove(struct coap_s *handle, uint8_t *scr_addr_ptr, uint16_t port, uint16_t msg_id);
00055 static void                  sn_coap_protocol_linked_list_duplication_info_remove_old_ones(struct coap_s *handle);
00056 #endif
00057 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
00058 static void                  sn_coap_protocol_linked_list_blockwise_msg_remove(struct coap_s *handle, coap_blockwise_msg_s *removed_msg_ptr);
00059 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, uint32_t block_number);
00060 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);
00061 static bool                  sn_coap_protocol_linked_list_blockwise_payload_compare_block_number(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint32_t block_number);
00062 static void                  sn_coap_protocol_linked_list_blockwise_payload_remove(struct coap_s *handle, coap_blockwise_payload_s *removed_payload_ptr);
00063 static void                  sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(struct coap_s *handle);
00064 static uint32_t              sn_coap_protocol_linked_list_blockwise_payloads_get_len(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr);
00065 static void                  sn_coap_protocol_linked_list_blockwise_remove_old_data(struct coap_s *handle);
00066 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);
00067 static sn_coap_hdr_s        *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coap_hdr_s *source_header_ptr);
00068 #endif
00069 #if ENABLE_RESENDINGS
00070 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);
00071 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);
00072 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);
00073 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);
00074 static void                  sn_coap_protocol_release_allocated_send_msg_mem(struct coap_s *handle, coap_send_msg_s *freed_send_msg_ptr);
00075 static uint16_t              sn_coap_count_linked_list_size(const coap_send_msg_list_t *linked_list_ptr);
00076 static uint32_t              sn_coap_calculate_new_resend_time(const uint32_t current_time, const uint8_t interval, const uint8_t counter);
00077 #endif
00078 
00079 /* * * * * * * * * * * * * * * * * */
00080 /* * * * GLOBAL DECLARATIONS * * * */
00081 /* * * * * * * * * * * * * * * * * */
00082 static uint16_t message_id;
00083 
00084 int8_t sn_coap_protocol_destroy(struct coap_s *handle)
00085 {
00086     if (handle == NULL) {
00087         return -1;
00088     }
00089 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
00090 
00091     sn_coap_protocol_clear_retransmission_buffer(handle);
00092 
00093 #endif
00094 
00095 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
00096     ns_list_foreach_safe(coap_duplication_info_s, tmp, &handle->linked_list_duplication_msgs) {
00097         if (tmp->coap == handle) {
00098             if (tmp->address) {
00099                 if (tmp->address->addr_ptr) {
00100                     handle->sn_coap_protocol_free(tmp->address->addr_ptr);
00101                     tmp->address->addr_ptr = 0;
00102                 }
00103                 handle->sn_coap_protocol_free(tmp->address);
00104                 tmp->address = 0;
00105             }
00106             if (tmp->packet_ptr) {
00107                 handle->sn_coap_protocol_free(tmp->packet_ptr);
00108                 tmp->packet_ptr = 0;
00109             }
00110             ns_list_remove(&handle->linked_list_duplication_msgs, tmp);
00111             handle->count_duplication_msgs--;
00112             handle->sn_coap_protocol_free(tmp);
00113             tmp = 0;
00114         }
00115     }
00116 
00117 #endif
00118 
00119 
00120 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwise is not used at all, this part of code will not be compiled */
00121     ns_list_foreach_safe(coap_blockwise_msg_s, tmp, &handle->linked_list_blockwise_sent_msgs) {
00122         if (tmp->coap == handle) {
00123             if (tmp->coap_msg_ptr) {
00124                 if (tmp->coap_msg_ptr->payload_ptr) {
00125                     handle->sn_coap_protocol_free(tmp->coap_msg_ptr->payload_ptr);
00126                     tmp->coap_msg_ptr->payload_ptr = 0;
00127                 }
00128                 sn_coap_parser_release_allocated_coap_msg_mem(tmp->coap, tmp->coap_msg_ptr);
00129             }
00130             ns_list_remove(&handle->linked_list_blockwise_sent_msgs, tmp);
00131             handle->sn_coap_protocol_free(tmp);
00132             tmp = 0;
00133         }
00134     }
00135     ns_list_foreach_safe(coap_blockwise_payload_s, tmp, &handle->linked_list_blockwise_received_payloads) {
00136         if (tmp->coap == handle) {
00137             if (tmp->addr_ptr) {
00138                 handle->sn_coap_protocol_free(tmp->addr_ptr);
00139                 tmp->addr_ptr = 0;
00140             }
00141             if (tmp->payload_ptr) {
00142                 handle->sn_coap_protocol_free(tmp->payload_ptr);
00143                 tmp->payload_ptr = 0;
00144             }
00145             ns_list_remove(&handle->linked_list_blockwise_received_payloads, tmp);
00146             handle->sn_coap_protocol_free(tmp);
00147             tmp = 0;
00148         }
00149     }
00150 #endif
00151 
00152     handle->sn_coap_protocol_free(handle);
00153     handle = 0;
00154     return 0;
00155 }
00156 
00157 struct coap_s *sn_coap_protocol_init(void *(*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void *),
00158                                      uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *),
00159                                      int8_t (*used_rx_callback_ptr)(sn_coap_hdr_s *, sn_nsdl_addr_s *, void *param))
00160 {
00161     /* Check paramters */
00162     if ((used_malloc_func_ptr == NULL) || (used_free_func_ptr == NULL) || (used_tx_callback_ptr == NULL)) {
00163         return NULL;
00164     }
00165 
00166     struct coap_s *handle;
00167     handle = used_malloc_func_ptr(sizeof(struct coap_s));
00168     if (handle == NULL) {
00169         return NULL;
00170     }
00171 
00172     memset(handle, 0, sizeof(struct coap_s));
00173 
00174     /* * * Handle tx callback * * */
00175     handle->sn_coap_tx_callback = used_tx_callback_ptr;
00176 
00177     handle->sn_coap_protocol_free = used_free_func_ptr;
00178     handle->sn_coap_protocol_malloc = used_malloc_func_ptr;
00179 
00180     /* * * Handle rx callback * * */
00181     /* If pointer = 0, then re-sending does not return error when failed */
00182     handle->sn_coap_rx_callback = used_rx_callback_ptr;
00183 
00184     // Handles internally all GET req responses
00185     handle->sn_coap_internal_block2_resp_handling = true;
00186 
00187 #if ENABLE_RESENDINGS  /* If Message resending is not used at all, this part of code will not be compiled */
00188     /* * * * Create Linked list for storing active resending messages  * * * */
00189     ns_list_init(&handle->linked_list_resent_msgs);
00190     handle->sn_coap_resending_queue_msgs = SN_COAP_RESENDING_QUEUE_SIZE_MSGS;
00191     handle->sn_coap_resending_queue_bytes = SN_COAP_RESENDING_QUEUE_SIZE_BYTES;
00192     handle->sn_coap_resending_intervall = DEFAULT_RESPONSE_TIMEOUT;
00193     handle->sn_coap_resending_count = SN_COAP_RESENDING_MAX_COUNT;
00194 
00195 
00196 #endif /* ENABLE_RESENDINGS */
00197 
00198 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
00199     /* * * * Create Linked list for storing Duplication info * * * */
00200     ns_list_init(&handle->linked_list_duplication_msgs);
00201     handle->sn_coap_duplication_buffer_size = SN_COAP_DUPLICATION_MAX_MSGS_COUNT;
00202 #endif
00203 
00204 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
00205 
00206     ns_list_init(&handle->linked_list_blockwise_sent_msgs);
00207     ns_list_init(&handle->linked_list_blockwise_received_payloads);
00208     handle->sn_coap_block_data_size = SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE;
00209 
00210 #endif /* ENABLE_RESENDINGS */
00211 
00212     /* Randomize global message ID */
00213     randLIB_seed_random();
00214     message_id = randLIB_get_16bit();
00215     if (message_id == 0) {
00216         message_id = 1;
00217     }
00218 
00219     return handle;
00220 }
00221 
00222 int8_t sn_coap_protocol_handle_block2_response_internally(struct coap_s *handle, uint8_t build_response)
00223 {
00224     if (handle == NULL) {
00225         return -1;
00226     }
00227 
00228     handle->sn_coap_internal_block2_resp_handling = build_response;
00229     return 0;
00230 }
00231 
00232 int8_t sn_coap_protocol_set_block_size(struct coap_s *handle, uint16_t block_size)
00233 {
00234     (void) handle;
00235     (void) block_size;
00236 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
00237     if (handle == NULL) {
00238         return -1;
00239     }
00240     switch (block_size) {
00241         case 0:
00242         case 16:
00243         case 32:
00244         case 64:
00245         case 128:
00246         case 256:
00247         case 512:
00248         case 1024:
00249             handle->sn_coap_block_data_size = block_size;
00250             return 0;
00251         default:
00252             break;
00253     }
00254 #endif
00255     return -1;
00256 
00257 }
00258 
00259 int8_t sn_coap_protocol_set_duplicate_buffer_size(struct coap_s *handle, uint8_t message_count)
00260 {
00261     (void) handle;
00262     (void) message_count;
00263 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT
00264     if (handle == NULL) {
00265         return -1;
00266     }
00267     if (message_count <= SN_COAP_MAX_ALLOWED_DUPLICATION_MESSAGE_COUNT) {
00268         handle->sn_coap_duplication_buffer_size = message_count;
00269         return 0;
00270     }
00271 #endif
00272     return -1;
00273 }
00274 
00275 int8_t sn_coap_protocol_set_retransmission_parameters(struct coap_s *handle,
00276         uint8_t resending_count, uint8_t resending_intervall)
00277 {
00278 #if ENABLE_RESENDINGS
00279     if (handle == NULL) {
00280         return -1;
00281     }
00282     if (resending_count <= SN_COAP_MAX_ALLOWED_RESENDING_COUNT &&
00283             resending_intervall <= SN_COAP_MAX_ALLOWED_RESPONSE_TIMEOUT) {
00284         handle->sn_coap_resending_count = resending_count;
00285 
00286         if (resending_intervall == 0) {
00287             handle->sn_coap_resending_intervall = 1;
00288         } else {
00289             handle->sn_coap_resending_intervall = resending_intervall;
00290         }
00291         return 0;
00292     }
00293 #endif
00294     return -1;
00295 }
00296 
00297 int8_t sn_coap_protocol_set_retransmission_buffer(struct coap_s *handle,
00298         uint8_t buffer_size_messages, uint16_t buffer_size_bytes)
00299 {
00300 #if ENABLE_RESENDINGS
00301     if (handle == NULL) {
00302         return -1;
00303     }
00304     if (buffer_size_bytes <= SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_BYTES &&
00305         buffer_size_messages <= SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_MSGS ) {
00306         handle->sn_coap_resending_queue_bytes = buffer_size_bytes;
00307         handle->sn_coap_resending_queue_msgs = buffer_size_messages;
00308         return 0;
00309     }
00310 
00311 #endif
00312     return -1;
00313 
00314 }
00315 
00316 void sn_coap_protocol_clear_retransmission_buffer(struct coap_s *handle)
00317 {
00318 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
00319     if (handle == NULL) {
00320         return;
00321     }
00322     ns_list_foreach_safe(coap_send_msg_s, tmp, &handle->linked_list_resent_msgs) {
00323         ns_list_remove(&handle->linked_list_resent_msgs, tmp);
00324         sn_coap_protocol_release_allocated_send_msg_mem(handle, tmp);
00325         --handle->count_resent_msgs;
00326     }
00327 #endif
00328 }
00329 
00330 int8_t sn_coap_protocol_delete_retransmission(struct coap_s *handle, uint16_t msg_id)
00331 {
00332 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
00333     if (handle == NULL) {
00334         return -1;
00335     }
00336     ns_list_foreach_safe(coap_send_msg_s, tmp, &handle->linked_list_resent_msgs) {
00337         if (tmp->send_msg_ptr && tmp->send_msg_ptr->packet_ptr ) {
00338             uint16_t temp_msg_id = (tmp->send_msg_ptr->packet_ptr[2] << 8);
00339             temp_msg_id += (uint16_t)tmp->send_msg_ptr->packet_ptr[3];
00340             if(temp_msg_id == msg_id){
00341                 ns_list_remove(&handle->linked_list_resent_msgs, tmp);
00342                 --handle->count_resent_msgs;
00343                 sn_coap_protocol_release_allocated_send_msg_mem(handle, tmp);
00344                 return 0;
00345             }
00346         }
00347     }
00348 #endif
00349     return -2;
00350 }
00351 
00352 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
00353 int8_t prepare_blockwise_message(struct coap_s *handle, sn_coap_hdr_s *src_coap_msg_ptr)
00354 {
00355     if ((src_coap_msg_ptr->payload_len > handle->sn_coap_block_data_size) && (handle->sn_coap_block_data_size > 0)) {
00356         /* * * * Add Blockwise option to send CoAP message * * */
00357 
00358         /* Allocate memory for less used options */
00359         if (sn_coap_parser_alloc_options(handle, src_coap_msg_ptr) == NULL) {
00360             tr_error("prepare_blockwise_message - failed to allocate options!");
00361             return -2;
00362         }
00363 
00364         /* Check if Request message */
00365         if (src_coap_msg_ptr->msg_code < COAP_MSG_CODE_RESPONSE_CREATED) {
00366             /* Add Blockwise option, use Block1 because Request payload */
00367             src_coap_msg_ptr->options_list_ptr->block1 = 0x08;      /* First block  (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */
00368             src_coap_msg_ptr->options_list_ptr->block1 |= sn_coap_convert_block_size(handle->sn_coap_block_data_size);
00369 
00370             /* Add size1 parameter */
00371 
00372             src_coap_msg_ptr->options_list_ptr->use_size1 = true;
00373             src_coap_msg_ptr->options_list_ptr->use_size2 = false;
00374             src_coap_msg_ptr->options_list_ptr->size1 = src_coap_msg_ptr->payload_len;
00375         } else { /* Response message */
00376             /* Add Blockwise option, use Block2 because Response payload */
00377             src_coap_msg_ptr->options_list_ptr->block2 = 0x08;      /* First block  (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */
00378             src_coap_msg_ptr->options_list_ptr->block2 |= sn_coap_convert_block_size(handle->sn_coap_block_data_size);
00379 
00380             src_coap_msg_ptr->options_list_ptr->use_size1 = false;
00381             src_coap_msg_ptr->options_list_ptr->use_size2 = true;
00382             src_coap_msg_ptr->options_list_ptr->size2 = src_coap_msg_ptr->payload_len;
00383         }
00384     }
00385     return 0;
00386 }
00387 #endif
00388 
00389 int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr,
00390                                uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, void *param)
00391 {
00392     int16_t  byte_count_built     = 0;
00393 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
00394     uint16_t original_payload_len = 0;
00395 #endif
00396     /* * * * Check given pointers  * * * */
00397     if ((dst_addr_ptr == NULL) || (dst_packet_data_ptr == NULL) || (src_coap_msg_ptr == NULL) || handle == NULL) {
00398         return -2;
00399     }
00400 
00401     if (dst_addr_ptr->addr_ptr == NULL) {
00402         return -2;
00403     }
00404 
00405     /* Check if built Message type is else than Acknowledgement or Reset i.e. message type is Confirmable or Non-confirmable */
00406     /* (for Acknowledgement and  Reset messages is written same Message ID than was in the Request message) */
00407     if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_ACKNOWLEDGEMENT &&
00408             src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_RESET &&
00409             src_coap_msg_ptr->msg_id == 0) {
00410         /* * * * Generate new Message ID and increase it by one  * * * */
00411         src_coap_msg_ptr->msg_id = message_id;
00412         message_id++;
00413         if (message_id == 0) {
00414             message_id = 1;
00415         }
00416     }
00417 
00418 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
00419 
00420     /* If blockwising needed */
00421     if ((src_coap_msg_ptr->payload_len > handle->sn_coap_block_data_size) && (handle->sn_coap_block_data_size > 0)) {
00422         /* Store original Payload length */
00423         original_payload_len = src_coap_msg_ptr->payload_len;
00424         /* Change Payload length of send message because Payload is blockwised */
00425         src_coap_msg_ptr->payload_len = handle->sn_coap_block_data_size;
00426     }
00427 
00428 #endif
00429     /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00430     /* * * * Build Packet data from CoAP message by using CoAP Header builder  * * * */
00431     /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00432 
00433     byte_count_built = sn_coap_builder_2(dst_packet_data_ptr, src_coap_msg_ptr, handle->sn_coap_block_data_size);
00434 
00435     if (byte_count_built < 0) {
00436         tr_error("sn_coap_protocol_build - failed to build message!");
00437         return byte_count_built;
00438     }
00439 
00440 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
00441 
00442     /* Check if built Message type was confirmable, only these messages are resent */
00443     if (src_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
00444         /* Store message to Linked list for resending purposes */
00445         uint32_t resend_time = sn_coap_calculate_new_resend_time(handle->system_time, handle->sn_coap_resending_intervall, 0);
00446         if (sn_coap_protocol_linked_list_send_msg_store(handle, dst_addr_ptr, byte_count_built, dst_packet_data_ptr,
00447                 resend_time,
00448                 param) == 0) {
00449             return -4;
00450         }
00451     }
00452 
00453 #endif /* ENABLE_RESENDINGS */
00454 
00455 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT
00456     if (src_coap_msg_ptr->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT &&
00457             handle->sn_coap_duplication_buffer_size != 0) {
00458         coap_duplication_info_s* info = sn_coap_protocol_linked_list_duplication_info_search(handle,
00459                                                                                              dst_addr_ptr,
00460                                                                                              src_coap_msg_ptr->msg_id);
00461         /* Update package data to duplication info struct if it's not there yet */
00462         if (info && info->packet_ptr == NULL) {
00463             info->packet_ptr = handle->sn_coap_protocol_malloc(byte_count_built);
00464             if (info->packet_ptr) {
00465                 memcpy(info->packet_ptr, dst_packet_data_ptr, byte_count_built);
00466                 info->packet_len = byte_count_built;
00467             } else {
00468                 tr_error("sn_coap_protocol_build - failed to allocate duplication info!");
00469                 return -4;
00470             }
00471         }
00472     }
00473 #endif
00474 
00475 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
00476 
00477     /* If blockwising needed */
00478     if ((original_payload_len > handle->sn_coap_block_data_size) && (handle->sn_coap_block_data_size > 0)) {
00479 
00480         /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00481         /* * * * Manage rest blockwise messages sending by storing them to Linked list * * * */
00482         /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00483 
00484         coap_blockwise_msg_s *stored_blockwise_msg_ptr;
00485 
00486         stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
00487         if (!stored_blockwise_msg_ptr) {
00488             //block paylaod save failed, only first block can be build. Perhaps we should return error.
00489             tr_error("sn_coap_protocol_build - blockwise message allocation failed!");
00490             return byte_count_built;
00491         }
00492         memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
00493 
00494         /* Fill struct */
00495         stored_blockwise_msg_ptr->timestamp = handle->system_time;
00496 
00497         stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(handle, src_coap_msg_ptr);
00498         if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){
00499             handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
00500             stored_blockwise_msg_ptr = 0;
00501             tr_error("sn_coap_protocol_build - block header copy failed!");
00502             return -2;
00503         }
00504 
00505         stored_blockwise_msg_ptr->coap_msg_ptr->payload_len = original_payload_len;
00506         stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr = handle->sn_coap_protocol_malloc(stored_blockwise_msg_ptr->coap_msg_ptr->payload_len);
00507 
00508         if (!stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr) {
00509             //block payload save failed, only first block can be build. Perhaps we should return error.
00510             sn_coap_parser_release_allocated_coap_msg_mem(handle, stored_blockwise_msg_ptr->coap_msg_ptr);
00511             handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
00512             stored_blockwise_msg_ptr = 0;
00513             tr_error("sn_coap_protocol_build - block payload allocation failed!");
00514             return byte_count_built;
00515         }
00516         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);
00517 
00518         stored_blockwise_msg_ptr->coap = handle;
00519         ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr);
00520     }
00521 
00522     else if (src_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
00523         /* Add message to linked list - response can be in blocks and we need header to build response.. */
00524         coap_blockwise_msg_s *stored_blockwise_msg_ptr;
00525 
00526         stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
00527         if (!stored_blockwise_msg_ptr) {
00528             tr_error("sn_coap_protocol_build - blockwise (GET) allocation failed!");
00529             return byte_count_built;
00530         }
00531         memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
00532 
00533         /* Fill struct */
00534         stored_blockwise_msg_ptr->timestamp = handle->system_time;
00535 
00536         stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(handle, src_coap_msg_ptr);
00537         if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){
00538             handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
00539             stored_blockwise_msg_ptr = 0;
00540             tr_error("sn_coap_protocol_build - blockwise (GET) copy header failed!");
00541             return -2;
00542         }
00543 
00544         stored_blockwise_msg_ptr->coap = handle;
00545         ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr);
00546     }
00547 
00548 #endif /* SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */
00549 
00550     /* * * * Return built CoAP message Packet data length  * * * */
00551     return byte_count_built;
00552 }
00553 
00554 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)
00555 {
00556     sn_coap_hdr_s   *returned_dst_coap_msg_ptr = NULL;
00557     coap_version_e   coap_version              = COAP_VERSION_UNKNOWN;
00558 
00559     /* * * * Check given pointer * * * */
00560     if (src_addr_ptr == NULL || src_addr_ptr->addr_ptr == NULL ||
00561         packet_data_ptr == NULL || handle == NULL) {
00562         return NULL;
00563     }
00564 
00565     /* * * * Parse Packet data to CoAP message by using CoAP Header parser * * * */
00566     returned_dst_coap_msg_ptr = sn_coap_parser(handle, packet_data_len, packet_data_ptr, &coap_version);
00567 
00568     /* Check status of returned pointer */
00569     if (returned_dst_coap_msg_ptr == NULL) {
00570         /* Memory allocation error in parser */
00571         tr_error("sn_coap_protocol_parse - allocation fail in parser!");
00572         return NULL;
00573     }
00574     /* * * * Send bad request response if parsing fails * * * */
00575     if (returned_dst_coap_msg_ptr->coap_status == COAP_STATUS_PARSER_ERROR_IN_HEADER) {
00576         sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
00577         sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr);
00578         tr_error("sn_coap_protocol_parse - COAP_STATUS_PARSER_ERROR_IN_HEADER");
00579         return NULL;
00580     }
00581 
00582     /* * * * Check validity of parsed Header values  * * * */
00583     if (sn_coap_header_validity_check(returned_dst_coap_msg_ptr, coap_version) != 0) {
00584         /* 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     */
00585         if (((returned_dst_coap_msg_ptr->msg_code >> 5) == 1) ||        // if class == 1
00586                 ((returned_dst_coap_msg_ptr->msg_code >> 5) == 6) ||    // if class == 6
00587                 ((returned_dst_coap_msg_ptr->msg_code >> 5) == 7)) {    // if class == 7
00588             tr_error("sn_coap_protocol_parse - message code not valid!");
00589             sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
00590         }
00591 
00592         /* Release memory of CoAP message */
00593         sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr);
00594 
00595         /* Return NULL because Header validity check failed */
00596         return NULL;
00597     }
00598 
00599     /* Check if we need to send reset message */
00600     /*  A recipient MUST acknowledge a Confirmable message with an Acknowledgement
00601         message or, if it lacks context to process the message properly
00602         (including the case where the message is Empty, uses a code with a
00603         reserved class (1, 6 or 7), or has a message format error), MUST
00604         reject it; rejecting a Confirmable message is effected by sending a
00605         matching Reset message and otherwise ignoring it. */
00606     if (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
00607         /* CoAP ping */
00608         if (returned_dst_coap_msg_ptr->msg_code == COAP_MSG_CODE_EMPTY) {
00609             sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
00610 
00611             /* Release memory of CoAP message */
00612             sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr);
00613 
00614             /* Return NULL because Header validity check failed */
00615             return NULL;
00616         }
00617     }
00618 
00619 
00620 #if !SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is used, this part of code will not be compiled */
00621     /* If blockwising used in received message */
00622     if (returned_dst_coap_msg_ptr->options_list_ptr != NULL &&
00623             (returned_dst_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE ||
00624              returned_dst_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE)) {
00625         /* Set returned status to User */
00626         returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED;
00627         tr_error("sn_coap_protocol_parse - COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED!");
00628         //todo: send response -> not implemented
00629         return returned_dst_coap_msg_ptr;
00630     }
00631 #endif /* !SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */
00632 
00633 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT/* If Message duplication is used, this part of code will not be compiled */
00634 
00635     /* * * * Manage received CoAP message duplicate detection  * * * */
00636 
00637     /* If no message duplication detected */
00638     if ((returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE ||
00639             returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_NON_CONFIRMABLE) &&
00640             handle->sn_coap_duplication_buffer_size != 0) {
00641         if (sn_coap_protocol_linked_list_duplication_info_search(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id) == NULL) {
00642             /* * * No Message duplication: Store received message for detecting later duplication * * */
00643 
00644             /* Get count of stored duplication messages */
00645             uint16_t stored_duplication_msgs_count = handle->count_duplication_msgs;
00646 
00647             /* Check if there is no room to store message for duplication detection purposes */
00648             if (stored_duplication_msgs_count >= handle->sn_coap_duplication_buffer_size) {
00649                 /* Get oldest stored duplication message */
00650                 coap_duplication_info_s *stored_duplication_info_ptr = ns_list_get_first(&handle->linked_list_duplication_msgs);
00651 
00652                 /* Remove oldest stored duplication message for getting room for new duplication message */
00653                 sn_coap_protocol_linked_list_duplication_info_remove(handle,
00654                                                                      stored_duplication_info_ptr->address->addr_ptr,
00655                                                                      stored_duplication_info_ptr->address->port,
00656                                                                      stored_duplication_info_ptr->msg_id);
00657             }
00658 
00659             /* Store Duplication info to Linked list */
00660             sn_coap_protocol_linked_list_duplication_info_store(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id, param);
00661         } else { /* * * Message duplication detected * * */
00662             /* Set returned status to User */
00663             returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_DUPLICATED_MSG;
00664             coap_duplication_info_s* response = sn_coap_protocol_linked_list_duplication_info_search(handle,
00665                                                                                                      src_addr_ptr,
00666                                                                                                      returned_dst_coap_msg_ptr->msg_id);
00667             /* Send ACK response */
00668             if (response) {
00669                 /* Check that response has been created */
00670                 if (response->packet_ptr) {
00671                     response->coap->sn_coap_tx_callback(response->packet_ptr,
00672                             response->packet_len, response->address, response->param);
00673                 }
00674             }
00675 
00676             return returned_dst_coap_msg_ptr;
00677         }
00678     }
00679 #endif
00680 
00681     /*** And here we check if message was block message ***/
00682     /*** If so, we call own block handling function and ***/
00683     /*** return to caller.                              ***/
00684 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
00685 
00686     if (returned_dst_coap_msg_ptr->options_list_ptr != NULL &&
00687             (returned_dst_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE ||
00688              returned_dst_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE)) {
00689         returned_dst_coap_msg_ptr = sn_coap_handle_blockwise_message(handle, src_addr_ptr, returned_dst_coap_msg_ptr, param);
00690     } else {
00691         /* Get ... */
00692         coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = NULL;
00693 
00694         ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) {
00695             if (returned_dst_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) {
00696                 stored_blockwise_msg_temp_ptr = msg;
00697                 break;
00698             }
00699         }
00700         /* Remove from the list if not an notification message.
00701          * Initial notification message is needed for sending rest of the blocks (GET request).
00702         */
00703         bool remove_from_the_list = false;
00704         if (stored_blockwise_msg_temp_ptr) {
00705             if (stored_blockwise_msg_temp_ptr->coap_msg_ptr &&
00706                 stored_blockwise_msg_temp_ptr->coap_msg_ptr->options_list_ptr &&
00707                 stored_blockwise_msg_temp_ptr->coap_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) {
00708                 remove_from_the_list = false;
00709             } else {
00710                 remove_from_the_list = true;
00711             }
00712         }
00713         if (remove_from_the_list) {
00714             ns_list_remove(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_temp_ptr);
00715             if (stored_blockwise_msg_temp_ptr->coap_msg_ptr) {
00716                 if(stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr){
00717                     handle->sn_coap_protocol_free(stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr);
00718                     stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = 0;
00719                 }
00720                 sn_coap_parser_release_allocated_coap_msg_mem(stored_blockwise_msg_temp_ptr->coap, stored_blockwise_msg_temp_ptr->coap_msg_ptr);
00721             }
00722 
00723             handle->sn_coap_protocol_free(stored_blockwise_msg_temp_ptr);
00724             stored_blockwise_msg_temp_ptr = 0;
00725         }
00726     }
00727 
00728     if (!returned_dst_coap_msg_ptr) {
00729         tr_error("sn_coap_protocol_parse - returned_dst_coap_msg_ptr null!");
00730         return NULL;
00731     }
00732 
00733 #endif
00734 
00735 
00736 #if ENABLE_RESENDINGS  /* If Message resending is not used at all, this part of code will not be compiled */
00737 
00738     /* Check if received Message type was acknowledgement */
00739     if ((returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT) || (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_RESET)) {
00740         /* * * * Manage CoAP message resending by removing active resending message from Linked list * * */
00741 
00742         /* Get node count i.e. count of active resending messages */
00743         uint16_t stored_resending_msgs_count = handle->count_resent_msgs;
00744 
00745         /* Check if there is ongoing active message resendings */
00746         if (stored_resending_msgs_count > 0) {
00747             sn_nsdl_transmit_s *removed_msg_ptr = NULL;
00748 
00749             /* Check if received message was confirmation for some active resending message */
00750             removed_msg_ptr = sn_coap_protocol_linked_list_send_msg_search(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
00751 
00752             if (removed_msg_ptr != NULL) {
00753                 /* Remove resending message from active message resending Linked list */
00754                 sn_coap_protocol_linked_list_send_msg_remove(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
00755             }
00756         }
00757     }
00758 #endif /* ENABLE_RESENDINGS */
00759 
00760     /* * * * Return parsed CoAP message  * * * */
00761     return returned_dst_coap_msg_ptr;
00762 }
00763 
00764 
00765 int8_t sn_coap_protocol_exec(struct coap_s *handle, uint32_t current_time)
00766 {
00767     if( !handle ){
00768        return -1;
00769     }
00770 
00771     /* * * * Store current System time * * * */
00772     handle->system_time = current_time;
00773 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
00774     /* * * * Remove old blocwise data * * * */
00775     sn_coap_protocol_linked_list_blockwise_remove_old_data(handle);
00776 #endif
00777 
00778 
00779 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT
00780     /* * * * Remove old duplication messages * * * */
00781     sn_coap_protocol_linked_list_duplication_info_remove_old_ones(handle);
00782 #endif
00783 
00784 #if ENABLE_RESENDINGS
00785     /* Check if there is ongoing active message sendings */
00786     /* foreach_safe isn't sufficient because callback routine could cancel messages. */
00787 rescan:
00788     ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) {
00789         // First check that msg belongs to handle
00790         if( stored_msg_ptr->coap == handle ){
00791             /* Check if it is time to send this message */
00792             if (current_time >= stored_msg_ptr->resending_time) {
00793                 /* * * Increase Resending counter  * * */
00794                 stored_msg_ptr->resending_counter++;
00795 
00796                 /* Check if all re-sendings have been done */
00797                 if (stored_msg_ptr->resending_counter > handle->sn_coap_resending_count) {
00798                     coap_version_e coap_version = COAP_VERSION_UNKNOWN;
00799 
00800                     /* Get message ID from stored sending message */
00801                     uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
00802                     temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
00803 
00804                     /* Remove message from Linked list */
00805                     ns_list_remove(&handle->linked_list_resent_msgs, stored_msg_ptr);
00806                     --handle->count_resent_msgs;
00807 
00808                     /* If RX callback have been defined.. */
00809                     if (stored_msg_ptr->coap->sn_coap_rx_callback != 0) {
00810                         sn_coap_hdr_s *tmp_coap_hdr_ptr;
00811                         /* Parse CoAP message, set status and call RX callback */
00812                         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);
00813 
00814                         if (tmp_coap_hdr_ptr != 0) {
00815                             tmp_coap_hdr_ptr->coap_status = COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED;
00816                             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);
00817 
00818                             sn_coap_parser_release_allocated_coap_msg_mem(stored_msg_ptr->coap, tmp_coap_hdr_ptr);
00819                         }
00820                     }
00821 
00822                     /* Free memory of stored message */
00823                     sn_coap_protocol_release_allocated_send_msg_mem(handle, stored_msg_ptr);
00824                 } else {
00825                     /* Send message  */
00826                     stored_msg_ptr->coap->sn_coap_tx_callback(stored_msg_ptr->send_msg_ptr->packet_ptr,
00827                             stored_msg_ptr->send_msg_ptr->packet_len, stored_msg_ptr->send_msg_ptr->dst_addr_ptr, stored_msg_ptr->param);
00828 
00829                     /* * * Count new Resending time  * * */
00830                     stored_msg_ptr->resending_time = sn_coap_calculate_new_resend_time(current_time,
00831                                                                                        handle->sn_coap_resending_intervall,
00832                                                                                        stored_msg_ptr->resending_counter);
00833                 }
00834                 /* Callback routine could have wiped the list (eg as a response to sending failed) */
00835                 /* Be super cautious and rescan from the start */
00836                 goto rescan;
00837             }
00838         }
00839     }
00840 
00841 #endif /* ENABLE_RESENDINGS */
00842 
00843     return 0;
00844 }
00845 
00846 #if ENABLE_RESENDINGS  /* If Message resending is not used at all, this part of code will not be compiled */
00847 
00848 /**************************************************************************//**
00849  * \fn static uint8_t sn_coap_protocol_linked_list_send_msg_store(sn_nsdl_addr_s *dst_addr_ptr, uint16_t send_packet_data_len, uint8_t *send_packet_data_ptr, uint32_t sending_time)
00850  *
00851  * \brief Stores message to Linked list for sending purposes.
00852 
00853  * \param *dst_addr_ptr is pointer to destination address where CoAP message will be sent
00854  *
00855  * \param send_packet_data_len is length of Packet data to be stored
00856  *
00857  * \param *send_packet_data_ptr is Packet data to be stored
00858  *
00859  * \param sending_time is stored sending time
00860  *
00861  * \return 0 Allocation or buffer limit reached
00862  *
00863  * \return 1 Msg stored properly
00864  *****************************************************************************/
00865 
00866 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,
00867         uint8_t *send_packet_data_ptr, uint32_t sending_time, void *param)
00868 {
00869 
00870     coap_send_msg_s *stored_msg_ptr              = NULL;
00871 
00872     /* If both queue parameters are "0" or resending count is "0", then re-sending is disabled */
00873     if (((handle->sn_coap_resending_queue_msgs == 0) && (handle->sn_coap_resending_queue_bytes == 0)) || (handle->sn_coap_resending_count == 0)) {
00874         return 1;
00875     }
00876 
00877     if (handle->sn_coap_resending_queue_msgs > 0) {
00878         if (handle->count_resent_msgs >= handle->sn_coap_resending_queue_msgs) {
00879             tr_error("sn_coap_protocol_linked_list_send_msg_store - resend queue full!");
00880             return 0;
00881         }
00882     }
00883 
00884     /* Count resending queue size, if buffer size is defined */
00885     if (handle->sn_coap_resending_queue_bytes > 0) {
00886         if ((sn_coap_count_linked_list_size(&handle->linked_list_resent_msgs) + send_packet_data_len) > handle->sn_coap_resending_queue_bytes) {
00887             tr_error("sn_coap_protocol_linked_list_send_msg_store - resend buffer size reached!");
00888             return 0;
00889         }
00890     }
00891 
00892     /* Allocating memory for stored message */
00893     stored_msg_ptr = sn_coap_protocol_allocate_mem_for_msg(handle, dst_addr_ptr, send_packet_data_len);
00894 
00895     if (stored_msg_ptr == 0) {
00896         tr_error("sn_coap_protocol_linked_list_send_msg_store - failed to allocate message!");
00897         return 0;
00898     }
00899 
00900     /* Filling of coap_send_msg_s with initialization values */
00901     stored_msg_ptr->resending_counter = 0;
00902     stored_msg_ptr->resending_time = sending_time;
00903 
00904     /* Filling of sn_nsdl_transmit_s */
00905     stored_msg_ptr->send_msg_ptr->protocol = SN_NSDL_PROTOCOL_COAP;
00906     stored_msg_ptr->send_msg_ptr->packet_len = send_packet_data_len;
00907     memcpy(stored_msg_ptr->send_msg_ptr->packet_ptr, send_packet_data_ptr, send_packet_data_len);
00908 
00909     /* Filling of sn_nsdl_addr_s */
00910     stored_msg_ptr->send_msg_ptr->dst_addr_ptr->type = dst_addr_ptr->type;
00911     stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_len = dst_addr_ptr->addr_len;
00912     memcpy(stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_len);
00913     stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port = dst_addr_ptr->port;
00914 
00915     stored_msg_ptr->coap = handle;
00916     stored_msg_ptr->param = param;
00917 
00918     /* Storing Resending message to Linked list */
00919     ns_list_add_to_end(&handle->linked_list_resent_msgs, stored_msg_ptr);
00920     ++handle->count_resent_msgs;
00921     return 1;
00922 }
00923 
00924 /**************************************************************************//**
00925  * \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)
00926  *
00927  * \brief Searches stored resending message from Linked list
00928  *
00929  * \param *src_addr_ptr is searching key for searched message
00930  *
00931  * \param msg_id is searching key for searched message
00932  *
00933  * \return Return value is pointer to found stored resending message in Linked
00934  *         list or NULL if message not found
00935  *****************************************************************************/
00936 
00937 static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(struct coap_s *handle,
00938         sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
00939 {
00940     /* Loop all stored resending messages Linked list */
00941     ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) {
00942         /* Get message ID from stored resending message */
00943         uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
00944         temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
00945 
00946         /* If message's Message ID is same than is searched */
00947         if (temp_msg_id == msg_id) {
00948             /* If message's Source address is same than is searched */
00949             if (0 == memcmp(src_addr_ptr->addr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, src_addr_ptr->addr_len)) {
00950                 /* If message's Source address port is same than is searched */
00951                 if (stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port == src_addr_ptr->port) {
00952                     /* * * Message found, return pointer to that stored resending message * * * */
00953                     return stored_msg_ptr->send_msg_ptr;
00954                 }
00955             }
00956         }
00957     }
00958 
00959     /* Message not found */
00960     return NULL;
00961 }
00962 /**************************************************************************//**
00963  * \fn static void sn_coap_protocol_linked_list_send_msg_remove(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
00964  *
00965  * \brief Removes stored resending message from Linked list
00966  *
00967  * \param *src_addr_ptr is searching key for searched message
00968  * \param msg_id is searching key for removed message
00969  *****************************************************************************/
00970 
00971 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)
00972 {
00973     /* Loop all stored resending messages in Linked list */
00974     ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) {
00975         /* Get message ID from stored resending message */
00976         uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
00977         temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
00978 
00979         /* If message's Message ID is same than is searched */
00980         if (temp_msg_id == msg_id) {
00981             /* If message's Source address is same than is searched */
00982             if (0 == memcmp(src_addr_ptr->addr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, src_addr_ptr->addr_len)) {
00983                 /* If message's Source address port is same than is searched */
00984                 if (stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port == src_addr_ptr->port) {
00985                     /* * * Message found * * */
00986 
00987                     /* Remove message from Linked list */
00988                     ns_list_remove(&handle->linked_list_resent_msgs, stored_msg_ptr);
00989                     --handle->count_resent_msgs;
00990 
00991                     /* Free memory of stored message */
00992                     sn_coap_protocol_release_allocated_send_msg_mem(handle, stored_msg_ptr);
00993 
00994                     return;
00995                 }
00996             }
00997         }
00998     }
00999 }
01000 
01001 uint32_t sn_coap_calculate_new_resend_time(const uint32_t current_time, const uint8_t interval, const uint8_t counter)
01002 {
01003     uint32_t resend_time = interval << counter;
01004     uint16_t random_factor = randLIB_get_random_in_range(100, RESPONSE_RANDOM_FACTOR * 100);
01005     return current_time + ((resend_time * random_factor) / 100);
01006 }
01007 
01008 #endif /* ENABLE_RESENDINGS */
01009 
01010 static void sn_coap_protocol_send_rst(struct coap_s *handle, uint16_t msg_id, sn_nsdl_addr_s *addr_ptr, void *param)
01011 {
01012     uint8_t packet_ptr[4];
01013 
01014     /* Add CoAP version and message type */
01015     packet_ptr[0] = COAP_VERSION_1;
01016     packet_ptr[0] |= COAP_MSG_TYPE_RESET;
01017 
01018     /* Add message code */
01019     packet_ptr[1] = COAP_MSG_CODE_EMPTY;
01020 
01021     /* Add message ID */
01022     packet_ptr[2] = msg_id >> 8;
01023     packet_ptr[3] = (uint8_t)msg_id;
01024 
01025     /* Send RST */
01026     handle->sn_coap_tx_callback(packet_ptr, 4, addr_ptr, param);
01027 
01028 }
01029 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
01030 
01031 /**************************************************************************//**
01032  * \fn static void sn_coap_protocol_linked_list_duplication_info_store(sn_nsdl_addr_s *addr_ptr, uint16_t msg_id)
01033  *
01034  * \brief Stores Duplication info to Linked list
01035  *
01036  * \param msg_id is Message ID to be stored
01037  * \param *addr_ptr is pointer to Address information to be stored
01038  *****************************************************************************/
01039 
01040 static void sn_coap_protocol_linked_list_duplication_info_store(struct coap_s *handle, sn_nsdl_addr_s *addr_ptr,
01041         uint16_t msg_id, void *param)
01042 {
01043     coap_duplication_info_s *stored_duplication_info_ptr = NULL;
01044 
01045     /* * * * Allocating memory for stored Duplication info * * * */
01046 
01047     /* Allocate memory for stored Duplication info's structure */
01048     stored_duplication_info_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_duplication_info_s));
01049 
01050     if (stored_duplication_info_ptr == NULL) {
01051         tr_error("sn_coap_protocol_linked_list_duplication_info_store - failed to allocate duplication info!");
01052         return;
01053     }
01054     memset(stored_duplication_info_ptr, 0, sizeof(coap_duplication_info_s));
01055 
01056     /* Allocate memory for stored Duplication info's address */
01057     stored_duplication_info_ptr->address = handle->sn_coap_protocol_malloc(sizeof(sn_nsdl_addr_s));
01058     if (stored_duplication_info_ptr->address == NULL) {
01059         tr_error("sn_coap_protocol_linked_list_duplication_info_store - failed to allocate address!");
01060         handle->sn_coap_protocol_free(stored_duplication_info_ptr);
01061         stored_duplication_info_ptr = 0;
01062         return;
01063     }
01064     memset(stored_duplication_info_ptr->address, 0, sizeof(sn_nsdl_addr_s));
01065 
01066     stored_duplication_info_ptr->address->addr_ptr = handle->sn_coap_protocol_malloc(addr_ptr->addr_len);
01067 
01068     if (stored_duplication_info_ptr->address->addr_ptr == NULL) {
01069         tr_error("sn_coap_protocol_linked_list_duplication_info_store - failed to allocate address pointer!");
01070         handle->sn_coap_protocol_free(stored_duplication_info_ptr->address);
01071         stored_duplication_info_ptr->address = 0;
01072         handle->sn_coap_protocol_free(stored_duplication_info_ptr);
01073         stored_duplication_info_ptr = 0;
01074         return;
01075     }
01076 
01077     /* * * * Filling fields of stored Duplication info * * * */
01078     stored_duplication_info_ptr->timestamp = handle->system_time;
01079     stored_duplication_info_ptr->address->addr_len = addr_ptr->addr_len;
01080     memcpy(stored_duplication_info_ptr->address->addr_ptr, addr_ptr->addr_ptr, addr_ptr->addr_len);
01081     stored_duplication_info_ptr->address->port = addr_ptr->port;
01082     stored_duplication_info_ptr->msg_id = msg_id;
01083 
01084     stored_duplication_info_ptr->coap = handle;
01085 
01086     stored_duplication_info_ptr->param = param;
01087     /* * * * Storing Duplication info to Linked list * * * */
01088 
01089     ns_list_add_to_end(&handle->linked_list_duplication_msgs, stored_duplication_info_ptr);
01090     ++handle->count_duplication_msgs;
01091 }
01092 
01093 /**************************************************************************//**
01094  * \fn static int8_t sn_coap_protocol_linked_list_duplication_info_search(sn_nsdl_addr_s *addr_ptr, uint16_t msg_id)
01095  *
01096  * \brief Searches stored message from Linked list (Address and Message ID as key)
01097  *
01098  * \param *addr_ptr is pointer to Address key to be searched
01099  * \param msg_id is Message ID key to be searched
01100  *
01101  * \return Return value is 0 when message found and -1 if not found
01102  *****************************************************************************/
01103 
01104 static coap_duplication_info_s* sn_coap_protocol_linked_list_duplication_info_search(struct coap_s *handle,
01105         sn_nsdl_addr_s *addr_ptr, uint16_t msg_id)
01106 {
01107     /* Loop all nodes in Linked list for searching Message ID */
01108     ns_list_foreach(coap_duplication_info_s, stored_duplication_info_ptr, &handle->linked_list_duplication_msgs) {
01109         /* If message's Message ID is same than is searched */
01110         if (stored_duplication_info_ptr->msg_id == msg_id) {
01111             /* If message's Source address is same than is searched */
01112             if (0 == memcmp(addr_ptr->addr_ptr, stored_duplication_info_ptr->address->addr_ptr, addr_ptr->addr_len)) {
01113                 /* If message's Source address port is same than is searched */
01114                 if (stored_duplication_info_ptr->address->port == addr_ptr->port) {
01115                     /* * * Correct Duplication info found * * * */
01116                     return stored_duplication_info_ptr;
01117                 }
01118             }
01119         }
01120     }
01121     return NULL;
01122 }
01123 
01124 /**************************************************************************//**
01125  * \fn static void sn_coap_protocol_linked_list_duplication_info_remove(struct coap_s *handle, uint8_t *addr_ptr, uint16_t port, uint16_t msg_id)
01126  *
01127  * \brief Removes stored Duplication info from Linked list
01128  *
01129  * \param *addr_ptr is pointer to Address key to be removed
01130  *
01131  * \param port is Port key to be removed
01132  *
01133  * \param msg_id is Message ID key to be removed
01134  *****************************************************************************/
01135 
01136 static void sn_coap_protocol_linked_list_duplication_info_remove(struct coap_s *handle, uint8_t *addr_ptr, uint16_t port, uint16_t msg_id)
01137 {
01138     /* Loop all stored duplication messages in Linked list */
01139     ns_list_foreach(coap_duplication_info_s, removed_duplication_info_ptr, &handle->linked_list_duplication_msgs) {
01140         /* If message's Address is same than is searched */
01141         if (handle == removed_duplication_info_ptr->coap && 0 == memcmp(addr_ptr,
01142                                                                         removed_duplication_info_ptr->address->addr_ptr,
01143                                                                         removed_duplication_info_ptr->address->addr_len)) {
01144             /* If message's Address prt is same than is searched */
01145             if (removed_duplication_info_ptr->address->port == port) {
01146                 /* If Message ID is same than is searched */
01147                 if (removed_duplication_info_ptr->msg_id == msg_id) {
01148                     /* * * * Correct Duplication info found, remove it from Linked list * * * */
01149                     ns_list_remove(&handle->linked_list_duplication_msgs, removed_duplication_info_ptr);
01150                     --handle->count_duplication_msgs;
01151 
01152                     /* Free memory of stored Duplication info */
01153                     handle->sn_coap_protocol_free(removed_duplication_info_ptr->address->addr_ptr);
01154                     removed_duplication_info_ptr->address->addr_ptr = 0;
01155                     handle->sn_coap_protocol_free(removed_duplication_info_ptr->address);
01156                     removed_duplication_info_ptr->address = 0;
01157                     handle->sn_coap_protocol_free(removed_duplication_info_ptr->packet_ptr);
01158                     removed_duplication_info_ptr->packet_ptr = 0;
01159                     handle->sn_coap_protocol_free(removed_duplication_info_ptr);
01160                     removed_duplication_info_ptr = 0;
01161                     return;
01162                 }
01163             }
01164         }
01165     }
01166 }
01167 
01168 /**************************************************************************//**
01169  * \fn static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(struct coap_s *handle)
01170  *
01171  * \brief Removes old stored Duplication detection infos from Linked list
01172  *****************************************************************************/
01173 
01174 static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(struct coap_s *handle)
01175 {
01176     /* Loop all stored duplication messages in Linked list */
01177     ns_list_foreach_safe(coap_duplication_info_s, removed_duplication_info_ptr, &handle->linked_list_duplication_msgs) {
01178         if ((handle->system_time - removed_duplication_info_ptr->timestamp)  > SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED) {
01179             /* * * * Old Duplication info found, remove it from Linked list * * * */
01180             ns_list_remove(&handle->linked_list_duplication_msgs, removed_duplication_info_ptr);
01181             --handle->count_duplication_msgs;
01182 
01183             /* Free memory of stored Duplication info */
01184             handle->sn_coap_protocol_free(removed_duplication_info_ptr->address->addr_ptr);
01185             removed_duplication_info_ptr->address->addr_ptr = 0;
01186             handle->sn_coap_protocol_free(removed_duplication_info_ptr->address);
01187             removed_duplication_info_ptr->address = 0;
01188             handle->sn_coap_protocol_free(removed_duplication_info_ptr->packet_ptr);
01189             removed_duplication_info_ptr->packet_ptr = 0;
01190             handle->sn_coap_protocol_free(removed_duplication_info_ptr);
01191             removed_duplication_info_ptr = 0;
01192         }
01193     }
01194 }
01195 
01196 #endif /* SN_COAP_DUPLICATION_MAX_MSGS_COUNT */
01197 
01198 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
01199 /**************************************************************************//**
01200  * \fn static void sn_coap_protocol_linked_list_blockwise_msg_remove(struct coap_s *handle, coap_blockwise_msg_s *removed_msg_ptr)
01201  *
01202  * \brief Removes stored blockwise message from Linked list
01203  *
01204  * \param removed_msg_ptr is message to be removed
01205  *****************************************************************************/
01206 
01207 static void sn_coap_protocol_linked_list_blockwise_msg_remove(struct coap_s *handle, coap_blockwise_msg_s *removed_msg_ptr)
01208 {
01209     if( removed_msg_ptr->coap == handle ){
01210         ns_list_remove(&handle->linked_list_blockwise_sent_msgs, removed_msg_ptr);
01211 
01212         if( removed_msg_ptr->coap_msg_ptr ){
01213             if (removed_msg_ptr->coap_msg_ptr->payload_ptr) {
01214                 handle->sn_coap_protocol_free(removed_msg_ptr->coap_msg_ptr->payload_ptr);
01215                 removed_msg_ptr->coap_msg_ptr->payload_ptr = 0;
01216             }
01217 
01218             sn_coap_parser_release_allocated_coap_msg_mem(handle, removed_msg_ptr->coap_msg_ptr);
01219         }
01220 
01221         handle->sn_coap_protocol_free(removed_msg_ptr);
01222         removed_msg_ptr = 0;
01223     }
01224 }
01225 
01226 /**************************************************************************//**
01227  * \fn static void sn_coap_protocol_linked_list_blockwise_payload_store(sn_nsdl_addr_s *addr_ptr, uint16_t stored_payload_len, uint8_t *stored_payload_ptr)
01228  *
01229  * \brief Stores blockwise payload to Linked list
01230  *
01231  * \param *addr_ptr is pointer to Address information to be stored
01232  * \param stored_payload_len is length of stored Payload
01233  * \param *stored_payload_ptr is pointer to stored Payload
01234  *****************************************************************************/
01235 
01236 static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s *handle, sn_nsdl_addr_s *addr_ptr,
01237         uint16_t stored_payload_len,
01238         uint8_t *stored_payload_ptr,
01239         uint32_t block_number)
01240 {
01241     if (!addr_ptr || !stored_payload_len || !stored_payload_ptr) {
01242         return;
01243     }
01244 
01245     coap_blockwise_payload_s *stored_blockwise_payload_ptr = NULL;
01246 
01247     /* * * * Allocating memory for stored Payload  * * * */
01248 
01249     /* Allocate memory for stored Payload's structure */
01250     stored_blockwise_payload_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_payload_s));
01251 
01252     if (stored_blockwise_payload_ptr == NULL) {
01253         tr_error("sn_coap_protocol_linked_list_blockwise_payload_store - failed to allocate blockwise!");
01254         return;
01255     }
01256 
01257 
01258     /* Allocate memory for stored Payload's data */
01259     stored_blockwise_payload_ptr->payload_ptr = handle->sn_coap_protocol_malloc(stored_payload_len);
01260 
01261     if (stored_blockwise_payload_ptr->payload_ptr == NULL) {
01262         tr_error("sn_coap_protocol_linked_list_blockwise_payload_store - failed to allocate payload!");
01263         handle->sn_coap_protocol_free(stored_blockwise_payload_ptr);
01264         stored_blockwise_payload_ptr = 0;
01265         return;
01266     }
01267 
01268     /* Allocate memory for stored Payload's address */
01269     stored_blockwise_payload_ptr->addr_ptr = handle->sn_coap_protocol_malloc(addr_ptr->addr_len);
01270 
01271     if (stored_blockwise_payload_ptr->addr_ptr == NULL) {
01272         tr_error("sn_coap_protocol_linked_list_blockwise_payload_store - failed to allocate address pointer!");
01273         handle->sn_coap_protocol_free(stored_blockwise_payload_ptr->payload_ptr);
01274         stored_blockwise_payload_ptr->payload_ptr = 0;
01275         handle->sn_coap_protocol_free(stored_blockwise_payload_ptr);
01276         stored_blockwise_payload_ptr = 0;
01277 
01278         return;
01279     }
01280 
01281     /* * * * Filling fields of stored Payload  * * * */
01282 
01283     stored_blockwise_payload_ptr->timestamp = handle->system_time;
01284 
01285     memcpy(stored_blockwise_payload_ptr->addr_ptr, addr_ptr->addr_ptr, addr_ptr->addr_len);
01286     stored_blockwise_payload_ptr->port = addr_ptr->port;
01287     memcpy(stored_blockwise_payload_ptr->payload_ptr, stored_payload_ptr, stored_payload_len);
01288     stored_blockwise_payload_ptr->payload_len = stored_payload_len;
01289 
01290     stored_blockwise_payload_ptr->coap = handle;
01291 
01292     stored_blockwise_payload_ptr->block_number = block_number;
01293 
01294     /* * * * Storing Payload to Linked list  * * * */
01295 
01296     ns_list_add_to_end(&handle->linked_list_blockwise_received_payloads, stored_blockwise_payload_ptr);
01297 }
01298 
01299 /**************************************************************************//**
01300  * \fn static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length)
01301  *
01302  * \brief Searches stored blockwise payload from Linked list (Address as key)
01303  *
01304  * \param *addr_ptr is pointer to Address key to be searched
01305  * \param *payload_length is pointer to returned Payload length
01306  *
01307  * \return Return value is pointer to found stored blockwise payload in Linked
01308  *         list or NULL if payload not found
01309  *****************************************************************************/
01310 
01311 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)
01312 {
01313     /* Loop all stored blockwise payloads in Linked list */
01314     ns_list_foreach(coap_blockwise_payload_s, stored_payload_info_ptr, &handle->linked_list_blockwise_received_payloads) {
01315         /* If payload's Source address is same than is searched */
01316         if (0 == memcmp(src_addr_ptr->addr_ptr, stored_payload_info_ptr->addr_ptr, src_addr_ptr->addr_len)) {
01317             /* If payload's Source address port is same than is searched */
01318             if (stored_payload_info_ptr->port == src_addr_ptr->port) {
01319                 /* * * Correct Payload found * * * */
01320                 *payload_length = stored_payload_info_ptr->payload_len;
01321 
01322                 return stored_payload_info_ptr->payload_ptr;
01323             }
01324         }
01325     }
01326 
01327     return NULL;
01328 }
01329 
01330 static bool sn_coap_protocol_linked_list_blockwise_payload_compare_block_number(struct coap_s *handle,
01331                                                                                    sn_nsdl_addr_s *src_addr_ptr,
01332                                                                                    uint32_t block_number)
01333 {
01334     /* Loop all stored blockwise payloads in Linked list */
01335     ns_list_foreach(coap_blockwise_payload_s, stored_payload_info_ptr, &handle->linked_list_blockwise_received_payloads) {
01336         /* If payload's Source address is same than is searched */
01337         if (0 == memcmp(src_addr_ptr->addr_ptr, stored_payload_info_ptr->addr_ptr, src_addr_ptr->addr_len)) {
01338             /* If payload's Source address port is same than is searched */
01339             if (stored_payload_info_ptr->port == src_addr_ptr->port) {
01340                 // Check that incoming block number matches to last received one
01341                 if (block_number - 1 == stored_payload_info_ptr->block_number) {
01342                     return true;
01343                 }
01344             }
01345         }
01346     }
01347     return false;
01348 }
01349 
01350 /**************************************************************************//**
01351  * \fn static void sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(struct coap_s *handle)
01352  *
01353  * \brief Removes current stored blockwise paylod from Linked list
01354  *****************************************************************************/
01355 
01356 static void sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(struct coap_s *handle)
01357 {
01358     coap_blockwise_payload_s *removed_payload_ptr;
01359 
01360     /* Remove oldest node in Linked list*/
01361     removed_payload_ptr = ns_list_get_first(&handle->linked_list_blockwise_received_payloads);
01362 
01363     if (removed_payload_ptr != NULL) {
01364         sn_coap_protocol_linked_list_blockwise_payload_remove(handle, removed_payload_ptr);
01365     }
01366 }
01367 
01368 /**************************************************************************//**
01369  * \fn static void sn_coap_protocol_linked_list_blockwise_payload_remove(struct coap_s *handle,
01370  *                                                      coap_blockwise_msg_s *removed_msg_ptr)
01371  *
01372  * \brief Removes stored blockwise payload from Linked list
01373  *
01374  * \param removed_payload_ptr is payload to be removed
01375  *****************************************************************************/
01376 
01377 static void sn_coap_protocol_linked_list_blockwise_payload_remove(struct coap_s *handle,
01378                                                                   coap_blockwise_payload_s *removed_payload_ptr)
01379 {
01380     ns_list_remove(&handle->linked_list_blockwise_received_payloads, removed_payload_ptr);
01381     /* Free memory of stored payload */
01382     if (removed_payload_ptr->addr_ptr != NULL) {
01383         handle->sn_coap_protocol_free(removed_payload_ptr->addr_ptr);
01384         removed_payload_ptr->addr_ptr = 0;
01385     }
01386 
01387     if (removed_payload_ptr->payload_ptr != NULL) {
01388         handle->sn_coap_protocol_free(removed_payload_ptr->payload_ptr);
01389         removed_payload_ptr->payload_ptr = 0;
01390     }
01391 
01392     handle->sn_coap_protocol_free(removed_payload_ptr);
01393     removed_payload_ptr = 0;
01394 }
01395 
01396 /**************************************************************************//**
01397  * \fn static uint32_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(sn_nsdl_addr_s *src_addr_ptr)
01398  *
01399  * \brief Counts length of Payloads in Linked list (Address as key)
01400  *
01401  * \param *addr_ptr is pointer to Address key
01402  *
01403  * \return Return value is length of Payloads as bytes
01404  *****************************************************************************/
01405 
01406 static uint32_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr)
01407 {
01408     uint32_t ret_whole_payload_len = 0;
01409     /* Loop all stored blockwise payloads in Linked list */
01410     ns_list_foreach(coap_blockwise_payload_s, searched_payload_info_ptr, &handle->linked_list_blockwise_received_payloads) {
01411         /* If payload's Source address is same than is searched */
01412         if (0 == memcmp(src_addr_ptr->addr_ptr, searched_payload_info_ptr->addr_ptr, src_addr_ptr->addr_len)) {
01413             /* If payload's Source address port is same than is searched */
01414             if (searched_payload_info_ptr->port == src_addr_ptr->port) {
01415                 /* * * Correct Payload found * * * */
01416                 ret_whole_payload_len += searched_payload_info_ptr->payload_len;
01417             }
01418         }
01419     }
01420 
01421     return ret_whole_payload_len;
01422 }
01423 
01424 /**************************************************************************//**
01425  * \fn static void sn_coap_protocol_linked_list_blockwise_remove_old_data(struct coap_s *handle)
01426  *
01427  * \brief Removes old stored Blockwise messages and payloads from Linked list
01428  *****************************************************************************/
01429 
01430 static void sn_coap_protocol_linked_list_blockwise_remove_old_data(struct coap_s *handle)
01431 {
01432     /* Loop all stored Blockwise messages in Linked list */
01433     ns_list_foreach_safe(coap_blockwise_msg_s, removed_blocwise_msg_ptr, &handle->linked_list_blockwise_sent_msgs) {
01434         if ((handle->system_time - removed_blocwise_msg_ptr->timestamp)  > SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED) {
01435             //TODO: Check do we need to check handle == removed_blocwise_msg_ptr->coap here?
01436 
01437             /* * * * Old Blockise message found, remove it from Linked list * * * */
01438             if( removed_blocwise_msg_ptr->coap_msg_ptr ){
01439                 if(removed_blocwise_msg_ptr->coap_msg_ptr->payload_ptr){
01440                     handle->sn_coap_protocol_free(removed_blocwise_msg_ptr->coap_msg_ptr->payload_ptr);
01441                     removed_blocwise_msg_ptr->coap_msg_ptr->payload_ptr = 0;
01442                 }
01443                 sn_coap_parser_release_allocated_coap_msg_mem(handle, removed_blocwise_msg_ptr->coap_msg_ptr);
01444                 removed_blocwise_msg_ptr->coap_msg_ptr = 0;
01445             }
01446             sn_coap_protocol_linked_list_blockwise_msg_remove(handle, removed_blocwise_msg_ptr);
01447         }
01448     }
01449 
01450     /* Loop all stored Blockwise payloads in Linked list */
01451     ns_list_foreach_safe(coap_blockwise_payload_s, removed_blocwise_payload_ptr, &handle->linked_list_blockwise_received_payloads) {
01452         if ((handle->system_time - removed_blocwise_payload_ptr->timestamp)  > SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED) {
01453             /* * * * Old Blockise payload found, remove it from Linked list * * * */
01454             sn_coap_protocol_linked_list_blockwise_payload_remove(handle, removed_blocwise_payload_ptr);
01455         }
01456     }
01457 }
01458 
01459 #endif /* SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */
01460 
01461 
01462 #if ENABLE_RESENDINGS  /* If Message resending is not used at all, this part of code will not be compiled */
01463 /***************************************************************************//**
01464  * \fn int8_t sn_coap_protocol_allocate_mem_for_msg(sn_nsdl_addr_s *dst_addr_ptr, uint16_t packet_data_len, coap_send_msg_s *msg_ptr)
01465  *
01466  * \brief Allocates memory for given message (send or blockwise message)
01467  *
01468  * \param *dst_addr_ptr is pointer to destination address where message will be sent
01469  * \param packet_data_len is length of allocated Packet data
01470  * \param uri_path_len is length of messages path url
01471  *
01472  * \return pointer to allocated struct
01473  *****************************************************************************/
01474 
01475 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)
01476 {
01477 
01478     coap_send_msg_s *msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_send_msg_s));
01479 
01480     if (msg_ptr == NULL) {
01481         return NULL;
01482     }
01483 
01484     //Locall structure for 1 malloc for send msg
01485     struct
01486     {
01487         sn_nsdl_transmit_s transmit;
01488         sn_nsdl_addr_s addr;
01489         uint8_t trail_data[];
01490     } *m;
01491     int trail_size = dst_addr_ptr->addr_len + packet_data_len;
01492 
01493     m = handle->sn_coap_protocol_malloc(sizeof *m + trail_size);
01494     if (!m) {
01495         handle->sn_coap_protocol_free(msg_ptr);
01496         return NULL;
01497     }
01498     //Init data
01499     memset(m, 0, sizeof(*m) + trail_size);
01500     memset(msg_ptr, 0, sizeof(coap_send_msg_s));
01501 
01502     msg_ptr->send_msg_ptr = &m->transmit;
01503     msg_ptr->send_msg_ptr->dst_addr_ptr = &m->addr;
01504 
01505     msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr = m->trail_data;
01506     if (packet_data_len) {
01507         msg_ptr->send_msg_ptr->packet_ptr = m->trail_data + dst_addr_ptr->addr_len;
01508     }
01509 
01510     return msg_ptr;
01511 }
01512 
01513 
01514 /**************************************************************************//**
01515  * \fn static void sn_coap_protocol_release_allocated_send_msg_mem(struct coap_s *handle, coap_send_msg_s *freed_send_msg_ptr)
01516  *
01517  * \brief Releases memory of given Sending message (coap_send_msg_s)
01518  *
01519  * \param *freed_send_msg_ptr is pointer to released Sending message
01520  *****************************************************************************/
01521 
01522 static void sn_coap_protocol_release_allocated_send_msg_mem(struct coap_s *handle, coap_send_msg_s *freed_send_msg_ptr)
01523 {
01524     if (freed_send_msg_ptr != NULL) {
01525         handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr);
01526         freed_send_msg_ptr->send_msg_ptr = NULL;
01527         handle->sn_coap_protocol_free(freed_send_msg_ptr);
01528         freed_send_msg_ptr = NULL;
01529     }
01530 }
01531 
01532 /**************************************************************************//**
01533  * \fn static uint16_t sn_coap_count_linked_list_size(const coap_send_msg_list_t *linked_list_ptr)
01534  *
01535  * \brief Counts total message size of all messages in linked list
01536  *
01537  * \param const coap_send_msg_list_t *linked_list_ptr pointer to linked list
01538  *****************************************************************************/
01539 static uint16_t sn_coap_count_linked_list_size(const coap_send_msg_list_t *linked_list_ptr)
01540 {
01541     uint16_t total_size = 0;
01542 
01543     ns_list_foreach(coap_send_msg_s, stored_msg_ptr, linked_list_ptr) {
01544         if (stored_msg_ptr->send_msg_ptr) {
01545             total_size += stored_msg_ptr->send_msg_ptr->packet_len;
01546         }
01547     }
01548 
01549     return total_size;
01550 }
01551 
01552 #endif
01553 
01554 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
01555 void sn_coap_protocol_remove_sent_blockwise_message(struct coap_s *handle, uint16_t message_id)
01556 {
01557     if (!handle) {
01558         return;
01559     }
01560 
01561     ns_list_foreach_safe(coap_blockwise_msg_s, tmp, &handle->linked_list_blockwise_sent_msgs) {
01562         if (tmp->coap == handle && tmp->coap_msg_ptr && tmp->coap_msg_ptr->msg_id == message_id) {
01563             handle->sn_coap_protocol_free(tmp->coap_msg_ptr->payload_ptr);
01564             sn_coap_parser_release_allocated_coap_msg_mem(tmp->coap, tmp->coap_msg_ptr);
01565             ns_list_remove(&handle->linked_list_blockwise_sent_msgs, tmp);
01566             handle->sn_coap_protocol_free(tmp);
01567             break;
01568         }
01569     }
01570 }
01571 
01572 void sn_coap_protocol_block_remove(struct coap_s *handle, sn_nsdl_addr_s *source_address, uint16_t payload_length, void *payload)
01573 {
01574     if (!handle || !source_address || !payload) {
01575         return;
01576     }
01577 
01578     /* Loop all stored blockwise payloads in Linked list */
01579     ns_list_foreach(coap_blockwise_payload_s, stored_payload_info_ptr, &handle->linked_list_blockwise_received_payloads) {
01580         /* If payload's Source address is not the same than is searched */
01581         if (memcmp(source_address->addr_ptr, stored_payload_info_ptr->addr_ptr, source_address->addr_len)) {
01582             continue;
01583         }
01584 
01585         /* If payload's Source address port is not the same than is searched */
01586         if (stored_payload_info_ptr->port != source_address->port) {
01587             continue;
01588         }
01589 
01590         /* Check the payload */
01591         if(payload_length != stored_payload_info_ptr->payload_len){
01592             continue;
01593         }
01594 
01595         if(!memcmp(stored_payload_info_ptr->payload_ptr, payload, stored_payload_info_ptr->payload_len))
01596         {
01597             /* Everything matches, remove and return. */
01598             sn_coap_protocol_linked_list_blockwise_payload_remove(handle, stored_payload_info_ptr);
01599             return;
01600         }
01601     }
01602 }
01603 /**************************************************************************//**
01604  * \fn static int8_t sn_coap_handle_blockwise_message(void)
01605  *
01606  * \brief Handles all received blockwise messages
01607  *
01608  * \param *src_addr_ptr pointer to source address information struct
01609  * \param *received_coap_msg_ptr pointer to parsed CoAP message structure
01610  *****************************************************************************/
01611 
01612 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)
01613 {
01614     sn_coap_hdr_s *src_coap_blockwise_ack_msg_ptr = NULL;
01615     uint16_t dst_packed_data_needed_mem = 0;
01616     uint8_t *dst_ack_packet_data_ptr = NULL;
01617     uint8_t block_temp = 0;
01618 
01619     uint16_t original_payload_len = 0;
01620     uint8_t *original_payload_ptr = NULL;
01621 
01622     /* Block1 Option in a request (e.g., PUT or POST) */
01623     // Blocked request sending, received ACK, sending next block..
01624     if (received_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) {
01625         if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) {
01626             if (received_coap_msg_ptr->options_list_ptr->block1 & 0x08) {
01627                 coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = NULL;
01628 
01629                 /* Get  */
01630                 ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) {
01631                     if (msg->coap_msg_ptr && received_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) {
01632                         stored_blockwise_msg_temp_ptr = msg;
01633                         break;
01634                     }
01635                 }
01636 
01637                 if (stored_blockwise_msg_temp_ptr) {
01638                     /* Build response message */
01639 
01640                     uint16_t block_size;
01641                     uint32_t block_number;
01642 
01643                     /* Get block option parameters from received message */
01644                     block_number = received_coap_msg_ptr->options_list_ptr->block1 >> 4;
01645                     block_temp = received_coap_msg_ptr->options_list_ptr->block1 & 0x07;
01646                     block_size = 1u << (block_temp + 4);
01647 
01648                     /* Build next block message */
01649                     src_coap_blockwise_ack_msg_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr;
01650 
01651                     if (src_coap_blockwise_ack_msg_ptr->options_list_ptr) {
01652                         src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 = COAP_OPTION_BLOCK_NONE;
01653                         src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE;
01654                     } else {
01655                         if (!sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr)) {
01656                             tr_error("sn_coap_handle_blockwise_message - (send block1) failed to allocate ack message!");
01657                             sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01658                             return 0;
01659                         }
01660                     }
01661 
01662                     block_number++;
01663                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 = (block_number << 4) | block_temp;
01664 
01665                     original_payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len;
01666                     original_payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr;
01667 
01668                     if ((block_size * (block_number + 1)) > stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len) {
01669                         src_coap_blockwise_ack_msg_ptr->payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len - (block_size * (block_number));
01670                         src_coap_blockwise_ack_msg_ptr->payload_ptr = src_coap_blockwise_ack_msg_ptr->payload_ptr + (block_size * block_number);
01671                     }
01672 
01673                     /* Not last block */
01674                     else {
01675                         /* set more - bit */
01676                         src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 |= 0x08;
01677                         src_coap_blockwise_ack_msg_ptr->payload_len = block_size;
01678                         src_coap_blockwise_ack_msg_ptr->payload_ptr = src_coap_blockwise_ack_msg_ptr->payload_ptr + (block_size * block_number);
01679                     }
01680                     /* Build and send block message */
01681                     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);
01682 
01683                     dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
01684                     if (!dst_ack_packet_data_ptr) {
01685                         tr_error("sn_coap_handle_blockwise_message - (send block1) failed to allocate ack message!");
01686                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
01687                         src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
01688                         handle->sn_coap_protocol_free(original_payload_ptr);
01689                         original_payload_ptr = 0;
01690                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01691                         src_coap_blockwise_ack_msg_ptr = 0;
01692                         stored_blockwise_msg_temp_ptr->coap_msg_ptr = NULL;
01693                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01694                         return NULL;
01695                     }
01696                     src_coap_blockwise_ack_msg_ptr->msg_id = message_id++;
01697                     if (message_id == 0) {
01698                         message_id = 1;
01699                     }
01700 
01701                     sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
01702                     handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
01703 
01704                     handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
01705                     dst_ack_packet_data_ptr = 0;
01706 
01707                     stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len = original_payload_len;
01708                     stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = original_payload_ptr;
01709 
01710                     received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_ACK;
01711                 }
01712             } else {
01713                 // XXX what was this trying to free?
01714                 received_coap_msg_ptr->coap_status = COAP_STATUS_OK;
01715 
01716             }
01717         }
01718 
01719         // Blocked request receiving
01720         else {
01721             if (received_coap_msg_ptr->payload_len > handle->sn_coap_block_data_size) {
01722                 received_coap_msg_ptr->payload_len = handle->sn_coap_block_data_size;
01723             }
01724 
01725             // Check that incoming block number is in order.
01726             uint32_t block_number = received_coap_msg_ptr->options_list_ptr->block1 >> 4;
01727             bool blocks_in_order = true;
01728             if (block_number > 0 &&
01729                 !sn_coap_protocol_linked_list_blockwise_payload_compare_block_number(handle,
01730                                                                                      src_addr_ptr,
01731                                                                                      block_number)) {
01732                 blocks_in_order = false;
01733             }
01734 
01735             sn_coap_protocol_linked_list_blockwise_payload_store(handle,
01736                                                                  src_addr_ptr,
01737                                                                  received_coap_msg_ptr->payload_len,
01738                                                                  received_coap_msg_ptr->payload_ptr,
01739                                                                  block_number);
01740 
01741             /* If not last block (more value is set) */
01742             /* 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. */
01743             if (received_coap_msg_ptr->options_list_ptr->block1 & 0x08) {
01744                 src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message(handle);
01745                 if (src_coap_blockwise_ack_msg_ptr == NULL) {
01746                     tr_error("sn_coap_handle_blockwise_message - (recv block1) failed to allocate ack message!");
01747                     sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01748                     return NULL;
01749                 }
01750 
01751                 if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) {
01752                    tr_error("sn_coap_handle_blockwise_message - (recv block1) failed to allocate options!");
01753                     handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01754                     src_coap_blockwise_ack_msg_ptr = 0;
01755                     sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01756                     return NULL;
01757                 }
01758 
01759                 if (!blocks_in_order) {
01760                     tr_error("sn_coap_handle_blockwise_message - (recv block1) COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE!");
01761                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE;
01762                 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
01763                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
01764                 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) {
01765                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTINUE;
01766                 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) {
01767                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTINUE;
01768                 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) {
01769                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_DELETED;
01770                 }
01771 
01772                 // Response with COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE if the payload size is more than we can handle
01773                 if (received_coap_msg_ptr->options_list_ptr->size1 > SN_COAP_MAX_INCOMING_BLOCK_MESSAGE_SIZE) {
01774                     // Include maximum size that stack can handle into response
01775                     tr_error("sn_coap_handle_blockwise_message - (recv block1) COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE!");
01776                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE;
01777                 }
01778                 else {
01779                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 = received_coap_msg_ptr->options_list_ptr->block1;
01780                     src_coap_blockwise_ack_msg_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
01781 
01782                     /* Check block size */
01783                     block_temp = (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 & 0x07);
01784                     uint16_t block_size = 1u << (block_temp + 4);
01785                     if (block_size >  handle->sn_coap_block_data_size) {
01786                          // Include maximum size that stack can handle into response
01787                          tr_error("sn_coap_handle_blockwise_message - (recv block1) COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE!");
01788                          src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE;
01789                          src_coap_blockwise_ack_msg_ptr->options_list_ptr->size1 = handle->sn_coap_block_data_size;
01790                          sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle);
01791                     }
01792 
01793                     if (block_temp > sn_coap_convert_block_size(handle->sn_coap_block_data_size)) {
01794                         src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 &= 0xFFFFF8;
01795                         src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 |= sn_coap_convert_block_size(handle->sn_coap_block_data_size);
01796                     }
01797                 }
01798 
01799                 src_coap_blockwise_ack_msg_ptr->msg_id = received_coap_msg_ptr->msg_id;
01800 
01801                 // Copy token to response
01802                 src_coap_blockwise_ack_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(received_coap_msg_ptr->token_len);
01803                 if (src_coap_blockwise_ack_msg_ptr->token_ptr) {
01804                     memcpy(src_coap_blockwise_ack_msg_ptr->token_ptr, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len);
01805                     src_coap_blockwise_ack_msg_ptr->token_len = received_coap_msg_ptr->token_len;
01806                 }
01807 
01808                 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);
01809 
01810                 dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
01811                 if (!dst_ack_packet_data_ptr) {
01812                     tr_error("sn_coap_handle_blockwise_message - (recv block1) message allocation failed!");
01813                     sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01814                     handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
01815                     src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
01816                     handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01817                     src_coap_blockwise_ack_msg_ptr = 0;
01818                     return NULL;
01819                 }
01820 
01821                 sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
01822                 handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
01823 
01824                 sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr);
01825                 handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
01826                 dst_ack_packet_data_ptr = 0;
01827 
01828                 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING;
01829 
01830             } else {
01831                 /* * * This is the last block when whole Blockwise payload from received * * */
01832                 /* * * blockwise messages is gathered and returned to User               * * */
01833 
01834                 /* Store last Blockwise payload to Linked list */
01835                 uint16_t payload_len            = 0;
01836                 uint8_t *payload_ptr            = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
01837                 uint32_t whole_payload_len      = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr);
01838                 uint8_t *temp_whole_payload_ptr = NULL;
01839 
01840                 temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len);
01841                 if (temp_whole_payload_ptr == NULL || whole_payload_len > UINT16_MAX) {
01842                     tr_error("sn_coap_handle_blockwise_message - (recv block1) failed to allocate all blocks!");
01843                     sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01844                     handle->sn_coap_protocol_free(temp_whole_payload_ptr);
01845                     return 0;
01846                 }
01847 
01848                 // In block message case, payload_ptr freeing must be done in application level
01849                 received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr;
01850                 received_coap_msg_ptr->payload_len = whole_payload_len;
01851 
01852                 /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */
01853                 while (payload_ptr != NULL) {
01854                     memcpy(temp_whole_payload_ptr, payload_ptr, payload_len);
01855                     temp_whole_payload_ptr += payload_len;
01856                     sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle);
01857                     payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
01858                 }
01859                 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED;
01860             }
01861         }
01862     }
01863 
01864 
01865     /* Block2 Option in a response (e.g., a 2.05 response for GET) */
01866     /* Message ID must be same than in received message */
01867     else {
01868         //This is response to request we made
01869         if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) {
01870             if (handle->sn_coap_internal_block2_resp_handling) {
01871                 uint32_t block_number = 0;
01872 
01873                 /* Store blockwise payload to Linked list */
01874                 //todo: add block number to stored values - just to make sure all packets are in order
01875                 sn_coap_protocol_linked_list_blockwise_payload_store(handle,
01876                                                                      src_addr_ptr,
01877                                                                      received_coap_msg_ptr->payload_len,
01878                                                                      received_coap_msg_ptr->payload_ptr,
01879                                                                      received_coap_msg_ptr->options_list_ptr->block2 >> 4);
01880                 /* If not last block (more value is set) */
01881                 if (received_coap_msg_ptr->options_list_ptr->block2 & 0x08) {
01882                     coap_blockwise_msg_s *previous_blockwise_msg_ptr = NULL;
01883                     //build and send ack
01884                     received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING;
01885 
01886                     ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) {
01887                         if (received_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) {
01888                             previous_blockwise_msg_ptr = msg;
01889                             break;
01890                         }
01891                     }
01892 
01893                     if (!previous_blockwise_msg_ptr || !previous_blockwise_msg_ptr->coap_msg_ptr) {
01894                         tr_error("sn_coap_handle_blockwise_message - (send block2) previous message null!");
01895                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01896                         return 0;
01897                     }
01898 
01899                     src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message(handle);
01900                     if (src_coap_blockwise_ack_msg_ptr == NULL) {
01901                         tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate message!");
01902                         return 0;
01903                     }
01904 
01905                     /* * * Then build CoAP Acknowledgement message * * */
01906 
01907                     if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) {
01908                         tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate options!");
01909                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01910                         src_coap_blockwise_ack_msg_ptr = 0;
01911                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01912                         return NULL;
01913                     }
01914 
01915                     src_coap_blockwise_ack_msg_ptr->msg_id = message_id++;
01916                     if (message_id == 0) {
01917                         message_id = 1;
01918                     }
01919 
01920                     /* Update block option */
01921                     block_temp = received_coap_msg_ptr->options_list_ptr->block2 & 0x07;
01922 
01923                     block_number = received_coap_msg_ptr->options_list_ptr->block2 >> 4;
01924                     block_number ++;
01925 
01926                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = (block_number << 4) | block_temp;
01927 
01928 
01929                     /* Set BLOCK2 (subsequent) GET msg code and copy uri path from previous msg*/
01930                     if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_RESPONSE_CONTENT) {
01931                         src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_REQUEST_GET;
01932                         if (previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_ptr) {
01933                             src_coap_blockwise_ack_msg_ptr->uri_path_len = previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_len;
01934                             src_coap_blockwise_ack_msg_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_len);
01935                             if (!src_coap_blockwise_ack_msg_ptr->uri_path_ptr) {
01936                                 sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr);
01937                                 tr_error("sn_coap_handle_blockwise_message - failed to allocate for uri path ptr!");
01938                                 return NULL;
01939                             }
01940                             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);
01941                         }
01942                         if (previous_blockwise_msg_ptr->coap_msg_ptr->token_ptr) {
01943                             src_coap_blockwise_ack_msg_ptr->token_len = previous_blockwise_msg_ptr->coap_msg_ptr->token_len;
01944                             src_coap_blockwise_ack_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(previous_blockwise_msg_ptr->coap_msg_ptr->token_len);
01945                             if (!src_coap_blockwise_ack_msg_ptr->token_ptr) {
01946                                 sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr);
01947                                 tr_error("sn_coap_handle_blockwise_message - failed to allocate for token ptr!");
01948                                 return NULL;
01949                             }
01950                             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);
01951                         }
01952                     }
01953 
01954                     ns_list_remove(&handle->linked_list_blockwise_sent_msgs, previous_blockwise_msg_ptr);
01955                     if (previous_blockwise_msg_ptr->coap_msg_ptr) {
01956                         if (previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr) {
01957                             handle->sn_coap_protocol_free(previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr);
01958                             previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr = 0;
01959                         }
01960                         sn_coap_parser_release_allocated_coap_msg_mem(handle, previous_blockwise_msg_ptr->coap_msg_ptr);
01961                         previous_blockwise_msg_ptr->coap_msg_ptr = 0;
01962                     }
01963                     handle->sn_coap_protocol_free(previous_blockwise_msg_ptr);
01964                     previous_blockwise_msg_ptr = 0;
01965 
01966                     /* Then get needed memory count for Packet data */
01967                     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);
01968 
01969                     /* Then allocate memory for Packet data */
01970                     dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
01971 
01972                     if (dst_ack_packet_data_ptr == NULL) {
01973                         tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate packet!");
01974                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
01975                         src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
01976                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01977                         src_coap_blockwise_ack_msg_ptr = 0;
01978                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01979                         return NULL;
01980                     }
01981                     memset(dst_ack_packet_data_ptr, 0, dst_packed_data_needed_mem);
01982 
01983                     /* * * Then build Acknowledgement message to Packed data * * */
01984                     if ((sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size)) < 0) {
01985                         tr_error("sn_coap_handle_blockwise_message - (send block2) builder failed!");
01986                         handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
01987                         dst_ack_packet_data_ptr = 0;
01988                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
01989                         src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
01990                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01991                         src_coap_blockwise_ack_msg_ptr = 0;
01992                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01993                         return NULL;
01994                     }
01995 
01996                     /* * * Save to linked list * * */
01997                     coap_blockwise_msg_s *stored_blockwise_msg_ptr;
01998 
01999                     stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
02000                     if (!stored_blockwise_msg_ptr) {
02001                         tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate blockwise message!");
02002                         handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
02003                         dst_ack_packet_data_ptr = 0;
02004                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
02005                         src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
02006                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
02007                         src_coap_blockwise_ack_msg_ptr = 0;
02008                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
02009                         return 0;
02010                     }
02011                     memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
02012 
02013                     stored_blockwise_msg_ptr->timestamp = handle->system_time;
02014 
02015                     stored_blockwise_msg_ptr->coap_msg_ptr = src_coap_blockwise_ack_msg_ptr;
02016                     stored_blockwise_msg_ptr->coap = handle;
02017                     ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr);
02018 
02019                     /* * * Then release memory of CoAP Acknowledgement message * * */
02020                     handle->sn_coap_tx_callback(dst_ack_packet_data_ptr,
02021                                                 dst_packed_data_needed_mem, src_addr_ptr, param);
02022 
02023     #if ENABLE_RESENDINGS
02024                     uint32_t resend_time = sn_coap_calculate_new_resend_time(handle->system_time, handle->sn_coap_resending_intervall, 0);
02025                     sn_coap_protocol_linked_list_send_msg_store(handle, src_addr_ptr,
02026                             dst_packed_data_needed_mem,
02027                             dst_ack_packet_data_ptr,
02028                             resend_time, param);
02029     #endif
02030                     handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
02031                     dst_ack_packet_data_ptr = 0;
02032                 }
02033 
02034                 //Last block received
02035                 else {
02036                     /* * * This is the last block when whole Blockwise payload from received * * */
02037                     /* * * blockwise messages is gathered and returned to User               * * */
02038 
02039                     /* Store last Blockwise payload to Linked list */
02040                     uint16_t payload_len            = 0;
02041                     uint8_t *payload_ptr            = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
02042                     uint16_t whole_payload_len      = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr);
02043                     uint8_t *temp_whole_payload_ptr = NULL;
02044 
02045                     temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len);
02046                     if (!temp_whole_payload_ptr) {
02047                         tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate whole payload!");
02048                         return 0;
02049                     }
02050 
02051                     received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr;
02052                     received_coap_msg_ptr->payload_len = whole_payload_len;
02053 
02054                     /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */
02055                     while (payload_ptr != NULL) {
02056                         memcpy(temp_whole_payload_ptr, payload_ptr, payload_len);
02057 
02058                         temp_whole_payload_ptr += payload_len;
02059 
02060                         sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle);
02061                         payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
02062                     }
02063                     received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED;
02064 
02065                     //todo: remove previous msg from list
02066                 }
02067             }
02068         }
02069 
02070         //Now we send data to request
02071         else {
02072             //Get message by using block number
02073             //NOTE: Getting the first from list might not be correct one
02074             coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = ns_list_get_first(&handle->linked_list_blockwise_sent_msgs);
02075             if (stored_blockwise_msg_temp_ptr) {
02076                 uint16_t block_size;
02077                 uint32_t block_number;
02078 
02079                 /* Resolve block parameters */
02080                 block_number = received_coap_msg_ptr->options_list_ptr->block2 >> 4;
02081                 block_temp = received_coap_msg_ptr->options_list_ptr->block2 & 0x07;
02082                 block_size = 1u << (block_temp + 4);
02083 
02084                 /* Build response message */
02085                 src_coap_blockwise_ack_msg_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr;
02086 
02087                 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr) {
02088                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 = COAP_OPTION_BLOCK_NONE;
02089                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE;
02090                 } else {
02091                     if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) {
02092                         tr_error("sn_coap_handle_blockwise_message - (recv block2) failed to allocate options!");
02093                         return 0;
02094                     }
02095                 }
02096 
02097                 src_coap_blockwise_ack_msg_ptr->msg_id = received_coap_msg_ptr->msg_id;
02098 
02099                 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = received_coap_msg_ptr->options_list_ptr->block2;
02100 
02101                 /* * Payload part * */
02102 
02103                 /* Check if last block */
02104 
02105                 original_payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len;
02106                 original_payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr;
02107 
02108                 if ((block_size * (block_number + 1)) >= stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len) {
02109                     src_coap_blockwise_ack_msg_ptr->payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len - (block_size * block_number);
02110                     src_coap_blockwise_ack_msg_ptr->payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr + (block_size * block_number);
02111                 }
02112                 /* Not last block */
02113                 else {
02114                     /* set more - bit */
02115                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 |= 0x08;
02116                     src_coap_blockwise_ack_msg_ptr->payload_len = block_size;
02117                     src_coap_blockwise_ack_msg_ptr->payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr + (block_size * block_number);
02118                 }
02119 
02120                 /* Update token to match one which is in GET request.
02121                  * This is needed only in case of notification message.
02122                 */
02123                 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr &&
02124                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) {
02125                     if (received_coap_msg_ptr->token_len && src_coap_blockwise_ack_msg_ptr->token_ptr) {
02126                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->token_ptr);
02127                         src_coap_blockwise_ack_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(received_coap_msg_ptr->token_len);
02128                         if (src_coap_blockwise_ack_msg_ptr->token_ptr) {
02129                             memcpy(src_coap_blockwise_ack_msg_ptr->token_ptr, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len);
02130                             src_coap_blockwise_ack_msg_ptr->token_len = received_coap_msg_ptr->token_len;
02131                         }
02132                     }
02133                 }
02134 
02135                 /* Build and send block message */
02136                 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);
02137 
02138                 dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
02139                 if (!dst_ack_packet_data_ptr) {
02140                     tr_error("sn_coap_handle_blockwise_message - (recv block2) failed to allocate packet!");
02141                     if(original_payload_ptr){
02142                         handle->sn_coap_protocol_free(original_payload_ptr);
02143                         original_payload_ptr = NULL;
02144                     }
02145                     handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
02146                     src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
02147                     handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
02148                     stored_blockwise_msg_temp_ptr->coap_msg_ptr = NULL;
02149                     return NULL;
02150                 }
02151 
02152                 sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
02153                 handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
02154 
02155                 handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
02156                 dst_ack_packet_data_ptr = 0;
02157 
02158                 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len = original_payload_len;
02159                 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = original_payload_ptr;
02160 
02161                 if ((block_size * (block_number + 1)) >= stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len) {
02162                     sn_coap_protocol_linked_list_blockwise_msg_remove(handle, stored_blockwise_msg_temp_ptr);
02163                 }
02164 
02165                 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_ACK;
02166             }
02167         }
02168     }
02169     return received_coap_msg_ptr;
02170 }
02171 
02172 int8_t sn_coap_convert_block_size(uint16_t block_size)
02173 {
02174     if (block_size == 16) {
02175         return 0;
02176     } else if (block_size == 32) {
02177         return 1;
02178     } else if (block_size == 64) {
02179         return 2;
02180     } else if (block_size == 128) {
02181         return 3;
02182     } else if (block_size == 256) {
02183         return 4;
02184     } else if (block_size == 512) {
02185         return 5;
02186     } else if (block_size == 1024) {
02187         return 6;
02188     }
02189 
02190     return 0;
02191 }
02192 
02193 static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coap_hdr_s *source_header_ptr)
02194 {
02195     sn_coap_hdr_s *destination_header_ptr;
02196 
02197     destination_header_ptr = sn_coap_parser_alloc_message(handle);
02198     if (!destination_header_ptr) {
02199         tr_error("sn_coap_protocol_copy_header - failed to allocate message!");
02200         return 0;
02201     }
02202 
02203     destination_header_ptr->coap_status = source_header_ptr->coap_status;
02204     destination_header_ptr->msg_type = source_header_ptr->msg_type;
02205     destination_header_ptr->msg_code = source_header_ptr->msg_code;
02206     destination_header_ptr->msg_id = source_header_ptr->msg_id;
02207 
02208     if (source_header_ptr->uri_path_ptr) {
02209         destination_header_ptr->uri_path_len = source_header_ptr->uri_path_len;
02210         destination_header_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->uri_path_len);
02211         if (!destination_header_ptr->uri_path_ptr) {
02212             tr_error("sn_coap_protocol_copy_header - failed to allocate uri path!");
02213             sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02214             return 0;
02215         }
02216         memcpy(destination_header_ptr->uri_path_ptr, source_header_ptr->uri_path_ptr, source_header_ptr->uri_path_len);
02217     }
02218 
02219     if (source_header_ptr->token_ptr) {
02220         destination_header_ptr->token_len = source_header_ptr->token_len;
02221         destination_header_ptr->token_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->token_len);
02222         if (!destination_header_ptr->token_ptr) {
02223             sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02224             tr_error("sn_coap_protocol_copy_header - failed to allocate token!");
02225             return 0;
02226         }
02227         memcpy(destination_header_ptr->token_ptr, source_header_ptr->token_ptr, source_header_ptr->token_len);
02228     }
02229 
02230     destination_header_ptr->content_format = source_header_ptr->content_format;
02231 
02232     /* Options list */
02233     if (source_header_ptr->options_list_ptr) {
02234         if (sn_coap_parser_alloc_options(handle, destination_header_ptr) == NULL) {
02235             sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02236             tr_error("sn_coap_protocol_copy_header - failed to allocate options!");
02237             return 0;
02238         }
02239 
02240         destination_header_ptr->options_list_ptr->max_age = source_header_ptr->options_list_ptr->max_age;
02241 
02242         if (source_header_ptr->options_list_ptr->proxy_uri_ptr) {
02243             destination_header_ptr->options_list_ptr->proxy_uri_len = source_header_ptr->options_list_ptr->proxy_uri_len;
02244             destination_header_ptr->options_list_ptr->proxy_uri_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->proxy_uri_len);
02245             if (!destination_header_ptr->options_list_ptr->proxy_uri_ptr) {
02246                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02247                 tr_error("sn_coap_protocol_copy_header - failed to allocate proxy uri!");
02248                 return 0;
02249             }
02250             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);
02251         }
02252 
02253         if (source_header_ptr->options_list_ptr->etag_ptr) {
02254             destination_header_ptr->options_list_ptr->etag_len = source_header_ptr->options_list_ptr->etag_len;
02255             destination_header_ptr->options_list_ptr->etag_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->etag_len);
02256             if (!destination_header_ptr->options_list_ptr->etag_ptr) {
02257                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02258                 tr_error("sn_coap_protocol_copy_header - failed to allocate etag!");
02259                 return 0;
02260             }
02261             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);
02262         }
02263 
02264         if (source_header_ptr->options_list_ptr->uri_host_ptr) {
02265             destination_header_ptr->options_list_ptr->uri_host_len = source_header_ptr->options_list_ptr->uri_host_len;
02266             destination_header_ptr->options_list_ptr->uri_host_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_host_len);
02267             if (!destination_header_ptr->options_list_ptr->uri_host_ptr) {
02268                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02269                 tr_error("sn_coap_protocol_copy_header - failed to allocate uri host!");
02270                 return 0;
02271             }
02272             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);
02273         }
02274 
02275         if (source_header_ptr->options_list_ptr->location_path_ptr) {
02276             destination_header_ptr->options_list_ptr->location_path_len = source_header_ptr->options_list_ptr->location_path_len;
02277             destination_header_ptr->options_list_ptr->location_path_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->location_path_len);
02278             if (!destination_header_ptr->options_list_ptr->location_path_ptr) {
02279                 tr_error("sn_coap_protocol_copy_header - failed to allocate location path!");
02280                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02281                 return 0;
02282             }
02283             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);
02284         }
02285 
02286         destination_header_ptr->options_list_ptr->uri_port = source_header_ptr->options_list_ptr->uri_port;
02287 
02288         if (source_header_ptr->options_list_ptr->location_query_ptr) {
02289             destination_header_ptr->options_list_ptr->location_query_len = source_header_ptr->options_list_ptr->location_query_len;
02290             destination_header_ptr->options_list_ptr->location_query_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->location_query_len);
02291             if (!destination_header_ptr->options_list_ptr->location_query_ptr) {
02292                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02293                 tr_error("sn_coap_protocol_copy_header - failed to allocate location query!");
02294                 return 0;
02295             }
02296             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);
02297         }
02298 
02299         destination_header_ptr->options_list_ptr->observe = source_header_ptr->options_list_ptr->observe;
02300         destination_header_ptr->options_list_ptr->accept = source_header_ptr->options_list_ptr->accept;
02301 
02302         if (source_header_ptr->options_list_ptr->uri_query_ptr) {
02303             destination_header_ptr->options_list_ptr->uri_query_len = source_header_ptr->options_list_ptr->uri_query_len;
02304             destination_header_ptr->options_list_ptr->uri_query_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_query_len);
02305             if (!destination_header_ptr->options_list_ptr->uri_query_ptr) {
02306                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02307                 tr_error("sn_coap_protocol_copy_header - failed to allocate uri query!");
02308                 return 0;
02309             }
02310             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);
02311         }
02312 
02313         destination_header_ptr->options_list_ptr->block1 = source_header_ptr->options_list_ptr->block1;
02314         destination_header_ptr->options_list_ptr->block2 = source_header_ptr->options_list_ptr->block2;
02315     }
02316 
02317     return destination_header_ptr;
02318 }
02319 #endif