|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 - 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +/** @file |
| 8 | + * |
| 9 | + * @defgroup ble_db_discovery BLE Nordic database discovery library |
| 10 | + * @{ |
| 11 | + * @brief Library for discovery of a service and its characteristics at the peer server. |
| 12 | + */ |
| 13 | + |
| 14 | +#include <stdint.h> |
| 15 | +#include <zephyr/kernel.h> |
| 16 | +#include <ble_gattc.h> |
| 17 | +#include <bm/bluetooth/ble_gq.h> |
| 18 | +#include <bm/bluetooth/ble_gatt_db.h> |
| 19 | + |
| 20 | +#ifndef BLE_DB_DISCOVERY_H__ |
| 21 | +#define BLE_DB_DISCOVERY_H__ |
| 22 | + |
| 23 | +/** |
| 24 | + * @brief Macro for defining a ble_db_discovery instance. |
| 25 | + * |
| 26 | + * @param _name Name of the instance. |
| 27 | + */ |
| 28 | +#define BLE_DB_DISCOVERY_DEF(_name) \ |
| 29 | + static struct ble_db_discovery _name = {.discovery_in_progress = 0, \ |
| 30 | + .conn_handle = BLE_CONN_HANDLE_INVALID}; \ |
| 31 | + NRF_SDH_BLE_OBSERVER(_name##_obs, ble_db_discovery_on_ble_evt, &_name, HIGH); |
| 32 | + |
| 33 | +/** |
| 34 | + * @brief BLE database discovery event type. |
| 35 | + */ |
| 36 | +enum ble_db_discovery_evt_type { |
| 37 | + /** |
| 38 | + * @brief Event indicating that the discovery of one service is complete. |
| 39 | + */ |
| 40 | + BLE_DB_DISCOVERY_COMPLETE, |
| 41 | + /** |
| 42 | + * @brief Event indicating that the service was not found at the peer. |
| 43 | + */ |
| 44 | + BLE_DB_DISCOVERY_SRV_NOT_FOUND, |
| 45 | + /** |
| 46 | + * @brief Event indicating that the DB discovery instance is available. |
| 47 | + */ |
| 48 | + BLE_DB_DISCOVERY_AVAILABLE, |
| 49 | + /** |
| 50 | + * @brief Event indicating that an internal error has occurred in the DB Discovery module. |
| 51 | + * |
| 52 | + * This could typically be because of the SoftDevice API returning an error code during the |
| 53 | + * DB discover. |
| 54 | + */ |
| 55 | + BLE_DB_DISCOVERY_ERROR, |
| 56 | +}; |
| 57 | + |
| 58 | +/** |
| 59 | + * @brief BLE database discovery event. |
| 60 | + */ |
| 61 | +struct ble_db_discovery_evt { |
| 62 | + /** |
| 63 | + * @brief Type of event. |
| 64 | + */ |
| 65 | + enum ble_db_discovery_evt_type evt_type; |
| 66 | + /** |
| 67 | + * @brief Handle of the connection for which this event has occurred. |
| 68 | + */ |
| 69 | + uint16_t conn_handle; |
| 70 | + union { |
| 71 | + /** |
| 72 | + * @brief Structure containing the information about the GATT Database at the |
| 73 | + * server. |
| 74 | + * |
| 75 | + * This will be filled when the event type is @ref BLE_DB_DISCOVERY_COMPLETE. The |
| 76 | + * UUID field of this will be filled when the event type is @ref |
| 77 | + * BLE_DB_DISCOVERY_SRV_NOT_FOUND. |
| 78 | + */ |
| 79 | + struct ble_gatt_db_srv discovered_db; |
| 80 | + /** |
| 81 | + * @brief nRF Error code indicating the type of error which occurred in the DB |
| 82 | + * Discovery module. |
| 83 | + * |
| 84 | + * This will be filled when the event type is @ref BLE_DB_DISCOVERY_ERROR. |
| 85 | + */ |
| 86 | + struct { |
| 87 | + /** |
| 88 | + * @brief nRF Error code indicating the type of error which occurred in the |
| 89 | + * DB Discovery module. |
| 90 | + */ |
| 91 | + uint32_t reason; |
| 92 | + } error; |
| 93 | + } params; |
| 94 | +}; |
| 95 | + |
| 96 | +/* Forward declaration. */ |
| 97 | +struct ble_db_discovery; |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief DB discovery event handler type. |
| 101 | + */ |
| 102 | +typedef void (*ble_db_discovery_evt_handler)(struct ble_db_discovery *db_discovery, |
| 103 | + struct ble_db_discovery_evt *evt); |
| 104 | + |
| 105 | +/** |
| 106 | + * @brief BLE database discovery configuration. |
| 107 | + */ |
| 108 | +struct ble_db_discovery_config { |
| 109 | + /** |
| 110 | + * @brief Event handler to be called by the DB Discovery module. |
| 111 | + */ |
| 112 | + ble_db_discovery_evt_handler evt_handler; |
| 113 | + /** |
| 114 | + * @brief Pointer to BLE GATT Queue instance. |
| 115 | + */ |
| 116 | + const struct ble_gq *gatt_queue; |
| 117 | +}; |
| 118 | + |
| 119 | +/** |
| 120 | + * @brief BLE database discovery user event. |
| 121 | + */ |
| 122 | +struct ble_db_discovery_user_evt { |
| 123 | + /** |
| 124 | + * @brief Pending event. |
| 125 | + */ |
| 126 | + struct ble_db_discovery_evt evt; |
| 127 | + /** |
| 128 | + * @brief Event handler which should be called to raise this event. |
| 129 | + */ |
| 130 | + ble_db_discovery_evt_handler evt_handler; |
| 131 | +}; |
| 132 | + |
| 133 | +/** |
| 134 | + * @brief BLE database discovery. |
| 135 | + */ |
| 136 | +struct ble_db_discovery { |
| 137 | + /** |
| 138 | + * @brief Information related to the current service being discovered. |
| 139 | + * |
| 140 | + * This is intended for internal use during service discovery. |
| 141 | + */ |
| 142 | + struct ble_gatt_db_srv services[CONFIG_BLE_DB_DISCOVERY_MAX_SRV]; |
| 143 | + /** |
| 144 | + * @brief UUID of registered handlers |
| 145 | + */ |
| 146 | + ble_uuid_t registered_uuids[CONFIG_BLE_DB_DISCOVERY_MAX_SRV]; |
| 147 | + /** |
| 148 | + * @brief Instance event handler. |
| 149 | + */ |
| 150 | + ble_db_discovery_evt_handler evt_handler; |
| 151 | + /** |
| 152 | + * @brief BLE GATT Queue instance. |
| 153 | + */ |
| 154 | + const struct ble_gq *gatt_queue; |
| 155 | + /** |
| 156 | + * @brief The number of UUIDs registered with the DB Discovery module. |
| 157 | + */ |
| 158 | + uint32_t num_registered_uuids; |
| 159 | + /** |
| 160 | + * @brief Number of services at the peer's GATT database. |
| 161 | + */ |
| 162 | + uint8_t srv_count; |
| 163 | + /** |
| 164 | + * @brief Index of the current characteristic being discovered. |
| 165 | + * |
| 166 | + * This is intended for internal use during service discovery. |
| 167 | + */ |
| 168 | + uint8_t curr_char_ind; |
| 169 | + /** |
| 170 | + * @brief Index of the current service being discovered. |
| 171 | + * |
| 172 | + * This is intended for internal use during service discovery. |
| 173 | + */ |
| 174 | + uint8_t curr_srv_ind; |
| 175 | + /** |
| 176 | + * @brief Number of service discoveries made, both successful and unsuccessful. |
| 177 | + */ |
| 178 | + uint8_t discoveries_count; |
| 179 | + /** |
| 180 | + * @brief Variable to indicate whether there is a service discovery in progress. |
| 181 | + */ |
| 182 | + bool discovery_in_progress; |
| 183 | + /** |
| 184 | + * @brief Connection handle on which the discovery is started. |
| 185 | + */ |
| 186 | + uint16_t conn_handle; |
| 187 | + /** |
| 188 | + * @brief The index to the pending user event array, pointing to the last added pending user |
| 189 | + * event. |
| 190 | + */ |
| 191 | + uint32_t pending_usr_evt_index; |
| 192 | + /** |
| 193 | + * @brief Whenever a discovery related event is to be raised to a user module, it is stored |
| 194 | + * in this array first. |
| 195 | + * |
| 196 | + * When all expected services have been discovered, all pending events are sent to the |
| 197 | + * corresponding user modules. |
| 198 | + */ |
| 199 | + struct ble_db_discovery_user_evt pending_usr_evts[CONFIG_BLE_DB_DISCOVERY_MAX_SRV]; |
| 200 | +}; |
| 201 | + |
| 202 | +/** |
| 203 | + * @brief Initialize the DB Discovery module. |
| 204 | + * |
| 205 | + * @param[in] db_discovery BLE DB discovery instance. |
| 206 | + * @param[in] db_config DB discovery initialization structure. |
| 207 | + * |
| 208 | + * @retval NRF_SUCCESS On successful initialization. |
| 209 | + * @retval NRF_ERROR_NULL If the initialization structure was NULL or |
| 210 | + * the structure content is empty. |
| 211 | + */ |
| 212 | +uint32_t ble_db_discovery_init(struct ble_db_discovery *db_discovery, |
| 213 | + struct ble_db_discovery_config *db_config); |
| 214 | + |
| 215 | +/** |
| 216 | + * @brief Start the discovery of the GATT database at the server. |
| 217 | + * |
| 218 | + * @param[out] db_discovery BLE DB Discovery instance. |
| 219 | + * @param[in] conn_handle The handle of the connection for which the discovery should be |
| 220 | + * started. |
| 221 | + * |
| 222 | + * @retval NRF_SUCCESS Operation success. |
| 223 | + * @retval NRF_ERROR_NULL When a NULL pointer is passed as input. |
| 224 | + * @retval NRF_ERROR_INVALID_STATE If this function is called without calling the |
| 225 | + * @ref ble_db_discovery_init, or without calling |
| 226 | + * @ref ble_db_discovery_service_register. |
| 227 | + * @retval NRF_ERROR_BUSY If a discovery is already in progress using @p db_discovery. |
| 228 | + * Use a different @ref struct ble_db_discovery structure, |
| 229 | + * or wait for a DB Discovery event before retrying. |
| 230 | + * @return This API propagates the error code returned by functions: |
| 231 | + * @ref nrf_ble_gq_conn_handle_register and @ref nrf_ble_gq_item_add. |
| 232 | + */ |
| 233 | +uint32_t ble_db_discovery_start(struct ble_db_discovery *db_discovery, uint16_t conn_handle); |
| 234 | + |
| 235 | +/** |
| 236 | + * @brief Register service with the DB Discovery module. |
| 237 | + * |
| 238 | + * @details The application can use this function to inform which service it is interested in |
| 239 | + * discovering at the server. |
| 240 | + * |
| 241 | + * @param[in] db_discovery BLE DB discovery instance. |
| 242 | + * @param[in] uuid UUID of the service to be discovered at the server. |
| 243 | + * |
| 244 | + * @note The total number of services that can be discovered by this module is @ref |
| 245 | + * CONFIG_BLE_DB_DISCOVERY_MAX_SRV. This effectively means that the maximum number of |
| 246 | + * registrations possible is equal to the @ref CONFIG_BLE_DB_DISCOVERY_MAX_SRV. |
| 247 | + * |
| 248 | + * @retval NRF_SUCCESS Operation success. |
| 249 | + * @retval NRF_ERROR_NULL When a NULL pointer is passed as input. |
| 250 | + * @retval NRF_ERROR_INVALID_STATE If this function is called without calling the |
| 251 | + * @ref ble_db_discovery_init. |
| 252 | + * @retval NRF_ERROR_NO_MEM The maximum number of registrations allowed by this module |
| 253 | + * has been reached. |
| 254 | + */ |
| 255 | +uint32_t ble_db_discovery_service_register(struct ble_db_discovery *db_discovery, |
| 256 | + const ble_uuid_t *const uuid); |
| 257 | + |
| 258 | +/** |
| 259 | + * @brief Application's BLE Stack event handler. |
| 260 | + * |
| 261 | + * @param[in] ble_evt BLE event received. |
| 262 | + * @param[in,out] context BLE DB discovery instance. |
| 263 | + */ |
| 264 | +void ble_db_discovery_on_ble_evt(ble_evt_t const *ble_evt, void *context); |
| 265 | + |
| 266 | +#endif /* BLE_DB_DISCOVERY_H__ */ |
| 267 | + |
| 268 | +/** @} */ |
0 commit comments