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