coap_observe_api.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 coap_observe_api.h
  41. *
  42. * @defgroup iot_sdk_coap_observe CoAP Observe
  43. * @ingroup iot_sdk_coap
  44. * @{
  45. * @brief Public API of Nordic's CoAP Observe implementation.
  46. */
  47. #ifndef COAP_OBSERVE_API_H__
  48. #define COAP_OBSERVE_API_H__
  49. #include <stdint.h>
  50. #include "coap_api.h"
  51. #include "coap_transport.h"
  52. #include "coap_queue.h"
  53. #include "compiler_abstraction.h"
  54. #include "sdk_errors.h"
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. #define COAP_OPT_OBSERVE 6 /**< Observe option number. */
  59. /**@brief Struct for CoAP Server for holding an instance of a remote observer. */
  60. typedef struct
  61. {
  62. coap_remote_t remote; /**< Remote address and port number. */
  63. uint8_t token[8]; /**< Message Token ID. */
  64. uint8_t token_len; /**< Length of the token. */
  65. coap_content_type_t ct; /**< Content type to use when sending notifications. */
  66. coap_resource_t * p_resource_of_interest; /**< Pointer to the resource of interest. */
  67. } coap_observer_t;
  68. /**@brief Struct for CoAP Client for holding an instance of a remote observable resource. */
  69. typedef struct
  70. {
  71. coap_remote_t remote; /**< Remote address and port number. */
  72. uint8_t token[8]; /**< Message Token ID. */
  73. uint8_t token_len; /**< Length of the token. */
  74. coap_response_callback_t response_callback; /**< Function callback set by the application to be called when a notifications has been received. Should be set by the application. */
  75. uint32_t max_age; /**< Max-Age of the observable resources value. If timed out, the value is no longer valid as a representation of the observable resource. */
  76. } coap_observable_t;
  77. /**@brief Register a new observer.
  78. *
  79. * @param[out] p_handle Handle to the observer instance registered. Returned by reference.
  80. * Should not be NULL.
  81. * @param[in] p_observer Pointer to the observer structure to register. The data will be
  82. * copied. Should not be NULL.
  83. *
  84. * @retval NRF_SUCCESS If the observer was registered successfully.
  85. * @retval NRF_ERROR_NO_MEM If the observer could not be added to the list.
  86. * @retval NRF_ERROR_NULL If one of the parameters is a NULL pointer.
  87. */
  88. uint32_t coap_observe_server_register(uint32_t * p_handle, coap_observer_t * p_observer);
  89. /**@brief Unregister an observer.
  90. *
  91. * @details Unregister the observer and clear the memory used by this instance.
  92. *
  93. * @param[in] handle Handle to the observer instance registered.
  94. *
  95. * @retval NRF_SUCCESS If the observer was successfully unregistered.
  96. * @retval NRF_ERROR_NOT_FOUND If the given handle was not found in the observer list.
  97. */
  98. uint32_t coap_observe_server_unregister(uint32_t handle);
  99. /**@brief Search the observer list for an observer matching remote address and subject given.
  100. *
  101. * @param[out] p_handle Handle to the observer instance registered. Returned by reference.
  102. * Should not be NULL.
  103. * @param[in] p_observer_addr Pointer to an address structure giving remote address of the observer and port number.
  104. * Should not be NULL.
  105. * @param[in] p_resource Pointer to the resource the observer is registered to. Should not be NULL.
  106. *
  107. * @retval NRF_SUCCESS If observer was found in the observer list.
  108. * @retval NRF_ERROR_NULL If one of the pointers are NULL.
  109. * @retval NRF_ERROR_NOT_FOUND If observer was not found.
  110. */
  111. uint32_t coap_observe_server_search(uint32_t * p_handle, coap_remote_t * p_observer_addr, coap_resource_t * p_resource);
  112. /**@brief Iterate through observers subscribing to a specific resource.
  113. *
  114. * @param[out] pp_observer Pointer to be filled by the search function upon finding the next observer starting from
  115. * from the p_observer pointer provided. Should not be NULL.
  116. * @param[in] p_observer Pointer to the observer where to start the search.
  117. * @param[in] p_resource Pointer to the resource of interest. Should not be NULL.
  118. *
  119. * @retval NRF_SUCCESS If observer was found.
  120. * @retval NRF_ERROR_NULL If pp_observer or p_resource pointer is NULL.
  121. * @retval NRF_ERROR_NOT_FOUND If next observer was not found.
  122. */
  123. uint32_t coap_observe_server_next_get(coap_observer_t ** pp_observer, coap_observer_t * p_observer, coap_resource_t * p_resource);
  124. /**@brief Retrieve the observer based on handle.
  125. *
  126. * @param[in] handle Handle to the coap_observer_t instance.
  127. * @param[out] pp_observer Pointer to an observer return by reference.
  128. * Should not be NULL.
  129. *
  130. * @retval NRF_SUCCESS If observer was found in the observer list.
  131. * @retval NRF_ERROR_NULL If pp_observer pointer is NULL.
  132. * @retval NRF_ERROR_NOT_FOUND If observer associated with the handle was not found.
  133. */
  134. uint32_t coap_observe_server_get(uint32_t handle, coap_observer_t ** pp_observer);
  135. /**@brief Register a new observable resource.
  136. *
  137. * @param[out] p_handle Handle to the observable resource instance registered. Returned by
  138. * reference. Should not be NULL.
  139. * @param[in] p_observable Pointer to a observable resource structure to register. The structure
  140. * will be copied. Should not be NULL.
  141. *
  142. * @retval NRF_SUCCESS If the observable resource was registered successfully.
  143. * @retval NRF_ERROR_NO_MEM If the observable resource could not be added to the list.
  144. * @retval NRF_ERROR_NULL If one of the parameters is a NULL pointer.
  145. */
  146. uint32_t coap_observe_client_register(uint32_t * p_handle, coap_observable_t * p_observable);
  147. /**@brief Unregister an observable resource.
  148. *
  149. * @details Unregister the observable resource and clear the memory used by this instance.
  150. *
  151. * @param[in] handle Handle to the observable resource instance registered.
  152. *
  153. * @retval NRF_SUCCESS If the observable resource was successfully unregistered.
  154. * @retval NRF_ERROR_NOT_FOUND If the given handle was not found in the observable
  155. * resource list.
  156. */
  157. uint32_t coap_observe_client_unregister(uint32_t handle);
  158. /**@brief Search for a observable resource instance by token.
  159. *
  160. * @param[out] p_handle Handle to the observable resource instance registered. Returned by
  161. * reference. Should not be NULL.
  162. * @param[in] p_token Pointer to the byte array holding the token id. Should not be NULL.
  163. * @param[in] token_len Length of the token.
  164. *
  165. * @retval NRF_SUCCESS If observable resource was found in the observable
  166. * resource list.
  167. * @retval NRF_ERROR_NULL If one of the pointers are NULL.
  168. * @retval NRF_ERROR_NOT_FOUND If observable resource was not found in the observable
  169. * resource list.
  170. */
  171. uint32_t coap_observe_client_search(uint32_t * p_handle, uint8_t * p_token, uint16_t token_len);
  172. /**@brief Retrieve the observable resource based on handle.
  173. *
  174. * @param[in] handle Handle to the coap_observable_t instance.
  175. * @param[out] pp_observable Pointer to an observable resource return by reference.
  176. * Should not be NULL.
  177. *
  178. * @retval NRF_SUCCESS If observable resource was found in the observable
  179. * resource list.
  180. * @retval NRF_ERROR_NULL If pp_observable pointer is NULL.
  181. * @retval NRF_ERROR_NOT_FOUND If observable resource associated with the handle
  182. * was not found.
  183. */
  184. uint32_t coap_observe_client_get(uint32_t handle, coap_observable_t ** pp_observable);
  185. /**@brief Iterate through observable resources.
  186. *
  187. * @param[out] pp_observable Pointer to be filled by the search function upon finding the next
  188. * observable resource starting from from the pointer provided.
  189. * Should not be NULL.
  190. * @param[out] p_handle Handler to the observable resource found returned by reference. Should
  191. * not be NULL.
  192. * @param[in] p_observable Pointer to the observable resource where to start the search.
  193. *
  194. * @retval NRF_SUCCESS If observer was found.
  195. * @retval NRF_ERROR_NULL If pp_observer or p_observer pointer is NULL.
  196. * @retval NRF_ERROR_NOT_FOUND If next observer was not found.
  197. */
  198. uint32_t coap_observe_client_next_get(coap_observable_t ** pp_observable, uint32_t * p_handle, coap_observable_t * p_observable);
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif // COAP_OBSERVE_API_H__
  203. /** @} */