sandbox / mbed-client-c

Fork of mbed-client-c by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:31:54 2016 -0600
Revision:
1:43f5c94c6771
Child:
4:5d91b0f5038c
Initial move of mbed-client-c to mercurial

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