Skip to content

Commit

Permalink
gatt-db: Fix gatt_db_attribute_notify
Browse files Browse the repository at this point in the history
gatt_db_attribute_notify was only accepting passing the Characteristic
Declaration instead of accepting its value as well,
gatt_db_service_foreach_desc also have similar limitation so both have
been updated to allow working with both value and declaration.
  • Loading branch information
Vudentz committed Mar 14, 2022
1 parent 8fb8f9e commit 411d63e
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions src/shared/gatt-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -1528,32 +1528,71 @@ void gatt_db_service_foreach_char(struct gatt_db_attribute *attrib,
gatt_db_service_foreach(attrib, &characteristic_uuid, func, user_data);
}

static int gatt_db_attribute_get_index(struct gatt_db_attribute *attrib)
{
struct gatt_db_service *service;
int index;

if (!attrib)
return -1;

service = attrib->service;
index = attrib->handle - service->attributes[0]->handle;

if (index > (service->num_handles - 1))
return -1;

return index;
}

static struct gatt_db_attribute *
gatt_db_attribute_get_value(struct gatt_db_attribute *attrib)
{
struct gatt_db_service *service;
int index;

if (!attrib)
return NULL;

index = gatt_db_attribute_get_index(attrib);
if (index < 0)
return NULL;

service = attrib->service;

if (!bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
index++;
else if (bt_uuid_cmp(&characteristic_uuid,
&service->attributes[index - 1]->uuid))
return NULL;

return service->attributes[index];
}

void gatt_db_service_foreach_desc(struct gatt_db_attribute *attrib,
gatt_db_attribute_cb_t func,
void *user_data)
{
struct gatt_db_service *service;
struct gatt_db_attribute *attr;
int index;
uint16_t i;

if (!attrib || !func)
return;

/* Return if this attribute is not a characteristic declaration */
if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
attrib = gatt_db_attribute_get_value(attrib);
if (!attrib)
return;

index = gatt_db_attribute_get_index(attrib);
if (index < 0)
return;

service = attrib->service;

/* Start from the attribute following the value handle */
for (i = 0; i < service->num_handles; i++) {
if (service->attributes[i] == attrib) {
i += 2;
break;
}
}

for (; i < service->num_handles; i++) {
for (i = index + 1; i < service->num_handles; i++) {
attr = service->attributes[i];
if (!attr)
continue;
Expand Down Expand Up @@ -2163,8 +2202,8 @@ bool gatt_db_attribute_notify(struct gatt_db_attribute *attrib,
if (!attrib || !attrib->notify_func)
return false;

/* Return if this attribute is not a characteristic declaration */
if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
attrib = gatt_db_attribute_get_value(attrib);
if (!attrib)
return false;

ccc = gatt_db_attribute_get_ccc(attrib);
Expand Down

0 comments on commit 411d63e

Please sign in to comment.