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