2 This file is part of PulseAudio.
4 Copyright 2008 Joao Paulo Rechi Vita
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
31 #include <pulsecore/module.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/modargs.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/llist.h>
36 #include <pulsecore/core-util.h>
37 #include <modules/dbus-util.h>
39 #include "module-bluetooth-discover-symdef.h"
40 #include "bluetooth-util.h"
42 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
43 PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
44 PA_MODULE_VERSION(PACKAGE_VERSION);
45 PA_MODULE_USAGE("sco_sink=<name of sink> "
46 "sco_source=<name of source>"
47 "async=<Asynchronous initialization?>");
49 static const char* const valid_modargs[] = {
60 pa_dbus_connection *connection;
61 pa_bluetooth_discovery *discovery;
64 static void load_module_for_device(struct userdata *u, pa_bluetooth_device *d, pa_bool_t good) {
69 d->device_connected > 0 &&
70 (d->audio_sink_connected > 0 || d->headset_connected > 0)) {
72 if (((uint32_t) PA_PTR_TO_UINT(d->data))-1 == PA_INVALID_INDEX) {
76 /* Oh, awesome, a new device has shown up and been connected! */
78 args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
80 if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
81 pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
84 tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
85 pa_modargs_get_value(u->modargs, "sco_sink", NULL),
86 pa_modargs_get_value(u->modargs, "sco_source", NULL));
91 pa_log_debug("Loading module-bluetooth-device %s", args);
92 m = pa_module_load(u->module->core, "module-bluetooth-device", args);
96 d->data = PA_UINT_TO_PTR((uint32_t) (m->index+1));
98 pa_log_debug("Failed to load module for device %s", d->path);
103 if (((uint32_t) PA_PTR_TO_UINT(d->data))-1 != PA_INVALID_INDEX) {
105 /* Hmm, disconnection? Then let's unload our module */
107 pa_log_debug("Unloading module for %s", d->path);
108 pa_module_unload_request_by_index(u->core, (uint32_t) (PA_PTR_TO_UINT(d->data))-1, TRUE);
114 static int setup_dbus(struct userdata *u) {
117 dbus_error_init(&err);
119 u->connection = pa_dbus_bus_get(u->core, DBUS_BUS_SYSTEM, &err);
121 if (dbus_error_is_set(&err) || !u->connection) {
122 pa_log("Failed to get D-Bus connection: %s", err.message);
123 dbus_error_free(&err);
130 int pa__init(pa_module* m) {
132 pa_modargs *ma = NULL;
133 pa_bool_t async = FALSE;
137 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
138 pa_log("Failed to parse module arguments");
142 if (pa_modargs_get_value_boolean(ma, "async", &async) < 0) {
143 pa_log("Failed to parse async argument.");
147 m->userdata = u = pa_xnew0(struct userdata, 1);
153 if (setup_dbus(u) < 0)
156 if (!(u->discovery = pa_bluetooth_discovery_new(pa_dbus_connection_get(u->connection), load_module_for_device, u)))
160 pa_bluetooth_discovery_sync(u->discovery);
173 void pa__done(pa_module* m) {
178 if (!(u = m->userdata))
182 pa_bluetooth_discovery_free(u->discovery);
185 pa_dbus_connection_unref(u->connection);
188 pa_modargs_free(u->modargs);