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