Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

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