Skip to content
Permalink
Browse files

Bluetooth: GATT: Add bt_gatt_foreach_attr_type

This adds bt_gatt_foreach_attr_type which can match attribute by UUID
and/or attribute user_data, in addition of that the user can also limit
the number of matches.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
  • Loading branch information...
Vudentz authored and jhedberg committed May 24, 2019
1 parent 58d839b commit a5c07aa7fbc4b1c0dbc8998e20d1ae8a10598540
Showing with 65 additions and 8 deletions.
  1. +25 −2 include/bluetooth/gatt.h
  2. +40 −6 subsys/bluetooth/host/gatt.c
@@ -339,6 +339,24 @@ enum {
typedef u8_t (*bt_gatt_attr_func_t)(const struct bt_gatt_attr *attr,
void *user_data);

/** @brief Attribute iterator by type.
*
* Iterate attributes in the given range matching given UUID and/or data.
*
* @param start_handle Start handle.
* @param end_handle End handle.
* @param uuid UUID to match, passing NULL skips UUID matching.
* @param attr_data Attribute data to match, passing NULL skips data matching.
* @param num_matches Number matches, passing 0 makes it unlimited.
* @param func Callback function.
* @param user_data Data to pass to the callback.
*/
void bt_gatt_foreach_attr_type(u16_t start_handle, u16_t end_handle,
const struct bt_uuid *uuid,
const void *attr_data, uint16_t num_matches,
bt_gatt_attr_func_t func,
void *user_data);

/** @brief Attribute iterator.
*
* Iterate attributes in the given range.
@@ -348,8 +366,13 @@ typedef u8_t (*bt_gatt_attr_func_t)(const struct bt_gatt_attr *attr,
* @param func Callback function.
* @param user_data Data to pass to the callback.
*/
void bt_gatt_foreach_attr(u16_t start_handle, u16_t end_handle,
bt_gatt_attr_func_t func, void *user_data);
static inline void bt_gatt_foreach_attr(u16_t start_handle, u16_t end_handle,
bt_gatt_attr_func_t func,
void *user_data)
{
bt_gatt_foreach_attr_type(start_handle, end_handle, NULL, NULL, 0, func,
user_data);
}

/** @brief Iterate to the next attribute
*
@@ -977,6 +977,8 @@ ssize_t bt_gatt_attr_read_chrc(struct bt_conn *conn,

static u8_t gatt_foreach_iter(const struct bt_gatt_attr *attr,
u16_t start_handle, u16_t end_handle,
const struct bt_uuid *uuid,
const void *attr_data, uint16_t *num_matches,
bt_gatt_attr_func_t func, void *user_data)
{
/* Stop if over the requested range */
@@ -989,15 +991,35 @@ static u8_t gatt_foreach_iter(const struct bt_gatt_attr *attr,
return BT_GATT_ITER_CONTINUE;
}

/* Match attribute UUID if set */
if (uuid && bt_uuid_cmp(uuid, attr->uuid)) {
return BT_GATT_ITER_CONTINUE;
}

/* Match attribute user_data if set */
if (attr_data && attr_data != attr->user_data) {
return BT_GATT_ITER_CONTINUE;
}

if (*num_matches) {
*num_matches -= 1;
}

return func(attr, user_data);
}

void bt_gatt_foreach_attr(u16_t start_handle, u16_t end_handle,
bt_gatt_attr_func_t func, void *user_data)
void bt_gatt_foreach_attr_type(u16_t start_handle, u16_t end_handle,
const struct bt_uuid *uuid,
const void *attr_data, uint16_t num_matches,
bt_gatt_attr_func_t func, void *user_data)
{
struct bt_gatt_service *svc;
int i;

if (!num_matches) {
num_matches = UINT16_MAX;
}

if (start_handle <= last_static_handle) {
const struct bt_gatt_service_static *static_svc;
u16_t handle;
@@ -1019,12 +1041,18 @@ void bt_gatt_foreach_attr(u16_t start_handle, u16_t end_handle,
attr.handle = handle;

if (gatt_foreach_iter(&attr, start_handle,
end_handle, func,
user_data) ==
end_handle, uuid,
attr_data, &num_matches,
func, user_data) ==
BT_GATT_ITER_STOP) {
return;
}
}

/* Stop if number of matches has reached 0 */
if (!num_matches) {
return;
}
}
}

@@ -1042,12 +1070,18 @@ void bt_gatt_foreach_attr(u16_t start_handle, u16_t end_handle,
for (i = 0; i < svc->attr_count; i++) {
struct bt_gatt_attr *attr = &svc->attrs[i];

if (gatt_foreach_iter(attr, start_handle,
end_handle, func, user_data) ==
if (gatt_foreach_iter(attr, start_handle, end_handle,
uuid, attr_data, &num_matches,
func, user_data) ==
BT_GATT_ITER_STOP) {
return;
}
}

/* Stop if number of matches has reached 0 */
if (!num_matches) {
return;
}
}
}

0 comments on commit a5c07aa

Please sign in to comment.
You can’t perform that action at this time.