coap_observe.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.h
  41. *
  42. * @defgroup iot_sdk_coap_observe CoAP Observe
  43. * @ingroup iot_sdk_coap
  44. * @{
  45. * @brief Internal API of Nordic's CoAP Observe implementation.
  46. */
  47. #ifndef COAP_OBSERVE_H__
  48. #define COAP_OBSERVE_H__
  49. #include <stdint.h>
  50. #include "coap_observe_api.h"
  51. #include "coap_api.h"
  52. #include "coap_queue.h"
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. /**@cond NO_DOXYGEN */
  57. /**@brief Register a new observer.
  58. *
  59. * @param[out] p_handle Handle to the observer instance registered. Returned by reference.
  60. * Should not be NULL.
  61. * @param[in] p_observer Pointer to the observer structure to register. The data will be
  62. * copied. Should not be NULL.
  63. *
  64. * @retval NRF_SUCCESS If the observer was registered successfully.
  65. * @retval NRF_ERROR_NO_MEM If the observer could not be added to the list.
  66. * @retval NRF_ERROR_NULL If one of the parameters is a NULL pointer.
  67. */
  68. uint32_t internal_coap_observe_server_register(uint32_t * p_handle, coap_observer_t * p_observer);
  69. /**@brief Unregister an observer.
  70. *
  71. * @details Unregister the observer and clear the memory used by this instance.
  72. *
  73. * @param[in] handle Handle to the observer instance registered.
  74. *
  75. * @retval NRF_SUCCESS If the observer was successfully unregistered.
  76. * @retval NRF_ERROR_NOT_FOUND If the given handle was not found in the observer list.
  77. */
  78. uint32_t internal_coap_observe_server_unregister(uint32_t handle);
  79. /**@brief Search the observer list for an observer matching remote address and subject given.
  80. *
  81. * @param[out] p_handle Handle to the observer instance registered. Returned by reference.
  82. * Should not be NULL.
  83. * @param[in] p_observer_addr Pointer to an address structure giving remote address of the observer and port number.
  84. * Should not be NULL.
  85. * @param[in] p_resource Pointer to the resource the observer is registered to. Should not be NULL.
  86. *
  87. * @retval NRF_SUCCESS If observer was found in the observer list.
  88. * @retval NRF_ERROR_NULL If one of the pointers are NULL.
  89. * @retval NRF_ERROR_NOT_FOUND If observer was not found.
  90. */
  91. uint32_t internal_coap_observe_server_search(uint32_t * p_handle, coap_remote_t * p_observer_addr, coap_resource_t * p_resource);
  92. /**@brief Iterate through observers subscribing to a specific resource.
  93. *
  94. * @param[out] pp_observer Pointer to be filled by the search function upon finding the next observer starting from
  95. * from the p_observer pointer provided. Should not be NULL.
  96. * @param[in] p_observer Pointer to the observer where to start the search.
  97. * @param[in] p_resource Pointer to the resource of interest. Should not be NULL.
  98. *
  99. * @retval NRF_SUCCESS If observer was found.
  100. * @retval NRF_ERROR_NULL If pp_observer or p_resource pointer is NULL.
  101. * @retval NRF_ERROR_NOT_FOUND If next observer was not found.
  102. */
  103. uint32_t internal_coap_observe_server_next_get(coap_observer_t ** pp_observer, coap_observer_t * p_observer, coap_resource_t * p_resource);
  104. /**@brief Retrieve the observer based on handle.
  105. *
  106. * @param[in] handle Handle to the coap_observer_t instance.
  107. * @param[out] pp_observer Pointer to an observer return by reference.
  108. * Should not be NULL.
  109. *
  110. * @retval NRF_SUCCESS If observer was found in the observer list.
  111. * @retval NRF_ERROR_NULL If pp_observer pointer is NULL.
  112. * @retval NRF_ERROR_NOT_FOUND If observer associated with the handle was not found.
  113. */
  114. uint32_t internal_coap_observe_server_get(uint32_t handle, coap_observer_t ** pp_observer);
  115. /**@brief Register a new observable resource.
  116. *
  117. * @param[out] p_handle Handle to the observable resource instance registered. Returned by
  118. * reference. Should not be NULL.
  119. * @param[in] p_observable Pointer to a observable resource structure to register. The structure
  120. * will be copied. Should not be NULL.
  121. *
  122. * @retval NRF_SUCCESS If the observable resource was registered successfully.
  123. * @retval NRF_ERROR_NO_MEM If the observable resource could not be added to the list.
  124. * @retval NRF_ERROR_NULL If one of the parameters is a NULL pointer.
  125. */
  126. uint32_t internal_coap_observe_client_register(uint32_t * p_handle, coap_observable_t * p_observable);
  127. /**@brief Unregister an observable resource.
  128. *
  129. * @details Unregister the observable resource and clear the memory used by this instance.
  130. *
  131. * @param[in] handle Handle to the observable resource instance registered.
  132. *
  133. * @retval NRF_SUCCESS If the observable resource was successfully unregistered.
  134. * @retval NRF_ERROR_NOT_FOUND If the given handle was not found in the observable
  135. * resource list.
  136. */
  137. uint32_t internal_coap_observe_client_unregister(uint32_t handle);
  138. /**@brief Search for a observable resource instance by token.
  139. *
  140. * @param[out] p_handle Handle to the observable resource instance registered. Returned by
  141. * reference. Should not be NULL.
  142. * @param[in] p_token Pointer to the byte array holding the token id. Should not be NULL.
  143. * @param[in] token_len Length of the token.
  144. *
  145. * @retval NRF_SUCCESS If observable resource was found in the observable
  146. * resource list.
  147. * @retval NRF_ERROR_NULL If one of the pointers are NULL.
  148. * @retval NRF_ERROR_NOT_FOUND If observable resource was not found in the observable
  149. * resource list.
  150. */
  151. uint32_t internal_coap_observe_client_search(uint32_t * p_handle, uint8_t * p_token, uint16_t token_len);
  152. /**@brief Retrieve the observable resource based on handle.
  153. *
  154. * @param[in] handle Handle to the coap_observable_t instance.
  155. * @param[out] pp_observable Pointer to an observable resource return by reference.
  156. * Should not be NULL.
  157. *
  158. * @retval NRF_SUCCESS If observable resource was found in the observable
  159. * resource list.
  160. * @retval NRF_ERROR_NULL If pp_observable pointer is NULL.
  161. * @retval NRF_ERROR_NOT_FOUND If observable resource associated with the handle
  162. * was not found.
  163. */
  164. uint32_t internal_coap_observe_client_get(uint32_t handle, coap_observable_t ** pp_observable);
  165. /**@brief Iterate through observable resources.
  166. *
  167. * @param[out] pp_observable Pointer to be filled by the search function upon finding the next
  168. * observable resource starting from from the pointer provided.
  169. * Should not be NULL.
  170. * @param[out] p_handle Handler to the observable resource found returned by reference. Should
  171. * not be NULL.
  172. * @param[in] p_observable Pointer to the observable resource where to start the search.
  173. *
  174. * @retval NRF_SUCCESS If observer was found.
  175. * @retval NRF_ERROR_NULL If pp_observer or p_observer pointer is NULL.
  176. * @retval NRF_ERROR_NOT_FOUND If next observer was not found.
  177. */
  178. uint32_t internal_coap_observe_client_next_get(coap_observable_t ** pp_observable, uint32_t * p_handle, coap_observable_t * p_observable);
  179. #if (COAP_ENABLE_OBSERVE_SERVER == 1) || (COAP_ENABLE_OBSERVE_CLIENT == 1)
  180. /**@brief Internal function to initilize observer (client) and observable (server) lists.
  181. *
  182. * @retval NRF_SUCCESS If initialization was successful.
  183. */
  184. void internal_coap_observe_init(void);
  185. #else // COAP_ENABLE_OBSERVE_SERVER || COAP_ENABLE_OBSERVE_CLIENT
  186. #define internal_coap_observe_init(...)
  187. #endif // COAP_ENABLE_OBSERVE_SERVER || COAP_ENABLE_OBSERVE_CLIENT
  188. #if (COAP_ENABLE_OBSERVE_CLIENT == 1)
  189. /**@brief Observe client function to be run when sending requests.
  190. *
  191. * @details The function will peek into the outgoing messages to see if any actions regarding
  192. * subscription to observable resources has to be done.
  193. *
  194. * @param[in] p_request Pointer to the outgoing request.
  195. */
  196. void coap_observe_client_send_handle(coap_message_t * p_request);
  197. /**@brief Observe client function to be run when response message has been received.
  198. *
  199. * @details The function will register and unregister observable resources based on the received
  200. * response messages. Upon a notification max-age values will be updated, and the correct
  201. * response callback will be called. If a notification is terminated by the peer, the function
  202. * will automatically terminate the subscription from the client by unregistering the
  203. * observable resource.
  204. *
  205. * @param[in] p_response Pointer to the response message received.
  206. * @param[in] p_item Pointer to the queued element of the outgoing request.
  207. */
  208. void coap_observe_client_response_handle(coap_message_t * p_response, coap_queue_item_t * p_item);
  209. #else // COAP_ENABLE_OBSERVE_CLIENT
  210. #define coap_observe_client_send_handle(...)
  211. #define coap_observe_client_response_handle(...)
  212. #endif // COAP_ENABLE_OBSERVE_CLIENT
  213. /**@endcond */
  214. #ifdef __cplusplus
  215. }
  216. #endif
  217. #endif // COAP_OBSERVE_H__
  218. /** @} */