nrf_sdh.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * Copyright (c) 2017 - 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
  41. *
  42. * @defgroup nrf_sdh SoftDevice Handler
  43. * @{
  44. * @ingroup app_common
  45. * @brief API for initializing and disabling the SoftDevice.
  46. */
  47. #ifndef NRF_SDH_H__
  48. #define NRF_SDH_H__
  49. #include <stdbool.h>
  50. #include "sdk_config.h"
  51. #include "sdk_errors.h"
  52. #include "nrf_section_iter.h"
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. /**
  57. * @name Softdevice Handler dispatch models
  58. * @{
  59. * @ingroup nrf_sdh */
  60. /**@brief SoftDevice events are passed to the application from the interrupt context. */
  61. #define NRF_SDH_DISPATCH_MODEL_INTERRUPT 0
  62. /**@brief SoftDevice events are passed to the application using @ref app_scheduler.
  63. *
  64. * @note @ref app_scheduler must be initialized before enabling the SoftDevice handler.
  65. */
  66. #define NRF_SDH_DISPATCH_MODEL_APPSH 1
  67. /**@brief SoftDevice events are polled manually using @ref nrf_sdh_evts_poll().
  68. *
  69. * @note In this mode, a user application can also implement SD_EVT_IRQHandler() to receive a
  70. * notification about incoming events.
  71. */
  72. #define NRF_SDH_DISPATCH_MODEL_POLLING 2
  73. /** @} */
  74. /**
  75. * @name SoftDevice Handler state change requests
  76. * @{
  77. * @ingroup nrf_sdh */
  78. /**@brief SoftDevice Handler state requests. */
  79. typedef enum
  80. {
  81. NRF_SDH_EVT_ENABLE_REQUEST, //!< Request to enable the SoftDevice.
  82. NRF_SDH_EVT_DISABLE_REQUEST, //!< Request to disable the SoftDevice.
  83. } nrf_sdh_req_evt_t;
  84. /**@brief SoftDevice Handler state request handler.
  85. *
  86. * @retval true If ready for the SoftDevice to change state.
  87. * @retval false If not ready for the SoftDevice to change state.
  88. * If false is returned, the state change is aborted.
  89. */
  90. typedef bool (*nrf_sdh_req_evt_handler_t)(nrf_sdh_req_evt_t request, void * p_context);
  91. /**@brief SoftDevice Handler state request observer. */
  92. typedef struct
  93. {
  94. nrf_sdh_req_evt_handler_t handler; //!< Request handler.
  95. void * p_context; //!< A parameter to the handler function.
  96. } const nrf_sdh_req_observer_t;
  97. /**@brief Macro for registering a SoftDevice state change request observer.
  98. *
  99. * An observer of SoftDevice state change requests receives requests to change the state of the
  100. * SoftDevice from enabled to disabled and vice versa. These requests may or may not be acknowledged
  101. * by the observer, depending on the value returned by its request handler function. Thus, a
  102. * request observer has the capability to defer the change of state of the SoftDevice. If it does
  103. * so, it has the responsibility to call @ref nrf_sdh_request_continue when it is ready to let the
  104. * SoftDevice change its state. If such capability is not necessary and you only need to be informed
  105. * about changes of the SoftDevice state, use the @ref NRF_SDH_STATE_OBSERVER macro instead.
  106. *
  107. * @note This macro places the observer in a section named "sdh_req_observers".
  108. *
  109. * @param[in] _observer Name of the observer.
  110. * @param[in] _prio Priority of the observer's event handler.
  111. * The smaller the number, the higher the priority.
  112. * @hideinitializer
  113. */
  114. #define NRF_SDH_REQUEST_OBSERVER(_observer, _prio) \
  115. STATIC_ASSERT(NRF_SDH_ENABLED, "NRF_SDH_ENABLED not set!"); \
  116. STATIC_ASSERT(_prio < NRF_SDH_REQ_OBSERVER_PRIO_LEVELS, "Priority level unavailable."); \
  117. /*lint -esym(528,*_observer) -esym(529,*_observer) : Symbol not referenced. */ \
  118. NRF_SECTION_SET_ITEM_REGISTER(sdh_req_observers, _prio, nrf_sdh_req_observer_t const _observer)
  119. /** @} */
  120. /**
  121. * @name SoftDevice Handler state events
  122. * @{
  123. * @ingroup nrf_sdh */
  124. /**@brief SoftDevice Handler state events. */
  125. typedef enum
  126. {
  127. NRF_SDH_EVT_STATE_ENABLE_PREPARE, //!< SoftDevice is going to be enabled.
  128. NRF_SDH_EVT_STATE_ENABLED, //!< SoftDevice is enabled.
  129. NRF_SDH_EVT_STATE_DISABLE_PREPARE, //!< SoftDevice is going to be disabled.
  130. NRF_SDH_EVT_STATE_DISABLED, //!< SoftDevice is disabled.
  131. } nrf_sdh_state_evt_t;
  132. /**@brief SoftDevice Handler state event handler. */
  133. typedef void (*nrf_sdh_state_evt_handler_t)(nrf_sdh_state_evt_t state, void * p_context);
  134. /**@brief SoftDevice Handler state observer. */
  135. typedef struct
  136. {
  137. nrf_sdh_state_evt_handler_t handler; //!< State event handler.
  138. void * p_context; //!< A parameter to the event handler.
  139. } const nrf_sdh_state_observer_t;
  140. /**@brief Macro for registering a SoftDevice state observer.
  141. *
  142. * A SoftDevice state observer receives events when the SoftDevice state has changed or is
  143. * about to change. These events are only meant to inform the state observer, which, contrary
  144. * to a state change request observer, does not have the capability to defer the change of state.
  145. * If such capability is required, use the @ref NRF_SDH_REQUEST_OBSERVER macro instead.
  146. *
  147. * This macro places the observer in a section named "sdh_state_observers".
  148. *
  149. * @param[in] _observer Name of the observer.
  150. * @param[in] _prio Priority of the observer's event handler.
  151. * The smaller the number, the higher the priority.
  152. * @hideinitializer
  153. */
  154. #define NRF_SDH_STATE_OBSERVER(_observer, _prio) \
  155. STATIC_ASSERT(NRF_SDH_ENABLED, "NRF_SDH_ENABLED not set!"); \
  156. STATIC_ASSERT(_prio < NRF_SDH_STATE_OBSERVER_PRIO_LEVELS, "Priority level unavailable."); \
  157. /*lint -esym(528,*_observer) -esym(529,*_observer) : Symbol not referenced. */ \
  158. NRF_SECTION_SET_ITEM_REGISTER(sdh_state_observers, _prio, static nrf_sdh_state_observer_t const _observer)
  159. /** @} */
  160. /**
  161. * @name SoftDevice stack events
  162. * @{
  163. * @ingroup nrf_sdh */
  164. /**@brief SoftDevice stack event handler. */
  165. typedef void (*nrf_sdh_stack_evt_handler_t)(void * p_evt);
  166. /**@brief SoftDevice stack event observer. */
  167. typedef struct
  168. {
  169. nrf_sdh_stack_evt_handler_t handler; //!< SoftDevice event handler.
  170. void * p_context; //!< A parameter to the event handler.
  171. } const nrf_sdh_stack_observer_t;
  172. /**@brief Macro for registering a SoftDevice stack events observer.
  173. *
  174. * A SoftDevice stack event observer receives all events from the SoftDevice. These events can be
  175. * either BLE, ANT, or SoC events. If you need to receive BLE, ANT, or SoC events separately, use the
  176. * @ref NRF_SDH_BLE_OBSERVER, @ref NRF_SDH_ANT_OBSERVER, or @ref NRF_SDH_SOC_OBSERVER macros
  177. * respectively.
  178. *
  179. * @note This macro places the observer in a section named "sdh_stack_observers".
  180. *
  181. * @param[in] _observer Name of the observer.
  182. * @param[in] _prio Priority of the observer's event handler.
  183. * The smaller the number, the higher the priority.
  184. ** @hideinitializer
  185. */
  186. #define NRF_SDH_STACK_OBSERVER(_observer, _prio) \
  187. STATIC_ASSERT(NRF_SDH_ENABLED, "NRF_SDH_ENABLED not set!"); \
  188. STATIC_ASSERT(_prio < NRF_SDH_STACK_OBSERVER_PRIO_LEVELS, "Priority level unavailable."); \
  189. /*lint -esym(528,*_observer) -esym(529,*_observer) : Symbol not referenced. */ \
  190. NRF_SECTION_SET_ITEM_REGISTER(sdh_stack_observers, _prio, static nrf_sdh_stack_observer_t const _observer)
  191. /** @} */
  192. /**@brief Function for requesting to enable the SoftDevice.
  193. *
  194. * This function issues a @ref NRF_SDH_EVT_ENABLE_REQUEST request to all observers that
  195. * were registered using the @ref NRF_SDH_REQUEST_OBSERVER macro. The observers may or
  196. * may not acknowledge the request. If all observers acknowledge the request, the
  197. * SoftDevice will be enabled. Otherwise, the process will be stopped and the observers
  198. * that did not acknowledge have the responsibility to restart it by calling
  199. * @ref nrf_sdh_request_continue when they are ready for the SoftDevice to change state.
  200. *
  201. * @retval NRF_SUCCESS The process is started.
  202. * @retval NRF_ERROR_INVALID_STATE The SoftDevice is already enabled.
  203. */
  204. ret_code_t nrf_sdh_enable_request(void);
  205. /**@brief Function for requesting to disable the SoftDevice.
  206. *
  207. * This function issues a @ref NRF_SDH_EVT_DISABLE_REQUEST request to all observers that
  208. * were registered using the @ref NRF_SDH_REQUEST_OBSERVER macro. The observers may or
  209. * may not acknowledge the request. If all observers acknowledge the request, the
  210. * SoftDevice will be disabled. Otherwise, the process will be stopped and the observers
  211. * that did not acknowledge have the responsibility to restart it by calling
  212. * @ref nrf_sdh_request_continue when they are ready for the SoftDevice to change state.
  213. *
  214. * @retval NRF_SUCCESS The process is started.
  215. * @retval NRF_ERROR_INVALID_STATE The SoftDevice is already disabled.
  216. */
  217. ret_code_t nrf_sdh_disable_request(void);
  218. /**@brief Function for restarting the SoftDevice Enable/Disable process.
  219. *
  220. * Modules which did not acknowledge a @ref NRF_SDH_EVT_ENABLE_REQUEST or
  221. * @ref NRF_SDH_EVT_DISABLE_REQUEST request must call this function to restart the
  222. * SoftDevice state change process.
  223. *
  224. * @retval NRF_SUCCESS The process is restarted.
  225. * @retval NRF_ERROR_INVALID_STATE No state change request was pending.
  226. */
  227. ret_code_t nrf_sdh_request_continue(void);
  228. /**@brief Function for retrieving the SoftDevice state.
  229. *
  230. * @retval true If the SoftDevice is enabled.
  231. * @retval false If the SoftDevice is disabled.
  232. */
  233. bool nrf_sdh_is_enabled(void);
  234. /**@brief Function for stopping the incoming stack events.
  235. *
  236. * This function disables the SoftDevice interrupt. To resume polling for events,
  237. * call @ref nrf_sdh_resume.
  238. */
  239. void nrf_sdh_suspend(void);
  240. /**@brief Function for resuming polling incoming events from the SoftDevice. */
  241. void nrf_sdh_resume(void);
  242. /**@brief Function for retrieving the information about the module state.
  243. *
  244. * @retval true The SoftDevice handler is paused, and it will not fetch events from the stack.
  245. * @retval false The SoftDevice handler is running, and it will fetch and dispatch events from
  246. * the stack to the registered stack observers.
  247. */
  248. bool nrf_sdh_is_suspended(void);
  249. /**@brief Function for polling stack events from the SoftDevice.
  250. *
  251. * The events are passed to the application using the registered event handlers.
  252. *
  253. * @note @ref NRF_SDH_DISPATCH_MODEL_POLLING must be selected to use this function.
  254. */
  255. void nrf_sdh_evts_poll(void);
  256. #ifdef __cplusplus
  257. }
  258. #endif
  259. #endif // NRF_SDH_H__
  260. /** @} */