Skip to content

Commit e4c5bb9

Browse files
Thalleykartben
authored andcommitted
Bluetooth: GATT: Change get_handle function of find_by_uuid
bt_gatt_find_by_uuid used bt_gatt_attr_value_handle but that function only works to get the value handle of a characteristic declaration, i.e. if the UUID is not BT_UUID_GATT_CHRC then it would always return handle = 0. This meant that bt_gatt_find_by_uuid would always use handle = 0 as the starting handle for non-BT_UUID_GATT_CHRC attributes, instead of the handle of the provided attr. This was not an issue for any UUIDs that may only exist once on a GATT server, which is most UUIDs, but for UUIDs like the BT_UUID_TBS_* UUIDs that may be multiple instances of, it would always return the first attribute rather than the one starting from the provided start attr. This commit also ensures that we do not overflow the `end_handle` when adding 2 uint16_t values. Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
1 parent 8b7beff commit e4c5bb9

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

include/zephyr/bluetooth/gatt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,8 @@ uint16_t bt_gatt_attr_get_handle(const struct bt_gatt_attr *attr);
717717
*
718718
* @param attr A Characteristic Attribute.
719719
*
720-
* @note The ``user_data`` of the attribute must of type @ref bt_gatt_chrc.
720+
* @note The ``user_data`` of the attribute must of type @ref bt_gatt_chrc and the ``uuid`` shall be
721+
* BT_UUID_GATT_CHRC
721722
*
722723
* @return the handle of the corresponding Characteristic Value. The value will
723724
* be zero (the invalid handle) if @p attr was not a characteristic

subsys/bluetooth/host/gatt.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* SPDX-License-Identifier: Apache-2.0
88
*/
99

10+
#include <stdint.h>
11+
12+
#include <zephyr/bluetooth/att.h>
1013
#include <zephyr/kernel.h>
1114
#include <string.h>
1215
#include <errno.h>
@@ -2852,12 +2855,20 @@ struct bt_gatt_attr *bt_gatt_find_by_uuid(const struct bt_gatt_attr *attr,
28522855
const struct bt_uuid *uuid)
28532856
{
28542857
struct bt_gatt_attr *found = NULL;
2855-
uint16_t start_handle = bt_gatt_attr_value_handle(attr);
2856-
uint16_t end_handle = start_handle && attr_count ?
2857-
start_handle + attr_count : 0xffff;
2858+
uint16_t start_handle = bt_gatt_attr_get_handle(attr);
2859+
uint16_t end_handle = start_handle && attr_count
2860+
? MIN(start_handle + attr_count, BT_ATT_LAST_ATTRIBUTE_HANDLE)
2861+
: BT_ATT_LAST_ATTRIBUTE_HANDLE;
2862+
2863+
if (attr != NULL && start_handle == 0U) {
2864+
/* If start_handle is 0 then `attr` is not in our database, and should not be used
2865+
* as a starting point for the search
2866+
*/
2867+
LOG_DBG("Could not find handle of attr %p", attr);
2868+
return NULL;
2869+
}
28582870

2859-
bt_gatt_foreach_attr_type(start_handle, end_handle, uuid, NULL, 1,
2860-
find_next, &found);
2871+
bt_gatt_foreach_attr_type(start_handle, end_handle, uuid, NULL, 1, find_next, &found);
28612872

28622873
return found;
28632874
}

tests/bsim/bluetooth/host/gatt/ccc_store/src/peripheral.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,8 @@ static void check_ccc_handle(void)
230230
__ASSERT(actual_val_handle == VAL_HANDLE,
231231
"Please update the VAL_HANDLE define (actual_val_handle=%d)", actual_val_handle);
232232

233-
struct bt_gatt_attr attr = {
234-
.uuid = BT_UUID_GATT_CHRC,
235-
.user_data = &(struct bt_gatt_chrc){ .value_handle = actual_val_handle }};
236-
237-
struct bt_gatt_attr *ccc_attr = bt_gatt_find_by_uuid(&attr, 0, BT_UUID_GATT_CCC);
233+
struct bt_gatt_attr *ccc_attr =
234+
bt_gatt_find_by_uuid(service_notify_attr, 0, BT_UUID_GATT_CCC);
238235
uint16_t actual_ccc_handle = bt_gatt_attr_get_handle(ccc_attr);
239236

240237
__ASSERT(actual_ccc_handle == CCC_HANDLE,

tests/bsim/bluetooth/host/security/ccc_update/src/peripheral.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,8 @@ static void check_ccc_handle(void)
272272
struct bt_gatt_attr *service_notify_attr =
273273
bt_gatt_find_by_uuid(NULL, 0, &notify_characteristic_uuid.uuid);
274274

275-
struct bt_gatt_attr attr = {
276-
.uuid = BT_UUID_GATT_CHRC,
277-
.user_data = &(struct bt_gatt_chrc){
278-
.value_handle = bt_gatt_attr_get_handle(service_notify_attr)}};
279-
280-
struct bt_gatt_attr *ccc_attr = bt_gatt_find_by_uuid(&attr, 0, BT_UUID_GATT_CCC);
275+
struct bt_gatt_attr *ccc_attr =
276+
bt_gatt_find_by_uuid(service_notify_attr, 0, BT_UUID_GATT_CCC);
281277
uint16_t actual_ccc_handle = bt_gatt_attr_get_handle(ccc_attr);
282278

283279
__ASSERT(actual_ccc_handle == CCC_HANDLE,

0 commit comments

Comments
 (0)