[pulseaudio-discuss] [RFCv3 23/43] bluetooth: Track devices in module-bluez5-discover

jprvita at gmail.com jprvita at gmail.com
Fri Sep 13 16:34:30 PDT 2013


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

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

diff --git a/src/modules/bluetooth/module-bluez5-discover.c b/src/modules/bluetooth/module-bluez5-discover.c
index 8cc0631..4e6a1d1 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,48 @@ PA_MODULE_LOAD_ONCE(true);
 struct userdata {
     pa_module *module;
     pa_core *core;
+    pa_hashmap *loaded_device_paths;
+    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) {
+    bool module_loaded;
+
+    pa_assert(d);
+    pa_assert(u);
+
+    module_loaded = pa_hashmap_get(u->loaded_device_paths, d->path) ? true : false;
+
+    if (module_loaded && !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->loaded_device_paths, d->path);
+        return PA_HOOK_OK;
+    }
+
+    if (!module_loaded && 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)
+            /* No need to duplicate the path here since the device object will
+             * exist for the whole hashmap entry lifespan */
+            pa_hashmap_put(u->loaded_device_paths, d->path, d->path);
+        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 +90,15 @@ int pa__init(pa_module *m) {
     m->userdata = u = pa_xnew0(struct userdata, 1);
     u->module = m;
     u->core = m->core;
+    u->loaded_device_paths = 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 +114,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->loaded_device_paths)
+        pa_hashmap_free(u->loaded_device_paths, NULL);
+
     pa_xfree(u);
 }
-- 
1.8.3.1



More information about the pulseaudio-discuss mailing list