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