Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Wed Aug 28 19:24:56 2019 +0000
Revision:
0:977e87915078
init

Who changed what in which revision?

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