sandbox / mbed-client-c

Fork of mbed-client-c by Christopher Haster

Committer:
Yogesh Pande
Date:
Sat Apr 02 00:39:03 2016 +0300
Revision:
4:5d91b0f5038c
Parent:
1:43f5c94c6771
Latest mbed-client-c sources from Github.

Who changed what in which revision?

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