coap_api.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /**
  2. * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file coap_api.h
  41. *
  42. * @defgroup iot_sdk_coap_api CoAP Application Programming Interface
  43. * @ingroup iot_sdk_coap
  44. * @{
  45. * @brief Public API of Nordic's CoAP implementation.
  46. *
  47. */
  48. #ifndef COAP_API_H__
  49. #define COAP_API_H__
  50. #include <stdint.h>
  51. #include "coap_transport.h"
  52. #include "coap_codes.h"
  53. #include "sdk_config.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**@defgroup COAP_CONTENT_TYPE_MASK Resource content type bitmask values
  58. * @{ */
  59. #define COAP_CT_MASK_PLAIN_TEXT 0x01 /**< Content type Plain text supported in the endpoint resource. */
  60. #define COAP_CT_MASK_CHARSET_UTF8 0x02 /**< Content type Charset-UTF8 supported in the endpoint resource. */
  61. #define COAP_CT_MASK_APP_LINK_FORMAT 0x04 /**< Content type Application/link-format supported in the endpoint resource. */
  62. #define COAP_CT_MASK_APP_XML 0x08 /**< Content type Application/xml supported in the endpoint resource. */
  63. #define COAP_CT_MASK_APP_OCTET_STREAM 0x10 /**< Content type Application/octet-stream supported in the endpoint resource. */
  64. #define COAP_CT_MASK_APP_EXI 0x20 /**< Content type Application/exi supported in the endpoint resource. */
  65. #define COAP_CT_MASK_APP_JSON 0x40 /**< Content type Application/json supported in the endpoint resource. */
  66. /**@} */
  67. /**@defgroup COAP_METHOD_PERMISSION Resource method permission bitmask values
  68. * @{ */
  69. #define COAP_PERM_NONE 0x0000 /**< Permission by default. Do not allow any method in the COAP/COAPS endpoint resource. */
  70. #define COAP_PERM_GET 0x0001 /**< Permission to allow GET method in the COAP endpoint resource. */
  71. #define COAP_PERM_POST 0x0002 /**< Permission to allow POST method in the COAP endpoint resource. */
  72. #define COAP_PERM_PUT 0x0004 /**< Permission to allow PUT method in the COAP endpoint resource. */
  73. #define COAP_PERM_DELETE 0x0008 /**< Permission to allow DELETE method in the COAP endpoint resource. */
  74. #define COAPS_PERM_GET 0x0010 /**< Permission to allow GET method in the COAPS endpoint resource. */
  75. #define COAPS_PERM_POST 0x0020 /**< Permission to allow POST method in the COAPS endpoint resource. */
  76. #define COAPS_PERM_PUT 0x0040 /**< Permission to allow PUT method in the COAPS endpoint resource. */
  77. #define COAPS_PERM_DELETE 0x0080 /**< Permission to allow DELETE method in the COAPS endpoint resource. */
  78. #define COAP_PERM_OBSERVE 0x0100 /**< Permission to allow OBSERVE of the endpoint resource. */
  79. /**@} */
  80. /**@cond */
  81. // Forward declare structs.
  82. typedef struct coap_message_t coap_message_t;
  83. typedef struct coap_resource_t coap_resource_t;
  84. /**@endcond */
  85. /**@brief Callback function to call upon undefined behaviour.
  86. *
  87. * @param[in] error_code Error code from CoAP module.
  88. * @param[in] p_message CoAP message processed when error ocoured. Could be NULL.
  89. */
  90. typedef void (*coap_error_callback_t)(uint32_t error_code, coap_message_t * p_message);
  91. /**@brief Callback function to be registered with CoAP messages.
  92. *
  93. * @param[in] status Response status. Possible status codes:
  94. * NRF_SUCCESS If response was successfully received,
  95. * COAP_TRANSMISSION_RESET_BY_PEER if a reset response was recieved or,
  96. * COAP_TRANSMISSION_TIMEOUT if transmission
  97. * @param[in] p_arg Miscellaneous pointer to application provided data that is associated with
  98. * the message.
  99. * @param[in] p_message Pointer to a CoAP Response message.
  100. */
  101. typedef void (*coap_response_callback_t)(uint32_t status, void * p_arg, coap_message_t * p_message);
  102. /**@brief Handler function for manually handling all incoming requests.
  103. *
  104. * @details If the function is set, the error code given back will trigger error messages
  105. * to be sent back by CoAP to indicate failure. Default error message will be 4.00
  106. * BAD REQUEST. On success, it is expected that the callback has already sent a
  107. * response message.
  108. *
  109. * @param[in] p_request Pointer to a CoAP Request message.
  110. *
  111. * @retval NRF_SUCCESS If the message was successfully has been handled.
  112. * @retval NRF_ERROR_NOT_FOUND If the message did not match any recognized resources, and a
  113. * 4.04 NOT FOUND error message should be sent back to the requester.
  114. * @retval NRF_ERROR_NULL If the message resolved the resource and operation not permitted,
  115. * and a 4.05 METHOD NOT ALLOWED error message should be sent back to
  116. * the reqester.
  117. *
  118. */
  119. typedef uint32_t (*coap_request_handler_t)(coap_message_t * p_request);
  120. #ifdef COAP_AUTOMODE
  121. /**@brief Callback function to be registered with CoAP endpoint resources. in auto-mode.
  122. *
  123. * @details The callback needs to implement any action based on the request. The p_response message
  124. * will automatically be sent as response when the callback function returns. The memory
  125. * is allocated by the caller, so the application does not have to free up the memory used
  126. * for the response.
  127. *
  128. * @param[in] p_resource Pointer to the request message's target resource.
  129. * @param[in] p_request Pointer to the request message.
  130. * @param[out] p_response Pointer to the prepared response message. The Application can override
  131. * its values.
  132. */
  133. typedef void (*coap_method_callback_t) (coap_resource_t * p_resource, coap_message_t * p_request, coap_message_t * p_response);
  134. #else // COAP_AUTOMODE
  135. /**@brief Callback function to be registered with CoAP endpoint resources. in auto-mode.
  136. *
  137. * @details The callback needs to implement any action based on the request. The callback is
  138. * responsible of handling the sending of any response back to the requester. The memory
  139. * for p_request will be freed up by the coap module after the callback has been
  140. * completed.
  141. *
  142. * @param[in] p_resource Pointer to the request message's target resource.
  143. * @param[in] p_request Pointer to the request message.
  144. */
  145. typedef void (*coap_method_callback_t) (coap_resource_t * p_resource, coap_message_t * p_request);
  146. #endif // COAP_AUTOMODE
  147. /**@brief Enumeration of CoAP content types. */
  148. typedef enum
  149. {
  150. COAP_CT_PLAIN_TEXT = 0, /**< Plain text content format number. Default. */
  151. COAP_CT_APP_LINK_FORMAT = 40, /**< Application/link-format content format number. */
  152. COAP_CT_APP_XML = 41, /**< Application/xml content format number. */
  153. COAP_CT_APP_OCTET_STREAM = 42, /**< Application/octet-stream content format number. */
  154. COAP_CT_APP_EXI = 47, /**< Application/exi content format number. */
  155. COAP_CT_APP_JSON = 50 /**< Application/json content format number. */
  156. } coap_content_type_t;
  157. /**@brief Enumeration of CoAP options numbers. */
  158. #define COAP_OPT_RESERVED0 0 /**< Reserved option number. */
  159. #define COAP_OPT_IF_MATCH 1 /**< If-Match option number. */
  160. #define COAP_OPT_URI_HOST 3 /**< URI-Host option number. */
  161. #define COAP_OPT_ETAG 4 /**< ETag option number. */
  162. #define COAP_OPT_IF_NONE_MATCH 5 /**< If-None-Match option number. */
  163. #define COAP_OPT_URI_PORT 7 /**< URI-Port option number. */
  164. #define COAP_OPT_LOCATION_PATH 8 /**< Location-Path option number. */
  165. #define COAP_OPT_URI_PATH 11 /**< URI-Path option number. */
  166. #define COAP_OPT_CONTENT_FORMAT 12 /**< Content-Format option number. */
  167. #define COAP_OPT_MAX_AGE 14 /**< Max-Age option number. */
  168. #define COAP_OPT_URI_QUERY 15 /**< URI-Query option number. */
  169. #define COAP_OPT_ACCEPT 17 /**< Accept option number. */
  170. #define COAP_OPT_LOCATION_QUERY 20 /**< Location-Query option number. */
  171. #define COAP_OPT_BLOCK2 23 /**< Block2 option number. */
  172. #define COAP_OPT_BLOCK1 27 /**< Block1 option number. */
  173. #define COAP_OPT_SIZE2 28 /**< Size2 option number. */
  174. #define COAP_OPT_PROXY_URI 35 /**< Proxy-URI option number. */
  175. #define COAP_OPT_PROXY_SCHEME 39 /**< Proxy-Scheme option number. */
  176. #define COAP_OPT_SIZE1 60 /**< Size1 option number. */
  177. #define COAP_OPT_RESERVED1 128 /**< Reserved option number. */
  178. #define COAP_OPT_RESERVED2 132 /**< Reserved option number. */
  179. #define COAP_OPT_RESERVED3 136 /**< Reserved option number. */
  180. #define COAP_OPT_RESERVED4 140 /**< Reserved option number. */
  181. /**@brief Enumeration of CoAP message types. */
  182. typedef enum
  183. {
  184. COAP_TYPE_CON = 0, /**< Confirmable Message type. */
  185. COAP_TYPE_NON, /**< Non-Confirmable Message type. */
  186. COAP_TYPE_ACK, /**< Acknowledge Message type. */
  187. COAP_TYPE_RST /**< Reset Message type. */
  188. } coap_msg_type_t;
  189. /**@brief Structure to hold a CoAP option.
  190. */
  191. typedef struct
  192. {
  193. uint16_t number; /**< Option number (including the extended delta value if any). */
  194. uint16_t length; /**< Option length (including the extended length value in any). */
  195. uint8_t * p_data; /**< Pointer to the memory where the data of the option is located. */
  196. } coap_option_t;
  197. /**@brief Structure to hold a CoAP message configuration.
  198. *
  199. * @details The structure is used when calling the \ref coap_message_new API function.
  200. * All data supplied will be copied to the created message.
  201. */
  202. typedef struct
  203. {
  204. coap_response_callback_t response_callback; /**< Callback function to be called when a response matching the token is identified. */
  205. uint8_t token[8]; /**< Message token. token_len must be set to indicate how many of the bytes should be used in the token. */
  206. uint8_t token_len; /**< Token size in bytes. */
  207. uint16_t id; /**< Message ID. If 0 is given, the library will replace this number with an autogenerated value. */
  208. coap_msg_type_t type; /**< Message type: COAP_TYPE_CON, COAP_TYPE_NON, COAP_TYPE_ACK, or COAP_TYPE_RST. */
  209. coap_msg_code_t code; /**< Message code (definitions found in coap_msg_code_t). */
  210. coap_port_t port; /**< Transport layer variable to associate the message with an underlying Transport Layer socket descriptor. */
  211. } coap_message_conf_t;
  212. /**@brief Structure to hold a CoAP message header.
  213. *
  214. * @details This structure holds the 4-byte mandatory CoAP header. The structure uses bitfields
  215. * to save memory footprint.
  216. */
  217. typedef struct
  218. {
  219. uint8_t version :2; /**< CoAP version number. The current specification RFC7252 mandates this to be version 1. The version number can be modified in sdk_config.h. */
  220. uint8_t type :2; /**< Message type: COAP_TYPE_CON, COAP_TYPE_NON, COAP_TYPE_ACK, or COAP_TYPE_RST. */
  221. uint8_t token_len :4; /**< Length of the message token. */
  222. uint8_t code; /**< Message code (definitions found in @ref coap_msg_code_t). */
  223. uint16_t id; /**< Message ID in little-endian format. Convertion to Network Byte Order will be handled by the library. */
  224. } coap_message_header_t;
  225. /**@brief Structure to hold a CoAP message.
  226. *
  227. * @details The CoAP message structure contains both internal and public members to
  228. * serialize and deserialize data supplied from the application to a byte buffer sent
  229. * over UDP. The message structure is used both in transmission and reception, which
  230. * makes it easy to handle in an application. Updating the message should be done
  231. * using the provided APIs, not by manually assigning new values to the members directly.
  232. * Reading the members, on the other hand, is fine.
  233. */
  234. struct coap_message_t
  235. {
  236. coap_remote_t remote; /**< Public. Structure containing address information and port number to the remote. */
  237. coap_remote_t local; /**< Public. Structure containing local destination address information and port number. */
  238. coap_message_header_t header; /**< Public. Header structure containing the mandatory CoAP 4-byte header fields. */
  239. uint8_t * p_payload; /**< Public. Pointer to the payload buffer in the message. */
  240. uint16_t payload_len; /**< Public. Size of the payload in the message. */
  241. uint8_t options_count; /**< Public. The number of options in the message. */
  242. coap_option_t options[COAP_MAX_NUMBER_OF_OPTIONS]; /**< Public. Array options in the message. */
  243. void * p_arg; /**< Public. Miscellaneous pointer to application provided data that is associated with the message. */
  244. coap_response_callback_t response_callback; /**< Internal. Function callback set by the application to be called when a response to this message is received. Should be set by the application through a configuration parameter. */
  245. uint8_t token[8]; /**< Internal. Array holding the variable-sized message token. Should be set by the application through a configuration parameter. */
  246. coap_port_t port; /**< Internal. Transport layer variable to associate the message with an underlying Transport Layer socket descriptor. */
  247. uint16_t options_len; /**< Internal. Length of the options including the mandatory header with extension bytes and option data. Accumulated every time a new options is added. */
  248. uint16_t options_offset; /**< Internal. Index to where the next option or payload can be added in the message's data buffer */
  249. uint16_t options_delta; /**< Internal. Current option number. Used to calculate the next option delta when adding new options to the message. */
  250. uint8_t * p_data; /**< Internal. Data buffer for adding dynamically sized options and payload. */
  251. uint16_t data_len; /**< Internal. Length of the provided data buffer for options and payload. */
  252. };
  253. /**@brief Structure to hold a CoAP endpoint resource.
  254. */
  255. struct coap_resource_t
  256. {
  257. uint8_t child_count; /**< Number of children in the linked list. */
  258. uint16_t permission; /**< Bitmask to tell which methods are permitted on the resource. Bit values available can be seen in \ref COAP_METHOD_PERMISSION. */
  259. coap_resource_t * p_sibling; /**< Sibling pointer to the next element in the list. */
  260. coap_resource_t * p_front; /**< Pointer to the beginning of the linked list. */
  261. coap_resource_t * p_tail; /**< Pointer to the last added child in the list. */
  262. coap_method_callback_t callback; /**< Callback to the resource handler. */
  263. uint32_t ct_support_mask; /**< Bitmask to tell which content types are supported by the resource. Bit values available can be seen in \ref COAP_CONTENT_TYPE_MASK. */
  264. uint32_t max_age; /**< Max age of resource endpoint value. */
  265. uint32_t expire_time; /**< Number of seconds until expire. */
  266. char name[COAP_RESOURCE_MAX_NAME_LEN+1]; /**< Name of the resource. Must be zero terminated. */
  267. };
  268. /**@brief Initializes the CoAP module.
  269. *
  270. * @details Initializes the library module and resets internal queues and resource registrations.
  271. *
  272. * @param[in] token_rand_seed Random number seed to be used to generate the token numbers.
  273. * @param[in] p_transport_params Pointer to transport parameters. Providing the list of ports
  274. * to be used by CoAP.
  275. *
  276. * @retval NRF_SUCCESS If initialization succeeded.
  277. */
  278. uint32_t coap_init(uint32_t token_rand_seed, coap_transport_init_t * p_transport_params);
  279. /**@brief Register error handler callback to the CoAP module.
  280. *
  281. * @param[in] error_callback Function to be called upon unknown messages and failure.
  282. *
  283. * @retval NRF_SUCCESS If registration was successful.
  284. */
  285. uint32_t coap_error_handler_register(coap_error_callback_t error_callback);
  286. /**@brief Register request handler which should handle all incoming requests.
  287. *
  288. * @details Setting this request handler redirects all requests to the application provided
  289. * callback routine. The callback handler might be cleared by NULL, making coap
  290. * module handle the requests and do resource lookup in order to process the
  291. * requests.
  292. *
  293. * @param[in] p_request_handler Function pointer to the provided request handler.
  294. *
  295. * @retval NRF_SUCCESS If registration was successful.
  296. */
  297. uint32_t coap_request_handler_register(coap_request_handler_t p_request_handler);
  298. /**@brief Sends a CoAP message.
  299. *
  300. * @details Sends out a request using the underlying transport layer. Before sending, the
  301. * \ref coap_message_t structure is serialized and added to an internal message queue
  302. * in the library. The handle returned can be used to abort the message from being
  303. * retransmitted at any time.
  304. *
  305. * @param[out] p_handle Handle to the message if CoAP CON/ACK messages has been used. Returned
  306. * by reference.
  307. * @param[in] p_message Message to be sent.
  308. *
  309. * @retval NRF_SUCCESS If the message was successfully encoded and scheduled for transmission.
  310. */
  311. uint32_t coap_message_send(uint32_t * p_handle, coap_message_t * p_message);
  312. /**@brief Abort a CoAP message.
  313. *
  314. * @details Aborts an ongoing transmission. If the message has not yet been sent, it will be
  315. * deleted from the message queue as well as stop any ongoing re-transmission of the
  316. * message.
  317. *
  318. * @param[in] handle Handle of the message to abort.
  319. *
  320. * @retval NRF_SUCCESS If the message was successfully aborted and removed from the
  321. * message queue.
  322. * @retval NRF_ERROR_NOT_FOUND If the message with the given handle was not located in the
  323. * message queue.
  324. */
  325. uint32_t coap_message_abort(uint32_t handle);
  326. /**@brief Creates CoAP message, initializes, and allocates the needed memory.
  327. *
  328. * @details Creates a CoAP message. This is an intermediate representation of the message,
  329. * because the message will be serialized by the library before it is transmitted. The structure
  330. * is verbose to facilitate configuring the message. Options, payload, and
  331. * remote address information can be added using API function calls.
  332. *
  333. * @param[inout] p_request Pointer to be filled by the allocated CoAP message structures.
  334. * @param[in] p_config Configuration for the message to be created. Manual configuration
  335. * can be performed after the message creation, except for the CLIENT port
  336. * association.
  337. *
  338. * @retval NRF_SUCCESS If the request was successfully allocated and initialized.
  339. * @retval NRF_ERROR_INVALID_PARAM If local port number was not configured.
  340. */
  341. uint32_t coap_message_new(coap_message_t ** p_request, coap_message_conf_t * p_config);
  342. /**@brief Deletes the CoAP request message.
  343. *
  344. * @details Frees up memory associated with the request message.
  345. *
  346. * @param[in] p_message Pointer to the request message to delete.
  347. */
  348. uint32_t coap_message_delete(coap_message_t * p_message);
  349. /**@brief Adds a payload to a CoAP message.
  350. *
  351. * @details Sets a data payload to a request or response message.
  352. *
  353. * This function must be called after all CoAP options have been added.
  354. * Due to internal buffers in the library, the payload will be added after any options
  355. * in the buffer. If an option is added after the payload, this option will over-write
  356. * the payload in the internal buffer.
  357. *
  358. * @param[inout] p_message Pointer to the message to add the payload to.
  359. * @param[in] p_payload Pointer to the payload to be added.
  360. * @param[in] payload_len Size of the payload to be added.
  361. *
  362. * @retval NRF_SUCCESS If the payload was successfully added to the message.
  363. * @retval NRF_ERROR_NO_MEM If the payload could not fit within the allocated payload memory
  364. * defined by sdk_config.h COAP_MESSAGE_DATA_MAX_SIZE.
  365. */
  366. uint32_t coap_message_payload_set(coap_message_t * p_message,
  367. void * p_payload,
  368. uint16_t payload_len);
  369. /**@brief Adds an empty CoAP option to the message.
  370. *
  371. * Option numbers must be in ascending order, adding the one with the smallest number
  372. * first and greatest last. If the order is incorrect, the delta number calculation will
  373. * result in an invalid or wrong delta number for the option.
  374. *
  375. * @param[inout] p_message Pointer to the message to add the option to. Should not be NULL.
  376. * @param[in] option_num The option number to add to the message.
  377. *
  378. * @retval NRF_SUCCESS If the empty option was successfully added to the message.
  379. * @retval NRF_ERROR_DATA_SIZE If the data exceeds the available message buffer data size.
  380. * @retval NRF_ERROR_NO_MEM If the maximum number of options that can be added to a message has been reached.
  381. */
  382. uint32_t coap_message_opt_empty_add(coap_message_t * p_message, uint16_t option_num);
  383. /**@brief Adds a uint CoAP option to the message.
  384. *
  385. * Option numbers must be in ascending order, adding the one with the smallest number
  386. * first and greatest last. If the order is incorrect, the delta number calculation will
  387. * result in an invalid or wrong delta number for the option.
  388. *
  389. * @param[inout] p_message Pointer to the message to add the option to. Should not be NULL.
  390. * @param[in] option_num The option number to add to the message.
  391. * @param[in] data An unsigned value (8-bit, 16-bit, or 32-bit) casted to uint32_t.
  392. * The value of the data is used to determine how many bytes
  393. * CoAP must use to represent this option value.
  394. *
  395. * @retval NRF_SUCCESS If the unsigned integer option was successfully added to the message.
  396. * @retval NRF_ERROR_DATA_SIZE If the data exceeds the available message buffer data size.
  397. * @retval NRF_ERROR_NO_MEM If the maximum number of options that can be added to a message has been reached.
  398. */
  399. uint32_t coap_message_opt_uint_add(coap_message_t * p_message, uint16_t option_num, uint32_t data);
  400. /**@brief Adds a string CoAP option to the message.
  401. *
  402. * Option numbers must be in ascending order, adding the one with the smallest number
  403. * first and greatest last. If the order is incorrect, the delta number calculation will
  404. * result in an invalid or wrong delta number for the option.
  405. *
  406. * @param[inout] p_message Pointer to the message to add the option to. Should not be NULL.
  407. * @param[in] option_num The option number to add to the message.
  408. * @param[in] p_data Pointer to a string buffer to be used as value for the option.
  409. * Should not be NULL.
  410. * @param[in] length Length of the string buffer provided.
  411. *
  412. * @retval NRF_SUCCESS If the string option was successfully added to the message.
  413. * @retval NRF_ERROR_DATA_SIZE If the data exceeds the available message buffer data size.
  414. * @retval NRF_ERROR_NO_MEM If the maximum number of options that can be added to a message has been reached.
  415. */
  416. uint32_t coap_message_opt_str_add(coap_message_t * p_message, uint16_t option_num, uint8_t * p_data, uint16_t length);
  417. /**@brief Adds an opaque CoAP option to the message.
  418. *
  419. * Option numbers must be in ascending order, adding the one with the smallest number
  420. * first and greatest last. If the order is incorrect, the delta number calculation will
  421. * result in an invalid or wrong delta number for the option.
  422. *
  423. * @param[inout] p_message Pointer to the message to add the option to. Should not be NULL.
  424. * @param[in] option_num The option number to add to the message.
  425. * @param[in] p_data Pointer to an opaque buffer to be used as value for the option.
  426. * Should not be NULL.
  427. * @param[in] length Length of the opaque buffer provided.
  428. *
  429. * @retval NRF_SUCCESS If the opaque option was successfully added to the message.
  430. * @retval NRF_ERROR_DATA_SIZE If the data exceeds the available message buffer data size.
  431. * @retval NRF_ERROR_NO_MEM If the maximum number of options that can be added to a message has been reached.
  432. */
  433. uint32_t coap_message_opt_opaque_add(coap_message_t * p_message, uint16_t option_num, uint8_t * p_data, uint16_t length);
  434. /**@brief Sets a remote address and port number to a CoAP message.
  435. *
  436. * @details Copies the content of the provided pointer into the CoAP message.
  437. *
  438. * @param[inout] p_message Pointer to the message to add the address information to.
  439. * Should not be NULL.
  440. * @param[in] p_address Pointer to a structure holding the address information for the remote server or client.
  441. * Should not be NULL.
  442. *
  443. * @retval NRF_SUCCESS When copying the content has finished.
  444. */
  445. uint32_t coap_message_remote_addr_set(coap_message_t * p_message, coap_remote_t * p_address);
  446. /**@brief Creates a CoAP endpoint resource.
  447. *
  448. * @details Initializes the \ref coap_resource_t members.
  449. *
  450. * The first resource that is created will be set as the root of the resource
  451. * hierarchy.
  452. *
  453. * @param[in] p_resource Pointer to coap_resource_t passed in by reference.
  454. * This variable must be stored in non-volatile memory.
  455. * Should not be NULL.
  456. * @param[in] name Verbose name of the service (zero-terminated
  457. * string). The maximum length of a name is defined
  458. * by COAP_RESOURCE_MAX_NAME_LEN in @c sdk_config.h
  459. * and can be adjusted if needed. Should not be NULL.
  460. * @retval NRF_ERROR_DATA_SIZE If the provided name is larger than the available name buffer.
  461. * @retval NRF_ERROR_NULL If the pointer to the resource or the provided
  462. * name buffer is NULL.
  463. */
  464. uint32_t coap_resource_create(coap_resource_t * p_resource, const char * name);
  465. /**@brief Adds a child resource.
  466. *
  467. * @details The hierarchy is constructed as a linked list with a maximum number of children.
  468. * COAP_RESOURCE_MAX_DEPTH in @c sdk_config.h sets the maximum depth. The maximum
  469. * number of children can be adjusted if more levels are needed.
  470. *
  471. * @param[in] p_parent Resource to attach the child to. Should not be NULL.
  472. * @param[in] p_child Child resource to attach. Should not be NULL.
  473. *
  474. * @retval NRF_SUCCESS If the child was successfully added.
  475. * @retval COAP_ERROR_MAX_DEPTH_REACHED If the child is exceeding the maximum depth defined.
  476. */
  477. uint32_t coap_resource_child_add(coap_resource_t * p_parent, coap_resource_t * p_child);
  478. /**@brief Generates .well-known/core string.
  479. *
  480. * @details This is a helper function for generating a CoRE link-format encoded string used for
  481. * CoAP discovery. The function traverse the resource hierarchy recursively.
  482. * The result will be resources listed in link-format. This function can be called when
  483. * all resources have been added by the application.
  484. *
  485. * @param[inout] string Buffer to use for the .well-known/core string. Should not be NULL.
  486. * @param[inout] length Length of the string buffer. Returns the used number of bytes from
  487. * the provided buffer.
  488. *
  489. * @retval NRF_SUCCESS If string generation was successful.
  490. * @retval NRF_ERROR_NULL If the string buffer was a NULL pointer.
  491. * @retval NRF_ERROR_DATA_SIZE If the size of the generated string exceeds the given buffer size.
  492. * @retval NRF_ERROR_INVALID_STATE If no resource has been registered.
  493. */
  494. uint32_t coap_resource_well_known_generate(uint8_t * string, uint16_t * length);
  495. /**@brief Get the root resource pointer.
  496. *
  497. * @param[out] pp_resource Pointer to be filled with pointer to the root resource.
  498. *
  499. * @retval NRF_SUCCESS If root resource was located.
  500. * @retval NRF_ERROR_NOT_FOUND If root resource was not located.
  501. * @retval NRF_ERROR_NULL If output pointer was NULL.
  502. */
  503. uint32_t coap_resource_root_get(coap_resource_t ** pp_resource);
  504. /**@brief Check whether a message contains a given CoAP Option.
  505. *
  506. * @param[in] p_message Pointer to the to check for the CoAP Option.
  507. * Should not be NULL.
  508. * @param[in] option CoAP Option to check for in the CoAP message.
  509. *
  510. * @retval NRF_SUCCESS If the CoAP Option is present in the message.
  511. * @retval NRF_ERROR_NULL If the pointer to the message is NULL.
  512. * @retval NRF_ERROR_NOT_FOUND If the CoAP Option is not present in the message.
  513. */
  514. uint32_t coap_message_opt_present(coap_message_t * p_message, uint16_t option);
  515. /**@brief Check whether a message contains a given CoAP Option and return the index of the entry
  516. * in the message option list.
  517. *
  518. * @param[in] p_index Value by reference to fill the resolved index into. Should not be NULL.
  519. * @param[in] p_message Pointer to the to check for the CoAP Option.
  520. * Should not be NULL.
  521. * @param[in] option CoAP Option to check for in the CoAP message.
  522. *
  523. * @retval NRF_SUCCESS If the CoAP Option is present in the message.
  524. * @retval NRF_ERROR_NULL If the pointer to the message or the p_index is NULL.
  525. * @retval NRF_ERROR_NOT_FOUND If the CoAP Option is not present in the message.
  526. */
  527. uint32_t coap_message_opt_index_get(uint8_t * p_index, coap_message_t * p_message, uint16_t option);
  528. /**@brief Find common content type between the CoAP message and the resource.
  529. *
  530. * @details The return value will be the first match between the ACCEPT options and the supported
  531. * content types in the resource. The priority is by content-format ID starting going
  532. * from the lowest value to the highest.
  533. *
  534. * @param[out] p_ct Resolved content type given by reference. Should not be NULL.
  535. * @param[in] p_message Pointer to the message. Should not be NULL.
  536. * @param[in] p_resource Pointer to the resource. Should not be NULL.
  537. *
  538. * @retval NRF_SUCCESS If match was found.
  539. * @retval NRF_ERROR_NOT_FOUND If no match was found.
  540. */
  541. uint32_t coap_message_ct_match_select(coap_content_type_t * p_ct, coap_message_t * p_message, coap_resource_t * p_resource);
  542. /**@brief CoAP time tick used for retransmitting any message in the queue if needed.
  543. *
  544. * @retval NRF_SUCCESS If time tick update was successfully handled.
  545. */
  546. uint32_t coap_time_tick(void);
  547. #if (COAP_DISABLE_DTLS_API == 0)
  548. /**@brief Setup secure DTLS session.
  549. *
  550. * @details For the client role, this API triggers a DTLS handshake. Until the handshake is complete
  551. * with the remote, \ref coap_message_send will fail.
  552. * For the server role, this API does not create any DTLS session. A DTLS session is
  553. * created each time a new client remote endpoint sends a request on the local port of the
  554. * server.
  555. *
  556. * @note The success of this function does not imply that the DTLS handshake is successfull.
  557. *
  558. * @note Only one DTLS session is permitted between a local and remote endpoint. Therefore, in case
  559. * a DTLS session was established between the local and remote endpoint, the existing DTLS
  560. * session will be reused irrespective of the role and number of times this API was called.
  561. * In case the application desires a fresh security setup, it must first call the
  562. * \ref coap_security_destroy to tear down the existing setup.
  563. *
  564. * @param[in] local_port Local port to bind the session to.
  565. * @param[in] role Role of the session. DTLS server or client defined in the enumeration
  566. * \ref nrf_tls_role_t.
  567. * @param[in] p_remote Pointer to a structure holding the address information for the remote
  568. * endpoint. If a the device is acting as a server, a NULL pointer shall be
  569. * given as a parameter. Rationale: The server is not envisioned to be
  570. * bound a pre-known client endpoint. Therefore, security server settings
  571. * shall be setup irrespective of the remote client.
  572. * @param[in] p_settings Pointer to a structure holding the DTLS credentials.
  573. *
  574. * @retval NRF_SUCCESS If setup of the secure DTLS session was successfull.
  575. */
  576. uint32_t coap_security_setup(uint16_t local_port,
  577. nrf_tls_role_t role,
  578. coap_remote_t * const p_remote,
  579. nrf_tls_key_settings_t * const p_settings);
  580. /**@brief Destroy a secure DTLS session.
  581. *
  582. * @details Terminate and clean up any session associated with the local port and the remote.
  583. *
  584. * @param[in] local_port Local port to unbind the session from.
  585. * @param[in] p_remote Pointer to a structure holding the address information for the remote
  586. * endpoint. Providing a NULL as p_remote will clean up all DTLS sessions
  587. * associated with the local port.
  588. *
  589. * @retval NRF_SUCCESS If the destruction of the secure DTLS session was successfull.
  590. */
  591. uint32_t coap_security_destroy(uint16_t local_port,
  592. coap_remote_t * const p_remote);
  593. #endif // COAP_DISABLE_DTLS_API
  594. /**@brief Process loop when using coap BSD socket transport implementation.
  595. *
  596. * @details This is blocking call. The function unblock is only
  597. * triggered upon an socket event registered to select() by coap transport.
  598. * This function must be called as often as possible in order to dispatch incoming
  599. * socket events. Preferred to be put in the application's main loop or similar.
  600. **/
  601. void coap_input(void);
  602. #ifdef __cplusplus
  603. }
  604. #endif
  605. #endif // COAP_API_H__
  606. /** @} */