Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

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