/***
- This file is part of PulseAudio.
+ This file is part of PulseAudio.
- Copyright 2008 Joao Paulo Rechi Vita
+ Copyright 2008-2013 João Paulo Rechi Vita
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published
- by the Free Software Foundation; either version 2 of the License,
- or (at your option) any later version.
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
- PulseAudio is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ PulseAudio is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
- You should have received a copy of the GNU Lesser General Public License
- along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
***/
#ifdef HAVE_CONFIG_H
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
#include <pulse/xmalloc.h>
#include <pulsecore/module.h>
+#include <pulsecore/core-util.h>
#include <pulsecore/modargs.h>
#include <pulsecore/macro.h>
-#include <pulsecore/llist.h>
#include <pulsecore/core-util.h>
+#include <pulsecore/dbus-shared.h>
-#include "dbus-util.h"
#include "module-bluetooth-discover-symdef.h"
+#include "bluetooth-util.h"
-PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
-PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
+PA_MODULE_AUTHOR("João Paulo Rechi Vita");
+PA_MODULE_DESCRIPTION("Detect available BlueZ 4 Bluetooth audio devices and load BlueZ 4 Bluetooth audio drivers");
PA_MODULE_VERSION(PACKAGE_VERSION);
-PA_MODULE_USAGE("");
-
-struct module {
- char *profile;
- pa_module *pa_m;
- PA_LLIST_FIELDS(struct module);
-};
-
-struct uuid {
- char *uuid;
- PA_LLIST_FIELDS(struct uuid);
-};
-
-struct device {
- char *name;
- char *object_path;
- int paired;
- char *alias;
- int connected;
- PA_LLIST_HEAD(struct uuid, uuid_list);
- char *address;
- int class;
- int trusted;
- PA_LLIST_HEAD(struct module, module_list);
- PA_LLIST_FIELDS(struct device);
+PA_MODULE_USAGE("sco_sink=<name of sink> "
+ "sco_source=<name of source> ");
+PA_MODULE_LOAD_ONCE(true);
+
+static const char* const valid_modargs[] = {
+ "sco_sink",
+ "sco_source",
+ "async", /* deprecated */
+ NULL
};
struct userdata {
pa_module *module;
- pa_dbus_connection *conn;
- PA_LLIST_HEAD(struct device, device_list);
+ pa_modargs *modargs;
+ pa_core *core;
+ pa_bluetooth_discovery *discovery;
+ pa_hook_slot *slot;
+ pa_hashmap *hashmap;
};
-static struct module *module_new(const char *profile, pa_module *pa_m) {
- struct module *m;
-
- m = pa_xnew(struct module, 1);
- m->profile = pa_xstrdup(profile);
- m->pa_m = pa_m;
- PA_LLIST_INIT(struct module, m);
-
- return m;
-}
-
-static void module_free(struct module *m) {
- pa_assert(m);
-
- pa_xfree(m->profile);
- pa_xfree(m);
-}
-
-static struct uuid *uuid_new(const char *uuid) {
- struct uuid *node;
-
- node = pa_xnew(struct uuid, 1);
- node->uuid = pa_xstrdup(uuid);
- PA_LLIST_INIT(struct uuid, node);
-
- return node;
-}
-
-static void uuid_free(struct uuid *uuid) {
- pa_assert(uuid);
-
- pa_xfree(uuid->uuid);
- pa_xfree(uuid);
-}
-
-static struct device *device_new(const char *object_path) {
- struct device *node;
-
- node = pa_xnew(struct device, 1);
- node->name = NULL;
- node->object_path = pa_xstrdup(object_path);
- node->paired = -1;
- node->alias = NULL;
- node->connected = -1;
- PA_LLIST_HEAD_INIT(struct uuid, node->uuid_list);
- node->address = NULL;
- node->class = -1;
- node->trusted = -1;
- PA_LLIST_HEAD_INIT(struct module, node->module_list);
- PA_LLIST_INIT(struct device, node);
-
- return node;
-}
-
-static void device_free(struct device *device) {
- struct module *m;
- struct uuid *i;
-
- pa_assert(device);
-
- while ((m = device->module_list)) {
- PA_LLIST_REMOVE(struct module, device->module_list, m);
- module_free(m);
- }
-
- while ((i = device->uuid_list)) {
- PA_LLIST_REMOVE(struct uuid, device->uuid_list, i);
- uuid_free(i);
- }
-
- pa_xfree(device->name);
- pa_xfree(device->object_path);
- pa_xfree(device->alias);
- pa_xfree(device->address);
- pa_xfree(device);
-}
-
-static struct device* device_find(struct userdata *u, const char *path) {
- struct device *i;
-
- for (i = u->device_list; i; i = i->next)
- if (pa_streq(i->object_path, path))
- return i;
-
- return NULL;
-}
+struct module_info {
+ char *path;
+ uint32_t module;
+};
-static int parse_device_property(struct userdata *u, struct device *d, DBusMessageIter *i) {
- const char *key;
- DBusMessageIter variant_i;
+static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) {
+ struct module_info *mi;
pa_assert(u);
pa_assert(d);
- pa_assert(i);
-
- if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
- pa_log("Property name not a string.");
- return -1;
- }
- dbus_message_iter_get_basic(i, &key);
+ mi = pa_hashmap_get(u->hashmap, d->path);
- if (!dbus_message_iter_next(i)) {
- pa_log("Property value missing");
- return -1;
- }
-
- if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
- pa_log("Property value not a variant.");
- return -1;
- }
+ if (pa_bluetooth_device_any_audio_connected(d)) {
- dbus_message_iter_recurse(i, &variant_i);
+ if (!mi) {
+ pa_module *m = NULL;
+ char *args;
- pa_log_debug("Parsing device property %s", key);
+ /* Oh, awesome, a new device has shown up and been connected! */
- switch (dbus_message_iter_get_arg_type(&variant_i)) {
+ args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
- case DBUS_TYPE_STRING: {
+ if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
+ pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
+ char *tmp;
- const char *value;
- dbus_message_iter_get_basic(&variant_i, &value);
-
- if (pa_streq(key, "Name")) {
- pa_xfree(d->name);
- d->name = pa_xstrdup(value);
- } else if (pa_streq(key, "Alias")) {
- pa_xfree(d->alias);
- d->alias = pa_xstrdup(value);
- } else if (pa_streq(key, "Address")) {
- pa_xfree(d->address);
- d->address = pa_xstrdup(value);
+ tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
+ pa_modargs_get_value(u->modargs, "sco_sink", NULL),
+ pa_modargs_get_value(u->modargs, "sco_source", NULL));
+ pa_xfree(args);
+ args = tmp;
}
- break;
- }
-
- case DBUS_TYPE_BOOLEAN: {
-
- dbus_bool_t value;
- dbus_message_iter_get_basic(&variant_i, &value);
+ pa_log_debug("Loading module-bluetooth-device %s", args);
+ m = pa_module_load(u->module->core, "module-bluetooth-device", args);
+ pa_xfree(args);
- if (pa_streq(key, "Paired"))
- d->paired = !!value;
- else if (pa_streq(key, "Connected"))
- d->connected = !!value;
- else if (pa_streq(key, "Trusted"))
- d->trusted = !!value;
+ if (m) {
+ mi = pa_xnew(struct module_info, 1);
+ mi->module = m->index;
+ mi->path = pa_xstrdup(d->path);
- break;
+ pa_hashmap_put(u->hashmap, mi->path, mi);
+ } else
+ pa_log_debug("Failed to load module for device %s", d->path);
}
- case DBUS_TYPE_UINT32: {
-
- uint32_t value;
- dbus_message_iter_get_basic(&variant_i, &value);
-
- if (pa_streq(key, "Class"))
- d->class = (int) value;
-
- break;
- }
-
- case DBUS_TYPE_ARRAY: {
-
- DBusMessageIter ai;
- dbus_message_iter_recurse(&variant_i, &ai);
-
- if (dbus_message_iter_get_arg_type(&ai) == DBUS_TYPE_STRING &&
- pa_streq(key, "UUIDs")) {
-
- while (dbus_message_iter_get_arg_type(&ai) != DBUS_TYPE_INVALID) {
- struct uuid *node;
- const char *value;
-
- dbus_message_iter_get_basic(&ai, &value);
- node = uuid_new(value);
- PA_LLIST_PREPEND(struct uuid, d->uuid_list, node);
-
- if (!dbus_message_iter_next(&ai))
- break;
- }
- }
-
- break;
- }
- }
-
- return 0;
-}
-
-static int get_device_properties(struct userdata *u, struct device *d) {
- DBusError e;
- DBusMessage *m = NULL, *r = NULL;
- DBusMessageIter arg_i, element_i;
- int ret = -1;
-
- pa_assert(u);
- pa_assert(d);
-
- dbus_error_init(&e);
-
- pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->object_path, "org.bluez.Device", "GetProperties"));
-
- r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e);
-
- if (!r) {
- pa_log("org.bluez.Device.GetProperties failed: %s", e.message);
- goto finish;
- }
-
- if (!dbus_message_iter_init(r, &arg_i)) {
- pa_log("org.bluez.Device.GetProperties reply has no arguments");
- goto finish;
- }
-
- if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) {
- pa_log("org.bluez.Device.GetProperties argument is not an array");
- goto finish;
- }
-
- dbus_message_iter_recurse(&arg_i, &element_i);
- while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
+ } else {
- if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter dict_i;
+ if (mi) {
- dbus_message_iter_recurse(&element_i, &dict_i);
+ /* Hmm, disconnection? Then the module unloads itself */
- if (parse_device_property(u, d, &dict_i) < 0)
- goto finish;
+ pa_log_debug("Unregistering module for %s", d->path);
+ pa_hashmap_remove(u->hashmap, mi->path);
+ pa_xfree(mi->path);
+ pa_xfree(mi);
}
-
- if (!dbus_message_iter_next(&element_i))
- break;
}
- ret = 0;
-
-finish:
- if (m)
- dbus_message_unref(m);
- if (r)
- dbus_message_unref(r);
-
- dbus_error_free(&e);
-
- return ret;
+ return PA_HOOK_OK;
}
-static void load_module_for_device(struct userdata *u, struct device *d, const char *profile) {
- char *args;
- pa_module *pa_m;
- struct module *m;
-
- pa_assert(u);
- pa_assert(d);
-
- get_device_properties(u, d);
- args = pa_sprintf_malloc("sink_name=\"%s\" address=\"%s\" profile=\"%s\"", d->name, d->address, profile);
- pa_m = pa_module_load(u->module->core, "module-bluetooth-device", args);
- pa_xfree(args);
-
- if (!m) {
- pa_log_debug("Failed to load module for device %s", d->object_path);
- return;
- }
-
- m = module_new(profile, pa_m);
- PA_LLIST_PREPEND(struct module, d->module_list, m);
-}
-
-static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void *userdata) {
- DBusMessageIter arg_i;
- DBusError err;
- const char *value;
+int pa__init(pa_module* m) {
struct userdata *u;
+ pa_modargs *ma = NULL;
- pa_assert(bus);
- pa_assert(msg);
- pa_assert(userdata);
- u = userdata;
-
- dbus_error_init(&err);
-
- pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
- dbus_message_get_interface(msg),
- dbus_message_get_path(msg),
- dbus_message_get_member(msg));
-
- if (dbus_message_is_signal(msg, "org.bluez.Adapter", "DeviceRemoved")) {
-
- if (!dbus_message_iter_init(msg, &arg_i))
- pa_log("dbus: message has no parameters");
- else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH)
- pa_log("dbus: argument is not object path");
- else {
- struct device *d;
-
- dbus_message_iter_get_basic(&arg_i, &value);
- pa_log_debug("hcid: device %s removed", value);
-
- if ((d = device_find(u, value))) {
- PA_LLIST_REMOVE(struct device, u->device_list, d);
- device_free(d);
- }
- }
-
- } else if (dbus_message_is_signal(msg, "org.bluez.Headset", "PropertyChanged") ||
- dbus_message_is_signal(msg, "org.bluez.AudioSink", "PropertyChanged")) {
-
- struct device *d;
- const char *profile;
- DBusMessageIter variant_i;
-
- if (!dbus_message_iter_init(msg, &arg_i)) {
- pa_log("dbus: message has no parameters");
- goto done;
- }
-
- if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_STRING) {
- pa_log("Property name not a string.");
- goto done;
- }
-
- dbus_message_iter_get_basic(&arg_i, &value);
+ pa_assert(m);
- if (!dbus_message_iter_next(&arg_i)) {
- pa_log("Property value missing");
- goto done;
- }
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
+ }
- if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_VARIANT) {
- pa_log("Property value not a variant.");
- goto done;
- }
+ if (pa_modargs_get_value(ma, "async", NULL))
+ pa_log_warn("The 'async' argument is deprecated and does nothing.");
- dbus_message_iter_recurse(&arg_i, &variant_i);
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ u->core = m->core;
+ u->modargs = ma;
+ ma = NULL;
+ u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
- if (dbus_message_iter_get_arg_type(&variant_i) == DBUS_TYPE_BOOLEAN) {
- dbus_bool_t connected;
- dbus_message_iter_get_basic(&variant_i, &connected);
+ if (!(u->discovery = pa_bluetooth_discovery_get(u->core)))
+ goto fail;
- if (!pa_streq(value, "Connected") || !connected)
- goto done;
- }
+ u->slot = pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED),
+ PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u);
- if (!(d = device_find(u, dbus_message_get_path(msg)))) {
- d = device_new(dbus_message_get_path(msg));
- PA_LLIST_PREPEND(struct device, u->device_list, d);
- }
+ return 0;
- if (dbus_message_is_signal(msg, "org.bluez.Headset", "PropertyChanged"))
- profile = "hsp";
- else
- profile = "a2dp";
+fail:
+ pa__done(m);
- load_module_for_device(u, d, profile);
- }
+ if (ma)
+ pa_modargs_free(ma);
-done:
- dbus_error_free(&err);
- return DBUS_HANDLER_RESULT_HANDLED;
+ return -1;
}
void pa__done(pa_module* m) {
struct userdata *u;
- struct device *i;
pa_assert(m);
if (!(u = m->userdata))
return;
- while ((i = u->device_list)) {
- PA_LLIST_REMOVE(struct device, u->device_list, i);
- device_free(i);
- }
+ if (u->slot)
+ pa_hook_slot_free(u->slot);
- if (u->conn)
- pa_dbus_connection_unref(u->conn);
+ if (u->discovery)
+ pa_bluetooth_discovery_unref(u->discovery);
- pa_xfree(u);
-}
+ if (u->hashmap) {
+ struct module_info *mi;
-int pa__init(pa_module* m) {
- DBusError err;
- struct userdata *u;
-
- pa_assert(m);
- dbus_error_init(&err);
-
- m->userdata = u = pa_xnew(struct userdata, 1);
- u->module = m;
- PA_LLIST_HEAD_INIT(struct device, u->device_list);
-
- /* connect to the bus */
- u->conn = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &err);
- if (dbus_error_is_set(&err) || (u->conn == NULL) ) {
- pa_log("Failed to get D-Bus connection: %s", err.message);
- goto fail;
- }
-
- /* dynamic detection of bluetooth audio devices */
- if (!dbus_connection_add_filter(pa_dbus_connection_get(u->conn), filter_cb, u, NULL)) {
- pa_log_error("Failed to add filter function");
- goto fail;
- }
-
- dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", &err);
- if (dbus_error_is_set(&err)) {
- pa_log_error("Unable to subscribe to org.bluez.Adapter signals: %s: %s", err.name, err.message);
- goto fail;
- }
-
- dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'", &err);
- if (dbus_error_is_set(&err)) {
- pa_log_error("Unable to subscribe to org.bluez.Headset signals: %s: %s", err.name, err.message);
- goto fail;
- }
+ while ((mi = pa_hashmap_steal_first(u->hashmap))) {
+ pa_xfree(mi->path);
+ pa_xfree(mi);
+ }
- dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", &err);
- if (dbus_error_is_set(&err)) {
- pa_log_error("Unable to subscribe to org.bluez.AudioSink signals: %s: %s", err.name, err.message);
- goto fail;
+ pa_hashmap_free(u->hashmap);
}
- return 0;
+ if (u->modargs)
+ pa_modargs_free(u->modargs);
-fail:
- dbus_error_free(&err);
- pa__done(m);
-
- return -1;
+ pa_xfree(u);
}