Daiki Kato / mbed-os-lychee

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

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