Skip to content
Permalink
Browse files

Bluetooth: GATT: Fix using invalid handle

When reading attributes from static services their handles will not be
set and must be resolved at runtime.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
  • Loading branch information...
Vudentz authored and jhedberg committed Jun 5, 2019
1 parent 1cbda7d commit aec04a02edd1ef134a9ab9891bb134887d1ac41d
Showing with 22 additions and 21 deletions.
  1. +22 −21 subsys/bluetooth/host/gatt.c
@@ -905,17 +905,35 @@ static u8_t get_service_handles(const struct bt_gatt_attr *attr,
return BT_GATT_ITER_CONTINUE;
}

static u16_t find_static_attr(const struct bt_gatt_attr *attr)
{
const struct bt_gatt_service_static *static_svc;
u16_t handle;

for (static_svc = _bt_services_start, handle = 1;
static_svc < _bt_services_end; static_svc++) {
for (int i = 0; i < static_svc->attr_count; i++, handle++) {
if (attr == &static_svc->attrs[i]) {
return handle;
}
}
}

return 0;
}

ssize_t bt_gatt_attr_read_included(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
void *buf, u16_t len, u16_t offset)
{
struct bt_gatt_attr *incl = attr->user_data;
u16_t handle = incl->handle ? : find_static_attr(incl);
struct bt_uuid *uuid = incl->user_data;
struct gatt_incl pdu;
u8_t value_len;

/* first attr points to the start handle */
pdu.start_handle = sys_cpu_to_le16(incl->handle);
pdu.start_handle = sys_cpu_to_le16(handle);
value_len = sizeof(pdu.start_handle) + sizeof(pdu.end_handle);

/*
@@ -929,8 +947,7 @@ ssize_t bt_gatt_attr_read_included(struct bt_conn *conn,
}

/* Lookup for service end handle */
bt_gatt_foreach_attr(incl->handle + 1, 0xffff, get_service_handles,
&pdu);
bt_gatt_foreach_attr(handle + 1, 0xffff, get_service_handles, &pdu);

return bt_gatt_attr_read(conn, attr, buf, len, offset, &pdu, value_len);
}
@@ -949,6 +966,7 @@ ssize_t bt_gatt_attr_read_chrc(struct bt_conn *conn,
u16_t len, u16_t offset)
{
struct bt_gatt_chrc *chrc = attr->user_data;
u16_t handle = attr->handle ? : find_static_attr(attr);
struct gatt_chrc pdu;
u8_t value_len;

@@ -960,7 +978,7 @@ ssize_t bt_gatt_attr_read_chrc(struct bt_conn *conn,
* declaration. All characteristic definitions shall have a
* Characteristic Value declaration.
*/
pdu.value_handle = sys_cpu_to_le16(attr->handle + 1);
pdu.value_handle = sys_cpu_to_le16(handle + 1);

value_len = sizeof(pdu.properties) + sizeof(pdu.value_handle);

@@ -1275,23 +1293,6 @@ ssize_t bt_gatt_attr_read_cpf(struct bt_conn *conn,
sizeof(*value));
}

static u16_t find_static_attr(const struct bt_gatt_attr *attr)
{
const struct bt_gatt_service_static *static_svc;
u16_t handle;

for (static_svc = _bt_services_start, handle = 1;
static_svc < _bt_services_end; static_svc++) {
for (int i = 0; i < static_svc->attr_count; i++, handle++) {
if (attr == &static_svc->attrs[i]) {
return handle;
}
}
}

return 0;
}

struct notify_data {
int err;
u16_t type;

0 comments on commit aec04a0

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