[pulseaudio-discuss] [PATCH 40/56] bluetooth: Track devices in module-bluez5-discover

jprvita at gmail.com jprvita at gmail.com
Fri Jul 12 11:06:55 PDT 2013


From: João Paulo Rechi Vita <jprvita at openbossa.org>

---
 src/modules/bluetooth/module-bluez5-discover.c | 68 ++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/src/modules/bluetooth/module-bluez5-discover.c b/src/modules/bluetooth/module-bluez5-discover.c
index 8409bd3..4c90744 100644
--- a/src/modules/bluetooth/module-bluez5-discover.c
+++ b/src/modules/bluetooth/module-bluez5-discover.c
@@ -24,6 +24,7 @@
 #endif
 
 #include <pulsecore/core.h>
+#include <pulsecore/core-util.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/module.h>
 
@@ -39,9 +40,57 @@ PA_MODULE_LOAD_ONCE(true);
 struct userdata {
     pa_module *module;
     pa_core *core;
+    pa_hashmap *device_modules;
+    pa_hook_slot *device_connection_changed_slot;
     pa_bluetooth_discovery *discovery;
 };
 
+struct module_info {
+    char *path;
+    uint32_t module;
+};
+
+static pa_hook_result_t device_connection_changed_cb(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) {
+    struct module_info *mi;
+
+    pa_assert(d);
+    pa_assert(u);
+
+    mi = pa_hashmap_get(u->device_modules, d->path);
+
+    if (mi && !pa_bluetooth_device_any_transport_connected(d)) {
+        /* disconnection, the module unloads itself */
+        pa_log_debug("Unregistering module for %s", d->path);
+        pa_hashmap_remove(u->device_modules, mi->path);
+        pa_xfree(mi->path);
+        pa_xfree(mi);
+        return PA_HOOK_OK;
+    }
+
+    if (!mi && pa_bluetooth_device_any_transport_connected(d)) {
+        /* a new device has been connected */
+        pa_module *m;
+        char *args = pa_sprintf_malloc("path=%s", d->path);
+
+        pa_log_debug("Loading module-bluez5-device %s", args);
+        m = pa_module_load(u->module->core, "module-bluez5-device", args);
+        pa_xfree(args);
+
+        if (m) {
+            mi = pa_xnew(struct module_info, 1);
+            mi->module = m->index;
+            mi->path = pa_xstrdup(d->path);
+
+            pa_hashmap_put(u->device_modules, mi->path, mi);
+        } else
+            pa_log_warn("Failed to load module for device %s", d->path);
+
+        return PA_HOOK_OK;
+    }
+
+    return PA_HOOK_OK;
+}
+
 int pa__init(pa_module* m) {
     struct userdata *u;
 
@@ -50,10 +99,15 @@ int pa__init(pa_module* m) {
     m->userdata = u = pa_xnew0(struct userdata, 1);
     u->module = m;
     u->core = m->core;
+    u->device_modules = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
     if (!(u->discovery = pa_bluetooth_discovery_get(u->core)))
         goto fail;
 
+    u->device_connection_changed_slot =
+        pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED),
+                        PA_HOOK_NORMAL, (pa_hook_cb_t) device_connection_changed_cb, u);
+
     return 0;
 
 fail:
@@ -69,8 +123,22 @@ void pa__done(pa_module* m) {
     if (!(u = m->userdata))
         return;
 
+    if (u->device_connection_changed_slot)
+        pa_hook_slot_free(u->device_connection_changed_slot);
+
     if (u->discovery)
         pa_bluetooth_discovery_unref(u->discovery);
 
+    if (u->device_modules) {
+        struct module_info *mi;
+
+        while ((mi = pa_hashmap_steal_first(u->device_modules))) {
+            pa_xfree(mi->path);
+            pa_xfree(mi);
+        }
+
+        pa_hashmap_free(u->device_modules, NULL);
+    }
+
     pa_xfree(u);
 }
-- 
1.7.11.7



More information about the pulseaudio-discuss mailing list