lwm2m_api.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. * Copyright (c) 2015 - 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 lwm2m_api.h
  41. *
  42. * @defgroup iot_sdk_lwm2m_api LWM2M Application Programming Interface
  43. * @ingroup iot_sdk_lwm2m
  44. * @{
  45. * @brief Public API of Nordic's LWM2M implementation.
  46. */
  47. #ifndef LWM2M_API_H__
  48. #define LWM2M_API_H__
  49. #include <stdint.h>
  50. #include "lwm2m.h"
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /**@addtogroup LWM2M_opcodes Types
  55. * @{
  56. * @brief LWMW2M Bootstrap type definitions.
  57. */
  58. /**@brief LWM2M remote structure type. */
  59. typedef coap_remote_t lwm2m_remote_t;
  60. /**@brief LWM2M time type. */
  61. typedef uint32_t lwm2m_time_t;
  62. /**@brief LWM2M string type. */
  63. typedef struct
  64. {
  65. char * p_val; /**< Pointer to the value of the string data. */
  66. uint32_t len; /**< Length of p_val. */
  67. } lwm2m_string_t;
  68. /**@brief LWM2M opaque type. */
  69. typedef struct
  70. {
  71. uint8_t * p_val; /**< Pointer to the value of the opaque data. */
  72. uint32_t len; /**< Length of p_val. */
  73. } lwm2m_opaque_t;
  74. /**@brief Application notification callback types. */
  75. typedef enum
  76. {
  77. LWM2M_NOTIFCATION_TYPE_BOOTSTRAP, /**< Notification from a bootstrap request. */
  78. LWM2M_NOTIFCATION_TYPE_REGISTER, /**< Notification from a register request. */
  79. LWM2M_NOTIFCATION_TYPE_UPDATE, /**< Notification from a update request. */
  80. LWM2M_NOTIFCATION_TYPE_DEREGISTER /**< Notification from a deregister request. */
  81. } lwm2m_notification_type_t;
  82. /**@brief LWM2M server configuration type. */
  83. typedef struct
  84. {
  85. uint32_t lifetime; /** Lifetime parameter **/
  86. uint64_t msisdn; /** SMS number MSISDN **/
  87. uint8_t lwm2m_version_major; /** LWM2M major version number **/
  88. uint8_t lwm2m_version_minor; /** LWM2M miner version number **/
  89. lwm2m_string_t binding;
  90. } lwm2m_server_config_t;
  91. /**@brief LWM2M client identity types. */
  92. typedef enum
  93. {
  94. LWM2M_CLIENT_ID_TYPE_UUID = 36,
  95. LWM2M_CLIENT_ID_TYPE_IMEI = 15,
  96. LWM2M_CLIENT_ID_TYPE_ESN = 8,
  97. LWM2M_CLIENT_ID_TYPE_MEID = 14
  98. } lwm2m_client_identity_type_t;
  99. /**@brief LWM2M identity string.
  100. *
  101. * @details Using the string representation of UUID/OPS/OS/IMEI/ESN/MEID.
  102. *
  103. * @note: OPS- and OS URN are not currently supported.
  104. */
  105. typedef union
  106. {
  107. char uuid[36];
  108. char imei[15];
  109. char esn[8];
  110. char meid[14];
  111. } lwm2m_identity_string_t;
  112. /**@brief LWM2M client identity structure type. */
  113. typedef struct
  114. {
  115. lwm2m_identity_string_t value;
  116. lwm2m_client_identity_type_t type;
  117. } lwm2m_client_identity_t;
  118. /**@} */
  119. /**@addtogroup LWM2M_defines Defines
  120. * @{
  121. * @brief LWMW2M operation code and invalid object/instance definitions.
  122. */
  123. /**
  124. * @warning The invalid resource and instance are not stated by the lwm2m spec as reserved and will
  125. * cause issues if instances or resources with these IDs is added.
  126. */
  127. #define LWM2M_NAMED_OBJECT 65535 /**< Flag to indicate that the object does not use Integer as object id. */
  128. #define LWM2M_INVALID_RESOURCE 65535 /**< Invalid Resource ID. */
  129. #define LWM2M_INVALID_INSTANCE 65535 /**< Invalid Instance ID. */
  130. #define LWM2M_OPERATION_CODE_NONE 0x00 /**< Bit mask for LWM2M no operation. */
  131. #define LWM2M_OPERATION_CODE_READ 0x01 /**< Bit mask for LWM2M read operation. */
  132. #define LWM2M_OPERATION_CODE_WRITE 0x02 /**< Bit mask for LWM2M write operation. */
  133. #define LWM2M_OPERATION_CODE_EXECUTE 0x04 /**< Bit mask for LWM2M execute operation. */
  134. #define LWM2M_OPERATION_CODE_DELETE 0x08 /**< Bit mask for LWM2M delete operation. */
  135. #define LWM2M_OPERATION_CODE_CREATE 0x10 /**< Bit mask for LWM2M create operation. */
  136. #define LWM2M_OPERATION_CODE_DISCOVER 0x20 /**< Bit mask for LWM2M discover operation. */
  137. #define LWM2M_OPERATION_CODE_OBSERVE 0x40 /**< Bit mask for LWM2M observe operation. */
  138. /**@} */
  139. /**@cond */
  140. // Forward declare structs.
  141. typedef struct lwm2m_object_prototype_t lwm2m_object_prototype_t;
  142. typedef struct lwm2m_instance_prototype_t lwm2m_instance_prototype_t;
  143. /**@endcond */
  144. /**@brief Callback function upon requests on a given LWM2M resource instance.
  145. *
  146. * @details Will be called when the request is for an instance Ex. /0/1.
  147. *
  148. * If no instance could be located the object callback will be called.
  149. * The instance_id corresponds to the one in the URI-patch in the CoAP request and may be used to
  150. * create a new instance. If the value of resource_id is set to LWM2M_INVALID_RESOURCE the callback
  151. * should treated it as a call to the instance instead of a resource inside of the instance.
  152. *
  153. * If a resource has been found p_instance pointer will be set, else it will be NULL.
  154. *
  155. * @param[in] p_instance Pointer to the located resource if it already exists.
  156. * @param[in] resource_id Id of the resource requested.
  157. * @param[in] op_code Opcode of the request. Values of the opcodes are defined
  158. * in \ref LWM2M_opcodes.
  159. * @param[in] p_request Pointer to the CoAP request message.
  160. *
  161. * @retval NRF_SUCCESS Will always return success.
  162. */
  163. typedef uint32_t (*lwm2m_instance_callback_t)(lwm2m_instance_prototype_t * p_instance,
  164. uint16_t resource_id,
  165. uint8_t op_code,
  166. coap_message_t * p_request);
  167. /**@brief Callback function upon request on a given LWM2M object or instance create.
  168. *
  169. * @details Will be called when the request is for an object Ex: /0 or /0/1 an instance and the
  170. * op_code is CREATE. Depending on the CoAP request code the user might create an instance
  171. * or just return the tlv of current instances. If the value of instance_id is set to
  172. * LWM2M_INVALID_INSTANCE the callback should treated it as a call to the instance instead
  173. * of an instance of the object.
  174. *
  175. * @param[in] p_object Pointer to the located object.
  176. * @param[in] instance_id Id of the instance requested.
  177. * @param[in] op_code Opcode of the request. Values of the opcodes are defined
  178. * in \ref LWM2M_opcodes.
  179. * @param[in] p_request Pointer to the CoAP request message.
  180. *
  181. * @retval NRF_SUCCESS Will always return success.
  182. */
  183. typedef uint32_t (*lwm2m_object_callback_t)(lwm2m_object_prototype_t * p_object,
  184. uint16_t instance_id,
  185. uint8_t op_code,
  186. coap_message_t * p_request);
  187. /**@brief LWM2M object prototype structure.
  188. *
  189. * @details Each instance will have this structure in the front of its instance structure.
  190. * The object is used to have a common way of looking up its object id and callback
  191. * structure for each of the inherited. As there is no instance of the objects themselves,
  192. * the prototype is used as a meta object in order to have a common set of functions
  193. * for all instances of a object kind.
  194. */
  195. struct lwm2m_object_prototype_t
  196. {
  197. uint16_t object_id; /**< Identifies the object. */
  198. lwm2m_object_callback_t callback; /**< Called when for request to /0 (object) and /0/1 if instance 1 is not found. */
  199. char * p_alias_name; /**< Alternative name of the resource, used when LWM2M_NAMED_OBJECT is set. */
  200. };
  201. /**@brief LWM2M instance structure.
  202. *
  203. * @details Prototype for the instance object, this enables us to search through the instances
  204. * without knowing the type.
  205. */
  206. struct lwm2m_instance_prototype_t
  207. {
  208. uint16_t object_id; /**< Identifies what object this instance belongs to. */
  209. uint16_t instance_id; /**< Used to identify the instance. */
  210. uint16_t num_resources; /**< Number of resources MUST equal number of members in the lwm2m instance, sizeof resource_access and sizeof resource_ids. */
  211. uint8_t operations_offset; /**< Internal use. */
  212. uint8_t resource_ids_offset; /**< Internal use. */
  213. lwm2m_instance_callback_t callback; /**< Called when an operation is done on this instance. */
  214. };
  215. /**@brief Callback interface from the enabler interface (bootstrap/register) to the application.
  216. *
  217. * @warning This is an interface function. MUST BE IMPLEMENTED BY APPLICATION.
  218. *
  219. * @param[in] type Notification type. The types are defined in \ref lwm2m_notification_type_t.
  220. * @param[in] p_remote remote that answered the request
  221. * @param[in] coap_code coap op code from the response
  222. *
  223. * @retval NRF_SUCCESS If the client application handled the notification successfully.
  224. */
  225. uint32_t lwm2m_notification(lwm2m_notification_type_t type,
  226. lwm2m_remote_t * p_remote,
  227. uint8_t coap_code);
  228. /**@brief CoAP Request handler for the root of the object/instance/resource hierarchy.
  229. *
  230. * @details The function is called when a request is for the lwm2m root (ie no object instance
  231. * or resource).
  232. *
  233. * @warning This is an interface function. MUST BE IMPLEMENTED BY APPLICATION.
  234. *
  235. * @param[in] op_code LWM2M operation code.
  236. * @param[in] p_request Pointer to CoAP request message.
  237. *
  238. * @retval NRF_SUCCESS If the handler processed the request successfully.
  239. */
  240. uint32_t lwm2m_coap_handler_root(uint8_t op_code, coap_message_t * p_request);
  241. /**@brief Initialize LWM2M library.
  242. *
  243. * @retval NRF_SUCCESS If initialization was successful.
  244. */
  245. uint32_t lwm2m_init(void);
  246. /**@brief Send bootstrap request to a remote bootstrap server.
  247. *
  248. * @details Sends a bootstrap request with specified ID to the specified remote, calls the
  249. * lwm2m_notification with answer from the bootstrap server.
  250. *
  251. * @param[in] p_remote Pointer to the structure holding connection information of the remote
  252. * LWM2M bootstrap server.
  253. * @param[in] p_id Pointer to the structure holding the Id of the client.
  254. * @param[in] local_port Port number of the local port to be used to send the bootstrap request.
  255. *
  256. * @retval NRF_SUCCESS If bootstrap request to the LWM2M bootstrap server was sent successfully.
  257. * @retval NRF_ERROR_NULL If one of the parameters was a NULL pointer.
  258. */
  259. uint32_t lwm2m_bootstrap(lwm2m_remote_t * p_remote,
  260. lwm2m_client_identity_t * p_id,
  261. uint16_t local_port);
  262. /**@brief Register with a remote LWM2M server.
  263. *
  264. * @param[in] p_remote Pointer to the structure holding connection information
  265. * of the remote LWM2M server.
  266. * @param[in] p_id Pointer to the structure holding the Id of the client.
  267. * @param[in] p_config Registration parameters.
  268. * @param[in] local_port Port number of the local port to be used to send the
  269. * register request.
  270. * @param[in] p_link_format_string Pointer to a link format encoded string to send in the
  271. * register request.
  272. * @param[in] link_format_len Length of the link format string provided.
  273. *
  274. * @retval NRF_SUCCESS If registration request to the LWM2M server was sent out successfully.
  275. */
  276. uint32_t lwm2m_register(lwm2m_remote_t * p_remote,
  277. lwm2m_client_identity_t * p_id ,
  278. lwm2m_server_config_t * p_config,
  279. uint16_t local_port,
  280. uint8_t * p_link_format_string,
  281. uint16_t link_format_len);
  282. /**@brief Update a registration with a remote server.
  283. *
  284. * @param[in] p_remote Pointer to the structure holding connection information of the remote
  285. * LWM2M server.
  286. * @param[in] p_config Registration parameters.
  287. * @param[in] local_port Port number of the local port to be used to send the update request.
  288. *
  289. * @retval NRF_SUCCESS If update request to the LWM2M server was sent out successfully.
  290. */
  291. uint32_t lwm2m_update(lwm2m_remote_t * p_remote,
  292. lwm2m_server_config_t * p_config,
  293. uint16_t local_port);
  294. /**@brief Deregister from a remote server.
  295. *
  296. * @param[in] p_remote Pointer to the structure holding connection information of the remote
  297. * LWM2M server.
  298. * @param[in] local_port Port number of the local port to be used to send the deregister request.
  299. *
  300. * @retval NRF_SUCCESS If deregister request to the LWM2M server was sent out successfully.
  301. */
  302. uint32_t lwm2m_deregister(lwm2m_remote_t * p_remote, uint16_t local_port);
  303. /**@brief Add an instance to coap_handler in order to match requests to the given instance.
  304. *
  305. * @details Add a new LWM2M instance to the coap_handler. The application MUST initialize
  306. * and allocate the additional data in the struct.
  307. *
  308. * @param[in] p_instance Pointer to the instance to add.
  309. *
  310. * @retval NRF_SUCCESS If registration was successful.
  311. * @retval NRF_ERROR_NO_MEM If the module was not able to add the instance. Verify that
  312. * the LWM2M_COAP_HANDLER_MAX_INSTANCES setting in sdk_config.h
  313. * has a correct value.
  314. */
  315. uint32_t lwm2m_coap_handler_instance_add(lwm2m_instance_prototype_t * p_instance);
  316. /**@brief Delete an instance from coap_handler in order to stop matching requests to the given
  317. * instance.
  318. *
  319. * @param[in] p_instance Pointer to the instance to delete.
  320. *
  321. * @retval NRF_SUCCESS If unregistration was a success.
  322. * @retval NRF_ERROR_NOT_FOUND If the given instance was not located.
  323. */
  324. uint32_t lwm2m_coap_handler_instance_delete(lwm2m_instance_prototype_t * p_instance);
  325. /**@brief Add an object to coap_handler in order to match requests to the given object.
  326. *
  327. * @details Add a new LWM2M object to the coap_handler. The application MUST initialize
  328. * and allocate the additional data in the struct.
  329. *
  330. * @param[in] p_object Pointer to the object to add.
  331. *
  332. * @retval NRF_SUCCESS If registration was successful.
  333. * @retval NRF_ERROR_NO_MEM If the module was not able to add the object. Verify that
  334. * the LWM2M_COAP_HANDLER_MAX_OBJECTS setting in sdk_config.h
  335. * has a correct value.
  336. */
  337. uint32_t lwm2m_coap_handler_object_add(lwm2m_object_prototype_t * p_object);
  338. /**@brief Delete an object from coap_handler in order to stop matching requests to the given
  339. * object.
  340. *
  341. * @param[in] p_object Pointer to the object to delete.
  342. *
  343. * @retval NRF_SUCCESS If unregistration was a success.
  344. * @retval NRF_ERROR_NOT_FOUND If the given object was not located.
  345. */
  346. uint32_t lwm2m_coap_handler_object_delete(lwm2m_object_prototype_t * p_object);
  347. /**@brief Generate link format string based on registered objects and instances.
  348. *
  349. * @note For generation of links to work properly it is required that objects is added
  350. * before instances.
  351. *
  352. * @param[inout] p_buffer Pointer to a buffer to fill with link format encoded string. If
  353. * a NULL pointer is provided the function will dry-run the function
  354. * in order to calculate how much memory that is needed for the link
  355. * format string.
  356. * @param[inout] p_buffer_len As input used to indicate the length of the buffer. It will return the
  357. * used amount of buffer length by reference in response. If NULL pointer
  358. * is provided for p_buffer, the value by reference output will be the number
  359. * of bytes needed to generate the link format string.
  360. *
  361. * @retval NRF_SUCCESS If generation of link format string was successful.
  362. * @retval NRF_ERROR_NO_MEM If the provided memory was not large enough.
  363. */
  364. uint32_t lwm2m_coap_handler_gen_link_format(uint8_t * p_buffer, uint16_t * p_buffer_len);
  365. /**@brief Send CoAP 2.05 Content response with the payload provided.
  366. *
  367. * @param[in] p_payload Pointer to the payload to send. Must not be NULL.
  368. * @param[in] payload_len Size of the payload.
  369. * @param[in] p_request Original CoAP request. Must not be NULL.
  370. *
  371. * @retval NRF_SUCCESS If the response was sent out successfully.
  372. */
  373. uint32_t lwm2m_respond_with_payload(uint8_t * p_payload,
  374. uint16_t payload_len,
  375. coap_message_t * p_request);
  376. /**@brief Send CoAP response with a given CoAP message code.
  377. *
  378. * @param [in] code CoAP response code to send.
  379. * @param [in] p_request Original CoAP request. Must not be NULL.
  380. *
  381. * @retval NRF_SUCCESS If the response was sent out successfully.
  382. */
  383. uint32_t lwm2m_respond_with_code(coap_msg_code_t code, coap_message_t * p_request);
  384. #ifdef __cplusplus
  385. }
  386. #endif
  387. #endif // LWM2M_API_H__
  388. /** @} */