Skip to content

Commit a80371e

Browse files
Thalleynordicjm
authored andcommitted
[nrf fromtree] 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 <[email protected]> (cherry picked from commit e4c5bb9)
1 parent ada90ac commit a80371e

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
@@ -721,7 +721,8 @@ uint16_t bt_gatt_attr_get_handle(const struct bt_gatt_attr *attr);
721721
*
722722
* @param attr A Characteristic Attribute.
723723
*
724-
* @note The ``user_data`` of the attribute must of type @ref bt_gatt_chrc.
724+
* @note The ``user_data`` of the attribute must of type @ref bt_gatt_chrc and the ``uuid`` shall be
725+
* BT_UUID_GATT_CHRC
725726
*
726727
* @return the handle of the corresponding Characteristic Value. The value will
727728
* 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>
@@ -2859,12 +2862,20 @@ struct bt_gatt_attr *bt_gatt_find_by_uuid(const struct bt_gatt_attr *attr,
28592862
const struct bt_uuid *uuid)
28602863
{
28612864
struct bt_gatt_attr *found = NULL;
2862-
uint16_t start_handle = bt_gatt_attr_value_handle(attr);
2863-
uint16_t end_handle = start_handle && attr_count ?
2864-
start_handle + attr_count : 0xffff;
2865+
uint16_t start_handle = bt_gatt_attr_get_handle(attr);
2866+
uint16_t end_handle = start_handle && attr_count
2867+
? MIN(start_handle + attr_count, BT_ATT_LAST_ATTRIBUTE_HANDLE)
2868+
: BT_ATT_LAST_ATTRIBUTE_HANDLE;
2869+
2870+
if (attr != NULL && start_handle == 0U) {
2871+
/* If start_handle is 0 then `attr` is not in our database, and should not be used
2872+
* as a starting point for the search
2873+
*/
2874+
LOG_DBG("Could not find handle of attr %p", attr);
2875+
return NULL;
2876+
}
28652877

2866-
bt_gatt_foreach_attr_type(start_handle, end_handle, uuid, NULL, 1,
2867-
find_next, &found);
2878+
bt_gatt_foreach_attr_type(start_handle, end_handle, uuid, NULL, 1, find_next, &found);
28682879

28692880
return found;
28702881
}

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
@@ -271,12 +271,8 @@ static void check_ccc_handle(void)
271271
struct bt_gatt_attr *service_notify_attr =
272272
bt_gatt_find_by_uuid(NULL, 0, &notify_characteristic_uuid.uuid);
273273

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

282278
__ASSERT(actual_ccc_handle == CCC_HANDLE,

0 commit comments

Comments
 (0)