Toyomasa Watarai / Mbed OS Mbed-example-WS-W27

Dependencies:   MMA7660 LM75B

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_block_remove(struct coap_s *handle, sn_nsdl_addr_s *source_address, uint16_t payload_length, void *payload)
01556 {
01557     if(!handle || !source_address || !payload){
01558         return;
01559     }
01560 
01561     /* Loop all stored blockwise payloads in Linked list */
01562     ns_list_foreach(coap_blockwise_payload_s, stored_payload_info_ptr, &handle->linked_list_blockwise_received_payloads) {
01563         /* If payload's Source address is not the same than is searched */
01564         if (memcmp(source_address->addr_ptr, stored_payload_info_ptr->addr_ptr, source_address->addr_len)) {
01565             continue;
01566         }
01567 
01568         /* If payload's Source address port is not the same than is searched */
01569         if (stored_payload_info_ptr->port != source_address->port) {
01570             continue;
01571         }
01572 
01573         /* Check the payload */
01574         if(payload_length != stored_payload_info_ptr->payload_len){
01575             continue;
01576         }
01577 
01578         if(!memcmp(stored_payload_info_ptr->payload_ptr, payload, stored_payload_info_ptr->payload_len))
01579         {
01580             /* Everything matches, remove and return. */
01581             sn_coap_protocol_linked_list_blockwise_payload_remove(handle, stored_payload_info_ptr);
01582             return;
01583         }
01584     }
01585 }
01586 /**************************************************************************//**
01587  * \fn static int8_t sn_coap_handle_blockwise_message(void)
01588  *
01589  * \brief Handles all received blockwise messages
01590  *
01591  * \param *src_addr_ptr pointer to source address information struct
01592  * \param *received_coap_msg_ptr pointer to parsed CoAP message structure
01593  *****************************************************************************/
01594 
01595 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)
01596 {
01597     sn_coap_hdr_s *src_coap_blockwise_ack_msg_ptr = NULL;
01598     uint16_t dst_packed_data_needed_mem = 0;
01599     uint8_t *dst_ack_packet_data_ptr = NULL;
01600     uint8_t block_temp = 0;
01601 
01602     uint16_t original_payload_len = 0;
01603     uint8_t *original_payload_ptr = NULL;
01604 
01605     /* Block1 Option in a request (e.g., PUT or POST) */
01606     // Blocked request sending, received ACK, sending next block..
01607     if (received_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) {
01608         if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) {
01609             if (received_coap_msg_ptr->options_list_ptr->block1 & 0x08) {
01610                 coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = NULL;
01611 
01612                 /* Get  */
01613                 ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) {
01614                     if (msg->coap_msg_ptr && received_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) {
01615                         stored_blockwise_msg_temp_ptr = msg;
01616                         break;
01617                     }
01618                 }
01619 
01620                 if (stored_blockwise_msg_temp_ptr) {
01621                     /* Build response message */
01622 
01623                     uint16_t block_size;
01624                     uint32_t block_number;
01625 
01626                     /* Get block option parameters from received message */
01627                     block_number = received_coap_msg_ptr->options_list_ptr->block1 >> 4;
01628                     block_temp = received_coap_msg_ptr->options_list_ptr->block1 & 0x07;
01629                     block_size = 1u << (block_temp + 4);
01630 
01631                     /* Build next block message */
01632                     src_coap_blockwise_ack_msg_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr;
01633 
01634                     if (src_coap_blockwise_ack_msg_ptr->options_list_ptr) {
01635                         src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 = COAP_OPTION_BLOCK_NONE;
01636                         src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE;
01637                     } else {
01638                         if (!sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr)) {
01639                             tr_error("sn_coap_handle_blockwise_message - (send block1) failed to allocate ack message!");
01640                             sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01641                             return 0;
01642                         }
01643                     }
01644 
01645                     block_number++;
01646                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 = (block_number << 4) | block_temp;
01647 
01648                     original_payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len;
01649                     original_payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr;
01650 
01651                     if ((block_size * (block_number + 1)) > stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len) {
01652                         src_coap_blockwise_ack_msg_ptr->payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len - (block_size * (block_number));
01653                         src_coap_blockwise_ack_msg_ptr->payload_ptr = src_coap_blockwise_ack_msg_ptr->payload_ptr + (block_size * block_number);
01654                     }
01655 
01656                     /* Not last block */
01657                     else {
01658                         /* set more - bit */
01659                         src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 |= 0x08;
01660                         src_coap_blockwise_ack_msg_ptr->payload_len = block_size;
01661                         src_coap_blockwise_ack_msg_ptr->payload_ptr = src_coap_blockwise_ack_msg_ptr->payload_ptr + (block_size * block_number);
01662                     }
01663                     /* Build and send block message */
01664                     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);
01665 
01666                     dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
01667                     if (!dst_ack_packet_data_ptr) {
01668                         tr_error("sn_coap_handle_blockwise_message - (send block1) failed to allocate ack message!");
01669                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
01670                         src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
01671                         handle->sn_coap_protocol_free(original_payload_ptr);
01672                         original_payload_ptr = 0;
01673                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01674                         src_coap_blockwise_ack_msg_ptr = 0;
01675                         stored_blockwise_msg_temp_ptr->coap_msg_ptr = NULL;
01676                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01677                         return NULL;
01678                     }
01679                     src_coap_blockwise_ack_msg_ptr->msg_id = message_id++;
01680                     if (message_id == 0) {
01681                         message_id = 1;
01682                     }
01683 
01684                     sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
01685                     handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
01686 
01687                     handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
01688                     dst_ack_packet_data_ptr = 0;
01689 
01690                     stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len = original_payload_len;
01691                     stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = original_payload_ptr;
01692 
01693                     received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_ACK;
01694                 }
01695             } else {
01696                 // XXX what was this trying to free?
01697                 received_coap_msg_ptr->coap_status = COAP_STATUS_OK;
01698 
01699             }
01700         }
01701 
01702         // Blocked request receiving
01703         else {
01704             if (received_coap_msg_ptr->payload_len > handle->sn_coap_block_data_size) {
01705                 received_coap_msg_ptr->payload_len = handle->sn_coap_block_data_size;
01706             }
01707 
01708             // Check that incoming block number is in order.
01709             uint32_t block_number = received_coap_msg_ptr->options_list_ptr->block1 >> 4;
01710             bool blocks_in_order = true;
01711             if (block_number > 0 &&
01712                 !sn_coap_protocol_linked_list_blockwise_payload_compare_block_number(handle,
01713                                                                                      src_addr_ptr,
01714                                                                                      block_number)) {
01715                 blocks_in_order = false;
01716             }
01717 
01718             sn_coap_protocol_linked_list_blockwise_payload_store(handle,
01719                                                                  src_addr_ptr,
01720                                                                  received_coap_msg_ptr->payload_len,
01721                                                                  received_coap_msg_ptr->payload_ptr,
01722                                                                  block_number);
01723 
01724             /* If not last block (more value is set) */
01725             /* 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. */
01726             if (received_coap_msg_ptr->options_list_ptr->block1 & 0x08) {
01727                 src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message(handle);
01728                 if (src_coap_blockwise_ack_msg_ptr == NULL) {
01729                     tr_error("sn_coap_handle_blockwise_message - (recv block1) failed to allocate ack message!");
01730                     sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01731                     return NULL;
01732                 }
01733 
01734                 if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) {
01735                    tr_error("sn_coap_handle_blockwise_message - (recv block1) failed to allocate options!");
01736                     handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01737                     src_coap_blockwise_ack_msg_ptr = 0;
01738                     sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01739                     return NULL;
01740                 }
01741 
01742                 if (!blocks_in_order) {
01743                     tr_error("sn_coap_handle_blockwise_message - (recv block1) COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE!");
01744                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE;
01745                 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
01746                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
01747                 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) {
01748                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTINUE;
01749                 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) {
01750                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTINUE;
01751                 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) {
01752                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_DELETED;
01753                 }
01754 
01755                 // Response with COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE if the payload size is more than we can handle
01756                 if (received_coap_msg_ptr->options_list_ptr->size1 > SN_COAP_MAX_INCOMING_BLOCK_MESSAGE_SIZE) {
01757                     // Include maximum size that stack can handle into response
01758                     tr_error("sn_coap_handle_blockwise_message - (recv block1) COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE!");
01759                     src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE;
01760                 }
01761                 else {
01762                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 = received_coap_msg_ptr->options_list_ptr->block1;
01763                     src_coap_blockwise_ack_msg_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
01764 
01765                     /* Check block size */
01766                     block_temp = (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 & 0x07);
01767                     uint16_t block_size = 1u << (block_temp + 4);
01768                     if (block_size >  handle->sn_coap_block_data_size) {
01769                          // Include maximum size that stack can handle into response
01770                          tr_error("sn_coap_handle_blockwise_message - (recv block1) COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE!");
01771                          src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE;
01772                          src_coap_blockwise_ack_msg_ptr->options_list_ptr->size1 = handle->sn_coap_block_data_size;
01773                          sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle);
01774                     }
01775 
01776                     if (block_temp > sn_coap_convert_block_size(handle->sn_coap_block_data_size)) {
01777                         src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 &= 0xFFFFF8;
01778                         src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 |= sn_coap_convert_block_size(handle->sn_coap_block_data_size);
01779                     }
01780                 }
01781 
01782                 src_coap_blockwise_ack_msg_ptr->msg_id = received_coap_msg_ptr->msg_id;
01783 
01784                 // Copy token to response
01785                 src_coap_blockwise_ack_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(received_coap_msg_ptr->token_len);
01786                 if (src_coap_blockwise_ack_msg_ptr->token_ptr) {
01787                     memcpy(src_coap_blockwise_ack_msg_ptr->token_ptr, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len);
01788                     src_coap_blockwise_ack_msg_ptr->token_len = received_coap_msg_ptr->token_len;
01789                 }
01790 
01791                 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);
01792 
01793                 dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
01794                 if (!dst_ack_packet_data_ptr) {
01795                     tr_error("sn_coap_handle_blockwise_message - (recv block1) message allocation failed!");
01796                     sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01797                     handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
01798                     src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
01799                     handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01800                     src_coap_blockwise_ack_msg_ptr = 0;
01801                     return NULL;
01802                 }
01803 
01804                 sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
01805                 handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
01806 
01807                 sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr);
01808                 handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
01809                 dst_ack_packet_data_ptr = 0;
01810 
01811                 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING;
01812 
01813             } else {
01814                 /* * * This is the last block when whole Blockwise payload from received * * */
01815                 /* * * blockwise messages is gathered and returned to User               * * */
01816 
01817                 /* Store last Blockwise payload to Linked list */
01818                 uint16_t payload_len            = 0;
01819                 uint8_t *payload_ptr            = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
01820                 uint32_t whole_payload_len      = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr);
01821                 uint8_t *temp_whole_payload_ptr = NULL;
01822 
01823                 temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len);
01824                 if (temp_whole_payload_ptr == NULL || whole_payload_len > UINT16_MAX) {
01825                     tr_error("sn_coap_handle_blockwise_message - (recv block1) failed to allocate all blocks!");
01826                     sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01827                     handle->sn_coap_protocol_free(temp_whole_payload_ptr);
01828                     return 0;
01829                 }
01830 
01831                 // In block message case, payload_ptr freeing must be done in application level
01832                 received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr;
01833                 received_coap_msg_ptr->payload_len = whole_payload_len;
01834 
01835                 /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */
01836                 while (payload_ptr != NULL) {
01837                     memcpy(temp_whole_payload_ptr, payload_ptr, payload_len);
01838                     temp_whole_payload_ptr += payload_len;
01839                     sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle);
01840                     payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
01841                 }
01842                 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED;
01843             }
01844         }
01845     }
01846 
01847 
01848     /* Block2 Option in a response (e.g., a 2.05 response for GET) */
01849     /* Message ID must be same than in received message */
01850     else {
01851         //This is response to request we made
01852         if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) {
01853             if (handle->sn_coap_internal_block2_resp_handling) {
01854                 uint32_t block_number = 0;
01855 
01856                 /* Store blockwise payload to Linked list */
01857                 //todo: add block number to stored values - just to make sure all packets are in order
01858                 sn_coap_protocol_linked_list_blockwise_payload_store(handle,
01859                                                                      src_addr_ptr,
01860                                                                      received_coap_msg_ptr->payload_len,
01861                                                                      received_coap_msg_ptr->payload_ptr,
01862                                                                      received_coap_msg_ptr->options_list_ptr->block2 >> 4);
01863                 /* If not last block (more value is set) */
01864                 if (received_coap_msg_ptr->options_list_ptr->block2 & 0x08) {
01865                     coap_blockwise_msg_s *previous_blockwise_msg_ptr = NULL;
01866                     //build and send ack
01867                     received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING;
01868 
01869                     ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) {
01870                         if (received_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) {
01871                             previous_blockwise_msg_ptr = msg;
01872                             break;
01873                         }
01874                     }
01875 
01876                     if (!previous_blockwise_msg_ptr || !previous_blockwise_msg_ptr->coap_msg_ptr) {
01877                         tr_error("sn_coap_handle_blockwise_message - (send block2) previous message null!");
01878                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01879                         return 0;
01880                     }
01881 
01882                     src_coap_blockwise_ack_msg_ptr = sn_coap_parser_alloc_message(handle);
01883                     if (src_coap_blockwise_ack_msg_ptr == NULL) {
01884                         tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate message!");
01885                         return 0;
01886                     }
01887 
01888                     /* * * Then build CoAP Acknowledgement message * * */
01889 
01890                     if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) {
01891                         tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate options!");
01892                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01893                         src_coap_blockwise_ack_msg_ptr = 0;
01894                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01895                         return NULL;
01896                     }
01897 
01898                     src_coap_blockwise_ack_msg_ptr->msg_id = message_id++;
01899                     if (message_id == 0) {
01900                         message_id = 1;
01901                     }
01902 
01903                     /* Update block option */
01904                     block_temp = received_coap_msg_ptr->options_list_ptr->block2 & 0x07;
01905 
01906                     block_number = received_coap_msg_ptr->options_list_ptr->block2 >> 4;
01907                     block_number ++;
01908 
01909                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = (block_number << 4) | block_temp;
01910 
01911 
01912                     /* Set BLOCK2 (subsequent) GET msg code and copy uri path from previous msg*/
01913                     if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_RESPONSE_CONTENT) {
01914                         src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_REQUEST_GET;
01915                         if (previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_ptr) {
01916                             src_coap_blockwise_ack_msg_ptr->uri_path_len = previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_len;
01917                             src_coap_blockwise_ack_msg_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(previous_blockwise_msg_ptr->coap_msg_ptr->uri_path_len);
01918                             if (!src_coap_blockwise_ack_msg_ptr->uri_path_ptr) {
01919                                 sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr);
01920                                 tr_error("sn_coap_handle_blockwise_message - failed to allocate for uri path ptr!");
01921                                 return NULL;
01922                             }
01923                             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);
01924                         }
01925                         if (previous_blockwise_msg_ptr->coap_msg_ptr->token_ptr) {
01926                             src_coap_blockwise_ack_msg_ptr->token_len = previous_blockwise_msg_ptr->coap_msg_ptr->token_len;
01927                             src_coap_blockwise_ack_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(previous_blockwise_msg_ptr->coap_msg_ptr->token_len);
01928                             if (!src_coap_blockwise_ack_msg_ptr->token_ptr) {
01929                                 sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr);
01930                                 tr_error("sn_coap_handle_blockwise_message - failed to allocate for token ptr!");
01931                                 return NULL;
01932                             }
01933                             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);
01934                         }
01935                     }
01936 
01937                     ns_list_remove(&handle->linked_list_blockwise_sent_msgs, previous_blockwise_msg_ptr);
01938                     if (previous_blockwise_msg_ptr->coap_msg_ptr) {
01939                         if (previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr) {
01940                             handle->sn_coap_protocol_free(previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr);
01941                             previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr = 0;
01942                         }
01943                         sn_coap_parser_release_allocated_coap_msg_mem(handle, previous_blockwise_msg_ptr->coap_msg_ptr);
01944                         previous_blockwise_msg_ptr->coap_msg_ptr = 0;
01945                     }
01946                     handle->sn_coap_protocol_free(previous_blockwise_msg_ptr);
01947                     previous_blockwise_msg_ptr = 0;
01948 
01949                     /* Then get needed memory count for Packet data */
01950                     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);
01951 
01952                     /* Then allocate memory for Packet data */
01953                     dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
01954 
01955                     if (dst_ack_packet_data_ptr == NULL) {
01956                         tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate packet!");
01957                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
01958                         src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
01959                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01960                         src_coap_blockwise_ack_msg_ptr = 0;
01961                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01962                         return NULL;
01963                     }
01964                     memset(dst_ack_packet_data_ptr, 0, dst_packed_data_needed_mem);
01965 
01966                     /* * * Then build Acknowledgement message to Packed data * * */
01967                     if ((sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size)) < 0) {
01968                         tr_error("sn_coap_handle_blockwise_message - (send block2) builder failed!");
01969                         handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
01970                         dst_ack_packet_data_ptr = 0;
01971                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
01972                         src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
01973                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01974                         src_coap_blockwise_ack_msg_ptr = 0;
01975                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01976                         return NULL;
01977                     }
01978 
01979                     /* * * Save to linked list * * */
01980                     coap_blockwise_msg_s *stored_blockwise_msg_ptr;
01981 
01982                     stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
01983                     if (!stored_blockwise_msg_ptr) {
01984                         tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate blockwise message!");
01985                         handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
01986                         dst_ack_packet_data_ptr = 0;
01987                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
01988                         src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
01989                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
01990                         src_coap_blockwise_ack_msg_ptr = 0;
01991                         sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
01992                         return 0;
01993                     }
01994                     memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
01995 
01996                     stored_blockwise_msg_ptr->timestamp = handle->system_time;
01997 
01998                     stored_blockwise_msg_ptr->coap_msg_ptr = src_coap_blockwise_ack_msg_ptr;
01999                     stored_blockwise_msg_ptr->coap = handle;
02000                     ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr);
02001 
02002                     /* * * Then release memory of CoAP Acknowledgement message * * */
02003                     handle->sn_coap_tx_callback(dst_ack_packet_data_ptr,
02004                                                 dst_packed_data_needed_mem, src_addr_ptr, param);
02005 
02006     #if ENABLE_RESENDINGS
02007                     uint32_t resend_time = sn_coap_calculate_new_resend_time(handle->system_time, handle->sn_coap_resending_intervall, 0);
02008                     sn_coap_protocol_linked_list_send_msg_store(handle, src_addr_ptr,
02009                             dst_packed_data_needed_mem,
02010                             dst_ack_packet_data_ptr,
02011                             resend_time, param);
02012     #endif
02013                     handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
02014                     dst_ack_packet_data_ptr = 0;
02015                 }
02016 
02017                 //Last block received
02018                 else {
02019                     /* * * This is the last block when whole Blockwise payload from received * * */
02020                     /* * * blockwise messages is gathered and returned to User               * * */
02021 
02022                     /* Store last Blockwise payload to Linked list */
02023                     uint16_t payload_len            = 0;
02024                     uint8_t *payload_ptr            = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
02025                     uint16_t whole_payload_len      = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr);
02026                     uint8_t *temp_whole_payload_ptr = NULL;
02027 
02028                     temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len);
02029                     if (!temp_whole_payload_ptr) {
02030                         tr_error("sn_coap_handle_blockwise_message - (send block2) failed to allocate whole payload!");
02031                         return 0;
02032                     }
02033 
02034                     received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr;
02035                     received_coap_msg_ptr->payload_len = whole_payload_len;
02036 
02037                     /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */
02038                     while (payload_ptr != NULL) {
02039                         memcpy(temp_whole_payload_ptr, payload_ptr, payload_len);
02040 
02041                         temp_whole_payload_ptr += payload_len;
02042 
02043                         sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle);
02044                         payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
02045                     }
02046                     received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED;
02047 
02048                     //todo: remove previous msg from list
02049                 }
02050             }
02051         }
02052 
02053         //Now we send data to request
02054         else {
02055             //Get message by using block number
02056             //NOTE: Getting the first from list might not be correct one
02057             coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = ns_list_get_first(&handle->linked_list_blockwise_sent_msgs);
02058             if (stored_blockwise_msg_temp_ptr) {
02059                 uint16_t block_size;
02060                 uint32_t block_number;
02061 
02062                 /* Resolve block parameters */
02063                 block_number = received_coap_msg_ptr->options_list_ptr->block2 >> 4;
02064                 block_temp = received_coap_msg_ptr->options_list_ptr->block2 & 0x07;
02065                 block_size = 1u << (block_temp + 4);
02066 
02067                 /* Build response message */
02068                 src_coap_blockwise_ack_msg_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr;
02069 
02070                 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr) {
02071                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1 = COAP_OPTION_BLOCK_NONE;
02072                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = COAP_OPTION_BLOCK_NONE;
02073                 } else {
02074                     if (sn_coap_parser_alloc_options(handle, src_coap_blockwise_ack_msg_ptr) == NULL) {
02075                         tr_error("sn_coap_handle_blockwise_message - (recv block2) failed to allocate options!");
02076                         return 0;
02077                     }
02078                 }
02079 
02080                 src_coap_blockwise_ack_msg_ptr->msg_id = received_coap_msg_ptr->msg_id;
02081 
02082                 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 = received_coap_msg_ptr->options_list_ptr->block2;
02083 
02084                 /* * Payload part * */
02085 
02086                 /* Check if last block */
02087 
02088                 original_payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len;
02089                 original_payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr;
02090 
02091                 if ((block_size * (block_number + 1)) >= stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len) {
02092                     src_coap_blockwise_ack_msg_ptr->payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len - (block_size * block_number);
02093                     src_coap_blockwise_ack_msg_ptr->payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr + (block_size * block_number);
02094                 }
02095                 /* Not last block */
02096                 else {
02097                     /* set more - bit */
02098                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2 |= 0x08;
02099                     src_coap_blockwise_ack_msg_ptr->payload_len = block_size;
02100                     src_coap_blockwise_ack_msg_ptr->payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr + (block_size * block_number);
02101                 }
02102 
02103                 /* Update token to match one which is in GET request.
02104                  * This is needed only in case of notification message.
02105                 */
02106                 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr &&
02107                     src_coap_blockwise_ack_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) {
02108                     if (received_coap_msg_ptr->token_len && src_coap_blockwise_ack_msg_ptr->token_ptr) {
02109                         handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->token_ptr);
02110                         src_coap_blockwise_ack_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(received_coap_msg_ptr->token_len);
02111                         if (src_coap_blockwise_ack_msg_ptr->token_ptr) {
02112                             memcpy(src_coap_blockwise_ack_msg_ptr->token_ptr, received_coap_msg_ptr->token_ptr, received_coap_msg_ptr->token_len);
02113                             src_coap_blockwise_ack_msg_ptr->token_len = received_coap_msg_ptr->token_len;
02114                         }
02115                     }
02116                 }
02117 
02118                 /* Build and send block message */
02119                 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);
02120 
02121                 dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
02122                 if (!dst_ack_packet_data_ptr) {
02123                     tr_error("sn_coap_handle_blockwise_message - (recv block2) failed to allocate packet!");
02124                     if(original_payload_ptr){
02125                         handle->sn_coap_protocol_free(original_payload_ptr);
02126                         original_payload_ptr = NULL;
02127                     }
02128                     handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
02129                     src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
02130                     handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
02131                     stored_blockwise_msg_temp_ptr->coap_msg_ptr = NULL;
02132                     return NULL;
02133                 }
02134 
02135                 sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
02136                 handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
02137 
02138                 handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
02139                 dst_ack_packet_data_ptr = 0;
02140 
02141                 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len = original_payload_len;
02142                 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = original_payload_ptr;
02143 
02144                 if ((block_size * (block_number + 1)) >= stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len) {
02145                     sn_coap_protocol_linked_list_blockwise_msg_remove(handle, stored_blockwise_msg_temp_ptr);
02146                 }
02147 
02148                 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_ACK;
02149             }
02150         }
02151     }
02152     return received_coap_msg_ptr;
02153 }
02154 
02155 int8_t sn_coap_convert_block_size(uint16_t block_size)
02156 {
02157     if (block_size == 16) {
02158         return 0;
02159     } else if (block_size == 32) {
02160         return 1;
02161     } else if (block_size == 64) {
02162         return 2;
02163     } else if (block_size == 128) {
02164         return 3;
02165     } else if (block_size == 256) {
02166         return 4;
02167     } else if (block_size == 512) {
02168         return 5;
02169     } else if (block_size == 1024) {
02170         return 6;
02171     }
02172 
02173     return 0;
02174 }
02175 
02176 static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coap_hdr_s *source_header_ptr)
02177 {
02178     sn_coap_hdr_s *destination_header_ptr;
02179 
02180     destination_header_ptr = sn_coap_parser_alloc_message(handle);
02181     if (!destination_header_ptr) {
02182         tr_error("sn_coap_protocol_copy_header - failed to allocate message!");
02183         return 0;
02184     }
02185 
02186     destination_header_ptr->coap_status = source_header_ptr->coap_status;
02187     destination_header_ptr->msg_type = source_header_ptr->msg_type;
02188     destination_header_ptr->msg_code = source_header_ptr->msg_code;
02189     destination_header_ptr->msg_id = source_header_ptr->msg_id;
02190 
02191     if (source_header_ptr->uri_path_ptr) {
02192         destination_header_ptr->uri_path_len = source_header_ptr->uri_path_len;
02193         destination_header_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->uri_path_len);
02194         if (!destination_header_ptr->uri_path_ptr) {
02195             tr_error("sn_coap_protocol_copy_header - failed to allocate uri path!");
02196             sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02197             return 0;
02198         }
02199         memcpy(destination_header_ptr->uri_path_ptr, source_header_ptr->uri_path_ptr, source_header_ptr->uri_path_len);
02200     }
02201 
02202     if (source_header_ptr->token_ptr) {
02203         destination_header_ptr->token_len = source_header_ptr->token_len;
02204         destination_header_ptr->token_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->token_len);
02205         if (!destination_header_ptr->token_ptr) {
02206             sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02207             tr_error("sn_coap_protocol_copy_header - failed to allocate token!");
02208             return 0;
02209         }
02210         memcpy(destination_header_ptr->token_ptr, source_header_ptr->token_ptr, source_header_ptr->token_len);
02211     }
02212 
02213     destination_header_ptr->content_format = source_header_ptr->content_format;
02214 
02215     /* Options list */
02216     if (source_header_ptr->options_list_ptr) {
02217         if (sn_coap_parser_alloc_options(handle, destination_header_ptr) == NULL) {
02218             sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02219             tr_error("sn_coap_protocol_copy_header - failed to allocate options!");
02220             return 0;
02221         }
02222 
02223         destination_header_ptr->options_list_ptr->max_age = source_header_ptr->options_list_ptr->max_age;
02224 
02225         if (source_header_ptr->options_list_ptr->proxy_uri_ptr) {
02226             destination_header_ptr->options_list_ptr->proxy_uri_len = source_header_ptr->options_list_ptr->proxy_uri_len;
02227             destination_header_ptr->options_list_ptr->proxy_uri_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->proxy_uri_len);
02228             if (!destination_header_ptr->options_list_ptr->proxy_uri_ptr) {
02229                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02230                 tr_error("sn_coap_protocol_copy_header - failed to allocate proxy uri!");
02231                 return 0;
02232             }
02233             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);
02234         }
02235 
02236         if (source_header_ptr->options_list_ptr->etag_ptr) {
02237             destination_header_ptr->options_list_ptr->etag_len = source_header_ptr->options_list_ptr->etag_len;
02238             destination_header_ptr->options_list_ptr->etag_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->etag_len);
02239             if (!destination_header_ptr->options_list_ptr->etag_ptr) {
02240                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02241                 tr_error("sn_coap_protocol_copy_header - failed to allocate etag!");
02242                 return 0;
02243             }
02244             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);
02245         }
02246 
02247         if (source_header_ptr->options_list_ptr->uri_host_ptr) {
02248             destination_header_ptr->options_list_ptr->uri_host_len = source_header_ptr->options_list_ptr->uri_host_len;
02249             destination_header_ptr->options_list_ptr->uri_host_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_host_len);
02250             if (!destination_header_ptr->options_list_ptr->uri_host_ptr) {
02251                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02252                 tr_error("sn_coap_protocol_copy_header - failed to allocate uri host!");
02253                 return 0;
02254             }
02255             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);
02256         }
02257 
02258         if (source_header_ptr->options_list_ptr->location_path_ptr) {
02259             destination_header_ptr->options_list_ptr->location_path_len = source_header_ptr->options_list_ptr->location_path_len;
02260             destination_header_ptr->options_list_ptr->location_path_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->location_path_len);
02261             if (!destination_header_ptr->options_list_ptr->location_path_ptr) {
02262                 tr_error("sn_coap_protocol_copy_header - failed to allocate location path!");
02263                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02264                 return 0;
02265             }
02266             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);
02267         }
02268 
02269         destination_header_ptr->options_list_ptr->uri_port = source_header_ptr->options_list_ptr->uri_port;
02270 
02271         if (source_header_ptr->options_list_ptr->location_query_ptr) {
02272             destination_header_ptr->options_list_ptr->location_query_len = source_header_ptr->options_list_ptr->location_query_len;
02273             destination_header_ptr->options_list_ptr->location_query_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->location_query_len);
02274             if (!destination_header_ptr->options_list_ptr->location_query_ptr) {
02275                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02276                 tr_error("sn_coap_protocol_copy_header - failed to allocate location query!");
02277                 return 0;
02278             }
02279             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);
02280         }
02281 
02282         destination_header_ptr->options_list_ptr->observe = source_header_ptr->options_list_ptr->observe;
02283         destination_header_ptr->options_list_ptr->accept = source_header_ptr->options_list_ptr->accept;
02284 
02285         if (source_header_ptr->options_list_ptr->uri_query_ptr) {
02286             destination_header_ptr->options_list_ptr->uri_query_len = source_header_ptr->options_list_ptr->uri_query_len;
02287             destination_header_ptr->options_list_ptr->uri_query_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_query_len);
02288             if (!destination_header_ptr->options_list_ptr->uri_query_ptr) {
02289                 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
02290                 tr_error("sn_coap_protocol_copy_header - failed to allocate uri query!");
02291                 return 0;
02292             }
02293             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);
02294         }
02295 
02296         destination_header_ptr->options_list_ptr->block1 = source_header_ptr->options_list_ptr->block1;
02297         destination_header_ptr->options_list_ptr->block2 = source_header_ptr->options_list_ptr->block2;
02298     }
02299 
02300     return destination_header_ptr;
02301 }
02302 #endif