Fix handling of SDP records.

Delete SDP records when device is deleted.
Update cache whenever SDP is performed.
Remove SDP records even when there is no driver associated with the UUID.

Bug: 2173752
Dr No: Eastham
diff --git a/src/device.c b/src/device.c
index 7c6ce4e..2aaf5b8 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1016,9 +1016,11 @@
 
 	adapter_get_address(device->adapter, &src);
 	ba2str(&device->bdaddr, addr);
+
 	device_remove_bonding(device, conn);
 	delete_entry(&src, "profiles", addr);
 	delete_entry(&src, "trusts", addr);
+	delete_all_records(&src, &device->bdaddr);
 }
 
 void device_remove(struct btd_device *device, DBusConnection *conn,
@@ -1213,8 +1215,6 @@
 		next = list->next;
 
 		for (uuid = driver->uuids; *uuid; uuid++) {
-			sdp_record_t *rec;
-
 			if (!g_slist_find_custom(uuids, *uuid,
 					(GCompareFunc) strcasecmp))
 				continue;
@@ -1227,24 +1227,28 @@
 								driver_data);
 			g_free(driver_data);
 
-			rec = find_record_in_list(records, *uuid);
-			if (!rec)
-				break;
-
-			delete_record(srcaddr, dstaddr, rec->handle);
-
-			records = sdp_list_remove(records, rec);
-			sdp_record_free(rec);
-
 			break;
 		}
 	}
 
+	for (list = uuids; list; list = list->next) {
+		sdp_record_t *rec;
+
+		device->uuids = g_slist_remove(device->uuids, list->data);
+
+		rec = find_record_in_list(records, list->data);
+		if (!rec)
+			continue;
+
+		delete_record(srcaddr, dstaddr, rec->handle);
+
+		records = sdp_list_remove(records, rec);
+		sdp_record_free(rec);
+
+	}
+
 	if (records)
 		sdp_list_free(records, (sdp_free_func_t) sdp_record_free);
-
-	for (list = uuids; list; list = list->next)
-		device->uuids = g_slist_remove(device->uuids, list->data);
 }
 
 static void services_changed(struct btd_device *device)
@@ -1386,11 +1390,6 @@
 
 	update_services(req, recs);
 
-	if (!req->profiles_added && !req->profiles_removed) {
-		debug("%s: No service update", device->path);
-		goto proceed;
-	}
-
 	if (device->tmp_records && req->records) {
 		sdp_list_free(device->tmp_records,
 					(sdp_free_func_t) sdp_record_free);
@@ -1398,6 +1397,11 @@
 		req->records = NULL;
 	}
 
+	if (!req->profiles_added && !req->profiles_removed) {
+		debug("%s: No service update", device->path);
+		goto proceed;
+	}
+
 	/* Probe matching drivers for services added */
 	if (req->profiles_added)
 		device_probe_drivers(device, req->profiles_added);
diff --git a/src/storage.c b/src/storage.c
index 47c401b..e2d7632 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -908,6 +908,26 @@
 	rec_list->recs = sdp_list_append(rec_list->recs, rec);
 }
 
+void delete_all_records(bdaddr_t *src, bdaddr_t *dst)
+{
+	sdp_list_t *records, *seq;
+	sdp_record_t *rec;
+	char srcaddr[18], dstaddr[18];
+
+	ba2str(src, srcaddr);
+	ba2str(dst, dstaddr);
+
+	records = read_records(src, dst);
+
+	for (seq = records; seq; seq = seq->next) {
+		rec = seq->data;
+		delete_record(srcaddr, dstaddr, rec->handle);
+	}
+
+	if (records)
+		sdp_list_free(records, (sdp_free_func_t) sdp_record_free);
+}
+
 sdp_list_t *read_records(bdaddr_t *src, bdaddr_t *dst)
 {
 	char filename[PATH_MAX + 1];
diff --git a/src/storage.h b/src/storage.h
index 6288da2..de0e837 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -63,6 +63,7 @@
 sdp_record_t *record_from_string(const gchar *str);
 sdp_record_t *fetch_record(const gchar *src, const gchar *dst, const uint32_t handle);
 int delete_record(const gchar *src, const gchar *dst, const uint32_t handle);
+void delete_all_records(bdaddr_t *src, bdaddr_t *dst);
 sdp_list_t *read_records(bdaddr_t *src, bdaddr_t *dst);
 sdp_record_t *find_record_in_list(sdp_list_t *recs, const char *uuid);
 int store_device_id(const gchar *src, const gchar *dst,