Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sn_coap_builder.c Source File

sn_coap_builder.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011-2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 /**
00018  * \file sn_coap_builder.c
00019  *
00020  * \brief CoAP Message builder
00021  *
00022  * Functionality: Builds CoAP message
00023  *
00024  */
00025 
00026 /* * * * * * * * * * * * * * */
00027 /* * * * INCLUDE FILES * * * */
00028 /* * * * * * * * * * * * * * */
00029 
00030 #include <string.h> /* For memset() and memcpy() */
00031 
00032 #include "ns_types.h"
00033 #include "mbed-coap/sn_coap_header.h"
00034 #include "sn_coap_header_internal.h"
00035 #include "sn_coap_protocol_internal.h"
00036 #include "mbed-trace/mbed_trace.h"
00037 
00038 #define TRACE_GROUP "coap"
00039 /* * * * LOCAL FUNCTION PROTOTYPES * * * */
00040 static int8_t   sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr);
00041 static int8_t   sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr);
00042 static uint16_t sn_coap_builder_options_calc_option_size(uint16_t query_len, const uint8_t *query_ptr, sn_coap_option_numbers_e option);
00043 static int16_t  sn_coap_builder_options_build_add_one_option(uint8_t **dst_packet_data_pptr, uint16_t option_len, const uint8_t *option_ptr, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number);
00044 static void     sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_packet_data_pptr, const uint8_t *src_pptr, uint16_t src_len_ptr, sn_coap_option_numbers_e option, uint16_t *previous_option_number);
00045 static uint8_t  sn_coap_builder_options_build_add_uint_option(uint8_t **dst_packet_data_pptr, uint32_t value, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number);
00046 static uint8_t  sn_coap_builder_options_get_option_part_count(uint16_t query_len, const uint8_t *query_ptr, sn_coap_option_numbers_e option);
00047 static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option_string(uint16_t query_len, const uint8_t *query_ptr, uint8_t query_index, sn_coap_option_numbers_e option);
00048 static int16_t  sn_coap_builder_options_get_option_part_position(uint16_t query_len, const uint8_t *query_ptr, uint8_t query_index, sn_coap_option_numbers_e option);
00049 static void     sn_coap_builder_payload_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr);
00050 static uint8_t  sn_coap_builder_options_calculate_jump_need(const sn_coap_hdr_s *src_coap_msg_ptr);
00051 
00052 sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, const sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code)
00053 {
00054     sn_coap_hdr_s *coap_res_ptr;
00055 
00056     if (!coap_packet_ptr || !handle) {
00057         return NULL;
00058     }
00059 
00060     coap_res_ptr = sn_coap_parser_alloc_message(handle);
00061     if (!coap_res_ptr) {
00062         tr_error("sn_coap_build_response - failed to allocate message!");
00063         return NULL;
00064     }
00065 
00066     if (msg_code == COAP_MSG_CODE_REQUEST_GET) {
00067         // Blockwise message response is new GET
00068         coap_res_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
00069         coap_res_ptr->msg_code = (sn_coap_msg_code_e)msg_code;
00070         /* msg_id needs to be set by the caller in this case */
00071     }
00072     else if (coap_packet_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
00073         coap_res_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
00074         coap_res_ptr->msg_code = (sn_coap_msg_code_e)msg_code;
00075         coap_res_ptr->msg_id = coap_packet_ptr->msg_id;
00076     }
00077     else if (coap_packet_ptr->msg_type == COAP_MSG_TYPE_NON_CONFIRMABLE) {
00078         coap_res_ptr->msg_type = COAP_MSG_TYPE_NON_CONFIRMABLE;
00079         coap_res_ptr->msg_code = (sn_coap_msg_code_e)msg_code;
00080         /* msg_id needs to be set by the caller in this case */
00081     }
00082     else {
00083         handle->sn_coap_protocol_free( coap_res_ptr );
00084         return NULL;
00085     }
00086 
00087     if (coap_packet_ptr->token_ptr) {
00088         coap_res_ptr->token_len = coap_packet_ptr->token_len;
00089         coap_res_ptr->token_ptr = sn_coap_protocol_malloc_copy(handle, coap_packet_ptr->token_ptr, coap_res_ptr->token_len);
00090         if (!coap_res_ptr->token_ptr) {
00091             tr_error("sn_coap_build_response - failed to allocate token!");
00092             handle->sn_coap_protocol_free(coap_res_ptr);
00093             return NULL;
00094         }
00095     }
00096     return coap_res_ptr;
00097 }
00098 
00099 int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, const sn_coap_hdr_s *src_coap_msg_ptr)
00100 {
00101     return sn_coap_builder_2(dst_packet_data_ptr, src_coap_msg_ptr, SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE);
00102 }
00103 
00104 int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, const sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size)
00105 {
00106     uint8_t *base_packet_data_ptr;
00107 
00108     /* * * * Check given pointers  * * * */
00109     if (dst_packet_data_ptr == NULL || src_coap_msg_ptr == NULL) {
00110         return -2;
00111     }
00112 
00113     /* Initialize given Packet data memory area with zero values */
00114     uint16_t dst_byte_count_to_be_built = sn_coap_builder_calc_needed_packet_data_size_2(src_coap_msg_ptr, blockwise_payload_size);
00115     if (!dst_byte_count_to_be_built) {
00116         tr_error("sn_coap_builder_2 - failed to allocate message!");
00117         return -1;
00118     }
00119 
00120     // XXX: this should not be needed anymore but I have no courage to remove it yet.
00121     memset(dst_packet_data_ptr, 0, dst_byte_count_to_be_built);
00122 
00123     /* * * * Store base (= original) destination Packet data pointer for later usage * * * */
00124     base_packet_data_ptr = dst_packet_data_ptr;
00125 
00126     /* * * * * * * * * * * * * * * * * * */
00127     /* * * * Header part building  * * * */
00128     /* * * * * * * * * * * * * * * * * * */
00129     if (sn_coap_builder_header_build(&dst_packet_data_ptr, src_coap_msg_ptr) != 0) {
00130         /* Header building failed */
00131         tr_error("sn_coap_builder_2 - header building failed!");
00132         return -1;
00133     }
00134 
00135     /* If else than Reset message because Reset message must be empty */
00136     if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_RESET) {
00137         /* * * * * * * * * * * * * * * * * * */
00138         /* * * * Options part building * * * */
00139         /* * * * * * * * * * * * * * * * * * */
00140         sn_coap_builder_options_build(&dst_packet_data_ptr, src_coap_msg_ptr);
00141 
00142         /* * * * * * * * * * * * * * * * * * */
00143         /* * * * Payload part building * * * */
00144         /* * * * * * * * * * * * * * * * * * */
00145         sn_coap_builder_payload_build(&dst_packet_data_ptr, src_coap_msg_ptr);
00146     }
00147     /* * * * Return built Packet data length * * * */
00148     return (dst_packet_data_ptr - base_packet_data_ptr);
00149 }
00150 uint16_t sn_coap_builder_calc_needed_packet_data_size(const sn_coap_hdr_s *src_coap_msg_ptr)
00151 {
00152     return sn_coap_builder_calc_needed_packet_data_size_2(src_coap_msg_ptr, SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE);
00153 }
00154 
00155 uint16_t sn_coap_builder_calc_needed_packet_data_size_2(const sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size)
00156 {
00157     (void)blockwise_payload_size;
00158     uint16_t returned_byte_count = 0;
00159 
00160     if (!src_coap_msg_ptr) {
00161         return 0;
00162     }
00163     /* * * * *  HEADER * * * * */
00164 
00165     /* Header size is fixed */
00166     returned_byte_count = COAP_HEADER_LENGTH;
00167 
00168     /* * * * * OPTIONS * * * * */
00169 
00170     /* If else than Reset message because Reset message must be empty */
00171     if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_RESET) {
00172         uint16_t repeatable_option_size = 0;
00173         /* TOKEN - Length is 1-8 bytes */
00174         if (src_coap_msg_ptr->token_ptr != NULL) {
00175             if (src_coap_msg_ptr->token_len > 8 || src_coap_msg_ptr->token_len < 1) { /* Check that option is not longer than defined */
00176                 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - token too large!");
00177                 return 0;
00178             }
00179 
00180             returned_byte_count += src_coap_msg_ptr->token_len;
00181         }
00182         /* URI PATH - Repeatable option. Length of one option is 0-255 */
00183         if (src_coap_msg_ptr->uri_path_ptr != NULL) {
00184             repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->uri_path_len,
00185                                      src_coap_msg_ptr->uri_path_ptr, COAP_OPTION_URI_PATH);
00186             if (repeatable_option_size) {
00187                 returned_byte_count += repeatable_option_size;
00188             } else {
00189                 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - uri path size failed!");
00190                 return 0;
00191             }
00192         }
00193 
00194         uint16_t tempInt = 0;
00195         /* CONTENT FORMAT - An integer option, up to 2 bytes */
00196         if (src_coap_msg_ptr->content_format != COAP_CT_NONE) {
00197             if ((uint32_t) src_coap_msg_ptr->content_format > 0xffff) {
00198                 tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - content format too large!");
00199                 return 0;
00200             }
00201 
00202             returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_coap_msg_ptr->content_format, COAP_OPTION_CONTENT_FORMAT, &tempInt);
00203         }
00204         /* If options list pointer exists */
00205         if (src_coap_msg_ptr->options_list_ptr != NULL) {
00206 
00207             const sn_coap_options_list_s *src_options_list_ptr = src_coap_msg_ptr->options_list_ptr;
00208 
00209             /* ACCEPT - An integer option, up to 2 bytes */
00210             if (src_options_list_ptr->accept != COAP_CT_NONE) {
00211                 if ((uint32_t) src_options_list_ptr->accept > 0xffff) {
00212                     tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - accept too large!");
00213                     return 0;
00214                 }
00215 
00216                 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->accept, COAP_OPTION_ACCEPT, &tempInt);
00217             }
00218             /* MAX AGE - An integer option, omitted for default. Up to 4 bytes */
00219             if (src_options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) {
00220                 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->max_age, COAP_OPTION_MAX_AGE, &tempInt);
00221             }
00222             /* PROXY URI - Length of this option is  1-1034 bytes */
00223             if (src_options_list_ptr->proxy_uri_ptr != NULL) {
00224                 if (src_options_list_ptr->proxy_uri_len >= 1 && src_options_list_ptr->proxy_uri_len <= 12) {            /* Add option header byte(s) - depending of option length */
00225                     returned_byte_count++;
00226                 }
00227 
00228                 else if (src_options_list_ptr->proxy_uri_len >= 13 && src_options_list_ptr->proxy_uri_len <= 269) {
00229                     returned_byte_count += 2;
00230                 }
00231 
00232                 else if (src_options_list_ptr->proxy_uri_len >= 270 && src_options_list_ptr->proxy_uri_len <= 1034) {
00233                     returned_byte_count += 3;
00234                 }
00235 
00236                 else {
00237                     tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - proxy uri too large!");
00238                     return 0;
00239                 }
00240 
00241                 /* Add needed memory for Option value */
00242                 returned_byte_count += src_options_list_ptr->proxy_uri_len;
00243             }
00244             /* ETAG - Repeatable option. Length of this option is 1-8 bytes*/
00245             if (src_options_list_ptr->etag_ptr != NULL) {
00246                 repeatable_option_size = sn_coap_builder_options_calc_option_size(src_options_list_ptr->etag_len,
00247                                          src_options_list_ptr->etag_ptr, COAP_OPTION_ETAG);
00248                 if (repeatable_option_size) {
00249                     returned_byte_count += repeatable_option_size;
00250                 } else {
00251                     tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - etag too large!");
00252                     return 0;
00253                 }
00254             }
00255             /* URI HOST - Length of this option is 1-255 bytes */
00256             if (src_options_list_ptr->uri_host_ptr != NULL) {
00257                 if (src_options_list_ptr->uri_host_len > 0 && src_options_list_ptr->uri_host_len <= 12) {
00258                     returned_byte_count++;
00259                 }
00260 
00261                 else if (src_options_list_ptr->uri_host_len >= 13 && src_options_list_ptr->uri_host_len <= 255) {
00262                     returned_byte_count += 2;
00263                 }
00264 
00265                 else {
00266                     tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - uri host too large!");
00267                     return 0;
00268                 }
00269 
00270                 returned_byte_count += src_options_list_ptr->uri_host_len;
00271             }
00272             /* LOCATION PATH - Repeatable option. Length of this option is 0-255 bytes*/
00273             if (src_options_list_ptr->location_path_ptr != NULL) {
00274                 repeatable_option_size = sn_coap_builder_options_calc_option_size(src_options_list_ptr->location_path_len,
00275                                          src_options_list_ptr->location_path_ptr, COAP_OPTION_LOCATION_PATH);
00276                 if (repeatable_option_size) {
00277                     returned_byte_count += repeatable_option_size;
00278                 } else {
00279                     tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - location path too large!");
00280                     return 0;
00281                 }
00282             }
00283             /* URI PORT - An integer option, up to 2 bytes */
00284             if (src_options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) {
00285                 if ((uint32_t) src_options_list_ptr->uri_port > 0xffff) {
00286                     tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - uri port too large!");
00287                     return 0;
00288                 }
00289                 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->uri_port, COAP_OPTION_URI_PORT, &tempInt);
00290             }
00291             /* lOCATION QUERY - Repeatable option. Length of this option is 0-255 bytes */
00292             if (src_options_list_ptr->location_query_ptr != NULL) {
00293                 repeatable_option_size = sn_coap_builder_options_calc_option_size(src_options_list_ptr->location_query_len,
00294                                          src_options_list_ptr->location_query_ptr, COAP_OPTION_LOCATION_QUERY);
00295                 if (repeatable_option_size) {
00296                     returned_byte_count += repeatable_option_size;
00297                 } else {
00298                     tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - location query too large!");
00299                     return 0;
00300                 }
00301             }
00302             /* OBSERVE - An integer option, up to 3 bytes */
00303             if (src_options_list_ptr->observe != COAP_OBSERVE_NONE) {
00304                 if ((uint32_t) src_options_list_ptr->observe > 0xffffff) {
00305                     return 0;
00306                 }
00307                 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->observe, COAP_OPTION_OBSERVE, &tempInt);
00308             }
00309             /* URI QUERY - Repeatable option. Length of this option is 1-255 */
00310             if (src_options_list_ptr->uri_query_ptr != NULL) {
00311                 repeatable_option_size = sn_coap_builder_options_calc_option_size(src_options_list_ptr->uri_query_len,
00312                                          src_options_list_ptr->uri_query_ptr, COAP_OPTION_URI_QUERY);
00313                 if (repeatable_option_size) {
00314                     returned_byte_count += repeatable_option_size;
00315                 } else {
00316                     tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - observe too large!");
00317                     return 0;
00318                 }
00319             }
00320 
00321             /* BLOCK 1 - An integer option, up to 3 bytes */
00322             if (src_options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) {
00323                 if ((uint32_t) src_options_list_ptr->block1 > 0xffffff) {
00324                     tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - block1 too large!");
00325                     return 0;
00326                 }
00327                 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->block1, COAP_OPTION_BLOCK1, &tempInt);
00328             }
00329             /* SIZE1 - Length of this option is 0-4 bytes */
00330             if (src_options_list_ptr->use_size1) {
00331                 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->size1, COAP_OPTION_SIZE1, &tempInt);
00332             }
00333             /* BLOCK 2 - An integer option, up to 3 bytes */
00334             if (src_options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) {
00335                 if ((uint32_t) src_options_list_ptr->block2 > 0xffffff) {
00336                     tr_error("sn_coap_builder_calc_needed_packet_data_size_2 - block2 too large!");
00337                     return 0;
00338                 }
00339                 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->block2, COAP_OPTION_BLOCK2, &tempInt);
00340             }
00341             /* SIZE2 - Length of this option is 0-4 bytes */
00342             if (src_coap_msg_ptr->options_list_ptr->use_size2) {
00343                 returned_byte_count += sn_coap_builder_options_build_add_uint_option(NULL, src_options_list_ptr->size2, COAP_OPTION_SIZE2, &tempInt);
00344             }
00345         }
00346 #if SN_COAP_BLOCKWISE_ENABLED || SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
00347         if ((src_coap_msg_ptr->payload_len > SN_COAP_MAX_NONBLOCKWISE_PAYLOAD_SIZE) &&
00348             (src_coap_msg_ptr->payload_len > blockwise_payload_size) &&
00349             (blockwise_payload_size > 0)) {
00350             returned_byte_count += blockwise_payload_size;
00351         } else {
00352             returned_byte_count += src_coap_msg_ptr->payload_len;
00353         }
00354 #else
00355         returned_byte_count += src_coap_msg_ptr->payload_len;
00356 #endif
00357         if (src_coap_msg_ptr->payload_len) {
00358             returned_byte_count ++;    /* For payload marker */
00359         }
00360         returned_byte_count += sn_coap_builder_options_calculate_jump_need(src_coap_msg_ptr);
00361     }
00362     return returned_byte_count;
00363 }
00364 /**
00365  * \fn static uint8_t sn_coap_builder_options_calculate_jump_need(sn_coap_hdr_s *src_coap_msg_ptr)
00366  *
00367  * \brief Checks if there is need for option jump
00368  *
00369  * \param  *src_coap_msg_ptr is source of checked CoAP message
00370  *
00371  * \param  block option marks if block option is to be added to message later. 0 = no block option, 1 = block1 and 2 = block2
00372  *
00373  * \return Returns bytes needed for jumping
00374  */
00375 
00376 static uint8_t sn_coap_builder_options_calculate_jump_need(const sn_coap_hdr_s *src_coap_msg_ptr)
00377 {
00378     uint8_t previous_option_number = 0;
00379     uint8_t needed_space           = 0;
00380 
00381     const sn_coap_options_list_s* options_list_ptr = src_coap_msg_ptr->options_list_ptr;
00382 
00383     if (options_list_ptr != NULL) {
00384 
00385 
00386         /* If option numbers greater than 12 is not used, then jumping is not needed */
00387         //TODO: Check if this is really needed! Does it enhance perf? If not -> remove
00388         if (!options_list_ptr->uri_query_ptr       &&
00389                 options_list_ptr->accept == COAP_CT_NONE &&
00390                 !options_list_ptr->location_query_ptr &&
00391                 options_list_ptr->block2 == COAP_OPTION_BLOCK_NONE &&
00392                 options_list_ptr->block1 == COAP_OPTION_BLOCK_NONE &&
00393                 !options_list_ptr->proxy_uri_ptr      &&
00394                 options_list_ptr->max_age == COAP_OPTION_MAX_AGE_DEFAULT &&
00395                 !options_list_ptr->use_size1          &&
00396                 !options_list_ptr->use_size2) {
00397             return 0;
00398         }
00399 
00400         if (options_list_ptr->uri_host_ptr != NULL) {
00401             previous_option_number = (COAP_OPTION_URI_HOST);
00402         }
00403 
00404         if (options_list_ptr->etag_ptr != NULL) {
00405             previous_option_number = (COAP_OPTION_ETAG);
00406         }
00407 
00408         if (options_list_ptr->observe != COAP_OBSERVE_NONE) {
00409             previous_option_number = (COAP_OPTION_OBSERVE);
00410         }
00411 
00412         if (options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) {
00413             previous_option_number = (COAP_OPTION_URI_PORT);
00414         }
00415 
00416         if (options_list_ptr->location_path_ptr != NULL) {
00417             previous_option_number = (COAP_OPTION_LOCATION_PATH);
00418         }
00419 
00420         if (src_coap_msg_ptr->uri_path_ptr != NULL) {
00421             previous_option_number = (COAP_OPTION_URI_PATH);
00422         }
00423         if (src_coap_msg_ptr->content_format != COAP_CT_NONE) {
00424             previous_option_number = (COAP_OPTION_CONTENT_FORMAT);
00425         }
00426 
00427         if (options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) {
00428             if ((COAP_OPTION_MAX_AGE - previous_option_number) > 12) {
00429                 needed_space += 1;
00430             }
00431             previous_option_number = (COAP_OPTION_MAX_AGE);
00432         }
00433 
00434         if (options_list_ptr->uri_query_ptr != NULL) {
00435             if ((COAP_OPTION_URI_QUERY - previous_option_number) > 12) {
00436                 needed_space += 1;
00437             }
00438             previous_option_number = (COAP_OPTION_URI_QUERY);
00439         }
00440         if (options_list_ptr->accept != COAP_CT_NONE) {
00441             if ((COAP_OPTION_ACCEPT - previous_option_number) > 12) {
00442                 needed_space += 1;
00443             }
00444             previous_option_number = (COAP_OPTION_ACCEPT);
00445         }
00446         if (options_list_ptr->location_query_ptr != NULL) {
00447             if ((COAP_OPTION_LOCATION_QUERY - previous_option_number) > 12) {
00448                 needed_space += 1;
00449             }
00450             previous_option_number = (COAP_OPTION_LOCATION_QUERY);
00451         }
00452         if (options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) {
00453             if ((COAP_OPTION_BLOCK2 - previous_option_number) > 12 ){
00454                 needed_space += 1;
00455             }
00456             previous_option_number = (COAP_OPTION_BLOCK2);
00457         }
00458         if (options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) {
00459             if ((COAP_OPTION_BLOCK1 - previous_option_number) > 12 ){
00460                 needed_space += 1;
00461             }
00462             previous_option_number = (COAP_OPTION_BLOCK1);
00463         }
00464         if (options_list_ptr->use_size2) {
00465             if ((COAP_OPTION_SIZE2 - previous_option_number) > 12) {
00466                 needed_space += 1;
00467             }
00468             previous_option_number = (COAP_OPTION_SIZE2);
00469         }
00470         if (options_list_ptr->proxy_uri_ptr != NULL) {
00471             if ((COAP_OPTION_PROXY_URI - previous_option_number) > 12) {
00472                 needed_space += 1;
00473             }
00474             if ((COAP_OPTION_PROXY_URI - previous_option_number) > 269) { //Can not happen
00475                 needed_space += 1;
00476             }
00477             previous_option_number = (COAP_OPTION_PROXY_URI);
00478         }
00479         if (options_list_ptr->use_size1) {
00480             if ((COAP_OPTION_SIZE1 - previous_option_number) > 12) {
00481                 needed_space += 1;
00482             }
00483             previous_option_number = (COAP_OPTION_SIZE1);
00484         }
00485     }
00486 
00487     return needed_space;
00488 }
00489 
00490 /**
00491  * \fn static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr)
00492  *
00493  * \brief Builds Header part of Packet data
00494  *
00495  * \param **dst_packet_data_pptr is destination for built Packet data
00496  *
00497  * \param *src_coap_msg_ptr is source for building Packet data
00498  *
00499  * \return Return value is 0 in ok case and -1 in failure case
00500  **************************************************************************** */
00501 static int8_t sn_coap_builder_header_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr)
00502 {
00503     /* * * * Check validity of Header values * * * */
00504     if (sn_coap_header_validity_check(src_coap_msg_ptr, COAP_VERSION) != 0) {
00505         tr_error("sn_coap_builder_header_build - header build failed!");
00506         return -1;
00507     }
00508 
00509     uint8_t* dest_packet = *dst_packet_data_pptr;
00510 
00511     /* Set CoAP Version, Message type and Token length */
00512     dest_packet[0] = COAP_VERSION | src_coap_msg_ptr->msg_type | src_coap_msg_ptr->token_len;
00513 
00514     /* * * Add Message code * * */
00515     dest_packet[1] = src_coap_msg_ptr->msg_code;
00516 
00517     /* * * Add Message ID * * */
00518     dest_packet[2] = (uint8_t)(src_coap_msg_ptr->msg_id >> COAP_HEADER_MSG_ID_MSB_SHIFT); /* MSB part */
00519     dest_packet[3] = (uint8_t)src_coap_msg_ptr->msg_id;                                   /* LSB part */
00520 
00521     *dst_packet_data_pptr = dest_packet + 4;
00522 
00523     /* Success */
00524     return 0;
00525 }
00526 
00527 /**
00528  * \fn static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr)
00529  *
00530  * \brief Builds Options part of Packet data
00531  *
00532  * \param **dst_packet_data_pptr is destination for built Packet data
00533  *
00534  * \param *src_coap_msg_ptr is source for building Packet data
00535  *
00536  * \return Return value is 0 in every case
00537  */
00538 static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr)
00539 {
00540     /* * * * Check if Options are used at all  * * * */
00541     if (src_coap_msg_ptr->uri_path_ptr == NULL && src_coap_msg_ptr->token_ptr == NULL &&
00542             src_coap_msg_ptr->content_format == COAP_CT_NONE && src_coap_msg_ptr->options_list_ptr == NULL) {
00543         /* If the empty message is confirmable it is CoAP ping. */
00544         if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_CONFIRMABLE) {
00545             tr_error("sn_coap_builder_options_build - options not used!");
00546         }
00547         return 0;
00548     }
00549 
00550     /* * * * First add Token option  * * * */
00551     if (src_coap_msg_ptr->token_len && src_coap_msg_ptr->token_ptr) {
00552         memcpy(*dst_packet_data_pptr, src_coap_msg_ptr->token_ptr, src_coap_msg_ptr->token_len);
00553     }
00554     (*dst_packet_data_pptr) += src_coap_msg_ptr->token_len;
00555 
00556     /* Then build rest of the options */
00557 
00558     /* * * * Initialize previous Option number for new built message * * * */
00559     uint16_t previous_option_number = 0;
00560 
00561     //missing: COAP_OPTION_IF_MATCH, COAP_OPTION_IF_NONE_MATCH, COAP_OPTION_SIZE
00562 
00563     const sn_coap_options_list_s *src_options_list_ptr = src_coap_msg_ptr->options_list_ptr;
00564 
00565     /* Check if less used options are used at all */
00566     if (src_options_list_ptr != NULL) {
00567         /* * * * Build Uri-Host option * * * */
00568         sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, src_options_list_ptr->uri_host_len,
00569                      src_options_list_ptr->uri_host_ptr, COAP_OPTION_URI_HOST, &previous_option_number);
00570 
00571         /* * * * Build ETag option  * * * */
00572         sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, src_options_list_ptr->etag_ptr,
00573                      src_options_list_ptr->etag_len, COAP_OPTION_ETAG, &previous_option_number);
00574 
00575         /* * * * Build Observe option  * * * * */
00576         if (src_options_list_ptr->observe != COAP_OBSERVE_NONE) {
00577             sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->observe,
00578                          COAP_OPTION_OBSERVE, &previous_option_number);
00579         }
00580 
00581         /* * * * Build Uri-Port option * * * */
00582         if (src_options_list_ptr->uri_port != COAP_OPTION_URI_PORT_NONE) {
00583             sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->uri_port,
00584                          COAP_OPTION_URI_PORT, &previous_option_number);
00585         }
00586 
00587         /* * * * Build Location-Path option  * * * */
00588         sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, src_options_list_ptr->location_path_ptr,
00589                      src_options_list_ptr->location_path_len, COAP_OPTION_LOCATION_PATH, &previous_option_number);
00590     }
00591     /* * * * Build Uri-Path option * * * */
00592     sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, src_coap_msg_ptr->uri_path_ptr,
00593              src_coap_msg_ptr->uri_path_len, COAP_OPTION_URI_PATH, &previous_option_number);
00594 
00595     /* * * * Build Content-Type option * * * */
00596     if (src_coap_msg_ptr->content_format != COAP_CT_NONE) {
00597         sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_coap_msg_ptr->content_format,
00598                      COAP_OPTION_CONTENT_FORMAT, &previous_option_number);
00599     }
00600 
00601     if (src_options_list_ptr != NULL) {
00602         /* * * * Build Max-Age option  * * * */
00603         if (src_options_list_ptr->max_age != COAP_OPTION_MAX_AGE_DEFAULT) {
00604             sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->max_age,
00605                          COAP_OPTION_MAX_AGE, &previous_option_number);
00606         }
00607 
00608         /* * * * Build Uri-Query option  * * * * */
00609         sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, src_options_list_ptr->uri_query_ptr,
00610                      src_options_list_ptr->uri_query_len, COAP_OPTION_URI_QUERY, &previous_option_number);
00611 
00612         /* * * * Build Accept option  * * * * */
00613         if (src_coap_msg_ptr->options_list_ptr->accept != COAP_CT_NONE) {
00614             sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->accept,
00615                          COAP_OPTION_ACCEPT, &previous_option_number);
00616         }
00617 
00618         /* * * * Build Location-Query option * * * */
00619         sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, src_options_list_ptr->location_query_ptr,
00620                      src_options_list_ptr->location_query_len, COAP_OPTION_LOCATION_QUERY, &previous_option_number);
00621 
00622         /* * * * Build Block2 option * * * * */
00623         if (src_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE) {
00624             sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->block2,
00625                          COAP_OPTION_BLOCK2, &previous_option_number);
00626         }
00627 
00628         /* * * * Build Block1 option * * * * */
00629         if (src_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE) {
00630             sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->block1,
00631                          COAP_OPTION_BLOCK1, &previous_option_number);
00632         }
00633 
00634         /* * * * Build Size2 option * * * */
00635         if (src_coap_msg_ptr->options_list_ptr->use_size2) {
00636             sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->size2,
00637                          COAP_OPTION_SIZE2, &previous_option_number);
00638         }
00639 
00640         /* * * * Build Proxy-Uri option * * * */
00641         sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, src_options_list_ptr->proxy_uri_len,
00642                      src_options_list_ptr->proxy_uri_ptr, COAP_OPTION_PROXY_URI, &previous_option_number);
00643 
00644 
00645         /* * * * Build Size1 option * * * */
00646         if (src_options_list_ptr->use_size1) {
00647             sn_coap_builder_options_build_add_uint_option(dst_packet_data_pptr, src_options_list_ptr->size1,
00648                          COAP_OPTION_SIZE1, &previous_option_number);
00649         }
00650     }
00651 
00652     /* Success */
00653     return 0;
00654 }
00655 
00656 /**
00657  * \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)
00658  *
00659  * \brief Adds Options part of Packet data
00660  *
00661  * \param **dst_packet_data_pptr is destination for built Packet data
00662  *
00663  * \param option_value_len is Option value length to be added
00664  *
00665  * \param *option_value_ptr is pointer to Option value data to be added
00666  *
00667  * \param option_number is Option number to be added
00668  *
00669  * \return Return value is 0 if option was not added, 1 if added
00670  */
00671 static int16_t sn_coap_builder_options_build_add_one_option(uint8_t **dst_packet_data_pptr, uint16_t option_len,
00672         const uint8_t *option_ptr, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number)
00673 {
00674     /* Check if there is option at all */
00675     if (option_ptr != NULL) {
00676         uint16_t option_delta;
00677 
00678         option_delta = (option_number - *previous_option_number);
00679 
00680         /* * * Build option header * * */
00681 
00682         uint8_t first_byte;
00683 
00684         /* First option length without extended part */
00685         if (option_len <= 12) {
00686             first_byte = option_len;
00687         }
00688 
00689         else if (option_len > 12 && option_len < 269) {
00690             first_byte = 0x0D;
00691         }
00692 
00693         else /*if (option_len >= 269)*/ {
00694             first_byte = 0x0E;
00695         }
00696 
00697         uint8_t *dest_packet = *dst_packet_data_pptr;
00698 
00699         /* Then option delta with extensions, and move pointer */
00700         if (option_delta <= 12) {
00701             dest_packet[0] = first_byte + (option_delta << 4);
00702             dest_packet += 1;
00703         }
00704 
00705         else if (option_delta > 12 && option_delta < 269) {
00706             dest_packet[0] = first_byte + 0xD0;
00707             option_delta -= 13;
00708 
00709             dest_packet[1] = (uint8_t)option_delta;
00710             dest_packet += 2;
00711         }
00712         //This is currently dead code (but possibly needed in future)
00713         else /*if (option_delta >= 269)*/ {
00714             dest_packet[0] = first_byte + 0xE0;
00715             option_delta -= 269;
00716 
00717             dest_packet[1] = (option_delta >> 8);
00718             dest_packet[2] = (uint8_t)option_delta;
00719             dest_packet += 3;
00720         }
00721 
00722         /* Now option length extensions, if needed */
00723         if (option_len > 12 && option_len < 269) {
00724             dest_packet[0] = (uint8_t)(option_len - 13);
00725             dest_packet += 1;
00726         }
00727 
00728         else if (option_len >= 269) {
00729             dest_packet[0] = ((option_len - 269) >> 8);
00730             dest_packet[1] = (uint8_t)(option_len - 269);
00731             dest_packet += 2;
00732         }
00733 
00734         *previous_option_number = option_number;
00735 
00736         /* Write Option value */
00737         memcpy(dest_packet, option_ptr, option_len);
00738 
00739         /* Increase destination Packet data pointer */
00740         dest_packet += option_len;
00741 
00742         *dst_packet_data_pptr = dest_packet;
00743 
00744         return 1;
00745     }
00746 
00747     /* Success */
00748     return 0;
00749 }
00750 
00751 /**
00752  * \brief Constructs a uint Options part of Packet data
00753  *
00754  * \param **dst_packet_data_pptr is destination for built Packet data; NULL
00755  *        to compute size only.
00756  *
00757  * \param option_value is Option value to be added
00758  *
00759  * \param option_number is Option number to be added
00760  *
00761  * \return Return value is total option size, or -1 in write failure case
00762  */
00763 static uint8_t sn_coap_builder_options_build_add_uint_option(uint8_t **dst_packet_data_pptr, uint32_t option_value, sn_coap_option_numbers_e option_number, uint16_t *previous_option_number)
00764 {
00765     uint8_t payload[4];
00766     uint8_t len = 0;
00767     /* Construct the variable-length payload representing the value */
00768     for (uint8_t i = 0; i < 4; i++) {
00769         if (len > 0 || (option_value & 0xff000000)) {
00770             payload[len++] = option_value >> 24;
00771         }
00772         option_value <<= 8;
00773     }
00774 
00775     /* If output pointer isn't NULL, write it out */
00776     if (dst_packet_data_pptr) {
00777         // No need to check & handle return value, as the function returns failure only if the option pointer is zero
00778         // and it is pointing to a local variable here.
00779         sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, len, payload, option_number, previous_option_number);
00780     }
00781 
00782     /* Return the total option size */
00783     return 1 + len;
00784 }
00785 
00786 /**
00787  * \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)
00788  *
00789  * \brief Builds Option Uri-Query from given CoAP Header structure to Packet data
00790  *
00791  * \param **dst_packet_data_pptr is destination for built Packet data
00792  *
00793  * \param uint8_t **src_ptr
00794  *
00795  *  \param uint16_t src_len
00796  *
00797  *  \paramsn_coap_option_numbers_e option option to be added
00798  *
00799  * \return Return value is 0 always
00800  */
00801 static void sn_coap_builder_options_build_add_multiple_option(uint8_t **dst_packet_data_pptr, const uint8_t *src_pptr, uint16_t src_len, sn_coap_option_numbers_e option, uint16_t *previous_option_number)
00802 {
00803     /* Check if there is option at all */
00804     if (src_pptr != NULL) {
00805         const uint8_t *query_ptr            = src_pptr;
00806         uint8_t     query_part_count        = 0;
00807         uint16_t    query_len               = src_len;
00808         uint8_t     i                       = 0;
00809         uint16_t    query_part_offset       = 0;
00810 
00811         /* Get query part count */
00812         query_part_count = sn_coap_builder_options_get_option_part_count(query_len, query_ptr, option);
00813 
00814         /* * * * Options by adding all parts to option * * * */
00815         for (i = 0; i < query_part_count; i++) {
00816             /* Get length of query part */
00817             uint16_t one_query_part_len = sn_coap_builder_options_get_option_part_length_from_whole_option_string(query_len, query_ptr, i, option);
00818 
00819             /* Get position of query part */
00820             query_part_offset = sn_coap_builder_options_get_option_part_position(query_len, query_ptr, i, option);
00821 
00822             /* Add Uri-query's one part to Options */
00823             sn_coap_builder_options_build_add_one_option(dst_packet_data_pptr, one_query_part_len, src_pptr + query_part_offset, option, previous_option_number);
00824         }
00825     }
00826     /* Success */
00827 }
00828 
00829 
00830 /**
00831  * \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)
00832  *
00833  * \brief Calculates needed Packet data memory size for option
00834  *
00835  * \param path_len is length of calculated strting(s)
00836  *
00837  * \param *path_ptr is pointer to calculated options
00838  *
00839  * \return Return value is count of needed memory as bytes for Uri-query option
00840  */
00841 static uint16_t sn_coap_builder_options_calc_option_size(uint16_t query_len, const uint8_t *query_ptr, sn_coap_option_numbers_e option)
00842 {
00843     uint8_t     query_part_count    = sn_coap_builder_options_get_option_part_count(query_len, query_ptr, option);
00844     uint8_t     i                   = 0;
00845     uint16_t    ret_value           = 0;
00846 
00847     /* * * * * * * * * * * * * * * * * * * * * * * * */
00848     /* * * * Calculate Uri-query options length  * * */
00849     /* * * * * * * * * * * * * * * * * * * * * * * * */
00850     for (i = 0; i < query_part_count; i++) {
00851         /* * * Length of Option number and Option value length * * */
00852 
00853         /* Get length of Query part */
00854         uint16_t one_query_part_len = sn_coap_builder_options_get_option_part_length_from_whole_option_string(query_len, query_ptr, i, option);
00855 
00856         /* Check option length */
00857         switch (option) {
00858             case (COAP_OPTION_ETAG):            /* Length 1-8 */
00859                 if (one_query_part_len < 1 || one_query_part_len > 8) {
00860                     return 0;
00861                 }
00862                 break;
00863             case (COAP_OPTION_LOCATION_PATH):   /* Length 0-255 */
00864             case (COAP_OPTION_URI_PATH):        /* Length 0-255 */
00865             case (COAP_OPTION_LOCATION_QUERY):  /* Length 0-255 */
00866                 if (one_query_part_len > 255) {
00867                     return 0;
00868                 }
00869                 break;
00870             case (COAP_OPTION_URI_QUERY):       /* Length 1-255 */
00871                 if (one_query_part_len < 1 || one_query_part_len > 255) {
00872                     return 0;
00873                 }
00874                 break;
00875 //            case (COAP_OPTION_ACCEPT):          /* Length 0-2 */
00876 //                if (one_query_part_len > 2) {
00877 //                    return 0;
00878 //                }
00879 //                break;
00880             default:
00881                 break; //impossible scenario currently
00882         }
00883 
00884         /* Check if 4 bits are enough for writing Option value length */
00885         if (one_query_part_len <= 12) {
00886             /* 4 bits are enough for Option value length */
00887             ret_value++;
00888         } else if (one_query_part_len >= 13 && one_query_part_len < 269) {
00889             /* Extra byte for Option value length is needed */
00890             ret_value += 2;
00891         }
00892         //This can only happen if we are in default case above, currently is not happening
00893         else if (one_query_part_len >= 270 && one_query_part_len < 1034) {
00894             /* Extra bytes for Option value length is needed */
00895             ret_value += 3;
00896         }
00897 
00898 
00899         /* * * Length of Option value * * */
00900 
00901         /* Increase options length */
00902         ret_value += one_query_part_len;
00903     }
00904 
00905     /* Success */
00906     return ret_value;
00907 }
00908 
00909 
00910 
00911 /**
00912  * \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)
00913  *
00914  * \brief Gets query part count from whole option string
00915  *
00916  * \param query_len is length of whole Path
00917  *
00918  * \param *query_ptr is pointer to the start of whole Path
00919  *
00920  * \return Return value is count of query parts
00921  */
00922 static uint8_t sn_coap_builder_options_get_option_part_count(uint16_t query_len, const uint8_t *query_ptr, sn_coap_option_numbers_e option)
00923 {
00924     uint8_t  returned_query_count = 0;
00925     uint16_t query_len_index      = 0;
00926     uint8_t  char_to_search       = '&';
00927 
00928     if (option == COAP_OPTION_URI_PATH || option == COAP_OPTION_LOCATION_PATH) {
00929         char_to_search = '/';
00930     }
00931 
00932     /* Loop whole query and search '\0' characters (not first and last char) */
00933     for (query_len_index = 1; query_len_index < query_len - 1; query_len_index++) {
00934         /* If new query part starts */
00935         if (*(query_ptr + query_len_index) == char_to_search) { /* If match */
00936             returned_query_count++;
00937         }
00938     }
00939 
00940     returned_query_count++;
00941 
00942     return returned_query_count;
00943 }
00944 
00945 /**
00946  * \fn static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option_string(uint16_t query_len,
00947                                                                              uint8_t *query_ptr,
00948                                                                              uint8_t query_index, sn_coap_option_numbers_e option)
00949  *
00950  * \brief Gets one's query part length from whole query string
00951  *
00952  * \param query_len is length of whole string
00953  *
00954  * \param *query_ptr is pointer to the start of whole string
00955  *
00956  * \param query_index is query part index to be found
00957  *
00958  * \param sn_coap_option_numbers_e option is option number of the option
00959  *
00960  * \return Return value is length of query part
00961  */
00962 static uint16_t sn_coap_builder_options_get_option_part_length_from_whole_option_string(uint16_t query_len, const uint8_t *query_ptr,
00963         uint8_t query_index, sn_coap_option_numbers_e option)
00964 {
00965     uint16_t returned_query_part_len = 0;
00966     uint8_t  temp_query_index        = 0;
00967     uint16_t query_len_index         = 0;
00968     uint8_t  char_to_search          = '&';
00969 
00970     if (option == COAP_OPTION_URI_PATH || option == COAP_OPTION_LOCATION_PATH) {
00971         char_to_search = '/';
00972     }
00973 
00974     /* Loop whole query and search '\0' characters */
00975     for (query_len_index = 0; query_len_index < query_len; query_len_index++) {
00976         /* Store character to temp_char for helping debugging */
00977         uint8_t temp_char = *query_ptr;
00978 
00979         /* If new query part starts */
00980         if (temp_char == char_to_search && returned_query_part_len > 0) { /* returned_query_part_len > 0 is for querys which start with "\0" */
00981             /* If query part index is wanted */
00982             if (temp_query_index == query_index) {
00983                 /* Return length of query part */
00984                 return returned_query_part_len;
00985             } else {
00986                 /* Reset length of query part because wanted query part finding continues*/
00987                 returned_query_part_len = 0;
00988             }
00989 
00990             /* Next query part is looped */
00991             temp_query_index++;
00992         } else if (temp_char != char_to_search) { /* Else if query part continues */
00993             /* Increase query part length */
00994             returned_query_part_len++;
00995         }
00996 
00997         query_ptr++;
00998     }
00999 
01000     /* Return length of query part in cases that query part does not finish to '\0' character (last query part can be like that) */
01001     return returned_query_part_len;
01002 }
01003 
01004 /**
01005  * \fn static uint16_t sn_coap_builder_options_get_option_part_position(uint16_t query_len,
01006                                                                uint8_t *query_ptr,
01007                                                                uint8_t query_index, sn_coap_option_numbers_e option)
01008  *
01009  * \brief Gets query part position in whole query
01010  *
01011  * \param query_len is length of whole query
01012  *
01013  * \param *query_ptr is pointer to the start of whole query
01014  *
01015  * \param query_index is query part index to be found
01016  *
01017  * \return Return value is position (= offset) of query part in whole query. In
01018  *         fail cases -1 is returned.
01019  */
01020 static int16_t sn_coap_builder_options_get_option_part_position(uint16_t query_len, const uint8_t *query_ptr,
01021         uint8_t query_index, sn_coap_option_numbers_e option)
01022 {
01023     uint16_t returned_query_part_offset = 0;
01024     uint8_t  temp_query_index           = 0;
01025     uint16_t query_len_index            = 0;
01026     uint8_t  char_to_search             = '&';
01027 
01028     if (option == COAP_OPTION_URI_PATH || option == COAP_OPTION_LOCATION_PATH) {
01029         char_to_search = '/';
01030     }
01031 
01032     if (query_index == 0) {
01033         if (*query_ptr == 0 || *query_ptr == char_to_search) {
01034             return 1;
01035         } else {
01036             return 0;
01037         }
01038     }
01039 
01040     /* Loop whole query and search separator characters */
01041     for (query_len_index = 0; query_len_index < query_len; query_len_index++) {
01042         /* Store character to temp_char for helping debugging */
01043         uint8_t temp_char = *query_ptr;
01044 
01045         /* If new query part starts */
01046         if (temp_char == char_to_search && returned_query_part_offset > 0) { /* returned_query_part_offset > 0 is for querys which start with searched char */
01047             /* If query part index is wanted */
01048             if (temp_query_index == (query_index - 1)) {
01049                 /* Return offset of query part */
01050                 return (returned_query_part_offset + 1); /* Plus one is for passing separator */
01051             }
01052 
01053             /* Next query part is looped */
01054             temp_query_index++;
01055         }
01056 
01057         returned_query_part_offset++;
01058 
01059         query_ptr++;
01060     }
01061 
01062     return -1; //Dead code?
01063 }
01064 
01065 
01066 /**
01067  * \fn static void sn_coap_builder_payload_build(uint8_t **dst_packet_data_pptr, sn_coap_hdr_s *src_coap_msg_ptr)
01068  *
01069  * \brief Builds Options part of Packet data
01070  *
01071  * \param **dst_packet_data_pptr is destination for built Packet data
01072  *
01073  * \param *src_coap_msg_ptr is source for building Packet data
01074  */
01075 static void sn_coap_builder_payload_build(uint8_t **dst_packet_data_pptr, const sn_coap_hdr_s *src_coap_msg_ptr)
01076 {
01077     /* Check if Payload is used at all */
01078     if (src_coap_msg_ptr->payload_len && src_coap_msg_ptr->payload_ptr != NULL) {
01079         /* Write Payload marker */
01080 
01081         **dst_packet_data_pptr = 0xff;
01082         (*dst_packet_data_pptr)++;
01083 
01084         /* Write Payload */
01085         memcpy(*dst_packet_data_pptr, src_coap_msg_ptr->payload_ptr, src_coap_msg_ptr->payload_len);
01086 
01087         /* Increase destination Packet data pointer */
01088         (*dst_packet_data_pptr) += src_coap_msg_ptr->payload_len;
01089     }
01090 }