Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-client-c by
sn_coap_header.h
00001 /* 00002 * Copyright (c) 2011-2015 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 /** 00018 * \file sn_coap_header.h 00019 * 00020 * \brief CoAP C-library User header interface header file 00021 */ 00022 00023 #ifndef SN_COAP_HEADER_H_ 00024 #define SN_COAP_HEADER_H_ 00025 00026 #ifdef __cplusplus 00027 extern "C" { 00028 #endif 00029 00030 /* Handle structure */ 00031 struct coap_s; 00032 00033 /* * * * * * * * * * * * * * */ 00034 /* * * * ENUMERATIONS * * * */ 00035 /* * * * * * * * * * * * * * */ 00036 00037 /** 00038 * \brief Enumeration for CoAP Version 00039 */ 00040 typedef enum coap_version_ { 00041 COAP_VERSION_1 = 0x40, 00042 COAP_VERSION_UNKNOWN = 0xFF 00043 } coap_version_e; 00044 00045 /** 00046 * \brief Enumeration for CoAP Message type, used in CoAP Header 00047 */ 00048 typedef enum sn_coap_msg_type_ { 00049 COAP_MSG_TYPE_CONFIRMABLE = 0x00, /**< Reliable Request messages */ 00050 COAP_MSG_TYPE_NON_CONFIRMABLE = 0x10, /**< Non-reliable Request and Response messages */ 00051 COAP_MSG_TYPE_ACKNOWLEDGEMENT = 0x20, /**< Response to a Confirmable Request */ 00052 COAP_MSG_TYPE_RESET = 0x30 /**< Answer a Bad Request */ 00053 } sn_coap_msg_type_e; 00054 00055 /** 00056 * \brief Enumeration for CoAP Message code, used in CoAP Header 00057 */ 00058 typedef enum sn_coap_msg_code_ { 00059 COAP_MSG_CODE_EMPTY = 0, 00060 COAP_MSG_CODE_REQUEST_GET = 1, 00061 COAP_MSG_CODE_REQUEST_POST = 2, 00062 COAP_MSG_CODE_REQUEST_PUT = 3, 00063 COAP_MSG_CODE_REQUEST_DELETE = 4, 00064 00065 COAP_MSG_CODE_RESPONSE_CREATED = 65, 00066 COAP_MSG_CODE_RESPONSE_DELETED = 66, 00067 COAP_MSG_CODE_RESPONSE_VALID = 67, 00068 COAP_MSG_CODE_RESPONSE_CHANGED = 68, 00069 COAP_MSG_CODE_RESPONSE_CONTENT = 69, 00070 COAP_MSG_CODE_RESPONSE_BAD_REQUEST = 128, 00071 COAP_MSG_CODE_RESPONSE_UNAUTHORIZED = 129, 00072 COAP_MSG_CODE_RESPONSE_BAD_OPTION = 130, 00073 COAP_MSG_CODE_RESPONSE_FORBIDDEN = 131, 00074 COAP_MSG_CODE_RESPONSE_NOT_FOUND = 132, 00075 COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED = 133, 00076 COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE = 134, 00077 COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE = 136, 00078 COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED = 140, 00079 COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE = 141, 00080 COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT = 143, 00081 COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR = 160, 00082 COAP_MSG_CODE_RESPONSE_NOT_IMPLEMENTED = 161, 00083 COAP_MSG_CODE_RESPONSE_BAD_GATEWAY = 162, 00084 COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE = 163, 00085 COAP_MSG_CODE_RESPONSE_GATEWAY_TIMEOUT = 164, 00086 COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED = 165 00087 } sn_coap_msg_code_e; 00088 00089 /** 00090 * \brief Enumeration for CoAP Option number, used in CoAP Header 00091 */ 00092 typedef enum sn_coap_option_numbers_ { 00093 COAP_OPTION_IF_MATCH = 1, 00094 COAP_OPTION_URI_HOST = 3, 00095 COAP_OPTION_ETAG = 4, 00096 COAP_OPTION_IF_NONE_MATCH = 5, 00097 COAP_OPTION_OBSERVE = 6, 00098 COAP_OPTION_URI_PORT = 7, 00099 COAP_OPTION_LOCATION_PATH = 8, 00100 COAP_OPTION_URI_PATH = 11, 00101 COAP_OPTION_CONTENT_FORMAT = 12, 00102 COAP_OPTION_MAX_AGE = 14, 00103 COAP_OPTION_URI_QUERY = 15, 00104 COAP_OPTION_ACCEPT = 17, 00105 COAP_OPTION_LOCATION_QUERY = 20, 00106 COAP_OPTION_BLOCK2 = 23, 00107 COAP_OPTION_BLOCK1 = 27, 00108 COAP_OPTION_PROXY_URI = 35, 00109 COAP_OPTION_PROXY_SCHEME = 39, 00110 COAP_OPTION_SIZE1 = 60 00111 // 128 = (Reserved) 00112 // 132 = (Reserved) 00113 // 136 = (Reserved) 00114 } sn_coap_option_numbers_e; 00115 00116 /** 00117 * \brief Enumeration for CoAP Content Format codes 00118 */ 00119 typedef enum sn_coap_content_format_ { 00120 COAP_CT_NONE = -1, 00121 COAP_CT_TEXT_PLAIN = 0, 00122 COAP_CT_LINK_FORMAT = 40, 00123 COAP_CT_XML = 41, 00124 COAP_CT_OCTET_STREAM = 42, 00125 COAP_CT_EXI = 47, 00126 COAP_CT_JSON = 50 00127 } sn_coap_content_format_e; 00128 00129 /** 00130 * \brief Enumeration for CoAP status, used in CoAP Header 00131 */ 00132 typedef enum sn_coap_status_ { 00133 COAP_STATUS_OK = 0, /**< Default value is OK */ 00134 COAP_STATUS_PARSER_ERROR_IN_HEADER = 1, /**< CoAP will send Reset message to invalid message sender */ 00135 COAP_STATUS_PARSER_DUPLICATED_MSG = 2, /**< CoAP will send Acknowledgement message to duplicated message sender */ 00136 COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING = 3, /**< User will get whole message after all message blocks received. 00137 User must release messages with this status. */ 00138 COAP_STATUS_PARSER_BLOCKWISE_ACK = 4, /**< Acknowledgement for sent Blockwise message received */ 00139 COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED = 5, /**< Blockwise message received but not supported by compiling switch */ 00140 COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED = 6, /**< Blockwise message fully received and returned to app. 00141 User must take care of releasing whole payload of the blockwise messages */ 00142 COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED = 7 /**< When re-transmissions have been done and ACK not received, CoAP library calls 00143 RX callback with this status */ 00144 } sn_coap_status_e; 00145 00146 00147 /* * * * * * * * * * * * * */ 00148 /* * * * STRUCTURES * * * */ 00149 /* * * * * * * * * * * * * */ 00150 00151 /** 00152 * \brief Structure for CoAP Options 00153 */ 00154 typedef struct sn_coap_options_list_ { 00155 00156 uint8_t max_age_len; /**< 0-4 bytes. */ 00157 uint8_t *max_age_ptr; /**< Must be set to NULL if not used */ 00158 00159 uint16_t proxy_uri_len; /**< 1-1034 bytes. */ 00160 uint8_t *proxy_uri_ptr; /**< Must be set to NULL if not used */ 00161 00162 uint8_t etag_len; /**< 1-8 bytes. Repeatable */ 00163 uint8_t *etag_ptr; /**< Must be set to NULL if not used */ 00164 00165 uint16_t uri_host_len; /**< 1-255 bytes. */ 00166 uint8_t *uri_host_ptr; /**< Must be set to NULL if not used */ 00167 00168 uint16_t location_path_len; /**< 0-255 bytes. Repeatable */ 00169 uint8_t *location_path_ptr; /**< Must be set to NULL if not used */ 00170 00171 uint8_t uri_port_len; /**< 0-2 bytes. */ 00172 uint8_t *uri_port_ptr; /**< Must be set to NULL if not used */ 00173 00174 uint16_t location_query_len; /**< 0-255 bytes. Repeatable */ 00175 uint8_t *location_query_ptr; /**< Must be set to NULL if not used */ 00176 00177 uint8_t observe; 00178 uint8_t observe_len; /**< 0-2 bytes. */ 00179 uint8_t *observe_ptr; /**< Must be set to NULL if not used */ 00180 00181 uint8_t accept_len; /**< 0-2 bytes. Repeatable */ 00182 uint8_t *accept_ptr; /**< Must be set to NULL if not used */ 00183 00184 uint16_t uri_query_len; /**< 1-255 bytes. Repeatable */ 00185 uint8_t *uri_query_ptr; /**< Must be set to NULL if not used */ 00186 00187 uint8_t block1_len; /**< 0-3 bytes. */ 00188 uint8_t *block1_ptr; /**< Not for User */ 00189 00190 uint8_t block2_len; /**< 0-3 bytes. */ 00191 uint8_t *block2_ptr; /**< Not for User */ 00192 } sn_coap_options_list_s; 00193 00194 00195 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 00196 /* !!! Main CoAP message struct !!! */ 00197 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 00198 00199 /** 00200 * \brief Main CoAP message struct 00201 */ 00202 typedef struct sn_coap_hdr_ { 00203 sn_coap_status_e coap_status; /**< Used for telling to User special cases when parsing message */ 00204 00205 /* * * * * * * * * * * */ 00206 /* * * * Header * * * */ 00207 /* * * * * * * * * * * */ 00208 00209 sn_coap_msg_type_e msg_type; /**< Confirmable, Non-Confirmable, Acknowledgement or Reset */ 00210 sn_coap_msg_code_e msg_code; /**< Empty: 0; Requests: 1-31; Responses: 64-191 */ 00211 uint16_t msg_id; /**< Message ID. Parser sets parsed message ID, builder sets message ID of built coap message */ 00212 00213 /* * * * * * * * * * * */ 00214 /* * * * Options * * * */ 00215 /* * * * * * * * * * * */ 00216 00217 /* Here are most often used Options */ 00218 00219 uint16_t uri_path_len; /**< 0-255 bytes. Repeatable. */ 00220 uint8_t *uri_path_ptr; /**< Must be set to NULL if not used. E.g: temp1/temp2 */ 00221 00222 uint8_t token_len; /**< 1-8 bytes. */ 00223 uint8_t *token_ptr; /**< Must be set to NULL if not used */ 00224 00225 uint8_t content_type_len; /**< 0-2 bytes. */ 00226 uint8_t *content_type_ptr; /**< Must be set to NULL if not used */ 00227 00228 /* Here are not so often used Options */ 00229 sn_coap_options_list_s *options_list_ptr; /**< Must be set to NULL if not used */ 00230 00231 /* * * * * * * * * * * */ 00232 /* * * * Payload * * * */ 00233 /* * * * * * * * * * * */ 00234 00235 uint16_t payload_len; /**< Must be set to zero if not used */ 00236 uint8_t *payload_ptr; /**< Must be set to NULL if not used */ 00237 } sn_coap_hdr_s; 00238 00239 /* * * * * * * * * * * * * * * * * * * * * * */ 00240 /* * * * EXTERNAL FUNCTION PROTOTYPES * * * */ 00241 /* * * * * * * * * * * * * * * * * * * * * * */ 00242 /** 00243 * \fn 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) 00244 * 00245 * \brief Parses CoAP message from given Packet data 00246 * 00247 * \param *handle Pointer to CoAP library handle 00248 * 00249 * \param packet_data_len is length of given Packet data to be parsed to CoAP message 00250 * 00251 * \param *packet_data_ptr is source for Packet data to be parsed to CoAP message 00252 * 00253 * \param *coap_version_ptr is destination for parsed CoAP specification version 00254 * 00255 * \return Return value is pointer to parsed CoAP message.\n 00256 * In following failure cases NULL is returned:\n 00257 * -Failure in given pointer (= NULL)\n 00258 * -Failure in memory allocation (malloc() returns NULL) 00259 */ 00260 extern 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); 00261 00262 /** 00263 * \fn void sn_coap_parser_release_allocated_coap_msg_mem(struct coap_s *handle, sn_coap_hdr_s *freed_coap_msg_ptr) 00264 * 00265 * \brief Releases memory of given CoAP message 00266 * 00267 * Note!!! Does not release Payload part 00268 * 00269 * \param *handle Pointer to CoAP library handle 00270 * 00271 * \param *freed_coap_msg_ptr is pointer to released CoAP message 00272 */ 00273 extern void sn_coap_parser_release_allocated_coap_msg_mem(struct coap_s *handle, sn_coap_hdr_s *freed_coap_msg_ptr); 00274 00275 /** 00276 * \fn int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr) 00277 * 00278 * \brief Builds an outgoing message buffer from a CoAP header structure. 00279 * 00280 * \param *dst_packet_data_ptr is pointer to allocated destination to built CoAP packet 00281 * 00282 * \param *src_coap_msg_ptr is pointer to source structure for building Packet data 00283 * 00284 * \return Return value is byte count of built Packet data. In failure cases:\n 00285 * -1 = Failure in given CoAP header structure\n 00286 * -2 = Failure in given pointer (= NULL) 00287 */ 00288 extern int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr); 00289 00290 /** 00291 * \fn uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr) 00292 * 00293 * \brief Calculates needed Packet data memory size for given CoAP message 00294 * 00295 * \param *src_coap_msg_ptr is pointer to data which needed Packet 00296 * data length is calculated 00297 * 00298 * \return Return value is count of needed memory as bytes for build Packet data 00299 * Null if failed 00300 */ 00301 extern uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr); 00302 00303 /** 00304 * \fn int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_size) 00305 * 00306 * \brief Builds an outgoing message buffer from a CoAP header structure. 00307 * 00308 * \param *dst_packet_data_ptr is pointer to allocated destination to built CoAP packet 00309 * 00310 * \param *src_coap_msg_ptr is pointer to source structure for building Packet data 00311 * 00312 * \param blockwise_payload_size Blockwise message maximum payload size 00313 * 00314 * \return Return value is byte count of built Packet data. In failure cases:\n 00315 * -1 = Failure in given CoAP header structure\n 00316 * -2 = Failure in given pointer (= NULL) 00317 */ 00318 extern int16_t sn_coap_builder_2(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size); 00319 00320 /** 00321 * \fn uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size) 00322 * 00323 * \brief Calculates needed Packet data memory size for given CoAP message 00324 * 00325 * \param *src_coap_msg_ptr is pointer to data which needed Packet 00326 * data length is calculated 00327 * \param blockwise_payload_size Blockwise message maximum payload size 00328 * 00329 * \return Return value is count of needed memory as bytes for build Packet data 00330 * Null if failed 00331 */ 00332 extern uint16_t sn_coap_builder_calc_needed_packet_data_size_2(sn_coap_hdr_s *src_coap_msg_ptr, uint16_t blockwise_payload_size); 00333 00334 /** 00335 * \fn sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code) 00336 * 00337 * \brief Prepares generic response packet from a request packet. This function allocates memory for the resulting sn_coap_hdr_s 00338 * 00339 * \param *handle Pointer to CoAP library handle 00340 * \param *coap_packet_ptr The request packet pointer 00341 * \param msg_code response messages code 00342 * 00343 * \return *coap_packet_ptr The allocated and pre-filled response packet pointer 00344 * NULL Error in parsing the request 00345 * 00346 */ 00347 extern sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code); 00348 00349 #ifdef __cplusplus 00350 } 00351 #endif 00352 00353 #endif /* SN_COAP_HEADER_H_ */
Generated on Wed Jul 13 2022 17:43:56 by
