sandbox / mbed-client-c

Fork of mbed-client-c by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:31:54 2016 -0600
Revision:
1:43f5c94c6771
Child:
4:5d91b0f5038c
Initial move of mbed-client-c to mercurial

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