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 published
8 by the Free Software Foundation; either version 2 of the License,
9 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 License
17 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/modargs.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/llist.h>
35 #include <pulsecore/core-util.h>
37 #include "dbus-util.h"
38 #include "module-bluetooth-discover-symdef.h"
40 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
41 PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
42 PA_MODULE_VERSION(PACKAGE_VERSION
);
48 PA_LLIST_FIELDS(struct module
);
53 PA_LLIST_FIELDS(struct uuid
);
62 PA_LLIST_HEAD(struct uuid
, uuid_list
);
66 PA_LLIST_HEAD(struct module
, module_list
);
67 PA_LLIST_FIELDS(struct device
);
72 pa_dbus_connection
*conn
;
73 PA_LLIST_HEAD(struct device
, device_list
);
76 static struct module
*module_new(const char *profile
, pa_module
*pa_m
) {
79 m
= pa_xnew(struct module
, 1);
80 m
->profile
= pa_xstrdup(profile
);
82 PA_LLIST_INIT(struct module
, m
);
87 static void module_free(struct module
*m
) {
94 static struct uuid
*uuid_new(const char *uuid
) {
97 node
= pa_xnew(struct uuid
, 1);
98 node
->uuid
= pa_xstrdup(uuid
);
99 PA_LLIST_INIT(struct uuid
, node
);
104 static void uuid_free(struct uuid
*uuid
) {
107 pa_xfree(uuid
->uuid
);
111 static struct device
*device_new(const char *object_path
) {
114 node
= pa_xnew(struct device
, 1);
116 node
->object_path
= pa_xstrdup(object_path
);
119 node
->connected
= -1;
120 PA_LLIST_HEAD_INIT(struct uuid
, node
->uuid_list
);
121 node
->address
= NULL
;
124 PA_LLIST_HEAD_INIT(struct module
, node
->module_list
);
125 PA_LLIST_INIT(struct device
, node
);
130 static void device_free(struct device
*device
) {
136 while ((m
= device
->module_list
)) {
137 PA_LLIST_REMOVE(struct module
, device
->module_list
, m
);
141 while ((i
= device
->uuid_list
)) {
142 PA_LLIST_REMOVE(struct uuid
, device
->uuid_list
, i
);
146 pa_xfree(device
->name
);
147 pa_xfree(device
->object_path
);
148 pa_xfree(device
->alias
);
149 pa_xfree(device
->address
);
153 static struct device
* device_find(struct userdata
*u
, const char *path
) {
156 for (i
= u
->device_list
; i
; i
= i
->next
)
157 if (pa_streq(i
->object_path
, path
))
163 static int parse_device_property(struct userdata
*u
, struct device
*d
, DBusMessageIter
*i
) {
165 DBusMessageIter variant_i
;
171 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
172 pa_log("Property name not a string.");
176 dbus_message_iter_get_basic(i
, &key
);
178 if (!dbus_message_iter_next(i
)) {
179 pa_log("Property value missing");
183 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
184 pa_log("Property value not a variant.");
188 dbus_message_iter_recurse(i
, &variant_i
);
190 pa_log_debug("Parsing device property %s", key
);
192 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
194 case DBUS_TYPE_STRING
: {
197 dbus_message_iter_get_basic(&variant_i
, &value
);
199 if (pa_streq(key
, "Name")) {
201 d
->name
= pa_xstrdup(value
);
202 } else if (pa_streq(key
, "Alias")) {
204 d
->alias
= pa_xstrdup(value
);
205 } else if (pa_streq(key
, "Address")) {
206 pa_xfree(d
->address
);
207 d
->address
= pa_xstrdup(value
);
213 case DBUS_TYPE_BOOLEAN
: {
216 dbus_message_iter_get_basic(&variant_i
, &value
);
218 if (pa_streq(key
, "Paired"))
220 else if (pa_streq(key
, "Connected"))
221 d
->connected
= !!value
;
222 else if (pa_streq(key
, "Trusted"))
223 d
->trusted
= !!value
;
228 case DBUS_TYPE_UINT32
: {
231 dbus_message_iter_get_basic(&variant_i
, &value
);
233 if (pa_streq(key
, "Class"))
234 d
->class = (int) value
;
239 case DBUS_TYPE_ARRAY
: {
242 dbus_message_iter_recurse(&variant_i
, &ai
);
244 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_STRING
&&
245 pa_streq(key
, "UUIDs")) {
247 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
251 dbus_message_iter_get_basic(&ai
, &value
);
252 node
= uuid_new(value
);
253 PA_LLIST_PREPEND(struct uuid
, d
->uuid_list
, node
);
255 if (!dbus_message_iter_next(&ai
))
267 static int get_device_properties(struct userdata
*u
, struct device
*d
) {
269 DBusMessage
*m
= NULL
, *r
= NULL
;
270 DBusMessageIter arg_i
, element_i
;
278 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->object_path
, "org.bluez.Device", "GetProperties"));
280 r
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u
->conn
), m
, -1, &e
);
283 pa_log("org.bluez.Device.GetProperties failed: %s", e
.message
);
287 if (!dbus_message_iter_init(r
, &arg_i
)) {
288 pa_log("org.bluez.Device.GetProperties reply has no arguments");
292 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
293 pa_log("org.bluez.Device.GetProperties argument is not an array");
297 dbus_message_iter_recurse(&arg_i
, &element_i
);
298 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
300 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
301 DBusMessageIter dict_i
;
303 dbus_message_iter_recurse(&element_i
, &dict_i
);
305 if (parse_device_property(u
, d
, &dict_i
) < 0)
309 if (!dbus_message_iter_next(&element_i
))
317 dbus_message_unref(m
);
319 dbus_message_unref(r
);
326 static void load_module_for_device(struct userdata
*u
, struct device
*d
, const char *profile
) {
334 get_device_properties(u
, d
);
335 args
= pa_sprintf_malloc("sink_name=\"%s\" address=\"%s\" profile=\"%s\"", d
->name
, d
->address
, profile
);
336 pa_m
= pa_module_load(u
->module
->core
, "module-bluetooth-device", args
);
340 pa_log_debug("Failed to load module for device %s", d
->object_path
);
344 m
= module_new(profile
, pa_m
);
345 PA_LLIST_PREPEND(struct module
, d
->module_list
, m
);
348 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*msg
, void *userdata
) {
349 DBusMessageIter arg_i
;
359 dbus_error_init(&err
);
361 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
362 dbus_message_get_interface(msg
),
363 dbus_message_get_path(msg
),
364 dbus_message_get_member(msg
));
366 if (dbus_message_is_signal(msg
, "org.bluez.Adapter", "DeviceRemoved")) {
368 if (!dbus_message_iter_init(msg
, &arg_i
))
369 pa_log("dbus: message has no parameters");
370 else if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_OBJECT_PATH
)
371 pa_log("dbus: argument is not object path");
375 dbus_message_iter_get_basic(&arg_i
, &value
);
376 pa_log_debug("hcid: device %s removed", value
);
378 if ((d
= device_find(u
, value
))) {
379 PA_LLIST_REMOVE(struct device
, u
->device_list
, d
);
384 } else if (dbus_message_is_signal(msg
, "org.bluez.Headset", "PropertyChanged") ||
385 dbus_message_is_signal(msg
, "org.bluez.AudioSink", "PropertyChanged")) {
389 DBusMessageIter variant_i
;
391 if (!dbus_message_iter_init(msg
, &arg_i
)) {
392 pa_log("dbus: message has no parameters");
396 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_STRING
) {
397 pa_log("Property name not a string.");
401 dbus_message_iter_get_basic(&arg_i
, &value
);
403 if (!dbus_message_iter_next(&arg_i
)) {
404 pa_log("Property value missing");
408 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_VARIANT
) {
409 pa_log("Property value not a variant.");
413 dbus_message_iter_recurse(&arg_i
, &variant_i
);
415 if (dbus_message_iter_get_arg_type(&variant_i
) == DBUS_TYPE_BOOLEAN
) {
416 dbus_bool_t connected
;
417 dbus_message_iter_get_basic(&variant_i
, &connected
);
419 if (!pa_streq(value
, "Connected") || !connected
)
423 if (!(d
= device_find(u
, dbus_message_get_path(msg
)))) {
424 d
= device_new(dbus_message_get_path(msg
));
425 PA_LLIST_PREPEND(struct device
, u
->device_list
, d
);
428 if (dbus_message_is_signal(msg
, "org.bluez.Headset", "PropertyChanged"))
433 load_module_for_device(u
, d
, profile
);
437 dbus_error_free(&err
);
438 return DBUS_HANDLER_RESULT_HANDLED
;
441 void pa__done(pa_module
* m
) {
447 if (!(u
= m
->userdata
))
450 while ((i
= u
->device_list
)) {
451 PA_LLIST_REMOVE(struct device
, u
->device_list
, i
);
456 pa_dbus_connection_unref(u
->conn
);
461 int pa__init(pa_module
* m
) {
466 dbus_error_init(&err
);
468 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
470 PA_LLIST_HEAD_INIT(struct device
, u
->device_list
);
472 /* connect to the bus */
473 u
->conn
= pa_dbus_bus_get(m
->core
, DBUS_BUS_SYSTEM
, &err
);
474 if (dbus_error_is_set(&err
) || (u
->conn
== NULL
) ) {
475 pa_log("Failed to get D-Bus connection: %s", err
.message
);
479 /* dynamic detection of bluetooth audio devices */
480 if (!dbus_connection_add_filter(pa_dbus_connection_get(u
->conn
), filter_cb
, u
, NULL
)) {
481 pa_log_error("Failed to add filter function");
485 dbus_bus_add_match(pa_dbus_connection_get(u
->conn
), "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", &err
);
486 if (dbus_error_is_set(&err
)) {
487 pa_log_error("Unable to subscribe to org.bluez.Adapter signals: %s: %s", err
.name
, err
.message
);
491 dbus_bus_add_match(pa_dbus_connection_get(u
->conn
), "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'", &err
);
492 if (dbus_error_is_set(&err
)) {
493 pa_log_error("Unable to subscribe to org.bluez.Headset signals: %s: %s", err
.name
, err
.message
);
497 dbus_bus_add_match(pa_dbus_connection_get(u
->conn
), "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", &err
);
498 if (dbus_error_is_set(&err
)) {
499 pa_log_error("Unable to subscribe to org.bluez.AudioSink signals: %s: %s", err
.name
, err
.message
);
506 dbus_error_free(&err
);