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

jprvita at gmail.com jprvita at gmail.com
Thu Jul 25 17:14:35 PDT 2013


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

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

diff --git a/src/modules/bluetooth/module-bluez5-discover.c b/src/modules/bluetooth/module-bluez5-discover.c
index 8409bd3..e782b2e 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,46 @@ 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;
 };
 
+static pa_hook_result_t device_connection_changed_cb(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) {
+    void *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, d->path);
+        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)
+            pa_hashmap_put(u->device_modules, d->path, (void *) 1);
+        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 +88,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 +112,14 @@ 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)
+        pa_hashmap_free(u->device_modules, NULL);
+
     pa_xfree(u);
 }
-- 
1.7.11.7



More information about the pulseaudio-discuss mailing list