version of nsdl to work with lwm2m, updated RD parameters and location option handling
Dependents: ArchPro_LWM2M_LED_Client Weather_Station_LWM2M mbedEndpointNetwork
Fork of nanoservice_client_1_12 by
sn_coap_parser.c@10:b5ecd6660d71, 2015-04-13 (annotated)
- Committer:
- michaeljkoster
- Date:
- Mon Apr 13 22:13:40 2015 +0000
- Revision:
- 10:b5ecd6660d71
- Parent:
- 0:aafd54b05111
Add error return from sn_nsdl_register_endpoint() in sn_nsdl.c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zdshelby | 0:aafd54b05111 | 1 | /** |
zdshelby | 0:aafd54b05111 | 2 | *\file sn_coap_parser.c |
zdshelby | 0:aafd54b05111 | 3 | * |
zdshelby | 0:aafd54b05111 | 4 | * \brief CoAP Header parser |
zdshelby | 0:aafd54b05111 | 5 | * |
zdshelby | 0:aafd54b05111 | 6 | * Functionality: Parses CoAP Header |
zdshelby | 0:aafd54b05111 | 7 | * |
zdshelby | 0:aafd54b05111 | 8 | * Created on: Jun 30, 2011 |
zdshelby | 0:aafd54b05111 | 9 | * Author: tero |
zdshelby | 0:aafd54b05111 | 10 | * |
zdshelby | 0:aafd54b05111 | 11 | * \note Supports draft-ietf-core-coap-12 |
zdshelby | 0:aafd54b05111 | 12 | */ |
zdshelby | 0:aafd54b05111 | 13 | |
zdshelby | 0:aafd54b05111 | 14 | /* * * * * * * * * * * * * * */ |
zdshelby | 0:aafd54b05111 | 15 | /* * * * INCLUDE FILES * * * */ |
zdshelby | 0:aafd54b05111 | 16 | /* * * * * * * * * * * * * * */ |
zdshelby | 0:aafd54b05111 | 17 | |
zdshelby | 0:aafd54b05111 | 18 | #include <stdio.h> |
zdshelby | 0:aafd54b05111 | 19 | #include <string.h> /* For memset() and memcpy() */ |
zdshelby | 0:aafd54b05111 | 20 | |
zdshelby | 0:aafd54b05111 | 21 | #include "nsdl_types.h" |
zdshelby | 0:aafd54b05111 | 22 | #include "sn_nsdl.h" |
zdshelby | 0:aafd54b05111 | 23 | #include "sn_coap_header.h" |
zdshelby | 0:aafd54b05111 | 24 | #include "sn_coap_protocol.h" |
zdshelby | 0:aafd54b05111 | 25 | #include "sn_coap_header_internal.h" |
zdshelby | 0:aafd54b05111 | 26 | #include "sn_coap_protocol_internal.h" |
zdshelby | 0:aafd54b05111 | 27 | |
zdshelby | 0:aafd54b05111 | 28 | /* * * * * * * * * * * * * * * * * * * * */ |
zdshelby | 0:aafd54b05111 | 29 | /* * * * LOCAL FUNCTION PROTOTYPES * * * */ |
zdshelby | 0:aafd54b05111 | 30 | /* * * * * * * * * * * * * * * * * * * * */ |
zdshelby | 0:aafd54b05111 | 31 | |
zdshelby | 0:aafd54b05111 | 32 | 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); |
zdshelby | 0:aafd54b05111 | 33 | static int8_t sn_coap_parser_options_parse(uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr, uint8_t *packet_data_start_ptr, uint16_t packet_len); |
zdshelby | 0:aafd54b05111 | 34 | static int8_t sn_coap_parser_options_parse_multiple_options(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); |
zdshelby | 0:aafd54b05111 | 35 | 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); |
zdshelby | 0:aafd54b05111 | 36 | 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); |
zdshelby | 0:aafd54b05111 | 37 | |
zdshelby | 0:aafd54b05111 | 38 | /* * * * * * * * * * * * * * * * * */ |
zdshelby | 0:aafd54b05111 | 39 | /* * * * GLOBAL DECLARATIONS * * * */ |
zdshelby | 0:aafd54b05111 | 40 | /* * * * * * * * * * * * * * * * * */ |
zdshelby | 0:aafd54b05111 | 41 | |
zdshelby | 0:aafd54b05111 | 42 | /** |
zdshelby | 0:aafd54b05111 | 43 | * \fn sn_coap_hdr_s *sn_coap_parser(uint16_t packet_data_len, uint8_t *packet_data_ptr, coap_version_e *coap_version_ptr) |
zdshelby | 0:aafd54b05111 | 44 | * |
zdshelby | 0:aafd54b05111 | 45 | * \brief Parses CoAP message from given Packet data |
zdshelby | 0:aafd54b05111 | 46 | * |
zdshelby | 0:aafd54b05111 | 47 | * \param packet_data_len is length of given Packet data to be parsed to CoAP message |
zdshelby | 0:aafd54b05111 | 48 | * |
zdshelby | 0:aafd54b05111 | 49 | * \param *packet_data_ptr is source for Packet data to be parsed to CoAP message |
zdshelby | 0:aafd54b05111 | 50 | * |
zdshelby | 0:aafd54b05111 | 51 | * \param *coap_version_ptr is destination for parsed CoAP specification version |
zdshelby | 0:aafd54b05111 | 52 | * |
zdshelby | 0:aafd54b05111 | 53 | * \return Return value is pointer to parsed CoAP message.\n |
zdshelby | 0:aafd54b05111 | 54 | * In following failure cases NULL is returned:\n |
zdshelby | 0:aafd54b05111 | 55 | * -Failure in given pointer (= NULL)\n |
zdshelby | 0:aafd54b05111 | 56 | * -Failure in memory allocation (malloc() returns NULL) |
zdshelby | 0:aafd54b05111 | 57 | */ |
zdshelby | 0:aafd54b05111 | 58 | sn_coap_hdr_s *sn_coap_parser(uint16_t packet_data_len, uint8_t *packet_data_ptr, coap_version_e *coap_version_ptr) |
zdshelby | 0:aafd54b05111 | 59 | { |
zdshelby | 0:aafd54b05111 | 60 | uint8_t *data_temp_ptr = packet_data_ptr; |
zdshelby | 0:aafd54b05111 | 61 | sn_coap_hdr_s *parsed_and_returned_coap_msg_ptr = NULL; |
zdshelby | 0:aafd54b05111 | 62 | |
zdshelby | 0:aafd54b05111 | 63 | /* * * * Check given pointer * * * */ |
zdshelby | 0:aafd54b05111 | 64 | if (packet_data_ptr == NULL || packet_data_len < 4) |
zdshelby | 0:aafd54b05111 | 65 | return NULL; |
zdshelby | 0:aafd54b05111 | 66 | |
zdshelby | 0:aafd54b05111 | 67 | /* * * * Allocate memory for parsed and returned CoAP message and initialize allocated memory with with zero values * * * */ |
zdshelby | 0:aafd54b05111 | 68 | parsed_and_returned_coap_msg_ptr = sn_coap_malloc(sizeof(sn_coap_hdr_s)); |
zdshelby | 0:aafd54b05111 | 69 | |
zdshelby | 0:aafd54b05111 | 70 | if (parsed_and_returned_coap_msg_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 71 | return NULL; |
zdshelby | 0:aafd54b05111 | 72 | |
zdshelby | 0:aafd54b05111 | 73 | memset(parsed_and_returned_coap_msg_ptr, 0x00, sizeof(sn_coap_hdr_s)); |
zdshelby | 0:aafd54b05111 | 74 | |
zdshelby | 0:aafd54b05111 | 75 | |
zdshelby | 0:aafd54b05111 | 76 | /* * * * Header parsing, move pointer over the header... * * * */ |
zdshelby | 0:aafd54b05111 | 77 | sn_coap_parser_header_parse(&data_temp_ptr, parsed_and_returned_coap_msg_ptr, coap_version_ptr); |
zdshelby | 0:aafd54b05111 | 78 | |
zdshelby | 0:aafd54b05111 | 79 | |
zdshelby | 0:aafd54b05111 | 80 | /* * * * Options parsing, move pointer over the options... * * * */ |
zdshelby | 0:aafd54b05111 | 81 | if (sn_coap_parser_options_parse(&data_temp_ptr, parsed_and_returned_coap_msg_ptr, packet_data_ptr, packet_data_len) != 0) |
zdshelby | 0:aafd54b05111 | 82 | { |
zdshelby | 0:aafd54b05111 | 83 | /* Release memory of CoAP message */ |
zdshelby | 0:aafd54b05111 | 84 | sn_coap_parser_release_allocated_coap_msg_mem(parsed_and_returned_coap_msg_ptr); |
zdshelby | 0:aafd54b05111 | 85 | return NULL; |
zdshelby | 0:aafd54b05111 | 86 | } |
zdshelby | 0:aafd54b05111 | 87 | |
zdshelby | 0:aafd54b05111 | 88 | /* * * * Payload parsing * * * */ |
zdshelby | 0:aafd54b05111 | 89 | if(sn_coap_parser_payload_parse(packet_data_len, packet_data_ptr, &data_temp_ptr, parsed_and_returned_coap_msg_ptr) == -1) |
zdshelby | 0:aafd54b05111 | 90 | { |
zdshelby | 0:aafd54b05111 | 91 | /* Release memory of CoAP message */ |
zdshelby | 0:aafd54b05111 | 92 | sn_coap_parser_release_allocated_coap_msg_mem(parsed_and_returned_coap_msg_ptr); |
zdshelby | 0:aafd54b05111 | 93 | return NULL; |
zdshelby | 0:aafd54b05111 | 94 | } |
zdshelby | 0:aafd54b05111 | 95 | /* * * * Return parsed CoAP message * * * * */ |
zdshelby | 0:aafd54b05111 | 96 | return parsed_and_returned_coap_msg_ptr; |
zdshelby | 0:aafd54b05111 | 97 | } |
zdshelby | 0:aafd54b05111 | 98 | |
zdshelby | 0:aafd54b05111 | 99 | /** |
zdshelby | 0:aafd54b05111 | 100 | * \fn void sn_coap_parser_release_allocated_coap_msg_mem(sn_coap_hdr_s *freed_coap_msg_ptr) |
zdshelby | 0:aafd54b05111 | 101 | * |
zdshelby | 0:aafd54b05111 | 102 | * \brief Releases memory of given CoAP message |
zdshelby | 0:aafd54b05111 | 103 | * |
zdshelby | 0:aafd54b05111 | 104 | * Note!!! Does not release Payload part |
zdshelby | 0:aafd54b05111 | 105 | * |
zdshelby | 0:aafd54b05111 | 106 | * \param *freed_coap_msg_ptr is pointer to released CoAP message |
zdshelby | 0:aafd54b05111 | 107 | */ |
zdshelby | 0:aafd54b05111 | 108 | |
zdshelby | 0:aafd54b05111 | 109 | void sn_coap_parser_release_allocated_coap_msg_mem(sn_coap_hdr_s *freed_coap_msg_ptr) |
zdshelby | 0:aafd54b05111 | 110 | { |
zdshelby | 0:aafd54b05111 | 111 | if (freed_coap_msg_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 112 | { |
zdshelby | 0:aafd54b05111 | 113 | if (freed_coap_msg_ptr->uri_path_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 114 | { |
zdshelby | 0:aafd54b05111 | 115 | sn_coap_free(freed_coap_msg_ptr->uri_path_ptr); |
zdshelby | 0:aafd54b05111 | 116 | } |
zdshelby | 0:aafd54b05111 | 117 | |
zdshelby | 0:aafd54b05111 | 118 | if (freed_coap_msg_ptr->token_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 119 | { |
zdshelby | 0:aafd54b05111 | 120 | sn_coap_free(freed_coap_msg_ptr->token_ptr); |
zdshelby | 0:aafd54b05111 | 121 | } |
zdshelby | 0:aafd54b05111 | 122 | |
zdshelby | 0:aafd54b05111 | 123 | if (freed_coap_msg_ptr->content_type_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 124 | { |
zdshelby | 0:aafd54b05111 | 125 | sn_coap_free(freed_coap_msg_ptr->content_type_ptr); |
zdshelby | 0:aafd54b05111 | 126 | } |
zdshelby | 0:aafd54b05111 | 127 | |
zdshelby | 0:aafd54b05111 | 128 | if (freed_coap_msg_ptr->options_list_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 129 | { |
zdshelby | 0:aafd54b05111 | 130 | if (freed_coap_msg_ptr->options_list_ptr->max_age_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 131 | { |
zdshelby | 0:aafd54b05111 | 132 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->max_age_ptr); |
zdshelby | 0:aafd54b05111 | 133 | } |
zdshelby | 0:aafd54b05111 | 134 | |
zdshelby | 0:aafd54b05111 | 135 | if (freed_coap_msg_ptr->options_list_ptr->proxy_uri_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 136 | { |
zdshelby | 0:aafd54b05111 | 137 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->proxy_uri_ptr); |
zdshelby | 0:aafd54b05111 | 138 | } |
zdshelby | 0:aafd54b05111 | 139 | |
zdshelby | 0:aafd54b05111 | 140 | if (freed_coap_msg_ptr->options_list_ptr->etag_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 141 | { |
zdshelby | 0:aafd54b05111 | 142 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->etag_ptr); |
zdshelby | 0:aafd54b05111 | 143 | } |
zdshelby | 0:aafd54b05111 | 144 | |
zdshelby | 0:aafd54b05111 | 145 | if (freed_coap_msg_ptr->options_list_ptr->uri_host_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 146 | { |
zdshelby | 0:aafd54b05111 | 147 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->uri_host_ptr); |
zdshelby | 0:aafd54b05111 | 148 | } |
zdshelby | 0:aafd54b05111 | 149 | |
zdshelby | 0:aafd54b05111 | 150 | if (freed_coap_msg_ptr->options_list_ptr->location_path_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 151 | { |
zdshelby | 0:aafd54b05111 | 152 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->location_path_ptr); |
zdshelby | 0:aafd54b05111 | 153 | } |
zdshelby | 0:aafd54b05111 | 154 | |
zdshelby | 0:aafd54b05111 | 155 | if (freed_coap_msg_ptr->options_list_ptr->uri_port_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 156 | { |
zdshelby | 0:aafd54b05111 | 157 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->uri_port_ptr); |
zdshelby | 0:aafd54b05111 | 158 | } |
zdshelby | 0:aafd54b05111 | 159 | |
zdshelby | 0:aafd54b05111 | 160 | if (freed_coap_msg_ptr->options_list_ptr->location_query_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 161 | { |
zdshelby | 0:aafd54b05111 | 162 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->location_query_ptr); |
zdshelby | 0:aafd54b05111 | 163 | } |
zdshelby | 0:aafd54b05111 | 164 | |
zdshelby | 0:aafd54b05111 | 165 | if (freed_coap_msg_ptr->options_list_ptr->observe_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 166 | { |
zdshelby | 0:aafd54b05111 | 167 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->observe_ptr); |
zdshelby | 0:aafd54b05111 | 168 | } |
zdshelby | 0:aafd54b05111 | 169 | |
zdshelby | 0:aafd54b05111 | 170 | if (freed_coap_msg_ptr->options_list_ptr->uri_query_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 171 | { |
zdshelby | 0:aafd54b05111 | 172 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->uri_query_ptr); |
zdshelby | 0:aafd54b05111 | 173 | } |
zdshelby | 0:aafd54b05111 | 174 | |
zdshelby | 0:aafd54b05111 | 175 | if (freed_coap_msg_ptr->options_list_ptr->block2_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 176 | { |
zdshelby | 0:aafd54b05111 | 177 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->block2_ptr); |
zdshelby | 0:aafd54b05111 | 178 | } |
zdshelby | 0:aafd54b05111 | 179 | |
zdshelby | 0:aafd54b05111 | 180 | if (freed_coap_msg_ptr->options_list_ptr->block1_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 181 | { |
zdshelby | 0:aafd54b05111 | 182 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->block1_ptr); |
zdshelby | 0:aafd54b05111 | 183 | } |
zdshelby | 0:aafd54b05111 | 184 | if (freed_coap_msg_ptr->options_list_ptr->accept_ptr != NULL) |
zdshelby | 0:aafd54b05111 | 185 | { |
zdshelby | 0:aafd54b05111 | 186 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr->accept_ptr); |
zdshelby | 0:aafd54b05111 | 187 | } |
zdshelby | 0:aafd54b05111 | 188 | |
zdshelby | 0:aafd54b05111 | 189 | sn_coap_free(freed_coap_msg_ptr->options_list_ptr); |
zdshelby | 0:aafd54b05111 | 190 | } |
zdshelby | 0:aafd54b05111 | 191 | |
zdshelby | 0:aafd54b05111 | 192 | sn_coap_free(freed_coap_msg_ptr); |
zdshelby | 0:aafd54b05111 | 193 | } |
zdshelby | 0:aafd54b05111 | 194 | } |
zdshelby | 0:aafd54b05111 | 195 | |
zdshelby | 0:aafd54b05111 | 196 | /** |
zdshelby | 0:aafd54b05111 | 197 | * \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) |
zdshelby | 0:aafd54b05111 | 198 | * |
zdshelby | 0:aafd54b05111 | 199 | * \brief Parses CoAP message's Header part from given Packet data |
zdshelby | 0:aafd54b05111 | 200 | * |
zdshelby | 0:aafd54b05111 | 201 | * \param **packet_data_ptr is source for Packet data to be parsed to CoAP message |
zdshelby | 0:aafd54b05111 | 202 | * |
zdshelby | 0:aafd54b05111 | 203 | * \param *dst_coap_msg_ptr is destination for parsed CoAP message |
zdshelby | 0:aafd54b05111 | 204 | * |
zdshelby | 0:aafd54b05111 | 205 | * \param *coap_version_ptr is destination for parsed CoAP specification version |
zdshelby | 0:aafd54b05111 | 206 | */ |
zdshelby | 0:aafd54b05111 | 207 | |
zdshelby | 0:aafd54b05111 | 208 | 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) |
zdshelby | 0:aafd54b05111 | 209 | { |
zdshelby | 0:aafd54b05111 | 210 | /* Parse CoAP Version and message type*/ |
zdshelby | 0:aafd54b05111 | 211 | *coap_version_ptr = (coap_version_e)(**packet_data_pptr & COAP_HEADER_VERSION_MASK); |
zdshelby | 0:aafd54b05111 | 212 | dst_coap_msg_ptr->msg_type = (sn_coap_msg_type_e)(**packet_data_pptr & COAP_HEADER_MSG_TYPE_MASK); |
zdshelby | 0:aafd54b05111 | 213 | (*packet_data_pptr) += 1; |
zdshelby | 0:aafd54b05111 | 214 | |
zdshelby | 0:aafd54b05111 | 215 | /* Parse Message code */ |
zdshelby | 0:aafd54b05111 | 216 | dst_coap_msg_ptr->msg_code = (sn_coap_msg_code_e)**packet_data_pptr; |
zdshelby | 0:aafd54b05111 | 217 | (*packet_data_pptr) += 1; |
zdshelby | 0:aafd54b05111 | 218 | |
zdshelby | 0:aafd54b05111 | 219 | /* Parse Message ID */ |
zdshelby | 0:aafd54b05111 | 220 | dst_coap_msg_ptr->msg_id = *(*packet_data_pptr+1); |
zdshelby | 0:aafd54b05111 | 221 | dst_coap_msg_ptr->msg_id += **packet_data_pptr << COAP_HEADER_MSG_ID_MSB_SHIFT; |
zdshelby | 0:aafd54b05111 | 222 | (*packet_data_pptr) += 2; |
zdshelby | 0:aafd54b05111 | 223 | |
zdshelby | 0:aafd54b05111 | 224 | } |
zdshelby | 0:aafd54b05111 | 225 | |
zdshelby | 0:aafd54b05111 | 226 | /** |
zdshelby | 0:aafd54b05111 | 227 | * \fn static uint8_t sn_coap_parser_options_parse(uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr) |
zdshelby | 0:aafd54b05111 | 228 | * |
zdshelby | 0:aafd54b05111 | 229 | * \brief Parses CoAP message's Options part from given Packet data |
zdshelby | 0:aafd54b05111 | 230 | * |
zdshelby | 0:aafd54b05111 | 231 | * \param **packet_data_pptr is source of Packet data to be parsed to CoAP message |
zdshelby | 0:aafd54b05111 | 232 | * \param *dst_coap_msg_ptr is destination for parsed CoAP message |
zdshelby | 0:aafd54b05111 | 233 | * |
zdshelby | 0:aafd54b05111 | 234 | * \return Return value is 0 in ok case and -1 in failure case |
zdshelby | 0:aafd54b05111 | 235 | */ |
zdshelby | 0:aafd54b05111 | 236 | |
zdshelby | 0:aafd54b05111 | 237 | static int8_t sn_coap_parser_options_parse(uint8_t **packet_data_pptr, sn_coap_hdr_s *dst_coap_msg_ptr, uint8_t *packet_data_start_ptr, uint16_t packet_len) |
zdshelby | 0:aafd54b05111 | 238 | { |
zdshelby | 0:aafd54b05111 | 239 | uint8_t previous_option_number = 0; |
zdshelby | 0:aafd54b05111 | 240 | uint8_t i = 0; |
zdshelby | 0:aafd54b05111 | 241 | int8_t ret_status = 0; |
zdshelby | 0:aafd54b05111 | 242 | uint16_t message_left = 0; |
zdshelby | 0:aafd54b05111 | 243 | |
zdshelby | 0:aafd54b05111 | 244 | |
zdshelby | 0:aafd54b05111 | 245 | /* Parse token, if exists */ |
zdshelby | 0:aafd54b05111 | 246 | dst_coap_msg_ptr->token_len = *packet_data_start_ptr & COAP_HEADER_TOKEN_LENGTH_MASK; |
zdshelby | 0:aafd54b05111 | 247 | |
zdshelby | 0:aafd54b05111 | 248 | if(dst_coap_msg_ptr->token_len) |
zdshelby | 0:aafd54b05111 | 249 | { |
zdshelby | 0:aafd54b05111 | 250 | if((dst_coap_msg_ptr->token_len > 8) || dst_coap_msg_ptr->token_ptr) |
zdshelby | 0:aafd54b05111 | 251 | return -1; |
zdshelby | 0:aafd54b05111 | 252 | |
zdshelby | 0:aafd54b05111 | 253 | dst_coap_msg_ptr->token_ptr = sn_coap_malloc(dst_coap_msg_ptr->token_len); |
zdshelby | 0:aafd54b05111 | 254 | |
zdshelby | 0:aafd54b05111 | 255 | if (dst_coap_msg_ptr->token_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 256 | { |
zdshelby | 0:aafd54b05111 | 257 | return -1; |
zdshelby | 0:aafd54b05111 | 258 | } |
zdshelby | 0:aafd54b05111 | 259 | |
zdshelby | 0:aafd54b05111 | 260 | memcpy(dst_coap_msg_ptr->token_ptr, *packet_data_pptr, dst_coap_msg_ptr->token_len); |
zdshelby | 0:aafd54b05111 | 261 | (*packet_data_pptr) += dst_coap_msg_ptr->token_len; |
zdshelby | 0:aafd54b05111 | 262 | } |
zdshelby | 0:aafd54b05111 | 263 | |
zdshelby | 0:aafd54b05111 | 264 | message_left = packet_len - ((*packet_data_pptr) - packet_data_start_ptr); |
zdshelby | 0:aafd54b05111 | 265 | |
zdshelby | 0:aafd54b05111 | 266 | /* Loop all Options */ |
zdshelby | 0:aafd54b05111 | 267 | while((**packet_data_pptr != 0xff) && message_left) |
zdshelby | 0:aafd54b05111 | 268 | { |
zdshelby | 0:aafd54b05111 | 269 | |
zdshelby | 0:aafd54b05111 | 270 | /* Get option length WITHOUT extensions */ |
zdshelby | 0:aafd54b05111 | 271 | uint16_t option_len = (**packet_data_pptr & 0x0F); |
zdshelby | 0:aafd54b05111 | 272 | |
zdshelby | 0:aafd54b05111 | 273 | /* Option number length 15 is reserved for the future use - ERROR */ |
zdshelby | 0:aafd54b05111 | 274 | if(option_len == 15) |
zdshelby | 0:aafd54b05111 | 275 | return -1; |
zdshelby | 0:aafd54b05111 | 276 | |
zdshelby | 0:aafd54b05111 | 277 | /* Resolve option delta */ |
zdshelby | 0:aafd54b05111 | 278 | uint16_t option_number = (**packet_data_pptr >> COAP_OPTIONS_OPTION_NUMBER_SHIFT); |
zdshelby | 0:aafd54b05111 | 279 | |
zdshelby | 0:aafd54b05111 | 280 | if(option_number == 13) |
zdshelby | 0:aafd54b05111 | 281 | { |
zdshelby | 0:aafd54b05111 | 282 | option_number = *(*packet_data_pptr+1) + 13; |
zdshelby | 0:aafd54b05111 | 283 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 284 | } |
zdshelby | 0:aafd54b05111 | 285 | else if(option_number == 14) |
zdshelby | 0:aafd54b05111 | 286 | { |
zdshelby | 0:aafd54b05111 | 287 | option_number = *(*packet_data_pptr+2); |
zdshelby | 0:aafd54b05111 | 288 | option_number += (*(*packet_data_pptr+1) << 8) + 269; |
zdshelby | 0:aafd54b05111 | 289 | (*packet_data_pptr) += 2; |
zdshelby | 0:aafd54b05111 | 290 | } |
zdshelby | 0:aafd54b05111 | 291 | /* Option number 15 reserved for payload marker. This is handled as a error! */ |
zdshelby | 0:aafd54b05111 | 292 | else if(option_number == 15) |
zdshelby | 0:aafd54b05111 | 293 | return -1; |
zdshelby | 0:aafd54b05111 | 294 | |
zdshelby | 0:aafd54b05111 | 295 | /* Add previous option to option delta and get option number */ |
zdshelby | 0:aafd54b05111 | 296 | option_number += previous_option_number; |
zdshelby | 0:aafd54b05111 | 297 | |
zdshelby | 0:aafd54b05111 | 298 | /* Add possible option length extension to resolve full length of the option */ |
zdshelby | 0:aafd54b05111 | 299 | if(option_len == 13) |
zdshelby | 0:aafd54b05111 | 300 | { |
zdshelby | 0:aafd54b05111 | 301 | option_len = *(*packet_data_pptr+1) + 13; |
zdshelby | 0:aafd54b05111 | 302 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 303 | } |
zdshelby | 0:aafd54b05111 | 304 | else if(option_len == 14) |
zdshelby | 0:aafd54b05111 | 305 | { |
zdshelby | 0:aafd54b05111 | 306 | option_len = *(*packet_data_pptr+2); |
zdshelby | 0:aafd54b05111 | 307 | option_len += (*(*packet_data_pptr+1) << 8) + 269; |
zdshelby | 0:aafd54b05111 | 308 | (*packet_data_pptr) += 2; |
zdshelby | 0:aafd54b05111 | 309 | } |
zdshelby | 0:aafd54b05111 | 310 | |
zdshelby | 0:aafd54b05111 | 311 | |
zdshelby | 0:aafd54b05111 | 312 | /* * * Parse option itself * * */ |
zdshelby | 0:aafd54b05111 | 313 | /* Some options are handled independently in own functions */ |
zdshelby | 0:aafd54b05111 | 314 | previous_option_number = option_number; |
zdshelby | 0:aafd54b05111 | 315 | |
zdshelby | 0:aafd54b05111 | 316 | /* Allocate options_list_ptr if needed */ |
zdshelby | 0:aafd54b05111 | 317 | switch (option_number) |
zdshelby | 0:aafd54b05111 | 318 | { |
zdshelby | 0:aafd54b05111 | 319 | case COAP_OPTION_MAX_AGE: |
zdshelby | 0:aafd54b05111 | 320 | case COAP_OPTION_PROXY_URI: |
zdshelby | 0:aafd54b05111 | 321 | case COAP_OPTION_ETAG: |
zdshelby | 0:aafd54b05111 | 322 | case COAP_OPTION_URI_HOST: |
zdshelby | 0:aafd54b05111 | 323 | case COAP_OPTION_LOCATION_PATH: |
zdshelby | 0:aafd54b05111 | 324 | case COAP_OPTION_URI_PORT: |
zdshelby | 0:aafd54b05111 | 325 | case COAP_OPTION_LOCATION_QUERY: |
zdshelby | 0:aafd54b05111 | 326 | case COAP_OPTION_OBSERVE: |
zdshelby | 0:aafd54b05111 | 327 | case COAP_OPTION_URI_QUERY: |
zdshelby | 0:aafd54b05111 | 328 | case COAP_OPTION_BLOCK2: |
zdshelby | 0:aafd54b05111 | 329 | case COAP_OPTION_BLOCK1: |
zdshelby | 0:aafd54b05111 | 330 | case COAP_OPTION_ACCEPT: |
zdshelby | 0:aafd54b05111 | 331 | if (dst_coap_msg_ptr->options_list_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 332 | { |
zdshelby | 0:aafd54b05111 | 333 | dst_coap_msg_ptr->options_list_ptr = sn_coap_malloc(sizeof(sn_coap_options_list_s)); |
zdshelby | 0:aafd54b05111 | 334 | if(NULL == dst_coap_msg_ptr->options_list_ptr) |
zdshelby | 0:aafd54b05111 | 335 | { |
zdshelby | 0:aafd54b05111 | 336 | return -1; |
zdshelby | 0:aafd54b05111 | 337 | } |
zdshelby | 0:aafd54b05111 | 338 | memset(dst_coap_msg_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
zdshelby | 0:aafd54b05111 | 339 | } |
zdshelby | 0:aafd54b05111 | 340 | break; |
zdshelby | 0:aafd54b05111 | 341 | } |
zdshelby | 0:aafd54b05111 | 342 | |
zdshelby | 0:aafd54b05111 | 343 | /* Parse option */ |
zdshelby | 0:aafd54b05111 | 344 | switch (option_number) |
zdshelby | 0:aafd54b05111 | 345 | { |
zdshelby | 0:aafd54b05111 | 346 | case COAP_OPTION_CONTENT_FORMAT: |
zdshelby | 0:aafd54b05111 | 347 | if((option_len > 2) || (dst_coap_msg_ptr->content_type_ptr)) |
zdshelby | 0:aafd54b05111 | 348 | return -1; |
zdshelby | 0:aafd54b05111 | 349 | dst_coap_msg_ptr->content_type_len = option_len; |
zdshelby | 0:aafd54b05111 | 350 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 351 | |
zdshelby | 0:aafd54b05111 | 352 | if(option_len) |
zdshelby | 0:aafd54b05111 | 353 | { |
zdshelby | 0:aafd54b05111 | 354 | dst_coap_msg_ptr->content_type_ptr = sn_coap_malloc(option_len); |
zdshelby | 0:aafd54b05111 | 355 | |
zdshelby | 0:aafd54b05111 | 356 | if (dst_coap_msg_ptr->content_type_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 357 | { |
zdshelby | 0:aafd54b05111 | 358 | return -1; |
zdshelby | 0:aafd54b05111 | 359 | } |
zdshelby | 0:aafd54b05111 | 360 | |
zdshelby | 0:aafd54b05111 | 361 | memcpy(dst_coap_msg_ptr->content_type_ptr, *packet_data_pptr, option_len); |
zdshelby | 0:aafd54b05111 | 362 | (*packet_data_pptr) += option_len; |
zdshelby | 0:aafd54b05111 | 363 | } |
zdshelby | 0:aafd54b05111 | 364 | break; |
zdshelby | 0:aafd54b05111 | 365 | |
zdshelby | 0:aafd54b05111 | 366 | case COAP_OPTION_MAX_AGE: |
zdshelby | 0:aafd54b05111 | 367 | if((option_len > 4) || dst_coap_msg_ptr->options_list_ptr->max_age_ptr) |
zdshelby | 0:aafd54b05111 | 368 | return -1; |
zdshelby | 0:aafd54b05111 | 369 | dst_coap_msg_ptr->options_list_ptr->max_age_len = option_len; |
zdshelby | 0:aafd54b05111 | 370 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 371 | |
zdshelby | 0:aafd54b05111 | 372 | if(option_len) |
zdshelby | 0:aafd54b05111 | 373 | { |
zdshelby | 0:aafd54b05111 | 374 | dst_coap_msg_ptr->options_list_ptr->max_age_ptr = sn_coap_malloc(option_len); |
zdshelby | 0:aafd54b05111 | 375 | |
zdshelby | 0:aafd54b05111 | 376 | if (dst_coap_msg_ptr->options_list_ptr->max_age_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 377 | { |
zdshelby | 0:aafd54b05111 | 378 | return -1; |
zdshelby | 0:aafd54b05111 | 379 | } |
zdshelby | 0:aafd54b05111 | 380 | |
zdshelby | 0:aafd54b05111 | 381 | memcpy(dst_coap_msg_ptr->options_list_ptr->max_age_ptr, *packet_data_pptr, option_len); |
zdshelby | 0:aafd54b05111 | 382 | (*packet_data_pptr) += option_len; |
zdshelby | 0:aafd54b05111 | 383 | } |
zdshelby | 0:aafd54b05111 | 384 | break; |
zdshelby | 0:aafd54b05111 | 385 | |
zdshelby | 0:aafd54b05111 | 386 | case COAP_OPTION_PROXY_URI: |
zdshelby | 0:aafd54b05111 | 387 | if ((option_len > 1034) || (option_len < 1) || dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr) |
zdshelby | 0:aafd54b05111 | 388 | return -1; |
zdshelby | 0:aafd54b05111 | 389 | dst_coap_msg_ptr->options_list_ptr->proxy_uri_len = option_len; |
zdshelby | 0:aafd54b05111 | 390 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 391 | |
zdshelby | 0:aafd54b05111 | 392 | dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr = sn_coap_malloc(option_len); |
zdshelby | 0:aafd54b05111 | 393 | |
zdshelby | 0:aafd54b05111 | 394 | if (dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 395 | { |
zdshelby | 0:aafd54b05111 | 396 | return -1; |
zdshelby | 0:aafd54b05111 | 397 | } |
zdshelby | 0:aafd54b05111 | 398 | memcpy(dst_coap_msg_ptr->options_list_ptr->proxy_uri_ptr, *packet_data_pptr, option_len); |
zdshelby | 0:aafd54b05111 | 399 | (*packet_data_pptr) += option_len; |
zdshelby | 0:aafd54b05111 | 400 | |
zdshelby | 0:aafd54b05111 | 401 | break; |
zdshelby | 0:aafd54b05111 | 402 | |
zdshelby | 0:aafd54b05111 | 403 | case COAP_OPTION_ETAG: |
zdshelby | 0:aafd54b05111 | 404 | if(dst_coap_msg_ptr->options_list_ptr->etag_ptr) |
zdshelby | 0:aafd54b05111 | 405 | return -1; |
zdshelby | 0:aafd54b05111 | 406 | /* This is managed independently because User gives this option in one character table */ |
zdshelby | 0:aafd54b05111 | 407 | |
zdshelby | 0:aafd54b05111 | 408 | ret_status = sn_coap_parser_options_parse_multiple_options (packet_data_pptr, |
zdshelby | 0:aafd54b05111 | 409 | message_left, |
zdshelby | 0:aafd54b05111 | 410 | &dst_coap_msg_ptr->options_list_ptr->etag_ptr, |
zdshelby | 0:aafd54b05111 | 411 | (uint16_t*)&dst_coap_msg_ptr->options_list_ptr->etag_len, |
zdshelby | 0:aafd54b05111 | 412 | COAP_OPTION_LOCATION_PATH, option_len); |
zdshelby | 0:aafd54b05111 | 413 | if (ret_status >= 0) |
zdshelby | 0:aafd54b05111 | 414 | { |
zdshelby | 0:aafd54b05111 | 415 | i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ |
zdshelby | 0:aafd54b05111 | 416 | } |
zdshelby | 0:aafd54b05111 | 417 | else |
zdshelby | 0:aafd54b05111 | 418 | { |
zdshelby | 0:aafd54b05111 | 419 | return -1; |
zdshelby | 0:aafd54b05111 | 420 | } |
zdshelby | 0:aafd54b05111 | 421 | break; |
zdshelby | 0:aafd54b05111 | 422 | |
zdshelby | 0:aafd54b05111 | 423 | case COAP_OPTION_URI_HOST: |
zdshelby | 0:aafd54b05111 | 424 | if((option_len > 255) || (option_len < 1) || dst_coap_msg_ptr->options_list_ptr->uri_host_ptr) |
zdshelby | 0:aafd54b05111 | 425 | return -1; |
zdshelby | 0:aafd54b05111 | 426 | dst_coap_msg_ptr->options_list_ptr->uri_host_len = option_len; |
zdshelby | 0:aafd54b05111 | 427 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 428 | |
zdshelby | 0:aafd54b05111 | 429 | dst_coap_msg_ptr->options_list_ptr->uri_host_ptr = sn_coap_malloc(option_len); |
zdshelby | 0:aafd54b05111 | 430 | |
zdshelby | 0:aafd54b05111 | 431 | if (dst_coap_msg_ptr->options_list_ptr->uri_host_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 432 | { |
zdshelby | 0:aafd54b05111 | 433 | return -1; |
zdshelby | 0:aafd54b05111 | 434 | } |
zdshelby | 0:aafd54b05111 | 435 | memcpy(dst_coap_msg_ptr->options_list_ptr->uri_host_ptr, *packet_data_pptr, option_len); |
zdshelby | 0:aafd54b05111 | 436 | (*packet_data_pptr) += option_len; |
zdshelby | 0:aafd54b05111 | 437 | |
zdshelby | 0:aafd54b05111 | 438 | break; |
zdshelby | 0:aafd54b05111 | 439 | |
zdshelby | 0:aafd54b05111 | 440 | case COAP_OPTION_LOCATION_PATH: |
zdshelby | 0:aafd54b05111 | 441 | if(dst_coap_msg_ptr->options_list_ptr->location_path_ptr) |
zdshelby | 0:aafd54b05111 | 442 | return -1; |
zdshelby | 0:aafd54b05111 | 443 | /* This is managed independently because User gives this option in one character table */ |
zdshelby | 0:aafd54b05111 | 444 | ret_status = sn_coap_parser_options_parse_multiple_options(packet_data_pptr, message_left, |
zdshelby | 0:aafd54b05111 | 445 | &dst_coap_msg_ptr->options_list_ptr->location_path_ptr, &dst_coap_msg_ptr->options_list_ptr->location_path_len, |
zdshelby | 0:aafd54b05111 | 446 | COAP_OPTION_LOCATION_PATH, option_len); |
zdshelby | 0:aafd54b05111 | 447 | if (ret_status >= 0) |
zdshelby | 0:aafd54b05111 | 448 | { |
zdshelby | 0:aafd54b05111 | 449 | i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ |
zdshelby | 0:aafd54b05111 | 450 | } |
zdshelby | 0:aafd54b05111 | 451 | else |
zdshelby | 0:aafd54b05111 | 452 | { |
zdshelby | 0:aafd54b05111 | 453 | return -1; |
zdshelby | 0:aafd54b05111 | 454 | } |
zdshelby | 0:aafd54b05111 | 455 | |
zdshelby | 0:aafd54b05111 | 456 | break; |
zdshelby | 0:aafd54b05111 | 457 | |
zdshelby | 0:aafd54b05111 | 458 | |
zdshelby | 0:aafd54b05111 | 459 | case COAP_OPTION_URI_PORT: |
zdshelby | 0:aafd54b05111 | 460 | if((option_len > 2) || dst_coap_msg_ptr->options_list_ptr->uri_port_ptr) |
zdshelby | 0:aafd54b05111 | 461 | return -1; |
zdshelby | 0:aafd54b05111 | 462 | dst_coap_msg_ptr->options_list_ptr->uri_port_len = option_len; |
zdshelby | 0:aafd54b05111 | 463 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 464 | |
zdshelby | 0:aafd54b05111 | 465 | if(option_len) |
zdshelby | 0:aafd54b05111 | 466 | { |
zdshelby | 0:aafd54b05111 | 467 | dst_coap_msg_ptr->options_list_ptr->uri_port_ptr = sn_coap_malloc(option_len); |
zdshelby | 0:aafd54b05111 | 468 | |
zdshelby | 0:aafd54b05111 | 469 | if (dst_coap_msg_ptr->options_list_ptr->uri_port_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 470 | { |
zdshelby | 0:aafd54b05111 | 471 | return -1; |
zdshelby | 0:aafd54b05111 | 472 | } |
zdshelby | 0:aafd54b05111 | 473 | memcpy(dst_coap_msg_ptr->options_list_ptr->uri_port_ptr, *packet_data_pptr, option_len); |
zdshelby | 0:aafd54b05111 | 474 | (*packet_data_pptr) += option_len; |
zdshelby | 0:aafd54b05111 | 475 | } |
zdshelby | 0:aafd54b05111 | 476 | break; |
zdshelby | 0:aafd54b05111 | 477 | |
zdshelby | 0:aafd54b05111 | 478 | case COAP_OPTION_LOCATION_QUERY: |
zdshelby | 0:aafd54b05111 | 479 | if(dst_coap_msg_ptr->options_list_ptr->location_query_ptr) |
zdshelby | 0:aafd54b05111 | 480 | return -1; |
zdshelby | 0:aafd54b05111 | 481 | ret_status = sn_coap_parser_options_parse_multiple_options(packet_data_pptr, message_left, |
zdshelby | 0:aafd54b05111 | 482 | &dst_coap_msg_ptr->options_list_ptr->location_query_ptr, &dst_coap_msg_ptr->options_list_ptr->location_query_len, |
zdshelby | 0:aafd54b05111 | 483 | COAP_OPTION_LOCATION_QUERY, option_len); |
zdshelby | 0:aafd54b05111 | 484 | if (ret_status >= 0) |
zdshelby | 0:aafd54b05111 | 485 | { |
zdshelby | 0:aafd54b05111 | 486 | i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ |
zdshelby | 0:aafd54b05111 | 487 | } |
zdshelby | 0:aafd54b05111 | 488 | else |
zdshelby | 0:aafd54b05111 | 489 | { |
zdshelby | 0:aafd54b05111 | 490 | return -1; |
zdshelby | 0:aafd54b05111 | 491 | } |
zdshelby | 0:aafd54b05111 | 492 | |
zdshelby | 0:aafd54b05111 | 493 | break; |
zdshelby | 0:aafd54b05111 | 494 | |
zdshelby | 0:aafd54b05111 | 495 | case COAP_OPTION_URI_PATH: |
zdshelby | 0:aafd54b05111 | 496 | if(dst_coap_msg_ptr->uri_path_ptr) |
zdshelby | 0:aafd54b05111 | 497 | return -1; |
zdshelby | 0:aafd54b05111 | 498 | ret_status = sn_coap_parser_options_parse_multiple_options(packet_data_pptr, message_left, |
zdshelby | 0:aafd54b05111 | 499 | &dst_coap_msg_ptr->uri_path_ptr, &dst_coap_msg_ptr->uri_path_len, |
zdshelby | 0:aafd54b05111 | 500 | COAP_OPTION_URI_PATH, option_len); |
zdshelby | 0:aafd54b05111 | 501 | if (ret_status >= 0) |
zdshelby | 0:aafd54b05111 | 502 | { |
zdshelby | 0:aafd54b05111 | 503 | i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ |
zdshelby | 0:aafd54b05111 | 504 | } |
zdshelby | 0:aafd54b05111 | 505 | else |
zdshelby | 0:aafd54b05111 | 506 | { |
zdshelby | 0:aafd54b05111 | 507 | return -1; |
zdshelby | 0:aafd54b05111 | 508 | } |
zdshelby | 0:aafd54b05111 | 509 | |
zdshelby | 0:aafd54b05111 | 510 | break; |
zdshelby | 0:aafd54b05111 | 511 | |
zdshelby | 0:aafd54b05111 | 512 | case COAP_OPTION_OBSERVE: |
zdshelby | 0:aafd54b05111 | 513 | if((option_len > 2) || dst_coap_msg_ptr->options_list_ptr->observe_ptr) |
zdshelby | 0:aafd54b05111 | 514 | return -1; |
zdshelby | 0:aafd54b05111 | 515 | |
zdshelby | 0:aafd54b05111 | 516 | dst_coap_msg_ptr->options_list_ptr->observe = 1; |
zdshelby | 0:aafd54b05111 | 517 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 518 | |
zdshelby | 0:aafd54b05111 | 519 | if(option_len) |
zdshelby | 0:aafd54b05111 | 520 | { |
zdshelby | 0:aafd54b05111 | 521 | |
zdshelby | 0:aafd54b05111 | 522 | dst_coap_msg_ptr->options_list_ptr->observe_len = option_len; |
zdshelby | 0:aafd54b05111 | 523 | |
zdshelby | 0:aafd54b05111 | 524 | dst_coap_msg_ptr->options_list_ptr->observe_ptr = sn_coap_malloc(option_len); |
zdshelby | 0:aafd54b05111 | 525 | |
zdshelby | 0:aafd54b05111 | 526 | if (dst_coap_msg_ptr->options_list_ptr->observe_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 527 | { |
zdshelby | 0:aafd54b05111 | 528 | return -1; |
zdshelby | 0:aafd54b05111 | 529 | } |
zdshelby | 0:aafd54b05111 | 530 | |
zdshelby | 0:aafd54b05111 | 531 | memcpy(dst_coap_msg_ptr->options_list_ptr->observe_ptr, *packet_data_pptr, option_len); |
zdshelby | 0:aafd54b05111 | 532 | (*packet_data_pptr) += option_len; |
zdshelby | 0:aafd54b05111 | 533 | } |
zdshelby | 0:aafd54b05111 | 534 | |
zdshelby | 0:aafd54b05111 | 535 | break; |
zdshelby | 0:aafd54b05111 | 536 | |
zdshelby | 0:aafd54b05111 | 537 | case COAP_OPTION_URI_QUERY: |
zdshelby | 0:aafd54b05111 | 538 | if(dst_coap_msg_ptr->options_list_ptr->uri_query_ptr) |
zdshelby | 0:aafd54b05111 | 539 | return -1; |
zdshelby | 0:aafd54b05111 | 540 | ret_status = sn_coap_parser_options_parse_multiple_options(packet_data_pptr, message_left, |
zdshelby | 0:aafd54b05111 | 541 | &dst_coap_msg_ptr->options_list_ptr->uri_query_ptr, &dst_coap_msg_ptr->options_list_ptr->uri_query_len, |
zdshelby | 0:aafd54b05111 | 542 | COAP_OPTION_URI_QUERY, option_len); |
zdshelby | 0:aafd54b05111 | 543 | if (ret_status >= 0) |
zdshelby | 0:aafd54b05111 | 544 | { |
zdshelby | 0:aafd54b05111 | 545 | i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ |
zdshelby | 0:aafd54b05111 | 546 | } |
zdshelby | 0:aafd54b05111 | 547 | else |
zdshelby | 0:aafd54b05111 | 548 | { |
zdshelby | 0:aafd54b05111 | 549 | return -1; |
zdshelby | 0:aafd54b05111 | 550 | } |
zdshelby | 0:aafd54b05111 | 551 | |
zdshelby | 0:aafd54b05111 | 552 | break; |
zdshelby | 0:aafd54b05111 | 553 | |
zdshelby | 0:aafd54b05111 | 554 | case COAP_OPTION_BLOCK2: |
zdshelby | 0:aafd54b05111 | 555 | if((option_len > 4) || dst_coap_msg_ptr->options_list_ptr->block2_ptr) |
zdshelby | 0:aafd54b05111 | 556 | return -1; |
zdshelby | 0:aafd54b05111 | 557 | dst_coap_msg_ptr->options_list_ptr->block2_len = option_len; |
zdshelby | 0:aafd54b05111 | 558 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 559 | |
zdshelby | 0:aafd54b05111 | 560 | dst_coap_msg_ptr->options_list_ptr->block2_ptr = sn_coap_malloc(option_len); |
zdshelby | 0:aafd54b05111 | 561 | |
zdshelby | 0:aafd54b05111 | 562 | if (dst_coap_msg_ptr->options_list_ptr->block2_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 563 | { |
zdshelby | 0:aafd54b05111 | 564 | return -1; |
zdshelby | 0:aafd54b05111 | 565 | } |
zdshelby | 0:aafd54b05111 | 566 | |
zdshelby | 0:aafd54b05111 | 567 | memcpy(dst_coap_msg_ptr->options_list_ptr->block2_ptr, *packet_data_pptr, option_len); |
zdshelby | 0:aafd54b05111 | 568 | (*packet_data_pptr) += option_len; |
zdshelby | 0:aafd54b05111 | 569 | |
zdshelby | 0:aafd54b05111 | 570 | break; |
zdshelby | 0:aafd54b05111 | 571 | |
zdshelby | 0:aafd54b05111 | 572 | case COAP_OPTION_BLOCK1: |
zdshelby | 0:aafd54b05111 | 573 | if((option_len > 4) || dst_coap_msg_ptr->options_list_ptr->block1_ptr) |
zdshelby | 0:aafd54b05111 | 574 | return -1; |
zdshelby | 0:aafd54b05111 | 575 | dst_coap_msg_ptr->options_list_ptr->block1_len = option_len; |
zdshelby | 0:aafd54b05111 | 576 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 577 | |
zdshelby | 0:aafd54b05111 | 578 | dst_coap_msg_ptr->options_list_ptr->block1_ptr = sn_coap_malloc(option_len); |
zdshelby | 0:aafd54b05111 | 579 | |
zdshelby | 0:aafd54b05111 | 580 | if (dst_coap_msg_ptr->options_list_ptr->block1_ptr == NULL) |
zdshelby | 0:aafd54b05111 | 581 | { |
zdshelby | 0:aafd54b05111 | 582 | return -1; |
zdshelby | 0:aafd54b05111 | 583 | } |
zdshelby | 0:aafd54b05111 | 584 | |
zdshelby | 0:aafd54b05111 | 585 | memcpy(dst_coap_msg_ptr->options_list_ptr->block1_ptr, *packet_data_pptr, option_len); |
zdshelby | 0:aafd54b05111 | 586 | (*packet_data_pptr) += option_len; |
zdshelby | 0:aafd54b05111 | 587 | |
zdshelby | 0:aafd54b05111 | 588 | break; |
zdshelby | 0:aafd54b05111 | 589 | |
zdshelby | 0:aafd54b05111 | 590 | case COAP_OPTION_ACCEPT: |
zdshelby | 0:aafd54b05111 | 591 | if(dst_coap_msg_ptr->options_list_ptr->accept_ptr) |
zdshelby | 0:aafd54b05111 | 592 | return -1; |
zdshelby | 0:aafd54b05111 | 593 | ret_status = sn_coap_parser_options_parse_multiple_options(packet_data_pptr, message_left, |
zdshelby | 0:aafd54b05111 | 594 | &dst_coap_msg_ptr->options_list_ptr->accept_ptr, (uint16_t *)&dst_coap_msg_ptr->options_list_ptr->accept_len, |
zdshelby | 0:aafd54b05111 | 595 | COAP_OPTION_ACCEPT, option_len); |
zdshelby | 0:aafd54b05111 | 596 | if (ret_status >= 0) |
zdshelby | 0:aafd54b05111 | 597 | { |
zdshelby | 0:aafd54b05111 | 598 | i += (ret_status - 1); /* i += is because possible several Options are handled by sn_coap_parser_options_parse_multiple_options() */ |
zdshelby | 0:aafd54b05111 | 599 | } |
zdshelby | 0:aafd54b05111 | 600 | else |
zdshelby | 0:aafd54b05111 | 601 | { |
zdshelby | 0:aafd54b05111 | 602 | return -1; |
zdshelby | 0:aafd54b05111 | 603 | } |
zdshelby | 0:aafd54b05111 | 604 | |
zdshelby | 0:aafd54b05111 | 605 | break; |
zdshelby | 0:aafd54b05111 | 606 | |
zdshelby | 0:aafd54b05111 | 607 | default: |
zdshelby | 0:aafd54b05111 | 608 | return -1; |
zdshelby | 0:aafd54b05111 | 609 | } |
zdshelby | 0:aafd54b05111 | 610 | |
zdshelby | 0:aafd54b05111 | 611 | /* Check for overflow */ |
zdshelby | 0:aafd54b05111 | 612 | if((*packet_data_pptr - packet_data_start_ptr) > packet_len) |
zdshelby | 0:aafd54b05111 | 613 | return -1; |
zdshelby | 0:aafd54b05111 | 614 | |
zdshelby | 0:aafd54b05111 | 615 | message_left = packet_len - (*packet_data_pptr - packet_data_start_ptr); //todo: if(*packet_data_pptr < packet_data_start_ptr) message_left = 0; else |
zdshelby | 0:aafd54b05111 | 616 | |
zdshelby | 0:aafd54b05111 | 617 | |
zdshelby | 0:aafd54b05111 | 618 | } |
zdshelby | 0:aafd54b05111 | 619 | |
zdshelby | 0:aafd54b05111 | 620 | return 0; |
zdshelby | 0:aafd54b05111 | 621 | } |
zdshelby | 0:aafd54b05111 | 622 | |
zdshelby | 0:aafd54b05111 | 623 | |
zdshelby | 0:aafd54b05111 | 624 | /** |
zdshelby | 0:aafd54b05111 | 625 | * \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, |
zdshelby | 0:aafd54b05111 | 626 | * uint16_t *dst_len_ptr, sn_coap_option_numbers_e option, uint16_t option_number_len) |
zdshelby | 0:aafd54b05111 | 627 | * |
zdshelby | 0:aafd54b05111 | 628 | * \brief Parses CoAP message's Uri-query options |
zdshelby | 0:aafd54b05111 | 629 | * |
zdshelby | 0:aafd54b05111 | 630 | * \param **packet_data_pptr is source for Packet data to be parsed to CoAP message |
zdshelby | 0:aafd54b05111 | 631 | * |
zdshelby | 0:aafd54b05111 | 632 | * \param *dst_coap_msg_ptr is destination for parsed CoAP message |
zdshelby | 0:aafd54b05111 | 633 | * |
zdshelby | 0:aafd54b05111 | 634 | * \param options_count_left tells how many options are unhandled in Packet data |
zdshelby | 0:aafd54b05111 | 635 | * |
zdshelby | 0:aafd54b05111 | 636 | * \param *previous_option_number_ptr is pointer to used and returned previous Option number |
zdshelby | 0:aafd54b05111 | 637 | * |
zdshelby | 0:aafd54b05111 | 638 | * \return Return value is count of Uri-query optios parsed. In failure case -1 is returned. |
zdshelby | 0:aafd54b05111 | 639 | */ |
zdshelby | 0:aafd54b05111 | 640 | |
zdshelby | 0:aafd54b05111 | 641 | static int8_t sn_coap_parser_options_parse_multiple_options(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) |
zdshelby | 0:aafd54b05111 | 642 | { |
zdshelby | 0:aafd54b05111 | 643 | 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); |
zdshelby | 0:aafd54b05111 | 644 | uint8_t *temp_parsed_uri_query_ptr = NULL; |
zdshelby | 0:aafd54b05111 | 645 | uint8_t returned_option_counter = 0; |
zdshelby | 0:aafd54b05111 | 646 | |
zdshelby | 0:aafd54b05111 | 647 | if(uri_query_needed_heap == -1) |
zdshelby | 0:aafd54b05111 | 648 | return -1; |
zdshelby | 0:aafd54b05111 | 649 | |
zdshelby | 0:aafd54b05111 | 650 | if(uri_query_needed_heap) |
zdshelby | 0:aafd54b05111 | 651 | { |
zdshelby | 0:aafd54b05111 | 652 | *dst_pptr = (uint8_t*) sn_coap_malloc(uri_query_needed_heap); |
zdshelby | 0:aafd54b05111 | 653 | |
zdshelby | 0:aafd54b05111 | 654 | if (*dst_pptr == NULL) |
zdshelby | 0:aafd54b05111 | 655 | return -1; |
zdshelby | 0:aafd54b05111 | 656 | } |
zdshelby | 0:aafd54b05111 | 657 | |
zdshelby | 0:aafd54b05111 | 658 | *dst_len_ptr = uri_query_needed_heap; |
zdshelby | 0:aafd54b05111 | 659 | |
zdshelby | 0:aafd54b05111 | 660 | temp_parsed_uri_query_ptr = *dst_pptr; |
zdshelby | 0:aafd54b05111 | 661 | |
zdshelby | 0:aafd54b05111 | 662 | /* Loop all Uri-Query options */ |
zdshelby | 0:aafd54b05111 | 663 | while ((temp_parsed_uri_query_ptr - *dst_pptr) < uri_query_needed_heap) |
zdshelby | 0:aafd54b05111 | 664 | { |
zdshelby | 0:aafd54b05111 | 665 | /* Check if this is first Uri-Query option */ |
zdshelby | 0:aafd54b05111 | 666 | if (returned_option_counter > 0) |
zdshelby | 0:aafd54b05111 | 667 | { |
zdshelby | 0:aafd54b05111 | 668 | /* Uri-Query is modified to following format: temp1'\0'temp2'\0'temp3 i.e. */ |
zdshelby | 0:aafd54b05111 | 669 | /* Uri-Path is modified to following format: temp1\temp2\temp3 i.e. */ |
zdshelby | 0:aafd54b05111 | 670 | if(option == COAP_OPTION_URI_QUERY || option == COAP_OPTION_LOCATION_QUERY || option == COAP_OPTION_ETAG || option == COAP_OPTION_ACCEPT) |
zdshelby | 0:aafd54b05111 | 671 | memset(temp_parsed_uri_query_ptr, '&', 1); |
zdshelby | 0:aafd54b05111 | 672 | else if(option == COAP_OPTION_URI_PATH || option == COAP_OPTION_LOCATION_PATH) |
zdshelby | 0:aafd54b05111 | 673 | memset(temp_parsed_uri_query_ptr, '/', 1); |
zdshelby | 0:aafd54b05111 | 674 | |
zdshelby | 0:aafd54b05111 | 675 | temp_parsed_uri_query_ptr++; |
zdshelby | 0:aafd54b05111 | 676 | } |
zdshelby | 0:aafd54b05111 | 677 | |
zdshelby | 0:aafd54b05111 | 678 | returned_option_counter++; |
zdshelby | 0:aafd54b05111 | 679 | |
zdshelby | 0:aafd54b05111 | 680 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 681 | |
zdshelby | 0:aafd54b05111 | 682 | if(((temp_parsed_uri_query_ptr - *dst_pptr) + option_number_len) > uri_query_needed_heap) |
zdshelby | 0:aafd54b05111 | 683 | return -1; |
zdshelby | 0:aafd54b05111 | 684 | |
zdshelby | 0:aafd54b05111 | 685 | memcpy(temp_parsed_uri_query_ptr, *packet_data_pptr, option_number_len); |
zdshelby | 0:aafd54b05111 | 686 | |
zdshelby | 0:aafd54b05111 | 687 | (*packet_data_pptr) += option_number_len; |
zdshelby | 0:aafd54b05111 | 688 | temp_parsed_uri_query_ptr += option_number_len; |
zdshelby | 0:aafd54b05111 | 689 | |
zdshelby | 0:aafd54b05111 | 690 | if(((**packet_data_pptr >> COAP_OPTIONS_OPTION_NUMBER_SHIFT) != 0) || (temp_parsed_uri_query_ptr - *dst_pptr) >= uri_query_needed_heap) |
zdshelby | 0:aafd54b05111 | 691 | { |
zdshelby | 0:aafd54b05111 | 692 | return returned_option_counter; |
zdshelby | 0:aafd54b05111 | 693 | } |
zdshelby | 0:aafd54b05111 | 694 | |
zdshelby | 0:aafd54b05111 | 695 | option_number_len = (**packet_data_pptr & 0x0F); |
zdshelby | 0:aafd54b05111 | 696 | if(option_number_len == 13) |
zdshelby | 0:aafd54b05111 | 697 | { |
zdshelby | 0:aafd54b05111 | 698 | option_number_len = *(*packet_data_pptr+1) + 13; |
zdshelby | 0:aafd54b05111 | 699 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 700 | } |
zdshelby | 0:aafd54b05111 | 701 | else if(option_number_len == 14) |
zdshelby | 0:aafd54b05111 | 702 | { |
zdshelby | 0:aafd54b05111 | 703 | option_number_len = *(*packet_data_pptr+2); |
zdshelby | 0:aafd54b05111 | 704 | option_number_len += (*(*packet_data_pptr+1) << 8) + 269; |
zdshelby | 0:aafd54b05111 | 705 | (*packet_data_pptr) += 2; |
zdshelby | 0:aafd54b05111 | 706 | } |
zdshelby | 0:aafd54b05111 | 707 | } |
zdshelby | 0:aafd54b05111 | 708 | |
zdshelby | 0:aafd54b05111 | 709 | return returned_option_counter; |
zdshelby | 0:aafd54b05111 | 710 | } |
zdshelby | 0:aafd54b05111 | 711 | |
zdshelby | 0:aafd54b05111 | 712 | |
zdshelby | 0:aafd54b05111 | 713 | |
zdshelby | 0:aafd54b05111 | 714 | |
zdshelby | 0:aafd54b05111 | 715 | /** |
zdshelby | 0:aafd54b05111 | 716 | * \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) |
zdshelby | 0:aafd54b05111 | 717 | * |
zdshelby | 0:aafd54b05111 | 718 | * \brief Counts needed memory for uri query option |
zdshelby | 0:aafd54b05111 | 719 | * |
zdshelby | 0:aafd54b05111 | 720 | * \param *packet_data_ptr is start of source for Packet data to be parsed to CoAP message |
zdshelby | 0:aafd54b05111 | 721 | * |
zdshelby | 0:aafd54b05111 | 722 | * \param options_count_left tells how many options are unhandled in Packet data |
zdshelby | 0:aafd54b05111 | 723 | * |
zdshelby | 0:aafd54b05111 | 724 | * \param previous_option_number is previous Option number |
zdshelby | 0:aafd54b05111 | 725 | * |
zdshelby | 0:aafd54b05111 | 726 | * \param sn_coap_option_numbers_e option option number to be calculated |
zdshelby | 0:aafd54b05111 | 727 | * |
zdshelby | 0:aafd54b05111 | 728 | * \param uint16_t option_number_len length of the first option part |
zdshelby | 0:aafd54b05111 | 729 | */ |
zdshelby | 0:aafd54b05111 | 730 | |
zdshelby | 0:aafd54b05111 | 731 | 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) |
zdshelby | 0:aafd54b05111 | 732 | { |
zdshelby | 0:aafd54b05111 | 733 | uint16_t ret_value = 0; |
zdshelby | 0:aafd54b05111 | 734 | uint16_t i = 1; |
zdshelby | 0:aafd54b05111 | 735 | |
zdshelby | 0:aafd54b05111 | 736 | /* Loop all Uri-Query options */ |
zdshelby | 0:aafd54b05111 | 737 | while (i < packet_left_len) |
zdshelby | 0:aafd54b05111 | 738 | { |
zdshelby | 0:aafd54b05111 | 739 | if(option == COAP_OPTION_LOCATION_PATH && option_number_len > 255) |
zdshelby | 0:aafd54b05111 | 740 | return -1; |
zdshelby | 0:aafd54b05111 | 741 | if(option == COAP_OPTION_URI_PATH && option_number_len > 255) |
zdshelby | 0:aafd54b05111 | 742 | return -1; |
zdshelby | 0:aafd54b05111 | 743 | if(option == COAP_OPTION_URI_QUERY && option_number_len > 255) |
zdshelby | 0:aafd54b05111 | 744 | return -1; |
zdshelby | 0:aafd54b05111 | 745 | if(option == COAP_OPTION_LOCATION_QUERY && option_number_len > 255) |
zdshelby | 0:aafd54b05111 | 746 | return -1; |
zdshelby | 0:aafd54b05111 | 747 | if(option == COAP_OPTION_ACCEPT && option_number_len > 2) |
zdshelby | 0:aafd54b05111 | 748 | return -1; |
zdshelby | 0:aafd54b05111 | 749 | if(option == COAP_OPTION_ETAG && option_number_len > 8) |
zdshelby | 0:aafd54b05111 | 750 | return -1; |
zdshelby | 0:aafd54b05111 | 751 | |
zdshelby | 0:aafd54b05111 | 752 | i += option_number_len; |
zdshelby | 0:aafd54b05111 | 753 | ret_value += option_number_len + 1; /* + 1 is for separator */ |
zdshelby | 0:aafd54b05111 | 754 | |
zdshelby | 0:aafd54b05111 | 755 | if((*(packet_data_ptr + i) >> COAP_OPTIONS_OPTION_NUMBER_SHIFT) != 0) |
zdshelby | 0:aafd54b05111 | 756 | { |
zdshelby | 0:aafd54b05111 | 757 | if(ret_value != 0) |
zdshelby | 0:aafd54b05111 | 758 | return (ret_value - 1); /* -1 because last Part path does not include separator */ |
zdshelby | 0:aafd54b05111 | 759 | else |
zdshelby | 0:aafd54b05111 | 760 | return 0; |
zdshelby | 0:aafd54b05111 | 761 | } |
zdshelby | 0:aafd54b05111 | 762 | |
zdshelby | 0:aafd54b05111 | 763 | option_number_len = (*(packet_data_ptr + i) & 0x0F); |
zdshelby | 0:aafd54b05111 | 764 | |
zdshelby | 0:aafd54b05111 | 765 | if (option_number_len == 13) |
zdshelby | 0:aafd54b05111 | 766 | { |
zdshelby | 0:aafd54b05111 | 767 | i++; |
zdshelby | 0:aafd54b05111 | 768 | option_number_len = *(packet_data_ptr + i) + 13; |
zdshelby | 0:aafd54b05111 | 769 | } |
zdshelby | 0:aafd54b05111 | 770 | else if (option_number_len == 14) |
zdshelby | 0:aafd54b05111 | 771 | { |
zdshelby | 0:aafd54b05111 | 772 | option_number_len = *(packet_data_ptr + 2); |
zdshelby | 0:aafd54b05111 | 773 | option_number_len += (*(packet_data_ptr + 1) << 8) + 269; |
zdshelby | 0:aafd54b05111 | 774 | i += 2; |
zdshelby | 0:aafd54b05111 | 775 | } |
zdshelby | 0:aafd54b05111 | 776 | else if (option_number_len == 15) |
zdshelby | 0:aafd54b05111 | 777 | return -1; |
zdshelby | 0:aafd54b05111 | 778 | i++; |
zdshelby | 0:aafd54b05111 | 779 | |
zdshelby | 0:aafd54b05111 | 780 | } |
zdshelby | 0:aafd54b05111 | 781 | |
zdshelby | 0:aafd54b05111 | 782 | if(ret_value != 0) |
zdshelby | 0:aafd54b05111 | 783 | return (ret_value - 1); /* -1 because last Part path does not include separator */ |
zdshelby | 0:aafd54b05111 | 784 | else |
zdshelby | 0:aafd54b05111 | 785 | return 0; |
zdshelby | 0:aafd54b05111 | 786 | } |
zdshelby | 0:aafd54b05111 | 787 | |
zdshelby | 0:aafd54b05111 | 788 | /** |
zdshelby | 0:aafd54b05111 | 789 | * \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) |
zdshelby | 0:aafd54b05111 | 790 | * |
zdshelby | 0:aafd54b05111 | 791 | * \brief Parses CoAP message's Payload part from given Packet data |
zdshelby | 0:aafd54b05111 | 792 | * |
zdshelby | 0:aafd54b05111 | 793 | * \param packet_data_len is length of given Packet data to be parsed to CoAP message |
zdshelby | 0:aafd54b05111 | 794 | * |
zdshelby | 0:aafd54b05111 | 795 | * \param *packet_data_ptr is start of source for Packet data to be parsed to CoAP message |
zdshelby | 0:aafd54b05111 | 796 | * |
zdshelby | 0:aafd54b05111 | 797 | * \param **packet_data_pptr is source for Packet data to be parsed to CoAP message |
zdshelby | 0:aafd54b05111 | 798 | * |
zdshelby | 0:aafd54b05111 | 799 | * \param *dst_coap_msg_ptr is destination for parsed CoAP message |
zdshelby | 0:aafd54b05111 | 800 | *****************************************************************************/ |
zdshelby | 0:aafd54b05111 | 801 | |
zdshelby | 0:aafd54b05111 | 802 | 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) |
zdshelby | 0:aafd54b05111 | 803 | { |
zdshelby | 0:aafd54b05111 | 804 | /* If there is payload */ |
zdshelby | 0:aafd54b05111 | 805 | if((*packet_data_pptr - packet_data_start_ptr) < packet_data_len) |
zdshelby | 0:aafd54b05111 | 806 | { |
zdshelby | 0:aafd54b05111 | 807 | if(**packet_data_pptr == 0xff) |
zdshelby | 0:aafd54b05111 | 808 | { |
zdshelby | 0:aafd54b05111 | 809 | (*packet_data_pptr)++; |
zdshelby | 0:aafd54b05111 | 810 | /* Parse Payload length */ |
zdshelby | 0:aafd54b05111 | 811 | dst_coap_msg_ptr->payload_len = packet_data_len - (*packet_data_pptr - packet_data_start_ptr); |
zdshelby | 0:aafd54b05111 | 812 | |
zdshelby | 0:aafd54b05111 | 813 | /* The presence of a marker followed by a zero-length payload MUST be processed as a message format error */ |
zdshelby | 0:aafd54b05111 | 814 | if(dst_coap_msg_ptr->payload_len == 0) |
zdshelby | 0:aafd54b05111 | 815 | return -1; |
zdshelby | 0:aafd54b05111 | 816 | |
zdshelby | 0:aafd54b05111 | 817 | /* Parse Payload by setting CoAP message's payload_ptr to point Payload in Packet data */ |
zdshelby | 0:aafd54b05111 | 818 | dst_coap_msg_ptr->payload_ptr = *packet_data_pptr; |
zdshelby | 0:aafd54b05111 | 819 | } |
zdshelby | 0:aafd54b05111 | 820 | /* No payload marker.. */ |
zdshelby | 0:aafd54b05111 | 821 | else |
zdshelby | 0:aafd54b05111 | 822 | return -1; |
zdshelby | 0:aafd54b05111 | 823 | } |
zdshelby | 0:aafd54b05111 | 824 | return 0; |
zdshelby | 0:aafd54b05111 | 825 | } |
zdshelby | 0:aafd54b05111 | 826 | |
zdshelby | 0:aafd54b05111 | 827 | |
zdshelby | 0:aafd54b05111 | 828 | /** |
zdshelby | 0:aafd54b05111 | 829 | * \fn void sn_coap_packet_debug(sn_coap_hdr_s *coap_packet_ptr) |
zdshelby | 0:aafd54b05111 | 830 | * |
zdshelby | 0:aafd54b05111 | 831 | * \brief Parses CoAP message and prints parts for debugging |
zdshelby | 0:aafd54b05111 | 832 | * |
zdshelby | 0:aafd54b05111 | 833 | * \param *coap_packet_ptr Pointer to the CoAP message to debug |
zdshelby | 0:aafd54b05111 | 834 | * |
zdshelby | 0:aafd54b05111 | 835 | *****************************************************************************/ |
zdshelby | 0:aafd54b05111 | 836 | |
zdshelby | 0:aafd54b05111 | 837 | void sn_coap_packet_debug(sn_coap_hdr_s *coap_packet_ptr) |
zdshelby | 0:aafd54b05111 | 838 | { |
zdshelby | 0:aafd54b05111 | 839 | #ifdef HAVE_DEBUG |
zdshelby | 0:aafd54b05111 | 840 | switch (coap_packet_ptr->msg_type) |
zdshelby | 0:aafd54b05111 | 841 | { |
zdshelby | 0:aafd54b05111 | 842 | case COAP_MSG_TYPE_CONFIRMABLE: |
zdshelby | 0:aafd54b05111 | 843 | printf("con "); |
zdshelby | 0:aafd54b05111 | 844 | break; |
zdshelby | 0:aafd54b05111 | 845 | |
zdshelby | 0:aafd54b05111 | 846 | case COAP_MSG_TYPE_NON_CONFIRMABLE: |
zdshelby | 0:aafd54b05111 | 847 | printf("non "); |
zdshelby | 0:aafd54b05111 | 848 | break; |
zdshelby | 0:aafd54b05111 | 849 | |
zdshelby | 0:aafd54b05111 | 850 | case COAP_MSG_TYPE_ACKNOWLEDGEMENT: |
zdshelby | 0:aafd54b05111 | 851 | printf("ack "); |
zdshelby | 0:aafd54b05111 | 852 | break; |
zdshelby | 0:aafd54b05111 | 853 | |
zdshelby | 0:aafd54b05111 | 854 | case COAP_MSG_TYPE_RESET: |
zdshelby | 0:aafd54b05111 | 855 | printf("rst "); |
zdshelby | 0:aafd54b05111 | 856 | break; |
zdshelby | 0:aafd54b05111 | 857 | } |
zdshelby | 0:aafd54b05111 | 858 | |
zdshelby | 0:aafd54b05111 | 859 | switch (coap_packet_ptr->msg_code) |
zdshelby | 0:aafd54b05111 | 860 | { |
zdshelby | 0:aafd54b05111 | 861 | case COAP_MSG_CODE_EMPTY: |
zdshelby | 0:aafd54b05111 | 862 | printf("NO CODE "); |
zdshelby | 0:aafd54b05111 | 863 | break; |
zdshelby | 0:aafd54b05111 | 864 | |
zdshelby | 0:aafd54b05111 | 865 | case COAP_MSG_CODE_REQUEST_GET: |
zdshelby | 0:aafd54b05111 | 866 | printf("GET "); |
zdshelby | 0:aafd54b05111 | 867 | break; |
zdshelby | 0:aafd54b05111 | 868 | |
zdshelby | 0:aafd54b05111 | 869 | case COAP_MSG_CODE_REQUEST_POST: |
zdshelby | 0:aafd54b05111 | 870 | printf("POST "); |
zdshelby | 0:aafd54b05111 | 871 | break; |
zdshelby | 0:aafd54b05111 | 872 | |
zdshelby | 0:aafd54b05111 | 873 | case COAP_MSG_CODE_REQUEST_PUT: |
zdshelby | 0:aafd54b05111 | 874 | printf("PUT "); |
zdshelby | 0:aafd54b05111 | 875 | break; |
zdshelby | 0:aafd54b05111 | 876 | |
zdshelby | 0:aafd54b05111 | 877 | case COAP_MSG_CODE_REQUEST_DELETE: |
zdshelby | 0:aafd54b05111 | 878 | printf("DELETE "); |
zdshelby | 0:aafd54b05111 | 879 | break; |
zdshelby | 0:aafd54b05111 | 880 | |
zdshelby | 0:aafd54b05111 | 881 | case COAP_MSG_CODE_RESPONSE_CREATED: |
zdshelby | 0:aafd54b05111 | 882 | printf("2.01 Created "); |
zdshelby | 0:aafd54b05111 | 883 | break; |
zdshelby | 0:aafd54b05111 | 884 | |
zdshelby | 0:aafd54b05111 | 885 | case COAP_MSG_CODE_RESPONSE_DELETED: |
zdshelby | 0:aafd54b05111 | 886 | printf("2.02 Deleted "); |
zdshelby | 0:aafd54b05111 | 887 | break; |
zdshelby | 0:aafd54b05111 | 888 | |
zdshelby | 0:aafd54b05111 | 889 | case COAP_MSG_CODE_RESPONSE_VALID: |
zdshelby | 0:aafd54b05111 | 890 | printf("2.03 Valid "); |
zdshelby | 0:aafd54b05111 | 891 | break; |
zdshelby | 0:aafd54b05111 | 892 | |
zdshelby | 0:aafd54b05111 | 893 | case COAP_MSG_CODE_RESPONSE_CHANGED: |
zdshelby | 0:aafd54b05111 | 894 | printf("2.04 Changed "); |
zdshelby | 0:aafd54b05111 | 895 | break; |
zdshelby | 0:aafd54b05111 | 896 | |
zdshelby | 0:aafd54b05111 | 897 | case COAP_MSG_CODE_RESPONSE_CONTENT: |
zdshelby | 0:aafd54b05111 | 898 | printf("2.05 Content "); |
zdshelby | 0:aafd54b05111 | 899 | break; |
zdshelby | 0:aafd54b05111 | 900 | |
zdshelby | 0:aafd54b05111 | 901 | case COAP_MSG_CODE_RESPONSE_BAD_REQUEST: |
zdshelby | 0:aafd54b05111 | 902 | printf("4.00 Bad Request "); |
zdshelby | 0:aafd54b05111 | 903 | break; |
zdshelby | 0:aafd54b05111 | 904 | |
zdshelby | 0:aafd54b05111 | 905 | case COAP_MSG_CODE_RESPONSE_UNAUTHORIZED: |
zdshelby | 0:aafd54b05111 | 906 | printf("4.01 Unauthorized "); |
zdshelby | 0:aafd54b05111 | 907 | break; |
zdshelby | 0:aafd54b05111 | 908 | |
zdshelby | 0:aafd54b05111 | 909 | case COAP_MSG_CODE_RESPONSE_BAD_OPTION: |
zdshelby | 0:aafd54b05111 | 910 | printf("4.02 Bad Option "); |
zdshelby | 0:aafd54b05111 | 911 | break; |
zdshelby | 0:aafd54b05111 | 912 | |
zdshelby | 0:aafd54b05111 | 913 | case COAP_MSG_CODE_RESPONSE_FORBIDDEN: |
zdshelby | 0:aafd54b05111 | 914 | printf("4.03 Forbidden "); |
zdshelby | 0:aafd54b05111 | 915 | break; |
zdshelby | 0:aafd54b05111 | 916 | |
zdshelby | 0:aafd54b05111 | 917 | case COAP_MSG_CODE_RESPONSE_NOT_FOUND: |
zdshelby | 0:aafd54b05111 | 918 | printf("4.04 Not Found "); |
zdshelby | 0:aafd54b05111 | 919 | break; |
zdshelby | 0:aafd54b05111 | 920 | |
zdshelby | 0:aafd54b05111 | 921 | case COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED: |
zdshelby | 0:aafd54b05111 | 922 | printf("4.05 Method Not Allowed "); |
zdshelby | 0:aafd54b05111 | 923 | break; |
zdshelby | 0:aafd54b05111 | 924 | |
zdshelby | 0:aafd54b05111 | 925 | case COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE: |
zdshelby | 0:aafd54b05111 | 926 | printf("4.06 Response Not Acceptable"); |
zdshelby | 0:aafd54b05111 | 927 | break; |
zdshelby | 0:aafd54b05111 | 928 | |
zdshelby | 0:aafd54b05111 | 929 | case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE: |
zdshelby | 0:aafd54b05111 | 930 | printf("4.08 Request Entity Incomplete "); |
zdshelby | 0:aafd54b05111 | 931 | break; |
zdshelby | 0:aafd54b05111 | 932 | |
zdshelby | 0:aafd54b05111 | 933 | case COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED: |
zdshelby | 0:aafd54b05111 | 934 | printf("4.12 Response Precondition Failed"); |
zdshelby | 0:aafd54b05111 | 935 | break; |
zdshelby | 0:aafd54b05111 | 936 | |
zdshelby | 0:aafd54b05111 | 937 | case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE: |
zdshelby | 0:aafd54b05111 | 938 | printf("4.13 Request Entity Too Large "); |
zdshelby | 0:aafd54b05111 | 939 | break; |
zdshelby | 0:aafd54b05111 | 940 | |
zdshelby | 0:aafd54b05111 | 941 | case COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT: |
zdshelby | 0:aafd54b05111 | 942 | printf("4.15 Unsupported Media Type "); |
zdshelby | 0:aafd54b05111 | 943 | break; |
zdshelby | 0:aafd54b05111 | 944 | |
zdshelby | 0:aafd54b05111 | 945 | case COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR: |
zdshelby | 0:aafd54b05111 | 946 | printf("5.00 Internal Server Error "); |
zdshelby | 0:aafd54b05111 | 947 | break; |
zdshelby | 0:aafd54b05111 | 948 | |
zdshelby | 0:aafd54b05111 | 949 | case COAP_MSG_CODE_RESPONSE_NOT_IMPLEMENTED: |
zdshelby | 0:aafd54b05111 | 950 | printf("5.01 Not Implemented "); |
zdshelby | 0:aafd54b05111 | 951 | break; |
zdshelby | 0:aafd54b05111 | 952 | |
zdshelby | 0:aafd54b05111 | 953 | case COAP_MSG_CODE_RESPONSE_BAD_GATEWAY: |
zdshelby | 0:aafd54b05111 | 954 | printf("5.02 Bad Gateway "); |
zdshelby | 0:aafd54b05111 | 955 | break; |
zdshelby | 0:aafd54b05111 | 956 | |
zdshelby | 0:aafd54b05111 | 957 | case COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE: |
zdshelby | 0:aafd54b05111 | 958 | printf("5.03 Service Unavailable "); |
zdshelby | 0:aafd54b05111 | 959 | break; |
zdshelby | 0:aafd54b05111 | 960 | |
zdshelby | 0:aafd54b05111 | 961 | case COAP_MSG_CODE_RESPONSE_GATEWAY_TIMEOUT: |
zdshelby | 0:aafd54b05111 | 962 | printf("5.04 Gateway Timeout "); |
zdshelby | 0:aafd54b05111 | 963 | break; |
zdshelby | 0:aafd54b05111 | 964 | |
zdshelby | 0:aafd54b05111 | 965 | case COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED: |
zdshelby | 0:aafd54b05111 | 966 | printf("5.05 Proxying Not Supported "); |
zdshelby | 0:aafd54b05111 | 967 | break; |
zdshelby | 0:aafd54b05111 | 968 | |
zdshelby | 0:aafd54b05111 | 969 | default: |
zdshelby | 0:aafd54b05111 | 970 | printf("UNKNOWN CODE "); |
zdshelby | 0:aafd54b05111 | 971 | break; |
zdshelby | 0:aafd54b05111 | 972 | } |
zdshelby | 0:aafd54b05111 | 973 | |
zdshelby | 0:aafd54b05111 | 974 | printf("mid=%i ", (int)(coap_packet_ptr->msg_id)); |
zdshelby | 0:aafd54b05111 | 975 | |
zdshelby | 0:aafd54b05111 | 976 | if (coap_packet_ptr->uri_path_ptr) |
zdshelby | 0:aafd54b05111 | 977 | { |
zdshelby | 0:aafd54b05111 | 978 | int i; |
zdshelby | 0:aafd54b05111 | 979 | printf("/"); |
zdshelby | 0:aafd54b05111 | 980 | for (i=0; i < coap_packet_ptr->uri_path_len; i++) printf("%c", (char)(coap_packet_ptr->uri_path_ptr[i])); |
zdshelby | 0:aafd54b05111 | 981 | if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->uri_query_ptr) |
zdshelby | 0:aafd54b05111 | 982 | { |
zdshelby | 0:aafd54b05111 | 983 | printf("?"); |
zdshelby | 0:aafd54b05111 | 984 | for (i=0; i < coap_packet_ptr->options_list_ptr->uri_query_len; i++) printf("%c", (char)(coap_packet_ptr->options_list_ptr->uri_query_ptr[i])); |
zdshelby | 0:aafd54b05111 | 985 | } |
zdshelby | 0:aafd54b05111 | 986 | printf(" "); |
zdshelby | 0:aafd54b05111 | 987 | } |
zdshelby | 0:aafd54b05111 | 988 | |
zdshelby | 0:aafd54b05111 | 989 | if (coap_packet_ptr->token_ptr) |
zdshelby | 0:aafd54b05111 | 990 | { |
zdshelby | 0:aafd54b05111 | 991 | int i; |
zdshelby | 0:aafd54b05111 | 992 | printf("token=0x"); |
zdshelby | 0:aafd54b05111 | 993 | for (i=0; i < coap_packet_ptr->token_len; i++) printf("%02x", (unsigned char)(coap_packet_ptr->token_ptr[i])); |
zdshelby | 0:aafd54b05111 | 994 | printf(" "); |
zdshelby | 0:aafd54b05111 | 995 | } |
zdshelby | 0:aafd54b05111 | 996 | |
zdshelby | 0:aafd54b05111 | 997 | if (coap_packet_ptr->content_type_ptr) |
zdshelby | 0:aafd54b05111 | 998 | { |
zdshelby | 0:aafd54b05111 | 999 | switch (coap_packet_ptr->content_type_ptr[0]) |
zdshelby | 0:aafd54b05111 | 1000 | { |
zdshelby | 0:aafd54b05111 | 1001 | case COAP_CT_TEXT_PLAIN: |
zdshelby | 0:aafd54b05111 | 1002 | printf("text/plain "); |
zdshelby | 0:aafd54b05111 | 1003 | break; |
zdshelby | 0:aafd54b05111 | 1004 | |
zdshelby | 0:aafd54b05111 | 1005 | case COAP_CT_LINK_FORMAT: |
zdshelby | 0:aafd54b05111 | 1006 | printf("application/link-format "); |
zdshelby | 0:aafd54b05111 | 1007 | break; |
zdshelby | 0:aafd54b05111 | 1008 | |
zdshelby | 0:aafd54b05111 | 1009 | case COAP_CT_XML: |
zdshelby | 0:aafd54b05111 | 1010 | printf("application/xml "); |
zdshelby | 0:aafd54b05111 | 1011 | break; |
zdshelby | 0:aafd54b05111 | 1012 | |
zdshelby | 0:aafd54b05111 | 1013 | case COAP_CT_OCTET_STREAM: |
zdshelby | 0:aafd54b05111 | 1014 | printf("application/octet-stream "); |
zdshelby | 0:aafd54b05111 | 1015 | break; |
zdshelby | 0:aafd54b05111 | 1016 | |
zdshelby | 0:aafd54b05111 | 1017 | case COAP_CT_EXI: |
zdshelby | 0:aafd54b05111 | 1018 | printf("application/exi "); |
zdshelby | 0:aafd54b05111 | 1019 | break; |
zdshelby | 0:aafd54b05111 | 1020 | |
zdshelby | 0:aafd54b05111 | 1021 | case COAP_CT_JSON: |
zdshelby | 0:aafd54b05111 | 1022 | printf("application/json "); |
zdshelby | 0:aafd54b05111 | 1023 | break; |
zdshelby | 0:aafd54b05111 | 1024 | default: |
zdshelby | 0:aafd54b05111 | 1025 | printf("uknown type (%i) ", (int)(coap_packet_ptr->content_type_ptr[0])); |
zdshelby | 0:aafd54b05111 | 1026 | break; |
zdshelby | 0:aafd54b05111 | 1027 | |
zdshelby | 0:aafd54b05111 | 1028 | } |
zdshelby | 0:aafd54b05111 | 1029 | } |
zdshelby | 0:aafd54b05111 | 1030 | |
zdshelby | 0:aafd54b05111 | 1031 | if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->location_path_ptr) |
zdshelby | 0:aafd54b05111 | 1032 | { |
zdshelby | 0:aafd54b05111 | 1033 | printf("Location: /"); |
zdshelby | 0:aafd54b05111 | 1034 | int i; |
zdshelby | 0:aafd54b05111 | 1035 | for (i=0; i < coap_packet_ptr->options_list_ptr->location_path_len; i++) printf("%c", (char)(coap_packet_ptr->options_list_ptr->location_path_ptr[i])); |
zdshelby | 0:aafd54b05111 | 1036 | printf(" "); |
zdshelby | 0:aafd54b05111 | 1037 | } |
zdshelby | 0:aafd54b05111 | 1038 | |
zdshelby | 0:aafd54b05111 | 1039 | if (coap_packet_ptr->payload_ptr) |
zdshelby | 0:aafd54b05111 | 1040 | { |
zdshelby | 0:aafd54b05111 | 1041 | int i; |
zdshelby | 0:aafd54b05111 | 1042 | printf("'"); |
zdshelby | 0:aafd54b05111 | 1043 | for (i=0; i < coap_packet_ptr->payload_len; i++) |
zdshelby | 0:aafd54b05111 | 1044 | printf("%c", *(coap_packet_ptr->payload_ptr + i)); |
zdshelby | 0:aafd54b05111 | 1045 | printf("' "); |
zdshelby | 0:aafd54b05111 | 1046 | } |
zdshelby | 0:aafd54b05111 | 1047 | |
zdshelby | 0:aafd54b05111 | 1048 | printf("\n"); |
zdshelby | 0:aafd54b05111 | 1049 | #endif |
zdshelby | 0:aafd54b05111 | 1050 | } |
zdshelby | 0:aafd54b05111 | 1051 |