gatt-db: Fix gatt_db_attribute_notify
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Mon, 14 Mar 2022 21:47:02 +0000 (14:47 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:53 +0000 (14:55 +0530)
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.

Signed-off-by: Manika Shrivastava <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/shared/gatt-db.c

index bf9d744..d85ed56 100644 (file)
@@ -1535,32 +1535,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;
@@ -2170,8 +2209,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);