sandbox / mbed-client-c

Fork of mbed-client-c by Christopher Haster

Committer:
Yogesh Pande
Date:
Sat Apr 02 00:39:03 2016 +0300
Revision:
4:5d91b0f5038c
Parent:
1:43f5c94c6771
Latest mbed-client-c sources from Github.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 1:43f5c94c6771 1 /*
Christopher Haster 1:43f5c94c6771 2 * Copyright (c) 2011-2015 ARM Limited. All rights reserved.
Christopher Haster 1:43f5c94c6771 3 * SPDX-License-Identifier: Apache-2.0
Christopher Haster 1:43f5c94c6771 4 * Licensed under the Apache License, Version 2.0 (the License); you may
Christopher Haster 1:43f5c94c6771 5 * not use this file except in compliance with the License.
Christopher Haster 1:43f5c94c6771 6 * You may obtain a copy of the License at
Christopher Haster 1:43f5c94c6771 7 *
Christopher Haster 1:43f5c94c6771 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 1:43f5c94c6771 9 *
Christopher Haster 1:43f5c94c6771 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 1:43f5c94c6771 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
Christopher Haster 1:43f5c94c6771 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 1:43f5c94c6771 13 * See the License for the specific language governing permissions and
Christopher Haster 1:43f5c94c6771 14 * limitations under the License.
Christopher Haster 1:43f5c94c6771 15 */
Christopher Haster 1:43f5c94c6771 16
Christopher Haster 1:43f5c94c6771 17 /**
Christopher Haster 1:43f5c94c6771 18 * \file sn_coap_protocol.c
Christopher Haster 1:43f5c94c6771 19 *
Christopher Haster 1:43f5c94c6771 20 * \brief CoAP Protocol implementation
Christopher Haster 1:43f5c94c6771 21 *
Christopher Haster 1:43f5c94c6771 22 * Functionality: CoAP Protocol
Christopher Haster 1:43f5c94c6771 23 *
Christopher Haster 1:43f5c94c6771 24 */
Christopher Haster 1:43f5c94c6771 25
Christopher Haster 1:43f5c94c6771 26
Christopher Haster 1:43f5c94c6771 27 /* * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 28 /* * * * INCLUDE FILES * * * */
Christopher Haster 1:43f5c94c6771 29 /* * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 30
Christopher Haster 1:43f5c94c6771 31 #include <stdio.h>
Christopher Haster 1:43f5c94c6771 32 #include <stdlib.h> /* For libary malloc() */
Christopher Haster 1:43f5c94c6771 33 #include <string.h> /* For memset() and memcpy() */
Yogesh Pande 4:5d91b0f5038c 34 #if defined __linux__ || defined TARGET_LIKE_MBED
Christopher Haster 1:43f5c94c6771 35 #include <time.h>
Christopher Haster 1:43f5c94c6771 36 #endif
Christopher Haster 1:43f5c94c6771 37
Christopher Haster 1:43f5c94c6771 38 #include "ns_types.h"
Christopher Haster 1:43f5c94c6771 39 #include "sn_nsdl.h"
Christopher Haster 1:43f5c94c6771 40 #include "sn_coap_protocol.h"
Christopher Haster 1:43f5c94c6771 41 #include "sn_coap_header_internal.h"
Christopher Haster 1:43f5c94c6771 42 #include "sn_coap_protocol_internal.h"
Christopher Haster 1:43f5c94c6771 43
Christopher Haster 1:43f5c94c6771 44 /* * * * * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 45 /* * * * LOCAL FUNCTION PROTOTYPES * * * */
Christopher Haster 1:43f5c94c6771 46 /* * * * * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 47
Christopher Haster 1:43f5c94c6771 48 static void sn_coap_protocol_send_rst(struct coap_s *handle, uint16_t msg_id, sn_nsdl_addr_s *addr_ptr, void *param);
Yogesh Pande 4:5d91b0f5038c 49 #if YOTTA_CFG_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 50 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);
Yogesh Pande 4:5d91b0f5038c 51 static int8_t sn_coap_protocol_linked_list_duplication_info_search(struct coap_s *handle, sn_nsdl_addr_s *scr_addr_ptr, uint16_t msg_id);
Christopher Haster 1:43f5c94c6771 52 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);
Christopher Haster 1:43f5c94c6771 53 static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(struct coap_s *handle);
Christopher Haster 1:43f5c94c6771 54 #endif
Yogesh Pande 4:5d91b0f5038c 55 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 56 static void sn_coap_protocol_linked_list_blockwise_msg_remove(struct coap_s *handle, coap_blockwise_msg_s *removed_msg_ptr);
Christopher Haster 1:43f5c94c6771 57 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);
Yogesh Pande 4:5d91b0f5038c 58 static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length);
Christopher Haster 1:43f5c94c6771 59 static void sn_coap_protocol_linked_list_blockwise_payload_remove(struct coap_s *handle, coap_blockwise_payload_s *removed_payload_ptr);
Christopher Haster 1:43f5c94c6771 60 static void sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(struct coap_s *handle);
Yogesh Pande 4:5d91b0f5038c 61 static uint32_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr);
Christopher Haster 1:43f5c94c6771 62 static void sn_coap_protocol_linked_list_blockwise_remove_old_data(struct coap_s *handle);
Christopher Haster 1:43f5c94c6771 63 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);
Christopher Haster 1:43f5c94c6771 64 static int8_t sn_coap_convert_block_size(uint16_t block_size);
Christopher Haster 1:43f5c94c6771 65 static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coap_hdr_s *source_header_ptr);
Christopher Haster 1:43f5c94c6771 66 #endif
Christopher Haster 1:43f5c94c6771 67 #if ENABLE_RESENDINGS
Yogesh Pande 4:5d91b0f5038c 68 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, uint8_t *uri_path_ptr, uint8_t uri_path_len);
Yogesh Pande 4:5d91b0f5038c 69 static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(struct coap_s *handle,sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id);
Christopher Haster 1:43f5c94c6771 70 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);
Christopher Haster 1:43f5c94c6771 71 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);
Christopher Haster 1:43f5c94c6771 72 static void sn_coap_protocol_release_allocated_send_msg_mem(struct coap_s *handle, coap_send_msg_s *freed_send_msg_ptr);
Christopher Haster 1:43f5c94c6771 73 static uint16_t sn_coap_count_linked_list_size(const coap_send_msg_list_t *linked_list_ptr);
Christopher Haster 1:43f5c94c6771 74 #endif
Christopher Haster 1:43f5c94c6771 75
Christopher Haster 1:43f5c94c6771 76 /* * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 77 /* * * * GLOBAL DECLARATIONS * * * */
Christopher Haster 1:43f5c94c6771 78 /* * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 79 static uint16_t message_id;
Christopher Haster 1:43f5c94c6771 80
Christopher Haster 1:43f5c94c6771 81 int8_t sn_coap_protocol_destroy(struct coap_s *handle)
Christopher Haster 1:43f5c94c6771 82 {
Christopher Haster 1:43f5c94c6771 83 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 84 return -1;
Christopher Haster 1:43f5c94c6771 85 }
Christopher Haster 1:43f5c94c6771 86 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 87
Christopher Haster 1:43f5c94c6771 88 sn_coap_protocol_clear_retransmission_buffer(handle);
Christopher Haster 1:43f5c94c6771 89
Christopher Haster 1:43f5c94c6771 90 #endif
Christopher Haster 1:43f5c94c6771 91
Yogesh Pande 4:5d91b0f5038c 92 #if YOTTA_CFG_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
Yogesh Pande 4:5d91b0f5038c 93 ns_list_foreach_safe(coap_duplication_info_s, tmp, &handle->linked_list_duplication_msgs) {
Christopher Haster 1:43f5c94c6771 94 if (tmp->coap == handle) {
Christopher Haster 1:43f5c94c6771 95 if (tmp->addr_ptr) {
Christopher Haster 1:43f5c94c6771 96 handle->sn_coap_protocol_free(tmp->addr_ptr);
Christopher Haster 1:43f5c94c6771 97 tmp->addr_ptr = 0;
Christopher Haster 1:43f5c94c6771 98 }
Yogesh Pande 4:5d91b0f5038c 99 ns_list_remove(&handle->linked_list_duplication_msgs, tmp);
Yogesh Pande 4:5d91b0f5038c 100 handle->count_duplication_msgs--;
Christopher Haster 1:43f5c94c6771 101 handle->sn_coap_protocol_free(tmp);
Christopher Haster 1:43f5c94c6771 102 tmp = 0;
Christopher Haster 1:43f5c94c6771 103 }
Christopher Haster 1:43f5c94c6771 104 }
Christopher Haster 1:43f5c94c6771 105 #endif
Christopher Haster 1:43f5c94c6771 106
Yogesh Pande 4:5d91b0f5038c 107 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwise is not used at all, this part of code will not be compiled */
Yogesh Pande 4:5d91b0f5038c 108 ns_list_foreach_safe(coap_blockwise_msg_s, tmp, &handle->linked_list_blockwise_sent_msgs) {
Christopher Haster 1:43f5c94c6771 109 if (tmp->coap == handle) {
Christopher Haster 1:43f5c94c6771 110 if (tmp->coap_msg_ptr) {
Christopher Haster 1:43f5c94c6771 111 if (tmp->coap_msg_ptr->payload_ptr) {
Christopher Haster 1:43f5c94c6771 112 handle->sn_coap_protocol_free(tmp->coap_msg_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 113 tmp->coap_msg_ptr->payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 114 }
Christopher Haster 1:43f5c94c6771 115 sn_coap_parser_release_allocated_coap_msg_mem(tmp->coap, tmp->coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 116 }
Yogesh Pande 4:5d91b0f5038c 117 ns_list_remove(&handle->linked_list_blockwise_sent_msgs, tmp);
Christopher Haster 1:43f5c94c6771 118 handle->sn_coap_protocol_free(tmp);
Christopher Haster 1:43f5c94c6771 119 tmp = 0;
Christopher Haster 1:43f5c94c6771 120 }
Christopher Haster 1:43f5c94c6771 121 }
Yogesh Pande 4:5d91b0f5038c 122 ns_list_foreach_safe(coap_blockwise_payload_s, tmp, &handle->linked_list_blockwise_received_payloads) {
Christopher Haster 1:43f5c94c6771 123 if (tmp->coap == handle) {
Christopher Haster 1:43f5c94c6771 124 if (tmp->addr_ptr) {
Christopher Haster 1:43f5c94c6771 125 handle->sn_coap_protocol_free(tmp->addr_ptr);
Christopher Haster 1:43f5c94c6771 126 tmp->addr_ptr = 0;
Christopher Haster 1:43f5c94c6771 127 }
Christopher Haster 1:43f5c94c6771 128 if (tmp->payload_ptr) {
Christopher Haster 1:43f5c94c6771 129 handle->sn_coap_protocol_free(tmp->payload_ptr);
Christopher Haster 1:43f5c94c6771 130 tmp->payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 131 }
Yogesh Pande 4:5d91b0f5038c 132 ns_list_remove(&handle->linked_list_blockwise_received_payloads, tmp);
Christopher Haster 1:43f5c94c6771 133 handle->sn_coap_protocol_free(tmp);
Christopher Haster 1:43f5c94c6771 134 tmp = 0;
Christopher Haster 1:43f5c94c6771 135 }
Christopher Haster 1:43f5c94c6771 136 }
Christopher Haster 1:43f5c94c6771 137 #endif
Christopher Haster 1:43f5c94c6771 138
Christopher Haster 1:43f5c94c6771 139 handle->sn_coap_protocol_free(handle);
Christopher Haster 1:43f5c94c6771 140 handle = 0;
Christopher Haster 1:43f5c94c6771 141 return 0;
Christopher Haster 1:43f5c94c6771 142 }
Christopher Haster 1:43f5c94c6771 143
Christopher Haster 1:43f5c94c6771 144 struct coap_s *sn_coap_protocol_init(void *(*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void *),
Christopher Haster 1:43f5c94c6771 145 uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *),
Christopher Haster 1:43f5c94c6771 146 int8_t (*used_rx_callback_ptr)(sn_coap_hdr_s *, sn_nsdl_addr_s *, void *param))
Christopher Haster 1:43f5c94c6771 147 {
Christopher Haster 1:43f5c94c6771 148 /* Check paramters */
Christopher Haster 1:43f5c94c6771 149 if ((used_malloc_func_ptr == NULL) || (used_free_func_ptr == NULL) || (used_tx_callback_ptr == NULL)) {
Christopher Haster 1:43f5c94c6771 150 return NULL;
Christopher Haster 1:43f5c94c6771 151 }
Christopher Haster 1:43f5c94c6771 152
Christopher Haster 1:43f5c94c6771 153 struct coap_s *handle;
Christopher Haster 1:43f5c94c6771 154 handle = used_malloc_func_ptr(sizeof(struct coap_s));
Christopher Haster 1:43f5c94c6771 155 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 156 return NULL;
Christopher Haster 1:43f5c94c6771 157 }
Christopher Haster 1:43f5c94c6771 158
Christopher Haster 1:43f5c94c6771 159 memset(handle, 0, sizeof(struct coap_s));
Christopher Haster 1:43f5c94c6771 160
Christopher Haster 1:43f5c94c6771 161 /* * * Handle tx callback * * */
Christopher Haster 1:43f5c94c6771 162 handle->sn_coap_tx_callback = used_tx_callback_ptr;
Christopher Haster 1:43f5c94c6771 163
Christopher Haster 1:43f5c94c6771 164 handle->sn_coap_protocol_free = used_free_func_ptr;
Christopher Haster 1:43f5c94c6771 165 handle->sn_coap_protocol_malloc = used_malloc_func_ptr;
Christopher Haster 1:43f5c94c6771 166
Christopher Haster 1:43f5c94c6771 167 /* * * Handle rx callback * * */
Christopher Haster 1:43f5c94c6771 168 /* If pointer = 0, then re-sending does not return error when failed */
Christopher Haster 1:43f5c94c6771 169 handle->sn_coap_rx_callback = used_rx_callback_ptr;
Christopher Haster 1:43f5c94c6771 170
Yogesh Pande 4:5d91b0f5038c 171
Yogesh Pande 4:5d91b0f5038c 172
Christopher Haster 1:43f5c94c6771 173 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 174
Christopher Haster 1:43f5c94c6771 175 /* * * * Create Linked list for storing active resending messages * * * */
Yogesh Pande 4:5d91b0f5038c 176 ns_list_init(&handle->linked_list_resent_msgs);
Yogesh Pande 4:5d91b0f5038c 177 handle->sn_coap_resending_queue_msgs = SN_COAP_RESENDING_QUEUE_SIZE_MSGS;
Yogesh Pande 4:5d91b0f5038c 178 handle->sn_coap_resending_queue_bytes = SN_COAP_RESENDING_QUEUE_SIZE_BYTES;
Yogesh Pande 4:5d91b0f5038c 179 handle->sn_coap_resending_intervall = DEFAULT_RESPONSE_TIMEOUT;
Yogesh Pande 4:5d91b0f5038c 180 handle->sn_coap_resending_count = SN_COAP_RESENDING_MAX_COUNT;
Yogesh Pande 4:5d91b0f5038c 181
Christopher Haster 1:43f5c94c6771 182
Christopher Haster 1:43f5c94c6771 183 #endif /* ENABLE_RESENDINGS */
Christopher Haster 1:43f5c94c6771 184
Yogesh Pande 4:5d91b0f5038c 185 #if YOTTA_CFG_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 186 /* * * * Create Linked list for storing Duplication info * * * */
Yogesh Pande 4:5d91b0f5038c 187 ns_list_init(&handle->linked_list_duplication_msgs);
Yogesh Pande 4:5d91b0f5038c 188 handle->sn_coap_duplication_buffer_size = YOTTA_CFG_COAP_DUPLICATION_MAX_MSGS_COUNT;
Christopher Haster 1:43f5c94c6771 189 #endif
Christopher Haster 1:43f5c94c6771 190
Yogesh Pande 4:5d91b0f5038c 191 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 192
Yogesh Pande 4:5d91b0f5038c 193 ns_list_init(&handle->linked_list_blockwise_sent_msgs);
Yogesh Pande 4:5d91b0f5038c 194 ns_list_init(&handle->linked_list_blockwise_received_payloads);
Yogesh Pande 4:5d91b0f5038c 195 handle->sn_coap_block_data_size = YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE;
Christopher Haster 1:43f5c94c6771 196
Christopher Haster 1:43f5c94c6771 197 #endif /* ENABLE_RESENDINGS */
Christopher Haster 1:43f5c94c6771 198
Christopher Haster 1:43f5c94c6771 199 /* Randomize global message ID */
Christopher Haster 1:43f5c94c6771 200 #if defined __linux__ || defined TARGET_LIKE_MBED
Christopher Haster 1:43f5c94c6771 201 srand(rand()^time(NULL));
Christopher Haster 1:43f5c94c6771 202 message_id = rand() % 400 + 100;
Christopher Haster 1:43f5c94c6771 203 #else
Christopher Haster 1:43f5c94c6771 204 message_id = 100;
Christopher Haster 1:43f5c94c6771 205 #endif
Christopher Haster 1:43f5c94c6771 206
Christopher Haster 1:43f5c94c6771 207 return handle;
Christopher Haster 1:43f5c94c6771 208 }
Christopher Haster 1:43f5c94c6771 209
Yogesh Pande 4:5d91b0f5038c 210 int8_t sn_coap_protocol_set_block_size(struct coap_s *handle, uint16_t block_size)
Christopher Haster 1:43f5c94c6771 211 {
Yogesh Pande 4:5d91b0f5038c 212 (void) handle;
Yogesh Pande 4:5d91b0f5038c 213 (void) block_size;
Yogesh Pande 4:5d91b0f5038c 214 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
Yogesh Pande 4:5d91b0f5038c 215 if (handle == NULL) {
Yogesh Pande 4:5d91b0f5038c 216 return -1;
Yogesh Pande 4:5d91b0f5038c 217 }
Christopher Haster 1:43f5c94c6771 218 switch (block_size) {
Christopher Haster 1:43f5c94c6771 219 case 0:
Christopher Haster 1:43f5c94c6771 220 case 16:
Christopher Haster 1:43f5c94c6771 221 case 32:
Christopher Haster 1:43f5c94c6771 222 case 64:
Christopher Haster 1:43f5c94c6771 223 case 128:
Christopher Haster 1:43f5c94c6771 224 case 256:
Christopher Haster 1:43f5c94c6771 225 case 512:
Christopher Haster 1:43f5c94c6771 226 case 1024:
Yogesh Pande 4:5d91b0f5038c 227 handle->sn_coap_block_data_size = block_size;
Christopher Haster 1:43f5c94c6771 228 return 0;
Christopher Haster 1:43f5c94c6771 229 default:
Christopher Haster 1:43f5c94c6771 230 break;
Christopher Haster 1:43f5c94c6771 231 }
Christopher Haster 1:43f5c94c6771 232 #endif
Christopher Haster 1:43f5c94c6771 233 return -1;
Christopher Haster 1:43f5c94c6771 234
Christopher Haster 1:43f5c94c6771 235 }
Christopher Haster 1:43f5c94c6771 236
Yogesh Pande 4:5d91b0f5038c 237 int8_t sn_coap_protocol_set_duplicate_buffer_size(struct coap_s *handle, uint8_t message_count)
Christopher Haster 1:43f5c94c6771 238 {
Yogesh Pande 4:5d91b0f5038c 239 (void) handle;
Yogesh Pande 4:5d91b0f5038c 240 (void) message_count;
Yogesh Pande 4:5d91b0f5038c 241 #if YOTTA_CFG_COAP_DUPLICATION_MAX_MSGS_COUNT
Yogesh Pande 4:5d91b0f5038c 242 if (handle == NULL) {
Yogesh Pande 4:5d91b0f5038c 243 return -1;
Yogesh Pande 4:5d91b0f5038c 244 }
Christopher Haster 1:43f5c94c6771 245 if (message_count <= SN_COAP_MAX_ALLOWED_DUPLICATION_MESSAGE_COUNT) {
Yogesh Pande 4:5d91b0f5038c 246 handle->sn_coap_duplication_buffer_size = message_count;
Christopher Haster 1:43f5c94c6771 247 return 0;
Christopher Haster 1:43f5c94c6771 248 }
Christopher Haster 1:43f5c94c6771 249 #endif
Christopher Haster 1:43f5c94c6771 250 return -1;
Christopher Haster 1:43f5c94c6771 251 }
Christopher Haster 1:43f5c94c6771 252
Yogesh Pande 4:5d91b0f5038c 253 int8_t sn_coap_protocol_set_retransmission_parameters(struct coap_s *handle,
Yogesh Pande 4:5d91b0f5038c 254 uint8_t resending_count, uint8_t resending_intervall)
Christopher Haster 1:43f5c94c6771 255 {
Christopher Haster 1:43f5c94c6771 256 #if ENABLE_RESENDINGS
Yogesh Pande 4:5d91b0f5038c 257 if (handle == NULL) {
Yogesh Pande 4:5d91b0f5038c 258 return -1;
Yogesh Pande 4:5d91b0f5038c 259 }
Christopher Haster 1:43f5c94c6771 260 if (resending_count <= SN_COAP_MAX_ALLOWED_RESENDING_COUNT &&
Christopher Haster 1:43f5c94c6771 261 resending_intervall <= SN_COAP_MAX_ALLOWED_RESPONSE_TIMEOUT) {
Yogesh Pande 4:5d91b0f5038c 262 handle->sn_coap_resending_count = resending_count;
Christopher Haster 1:43f5c94c6771 263
Christopher Haster 1:43f5c94c6771 264 if (resending_intervall == 0) {
Yogesh Pande 4:5d91b0f5038c 265 handle->sn_coap_resending_intervall = 1;
Christopher Haster 1:43f5c94c6771 266 } else {
Yogesh Pande 4:5d91b0f5038c 267 handle->sn_coap_resending_intervall = resending_intervall;
Christopher Haster 1:43f5c94c6771 268 }
Christopher Haster 1:43f5c94c6771 269 return 0;
Christopher Haster 1:43f5c94c6771 270 }
Christopher Haster 1:43f5c94c6771 271 #endif
Christopher Haster 1:43f5c94c6771 272 return -1;
Christopher Haster 1:43f5c94c6771 273 }
Christopher Haster 1:43f5c94c6771 274
Yogesh Pande 4:5d91b0f5038c 275 int8_t sn_coap_protocol_set_retransmission_buffer(struct coap_s *handle,
Yogesh Pande 4:5d91b0f5038c 276 uint8_t buffer_size_messages, uint16_t buffer_size_bytes)
Christopher Haster 1:43f5c94c6771 277 {
Christopher Haster 1:43f5c94c6771 278 #if ENABLE_RESENDINGS
Yogesh Pande 4:5d91b0f5038c 279 if (handle == NULL) {
Yogesh Pande 4:5d91b0f5038c 280 return -1;
Yogesh Pande 4:5d91b0f5038c 281 }
Christopher Haster 1:43f5c94c6771 282 if (buffer_size_bytes <= SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_BYTES &&
Christopher Haster 1:43f5c94c6771 283 buffer_size_messages <= SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_MSGS ) {
Yogesh Pande 4:5d91b0f5038c 284 handle->sn_coap_resending_queue_bytes = buffer_size_bytes;
Yogesh Pande 4:5d91b0f5038c 285 handle->sn_coap_resending_queue_msgs = buffer_size_messages;
Christopher Haster 1:43f5c94c6771 286 return 0;
Christopher Haster 1:43f5c94c6771 287 }
Christopher Haster 1:43f5c94c6771 288
Christopher Haster 1:43f5c94c6771 289 #endif
Christopher Haster 1:43f5c94c6771 290 return -1;
Christopher Haster 1:43f5c94c6771 291
Christopher Haster 1:43f5c94c6771 292 }
Christopher Haster 1:43f5c94c6771 293
Christopher Haster 1:43f5c94c6771 294 void sn_coap_protocol_clear_retransmission_buffer(struct coap_s *handle)
Christopher Haster 1:43f5c94c6771 295 {
Christopher Haster 1:43f5c94c6771 296 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
Yogesh Pande 4:5d91b0f5038c 297 if (handle == NULL) {
Yogesh Pande 4:5d91b0f5038c 298 return;
Yogesh Pande 4:5d91b0f5038c 299 }
Yogesh Pande 4:5d91b0f5038c 300 ns_list_foreach_safe(coap_send_msg_s, tmp, &handle->linked_list_resent_msgs) {
Christopher Haster 1:43f5c94c6771 301 if (tmp->send_msg_ptr) {
Christopher Haster 1:43f5c94c6771 302 if (tmp->send_msg_ptr->dst_addr_ptr) {
Christopher Haster 1:43f5c94c6771 303 if (tmp->send_msg_ptr->dst_addr_ptr->addr_ptr) {
Christopher Haster 1:43f5c94c6771 304 handle->sn_coap_protocol_free(tmp->send_msg_ptr->dst_addr_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 305 tmp->send_msg_ptr->dst_addr_ptr->addr_ptr = 0;
Christopher Haster 1:43f5c94c6771 306 }
Christopher Haster 1:43f5c94c6771 307 handle->sn_coap_protocol_free(tmp->send_msg_ptr->dst_addr_ptr);
Christopher Haster 1:43f5c94c6771 308 tmp->send_msg_ptr->dst_addr_ptr = 0;
Christopher Haster 1:43f5c94c6771 309 }
Christopher Haster 1:43f5c94c6771 310 if (tmp->send_msg_ptr->packet_ptr) {
Christopher Haster 1:43f5c94c6771 311 handle->sn_coap_protocol_free(tmp->send_msg_ptr->packet_ptr);
Christopher Haster 1:43f5c94c6771 312 tmp->send_msg_ptr->packet_ptr = 0;
Christopher Haster 1:43f5c94c6771 313 }
Yogesh Pande 4:5d91b0f5038c 314 if (tmp->send_msg_ptr->uri_path_ptr) {
Yogesh Pande 4:5d91b0f5038c 315 handle->sn_coap_protocol_free(tmp->send_msg_ptr->uri_path_ptr);
Yogesh Pande 4:5d91b0f5038c 316 tmp->send_msg_ptr->uri_path_ptr = 0;
Yogesh Pande 4:5d91b0f5038c 317 }
Christopher Haster 1:43f5c94c6771 318 handle->sn_coap_protocol_free(tmp->send_msg_ptr);
Christopher Haster 1:43f5c94c6771 319 tmp->send_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 320 }
Yogesh Pande 4:5d91b0f5038c 321 ns_list_remove(&handle->linked_list_resent_msgs, tmp);
Yogesh Pande 4:5d91b0f5038c 322 --handle->count_resent_msgs;
Christopher Haster 1:43f5c94c6771 323 handle->sn_coap_protocol_free(tmp);
Christopher Haster 1:43f5c94c6771 324 tmp = 0;
Christopher Haster 1:43f5c94c6771 325 }
Christopher Haster 1:43f5c94c6771 326 #endif
Christopher Haster 1:43f5c94c6771 327 }
Christopher Haster 1:43f5c94c6771 328
Christopher Haster 1:43f5c94c6771 329
Christopher Haster 1:43f5c94c6771 330 int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr,
Christopher Haster 1:43f5c94c6771 331 uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, void *param)
Christopher Haster 1:43f5c94c6771 332 {
Christopher Haster 1:43f5c94c6771 333 int16_t byte_count_built = 0;
Yogesh Pande 4:5d91b0f5038c 334 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 335 uint16_t original_payload_len = 0;
Christopher Haster 1:43f5c94c6771 336 #endif
Christopher Haster 1:43f5c94c6771 337 /* * * * Check given pointers * * * */
Christopher Haster 1:43f5c94c6771 338 if ((dst_addr_ptr == NULL) || (dst_packet_data_ptr == NULL) || (src_coap_msg_ptr == NULL) || handle == NULL) {
Christopher Haster 1:43f5c94c6771 339 return -2;
Christopher Haster 1:43f5c94c6771 340 }
Christopher Haster 1:43f5c94c6771 341
Christopher Haster 1:43f5c94c6771 342 if (dst_addr_ptr->addr_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 343 return -2;
Christopher Haster 1:43f5c94c6771 344 }
Christopher Haster 1:43f5c94c6771 345
Christopher Haster 1:43f5c94c6771 346 /* Check if built Message type is else than Acknowledgement or Reset i.e. message type is Confirmable or Non-confirmable */
Christopher Haster 1:43f5c94c6771 347 /* (for Acknowledgement and Reset messages is written same Message ID than was in the Request message) */
Christopher Haster 1:43f5c94c6771 348 if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_ACKNOWLEDGEMENT &&
Christopher Haster 1:43f5c94c6771 349 src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_RESET &&
Christopher Haster 1:43f5c94c6771 350 src_coap_msg_ptr->msg_id == 0) {
Christopher Haster 1:43f5c94c6771 351 /* * * * Generate new Message ID and increase it by one * * * */
Christopher Haster 1:43f5c94c6771 352 src_coap_msg_ptr->msg_id = message_id;
Christopher Haster 1:43f5c94c6771 353 message_id++;
Christopher Haster 1:43f5c94c6771 354 if (message_id == 0) {
Christopher Haster 1:43f5c94c6771 355 message_id = 1;
Christopher Haster 1:43f5c94c6771 356 }
Christopher Haster 1:43f5c94c6771 357 }
Christopher Haster 1:43f5c94c6771 358
Yogesh Pande 4:5d91b0f5038c 359 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 360
Christopher Haster 1:43f5c94c6771 361 /* If blockwising needed */
Yogesh Pande 4:5d91b0f5038c 362 if ((src_coap_msg_ptr->payload_len > handle->sn_coap_block_data_size) && (handle->sn_coap_block_data_size > 0)) {
Christopher Haster 1:43f5c94c6771 363 /* * * * Add Blockwise option to send CoAP message * * */
Christopher Haster 1:43f5c94c6771 364 if (src_coap_msg_ptr->options_list_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 365 /* Allocate memory for less used options */
Christopher Haster 1:43f5c94c6771 366 src_coap_msg_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 367
Christopher Haster 1:43f5c94c6771 368 if (src_coap_msg_ptr->options_list_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 369 return -2;
Christopher Haster 1:43f5c94c6771 370 }
Christopher Haster 1:43f5c94c6771 371 memset(src_coap_msg_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 372 }
Christopher Haster 1:43f5c94c6771 373
Christopher Haster 1:43f5c94c6771 374
Christopher Haster 1:43f5c94c6771 375 /* Check if Request message */
Christopher Haster 1:43f5c94c6771 376 if (src_coap_msg_ptr->msg_code < COAP_MSG_CODE_RESPONSE_CREATED) {
Christopher Haster 1:43f5c94c6771 377 /* Add Blockwise option, use Block1 because Request payload */
Christopher Haster 1:43f5c94c6771 378 src_coap_msg_ptr->options_list_ptr->block1_len = 1;
Christopher Haster 1:43f5c94c6771 379 if( src_coap_msg_ptr->options_list_ptr->block1_ptr ){
Christopher Haster 1:43f5c94c6771 380 handle->sn_coap_protocol_free(src_coap_msg_ptr->options_list_ptr->block1_ptr);
Christopher Haster 1:43f5c94c6771 381 src_coap_msg_ptr->options_list_ptr->block1_ptr = 0;
Christopher Haster 1:43f5c94c6771 382 }
Christopher Haster 1:43f5c94c6771 383 src_coap_msg_ptr->options_list_ptr->block1_ptr = handle->sn_coap_protocol_malloc(1);
Christopher Haster 1:43f5c94c6771 384
Christopher Haster 1:43f5c94c6771 385 if (src_coap_msg_ptr->options_list_ptr->block1_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 386 handle->sn_coap_protocol_free(src_coap_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 387 src_coap_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 388 return -2;
Christopher Haster 1:43f5c94c6771 389 }
Christopher Haster 1:43f5c94c6771 390
Christopher Haster 1:43f5c94c6771 391 *(src_coap_msg_ptr->options_list_ptr->block1_ptr) = 0x08; /* First block (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */
Yogesh Pande 4:5d91b0f5038c 392 *(src_coap_msg_ptr->options_list_ptr->block1_ptr) |= sn_coap_convert_block_size(handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 393
Christopher Haster 1:43f5c94c6771 394 } else { /* Response message */
Christopher Haster 1:43f5c94c6771 395 /* Add Blockwise option, use Block2 because Response payload */
Christopher Haster 1:43f5c94c6771 396 src_coap_msg_ptr->options_list_ptr->block2_len = 1;
Christopher Haster 1:43f5c94c6771 397 if( src_coap_msg_ptr->options_list_ptr->block2_ptr ){
Christopher Haster 1:43f5c94c6771 398 handle->sn_coap_protocol_free(src_coap_msg_ptr->options_list_ptr->block2_ptr);
Christopher Haster 1:43f5c94c6771 399 src_coap_msg_ptr->options_list_ptr->block2_ptr = 0;
Christopher Haster 1:43f5c94c6771 400 }
Christopher Haster 1:43f5c94c6771 401
Christopher Haster 1:43f5c94c6771 402 src_coap_msg_ptr->options_list_ptr->block2_ptr = handle->sn_coap_protocol_malloc(1);
Christopher Haster 1:43f5c94c6771 403
Christopher Haster 1:43f5c94c6771 404 if (src_coap_msg_ptr->options_list_ptr->block2_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 405 handle->sn_coap_protocol_free(src_coap_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 406 src_coap_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 407 return -2;
Christopher Haster 1:43f5c94c6771 408 }
Christopher Haster 1:43f5c94c6771 409
Christopher Haster 1:43f5c94c6771 410 *(src_coap_msg_ptr->options_list_ptr->block2_ptr) = 0x08; /* First block (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */
Yogesh Pande 4:5d91b0f5038c 411 *(src_coap_msg_ptr->options_list_ptr->block2_ptr) |= sn_coap_convert_block_size(handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 412 }
Christopher Haster 1:43f5c94c6771 413
Christopher Haster 1:43f5c94c6771 414 /* Store original Payload length */
Christopher Haster 1:43f5c94c6771 415 original_payload_len = src_coap_msg_ptr->payload_len;
Christopher Haster 1:43f5c94c6771 416 /* Change Payload length of send message because Payload is blockwised */
Yogesh Pande 4:5d91b0f5038c 417 src_coap_msg_ptr->payload_len = handle->sn_coap_block_data_size;
Christopher Haster 1:43f5c94c6771 418 }
Christopher Haster 1:43f5c94c6771 419
Christopher Haster 1:43f5c94c6771 420 #endif
Christopher Haster 1:43f5c94c6771 421 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 422 /* * * * Build Packet data from CoAP message by using CoAP Header builder * * * */
Christopher Haster 1:43f5c94c6771 423 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 424
Yogesh Pande 4:5d91b0f5038c 425 byte_count_built = sn_coap_builder_2(dst_packet_data_ptr, src_coap_msg_ptr, handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 426
Christopher Haster 1:43f5c94c6771 427 if (byte_count_built < 0) {
Christopher Haster 1:43f5c94c6771 428 return byte_count_built;
Christopher Haster 1:43f5c94c6771 429 }
Christopher Haster 1:43f5c94c6771 430
Christopher Haster 1:43f5c94c6771 431 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 432
Christopher Haster 1:43f5c94c6771 433 /* Check if built Message type was confirmable, only these messages are resent */
Christopher Haster 1:43f5c94c6771 434 if (src_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
Christopher Haster 1:43f5c94c6771 435 /* Store message to Linked list for resending purposes */
Christopher Haster 1:43f5c94c6771 436 sn_coap_protocol_linked_list_send_msg_store(handle, dst_addr_ptr, byte_count_built, dst_packet_data_ptr,
Yogesh Pande 4:5d91b0f5038c 437 handle->system_time + (uint32_t)(handle->sn_coap_resending_intervall * RESPONSE_RANDOM_FACTOR),
Yogesh Pande 4:5d91b0f5038c 438 param, src_coap_msg_ptr->uri_path_ptr, src_coap_msg_ptr->uri_path_len);
Christopher Haster 1:43f5c94c6771 439 }
Christopher Haster 1:43f5c94c6771 440
Christopher Haster 1:43f5c94c6771 441 #endif /* ENABLE_RESENDINGS */
Christopher Haster 1:43f5c94c6771 442
Yogesh Pande 4:5d91b0f5038c 443 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 444
Christopher Haster 1:43f5c94c6771 445 /* If blockwising needed */
Yogesh Pande 4:5d91b0f5038c 446 if ((original_payload_len > handle->sn_coap_block_data_size) && (handle->sn_coap_block_data_size > 0)) {
Christopher Haster 1:43f5c94c6771 447
Christopher Haster 1:43f5c94c6771 448 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 449 /* * * * Manage rest blockwise messages sending by storing them to Linked list * * * */
Christopher Haster 1:43f5c94c6771 450 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 451
Christopher Haster 1:43f5c94c6771 452 coap_blockwise_msg_s *stored_blockwise_msg_ptr;
Christopher Haster 1:43f5c94c6771 453
Christopher Haster 1:43f5c94c6771 454 stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
Christopher Haster 1:43f5c94c6771 455 if (!stored_blockwise_msg_ptr) {
Christopher Haster 1:43f5c94c6771 456 //block paylaod save failed, only first block can be build. Perhaps we should return error.
Christopher Haster 1:43f5c94c6771 457 return byte_count_built;
Christopher Haster 1:43f5c94c6771 458 }
Christopher Haster 1:43f5c94c6771 459 memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
Christopher Haster 1:43f5c94c6771 460
Christopher Haster 1:43f5c94c6771 461 /* Fill struct */
Yogesh Pande 4:5d91b0f5038c 462 stored_blockwise_msg_ptr->timestamp = handle->system_time;
Christopher Haster 1:43f5c94c6771 463
Christopher Haster 1:43f5c94c6771 464 stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(handle, src_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 465 if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){
Christopher Haster 1:43f5c94c6771 466 handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
Christopher Haster 1:43f5c94c6771 467 stored_blockwise_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 468 return -2;
Christopher Haster 1:43f5c94c6771 469 }
Christopher Haster 1:43f5c94c6771 470
Christopher Haster 1:43f5c94c6771 471 stored_blockwise_msg_ptr->coap_msg_ptr->payload_len = original_payload_len;
Christopher Haster 1:43f5c94c6771 472 stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr = handle->sn_coap_protocol_malloc(stored_blockwise_msg_ptr->coap_msg_ptr->payload_len);
Christopher Haster 1:43f5c94c6771 473
Christopher Haster 1:43f5c94c6771 474 if (!stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr) {
Christopher Haster 1:43f5c94c6771 475 //block payload save failed, only first block can be build. Perhaps we should return error.
Christopher Haster 1:43f5c94c6771 476 sn_coap_parser_release_allocated_coap_msg_mem(handle, stored_blockwise_msg_ptr->coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 477 handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
Christopher Haster 1:43f5c94c6771 478 stored_blockwise_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 479 return byte_count_built;
Christopher Haster 1:43f5c94c6771 480 }
Christopher Haster 1:43f5c94c6771 481 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);
Christopher Haster 1:43f5c94c6771 482
Christopher Haster 1:43f5c94c6771 483 stored_blockwise_msg_ptr->coap = handle;
Christopher Haster 1:43f5c94c6771 484
Yogesh Pande 4:5d91b0f5038c 485 ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr);
Christopher Haster 1:43f5c94c6771 486 }
Christopher Haster 1:43f5c94c6771 487
Christopher Haster 1:43f5c94c6771 488 else if (src_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
Christopher Haster 1:43f5c94c6771 489 /* Add message to linked list - response can be in blocks and we need header to build response.. */
Christopher Haster 1:43f5c94c6771 490 coap_blockwise_msg_s *stored_blockwise_msg_ptr;
Christopher Haster 1:43f5c94c6771 491
Christopher Haster 1:43f5c94c6771 492 stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
Christopher Haster 1:43f5c94c6771 493 if (!stored_blockwise_msg_ptr) {
Christopher Haster 1:43f5c94c6771 494 return byte_count_built;
Christopher Haster 1:43f5c94c6771 495 }
Christopher Haster 1:43f5c94c6771 496 memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
Christopher Haster 1:43f5c94c6771 497
Christopher Haster 1:43f5c94c6771 498 /* Fill struct */
Yogesh Pande 4:5d91b0f5038c 499 stored_blockwise_msg_ptr->timestamp = handle->system_time;
Christopher Haster 1:43f5c94c6771 500
Christopher Haster 1:43f5c94c6771 501 stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(handle, src_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 502 if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){
Christopher Haster 1:43f5c94c6771 503 handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
Christopher Haster 1:43f5c94c6771 504 stored_blockwise_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 505 return -2;
Christopher Haster 1:43f5c94c6771 506 }
Christopher Haster 1:43f5c94c6771 507
Christopher Haster 1:43f5c94c6771 508 stored_blockwise_msg_ptr->coap = handle;
Christopher Haster 1:43f5c94c6771 509
Yogesh Pande 4:5d91b0f5038c 510 ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr);
Christopher Haster 1:43f5c94c6771 511 }
Christopher Haster 1:43f5c94c6771 512
Yogesh Pande 4:5d91b0f5038c 513 #endif /* YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */
Christopher Haster 1:43f5c94c6771 514
Christopher Haster 1:43f5c94c6771 515 /* * * * Return built CoAP message Packet data length * * * */
Christopher Haster 1:43f5c94c6771 516 return byte_count_built;
Christopher Haster 1:43f5c94c6771 517 }
Christopher Haster 1:43f5c94c6771 518
Christopher Haster 1:43f5c94c6771 519 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)
Christopher Haster 1:43f5c94c6771 520 {
Christopher Haster 1:43f5c94c6771 521 sn_coap_hdr_s *returned_dst_coap_msg_ptr = NULL;
Christopher Haster 1:43f5c94c6771 522 coap_version_e coap_version = COAP_VERSION_UNKNOWN;
Christopher Haster 1:43f5c94c6771 523
Christopher Haster 1:43f5c94c6771 524 /* * * * Check given pointer * * * */
Christopher Haster 1:43f5c94c6771 525 if (src_addr_ptr == NULL || src_addr_ptr->addr_ptr == NULL ||
Christopher Haster 1:43f5c94c6771 526 packet_data_ptr == NULL || handle == NULL) {
Christopher Haster 1:43f5c94c6771 527 return NULL;
Christopher Haster 1:43f5c94c6771 528 }
Christopher Haster 1:43f5c94c6771 529
Christopher Haster 1:43f5c94c6771 530 /* * * * Parse Packet data to CoAP message by using CoAP Header parser * * * */
Christopher Haster 1:43f5c94c6771 531 returned_dst_coap_msg_ptr = sn_coap_parser(handle, packet_data_len, packet_data_ptr, &coap_version);
Christopher Haster 1:43f5c94c6771 532
Christopher Haster 1:43f5c94c6771 533 /* Check status of returned pointer */
Christopher Haster 1:43f5c94c6771 534 if (returned_dst_coap_msg_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 535 /* Memory allocation error in parser */
Christopher Haster 1:43f5c94c6771 536 return NULL;
Christopher Haster 1:43f5c94c6771 537 }
Yogesh Pande 4:5d91b0f5038c 538 /* * * * Send bad request response if parsing fails * * * */
Yogesh Pande 4:5d91b0f5038c 539 if (returned_dst_coap_msg_ptr->coap_status == COAP_STATUS_PARSER_ERROR_IN_HEADER) {
Yogesh Pande 4:5d91b0f5038c 540 sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
Yogesh Pande 4:5d91b0f5038c 541 sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr);
Yogesh Pande 4:5d91b0f5038c 542 return NULL;
Yogesh Pande 4:5d91b0f5038c 543 }
Christopher Haster 1:43f5c94c6771 544
Christopher Haster 1:43f5c94c6771 545 /* * * * Check validity of parsed Header values * * * */
Christopher Haster 1:43f5c94c6771 546 if (sn_coap_header_validity_check(returned_dst_coap_msg_ptr, coap_version) != 0) {
Christopher Haster 1:43f5c94c6771 547 /* 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 */
Christopher Haster 1:43f5c94c6771 548 if (((returned_dst_coap_msg_ptr->msg_code >> 5) == 1) || // if class == 1
Christopher Haster 1:43f5c94c6771 549 ((returned_dst_coap_msg_ptr->msg_code >> 5) == 6) || // if class == 6
Christopher Haster 1:43f5c94c6771 550 ((returned_dst_coap_msg_ptr->msg_code >> 5) == 7)) { // if class == 7
Yogesh Pande 4:5d91b0f5038c 551 sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
Christopher Haster 1:43f5c94c6771 552 }
Christopher Haster 1:43f5c94c6771 553
Christopher Haster 1:43f5c94c6771 554 /* Release memory of CoAP message */
Christopher Haster 1:43f5c94c6771 555 sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 556
Christopher Haster 1:43f5c94c6771 557 /* Return NULL because Header validity check failed */
Christopher Haster 1:43f5c94c6771 558 return NULL;
Christopher Haster 1:43f5c94c6771 559 }
Christopher Haster 1:43f5c94c6771 560
Christopher Haster 1:43f5c94c6771 561 /* Check if we need to send reset message */
Christopher Haster 1:43f5c94c6771 562 /* A recipient MUST acknowledge a Confirmable message with an Acknowledgement
Christopher Haster 1:43f5c94c6771 563 message or, if it lacks context to process the message properly
Christopher Haster 1:43f5c94c6771 564 (including the case where the message is Empty, uses a code with a
Christopher Haster 1:43f5c94c6771 565 reserved class (1, 6 or 7), or has a message format error), MUST
Christopher Haster 1:43f5c94c6771 566 reject it; rejecting a Confirmable message is effected by sending a
Christopher Haster 1:43f5c94c6771 567 matching Reset message and otherwise ignoring it. */
Christopher Haster 1:43f5c94c6771 568 if (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
Christopher Haster 1:43f5c94c6771 569 /* CoAP ping */
Christopher Haster 1:43f5c94c6771 570 if (returned_dst_coap_msg_ptr->msg_code == COAP_MSG_CODE_EMPTY) {
Christopher Haster 1:43f5c94c6771 571 sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
Christopher Haster 1:43f5c94c6771 572
Christopher Haster 1:43f5c94c6771 573 /* Release memory of CoAP message */
Christopher Haster 1:43f5c94c6771 574 sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 575
Christopher Haster 1:43f5c94c6771 576 /* Return NULL because Header validity check failed */
Christopher Haster 1:43f5c94c6771 577 return NULL;
Christopher Haster 1:43f5c94c6771 578 }
Christopher Haster 1:43f5c94c6771 579 }
Christopher Haster 1:43f5c94c6771 580
Yogesh Pande 4:5d91b0f5038c 581
Yogesh Pande 4:5d91b0f5038c 582 #if !YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is used, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 583 /* If blockwising used in received message */
Christopher Haster 1:43f5c94c6771 584 if (returned_dst_coap_msg_ptr->options_list_ptr != NULL &&
Christopher Haster 1:43f5c94c6771 585 (returned_dst_coap_msg_ptr->options_list_ptr->block1_ptr != NULL ||
Christopher Haster 1:43f5c94c6771 586 returned_dst_coap_msg_ptr->options_list_ptr->block2_ptr != NULL)) {
Christopher Haster 1:43f5c94c6771 587 /* Set returned status to User */
Christopher Haster 1:43f5c94c6771 588 returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED;
Christopher Haster 1:43f5c94c6771 589 //todo: send response -> not implemented
Christopher Haster 1:43f5c94c6771 590 return returned_dst_coap_msg_ptr;
Christopher Haster 1:43f5c94c6771 591 }
Yogesh Pande 4:5d91b0f5038c 592 #endif /* !YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */
Christopher Haster 1:43f5c94c6771 593
Yogesh Pande 4:5d91b0f5038c 594 #if YOTTA_CFG_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication is used, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 595
Christopher Haster 1:43f5c94c6771 596 /* * * * Manage received CoAP message duplicate detection * * * */
Christopher Haster 1:43f5c94c6771 597
Christopher Haster 1:43f5c94c6771 598 /* If no message duplication detected */
Yogesh Pande 4:5d91b0f5038c 599 if (sn_coap_protocol_linked_list_duplication_info_search(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id) == -1) {
Christopher Haster 1:43f5c94c6771 600 /* * * No Message duplication: Store received message for detecting later duplication * * */
Christopher Haster 1:43f5c94c6771 601
Christopher Haster 1:43f5c94c6771 602 /* Get count of stored duplication messages */
Yogesh Pande 4:5d91b0f5038c 603 uint16_t stored_duplication_msgs_count = handle->count_duplication_msgs;
Christopher Haster 1:43f5c94c6771 604
Christopher Haster 1:43f5c94c6771 605 /* Check if there is no room to store message for duplication detection purposes */
Yogesh Pande 4:5d91b0f5038c 606 if (stored_duplication_msgs_count >= handle->sn_coap_duplication_buffer_size) {
Christopher Haster 1:43f5c94c6771 607 /* Get oldest stored duplication message */
Yogesh Pande 4:5d91b0f5038c 608 coap_duplication_info_s *stored_duplication_info_ptr = ns_list_get_first(&handle->linked_list_duplication_msgs);
Christopher Haster 1:43f5c94c6771 609
Christopher Haster 1:43f5c94c6771 610 /* Remove oldest stored duplication message for getting room for new duplication message */
Christopher Haster 1:43f5c94c6771 611 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);
Christopher Haster 1:43f5c94c6771 612 }
Christopher Haster 1:43f5c94c6771 613
Christopher Haster 1:43f5c94c6771 614 /* Store Duplication info to Linked list */
Christopher Haster 1:43f5c94c6771 615 sn_coap_protocol_linked_list_duplication_info_store(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
Christopher Haster 1:43f5c94c6771 616 } else { /* * * Message duplication detected * * */
Christopher Haster 1:43f5c94c6771 617 /* Set returned status to User */
Christopher Haster 1:43f5c94c6771 618 returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_DUPLICATED_MSG;
Christopher Haster 1:43f5c94c6771 619
Christopher Haster 1:43f5c94c6771 620 /* Because duplicate message, return with coap_status set */
Christopher Haster 1:43f5c94c6771 621 return returned_dst_coap_msg_ptr;
Christopher Haster 1:43f5c94c6771 622 }
Christopher Haster 1:43f5c94c6771 623 #endif
Christopher Haster 1:43f5c94c6771 624
Christopher Haster 1:43f5c94c6771 625 /*** And here we check if message was block message ***/
Christopher Haster 1:43f5c94c6771 626 /*** If so, we call own block handling function and ***/
Christopher Haster 1:43f5c94c6771 627 /*** return to caller. ***/
Yogesh Pande 4:5d91b0f5038c 628 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
Christopher Haster 1:43f5c94c6771 629
Christopher Haster 1:43f5c94c6771 630 if (returned_dst_coap_msg_ptr->options_list_ptr != NULL &&
Christopher Haster 1:43f5c94c6771 631 (returned_dst_coap_msg_ptr->options_list_ptr->block1_ptr != NULL ||
Christopher Haster 1:43f5c94c6771 632 returned_dst_coap_msg_ptr->options_list_ptr->block2_ptr != NULL)) {
Christopher Haster 1:43f5c94c6771 633 returned_dst_coap_msg_ptr = sn_coap_handle_blockwise_message(handle, src_addr_ptr, returned_dst_coap_msg_ptr, param);
Christopher Haster 1:43f5c94c6771 634 } else {
Christopher Haster 1:43f5c94c6771 635 /* Get ... */
Christopher Haster 1:43f5c94c6771 636 coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = NULL;
Christopher Haster 1:43f5c94c6771 637
Yogesh Pande 4:5d91b0f5038c 638 ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) {
Christopher Haster 1:43f5c94c6771 639 if (returned_dst_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) {
Christopher Haster 1:43f5c94c6771 640 stored_blockwise_msg_temp_ptr = msg;
Christopher Haster 1:43f5c94c6771 641 break;
Christopher Haster 1:43f5c94c6771 642 }
Christopher Haster 1:43f5c94c6771 643 }
Christopher Haster 1:43f5c94c6771 644
Christopher Haster 1:43f5c94c6771 645 if (stored_blockwise_msg_temp_ptr) {
Yogesh Pande 4:5d91b0f5038c 646 ns_list_remove(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_temp_ptr);
Christopher Haster 1:43f5c94c6771 647
Christopher Haster 1:43f5c94c6771 648 if (stored_blockwise_msg_temp_ptr->coap_msg_ptr) {
Christopher Haster 1:43f5c94c6771 649 if(stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr){
Christopher Haster 1:43f5c94c6771 650 handle->sn_coap_protocol_free(stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 651 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 652 }
Christopher Haster 1:43f5c94c6771 653 sn_coap_parser_release_allocated_coap_msg_mem(stored_blockwise_msg_temp_ptr->coap, stored_blockwise_msg_temp_ptr->coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 654 }
Christopher Haster 1:43f5c94c6771 655
Christopher Haster 1:43f5c94c6771 656 handle->sn_coap_protocol_free(stored_blockwise_msg_temp_ptr);
Christopher Haster 1:43f5c94c6771 657 stored_blockwise_msg_temp_ptr = 0;
Christopher Haster 1:43f5c94c6771 658 }
Christopher Haster 1:43f5c94c6771 659 }
Christopher Haster 1:43f5c94c6771 660
Christopher Haster 1:43f5c94c6771 661 if (!returned_dst_coap_msg_ptr) {
Christopher Haster 1:43f5c94c6771 662 return NULL;
Christopher Haster 1:43f5c94c6771 663 }
Christopher Haster 1:43f5c94c6771 664
Christopher Haster 1:43f5c94c6771 665 #endif
Christopher Haster 1:43f5c94c6771 666
Christopher Haster 1:43f5c94c6771 667
Christopher Haster 1:43f5c94c6771 668 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 669
Christopher Haster 1:43f5c94c6771 670 /* Check if received Message type was acknowledgement */
Christopher Haster 1:43f5c94c6771 671 if ((returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT) || (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_RESET)) {
Christopher Haster 1:43f5c94c6771 672 /* * * * Manage CoAP message resending by removing active resending message from Linked list * * */
Christopher Haster 1:43f5c94c6771 673
Christopher Haster 1:43f5c94c6771 674 /* Get node count i.e. count of active resending messages */
Yogesh Pande 4:5d91b0f5038c 675 uint16_t stored_resending_msgs_count = handle->count_resent_msgs;
Christopher Haster 1:43f5c94c6771 676
Christopher Haster 1:43f5c94c6771 677 /* Check if there is ongoing active message resendings */
Christopher Haster 1:43f5c94c6771 678 if (stored_resending_msgs_count > 0) {
Christopher Haster 1:43f5c94c6771 679 sn_nsdl_transmit_s *removed_msg_ptr = NULL;
Christopher Haster 1:43f5c94c6771 680
Christopher Haster 1:43f5c94c6771 681 /* Check if received message was confirmation for some active resending message */
Yogesh Pande 4:5d91b0f5038c 682 removed_msg_ptr = sn_coap_protocol_linked_list_send_msg_search(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
Christopher Haster 1:43f5c94c6771 683
Christopher Haster 1:43f5c94c6771 684 if (removed_msg_ptr != NULL) {
Yogesh Pande 4:5d91b0f5038c 685 if (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_RESET) {
Yogesh Pande 4:5d91b0f5038c 686 if(removed_msg_ptr->uri_path_len) {
Yogesh Pande 4:5d91b0f5038c 687 returned_dst_coap_msg_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(removed_msg_ptr->uri_path_len);
Yogesh Pande 4:5d91b0f5038c 688 if (returned_dst_coap_msg_ptr->uri_path_ptr != NULL) {
Yogesh Pande 4:5d91b0f5038c 689 memcpy(returned_dst_coap_msg_ptr->uri_path_ptr, removed_msg_ptr->uri_path_ptr, removed_msg_ptr->uri_path_len);
Yogesh Pande 4:5d91b0f5038c 690 returned_dst_coap_msg_ptr->uri_path_len = removed_msg_ptr->uri_path_len;
Yogesh Pande 4:5d91b0f5038c 691 }
Yogesh Pande 4:5d91b0f5038c 692 }
Yogesh Pande 4:5d91b0f5038c 693 }
Christopher Haster 1:43f5c94c6771 694 /* Remove resending message from active message resending Linked list */
Christopher Haster 1:43f5c94c6771 695 sn_coap_protocol_linked_list_send_msg_remove(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
Christopher Haster 1:43f5c94c6771 696 }
Christopher Haster 1:43f5c94c6771 697 }
Christopher Haster 1:43f5c94c6771 698 }
Christopher Haster 1:43f5c94c6771 699 #endif /* ENABLE_RESENDINGS */
Christopher Haster 1:43f5c94c6771 700
Christopher Haster 1:43f5c94c6771 701 /* * * * Return parsed CoAP message * * * */
Yogesh Pande 4:5d91b0f5038c 702 return returned_dst_coap_msg_ptr;
Christopher Haster 1:43f5c94c6771 703 }
Christopher Haster 1:43f5c94c6771 704
Christopher Haster 1:43f5c94c6771 705
Christopher Haster 1:43f5c94c6771 706 int8_t sn_coap_protocol_exec(struct coap_s *handle, uint32_t current_time)
Christopher Haster 1:43f5c94c6771 707 {
Christopher Haster 1:43f5c94c6771 708 if( !handle ){
Christopher Haster 1:43f5c94c6771 709 return -1;
Christopher Haster 1:43f5c94c6771 710 }
Christopher Haster 1:43f5c94c6771 711
Yogesh Pande 4:5d91b0f5038c 712 /* * * * Store current System time * * * */
Yogesh Pande 4:5d91b0f5038c 713 handle->system_time = current_time;
Yogesh Pande 4:5d91b0f5038c 714
Yogesh Pande 4:5d91b0f5038c 715 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
Christopher Haster 1:43f5c94c6771 716 /* * * * Remove old blocwise data * * * */
Christopher Haster 1:43f5c94c6771 717 sn_coap_protocol_linked_list_blockwise_remove_old_data(handle);
Christopher Haster 1:43f5c94c6771 718 #endif
Christopher Haster 1:43f5c94c6771 719
Christopher Haster 1:43f5c94c6771 720
Yogesh Pande 4:5d91b0f5038c 721 #if YOTTA_CFG_COAP_DUPLICATION_MAX_MSGS_COUNT
Christopher Haster 1:43f5c94c6771 722 /* * * * Remove old duplication messages * * * */
Christopher Haster 1:43f5c94c6771 723 sn_coap_protocol_linked_list_duplication_info_remove_old_ones(handle);
Christopher Haster 1:43f5c94c6771 724 #endif
Christopher Haster 1:43f5c94c6771 725
Christopher Haster 1:43f5c94c6771 726 #if ENABLE_RESENDINGS
Christopher Haster 1:43f5c94c6771 727 /* Check if there is ongoing active message sendings */
Yogesh Pande 4:5d91b0f5038c 728 ns_list_foreach_safe(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) {
Christopher Haster 1:43f5c94c6771 729 // First check that msg belongs to handle
Christopher Haster 1:43f5c94c6771 730 if( stored_msg_ptr->coap == handle ){
Christopher Haster 1:43f5c94c6771 731 /* Check if it is time to send this message */
Christopher Haster 1:43f5c94c6771 732 if (current_time >= stored_msg_ptr->resending_time) {
Christopher Haster 1:43f5c94c6771 733 /* * * Increase Resending counter * * */
Christopher Haster 1:43f5c94c6771 734 stored_msg_ptr->resending_counter++;
Christopher Haster 1:43f5c94c6771 735
Christopher Haster 1:43f5c94c6771 736 /* Check if all re-sendings have been done */
Yogesh Pande 4:5d91b0f5038c 737 if (stored_msg_ptr->resending_counter > handle->sn_coap_resending_count) {
Christopher Haster 1:43f5c94c6771 738 coap_version_e coap_version = COAP_VERSION_UNKNOWN;
Christopher Haster 1:43f5c94c6771 739
Christopher Haster 1:43f5c94c6771 740 /* Get message ID from stored sending message */
Christopher Haster 1:43f5c94c6771 741 uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
Christopher Haster 1:43f5c94c6771 742 temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
Christopher Haster 1:43f5c94c6771 743
Christopher Haster 1:43f5c94c6771 744 /* If RX callback have been defined.. */
Christopher Haster 1:43f5c94c6771 745 if (stored_msg_ptr->coap->sn_coap_rx_callback != 0) {
Christopher Haster 1:43f5c94c6771 746 sn_coap_hdr_s *tmp_coap_hdr_ptr;
Christopher Haster 1:43f5c94c6771 747 /* Parse CoAP message, set status and call RX callback */
Christopher Haster 1:43f5c94c6771 748 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);
Christopher Haster 1:43f5c94c6771 749
Christopher Haster 1:43f5c94c6771 750 if (tmp_coap_hdr_ptr != 0) {
Christopher Haster 1:43f5c94c6771 751 tmp_coap_hdr_ptr->coap_status = COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED;
Christopher Haster 1:43f5c94c6771 752
Christopher Haster 1:43f5c94c6771 753 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);
Christopher Haster 1:43f5c94c6771 754
Christopher Haster 1:43f5c94c6771 755 sn_coap_parser_release_allocated_coap_msg_mem(stored_msg_ptr->coap, tmp_coap_hdr_ptr);
Christopher Haster 1:43f5c94c6771 756 }
Christopher Haster 1:43f5c94c6771 757 }
Christopher Haster 1:43f5c94c6771 758 /* Remove message from Linked list */
Christopher Haster 1:43f5c94c6771 759 sn_coap_protocol_linked_list_send_msg_remove(handle, stored_msg_ptr->send_msg_ptr->dst_addr_ptr, temp_msg_id);
Christopher Haster 1:43f5c94c6771 760 } else {
Christopher Haster 1:43f5c94c6771 761 /* Send message */
Christopher Haster 1:43f5c94c6771 762 stored_msg_ptr->coap->sn_coap_tx_callback(stored_msg_ptr->send_msg_ptr->packet_ptr,
Christopher Haster 1:43f5c94c6771 763 stored_msg_ptr->send_msg_ptr->packet_len, stored_msg_ptr->send_msg_ptr->dst_addr_ptr, stored_msg_ptr->param);
Christopher Haster 1:43f5c94c6771 764
Christopher Haster 1:43f5c94c6771 765 /* * * Count new Resending time * * */
Yogesh Pande 4:5d91b0f5038c 766 stored_msg_ptr->resending_time = current_time + (((uint32_t)(handle->sn_coap_resending_intervall * RESPONSE_RANDOM_FACTOR)) <<
Christopher Haster 1:43f5c94c6771 767 stored_msg_ptr->resending_counter);
Christopher Haster 1:43f5c94c6771 768 }
Christopher Haster 1:43f5c94c6771 769
Christopher Haster 1:43f5c94c6771 770 }
Christopher Haster 1:43f5c94c6771 771 }
Christopher Haster 1:43f5c94c6771 772 }
Christopher Haster 1:43f5c94c6771 773
Christopher Haster 1:43f5c94c6771 774 #endif /* ENABLE_RESENDINGS */
Christopher Haster 1:43f5c94c6771 775
Christopher Haster 1:43f5c94c6771 776 return 0;
Christopher Haster 1:43f5c94c6771 777 }
Christopher Haster 1:43f5c94c6771 778
Christopher Haster 1:43f5c94c6771 779 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 780
Christopher Haster 1:43f5c94c6771 781 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 782 * \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)
Christopher Haster 1:43f5c94c6771 783 *
Christopher Haster 1:43f5c94c6771 784 * \brief Stores message to Linked list for sending purposes.
Christopher Haster 1:43f5c94c6771 785
Christopher Haster 1:43f5c94c6771 786 * \param *dst_addr_ptr is pointer to destination address where CoAP message will be sent
Christopher Haster 1:43f5c94c6771 787 *
Christopher Haster 1:43f5c94c6771 788 * \param send_packet_data_len is length of Packet data to be stored
Christopher Haster 1:43f5c94c6771 789 *
Christopher Haster 1:43f5c94c6771 790 * \param *send_packet_data_ptr is Packet data to be stored
Christopher Haster 1:43f5c94c6771 791 *
Christopher Haster 1:43f5c94c6771 792 * \param sending_time is stored sending time
Christopher Haster 1:43f5c94c6771 793 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 794
Christopher Haster 1:43f5c94c6771 795 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,
Yogesh Pande 4:5d91b0f5038c 796 uint8_t *send_packet_data_ptr, uint32_t sending_time, void *param, uint8_t *uri_path_ptr, uint8_t uri_path_len)
Christopher Haster 1:43f5c94c6771 797 {
Christopher Haster 1:43f5c94c6771 798
Christopher Haster 1:43f5c94c6771 799 coap_send_msg_s *stored_msg_ptr = NULL;
Christopher Haster 1:43f5c94c6771 800
Christopher Haster 1:43f5c94c6771 801 /* If both queue parameters are "0" or resending count is "0", then re-sending is disabled */
Yogesh Pande 4:5d91b0f5038c 802 if (((handle->sn_coap_resending_queue_msgs == 0) && (handle->sn_coap_resending_queue_bytes == 0)) || (handle->sn_coap_resending_count == 0)) {
Christopher Haster 1:43f5c94c6771 803 return;
Christopher Haster 1:43f5c94c6771 804 }
Christopher Haster 1:43f5c94c6771 805
Yogesh Pande 4:5d91b0f5038c 806 if (handle->sn_coap_resending_queue_msgs > 0) {
Yogesh Pande 4:5d91b0f5038c 807 if (handle->count_resent_msgs >= handle->sn_coap_resending_queue_msgs) {
Christopher Haster 1:43f5c94c6771 808 return;
Christopher Haster 1:43f5c94c6771 809 }
Christopher Haster 1:43f5c94c6771 810 }
Christopher Haster 1:43f5c94c6771 811
Christopher Haster 1:43f5c94c6771 812 /* Count resending queue size, if buffer size is defined */
Yogesh Pande 4:5d91b0f5038c 813 if (handle->sn_coap_resending_queue_bytes > 0) {
Yogesh Pande 4:5d91b0f5038c 814 if ((sn_coap_count_linked_list_size(&handle->linked_list_resent_msgs) + send_packet_data_len) > handle->sn_coap_resending_queue_bytes) {
Christopher Haster 1:43f5c94c6771 815 return;
Christopher Haster 1:43f5c94c6771 816 }
Christopher Haster 1:43f5c94c6771 817 }
Christopher Haster 1:43f5c94c6771 818
Christopher Haster 1:43f5c94c6771 819 /* Allocating memory for stored message */
Christopher Haster 1:43f5c94c6771 820 stored_msg_ptr = sn_coap_protocol_allocate_mem_for_msg(handle, dst_addr_ptr, send_packet_data_len);
Christopher Haster 1:43f5c94c6771 821
Christopher Haster 1:43f5c94c6771 822 if (stored_msg_ptr == 0) {
Christopher Haster 1:43f5c94c6771 823 return;
Christopher Haster 1:43f5c94c6771 824 }
Christopher Haster 1:43f5c94c6771 825
Christopher Haster 1:43f5c94c6771 826 /* Filling of coap_send_msg_s with initialization values */
Christopher Haster 1:43f5c94c6771 827 stored_msg_ptr->resending_counter = 0;
Christopher Haster 1:43f5c94c6771 828 stored_msg_ptr->resending_time = sending_time;
Christopher Haster 1:43f5c94c6771 829
Christopher Haster 1:43f5c94c6771 830 /* Filling of sn_nsdl_transmit_s */
Christopher Haster 1:43f5c94c6771 831 stored_msg_ptr->send_msg_ptr->protocol = SN_NSDL_PROTOCOL_COAP;
Christopher Haster 1:43f5c94c6771 832 stored_msg_ptr->send_msg_ptr->packet_len = send_packet_data_len;
Christopher Haster 1:43f5c94c6771 833 memcpy(stored_msg_ptr->send_msg_ptr->packet_ptr, send_packet_data_ptr, send_packet_data_len);
Christopher Haster 1:43f5c94c6771 834
Christopher Haster 1:43f5c94c6771 835 /* Filling of sn_nsdl_addr_s */
Christopher Haster 1:43f5c94c6771 836 stored_msg_ptr->send_msg_ptr->dst_addr_ptr->type = dst_addr_ptr->type;
Christopher Haster 1:43f5c94c6771 837 stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_len = dst_addr_ptr->addr_len;
Christopher Haster 1:43f5c94c6771 838 memcpy(stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 839 stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port = dst_addr_ptr->port;
Christopher Haster 1:43f5c94c6771 840
Christopher Haster 1:43f5c94c6771 841 stored_msg_ptr->coap = handle;
Christopher Haster 1:43f5c94c6771 842 stored_msg_ptr->param = param;
Christopher Haster 1:43f5c94c6771 843
Yogesh Pande 4:5d91b0f5038c 844 if (uri_path_len) {
Yogesh Pande 4:5d91b0f5038c 845 stored_msg_ptr->send_msg_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(uri_path_len);
Yogesh Pande 4:5d91b0f5038c 846 if (stored_msg_ptr->send_msg_ptr->uri_path_ptr == NULL){
Yogesh Pande 4:5d91b0f5038c 847 return;
Yogesh Pande 4:5d91b0f5038c 848 }
Yogesh Pande 4:5d91b0f5038c 849 stored_msg_ptr->send_msg_ptr->uri_path_len = uri_path_len;
Yogesh Pande 4:5d91b0f5038c 850 memcpy(stored_msg_ptr->send_msg_ptr->uri_path_ptr, uri_path_ptr, uri_path_len);
Yogesh Pande 4:5d91b0f5038c 851 }
Yogesh Pande 4:5d91b0f5038c 852
Yogesh Pande 4:5d91b0f5038c 853
Christopher Haster 1:43f5c94c6771 854 /* Storing Resending message to Linked list */
Yogesh Pande 4:5d91b0f5038c 855 ns_list_add_to_end(&handle->linked_list_resent_msgs, stored_msg_ptr);
Yogesh Pande 4:5d91b0f5038c 856 ++handle->count_resent_msgs;
Christopher Haster 1:43f5c94c6771 857 }
Christopher Haster 1:43f5c94c6771 858
Christopher Haster 1:43f5c94c6771 859 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 860 * \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)
Christopher Haster 1:43f5c94c6771 861 *
Christopher Haster 1:43f5c94c6771 862 * \brief Searches stored resending message from Linked list
Christopher Haster 1:43f5c94c6771 863 *
Christopher Haster 1:43f5c94c6771 864 * \param *src_addr_ptr is searching key for searched message
Christopher Haster 1:43f5c94c6771 865 *
Christopher Haster 1:43f5c94c6771 866 * \param msg_id is searching key for searched message
Christopher Haster 1:43f5c94c6771 867 *
Christopher Haster 1:43f5c94c6771 868 * \return Return value is pointer to found stored resending message in Linked
Christopher Haster 1:43f5c94c6771 869 * list or NULL if message not found
Christopher Haster 1:43f5c94c6771 870 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 871
Yogesh Pande 4:5d91b0f5038c 872 static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(struct coap_s *handle,
Yogesh Pande 4:5d91b0f5038c 873 sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
Christopher Haster 1:43f5c94c6771 874 {
Christopher Haster 1:43f5c94c6771 875 /* Loop all stored resending messages Linked list */
Yogesh Pande 4:5d91b0f5038c 876 ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) {
Christopher Haster 1:43f5c94c6771 877 /* Get message ID from stored resending message */
Christopher Haster 1:43f5c94c6771 878 uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
Christopher Haster 1:43f5c94c6771 879 temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
Christopher Haster 1:43f5c94c6771 880
Christopher Haster 1:43f5c94c6771 881 /* If message's Message ID is same than is searched */
Christopher Haster 1:43f5c94c6771 882 if (temp_msg_id == msg_id) {
Christopher Haster 1:43f5c94c6771 883 /* If message's Source address is same than is searched */
Christopher Haster 1:43f5c94c6771 884 if (0 == memcmp(src_addr_ptr->addr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, src_addr_ptr->addr_len)) {
Christopher Haster 1:43f5c94c6771 885 /* If message's Source address port is same than is searched */
Christopher Haster 1:43f5c94c6771 886 if (stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port == src_addr_ptr->port) {
Christopher Haster 1:43f5c94c6771 887 /* * * Message found, return pointer to that stored resending message * * * */
Christopher Haster 1:43f5c94c6771 888 return stored_msg_ptr->send_msg_ptr;
Christopher Haster 1:43f5c94c6771 889 }
Christopher Haster 1:43f5c94c6771 890 }
Christopher Haster 1:43f5c94c6771 891 }
Christopher Haster 1:43f5c94c6771 892 }
Christopher Haster 1:43f5c94c6771 893
Christopher Haster 1:43f5c94c6771 894 /* Message not found */
Christopher Haster 1:43f5c94c6771 895 return NULL;
Christopher Haster 1:43f5c94c6771 896 }
Christopher Haster 1:43f5c94c6771 897 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 898 * \fn static void sn_coap_protocol_linked_list_send_msg_remove(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
Christopher Haster 1:43f5c94c6771 899 *
Christopher Haster 1:43f5c94c6771 900 * \brief Removes stored resending message from Linked list
Christopher Haster 1:43f5c94c6771 901 *
Christopher Haster 1:43f5c94c6771 902 * \param *src_addr_ptr is searching key for searched message
Christopher Haster 1:43f5c94c6771 903 * \param msg_id is searching key for removed message
Christopher Haster 1:43f5c94c6771 904 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 905
Christopher Haster 1:43f5c94c6771 906 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)
Christopher Haster 1:43f5c94c6771 907 {
Christopher Haster 1:43f5c94c6771 908 /* Loop all stored resending messages in Linked list */
Yogesh Pande 4:5d91b0f5038c 909 ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) {
Christopher Haster 1:43f5c94c6771 910 /* Get message ID from stored resending message */
Christopher Haster 1:43f5c94c6771 911 uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
Christopher Haster 1:43f5c94c6771 912 temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
Christopher Haster 1:43f5c94c6771 913
Christopher Haster 1:43f5c94c6771 914 /* If message's Message ID is same than is searched */
Christopher Haster 1:43f5c94c6771 915 if (temp_msg_id == msg_id) {
Christopher Haster 1:43f5c94c6771 916 /* If message's Source address is same than is searched */
Christopher Haster 1:43f5c94c6771 917 if (0 == memcmp(src_addr_ptr->addr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, src_addr_ptr->addr_len)) {
Christopher Haster 1:43f5c94c6771 918 /* If message's Source address port is same than is searched */
Christopher Haster 1:43f5c94c6771 919 if (stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port == src_addr_ptr->port) {
Christopher Haster 1:43f5c94c6771 920 /* * * Message found * * */
Christopher Haster 1:43f5c94c6771 921
Christopher Haster 1:43f5c94c6771 922 /* Remove message from Linked list */
Yogesh Pande 4:5d91b0f5038c 923 ns_list_remove(&handle->linked_list_resent_msgs, stored_msg_ptr);
Yogesh Pande 4:5d91b0f5038c 924 --handle->count_resent_msgs;
Christopher Haster 1:43f5c94c6771 925
Christopher Haster 1:43f5c94c6771 926 /* Free memory of stored message */
Christopher Haster 1:43f5c94c6771 927 sn_coap_protocol_release_allocated_send_msg_mem(handle, stored_msg_ptr);
Christopher Haster 1:43f5c94c6771 928
Christopher Haster 1:43f5c94c6771 929 return;
Christopher Haster 1:43f5c94c6771 930 }
Christopher Haster 1:43f5c94c6771 931 }
Christopher Haster 1:43f5c94c6771 932 }
Christopher Haster 1:43f5c94c6771 933 }
Christopher Haster 1:43f5c94c6771 934 }
Christopher Haster 1:43f5c94c6771 935 #endif /* ENABLE_RESENDINGS */
Christopher Haster 1:43f5c94c6771 936
Christopher Haster 1:43f5c94c6771 937
Christopher Haster 1:43f5c94c6771 938 static void sn_coap_protocol_send_rst(struct coap_s *handle, uint16_t msg_id, sn_nsdl_addr_s *addr_ptr, void *param)
Christopher Haster 1:43f5c94c6771 939 {
Christopher Haster 1:43f5c94c6771 940 uint8_t packet_ptr[4];
Christopher Haster 1:43f5c94c6771 941
Christopher Haster 1:43f5c94c6771 942 /* Add CoAP version and message type */
Christopher Haster 1:43f5c94c6771 943 packet_ptr[0] = COAP_VERSION_1;
Christopher Haster 1:43f5c94c6771 944 packet_ptr[0] |= COAP_MSG_TYPE_RESET;
Christopher Haster 1:43f5c94c6771 945
Christopher Haster 1:43f5c94c6771 946 /* Add message code */
Christopher Haster 1:43f5c94c6771 947 packet_ptr[1] = COAP_MSG_CODE_EMPTY;
Christopher Haster 1:43f5c94c6771 948
Christopher Haster 1:43f5c94c6771 949 /* Add message ID */
Christopher Haster 1:43f5c94c6771 950 packet_ptr[2] = msg_id >> 8;
Christopher Haster 1:43f5c94c6771 951 packet_ptr[3] = (uint8_t)msg_id;
Christopher Haster 1:43f5c94c6771 952
Christopher Haster 1:43f5c94c6771 953 /* Send RST */
Christopher Haster 1:43f5c94c6771 954 handle->sn_coap_tx_callback(packet_ptr, 4, addr_ptr, param);
Christopher Haster 1:43f5c94c6771 955
Christopher Haster 1:43f5c94c6771 956 }
Yogesh Pande 4:5d91b0f5038c 957 #if YOTTA_CFG_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 958
Christopher Haster 1:43f5c94c6771 959 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 960 * \fn static void sn_coap_protocol_linked_list_duplication_info_store(sn_nsdl_addr_s *addr_ptr, uint16_t msg_id)
Christopher Haster 1:43f5c94c6771 961 *
Christopher Haster 1:43f5c94c6771 962 * \brief Stores Duplication info to Linked list
Christopher Haster 1:43f5c94c6771 963 *
Christopher Haster 1:43f5c94c6771 964 * \param msg_id is Message ID to be stored
Christopher Haster 1:43f5c94c6771 965 * \param *addr_ptr is pointer to Address information to be stored
Christopher Haster 1:43f5c94c6771 966 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 967
Christopher Haster 1:43f5c94c6771 968 static void sn_coap_protocol_linked_list_duplication_info_store(struct coap_s *handle, sn_nsdl_addr_s *addr_ptr,
Christopher Haster 1:43f5c94c6771 969 uint16_t msg_id)
Christopher Haster 1:43f5c94c6771 970 {
Christopher Haster 1:43f5c94c6771 971 coap_duplication_info_s *stored_duplication_info_ptr = NULL;
Christopher Haster 1:43f5c94c6771 972
Christopher Haster 1:43f5c94c6771 973 /* * * * Allocating memory for stored Duplication info * * * */
Christopher Haster 1:43f5c94c6771 974
Christopher Haster 1:43f5c94c6771 975 /* Allocate memory for stored Duplication info's structure */
Christopher Haster 1:43f5c94c6771 976 stored_duplication_info_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_duplication_info_s));
Christopher Haster 1:43f5c94c6771 977
Christopher Haster 1:43f5c94c6771 978 if (stored_duplication_info_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 979 return;
Christopher Haster 1:43f5c94c6771 980 }
Christopher Haster 1:43f5c94c6771 981
Christopher Haster 1:43f5c94c6771 982 /* Allocate memory for stored Duplication info's address */
Christopher Haster 1:43f5c94c6771 983 stored_duplication_info_ptr->addr_ptr = handle->sn_coap_protocol_malloc(addr_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 984
Christopher Haster 1:43f5c94c6771 985 if (stored_duplication_info_ptr->addr_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 986 handle->sn_coap_protocol_free(stored_duplication_info_ptr);
Christopher Haster 1:43f5c94c6771 987 stored_duplication_info_ptr = 0;
Christopher Haster 1:43f5c94c6771 988 return;
Christopher Haster 1:43f5c94c6771 989 }
Christopher Haster 1:43f5c94c6771 990
Christopher Haster 1:43f5c94c6771 991 /* * * * Filling fields of stored Duplication info * * * */
Christopher Haster 1:43f5c94c6771 992
Yogesh Pande 4:5d91b0f5038c 993 stored_duplication_info_ptr->timestamp = handle->system_time;
Christopher Haster 1:43f5c94c6771 994 stored_duplication_info_ptr->addr_len = addr_ptr->addr_len;
Christopher Haster 1:43f5c94c6771 995 memcpy(stored_duplication_info_ptr->addr_ptr, addr_ptr->addr_ptr, addr_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 996 stored_duplication_info_ptr->port = addr_ptr->port;
Christopher Haster 1:43f5c94c6771 997 stored_duplication_info_ptr->msg_id = msg_id;
Christopher Haster 1:43f5c94c6771 998
Christopher Haster 1:43f5c94c6771 999 stored_duplication_info_ptr->coap = handle;
Christopher Haster 1:43f5c94c6771 1000
Christopher Haster 1:43f5c94c6771 1001 /* * * * Storing Duplication info to Linked list * * * */
Christopher Haster 1:43f5c94c6771 1002
Yogesh Pande 4:5d91b0f5038c 1003 ns_list_add_to_end(&handle->linked_list_duplication_msgs, stored_duplication_info_ptr);
Yogesh Pande 4:5d91b0f5038c 1004 ++handle->count_duplication_msgs;
Christopher Haster 1:43f5c94c6771 1005 }
Christopher Haster 1:43f5c94c6771 1006
Christopher Haster 1:43f5c94c6771 1007 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1008 * \fn static int8_t sn_coap_protocol_linked_list_duplication_info_search(sn_nsdl_addr_s *addr_ptr, uint16_t msg_id)
Christopher Haster 1:43f5c94c6771 1009 *
Christopher Haster 1:43f5c94c6771 1010 * \brief Searches stored message from Linked list (Address and Message ID as key)
Christopher Haster 1:43f5c94c6771 1011 *
Christopher Haster 1:43f5c94c6771 1012 * \param *addr_ptr is pointer to Address key to be searched
Christopher Haster 1:43f5c94c6771 1013 * \param msg_id is Message ID key to be searched
Christopher Haster 1:43f5c94c6771 1014 *
Christopher Haster 1:43f5c94c6771 1015 * \return Return value is 0 when message found and -1 if not found
Christopher Haster 1:43f5c94c6771 1016 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1017
Yogesh Pande 4:5d91b0f5038c 1018 static int8_t sn_coap_protocol_linked_list_duplication_info_search(struct coap_s *handle,
Yogesh Pande 4:5d91b0f5038c 1019 sn_nsdl_addr_s *addr_ptr, uint16_t msg_id)
Christopher Haster 1:43f5c94c6771 1020 {
Christopher Haster 1:43f5c94c6771 1021 /* Loop all nodes in Linked list for searching Message ID */
Yogesh Pande 4:5d91b0f5038c 1022 ns_list_foreach(coap_duplication_info_s, stored_duplication_info_ptr, &handle->linked_list_duplication_msgs) {
Christopher Haster 1:43f5c94c6771 1023 /* If message's Message ID is same than is searched */
Christopher Haster 1:43f5c94c6771 1024 if (stored_duplication_info_ptr->msg_id == msg_id) {
Christopher Haster 1:43f5c94c6771 1025 /* If message's Source address is same than is searched */
Christopher Haster 1:43f5c94c6771 1026 if (0 == memcmp(addr_ptr->addr_ptr, stored_duplication_info_ptr->addr_ptr, addr_ptr->addr_len)) {
Christopher Haster 1:43f5c94c6771 1027 /* If message's Source address port is same than is searched */
Christopher Haster 1:43f5c94c6771 1028 if (stored_duplication_info_ptr->port == addr_ptr->port) {
Christopher Haster 1:43f5c94c6771 1029 /* * * Correct Duplication info found * * * */
Christopher Haster 1:43f5c94c6771 1030 return 0;
Christopher Haster 1:43f5c94c6771 1031 }
Christopher Haster 1:43f5c94c6771 1032 }
Christopher Haster 1:43f5c94c6771 1033 }
Christopher Haster 1:43f5c94c6771 1034 }
Christopher Haster 1:43f5c94c6771 1035
Christopher Haster 1:43f5c94c6771 1036 return -1;
Christopher Haster 1:43f5c94c6771 1037 }
Christopher Haster 1:43f5c94c6771 1038
Christopher Haster 1:43f5c94c6771 1039 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1040 * \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)
Christopher Haster 1:43f5c94c6771 1041 *
Christopher Haster 1:43f5c94c6771 1042 * \brief Removes stored Duplication info from Linked list
Christopher Haster 1:43f5c94c6771 1043 *
Christopher Haster 1:43f5c94c6771 1044 * \param *addr_ptr is pointer to Address key to be removed
Christopher Haster 1:43f5c94c6771 1045 *
Christopher Haster 1:43f5c94c6771 1046 * \param port is Port key to be removed
Christopher Haster 1:43f5c94c6771 1047 *
Christopher Haster 1:43f5c94c6771 1048 * \param msg_id is Message ID key to be removed
Christopher Haster 1:43f5c94c6771 1049 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1050
Christopher Haster 1:43f5c94c6771 1051 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)
Christopher Haster 1:43f5c94c6771 1052 {
Christopher Haster 1:43f5c94c6771 1053 /* Loop all stored duplication messages in Linked list */
Yogesh Pande 4:5d91b0f5038c 1054 ns_list_foreach(coap_duplication_info_s, removed_duplication_info_ptr, &handle->linked_list_duplication_msgs) {
Christopher Haster 1:43f5c94c6771 1055 /* If message's Address is same than is searched */
Christopher Haster 1:43f5c94c6771 1056 if (handle == removed_duplication_info_ptr->coap && 0 == memcmp(addr_ptr, removed_duplication_info_ptr->addr_ptr, removed_duplication_info_ptr->addr_len)) {
Christopher Haster 1:43f5c94c6771 1057 /* If message's Address prt is same than is searched */
Christopher Haster 1:43f5c94c6771 1058 if (removed_duplication_info_ptr->port == port) {
Christopher Haster 1:43f5c94c6771 1059 /* If Message ID is same than is searched */
Christopher Haster 1:43f5c94c6771 1060 if (removed_duplication_info_ptr->msg_id == msg_id) {
Christopher Haster 1:43f5c94c6771 1061 /* * * * Correct Duplication info found, remove it from Linked list * * * */
Yogesh Pande 4:5d91b0f5038c 1062 ns_list_remove(&handle->linked_list_duplication_msgs, removed_duplication_info_ptr);
Yogesh Pande 4:5d91b0f5038c 1063 --handle->count_duplication_msgs;
Christopher Haster 1:43f5c94c6771 1064
Christopher Haster 1:43f5c94c6771 1065 /* Free memory of stored Duplication info */
Christopher Haster 1:43f5c94c6771 1066 handle->sn_coap_protocol_free(removed_duplication_info_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 1067 removed_duplication_info_ptr->addr_ptr = 0;
Christopher Haster 1:43f5c94c6771 1068 handle->sn_coap_protocol_free(removed_duplication_info_ptr);
Christopher Haster 1:43f5c94c6771 1069 removed_duplication_info_ptr = 0;
Christopher Haster 1:43f5c94c6771 1070
Christopher Haster 1:43f5c94c6771 1071 return;
Christopher Haster 1:43f5c94c6771 1072 }
Christopher Haster 1:43f5c94c6771 1073 }
Christopher Haster 1:43f5c94c6771 1074 }
Christopher Haster 1:43f5c94c6771 1075 }
Christopher Haster 1:43f5c94c6771 1076 }
Christopher Haster 1:43f5c94c6771 1077
Christopher Haster 1:43f5c94c6771 1078 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1079 * \fn static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(struct coap_s *handle)
Christopher Haster 1:43f5c94c6771 1080 *
Christopher Haster 1:43f5c94c6771 1081 * \brief Removes old stored Duplication detection infos from Linked list
Christopher Haster 1:43f5c94c6771 1082 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1083
Christopher Haster 1:43f5c94c6771 1084 static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(struct coap_s *handle)
Christopher Haster 1:43f5c94c6771 1085 {
Christopher Haster 1:43f5c94c6771 1086 /* Loop all stored duplication messages in Linked list */
Yogesh Pande 4:5d91b0f5038c 1087 ns_list_foreach_safe(coap_duplication_info_s, removed_duplication_info_ptr, &handle->linked_list_duplication_msgs) {
Yogesh Pande 4:5d91b0f5038c 1088 if ((handle->system_time - removed_duplication_info_ptr->timestamp) > SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED) {
Christopher Haster 1:43f5c94c6771 1089 /* * * * Old Duplication info found, remove it from Linked list * * * */
Yogesh Pande 4:5d91b0f5038c 1090 ns_list_remove(&handle->linked_list_duplication_msgs, removed_duplication_info_ptr);
Yogesh Pande 4:5d91b0f5038c 1091 --handle->count_duplication_msgs;
Christopher Haster 1:43f5c94c6771 1092
Christopher Haster 1:43f5c94c6771 1093 /* Free memory of stored Duplication info */
Christopher Haster 1:43f5c94c6771 1094 handle->sn_coap_protocol_free(removed_duplication_info_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 1095 removed_duplication_info_ptr->addr_ptr = 0;
Christopher Haster 1:43f5c94c6771 1096 handle->sn_coap_protocol_free(removed_duplication_info_ptr);
Christopher Haster 1:43f5c94c6771 1097 removed_duplication_info_ptr = 0;
Christopher Haster 1:43f5c94c6771 1098 }
Christopher Haster 1:43f5c94c6771 1099 }
Christopher Haster 1:43f5c94c6771 1100 }
Christopher Haster 1:43f5c94c6771 1101
Yogesh Pande 4:5d91b0f5038c 1102 #endif /* YOTTA_CFG_COAP_DUPLICATION_MAX_MSGS_COUNT */
Christopher Haster 1:43f5c94c6771 1103
Yogesh Pande 4:5d91b0f5038c 1104 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
Christopher Haster 1:43f5c94c6771 1105 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1106 * \fn static void sn_coap_protocol_linked_list_blockwise_msg_remove(struct coap_s *handle, coap_blockwise_msg_s *removed_msg_ptr)
Christopher Haster 1:43f5c94c6771 1107 *
Christopher Haster 1:43f5c94c6771 1108 * \brief Removes stored blockwise message from Linked list
Christopher Haster 1:43f5c94c6771 1109 *
Christopher Haster 1:43f5c94c6771 1110 * \param removed_msg_ptr is message to be removed
Christopher Haster 1:43f5c94c6771 1111 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1112
Christopher Haster 1:43f5c94c6771 1113 static void sn_coap_protocol_linked_list_blockwise_msg_remove(struct coap_s *handle, coap_blockwise_msg_s *removed_msg_ptr)
Christopher Haster 1:43f5c94c6771 1114 {
Christopher Haster 1:43f5c94c6771 1115 if( removed_msg_ptr->coap == handle ){
Yogesh Pande 4:5d91b0f5038c 1116 ns_list_remove(&handle->linked_list_blockwise_sent_msgs, removed_msg_ptr);
Christopher Haster 1:43f5c94c6771 1117
Christopher Haster 1:43f5c94c6771 1118 if( removed_msg_ptr->coap_msg_ptr ){
Christopher Haster 1:43f5c94c6771 1119 if (removed_msg_ptr->coap_msg_ptr->payload_ptr) {
Christopher Haster 1:43f5c94c6771 1120 handle->sn_coap_protocol_free(removed_msg_ptr->coap_msg_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 1121 removed_msg_ptr->coap_msg_ptr->payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 1122 }
Christopher Haster 1:43f5c94c6771 1123
Christopher Haster 1:43f5c94c6771 1124 sn_coap_parser_release_allocated_coap_msg_mem(handle, removed_msg_ptr->coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1125 }
Christopher Haster 1:43f5c94c6771 1126
Christopher Haster 1:43f5c94c6771 1127 handle->sn_coap_protocol_free(removed_msg_ptr);
Christopher Haster 1:43f5c94c6771 1128 removed_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1129 }
Christopher Haster 1:43f5c94c6771 1130 }
Christopher Haster 1:43f5c94c6771 1131
Christopher Haster 1:43f5c94c6771 1132 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1133 * \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)
Christopher Haster 1:43f5c94c6771 1134 *
Christopher Haster 1:43f5c94c6771 1135 * \brief Stores blockwise payload to Linked list
Christopher Haster 1:43f5c94c6771 1136 *
Christopher Haster 1:43f5c94c6771 1137 * \param *addr_ptr is pointer to Address information to be stored
Christopher Haster 1:43f5c94c6771 1138 * \param stored_payload_len is length of stored Payload
Christopher Haster 1:43f5c94c6771 1139 * \param *stored_payload_ptr is pointer to stored Payload
Christopher Haster 1:43f5c94c6771 1140 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1141
Christopher Haster 1:43f5c94c6771 1142 static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s *handle, sn_nsdl_addr_s *addr_ptr,
Christopher Haster 1:43f5c94c6771 1143 uint16_t stored_payload_len,
Christopher Haster 1:43f5c94c6771 1144 uint8_t *stored_payload_ptr)
Christopher Haster 1:43f5c94c6771 1145 {
Christopher Haster 1:43f5c94c6771 1146 if (!addr_ptr || !stored_payload_len || !stored_payload_ptr) {
Christopher Haster 1:43f5c94c6771 1147 return;
Christopher Haster 1:43f5c94c6771 1148 }
Christopher Haster 1:43f5c94c6771 1149
Christopher Haster 1:43f5c94c6771 1150 coap_blockwise_payload_s *stored_blockwise_payload_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1151
Christopher Haster 1:43f5c94c6771 1152 /* * * * Allocating memory for stored Payload * * * */
Christopher Haster 1:43f5c94c6771 1153
Christopher Haster 1:43f5c94c6771 1154 /* Allocate memory for stored Payload's structure */
Christopher Haster 1:43f5c94c6771 1155 stored_blockwise_payload_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_payload_s));
Christopher Haster 1:43f5c94c6771 1156
Christopher Haster 1:43f5c94c6771 1157 if (stored_blockwise_payload_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1158 return;
Christopher Haster 1:43f5c94c6771 1159 }
Christopher Haster 1:43f5c94c6771 1160
Christopher Haster 1:43f5c94c6771 1161 /* Allocate memory for stored Payload's data */
Christopher Haster 1:43f5c94c6771 1162 stored_blockwise_payload_ptr->payload_ptr = handle->sn_coap_protocol_malloc(stored_payload_len);
Christopher Haster 1:43f5c94c6771 1163
Christopher Haster 1:43f5c94c6771 1164 if (stored_blockwise_payload_ptr->payload_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1165 handle->sn_coap_protocol_free(stored_blockwise_payload_ptr);
Christopher Haster 1:43f5c94c6771 1166 stored_blockwise_payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 1167 return;
Christopher Haster 1:43f5c94c6771 1168 }
Christopher Haster 1:43f5c94c6771 1169
Christopher Haster 1:43f5c94c6771 1170 /* Allocate memory for stored Payload's address */
Christopher Haster 1:43f5c94c6771 1171 stored_blockwise_payload_ptr->addr_ptr = handle->sn_coap_protocol_malloc(addr_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 1172
Christopher Haster 1:43f5c94c6771 1173 if (stored_blockwise_payload_ptr->addr_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1174 handle->sn_coap_protocol_free(stored_blockwise_payload_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 1175 stored_blockwise_payload_ptr->payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 1176 handle->sn_coap_protocol_free(stored_blockwise_payload_ptr);
Christopher Haster 1:43f5c94c6771 1177 stored_blockwise_payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 1178
Christopher Haster 1:43f5c94c6771 1179 return;
Christopher Haster 1:43f5c94c6771 1180 }
Christopher Haster 1:43f5c94c6771 1181
Christopher Haster 1:43f5c94c6771 1182 /* * * * Filling fields of stored Payload * * * */
Christopher Haster 1:43f5c94c6771 1183
Yogesh Pande 4:5d91b0f5038c 1184 stored_blockwise_payload_ptr->timestamp = handle->system_time;
Christopher Haster 1:43f5c94c6771 1185
Christopher Haster 1:43f5c94c6771 1186 memcpy(stored_blockwise_payload_ptr->addr_ptr, addr_ptr->addr_ptr, addr_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 1187 stored_blockwise_payload_ptr->port = addr_ptr->port;
Christopher Haster 1:43f5c94c6771 1188 memcpy(stored_blockwise_payload_ptr->payload_ptr, stored_payload_ptr, stored_payload_len);
Christopher Haster 1:43f5c94c6771 1189 stored_blockwise_payload_ptr->payload_len = stored_payload_len;
Christopher Haster 1:43f5c94c6771 1190
Christopher Haster 1:43f5c94c6771 1191 stored_blockwise_payload_ptr->coap = handle;
Christopher Haster 1:43f5c94c6771 1192
Christopher Haster 1:43f5c94c6771 1193 /* * * * Storing Payload to Linked list * * * */
Christopher Haster 1:43f5c94c6771 1194
Yogesh Pande 4:5d91b0f5038c 1195 ns_list_add_to_end(&handle->linked_list_blockwise_received_payloads, stored_blockwise_payload_ptr);
Christopher Haster 1:43f5c94c6771 1196 }
Christopher Haster 1:43f5c94c6771 1197
Christopher Haster 1:43f5c94c6771 1198 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1199 * \fn static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length)
Christopher Haster 1:43f5c94c6771 1200 *
Christopher Haster 1:43f5c94c6771 1201 * \brief Searches stored blockwise payload from Linked list (Address as key)
Christopher Haster 1:43f5c94c6771 1202 *
Christopher Haster 1:43f5c94c6771 1203 * \param *addr_ptr is pointer to Address key to be searched
Christopher Haster 1:43f5c94c6771 1204 * \param *payload_length is pointer to returned Payload length
Christopher Haster 1:43f5c94c6771 1205 *
Christopher Haster 1:43f5c94c6771 1206 * \return Return value is pointer to found stored blockwise payload in Linked
Christopher Haster 1:43f5c94c6771 1207 * list or NULL if payload not found
Christopher Haster 1:43f5c94c6771 1208 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1209
Yogesh Pande 4:5d91b0f5038c 1210 static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length)
Christopher Haster 1:43f5c94c6771 1211 {
Christopher Haster 1:43f5c94c6771 1212 /* Loop all stored blockwise payloads in Linked list */
Yogesh Pande 4:5d91b0f5038c 1213 ns_list_foreach(coap_blockwise_payload_s, stored_payload_info_ptr, &handle->linked_list_blockwise_received_payloads) {
Christopher Haster 1:43f5c94c6771 1214 /* If payload's Source address is same than is searched */
Christopher Haster 1:43f5c94c6771 1215 if (0 == memcmp(src_addr_ptr->addr_ptr, stored_payload_info_ptr->addr_ptr, src_addr_ptr->addr_len)) {
Christopher Haster 1:43f5c94c6771 1216 /* If payload's Source address port is same than is searched */
Christopher Haster 1:43f5c94c6771 1217 if (stored_payload_info_ptr->port == src_addr_ptr->port) {
Christopher Haster 1:43f5c94c6771 1218 /* * * Correct Payload found * * * */
Christopher Haster 1:43f5c94c6771 1219 *payload_length = stored_payload_info_ptr->payload_len;
Christopher Haster 1:43f5c94c6771 1220
Christopher Haster 1:43f5c94c6771 1221 return stored_payload_info_ptr->payload_ptr;
Christopher Haster 1:43f5c94c6771 1222 }
Christopher Haster 1:43f5c94c6771 1223 }
Christopher Haster 1:43f5c94c6771 1224 }
Christopher Haster 1:43f5c94c6771 1225
Christopher Haster 1:43f5c94c6771 1226 return NULL;
Christopher Haster 1:43f5c94c6771 1227 }
Christopher Haster 1:43f5c94c6771 1228
Christopher Haster 1:43f5c94c6771 1229 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1230 * \fn static void sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(struct coap_s *handle)
Christopher Haster 1:43f5c94c6771 1231 *
Christopher Haster 1:43f5c94c6771 1232 * \brief Removes current stored blockwise paylod from Linked list
Christopher Haster 1:43f5c94c6771 1233 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1234
Christopher Haster 1:43f5c94c6771 1235 static void sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(struct coap_s *handle)
Christopher Haster 1:43f5c94c6771 1236 {
Christopher Haster 1:43f5c94c6771 1237 coap_blockwise_payload_s *removed_payload_ptr;
Christopher Haster 1:43f5c94c6771 1238
Christopher Haster 1:43f5c94c6771 1239 /* Remove oldest node in Linked list*/
Yogesh Pande 4:5d91b0f5038c 1240 removed_payload_ptr = ns_list_get_first(&handle->linked_list_blockwise_received_payloads);
Christopher Haster 1:43f5c94c6771 1241
Christopher Haster 1:43f5c94c6771 1242 if (removed_payload_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 1243 sn_coap_protocol_linked_list_blockwise_payload_remove(handle, removed_payload_ptr);
Christopher Haster 1:43f5c94c6771 1244 }
Christopher Haster 1:43f5c94c6771 1245 }
Christopher Haster 1:43f5c94c6771 1246
Christopher Haster 1:43f5c94c6771 1247 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1248 * \fn static void sn_coap_protocol_linked_list_blockwise_payload_remove(struct coap_s *handle,
Christopher Haster 1:43f5c94c6771 1249 * coap_blockwise_msg_s *removed_msg_ptr)
Christopher Haster 1:43f5c94c6771 1250 *
Christopher Haster 1:43f5c94c6771 1251 * \brief Removes stored blockwise payload from Linked list
Christopher Haster 1:43f5c94c6771 1252 *
Christopher Haster 1:43f5c94c6771 1253 * \param removed_payload_ptr is payload to be removed
Christopher Haster 1:43f5c94c6771 1254 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1255
Christopher Haster 1:43f5c94c6771 1256 static void sn_coap_protocol_linked_list_blockwise_payload_remove(struct coap_s *handle,
Christopher Haster 1:43f5c94c6771 1257 coap_blockwise_payload_s *removed_payload_ptr)
Christopher Haster 1:43f5c94c6771 1258 {
Yogesh Pande 4:5d91b0f5038c 1259 ns_list_remove(&handle->linked_list_blockwise_received_payloads, removed_payload_ptr);
Christopher Haster 1:43f5c94c6771 1260
Christopher Haster 1:43f5c94c6771 1261 /* Free memory of stored payload */
Christopher Haster 1:43f5c94c6771 1262 if (removed_payload_ptr->addr_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 1263 handle->sn_coap_protocol_free(removed_payload_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 1264 removed_payload_ptr->addr_ptr = 0;
Christopher Haster 1:43f5c94c6771 1265 }
Christopher Haster 1:43f5c94c6771 1266
Christopher Haster 1:43f5c94c6771 1267 if (removed_payload_ptr->payload_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 1268 handle->sn_coap_protocol_free(removed_payload_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 1269 removed_payload_ptr->payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 1270 }
Christopher Haster 1:43f5c94c6771 1271
Christopher Haster 1:43f5c94c6771 1272 handle->sn_coap_protocol_free(removed_payload_ptr);
Christopher Haster 1:43f5c94c6771 1273 removed_payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 1274 }
Christopher Haster 1:43f5c94c6771 1275
Christopher Haster 1:43f5c94c6771 1276 /**************************************************************************//**
Yogesh Pande 4:5d91b0f5038c 1277 * \fn static uint32_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(sn_nsdl_addr_s *src_addr_ptr)
Christopher Haster 1:43f5c94c6771 1278 *
Christopher Haster 1:43f5c94c6771 1279 * \brief Counts length of Payloads in Linked list (Address as key)
Christopher Haster 1:43f5c94c6771 1280 *
Christopher Haster 1:43f5c94c6771 1281 * \param *addr_ptr is pointer to Address key
Christopher Haster 1:43f5c94c6771 1282 *
Christopher Haster 1:43f5c94c6771 1283 * \return Return value is length of Payloads as bytes
Christopher Haster 1:43f5c94c6771 1284 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1285
Yogesh Pande 4:5d91b0f5038c 1286 static uint32_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr)
Christopher Haster 1:43f5c94c6771 1287 {
Yogesh Pande 4:5d91b0f5038c 1288 uint32_t ret_whole_payload_len = 0;
Christopher Haster 1:43f5c94c6771 1289 /* Loop all stored blockwise payloads in Linked list */
Yogesh Pande 4:5d91b0f5038c 1290 ns_list_foreach(coap_blockwise_payload_s, searched_payload_info_ptr, &handle->linked_list_blockwise_received_payloads) {
Christopher Haster 1:43f5c94c6771 1291 /* If payload's Source address is same than is searched */
Christopher Haster 1:43f5c94c6771 1292 if (0 == memcmp(src_addr_ptr->addr_ptr, searched_payload_info_ptr->addr_ptr, src_addr_ptr->addr_len)) {
Christopher Haster 1:43f5c94c6771 1293 /* If payload's Source address port is same than is searched */
Christopher Haster 1:43f5c94c6771 1294 if (searched_payload_info_ptr->port == src_addr_ptr->port) {
Christopher Haster 1:43f5c94c6771 1295 /* * * Correct Payload found * * * */
Christopher Haster 1:43f5c94c6771 1296 ret_whole_payload_len += searched_payload_info_ptr->payload_len;
Christopher Haster 1:43f5c94c6771 1297 }
Christopher Haster 1:43f5c94c6771 1298 }
Christopher Haster 1:43f5c94c6771 1299 }
Christopher Haster 1:43f5c94c6771 1300
Christopher Haster 1:43f5c94c6771 1301 return ret_whole_payload_len;
Christopher Haster 1:43f5c94c6771 1302 }
Christopher Haster 1:43f5c94c6771 1303
Christopher Haster 1:43f5c94c6771 1304 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1305 * \fn static void sn_coap_protocol_linked_list_blockwise_remove_old_data(struct coap_s *handle)
Christopher Haster 1:43f5c94c6771 1306 *
Christopher Haster 1:43f5c94c6771 1307 * \brief Removes old stored Blockwise messages and payloads from Linked list
Christopher Haster 1:43f5c94c6771 1308 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1309
Christopher Haster 1:43f5c94c6771 1310 static void sn_coap_protocol_linked_list_blockwise_remove_old_data(struct coap_s *handle)
Christopher Haster 1:43f5c94c6771 1311 {
Christopher Haster 1:43f5c94c6771 1312 /* Loop all stored Blockwise messages in Linked list */
Yogesh Pande 4:5d91b0f5038c 1313 ns_list_foreach_safe(coap_blockwise_msg_s, removed_blocwise_msg_ptr, &handle->linked_list_blockwise_sent_msgs) {
Yogesh Pande 4:5d91b0f5038c 1314 if ((handle->system_time - removed_blocwise_msg_ptr->timestamp) > SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED) {
Christopher Haster 1:43f5c94c6771 1315 //TODO: Check do we need to check handle == removed_blocwise_msg_ptr->coap here?
Christopher Haster 1:43f5c94c6771 1316
Christopher Haster 1:43f5c94c6771 1317 /* * * * Old Blockise message found, remove it from Linked list * * * */
Christopher Haster 1:43f5c94c6771 1318 if( removed_blocwise_msg_ptr->coap_msg_ptr ){
Christopher Haster 1:43f5c94c6771 1319 if(removed_blocwise_msg_ptr->coap_msg_ptr->payload_ptr){
Christopher Haster 1:43f5c94c6771 1320 handle->sn_coap_protocol_free(removed_blocwise_msg_ptr->coap_msg_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 1321 removed_blocwise_msg_ptr->coap_msg_ptr->payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 1322 }
Christopher Haster 1:43f5c94c6771 1323 sn_coap_parser_release_allocated_coap_msg_mem(handle, removed_blocwise_msg_ptr->coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1324 removed_blocwise_msg_ptr->coap_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1325 }
Christopher Haster 1:43f5c94c6771 1326 sn_coap_protocol_linked_list_blockwise_msg_remove(handle, removed_blocwise_msg_ptr);
Christopher Haster 1:43f5c94c6771 1327 }
Christopher Haster 1:43f5c94c6771 1328 }
Christopher Haster 1:43f5c94c6771 1329
Christopher Haster 1:43f5c94c6771 1330 /* Loop all stored Blockwise payloads in Linked list */
Yogesh Pande 4:5d91b0f5038c 1331 ns_list_foreach_safe(coap_blockwise_payload_s, removed_blocwise_payload_ptr, &handle->linked_list_blockwise_received_payloads) {
Yogesh Pande 4:5d91b0f5038c 1332 if ((handle->system_time - removed_blocwise_payload_ptr->timestamp) > SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED) {
Christopher Haster 1:43f5c94c6771 1333 /* * * * Old Blockise payload found, remove it from Linked list * * * */
Christopher Haster 1:43f5c94c6771 1334 sn_coap_protocol_linked_list_blockwise_payload_remove(handle, removed_blocwise_payload_ptr);
Christopher Haster 1:43f5c94c6771 1335 }
Christopher Haster 1:43f5c94c6771 1336 }
Christopher Haster 1:43f5c94c6771 1337 }
Christopher Haster 1:43f5c94c6771 1338
Yogesh Pande 4:5d91b0f5038c 1339 #endif /* YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */
Christopher Haster 1:43f5c94c6771 1340
Christopher Haster 1:43f5c94c6771 1341
Christopher Haster 1:43f5c94c6771 1342 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 1343 /***************************************************************************//**
Christopher Haster 1:43f5c94c6771 1344 * \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)
Christopher Haster 1:43f5c94c6771 1345 *
Christopher Haster 1:43f5c94c6771 1346 * \brief Allocates memory for given message (send or blockwise message)
Christopher Haster 1:43f5c94c6771 1347 *
Christopher Haster 1:43f5c94c6771 1348 * \param *dst_addr_ptr is pointer to destination address where message will be sent
Christopher Haster 1:43f5c94c6771 1349 * \param packet_data_len is length of allocated Packet data
Christopher Haster 1:43f5c94c6771 1350 *
Christopher Haster 1:43f5c94c6771 1351 * \return pointer to allocated struct
Christopher Haster 1:43f5c94c6771 1352 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1353
Christopher Haster 1:43f5c94c6771 1354 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)
Christopher Haster 1:43f5c94c6771 1355 {
Christopher Haster 1:43f5c94c6771 1356
Christopher Haster 1:43f5c94c6771 1357 coap_send_msg_s *msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_send_msg_s));
Christopher Haster 1:43f5c94c6771 1358
Christopher Haster 1:43f5c94c6771 1359 if (msg_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1360 return 0;
Christopher Haster 1:43f5c94c6771 1361 }
Christopher Haster 1:43f5c94c6771 1362
Christopher Haster 1:43f5c94c6771 1363 memset(msg_ptr, 0, sizeof(coap_send_msg_s));
Christopher Haster 1:43f5c94c6771 1364
Christopher Haster 1:43f5c94c6771 1365 msg_ptr->send_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_nsdl_transmit_s));
Christopher Haster 1:43f5c94c6771 1366
Christopher Haster 1:43f5c94c6771 1367 if (msg_ptr->send_msg_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1368 sn_coap_protocol_release_allocated_send_msg_mem(handle, msg_ptr);
Christopher Haster 1:43f5c94c6771 1369 return 0;
Christopher Haster 1:43f5c94c6771 1370 }
Christopher Haster 1:43f5c94c6771 1371
Christopher Haster 1:43f5c94c6771 1372 memset(msg_ptr->send_msg_ptr, 0 , sizeof(sn_nsdl_transmit_s));
Christopher Haster 1:43f5c94c6771 1373
Christopher Haster 1:43f5c94c6771 1374 msg_ptr->send_msg_ptr->dst_addr_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_nsdl_addr_s));
Christopher Haster 1:43f5c94c6771 1375
Christopher Haster 1:43f5c94c6771 1376 if (msg_ptr->send_msg_ptr->dst_addr_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1377 sn_coap_protocol_release_allocated_send_msg_mem(handle, msg_ptr);
Christopher Haster 1:43f5c94c6771 1378 return 0;
Christopher Haster 1:43f5c94c6771 1379 }
Christopher Haster 1:43f5c94c6771 1380
Christopher Haster 1:43f5c94c6771 1381 memset(msg_ptr->send_msg_ptr->dst_addr_ptr, 0, sizeof(sn_nsdl_addr_s));
Christopher Haster 1:43f5c94c6771 1382
Christopher Haster 1:43f5c94c6771 1383 msg_ptr->send_msg_ptr->packet_ptr = handle->sn_coap_protocol_malloc(packet_data_len);
Christopher Haster 1:43f5c94c6771 1384
Christopher Haster 1:43f5c94c6771 1385 if (msg_ptr->send_msg_ptr->packet_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1386 sn_coap_protocol_release_allocated_send_msg_mem(handle, msg_ptr);
Christopher Haster 1:43f5c94c6771 1387 return 0;
Christopher Haster 1:43f5c94c6771 1388 }
Christopher Haster 1:43f5c94c6771 1389
Christopher Haster 1:43f5c94c6771 1390 msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr = handle->sn_coap_protocol_malloc(dst_addr_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 1391
Christopher Haster 1:43f5c94c6771 1392 if (msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1393 sn_coap_protocol_release_allocated_send_msg_mem(handle, msg_ptr);
Christopher Haster 1:43f5c94c6771 1394 return 0;
Christopher Haster 1:43f5c94c6771 1395 }
Christopher Haster 1:43f5c94c6771 1396
Christopher Haster 1:43f5c94c6771 1397 memset(msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, 0, dst_addr_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 1398
Christopher Haster 1:43f5c94c6771 1399 return msg_ptr;
Christopher Haster 1:43f5c94c6771 1400 }
Christopher Haster 1:43f5c94c6771 1401
Christopher Haster 1:43f5c94c6771 1402
Christopher Haster 1:43f5c94c6771 1403 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1404 * \fn static void sn_coap_protocol_release_allocated_send_msg_mem(struct coap_s *handle, coap_send_msg_s *freed_send_msg_ptr)
Christopher Haster 1:43f5c94c6771 1405 *
Christopher Haster 1:43f5c94c6771 1406 * \brief Releases memory of given Sending message (coap_send_msg_s)
Christopher Haster 1:43f5c94c6771 1407 *
Christopher Haster 1:43f5c94c6771 1408 * \param *freed_send_msg_ptr is pointer to released Sending message
Christopher Haster 1:43f5c94c6771 1409 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1410
Christopher Haster 1:43f5c94c6771 1411 static void sn_coap_protocol_release_allocated_send_msg_mem(struct coap_s *handle, coap_send_msg_s *freed_send_msg_ptr)
Christopher Haster 1:43f5c94c6771 1412 {
Christopher Haster 1:43f5c94c6771 1413 if (freed_send_msg_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 1414 if (freed_send_msg_ptr->send_msg_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 1415 if (freed_send_msg_ptr->send_msg_ptr->dst_addr_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 1416 if (freed_send_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 1417 handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 1418 freed_send_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr = 0;
Christopher Haster 1:43f5c94c6771 1419 }
Christopher Haster 1:43f5c94c6771 1420
Christopher Haster 1:43f5c94c6771 1421 handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr->dst_addr_ptr);
Christopher Haster 1:43f5c94c6771 1422 freed_send_msg_ptr->send_msg_ptr->dst_addr_ptr = 0;
Christopher Haster 1:43f5c94c6771 1423 }
Christopher Haster 1:43f5c94c6771 1424
Christopher Haster 1:43f5c94c6771 1425 if (freed_send_msg_ptr->send_msg_ptr->packet_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 1426 handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr->packet_ptr);
Christopher Haster 1:43f5c94c6771 1427 freed_send_msg_ptr->send_msg_ptr->packet_ptr = 0;
Christopher Haster 1:43f5c94c6771 1428 }
Christopher Haster 1:43f5c94c6771 1429
Yogesh Pande 4:5d91b0f5038c 1430 if (freed_send_msg_ptr->send_msg_ptr->uri_path_ptr != NULL) {
Yogesh Pande 4:5d91b0f5038c 1431 handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr->uri_path_ptr);
Yogesh Pande 4:5d91b0f5038c 1432 freed_send_msg_ptr->send_msg_ptr->uri_path_ptr = 0;
Yogesh Pande 4:5d91b0f5038c 1433 }
Yogesh Pande 4:5d91b0f5038c 1434
Christopher Haster 1:43f5c94c6771 1435 handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr);
Christopher Haster 1:43f5c94c6771 1436 freed_send_msg_ptr->send_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1437 }
Christopher Haster 1:43f5c94c6771 1438
Christopher Haster 1:43f5c94c6771 1439 handle->sn_coap_protocol_free(freed_send_msg_ptr);
Christopher Haster 1:43f5c94c6771 1440 freed_send_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1441 }
Christopher Haster 1:43f5c94c6771 1442 }
Christopher Haster 1:43f5c94c6771 1443
Christopher Haster 1:43f5c94c6771 1444 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1445 * \fn static uint16_t sn_coap_count_linked_list_size(const coap_send_msg_list_t *linked_list_ptr)
Christopher Haster 1:43f5c94c6771 1446 *
Christopher Haster 1:43f5c94c6771 1447 * \brief Counts total message size of all messages in linked list
Christopher Haster 1:43f5c94c6771 1448 *
Christopher Haster 1:43f5c94c6771 1449 * \param const coap_send_msg_list_t *linked_list_ptr pointer to linked list
Christopher Haster 1:43f5c94c6771 1450 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1451 static uint16_t sn_coap_count_linked_list_size(const coap_send_msg_list_t *linked_list_ptr)
Christopher Haster 1:43f5c94c6771 1452 {
Christopher Haster 1:43f5c94c6771 1453 uint16_t total_size = 0;
Christopher Haster 1:43f5c94c6771 1454
Christopher Haster 1:43f5c94c6771 1455 ns_list_foreach(coap_send_msg_s, stored_msg_ptr, linked_list_ptr) {
Christopher Haster 1:43f5c94c6771 1456 if (stored_msg_ptr->send_msg_ptr) {
Christopher Haster 1:43f5c94c6771 1457 total_size += stored_msg_ptr->send_msg_ptr->packet_len;
Christopher Haster 1:43f5c94c6771 1458 }
Christopher Haster 1:43f5c94c6771 1459 }
Christopher Haster 1:43f5c94c6771 1460
Christopher Haster 1:43f5c94c6771 1461 return total_size;
Christopher Haster 1:43f5c94c6771 1462 }
Christopher Haster 1:43f5c94c6771 1463
Christopher Haster 1:43f5c94c6771 1464 #endif
Yogesh Pande 4:5d91b0f5038c 1465 #if YOTTA_CFG_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
Christopher Haster 1:43f5c94c6771 1466
Christopher Haster 1:43f5c94c6771 1467 /**************************************************************************//**
Christopher Haster 1:43f5c94c6771 1468 * \fn static int8_t sn_coap_handle_blockwise_message(void)
Christopher Haster 1:43f5c94c6771 1469 *
Christopher Haster 1:43f5c94c6771 1470 * \brief Handles all received blockwise messages
Christopher Haster 1:43f5c94c6771 1471 *
Christopher Haster 1:43f5c94c6771 1472 * \param *src_addr_ptr pointer to source address information struct
Christopher Haster 1:43f5c94c6771 1473 * \param *received_coap_msg_ptr pointer to parsed CoAP message structure
Christopher Haster 1:43f5c94c6771 1474 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 1475
Christopher Haster 1:43f5c94c6771 1476 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)
Christopher Haster 1:43f5c94c6771 1477 {
Christopher Haster 1:43f5c94c6771 1478 sn_coap_hdr_s *src_coap_blockwise_ack_msg_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1479 uint16_t dst_packed_data_needed_mem = 0;
Christopher Haster 1:43f5c94c6771 1480 uint8_t *dst_ack_packet_data_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1481 uint8_t block_temp = 0;
Christopher Haster 1:43f5c94c6771 1482
Christopher Haster 1:43f5c94c6771 1483 uint16_t original_payload_len = 0;
Christopher Haster 1:43f5c94c6771 1484 uint8_t *original_payload_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1485
Christopher Haster 1:43f5c94c6771 1486 /* Block1 Option in a request (e.g., PUT or POST) */
Christopher Haster 1:43f5c94c6771 1487 // Blocked request sending, received ACK, sending next block..
Christopher Haster 1:43f5c94c6771 1488 if (received_coap_msg_ptr->options_list_ptr->block1_ptr) {
Christopher Haster 1:43f5c94c6771 1489 if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) {
Christopher Haster 1:43f5c94c6771 1490 if (*(received_coap_msg_ptr->options_list_ptr->block1_ptr + (received_coap_msg_ptr->options_list_ptr->block1_len - 1)) & 0x08) {
Christopher Haster 1:43f5c94c6771 1491 coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1492
Christopher Haster 1:43f5c94c6771 1493 /* Get */
Yogesh Pande 4:5d91b0f5038c 1494 ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) {
Christopher Haster 1:43f5c94c6771 1495 if (msg->coap_msg_ptr && received_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) {
Christopher Haster 1:43f5c94c6771 1496 stored_blockwise_msg_temp_ptr = msg;
Christopher Haster 1:43f5c94c6771 1497 break;
Christopher Haster 1:43f5c94c6771 1498 }
Christopher Haster 1:43f5c94c6771 1499 }
Christopher Haster 1:43f5c94c6771 1500
Christopher Haster 1:43f5c94c6771 1501 if (stored_blockwise_msg_temp_ptr) {
Christopher Haster 1:43f5c94c6771 1502 /* Build response message */
Christopher Haster 1:43f5c94c6771 1503
Christopher Haster 1:43f5c94c6771 1504 uint16_t block_size = 1;
Christopher Haster 1:43f5c94c6771 1505 uint32_t block_number = 0;
Christopher Haster 1:43f5c94c6771 1506
Christopher Haster 1:43f5c94c6771 1507 /* Get block option parameters from received message */
Christopher Haster 1:43f5c94c6771 1508 if (received_coap_msg_ptr->options_list_ptr->block1_len == 3) {
Christopher Haster 1:43f5c94c6771 1509 block_number = *(received_coap_msg_ptr->options_list_ptr->block1_ptr) << 12;
Christopher Haster 1:43f5c94c6771 1510 block_number |= *(received_coap_msg_ptr->options_list_ptr->block1_ptr + 1) << 4;
Christopher Haster 1:43f5c94c6771 1511 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block1_ptr + 2)) >> 4;
Christopher Haster 1:43f5c94c6771 1512 }
Christopher Haster 1:43f5c94c6771 1513
Christopher Haster 1:43f5c94c6771 1514 else if (received_coap_msg_ptr->options_list_ptr->block1_len == 2) {
Christopher Haster 1:43f5c94c6771 1515 block_number = *(received_coap_msg_ptr->options_list_ptr->block1_ptr) << 4;
Christopher Haster 1:43f5c94c6771 1516 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block1_ptr + 1)) >> 4;
Christopher Haster 1:43f5c94c6771 1517 } else if (received_coap_msg_ptr->options_list_ptr->block1_len == 1) {
Christopher Haster 1:43f5c94c6771 1518 block_number = (*received_coap_msg_ptr->options_list_ptr->block1_ptr) >> 4;
Christopher Haster 1:43f5c94c6771 1519 }
Christopher Haster 1:43f5c94c6771 1520
Christopher Haster 1:43f5c94c6771 1521
Christopher Haster 1:43f5c94c6771 1522 block_temp = *(received_coap_msg_ptr->options_list_ptr->block1_ptr + (received_coap_msg_ptr->options_list_ptr->block1_len - 1)) & 0x07;
Christopher Haster 1:43f5c94c6771 1523 block_size = block_size << (block_temp + 4);
Christopher Haster 1:43f5c94c6771 1524
Christopher Haster 1:43f5c94c6771 1525
Christopher Haster 1:43f5c94c6771 1526 /* Build next block message */
Christopher Haster 1:43f5c94c6771 1527 src_coap_blockwise_ack_msg_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr;
Christopher Haster 1:43f5c94c6771 1528
Christopher Haster 1:43f5c94c6771 1529 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr) {
Christopher Haster 1:43f5c94c6771 1530 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr) {
Christopher Haster 1:43f5c94c6771 1531 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr);
Christopher Haster 1:43f5c94c6771 1532 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr = 0;
Christopher Haster 1:43f5c94c6771 1533
Christopher Haster 1:43f5c94c6771 1534 }
Christopher Haster 1:43f5c94c6771 1535 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr) {
Christopher Haster 1:43f5c94c6771 1536 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
Christopher Haster 1:43f5c94c6771 1537 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = 0;
Christopher Haster 1:43f5c94c6771 1538 }
Christopher Haster 1:43f5c94c6771 1539 } else {
Christopher Haster 1:43f5c94c6771 1540 src_coap_blockwise_ack_msg_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 1541 if (!src_coap_blockwise_ack_msg_ptr->options_list_ptr) {
Christopher Haster 1:43f5c94c6771 1542 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1543 return 0;
Christopher Haster 1:43f5c94c6771 1544 }
Christopher Haster 1:43f5c94c6771 1545 memset(src_coap_blockwise_ack_msg_ptr->options_list_ptr, 0, (sizeof(sn_coap_options_list_s)));
Christopher Haster 1:43f5c94c6771 1546 }
Christopher Haster 1:43f5c94c6771 1547
Christopher Haster 1:43f5c94c6771 1548 block_number++;
Christopher Haster 1:43f5c94c6771 1549
Christopher Haster 1:43f5c94c6771 1550 if (block_number <= 0x0f) {
Christopher Haster 1:43f5c94c6771 1551 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len = 1;
Christopher Haster 1:43f5c94c6771 1552 } else if (block_number <= 0x0fff) {
Christopher Haster 1:43f5c94c6771 1553 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len = 2;
Christopher Haster 1:43f5c94c6771 1554 } else if (block_number <= 0x0fffff) {
Christopher Haster 1:43f5c94c6771 1555 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len = 3;
Christopher Haster 1:43f5c94c6771 1556 }
Christopher Haster 1:43f5c94c6771 1557
Christopher Haster 1:43f5c94c6771 1558 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);
Christopher Haster 1:43f5c94c6771 1559
Christopher Haster 1:43f5c94c6771 1560 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr == 0) {
Christopher Haster 1:43f5c94c6771 1561 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1562 return 0;
Christopher Haster 1:43f5c94c6771 1563 }
Christopher Haster 1:43f5c94c6771 1564
Christopher Haster 1:43f5c94c6771 1565 *(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;
Christopher Haster 1:43f5c94c6771 1566 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr |= block_number << 4;
Christopher Haster 1:43f5c94c6771 1567
Christopher Haster 1:43f5c94c6771 1568
Christopher Haster 1:43f5c94c6771 1569 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len == 3) {
Christopher Haster 1:43f5c94c6771 1570 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + 2) = block_number << 4;
Christopher Haster 1:43f5c94c6771 1571 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + 1) |= block_number >> 4;
Christopher Haster 1:43f5c94c6771 1572 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr |= block_number >> 12;
Christopher Haster 1:43f5c94c6771 1573 } else if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len == 2) {
Christopher Haster 1:43f5c94c6771 1574 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + 1) |= block_number << 4;
Christopher Haster 1:43f5c94c6771 1575 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr |= block_number >> 4;
Christopher Haster 1:43f5c94c6771 1576 } else {
Christopher Haster 1:43f5c94c6771 1577 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr |= block_number << 4;
Christopher Haster 1:43f5c94c6771 1578 }
Christopher Haster 1:43f5c94c6771 1579
Christopher Haster 1:43f5c94c6771 1580 original_payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len;
Christopher Haster 1:43f5c94c6771 1581 original_payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr;
Christopher Haster 1:43f5c94c6771 1582
Christopher Haster 1:43f5c94c6771 1583 if ((block_size * (block_number + 1)) > stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len) {
Christopher Haster 1:43f5c94c6771 1584 src_coap_blockwise_ack_msg_ptr->payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len - (block_size * (block_number));
Christopher Haster 1:43f5c94c6771 1585 src_coap_blockwise_ack_msg_ptr->payload_ptr = src_coap_blockwise_ack_msg_ptr->payload_ptr + (block_size * block_number);
Christopher Haster 1:43f5c94c6771 1586 }
Christopher Haster 1:43f5c94c6771 1587
Christopher Haster 1:43f5c94c6771 1588 /* Not last block */
Christopher Haster 1:43f5c94c6771 1589 else {
Christopher Haster 1:43f5c94c6771 1590 /* set more - bit */
Christopher Haster 1:43f5c94c6771 1591 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len - 1)) |= 0x08;
Christopher Haster 1:43f5c94c6771 1592 src_coap_blockwise_ack_msg_ptr->payload_len = block_size;
Christopher Haster 1:43f5c94c6771 1593 src_coap_blockwise_ack_msg_ptr->payload_ptr = src_coap_blockwise_ack_msg_ptr->payload_ptr + (block_size * block_number);
Christopher Haster 1:43f5c94c6771 1594 }
Christopher Haster 1:43f5c94c6771 1595
Christopher Haster 1:43f5c94c6771 1596 /* Build and send block message */
Yogesh Pande 4:5d91b0f5038c 1597 dst_packed_data_needed_mem = sn_coap_builder_calc_needed_packet_data_size_2(src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 1598
Christopher Haster 1:43f5c94c6771 1599 dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
Christopher Haster 1:43f5c94c6771 1600 if (!dst_ack_packet_data_ptr) {
Christopher Haster 1:43f5c94c6771 1601 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr);
Christopher Haster 1:43f5c94c6771 1602 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr = 0;
Christopher Haster 1:43f5c94c6771 1603 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 1604 src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 1605 handle->sn_coap_protocol_free(original_payload_ptr);
Christopher Haster 1:43f5c94c6771 1606 original_payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 1607 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 1608 src_coap_blockwise_ack_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1609 stored_blockwise_msg_temp_ptr->coap_msg_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1610 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1611 return NULL;
Christopher Haster 1:43f5c94c6771 1612 }
Christopher Haster 1:43f5c94c6771 1613 src_coap_blockwise_ack_msg_ptr->msg_id = message_id++;
Christopher Haster 1:43f5c94c6771 1614 if (message_id == 0) {
Christopher Haster 1:43f5c94c6771 1615 message_id = 1;
Christopher Haster 1:43f5c94c6771 1616 }
Christopher Haster 1:43f5c94c6771 1617
Yogesh Pande 4:5d91b0f5038c 1618 sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 1619
Christopher Haster 1:43f5c94c6771 1620 handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
Christopher Haster 1:43f5c94c6771 1621
Christopher Haster 1:43f5c94c6771 1622 handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
Christopher Haster 1:43f5c94c6771 1623 dst_ack_packet_data_ptr = 0;
Christopher Haster 1:43f5c94c6771 1624
Christopher Haster 1:43f5c94c6771 1625 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len = original_payload_len;
Christopher Haster 1:43f5c94c6771 1626 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = original_payload_ptr;
Christopher Haster 1:43f5c94c6771 1627
Christopher Haster 1:43f5c94c6771 1628 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_ACK;
Christopher Haster 1:43f5c94c6771 1629 }
Christopher Haster 1:43f5c94c6771 1630 } else {
Christopher Haster 1:43f5c94c6771 1631 // XXX what was this trying to free?
Christopher Haster 1:43f5c94c6771 1632 //sn_coap_protocol_linked_list_blockwise_msg_remove_current();
Christopher Haster 1:43f5c94c6771 1633 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_ACK;
Christopher Haster 1:43f5c94c6771 1634 }
Christopher Haster 1:43f5c94c6771 1635 }
Christopher Haster 1:43f5c94c6771 1636
Christopher Haster 1:43f5c94c6771 1637 // Blocked request receiving
Christopher Haster 1:43f5c94c6771 1638 else {
Yogesh Pande 4:5d91b0f5038c 1639 if (received_coap_msg_ptr->payload_len > handle->sn_coap_block_data_size) {
Yogesh Pande 4:5d91b0f5038c 1640 received_coap_msg_ptr->payload_len = handle->sn_coap_block_data_size;
Christopher Haster 1:43f5c94c6771 1641 }
Christopher Haster 1:43f5c94c6771 1642
Christopher Haster 1:43f5c94c6771 1643 sn_coap_protocol_linked_list_blockwise_payload_store(handle, src_addr_ptr, received_coap_msg_ptr->payload_len, received_coap_msg_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 1644 /* If not last block (more value is set) */
Christopher Haster 1:43f5c94c6771 1645 /* 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. */
Christopher Haster 1:43f5c94c6771 1646 if (*(received_coap_msg_ptr->options_list_ptr->block1_ptr + (received_coap_msg_ptr->options_list_ptr->block1_len - 1)) & 0x08) {
Christopher Haster 1:43f5c94c6771 1647 //send ack
Christopher Haster 1:43f5c94c6771 1648 src_coap_blockwise_ack_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 1649
Christopher Haster 1:43f5c94c6771 1650 if (src_coap_blockwise_ack_msg_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1651 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1652 return NULL;
Christopher Haster 1:43f5c94c6771 1653 }
Christopher Haster 1:43f5c94c6771 1654
Christopher Haster 1:43f5c94c6771 1655 memset(src_coap_blockwise_ack_msg_ptr, 0, sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 1656
Christopher Haster 1:43f5c94c6771 1657 src_coap_blockwise_ack_msg_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 1658
Christopher Haster 1:43f5c94c6771 1659 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1660 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 1661 src_coap_blockwise_ack_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1662 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1663 return NULL;
Christopher Haster 1:43f5c94c6771 1664 }
Christopher Haster 1:43f5c94c6771 1665
Christopher Haster 1:43f5c94c6771 1666 memset(src_coap_blockwise_ack_msg_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 1667
Christopher Haster 1:43f5c94c6771 1668 if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
Christopher Haster 1:43f5c94c6771 1669 src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
Christopher Haster 1:43f5c94c6771 1670 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) {
Christopher Haster 1:43f5c94c6771 1671 src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CREATED;
Christopher Haster 1:43f5c94c6771 1672 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) {
Christopher Haster 1:43f5c94c6771 1673 src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
Christopher Haster 1:43f5c94c6771 1674 } else if (received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) {
Christopher Haster 1:43f5c94c6771 1675 src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_DELETED;
Christopher Haster 1:43f5c94c6771 1676 }
Christopher Haster 1:43f5c94c6771 1677
Christopher Haster 1:43f5c94c6771 1678 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len = received_coap_msg_ptr->options_list_ptr->block1_len;
Christopher Haster 1:43f5c94c6771 1679 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);
Christopher Haster 1:43f5c94c6771 1680 if (!src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr) {
Christopher Haster 1:43f5c94c6771 1681 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 1682 src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 1683 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 1684 src_coap_blockwise_ack_msg_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1685 return NULL;
Christopher Haster 1:43f5c94c6771 1686 }
Christopher Haster 1:43f5c94c6771 1687
Christopher Haster 1:43f5c94c6771 1688 src_coap_blockwise_ack_msg_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
Christopher Haster 1:43f5c94c6771 1689
Christopher Haster 1:43f5c94c6771 1690 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);
Christopher Haster 1:43f5c94c6771 1691
Christopher Haster 1:43f5c94c6771 1692 /* Check block size */
Christopher Haster 1:43f5c94c6771 1693 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);
Yogesh Pande 4:5d91b0f5038c 1694 if (block_temp > sn_coap_convert_block_size(handle->sn_coap_block_data_size)) {
Christopher Haster 1:43f5c94c6771 1695 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len - 1)) &= 0xF8;
Yogesh Pande 4:5d91b0f5038c 1696 *(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(handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 1697 }
Christopher Haster 1:43f5c94c6771 1698
Christopher Haster 1:43f5c94c6771 1699 src_coap_blockwise_ack_msg_ptr->msg_id = received_coap_msg_ptr->msg_id;
Christopher Haster 1:43f5c94c6771 1700
Yogesh Pande 4:5d91b0f5038c 1701 dst_packed_data_needed_mem = sn_coap_builder_calc_needed_packet_data_size_2(src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 1702
Christopher Haster 1:43f5c94c6771 1703 dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
Christopher Haster 1:43f5c94c6771 1704 if (!dst_ack_packet_data_ptr) {
Christopher Haster 1:43f5c94c6771 1705 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1706 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr);
Christopher Haster 1:43f5c94c6771 1707 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr = 0;
Christopher Haster 1:43f5c94c6771 1708 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 1709 src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 1710 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 1711 src_coap_blockwise_ack_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1712 return NULL;
Christopher Haster 1:43f5c94c6771 1713 }
Christopher Haster 1:43f5c94c6771 1714
Yogesh Pande 4:5d91b0f5038c 1715 sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 1716
Christopher Haster 1:43f5c94c6771 1717 handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
Christopher Haster 1:43f5c94c6771 1718
Christopher Haster 1:43f5c94c6771 1719 sn_coap_parser_release_allocated_coap_msg_mem(handle, src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 1720 handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
Christopher Haster 1:43f5c94c6771 1721 dst_ack_packet_data_ptr = 0;
Christopher Haster 1:43f5c94c6771 1722
Christopher Haster 1:43f5c94c6771 1723 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING;
Christopher Haster 1:43f5c94c6771 1724
Christopher Haster 1:43f5c94c6771 1725 } else {
Christopher Haster 1:43f5c94c6771 1726 /* * * This is the last block when whole Blockwise payload from received * * */
Christopher Haster 1:43f5c94c6771 1727 /* * * blockwise messages is gathered and returned to User * * */
Christopher Haster 1:43f5c94c6771 1728
Christopher Haster 1:43f5c94c6771 1729 /* Store last Blockwise payload to Linked list */
Christopher Haster 1:43f5c94c6771 1730 uint16_t payload_len = 0;
Yogesh Pande 4:5d91b0f5038c 1731 uint8_t *payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
Yogesh Pande 4:5d91b0f5038c 1732 uint32_t whole_payload_len = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr);
Christopher Haster 1:43f5c94c6771 1733 uint8_t *temp_whole_payload_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1734
Christopher Haster 1:43f5c94c6771 1735 temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len);
Yogesh Pande 4:5d91b0f5038c 1736 if (temp_whole_payload_ptr == NULL || whole_payload_len > UINT16_MAX) {
Christopher Haster 1:43f5c94c6771 1737 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Yogesh Pande 4:5d91b0f5038c 1738 handle->sn_coap_protocol_free(temp_whole_payload_ptr);
Christopher Haster 1:43f5c94c6771 1739 return 0;
Christopher Haster 1:43f5c94c6771 1740 }
Christopher Haster 1:43f5c94c6771 1741
Yogesh Pande 4:5d91b0f5038c 1742 // In block message case, payload_ptr freeing must be done in application level
Christopher Haster 1:43f5c94c6771 1743 received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr;
Christopher Haster 1:43f5c94c6771 1744 received_coap_msg_ptr->payload_len = whole_payload_len;
Christopher Haster 1:43f5c94c6771 1745
Christopher Haster 1:43f5c94c6771 1746 /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */
Christopher Haster 1:43f5c94c6771 1747 while (payload_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 1748 memcpy(temp_whole_payload_ptr, payload_ptr, payload_len);
Christopher Haster 1:43f5c94c6771 1749
Christopher Haster 1:43f5c94c6771 1750 temp_whole_payload_ptr += payload_len;
Christopher Haster 1:43f5c94c6771 1751
Christopher Haster 1:43f5c94c6771 1752 sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle);
Yogesh Pande 4:5d91b0f5038c 1753 payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
Christopher Haster 1:43f5c94c6771 1754 }
Christopher Haster 1:43f5c94c6771 1755 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED;
Christopher Haster 1:43f5c94c6771 1756 }
Christopher Haster 1:43f5c94c6771 1757 }
Christopher Haster 1:43f5c94c6771 1758 }
Christopher Haster 1:43f5c94c6771 1759
Christopher Haster 1:43f5c94c6771 1760
Christopher Haster 1:43f5c94c6771 1761 /* Block2 Option in a response (e.g., a 2.05 response for GET) */
Christopher Haster 1:43f5c94c6771 1762 /* Message ID must be same than in received message */
Christopher Haster 1:43f5c94c6771 1763 else {
Christopher Haster 1:43f5c94c6771 1764 //This is response to request we made
Christopher Haster 1:43f5c94c6771 1765 if (received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) {
Christopher Haster 1:43f5c94c6771 1766 uint32_t block_number = 0;
Christopher Haster 1:43f5c94c6771 1767
Christopher Haster 1:43f5c94c6771 1768 /* Store blockwise payload to Linked list */
Christopher Haster 1:43f5c94c6771 1769 //todo: add block number to stored values - just to make sure all packets are in order
Christopher Haster 1:43f5c94c6771 1770 sn_coap_protocol_linked_list_blockwise_payload_store(handle, src_addr_ptr, received_coap_msg_ptr->payload_len, received_coap_msg_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 1771
Christopher Haster 1:43f5c94c6771 1772 /* If not last block (more value is set) */
Christopher Haster 1:43f5c94c6771 1773 if (*(received_coap_msg_ptr->options_list_ptr->block2_ptr + (received_coap_msg_ptr->options_list_ptr->block2_len - 1)) & 0x08) {
Christopher Haster 1:43f5c94c6771 1774 coap_blockwise_msg_s *previous_blockwise_msg_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1775
Christopher Haster 1:43f5c94c6771 1776 //build and send ack
Christopher Haster 1:43f5c94c6771 1777 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING;
Christopher Haster 1:43f5c94c6771 1778
Yogesh Pande 4:5d91b0f5038c 1779 ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) {
Christopher Haster 1:43f5c94c6771 1780 if (received_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) {
Christopher Haster 1:43f5c94c6771 1781 previous_blockwise_msg_ptr = msg;
Christopher Haster 1:43f5c94c6771 1782 break;
Christopher Haster 1:43f5c94c6771 1783 }
Christopher Haster 1:43f5c94c6771 1784 }
Christopher Haster 1:43f5c94c6771 1785
Christopher Haster 1:43f5c94c6771 1786 if (!previous_blockwise_msg_ptr || !previous_blockwise_msg_ptr->coap_msg_ptr) {
Christopher Haster 1:43f5c94c6771 1787 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1788 return 0;
Christopher Haster 1:43f5c94c6771 1789 }
Christopher Haster 1:43f5c94c6771 1790
Christopher Haster 1:43f5c94c6771 1791 src_coap_blockwise_ack_msg_ptr = (sn_coap_hdr_s*)handle->sn_coap_protocol_malloc(sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 1792 if( !src_coap_blockwise_ack_msg_ptr ){
Christopher Haster 1:43f5c94c6771 1793 return 0;
Christopher Haster 1:43f5c94c6771 1794 }
Christopher Haster 1:43f5c94c6771 1795 memset(src_coap_blockwise_ack_msg_ptr,0,sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 1796
Yogesh Pande 4:5d91b0f5038c 1797 ns_list_remove(&handle->linked_list_blockwise_sent_msgs, previous_blockwise_msg_ptr);
Christopher Haster 1:43f5c94c6771 1798 if( previous_blockwise_msg_ptr->coap_msg_ptr ){
Christopher Haster 1:43f5c94c6771 1799 if(previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr){
Christopher Haster 1:43f5c94c6771 1800 handle->sn_coap_protocol_free(previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 1801 previous_blockwise_msg_ptr->coap_msg_ptr->payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 1802 }
Christopher Haster 1:43f5c94c6771 1803 sn_coap_parser_release_allocated_coap_msg_mem(handle, previous_blockwise_msg_ptr->coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1804 previous_blockwise_msg_ptr->coap_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1805 }
Christopher Haster 1:43f5c94c6771 1806 handle->sn_coap_protocol_free(previous_blockwise_msg_ptr);
Christopher Haster 1:43f5c94c6771 1807 previous_blockwise_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1808
Christopher Haster 1:43f5c94c6771 1809 // if (src_coap_blockwise_ack_msg_ptr->payload_ptr) {
Christopher Haster 1:43f5c94c6771 1810
Christopher Haster 1:43f5c94c6771 1811 // src_coap_blockwise_ack_msg_ptr->payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 1812 // src_coap_blockwise_ack_msg_ptr->payload_len = 0;
Christopher Haster 1:43f5c94c6771 1813 // }
Christopher Haster 1:43f5c94c6771 1814
Christopher Haster 1:43f5c94c6771 1815 /* * * Then build CoAP Acknowledgement message * * */
Christopher Haster 1:43f5c94c6771 1816
Christopher Haster 1:43f5c94c6771 1817 src_coap_blockwise_ack_msg_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 1818 if (!src_coap_blockwise_ack_msg_ptr->options_list_ptr) {
Christopher Haster 1:43f5c94c6771 1819 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1820 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 1821 src_coap_blockwise_ack_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1822 return 0;
Christopher Haster 1:43f5c94c6771 1823 }
Christopher Haster 1:43f5c94c6771 1824 memset(src_coap_blockwise_ack_msg_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 1825
Christopher Haster 1:43f5c94c6771 1826 src_coap_blockwise_ack_msg_ptr->msg_id = message_id++;
Christopher Haster 1:43f5c94c6771 1827 if (message_id == 0) {
Christopher Haster 1:43f5c94c6771 1828 message_id = 1;
Christopher Haster 1:43f5c94c6771 1829 }
Christopher Haster 1:43f5c94c6771 1830
Christopher Haster 1:43f5c94c6771 1831 // if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr) {
Christopher Haster 1:43f5c94c6771 1832 // handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
Christopher Haster 1:43f5c94c6771 1833 // src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = 0;
Christopher Haster 1:43f5c94c6771 1834 // }
Christopher Haster 1:43f5c94c6771 1835
Christopher Haster 1:43f5c94c6771 1836 /* Update block option */
Christopher Haster 1:43f5c94c6771 1837 block_temp = *(received_coap_msg_ptr->options_list_ptr->block2_ptr + (received_coap_msg_ptr->options_list_ptr->block2_len - 1)) & 0x07;
Christopher Haster 1:43f5c94c6771 1838
Christopher Haster 1:43f5c94c6771 1839 if (received_coap_msg_ptr->options_list_ptr->block2_len == 3) {
Christopher Haster 1:43f5c94c6771 1840 block_number = *(received_coap_msg_ptr->options_list_ptr->block2_ptr) << 12;
Christopher Haster 1:43f5c94c6771 1841 block_number |= *(received_coap_msg_ptr->options_list_ptr->block2_ptr + 1) << 4;
Christopher Haster 1:43f5c94c6771 1842 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block2_ptr + 2)) >> 4;
Christopher Haster 1:43f5c94c6771 1843 }
Christopher Haster 1:43f5c94c6771 1844
Christopher Haster 1:43f5c94c6771 1845 else if (received_coap_msg_ptr->options_list_ptr->block2_len == 2) {
Christopher Haster 1:43f5c94c6771 1846 block_number = *(received_coap_msg_ptr->options_list_ptr->block2_ptr) << 4;
Christopher Haster 1:43f5c94c6771 1847 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block2_ptr + 1)) >> 4;
Christopher Haster 1:43f5c94c6771 1848 } else if (received_coap_msg_ptr->options_list_ptr->block2_len == 1) {
Christopher Haster 1:43f5c94c6771 1849 block_number = (*received_coap_msg_ptr->options_list_ptr->block2_ptr) >> 4;
Christopher Haster 1:43f5c94c6771 1850 }
Christopher Haster 1:43f5c94c6771 1851
Christopher Haster 1:43f5c94c6771 1852 block_number ++;
Christopher Haster 1:43f5c94c6771 1853
Christopher Haster 1:43f5c94c6771 1854 if (block_number <= 0x0f) {
Christopher Haster 1:43f5c94c6771 1855 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len = 1;
Christopher Haster 1:43f5c94c6771 1856 } else if (block_number <= 0x0fff) {
Christopher Haster 1:43f5c94c6771 1857 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len = 2;
Christopher Haster 1:43f5c94c6771 1858 } else if (block_number <= 0x0fffff) {
Christopher Haster 1:43f5c94c6771 1859 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len = 3;
Christopher Haster 1:43f5c94c6771 1860 }
Christopher Haster 1:43f5c94c6771 1861
Christopher Haster 1:43f5c94c6771 1862 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);
Christopher Haster 1:43f5c94c6771 1863
Christopher Haster 1:43f5c94c6771 1864 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr == 0) {
Christopher Haster 1:43f5c94c6771 1865 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 1866 src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 1867 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 1868 src_coap_blockwise_ack_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1869 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1870 return 0;
Christopher Haster 1:43f5c94c6771 1871 }
Christopher Haster 1:43f5c94c6771 1872
Christopher Haster 1:43f5c94c6771 1873 *(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;
Christopher Haster 1:43f5c94c6771 1874
Christopher Haster 1:43f5c94c6771 1875 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len == 3) {
Christopher Haster 1:43f5c94c6771 1876 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr + 2) = block_number << 4;
Christopher Haster 1:43f5c94c6771 1877 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr + 1) |= block_number >> 4;
Christopher Haster 1:43f5c94c6771 1878 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr |= block_number >> 12;
Christopher Haster 1:43f5c94c6771 1879 } else if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len == 2) {
Christopher Haster 1:43f5c94c6771 1880 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr + 1) = block_number << 4;
Christopher Haster 1:43f5c94c6771 1881 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr |= block_number >> 4;
Christopher Haster 1:43f5c94c6771 1882 } else {
Christopher Haster 1:43f5c94c6771 1883 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr |= block_number << 4;
Christopher Haster 1:43f5c94c6771 1884 }
Christopher Haster 1:43f5c94c6771 1885
Christopher Haster 1:43f5c94c6771 1886 /* Then get needed memory count for Packet data */
Yogesh Pande 4:5d91b0f5038c 1887 dst_packed_data_needed_mem = sn_coap_builder_calc_needed_packet_data_size_2(src_coap_blockwise_ack_msg_ptr ,handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 1888
Christopher Haster 1:43f5c94c6771 1889 /* Then allocate memory for Packet data */
Christopher Haster 1:43f5c94c6771 1890 dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
Christopher Haster 1:43f5c94c6771 1891
Christopher Haster 1:43f5c94c6771 1892 if (dst_ack_packet_data_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1893 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
Christopher Haster 1:43f5c94c6771 1894 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = 0;
Christopher Haster 1:43f5c94c6771 1895 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 1896 src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 1897 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 1898 src_coap_blockwise_ack_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1899 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1900 return NULL;
Christopher Haster 1:43f5c94c6771 1901 }
Christopher Haster 1:43f5c94c6771 1902 memset(dst_ack_packet_data_ptr, 0, dst_packed_data_needed_mem);
Christopher Haster 1:43f5c94c6771 1903
Christopher Haster 1:43f5c94c6771 1904 /* * * Then build Acknowledgement message to Packed data * * */
Yogesh Pande 4:5d91b0f5038c 1905 if ((sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size)) < 0) {
Christopher Haster 1:43f5c94c6771 1906 handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
Christopher Haster 1:43f5c94c6771 1907 dst_ack_packet_data_ptr = 0;
Christopher Haster 1:43f5c94c6771 1908 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
Christopher Haster 1:43f5c94c6771 1909 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = 0;
Christopher Haster 1:43f5c94c6771 1910 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 1911 src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 1912 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 1913 src_coap_blockwise_ack_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1914 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1915 return NULL;
Christopher Haster 1:43f5c94c6771 1916 }
Christopher Haster 1:43f5c94c6771 1917
Christopher Haster 1:43f5c94c6771 1918 /* * * Save to linked list * * */
Christopher Haster 1:43f5c94c6771 1919 coap_blockwise_msg_s *stored_blockwise_msg_ptr;
Christopher Haster 1:43f5c94c6771 1920
Christopher Haster 1:43f5c94c6771 1921 stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
Christopher Haster 1:43f5c94c6771 1922 if (!stored_blockwise_msg_ptr) {
Christopher Haster 1:43f5c94c6771 1923 handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
Christopher Haster 1:43f5c94c6771 1924 dst_ack_packet_data_ptr = 0;
Christopher Haster 1:43f5c94c6771 1925 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
Christopher Haster 1:43f5c94c6771 1926 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = 0;
Christopher Haster 1:43f5c94c6771 1927 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 1928 src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 1929 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 1930 src_coap_blockwise_ack_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 1931 sn_coap_parser_release_allocated_coap_msg_mem(handle, received_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 1932 return 0;
Christopher Haster 1:43f5c94c6771 1933 }
Christopher Haster 1:43f5c94c6771 1934 memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
Christopher Haster 1:43f5c94c6771 1935
Yogesh Pande 4:5d91b0f5038c 1936 stored_blockwise_msg_ptr->timestamp = handle->system_time;
Christopher Haster 1:43f5c94c6771 1937
Christopher Haster 1:43f5c94c6771 1938 stored_blockwise_msg_ptr->coap_msg_ptr = src_coap_blockwise_ack_msg_ptr;
Christopher Haster 1:43f5c94c6771 1939 stored_blockwise_msg_ptr->coap = handle;
Christopher Haster 1:43f5c94c6771 1940
Yogesh Pande 4:5d91b0f5038c 1941 ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr);
Christopher Haster 1:43f5c94c6771 1942
Christopher Haster 1:43f5c94c6771 1943 /* * * Then release memory of CoAP Acknowledgement message * * */
Christopher Haster 1:43f5c94c6771 1944 handle->sn_coap_tx_callback(dst_ack_packet_data_ptr,
Christopher Haster 1:43f5c94c6771 1945 dst_packed_data_needed_mem, src_addr_ptr, param);
Christopher Haster 1:43f5c94c6771 1946
Christopher Haster 1:43f5c94c6771 1947 #if ENABLE_RESENDINGS
Christopher Haster 1:43f5c94c6771 1948 sn_coap_protocol_linked_list_send_msg_store(handle, src_addr_ptr,
Christopher Haster 1:43f5c94c6771 1949 dst_packed_data_needed_mem,
Christopher Haster 1:43f5c94c6771 1950 dst_ack_packet_data_ptr,
Yogesh Pande 4:5d91b0f5038c 1951 handle->system_time + (uint32_t)(handle->sn_coap_resending_intervall * RESPONSE_RANDOM_FACTOR), param, NULL, 0);
Christopher Haster 1:43f5c94c6771 1952 #endif
Christopher Haster 1:43f5c94c6771 1953 handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
Christopher Haster 1:43f5c94c6771 1954 dst_ack_packet_data_ptr = 0;
Christopher Haster 1:43f5c94c6771 1955 }
Christopher Haster 1:43f5c94c6771 1956
Christopher Haster 1:43f5c94c6771 1957 //Last block received
Christopher Haster 1:43f5c94c6771 1958 else {
Christopher Haster 1:43f5c94c6771 1959 /* * * This is the last block when whole Blockwise payload from received * * */
Christopher Haster 1:43f5c94c6771 1960 /* * * blockwise messages is gathered and returned to User * * */
Christopher Haster 1:43f5c94c6771 1961
Christopher Haster 1:43f5c94c6771 1962 /* Store last Blockwise payload to Linked list */
Christopher Haster 1:43f5c94c6771 1963 uint16_t payload_len = 0;
Yogesh Pande 4:5d91b0f5038c 1964 uint8_t *payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
Yogesh Pande 4:5d91b0f5038c 1965 uint16_t whole_payload_len = sn_coap_protocol_linked_list_blockwise_payloads_get_len(handle, src_addr_ptr);
Christopher Haster 1:43f5c94c6771 1966 uint8_t *temp_whole_payload_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1967
Christopher Haster 1:43f5c94c6771 1968 temp_whole_payload_ptr = handle->sn_coap_protocol_malloc(whole_payload_len);
Christopher Haster 1:43f5c94c6771 1969 if (!temp_whole_payload_ptr) {
Christopher Haster 1:43f5c94c6771 1970 return 0;
Christopher Haster 1:43f5c94c6771 1971 }
Christopher Haster 1:43f5c94c6771 1972
Christopher Haster 1:43f5c94c6771 1973 received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr;
Christopher Haster 1:43f5c94c6771 1974 received_coap_msg_ptr->payload_len = whole_payload_len;
Christopher Haster 1:43f5c94c6771 1975
Christopher Haster 1:43f5c94c6771 1976 /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */
Christopher Haster 1:43f5c94c6771 1977 while (payload_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 1978 memcpy(temp_whole_payload_ptr, payload_ptr, payload_len);
Christopher Haster 1:43f5c94c6771 1979
Christopher Haster 1:43f5c94c6771 1980 temp_whole_payload_ptr += payload_len;
Christopher Haster 1:43f5c94c6771 1981
Christopher Haster 1:43f5c94c6771 1982 sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(handle);
Yogesh Pande 4:5d91b0f5038c 1983 payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(handle, src_addr_ptr, &payload_len);
Christopher Haster 1:43f5c94c6771 1984 }
Christopher Haster 1:43f5c94c6771 1985 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED;
Christopher Haster 1:43f5c94c6771 1986
Christopher Haster 1:43f5c94c6771 1987 //todo: remove previous msg from list
Christopher Haster 1:43f5c94c6771 1988 }
Christopher Haster 1:43f5c94c6771 1989
Christopher Haster 1:43f5c94c6771 1990 }
Christopher Haster 1:43f5c94c6771 1991
Christopher Haster 1:43f5c94c6771 1992 //Now we send data to request
Christopher Haster 1:43f5c94c6771 1993 else {
Christopher Haster 1:43f5c94c6771 1994 //Get message by using block number
Christopher Haster 1:43f5c94c6771 1995 //NOTE: Getting the first from list might not be correct one
Yogesh Pande 4:5d91b0f5038c 1996 coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = ns_list_get_first(&handle->linked_list_blockwise_sent_msgs);
Christopher Haster 1:43f5c94c6771 1997 if (stored_blockwise_msg_temp_ptr) {
Christopher Haster 1:43f5c94c6771 1998 uint16_t block_size = 1;
Christopher Haster 1:43f5c94c6771 1999 uint32_t block_number = 0;
Christopher Haster 1:43f5c94c6771 2000
Christopher Haster 1:43f5c94c6771 2001 /* Resolve block parameters */
Christopher Haster 1:43f5c94c6771 2002 if (received_coap_msg_ptr->options_list_ptr->block2_len == 3) {
Christopher Haster 1:43f5c94c6771 2003 block_number = *(received_coap_msg_ptr->options_list_ptr->block2_ptr) << 12;
Christopher Haster 1:43f5c94c6771 2004 block_number |= *(received_coap_msg_ptr->options_list_ptr->block2_ptr + 1) << 4;
Christopher Haster 1:43f5c94c6771 2005 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block2_ptr + 2)) >> 4;
Christopher Haster 1:43f5c94c6771 2006 }
Christopher Haster 1:43f5c94c6771 2007
Christopher Haster 1:43f5c94c6771 2008 else if (received_coap_msg_ptr->options_list_ptr->block2_len == 2) {
Christopher Haster 1:43f5c94c6771 2009 block_number = *(received_coap_msg_ptr->options_list_ptr->block2_ptr) << 4;
Christopher Haster 1:43f5c94c6771 2010 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block2_ptr + 1)) >> 4;
Christopher Haster 1:43f5c94c6771 2011 } else if (received_coap_msg_ptr->options_list_ptr->block2_len == 1) {
Christopher Haster 1:43f5c94c6771 2012 block_number = (*received_coap_msg_ptr->options_list_ptr->block2_ptr) >> 4;
Christopher Haster 1:43f5c94c6771 2013 }
Christopher Haster 1:43f5c94c6771 2014
Christopher Haster 1:43f5c94c6771 2015 block_temp = *(received_coap_msg_ptr->options_list_ptr->block2_ptr + (received_coap_msg_ptr->options_list_ptr->block2_len - 1)) & 0x07;
Christopher Haster 1:43f5c94c6771 2016 block_size = block_size << (block_temp + 4);
Christopher Haster 1:43f5c94c6771 2017
Christopher Haster 1:43f5c94c6771 2018 /* Build response message */
Christopher Haster 1:43f5c94c6771 2019 src_coap_blockwise_ack_msg_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr;
Christopher Haster 1:43f5c94c6771 2020
Christopher Haster 1:43f5c94c6771 2021 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr) {
Christopher Haster 1:43f5c94c6771 2022 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr) {
Christopher Haster 1:43f5c94c6771 2023 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr);
Christopher Haster 1:43f5c94c6771 2024 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr = 0;
Christopher Haster 1:43f5c94c6771 2025
Christopher Haster 1:43f5c94c6771 2026 }
Christopher Haster 1:43f5c94c6771 2027 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr) {
Christopher Haster 1:43f5c94c6771 2028 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
Christopher Haster 1:43f5c94c6771 2029 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = 0;
Christopher Haster 1:43f5c94c6771 2030 }
Christopher Haster 1:43f5c94c6771 2031 } else {
Christopher Haster 1:43f5c94c6771 2032 src_coap_blockwise_ack_msg_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 2033 if (!src_coap_blockwise_ack_msg_ptr->options_list_ptr) {
Christopher Haster 1:43f5c94c6771 2034 return 0;
Christopher Haster 1:43f5c94c6771 2035 }
Christopher Haster 1:43f5c94c6771 2036 memset(src_coap_blockwise_ack_msg_ptr->options_list_ptr, 0, (sizeof(sn_coap_options_list_s)));
Christopher Haster 1:43f5c94c6771 2037 }
Christopher Haster 1:43f5c94c6771 2038
Christopher Haster 1:43f5c94c6771 2039
Christopher Haster 1:43f5c94c6771 2040 src_coap_blockwise_ack_msg_ptr->msg_id = received_coap_msg_ptr->msg_id;
Christopher Haster 1:43f5c94c6771 2041
Christopher Haster 1:43f5c94c6771 2042 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len = received_coap_msg_ptr->options_list_ptr->block2_len;
Christopher Haster 1:43f5c94c6771 2043 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);
Christopher Haster 1:43f5c94c6771 2044
Christopher Haster 1:43f5c94c6771 2045 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 2046 if(src_coap_blockwise_ack_msg_ptr->payload_ptr){
Christopher Haster 1:43f5c94c6771 2047 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 2048 }
Christopher Haster 1:43f5c94c6771 2049 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 2050 src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 2051 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 2052 stored_blockwise_msg_temp_ptr->coap_msg_ptr = NULL;
Christopher Haster 1:43f5c94c6771 2053 src_coap_blockwise_ack_msg_ptr = 0;
Christopher Haster 1:43f5c94c6771 2054 return NULL;
Christopher Haster 1:43f5c94c6771 2055 }
Christopher Haster 1:43f5c94c6771 2056 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);
Christopher Haster 1:43f5c94c6771 2057
Christopher Haster 1:43f5c94c6771 2058 /* * Payload part * */
Christopher Haster 1:43f5c94c6771 2059
Christopher Haster 1:43f5c94c6771 2060 /* Check if last block */
Christopher Haster 1:43f5c94c6771 2061
Christopher Haster 1:43f5c94c6771 2062 original_payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len;
Christopher Haster 1:43f5c94c6771 2063 original_payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr;
Christopher Haster 1:43f5c94c6771 2064
Christopher Haster 1:43f5c94c6771 2065 if ((block_size * (block_number + 1)) > stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len) {
Christopher Haster 1:43f5c94c6771 2066 src_coap_blockwise_ack_msg_ptr->payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len - (block_size * block_number);
Christopher Haster 1:43f5c94c6771 2067 src_coap_blockwise_ack_msg_ptr->payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr + (block_size * block_number);
Christopher Haster 1:43f5c94c6771 2068 }
Christopher Haster 1:43f5c94c6771 2069 /* Not last block */
Christopher Haster 1:43f5c94c6771 2070 else {
Christopher Haster 1:43f5c94c6771 2071 /* set more - bit */
Christopher Haster 1:43f5c94c6771 2072 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr + (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len - 1)) |= 0x08;
Christopher Haster 1:43f5c94c6771 2073 src_coap_blockwise_ack_msg_ptr->payload_len = block_size;
Christopher Haster 1:43f5c94c6771 2074 src_coap_blockwise_ack_msg_ptr->payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr + (block_size * block_number);
Christopher Haster 1:43f5c94c6771 2075 }
Christopher Haster 1:43f5c94c6771 2076
Christopher Haster 1:43f5c94c6771 2077 /* Build and send block message */
Yogesh Pande 4:5d91b0f5038c 2078 dst_packed_data_needed_mem = sn_coap_builder_calc_needed_packet_data_size_2(src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 2079
Christopher Haster 1:43f5c94c6771 2080 dst_ack_packet_data_ptr = handle->sn_coap_protocol_malloc(dst_packed_data_needed_mem);
Christopher Haster 1:43f5c94c6771 2081 if (!dst_ack_packet_data_ptr) {
Christopher Haster 1:43f5c94c6771 2082 if(original_payload_ptr){
Christopher Haster 1:43f5c94c6771 2083 handle->sn_coap_protocol_free(original_payload_ptr);
Christopher Haster 1:43f5c94c6771 2084 original_payload_ptr = NULL;
Christopher Haster 1:43f5c94c6771 2085 }
Christopher Haster 1:43f5c94c6771 2086 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
Christopher Haster 1:43f5c94c6771 2087 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = 0;
Christopher Haster 1:43f5c94c6771 2088 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 2089 src_coap_blockwise_ack_msg_ptr->options_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 2090 handle->sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
Christopher Haster 1:43f5c94c6771 2091 stored_blockwise_msg_temp_ptr->coap_msg_ptr = NULL;
Christopher Haster 1:43f5c94c6771 2092 return NULL;
Christopher Haster 1:43f5c94c6771 2093 }
Christopher Haster 1:43f5c94c6771 2094
Yogesh Pande 4:5d91b0f5038c 2095 sn_coap_builder_2(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr, handle->sn_coap_block_data_size);
Christopher Haster 1:43f5c94c6771 2096
Christopher Haster 1:43f5c94c6771 2097 handle->sn_coap_tx_callback(dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr, param);
Christopher Haster 1:43f5c94c6771 2098
Christopher Haster 1:43f5c94c6771 2099 handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
Christopher Haster 1:43f5c94c6771 2100 dst_ack_packet_data_ptr = 0;
Christopher Haster 1:43f5c94c6771 2101
Christopher Haster 1:43f5c94c6771 2102 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len = original_payload_len;
Christopher Haster 1:43f5c94c6771 2103 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = original_payload_ptr;
Christopher Haster 1:43f5c94c6771 2104
Christopher Haster 1:43f5c94c6771 2105 if ((block_size * (block_number + 1)) > stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len) {
Christopher Haster 1:43f5c94c6771 2106 sn_coap_protocol_linked_list_blockwise_msg_remove(handle, stored_blockwise_msg_temp_ptr);
Christopher Haster 1:43f5c94c6771 2107 }
Christopher Haster 1:43f5c94c6771 2108
Christopher Haster 1:43f5c94c6771 2109 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_ACK;
Christopher Haster 1:43f5c94c6771 2110 }
Christopher Haster 1:43f5c94c6771 2111 }
Christopher Haster 1:43f5c94c6771 2112 }
Christopher Haster 1:43f5c94c6771 2113 return received_coap_msg_ptr;
Christopher Haster 1:43f5c94c6771 2114 }
Christopher Haster 1:43f5c94c6771 2115
Christopher Haster 1:43f5c94c6771 2116 static int8_t sn_coap_convert_block_size(uint16_t block_size)
Christopher Haster 1:43f5c94c6771 2117 {
Christopher Haster 1:43f5c94c6771 2118 if (block_size == 16) {
Christopher Haster 1:43f5c94c6771 2119 return 0;
Christopher Haster 1:43f5c94c6771 2120 } else if (block_size == 32) {
Christopher Haster 1:43f5c94c6771 2121 return 1;
Christopher Haster 1:43f5c94c6771 2122 } else if (block_size == 64) {
Christopher Haster 1:43f5c94c6771 2123 return 2;
Christopher Haster 1:43f5c94c6771 2124 } else if (block_size == 128) {
Christopher Haster 1:43f5c94c6771 2125 return 3;
Christopher Haster 1:43f5c94c6771 2126 } else if (block_size == 256) {
Christopher Haster 1:43f5c94c6771 2127 return 4;
Christopher Haster 1:43f5c94c6771 2128 } else if (block_size == 512) {
Christopher Haster 1:43f5c94c6771 2129 return 5;
Christopher Haster 1:43f5c94c6771 2130 } else if (block_size == 1024) {
Christopher Haster 1:43f5c94c6771 2131 return 6;
Christopher Haster 1:43f5c94c6771 2132 }
Christopher Haster 1:43f5c94c6771 2133
Christopher Haster 1:43f5c94c6771 2134 return 0;
Christopher Haster 1:43f5c94c6771 2135 }
Christopher Haster 1:43f5c94c6771 2136
Christopher Haster 1:43f5c94c6771 2137 static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coap_hdr_s *source_header_ptr)
Christopher Haster 1:43f5c94c6771 2138 {
Christopher Haster 1:43f5c94c6771 2139 sn_coap_hdr_s *destination_header_ptr;
Christopher Haster 1:43f5c94c6771 2140
Christopher Haster 1:43f5c94c6771 2141 destination_header_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 2142 if (!destination_header_ptr) {
Christopher Haster 1:43f5c94c6771 2143 return 0;
Christopher Haster 1:43f5c94c6771 2144 }
Christopher Haster 1:43f5c94c6771 2145 memset(destination_header_ptr, 0, sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 2146
Christopher Haster 1:43f5c94c6771 2147 destination_header_ptr->coap_status = source_header_ptr->coap_status;
Christopher Haster 1:43f5c94c6771 2148 destination_header_ptr->msg_type = source_header_ptr->msg_type;
Christopher Haster 1:43f5c94c6771 2149 destination_header_ptr->msg_code = source_header_ptr->msg_code;
Christopher Haster 1:43f5c94c6771 2150 destination_header_ptr->msg_id = source_header_ptr->msg_id;
Christopher Haster 1:43f5c94c6771 2151
Christopher Haster 1:43f5c94c6771 2152 if (source_header_ptr->uri_path_ptr) {
Christopher Haster 1:43f5c94c6771 2153 destination_header_ptr->uri_path_len = source_header_ptr->uri_path_len;
Christopher Haster 1:43f5c94c6771 2154 destination_header_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->uri_path_len);
Christopher Haster 1:43f5c94c6771 2155 if (!destination_header_ptr->uri_path_ptr) {
Christopher Haster 1:43f5c94c6771 2156 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2157 return 0;
Christopher Haster 1:43f5c94c6771 2158 }
Christopher Haster 1:43f5c94c6771 2159 memcpy(destination_header_ptr->uri_path_ptr, source_header_ptr->uri_path_ptr, source_header_ptr->uri_path_len);
Christopher Haster 1:43f5c94c6771 2160 }
Christopher Haster 1:43f5c94c6771 2161
Christopher Haster 1:43f5c94c6771 2162 if (source_header_ptr->token_ptr) {
Christopher Haster 1:43f5c94c6771 2163 destination_header_ptr->token_len = source_header_ptr->token_len;
Christopher Haster 1:43f5c94c6771 2164 destination_header_ptr->token_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->token_len);
Christopher Haster 1:43f5c94c6771 2165 if (!destination_header_ptr->token_ptr) {
Christopher Haster 1:43f5c94c6771 2166 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2167 return 0;
Christopher Haster 1:43f5c94c6771 2168 }
Christopher Haster 1:43f5c94c6771 2169 memcpy(destination_header_ptr->token_ptr, source_header_ptr->token_ptr, source_header_ptr->token_len);
Christopher Haster 1:43f5c94c6771 2170 }
Christopher Haster 1:43f5c94c6771 2171
Christopher Haster 1:43f5c94c6771 2172 if (source_header_ptr->content_type_ptr) {
Christopher Haster 1:43f5c94c6771 2173 destination_header_ptr->content_type_len = source_header_ptr->content_type_len;
Christopher Haster 1:43f5c94c6771 2174 destination_header_ptr->content_type_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->content_type_len);
Christopher Haster 1:43f5c94c6771 2175 if (!destination_header_ptr->content_type_ptr) {
Christopher Haster 1:43f5c94c6771 2176 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2177 return 0;
Christopher Haster 1:43f5c94c6771 2178 }
Christopher Haster 1:43f5c94c6771 2179 memcpy(destination_header_ptr->content_type_ptr, source_header_ptr->content_type_ptr, source_header_ptr->content_type_len);
Christopher Haster 1:43f5c94c6771 2180 }
Christopher Haster 1:43f5c94c6771 2181
Christopher Haster 1:43f5c94c6771 2182 /* Options list */
Christopher Haster 1:43f5c94c6771 2183 if (source_header_ptr->options_list_ptr) {
Christopher Haster 1:43f5c94c6771 2184 destination_header_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 2185 if (!destination_header_ptr->options_list_ptr) {
Christopher Haster 1:43f5c94c6771 2186 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2187 return 0;
Christopher Haster 1:43f5c94c6771 2188 }
Christopher Haster 1:43f5c94c6771 2189 memset(destination_header_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 2190
Christopher Haster 1:43f5c94c6771 2191
Christopher Haster 1:43f5c94c6771 2192 if (source_header_ptr->options_list_ptr->max_age_ptr) {
Christopher Haster 1:43f5c94c6771 2193 destination_header_ptr->options_list_ptr->max_age_len = source_header_ptr->options_list_ptr->max_age_len;
Christopher Haster 1:43f5c94c6771 2194 destination_header_ptr->options_list_ptr->max_age_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->max_age_len);
Christopher Haster 1:43f5c94c6771 2195 if (!destination_header_ptr->options_list_ptr->max_age_ptr) {
Christopher Haster 1:43f5c94c6771 2196 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2197 return 0;
Christopher Haster 1:43f5c94c6771 2198 }
Christopher Haster 1:43f5c94c6771 2199 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);
Christopher Haster 1:43f5c94c6771 2200 }
Christopher Haster 1:43f5c94c6771 2201
Christopher Haster 1:43f5c94c6771 2202 if (source_header_ptr->options_list_ptr->proxy_uri_ptr) {
Christopher Haster 1:43f5c94c6771 2203 destination_header_ptr->options_list_ptr->proxy_uri_len = source_header_ptr->options_list_ptr->proxy_uri_len;
Christopher Haster 1:43f5c94c6771 2204 destination_header_ptr->options_list_ptr->proxy_uri_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->proxy_uri_len);
Christopher Haster 1:43f5c94c6771 2205 if (!destination_header_ptr->options_list_ptr->proxy_uri_ptr) {
Christopher Haster 1:43f5c94c6771 2206 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2207 return 0;
Christopher Haster 1:43f5c94c6771 2208 }
Christopher Haster 1:43f5c94c6771 2209 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);
Christopher Haster 1:43f5c94c6771 2210 }
Christopher Haster 1:43f5c94c6771 2211
Christopher Haster 1:43f5c94c6771 2212 if (source_header_ptr->options_list_ptr->etag_ptr) {
Christopher Haster 1:43f5c94c6771 2213 destination_header_ptr->options_list_ptr->etag_len = source_header_ptr->options_list_ptr->etag_len;
Christopher Haster 1:43f5c94c6771 2214 destination_header_ptr->options_list_ptr->etag_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->etag_len);
Christopher Haster 1:43f5c94c6771 2215 if (!destination_header_ptr->options_list_ptr->etag_ptr) {
Christopher Haster 1:43f5c94c6771 2216 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2217 return 0;
Christopher Haster 1:43f5c94c6771 2218 }
Christopher Haster 1:43f5c94c6771 2219 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);
Christopher Haster 1:43f5c94c6771 2220 }
Christopher Haster 1:43f5c94c6771 2221
Christopher Haster 1:43f5c94c6771 2222 if (source_header_ptr->options_list_ptr->uri_host_ptr) {
Christopher Haster 1:43f5c94c6771 2223 destination_header_ptr->options_list_ptr->uri_host_len = source_header_ptr->options_list_ptr->uri_host_len;
Christopher Haster 1:43f5c94c6771 2224 destination_header_ptr->options_list_ptr->uri_host_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_host_len);
Christopher Haster 1:43f5c94c6771 2225 if (!destination_header_ptr->options_list_ptr->uri_host_ptr) {
Christopher Haster 1:43f5c94c6771 2226 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2227 return 0;
Christopher Haster 1:43f5c94c6771 2228 }
Christopher Haster 1:43f5c94c6771 2229 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);
Christopher Haster 1:43f5c94c6771 2230 }
Christopher Haster 1:43f5c94c6771 2231
Christopher Haster 1:43f5c94c6771 2232 if (source_header_ptr->options_list_ptr->location_path_ptr) {
Christopher Haster 1:43f5c94c6771 2233 destination_header_ptr->options_list_ptr->location_path_len = source_header_ptr->options_list_ptr->location_path_len;
Christopher Haster 1:43f5c94c6771 2234 destination_header_ptr->options_list_ptr->location_path_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->location_path_len);
Christopher Haster 1:43f5c94c6771 2235 if (!destination_header_ptr->options_list_ptr->location_path_ptr) {
Christopher Haster 1:43f5c94c6771 2236 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2237 return 0;
Christopher Haster 1:43f5c94c6771 2238 }
Christopher Haster 1:43f5c94c6771 2239 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);
Christopher Haster 1:43f5c94c6771 2240 }
Christopher Haster 1:43f5c94c6771 2241
Christopher Haster 1:43f5c94c6771 2242 if (source_header_ptr->options_list_ptr->uri_port_ptr) {
Christopher Haster 1:43f5c94c6771 2243 destination_header_ptr->options_list_ptr->uri_port_len = source_header_ptr->options_list_ptr->uri_port_len;
Christopher Haster 1:43f5c94c6771 2244 destination_header_ptr->options_list_ptr->uri_port_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_port_len);
Christopher Haster 1:43f5c94c6771 2245 if (!destination_header_ptr->options_list_ptr->uri_port_ptr) {
Christopher Haster 1:43f5c94c6771 2246 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2247 return 0;
Christopher Haster 1:43f5c94c6771 2248 }
Christopher Haster 1:43f5c94c6771 2249 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);
Christopher Haster 1:43f5c94c6771 2250 }
Christopher Haster 1:43f5c94c6771 2251
Christopher Haster 1:43f5c94c6771 2252 if (source_header_ptr->options_list_ptr->location_query_ptr) {
Christopher Haster 1:43f5c94c6771 2253 destination_header_ptr->options_list_ptr->location_query_len = source_header_ptr->options_list_ptr->location_query_len;
Christopher Haster 1:43f5c94c6771 2254 destination_header_ptr->options_list_ptr->location_query_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->location_query_len);
Christopher Haster 1:43f5c94c6771 2255 if (!destination_header_ptr->options_list_ptr->location_query_ptr) {
Christopher Haster 1:43f5c94c6771 2256 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2257 return 0;
Christopher Haster 1:43f5c94c6771 2258 }
Christopher Haster 1:43f5c94c6771 2259 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);
Christopher Haster 1:43f5c94c6771 2260 }
Christopher Haster 1:43f5c94c6771 2261
Christopher Haster 1:43f5c94c6771 2262 destination_header_ptr->options_list_ptr->observe = source_header_ptr->options_list_ptr->observe;
Christopher Haster 1:43f5c94c6771 2263
Christopher Haster 1:43f5c94c6771 2264 if (source_header_ptr->options_list_ptr->observe_ptr) {
Christopher Haster 1:43f5c94c6771 2265 destination_header_ptr->options_list_ptr->observe_len = source_header_ptr->options_list_ptr->observe_len;
Christopher Haster 1:43f5c94c6771 2266 destination_header_ptr->options_list_ptr->observe_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->observe_len);
Christopher Haster 1:43f5c94c6771 2267 if (!destination_header_ptr->options_list_ptr->observe_ptr) {
Christopher Haster 1:43f5c94c6771 2268 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2269 return 0;
Christopher Haster 1:43f5c94c6771 2270 }
Christopher Haster 1:43f5c94c6771 2271 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);
Christopher Haster 1:43f5c94c6771 2272 }
Christopher Haster 1:43f5c94c6771 2273
Christopher Haster 1:43f5c94c6771 2274 if (source_header_ptr->options_list_ptr->accept_ptr) {
Christopher Haster 1:43f5c94c6771 2275 destination_header_ptr->options_list_ptr->accept_len = source_header_ptr->options_list_ptr->accept_len;
Christopher Haster 1:43f5c94c6771 2276 destination_header_ptr->options_list_ptr->accept_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->accept_len);
Christopher Haster 1:43f5c94c6771 2277 if (!destination_header_ptr->options_list_ptr->accept_ptr) {
Christopher Haster 1:43f5c94c6771 2278 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2279 return 0;
Christopher Haster 1:43f5c94c6771 2280 }
Christopher Haster 1:43f5c94c6771 2281 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);
Christopher Haster 1:43f5c94c6771 2282 }
Christopher Haster 1:43f5c94c6771 2283
Christopher Haster 1:43f5c94c6771 2284 if (source_header_ptr->options_list_ptr->uri_query_ptr) {
Christopher Haster 1:43f5c94c6771 2285 destination_header_ptr->options_list_ptr->uri_query_len = source_header_ptr->options_list_ptr->uri_query_len;
Christopher Haster 1:43f5c94c6771 2286 destination_header_ptr->options_list_ptr->uri_query_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_query_len);
Christopher Haster 1:43f5c94c6771 2287 if (!destination_header_ptr->options_list_ptr->uri_query_ptr) {
Christopher Haster 1:43f5c94c6771 2288 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2289 return 0;
Christopher Haster 1:43f5c94c6771 2290 }
Christopher Haster 1:43f5c94c6771 2291 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);
Christopher Haster 1:43f5c94c6771 2292 }
Christopher Haster 1:43f5c94c6771 2293
Christopher Haster 1:43f5c94c6771 2294 if (source_header_ptr->options_list_ptr->block1_ptr) {
Christopher Haster 1:43f5c94c6771 2295 destination_header_ptr->options_list_ptr->block1_len = source_header_ptr->options_list_ptr->block1_len;
Christopher Haster 1:43f5c94c6771 2296 destination_header_ptr->options_list_ptr->block1_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->block1_len);
Christopher Haster 1:43f5c94c6771 2297 if (!destination_header_ptr->options_list_ptr->block1_ptr) {
Christopher Haster 1:43f5c94c6771 2298 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2299 return 0;
Christopher Haster 1:43f5c94c6771 2300 }
Christopher Haster 1:43f5c94c6771 2301 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);
Christopher Haster 1:43f5c94c6771 2302 }
Christopher Haster 1:43f5c94c6771 2303
Christopher Haster 1:43f5c94c6771 2304 if (source_header_ptr->options_list_ptr->block2_ptr) {
Christopher Haster 1:43f5c94c6771 2305 destination_header_ptr->options_list_ptr->block2_len = source_header_ptr->options_list_ptr->block2_len;
Christopher Haster 1:43f5c94c6771 2306 destination_header_ptr->options_list_ptr->block2_ptr = handle->sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->block2_len);
Christopher Haster 1:43f5c94c6771 2307 if (!destination_header_ptr->options_list_ptr->block2_ptr) {
Christopher Haster 1:43f5c94c6771 2308 sn_coap_parser_release_allocated_coap_msg_mem(handle, destination_header_ptr);
Christopher Haster 1:43f5c94c6771 2309 return 0;
Christopher Haster 1:43f5c94c6771 2310 }
Christopher Haster 1:43f5c94c6771 2311 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);
Christopher Haster 1:43f5c94c6771 2312 }
Christopher Haster 1:43f5c94c6771 2313 }
Christopher Haster 1:43f5c94c6771 2314
Christopher Haster 1:43f5c94c6771 2315 return destination_header_ptr;
Christopher Haster 1:43f5c94c6771 2316 }
Christopher Haster 1:43f5c94c6771 2317 #endif