summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Makefile.am21
-rw-r--r--src/modules/bluetooth/bluetooth-util.c805
-rw-r--r--src/modules/bluetooth/bluetooth-util.h77
-rw-r--r--src/modules/bluetooth/module-bluetooth-device.c579
-rw-r--r--src/modules/bluetooth/module-bluetooth-discover.c839
5 files changed, 1396 insertions, 925 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 00dd53d1..dbb2d0ad 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1003,9 +1003,11 @@ if HAVE_BLUEZ
modlibexec_LTLIBRARIES += \
module-bluetooth-proximity.la \
module-bluetooth-discover.la \
+ libbluetooth-util.la \
libbluetooth-ipc.la \
- libbluetooth-sbc.la \
- module-bluetooth-device.la
+ libbluetooth-sbc.la
+#\
+# module-bluetooth-device.la
pulselibexec_PROGRAMS += \
proximity-helper
@@ -1445,7 +1447,7 @@ proximity_helper_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS)
# Bluetooth sink / source
module_bluetooth_discover_la_SOURCES = modules/bluetooth/module-bluetooth-discover.c
module_bluetooth_discover_la_LDFLAGS = $(MODULE_LDFLAGS)
-module_bluetooth_discover_la_LIBADD = $(AM_LIBADD) $(DBUS_LIBS) libpulsecore-@PA_MAJORMINORMICRO@.la libdbus-util.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
+module_bluetooth_discover_la_LIBADD = $(AM_LIBADD) $(DBUS_LIBS) libpulsecore-@PA_MAJORMINORMICRO@.la libdbus-util.la libbluetooth-util.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
module_bluetooth_discover_la_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS)
libbluetooth_sbc_la_SOURCES = modules/bluetooth/sbc.c modules/bluetooth/sbc.h modules/bluetooth/sbc_tables.h modules/bluetooth/sbc_math.h
@@ -1459,10 +1461,15 @@ libbluetooth_ipc_la_LDFLAGS = -avoid-version
libbluetooth_ipc_la_LIBADD = $(AM_LIBADD)libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
libbluetooth_ipc_la_CFLAGS = $(AM_CFLAGS) -w
-module_bluetooth_device_la_SOURCES = modules/bluetooth/module-bluetooth-device.c modules/bluetooth/rtp.h
-module_bluetooth_device_la_LDFLAGS = $(MODULE_LDFLAGS)
-module_bluetooth_device_la_LIBADD = $(AM_LIBADD) $(DBUS_LIBS) libpulsecore-@PA_MAJORMINORMICRO@.la libdbus-util.la libbluetooth-ipc.la libbluetooth-sbc.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
-module_bluetooth_device_la_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS) -w
+libbluetooth_util_la_SOURCES = modules/bluetooth/bluetooth-util.c modules/bluetooth/bluetooth-util.h
+libbluetooth_util_la_LDFLAGS = -avoid-version
+libbluetooth_util_la_LIBADD = $(AM_LIBADD) $(DBUS_LIBS) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la libdbus-util.la
+libbluetooth_util_la_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS)
+
+#module_bluetooth_device_la_SOURCES = modules/bluetooth/module-bluetooth-device.c modules/bluetooth/rtp.h
+#module_bluetooth_device_la_LDFLAGS = $(MODULE_LDFLAGS)
+#module_bluetooth_device_la_LIBADD = $(AM_LIBADD) $(DBUS_LIBS) libpulsecore-@PA_MAJORMINORMICRO@.la libdbus-util.la libbluetooth-ipc.la libbluetooth-sbc.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
+#module_bluetooth_device_la_CFLAGS = $(AM_CFLAGS) $(DBUS_CFLAGS) -w
# Apple Airtunes/RAOP
module_raop_sink_la_SOURCES = modules/module-raop-sink.c
diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
new file mode 100644
index 00000000..019b97e8
--- /dev/null
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -0,0 +1,805 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Joao 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 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.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/core-util.h>
+#include <modules/dbus-util.h>
+
+#include "bluetooth-util.h"
+
+enum mode {
+ MODE_FIND,
+ MODE_GET,
+ MODE_DISCOVER
+};
+
+struct pa_bluetooth_discovery {
+ DBusConnection *connection;
+ PA_LLIST_HEAD(pa_dbus_pending, pending);
+
+ enum mode mode;
+
+ /* If mode == MODE_FIND look for a specific device by its address.
+ If mode == MODE_GET look for a specific device by its path. */
+ const char *looking_for;
+ pa_bluetooth_device *found_device;
+
+ /* If looking_for is NULL we do long-time discovery */
+ pa_hashmap *devices;
+ pa_bluetooth_device_callback_t callback;
+ struct userdata *userdata;
+};
+
+static pa_bluetooth_uuid *uuid_new(const char *uuid) {
+ pa_bluetooth_uuid *u;
+
+ u = pa_xnew(pa_bluetooth_uuid, 1);
+ u->uuid = pa_xstrdup(uuid);
+ PA_LLIST_INIT(pa_bluetooth_uuid, u);
+
+ return u;
+}
+
+static void uuid_free(pa_bluetooth_uuid *u) {
+ pa_assert(u);
+
+ pa_xfree(u->uuid);
+ pa_xfree(u);
+}
+
+static pa_bluetooth_device* device_new(const char *path) {
+ pa_bluetooth_device *d;
+
+ d = pa_xnew(pa_bluetooth_device, 1);
+
+ d->device_info_valid = d->audio_sink_info_valid = d->headset_info_valid = 0;
+
+ d->data = NULL;
+
+ d->name = NULL;
+ d->path = pa_xstrdup(path);
+ d->paired = -1;
+ d->alias = NULL;
+ d->device_connected = -1;
+ PA_LLIST_HEAD_INIT(pa_bluetooth_uuid, d->uuids);
+ d->address = NULL;
+ d->class = -1;
+ d->trusted = -1;
+
+ d->audio_sink_connected = -1;
+
+ d->headset_connected = -1;
+
+ return d;
+}
+
+void pa_bluetooth_device_free(pa_bluetooth_device *d) {
+ pa_bluetooth_uuid *u;
+
+ pa_assert(d);
+
+ while ((u = d->uuids)) {
+ PA_LLIST_REMOVE(pa_bluetooth_uuid, d->uuids, u);
+ uuid_free(u);
+ }
+
+ pa_xfree(d->name);
+ pa_xfree(d->path);
+ pa_xfree(d->alias);
+ pa_xfree(d->address);
+ pa_xfree(d);
+}
+
+static pa_bool_t device_is_loaded(pa_bluetooth_device *d) {
+ pa_assert(d);
+
+ return d->device_info_valid && d->audio_sink_info_valid && d->headset_info_valid;
+}
+
+static pa_bool_t device_is_audio(pa_bluetooth_device *d) {
+ pa_assert(d);
+
+ pa_assert(d->device_info_valid);
+ pa_assert(d->audio_sink_info_valid);
+ pa_assert(d->headset_info_valid);
+
+ return d->device_info_valid > 0 &&
+ (d->audio_sink_info_valid > 0 || d->headset_info_valid > 0);
+}
+
+static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessageIter *i) {
+ const char *key;
+ DBusMessageIter variant_i;
+
+ pa_assert(y);
+ 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);
+
+ 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;
+ }
+
+ dbus_message_iter_recurse(i, &variant_i);
+
+ pa_log_debug("Parsing property org.bluez.Device.%s", key);
+
+ switch (dbus_message_iter_get_arg_type(&variant_i)) {
+
+ case DBUS_TYPE_STRING: {
+
+ 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);
+ }
+
+ pa_log_debug("Value %s", value);
+
+ break;
+ }
+
+ case DBUS_TYPE_BOOLEAN: {
+
+ dbus_bool_t value;
+ dbus_message_iter_get_basic(&variant_i, &value);
+
+ if (pa_streq(key, "Paired"))
+ d->paired = !!value;
+ else if (pa_streq(key, "Connected"))
+ d->device_connected = !!value;
+ else if (pa_streq(key, "Trusted"))
+ d->trusted = !!value;
+
+ pa_log_debug("Value %s", pa_yes_no(value));
+
+ break;
+ }
+
+ case DBUS_TYPE_UINT32: {
+
+ uint32_t value;
+ dbus_message_iter_get_basic(&variant_i, &value);
+
+ if (pa_streq(key, "Class"))
+ d->class = (int) value;
+
+ pa_log_debug("Value %u", (unsigned) 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) {
+ pa_bluetooth_uuid *node;
+ const char *value;
+
+ dbus_message_iter_get_basic(&ai, &value);
+ node = uuid_new(value);
+ PA_LLIST_PREPEND(pa_bluetooth_uuid, d->uuids, node);
+
+ if (!dbus_message_iter_next(&ai))
+ break;
+ }
+ }
+
+ break;
+ }
+ }
+
+ return 0;
+}
+
+static int parse_audio_property(pa_bluetooth_discovery *u, int *connected, DBusMessageIter *i) {
+ const char *key;
+ DBusMessageIter variant_i;
+
+ pa_assert(u);
+ pa_assert(connected);
+ 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);
+
+ 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;
+ }
+
+ dbus_message_iter_recurse(i, &variant_i);
+
+ pa_log_debug("Parsing property org.bluez.{AudioSink|Headset}.%s", key);
+
+ switch (dbus_message_iter_get_arg_type(&variant_i)) {
+
+ case DBUS_TYPE_BOOLEAN: {
+
+ dbus_bool_t value;
+ dbus_message_iter_get_basic(&variant_i, &value);
+
+ if (pa_streq(key, "Connected"))
+ *connected = !!value;
+
+ pa_log_debug("Value %s", pa_yes_no(value));
+
+ break;
+ }
+ }
+
+ return 0;
+}
+
+
+static void run_callback(pa_bluetooth_discovery *y, pa_bluetooth_device *d, pa_bool_t good) {
+ pa_assert(y);
+ pa_assert(d);
+
+ if (y->mode != MODE_DISCOVER)
+ return;
+
+ if (!device_is_loaded(d))
+ return;
+
+ if (!device_is_audio(d))
+ return;
+
+ y->callback(y->userdata, d, good);
+
+}
+
+static void get_properties_reply(DBusPendingCall *pending, void *userdata) {
+ DBusMessage *r;
+ DBusMessageIter arg_i, element_i;
+ pa_dbus_pending *p;
+ pa_bluetooth_device *d;
+ pa_bluetooth_discovery *y;
+ int valid;
+
+ pa_assert_se(p = userdata);
+ pa_assert_se(y = p->context_data);
+ pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+/* pa_log_debug("Got %s.GetProperties response for %s", */
+/* dbus_message_get_interface(p->message), */
+/* dbus_message_get_path(p->message)); */
+
+ d = p->call_data;
+
+ valid = dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR ? -1 : 1;
+
+ if (dbus_message_is_method_call(p->message, "org.bluez.Device", "GetProperties"))
+ d->device_info_valid = valid;
+ else if (dbus_message_is_method_call(p->message, "org.bluez.Headset", "GetProperties"))
+ d->headset_info_valid = valid;
+ else if (dbus_message_is_method_call(p->message, "org.bluez.AudioSink", "GetProperties"))
+ d->audio_sink_info_valid = valid;
+
+ if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+
+ if (!dbus_message_is_error(r, DBUS_ERROR_UNKNOWN_METHOD))
+ pa_log("Error from GetProperties reply: %s", dbus_message_get_error_name(r));
+
+ goto finish;
+ }
+
+ if (!dbus_message_iter_init(r, &arg_i)) {
+ pa_log("GetProperties reply has no arguments.");
+ goto finish;
+ }
+
+ if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) {
+ pa_log("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) {
+
+ if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
+ DBusMessageIter dict_i;
+
+ dbus_message_iter_recurse(&element_i, &dict_i);
+
+ if (dbus_message_has_interface(p->message, "org.bluez.Device")) {
+ if (parse_device_property(y, d, &dict_i) < 0)
+ goto finish;
+
+ } else if (dbus_message_has_interface(p->message, "org.bluez.Headset")) {
+ if (parse_audio_property(y, &d->headset_connected, &dict_i) < 0)
+ goto finish;
+
+ } else if (dbus_message_has_interface(p->message, "org.bluez.AudioSink")) {
+ if (parse_audio_property(y, &d->audio_sink_connected, &dict_i) < 0)
+ goto finish;
+ }
+ }
+
+ if (!dbus_message_iter_next(&element_i))
+ break;
+ }
+
+finish:
+ run_callback(y, d, TRUE);
+
+ dbus_message_unref(r);
+
+ PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
+ pa_dbus_pending_free(p);
+}
+
+static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessage *m, DBusPendingCallNotifyFunction func) {
+ pa_dbus_pending *p;
+ DBusPendingCall *call;
+
+ pa_assert(y);
+ pa_assert(m);
+
+ pa_assert_se(dbus_connection_send_with_reply(y->connection, m, &call, -1));
+
+ p = pa_dbus_pending_new(m, call, y, d);
+ PA_LLIST_PREPEND(pa_dbus_pending, y->pending, p);
+ dbus_pending_call_set_notify(call, func, p, NULL);
+
+ return p;
+}
+
+static void found_device(pa_bluetooth_discovery *y, const char* path) {
+ DBusMessage *m;
+ pa_bluetooth_device *d;
+
+ pa_assert(y);
+ pa_assert(path);
+
+ d = device_new(path);
+
+ if (y->mode == MODE_DISCOVER) {
+ pa_assert(y->devices);
+ pa_hashmap_put(y->devices, d->path, d);
+ } else {
+ pa_assert(!y->found_device);
+ y->found_device = d;
+ }
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Device", "GetProperties"));
+ send_and_add_to_pending(y, d, m, get_properties_reply);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Headset", "GetProperties"));
+ send_and_add_to_pending(y, d, m, get_properties_reply);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.AudioSink", "GetProperties"));
+ send_and_add_to_pending(y, d, m, get_properties_reply);
+}
+
+static void list_devices_reply(DBusPendingCall *pending, void *userdata) {
+ DBusError e;
+ DBusMessage *r;
+ char **paths = NULL;
+ int num = -1;
+ pa_dbus_pending *p;
+ pa_bluetooth_discovery *y;
+
+ pa_assert(pending);
+
+ dbus_error_init(&e);
+
+ pa_assert_se(p = userdata);
+ pa_assert_se(y = p->context_data);
+ pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+ if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+ pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r));
+ goto end;
+ }
+
+ if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) {
+ pa_log("org.bluez.Adapter.ListDevices returned an error: '%s'\n", e.message);
+ dbus_error_free(&e);
+ } else {
+ int i;
+
+ for (i = 0; i < num; ++i)
+ found_device(y, paths[i]);
+ }
+
+end:
+ if (paths)
+ dbus_free_string_array (paths);
+
+ dbus_message_unref(r);
+
+ PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
+ pa_dbus_pending_free(p);
+}
+
+static void find_device_reply(DBusPendingCall *pending, void *userdata) {
+ DBusError e;
+ DBusMessage *r;
+ char *path = NULL;
+ pa_dbus_pending *p;
+ pa_bluetooth_discovery *y;
+
+ pa_assert(pending);
+
+ dbus_error_init(&e);
+
+ pa_assert_se(p = userdata);
+ pa_assert_se(y = p->context_data);
+ pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+ if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+ pa_log("Error from FindDevice reply: %s", dbus_message_get_error_name(r));
+ goto end;
+ }
+
+ if (!dbus_message_get_args(r, &e, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ pa_log("org.bluez.Adapter.FindDevice returned an error: '%s'\n", e.message);
+ dbus_error_free(&e);
+ } else
+ found_device(y, path);
+
+end:
+ dbus_message_unref(r);
+
+ PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
+ pa_dbus_pending_free(p);
+}
+
+static void found_adapter(pa_bluetooth_discovery *y, const char *path) {
+ DBusMessage *m;
+
+ if (y->mode == MODE_FIND) {
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Adapter", "FindDevice"));
+
+ pa_assert_se(dbus_message_append_args(m,
+ DBUS_TYPE_STRING, &y->looking_for,
+ DBUS_TYPE_INVALID));
+
+ send_and_add_to_pending(y, NULL, m, find_device_reply);
+
+ } else {
+ pa_assert(y->mode == MODE_DISCOVER);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Adapter", "ListDevices"));
+ send_and_add_to_pending(y, NULL, m, list_devices_reply);
+ }
+}
+
+static void list_adapters_reply(DBusPendingCall *pending, void *userdata) {
+ DBusError e;
+ DBusMessage *r;
+ char **paths = NULL;
+ int num = -1;
+ pa_dbus_pending *p;
+ pa_bluetooth_discovery *y;
+
+ pa_assert(pending);
+
+ dbus_error_init(&e);
+
+ pa_assert_se(p = userdata);
+ pa_assert_se(y = p->context_data);
+ pa_assert_se(r = dbus_pending_call_steal_reply(pending));
+
+ if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
+ pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r));
+ goto end;
+ }
+
+ if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) {
+ pa_log("org.bluez.Manager.ListAdapters returned an error: %s", e.message);
+ dbus_error_free(&e);
+ } else {
+ int i;
+
+ for (i = 0; i < num; ++i)
+ found_adapter(y, paths[i]);
+ }
+
+end:
+ if (paths)
+ dbus_free_string_array (paths);
+
+ dbus_message_unref(r);
+
+ PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
+ pa_dbus_pending_free(p);
+}
+
+static void list_adapters(pa_bluetooth_discovery *y) {
+ DBusMessage *m;
+ pa_assert(y);
+
+ pa_assert_se(m = dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
+ send_and_add_to_pending(y, NULL, m, list_adapters_reply);
+}
+
+static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
+ DBusError err;
+ pa_bluetooth_discovery *y;
+
+ pa_assert(bus);
+ pa_assert(m);
+
+ pa_assert_se(y = userdata);
+
+ dbus_error_init(&err);
+
+ pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
+ dbus_message_get_interface(m),
+ dbus_message_get_path(m),
+ dbus_message_get_member(m));
+
+ if (dbus_message_is_signal(m, "org.bluez.Adapter", "DeviceRemoved")) {
+ const char *path;
+ pa_bluetooth_device *d;
+
+ if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err.message);
+ goto fail;
+ }
+
+ pa_log_debug("Device %s removed", path);
+
+ if ((d = pa_hashmap_remove(y->devices, path))) {
+
+ pa_assert_se(y->mode == MODE_DISCOVER);
+ run_callback(y, d, FALSE);
+
+ pa_bluetooth_device_free(d);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+ } else if (dbus_message_is_signal(m, "org.bluez.Adapter", "DeviceCreated")) {
+ const char *path;
+
+ if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err.message);
+ goto fail;
+ }
+
+ pa_log_debug("Device %s created", path);
+
+ found_device(y, path);
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+ } else if (dbus_message_is_signal(m, "org.bluez.Manager", "AdapterAdded")) {
+ const char *path;
+
+ if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
+ pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err.message);
+ goto fail;
+ }
+
+ pa_log_debug("Adapter %s created", path);
+
+ found_adapter(y, path);
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+ } else if (dbus_message_is_signal(m, "org.bluez.Headset", "PropertyChanged") ||
+ dbus_message_is_signal(m, "org.bluez.AudioSink", "PropertyChanged") ||
+ dbus_message_is_signal(m, "org.bluez.Device", "PropertyChanged")) {
+
+ pa_bluetooth_device *d;
+
+ if ((d = pa_hashmap_get(y->devices, dbus_message_get_path(m)))) {
+ DBusMessageIter arg_i;
+
+ if (!dbus_message_iter_init(m, &arg_i)) {
+ pa_log("Failed to parse PropertyChanged: %s", err.message);
+ goto fail;
+ }
+
+ if (dbus_message_has_interface(m, "org.bluez.Device")) {
+ if (parse_device_property(y, d, &arg_i) < 0)
+ goto fail;
+
+ } else if (dbus_message_has_interface(m, "org.bluez.Headset")) {
+ if (parse_audio_property(y, &d->headset_connected, &arg_i) < 0)
+ goto fail;
+
+ } else if (dbus_message_has_interface(m, "org.bluez.AudioSink")) {
+ if (parse_audio_property(y, &d->audio_sink_connected, &arg_i) < 0)
+ goto fail;
+ }
+
+ pa_assert_se(y->mode == MODE_DISCOVER);
+ run_callback(y, d, TRUE);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+fail:
+ dbus_error_free(&err);
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+pa_bluetooth_device* pa_bluetooth_find_device(DBusConnection *c, const char* address) {
+ pa_bluetooth_discovery y;
+
+ memset(&y, 0, sizeof(y));
+ y.mode = MODE_FIND;
+ y.looking_for = address;
+ y.connection = c;
+ PA_LLIST_HEAD_INIT(pa_dbus_pending, y.pending);
+
+ list_adapters(&y);
+
+ pa_dbus_sync_pending_list(&y.pending);
+ pa_assert(!y.pending);
+
+ if (y.found_device) {
+ pa_assert(device_is_loaded(y.found_device));
+
+ if (!device_is_audio(y.found_device)) {
+ pa_bluetooth_device_free(y.found_device);
+ return NULL;
+ }
+ }
+
+ return y.found_device;
+}
+
+pa_bluetooth_device* pa_bluetooth_get_device(DBusConnection *c, const char* path) {
+ pa_bluetooth_discovery y;
+
+ memset(&y, 0, sizeof(y));
+ y.mode = MODE_GET;
+ y.connection = c;
+ PA_LLIST_HEAD_INIT(pa_dbus_pending, y.pending);
+
+ found_device(&y, path);
+
+ pa_dbus_sync_pending_list(&y.pending);
+ pa_assert(!y.pending);
+
+ if (y.found_device) {
+ pa_assert(device_is_loaded(y.found_device));
+
+ if (!device_is_audio(y.found_device)) {
+ pa_bluetooth_device_free(y.found_device);
+ return NULL;
+ }
+ }
+
+ return y.found_device;
+}
+
+pa_bluetooth_discovery* pa_bluetooth_discovery_new(DBusConnection *c, pa_bluetooth_device_callback_t cb, struct userdata *u) {
+ DBusError err;
+ pa_bluetooth_discovery *y;
+
+ pa_assert(c);
+ pa_assert(cb);
+
+ dbus_error_init(&err);
+
+ y = pa_xnew0(pa_bluetooth_discovery, 1);
+ y->mode = MODE_DISCOVER;
+ y->connection = c;
+ y->callback = cb;
+ y->userdata = u;
+ y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+ PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
+
+ /* dynamic detection of bluetooth audio devices */
+ if (!dbus_connection_add_filter(c, filter_cb, y, NULL)) {
+ pa_log_error("Failed to add filter function");
+ goto fail;
+ }
+
+ if (pa_dbus_add_matches(
+ c, &err,
+ "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL) < 0) {
+ pa_log("Failed to add D-Bus matches: %s", err.message);
+ goto fail;
+ }
+
+ list_adapters(y);
+
+ return y;
+
+fail:
+ dbus_error_free(&err);
+ return NULL;
+}
+
+void pa_bluetooth_discovery_free(pa_bluetooth_discovery *y) {
+ pa_bluetooth_device *d;
+
+ pa_assert(y);
+
+ pa_dbus_free_pending_list(&y->pending);
+
+ if (y->devices) {
+ while ((d = pa_hashmap_steal_first(y->devices))) {
+ run_callback(y, d, FALSE);
+ pa_bluetooth_device_free(d);
+ }
+
+ pa_hashmap_free(y->devices, NULL, NULL);
+ }
+
+ if (y->connection) {
+ pa_dbus_remove_matches(y->connection,
+ "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
+ "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL);
+
+ dbus_connection_remove_filter(y->connection, filter_cb, y);
+ }
+}
+
+void pa_bluetooth_discovery_sync(pa_bluetooth_discovery *y) {
+ pa_assert(y);
+
+ pa_dbus_sync_pending_list(&y->pending);
+}
diff --git a/src/modules/bluetooth/bluetooth-util.h b/src/modules/bluetooth/bluetooth-util.h
new file mode 100644
index 00000000..ea6687a1
--- /dev/null
+++ b/src/modules/bluetooth/bluetooth-util.h
@@ -0,0 +1,77 @@
+#ifndef foobluetoothutilhfoo
+#define foobluetoothutilhfoo
+
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2008 Joao 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 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.
+***/
+
+#include <dbus/dbus.h>
+
+#include <pulsecore/llist.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/core-util.h>
+
+typedef struct pa_bluetooth_uuid pa_bluetooth_uuid;
+typedef struct pa_bluetooth_device pa_bluetooth_device;
+typedef struct pa_bluetooth_discovery pa_bluetooth_discovery;
+
+struct userdata;
+
+struct pa_bluetooth_uuid {
+ char *uuid;
+ PA_LLIST_FIELDS(pa_bluetooth_uuid);
+};
+
+struct pa_bluetooth_device {
+ void *data; /* arbitrary information for the one owning the discovery object */
+
+ int device_info_valid; /* 0: no results yet; 1: good results; -1: bad results ... */
+ int audio_sink_info_valid; /* ... same here ... */
+ int headset_info_valid; /* ... and here */
+
+ /* Device information */
+ char *name;
+ char *path;
+ int paired;
+ char *alias;
+ int device_connected;
+ PA_LLIST_HEAD(pa_bluetooth_uuid, uuids);
+ char *address;
+ int class;
+ int trusted;
+
+ /* AudioSink information */
+ int audio_sink_connected;
+
+ /* Headset information */
+ int headset_connected;
+};
+
+void pa_bluetooth_device_free(pa_bluetooth_device *d);
+
+pa_bluetooth_device* pa_bluetooth_get_device(DBusConnection *c, const char* path);
+pa_bluetooth_device* pa_bluetooth_find_device(DBusConnection *c, const char* address);
+
+typedef void (*pa_bluetooth_device_callback_t)(struct userdata *u, pa_bluetooth_device *d, pa_bool_t good);
+pa_bluetooth_discovery* pa_bluetooth_discovery_new(DBusConnection *c, pa_bluetooth_device_callback_t cb, struct userdata *u);
+void pa_bluetooth_discovery_free(pa_bluetooth_discovery *d);
+void pa_bluetooth_discovery_sync(pa_bluetooth_discovery *d);
+
+#endif
diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c
index 11ed7d33..e4b834a7 100644
--- a/src/modules/bluetooth/module-bluetooth-device.c
+++ b/src/modules/bluetooth/module-bluetooth-device.c
@@ -1,22 +1,22 @@
/***
- This file is part of PulseAudio.
+ This file is part of PulseAudio.
- Copyright 2008 Joao Paulo Rechi Vita
+ Copyright 2008 Joao 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 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
@@ -44,13 +44,14 @@
#include <pulsecore/time-smoother.h>
#include <pulsecore/rtclock.h>
-#include "../dbus-util.h"
+#include <modules/dbus-util.h>
+
#include "module-bluetooth-device-symdef.h"
#include "ipc.h"
#include "sbc.h"
#include "rtp.h"
+#include "bluetooth-util.h"
-#define DEFAULT_SINK_NAME "bluetooth_sink"
#define BUFFER_SIZE 2048
#define MAX_BITPOOL 64
#define MIN_BITPOOL 2U
@@ -63,13 +64,29 @@ PA_MODULE_DESCRIPTION("Bluetooth audio sink and source");
PA_MODULE_VERSION(PACKAGE_VERSION);
PA_MODULE_LOAD_ONCE(FALSE);
PA_MODULE_USAGE(
- "sink_name=<name of the device> "
+ "name=<name for the card/sink/source, to be prefixed> "
+ "card_name=<name for the card> "
+ "sink_name=<name for the sink> "
+ "source_name=<name for the source> "
"address=<address of the device> "
"profile=<a2dp|hsp> "
"rate=<sample rate> "
"channels=<number of channels> "
"path=<device object path>");
+static const char* const valid_modargs[] = {
+ "name",
+ "card_name",
+ "sink_name",
+ "source_name",
+ "address",
+ "profile",
+ "rate",
+ "channels",
+ "path",
+ NULL
+};
+
struct bt_a2dp {
sbc_capabilities_t sbc_capabilities;
sbc_t sbc; /* Codec data */
@@ -84,6 +101,12 @@ struct bt_a2dp {
unsigned frame_count; /* Current frames in buffer*/
};
+enum profile {
+ PROFILE_A2DP,
+ PROFILE_SCO,
+ PROFILE_OFF
+};
+
struct userdata {
pa_core *core;
pa_module *module;
@@ -98,33 +121,24 @@ struct userdata {
uint64_t offset;
pa_smoother *smoother;
- char *name;
- char *addr;
- char *profile;
- pa_sample_spec ss;
+ char *address;
+ pa_sample_spec sample_spec;
- int audioservice_fd;
+ int service_fd;
int stream_fd;
uint8_t transport;
- char *strtransport;
size_t link_mtu;
size_t block_size;
pa_usec_t latency;
struct bt_a2dp a2dp;
char *path;
- pa_dbus_connection *conn;
-};
+ pa_dbus_connection *connection;
-static const char* const valid_modargs[] = {
- "sink_name",
- "address",
- "profile",
- "rate",
- "channels",
- "path",
- NULL
+ enum profile profile;
+
+ pa_bluetooth_device *device;
};
static int bt_audioservice_send(int sk, const bt_audio_msg_header_t *msg) {
@@ -234,7 +248,7 @@ static int bt_getcaps(struct userdata *u) {
msg.getcaps_req.h.name = BT_GET_CAPABILITIES;
msg.getcaps_req.h.length = sizeof(msg.getcaps_req);
- strncpy(msg.getcaps_req.device, u->addr, 18);
+ strncpy(msg.getcaps_req.device, u->address, 18);
if (pa_streq(u->profile, "a2dp"))
msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_A2DP;
else if (pa_streq(u->profile, "hsp"))
@@ -489,7 +503,6 @@ static int bt_setconf(struct userdata *u) {
}
u->transport = msg.setconf_rsp.transport;
- u->strtransport = (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP ? pa_xstrdup("A2DP") : pa_xstrdup("SCO"));
u->link_mtu = msg.setconf_rsp.link_mtu;
/* setup SBC encoder now we agree on parameters */
@@ -933,176 +946,398 @@ static int source_set_volume_cb(pa_source *s) {
return 0;
}
-int pa__init(pa_module* m) {
- int e;
- pa_modargs *ma;
- uint32_t channels;
- pa_sink_new_data data;
- struct pollfd *pollfd;
- struct userdata *u;
- DBusError err;
- char *tmp;
+static char *get_name(const char *type, pa_modargs *ma, const char *device_id, pa_bool_t *namereg_fail) {
+ char *t;
+ const char *n;
- pa_assert(m);
- dbus_error_init(&err);
+ pa_assert(type);
+ pa_assert(ma);
+ pa_assert(device_id);
+ pa_assert(namereg_fail);
- m->userdata = u = pa_xnew0(struct userdata, 1);
- u->module = m;
- u->core = m->core;
- u->audioservice_fd = -1;
- u->stream_fd = -1;
- u->transport = (uint8_t) -1;
- u->offset = 0;
- u->latency = 0;
- u->a2dp.sbc_initialized = FALSE;
- u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10);
- u->rtpoll = pa_rtpoll_new();
- pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll);
- u->rtpoll_item = NULL;
- u->ss = m->core->default_sample_spec;
+ t = pa_sprintf_malloc("%s_name", type);
+ n = pa_modargs_get_value(ma, t, NULL);
+ pa_xfree(t);
- if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log_error("Failed to parse module arguments");
- goto fail;
- }
- if (!(u->name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME)))) {
- pa_log_error("Failed to get device name from module arguments");
- goto fail;
- }
- if (!(u->addr = pa_xstrdup(pa_modargs_get_value(ma, "address", NULL)))) {
- pa_log_error("Failed to get device address from module arguments");
- goto fail;
+ if (n) {
+ *namereg_fail = TRUE;
+ return pa_xstrdup(n);
}
- if (!(u->profile = pa_xstrdup(pa_modargs_get_value(ma, "profile", NULL)))) {
- pa_log_error("Failed to get profile from module arguments");
- goto fail;
+
+ if ((n = pa_modargs_get_value(ma, "name", NULL)))
+ *namereg_fail = TRUE;
+ else {
+ n = device_id;
+ *namereg_fail = FALSE;
}
- if (pa_modargs_get_value_u32(ma, "rate", &u->ss.rate) < 0) {
- pa_log_error("Failed to get rate from module arguments");
- goto fail;
+
+ return pa_sprintf_malloc("bluez_%s.%s", type, n);
+}
+
+static int add_sink(struct userdata *u) {
+ pa_sink_new_data data;
+ pa_bool_t b;
+
+ pa_sink_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_sink_new_data_set_sample_spec(&data, &u->ss);
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Bluetooth %s on %s", u->profile == PROFILE_A2DP ? "A2DP" : "HSP/HFP", u->addr);
+ pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "sco");
+ data.card = u->card;
+ data.name = get_name("sink", ma, u->addr, &b);
+ data.namereg_fail = b;
+
+ u->sink = pa_sink_new(u->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
+ pa_sink_new_data_done(&data);
+
+ if (!u->sink) {
+ pa_log_error("Failed to create sink");
+ return -1;
}
- u->path = pa_xstrdup(pa_modargs_get_value(ma, "path", NULL));
- channels = u->ss.channels;
- if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0) {
- pa_log_error("Failed to get channels from module arguments");
- goto fail;
+ u->sink->userdata = u;
+ u->sink->parent.process_msg = sink_process_msg;
+ u->sink->get_volume = sink_get_volume_cb;
+ u->sink->set_volume = sink_set_volume_cb;
+
+ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
+ pa_sink_set_rtpoll(u->sink, u->rtpoll);
+
+ return 0;
+}
+
+static int add_source(struct userdata *u) {
+ pa_source_new_data data;
+ pa_bool_t b;
+
+ pa_source_new_data_init(&data);
+ data.driver = __FILE__;
+ data.module = m;
+ pa_source_new_data_set_sample_spec(&data, &u->ss);
+ pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Bluetooth %s on %s", u->profile == PROFILE_A2DP ? "A2DP" : "HSP/HFP", u->addr);
+ pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "sco");
+ data.card = u->card;
+ data.name = get_name("source", ma, u->addr, &b);
+ data.namereg_fail = b;
+
+ u->source = pa_source_new(u->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY);
+ pa_source_new_data_done(&data);
+
+ if (!u->source) {
+ pa_log_error("Failed to create source");
+ return -1;
}
- u->ss.channels = (uint8_t) channels;
+
+ u->source->userdata = u;
+ u->source->parent.process_msg = source_process_msg;
+ u->source->get_volume = source_get_volume_cb;
+ u->source->set_volume = source_set_volume_cb;
+
+ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
+ pa_source_set_rtpoll(u->source, u->rtpoll);
+
+ return 0;
+}
+
+static void init_profile(struct userdata *u) {
+ enum profile *d;
+
+ pa_assert(u);
+
+ if (u->profile == PROFILE_A2DP ||
+ u->profile == PROFILE_SCO)
+ add_sink(u);
+
+ if (u->profile == PROFILE_SCO)
+ add_source(u);
+}
+
+static int init_bt(struct userdata *u) {
+ pa_assert(u);
/* connect to the bluez audio service */
- u->audioservice_fd = bt_audio_service_open();
- if (u->audioservice_fd <= 0) {
+ if ((u->audioservice_fd = bt_audio_service_open()) < 0) {
pa_log_error("Couldn't connect to bluetooth audio service");
- goto fail;
+ return -1;
}
pa_log_debug("Connected to the bluetooth audio service");
/* queries device capabilities */
- e = bt_getcaps(u);
- if (e < 0) {
+ if (bt_getcaps(u) < 0) {
pa_log_error("Failed to get device capabilities");
- goto fail;
+ return -1;
}
pa_log_debug("Got device capabilities");
+ return 0;
+}
+
+static setup_bt(struct userdata *u) {
+ pa_assert(u);
+
/* configures the connection */
- e = bt_setconf(u);
- if (e < 0) {
+ if (bt_setconf(u) < 0) {
pa_log_error("Failed to set config");
- goto fail;
+ return -1;
}
pa_log_debug("Connection to the device configured");
/* gets the device socket */
- e = bt_getstreamfd(u);
- if (e < 0) {
+ if (bt_getstreamfd(u) < 0) {
pa_log_error("Failed to get stream fd (%d)", e);
- goto fail;
+ return -1;
}
pa_log_debug("Got the device socket");
+}
- /* create sink */
- pa_sink_new_data_init(&data);
+static int card_set_profile(pa_card *c, pa_card_profile *new_profile) {
+ struct userdata *u;
+ enum profile *d;
+ pa_queue *inputs = NULL, *outputs = NULL;
+
+ pa_assert(c);
+ pa_assert(new_profile);
+ pa_assert_se(u = c->userdata);
+
+ d = PA_CARD_PROFILE_DATA(new_profile);
+
+ if (u->sink) {
+ inputs = pa_sink_move_all_start(u->sink);
+
+ pa_sink_unlink(u->sink);
+ pa_sink_unref(u->sink);
+ u->sink = NULL;
+ }
+
+ if (u->source) {
+ outputs = pa_source_move_all_start(u->source);
+
+ pa_source_unlink(u->source);
+ pa_source_unref(u->source);
+ u->source = NULL;
+ }
+
+ u->profile = *d;
+
+ init_profile(u);
+
+ if (inputs) {
+ if (u->sink)
+ pa_sink_move_all_finish(u->sink, inputs, FALSE);
+ else
+ pa_sink_move_all_fail(inputs);
+ }
+
+ if (outputs) {
+ if (u->source)
+ pa_source_move_all_finish(u->source, outputs, FALSE);
+ else
+ pa_source_move_all_fail(outputs);
+ }
+
+ return 0;
+}
+
+static int add_card(struct userdata *u) {
+ pa_card_new_data data;
+ pa_bool_t b;
+ pa_card_profile *p;
+ enum profile *d;
+
+ pa_card_new_data_init(&data);
data.driver = __FILE__;
- data.module = m;
- pa_sink_new_data_set_name(&data, u->name);
- pa_sink_new_data_set_sample_spec(&data, &u->ss);
- pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->name);
- pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Bluetooth %s '%s' (%s)", u->strtransport, u->name, u->addr);
- pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile);
+ data.module = u->module;
+ pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->addr);
pa_proplist_setf(data.proplist, PA_PROP_DEVICE_API, "bluez");
pa_proplist_setf(data.proplist, PA_PROP_DEVICE_CLASS, "sound");
pa_proplist_setf(data.proplist, PA_PROP_DEVICE_CONNECTOR, "bluetooth");
/* pa_proplist_setf(data.proplist, PA_PROP_DEVICE_FORM_FACTOR, "headset"); /\*FIXME*\/ */
/* pa_proplist_setf(data.proplist, PA_PROP_DEVICE_VENDOR_PRODUCT_ID, "product_id"); /\*FIXME*\/ */
/* pa_proplist_setf(data.proplist, PA_PROP_DEVICE_SERIAL, "serial"); /\*FIXME*\/ */
- u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY);
- pa_sink_new_data_done(&data);
- if (!u->sink) {
- pa_log_error("Failed to create sink");
+ data.name = get_name("card", ma, u->addr, &b);
+ data.namereg_fail = b;
+
+ data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+
+ if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP ||
+ u->transport == BT_CAPABILITIES_TRANSPORT_ANY) {
+
+ p = pa_card_profile_new("a2dp", "A2DP Advanced Audio Distribution Profile", sizeof(enum profile));
+ p->priority = 10;
+ p->n_sinks = 1;
+ p->n_sources = 0;
+ p->max_sink_channels = 2;
+ p->max_source_channels = 0;
+
+ d = PA_CARD_PROFILE_DATA(p);
+ *d = PROFILE_A2DP;
+ }
+
+ if (u->transport == BT_CAPABILITIES_TRANSPORT_SCO ||
+ u->transport == BT_CAPABILITIES_TRANSPORT_ANY) {
+
+ p = pa_card_profile_new("hsp", "HSP/HFP Headset/Hands-Free Profile", sizeof(enum profile));
+ p->priority = 20;
+ p->n_sinks = 1;
+ p->n_sources = 1;
+ p->max_sink_channels = 1;
+ p->max_source_channels = 1;
+
+ d = PA_CARD_PROFILE_DATA(p);
+ *d = PROFILE_SCO;
+ }
+
+ pa_assert(!pa_hashmap_isempty(data.profiles));
+
+ p = pa_card_profile_new("off", "Off", sizeof(enum profile));
+ d = PA_CARD_PROFILE_DATA(p);
+ *d = PROFILE_OFF;
+
+ u->card = pa_card_new(m->core, &data);
+ pa_card_new_data_done(&data);
+
+ if (!u->card) {
+ pa_log("Failed to allocate card.");
+ return -1;
+ }
+
+ card->userdata = u;
+ card->set_profile = card_set_profile;
+
+ d = PA_CARD_PROFILE_DATA(u->card->active_profile);
+ u->profile = *d;
+
+ return 0;
+}
+
+int pa__init(pa_module* m) {
+ pa_modargs *ma;
+ uint32_t channels;
+ pa_sink_new_data sink_data;
+ pa_source_new_data source_data;
+ pa_card card_data;
+ struct pollfd *pollfd;
+ struct userdata *u;
+ pa_bool_t b;
+ const char *p;
+
+ pa_assert(m);
+
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log_error("Failed to parse module arguments");
goto fail;
}
- u->sink->userdata = u;
- u->sink->parent.process_msg = sink_process_msg;
- pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
- pa_sink_set_rtpoll(u->sink, u->rtpoll);
- u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
- pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
- pollfd->fd = u->stream_fd;
- pollfd->events = pollfd->revents = 0;
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ u->core = m->core;
+ u->audioservice_fd = -1;
+ u->stream_fd = -1;
+ u->transport = (uint8_t) -1;
+ u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10);
+ u->rtpoll = pa_rtpoll_new();
+ pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll);
+ u->add_source = m->core->default_sample_spec;
- /* start rt thread */
- if (!(u->thread = pa_thread_new(thread_func, u))) {
- pa_log_error("Failed to create IO thread");
+ if (!(u->address = pa_xstrdup(pa_modargs_get_value(ma, "address", NULL)))) {
+ pa_log_error("Failed to get device address from module arguments");
+ goto fail;
+ }
+
+ u->path = pa_xstrdup(pa_modargs_get_value(ma, "path", NULL));
+
+ p = pa_modargs_get_value(ma, "profile", NULL);
+ if (p && !pa_streq(p, "hsp") && !pa_streq(p, "a2dp")) {
+ pa_log_error("Failed to get profile from module arguments");
goto fail;
}
- pa_sink_put(u->sink);
- if (!u->path)
- goto end;
-
- /* 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);
+
+ if (pa_modargs_get_value_u32(ma, "rate", &u->ss.rate) < 0 ||
+ u->ss.rate <= 0 || u->ss.rate > PA_RATE_MAX) {
+ pa_log_error("Failed to get rate from module arguments");
goto fail;
}
- /* monitor property changes */
- if (!dbus_connection_add_filter(pa_dbus_connection_get(u->conn), filter_cb, u, NULL)) {
- pa_log_error("Failed to add filter function");
+ channels = u->ss.channels;
+ if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 ||
+ channels <= 0 || channels > PA_CHANNELS_MAX) {
+ pa_log_error("Failed to get channels from module arguments");
goto fail;
}
+ u->ss.channels = (uint8_t) channels;
+
+ /* Connect to the BT service and query capabilities */
+ if (init_bt(u) < 0)
+ goto fail;
+
+ /* Add the card structure. This will also initialize the default profile */
+ if (add_card(u) < 0)
+ goto fail;
- if (pa_streq(u->profile, "hsp")) {
+ /* Now configure the the right profile */
+ if (setup_bt(u) < 0)
- tmp = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged',path='%s'", u->path);
- dbus_bus_add_match(pa_dbus_connection_get(u->conn), tmp, &err);
- pa_xfree(tmp);
- if (dbus_error_is_set(&err)) {
- pa_log_error("Unable to subscribe to org.bluez.Headset signals: %s: %s", err.name, err.message);
+ if (init_profile(u) < 0)
+ goto fail;
+
+ if (u->path) {
+ DBusError err;
+ dbus_error_init(&err);
+ char *t;
+
+ u->conn = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &err);
+ if (dbus_error_is_set(&err) || (!u->conn)) {
+ pa_log("Failed to get D-Bus connection: %s", err.message);
goto fail;
}
- } else {
- tmp = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged',path='%s'", u->path);
- dbus_bus_add_match(pa_dbus_connection_get(u->conn), tmp, &err);
- pa_xfree(tmp);
- if (dbus_error_is_set(&err)) {
- pa_log_error("Unable to subscribe to org.bluez.AudioSink signals: %s: %s", err.name, err.message);
+ 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;
}
+
+ if (u->transport == BT_CAPABILITIES_TRANSPORT_SCO ||
+ u->transport == BT_CAPABILITIES_TRANSPORT_ANY) {
+ t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged',path='%s'", u->path);
+ dbus_bus_add_match(pa_dbus_connection_get(u->conn), t, &err);
+ pa_xfree(t);
+
+ 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;
+ }
+ }
+
+ if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP ||
+ u->transport == BT_CAPABILITIES_TRANSPORT_ANY) {
+ t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged',path='%s'", u->path);
+ dbus_bus_add_match(pa_dbus_connection_get(u->conn), t, &err);
+ pa_xfree(t);
+
+ 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;
+ }
+ }
}
- u->sink->get_volume = sink_get_volume_cb;
- u->sink->set_volume = sink_set_volume_cb;
- if (u->source) {
- u->source->get_volume = source_get_volume_cb;
- u->source->set_volume = source_set_volume_cb;
+ u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
+ pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
+ pollfd->fd = u->stream_fd;
+ pollfd->events = pollfd->revents = 0;
+
+ if (!(u->thread = pa_thread_new(thread_func, u))) {
+ pa_log_error("Failed to create IO thread");
+ goto fail;
}
-end:
+ if (u->sink)
+ pa_sink_put(u->sink);
+
+ if (u->source)
+ pa_sink_put(u->source);
+
pa_modargs_free(ma);
return 0;
@@ -1114,6 +1349,17 @@ fail:
return -1;
}
+int pa__get_n_used(pa_module *m) {
+ struct userdata *u;
+
+ pa_assert(m);
+ pa_assert_se(u = m->userdata);
+
+ return
+ (u->sink ? pa_sink_linked_by(u->sink) : 0) +
+ (u->source ? pa_source_linked_by(u->source) : 0);
+}
+
void pa__done(pa_module *m) {
struct userdata *u;
pa_assert(m);
@@ -1123,23 +1369,29 @@ void pa__done(pa_module *m) {
if (u->conn) {
DBusError error;
- char *tmp;
- dbus_error_init(&error);
+ char *t;
- if (pa_streq(u->profile, "hsp")) {
- tmp = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged',path='%s'", u->path);
- dbus_bus_remove_match(pa_dbus_connection_get(u->conn), tmp, &error);
- pa_xfree(tmp);
+ if (u->transport == BT_CAPABILITIES_TRANSPORT_SCO ||
+ u->transport == BT_CAPABILITIES_TRANSPORT_ANY) {
+
+ t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged',path='%s'", u->path);
+ dbus_error_init(&error);
+ dbus_bus_remove_match(pa_dbus_connection_get(u->conn), t, &error);
dbus_error_free(&error);
- } else {
- tmp = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged',path='%s'", u->path);
- dbus_bus_remove_match(pa_dbus_connection_get(u->conn), tmp, &error);
- pa_xfree(tmp);
+ pa_xfree(t);
+ }
+
+ if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP ||
+ u->transport == BT_CAPABILITIES_TRANSPORT_ANY) {
+
+ t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged',path='%s'", u->path);
+ dbus_error_init(&error);
+ dbus_bus_remove_match(pa_dbus_connection_get(u->conn), t, &error);
dbus_error_free(&error);
+ pa_xfree(t);
}
dbus_connection_remove_filter(pa_dbus_connection_get(u->conn), filter_cb, u);
-
pa_dbus_connection_unref(u->conn);
}
@@ -1149,6 +1401,9 @@ void pa__done(pa_module *m) {
if (u->sink)
pa_sink_unlink(u->sink);
+ if (u->source)
+ pa_source_unlink(u->source);
+
if (u->thread) {
pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
pa_thread_free(u->thread);
@@ -1157,6 +1412,12 @@ void pa__done(pa_module *m) {
if (u->sink)
pa_sink_unref(u->sink);
+ if (u->source)
+ pa_source_unref(u->source);
+
+ if (u->card)
+ pa_card_unref(u->card);
+
pa_thread_mq_done(&u->thread_mq);
if (u->rtpoll_item)
@@ -1177,7 +1438,7 @@ void pa__done(pa_module *m) {
pa_close(u->stream_fd);
if (u->audioservice_fd >= 0)
- pa_close(u->audioservice_fd);
+ pa_close(u->service_fd);
pa_xfree(u);
}
diff --git a/src/modules/bluetooth/module-bluetooth-discover.c b/src/modules/bluetooth/module-bluetooth-discover.c
index 1bc05c02..d9d3449b 100644
--- a/src/modules/bluetooth/module-bluetooth-discover.c
+++ b/src/modules/bluetooth/module-bluetooth-discover.c
@@ -1,22 +1,22 @@
/***
- This file is part of PulseAudio.
+ This file is part of PulseAudio.
- Copyright 2008 Joao Paulo Rechi Vita
+ Copyright 2008 Joao 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 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
@@ -29,817 +29,138 @@
#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 <modules/dbus-util.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_VERSION(PACKAGE_VERSION);
-PA_MODULE_USAGE("");
-
-struct module {
- char *profile;
- uint32_t index;
- PA_LLIST_FIELDS(struct module);
-};
-
-struct uuid {
- char *uuid;
- PA_LLIST_FIELDS(struct uuid);
-};
+PA_MODULE_USAGE("async=<Asynchronous initialization?>");
-struct dbus_pending {
- char *path;
- char *profile;
- DBusPendingCall *pending;
- PA_LLIST_FIELDS(struct dbus_pending);
-};
-
-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);
+static const char* const valid_modargs[] = {
+ "async",
+ NULL
};
struct userdata {
pa_module *module;
- pa_dbus_connection *conn;
- dbus_int32_t dbus_data_slot;
- PA_LLIST_HEAD(struct device, device_list);
- PA_LLIST_HEAD(struct dbus_pending, dbus_pending_list);
+ pa_core *core;
+ pa_dbus_connection *connection;
+ pa_bluetooth_discovery *discovery;
};
-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->index = pa_m->index;
- 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 module* module_find(struct device *d, const char *profile) {
- struct module *m;
-
- for (m = d->module_list; m; m = m->next)
- if (pa_streq(m->profile, profile))
- return m;
-
- return NULL;
-}
-
-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 dbus_pending *dbus_pending_new(struct userdata *u, DBusPendingCall *pending, const char *path, const char *profile) {
- struct dbus_pending *node;
-
- pa_assert(pending);
-
- node = pa_xnew(struct dbus_pending, 1);
- node->pending = pending;
- node->path = pa_xstrdup(path);
- node->profile = pa_xstrdup(profile);
- PA_LLIST_INIT(struct dbus_pending, node);
- dbus_pending_call_set_data(pending, u->dbus_data_slot, node, NULL);
-
- return node;
-}
-
-static void dbus_pending_free(struct dbus_pending *pending) {
- pa_assert(pending);
-
- pa_xfree(pending->path);
- pa_xfree(pending->profile);
- dbus_pending_call_cancel(pending->pending);
- dbus_pending_call_unref(pending->pending);
- pa_xfree(pending);
-}
-
-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;
-}
-
-static int parse_device_property(struct userdata *u, struct device *d, DBusMessageIter *i) {
- const char *key;
- DBusMessageIter variant_i;
-
+static void load_module_for_device(struct userdata *u, pa_bluetooth_device *d, pa_bool_t good) {
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);
-
- 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;
- }
-
- dbus_message_iter_recurse(i, &variant_i);
-
- pa_log_debug("Parsing device property %s", key);
-
- switch (dbus_message_iter_get_arg_type(&variant_i)) {
-
- case DBUS_TYPE_STRING: {
-
- 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);
- }
-
- break;
- }
+ if (good &&
+ d->device_connected > 0 &&
+ (d->audio_sink_connected > 0 || d->headset_connected > 0)) {
- case DBUS_TYPE_BOOLEAN: {
+ if (PA_PTR_TO_UINT(d->data) == PA_INVALID_INDEX) {
+ pa_module *m = NULL;
+ char *args;
- dbus_bool_t value;
- dbus_message_iter_get_basic(&variant_i, &value);
+ /* Oh, awesome, a new device has shown up and been connected! */
- 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;
+ args = pa_sprintf_malloc("name=\"%s\" address=\"%s\" path=\"%s\"", d->address, d->address, d->path);
+ pa_log_debug("Loading module-bluetooth-device %s", args);
+/* m = pa_module_load(u->module->core, "module-bluetooth-device", args); */
+ pa_xfree(args);
- break;
+ if (m)
+ d->data = PA_UINT_TO_PTR(m->index);
+ 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;
+ } else {
- dbus_message_iter_get_basic(&ai, &value);
- node = uuid_new(value);
- PA_LLIST_PREPEND(struct uuid, d->uuid_list, node);
+ if (PA_PTR_TO_UINT(d->data) != PA_INVALID_INDEX) {
- if (!dbus_message_iter_next(&ai))
- break;
- }
- }
+ /* Hmm, disconnection? Then let's unload our module */
- break;
+ pa_log_debug("Unloading module for %s", d->path);
+/* pa_module_unload_request_by_index(u->core, PA_PTR_TO_UINT(d->data), TRUE); */
+ d->data = NULL;
}
}
-
- 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) {
-
- if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter dict_i;
-
- dbus_message_iter_recurse(&element_i, &dict_i);
-
- if (parse_device_property(u, d, &dict_i) < 0)
- goto finish;
- }
-
- 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;
-}
-
-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\" path=\"%s\"", d->name, d->address, profile, d->object_path);
- pa_m = pa_module_load(u->module->core, "module-bluetooth-device", args);
- pa_xfree(args);
-
- if (!pa_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 void unload_module_for_device(struct userdata *u, struct device *d, const char *profile) {
- struct module *m;
-
- pa_assert(u);
- pa_assert(d);
-
- if (!(m = module_find(d, profile)))
- return;
-
- pa_module_unload_request_by_index(u->module->core, m->index, TRUE);
-
- PA_LLIST_REMOVE(struct module, d->module_list, m);
- module_free(m);
-}
-
-static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void *userdata) {
- DBusMessageIter arg_i;
+static int setup_dbus(struct userdata *u) {
DBusError err;
- const char *value;
- struct userdata *u;
-
- 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;
- dbus_bool_t connected;
-
- 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);
-
- if (!pa_streq(value, "Connected"))
- goto done;
-
- if (!dbus_message_iter_next(&arg_i)) {
- pa_log("Property value missing");
- goto done;
- }
-
- if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_VARIANT) {
- pa_log("Property value not a variant.");
- goto done;
- }
-
- dbus_message_iter_recurse(&arg_i, &variant_i);
-
- if (dbus_message_iter_get_arg_type(&variant_i) != DBUS_TYPE_BOOLEAN) {
- pa_log("Property value not a boolean.");
- goto done;
- }
-
- dbus_message_iter_get_basic(&variant_i, &connected);
-
- if (dbus_message_is_signal(msg, "org.bluez.Headset", "PropertyChanged"))
- profile = "hsp";
- else
- profile = "a2dp";
-
- d = device_find(u, dbus_message_get_path(msg));
-
- if (connected) {
- if (!d) {
- d = device_new(dbus_message_get_path(msg));
- PA_LLIST_PREPEND(struct device, u->device_list, d);
- }
-
- load_module_for_device(u, d, profile);
- } else if (d)
- unload_module_for_device(u, d, profile);
- }
-
-done:
- dbus_error_free(&err);
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-}
-
-
-
-static void get_properties_reply(DBusPendingCall *pending, void *user_data) {
- struct userdata *u;
- DBusMessage *r;
- dbus_bool_t connected;
- DBusMessageIter arg_i, element_i;
- DBusMessageIter variant_i;
- struct device *d;
- struct dbus_pending *p;
-
- pa_assert(u = user_data);
-
- r = dbus_pending_call_steal_reply(pending);
- if (!r)
- goto end;
-
- if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
- pa_log("Error from GetProperties reply: %s", dbus_message_get_error_name(r));
- goto end;
- }
-
- if (!dbus_message_iter_init(r, &arg_i)) {
- pa_log("%s GetProperties reply has no arguments", p->profile);
- goto end;
- }
-
- if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) {
- pa_log("%s GetProperties argument is not an array", p->profile);
- goto end;
- }
-
- connected = FALSE;
- dbus_message_iter_recurse(&arg_i, &element_i);
- while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
-
- if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
- DBusMessageIter dict_i;
- const char *key;
-
- dbus_message_iter_recurse(&element_i, &dict_i);
-
- if (dbus_message_iter_get_arg_type(&dict_i) != DBUS_TYPE_STRING) {
- pa_log("Property name not a string.");
- goto end;
- }
+ u->connection = pa_dbus_bus_get(u->core, DBUS_BUS_SYSTEM, &err);
- dbus_message_iter_get_basic(&dict_i, &key);
-
- if (!dbus_message_iter_next(&dict_i)) {
- pa_log("Property value missing");
- goto end;
- }
-
- if (dbus_message_iter_get_arg_type(&dict_i) != DBUS_TYPE_VARIANT) {
- pa_log("Property value not a variant.");
- goto end;
- }
-
- dbus_message_iter_recurse(&dict_i, &variant_i);
-
- switch (dbus_message_iter_get_arg_type(&variant_i)) {
-
- case DBUS_TYPE_BOOLEAN: {
-
- dbus_bool_t value;
- dbus_message_iter_get_basic(&variant_i, &value);
-
- if (pa_streq(key, "Connected")) {
- connected = value;
- goto endloop;
- }
-
- break;
- }
- }
- }
-
- if (!dbus_message_iter_next(&element_i))
- break;
- }
-
-endloop:
- if (connected) {
- p = dbus_pending_call_get_data(pending, u->dbus_data_slot);
- pa_log_debug("%s: %s connected", p->path, p->profile);
- d = device_find(u, p->path);
-
- if (!d) {
- d = device_new(p->path);
- PA_LLIST_PREPEND(struct device, u->device_list, d);
- }
-
- load_module_for_device(u, d, p->profile);
- }
-
- dbus_message_unref(r);
-
-end:
- p = dbus_pending_call_get_data(pending, u->dbus_data_slot);
- PA_LLIST_REMOVE(struct dbus_pending, u->dbus_pending_list, p);
- dbus_pending_free(p);
-}
-
-static void list_devices_reply(DBusPendingCall *pending, void *user_data) {
- DBusMessage *r, *m;
- DBusPendingCall *call;
- DBusError e;
- char **paths = NULL;
- int i, num = -1;
- struct dbus_pending *p;
- struct userdata *u;
-
- pa_assert(u = user_data);
- dbus_error_init(&e);
-
- r = dbus_pending_call_steal_reply(pending);
- if (!r) {
- pa_log("Failed to get ListDevices reply");
- goto end;
- }
-
- if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
- pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r));
- goto end;
- }
-
- if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) {
- pa_log("org.bluez.Adapter.ListDevices returned an error: '%s'\n", e.message);
- dbus_error_free(&e);
- } else {
- for (i = 0; i < num; ++i) {
- pa_assert_se(m = dbus_message_new_method_call("org.bluez", paths[i], "org.bluez.Headset", "GetProperties"));
- if (dbus_connection_send_with_reply(pa_dbus_connection_get(u->conn), m, &call, -1)) {
- p = dbus_pending_new(u, call, paths[i], "hsp");
- PA_LLIST_PREPEND(struct dbus_pending, u->dbus_pending_list, p);
- dbus_pending_call_set_notify(call, get_properties_reply, u, NULL);
- } else {
- pa_log("Failed to send GetProperties");
- }
-
- dbus_message_unref(m);
-
- pa_assert_se(m = dbus_message_new_method_call("org.bluez", paths[i], "org.bluez.AudioSink", "GetProperties"));
- if (dbus_connection_send_with_reply(pa_dbus_connection_get(u->conn), m, &call, -1)) {
- p = dbus_pending_new(u, call, paths[i], "a2dp");
- PA_LLIST_PREPEND(struct dbus_pending, u->dbus_pending_list, p);
- dbus_pending_call_set_notify(call, get_properties_reply, u, NULL);
- } else {
- pa_log("Failed to send GetProperties");
- }
-
- dbus_message_unref(m);
- }
+ if (dbus_error_is_set(&err) || !u->connection) {
+ pa_log("Failed to get D-Bus connection: %s", err.message);
+ dbus_error_free(&err);
+ return -1;
}
- if (paths)
- dbus_free_string_array (paths);
- dbus_message_unref(r);
-
-end:
- p = dbus_pending_call_get_data(pending, u->dbus_data_slot);
- PA_LLIST_REMOVE(struct dbus_pending, u->dbus_pending_list, p);
- dbus_pending_free(p);
+ return 0;
}
-static void list_adapters_reply(DBusPendingCall *pending, void *user_data) {
- DBusMessage *r, *m;
- DBusPendingCall *call;
- DBusError e;
- char **paths = NULL;
- int i, num = -1;
- struct dbus_pending *p;
+int pa__init(pa_module* m) {
struct userdata *u;
+ pa_modargs *ma;
+ pa_bool_t async = FALSE;
- pa_assert(u = user_data);
- dbus_error_init(&e);
+ pa_assert(m);
- r = dbus_pending_call_steal_reply(pending);
- if (!r) {
- pa_log("Failed to get ListAdapters reply");
- goto end;
+ if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
+ pa_log("Failed to parse module arguments");
+ goto fail;
}
- if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
- pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r));
- goto end;
+ if (pa_modargs_get_value_boolean(ma, "async", &async) < 0) {
+ pa_log("Failed to parse tsched argument.");
+ goto fail;
}
- if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) {
- pa_log("org.bluez.Manager.ListAdapters returned an error: '%s'\n", e.message);
- dbus_error_free(&e);
- } else {
- for (i = 0; i < num; ++i) {
- pa_assert_se(m = dbus_message_new_method_call("org.bluez", paths[i], "org.bluez.Adapter", "ListDevices"));
- if (dbus_connection_send_with_reply(pa_dbus_connection_get(u->conn), m, &call, -1)) {
- p = dbus_pending_new(u, call, NULL, NULL);
- PA_LLIST_PREPEND(struct dbus_pending, u->dbus_pending_list, p);
- dbus_pending_call_set_notify(call, list_devices_reply, u, NULL);
- } else {
- pa_log("Failed to send ListDevices");
- }
-
- dbus_message_unref(m);
- }
- }
+ m->userdata = u = pa_xnew0(struct userdata, 1);
+ u->module = m;
+ u->core = m->core;
- if (paths)
- dbus_free_string_array (paths);
- dbus_message_unref(r);
+ if (setup_dbus(u) < 0)
+ goto fail;
-end:
- p = dbus_pending_call_get_data(pending, u->dbus_data_slot);
- PA_LLIST_REMOVE(struct dbus_pending, u->dbus_pending_list, p);
- dbus_pending_free(p);
-}
+ if (!(u->discovery = pa_bluetooth_discovery_new(pa_dbus_connection_get(u->connection), load_module_for_device, u)))
+ goto fail;
-static void lookup_devices(struct userdata *u) {
- DBusMessage *m;
- DBusPendingCall *call;
- struct dbus_pending *p;
+ if (!async)
+ pa_bluetooth_discovery_sync(u->discovery);
- pa_assert(u);
+ return 0;
- pa_assert_se(m = dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
- if (dbus_connection_send_with_reply(pa_dbus_connection_get(u->conn), m, &call, -1)) {
- p = dbus_pending_new(u, call, NULL, NULL);
- PA_LLIST_PREPEND(struct dbus_pending, u->dbus_pending_list, p);
- dbus_pending_call_set_notify(call, list_adapters_reply, u, NULL);
- } else {
- pa_log("Failed to send ListAdapters");
- }
+fail:
+ pa__done(m);
- dbus_message_unref(m);
+ return -1;
}
void pa__done(pa_module* m) {
struct userdata *u;
- struct device *i;
- struct dbus_pending *p;
pa_assert(m);
if (!(u = m->userdata))
return;
- while ((p = u->dbus_pending_list)) {
- PA_LLIST_REMOVE(struct dbus_pending, u->dbus_pending_list, p);
- dbus_pending_free(p);
- }
-
- while ((i = u->device_list)) {
- PA_LLIST_REMOVE(struct device, u->device_list, i);
- device_free(i);
- }
-
- if (u->dbus_data_slot != -1) {
- dbus_pending_call_free_data_slot(&u->dbus_data_slot);
- }
-
- if (u->conn) {
- DBusError error;
- dbus_error_init(&error);
-
- dbus_bus_remove_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", &error);
- dbus_error_free(&error);
+ if (u->discovery)
+ pa_bluetooth_discovery_free(u->discovery);
- dbus_bus_remove_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'", &error);
- dbus_error_free(&error);
-
- dbus_bus_remove_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", &error);
- dbus_error_free(&error);
-
- dbus_connection_remove_filter(pa_dbus_connection_get(u->conn), filter_cb, u);
-
- pa_dbus_connection_unref(u->conn);
- }
+ if (u->connection)
+ pa_dbus_connection_unref(u->connection);
pa_xfree(u);
}
-
-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->dbus_data_slot = -1;
- u->module = m;
- PA_LLIST_HEAD_INIT(struct device, u->device_list);
- PA_LLIST_HEAD_INIT(DBusPendingCall, u->dbus_pending_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;
- }
-
- if (!dbus_pending_call_allocate_data_slot(&u->dbus_data_slot))
- 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;
- }
-
- 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;
- }
-
- lookup_devices(u);
-
- return 0;
-
-fail:
- dbus_error_free(&err);
- pa__done(m);
-
- return -1;
-}