nrf_ble_gq.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * Copyright (c) 2018 - 2020, 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_ble_gq BLE GATT Queue
  43. * @{
  44. * @ingroup ble_sdk_lib
  45. * @brief Queue for the BLE GATT requests.
  46. *
  47. * @details The BLE GATT Queue (BGQ) module can be used to queue BLE GATT requests if the SoftDevice is not
  48. * able to handle them at the moment. In this case, processing of queued request is
  49. * postponed. Later on, when corresponding BLE event indicates that the SoftDevice may be
  50. * free, the request is retried. For conceptual documentation of this module, see
  51. * @ref lib_ble_gatt_queue.
  52. *
  53. */
  54. #ifndef NRF_BLE_GQ_H__
  55. #define NRF_BLE_GQ_H__
  56. #include <stdint.h>
  57. #include "sdk_common.h"
  58. #include "nrf_memobj.h"
  59. #include "nrf_queue.h"
  60. #include "nrf_sdh_ble.h"
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. /**@brief Macro for defining a nrf_ble_gq_t instance with default parameters.
  65. *
  66. * @param _name Name of the instance.
  67. * @param _max_connections The maximal number of connection handles that can be registered.
  68. * @param _queue_size The maximal number of nrf_ble_gq_req_t instances that queue can hold.
  69. * @hideinitializer
  70. */
  71. #define NRF_BLE_GQ_DEF(_name, _max_connections, _queue_size) \
  72. NRF_BLE_GQ_CUSTOM_DEF(_name, \
  73. _max_connections, \
  74. _queue_size, \
  75. NRF_BLE_GQ_DATAPOOL_ELEMENT_SIZE, \
  76. NRF_BLE_GQ_DATAPOOL_ELEMENT_COUNT)
  77. #if !(defined(__LINT__))
  78. /**@brief Macro for defining a nrf_ble_gq_t instance.
  79. *
  80. * @param _name Name of the instance.
  81. * @param _max_connections The maximal number of connection handles that can be registered.
  82. * @param _queue_size The maximal number of nrf_ble_gq_req_t instances that queue can hold.
  83. * @param _pool_elem_size Size of a single element in the pool of memory objects.
  84. * @param _pool_elem_count Number of elements in the pool of memory objects.
  85. * @hideinitializer
  86. */
  87. #define NRF_BLE_GQ_CUSTOM_DEF(_name, _max_connections, _queue_size, _pool_elem_size, _pool_elem_count) \
  88. static uint16_t CONCAT_2(_name, conn_handles_arr)[] = \
  89. { \
  90. MACRO_REPEAT(_max_connections, NRF_BLE_GQ_CONN_HANDLE_INIT) \
  91. }; \
  92. STATIC_ASSERT(ARRAY_SIZE(CONCAT_2(_name, conn_handles_arr)) == (_max_connections)); \
  93. NRF_QUEUE_ARRAY_DEF(nrf_ble_gq_req_t, CONCAT_2(_name, req_queue), _queue_size, \
  94. NRF_QUEUE_MODE_NO_OVERFLOW, _max_connections); \
  95. NRF_QUEUE_DEF(uint16_t, CONCAT_2(_name, purge_queue), _max_connections, \
  96. NRF_QUEUE_MODE_NO_OVERFLOW); \
  97. NRF_MEMOBJ_POOL_DEF(CONCAT_2(_name, pool), _pool_elem_size, _pool_elem_count); \
  98. static nrf_ble_gq_t _name = \
  99. { \
  100. .max_conns = (_max_connections), \
  101. .p_conn_handles = CONCAT_2(_name, conn_handles_arr), \
  102. .p_req_queue = CONCAT_2(_name, req_queue), \
  103. .p_purge_queue = &CONCAT_2(_name, purge_queue), \
  104. .p_data_pool = &CONCAT_2(_name, pool) \
  105. }; \
  106. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  107. NRF_BLE_GQ_BLE_OBSERVER_PRIO, \
  108. nrf_ble_gq_on_ble_evt, &_name)
  109. #else
  110. #define NRF_BLE_GQ_CUSTOM_DEF(_name, _max_connections, _queue_size, _pool_elem_size, _pool_elem_count) \
  111. static nrf_ble_gq_t _name;
  112. #endif // !(defined(__LINT__))
  113. /**@brief Helping macro used to properly initialize connection handle array for nrf_ble_gq_t instance.
  114. * Used in @ref NRF_BLE_GQ_CUSTOM_DEF.
  115. */
  116. #define NRF_BLE_GQ_CONN_HANDLE_INIT(_arg) BLE_CONN_HANDLE_INVALID,
  117. /**@brief BLE GATT request types. */
  118. typedef enum
  119. {
  120. NRF_BLE_GQ_REQ_GATTC_READ, /**< GATTC Read Request. See @ref nrf_ble_gq_gattc_read_t and @ref sd_ble_gattc_read */
  121. NRF_BLE_GQ_REQ_GATTC_WRITE, /**< GATTC Write Request. See @ref nrf_ble_gq_gattc_write_t and @ref sd_ble_gattc_write */
  122. NRF_BLE_GQ_REQ_SRV_DISCOVERY, /**< GATTC Service Discovery Request. See @ref nrf_ble_gq_gattc_write_t and @ref sd_ble_gattc_primary_services_discover. */
  123. NRF_BLE_GQ_REQ_CHAR_DISCOVERY, /**< GATTC Characteristic Discovery Request. See @ref nrf_ble_gq_gattc_char_disc_t and @ref sd_ble_gattc_characteristics_discover. */
  124. NRF_BLE_GQ_REQ_DESC_DISCOVERY, /**< GATTC Characteristic Descriptor Discovery Request. See @ref nrf_ble_gq_gattc_desc_disc_t and @ref sd_ble_gattc_descriptors_discover*/
  125. NRF_BLE_GQ_REQ_GATTS_HVX, /**< GATTS Handle Value Notification or Indication. See @ref nrf_ble_gq_gatts_hvx_t and @ref ble_gatts_hvx_params_t */
  126. NRF_BLE_GQ_REQ_NUM /**< Total number of different GATT Request types */
  127. } nrf_ble_gq_req_type_t;
  128. /**@brief Pointer used to describe error handler for GATTC request. */
  129. typedef void (* nrf_ble_gq_req_error_cb_t) (uint32_t nrf_error,
  130. void * p_context,
  131. uint16_t conn_handle);
  132. /**@brief Structure used to describe @ref NRF_BLE_GQ_REQ_GATTC_READ request type. */
  133. typedef struct
  134. {
  135. uint16_t handle; /**< Handle of the Attribute to be read. */
  136. uint16_t offset; /**< Offset into the Attribute Value to be read. */
  137. } nrf_ble_gq_gattc_read_t;
  138. /**@brief Structure used to describe @ref NRF_BLE_GQ_REQ_GATTC_WRITE request type. */
  139. typedef ble_gattc_write_params_t nrf_ble_gq_gattc_write_t;
  140. /**@brief Structure used to describe @ref NRF_BLE_GQ_REQ_SRV_DISCOVERY request type. */
  141. typedef struct
  142. {
  143. uint16_t start_handle; /**< The start handle value used during service discovery. */
  144. ble_uuid_t srvc_uuid; /**< The service UUID to be found. */
  145. } nrf_ble_gq_gattc_srv_discovery_t;
  146. /**@brief Structure used to describe @ref NRF_BLE_GQ_REQ_CHAR_DISCOVERY request type. */
  147. typedef ble_gattc_handle_range_t nrf_ble_gq_gattc_char_disc_t;
  148. /**@brief Structure used to describe @ref NRF_BLE_GQ_REQ_DESC_DISCOVERY request type. */
  149. typedef ble_gattc_handle_range_t nrf_ble_gq_gattc_desc_disc_t;
  150. /**@brief Structure used to describe @ref NRF_BLE_GQ_REQ_GATTS_HVX request type. */
  151. typedef ble_gatts_hvx_params_t nrf_ble_gq_gatts_hvx_t;
  152. /**@brief Structure used to handle SoftDevice error. */
  153. typedef struct
  154. {
  155. nrf_ble_gq_req_error_cb_t cb; /**< Error handler to be called in case of an error from SoftDevice. */
  156. void * p_ctx; /**< Parameter to the error handler. */
  157. } nrf_ble_gq_req_error_handler_t;
  158. /**@brief Structure used to describe BLE GATT request. */
  159. typedef struct
  160. {
  161. nrf_ble_gq_req_type_t type; /**< Type of request. */
  162. nrf_memobj_t * p_mem_obj; /**< Memory object for data that cannot be contained in request descriptor. */
  163. nrf_ble_gq_req_error_handler_t error_handler; /**< Error handler structure. */
  164. union
  165. {
  166. nrf_ble_gq_gattc_read_t gattc_read; /**< GATTC read parameters. Filled when nrf_ble_gq_req_t::type is @ref NRF_BLE_GQ_REQ_GATTC_READ. */
  167. nrf_ble_gq_gattc_write_t gattc_write; /**< GATTC write parameters. Filled when nrf_ble_gq_req_t::type is @ref NRF_BLE_GQ_REQ_GATTC_WRITE. */
  168. nrf_ble_gq_gattc_srv_discovery_t gattc_srv_disc; /**< GATTC Service discovery parameters. Filled when nrf_ble_gq_req_t::type is @ref NRF_BLE_GQ_REQ_SRV_DISCOVERY. */
  169. nrf_ble_gq_gattc_char_disc_t gattc_char_disc; /**< GATTC characteristic discovery parameters. Filled when nrf_ble_gq_req_t::type is @ref NRF_BLE_GQ_REQ_CHAR_DISCOVERY. */
  170. nrf_ble_gq_gattc_desc_disc_t gattc_desc_disc; /**< GATTC characteristic descriptor discovery parameters. Filled when nrf_ble_gq_req_t::type is NRF_BLE_GQ_REQ_DESC_DISCOVERY. */
  171. nrf_ble_gq_gatts_hvx_t gatts_hvx; /**< GATTS Handle Value Notification or Indication Parameters. Filled when nrf_ble_gq_req_t::type is @ref NRF_BLE_GQ_REQ_GATTS_HVX. */
  172. } params;
  173. } nrf_ble_gq_req_t;
  174. /**@brief Descriptor for the BLE GATT Queue instance. */
  175. typedef struct
  176. {
  177. uint16_t const max_conns; /**< Maximal number of connection handles that can be registered. */
  178. uint16_t * p_conn_handles; /**< Pointer to array with registered connection handles.*/
  179. nrf_queue_t const * const p_req_queue; /**< Pointer to array of queue instances used to hold nrf_ble_gq_req_t instances.*/
  180. nrf_queue_t const * const p_purge_queue; /**< Pointer to the queue instance used to hold indexes of queues to purge.*/
  181. nrf_memobj_pool_t const * p_data_pool; /**< Memory pool used to obtain nrf_memobj_t instances.*/
  182. } nrf_ble_gq_t;
  183. /**@brief Function for adding a GATT request to the BGQ instance.
  184. *
  185. * @details This function adds a request to the BGQ instance and allocates necessary memory
  186. * for data that can be held within the request descriptor. If the SoftDevice is free,
  187. * this request will be processed immediately. Otherwise, the request remains in
  188. * in the queue and is processed later.
  189. *
  190. * @param[in] p_gatt_queue Pointer to the BGQ instance.
  191. * @param[in] p_req Pointer to the request.
  192. * @param[in] conn_handle Connection handle associated with the request.
  193. *
  194. * @retval NRF_SUCCESS If the request was added successfully.
  195. * @retval NRF_ERROR_NULL Any parameter was NULL.
  196. * @retval NRF_ERROR_NO_MEM There was no room in the queue or in the data pool.
  197. * @retval NRF_ERROR_INVALID_PARAM If \p conn_handle is not registered or type of request -
  198. * \p p_req is not valid.
  199. * @retval err_code Other request specific error codes may be returned.
  200. */
  201. ret_code_t nrf_ble_gq_item_add(nrf_ble_gq_t const * const p_gatt_queue,
  202. nrf_ble_gq_req_t * const p_req,
  203. uint16_t conn_handle);
  204. /**@brief Function for registering connection handle in the BGQ instance.
  205. *
  206. * @details This function is used for registering connection handle in the BGQ instance. From this
  207. * point, the BGQ instance can handle GATT requests associated with the handle until connection
  208. * is no longer valid (disconnect event occurs).
  209. *
  210. * @param[in] p_gatt_queue Pointer to the BGQ instance.
  211. * @param[in] conn_handle Connection handle.
  212. *
  213. * @retval NRF_SUCCESS If the registration was successful.
  214. * @retval NRF_ERROR_NULL If \p p_gatt_queue was NULL.
  215. * @retval NRF_ERROR_NO_MEM If there was no space for another connection handle.
  216. */
  217. ret_code_t nrf_ble_gq_conn_handle_register(nrf_ble_gq_t * const p_gatt_queue, uint16_t conn_handle);
  218. /**@brief Function for handling BLE events from the SoftDevice.
  219. *
  220. * @details This function handles the BLE events received from the SoftDevice. If a BLE
  221. * event is relevant to the BGQ module, it is used to update internal variables,
  222. * process queued GATT requests and, if necessary, send errors to the application.
  223. *
  224. * @param[in] p_ble_evt Pointer to the BLE event.
  225. * @param[in] p_context Pointer to the BGQ instance.
  226. */
  227. void nrf_ble_gq_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  228. #ifdef __cplusplus
  229. }
  230. #endif
  231. #endif // NRF_BLE_GQ_H__
  232. /** @} */