[pulseaudio-discuss] [PATCH next v0 6/8] bluetooth: Add transport hashmap to discovery

Mikel Astiz mikel.astiz.oss at gmail.com
Wed Dec 5 08:23:16 PST 2012


From: Mikel Astiz <mikel.astiz at bmw-carit.de>

Path-based transport lookups are required in a discovery basis, before
the associated device is known. Therefore, it makes more sense to
maintain a hashmap in the discovery structure itself, instead of
iterating all devices.
---
 src/modules/bluetooth/bluetooth-util.c | 35 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
index 0daf1c9..8ca6fae 100644
--- a/src/modules/bluetooth/bluetooth-util.c
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -68,6 +68,7 @@ struct pa_bluetooth_discovery {
     pa_dbus_connection *connection;
     PA_LLIST_HEAD(pa_dbus_pending, pending);
     pa_hashmap *devices;
+    pa_hashmap *transports;
     pa_hook hook;
     pa_bool_t filter_added;
 };
@@ -154,6 +155,8 @@ static void transport_free(pa_bluetooth_transport *t) {
         pa_hook_done(&t->hooks[i]);
 
     pa_xfree(t->owner);
+    pa_hashmap_remove(t->device->discovery->transports, t->path);
+
     pa_xfree(t->path);
     pa_xfree(t->config);
     pa_xfree(t);
@@ -913,16 +916,10 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us
 
         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
     } else if (dbus_message_is_signal(m, "org.bluez.MediaTransport", "PropertyChanged")) {
-        pa_bluetooth_device *d;
-        pa_bluetooth_transport *t = NULL;
-        void *state = NULL;
+        pa_bluetooth_transport *t;
         DBusMessageIter arg_i;
 
-        while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
-            if ((t = pa_hashmap_get(d->transports, dbus_message_get_path(m))))
-                break;
-
-        if (!t)
+        if ((t = pa_hashmap_get(y->transports, dbus_message_get_path(m))) == NULL)
             goto fail;
 
         if (!dbus_message_iter_init(m, &arg_i)) {
@@ -1193,6 +1190,7 @@ static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage
     if (nrec)
         t->nrec = nrec;
     pa_hashmap_put(d->transports, t->path, t);
+    pa_hashmap_put(y->transports, t->path, t);
 
     pa_log_debug("Transport %s profile %d available", t->path, t->profile);
 
@@ -1209,9 +1207,7 @@ fail:
 
 static DBusMessage *endpoint_clear_configuration(DBusConnection *c, DBusMessage *m, void *userdata) {
     pa_bluetooth_discovery *y = userdata;
-    pa_bluetooth_device *d;
     pa_bluetooth_transport *t;
-    void *state = NULL;
     DBusMessage *r;
     DBusError e;
     const char *path;
@@ -1224,14 +1220,11 @@ static DBusMessage *endpoint_clear_configuration(DBusConnection *c, DBusMessage
         goto fail;
     }
 
-    while ((d = pa_hashmap_iterate(y->devices, &state, NULL))) {
-        if ((t = pa_hashmap_get(d->transports, path))) {
-            pa_log_debug("Clearing transport %s profile %d", t->path, t->profile);
-            pa_hashmap_remove(d->transports, t->path);
-            pa_hook_fire(&t->hooks[PA_BLUETOOTH_TRANSPORT_HOOK_REMOVED], NULL);
-            transport_free(t);
-            break;
-        }
+    if ((t = pa_hashmap_get(y->transports, path)) != NULL) {
+        pa_log_debug("Clearing transport %s profile %d", t->path, t->profile);
+        pa_hashmap_remove(t->device->transports, t->path);
+        pa_hook_fire(&t->hooks[PA_BLUETOOTH_TRANSPORT_HOOK_REMOVED], NULL);
+        transport_free(t);
     }
 
     pa_assert_se(r = dbus_message_new_method_return(m));
@@ -1475,6 +1468,7 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
     PA_REFCNT_INIT(y);
     y->core = c;
     y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+    y->transports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
     PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
     pa_hook_init(&y->hook, y);
     pa_shared_set(c, "bluetooth-discovery", y);
@@ -1549,6 +1543,11 @@ void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
         pa_hashmap_free(y->devices, NULL, NULL);
     }
 
+    if (y->transports) {
+        pa_assert(pa_hashmap_size(y->transports) == 0);
+        pa_hashmap_free(y->transports, NULL, NULL);
+    }
+
     if (y->connection) {
         dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), HFP_AG_ENDPOINT);
         dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), HFP_HS_ENDPOINT);
-- 
1.7.11.7



More information about the pulseaudio-discuss mailing list