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 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
26 #include <pulsecore/core-util.h>
27 #include <modules/dbus-util.h>
29 #include "bluetooth-util.h"
37 struct pa_bluetooth_discovery
{
38 DBusConnection
*connection
;
39 PA_LLIST_HEAD(pa_dbus_pending
, pending
);
43 /* If mode == MODE_FIND look for a specific device by its address.
44 If mode == MODE_GET look for a specific device by its path. */
45 const char *looking_for
;
46 pa_bluetooth_device
*found_device
;
48 /* If looking_for is NULL we do long-time discovery */
50 pa_bluetooth_device_callback_t callback
;
51 struct userdata
*userdata
;
54 static pa_bluetooth_uuid
*uuid_new(const char *uuid
) {
57 u
= pa_xnew(pa_bluetooth_uuid
, 1);
58 u
->uuid
= pa_xstrdup(uuid
);
59 PA_LLIST_INIT(pa_bluetooth_uuid
, u
);
64 static void uuid_free(pa_bluetooth_uuid
*u
) {
71 static pa_bluetooth_device
* device_new(const char *path
) {
72 pa_bluetooth_device
*d
;
74 d
= pa_xnew(pa_bluetooth_device
, 1);
76 d
->device_info_valid
= d
->audio_sink_info_valid
= d
->headset_info_valid
= 0;
81 d
->path
= pa_xstrdup(path
);
84 d
->device_connected
= -1;
85 PA_LLIST_HEAD_INIT(pa_bluetooth_uuid
, d
->uuids
);
90 d
->audio_sink_connected
= -1;
92 d
->headset_connected
= -1;
97 void pa_bluetooth_device_free(pa_bluetooth_device
*d
) {
102 while ((u
= d
->uuids
)) {
103 PA_LLIST_REMOVE(pa_bluetooth_uuid
, d
->uuids
, u
);
110 pa_xfree(d
->address
);
114 static pa_bool_t
device_is_loaded(pa_bluetooth_device
*d
) {
117 return d
->device_info_valid
&& d
->audio_sink_info_valid
&& d
->headset_info_valid
;
120 static pa_bool_t
device_is_audio(pa_bluetooth_device
*d
) {
123 pa_assert(d
->device_info_valid
);
124 pa_assert(d
->audio_sink_info_valid
);
125 pa_assert(d
->headset_info_valid
);
127 return d
->device_info_valid
> 0 &&
128 (d
->audio_sink_info_valid
> 0 || d
->headset_info_valid
> 0);
131 static int parse_device_property(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessageIter
*i
) {
133 DBusMessageIter variant_i
;
139 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
140 pa_log("Property name not a string.");
144 dbus_message_iter_get_basic(i
, &key
);
146 if (!dbus_message_iter_next(i
)) {
147 pa_log("Property value missing");
151 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
152 pa_log("Property value not a variant.");
156 dbus_message_iter_recurse(i
, &variant_i
);
158 pa_log_debug("Parsing property org.bluez.Device.%s", key
);
160 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
162 case DBUS_TYPE_STRING
: {
165 dbus_message_iter_get_basic(&variant_i
, &value
);
167 if (pa_streq(key
, "Name")) {
169 d
->name
= pa_xstrdup(value
);
170 } else if (pa_streq(key
, "Alias")) {
172 d
->alias
= pa_xstrdup(value
);
173 } else if (pa_streq(key
, "Address")) {
174 pa_xfree(d
->address
);
175 d
->address
= pa_xstrdup(value
);
178 pa_log_debug("Value %s", value
);
183 case DBUS_TYPE_BOOLEAN
: {
186 dbus_message_iter_get_basic(&variant_i
, &value
);
188 if (pa_streq(key
, "Paired"))
190 else if (pa_streq(key
, "Connected"))
191 d
->device_connected
= !!value
;
192 else if (pa_streq(key
, "Trusted"))
193 d
->trusted
= !!value
;
195 pa_log_debug("Value %s", pa_yes_no(value
));
200 case DBUS_TYPE_UINT32
: {
203 dbus_message_iter_get_basic(&variant_i
, &value
);
205 if (pa_streq(key
, "Class"))
206 d
->class = (int) value
;
208 pa_log_debug("Value %u", (unsigned) value
);
213 case DBUS_TYPE_ARRAY
: {
216 dbus_message_iter_recurse(&variant_i
, &ai
);
218 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_STRING
&&
219 pa_streq(key
, "UUIDs")) {
221 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
222 pa_bluetooth_uuid
*node
;
225 dbus_message_iter_get_basic(&ai
, &value
);
226 node
= uuid_new(value
);
227 PA_LLIST_PREPEND(pa_bluetooth_uuid
, d
->uuids
, node
);
229 if (!dbus_message_iter_next(&ai
))
241 static int parse_audio_property(pa_bluetooth_discovery
*u
, int *connected
, DBusMessageIter
*i
) {
243 DBusMessageIter variant_i
;
246 pa_assert(connected
);
249 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
250 pa_log("Property name not a string.");
254 dbus_message_iter_get_basic(i
, &key
);
256 if (!dbus_message_iter_next(i
)) {
257 pa_log("Property value missing");
261 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
262 pa_log("Property value not a variant.");
266 dbus_message_iter_recurse(i
, &variant_i
);
268 pa_log_debug("Parsing property org.bluez.{AudioSink|Headset}.%s", key
);
270 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
272 case DBUS_TYPE_BOOLEAN
: {
275 dbus_message_iter_get_basic(&variant_i
, &value
);
277 if (pa_streq(key
, "Connected"))
278 *connected
= !!value
;
280 pa_log_debug("Value %s", pa_yes_no(value
));
290 static void run_callback(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, pa_bool_t good
) {
294 if (y
->mode
!= MODE_DISCOVER
)
297 if (!device_is_loaded(d
))
300 if (!device_is_audio(d
))
303 y
->callback(y
->userdata
, d
, good
);
307 static void get_properties_reply(DBusPendingCall
*pending
, void *userdata
) {
309 DBusMessageIter arg_i
, element_i
;
311 pa_bluetooth_device
*d
;
312 pa_bluetooth_discovery
*y
;
315 pa_assert_se(p
= userdata
);
316 pa_assert_se(y
= p
->context_data
);
317 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
319 /* pa_log_debug("Got %s.GetProperties response for %s", */
320 /* dbus_message_get_interface(p->message), */
321 /* dbus_message_get_path(p->message)); */
325 valid
= dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
? -1 : 1;
327 if (dbus_message_is_method_call(p
->message
, "org.bluez.Device", "GetProperties"))
328 d
->device_info_valid
= valid
;
329 else if (dbus_message_is_method_call(p
->message
, "org.bluez.Headset", "GetProperties"))
330 d
->headset_info_valid
= valid
;
331 else if (dbus_message_is_method_call(p
->message
, "org.bluez.AudioSink", "GetProperties"))
332 d
->audio_sink_info_valid
= valid
;
334 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
336 if (!dbus_message_is_error(r
, DBUS_ERROR_UNKNOWN_METHOD
))
337 pa_log("Error from GetProperties reply: %s", dbus_message_get_error_name(r
));
342 if (!dbus_message_iter_init(r
, &arg_i
)) {
343 pa_log("GetProperties reply has no arguments.");
347 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
348 pa_log("GetProperties argument is not an array.");
352 dbus_message_iter_recurse(&arg_i
, &element_i
);
353 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
355 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
356 DBusMessageIter dict_i
;
358 dbus_message_iter_recurse(&element_i
, &dict_i
);
360 if (dbus_message_has_interface(p
->message
, "org.bluez.Device")) {
361 if (parse_device_property(y
, d
, &dict_i
) < 0)
364 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Headset")) {
365 if (parse_audio_property(y
, &d
->headset_connected
, &dict_i
) < 0)
368 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSink")) {
369 if (parse_audio_property(y
, &d
->audio_sink_connected
, &dict_i
) < 0)
374 if (!dbus_message_iter_next(&element_i
))
379 run_callback(y
, d
, TRUE
);
381 dbus_message_unref(r
);
383 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
384 pa_dbus_pending_free(p
);
387 static pa_dbus_pending
* send_and_add_to_pending(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessage
*m
, DBusPendingCallNotifyFunction func
) {
389 DBusPendingCall
*call
;
394 pa_assert_se(dbus_connection_send_with_reply(y
->connection
, m
, &call
, -1));
396 p
= pa_dbus_pending_new(m
, call
, y
, d
);
397 PA_LLIST_PREPEND(pa_dbus_pending
, y
->pending
, p
);
398 dbus_pending_call_set_notify(call
, func
, p
, NULL
);
403 static void found_device(pa_bluetooth_discovery
*y
, const char* path
) {
405 pa_bluetooth_device
*d
;
410 d
= device_new(path
);
412 if (y
->mode
== MODE_DISCOVER
) {
413 pa_assert(y
->devices
);
414 pa_hashmap_put(y
->devices
, d
->path
, d
);
416 pa_assert(!y
->found_device
);
420 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Device", "GetProperties"));
421 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
423 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Headset", "GetProperties"));
424 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
426 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.AudioSink", "GetProperties"));
427 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
430 static void list_devices_reply(DBusPendingCall
*pending
, void *userdata
) {
436 pa_bluetooth_discovery
*y
;
442 pa_assert_se(p
= userdata
);
443 pa_assert_se(y
= p
->context_data
);
444 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
446 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
447 pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r
));
451 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_OBJECT_PATH
, &paths
, &num
, DBUS_TYPE_INVALID
)) {
452 pa_log("org.bluez.Adapter.ListDevices returned an error: '%s'\n", e
.message
);
457 for (i
= 0; i
< num
; ++i
)
458 found_device(y
, paths
[i
]);
463 dbus_free_string_array (paths
);
465 dbus_message_unref(r
);
467 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
468 pa_dbus_pending_free(p
);
471 static void find_device_reply(DBusPendingCall
*pending
, void *userdata
) {
476 pa_bluetooth_discovery
*y
;
482 pa_assert_se(p
= userdata
);
483 pa_assert_se(y
= p
->context_data
);
484 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
486 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
487 pa_log("Error from FindDevice reply: %s", dbus_message_get_error_name(r
));
491 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
492 pa_log("org.bluez.Adapter.FindDevice returned an error: '%s'\n", e
.message
);
495 found_device(y
, path
);
498 dbus_message_unref(r
);
500 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
501 pa_dbus_pending_free(p
);
504 static void found_adapter(pa_bluetooth_discovery
*y
, const char *path
) {
507 if (y
->mode
== MODE_FIND
) {
508 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Adapter", "FindDevice"));
510 pa_assert_se(dbus_message_append_args(m
,
511 DBUS_TYPE_STRING
, &y
->looking_for
,
514 send_and_add_to_pending(y
, NULL
, m
, find_device_reply
);
517 pa_assert(y
->mode
== MODE_DISCOVER
);
519 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Adapter", "ListDevices"));
520 send_and_add_to_pending(y
, NULL
, m
, list_devices_reply
);
524 static void list_adapters_reply(DBusPendingCall
*pending
, void *userdata
) {
530 pa_bluetooth_discovery
*y
;
536 pa_assert_se(p
= userdata
);
537 pa_assert_se(y
= p
->context_data
);
538 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
540 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
541 pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r
));
545 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_OBJECT_PATH
, &paths
, &num
, DBUS_TYPE_INVALID
)) {
546 pa_log("org.bluez.Manager.ListAdapters returned an error: %s", e
.message
);
551 for (i
= 0; i
< num
; ++i
)
552 found_adapter(y
, paths
[i
]);
557 dbus_free_string_array (paths
);
559 dbus_message_unref(r
);
561 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
562 pa_dbus_pending_free(p
);
565 static void list_adapters(pa_bluetooth_discovery
*y
) {
569 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
570 send_and_add_to_pending(y
, NULL
, m
, list_adapters_reply
);
573 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*m
, void *userdata
) {
575 pa_bluetooth_discovery
*y
;
580 pa_assert_se(y
= userdata
);
582 dbus_error_init(&err
);
584 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
585 dbus_message_get_interface(m
),
586 dbus_message_get_path(m
),
587 dbus_message_get_member(m
));
589 if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceRemoved")) {
591 pa_bluetooth_device
*d
;
593 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
594 pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err
.message
);
598 pa_log_debug("Device %s removed", path
);
600 if ((d
= pa_hashmap_remove(y
->devices
, path
))) {
602 pa_assert_se(y
->mode
== MODE_DISCOVER
);
603 run_callback(y
, d
, FALSE
);
605 pa_bluetooth_device_free(d
);
608 return DBUS_HANDLER_RESULT_HANDLED
;
610 } else if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceCreated")) {
613 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
614 pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err
.message
);
618 pa_log_debug("Device %s created", path
);
620 found_device(y
, path
);
621 return DBUS_HANDLER_RESULT_HANDLED
;
623 } else if (dbus_message_is_signal(m
, "org.bluez.Manager", "AdapterAdded")) {
626 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
627 pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err
.message
);
631 pa_log_debug("Adapter %s created", path
);
633 found_adapter(y
, path
);
634 return DBUS_HANDLER_RESULT_HANDLED
;
636 } else if (dbus_message_is_signal(m
, "org.bluez.Headset", "PropertyChanged") ||
637 dbus_message_is_signal(m
, "org.bluez.AudioSink", "PropertyChanged") ||
638 dbus_message_is_signal(m
, "org.bluez.Device", "PropertyChanged")) {
640 pa_bluetooth_device
*d
;
642 if ((d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(m
)))) {
643 DBusMessageIter arg_i
;
645 if (!dbus_message_iter_init(m
, &arg_i
)) {
646 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
650 if (dbus_message_has_interface(m
, "org.bluez.Device")) {
651 if (parse_device_property(y
, d
, &arg_i
) < 0)
654 } else if (dbus_message_has_interface(m
, "org.bluez.Headset")) {
655 if (parse_audio_property(y
, &d
->headset_connected
, &arg_i
) < 0)
658 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSink")) {
659 if (parse_audio_property(y
, &d
->audio_sink_connected
, &arg_i
) < 0)
663 pa_assert_se(y
->mode
== MODE_DISCOVER
);
664 run_callback(y
, d
, TRUE
);
667 return DBUS_HANDLER_RESULT_HANDLED
;
671 dbus_error_free(&err
);
673 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
676 pa_bluetooth_device
* pa_bluetooth_find_device(DBusConnection
*c
, const char* address
) {
677 pa_bluetooth_discovery y
;
679 memset(&y
, 0, sizeof(y
));
681 y
.looking_for
= address
;
683 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
.pending
);
687 pa_dbus_sync_pending_list(&y
.pending
);
688 pa_assert(!y
.pending
);
690 if (y
.found_device
) {
691 pa_assert(device_is_loaded(y
.found_device
));
693 if (!device_is_audio(y
.found_device
)) {
694 pa_bluetooth_device_free(y
.found_device
);
699 return y
.found_device
;
702 pa_bluetooth_device
* pa_bluetooth_get_device(DBusConnection
*c
, const char* path
) {
703 pa_bluetooth_discovery y
;
705 memset(&y
, 0, sizeof(y
));
708 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
.pending
);
710 found_device(&y
, path
);
712 pa_dbus_sync_pending_list(&y
.pending
);
713 pa_assert(!y
.pending
);
715 if (y
.found_device
) {
716 pa_assert(device_is_loaded(y
.found_device
));
718 if (!device_is_audio(y
.found_device
)) {
719 pa_bluetooth_device_free(y
.found_device
);
724 return y
.found_device
;
727 pa_bluetooth_discovery
* pa_bluetooth_discovery_new(DBusConnection
*c
, pa_bluetooth_device_callback_t cb
, struct userdata
*u
) {
729 pa_bluetooth_discovery
*y
;
734 dbus_error_init(&err
);
736 y
= pa_xnew0(pa_bluetooth_discovery
, 1);
737 y
->mode
= MODE_DISCOVER
;
741 y
->devices
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
742 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
->pending
);
744 /* dynamic detection of bluetooth audio devices */
745 if (!dbus_connection_add_filter(c
, filter_cb
, y
, NULL
)) {
746 pa_log_error("Failed to add filter function");
750 if (pa_dbus_add_matches(
752 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
753 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
754 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
755 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
756 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
757 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL
) < 0) {
758 pa_log("Failed to add D-Bus matches: %s", err
.message
);
767 dbus_error_free(&err
);
771 void pa_bluetooth_discovery_free(pa_bluetooth_discovery
*y
) {
772 pa_bluetooth_device
*d
;
776 pa_dbus_free_pending_list(&y
->pending
);
779 while ((d
= pa_hashmap_steal_first(y
->devices
))) {
780 run_callback(y
, d
, FALSE
);
781 pa_bluetooth_device_free(d
);
784 pa_hashmap_free(y
->devices
, NULL
, NULL
);
788 pa_dbus_remove_matches(y
->connection
,
789 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
790 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
791 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
792 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
793 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
794 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
795 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL
);
797 dbus_connection_remove_filter(y
->connection
, filter_cb
, y
);
801 void pa_bluetooth_discovery_sync(pa_bluetooth_discovery
*y
) {
804 pa_dbus_sync_pending_list(&y
->pending
);