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