sandbox / mbed-client-c

Fork of mbed-client-c by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:31:54 2016 -0600
Revision:
1:43f5c94c6771
Child:
4:5d91b0f5038c
Initial move of mbed-client-c to mercurial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 1:43f5c94c6771 1 /*
Christopher Haster 1:43f5c94c6771 2 * Copyright (c) 2011-2015 ARM Limited. All rights reserved.
Christopher Haster 1:43f5c94c6771 3 * SPDX-License-Identifier: Apache-2.0
Christopher Haster 1:43f5c94c6771 4 * Licensed under the Apache License, Version 2.0 (the License); you may
Christopher Haster 1:43f5c94c6771 5 * not use this file except in compliance with the License.
Christopher Haster 1:43f5c94c6771 6 * You may obtain a copy of the License at
Christopher Haster 1:43f5c94c6771 7 *
Christopher Haster 1:43f5c94c6771 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 1:43f5c94c6771 9 *
Christopher Haster 1:43f5c94c6771 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 1:43f5c94c6771 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
Christopher Haster 1:43f5c94c6771 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 1:43f5c94c6771 13 * See the License for the specific language governing permissions and
Christopher Haster 1:43f5c94c6771 14 * limitations under the License.
Christopher Haster 1:43f5c94c6771 15 */
Christopher Haster 1:43f5c94c6771 16
Christopher Haster 1:43f5c94c6771 17 /**
Christopher Haster 1:43f5c94c6771 18 *\file sn_coap_parser.c
Christopher Haster 1:43f5c94c6771 19 *
Christopher Haster 1:43f5c94c6771 20 * \brief CoAP Header parser
Christopher Haster 1:43f5c94c6771 21 *
Christopher Haster 1:43f5c94c6771 22 * Functionality: Parses CoAP Header
Christopher Haster 1:43f5c94c6771 23 *
Christopher Haster 1:43f5c94c6771 24 */
Christopher Haster 1:43f5c94c6771 25
Christopher Haster 1:43f5c94c6771 26 /* * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 27 /* * * * INCLUDE FILES * * * */
Christopher Haster 1:43f5c94c6771 28 /* * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 29
Christopher Haster 1:43f5c94c6771 30 #include <stdio.h>
Christopher Haster 1:43f5c94c6771 31 #include <string.h> /* For memset() and memcpy() */
Christopher Haster 1:43f5c94c6771 32
Christopher Haster 1:43f5c94c6771 33 #include "ns_types.h"
Christopher Haster 1:43f5c94c6771 34 #include "sn_nsdl.h"
Christopher Haster 1:43f5c94c6771 35 #include "sn_coap_header.h"
Christopher Haster 1:43f5c94c6771 36 #include "sn_coap_protocol.h"
Christopher Haster 1:43f5c94c6771 37 #include "sn_coap_header_internal.h"
Christopher Haster 1:43f5c94c6771 38 #include "sn_coap_protocol_internal.h"
Christopher Haster 1:43f5c94c6771 39
Christopher Haster 1:43f5c94c6771 40 /* * * * * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 41 /* * * * LOCAL FUNCTION PROTOTYPES * * * */
Christopher Haster 1:43f5c94c6771 42 /* * * * * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 43
Christopher Haster 1:43f5c94c6771 44 static void sn_coap_parser_header_parse(uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr, coap_version_e *coap_version_ptr);
Christopher Haster 1:43f5c94c6771 45 static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr, uint8_t *packet_data_start_ptr, uint16_t packet_len);
Christopher Haster 1:43f5c94c6771 46 static int8_t sn_coap_parser_options_parse_multiple_options(struct coap_s *handle, uint8_t **packet_data_pptr, uint16_t packet_left_len, uint8_t **dst_pptr, uint16_t *dst_len_ptr, sn_coap_option_numbers_e option, uint16_t option_number_len);
Christopher Haster 1:43f5c94c6771 47 static int16_t sn_coap_parser_options_count_needed_memory_multiple_option(uint8_t *packet_data_ptr, uint16_t packet_left_len, sn_coap_option_numbers_e option, uint16_t option_number_len);
Christopher Haster 1:43f5c94c6771 48 static int8_t sn_coap_parser_payload_parse(uint16_t packet_data_len, uint8_t *packet_data_start_ptr, uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 49
Christopher Haster 1:43f5c94c6771 50 sn_coap_hdr_s *sn_coap_parser(struct coap_s *handle, uint16_t packet_data_len, uint8_t *packet_data_ptr, coap_version_e *coap_version_ptr)
Christopher Haster 1:43f5c94c6771 51 {
Christopher Haster 1:43f5c94c6771 52 uint8_t *data_temp_ptr = packet_data_ptr;
Christopher Haster 1:43f5c94c6771 53 sn_coap_hdr_s *parsed_and_returned_coap_msg_ptr = NULL;
Christopher Haster 1:43f5c94c6771 54
Christopher Haster 1:43f5c94c6771 55 /* * * * Check given pointer * * * */
Christopher Haster 1:43f5c94c6771 56 if (packet_data_ptr == NULL || packet_data_len < 4 || handle == NULL) {
Christopher Haster 1:43f5c94c6771 57 return NULL;
Christopher Haster 1:43f5c94c6771 58 }
Christopher Haster 1:43f5c94c6771 59
Christopher Haster 1:43f5c94c6771 60 /* * * * Allocate memory for parsed and returned CoAP message and initialize allocated memory with with zero values * * * */
Christopher Haster 1:43f5c94c6771 61 parsed_and_returned_coap_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 62
Christopher Haster 1:43f5c94c6771 63 if (parsed_and_returned_coap_msg_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 64 return NULL;
Christopher Haster 1:43f5c94c6771 65 }
Christopher Haster 1:43f5c94c6771 66
Christopher Haster 1:43f5c94c6771 67 memset(parsed_and_returned_coap_msg_ptr, 0x00, sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 68
Christopher Haster 1:43f5c94c6771 69
Christopher Haster 1:43f5c94c6771 70 /* * * * Header parsing, move pointer over the header... * * * */
Christopher Haster 1:43f5c94c6771 71 sn_coap_parser_header_parse(&data_temp_ptr, parsed_and_returned_coap_msg_ptr, coap_version_ptr);
Christopher Haster 1:43f5c94c6771 72
Christopher Haster 1:43f5c94c6771 73
Christopher Haster 1:43f5c94c6771 74 /* * * * Options parsing, move pointer over the options... * * * */
Christopher Haster 1:43f5c94c6771 75 if (sn_coap_parser_options_parse(handle, &data_temp_ptr, parsed_and_returned_coap_msg_ptr, packet_data_ptr, packet_data_len) != 0) {
Christopher Haster 1:43f5c94c6771 76 /* Release memory of CoAP message */
Christopher Haster 1:43f5c94c6771 77 sn_coap_parser_release_allocated_coap_msg_mem(handle, parsed_and_returned_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 78 return NULL;
Christopher Haster 1:43f5c94c6771 79 }
Christopher Haster 1:43f5c94c6771 80
Christopher Haster 1:43f5c94c6771 81 /* * * * Payload parsing * * * */
Christopher Haster 1:43f5c94c6771 82 if (sn_coap_parser_payload_parse(packet_data_len, packet_data_ptr, &data_temp_ptr, parsed_and_returned_coap_msg_ptr) == -1) {
Christopher Haster 1:43f5c94c6771 83 /* Release memory of CoAP message */
Christopher Haster 1:43f5c94c6771 84 sn_coap_parser_release_allocated_coap_msg_mem(handle, parsed_and_returned_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 85 return NULL;
Christopher Haster 1:43f5c94c6771 86 }
Christopher Haster 1:43f5c94c6771 87 /* * * * Return parsed CoAP message * * * * */
Christopher Haster 1:43f5c94c6771 88 return parsed_and_returned_coap_msg_ptr;
Christopher Haster 1:43f5c94c6771 89 }
Christopher Haster 1:43f5c94c6771 90
Christopher Haster 1:43f5c94c6771 91 void sn_coap_parser_release_allocated_coap_msg_mem(struct coap_s *handle, sn_coap_hdr_s *freed_coap_msg_ptr)
Christopher Haster 1:43f5c94c6771 92 {
Christopher Haster 1:43f5c94c6771 93 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 94 return;
Christopher Haster 1:43f5c94c6771 95 }
Christopher Haster 1:43f5c94c6771 96
Christopher Haster 1:43f5c94c6771 97 if (freed_coap_msg_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 98 if (freed_coap_msg_ptr->uri_path_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 99 handle->sn_coap_protocol_free(freed_coap_msg_ptr->uri_path_ptr);
Christopher Haster 1:43f5c94c6771 100 }
Christopher Haster 1:43f5c94c6771 101
Christopher Haster 1:43f5c94c6771 102 if (freed_coap_msg_ptr->token_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 103 handle->sn_coap_protocol_free(freed_coap_msg_ptr->token_ptr);
Christopher Haster 1:43f5c94c6771 104 }
Christopher Haster 1:43f5c94c6771 105
Christopher Haster 1:43f5c94c6771 106 if (freed_coap_msg_ptr->content_type_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 107 handle->sn_coap_protocol_free(freed_coap_msg_ptr->content_type_ptr);
Christopher Haster 1:43f5c94c6771 108 }
Christopher Haster 1:43f5c94c6771 109
Christopher Haster 1:43f5c94c6771 110 if (freed_coap_msg_ptr->options_list_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 111 if (freed_coap_msg_ptr->options_list_ptr->max_age_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 112 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->max_age_ptr);
Christopher Haster 1:43f5c94c6771 113 }
Christopher Haster 1:43f5c94c6771 114
Christopher Haster 1:43f5c94c6771 115 if (freed_coap_msg_ptr->options_list_ptr->proxy_uri_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 116 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->proxy_uri_ptr);
Christopher Haster 1:43f5c94c6771 117 }
Christopher Haster 1:43f5c94c6771 118
Christopher Haster 1:43f5c94c6771 119 if (freed_coap_msg_ptr->options_list_ptr->etag_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 120 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->etag_ptr);
Christopher Haster 1:43f5c94c6771 121 }
Christopher Haster 1:43f5c94c6771 122
Christopher Haster 1:43f5c94c6771 123 if (freed_coap_msg_ptr->options_list_ptr->uri_host_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 124 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->uri_host_ptr);
Christopher Haster 1:43f5c94c6771 125 }
Christopher Haster 1:43f5c94c6771 126
Christopher Haster 1:43f5c94c6771 127 if (freed_coap_msg_ptr->options_list_ptr->location_path_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 128 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->location_path_ptr);
Christopher Haster 1:43f5c94c6771 129 }
Christopher Haster 1:43f5c94c6771 130
Christopher Haster 1:43f5c94c6771 131 if (freed_coap_msg_ptr->options_list_ptr->uri_port_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 132 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->uri_port_ptr);
Christopher Haster 1:43f5c94c6771 133 }
Christopher Haster 1:43f5c94c6771 134
Christopher Haster 1:43f5c94c6771 135 if (freed_coap_msg_ptr->options_list_ptr->location_query_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 136 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->location_query_ptr);
Christopher Haster 1:43f5c94c6771 137 }
Christopher Haster 1:43f5c94c6771 138
Christopher Haster 1:43f5c94c6771 139 if (freed_coap_msg_ptr->options_list_ptr->observe_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 140 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->observe_ptr);
Christopher Haster 1:43f5c94c6771 141 }
Christopher Haster 1:43f5c94c6771 142
Christopher Haster 1:43f5c94c6771 143 if (freed_coap_msg_ptr->options_list_ptr->uri_query_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 144 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->uri_query_ptr);
Christopher Haster 1:43f5c94c6771 145 }
Christopher Haster 1:43f5c94c6771 146
Christopher Haster 1:43f5c94c6771 147 if (freed_coap_msg_ptr->options_list_ptr->block2_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 148 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->block2_ptr);
Christopher Haster 1:43f5c94c6771 149 }
Christopher Haster 1:43f5c94c6771 150
Christopher Haster 1:43f5c94c6771 151 if (freed_coap_msg_ptr->options_list_ptr->block1_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 152 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->block1_ptr);
Christopher Haster 1:43f5c94c6771 153 }
Christopher Haster 1:43f5c94c6771 154 if (freed_coap_msg_ptr->options_list_ptr->accept_ptr != NULL) {
Christopher Haster 1:43f5c94c6771 155 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr->accept_ptr);
Christopher Haster 1:43f5c94c6771 156 }
Christopher Haster 1:43f5c94c6771 157
Christopher Haster 1:43f5c94c6771 158 handle->sn_coap_protocol_free(freed_coap_msg_ptr->options_list_ptr);
Christopher Haster 1:43f5c94c6771 159 }
Christopher Haster 1:43f5c94c6771 160
Christopher Haster 1:43f5c94c6771 161 handle->sn_coap_protocol_free(freed_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 162 }
Christopher Haster 1:43f5c94c6771 163 }
Christopher Haster 1:43f5c94c6771 164
Christopher Haster 1:43f5c94c6771 165 /**
Christopher Haster 1:43f5c94c6771 166 * \fn static void sn_coap_parser_header_parse(uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr, coap_version_e *coap_version_ptr)
Christopher Haster 1:43f5c94c6771 167 *
Christopher Haster 1:43f5c94c6771 168 * \brief Parses CoAP message's Header part from given Packet data
Christopher Haster 1:43f5c94c6771 169 *
Christopher Haster 1:43f5c94c6771 170 * \param **packet_data_ptr is source for Packet data to be parsed to CoAP message
Christopher Haster 1:43f5c94c6771 171 *
Christopher Haster 1:43f5c94c6771 172 * \param *dst_coap_msg_ptr is destination for parsed CoAP message
Christopher Haster 1:43f5c94c6771 173 *
Christopher Haster 1:43f5c94c6771 174 * \param *coap_version_ptr is destination for parsed CoAP specification version
Christopher Haster 1:43f5c94c6771 175 */
Christopher Haster 1:43f5c94c6771 176 static void sn_coap_parser_header_parse(uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr, coap_version_e *coap_version_ptr)
Christopher Haster 1:43f5c94c6771 177 {
Christopher Haster 1:43f5c94c6771 178 /* Parse CoAP Version and message type*/
Christopher Haster 1:43f5c94c6771 179 *coap_version_ptr = (coap_version_e)(**packet_data_pptr & COAP_HEADER_VERSION_MASK);
Christopher Haster 1:43f5c94c6771 180 dst_coap_msg_ptr->msg_type = (sn_coap_msg_type_e)(**packet_data_pptr & COAP_HEADER_MSG_TYPE_MASK);
Christopher Haster 1:43f5c94c6771 181 (*packet_data_pptr) += 1;
Christopher Haster 1:43f5c94c6771 182
Christopher Haster 1:43f5c94c6771 183 /* Parse Message code */
Christopher Haster 1:43f5c94c6771 184 dst_coap_msg_ptr->msg_code = (sn_coap_msg_code_e) **packet_data_pptr;
Christopher Haster 1:43f5c94c6771 185 (*packet_data_pptr) += 1;
Christopher Haster 1:43f5c94c6771 186
Christopher Haster 1:43f5c94c6771 187 /* Parse Message ID */
Christopher Haster 1:43f5c94c6771 188 dst_coap_msg_ptr->msg_id = *(*packet_data_pptr + 1);
Christopher Haster 1:43f5c94c6771 189 dst_coap_msg_ptr->msg_id += **packet_data_pptr << COAP_HEADER_MSG_ID_MSB_SHIFT;
Christopher Haster 1:43f5c94c6771 190 (*packet_data_pptr) += 2;
Christopher Haster 1:43f5c94c6771 191
Christopher Haster 1:43f5c94c6771 192 }
Christopher Haster 1:43f5c94c6771 193
Christopher Haster 1:43f5c94c6771 194 /**
Christopher Haster 1:43f5c94c6771 195 * \fn static uint8_t sn_coap_parser_options_parse(uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr)
Christopher Haster 1:43f5c94c6771 196 *
Christopher Haster 1:43f5c94c6771 197 * \brief Parses CoAP message's Options part from given Packet data
Christopher Haster 1:43f5c94c6771 198 *
Christopher Haster 1:43f5c94c6771 199 * \param **packet_data_pptr is source of Packet data to be parsed to CoAP message
Christopher Haster 1:43f5c94c6771 200 * \param *dst_coap_msg_ptr is destination for parsed CoAP message
Christopher Haster 1:43f5c94c6771 201 *
Christopher Haster 1:43f5c94c6771 202 * \return Return value is 0 in ok case and -1 in failure case
Christopher Haster 1:43f5c94c6771 203 */
Christopher Haster 1:43f5c94c6771 204 static int8_t sn_coap_parser_options_parse(struct coap_s *handle, uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr, uint8_t *packet_data_start_ptr, uint16_t packet_len)
Christopher Haster 1:43f5c94c6771 205 {
Christopher Haster 1:43f5c94c6771 206 uint8_t previous_option_number = 0;
Christopher Haster 1:43f5c94c6771 207 uint8_t i = 0;
Christopher Haster 1:43f5c94c6771 208 int8_t ret_status = 0;
Christopher Haster 1:43f5c94c6771 209 uint16_t message_left = 0;
Christopher Haster 1:43f5c94c6771 210
Christopher Haster 1:43f5c94c6771 211 /* Parse token, if exists */
Christopher Haster 1:43f5c94c6771 212 dst_coap_msg_ptr->token_len = *packet_data_start_ptr & COAP_HEADER_TOKEN_LENGTH_MASK;
Christopher Haster 1:43f5c94c6771 213
Christopher Haster 1:43f5c94c6771 214 if (dst_coap_msg_ptr->token_len) {
Christopher Haster 1:43f5c94c6771 215 if ((dst_coap_msg_ptr->token_len > 8) || dst_coap_msg_ptr->token_ptr) {
Christopher Haster 1:43f5c94c6771 216 return -1;
Christopher Haster 1:43f5c94c6771 217 }
Christopher Haster 1:43f5c94c6771 218
Christopher Haster 1:43f5c94c6771 219 dst_coap_msg_ptr->token_ptr = handle->sn_coap_protocol_malloc(dst_coap_msg_ptr->token_len);
Christopher Haster 1:43f5c94c6771 220
Christopher Haster 1:43f5c94c6771 221 if (dst_coap_msg_ptr->token_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 222 return -1;
Christopher Haster 1:43f5c94c6771 223 }
Christopher Haster 1:43f5c94c6771 224
Christopher Haster 1:43f5c94c6771 225 memcpy(dst_coap_msg_ptr->token_ptr, *packet_data_pptr, dst_coap_msg_ptr->token_len);
Christopher Haster 1:43f5c94c6771 226 (*packet_data_pptr) += dst_coap_msg_ptr->token_len;
Christopher Haster 1:43f5c94c6771 227 }
Christopher Haster 1:43f5c94c6771 228
Christopher Haster 1:43f5c94c6771 229 message_left = packet_len - ((*packet_data_pptr) - packet_data_start_ptr);
Christopher Haster 1:43f5c94c6771 230
Christopher Haster 1:43f5c94c6771 231 /* Loop all Options */
Christopher Haster 1:43f5c94c6771 232 while (message_left && (**packet_data_pptr != 0xff)) {
Christopher Haster 1:43f5c94c6771 233
Christopher Haster 1:43f5c94c6771 234 /* Get option length WITHOUT extensions */
Christopher Haster 1:43f5c94c6771 235 uint16_t option_len = (**packet_data_pptr & 0x0F);
Christopher Haster 1:43f5c94c6771 236
Christopher Haster 1:43f5c94c6771 237 /* Option number length 15 is reserved for the future use - ERROR */
Christopher Haster 1:43f5c94c6771 238 if (option_len == 15) {
Christopher Haster 1:43f5c94c6771 239 return -1;
Christopher Haster 1:43f5c94c6771 240 }
Christopher Haster 1:43f5c94c6771 241
Christopher Haster 1:43f5c94c6771 242 /* Resolve option delta */
Christopher Haster 1:43f5c94c6771 243 uint16_t option_number = (**packet_data_pptr >> COAP_OPTIONS_OPTION_NUMBER_SHIFT);
Christopher Haster 1:43f5c94c6771 244
Christopher Haster 1:43f5c94c6771 245 if (option_number == 13) {
Christopher Haster 1:43f5c94c6771 246 option_number = *(*packet_data_pptr + 1) + 13;
Christopher Haster 1:43f5c94c6771 247 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 248 } else if (option_number == 14) {
Christopher Haster 1:43f5c94c6771 249 option_number = *(*packet_data_pptr + 2);
Christopher Haster 1:43f5c94c6771 250 option_number += (*(*packet_data_pptr + 1) << 8) + 269;
Christopher Haster 1:43f5c94c6771 251 (*packet_data_pptr) += 2;
Christopher Haster 1:43f5c94c6771 252 }
Christopher Haster 1:43f5c94c6771 253 /* Option number 15 reserved for payload marker. This is handled as a error! */
Christopher Haster 1:43f5c94c6771 254 else if (option_number == 15) {
Christopher Haster 1:43f5c94c6771 255 return -1;
Christopher Haster 1:43f5c94c6771 256 }
Christopher Haster 1:43f5c94c6771 257
Christopher Haster 1:43f5c94c6771 258 /* Add previous option to option delta and get option number */
Christopher Haster 1:43f5c94c6771 259 option_number += previous_option_number;
Christopher Haster 1:43f5c94c6771 260
Christopher Haster 1:43f5c94c6771 261 /* Add possible option length extension to resolve full length of the option */
Christopher Haster 1:43f5c94c6771 262 if (option_len == 13) {
Christopher Haster 1:43f5c94c6771 263 option_len = *(*packet_data_pptr + 1) + 13;
Christopher Haster 1:43f5c94c6771 264 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 265 } else if (option_len == 14) {
Christopher Haster 1:43f5c94c6771 266 option_len = *(*packet_data_pptr + 2);
Christopher Haster 1:43f5c94c6771 267 option_len += (*(*packet_data_pptr + 1) << 8) + 269;
Christopher Haster 1:43f5c94c6771 268 (*packet_data_pptr) += 2;
Christopher Haster 1:43f5c94c6771 269 }
Christopher Haster 1:43f5c94c6771 270
Christopher Haster 1:43f5c94c6771 271
Christopher Haster 1:43f5c94c6771 272 /* * * Parse option itself * * */
Christopher Haster 1:43f5c94c6771 273 /* Some options are handled independently in own functions */
Christopher Haster 1:43f5c94c6771 274 previous_option_number = option_number;
Christopher Haster 1:43f5c94c6771 275
Christopher Haster 1:43f5c94c6771 276 /* Allocate options_list_ptr if needed */
Christopher Haster 1:43f5c94c6771 277 switch (option_number) {
Christopher Haster 1:43f5c94c6771 278 case COAP_OPTION_MAX_AGE:
Christopher Haster 1:43f5c94c6771 279 case COAP_OPTION_PROXY_URI:
Christopher Haster 1:43f5c94c6771 280 case COAP_OPTION_ETAG:
Christopher Haster 1:43f5c94c6771 281 case COAP_OPTION_URI_HOST:
Christopher Haster 1:43f5c94c6771 282 case COAP_OPTION_LOCATION_PATH:
Christopher Haster 1:43f5c94c6771 283 case COAP_OPTION_URI_PORT:
Christopher Haster 1:43f5c94c6771 284 case COAP_OPTION_LOCATION_QUERY:
Christopher Haster 1:43f5c94c6771 285 case COAP_OPTION_OBSERVE:
Christopher Haster 1:43f5c94c6771 286 case COAP_OPTION_URI_QUERY:
Christopher Haster 1:43f5c94c6771 287 case COAP_OPTION_BLOCK2:
Christopher Haster 1:43f5c94c6771 288 case COAP_OPTION_BLOCK1:
Christopher Haster 1:43f5c94c6771 289 case COAP_OPTION_ACCEPT:
Christopher Haster 1:43f5c94c6771 290 if (dst_coap_msg_ptr->options_list_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 291 dst_coap_msg_ptr->options_list_ptr = handle->sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 292 if (NULL == dst_coap_msg_ptr->options_list_ptr) {
Christopher Haster 1:43f5c94c6771 293 return -1;
Christopher Haster 1:43f5c94c6771 294 }
Christopher Haster 1:43f5c94c6771 295 memset(dst_coap_msg_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 296 }
Christopher Haster 1:43f5c94c6771 297 break;
Christopher Haster 1:43f5c94c6771 298 }
Christopher Haster 1:43f5c94c6771 299
Christopher Haster 1:43f5c94c6771 300 /* Parse option */
Christopher Haster 1:43f5c94c6771 301 switch (option_number) {
Christopher Haster 1:43f5c94c6771 302 case COAP_OPTION_CONTENT_FORMAT:
Christopher Haster 1:43f5c94c6771 303 if ((option_len > 2) || (dst_coap_msg_ptr->content_type_ptr)) {
Christopher Haster 1:43f5c94c6771 304 return -1;
Christopher Haster 1:43f5c94c6771 305 }
Christopher Haster 1:43f5c94c6771 306 dst_coap_msg_ptr->content_type_len = option_len;
Christopher Haster 1:43f5c94c6771 307 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 308
Christopher Haster 1:43f5c94c6771 309 if (option_len) {
Christopher Haster 1:43f5c94c6771 310 dst_coap_msg_ptr->content_type_ptr = handle->sn_coap_protocol_malloc(option_len);
Christopher Haster 1:43f5c94c6771 311
Christopher Haster 1:43f5c94c6771 312 if (dst_coap_msg_ptr->content_type_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 313 return -1;
Christopher Haster 1:43f5c94c6771 314 }
Christopher Haster 1:43f5c94c6771 315
Christopher Haster 1:43f5c94c6771 316 memcpy(dst_coap_msg_ptr->content_type_ptr, *packet_data_pptr, option_len);
Christopher Haster 1:43f5c94c6771 317 (*packet_data_pptr) += option_len;
Christopher Haster 1:43f5c94c6771 318 }
Christopher Haster 1:43f5c94c6771 319 break;
Christopher Haster 1:43f5c94c6771 320
Christopher Haster 1:43f5c94c6771 321 case COAP_OPTION_MAX_AGE:
Christopher Haster 1:43f5c94c6771 322 if ((option_len > 4) || dst_coap_msg_ptr->options_list_ptr->max_age_ptr) {
Christopher Haster 1:43f5c94c6771 323 return -1;
Christopher Haster 1:43f5c94c6771 324 }
Christopher Haster 1:43f5c94c6771 325 dst_coap_msg_ptr->options_list_ptr->max_age_len = option_len;
Christopher Haster 1:43f5c94c6771 326 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 327
Christopher Haster 1:43f5c94c6771 328 if (option_len) {
Christopher Haster 1:43f5c94c6771 329 dst_coap_msg_ptr->options_list_ptr->max_age_ptr = handle->sn_coap_protocol_malloc(option_len);
Christopher Haster 1:43f5c94c6771 330
Christopher Haster 1:43f5c94c6771 331 if (dst_coap_msg_ptr->options_list_ptr->max_age_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 332 return -1;
Christopher Haster 1:43f5c94c6771 333 }
Christopher Haster 1:43f5c94c6771 334
Christopher Haster 1:43f5c94c6771 335 memcpy(dst_coap_msg_ptr->options_list_ptr->max_age_ptr, *packet_data_pptr, option_len);
Christopher Haster 1:43f5c94c6771 336 (*packet_data_pptr) += option_len;
Christopher Haster 1:43f5c94c6771 337 }
Christopher Haster 1:43f5c94c6771 338 break;
Christopher Haster 1:43f5c94c6771 339
Christopher Haster 1:43f5c94c6771 340 case COAP_OPTION_PROXY_URI:
Christopher Haster 1:43f5c94c6771 341 if ((option_len > 1034) || (option_len < 1) || dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr) {
Christopher Haster 1:43f5c94c6771 342 return -1;
Christopher Haster 1:43f5c94c6771 343 }
Christopher Haster 1:43f5c94c6771 344 dst_coap_msg_ptr->options_list_ptr->proxy_uri_len = option_len;
Christopher Haster 1:43f5c94c6771 345 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 346
Christopher Haster 1:43f5c94c6771 347 dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr = handle->sn_coap_protocol_malloc(option_len);
Christopher Haster 1:43f5c94c6771 348
Christopher Haster 1:43f5c94c6771 349 if (dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 350 return -1;
Christopher Haster 1:43f5c94c6771 351 }
Christopher Haster 1:43f5c94c6771 352 memcpy(dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr, *packet_data_pptr, option_len);
Christopher Haster 1:43f5c94c6771 353 (*packet_data_pptr) += option_len;
Christopher Haster 1:43f5c94c6771 354
Christopher Haster 1:43f5c94c6771 355 break;
Christopher Haster 1:43f5c94c6771 356
Christopher Haster 1:43f5c94c6771 357 case COAP_OPTION_ETAG:
Christopher Haster 1:43f5c94c6771 358 /* This is managed independently because User gives this option in one character table */
Christopher Haster 1:43f5c94c6771 359
Christopher Haster 1:43f5c94c6771 360 ret_status = sn_coap_parser_options_parse_multiple_options(handle, packet_data_pptr,
Christopher Haster 1:43f5c94c6771 361 message_left,
Christopher Haster 1:43f5c94c6771 362 &dst_coap_msg_ptr->options_list_ptr->etag_ptr,
Christopher Haster 1:43f5c94c6771 363 (uint16_t *)&dst_coap_msg_ptr->options_list_ptr->etag_len,
Christopher Haster 1:43f5c94c6771 364 COAP_OPTION_ETAG, option_len);
Christopher Haster 1:43f5c94c6771 365 if (ret_status >= 0) {
Christopher Haster 1:43f5c94c6771 366 i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
Christopher Haster 1:43f5c94c6771 367 } else {
Christopher Haster 1:43f5c94c6771 368 return -1;
Christopher Haster 1:43f5c94c6771 369 }
Christopher Haster 1:43f5c94c6771 370 break;
Christopher Haster 1:43f5c94c6771 371
Christopher Haster 1:43f5c94c6771 372 case COAP_OPTION_URI_HOST:
Christopher Haster 1:43f5c94c6771 373 if ((option_len > 255) || (option_len < 1) || dst_coap_msg_ptr->options_list_ptr->uri_host_ptr) {
Christopher Haster 1:43f5c94c6771 374 return -1;
Christopher Haster 1:43f5c94c6771 375 }
Christopher Haster 1:43f5c94c6771 376 dst_coap_msg_ptr->options_list_ptr->uri_host_len = option_len;
Christopher Haster 1:43f5c94c6771 377 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 378
Christopher Haster 1:43f5c94c6771 379 dst_coap_msg_ptr->options_list_ptr->uri_host_ptr = handle->sn_coap_protocol_malloc(option_len);
Christopher Haster 1:43f5c94c6771 380
Christopher Haster 1:43f5c94c6771 381 if (dst_coap_msg_ptr->options_list_ptr->uri_host_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 382 return -1;
Christopher Haster 1:43f5c94c6771 383 }
Christopher Haster 1:43f5c94c6771 384 memcpy(dst_coap_msg_ptr->options_list_ptr->uri_host_ptr, *packet_data_pptr, option_len);
Christopher Haster 1:43f5c94c6771 385 (*packet_data_pptr) += option_len;
Christopher Haster 1:43f5c94c6771 386
Christopher Haster 1:43f5c94c6771 387 break;
Christopher Haster 1:43f5c94c6771 388
Christopher Haster 1:43f5c94c6771 389 case COAP_OPTION_LOCATION_PATH:
Christopher Haster 1:43f5c94c6771 390 if (dst_coap_msg_ptr->options_list_ptr->location_path_ptr) {
Christopher Haster 1:43f5c94c6771 391 return -1;
Christopher Haster 1:43f5c94c6771 392 }
Christopher Haster 1:43f5c94c6771 393 /* This is managed independently because User gives this option in one character table */
Christopher Haster 1:43f5c94c6771 394 ret_status = sn_coap_parser_options_parse_multiple_options(handle, packet_data_pptr, message_left,
Christopher Haster 1:43f5c94c6771 395 &dst_coap_msg_ptr->options_list_ptr->location_path_ptr, &dst_coap_msg_ptr->options_list_ptr->location_path_len,
Christopher Haster 1:43f5c94c6771 396 COAP_OPTION_LOCATION_PATH, option_len);
Christopher Haster 1:43f5c94c6771 397 if (ret_status >= 0) {
Christopher Haster 1:43f5c94c6771 398 i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
Christopher Haster 1:43f5c94c6771 399 } else {
Christopher Haster 1:43f5c94c6771 400 return -1;
Christopher Haster 1:43f5c94c6771 401 }
Christopher Haster 1:43f5c94c6771 402
Christopher Haster 1:43f5c94c6771 403 break;
Christopher Haster 1:43f5c94c6771 404
Christopher Haster 1:43f5c94c6771 405
Christopher Haster 1:43f5c94c6771 406 case COAP_OPTION_URI_PORT:
Christopher Haster 1:43f5c94c6771 407 if ((option_len > 2) || dst_coap_msg_ptr->options_list_ptr->uri_port_ptr) {
Christopher Haster 1:43f5c94c6771 408 return -1;
Christopher Haster 1:43f5c94c6771 409 }
Christopher Haster 1:43f5c94c6771 410 dst_coap_msg_ptr->options_list_ptr->uri_port_len = option_len;
Christopher Haster 1:43f5c94c6771 411 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 412
Christopher Haster 1:43f5c94c6771 413 if (option_len) {
Christopher Haster 1:43f5c94c6771 414 dst_coap_msg_ptr->options_list_ptr->uri_port_ptr = handle->sn_coap_protocol_malloc(option_len);
Christopher Haster 1:43f5c94c6771 415
Christopher Haster 1:43f5c94c6771 416 if (dst_coap_msg_ptr->options_list_ptr->uri_port_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 417 return -1;
Christopher Haster 1:43f5c94c6771 418 }
Christopher Haster 1:43f5c94c6771 419 memcpy(dst_coap_msg_ptr->options_list_ptr->uri_port_ptr, *packet_data_pptr, option_len);
Christopher Haster 1:43f5c94c6771 420 (*packet_data_pptr) += option_len;
Christopher Haster 1:43f5c94c6771 421 }
Christopher Haster 1:43f5c94c6771 422 break;
Christopher Haster 1:43f5c94c6771 423
Christopher Haster 1:43f5c94c6771 424 case COAP_OPTION_LOCATION_QUERY:
Christopher Haster 1:43f5c94c6771 425 ret_status = sn_coap_parser_options_parse_multiple_options(handle, packet_data_pptr, message_left,
Christopher Haster 1:43f5c94c6771 426 &dst_coap_msg_ptr->options_list_ptr->location_query_ptr, &dst_coap_msg_ptr->options_list_ptr->location_query_len,
Christopher Haster 1:43f5c94c6771 427 COAP_OPTION_LOCATION_QUERY, option_len);
Christopher Haster 1:43f5c94c6771 428 if (ret_status >= 0) {
Christopher Haster 1:43f5c94c6771 429 i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
Christopher Haster 1:43f5c94c6771 430 } else {
Christopher Haster 1:43f5c94c6771 431 return -1;
Christopher Haster 1:43f5c94c6771 432 }
Christopher Haster 1:43f5c94c6771 433
Christopher Haster 1:43f5c94c6771 434 break;
Christopher Haster 1:43f5c94c6771 435
Christopher Haster 1:43f5c94c6771 436 case COAP_OPTION_URI_PATH:
Christopher Haster 1:43f5c94c6771 437 ret_status = sn_coap_parser_options_parse_multiple_options(handle, packet_data_pptr, message_left,
Christopher Haster 1:43f5c94c6771 438 &dst_coap_msg_ptr->uri_path_ptr, &dst_coap_msg_ptr->uri_path_len,
Christopher Haster 1:43f5c94c6771 439 COAP_OPTION_URI_PATH, option_len);
Christopher Haster 1:43f5c94c6771 440 if (ret_status >= 0) {
Christopher Haster 1:43f5c94c6771 441 i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
Christopher Haster 1:43f5c94c6771 442 } else {
Christopher Haster 1:43f5c94c6771 443 return -1;
Christopher Haster 1:43f5c94c6771 444 }
Christopher Haster 1:43f5c94c6771 445
Christopher Haster 1:43f5c94c6771 446 break;
Christopher Haster 1:43f5c94c6771 447
Christopher Haster 1:43f5c94c6771 448 case COAP_OPTION_OBSERVE:
Christopher Haster 1:43f5c94c6771 449 if ((option_len > 2) || dst_coap_msg_ptr->options_list_ptr->observe_ptr) {
Christopher Haster 1:43f5c94c6771 450 return -1;
Christopher Haster 1:43f5c94c6771 451 }
Christopher Haster 1:43f5c94c6771 452
Christopher Haster 1:43f5c94c6771 453 dst_coap_msg_ptr->options_list_ptr->observe = 1;
Christopher Haster 1:43f5c94c6771 454 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 455
Christopher Haster 1:43f5c94c6771 456 if (option_len) {
Christopher Haster 1:43f5c94c6771 457
Christopher Haster 1:43f5c94c6771 458 dst_coap_msg_ptr->options_list_ptr->observe_len = option_len;
Christopher Haster 1:43f5c94c6771 459
Christopher Haster 1:43f5c94c6771 460 dst_coap_msg_ptr->options_list_ptr->observe_ptr = handle->sn_coap_protocol_malloc(option_len);
Christopher Haster 1:43f5c94c6771 461
Christopher Haster 1:43f5c94c6771 462 if (dst_coap_msg_ptr->options_list_ptr->observe_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 463 return -1;
Christopher Haster 1:43f5c94c6771 464 }
Christopher Haster 1:43f5c94c6771 465
Christopher Haster 1:43f5c94c6771 466 memcpy(dst_coap_msg_ptr->options_list_ptr->observe_ptr, *packet_data_pptr, option_len);
Christopher Haster 1:43f5c94c6771 467 (*packet_data_pptr) += option_len;
Christopher Haster 1:43f5c94c6771 468 }
Christopher Haster 1:43f5c94c6771 469
Christopher Haster 1:43f5c94c6771 470 break;
Christopher Haster 1:43f5c94c6771 471
Christopher Haster 1:43f5c94c6771 472 case COAP_OPTION_URI_QUERY:
Christopher Haster 1:43f5c94c6771 473 ret_status = sn_coap_parser_options_parse_multiple_options(handle, packet_data_pptr, message_left,
Christopher Haster 1:43f5c94c6771 474 &dst_coap_msg_ptr->options_list_ptr->uri_query_ptr, &dst_coap_msg_ptr->options_list_ptr->uri_query_len,
Christopher Haster 1:43f5c94c6771 475 COAP_OPTION_URI_QUERY, option_len);
Christopher Haster 1:43f5c94c6771 476 if (ret_status >= 0) {
Christopher Haster 1:43f5c94c6771 477 i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
Christopher Haster 1:43f5c94c6771 478 } else {
Christopher Haster 1:43f5c94c6771 479 return -1;
Christopher Haster 1:43f5c94c6771 480 }
Christopher Haster 1:43f5c94c6771 481
Christopher Haster 1:43f5c94c6771 482 break;
Christopher Haster 1:43f5c94c6771 483
Christopher Haster 1:43f5c94c6771 484 case COAP_OPTION_BLOCK2:
Christopher Haster 1:43f5c94c6771 485 if ((option_len > 4) || dst_coap_msg_ptr->options_list_ptr->block2_ptr) {
Christopher Haster 1:43f5c94c6771 486 return -1;
Christopher Haster 1:43f5c94c6771 487 }
Christopher Haster 1:43f5c94c6771 488 dst_coap_msg_ptr->options_list_ptr->block2_len = option_len;
Christopher Haster 1:43f5c94c6771 489 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 490
Christopher Haster 1:43f5c94c6771 491 dst_coap_msg_ptr->options_list_ptr->block2_ptr = handle->sn_coap_protocol_malloc(option_len);
Christopher Haster 1:43f5c94c6771 492
Christopher Haster 1:43f5c94c6771 493 if (dst_coap_msg_ptr->options_list_ptr->block2_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 494 return -1;
Christopher Haster 1:43f5c94c6771 495 }
Christopher Haster 1:43f5c94c6771 496
Christopher Haster 1:43f5c94c6771 497 memcpy(dst_coap_msg_ptr->options_list_ptr->block2_ptr, *packet_data_pptr, option_len);
Christopher Haster 1:43f5c94c6771 498 (*packet_data_pptr) += option_len;
Christopher Haster 1:43f5c94c6771 499
Christopher Haster 1:43f5c94c6771 500 break;
Christopher Haster 1:43f5c94c6771 501
Christopher Haster 1:43f5c94c6771 502 case COAP_OPTION_BLOCK1:
Christopher Haster 1:43f5c94c6771 503 if ((option_len > 4) || dst_coap_msg_ptr->options_list_ptr->block1_ptr) {
Christopher Haster 1:43f5c94c6771 504 return -1;
Christopher Haster 1:43f5c94c6771 505 }
Christopher Haster 1:43f5c94c6771 506 dst_coap_msg_ptr->options_list_ptr->block1_len = option_len;
Christopher Haster 1:43f5c94c6771 507 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 508
Christopher Haster 1:43f5c94c6771 509 dst_coap_msg_ptr->options_list_ptr->block1_ptr = handle->sn_coap_protocol_malloc(option_len);
Christopher Haster 1:43f5c94c6771 510
Christopher Haster 1:43f5c94c6771 511 if (dst_coap_msg_ptr->options_list_ptr->block1_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 512 return -1;
Christopher Haster 1:43f5c94c6771 513 }
Christopher Haster 1:43f5c94c6771 514
Christopher Haster 1:43f5c94c6771 515 memcpy(dst_coap_msg_ptr->options_list_ptr->block1_ptr, *packet_data_pptr, option_len);
Christopher Haster 1:43f5c94c6771 516 (*packet_data_pptr) += option_len;
Christopher Haster 1:43f5c94c6771 517
Christopher Haster 1:43f5c94c6771 518 break;
Christopher Haster 1:43f5c94c6771 519
Christopher Haster 1:43f5c94c6771 520 case COAP_OPTION_ACCEPT:
Christopher Haster 1:43f5c94c6771 521 ret_status = sn_coap_parser_options_parse_multiple_options(handle, packet_data_pptr, message_left,
Christopher Haster 1:43f5c94c6771 522 &dst_coap_msg_ptr->options_list_ptr->accept_ptr, (uint16_t *)&dst_coap_msg_ptr->options_list_ptr->accept_len,
Christopher Haster 1:43f5c94c6771 523 COAP_OPTION_ACCEPT, option_len);
Christopher Haster 1:43f5c94c6771 524 if (ret_status >= 0) {
Christopher Haster 1:43f5c94c6771 525 i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */
Christopher Haster 1:43f5c94c6771 526 } else {
Christopher Haster 1:43f5c94c6771 527 return -1;
Christopher Haster 1:43f5c94c6771 528 }
Christopher Haster 1:43f5c94c6771 529
Christopher Haster 1:43f5c94c6771 530 break;
Christopher Haster 1:43f5c94c6771 531
Christopher Haster 1:43f5c94c6771 532 default:
Christopher Haster 1:43f5c94c6771 533 return -1;
Christopher Haster 1:43f5c94c6771 534 }
Christopher Haster 1:43f5c94c6771 535
Christopher Haster 1:43f5c94c6771 536 /* Check for overflow */
Christopher Haster 1:43f5c94c6771 537 if ((*packet_data_pptr - packet_data_start_ptr) > packet_len) {
Christopher Haster 1:43f5c94c6771 538 return -1;
Christopher Haster 1:43f5c94c6771 539 }
Christopher Haster 1:43f5c94c6771 540
Christopher Haster 1:43f5c94c6771 541 message_left = packet_len - (*packet_data_pptr - packet_data_start_ptr);
Christopher Haster 1:43f5c94c6771 542
Christopher Haster 1:43f5c94c6771 543
Christopher Haster 1:43f5c94c6771 544 }
Christopher Haster 1:43f5c94c6771 545
Christopher Haster 1:43f5c94c6771 546 return 0;
Christopher Haster 1:43f5c94c6771 547 }
Christopher Haster 1:43f5c94c6771 548
Christopher Haster 1:43f5c94c6771 549
Christopher Haster 1:43f5c94c6771 550 /**
Christopher Haster 1:43f5c94c6771 551 * \fn static int8_t sn_coap_parser_options_parse_multiple_options(uint8_t **packet_data_pptr, uint8_t options_count_left, uint8_t *previous_option_number_ptr, uint8_t **dst_pptr,
Christopher Haster 1:43f5c94c6771 552 * uint16_t *dst_len_ptr, sn_coap_option_numbers_e option, uint16_t option_number_len)
Christopher Haster 1:43f5c94c6771 553 *
Christopher Haster 1:43f5c94c6771 554 * \brief Parses CoAP message's Uri-query options
Christopher Haster 1:43f5c94c6771 555 *
Christopher Haster 1:43f5c94c6771 556 * \param **packet_data_pptr is source for Packet data to be parsed to CoAP message
Christopher Haster 1:43f5c94c6771 557 *
Christopher Haster 1:43f5c94c6771 558 * \param *dst_coap_msg_ptr is destination for parsed CoAP message
Christopher Haster 1:43f5c94c6771 559 *
Christopher Haster 1:43f5c94c6771 560 * \param options_count_left tells how many options are unhandled in Packet data
Christopher Haster 1:43f5c94c6771 561 *
Christopher Haster 1:43f5c94c6771 562 * \param *previous_option_number_ptr is pointer to used and returned previous Option number
Christopher Haster 1:43f5c94c6771 563 *
Christopher Haster 1:43f5c94c6771 564 * \return Return value is count of Uri-query optios parsed. In failure case -1 is returned.
Christopher Haster 1:43f5c94c6771 565 */
Christopher Haster 1:43f5c94c6771 566 static int8_t sn_coap_parser_options_parse_multiple_options(struct coap_s *handle, uint8_t **packet_data_pptr, uint16_t packet_left_len, uint8_t **dst_pptr, uint16_t *dst_len_ptr, sn_coap_option_numbers_e option, uint16_t option_number_len)
Christopher Haster 1:43f5c94c6771 567 {
Christopher Haster 1:43f5c94c6771 568 int16_t uri_query_needed_heap = sn_coap_parser_options_count_needed_memory_multiple_option(*packet_data_pptr, packet_left_len, option, option_number_len);
Christopher Haster 1:43f5c94c6771 569 uint8_t *temp_parsed_uri_query_ptr = NULL;
Christopher Haster 1:43f5c94c6771 570 uint8_t returned_option_counter = 0;
Christopher Haster 1:43f5c94c6771 571
Christopher Haster 1:43f5c94c6771 572 if (uri_query_needed_heap == -1) {
Christopher Haster 1:43f5c94c6771 573 return -1;
Christopher Haster 1:43f5c94c6771 574 }
Christopher Haster 1:43f5c94c6771 575
Christopher Haster 1:43f5c94c6771 576 if (uri_query_needed_heap) {
Christopher Haster 1:43f5c94c6771 577 *dst_pptr = (uint8_t *) handle->sn_coap_protocol_malloc(uri_query_needed_heap);
Christopher Haster 1:43f5c94c6771 578
Christopher Haster 1:43f5c94c6771 579 if (*dst_pptr == NULL) {
Christopher Haster 1:43f5c94c6771 580 return -1;
Christopher Haster 1:43f5c94c6771 581 }
Christopher Haster 1:43f5c94c6771 582 }
Christopher Haster 1:43f5c94c6771 583
Christopher Haster 1:43f5c94c6771 584 *dst_len_ptr = uri_query_needed_heap;
Christopher Haster 1:43f5c94c6771 585
Christopher Haster 1:43f5c94c6771 586 temp_parsed_uri_query_ptr = *dst_pptr;
Christopher Haster 1:43f5c94c6771 587
Christopher Haster 1:43f5c94c6771 588 /* Loop all Uri-Query options */
Christopher Haster 1:43f5c94c6771 589 while ((temp_parsed_uri_query_ptr - *dst_pptr) < uri_query_needed_heap) {
Christopher Haster 1:43f5c94c6771 590 /* Check if this is first Uri-Query option */
Christopher Haster 1:43f5c94c6771 591 if (returned_option_counter > 0) {
Christopher Haster 1:43f5c94c6771 592 /* Uri-Query is modified to following format: temp1'\0'temp2'\0'temp3 i.e. */
Christopher Haster 1:43f5c94c6771 593 /* Uri-Path is modified to following format: temp1\temp2\temp3 i.e. */
Christopher Haster 1:43f5c94c6771 594 if (option == COAP_OPTION_URI_QUERY || option == COAP_OPTION_LOCATION_QUERY || option == COAP_OPTION_ETAG || option == COAP_OPTION_ACCEPT) {
Christopher Haster 1:43f5c94c6771 595 memset(temp_parsed_uri_query_ptr, '&', 1);
Christopher Haster 1:43f5c94c6771 596 } else if (option == COAP_OPTION_URI_PATH || option == COAP_OPTION_LOCATION_PATH) {
Christopher Haster 1:43f5c94c6771 597 memset(temp_parsed_uri_query_ptr, '/', 1);
Christopher Haster 1:43f5c94c6771 598 }
Christopher Haster 1:43f5c94c6771 599
Christopher Haster 1:43f5c94c6771 600 temp_parsed_uri_query_ptr++;
Christopher Haster 1:43f5c94c6771 601 }
Christopher Haster 1:43f5c94c6771 602
Christopher Haster 1:43f5c94c6771 603 returned_option_counter++;
Christopher Haster 1:43f5c94c6771 604
Christopher Haster 1:43f5c94c6771 605 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 606
Christopher Haster 1:43f5c94c6771 607 if (((temp_parsed_uri_query_ptr - *dst_pptr) + option_number_len) > uri_query_needed_heap) {
Christopher Haster 1:43f5c94c6771 608 return -1;
Christopher Haster 1:43f5c94c6771 609 }
Christopher Haster 1:43f5c94c6771 610
Christopher Haster 1:43f5c94c6771 611 memcpy(temp_parsed_uri_query_ptr, *packet_data_pptr, option_number_len);
Christopher Haster 1:43f5c94c6771 612
Christopher Haster 1:43f5c94c6771 613 (*packet_data_pptr) += option_number_len;
Christopher Haster 1:43f5c94c6771 614 temp_parsed_uri_query_ptr += option_number_len;
Christopher Haster 1:43f5c94c6771 615
Christopher Haster 1:43f5c94c6771 616 if ((temp_parsed_uri_query_ptr - *dst_pptr) >= uri_query_needed_heap || ((**packet_data_pptr >> COAP_OPTIONS_OPTION_NUMBER_SHIFT) != 0)) {
Christopher Haster 1:43f5c94c6771 617 return returned_option_counter;
Christopher Haster 1:43f5c94c6771 618 }
Christopher Haster 1:43f5c94c6771 619
Christopher Haster 1:43f5c94c6771 620 option_number_len = (**packet_data_pptr & 0x0F);
Christopher Haster 1:43f5c94c6771 621 if (option_number_len == 13) {
Christopher Haster 1:43f5c94c6771 622 option_number_len = *(*packet_data_pptr + 1) + 13;
Christopher Haster 1:43f5c94c6771 623 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 624 } else if (option_number_len == 14) {
Christopher Haster 1:43f5c94c6771 625 option_number_len = *(*packet_data_pptr + 2);
Christopher Haster 1:43f5c94c6771 626 option_number_len += (*(*packet_data_pptr + 1) << 8) + 269;
Christopher Haster 1:43f5c94c6771 627 (*packet_data_pptr) += 2;
Christopher Haster 1:43f5c94c6771 628 }
Christopher Haster 1:43f5c94c6771 629 }
Christopher Haster 1:43f5c94c6771 630
Christopher Haster 1:43f5c94c6771 631 return returned_option_counter;
Christopher Haster 1:43f5c94c6771 632 }
Christopher Haster 1:43f5c94c6771 633
Christopher Haster 1:43f5c94c6771 634
Christopher Haster 1:43f5c94c6771 635
Christopher Haster 1:43f5c94c6771 636
Christopher Haster 1:43f5c94c6771 637 /**
Christopher Haster 1:43f5c94c6771 638 * \fn static uint16_t sn_coap_parser_options_count_needed_memory_multiple_option(uint8_t *packet_data_ptr, uint8_t options_count_left, uint8_t previous_option_number, sn_coap_option_numbers_e option, uint16_t option_number_len)
Christopher Haster 1:43f5c94c6771 639 *
Christopher Haster 1:43f5c94c6771 640 * \brief Counts needed memory for uri query option
Christopher Haster 1:43f5c94c6771 641 *
Christopher Haster 1:43f5c94c6771 642 * \param *packet_data_ptr is start of source for Packet data to be parsed to CoAP message
Christopher Haster 1:43f5c94c6771 643 *
Christopher Haster 1:43f5c94c6771 644 * \param options_count_left tells how many options are unhandled in Packet data
Christopher Haster 1:43f5c94c6771 645 *
Christopher Haster 1:43f5c94c6771 646 * \param previous_option_number is previous Option number
Christopher Haster 1:43f5c94c6771 647 *
Christopher Haster 1:43f5c94c6771 648 * \param sn_coap_option_numbers_e option option number to be calculated
Christopher Haster 1:43f5c94c6771 649 *
Christopher Haster 1:43f5c94c6771 650 * \param uint16_t option_number_len length of the first option part
Christopher Haster 1:43f5c94c6771 651 */
Christopher Haster 1:43f5c94c6771 652 static int16_t sn_coap_parser_options_count_needed_memory_multiple_option(uint8_t *packet_data_ptr, uint16_t packet_left_len, sn_coap_option_numbers_e option, uint16_t option_number_len)
Christopher Haster 1:43f5c94c6771 653 {
Christopher Haster 1:43f5c94c6771 654 uint16_t ret_value = 0;
Christopher Haster 1:43f5c94c6771 655 uint16_t i = 1;
Christopher Haster 1:43f5c94c6771 656
Christopher Haster 1:43f5c94c6771 657 /* Loop all Uri-Query options */
Christopher Haster 1:43f5c94c6771 658 while (i < packet_left_len) {
Christopher Haster 1:43f5c94c6771 659 if (option == COAP_OPTION_LOCATION_PATH && option_number_len > 255) {
Christopher Haster 1:43f5c94c6771 660 return -1;
Christopher Haster 1:43f5c94c6771 661 }
Christopher Haster 1:43f5c94c6771 662 if (option == COAP_OPTION_URI_PATH && option_number_len > 255) {
Christopher Haster 1:43f5c94c6771 663 return -1;
Christopher Haster 1:43f5c94c6771 664 }
Christopher Haster 1:43f5c94c6771 665 if (option == COAP_OPTION_URI_QUERY && option_number_len > 255) {
Christopher Haster 1:43f5c94c6771 666 return -1;
Christopher Haster 1:43f5c94c6771 667 }
Christopher Haster 1:43f5c94c6771 668 if (option == COAP_OPTION_LOCATION_QUERY && option_number_len > 255) {
Christopher Haster 1:43f5c94c6771 669 return -1;
Christopher Haster 1:43f5c94c6771 670 }
Christopher Haster 1:43f5c94c6771 671 if (option == COAP_OPTION_ACCEPT && option_number_len > 2) {
Christopher Haster 1:43f5c94c6771 672 return -1;
Christopher Haster 1:43f5c94c6771 673 }
Christopher Haster 1:43f5c94c6771 674 if (option == COAP_OPTION_ETAG && option_number_len > 8) {
Christopher Haster 1:43f5c94c6771 675 return -1;
Christopher Haster 1:43f5c94c6771 676 }
Christopher Haster 1:43f5c94c6771 677
Christopher Haster 1:43f5c94c6771 678 i += option_number_len;
Christopher Haster 1:43f5c94c6771 679 ret_value += option_number_len + 1; /* + 1 is for separator */
Christopher Haster 1:43f5c94c6771 680 if(ret_value >= packet_left_len)
Christopher Haster 1:43f5c94c6771 681 break;
Christopher Haster 1:43f5c94c6771 682
Christopher Haster 1:43f5c94c6771 683 if(ret_value >= packet_left_len)
Christopher Haster 1:43f5c94c6771 684 break;
Christopher Haster 1:43f5c94c6771 685
Christopher Haster 1:43f5c94c6771 686 if ((*(packet_data_ptr + i) >> COAP_OPTIONS_OPTION_NUMBER_SHIFT) != 0) {
Christopher Haster 1:43f5c94c6771 687 return (ret_value - 1); /* -1 because last Part path does not include separator */
Christopher Haster 1:43f5c94c6771 688 }
Christopher Haster 1:43f5c94c6771 689
Christopher Haster 1:43f5c94c6771 690 option_number_len = (*(packet_data_ptr + i) & 0x0F);
Christopher Haster 1:43f5c94c6771 691
Christopher Haster 1:43f5c94c6771 692 if (option_number_len == 13) {
Christopher Haster 1:43f5c94c6771 693 i++;
Christopher Haster 1:43f5c94c6771 694 option_number_len = *(packet_data_ptr + i) + 13;
Christopher Haster 1:43f5c94c6771 695 } else if (option_number_len == 14) {
Christopher Haster 1:43f5c94c6771 696 option_number_len = *(packet_data_ptr + i + 2);
Christopher Haster 1:43f5c94c6771 697 option_number_len += (*(packet_data_ptr + i + 1) << 8) + 269;
Christopher Haster 1:43f5c94c6771 698 i += 2;
Christopher Haster 1:43f5c94c6771 699 } else if (option_number_len == 15) {
Christopher Haster 1:43f5c94c6771 700 return -1;
Christopher Haster 1:43f5c94c6771 701 }
Christopher Haster 1:43f5c94c6771 702 i++;
Christopher Haster 1:43f5c94c6771 703
Christopher Haster 1:43f5c94c6771 704 }
Christopher Haster 1:43f5c94c6771 705
Christopher Haster 1:43f5c94c6771 706 if (ret_value != 0) {
Christopher Haster 1:43f5c94c6771 707 return (ret_value - 1); /* -1 because last Part path does not include separator */
Christopher Haster 1:43f5c94c6771 708 } else {
Christopher Haster 1:43f5c94c6771 709 return 0;
Christopher Haster 1:43f5c94c6771 710 }
Christopher Haster 1:43f5c94c6771 711 }
Christopher Haster 1:43f5c94c6771 712
Christopher Haster 1:43f5c94c6771 713 /**
Christopher Haster 1:43f5c94c6771 714 * \fn static void sn_coap_parser_payload_parse(uint16_t packet_data_len, uint8_t *packet_data_ptr, uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr)
Christopher Haster 1:43f5c94c6771 715 *
Christopher Haster 1:43f5c94c6771 716 * \brief Parses CoAP message's Payload part from given Packet data
Christopher Haster 1:43f5c94c6771 717 *
Christopher Haster 1:43f5c94c6771 718 * \param packet_data_len is length of given Packet data to be parsed to CoAP message
Christopher Haster 1:43f5c94c6771 719 *
Christopher Haster 1:43f5c94c6771 720 * \param *packet_data_ptr is start of source for Packet data to be parsed to CoAP message
Christopher Haster 1:43f5c94c6771 721 *
Christopher Haster 1:43f5c94c6771 722 * \param **packet_data_pptr is source for Packet data to be parsed to CoAP message
Christopher Haster 1:43f5c94c6771 723 *
Christopher Haster 1:43f5c94c6771 724 * \param *dst_coap_msg_ptr is destination for parsed CoAP message
Christopher Haster 1:43f5c94c6771 725 *****************************************************************************/
Christopher Haster 1:43f5c94c6771 726 static int8_t sn_coap_parser_payload_parse(uint16_t packet_data_len, uint8_t *packet_data_start_ptr, uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr)
Christopher Haster 1:43f5c94c6771 727 {
Christopher Haster 1:43f5c94c6771 728 /* If there is payload */
Christopher Haster 1:43f5c94c6771 729 if ((*packet_data_pptr - packet_data_start_ptr) < packet_data_len) {
Christopher Haster 1:43f5c94c6771 730 if (**packet_data_pptr == 0xff) {
Christopher Haster 1:43f5c94c6771 731 (*packet_data_pptr)++;
Christopher Haster 1:43f5c94c6771 732 /* Parse Payload length */
Christopher Haster 1:43f5c94c6771 733 dst_coap_msg_ptr->payload_len = packet_data_len - (*packet_data_pptr - packet_data_start_ptr);
Christopher Haster 1:43f5c94c6771 734
Christopher Haster 1:43f5c94c6771 735 /* The presence of a marker followed by a zero-length payload MUST be processed as a message format error */
Christopher Haster 1:43f5c94c6771 736 if (dst_coap_msg_ptr->payload_len == 0) {
Christopher Haster 1:43f5c94c6771 737 return -1;
Christopher Haster 1:43f5c94c6771 738 }
Christopher Haster 1:43f5c94c6771 739
Christopher Haster 1:43f5c94c6771 740 /* Parse Payload by setting CoAP message's payload_ptr to point Payload in Packet data */
Christopher Haster 1:43f5c94c6771 741 dst_coap_msg_ptr->payload_ptr = *packet_data_pptr;
Christopher Haster 1:43f5c94c6771 742 }
Christopher Haster 1:43f5c94c6771 743 /* No payload marker.. */
Christopher Haster 1:43f5c94c6771 744 else {
Christopher Haster 1:43f5c94c6771 745 return -1;
Christopher Haster 1:43f5c94c6771 746 }
Christopher Haster 1:43f5c94c6771 747 }
Christopher Haster 1:43f5c94c6771 748 return 0;
Christopher Haster 1:43f5c94c6771 749 }
Christopher Haster 1:43f5c94c6771 750