Timothy Beight / Mbed 2 deprecated 6_songs-from-the-cloud

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

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