leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2011-2015 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16
leothedragon 0:8f0bb79ddd48 17 /**
leothedragon 0:8f0bb79ddd48 18 * \file sn_coap_builder.c
leothedragon 0:8f0bb79ddd48 19 *
leothedragon 0:8f0bb79ddd48 20 * \brief CoAP Message builder
leothedragon 0:8f0bb79ddd48 21 *
leothedragon 0:8f0bb79ddd48 22 * Functionality: Builds CoAP message
leothedragon 0:8f0bb79ddd48 23 *
leothedragon 0:8f0bb79ddd48 24 */
leothedragon 0:8f0bb79ddd48 25
leothedragon 0:8f0bb79ddd48 26 /* * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 27 /* * * * INCLUDE FILES * * * */
leothedragon 0:8f0bb79ddd48 28 /* * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 29
leothedragon 0:8f0bb79ddd48 30 #include <string.h> /* For memset() and memcpy() */
leothedragon 0:8f0bb79ddd48 31
leothedragon 0:8f0bb79ddd48 32 #include "ns_types.h"
leothedragon 0:8f0bb79ddd48 33 #include "mbed-coap/sn_coap_header.h"
leothedragon 0:8f0bb79ddd48 34 #include "sn_coap_header_internal.h"
leothedragon 0:8f0bb79ddd48 35 #include "sn_coap_protocol_internal.h"
leothedragon 0:8f0bb79ddd48 36 #include "mbed-trace/mbed_trace.h"
leothedragon 0:8f0bb79ddd48 37
leothedragon 0:8f0bb79ddd48 38 #define TRACE_GROUP "coap"
leothedragon 0:8f0bb79ddd48 39 /* * * * LOCAL FUNCTION PROTOTYPES * * * */
leothedragon 0:8f0bb79ddd48 40 static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr);
leothedragon 0:8f0bb79ddd48 41 static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr);
leothedragon 0:8f0bb79ddd48 42 static uint16_t sn_coap_builder_options_calc_option_size(uint16_t query_len, uint8_t *query_ptr, sn_coap_option_numbers_e option);
leothedragon 0:8f0bb79ddd48 43 static int16_t sn_coap_builder_options_build_add_one_option(uint8_t **dst_packet_data_pptr, uint16_t option_len, uint8_t *option_ptr, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number);
leothedragon 0:8f0bb79ddd48 44 static int16_t sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_packet_data_pptr, uint8_t **src_pptr, uint16_t *src_len_ptr, sn_coap_option_numbers_e option, uint16_t *previous_option_number);
leothedragon 0:8f0bb79ddd48 45 static uint8_t sn_coap_builder_options_build_add_uint_option(uint8_t **dst_packet_data_pptr, uint32_t value, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number);
leothedragon 0:8f0bb79ddd48 46 static uint8_t sn_coap_builder_options_get_option_part_count(uint16_t query_len, uint8_t *query_ptr, sn_coap_option_numbers_e option);
leothedragon 0:8f0bb79ddd48 47 static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option_string(uint16_t query_len, uint8_t *query_ptr, uint8_t query_index, sn_coap_option_numbers_e option);
leothedragon 0:8f0bb79ddd48 48 static int16_t sn_coap_builder_options_get_option_part_position(uint16_t query_len, uint8_t *query_ptr, uint8_t query_index, sn_coap_option_numbers_e option);
leothedragon 0:8f0bb79ddd48 49 static void sn_coap_builder_payload_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr);
leothedragon 0:8f0bb79ddd48 50 static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_coap_msg_ptr/*, uint8_t block_option*/);
leothedragon 0:8f0bb79ddd48 51
leothedragon 0:8f0bb79ddd48 52 sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code)
leothedragon 0:8f0bb79ddd48 53 {
leothedragon 0:8f0bb79ddd48 54 sn_coap_hdr_s *coap_res_ptr;
leothedragon 0:8f0bb79ddd48 55
leothedragon 0:8f0bb79ddd48 56 if (!coap_packet_ptr || !handle) {
leothedragon 0:8f0bb79ddd48 57 return NULL;
leothedragon 0:8f0bb79ddd48 58 }
leothedragon 0:8f0bb79ddd48 59
leothedragon 0:8f0bb79ddd48 60 coap_res_ptr = sn_coap_parser_alloc_message(handle);
leothedragon 0:8f0bb79ddd48 61 if (!coap_res_ptr) {
leothedragon 0:8f0bb79ddd48 62 tr_error("sn_coap_build_response - failed to allocate message!");
leothedragon 0:8f0bb79ddd48 63 return NULL;
leothedragon 0:8f0bb79ddd48 64 }
leothedragon 0:8f0bb79ddd48 65
leothedragon 0:8f0bb79ddd48 66 if (msg_code == COAP_MSG_CODE_REQUEST_GET) {
leothedragon 0:8f0bb79ddd48 67 // Blockwise message response is new GET
leothedragon 0:8f0bb79ddd48 68 coap_res_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
leothedragon 0:8f0bb79ddd48 69 coap_res_ptr->msg_code = (sn_coap_msg_code_e)msg_code;
leothedragon 0:8f0bb79ddd48 70 /* msg_id needs to be set by the caller in this case */
leothedragon 0:8f0bb79ddd48 71 }
leothedragon 0:8f0bb79ddd48 72 else if (coap_packet_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
leothedragon 0:8f0bb79ddd48 73 coap_res_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
leothedragon 0:8f0bb79ddd48 74 coap_res_ptr->msg_code = (sn_coap_msg_code_e)msg_code;
leothedragon 0:8f0bb79ddd48 75 coap_res_ptr->msg_id = coap_packet_ptr->msg_id;
leothedragon 0:8f0bb79ddd48 76 }
leothedragon 0:8f0bb79ddd48 77 else if (coap_packet_ptr->msg_type == COAP_MSG_TYPE_NON_CONFIRMABLE) {
leothedragon 0:8f0bb79ddd48 78 coap_res_ptr->msg_type = COAP_MSG_TYPE_NON_CONFIRMABLE;
leothedragon 0:8f0bb79ddd48 79 coap_res_ptr->msg_code = (sn_coap_msg_code_e)msg_code;
leothedragon 0:8f0bb79ddd48 80 /* msg_id needs to be set by the caller in this case */
leothedragon 0:8f0bb79ddd48 81 }
leothedragon 0:8f0bb79ddd48 82 else {
leothedragon 0:8f0bb79ddd48 83 handle->sn_coap_protocol_free( coap_res_ptr );
leothedragon 0:8f0bb79ddd48 84 return NULL;
leothedragon 0:8f0bb79ddd48 85 }
leothedragon 0:8f0bb79ddd48 86
leothedragon 0:8f0bb79ddd48 87 if (coap_packet_ptr->token_ptr) {
leothedragon 0:8f0bb79ddd48 88 coap_res_ptr->token_len = coap_packet_ptr->token_len;
leothedragon 0:8f0bb79ddd48 89 coap_res_ptr->token_ptr = handle->sn_coap_protocol_malloc(coap_res_ptr->token_len);
leothedragon 0:8f0bb79ddd48 90 if (!coap_res_ptr->token_ptr) {
leothedragon 0:8f0bb79ddd48 91 tr_error("sn_coap_build_response - failed to allocate token!");
leothedragon 0:8f0bb79ddd48 92 handle->sn_coap_protocol_free(coap_res_ptr);
leothedragon 0:8f0bb79ddd48 93 return NULL;
leothedragon 0:8f0bb79ddd48 94 }
leothedragon 0:8f0bb79ddd48 95 memcpy(coap_res_ptr->token_ptr, coap_packet_ptr->token_ptr, coap_res_ptr->token_len);
leothedragon 0:8f0bb79ddd48 96 }
leothedragon 0:8f0bb79ddd48 97 return coap_res_ptr;
leothedragon 0:8f0bb79ddd48 98 }
leothedragon 0:8f0bb79ddd48 99
leothedragon 0:8f0bb79ddd48 100 int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr)
leothedragon 0:8f0bb79ddd48 101 {
leothedragon 0:8f0bb79ddd48 102 return sn_coap_builder_2(dst_packet_data_ptr, src_coap_msg_ptr, SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE);
leothedragon 0:8f0bb79ddd48 103 }
leothedragon 0:8f0bb79ddd48 104
leothedragon 0:8f0bb79ddd48 105 int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size)
leothedragon 0:8f0bb79ddd48 106 {
leothedragon 0:8f0bb79ddd48 107 uint8_t *base_packet_data_ptr = NULL;
leothedragon 0:8f0bb79ddd48 108
leothedragon 0:8f0bb79ddd48 109 /* * * * Check given pointers * * * */
leothedragon 0:8f0bb79ddd48 110 if (dst_packet_data_ptr == NULL || src_coap_msg_ptr == NULL) {
leothedragon 0:8f0bb79ddd48 111 return -2;
leothedragon 0:8f0bb79ddd48 112 }
leothedragon 0:8f0bb79ddd48 113
leothedragon 0:8f0bb79ddd48 114 /* Initialize given Packet data memory area with zero values */
leothedragon 0:8f0bb79ddd48 115 uint16_t dst_byte_count_to_be_built = sn_coap_builder_calc_needed_packet_data_size_2(src_coap_msg_ptr, blockwise_payload_size);
leothedragon 0:8f0bb79ddd48 116 if (!dst_byte_count_to_be_built) {
leothedragon 0:8f0bb79ddd48 117 tr_error("sn_coap_builder_2 - failed to allocate message!");
leothedragon 0:8f0bb79ddd48 118 return -1;
leothedragon 0:8f0bb79ddd48 119 }
leothedragon 0:8f0bb79ddd48 120
leothedragon 0:8f0bb79ddd48 121 memset(dst_packet_data_ptr, 0, dst_byte_count_to_be_built);
leothedragon 0:8f0bb79ddd48 122
leothedragon 0:8f0bb79ddd48 123 /* * * * Store base (= original) destination Packet data pointer for later usage * * * */
leothedragon 0:8f0bb79ddd48 124 base_packet_data_ptr = dst_packet_data_ptr;
leothedragon 0:8f0bb79ddd48 125
leothedragon 0:8f0bb79ddd48 126 /* * * * * * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 127 /* * * * Header part building * * * */
leothedragon 0:8f0bb79ddd48 128 /* * * * * * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 129 if (sn_coap_builder_header_build(&dst_packet_data_ptr, src_coap_msg_ptr) != 0) {
leothedragon 0:8f0bb79ddd48 130 /* Header building failed */
leothedragon 0:8f0bb79ddd48 131 tr_error("sn_coap_builder_2 - header building failed!");
leothedragon 0:8f0bb79ddd48 132 return -1;
leothedragon 0:8f0bb79ddd48 133 }
leothedragon 0:8f0bb79ddd48 134
leothedragon 0:8f0bb79ddd48 135 /* If else than Reset message because Reset message must be empty */
leothedragon 0:8f0bb79ddd48 136 if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_RESET) {
leothedragon 0:8f0bb79ddd48 137 /* * * * * * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 138 /* * * * Options part building * * * */
leothedragon 0:8f0bb79ddd48 139 /* * * * * * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 140 sn_coap_builder_options_build(&dst_packet_data_ptr, src_coap_msg_ptr);
leothedragon 0:8f0bb79ddd48 141
leothedragon 0:8f0bb79ddd48 142 /* * * * * * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 143 /* * * * Payload part building * * * */
leothedragon 0:8f0bb79ddd48 144 /* * * * * * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 145 sn_coap_builder_payload_build(&dst_packet_data_ptr, src_coap_msg_ptr);
leothedragon 0:8f0bb79ddd48 146 }
leothedragon 0:8f0bb79ddd48 147 /* * * * Return built Packet data length * * * */
leothedragon 0:8f0bb79ddd48 148 return (dst_packet_data_ptr - base_packet_data_ptr);
leothedragon 0:8f0bb79ddd48 149 }
leothedragon 0:8f0bb79ddd48 150 uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr)
leothedragon 0:8f0bb79ddd48 151 {
leothedragon 0:8f0bb79ddd48 152 return sn_coap_builder_calc_needed_packet_data_size_2(src_coap_msg_ptr, SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE);
leothedragon 0:8f0bb79ddd48 153 }
leothedragon 0:8f0bb79ddd48 154
leothedragon 0:8f0bb79ddd48 155 uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size)
leothedragon 0:8f0bb79ddd48 156 {
leothedragon 0:8f0bb79ddd48 157 (void)blockwise_payload_size;
leothedragon 0:8f0bb79ddd48 158 uint16_t returned_byte_count = 0;
leothedragon 0:8f0bb79ddd48 159
leothedragon 0:8f0bb79ddd48 160 if (!src_coap_msg_ptr) {
leothedragon 0:8f0bb79ddd48 161 return 0;
leothedragon 0:8f0bb79ddd48 162 }
leothedragon 0:8f0bb79ddd48 163 /* * * * * HEADER * * * * */
leothedragon 0:8f0bb79ddd48 164
leothedragon 0:8f0bb79ddd48 165 /* Header size is fixed */
leothedragon 0:8f0bb79ddd48 166 returned_byte_count = COAP_HEADER_LENGTH;
leothedragon 0:8f0bb79ddd48 167
leothedragon 0:8f0bb79ddd48 168 /* * * * * OPTIONS * * * * */
leothedragon 0:8f0bb79ddd48 169
leothedragon 0:8f0bb79ddd48 170 /* If else than Reset message because Reset message must be empty */
leothedragon 0:8f0bb79ddd48 171 if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_RESET) {
leothedragon 0:8f0bb79ddd48 172 uint16_t repeatable_option_size = 0;
leothedragon 0:8f0bb79ddd48 173 /* TOKEN - Length is 1-8 bytes */
leothedragon 0:8f0bb79ddd48 174 if (src_coap_msg_ptr->token_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 175 if (src_coap_msg_ptr->token_len > 8 || src_coap_msg_ptr->token_len < 1) { /* Check that option is not longer than defined */
leothedragon 0:8f0bb79ddd48 176 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - token too large!");
leothedragon 0:8f0bb79ddd48 177 return 0;
leothedragon 0:8f0bb79ddd48 178 }
leothedragon 0:8f0bb79ddd48 179
leothedragon 0:8f0bb79ddd48 180 returned_byte_count += src_coap_msg_ptr->token_len;
leothedragon 0:8f0bb79ddd48 181 }
leothedragon 0:8f0bb79ddd48 182 /* URI PATH - Repeatable option. Length of one option is 0-255 */
leothedragon 0:8f0bb79ddd48 183 if (src_coap_msg_ptr->uri_path_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 184 repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->uri_path_len,
leothedragon 0:8f0bb79ddd48 185 src_coap_msg_ptr->uri_path_ptr, COAP_OPTION_URI_PATH);
leothedragon 0:8f0bb79ddd48 186 if (repeatable_option_size) {
leothedragon 0:8f0bb79ddd48 187 returned_byte_count += repeatable_option_size;
leothedragon 0:8f0bb79ddd48 188 } else {
leothedragon 0:8f0bb79ddd48 189 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - uri path size failed!");
leothedragon 0:8f0bb79ddd48 190 return 0;
leothedragon 0:8f0bb79ddd48 191 }
leothedragon 0:8f0bb79ddd48 192 }
leothedragon 0:8f0bb79ddd48 193
leothedragon 0:8f0bb79ddd48 194 uint16_t tempInt = 0;
leothedragon 0:8f0bb79ddd48 195 /* CONTENT FORMAT - An integer option, up to 2 bytes */
leothedragon 0:8f0bb79ddd48 196 if (src_coap_msg_ptr->content_format != COAP_CT_NONE) {
leothedragon 0:8f0bb79ddd48 197 if ((uint32_t) src_coap_msg_ptr->content_format > 0xffff) {
leothedragon 0:8f0bb79ddd48 198 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - content format too large!");
leothedragon 0:8f0bb79ddd48 199 return 0;
leothedragon 0:8f0bb79ddd48 200 }
leothedragon 0:8f0bb79ddd48 201
leothedragon 0:8f0bb79ddd48 202 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->content_format, COAP_OPTION_CONTENT_FORMAT, &tempInt);
leothedragon 0:8f0bb79ddd48 203 }
leothedragon 0:8f0bb79ddd48 204 /* If options list pointer exists */
leothedragon 0:8f0bb79ddd48 205 if (src_coap_msg_ptr->options_list_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 206 /* ACCEPT - An integer option, up to 2 bytes */
leothedragon 0:8f0bb79ddd48 207 if (src_coap_msg_ptr->options_list_ptr->accept != COAP_CT_NONE) {
leothedragon 0:8f0bb79ddd48 208 if ((uint32_t) src_coap_msg_ptr->options_list_ptr->accept > 0xffff) {
leothedragon 0:8f0bb79ddd48 209 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - accept too large!");
leothedragon 0:8f0bb79ddd48 210 return 0;
leothedragon 0:8f0bb79ddd48 211 }
leothedragon 0:8f0bb79ddd48 212
leothedragon 0:8f0bb79ddd48 213 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->accept, COAP_OPTION_ACCEPT, &tempInt);
leothedragon 0:8f0bb79ddd48 214 }
leothedragon 0:8f0bb79ddd48 215 /* MAX AGE - An integer option, omitted for default. Up to 4 bytes */
leothedragon 0:8f0bb79ddd48 216 if (src_coap_msg_ptr->options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) {
leothedragon 0:8f0bb79ddd48 217 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->max_age, COAP_OPTION_MAX_AGE, &tempInt);
leothedragon 0:8f0bb79ddd48 218 }
leothedragon 0:8f0bb79ddd48 219 /* PROXY URI - Length of this option is 1-1034 bytes */
leothedragon 0:8f0bb79ddd48 220 if (src_coap_msg_ptr->options_list_ptr->proxy_uri_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 221 if (src_coap_msg_ptr->options_list_ptr->proxy_uri_len >= 1 && src_coap_msg_ptr->options_list_ptr->proxy_uri_len <= 12) { /* Add option header byte(s) - depending of option length */
leothedragon 0:8f0bb79ddd48 222 returned_byte_count++;
leothedragon 0:8f0bb79ddd48 223 }
leothedragon 0:8f0bb79ddd48 224
leothedragon 0:8f0bb79ddd48 225 else if (src_coap_msg_ptr->options_list_ptr->proxy_uri_len >= 13 && src_coap_msg_ptr->options_list_ptr->proxy_uri_len <= 269) {
leothedragon 0:8f0bb79ddd48 226 returned_byte_count += 2;
leothedragon 0:8f0bb79ddd48 227 }
leothedragon 0:8f0bb79ddd48 228
leothedragon 0:8f0bb79ddd48 229 else if (src_coap_msg_ptr->options_list_ptr->proxy_uri_len >= 270 && src_coap_msg_ptr->options_list_ptr->proxy_uri_len <= 1034) {
leothedragon 0:8f0bb79ddd48 230 returned_byte_count += 3;
leothedragon 0:8f0bb79ddd48 231 }
leothedragon 0:8f0bb79ddd48 232
leothedragon 0:8f0bb79ddd48 233 else {
leothedragon 0:8f0bb79ddd48 234 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - proxy uri too large!");
leothedragon 0:8f0bb79ddd48 235 return 0;
leothedragon 0:8f0bb79ddd48 236 }
leothedragon 0:8f0bb79ddd48 237
leothedragon 0:8f0bb79ddd48 238 /* Add needed memory for Option value */
leothedragon 0:8f0bb79ddd48 239 returned_byte_count += src_coap_msg_ptr->options_list_ptr->proxy_uri_len;
leothedragon 0:8f0bb79ddd48 240 }
leothedragon 0:8f0bb79ddd48 241 /* ETAG - Repeatable option. Length of this option is 1-8 bytes*/
leothedragon 0:8f0bb79ddd48 242 if (src_coap_msg_ptr->options_list_ptr->etag_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 243 repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->options_list_ptr->etag_len,
leothedragon 0:8f0bb79ddd48 244 src_coap_msg_ptr->options_list_ptr->etag_ptr, COAP_OPTION_ETAG);
leothedragon 0:8f0bb79ddd48 245 if (repeatable_option_size) {
leothedragon 0:8f0bb79ddd48 246 returned_byte_count += repeatable_option_size;
leothedragon 0:8f0bb79ddd48 247 } else {
leothedragon 0:8f0bb79ddd48 248 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - etag too large!");
leothedragon 0:8f0bb79ddd48 249 return 0;
leothedragon 0:8f0bb79ddd48 250 }
leothedragon 0:8f0bb79ddd48 251 }
leothedragon 0:8f0bb79ddd48 252 /* URI HOST - Length of this option is 1-255 bytes */
leothedragon 0:8f0bb79ddd48 253 if (src_coap_msg_ptr->options_list_ptr->uri_host_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 254 if (src_coap_msg_ptr->options_list_ptr->uri_host_len > 0 && src_coap_msg_ptr->options_list_ptr->uri_host_len <= 12) {
leothedragon 0:8f0bb79ddd48 255 returned_byte_count++;
leothedragon 0:8f0bb79ddd48 256 }
leothedragon 0:8f0bb79ddd48 257
leothedragon 0:8f0bb79ddd48 258 else if (src_coap_msg_ptr->options_list_ptr->uri_host_len >= 13 && src_coap_msg_ptr->options_list_ptr->uri_host_len <= 255) {
leothedragon 0:8f0bb79ddd48 259 returned_byte_count += 2;
leothedragon 0:8f0bb79ddd48 260 }
leothedragon 0:8f0bb79ddd48 261
leothedragon 0:8f0bb79ddd48 262 else {
leothedragon 0:8f0bb79ddd48 263 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - uri host too large!");
leothedragon 0:8f0bb79ddd48 264 return 0;
leothedragon 0:8f0bb79ddd48 265 }
leothedragon 0:8f0bb79ddd48 266
leothedragon 0:8f0bb79ddd48 267 returned_byte_count += src_coap_msg_ptr->options_list_ptr->uri_host_len;
leothedragon 0:8f0bb79ddd48 268 }
leothedragon 0:8f0bb79ddd48 269 /* LOCATION PATH - Repeatable option. Length of this option is 0-255 bytes*/
leothedragon 0:8f0bb79ddd48 270 if (src_coap_msg_ptr->options_list_ptr->location_path_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 271 repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->options_list_ptr->location_path_len,
leothedragon 0:8f0bb79ddd48 272 src_coap_msg_ptr->options_list_ptr->location_path_ptr, COAP_OPTION_LOCATION_PATH);
leothedragon 0:8f0bb79ddd48 273 if (repeatable_option_size) {
leothedragon 0:8f0bb79ddd48 274 returned_byte_count += repeatable_option_size;
leothedragon 0:8f0bb79ddd48 275 } else {
leothedragon 0:8f0bb79ddd48 276 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - location path too large!");
leothedragon 0:8f0bb79ddd48 277 return 0;
leothedragon 0:8f0bb79ddd48 278 }
leothedragon 0:8f0bb79ddd48 279 }
leothedragon 0:8f0bb79ddd48 280 /* URI PORT - An integer option, up to 2 bytes */
leothedragon 0:8f0bb79ddd48 281 if (src_coap_msg_ptr->options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) {
leothedragon 0:8f0bb79ddd48 282 if ((uint32_t) src_coap_msg_ptr->options_list_ptr->uri_port > 0xffff) {
leothedragon 0:8f0bb79ddd48 283 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - uri port too large!");
leothedragon 0:8f0bb79ddd48 284 return 0;
leothedragon 0:8f0bb79ddd48 285 }
leothedragon 0:8f0bb79ddd48 286 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->uri_port, COAP_OPTION_URI_PORT, &tempInt);
leothedragon 0:8f0bb79ddd48 287 }
leothedragon 0:8f0bb79ddd48 288 /* lOCATION QUERY - Repeatable option. Length of this option is 0-255 bytes */
leothedragon 0:8f0bb79ddd48 289 if (src_coap_msg_ptr->options_list_ptr->location_query_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 290 repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->options_list_ptr->location_query_len,
leothedragon 0:8f0bb79ddd48 291 src_coap_msg_ptr->options_list_ptr->location_query_ptr, COAP_OPTION_LOCATION_QUERY);
leothedragon 0:8f0bb79ddd48 292 if (repeatable_option_size) {
leothedragon 0:8f0bb79ddd48 293 returned_byte_count += repeatable_option_size;
leothedragon 0:8f0bb79ddd48 294 } else {
leothedragon 0:8f0bb79ddd48 295 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - location query too large!");
leothedragon 0:8f0bb79ddd48 296 return 0;
leothedragon 0:8f0bb79ddd48 297 }
leothedragon 0:8f0bb79ddd48 298 }
leothedragon 0:8f0bb79ddd48 299 /* OBSERVE - An integer option, up to 3 bytes */
leothedragon 0:8f0bb79ddd48 300 if (src_coap_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) {
leothedragon 0:8f0bb79ddd48 301 if ((uint32_t) src_coap_msg_ptr->options_list_ptr->observe > 0xffffff) {
leothedragon 0:8f0bb79ddd48 302 return 0;
leothedragon 0:8f0bb79ddd48 303 }
leothedragon 0:8f0bb79ddd48 304 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->observe, COAP_OPTION_OBSERVE, &tempInt);
leothedragon 0:8f0bb79ddd48 305 }
leothedragon 0:8f0bb79ddd48 306 /* URI QUERY - Repeatable option. Length of this option is 1-255 */
leothedragon 0:8f0bb79ddd48 307 if (src_coap_msg_ptr->options_list_ptr->uri_query_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 308 repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->options_list_ptr->uri_query_len,
leothedragon 0:8f0bb79ddd48 309 src_coap_msg_ptr->options_list_ptr->uri_query_ptr, COAP_OPTION_URI_QUERY);
leothedragon 0:8f0bb79ddd48 310 if (repeatable_option_size) {
leothedragon 0:8f0bb79ddd48 311 returned_byte_count += repeatable_option_size;
leothedragon 0:8f0bb79ddd48 312 } else {
leothedragon 0:8f0bb79ddd48 313 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - observe too large!");
leothedragon 0:8f0bb79ddd48 314 return 0;
leothedragon 0:8f0bb79ddd48 315 }
leothedragon 0:8f0bb79ddd48 316 }
leothedragon 0:8f0bb79ddd48 317
leothedragon 0:8f0bb79ddd48 318 /* BLOCK 1 - An integer option, up to 3 bytes */
leothedragon 0:8f0bb79ddd48 319 if (src_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) {
leothedragon 0:8f0bb79ddd48 320 if ((uint32_t) src_coap_msg_ptr->options_list_ptr->block1 > 0xffffff) {
leothedragon 0:8f0bb79ddd48 321 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - block1 too large!");
leothedragon 0:8f0bb79ddd48 322 return 0;
leothedragon 0:8f0bb79ddd48 323 }
leothedragon 0:8f0bb79ddd48 324 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->block1, COAP_OPTION_BLOCK1, &tempInt);
leothedragon 0:8f0bb79ddd48 325 }
leothedragon 0:8f0bb79ddd48 326 /* SIZE1 - Length of this option is 0-4 bytes */
leothedragon 0:8f0bb79ddd48 327 if (src_coap_msg_ptr->options_list_ptr->use_size1) {
leothedragon 0:8f0bb79ddd48 328 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->size1, COAP_OPTION_SIZE1, &tempInt);
leothedragon 0:8f0bb79ddd48 329 }
leothedragon 0:8f0bb79ddd48 330 /* BLOCK 2 - An integer option, up to 3 bytes */
leothedragon 0:8f0bb79ddd48 331 if (src_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) {
leothedragon 0:8f0bb79ddd48 332 if ((uint32_t) src_coap_msg_ptr->options_list_ptr->block2 > 0xffffff) {
leothedragon 0:8f0bb79ddd48 333 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - block2 too large!");
leothedragon 0:8f0bb79ddd48 334 return 0;
leothedragon 0:8f0bb79ddd48 335 }
leothedragon 0:8f0bb79ddd48 336 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->block2, COAP_OPTION_BLOCK2, &tempInt);
leothedragon 0:8f0bb79ddd48 337 }
leothedragon 0:8f0bb79ddd48 338 /* SIZE2 - Length of this option is 0-4 bytes */
leothedragon 0:8f0bb79ddd48 339 if (src_coap_msg_ptr->options_list_ptr->use_size2) {
leothedragon 0:8f0bb79ddd48 340 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->options_list_ptr->size2, COAP_OPTION_SIZE2, &tempInt);
leothedragon 0:8f0bb79ddd48 341 }
leothedragon 0:8f0bb79ddd48 342 }
leothedragon 0:8f0bb79ddd48 343 #if SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
leothedragon 0:8f0bb79ddd48 344 if ((src_coap_msg_ptr->payload_len > SN_COAP_MAX_NONBLOCKWISE_PAYLOAD_SIZE) &&
leothedragon 0:8f0bb79ddd48 345 (src_coap_msg_ptr->payload_len > blockwise_payload_size) &&
leothedragon 0:8f0bb79ddd48 346 (blockwise_payload_size > 0)) {
leothedragon 0:8f0bb79ddd48 347 returned_byte_count += blockwise_payload_size;
leothedragon 0:8f0bb79ddd48 348 } else {
leothedragon 0:8f0bb79ddd48 349 returned_byte_count += src_coap_msg_ptr->payload_len;
leothedragon 0:8f0bb79ddd48 350 }
leothedragon 0:8f0bb79ddd48 351 #else
leothedragon 0:8f0bb79ddd48 352 returned_byte_count += src_coap_msg_ptr->payload_len;
leothedragon 0:8f0bb79ddd48 353 #endif
leothedragon 0:8f0bb79ddd48 354 if (src_coap_msg_ptr->payload_len) {
leothedragon 0:8f0bb79ddd48 355 returned_byte_count ++; /* For payload marker */
leothedragon 0:8f0bb79ddd48 356 }
leothedragon 0:8f0bb79ddd48 357 returned_byte_count += sn_coap_builder_options_calculate_jump_need(src_coap_msg_ptr/*, 0*/);
leothedragon 0:8f0bb79ddd48 358 }
leothedragon 0:8f0bb79ddd48 359 return returned_byte_count;
leothedragon 0:8f0bb79ddd48 360 }
leothedragon 0:8f0bb79ddd48 361 /**
leothedragon 0:8f0bb79ddd48 362 * \fn static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_coap_msg_ptr, uint8_t block_option)
leothedragon 0:8f0bb79ddd48 363 *
leothedragon 0:8f0bb79ddd48 364 * \brief Checks if there is need for option jump
leothedragon 0:8f0bb79ddd48 365 *
leothedragon 0:8f0bb79ddd48 366 * \param *src_coap_msg_ptr is source of checked CoAP message
leothedragon 0:8f0bb79ddd48 367 *
leothedragon 0:8f0bb79ddd48 368 * \param block option marks if block option is to be added to message later. 0 = no block option, 1 = block1 and 2 = block2
leothedragon 0:8f0bb79ddd48 369 *
leothedragon 0:8f0bb79ddd48 370 * \return Returns bytes needed for jumping
leothedragon 0:8f0bb79ddd48 371 */
leothedragon 0:8f0bb79ddd48 372
leothedragon 0:8f0bb79ddd48 373 static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_coap_msg_ptr/*, uint8_t block_option*/)
leothedragon 0:8f0bb79ddd48 374 {
leothedragon 0:8f0bb79ddd48 375 uint8_t previous_option_number = 0;
leothedragon 0:8f0bb79ddd48 376 uint8_t needed_space = 0;
leothedragon 0:8f0bb79ddd48 377
leothedragon 0:8f0bb79ddd48 378 if (src_coap_msg_ptr->options_list_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 379 /* If option numbers greater than 12 is not used, then jumping is not needed */
leothedragon 0:8f0bb79ddd48 380 //TODO: Check if this is really needed! Does it enhance perf? If not -> remove
leothedragon 0:8f0bb79ddd48 381 if (!src_coap_msg_ptr->options_list_ptr->uri_query_ptr &&
leothedragon 0:8f0bb79ddd48 382 src_coap_msg_ptr->options_list_ptr->accept == COAP_CT_NONE &&
leothedragon 0:8f0bb79ddd48 383 !src_coap_msg_ptr->options_list_ptr->location_query_ptr &&
leothedragon 0:8f0bb79ddd48 384 src_coap_msg_ptr->options_list_ptr->block2 == COAP_OPTION_BLOCK_NONE &&
leothedragon 0:8f0bb79ddd48 385 src_coap_msg_ptr->options_list_ptr->block1 == COAP_OPTION_BLOCK_NONE &&
leothedragon 0:8f0bb79ddd48 386 !src_coap_msg_ptr->options_list_ptr->proxy_uri_ptr &&
leothedragon 0:8f0bb79ddd48 387 src_coap_msg_ptr->options_list_ptr->max_age == COAP_OPTION_MAX_AGE_DEFAULT &&
leothedragon 0:8f0bb79ddd48 388 !src_coap_msg_ptr->options_list_ptr->use_size1 &&
leothedragon 0:8f0bb79ddd48 389 !src_coap_msg_ptr->options_list_ptr->use_size2) {
leothedragon 0:8f0bb79ddd48 390 return 0;
leothedragon 0:8f0bb79ddd48 391 }
leothedragon 0:8f0bb79ddd48 392
leothedragon 0:8f0bb79ddd48 393 if (src_coap_msg_ptr->options_list_ptr->uri_host_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 394 previous_option_number = (COAP_OPTION_URI_HOST);
leothedragon 0:8f0bb79ddd48 395 }
leothedragon 0:8f0bb79ddd48 396
leothedragon 0:8f0bb79ddd48 397 if (src_coap_msg_ptr->options_list_ptr->etag_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 398 previous_option_number = (COAP_OPTION_ETAG);
leothedragon 0:8f0bb79ddd48 399 }
leothedragon 0:8f0bb79ddd48 400
leothedragon 0:8f0bb79ddd48 401 if (src_coap_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) {
leothedragon 0:8f0bb79ddd48 402 previous_option_number = (COAP_OPTION_OBSERVE);
leothedragon 0:8f0bb79ddd48 403 }
leothedragon 0:8f0bb79ddd48 404
leothedragon 0:8f0bb79ddd48 405 if (src_coap_msg_ptr->options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) {
leothedragon 0:8f0bb79ddd48 406 previous_option_number = (COAP_OPTION_URI_PORT);
leothedragon 0:8f0bb79ddd48 407 }
leothedragon 0:8f0bb79ddd48 408
leothedragon 0:8f0bb79ddd48 409 if (src_coap_msg_ptr->options_list_ptr->location_path_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 410 previous_option_number = (COAP_OPTION_LOCATION_PATH);
leothedragon 0:8f0bb79ddd48 411 }
leothedragon 0:8f0bb79ddd48 412
leothedragon 0:8f0bb79ddd48 413 if (src_coap_msg_ptr->uri_path_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 414 previous_option_number = (COAP_OPTION_URI_PATH);
leothedragon 0:8f0bb79ddd48 415 }
leothedragon 0:8f0bb79ddd48 416 if (src_coap_msg_ptr->content_format != COAP_CT_NONE) {
leothedragon 0:8f0bb79ddd48 417 previous_option_number = (COAP_OPTION_CONTENT_FORMAT);
leothedragon 0:8f0bb79ddd48 418 }
leothedragon 0:8f0bb79ddd48 419 if (src_coap_msg_ptr->options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) {
leothedragon 0:8f0bb79ddd48 420 if ((COAP_OPTION_MAX_AGE - previous_option_number) > 12) {
leothedragon 0:8f0bb79ddd48 421 needed_space += 1;
leothedragon 0:8f0bb79ddd48 422 }
leothedragon 0:8f0bb79ddd48 423 previous_option_number = (COAP_OPTION_MAX_AGE);
leothedragon 0:8f0bb79ddd48 424 }
leothedragon 0:8f0bb79ddd48 425
leothedragon 0:8f0bb79ddd48 426 if (src_coap_msg_ptr->options_list_ptr->uri_query_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 427 if ((COAP_OPTION_URI_QUERY - previous_option_number) > 12) {
leothedragon 0:8f0bb79ddd48 428 needed_space += 1;
leothedragon 0:8f0bb79ddd48 429 }
leothedragon 0:8f0bb79ddd48 430 previous_option_number = (COAP_OPTION_URI_QUERY);
leothedragon 0:8f0bb79ddd48 431 }
leothedragon 0:8f0bb79ddd48 432 if (src_coap_msg_ptr->options_list_ptr->accept != COAP_CT_NONE) {
leothedragon 0:8f0bb79ddd48 433 if ((COAP_OPTION_ACCEPT - previous_option_number) > 12) {
leothedragon 0:8f0bb79ddd48 434 needed_space += 1;
leothedragon 0:8f0bb79ddd48 435 }
leothedragon 0:8f0bb79ddd48 436 previous_option_number = (COAP_OPTION_ACCEPT);
leothedragon 0:8f0bb79ddd48 437 }
leothedragon 0:8f0bb79ddd48 438 if (src_coap_msg_ptr->options_list_ptr->location_query_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 439 if ((COAP_OPTION_LOCATION_QUERY - previous_option_number) > 12) {
leothedragon 0:8f0bb79ddd48 440 needed_space += 1;
leothedragon 0:8f0bb79ddd48 441 }
leothedragon 0:8f0bb79ddd48 442 previous_option_number = (COAP_OPTION_LOCATION_QUERY);
leothedragon 0:8f0bb79ddd48 443 }
leothedragon 0:8f0bb79ddd48 444 if (src_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) {
leothedragon 0:8f0bb79ddd48 445 if ((COAP_OPTION_BLOCK2 - previous_option_number) > 12 ){
leothedragon 0:8f0bb79ddd48 446 needed_space += 1;
leothedragon 0:8f0bb79ddd48 447 }
leothedragon 0:8f0bb79ddd48 448 previous_option_number = (COAP_OPTION_BLOCK2);
leothedragon 0:8f0bb79ddd48 449 }
leothedragon 0:8f0bb79ddd48 450 if (src_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) {
leothedragon 0:8f0bb79ddd48 451 if ((COAP_OPTION_BLOCK1 - previous_option_number) > 12 ){
leothedragon 0:8f0bb79ddd48 452 needed_space += 1;
leothedragon 0:8f0bb79ddd48 453 }
leothedragon 0:8f0bb79ddd48 454 previous_option_number = (COAP_OPTION_BLOCK1);
leothedragon 0:8f0bb79ddd48 455 }
leothedragon 0:8f0bb79ddd48 456 if (src_coap_msg_ptr->options_list_ptr->use_size2) {
leothedragon 0:8f0bb79ddd48 457 if ((COAP_OPTION_SIZE2 - previous_option_number) > 12) {
leothedragon 0:8f0bb79ddd48 458 needed_space += 1;
leothedragon 0:8f0bb79ddd48 459 }
leothedragon 0:8f0bb79ddd48 460 previous_option_number = (COAP_OPTION_SIZE2);
leothedragon 0:8f0bb79ddd48 461 }
leothedragon 0:8f0bb79ddd48 462 if (src_coap_msg_ptr->options_list_ptr->proxy_uri_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 463 if ((COAP_OPTION_PROXY_URI - previous_option_number) > 12) {
leothedragon 0:8f0bb79ddd48 464 needed_space += 1;
leothedragon 0:8f0bb79ddd48 465 }
leothedragon 0:8f0bb79ddd48 466 if ((COAP_OPTION_PROXY_URI - previous_option_number) > 269) { //Can not happen
leothedragon 0:8f0bb79ddd48 467 needed_space += 1;
leothedragon 0:8f0bb79ddd48 468 }
leothedragon 0:8f0bb79ddd48 469 previous_option_number = (COAP_OPTION_PROXY_URI);
leothedragon 0:8f0bb79ddd48 470 }
leothedragon 0:8f0bb79ddd48 471 if (src_coap_msg_ptr->options_list_ptr->use_size1 ) {
leothedragon 0:8f0bb79ddd48 472 if ((COAP_OPTION_SIZE1 - previous_option_number) > 12) {
leothedragon 0:8f0bb79ddd48 473 needed_space += 1;
leothedragon 0:8f0bb79ddd48 474 }
leothedragon 0:8f0bb79ddd48 475 previous_option_number = (COAP_OPTION_SIZE1);
leothedragon 0:8f0bb79ddd48 476 }
leothedragon 0:8f0bb79ddd48 477 }
leothedragon 0:8f0bb79ddd48 478
leothedragon 0:8f0bb79ddd48 479 else {
leothedragon 0:8f0bb79ddd48 480 if (src_coap_msg_ptr->uri_path_ptr != 0) {
leothedragon 0:8f0bb79ddd48 481 previous_option_number = (COAP_OPTION_URI_PATH);
leothedragon 0:8f0bb79ddd48 482 }
leothedragon 0:8f0bb79ddd48 483
leothedragon 0:8f0bb79ddd48 484 if (src_coap_msg_ptr->content_format != COAP_CT_NONE) {
leothedragon 0:8f0bb79ddd48 485 previous_option_number = (COAP_OPTION_CONTENT_FORMAT);
leothedragon 0:8f0bb79ddd48 486 }
leothedragon 0:8f0bb79ddd48 487 }
leothedragon 0:8f0bb79ddd48 488 return needed_space;
leothedragon 0:8f0bb79ddd48 489 }
leothedragon 0:8f0bb79ddd48 490
leothedragon 0:8f0bb79ddd48 491 /**
leothedragon 0:8f0bb79ddd48 492 * \fn static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr)
leothedragon 0:8f0bb79ddd48 493 *
leothedragon 0:8f0bb79ddd48 494 * \brief Builds Header part of Packet data
leothedragon 0:8f0bb79ddd48 495 *
leothedragon 0:8f0bb79ddd48 496 * \param **dst_packet_data_pptr is destination for built Packet data
leothedragon 0:8f0bb79ddd48 497 *
leothedragon 0:8f0bb79ddd48 498 * \param *src_coap_msg_ptr is source for building Packet data
leothedragon 0:8f0bb79ddd48 499 *
leothedragon 0:8f0bb79ddd48 500 * \return Return value is 0 in ok case and -1 in failure case
leothedragon 0:8f0bb79ddd48 501 **************************************************************************** */
leothedragon 0:8f0bb79ddd48 502 static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr)
leothedragon 0:8f0bb79ddd48 503 {
leothedragon 0:8f0bb79ddd48 504 /* * * * Check validity of Header values * * * */
leothedragon 0:8f0bb79ddd48 505 if (sn_coap_header_validity_check(src_coap_msg_ptr, COAP_VERSION) != 0) {
leothedragon 0:8f0bb79ddd48 506 tr_error("sn_coap_builder_header_build - header build failed!");
leothedragon 0:8f0bb79ddd48 507 return -1;
leothedragon 0:8f0bb79ddd48 508 }
leothedragon 0:8f0bb79ddd48 509
leothedragon 0:8f0bb79ddd48 510 /* * * Add CoAP Version * * */
leothedragon 0:8f0bb79ddd48 511 **dst_packet_data_pptr += COAP_VERSION;
leothedragon 0:8f0bb79ddd48 512
leothedragon 0:8f0bb79ddd48 513 /* * * Add Message type * * */
leothedragon 0:8f0bb79ddd48 514 **dst_packet_data_pptr += src_coap_msg_ptr->msg_type;
leothedragon 0:8f0bb79ddd48 515
leothedragon 0:8f0bb79ddd48 516 /* * * Add Token length * * */
leothedragon 0:8f0bb79ddd48 517 **dst_packet_data_pptr += (src_coap_msg_ptr->token_len);
leothedragon 0:8f0bb79ddd48 518
leothedragon 0:8f0bb79ddd48 519 (*dst_packet_data_pptr) ++;
leothedragon 0:8f0bb79ddd48 520 /* * * Add Message code * * */
leothedragon 0:8f0bb79ddd48 521 **dst_packet_data_pptr = src_coap_msg_ptr->msg_code;
leothedragon 0:8f0bb79ddd48 522 (*dst_packet_data_pptr) ++;
leothedragon 0:8f0bb79ddd48 523
leothedragon 0:8f0bb79ddd48 524 /* * * Add Message ID * * */
leothedragon 0:8f0bb79ddd48 525 **dst_packet_data_pptr = (uint8_t)(src_coap_msg_ptr->msg_id >> COAP_HEADER_MSG_ID_MSB_SHIFT); /* MSB part */
leothedragon 0:8f0bb79ddd48 526 (*dst_packet_data_pptr) ++;
leothedragon 0:8f0bb79ddd48 527 **dst_packet_data_pptr = (uint8_t)src_coap_msg_ptr->msg_id; /* LSB part */
leothedragon 0:8f0bb79ddd48 528 (*dst_packet_data_pptr) ++;
leothedragon 0:8f0bb79ddd48 529
leothedragon 0:8f0bb79ddd48 530 /* Success */
leothedragon 0:8f0bb79ddd48 531 return 0;
leothedragon 0:8f0bb79ddd48 532 }
leothedragon 0:8f0bb79ddd48 533
leothedragon 0:8f0bb79ddd48 534 /**
leothedragon 0:8f0bb79ddd48 535 * \fn static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr)
leothedragon 0:8f0bb79ddd48 536 *
leothedragon 0:8f0bb79ddd48 537 * \brief Builds Options part of Packet data
leothedragon 0:8f0bb79ddd48 538 *
leothedragon 0:8f0bb79ddd48 539 * \param **dst_packet_data_pptr is destination for built Packet data
leothedragon 0:8f0bb79ddd48 540 *
leothedragon 0:8f0bb79ddd48 541 * \param *src_coap_msg_ptr is source for building Packet data
leothedragon 0:8f0bb79ddd48 542 *
leothedragon 0:8f0bb79ddd48 543 * \return Return value is 0 in every case
leothedragon 0:8f0bb79ddd48 544 */
leothedragon 0:8f0bb79ddd48 545 static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr)
leothedragon 0:8f0bb79ddd48 546 {
leothedragon 0:8f0bb79ddd48 547 /* * * * Check if Options are used at all * * * */
leothedragon 0:8f0bb79ddd48 548 if (src_coap_msg_ptr->uri_path_ptr == NULL && src_coap_msg_ptr->token_ptr == NULL &&
leothedragon 0:8f0bb79ddd48 549 src_coap_msg_ptr->content_format == COAP_CT_NONE && src_coap_msg_ptr->options_list_ptr == NULL) {
leothedragon 0:8f0bb79ddd48 550 /* If the empty message is confirmable it is CoAP ping. */
leothedragon 0:8f0bb79ddd48 551 if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_CONFIRMABLE) {
leothedragon 0:8f0bb79ddd48 552 tr_error("sn_coap_builder_options_build - options not used!");
leothedragon 0:8f0bb79ddd48 553 }
leothedragon 0:8f0bb79ddd48 554 return 0;
leothedragon 0:8f0bb79ddd48 555 }
leothedragon 0:8f0bb79ddd48 556
leothedragon 0:8f0bb79ddd48 557 /* * * * First add Token option * * * */
leothedragon 0:8f0bb79ddd48 558 if (src_coap_msg_ptr->token_len && src_coap_msg_ptr->token_ptr) {
leothedragon 0:8f0bb79ddd48 559 memcpy(*dst_packet_data_pptr, src_coap_msg_ptr->token_ptr, src_coap_msg_ptr->token_len);
leothedragon 0:8f0bb79ddd48 560 }
leothedragon 0:8f0bb79ddd48 561 (*dst_packet_data_pptr) += src_coap_msg_ptr->token_len;
leothedragon 0:8f0bb79ddd48 562
leothedragon 0:8f0bb79ddd48 563 /* Then build rest of the options */
leothedragon 0:8f0bb79ddd48 564
leothedragon 0:8f0bb79ddd48 565 /* * * * Initialize previous Option number for new built message * * * */
leothedragon 0:8f0bb79ddd48 566 uint16_t previous_option_number = 0;
leothedragon 0:8f0bb79ddd48 567
leothedragon 0:8f0bb79ddd48 568 //missing: COAP_OPTION_IF_MATCH, COAP_OPTION_IF_NONE_MATCH, COAP_OPTION_SIZE
leothedragon 0:8f0bb79ddd48 569
leothedragon 0:8f0bb79ddd48 570 /* Check if less used options are used at all */
leothedragon 0:8f0bb79ddd48 571 if (src_coap_msg_ptr->options_list_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 572 /* * * * Build Uri-Host option * * * */
leothedragon 0:8f0bb79ddd48 573 sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->uri_host_len,
leothedragon 0:8f0bb79ddd48 574 src_coap_msg_ptr->options_list_ptr->uri_host_ptr, COAP_OPTION_URI_HOST, &previous_option_number);
leothedragon 0:8f0bb79ddd48 575
leothedragon 0:8f0bb79ddd48 576 /* * * * Build ETag option * * * */
leothedragon 0:8f0bb79ddd48 577 sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->options_list_ptr->etag_ptr,
leothedragon 0:8f0bb79ddd48 578 (uint16_t *)&src_coap_msg_ptr->options_list_ptr->etag_len, COAP_OPTION_ETAG, &previous_option_number);
leothedragon 0:8f0bb79ddd48 579
leothedragon 0:8f0bb79ddd48 580 /* * * * Build Observe option * * * * */
leothedragon 0:8f0bb79ddd48 581 if (src_coap_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) {
leothedragon 0:8f0bb79ddd48 582 sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->observe,
leothedragon 0:8f0bb79ddd48 583 COAP_OPTION_OBSERVE, &previous_option_number);
leothedragon 0:8f0bb79ddd48 584 }
leothedragon 0:8f0bb79ddd48 585
leothedragon 0:8f0bb79ddd48 586 /* * * * Build Uri-Port option * * * */
leothedragon 0:8f0bb79ddd48 587 if (src_coap_msg_ptr->options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) {
leothedragon 0:8f0bb79ddd48 588 sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->uri_port,
leothedragon 0:8f0bb79ddd48 589 COAP_OPTION_URI_PORT, &previous_option_number);
leothedragon 0:8f0bb79ddd48 590 }
leothedragon 0:8f0bb79ddd48 591
leothedragon 0:8f0bb79ddd48 592 /* * * * Build Location-Path option * * * */
leothedragon 0:8f0bb79ddd48 593 sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->options_list_ptr->location_path_ptr,
leothedragon 0:8f0bb79ddd48 594 &src_coap_msg_ptr->options_list_ptr->location_path_len, COAP_OPTION_LOCATION_PATH, &previous_option_number);
leothedragon 0:8f0bb79ddd48 595 }
leothedragon 0:8f0bb79ddd48 596 /* * * * Build Uri-Path option * * * */
leothedragon 0:8f0bb79ddd48 597 sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->uri_path_ptr,
leothedragon 0:8f0bb79ddd48 598 &src_coap_msg_ptr->uri_path_len, COAP_OPTION_URI_PATH, &previous_option_number);
leothedragon 0:8f0bb79ddd48 599
leothedragon 0:8f0bb79ddd48 600 /* * * * Build Content-Type option * * * */
leothedragon 0:8f0bb79ddd48 601 if (src_coap_msg_ptr->content_format != COAP_CT_NONE) {
leothedragon 0:8f0bb79ddd48 602 sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->content_format,
leothedragon 0:8f0bb79ddd48 603 COAP_OPTION_CONTENT_FORMAT, &previous_option_number);
leothedragon 0:8f0bb79ddd48 604 }
leothedragon 0:8f0bb79ddd48 605
leothedragon 0:8f0bb79ddd48 606 if (src_coap_msg_ptr->options_list_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 607 /* * * * Build Max-Age option * * * */
leothedragon 0:8f0bb79ddd48 608 if (src_coap_msg_ptr->options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) {
leothedragon 0:8f0bb79ddd48 609 sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->max_age,
leothedragon 0:8f0bb79ddd48 610 COAP_OPTION_MAX_AGE, &previous_option_number);
leothedragon 0:8f0bb79ddd48 611 }
leothedragon 0:8f0bb79ddd48 612
leothedragon 0:8f0bb79ddd48 613 /* * * * Build Uri-Query option * * * * */
leothedragon 0:8f0bb79ddd48 614 sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->options_list_ptr->uri_query_ptr,
leothedragon 0:8f0bb79ddd48 615 &src_coap_msg_ptr->options_list_ptr->uri_query_len, COAP_OPTION_URI_QUERY, &previous_option_number);
leothedragon 0:8f0bb79ddd48 616
leothedragon 0:8f0bb79ddd48 617 /* * * * Build Accept option * * * * */
leothedragon 0:8f0bb79ddd48 618 if (src_coap_msg_ptr->options_list_ptr->accept != COAP_CT_NONE) {
leothedragon 0:8f0bb79ddd48 619 sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->accept,
leothedragon 0:8f0bb79ddd48 620 COAP_OPTION_ACCEPT, &previous_option_number);
leothedragon 0:8f0bb79ddd48 621 }
leothedragon 0:8f0bb79ddd48 622 }
leothedragon 0:8f0bb79ddd48 623
leothedragon 0:8f0bb79ddd48 624 if (src_coap_msg_ptr->options_list_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 625 /* * * * Build Location-Query option * * * */
leothedragon 0:8f0bb79ddd48 626 sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->options_list_ptr->location_query_ptr,
leothedragon 0:8f0bb79ddd48 627 &src_coap_msg_ptr->options_list_ptr->location_query_len, COAP_OPTION_LOCATION_QUERY, &previous_option_number);
leothedragon 0:8f0bb79ddd48 628
leothedragon 0:8f0bb79ddd48 629 /* * * * Build Block2 option * * * * */
leothedragon 0:8f0bb79ddd48 630 if (src_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) {
leothedragon 0:8f0bb79ddd48 631 sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->block2,
leothedragon 0:8f0bb79ddd48 632 COAP_OPTION_BLOCK2, &previous_option_number);
leothedragon 0:8f0bb79ddd48 633 }
leothedragon 0:8f0bb79ddd48 634
leothedragon 0:8f0bb79ddd48 635 /* * * * Build Block1 option * * * * */
leothedragon 0:8f0bb79ddd48 636 if (src_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) {
leothedragon 0:8f0bb79ddd48 637 sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->block1,
leothedragon 0:8f0bb79ddd48 638 COAP_OPTION_BLOCK1, &previous_option_number);
leothedragon 0:8f0bb79ddd48 639 }
leothedragon 0:8f0bb79ddd48 640
leothedragon 0:8f0bb79ddd48 641 /* * * * Build Size2 option * * * */
leothedragon 0:8f0bb79ddd48 642 if (src_coap_msg_ptr->options_list_ptr->use_size2) {
leothedragon 0:8f0bb79ddd48 643 sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->size2,
leothedragon 0:8f0bb79ddd48 644 COAP_OPTION_SIZE2, &previous_option_number);
leothedragon 0:8f0bb79ddd48 645 }
leothedragon 0:8f0bb79ddd48 646
leothedragon 0:8f0bb79ddd48 647 /* * * * Build Proxy-Uri option * * * */
leothedragon 0:8f0bb79ddd48 648 sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->proxy_uri_len,
leothedragon 0:8f0bb79ddd48 649 src_coap_msg_ptr->options_list_ptr->proxy_uri_ptr, COAP_OPTION_PROXY_URI, &previous_option_number);
leothedragon 0:8f0bb79ddd48 650
leothedragon 0:8f0bb79ddd48 651
leothedragon 0:8f0bb79ddd48 652 /* * * * Build Size1 option * * * */
leothedragon 0:8f0bb79ddd48 653 if (src_coap_msg_ptr->options_list_ptr->use_size1) {
leothedragon 0:8f0bb79ddd48 654 sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->options_list_ptr->size1,
leothedragon 0:8f0bb79ddd48 655 COAP_OPTION_SIZE1, &previous_option_number);
leothedragon 0:8f0bb79ddd48 656 }
leothedragon 0:8f0bb79ddd48 657 }
leothedragon 0:8f0bb79ddd48 658
leothedragon 0:8f0bb79ddd48 659 /* Success */
leothedragon 0:8f0bb79ddd48 660 return 0;
leothedragon 0:8f0bb79ddd48 661 }
leothedragon 0:8f0bb79ddd48 662
leothedragon 0:8f0bb79ddd48 663 /**
leothedragon 0:8f0bb79ddd48 664 * \fn static int16_t sn_coap_builder_options_build_add_one_option(uint8_t **dst_packet_data_pptr, uint16_t option_value_len, uint8_t *option_value_ptr, sn_coap_option_numbers_e option_number)
leothedragon 0:8f0bb79ddd48 665 *
leothedragon 0:8f0bb79ddd48 666 * \brief Adds Options part of Packet data
leothedragon 0:8f0bb79ddd48 667 *
leothedragon 0:8f0bb79ddd48 668 * \param **dst_packet_data_pptr is destination for built Packet data
leothedragon 0:8f0bb79ddd48 669 *
leothedragon 0:8f0bb79ddd48 670 * \param option_value_len is Option value length to be added
leothedragon 0:8f0bb79ddd48 671 *
leothedragon 0:8f0bb79ddd48 672 * \param *option_value_ptr is pointer to Option value data to be added
leothedragon 0:8f0bb79ddd48 673 *
leothedragon 0:8f0bb79ddd48 674 * \param option_number is Option number to be added
leothedragon 0:8f0bb79ddd48 675 *
leothedragon 0:8f0bb79ddd48 676 * \return Return value is 0 if option was not added, 1 if added
leothedragon 0:8f0bb79ddd48 677 */
leothedragon 0:8f0bb79ddd48 678 static int16_t sn_coap_builder_options_build_add_one_option(uint8_t **dst_packet_data_pptr, uint16_t option_len,
leothedragon 0:8f0bb79ddd48 679 uint8_t *option_ptr, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number)
leothedragon 0:8f0bb79ddd48 680 {
leothedragon 0:8f0bb79ddd48 681 /* Check if there is option at all */
leothedragon 0:8f0bb79ddd48 682 if (option_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 683 uint16_t option_delta;
leothedragon 0:8f0bb79ddd48 684
leothedragon 0:8f0bb79ddd48 685 option_delta = (option_number - *previous_option_number);
leothedragon 0:8f0bb79ddd48 686
leothedragon 0:8f0bb79ddd48 687 /* * * Build option header * * */
leothedragon 0:8f0bb79ddd48 688
leothedragon 0:8f0bb79ddd48 689 /* First option length without extended part */
leothedragon 0:8f0bb79ddd48 690 if (option_len <= 12) {
leothedragon 0:8f0bb79ddd48 691 **dst_packet_data_pptr = option_len;
leothedragon 0:8f0bb79ddd48 692 }
leothedragon 0:8f0bb79ddd48 693
leothedragon 0:8f0bb79ddd48 694 else if (option_len > 12 && option_len < 269) {
leothedragon 0:8f0bb79ddd48 695 **dst_packet_data_pptr = 0x0D;
leothedragon 0:8f0bb79ddd48 696 }
leothedragon 0:8f0bb79ddd48 697
leothedragon 0:8f0bb79ddd48 698 else if (option_len >= 269) {
leothedragon 0:8f0bb79ddd48 699 **dst_packet_data_pptr = 0x0E;
leothedragon 0:8f0bb79ddd48 700 }
leothedragon 0:8f0bb79ddd48 701
leothedragon 0:8f0bb79ddd48 702 /* Then option delta with extensions, and move pointer */
leothedragon 0:8f0bb79ddd48 703 if (option_delta <= 12) {
leothedragon 0:8f0bb79ddd48 704 **dst_packet_data_pptr += (option_delta << 4);
leothedragon 0:8f0bb79ddd48 705 *dst_packet_data_pptr += 1;
leothedragon 0:8f0bb79ddd48 706 }
leothedragon 0:8f0bb79ddd48 707
leothedragon 0:8f0bb79ddd48 708 else if (option_delta > 12 && option_delta < 269) {
leothedragon 0:8f0bb79ddd48 709 **dst_packet_data_pptr += 0xD0;
leothedragon 0:8f0bb79ddd48 710 option_delta -= 13;
leothedragon 0:8f0bb79ddd48 711
leothedragon 0:8f0bb79ddd48 712 *(*dst_packet_data_pptr + 1) = (uint8_t)option_delta;
leothedragon 0:8f0bb79ddd48 713 *dst_packet_data_pptr += 2;
leothedragon 0:8f0bb79ddd48 714 }
leothedragon 0:8f0bb79ddd48 715 //This is currently dead code (but possibly needed in future)
leothedragon 0:8f0bb79ddd48 716 else if (option_delta >= 269) {
leothedragon 0:8f0bb79ddd48 717 **dst_packet_data_pptr += 0xE0;
leothedragon 0:8f0bb79ddd48 718 option_delta -= 269;
leothedragon 0:8f0bb79ddd48 719
leothedragon 0:8f0bb79ddd48 720 *(*dst_packet_data_pptr + 2) = (uint8_t)option_delta;
leothedragon 0:8f0bb79ddd48 721 *(*dst_packet_data_pptr + 1) = (option_delta >> 8);
leothedragon 0:8f0bb79ddd48 722 *dst_packet_data_pptr += 3;
leothedragon 0:8f0bb79ddd48 723 }
leothedragon 0:8f0bb79ddd48 724
leothedragon 0:8f0bb79ddd48 725 /* Now option length extensions, if needed */
leothedragon 0:8f0bb79ddd48 726 if (option_len > 12 && option_len < 269) {
leothedragon 0:8f0bb79ddd48 727 **dst_packet_data_pptr = (uint8_t)(option_len - 13);
leothedragon 0:8f0bb79ddd48 728 *dst_packet_data_pptr += 1;
leothedragon 0:8f0bb79ddd48 729 }
leothedragon 0:8f0bb79ddd48 730
leothedragon 0:8f0bb79ddd48 731 else if (option_len >= 269) {
leothedragon 0:8f0bb79ddd48 732 *(*dst_packet_data_pptr + 1) = (uint8_t)(option_len - 269);
leothedragon 0:8f0bb79ddd48 733 **dst_packet_data_pptr = ((option_len - 269) >> 8);
leothedragon 0:8f0bb79ddd48 734 *dst_packet_data_pptr += 2;
leothedragon 0:8f0bb79ddd48 735 }
leothedragon 0:8f0bb79ddd48 736
leothedragon 0:8f0bb79ddd48 737 *previous_option_number = option_number;
leothedragon 0:8f0bb79ddd48 738
leothedragon 0:8f0bb79ddd48 739 /* Write Option value */
leothedragon 0:8f0bb79ddd48 740 memcpy(*dst_packet_data_pptr, option_ptr, option_len);
leothedragon 0:8f0bb79ddd48 741
leothedragon 0:8f0bb79ddd48 742 /* Increase destination Packet data pointer */
leothedragon 0:8f0bb79ddd48 743 (*dst_packet_data_pptr) += option_len;
leothedragon 0:8f0bb79ddd48 744
leothedragon 0:8f0bb79ddd48 745 return 1;
leothedragon 0:8f0bb79ddd48 746 }
leothedragon 0:8f0bb79ddd48 747
leothedragon 0:8f0bb79ddd48 748 /* Success */
leothedragon 0:8f0bb79ddd48 749 return 0;
leothedragon 0:8f0bb79ddd48 750 }
leothedragon 0:8f0bb79ddd48 751
leothedragon 0:8f0bb79ddd48 752 /**
leothedragon 0:8f0bb79ddd48 753 * \brief Constructs a uint Options part of Packet data
leothedragon 0:8f0bb79ddd48 754 *
leothedragon 0:8f0bb79ddd48 755 * \param **dst_packet_data_pptr is destination for built Packet data; NULL
leothedragon 0:8f0bb79ddd48 756 * to compute size only.
leothedragon 0:8f0bb79ddd48 757 *
leothedragon 0:8f0bb79ddd48 758 * \param option_value is Option value to be added
leothedragon 0:8f0bb79ddd48 759 *
leothedragon 0:8f0bb79ddd48 760 * \param option_number is Option number to be added
leothedragon 0:8f0bb79ddd48 761 *
leothedragon 0:8f0bb79ddd48 762 * \return Return value is total option size, or -1 in write failure case
leothedragon 0:8f0bb79ddd48 763 */
leothedragon 0:8f0bb79ddd48 764 static uint8_t sn_coap_builder_options_build_add_uint_option(uint8_t **dst_packet_data_pptr, uint32_t option_value, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number)
leothedragon 0:8f0bb79ddd48 765 {
leothedragon 0:8f0bb79ddd48 766 uint8_t payload[4];
leothedragon 0:8f0bb79ddd48 767 uint8_t len = 0;
leothedragon 0:8f0bb79ddd48 768 /* Construct the variable-length payload representing the value */
leothedragon 0:8f0bb79ddd48 769 for (uint8_t i = 0; i < 4; i++) {
leothedragon 0:8f0bb79ddd48 770 if (len > 0 || (option_value & 0xff000000)) {
leothedragon 0:8f0bb79ddd48 771 payload[len++] = option_value >> 24;
leothedragon 0:8f0bb79ddd48 772 }
leothedragon 0:8f0bb79ddd48 773 option_value <<= 8;
leothedragon 0:8f0bb79ddd48 774 }
leothedragon 0:8f0bb79ddd48 775
leothedragon 0:8f0bb79ddd48 776 /* If output pointer isn't NULL, write it out */
leothedragon 0:8f0bb79ddd48 777 if (dst_packet_data_pptr) {
leothedragon 0:8f0bb79ddd48 778 int16_t ret = sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, len, payload, option_number, previous_option_number);
leothedragon 0:8f0bb79ddd48 779 /* Allow for failure returns when writing (why even permit failure returns?) */
leothedragon 0:8f0bb79ddd48 780 if (ret < 0) {
leothedragon 0:8f0bb79ddd48 781 return ret;
leothedragon 0:8f0bb79ddd48 782 }
leothedragon 0:8f0bb79ddd48 783 }
leothedragon 0:8f0bb79ddd48 784
leothedragon 0:8f0bb79ddd48 785 /* Return the total option size */
leothedragon 0:8f0bb79ddd48 786 return 1 + len;
leothedragon 0:8f0bb79ddd48 787 }
leothedragon 0:8f0bb79ddd48 788
leothedragon 0:8f0bb79ddd48 789 /**
leothedragon 0:8f0bb79ddd48 790 * \fn static int16_t sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_packet_data_pptr, uint8_t **src_pptr, uint16_t *src_len_ptr, sn_coap_option_numbers_e option)
leothedragon 0:8f0bb79ddd48 791 *
leothedragon 0:8f0bb79ddd48 792 * \brief Builds Option Uri-Query from given CoAP Header structure to Packet data
leothedragon 0:8f0bb79ddd48 793 *
leothedragon 0:8f0bb79ddd48 794 * \param **dst_packet_data_pptr is destination for built Packet data
leothedragon 0:8f0bb79ddd48 795 *
leothedragon 0:8f0bb79ddd48 796 * \param uint8_t **src_pptr
leothedragon 0:8f0bb79ddd48 797 *
leothedragon 0:8f0bb79ddd48 798 * \param uint16_t *src_len_ptr
leothedragon 0:8f0bb79ddd48 799 *
leothedragon 0:8f0bb79ddd48 800 * \paramsn_coap_option_numbers_e option option to be added
leothedragon 0:8f0bb79ddd48 801 *
leothedragon 0:8f0bb79ddd48 802 * \return Return value is 0 always
leothedragon 0:8f0bb79ddd48 803 */
leothedragon 0:8f0bb79ddd48 804 static int16_t sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_packet_data_pptr, uint8_t **src_pptr, uint16_t *src_len_ptr, sn_coap_option_numbers_e option, uint16_t *previous_option_number)
leothedragon 0:8f0bb79ddd48 805 {
leothedragon 0:8f0bb79ddd48 806 /* Check if there is option at all */
leothedragon 0:8f0bb79ddd48 807 if (*src_pptr != NULL) {
leothedragon 0:8f0bb79ddd48 808 uint8_t *query_ptr = *src_pptr;
leothedragon 0:8f0bb79ddd48 809 uint8_t query_part_count = 0;
leothedragon 0:8f0bb79ddd48 810 uint16_t query_len = *src_len_ptr;
leothedragon 0:8f0bb79ddd48 811 uint8_t i = 0;
leothedragon 0:8f0bb79ddd48 812 uint16_t query_part_offset = 0;
leothedragon 0:8f0bb79ddd48 813
leothedragon 0:8f0bb79ddd48 814 /* Get query part count */
leothedragon 0:8f0bb79ddd48 815 query_part_count = sn_coap_builder_options_get_option_part_count(query_len, query_ptr, option);
leothedragon 0:8f0bb79ddd48 816
leothedragon 0:8f0bb79ddd48 817 /* * * * Options by adding all parts to option * * * */
leothedragon 0:8f0bb79ddd48 818 for (i = 0; i < query_part_count; i++) {
leothedragon 0:8f0bb79ddd48 819 /* Get length of query part */
leothedragon 0:8f0bb79ddd48 820 uint16_t one_query_part_len = sn_coap_builder_options_get_option_part_length_from_whole_option_string(query_len, query_ptr, i, option);
leothedragon 0:8f0bb79ddd48 821
leothedragon 0:8f0bb79ddd48 822 /* Get position of query part */
leothedragon 0:8f0bb79ddd48 823 query_part_offset = sn_coap_builder_options_get_option_part_position(query_len, query_ptr, i, option);
leothedragon 0:8f0bb79ddd48 824
leothedragon 0:8f0bb79ddd48 825 /* Add Uri-query's one part to Options */
leothedragon 0:8f0bb79ddd48 826 sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, one_query_part_len, *src_pptr + query_part_offset, option, previous_option_number);
leothedragon 0:8f0bb79ddd48 827 }
leothedragon 0:8f0bb79ddd48 828 }
leothedragon 0:8f0bb79ddd48 829 /* Success */
leothedragon 0:8f0bb79ddd48 830 return 0;
leothedragon 0:8f0bb79ddd48 831 }
leothedragon 0:8f0bb79ddd48 832
leothedragon 0:8f0bb79ddd48 833
leothedragon 0:8f0bb79ddd48 834 /**
leothedragon 0:8f0bb79ddd48 835 * \fn static uint16_t sn_coap_builder_options_calc_option_size(uint16_t query_len, uint8_t *query_ptr, sn_coap_option_numbers_e option)
leothedragon 0:8f0bb79ddd48 836 *
leothedragon 0:8f0bb79ddd48 837 * \brief Calculates needed Packet data memory size for option
leothedragon 0:8f0bb79ddd48 838 *
leothedragon 0:8f0bb79ddd48 839 * \param path_len is length of calculated strting(s)
leothedragon 0:8f0bb79ddd48 840 *
leothedragon 0:8f0bb79ddd48 841 * \param *path_ptr is pointer to calculated options
leothedragon 0:8f0bb79ddd48 842 *
leothedragon 0:8f0bb79ddd48 843 * \return Return value is count of needed memory as bytes for Uri-query option
leothedragon 0:8f0bb79ddd48 844 */
leothedragon 0:8f0bb79ddd48 845 static uint16_t sn_coap_builder_options_calc_option_size(uint16_t query_len, uint8_t *query_ptr, sn_coap_option_numbers_e option)
leothedragon 0:8f0bb79ddd48 846 {
leothedragon 0:8f0bb79ddd48 847 uint8_t query_part_count = sn_coap_builder_options_get_option_part_count(query_len, query_ptr, option);
leothedragon 0:8f0bb79ddd48 848 uint8_t i = 0;
leothedragon 0:8f0bb79ddd48 849 uint16_t ret_value = 0;
leothedragon 0:8f0bb79ddd48 850
leothedragon 0:8f0bb79ddd48 851 /* * * * * * * * * * * * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 852 /* * * * Calculate Uri-query options length * * */
leothedragon 0:8f0bb79ddd48 853 /* * * * * * * * * * * * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 854 for (i = 0; i < query_part_count; i++) {
leothedragon 0:8f0bb79ddd48 855 /* * * Length of Option number and Option value length * * */
leothedragon 0:8f0bb79ddd48 856
leothedragon 0:8f0bb79ddd48 857 /* Get length of Query part */
leothedragon 0:8f0bb79ddd48 858 uint16_t one_query_part_len = sn_coap_builder_options_get_option_part_length_from_whole_option_string(query_len, query_ptr, i, option);
leothedragon 0:8f0bb79ddd48 859
leothedragon 0:8f0bb79ddd48 860 /* Check option length */
leothedragon 0:8f0bb79ddd48 861 switch (option) {
leothedragon 0:8f0bb79ddd48 862 case (COAP_OPTION_ETAG): /* Length 1-8 */
leothedragon 0:8f0bb79ddd48 863 if (one_query_part_len < 1 || one_query_part_len > 8) {
leothedragon 0:8f0bb79ddd48 864 return 0;
leothedragon 0:8f0bb79ddd48 865 }
leothedragon 0:8f0bb79ddd48 866 break;
leothedragon 0:8f0bb79ddd48 867 case (COAP_OPTION_LOCATION_PATH): /* Length 0-255 */
leothedragon 0:8f0bb79ddd48 868 case (COAP_OPTION_URI_PATH): /* Length 0-255 */
leothedragon 0:8f0bb79ddd48 869 case (COAP_OPTION_LOCATION_QUERY): /* Length 0-255 */
leothedragon 0:8f0bb79ddd48 870 if (one_query_part_len > 255) {
leothedragon 0:8f0bb79ddd48 871 return 0;
leothedragon 0:8f0bb79ddd48 872 }
leothedragon 0:8f0bb79ddd48 873 break;
leothedragon 0:8f0bb79ddd48 874 case (COAP_OPTION_URI_QUERY): /* Length 1-255 */
leothedragon 0:8f0bb79ddd48 875 if (one_query_part_len < 1 || one_query_part_len > 255) {
leothedragon 0:8f0bb79ddd48 876 return 0;
leothedragon 0:8f0bb79ddd48 877 }
leothedragon 0:8f0bb79ddd48 878 break;
leothedragon 0:8f0bb79ddd48 879 // case (COAP_OPTION_ACCEPT): /* Length 0-2 */
leothedragon 0:8f0bb79ddd48 880 // if (one_query_part_len > 2) {
leothedragon 0:8f0bb79ddd48 881 // return 0;
leothedragon 0:8f0bb79ddd48 882 // }
leothedragon 0:8f0bb79ddd48 883 // break;
leothedragon 0:8f0bb79ddd48 884 default:
leothedragon 0:8f0bb79ddd48 885 break; //impossible scenario currently
leothedragon 0:8f0bb79ddd48 886 }
leothedragon 0:8f0bb79ddd48 887
leothedragon 0:8f0bb79ddd48 888 /* Check if 4 bits are enough for writing Option value length */
leothedragon 0:8f0bb79ddd48 889 if (one_query_part_len <= 12) {
leothedragon 0:8f0bb79ddd48 890 /* 4 bits are enough for Option value length */
leothedragon 0:8f0bb79ddd48 891 ret_value++;
leothedragon 0:8f0bb79ddd48 892 } else if (one_query_part_len >= 13 && one_query_part_len < 269) {
leothedragon 0:8f0bb79ddd48 893 /* Extra byte for Option value length is needed */
leothedragon 0:8f0bb79ddd48 894 ret_value += 2;
leothedragon 0:8f0bb79ddd48 895 }
leothedragon 0:8f0bb79ddd48 896 //This can only happen if we are in default case above, currently is not happening
leothedragon 0:8f0bb79ddd48 897 else if (one_query_part_len >= 270 && one_query_part_len < 1034) {
leothedragon 0:8f0bb79ddd48 898 /* Extra bytes for Option value length is needed */
leothedragon 0:8f0bb79ddd48 899 ret_value += 3;
leothedragon 0:8f0bb79ddd48 900 }
leothedragon 0:8f0bb79ddd48 901
leothedragon 0:8f0bb79ddd48 902
leothedragon 0:8f0bb79ddd48 903 /* * * Length of Option value * * */
leothedragon 0:8f0bb79ddd48 904
leothedragon 0:8f0bb79ddd48 905 /* Increase options length */
leothedragon 0:8f0bb79ddd48 906 ret_value += one_query_part_len;
leothedragon 0:8f0bb79ddd48 907 }
leothedragon 0:8f0bb79ddd48 908
leothedragon 0:8f0bb79ddd48 909 /* Success */
leothedragon 0:8f0bb79ddd48 910 return ret_value;
leothedragon 0:8f0bb79ddd48 911 }
leothedragon 0:8f0bb79ddd48 912
leothedragon 0:8f0bb79ddd48 913
leothedragon 0:8f0bb79ddd48 914
leothedragon 0:8f0bb79ddd48 915 /**
leothedragon 0:8f0bb79ddd48 916 * \fn static uint8_t sn_coap_builder_options_get_option_part_count(uint16_t query_len, uint8_t *query_ptr, sn_coap_option_numbers_e option)
leothedragon 0:8f0bb79ddd48 917 *
leothedragon 0:8f0bb79ddd48 918 * \brief Gets query part count from whole option string
leothedragon 0:8f0bb79ddd48 919 *
leothedragon 0:8f0bb79ddd48 920 * \param query_len is length of whole Path
leothedragon 0:8f0bb79ddd48 921 *
leothedragon 0:8f0bb79ddd48 922 * \param *query_ptr is pointer to the start of whole Path
leothedragon 0:8f0bb79ddd48 923 *
leothedragon 0:8f0bb79ddd48 924 * \return Return value is count of query parts
leothedragon 0:8f0bb79ddd48 925 */
leothedragon 0:8f0bb79ddd48 926 static uint8_t sn_coap_builder_options_get_option_part_count(uint16_t query_len, uint8_t *query_ptr, sn_coap_option_numbers_e option)
leothedragon 0:8f0bb79ddd48 927 {
leothedragon 0:8f0bb79ddd48 928 uint8_t returned_query_count = 0;
leothedragon 0:8f0bb79ddd48 929 uint16_t query_len_index = 0;
leothedragon 0:8f0bb79ddd48 930 uint8_t char_to_search = '&';
leothedragon 0:8f0bb79ddd48 931
leothedragon 0:8f0bb79ddd48 932 if (option == COAP_OPTION_URI_PATH || option == COAP_OPTION_LOCATION_PATH) {
leothedragon 0:8f0bb79ddd48 933 char_to_search = '/';
leothedragon 0:8f0bb79ddd48 934 }
leothedragon 0:8f0bb79ddd48 935
leothedragon 0:8f0bb79ddd48 936 /* Loop whole query and search '\0' characters (not first and last char) */
leothedragon 0:8f0bb79ddd48 937 for (query_len_index = 1; query_len_index < query_len - 1; query_len_index++) {
leothedragon 0:8f0bb79ddd48 938 /* If new query part starts */
leothedragon 0:8f0bb79ddd48 939 if (*(query_ptr + query_len_index) == char_to_search) { /* If match */
leothedragon 0:8f0bb79ddd48 940 returned_query_count++;
leothedragon 0:8f0bb79ddd48 941 }
leothedragon 0:8f0bb79ddd48 942 }
leothedragon 0:8f0bb79ddd48 943
leothedragon 0:8f0bb79ddd48 944 returned_query_count++;
leothedragon 0:8f0bb79ddd48 945
leothedragon 0:8f0bb79ddd48 946 return returned_query_count;
leothedragon 0:8f0bb79ddd48 947 }
leothedragon 0:8f0bb79ddd48 948
leothedragon 0:8f0bb79ddd48 949 /**
leothedragon 0:8f0bb79ddd48 950 * \fn static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option_string(uint16_t query_len,
leothedragon 0:8f0bb79ddd48 951 uint8_t *query_ptr,
leothedragon 0:8f0bb79ddd48 952 uint8_t query_index, sn_coap_option_numbers_e option)
leothedragon 0:8f0bb79ddd48 953 *
leothedragon 0:8f0bb79ddd48 954 * \brief Gets one's query part length from whole query string
leothedragon 0:8f0bb79ddd48 955 *
leothedragon 0:8f0bb79ddd48 956 * \param query_len is length of whole string
leothedragon 0:8f0bb79ddd48 957 *
leothedragon 0:8f0bb79ddd48 958 * \param *query_ptr is pointer to the start of whole string
leothedragon 0:8f0bb79ddd48 959 *
leothedragon 0:8f0bb79ddd48 960 * \param query_index is query part index to be found
leothedragon 0:8f0bb79ddd48 961 *
leothedragon 0:8f0bb79ddd48 962 * \param sn_coap_option_numbers_e option is option number of the option
leothedragon 0:8f0bb79ddd48 963 *
leothedragon 0:8f0bb79ddd48 964 * \return Return value is length of query part
leothedragon 0:8f0bb79ddd48 965 */
leothedragon 0:8f0bb79ddd48 966 static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option_string(uint16_t query_len, uint8_t *query_ptr,
leothedragon 0:8f0bb79ddd48 967 uint8_t query_index, sn_coap_option_numbers_e option)
leothedragon 0:8f0bb79ddd48 968 {
leothedragon 0:8f0bb79ddd48 969 uint16_t returned_query_part_len = 0;
leothedragon 0:8f0bb79ddd48 970 uint8_t temp_query_index = 0;
leothedragon 0:8f0bb79ddd48 971 uint16_t query_len_index = 0;
leothedragon 0:8f0bb79ddd48 972 uint8_t char_to_search = '&';
leothedragon 0:8f0bb79ddd48 973
leothedragon 0:8f0bb79ddd48 974 if (option == COAP_OPTION_URI_PATH || option == COAP_OPTION_LOCATION_PATH) {
leothedragon 0:8f0bb79ddd48 975 char_to_search = '/';
leothedragon 0:8f0bb79ddd48 976 }
leothedragon 0:8f0bb79ddd48 977
leothedragon 0:8f0bb79ddd48 978 /* Loop whole query and search '\0' characters */
leothedragon 0:8f0bb79ddd48 979 for (query_len_index = 0; query_len_index < query_len; query_len_index++) {
leothedragon 0:8f0bb79ddd48 980 /* Store character to temp_char for helping debugging */
leothedragon 0:8f0bb79ddd48 981 uint8_t temp_char = *query_ptr;
leothedragon 0:8f0bb79ddd48 982
leothedragon 0:8f0bb79ddd48 983 /* If new query part starts */
leothedragon 0:8f0bb79ddd48 984 if (temp_char == char_to_search && returned_query_part_len > 0) { /* returned_query_part_len > 0 is for querys which start with "\0" */
leothedragon 0:8f0bb79ddd48 985 /* If query part index is wanted */
leothedragon 0:8f0bb79ddd48 986 if (temp_query_index == query_index) {
leothedragon 0:8f0bb79ddd48 987 /* Return length of query part */
leothedragon 0:8f0bb79ddd48 988 return returned_query_part_len;
leothedragon 0:8f0bb79ddd48 989 } else {
leothedragon 0:8f0bb79ddd48 990 /* Reset length of query part because wanted query part finding continues*/
leothedragon 0:8f0bb79ddd48 991 returned_query_part_len = 0;
leothedragon 0:8f0bb79ddd48 992 }
leothedragon 0:8f0bb79ddd48 993
leothedragon 0:8f0bb79ddd48 994 /* Next query part is looped */
leothedragon 0:8f0bb79ddd48 995 temp_query_index++;
leothedragon 0:8f0bb79ddd48 996 } else if (temp_char != char_to_search) { /* Else if query part continues */
leothedragon 0:8f0bb79ddd48 997 /* Increase query part length */
leothedragon 0:8f0bb79ddd48 998 returned_query_part_len++;
leothedragon 0:8f0bb79ddd48 999 }
leothedragon 0:8f0bb79ddd48 1000
leothedragon 0:8f0bb79ddd48 1001 query_ptr++;
leothedragon 0:8f0bb79ddd48 1002 }
leothedragon 0:8f0bb79ddd48 1003
leothedragon 0:8f0bb79ddd48 1004 /* Return length of query part in cases that query part does not finish to '\0' character (last query part can be like that) */
leothedragon 0:8f0bb79ddd48 1005 return returned_query_part_len;
leothedragon 0:8f0bb79ddd48 1006 }
leothedragon 0:8f0bb79ddd48 1007
leothedragon 0:8f0bb79ddd48 1008 /**
leothedragon 0:8f0bb79ddd48 1009 * \fn static uint16_t sn_coap_builder_options_get_option_part_position(uint16_t query_len,
leothedragon 0:8f0bb79ddd48 1010 uint8_t *query_ptr,
leothedragon 0:8f0bb79ddd48 1011 uint8_t query_index, sn_coap_option_numbers_e option)
leothedragon 0:8f0bb79ddd48 1012 *
leothedragon 0:8f0bb79ddd48 1013 * \brief Gets query part position in whole query
leothedragon 0:8f0bb79ddd48 1014 *
leothedragon 0:8f0bb79ddd48 1015 * \param query_len is length of whole query
leothedragon 0:8f0bb79ddd48 1016 *
leothedragon 0:8f0bb79ddd48 1017 * \param *query_ptr is pointer to the start of whole query
leothedragon 0:8f0bb79ddd48 1018 *
leothedragon 0:8f0bb79ddd48 1019 * \param query_index is query part index to be found
leothedragon 0:8f0bb79ddd48 1020 *
leothedragon 0:8f0bb79ddd48 1021 * \return Return value is position (= offset) of query part in whole query. In
leothedragon 0:8f0bb79ddd48 1022 * fail cases -1 is returned.
leothedragon 0:8f0bb79ddd48 1023 */
leothedragon 0:8f0bb79ddd48 1024 static int16_t sn_coap_builder_options_get_option_part_position(uint16_t query_len, uint8_t *query_ptr,
leothedragon 0:8f0bb79ddd48 1025 uint8_t query_index, sn_coap_option_numbers_e option)
leothedragon 0:8f0bb79ddd48 1026 {
leothedragon 0:8f0bb79ddd48 1027 uint16_t returned_query_part_offset = 0;
leothedragon 0:8f0bb79ddd48 1028 uint8_t temp_query_index = 0;
leothedragon 0:8f0bb79ddd48 1029 uint16_t query_len_index = 0;
leothedragon 0:8f0bb79ddd48 1030 uint8_t char_to_search = '&';
leothedragon 0:8f0bb79ddd48 1031
leothedragon 0:8f0bb79ddd48 1032 if (option == COAP_OPTION_URI_PATH || option == COAP_OPTION_LOCATION_PATH) {
leothedragon 0:8f0bb79ddd48 1033 char_to_search = '/';
leothedragon 0:8f0bb79ddd48 1034 }
leothedragon 0:8f0bb79ddd48 1035
leothedragon 0:8f0bb79ddd48 1036 if (query_index == 0) {
leothedragon 0:8f0bb79ddd48 1037 if (*query_ptr == 0 || *query_ptr == char_to_search) {
leothedragon 0:8f0bb79ddd48 1038 return 1;
leothedragon 0:8f0bb79ddd48 1039 } else {
leothedragon 0:8f0bb79ddd48 1040 return 0;
leothedragon 0:8f0bb79ddd48 1041 }
leothedragon 0:8f0bb79ddd48 1042 }
leothedragon 0:8f0bb79ddd48 1043
leothedragon 0:8f0bb79ddd48 1044 /* Loop whole query and search separator characters */
leothedragon 0:8f0bb79ddd48 1045 for (query_len_index = 0; query_len_index < query_len; query_len_index++) {
leothedragon 0:8f0bb79ddd48 1046 /* Store character to temp_char for helping debugging */
leothedragon 0:8f0bb79ddd48 1047 uint8_t temp_char = *query_ptr;
leothedragon 0:8f0bb79ddd48 1048
leothedragon 0:8f0bb79ddd48 1049 /* If new query part starts */
leothedragon 0:8f0bb79ddd48 1050 if (temp_char == char_to_search && returned_query_part_offset > 0) { /* returned_query_part_offset > 0 is for querys which start with searched char */
leothedragon 0:8f0bb79ddd48 1051 /* If query part index is wanted */
leothedragon 0:8f0bb79ddd48 1052 if (temp_query_index == (query_index - 1)) {
leothedragon 0:8f0bb79ddd48 1053 /* Return offset of query part */
leothedragon 0:8f0bb79ddd48 1054 return (returned_query_part_offset + 1); /* Plus one is for passing separator */
leothedragon 0:8f0bb79ddd48 1055 }
leothedragon 0:8f0bb79ddd48 1056
leothedragon 0:8f0bb79ddd48 1057 /* Next query part is looped */
leothedragon 0:8f0bb79ddd48 1058 temp_query_index++;
leothedragon 0:8f0bb79ddd48 1059 }
leothedragon 0:8f0bb79ddd48 1060
leothedragon 0:8f0bb79ddd48 1061 returned_query_part_offset++;
leothedragon 0:8f0bb79ddd48 1062
leothedragon 0:8f0bb79ddd48 1063 query_ptr++;
leothedragon 0:8f0bb79ddd48 1064 }
leothedragon 0:8f0bb79ddd48 1065
leothedragon 0:8f0bb79ddd48 1066 return -1; //Dead code?
leothedragon 0:8f0bb79ddd48 1067 }
leothedragon 0:8f0bb79ddd48 1068
leothedragon 0:8f0bb79ddd48 1069
leothedragon 0:8f0bb79ddd48 1070 /**
leothedragon 0:8f0bb79ddd48 1071 * \fn static void sn_coap_builder_payload_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr)
leothedragon 0:8f0bb79ddd48 1072 *
leothedragon 0:8f0bb79ddd48 1073 * \brief Builds Options part of Packet data
leothedragon 0:8f0bb79ddd48 1074 *
leothedragon 0:8f0bb79ddd48 1075 * \param **dst_packet_data_pptr is destination for built Packet data
leothedragon 0:8f0bb79ddd48 1076 *
leothedragon 0:8f0bb79ddd48 1077 * \param *src_coap_msg_ptr is source for building Packet data
leothedragon 0:8f0bb79ddd48 1078 */
leothedragon 0:8f0bb79ddd48 1079 static void sn_coap_builder_payload_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr)
leothedragon 0:8f0bb79ddd48 1080 {
leothedragon 0:8f0bb79ddd48 1081 /* Check if Payload is used at all */
leothedragon 0:8f0bb79ddd48 1082 if (src_coap_msg_ptr->payload_len && src_coap_msg_ptr->payload_ptr != NULL) {
leothedragon 0:8f0bb79ddd48 1083 /* Write Payload marker */
leothedragon 0:8f0bb79ddd48 1084
leothedragon 0:8f0bb79ddd48 1085 **dst_packet_data_pptr = 0xff;
leothedragon 0:8f0bb79ddd48 1086 (*dst_packet_data_pptr)++;
leothedragon 0:8f0bb79ddd48 1087
leothedragon 0:8f0bb79ddd48 1088 /* Write Payload */
leothedragon 0:8f0bb79ddd48 1089 memcpy(*dst_packet_data_pptr, src_coap_msg_ptr->payload_ptr, src_coap_msg_ptr->payload_len);
leothedragon 0:8f0bb79ddd48 1090
leothedragon 0:8f0bb79ddd48 1091 /* Increase destination Packet data pointer */
leothedragon 0:8f0bb79ddd48 1092 (*dst_packet_data_pptr) += src_coap_msg_ptr->payload_len;
leothedragon 0:8f0bb79ddd48 1093 }
leothedragon 0:8f0bb79ddd48 1094 }