2 This file is part of PulseAudio.
4 Copyright 2008-2009 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
26 #include <pulsecore/core-util.h>
27 #include <pulsecore/shared.h>
28 #include <pulsecore/dbus-shared.h>
30 #include "bluetooth-util.h"
32 struct pa_bluetooth_discovery
{
36 pa_dbus_connection
*connection
;
37 PA_LLIST_HEAD(pa_dbus_pending
, pending
);
40 pa_bool_t filter_added
;
43 static void get_properties_reply(DBusPendingCall
*pending
, void *userdata
);
44 static pa_dbus_pending
* send_and_add_to_pending(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessage
*m
, DBusPendingCallNotifyFunction func
);
46 static pa_bt_audio_state_t
pa_bt_audio_state_from_string(const char* value
) {
49 if (pa_streq(value
, "disconnected"))
50 return PA_BT_AUDIO_STATE_DISCONNECTED
;
51 else if (pa_streq(value
, "connecting"))
52 return PA_BT_AUDIO_STATE_CONNECTING
;
53 else if (pa_streq(value
, "connected"))
54 return PA_BT_AUDIO_STATE_CONNECTED
;
55 else if (pa_streq(value
, "playing"))
56 return PA_BT_AUDIO_STATE_PLAYING
;
58 return PA_BT_AUDIO_STATE_INVALID
;
61 static pa_bluetooth_uuid
*uuid_new(const char *uuid
) {
64 u
= pa_xnew(pa_bluetooth_uuid
, 1);
65 u
->uuid
= pa_xstrdup(uuid
);
66 PA_LLIST_INIT(pa_bluetooth_uuid
, u
);
71 static void uuid_free(pa_bluetooth_uuid
*u
) {
78 static pa_bluetooth_device
* device_new(const char *path
) {
79 pa_bluetooth_device
*d
;
81 d
= pa_xnew(pa_bluetooth_device
, 1);
85 d
->device_info_valid
= 0;
88 d
->path
= pa_xstrdup(path
);
91 d
->device_connected
= -1;
92 PA_LLIST_HEAD_INIT(pa_bluetooth_uuid
, d
->uuids
);
97 d
->audio_state
= PA_BT_AUDIO_STATE_INVALID
;
98 d
->audio_sink_state
= PA_BT_AUDIO_STATE_INVALID
;
99 d
->audio_source_state
= PA_BT_AUDIO_STATE_INVALID
;
100 d
->headset_state
= PA_BT_AUDIO_STATE_INVALID
;
105 static void device_free(pa_bluetooth_device
*d
) {
106 pa_bluetooth_uuid
*u
;
110 while ((u
= d
->uuids
)) {
111 PA_LLIST_REMOVE(pa_bluetooth_uuid
, d
->uuids
, u
);
118 pa_xfree(d
->address
);
122 static pa_bool_t
device_is_audio(pa_bluetooth_device
*d
) {
126 d
->device_info_valid
&&
127 (d
->audio_state
!= PA_BT_AUDIO_STATE_INVALID
&&
128 (d
->audio_sink_state
!= PA_BT_AUDIO_STATE_INVALID
||
129 d
->audio_source_state
!= PA_BT_AUDIO_STATE_INVALID
||
130 d
->headset_state
!= PA_BT_AUDIO_STATE_INVALID
));
133 static int parse_device_property(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessageIter
*i
) {
135 DBusMessageIter variant_i
;
141 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
142 pa_log("Property name not a string.");
146 dbus_message_iter_get_basic(i
, &key
);
148 if (!dbus_message_iter_next(i
)) {
149 pa_log("Property value missing");
153 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
154 pa_log("Property value not a variant.");
158 dbus_message_iter_recurse(i
, &variant_i
);
160 /* pa_log_debug("Parsing property org.bluez.Device.%s", key); */
162 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
164 case DBUS_TYPE_STRING
: {
167 dbus_message_iter_get_basic(&variant_i
, &value
);
169 if (pa_streq(key
, "Name")) {
171 d
->name
= pa_xstrdup(value
);
172 } else if (pa_streq(key
, "Alias")) {
174 d
->alias
= pa_xstrdup(value
);
175 } else if (pa_streq(key
, "Address")) {
176 pa_xfree(d
->address
);
177 d
->address
= pa_xstrdup(value
);
180 /* pa_log_debug("Value %s", value); */
185 case DBUS_TYPE_BOOLEAN
: {
188 dbus_message_iter_get_basic(&variant_i
, &value
);
190 if (pa_streq(key
, "Paired"))
192 else if (pa_streq(key
, "Connected"))
193 d
->device_connected
= !!value
;
194 else if (pa_streq(key
, "Trusted"))
195 d
->trusted
= !!value
;
197 /* pa_log_debug("Value %s", pa_yes_no(value)); */
202 case DBUS_TYPE_UINT32
: {
205 dbus_message_iter_get_basic(&variant_i
, &value
);
207 if (pa_streq(key
, "Class"))
208 d
->class = (int) value
;
210 /* pa_log_debug("Value %u", (unsigned) value); */
215 case DBUS_TYPE_ARRAY
: {
218 dbus_message_iter_recurse(&variant_i
, &ai
);
220 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_STRING
&&
221 pa_streq(key
, "UUIDs")) {
223 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
224 pa_bluetooth_uuid
*node
;
228 dbus_message_iter_get_basic(&ai
, &value
);
229 node
= uuid_new(value
);
230 PA_LLIST_PREPEND(pa_bluetooth_uuid
, d
->uuids
, node
);
232 /* Vudentz said the interfaces are here when the UUIDs are announced */
233 if (strcasecmp(HSP_HS_UUID
, value
) == 0 || strcasecmp(HFP_HS_UUID
, value
) == 0) {
234 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.Headset", "GetProperties"));
235 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
236 } else if (strcasecmp(A2DP_SINK_UUID
, value
) == 0) {
237 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.AudioSink", "GetProperties"));
238 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
239 } else if (strcasecmp(A2DP_SOURCE_UUID
, value
) == 0) {
240 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.AudioSource", "GetProperties"));
241 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
244 /* this might eventually be racy if .Audio is not there yet, but the State change will come anyway later, so this call is for cold-detection mostly */
245 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.Audio", "GetProperties"));
246 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
248 if (!dbus_message_iter_next(&ai
))
260 static int parse_audio_property(pa_bluetooth_discovery
*u
, int *state
, DBusMessageIter
*i
) {
262 DBusMessageIter variant_i
;
268 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
269 pa_log("Property name not a string.");
273 dbus_message_iter_get_basic(i
, &key
);
275 if (!dbus_message_iter_next(i
)) {
276 pa_log("Property value missing");
280 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
281 pa_log("Property value not a variant.");
285 dbus_message_iter_recurse(i
, &variant_i
);
287 /* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|AudioSource|Headset}.%s", key); */
289 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
291 case DBUS_TYPE_STRING
: {
294 dbus_message_iter_get_basic(&variant_i
, &value
);
296 if (pa_streq(key
, "State"))
297 *state
= pa_bt_audio_state_from_string(value
);
298 /* pa_log_debug("Value %s", value); */
307 static void run_callback(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, pa_bool_t dead
) {
311 if (!device_is_audio(d
))
315 pa_hook_fire(&y
->hook
, d
);
318 static void remove_all_devices(pa_bluetooth_discovery
*y
) {
319 pa_bluetooth_device
*d
;
323 while ((d
= pa_hashmap_steal_first(y
->devices
))) {
324 run_callback(y
, d
, TRUE
);
329 static void get_properties_reply(DBusPendingCall
*pending
, void *userdata
) {
331 DBusMessageIter arg_i
, element_i
;
333 pa_bluetooth_device
*d
;
334 pa_bluetooth_discovery
*y
;
337 pa_assert_se(p
= userdata
);
338 pa_assert_se(y
= p
->context_data
);
339 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
341 /* pa_log_debug("Got %s.GetProperties response for %s", */
342 /* dbus_message_get_interface(p->message), */
343 /* dbus_message_get_path(p->message)); */
345 /* We don't use p->call_data here right-away since the device
346 * might already be invalidated at this point */
348 if (!(d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(p
->message
))))
351 pa_assert(p
->call_data
== d
);
353 valid
= dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
? -1 : 1;
355 if (dbus_message_is_method_call(p
->message
, "org.bluez.Device", "GetProperties"))
356 d
->device_info_valid
= valid
;
358 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
359 pa_log_debug("Bluetooth daemon is apparently not available.");
360 remove_all_devices(y
);
364 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
366 if (!dbus_message_is_error(r
, DBUS_ERROR_UNKNOWN_METHOD
))
367 pa_log("Error from GetProperties reply: %s", dbus_message_get_error_name(r
));
372 if (!dbus_message_iter_init(r
, &arg_i
)) {
373 pa_log("GetProperties reply has no arguments.");
377 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
378 pa_log("GetProperties argument is not an array.");
382 dbus_message_iter_recurse(&arg_i
, &element_i
);
383 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
385 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
386 DBusMessageIter dict_i
;
388 dbus_message_iter_recurse(&element_i
, &dict_i
);
390 if (dbus_message_has_interface(p
->message
, "org.bluez.Device")) {
391 if (parse_device_property(y
, d
, &dict_i
) < 0)
394 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Audio")) {
395 if (parse_audio_property(y
, &d
->audio_state
, &dict_i
) < 0)
398 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Headset")) {
399 if (parse_audio_property(y
, &d
->headset_state
, &dict_i
) < 0)
402 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSink")) {
403 if (parse_audio_property(y
, &d
->audio_sink_state
, &dict_i
) < 0)
405 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSource")) {
406 if (parse_audio_property(y
, &d
->audio_source_state
, &dict_i
) < 0)
411 if (!dbus_message_iter_next(&element_i
))
416 run_callback(y
, d
, FALSE
);
419 dbus_message_unref(r
);
421 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
422 pa_dbus_pending_free(p
);
425 static pa_dbus_pending
* send_and_add_to_pending(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessage
*m
, DBusPendingCallNotifyFunction func
) {
427 DBusPendingCall
*call
;
432 pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y
->connection
), m
, &call
, -1));
434 p
= pa_dbus_pending_new(pa_dbus_connection_get(y
->connection
), m
, call
, y
, d
);
435 PA_LLIST_PREPEND(pa_dbus_pending
, y
->pending
, p
);
436 dbus_pending_call_set_notify(call
, func
, p
, NULL
);
441 static void found_device(pa_bluetooth_discovery
*y
, const char* path
) {
443 pa_bluetooth_device
*d
;
448 if (pa_hashmap_get(y
->devices
, path
))
451 d
= device_new(path
);
453 pa_hashmap_put(y
->devices
, d
->path
, d
);
455 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Device", "GetProperties"));
456 send_and_add_to_pending(y
, d
, m
, get_properties_reply
);
458 /* Before we read the other properties (Audio, AudioSink, AudioSource,
459 * Headset) we wait that the UUID is read */
462 static void list_devices_reply(DBusPendingCall
*pending
, void *userdata
) {
468 pa_bluetooth_discovery
*y
;
474 pa_assert_se(p
= userdata
);
475 pa_assert_se(y
= p
->context_data
);
476 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
478 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
479 pa_log_debug("Bluetooth daemon is apparently not available.");
480 remove_all_devices(y
);
484 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
485 pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r
));
489 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_OBJECT_PATH
, &paths
, &num
, DBUS_TYPE_INVALID
)) {
490 pa_log("org.bluez.Adapter.ListDevices returned an error: '%s'\n", e
.message
);
495 for (i
= 0; i
< num
; ++i
)
496 found_device(y
, paths
[i
]);
501 dbus_free_string_array (paths
);
503 dbus_message_unref(r
);
505 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
506 pa_dbus_pending_free(p
);
509 static void found_adapter(pa_bluetooth_discovery
*y
, const char *path
) {
512 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Adapter", "ListDevices"));
513 send_and_add_to_pending(y
, NULL
, m
, list_devices_reply
);
516 static void list_adapters_reply(DBusPendingCall
*pending
, void *userdata
) {
522 pa_bluetooth_discovery
*y
;
528 pa_assert_se(p
= userdata
);
529 pa_assert_se(y
= p
->context_data
);
530 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
532 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
533 pa_log_debug("Bluetooth daemon is apparently not available.");
534 remove_all_devices(y
);
538 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
539 pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r
));
543 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_OBJECT_PATH
, &paths
, &num
, DBUS_TYPE_INVALID
)) {
544 pa_log("org.bluez.Manager.ListAdapters returned an error: %s", e
.message
);
549 for (i
= 0; i
< num
; ++i
)
550 found_adapter(y
, paths
[i
]);
555 dbus_free_string_array (paths
);
557 dbus_message_unref(r
);
559 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
560 pa_dbus_pending_free(p
);
563 static void list_adapters(pa_bluetooth_discovery
*y
) {
567 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
568 send_and_add_to_pending(y
, NULL
, m
, list_adapters_reply
);
571 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*m
, void *userdata
) {
573 pa_bluetooth_discovery
*y
;
578 pa_assert_se(y
= userdata
);
580 dbus_error_init(&err
);
582 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
583 dbus_message_get_interface(m
),
584 dbus_message_get_path(m
),
585 dbus_message_get_member(m
));
587 if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceRemoved")) {
589 pa_bluetooth_device
*d
;
591 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
592 pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err
.message
);
596 pa_log_debug("Device %s removed", path
);
598 if ((d
= pa_hashmap_remove(y
->devices
, path
))) {
599 run_callback(y
, d
, TRUE
);
603 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
605 } else if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceCreated")) {
608 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
609 pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err
.message
);
613 pa_log_debug("Device %s created", path
);
615 found_device(y
, path
);
616 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
618 } else if (dbus_message_is_signal(m
, "org.bluez.Manager", "AdapterAdded")) {
621 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
622 pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err
.message
);
626 pa_log_debug("Adapter %s created", path
);
628 found_adapter(y
, path
);
629 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
631 } else if (dbus_message_is_signal(m
, "org.bluez.Audio", "PropertyChanged") ||
632 dbus_message_is_signal(m
, "org.bluez.Headset", "PropertyChanged") ||
633 dbus_message_is_signal(m
, "org.bluez.AudioSink", "PropertyChanged") ||
634 dbus_message_is_signal(m
, "org.bluez.AudioSource", "PropertyChanged") ||
635 dbus_message_is_signal(m
, "org.bluez.Device", "PropertyChanged")) {
637 pa_bluetooth_device
*d
;
639 if ((d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(m
)))) {
640 DBusMessageIter arg_i
;
642 if (!dbus_message_iter_init(m
, &arg_i
)) {
643 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
647 if (dbus_message_has_interface(m
, "org.bluez.Device")) {
648 if (parse_device_property(y
, d
, &arg_i
) < 0)
651 } else if (dbus_message_has_interface(m
, "org.bluez.Audio")) {
652 if (parse_audio_property(y
, &d
->audio_state
, &arg_i
) < 0)
655 } else if (dbus_message_has_interface(m
, "org.bluez.Headset")) {
656 if (parse_audio_property(y
, &d
->headset_state
, &arg_i
) < 0)
659 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSink")) {
660 if (parse_audio_property(y
, &d
->audio_sink_state
, &arg_i
) < 0)
662 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSource")) {
663 if (parse_audio_property(y
, &d
->audio_source_state
, &arg_i
) < 0)
667 run_callback(y
, d
, FALSE
);
670 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
672 } else if (dbus_message_is_signal(m
, "org.bluez.Device", "DisconnectRequested")) {
673 pa_bluetooth_device
*d
;
675 if ((d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(m
)))) {
676 /* Device will disconnect in 2 sec */
677 d
->audio_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
678 d
->audio_sink_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
679 d
->audio_source_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
680 d
->headset_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
682 run_callback(y
, d
, FALSE
);
685 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
687 } else if (dbus_message_is_signal(m
, "org.freedesktop.DBus", "NameOwnerChanged")) {
688 const char *name
, *old_owner
, *new_owner
;
690 if (!dbus_message_get_args(m
, &err
,
691 DBUS_TYPE_STRING
, &name
,
692 DBUS_TYPE_STRING
, &old_owner
,
693 DBUS_TYPE_STRING
, &new_owner
,
694 DBUS_TYPE_INVALID
)) {
695 pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err
.message
);
699 if (pa_streq(name
, "org.bluez")) {
700 if (old_owner
&& *old_owner
) {
701 pa_log_debug("Bluetooth daemon disappeared.");
702 remove_all_devices(y
);
705 if (new_owner
&& *new_owner
) {
706 pa_log_debug("Bluetooth daemon appeared.");
711 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
715 dbus_error_free(&err
);
717 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
720 const pa_bluetooth_device
* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery
*y
, const char* address
) {
721 pa_bluetooth_device
*d
;
725 pa_assert(PA_REFCNT_VALUE(y
) > 0);
728 if (!pa_hook_is_firing(&y
->hook
))
729 pa_bluetooth_discovery_sync(y
);
731 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
732 if (pa_streq(d
->address
, address
))
733 return device_is_audio(d
) ? d
: NULL
;
738 const pa_bluetooth_device
* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery
*y
, const char* path
) {
739 pa_bluetooth_device
*d
;
742 pa_assert(PA_REFCNT_VALUE(y
) > 0);
745 if (!pa_hook_is_firing(&y
->hook
))
746 pa_bluetooth_discovery_sync(y
);
748 if ((d
= pa_hashmap_get(y
->devices
, path
)))
749 if (device_is_audio(d
))
755 static int setup_dbus(pa_bluetooth_discovery
*y
) {
758 dbus_error_init(&err
);
760 y
->connection
= pa_dbus_bus_get(y
->core
, DBUS_BUS_SYSTEM
, &err
);
762 if (dbus_error_is_set(&err
) || !y
->connection
) {
763 pa_log("Failed to get D-Bus connection: %s", err
.message
);
764 dbus_error_free(&err
);
771 pa_bluetooth_discovery
* pa_bluetooth_discovery_get(pa_core
*c
) {
773 pa_bluetooth_discovery
*y
;
777 dbus_error_init(&err
);
779 if ((y
= pa_shared_get(c
, "bluetooth-discovery")))
780 return pa_bluetooth_discovery_ref(y
);
782 y
= pa_xnew0(pa_bluetooth_discovery
, 1);
785 y
->devices
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
786 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
->pending
);
787 pa_hook_init(&y
->hook
, y
);
788 pa_shared_set(c
, "bluetooth-discovery", y
);
790 if (setup_dbus(y
) < 0)
793 /* dynamic detection of bluetooth audio devices */
794 if (!dbus_connection_add_filter(pa_dbus_connection_get(y
->connection
), filter_cb
, y
, NULL
)) {
795 pa_log_error("Failed to add filter function");
798 y
->filter_added
= TRUE
;
800 if (pa_dbus_add_matches(
801 pa_dbus_connection_get(y
->connection
), &err
,
802 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
803 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
804 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
805 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
806 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
807 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'",
808 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
809 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
810 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
811 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'", NULL
) < 0) {
812 pa_log("Failed to add D-Bus matches: %s", err
.message
);
823 pa_bluetooth_discovery_unref(y
);
825 dbus_error_free(&err
);
830 pa_bluetooth_discovery
* pa_bluetooth_discovery_ref(pa_bluetooth_discovery
*y
) {
832 pa_assert(PA_REFCNT_VALUE(y
) > 0);
839 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery
*y
) {
841 pa_assert(PA_REFCNT_VALUE(y
) > 0);
843 if (PA_REFCNT_DEC(y
) > 0)
846 pa_dbus_free_pending_list(&y
->pending
);
849 remove_all_devices(y
);
850 pa_hashmap_free(y
->devices
, NULL
, NULL
);
854 pa_dbus_remove_matches(pa_dbus_connection_get(y
->connection
),
855 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
856 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
857 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
858 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
859 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
860 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
861 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'",
862 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
863 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
864 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
865 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'", NULL
);
868 dbus_connection_remove_filter(pa_dbus_connection_get(y
->connection
), filter_cb
, y
);
870 pa_dbus_connection_unref(y
->connection
);
873 pa_hook_done(&y
->hook
);
876 pa_shared_remove(y
->core
, "bluetooth-discovery");
881 void pa_bluetooth_discovery_sync(pa_bluetooth_discovery
*y
) {
883 pa_assert(PA_REFCNT_VALUE(y
) > 0);
885 pa_dbus_sync_pending_list(&y
->pending
);
888 pa_hook
* pa_bluetooth_discovery_hook(pa_bluetooth_discovery
*y
) {
890 pa_assert(PA_REFCNT_VALUE(y
) > 0);
895 const char*pa_bluetooth_get_form_factor(uint32_t class) {
899 static const char * const table
[] = {
910 if (((class >> 8) & 31) != 4)
913 if ((i
= (class >> 2) & 63) > PA_ELEMENTSOF(table
))
919 pa_log_debug("Unknown Bluetooth minor device class %u", i
);
924 char *pa_bluetooth_cleanup_name(const char *name
) {
926 pa_bool_t space
= FALSE
;
930 while ((*name
>= 1 && *name
<= 32) || *name
>= 127)
933 t
= pa_xstrdup(name
);
935 for (s
= d
= t
; *s
; s
++) {
937 if (*s
<= 32 || *s
>= 127 || *s
== '_') {
955 pa_bool_t
pa_bluetooth_uuid_has(pa_bluetooth_uuid
*uuids
, const char *uuid
) {
959 if (strcasecmp(uuids
->uuid
, uuid
) == 0)