joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sn_coap_header.h Source File

sn_coap_header.h

Go to the documentation of this file.
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_CONTINUE                     = 95,
00071     COAP_MSG_CODE_RESPONSE_BAD_REQUEST                  = 128,
00072     COAP_MSG_CODE_RESPONSE_UNAUTHORIZED                 = 129,
00073     COAP_MSG_CODE_RESPONSE_BAD_OPTION                   = 130,
00074     COAP_MSG_CODE_RESPONSE_FORBIDDEN                    = 131,
00075     COAP_MSG_CODE_RESPONSE_NOT_FOUND                    = 132,
00076     COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED           = 133,
00077     COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE               = 134,
00078     COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE    = 136,
00079     COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED          = 140,
00080     COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE     = 141,
00081     COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT   = 143,
00082     COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR        = 160,
00083     COAP_MSG_CODE_RESPONSE_NOT_IMPLEMENTED              = 161,
00084     COAP_MSG_CODE_RESPONSE_BAD_GATEWAY                  = 162,
00085     COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE          = 163,
00086     COAP_MSG_CODE_RESPONSE_GATEWAY_TIMEOUT              = 164,
00087     COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED       = 165
00088 } sn_coap_msg_code_e;
00089 
00090 /**
00091  * \brief Enumeration for CoAP Option number, used in CoAP Header
00092  */
00093 typedef enum sn_coap_option_numbers_ {
00094     COAP_OPTION_IF_MATCH        = 1,
00095     COAP_OPTION_URI_HOST        = 3,
00096     COAP_OPTION_ETAG            = 4,
00097     COAP_OPTION_IF_NONE_MATCH   = 5,
00098     COAP_OPTION_OBSERVE         = 6,
00099     COAP_OPTION_URI_PORT        = 7,
00100     COAP_OPTION_LOCATION_PATH   = 8,
00101     COAP_OPTION_URI_PATH        = 11,
00102     COAP_OPTION_CONTENT_FORMAT  = 12,
00103     COAP_OPTION_MAX_AGE         = 14,
00104     COAP_OPTION_URI_QUERY       = 15,
00105     COAP_OPTION_ACCEPT          = 17,
00106     COAP_OPTION_LOCATION_QUERY  = 20,
00107     COAP_OPTION_BLOCK2          = 23,
00108     COAP_OPTION_BLOCK1          = 27,
00109     COAP_OPTION_SIZE2           = 28,
00110     COAP_OPTION_PROXY_URI       = 35,
00111     COAP_OPTION_PROXY_SCHEME    = 39,
00112     COAP_OPTION_SIZE1           = 60
00113 //  128 =   (Reserved)
00114 //  132 =   (Reserved)
00115 //  136 =   (Reserved)
00116 } sn_coap_option_numbers_e;
00117 
00118 /**
00119  * \brief Enumeration for CoAP Content Format codes
00120  */
00121 typedef enum sn_coap_content_format_ {
00122     COAP_CT_NONE                = -1,
00123     COAP_CT_TEXT_PLAIN          = 0,
00124     COAP_CT_LINK_FORMAT         = 40,
00125     COAP_CT_XML                 = 41,
00126     COAP_CT_OCTET_STREAM        = 42,
00127     COAP_CT_EXI                 = 47,
00128     COAP_CT_JSON                = 50
00129 } sn_coap_content_format_e;
00130 
00131 /**
00132  * \brief Enumeration for CoAP status, used in CoAP Header
00133  */
00134 typedef enum sn_coap_status_ {
00135     COAP_STATUS_OK                             = 0, /**< Default value is OK */
00136     COAP_STATUS_PARSER_ERROR_IN_HEADER         = 1, /**< CoAP will send Reset message to invalid message sender */
00137     COAP_STATUS_PARSER_DUPLICATED_MSG          = 2, /**< CoAP will send Acknowledgement message to duplicated message sender */
00138     COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING = 3, /**< User will get whole message after all message blocks received.
00139                                                          User must release messages with this status. */
00140     COAP_STATUS_PARSER_BLOCKWISE_ACK           = 4, /**< Acknowledgement for sent Blockwise message received */
00141     COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED  = 5, /**< Blockwise message received but not supported by compiling switch */
00142     COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED  = 6, /**< Blockwise message fully received and returned to app.
00143                                                          User must take care of releasing whole payload of the blockwise messages */
00144     COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED = 7  /**< When re-transmissions have been done and ACK not received, CoAP library calls
00145                                                          RX callback with this status */
00146 } sn_coap_status_e;
00147 
00148 
00149 /* * * * * * * * * * * * * */
00150 /* * * * STRUCTURES  * * * */
00151 /* * * * * * * * * * * * * */
00152 
00153 /**
00154  * \brief Structure for CoAP Options
00155  */
00156 typedef struct sn_coap_options_list_ {
00157 
00158     uint8_t     max_age_len;        /**< 0-4 bytes. */
00159     uint8_t    *max_age_ptr;        /**< Must be set to NULL if not used */
00160 
00161     uint16_t    proxy_uri_len;      /**< 1-1034 bytes. */
00162     uint8_t    *proxy_uri_ptr;      /**< Must be set to NULL if not used */
00163 
00164     uint8_t     etag_len;           /**< 1-8 bytes. Repeatable */
00165     uint8_t    *etag_ptr;           /**< Must be set to NULL if not used */
00166 
00167     uint16_t    uri_host_len;       /**< 1-255 bytes. */
00168     uint8_t    *uri_host_ptr;       /**< Must be set to NULL if not used */
00169 
00170     uint16_t    location_path_len;  /**< 0-255 bytes. Repeatable */
00171     uint8_t    *location_path_ptr;  /**< Must be set to NULL if not used */
00172 
00173     uint8_t     uri_port_len;       /**< 0-2 bytes. */
00174     uint8_t    *uri_port_ptr;       /**< Must be set to NULL if not used */
00175 
00176     uint16_t    location_query_len; /**< 0-255 bytes. Repeatable */
00177     uint8_t    *location_query_ptr; /**< Must be set to NULL if not used */
00178 
00179     uint8_t     observe;
00180     uint8_t     observe_len;        /**< 0-2 bytes. */
00181     uint8_t    *observe_ptr;        /**< Must be set to NULL if not used */
00182 
00183     uint8_t     accept_len;         /**< 0-2 bytes. Repeatable */
00184     uint8_t     *accept_ptr;        /**< Must be set to NULL if not used */
00185 
00186     uint16_t    uri_query_len;      /**< 1-255 bytes. Repeatable */
00187     uint8_t    *uri_query_ptr;      /**< Must be set to NULL if not used */
00188 
00189     uint8_t     block1_len;         /**< 0-3 bytes. */
00190     uint8_t    *block1_ptr;         /**< Not for User */
00191 
00192     uint8_t     block2_len;         /**< 0-3 bytes. */
00193     uint8_t    *block2_ptr;         /**< Not for User */
00194 
00195     uint8_t     size1_len;         /**< 0-4 bytes. */
00196     uint8_t    *size1_ptr;         /**< Not for User */
00197 
00198     uint8_t     size2_len;         /**< 0-4 bytes. */
00199     uint8_t    *size2_ptr;         /**< Not for User */
00200 } sn_coap_options_list_s;
00201 
00202 
00203 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
00204 /* !!! Main CoAP message struct !!! */
00205 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
00206 
00207 /**
00208  * \brief Main CoAP message struct
00209  */
00210 typedef struct sn_coap_hdr_ {
00211     sn_coap_status_e        coap_status;        /**< Used for telling to User special cases when parsing message */
00212 
00213     /* * * * * * * * * * * */
00214     /* * * * Header  * * * */
00215     /* * * * * * * * * * * */
00216 
00217     sn_coap_msg_type_e      msg_type;           /**< Confirmable, Non-Confirmable, Acknowledgement or Reset */
00218     sn_coap_msg_code_e      msg_code;           /**< Empty: 0; Requests: 1-31; Responses: 64-191 */
00219     uint16_t                msg_id;             /**< Message ID. Parser sets parsed message ID, builder sets message ID of built coap message */
00220 
00221     /* * * * * * * * * * * */
00222     /* * * * Options * * * */
00223     /* * * * * * * * * * * */
00224 
00225     /* Here are most often used Options */
00226 
00227     uint16_t                uri_path_len;       /**< 0-255 bytes. Repeatable. */
00228     uint8_t                *uri_path_ptr;       /**< Must be set to NULL if not used. E.g: temp1/temp2 */
00229 
00230     uint8_t                 token_len;          /**< 1-8 bytes. */
00231     uint8_t                *token_ptr;          /**< Must be set to NULL if not used */
00232 
00233     uint8_t                 content_type_len;   /**< 0-2 bytes. */
00234     uint8_t                *content_type_ptr;   /**< Must be set to NULL if not used */
00235 
00236     /* Here are not so often used Options */
00237     sn_coap_options_list_s *options_list_ptr;   /**< Must be set to NULL if not used */
00238 
00239     /* * * * * * * * * * * */
00240     /* * * * Payload * * * */
00241     /* * * * * * * * * * * */
00242 
00243     uint16_t                payload_len;        /**< Must be set to zero if not used */
00244     uint8_t                *payload_ptr;        /**< Must be set to NULL if not used */
00245 } sn_coap_hdr_s;
00246 
00247 /* * * * * * * * * * * * * * * * * * * * * * */
00248 /* * * * EXTERNAL FUNCTION PROTOTYPES  * * * */
00249 /* * * * * * * * * * * * * * * * * * * * * * */
00250 /**
00251  * \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)
00252  *
00253  * \brief Parses CoAP message from given Packet data
00254  *
00255  * \param *handle Pointer to CoAP library handle
00256  *
00257  * \param packet_data_len is length of given Packet data to be parsed to CoAP message
00258  *
00259  * \param *packet_data_ptr is source for Packet data to be parsed to CoAP message
00260  *
00261  * \param *coap_version_ptr is destination for parsed CoAP specification version
00262  *
00263  * \return Return value is pointer to parsed CoAP message.\n
00264  *         In following failure cases NULL is returned:\n
00265  *          -Failure in given pointer (= NULL)\n
00266  *          -Failure in memory allocation (malloc() returns NULL)
00267  */
00268 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);
00269 
00270 /**
00271  * \fn void sn_coap_parser_release_allocated_coap_msg_mem(struct coap_s *handle, sn_coap_hdr_s *freed_coap_msg_ptr)
00272  *
00273  * \brief Releases memory of given CoAP message
00274  *
00275  *        Note!!! Does not release Payload part
00276  *
00277  * \param *handle Pointer to CoAP library handle
00278  *
00279  * \param *freed_coap_msg_ptr is pointer to released CoAP message
00280  */
00281 extern void sn_coap_parser_release_allocated_coap_msg_mem(struct coap_s *handle, sn_coap_hdr_s *freed_coap_msg_ptr);
00282 
00283 /**
00284  * \fn int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr)
00285  *
00286  * \brief Builds an outgoing message buffer from a CoAP header structure.
00287  *
00288  * \param *dst_packet_data_ptr is pointer to allocated destination to built CoAP packet
00289  *
00290  * \param *src_coap_msg_ptr is pointer to source structure for building Packet data
00291  *
00292  * \return Return value is byte count of built Packet data. In failure cases:\n
00293  *          -1 = Failure in given CoAP header structure\n
00294  *          -2 = Failure in given pointer (= NULL)
00295  */
00296 extern int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr);
00297 
00298 /**
00299  * \fn uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr)
00300  *
00301  * \brief Calculates needed Packet data memory size for given CoAP message
00302  *
00303  * \param *src_coap_msg_ptr is pointer to data which needed Packet
00304  *          data length is calculated
00305  *
00306  * \return Return value is count of needed memory as bytes for build Packet data
00307  *          Null if failed
00308  */
00309 extern uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr);
00310 
00311 /**
00312  * \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)
00313  *
00314  * \brief Builds an outgoing message buffer from a CoAP header structure.
00315  *
00316  * \param *dst_packet_data_ptr is pointer to allocated destination to built CoAP packet
00317  *
00318  * \param *src_coap_msg_ptr is pointer to source structure for building Packet data
00319  *
00320  * \param blockwise_payload_size Blockwise message maximum payload size
00321  *
00322  * \return Return value is byte count of built Packet data. In failure cases:\n
00323  *          -1 = Failure in given CoAP header structure\n
00324  *          -2 = Failure in given pointer (= NULL)
00325  */
00326 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);
00327 
00328 /**
00329  * \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)
00330  *
00331  * \brief Calculates needed Packet data memory size for given CoAP message
00332  *
00333  * \param *src_coap_msg_ptr is pointer to data which needed Packet
00334  *          data length is calculated
00335  * \param blockwise_payload_size Blockwise message maximum payload size
00336  *
00337  * \return Return value is count of needed memory as bytes for build Packet data
00338  *          Null if failed
00339  */
00340 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);
00341 
00342 /**
00343  * \fn sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code)
00344  *
00345  * \brief Prepares generic response packet from a request packet. This function allocates memory for the resulting sn_coap_hdr_s
00346  *
00347  * \param *handle Pointer to CoAP library handle
00348  * \param *coap_packet_ptr The request packet pointer
00349  * \param msg_code response messages code
00350  *
00351  * \return *coap_packet_ptr The allocated and pre-filled response packet pointer
00352  *          NULL    Error in parsing the request
00353  *
00354  */
00355 extern sn_coap_hdr_s *sn_coap_build_response(struct coap_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code);
00356 
00357 /**
00358  * \brief Initialise a message structure to empty
00359  *
00360  * \param *coap_msg_ptr is pointer to CoAP message to initialise
00361  *
00362  * \return Return value is pointer passed in
00363  */
00364 extern sn_coap_hdr_s *sn_coap_parser_init_message(sn_coap_hdr_s *coap_msg_ptr);
00365 
00366 /**
00367  * \brief Allocate an empty message structure
00368  *
00369  * \param *handle Pointer to CoAP library handle
00370  *
00371  * \return Return value is pointer to an empty CoAP message.\n
00372  *         In following failure cases NULL is returned:\n
00373  *          -Failure in given pointer (= NULL)\n
00374  *          -Failure in memory allocation (malloc() returns NULL)
00375  */
00376 extern sn_coap_hdr_s *sn_coap_parser_alloc_message(struct coap_s *handle);
00377 
00378 /**
00379  * \brief Allocate an empty options structure
00380  *
00381  * \param *handle Pointer to CoAP library handle
00382  * \param *coap_msg_ptr is pointer to CoAP message that will contain the options
00383  *
00384  * If the message already has a pointer to an option structure, that pointer
00385  * is returned, rather than a new structure being allocated.
00386  *
00387  * \return Return value is pointer to the CoAP options structure.\n
00388  *         In following failure cases NULL is returned:\n
00389  *          -Failure in given pointer (= NULL)\n
00390  *          -Failure in memory allocation (malloc() returns NULL)
00391  */
00392 extern sn_coap_options_list_s *sn_coap_parser_alloc_options(struct coap_s *handle, sn_coap_hdr_s *coap_msg_ptr);
00393 
00394 #ifdef __cplusplus
00395 }
00396 #endif
00397 
00398 #endif /* SN_COAP_HEADER_H_ */