gatt: Make use of gatt_db_attribute_notify
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Fri, 21 Jan 2022 20:52:14 +0000 (12:52 -0800)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:53 +0000 (14:55 +0530)
This makes use of gatt_db_attribute_notify to send indications of
Service Changed.

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

index cadd718..75ffd57 100644 (file)
@@ -446,6 +446,18 @@ find_device_state_from_address(struct btd_gatt_database *database, const bdaddr_
 }
 #endif
 
+static struct device_state *
+find_device_state_by_att(struct btd_gatt_database *database, struct bt_att *att)
+{
+       bdaddr_t bdaddr;
+       uint8_t bdaddr_type;
+
+       if (!get_dst_info(att, &bdaddr, &bdaddr_type))
+               return NULL;
+
+       return find_device_state(database, &bdaddr, bdaddr_type);
+}
+
 static struct device_state *get_device_state(struct btd_gatt_database *database,
                                                struct bt_att *att)
 {
@@ -1519,17 +1531,6 @@ static void populate_devinfo_service(struct btd_gatt_database *database)
 
        database_add_record(database, service);
 }
-static void register_core_services(struct btd_gatt_database *database)
-{
-       gatt_db_ccc_register(database->db, gatt_ccc_read_cb, gatt_ccc_write_cb,
-                                                       NULL, database);
-
-       populate_gap_service(database);
-       populate_gatt_service(database);
-
-       populate_devinfo_service(database);
-
-}
 
 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
 struct notify_indicate {
@@ -1836,6 +1837,49 @@ remove:
        }
 }
 
+static void gatt_notify_cb(struct gatt_db_attribute *attrib,
+                                       struct gatt_db_attribute *ccc,
+                                       const uint8_t *value, size_t len,
+                                       struct bt_att *att, void *user_data)
+{
+       struct btd_gatt_database *database = user_data;
+       struct notify notify;
+
+       memset(&notify, 0, sizeof(notify));
+
+       notify.database = database;
+       notify.handle = gatt_db_attribute_get_handle(attrib);
+       notify.ccc_handle = gatt_db_attribute_get_handle(ccc);
+       notify.value = (void *) value;
+       notify.len = len;
+
+       if (attrib == database->svc_chngd)
+               notify.conf = service_changed_conf;
+
+       /* If a specific att is provided notify only to that device */
+       if (att) {
+               struct device_state *state;
+
+               state = find_device_state_by_att(database, att);
+               if (!state)
+                       return;
+
+               send_notification_to_device(state, &notify);
+       } else
+               queue_foreach(database->device_states,
+                               send_notification_to_device, &notify);
+}
+
+static void register_core_services(struct btd_gatt_database *database)
+{
+       gatt_db_ccc_register(database->db, gatt_ccc_read_cb, gatt_ccc_write_cb,
+                                               gatt_notify_cb, database);
+
+       populate_gap_service(database);
+       populate_gatt_service(database);
+       populate_devinfo_service(database);
+}
+
 static void send_notification_to_devices(struct btd_gatt_database *database,
                                        uint16_t handle, uint8_t *value,
                                        uint16_t len, uint16_t ccc_handle,
@@ -1882,8 +1926,8 @@ static void send_service_changed(struct btd_gatt_database *database,
        put_le16(start, value);
        put_le16(end, value + 2);
 
-       send_notification_to_devices(database, handle, value, sizeof(value),
-                                       ccc_handle, service_changed_conf, NULL);
+       gatt_db_attribute_notify(database->svc_chngd, value, sizeof(value),
+                               NULL);
 }
 
 static void gatt_db_service_added(struct gatt_db_attribute *attrib,
@@ -4633,6 +4677,6 @@ void btd_gatt_database_restore_svc_chng_ccc(struct btd_gatt_database *database)
        put_le16(0x0001, value);
        put_le16(0xffff, value + 2);
 
-       send_notification_to_devices(database, handle, value, sizeof(value),
-                                       ccc_handle, service_changed_conf, NULL);
+       gatt_db_attribute_notify(database->svc_chngd, value, sizeof(value),
+                               NULL);
 }