ble_db_discovery.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * Copyright (c) 2013 - 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 ble_db_discovery Database Discovery
  43. * @{
  44. * @ingroup ble_sdk_lib
  45. *
  46. * @brief Database discovery module.
  47. *
  48. * @details This module contains the APIs and types exposed by the DB Discovery module. These APIs
  49. * and types can be used by the application to perform discovery of a service and its
  50. * characteristics at the peer server. This module can also be used to discover the
  51. * desired services in multiple remote devices.
  52. *
  53. * @warning The maximum number of characteristics per service that can be discovered by this module
  54. * is determined by the number of characteristics in the service structure defined in
  55. * db_disc_config.h. If the peer has more than the supported number of characteristics, then
  56. * the first found will be discovered and any further characteristics will be ignored. Only the
  57. * following descriptors will be searched for at the peer: Client Characteristic Configuration,
  58. * Characteristic Extended Properties, Characteristic User Description, and Report Reference.
  59. *
  60. * @note Presently only one instance of a Primary Service can be discovered by this module. If
  61. * there are multiple instances of the service at the peer, only the first instance
  62. * of it at the peer is fetched and returned to the application.
  63. *
  64. * @note The application must propagate BLE stack events to this module by calling
  65. * ble_db_discovery_on_ble_evt().
  66. *
  67. */
  68. #ifndef BLE_DB_DISCOVERY_H__
  69. #define BLE_DB_DISCOVERY_H__
  70. #include <stdint.h>
  71. #include <stdbool.h>
  72. #include "nrf_error.h"
  73. #include "ble.h"
  74. #include "ble_gattc.h"
  75. #include "ble_gatt_db.h"
  76. #include "nrf_ble_gq.h"
  77. #ifdef __cplusplus
  78. extern "C" {
  79. #endif
  80. #if !(defined(__LINT__))
  81. /**@brief Macro for defining a ble_db_discovery instance.
  82. *
  83. * @param _name Name of the instance.
  84. * @hideinitializer
  85. */
  86. #define BLE_DB_DISCOVERY_DEF(_name) \
  87. static ble_db_discovery_t _name = {.discovery_in_progress = 0, \
  88. .conn_handle = BLE_CONN_HANDLE_INVALID}; \
  89. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  90. BLE_DB_DISC_BLE_OBSERVER_PRIO, \
  91. ble_db_discovery_on_ble_evt, &_name)
  92. /** @brief Macro for defining multiple ble_db_discovery instances.
  93. *
  94. * @param _name Name of the array of instances.
  95. * @param _cnt Number of instances to define.
  96. */
  97. #define BLE_DB_DISCOVERY_ARRAY_DEF(_name, _cnt) \
  98. static ble_db_discovery_t _name[_cnt] = {MACRO_REPEAT(_cnt, DB_INIT)}; \
  99. \
  100. NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
  101. BLE_DB_DISC_BLE_OBSERVER_PRIO, \
  102. ble_db_discovery_on_ble_evt, &_name, _cnt)
  103. #if !(defined(DOXYGEN))
  104. #define DB_INIT() \
  105. { \
  106. .discovery_in_progress = 0, \
  107. .conn_handle = BLE_CONN_HANDLE_INVALID, \
  108. },
  109. #endif
  110. #else // __LINT__
  111. /* Swallow semicolons */
  112. /*lint -save -esym(528, *) -esym(529, *) : Symbol not referenced. */
  113. #define BLE_DB_DISCOVERY_ARRAY_DEF(A, B) static ble_db_discovery_t A[B]
  114. #define BLE_DB_DISCOVERY_DEF(A) static ble_db_discovery_t A
  115. /*lint -restore */
  116. #endif //!(defined(__LINT__))
  117. #define BLE_DB_DISCOVERY_MAX_SRV 6 /**< Maximum number of services supported by this module. This also indicates the maximum number of users allowed to be registered to this module (one user per service). */
  118. /**@brief DB Discovery event type. */
  119. typedef enum
  120. {
  121. BLE_DB_DISCOVERY_COMPLETE, /**< Event indicating that the discovery of one service is complete. */
  122. BLE_DB_DISCOVERY_ERROR, /**< Event indicating that an internal error has occurred in the DB Discovery module. This could typically be because of the SoftDevice API returning an error code during the DB discover.*/
  123. BLE_DB_DISCOVERY_SRV_NOT_FOUND, /**< Event indicating that the service was not found at the peer.*/
  124. BLE_DB_DISCOVERY_AVAILABLE /**< Event indicating that the DB discovery instance is available.*/
  125. } ble_db_discovery_evt_type_t;
  126. /**@brief Structure containing the event from the DB discovery module to the application. */
  127. typedef struct
  128. {
  129. ble_db_discovery_evt_type_t evt_type; /**< Type of event. */
  130. uint16_t conn_handle; /**< Handle of the connection for which this event has occurred. */
  131. union
  132. {
  133. ble_gatt_db_srv_t discovered_db; /**< Structure containing the information about the GATT Database at the server. This will be filled when the event type is @ref BLE_DB_DISCOVERY_COMPLETE. The UUID field of this will be filled when the event type is @ref BLE_DB_DISCOVERY_SRV_NOT_FOUND. */
  134. void const * p_db_instance; /**< Pointer to DB discovery instance @ref ble_db_discovery_t, indicating availability to the new discovery process. This will be filled when the event type is @ref BLE_DB_DISCOVERY_AVAILABLE. */
  135. uint32_t err_code; /**< nRF Error code indicating the type of error which occurred in the DB Discovery module. This will be filled when the event type is @ref BLE_DB_DISCOVERY_ERROR. */
  136. } params;
  137. } ble_db_discovery_evt_t;
  138. /**@brief DB Discovery event handler type. */
  139. typedef void (* ble_db_discovery_evt_handler_t)(ble_db_discovery_evt_t * p_evt);
  140. /**@brief Structure containing the pending event. */
  141. typedef struct
  142. {
  143. ble_db_discovery_evt_t evt; /**< Pending event. */
  144. ble_db_discovery_evt_handler_t evt_handler; /**< Event handler which should be called to raise this event. */
  145. } ble_db_discovery_user_evt_t;
  146. /**@brief Structure for holding the information related to the GATT database at the server.
  147. *
  148. * @details This module identifies a remote database. Use one instance of this structure per
  149. * connection.
  150. *
  151. * @warning This structure must be zero-initialized.
  152. */
  153. typedef struct
  154. {
  155. ble_gatt_db_srv_t services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service being discovered. This is intended for internal use during service discovery.*/
  156. uint8_t srv_count; /**< Number of services at the peer's GATT database.*/
  157. uint8_t curr_char_ind; /**< Index of the current characteristic being discovered. This is intended for internal use during service discovery.*/
  158. uint8_t curr_srv_ind; /**< Index of the current service being discovered. This is intended for internal use during service discovery.*/
  159. uint8_t discoveries_count; /**< Number of service discoveries made, both successful and unsuccessful. */
  160. bool discovery_in_progress; /**< Variable to indicate whether there is a service discovery in progress. */
  161. uint16_t conn_handle; /**< Connection handle on which the discovery is started. */
  162. uint32_t pending_usr_evt_index; /**< The index to the pending user event array, pointing to the last added pending user event. */
  163. ble_db_discovery_user_evt_t pending_usr_evts[BLE_DB_DISCOVERY_MAX_SRV]; /**< Whenever a discovery related event is to be raised to a user module, it is stored in this array first. When all expected services have been discovered, all pending events are sent to the corresponding user modules. */
  164. } ble_db_discovery_t;
  165. /**@brief DB discovery module initialization struct. */
  166. typedef struct
  167. {
  168. ble_db_discovery_evt_handler_t evt_handler; /**< Event handler to be called by the DB Discovery module. */
  169. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  170. } ble_db_discovery_init_t;
  171. /**@brief Function for initializing the DB Discovery module.
  172. *
  173. * @param[in] p_db_init Pointer to DB discovery initialization structure.
  174. *
  175. * @retval NRF_SUCCESS On successful initialization.
  176. * @retval NRF_ERROR_NULL If the initialization structure was NULL or
  177. * the structure content is empty.
  178. */
  179. uint32_t ble_db_discovery_init(ble_db_discovery_init_t * p_db_init);
  180. /**@brief Function for closing the DB Discovery module.
  181. *
  182. * @details This function will clear up any internal variables and states maintained by the
  183. * module. To re-use the module after calling this function, the function @ref
  184. * ble_db_discovery_init must be called again. When using more than one DB Discovery
  185. * instance, this function should be called for each instance.
  186. *
  187. * @param[out] p_db_discovery Pointer to the DB discovery structure.
  188. *
  189. * @retval NRF_SUCCESS Operation success.
  190. */
  191. uint32_t ble_db_discovery_close(ble_db_discovery_t * const p_db_discovery);
  192. /**@brief Function for registering with the DB Discovery module.
  193. *
  194. * @details The application can use this function to inform which service it is interested in
  195. * discovering at the server.
  196. *
  197. * @param[in] p_uuid Pointer to the UUID of the service to be discovered at the server.
  198. *
  199. * @note The total number of services that can be discovered by this module is @ref
  200. * BLE_DB_DISCOVERY_MAX_SRV. This effectively means that the maximum number of
  201. * registrations possible is equal to the @ref BLE_DB_DISCOVERY_MAX_SRV.
  202. *
  203. * @retval NRF_SUCCESS Operation success.
  204. * @retval NRF_ERROR_NULL When a NULL pointer is passed as input.
  205. * @retval NRF_ERROR_INVALID_STATE If this function is called without calling the
  206. * @ref ble_db_discovery_init.
  207. * @retval NRF_ERROR_NO_MEM The maximum number of registrations allowed by this module
  208. * has been reached.
  209. */
  210. uint32_t ble_db_discovery_evt_register(const ble_uuid_t * const p_uuid);
  211. /**@brief Function for starting the discovery of the GATT database at the server.
  212. *
  213. * @param[out] p_db_discovery Pointer to the DB Discovery structure.
  214. * @param[in] conn_handle The handle of the connection for which the discovery should be
  215. * started.
  216. *
  217. * @retval NRF_SUCCESS Operation success.
  218. * @retval NRF_ERROR_NULL When a NULL pointer is passed as input.
  219. * @retval NRF_ERROR_INVALID_STATE If this function is called without calling the
  220. * @ref ble_db_discovery_init, or without calling
  221. * @ref ble_db_discovery_evt_register.
  222. * @retval NRF_ERROR_BUSY If a discovery is already in progress using
  223. * @p p_db_discovery. Use a different @ref ble_db_discovery_t
  224. * structure, or wait for a DB Discovery event before retrying.
  225. * @return This API propagates the error code returned by functions:
  226. * @ref nrf_ble_gq_conn_handle_register and @ref nrf_ble_gq_item_add.
  227. */
  228. uint32_t ble_db_discovery_start(ble_db_discovery_t * p_db_discovery,
  229. uint16_t conn_handle);
  230. /**@brief Function for handling the Application's BLE Stack events.
  231. *
  232. * @param[in] p_ble_evt Pointer to the BLE event received.
  233. * @param[in,out] p_context Pointer to the DB Discovery structure.
  234. */
  235. void ble_db_discovery_on_ble_evt(ble_evt_t const * p_ble_evt,
  236. void * p_context);
  237. #ifdef __cplusplus
  238. }
  239. #endif
  240. #endif // BLE_DB_DISCOVERY_H__
  241. /** @} */