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 module
* module_find(struct device
*d
, const char *profile
) {
97 for (m
= d
->module_list
; d
; d
= d
->next
)
98 if (pa_streq(m
->profile
, profile
))
104 static struct uuid
*uuid_new(const char *uuid
) {
107 node
= pa_xnew(struct uuid
, 1);
108 node
->uuid
= pa_xstrdup(uuid
);
109 PA_LLIST_INIT(struct uuid
, node
);
114 static void uuid_free(struct uuid
*uuid
) {
117 pa_xfree(uuid
->uuid
);
121 static struct device
*device_new(const char *object_path
) {
124 node
= pa_xnew(struct device
, 1);
126 node
->object_path
= pa_xstrdup(object_path
);
129 node
->connected
= -1;
130 PA_LLIST_HEAD_INIT(struct uuid
, node
->uuid_list
);
131 node
->address
= NULL
;
134 PA_LLIST_HEAD_INIT(struct module
, node
->module_list
);
135 PA_LLIST_INIT(struct device
, node
);
140 static void device_free(struct device
*device
) {
146 while ((m
= device
->module_list
)) {
147 PA_LLIST_REMOVE(struct module
, device
->module_list
, m
);
151 while ((i
= device
->uuid_list
)) {
152 PA_LLIST_REMOVE(struct uuid
, device
->uuid_list
, i
);
156 pa_xfree(device
->name
);
157 pa_xfree(device
->object_path
);
158 pa_xfree(device
->alias
);
159 pa_xfree(device
->address
);
163 static struct device
* device_find(struct userdata
*u
, const char *path
) {
166 for (i
= u
->device_list
; i
; i
= i
->next
)
167 if (pa_streq(i
->object_path
, path
))
173 static int parse_device_property(struct userdata
*u
, struct device
*d
, DBusMessageIter
*i
) {
175 DBusMessageIter variant_i
;
181 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
182 pa_log("Property name not a string.");
186 dbus_message_iter_get_basic(i
, &key
);
188 if (!dbus_message_iter_next(i
)) {
189 pa_log("Property value missing");
193 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
194 pa_log("Property value not a variant.");
198 dbus_message_iter_recurse(i
, &variant_i
);
200 pa_log_debug("Parsing device property %s", key
);
202 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
204 case DBUS_TYPE_STRING
: {
207 dbus_message_iter_get_basic(&variant_i
, &value
);
209 if (pa_streq(key
, "Name")) {
211 d
->name
= pa_xstrdup(value
);
212 } else if (pa_streq(key
, "Alias")) {
214 d
->alias
= pa_xstrdup(value
);
215 } else if (pa_streq(key
, "Address")) {
216 pa_xfree(d
->address
);
217 d
->address
= pa_xstrdup(value
);
223 case DBUS_TYPE_BOOLEAN
: {
226 dbus_message_iter_get_basic(&variant_i
, &value
);
228 if (pa_streq(key
, "Paired"))
230 else if (pa_streq(key
, "Connected"))
231 d
->connected
= !!value
;
232 else if (pa_streq(key
, "Trusted"))
233 d
->trusted
= !!value
;
238 case DBUS_TYPE_UINT32
: {
241 dbus_message_iter_get_basic(&variant_i
, &value
);
243 if (pa_streq(key
, "Class"))
244 d
->class = (int) value
;
249 case DBUS_TYPE_ARRAY
: {
252 dbus_message_iter_recurse(&variant_i
, &ai
);
254 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_STRING
&&
255 pa_streq(key
, "UUIDs")) {
257 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
261 dbus_message_iter_get_basic(&ai
, &value
);
262 node
= uuid_new(value
);
263 PA_LLIST_PREPEND(struct uuid
, d
->uuid_list
, node
);
265 if (!dbus_message_iter_next(&ai
))
277 static int get_device_properties(struct userdata
*u
, struct device
*d
) {
279 DBusMessage
*m
= NULL
, *r
= NULL
;
280 DBusMessageIter arg_i
, element_i
;
288 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->object_path
, "org.bluez.Device", "GetProperties"));
290 r
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u
->conn
), m
, -1, &e
);
293 pa_log("org.bluez.Device.GetProperties failed: %s", e
.message
);
297 if (!dbus_message_iter_init(r
, &arg_i
)) {
298 pa_log("org.bluez.Device.GetProperties reply has no arguments");
302 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
303 pa_log("org.bluez.Device.GetProperties argument is not an array");
307 dbus_message_iter_recurse(&arg_i
, &element_i
);
308 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
310 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
311 DBusMessageIter dict_i
;
313 dbus_message_iter_recurse(&element_i
, &dict_i
);
315 if (parse_device_property(u
, d
, &dict_i
) < 0)
319 if (!dbus_message_iter_next(&element_i
))
327 dbus_message_unref(m
);
329 dbus_message_unref(r
);
336 static void load_module_for_device(struct userdata
*u
, struct device
*d
, const char *profile
) {
344 get_device_properties(u
, d
);
345 args
= pa_sprintf_malloc("sink_name=\"%s\" address=\"%s\" profile=\"%s\"", d
->name
, d
->address
, profile
);
346 pa_m
= pa_module_load(u
->module
->core
, "module-bluetooth-device", args
);
350 pa_log_debug("Failed to load module for device %s", d
->object_path
);
354 m
= module_new(profile
, pa_m
);
355 PA_LLIST_PREPEND(struct module
, d
->module_list
, m
);
358 static void unload_module_for_device(struct userdata
*u
, struct device
*d
, const char *profile
) {
364 if (!(m
= module_find(d
, profile
)))
367 pa_module_unload_request(m
->pa_m
, TRUE
);
369 PA_LLIST_REMOVE(struct module
, d
->module_list
, m
);
373 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*msg
, void *userdata
) {
374 DBusMessageIter arg_i
;
384 dbus_error_init(&err
);
386 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
387 dbus_message_get_interface(msg
),
388 dbus_message_get_path(msg
),
389 dbus_message_get_member(msg
));
391 if (dbus_message_is_signal(msg
, "org.bluez.Adapter", "DeviceRemoved")) {
393 if (!dbus_message_iter_init(msg
, &arg_i
))
394 pa_log("dbus: message has no parameters");
395 else if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_OBJECT_PATH
)
396 pa_log("dbus: argument is not object path");
400 dbus_message_iter_get_basic(&arg_i
, &value
);
401 pa_log_debug("hcid: device %s removed", value
);
403 if ((d
= device_find(u
, value
))) {
404 PA_LLIST_REMOVE(struct device
, u
->device_list
, d
);
409 } else if (dbus_message_is_signal(msg
, "org.bluez.Headset", "PropertyChanged") ||
410 dbus_message_is_signal(msg
, "org.bluez.AudioSink", "PropertyChanged")) {
414 DBusMessageIter variant_i
;
415 dbus_bool_t connected
;
417 if (!dbus_message_iter_init(msg
, &arg_i
)) {
418 pa_log("dbus: message has no parameters");
422 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_STRING
) {
423 pa_log("Property name not a string.");
427 dbus_message_iter_get_basic(&arg_i
, &value
);
429 if (!pa_streq(value
, "Connected"))
432 if (!dbus_message_iter_next(&arg_i
)) {
433 pa_log("Property value missing");
437 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_VARIANT
) {
438 pa_log("Property value not a variant.");
442 dbus_message_iter_recurse(&arg_i
, &variant_i
);
444 if (dbus_message_iter_get_arg_type(&variant_i
) != DBUS_TYPE_BOOLEAN
) {
445 pa_log("Property value not a boolean.");
449 dbus_message_iter_get_basic(&variant_i
, &connected
);
451 if (dbus_message_is_signal(msg
, "org.bluez.Headset", "PropertyChanged"))
456 d
= device_find(u
, dbus_message_get_path(msg
));
460 d
= device_new(dbus_message_get_path(msg
));
461 PA_LLIST_PREPEND(struct device
, u
->device_list
, d
);
464 load_module_for_device(u
, d
, profile
);
466 unload_module_for_device(u
, d
, profile
);
470 dbus_error_free(&err
);
471 return DBUS_HANDLER_RESULT_HANDLED
;
474 void pa__done(pa_module
* m
) {
480 if (!(u
= m
->userdata
))
483 while ((i
= u
->device_list
)) {
484 PA_LLIST_REMOVE(struct device
, u
->device_list
, i
);
489 pa_dbus_connection_unref(u
->conn
);
494 int pa__init(pa_module
* m
) {
499 dbus_error_init(&err
);
501 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
503 PA_LLIST_HEAD_INIT(struct device
, u
->device_list
);
505 /* connect to the bus */
506 u
->conn
= pa_dbus_bus_get(m
->core
, DBUS_BUS_SYSTEM
, &err
);
507 if (dbus_error_is_set(&err
) || (u
->conn
== NULL
) ) {
508 pa_log("Failed to get D-Bus connection: %s", err
.message
);
512 /* dynamic detection of bluetooth audio devices */
513 if (!dbus_connection_add_filter(pa_dbus_connection_get(u
->conn
), filter_cb
, u
, NULL
)) {
514 pa_log_error("Failed to add filter function");
518 dbus_bus_add_match(pa_dbus_connection_get(u
->conn
), "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", &err
);
519 if (dbus_error_is_set(&err
)) {
520 pa_log_error("Unable to subscribe to org.bluez.Adapter signals: %s: %s", err
.name
, err
.message
);
524 dbus_bus_add_match(pa_dbus_connection_get(u
->conn
), "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'", &err
);
525 if (dbus_error_is_set(&err
)) {
526 pa_log_error("Unable to subscribe to org.bluez.Headset signals: %s: %s", err
.name
, err
.message
);
530 dbus_bus_add_match(pa_dbus_connection_get(u
->conn
), "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", &err
);
531 if (dbus_error_is_set(&err
)) {
532 pa_log_error("Unable to subscribe to org.bluez.AudioSink signals: %s: %s", err
.name
, err
.message
);
539 dbus_error_free(&err
);