Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

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