NSDL C library

Dependents:   NSDL_HelloWorld_WiFi UbloxModemNanoServiceClient IOT-NSDL_HelloWorld LWM2M_NanoService_Ethernet ... more

Fork of nsdl_lib by Tero Heinonen

Note that use of this software requires acceptance of the Sensinode EULA: http://mbed.org/teams/Sensinode/code/nsdl_lib/wiki/EULA

Committer:
bogdanm
Date:
Tue Oct 15 12:43:59 2013 +0000
Revision:
7:050d0f54a0dc
Parent:
6:1caf76131c9a
Fixed guards in headers.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
terohoo 0:58c4f13c4b9a 1 /**
terohoo 0:58c4f13c4b9a 2 * \file sn_coap_header.h
terohoo 0:58c4f13c4b9a 3 *
terohoo 0:58c4f13c4b9a 4 * \brief CoAP C-library User header interface header file
terohoo 0:58c4f13c4b9a 5 *
terohoo 0:58c4f13c4b9a 6 * Created on: Jun 30, 2011
terohoo 0:58c4f13c4b9a 7 * Author: tero
terohoo 0:58c4f13c4b9a 8 *
terohoo 0:58c4f13c4b9a 9 * \note Supports draft-ietf-core-coap-18
terohoo 0:58c4f13c4b9a 10 */
terohoo 0:58c4f13c4b9a 11
terohoo 0:58c4f13c4b9a 12 #ifndef SN_COAP_HEADER_H_
terohoo 0:58c4f13c4b9a 13 #define SN_COAP_HEADER_H_
terohoo 0:58c4f13c4b9a 14
terohoo 0:58c4f13c4b9a 15 #ifdef __cplusplus
terohoo 0:58c4f13c4b9a 16 extern "C" {
terohoo 0:58c4f13c4b9a 17 #endif
terohoo 0:58c4f13c4b9a 18
terohoo 0:58c4f13c4b9a 19 /* * * * * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 20 /* * * * ENUMERATIONS * * * */
terohoo 0:58c4f13c4b9a 21 /* * * * * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 22
screamer 1:01d723824294 23 /**
screamer 3:d8cb1afd7285 24 * \brief Enumeration for CoAP Version
screamer 1:01d723824294 25 */
terohoo 0:58c4f13c4b9a 26 typedef enum coap_version_
terohoo 0:58c4f13c4b9a 27 {
terohoo 0:58c4f13c4b9a 28 COAP_VERSION_1 = 0x40,
terohoo 0:58c4f13c4b9a 29 COAP_VERSION_UNKNOWN = 0xFF
terohoo 0:58c4f13c4b9a 30 } coap_version_e;
terohoo 0:58c4f13c4b9a 31
screamer 1:01d723824294 32 /**
screamer 3:d8cb1afd7285 33 * \brief Enumeration for CoAP Message type, used in CoAP Header
screamer 1:01d723824294 34 */
terohoo 0:58c4f13c4b9a 35 typedef enum sn_coap_msg_type_
terohoo 0:58c4f13c4b9a 36 {
screamer 3:d8cb1afd7285 37 COAP_MSG_TYPE_CONFIRMABLE = 0x00, /**< Reliable Request messages */
screamer 3:d8cb1afd7285 38 COAP_MSG_TYPE_NON_CONFIRMABLE = 0x10, /**< Non-reliable Request and Response messages */
screamer 3:d8cb1afd7285 39 COAP_MSG_TYPE_ACKNOWLEDGEMENT = 0x20, /**< Response to a Confirmable Request */
screamer 3:d8cb1afd7285 40 COAP_MSG_TYPE_RESET = 0x30 /**< Answer a Bad Request */
terohoo 0:58c4f13c4b9a 41 } sn_coap_msg_type_e;
terohoo 0:58c4f13c4b9a 42
screamer 1:01d723824294 43 /**
screamer 3:d8cb1afd7285 44 * \brief Enumeration for CoAP Message code, used in CoAP Header
screamer 1:01d723824294 45 */
terohoo 0:58c4f13c4b9a 46 typedef enum sn_coap_msg_code_
terohoo 0:58c4f13c4b9a 47 {
terohoo 0:58c4f13c4b9a 48 COAP_MSG_CODE_EMPTY = 0,
terohoo 0:58c4f13c4b9a 49 COAP_MSG_CODE_REQUEST_GET = 1,
terohoo 0:58c4f13c4b9a 50 COAP_MSG_CODE_REQUEST_POST = 2,
terohoo 0:58c4f13c4b9a 51 COAP_MSG_CODE_REQUEST_PUT = 3,
terohoo 0:58c4f13c4b9a 52 COAP_MSG_CODE_REQUEST_DELETE = 4,
terohoo 0:58c4f13c4b9a 53
terohoo 0:58c4f13c4b9a 54 COAP_MSG_CODE_RESPONSE_CREATED = 65,
terohoo 0:58c4f13c4b9a 55 COAP_MSG_CODE_RESPONSE_DELETED = 66,
terohoo 0:58c4f13c4b9a 56 COAP_MSG_CODE_RESPONSE_VALID = 67,
terohoo 0:58c4f13c4b9a 57 COAP_MSG_CODE_RESPONSE_CHANGED = 68,
terohoo 0:58c4f13c4b9a 58 COAP_MSG_CODE_RESPONSE_CONTENT = 69,
terohoo 0:58c4f13c4b9a 59 COAP_MSG_CODE_RESPONSE_BAD_REQUEST = 128,
terohoo 0:58c4f13c4b9a 60 COAP_MSG_CODE_RESPONSE_UNAUTHORIZED = 129,
terohoo 0:58c4f13c4b9a 61 COAP_MSG_CODE_RESPONSE_BAD_OPTION = 130,
terohoo 0:58c4f13c4b9a 62 COAP_MSG_CODE_RESPONSE_FORBIDDEN = 131,
terohoo 0:58c4f13c4b9a 63 COAP_MSG_CODE_RESPONSE_NOT_FOUND = 132,
terohoo 0:58c4f13c4b9a 64 COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED = 133,
terohoo 0:58c4f13c4b9a 65 COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE = 134,
terohoo 5:da1db64e7fb9 66 COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE = 136,
terohoo 0:58c4f13c4b9a 67 COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED = 140,
terohoo 0:58c4f13c4b9a 68 COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE = 141,
terohoo 0:58c4f13c4b9a 69 COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT = 143,
terohoo 0:58c4f13c4b9a 70 COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR = 160,
terohoo 0:58c4f13c4b9a 71 COAP_MSG_CODE_RESPONSE_NOT_IMPLEMENTED = 161,
terohoo 0:58c4f13c4b9a 72 COAP_MSG_CODE_RESPONSE_BAD_GATEWAY = 162,
terohoo 0:58c4f13c4b9a 73 COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE = 163,
terohoo 0:58c4f13c4b9a 74 COAP_MSG_CODE_RESPONSE_GATEWAY_TIMEOUT = 164,
terohoo 0:58c4f13c4b9a 75 COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED = 165
terohoo 0:58c4f13c4b9a 76 } sn_coap_msg_code_e;
terohoo 0:58c4f13c4b9a 77
screamer 1:01d723824294 78 /**
screamer 3:d8cb1afd7285 79 * \brief Enumeration for CoAP Option number, used in CoAP Header
screamer 1:01d723824294 80 */
terohoo 0:58c4f13c4b9a 81 typedef enum sn_coap_option_numbers_
terohoo 0:58c4f13c4b9a 82 {
terohoo 0:58c4f13c4b9a 83 COAP_OPTION_IF_MATCH = 1,
terohoo 0:58c4f13c4b9a 84 COAP_OPTION_URI_HOST = 3,
terohoo 0:58c4f13c4b9a 85 COAP_OPTION_ETAG = 4,
terohoo 0:58c4f13c4b9a 86 COAP_OPTION_IF_NONE_MATCH = 5,
terohoo 0:58c4f13c4b9a 87 COAP_OPTION_OBSERVE = 6,
terohoo 0:58c4f13c4b9a 88 COAP_OPTION_URI_PORT = 7,
terohoo 0:58c4f13c4b9a 89 COAP_OPTION_LOCATION_PATH = 8,
terohoo 0:58c4f13c4b9a 90 COAP_OPTION_URI_PATH = 11,
terohoo 0:58c4f13c4b9a 91 COAP_OPTION_CONTENT_FORMAT = 12,
terohoo 0:58c4f13c4b9a 92 COAP_OPTION_MAX_AGE = 14,
terohoo 0:58c4f13c4b9a 93 COAP_OPTION_URI_QUERY = 15,
terohoo 0:58c4f13c4b9a 94 COAP_OPTION_ACCEPT = 17,
terohoo 0:58c4f13c4b9a 95 COAP_OPTION_LOCATION_QUERY = 20,
terohoo 0:58c4f13c4b9a 96 COAP_OPTION_BLOCK2 = 23,
terohoo 0:58c4f13c4b9a 97 COAP_OPTION_BLOCK1 = 27,
terohoo 0:58c4f13c4b9a 98 COAP_OPTION_PROXY_URI = 35,
terohoo 0:58c4f13c4b9a 99 COAP_OPTION_PROXY_SCHEME = 39,
terohoo 0:58c4f13c4b9a 100 COAP_OPTION_SIZE1 = 60
terohoo 0:58c4f13c4b9a 101 // 128 = (Reserved)
terohoo 0:58c4f13c4b9a 102 // 132 = (Reserved)
terohoo 0:58c4f13c4b9a 103 // 136 = (Reserved)
terohoo 0:58c4f13c4b9a 104 } sn_coap_option_numbers_e;
terohoo 0:58c4f13c4b9a 105
screamer 1:01d723824294 106 /**
screamer 3:d8cb1afd7285 107 * \brief Enumeration for CoAP Content Format codes
screamer 1:01d723824294 108 */
terohoo 0:58c4f13c4b9a 109 typedef enum sn_coap_content_format_
terohoo 0:58c4f13c4b9a 110 {
terohoo 0:58c4f13c4b9a 111 COAP_CT_NONE = -1,
terohoo 0:58c4f13c4b9a 112 COAP_CT_TEXT_PLAIN = 0,
terohoo 0:58c4f13c4b9a 113 COAP_CT_LINK_FORMAT = 40,
terohoo 0:58c4f13c4b9a 114 COAP_CT_XML = 41,
terohoo 0:58c4f13c4b9a 115 COAP_CT_OCTET_STREAM = 42,
terohoo 0:58c4f13c4b9a 116 COAP_CT_EXI = 47,
terohoo 0:58c4f13c4b9a 117 COAP_CT_JSON = 50,
terohoo 0:58c4f13c4b9a 118 } sn_coap_content_format_e;
terohoo 0:58c4f13c4b9a 119
screamer 1:01d723824294 120 /**
screamer 3:d8cb1afd7285 121 * \brief Enumeration for CoAP status, used in CoAP Header
screamer 1:01d723824294 122 */
terohoo 0:58c4f13c4b9a 123 typedef enum sn_coap_status_
terohoo 0:58c4f13c4b9a 124 {
screamer 1:01d723824294 125 COAP_STATUS_OK = 0, /**< Default value is OK */
screamer 1:01d723824294 126 COAP_STATUS_PARSER_ERROR_IN_HEADER = 1, /**< CoAP will send Reset message to invalid message sender */
screamer 1:01d723824294 127 COAP_STATUS_PARSER_DUPLICATED_MSG = 2, /**< CoAP will send Acknowledgement message to duplicated message sender */
screamer 1:01d723824294 128 COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING = 3, /**< User will get whole message after all message blocks received.
screamer 1:01d723824294 129 User must release messages with this status. */
screamer 1:01d723824294 130 COAP_STATUS_PARSER_BLOCKWISE_ACK = 4, /**< Acknowledgement for sent Blockwise message received */
screamer 1:01d723824294 131 COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED = 5, /**< Blockwise message received but not supported by compiling switch */
screamer 1:01d723824294 132 COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED = 6 /**< Blockwise message fully received and returned to app.
screamer 1:01d723824294 133 User must take care of releasing whole payload of the blockwise messages */
terohoo 0:58c4f13c4b9a 134 } sn_coap_status_e;
terohoo 0:58c4f13c4b9a 135
terohoo 0:58c4f13c4b9a 136
terohoo 0:58c4f13c4b9a 137 /* * * * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 138 /* * * * STRUCTURES * * * */
terohoo 0:58c4f13c4b9a 139 /* * * * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 140
screamer 1:01d723824294 141 /**
screamer 3:d8cb1afd7285 142 * \brief Structure for CoAP Options
screamer 1:01d723824294 143 */
terohoo 0:58c4f13c4b9a 144 typedef struct sn_coap_options_list_
terohoo 0:58c4f13c4b9a 145 {
terohoo 0:58c4f13c4b9a 146
screamer 1:01d723824294 147 uint8_t max_age_len; /**< 0-4 bytes. */
screamer 1:01d723824294 148 uint8_t *max_age_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 149
screamer 1:01d723824294 150 uint16_t proxy_uri_len; /**< 1-1034 bytes. */
screamer 1:01d723824294 151 uint8_t *proxy_uri_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 152
screamer 1:01d723824294 153 uint8_t etag_len; /**< 1-8 bytes. Repeatable */
screamer 1:01d723824294 154 uint8_t *etag_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 155
screamer 1:01d723824294 156 uint16_t uri_host_len; /**< 1-255 bytes. */
screamer 1:01d723824294 157 uint8_t *uri_host_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 158
screamer 1:01d723824294 159 uint16_t location_path_len; /**< 0-255 bytes. Repeatable */
screamer 1:01d723824294 160 uint8_t *location_path_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 161
screamer 1:01d723824294 162 uint8_t uri_port_len; /**< 0-2 bytes. */
screamer 1:01d723824294 163 uint8_t *uri_port_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 164
screamer 1:01d723824294 165 uint16_t location_query_len; /**< 0-255 bytes. Repeatable */
screamer 1:01d723824294 166 uint8_t *location_query_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 167
terohoo 0:58c4f13c4b9a 168 uint8_t observe;
screamer 1:01d723824294 169 uint8_t observe_len; /**< 0-2 bytes. */
screamer 1:01d723824294 170 uint8_t *observe_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 171
screamer 1:01d723824294 172 uint8_t accept_len; /**< 0-2 bytes. Repeatable */
screamer 1:01d723824294 173 uint8_t *accept_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 174
screamer 1:01d723824294 175 uint16_t uri_query_len; /**< 1-255 bytes. Repeatable */
screamer 1:01d723824294 176 uint8_t *uri_query_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 177
screamer 1:01d723824294 178 uint8_t block1_len; /**< 0-3 bytes. */
screamer 1:01d723824294 179 uint8_t *block1_ptr; /**< Not for User */
terohoo 0:58c4f13c4b9a 180
screamer 1:01d723824294 181 uint8_t block2_len; /**< 0-3 bytes. */
screamer 1:01d723824294 182 uint8_t *block2_ptr; /**< Not for User */
terohoo 0:58c4f13c4b9a 183 } sn_coap_options_list_s;
terohoo 0:58c4f13c4b9a 184
screamer 1:01d723824294 185
terohoo 0:58c4f13c4b9a 186 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
terohoo 0:58c4f13c4b9a 187 /* !!! Main CoAP message struct !!! */
terohoo 0:58c4f13c4b9a 188 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
screamer 1:01d723824294 189
screamer 1:01d723824294 190 /**
screamer 3:d8cb1afd7285 191 * \brief Main CoAP message struct
screamer 1:01d723824294 192 */
terohoo 0:58c4f13c4b9a 193 typedef struct sn_coap_hdr_
terohoo 0:58c4f13c4b9a 194 {
screamer 1:01d723824294 195 sn_coap_status_e coap_status; /**< Used for telling to User special cases when parsing message */
terohoo 0:58c4f13c4b9a 196
terohoo 0:58c4f13c4b9a 197 /* * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 198 /* * * * Header * * * */
terohoo 0:58c4f13c4b9a 199 /* * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 200
screamer 1:01d723824294 201 sn_coap_msg_type_e msg_type; /**< Confirmable, Non-Confirmable, Acknowledgement or Reset */
screamer 1:01d723824294 202 sn_coap_msg_code_e msg_code; /**< Empty: 0; Requests: 1-31; Responses: 64-191 */
screamer 1:01d723824294 203 uint16_t msg_id; /**< Message ID. Parser sets parsed message ID, builder sets message ID of built coap message */
terohoo 0:58c4f13c4b9a 204
terohoo 0:58c4f13c4b9a 205 /* * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 206 /* * * * Options * * * */
terohoo 0:58c4f13c4b9a 207 /* * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 208
terohoo 0:58c4f13c4b9a 209 /* Here are most often used Options */
terohoo 0:58c4f13c4b9a 210
screamer 1:01d723824294 211 uint16_t uri_path_len; /**< 0-255 bytes. Repeatable. */
screamer 1:01d723824294 212 uint8_t *uri_path_ptr; /**< Must be set to NULL if not used. E.g: temp1/temp2 */
terohoo 0:58c4f13c4b9a 213
screamer 1:01d723824294 214 uint8_t token_len; /**< 1-8 bytes. */
screamer 1:01d723824294 215 uint8_t *token_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 216
screamer 1:01d723824294 217 uint8_t content_type_len; /**< 0-2 bytes. */
screamer 1:01d723824294 218 uint8_t *content_type_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 219
terohoo 0:58c4f13c4b9a 220 /* Here are not so often used Options */
screamer 1:01d723824294 221 sn_coap_options_list_s *options_list_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 222
terohoo 0:58c4f13c4b9a 223 /* * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 224 /* * * * Payload * * * */
terohoo 0:58c4f13c4b9a 225 /* * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 226
screamer 1:01d723824294 227 uint16_t payload_len; /**< Must be set to zero if not used */
screamer 1:01d723824294 228 uint8_t *payload_ptr; /**< Must be set to NULL if not used */
terohoo 0:58c4f13c4b9a 229 } sn_coap_hdr_s;
terohoo 0:58c4f13c4b9a 230
terohoo 0:58c4f13c4b9a 231 /* * * * * * * * * * * * * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 232 /* * * * EXTERNAL FUNCTION PROTOTYPES * * * */
terohoo 0:58c4f13c4b9a 233 /* * * * * * * * * * * * * * * * * * * * * * */
terohoo 0:58c4f13c4b9a 234
screamer 2:ab50a2ab6ec9 235 /**
screamer 3:d8cb1afd7285 236 * \brief Sets the memory allocation and deallocation functions the library will use, and must be called first.
screamer 2:ab50a2ab6ec9 237 */
terohoo 0:58c4f13c4b9a 238 extern void sn_coap_builder_and_parser_init(void* (*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void*));
screamer 2:ab50a2ab6ec9 239
screamer 2:ab50a2ab6ec9 240 /**
screamer 3:d8cb1afd7285 241 * \brief Parses an incoming message buffer to a CoAP header structure.
screamer 2:ab50a2ab6ec9 242 */
terohoo 0:58c4f13c4b9a 243 extern sn_coap_hdr_s *sn_coap_parser(uint16_t packet_data_len, uint8_t *packet_data_ptr, coap_version_e *coap_version_ptr);
screamer 2:ab50a2ab6ec9 244
screamer 2:ab50a2ab6ec9 245 /**
screamer 3:d8cb1afd7285 246 * \brief Releases any memory allocated by a CoAP message structure.
screamer 2:ab50a2ab6ec9 247 */
screamer 2:ab50a2ab6ec9 248 extern void sn_coap_parser_release_allocated_coap_msg_mem(sn_coap_hdr_s *freed_coap_msg_ptr);
screamer 2:ab50a2ab6ec9 249
screamer 2:ab50a2ab6ec9 250 /**
screamer 3:d8cb1afd7285 251 * \brief Builds an outgoing message buffer from a CoAP header structure.
screamer 2:ab50a2ab6ec9 252 */
terohoo 0:58c4f13c4b9a 253 extern int16_t sn_coap_builder(uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr);
screamer 2:ab50a2ab6ec9 254
screamer 2:ab50a2ab6ec9 255 /**
screamer 3:d8cb1afd7285 256 * \brief Calculates the needed message buffer size from a CoAP message structure.
screamer 2:ab50a2ab6ec9 257 */
terohoo 0:58c4f13c4b9a 258 extern uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_msg_ptr);
screamer 2:ab50a2ab6ec9 259
screamer 2:ab50a2ab6ec9 260 /**
screamer 3:d8cb1afd7285 261 * \brief Automates the building of a response to an incoming request.
screamer 2:ab50a2ab6ec9 262 */
terohoo 0:58c4f13c4b9a 263 extern sn_coap_hdr_s *sn_coap_build_response(sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code);
screamer 2:ab50a2ab6ec9 264
screamer 2:ab50a2ab6ec9 265 /**
terohoo 5:da1db64e7fb9 266 * \brief CoAP packet debugging.
screamer 2:ab50a2ab6ec9 267 */
terohoo 0:58c4f13c4b9a 268 extern void sn_coap_packet_debug(sn_coap_hdr_s *coap_packet_ptr);
terohoo 0:58c4f13c4b9a 269
terohoo 0:58c4f13c4b9a 270 #ifdef __cplusplus
terohoo 0:58c4f13c4b9a 271 }
terohoo 0:58c4f13c4b9a 272 #endif
bogdanm 7:050d0f54a0dc 273
bogdanm 7:050d0f54a0dc 274 #endif /* SN_COAP_HEADER_H_ */