Skip to content

Commit 4b151ae

Browse files
sondrepasilz
andcommitted
bluetooth: services: db_discovery: clean and finish db_discovery
- Remove Hungarian notation - Standardise comments - Add kconfigs - Fix error handling Signed-off-by: Sondre Pettersen <[email protected]> Co-authored-by: Asil Zogby <[email protected]>
1 parent c7f59c4 commit 4b151ae

File tree

3 files changed

+545
-655
lines changed

3 files changed

+545
-655
lines changed

include/ble_db_discovery.h

Lines changed: 137 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,10 @@
1-
/**
2-
* Copyright (c) 2013 - 2021, 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.
1+
/*
2+
* Copyright (c) 2013 - 2025 Nordic Semiconductor ASA
383
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
395
*/
40-
/**@file
6+
/**
7+
* @file
418
*
429
* @defgroup ble_db_discovery Database Discovery
4310
* @{
@@ -71,13 +38,12 @@
7138
#include <zephyr/kernel.h>
7239
#include <ble_gattc.h>
7340
#include <ble_gq.h>
74-
#define BLE_GATT_DB_MAX_CHARS 6
75-
#define BLE_DB_DISCOVERY_MAX_SRV 6
7641

7742
#ifndef BLE_DB_DISCOVERY_H__
7843
#define BLE_DB_DISCOVERY_H__
7944

80-
/**@brief Macro for defining a ble_db_discovery instance.
45+
/**
46+
* @brief Macro for defining a ble_db_discovery instance.
8147
*
8248
* @param _name Name of the instance.
8349
* @hideinitializer
@@ -89,135 +55,171 @@
8955
BLE_DB_DISC_BLE_OBSERVER_PRIO)
9056

9157
struct ble_gatt_db_char {
92-
ble_gattc_char_t
93-
characteristic; /**< Structure containing information about the characteristic. */
94-
uint16_t cccd_handle; /**< CCCD Handle value for this characteristic. This will be set to
95-
BLE_GATT_HANDLE_INVALID if a CCCD is not present at the server. */
96-
uint16_t ext_prop_handle; /**< Extended Properties Handle value for this characteristic.
97-
This will be set to BLE_GATT_HANDLE_INVALID if an Extended
98-
Properties descriptor is not present at the server. */
99-
uint16_t user_desc_handle; /**< User Description Handle value for this characteristic. This
100-
will be set to BLE_GATT_HANDLE_INVALID if a User Description
101-
descriptor is not present at the server. */
102-
uint16_t report_ref_handle; /**< Report Reference Handle value for this characteristic. This
103-
will be set to BLE_GATT_HANDLE_INVALID if a Report Reference
104-
descriptor is not present at the server. */
58+
/* Structure containing information about the characteristic. */
59+
ble_gattc_char_t characteristic;
60+
/* CCCD Handle value for this characteristic. This will be set to
61+
* BLE_GATT_HANDLE_INVALID if a CCCD is not present at the server.
62+
*/
63+
uint16_t cccd_handle;
64+
/* Extended Properties Handle value for this characteristic.
65+
* This will be set to BLE_GATT_HANDLE_INVALID if an Extended
66+
* Properties descriptor is not present at the server.
67+
*/
68+
uint16_t ext_prop_handle;
69+
/* User Description Handle value for this characteristic. This
70+
* will be set to BLE_GATT_HANDLE_INVALID if a User Description
71+
* descriptor is not present at the server.
72+
*/
73+
uint16_t user_desc_handle;
74+
/* Report Reference Handle value for this characteristic. This
75+
* will be set to BLE_GATT_HANDLE_INVALID if a Report Reference
76+
* descriptor is not present at the server.
77+
*/
78+
uint16_t report_ref_handle;
10579
};
10680

10781
struct ble_gatt_db_srv {
108-
ble_uuid_t srv_uuid; /**< UUID of the service. */
109-
uint8_t char_count; /**< Number of characteristics present in the service. */
110-
ble_gattc_handle_range_t handle_range; /**< Service Handle Range. */
111-
struct ble_gatt_db_char
112-
charateristics[BLE_GATT_DB_MAX_CHARS]; /**< Array of information related to the
113-
characteristics present in the service.
114-
This list can extend further than one. */
82+
/* UUID of the service. */
83+
ble_uuid_t srv_uuid;
84+
/* Number of characteristics present in the service. */
85+
uint8_t char_count;
86+
/* Service Handle Range. */
87+
ble_gattc_handle_range_t handle_range;
88+
/* Array of information related to the
89+
* characteristics present in the service.
90+
* This list can extend further than one.
91+
*/
92+
struct ble_gatt_db_char charateristics[CONFIG_BLE_GATT_DB_MAX_CHARS];
11593
};
11694

11795
enum ble_db_discovery_evt_type {
118-
BLE_DB_DISCOVERY_COMPLETE, /**< Event indicating that the discovery of one service is
119-
complete. */
120-
BLE_DB_DISCOVERY_ERROR, /**< Event indicating that an internal error has occurred in the DB
121-
Discovery module. This could typically be because of the
122-
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
124-
peer.*/
125-
BLE_DB_DISCOVERY_AVAILABLE /**< Event indicating that the DB discovery instance is
126-
available.*/
96+
/* Event indicating that the discovery of one service is complete. */
97+
BLE_DB_DISCOVERY_COMPLETE,
98+
/* Event indicating that an internal error has occurred in the DB
99+
* Discovery module. This could typically be because of the
100+
* SoftDevice API returning an error code during the DB discover.
101+
*/
102+
BLE_DB_DISCOVERY_ERROR,
103+
/* Event indicating that the service was not found at thepeer.*/
104+
BLE_DB_DISCOVERY_SRV_NOT_FOUND,
105+
/* Event indicating that the DB discovery instance is available.*/
106+
BLE_DB_DISCOVERY_AVAILABLE
127107
};
128108

129109
struct ble_db_discovery_evt {
130-
enum ble_db_discovery_evt_type evt_type; /**< Type of event. */
131-
uint16_t conn_handle; /**< Handle of the connection for which this event has occurred. */
110+
/* Type of event. */
111+
enum ble_db_discovery_evt_type evt_type;
112+
/* Handle of the connection for which this event has occurred. */
113+
uint16_t conn_handle;
132114
union {
133-
struct ble_gatt_db_srv
134-
discovered_db; /**< Structure containing the information about the GATT
135-
Database at the server. This will be filled when the event
136-
type is @ref BLE_DB_DISCOVERY_COMPLETE. The UUID field of
137-
this will be filled when the event type is @ref
138-
BLE_DB_DISCOVERY_SRV_NOT_FOUND. */
139-
void const *p_db_instance; /**< Pointer to DB discovery instance @ref
140-
ble_db_discovery_t, indicating availability to the new
141-
discovery process. This will be filled when the event
142-
type is @ref BLE_DB_DISCOVERY_AVAILABLE. */
143-
uint32_t err_code; /**< nRF Error code indicating the type of error which occurred
144-
in the DB Discovery module. This will be filled when the event
145-
type is @ref BLE_DB_DISCOVERY_ERROR. */
115+
/* Structure containing the information about the GATT
116+
* Database at the server. This will be filled when the event
117+
* type is @ref BLE_DB_DISCOVERY_COMPLETE. The UUID field of
118+
* this will be filled when the event type is @ref
119+
* BLE_DB_DISCOVERY_SRV_NOT_FOUND.
120+
*/
121+
struct ble_gatt_db_srv discovered_db;
122+
/* Pointer to DB discovery instance @ref
123+
* ble_db_discovery_t, indicating availability to the new
124+
* discovery process. This will be filled when the event
125+
* type is @ref BLE_DB_DISCOVERY_AVAILABLE.
126+
*/
127+
void const *db_instance;
128+
/* nRF Error code indicating the type of error which occurred
129+
* in the DB Discovery module. This will be filled when the event
130+
* type is @ref BLE_DB_DISCOVERY_ERROR.
131+
*/
132+
uint32_t err_code;
146133
} params;
147134
};
148135

149-
typedef void (*ble_db_discovery_evt_handler)(struct ble_db_discovery_evt *p_evt);
136+
typedef void (*ble_db_discovery_evt_handler)(struct ble_db_discovery_evt *evt);
150137

151138
struct ble_db_discovery_init {
152-
ble_db_discovery_evt_handler
153-
evt_handler; /**< Event handler to be called by the DB Discovery module. */
154-
struct ble_gq *p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
139+
/* Event handler to be called by the DB Discovery module. */
140+
ble_db_discovery_evt_handler evt_handler;
141+
/* Pointer to BLE GATT Queue instance. */
142+
struct ble_gq *gatt_queue;
155143
};
156144

157145
struct ble_db_discovery_user_evt {
158-
struct ble_db_discovery_evt evt; /**< Pending event. */
159-
ble_db_discovery_evt_handler
160-
evt_handler; /**< Event handler which should be called to raise this event. */
146+
/* Pending event. */
147+
struct ble_db_discovery_evt evt;
148+
/* Event handler which should be called to raise this event. */
149+
ble_db_discovery_evt_handler evt_handler;
161150
};
162151

163152
struct ble_db_discovery {
164-
struct ble_gatt_db_srv
165-
services[BLE_DB_DISCOVERY_MAX_SRV]; /**< Information related to the current service
166-
being discovered. This is intended for
167-
internal use during service discovery.*/
168-
uint8_t srv_count; /**< Number of services at the peer's GATT database.*/
169-
uint8_t curr_char_ind; /**< Index of the current characteristic being discovered. This is
170-
intended for internal use during service discovery.*/
171-
uint8_t curr_srv_ind; /**< Index of the current service being discovered. This is intended
172-
for internal use during service discovery.*/
173-
uint8_t discoveries_count; /**< Number of service discoveries made, both successful and
174-
unsuccessful. */
175-
bool discovery_in_progress; /**< Variable to indicate whether there is a service discovery
176-
in progress. */
177-
uint16_t conn_handle; /**< Connection handle on which the discovery is started. */
178-
uint32_t pending_usr_evt_index; /**< The index to the pending user event array, pointing to
179-
the last added pending user event. */
180-
struct ble_db_discovery_user_evt pending_usr_evts
181-
[BLE_DB_DISCOVERY_MAX_SRV]; /**< Whenever a discovery related event is to be raised
182-
to a user module, it is stored in this array first.
183-
When all expected services have been discovered, all
184-
pending events are sent to the corresponding user
185-
modules. */
153+
/* Information related to the current service
154+
* being discovered. This is intended for
155+
* internal use during service discovery.
156+
*/
157+
struct ble_gatt_db_srv services[CONFIG_BLE_DB_DISCOVERY_MAX_SRV];
158+
/* Number of services at the peer's GATT database.*/
159+
uint8_t srv_count;
160+
/* Index of the current characteristic being discovered. This is
161+
* intended for internal use during service discovery.
162+
*/
163+
uint8_t curr_char_ind;
164+
/* Index of the current service being discovered. This is intended
165+
* for internal use during service discovery.
166+
*/
167+
uint8_t curr_srv_ind;
168+
/* Number of service discoveries made, both successful and unsuccessful. */
169+
uint8_t discoveries_count;
170+
/* Variable to indicate whether there is a service discovery in progress. */
171+
bool discovery_in_progress;
172+
/* Connection handle on which the discovery is started. */
173+
uint16_t conn_handle;
174+
/* The index to the pending user event array, pointing to
175+
* the last added pending user event.
176+
*/
177+
uint32_t pending_usr_evt_index;
178+
/* Whenever a discovery related event is to be raised
179+
* to a user module, it is stored in this array first.
180+
* When all expected services have been discovered, all
181+
* pending events are sent to the corresponding user
182+
* modules.
183+
*/
184+
struct ble_db_discovery_user_evt pending_usr_evts[CONFIG_BLE_DB_DISCOVERY_MAX_SRV];
186185
};
187186

188-
/**@brief Function for initializing the DB Discovery module.
187+
/**
188+
* @brief Function for initializing the DB Discovery module.
189189
*
190-
* @param[in] p_db_init Pointer to DB discovery initialization structure.
190+
* @param[in] db_init Pointer to DB discovery initialization structure.
191191
*
192192
* @retval NRF_SUCCESS On successful initialization.
193193
* @retval NRF_ERROR_NULL If the initialization structure was NULL or
194194
* the structure content is empty.
195195
*/
196-
uint32_t ble_db_discovery_init(struct ble_db_discovery_init *p_db_init);
196+
uint32_t ble_db_discovery_init(struct ble_db_discovery_init *db_init);
197197

198-
/**@brief Function for closing the DB Discovery module.
198+
/**
199+
* @brief Function for closing the DB Discovery module.
199200
*
200201
* @details This function will clear up any internal variables and states maintained by the
201202
* module. To re-use the module after calling this function, the function @ref
202203
* ble_db_discovery_init must be called again. When using more than one DB Discovery
203204
* instance, this function should be called for each instance.
204205
*
205-
* @param[out] p_db_discovery Pointer to the DB discovery structure.
206+
* @param[out] db_discovery Pointer to the DB discovery structure.
206207
*
207208
* @retval NRF_SUCCESS Operation success.
208209
*/
209-
uint32_t ble_db_discovery_close(struct ble_db_discovery *const p_db_discovery);
210+
uint32_t ble_db_discovery_close(struct ble_db_discovery *const db_discovery);
210211

211-
/**@brief Function for registering with the DB Discovery module.
212+
/**
213+
* @brief Function for registering with the DB Discovery module.
212214
*
213215
* @details The application can use this function to inform which service it is interested in
214216
* discovering at the server.
215217
*
216-
* @param[in] p_uuid Pointer to the UUID of the service to be discovered at the server.
218+
* @param[in] uuid Pointer to the UUID of the service to be discovered at the server.
217219
*
218220
* @note The total number of services that can be discovered by this module is @ref
219-
* BLE_DB_DISCOVERY_MAX_SRV. This effectively means that the maximum number of
220-
* registrations possible is equal to the @ref BLE_DB_DISCOVERY_MAX_SRV.
221+
* CONFIG_BLE_DB_DISCOVERY_MAX_SRV. This effectively means that the maximum number of
222+
* registrations possible is equal to the @ref CONFIG_BLE_DB_DISCOVERY_MAX_SRV.
221223
*
222224
* @retval NRF_SUCCESS Operation success.
223225
* @retval NRF_ERROR_NULL When a NULL pointer is passed as input.
@@ -226,11 +228,12 @@ uint32_t ble_db_discovery_close(struct ble_db_discovery *const p_db_discovery);
226228
* @retval NRF_ERROR_NO_MEM The maximum number of registrations allowed by this module
227229
* has been reached.
228230
*/
229-
uint32_t ble_db_discovery_evt_register(const ble_uuid_t *const p_uuid);
231+
uint32_t ble_db_discovery_evt_register(const ble_uuid_t *const uuid);
230232

231-
/**@brief Function for starting the discovery of the GATT database at the server.
233+
/**
234+
* @brief Function for starting the discovery of the GATT database at the server.
232235
*
233-
* @param[out] p_db_discovery Pointer to the DB Discovery structure.
236+
* @param[out] db_discovery Pointer to the DB Discovery structure.
234237
* @param[in] conn_handle The handle of the connection for which the discovery should be
235238
* started.
236239
*
@@ -240,20 +243,21 @@ uint32_t ble_db_discovery_evt_register(const ble_uuid_t *const p_uuid);
240243
* @ref ble_db_discovery_init, or without calling
241244
* @ref ble_db_discovery_evt_register.
242245
* @retval NRF_ERROR_BUSY If a discovery is already in progress using
243-
* @p p_db_discovery. Use a different @ref ble_db_discovery_t
246+
* @p db_discovery. Use a different @ref ble_db_discovery
244247
* structure, or wait for a DB Discovery event before retrying.
245248
* @return This API propagates the error code returned by functions:
246249
* @ref nrf_ble_gq_conn_handle_register and @ref
247250
* nrf_ble_gq_item_add.
248251
*/
249-
uint32_t ble_db_discovery_start(struct ble_db_discovery *p_db_discovery, uint16_t conn_handle);
252+
uint32_t ble_db_discovery_start(struct ble_db_discovery *db_discovery, uint16_t conn_handle);
250253

251-
/**@brief Function for handling the Application's BLE Stack events.
254+
/**
255+
* @brief Function for handling the Application's BLE Stack events.
252256
*
253-
* @param[in] p_ble_evt Pointer to the BLE event received.
254-
* @param[in,out] p_context Pointer to the DB Discovery structure.
257+
* @param[in] ble_evt Pointer to the BLE event received.
258+
* @param[in,out] context Pointer to the DB Discovery structure.
255259
*/
256-
void ble_db_discovery_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context);
260+
void ble_db_discovery_on_ble_evt(ble_evt_t const *ble_evt, void *context);
257261

258262
#endif // BLE_DB_DISCOVERY_H__
259263

lib/ble_db_discovery/Kconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,21 @@ menuconfig BLE_DB_DISCOVERY
1010
select EXPERIMENTAL
1111
help
1212
database discovery.
13+
14+
if BLE_DB_DISCOVERY
15+
16+
config BLE_GATT_DB_MAX_CHARS
17+
int "database max characteristics"
18+
range 1 6
19+
default 6
20+
help
21+
The maximum number of database characteristics.
22+
23+
config BLE_DB_DISCOVERY_MAX_SRV
24+
int "max discovered services"
25+
range 1 6
26+
default 6
27+
help
28+
The total number of services that can be discovered by this module
29+
30+
endif # BLE_DB_DISCOVERY

0 commit comments

Comments
 (0)