UPSTREAM: core/device: Fix not calling accept callback

For GATT based services accept is used to indicate that there is a new
connection available so it needs to be called immediately once ATT
connects so the driver can register for notification, etc.

This also renames probe_gatt_profile to add_gatt_service which is what
in fact it was doing.

BUG=chromium:547822

Change-Id: I70670e7ba75fdf19811e1f5dc592673f929bc1d9
Reviewed-on: https://chromium-review.googlesource.com/308734
Commit-Ready: Jakub x Jakub Pawlowski <jpawlowski@chromium.org>
Tested-by: Jakub x Jakub Pawlowski <jpawlowski@chromium.org>
Reviewed-by: Arman Uguray <armansito@chromium.org>
Reviewed-by: Jakub x Jakub Pawlowski <jpawlowski@chromium.org>
diff --git a/src/device.c b/src/device.c
index e35d885..c260c22 100644
--- a/src/device.c
+++ b/src/device.c
@@ -3235,7 +3235,7 @@
 	return true;
 }
 
-static void probe_gatt_profile(struct gatt_db_attribute *attr, void *user_data)
+static void add_gatt_service(struct gatt_db_attribute *attr, void *user_data)
 {
 	struct btd_device *device = user_data;
 	struct btd_service *service;
@@ -3247,6 +3247,11 @@
 	gatt_db_attribute_get_service_uuid(attr, &uuid);
 	bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
 
+	/* Check if service was already probed */
+	l = find_service_with_uuid(device->services, uuid_str);
+	if (l)
+		return;
+
 	/* Add UUID and probe service */
 	btd_device_add_uuid(device, uuid_str);
 
@@ -3261,15 +3266,17 @@
 	service = l->data;
 	profile = btd_service_get_profile(service);
 
-	/* Don't claim attributes of external profiles */
-	if (profile->external)
-		return;
+	/* Claim attributes of internal profiles */
+	if (!profile->external) {
+		/* Mark the service as claimed by the existing profile. */
+		gatt_db_service_set_claimed(attr, true);
+	}
 
-	/* Mark the service as claimed by the existing profile. */
-	gatt_db_service_set_claimed(attr, true);
+	/* Notify driver about the new connection */
+	service_accept(service);
 }
 
-static void device_probe_gatt_profiles(struct btd_device *device)
+static void device_add_gatt_services(struct btd_device *device)
 {
 	char addr[18];
 
@@ -3280,7 +3287,7 @@
 		return;
 	}
 
-	gatt_db_foreach_service(device->db, NULL, probe_gatt_profile, device);
+	gatt_db_foreach_service(device->db, NULL, add_gatt_service, device);
 }
 
 static void device_accept_gatt_profiles(struct btd_device *device)
@@ -3328,16 +3335,12 @@
 {
 	struct btd_device *device = user_data;
 	GSList *new_service = NULL;
-	bt_uuid_t uuid;
-	char uuid_str[MAX_LEN_UUID_STR];
 	uint16_t start, end;
-	GSList *l;
 
 	if (!bt_gatt_client_is_ready(device->client))
 		return;
 
-	gatt_db_attribute_get_service_data(attr, &start, &end, NULL, &uuid);
-	bt_uuid_to_string(&uuid, uuid_str, sizeof(uuid_str));
+	gatt_db_attribute_get_service_data(attr, &start, &end, NULL, NULL);
 
 	DBG("start: 0x%04x, end: 0x%04x", start, end);
 
@@ -3349,21 +3352,9 @@
 	if (!new_service)
 		return;
 
-	l = find_service_with_uuid(device->services, uuid_str);
-
 	device_register_primaries(device, new_service, -1);
 
-	/*
-	 * If the profile was probed for the first time then call accept on
-	 * the service.
-	 */
-	if (!l) {
-		l = find_service_with_uuid(device->services, uuid_str);
-		if (l)
-			service_accept(l->data);
-	}
-
-	btd_device_add_uuid(device, uuid_str);
+	add_gatt_service(attr, device);
 
 	btd_gatt_client_service_added(device->client_dbus, attr);
 
@@ -4523,7 +4514,7 @@
 
 	device_register_primaries(device, services, -1);
 
-	device_probe_gatt_profiles(device);
+	device_add_gatt_services(device);
 
 	device_svc_resolved(device, device->bdaddr_type, 0);
 }
@@ -4544,8 +4535,6 @@
 
 	register_gatt_services(device);
 
-	device_accept_gatt_profiles(device);
-
 	btd_gatt_client_ready(device->client_dbus);
 
 	/*
@@ -4590,6 +4579,12 @@
 	/* Notify attio so it can react to notifications */
 	g_slist_foreach(device->attios, attio_connected, device->attrib);
 
+	/*
+	 * Notify notify existing service about the new connection so they can
+	 * react to notifications while discovering services
+	 */
+	device_accept_gatt_profiles(device);
+
 	if (!bt_gatt_client_set_ready_handler(device->client,
 							gatt_client_ready_cb,
 							device, NULL)) {