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 <pulse/xmalloc.h>
28 #include <pulsecore/core-util.h>
29 #include <pulsecore/shared.h>
30 #include <pulsecore/dbus-shared.h>
32 #include "bluetooth-util.h"
33 #include "a2dp-codecs.h"
35 #define HFP_AG_ENDPOINT "/MediaEndpoint/HFPAG"
36 #define HFP_HS_ENDPOINT "/MediaEndpoint/HFPHS"
37 #define A2DP_SOURCE_ENDPOINT "/MediaEndpoint/A2DPSource"
38 #define A2DP_SINK_ENDPOINT "/MediaEndpoint/A2DPSink"
40 #define ENDPOINT_INTROSPECT_XML \
41 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
43 " <interface name=\"org.bluez.MediaEndpoint\">" \
44 " <method name=\"SetConfiguration\">" \
45 " <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
46 " <arg name=\"configuration\" direction=\"in\" type=\"ay\"/>" \
48 " <method name=\"SelectConfiguration\">" \
49 " <arg name=\"capabilities\" direction=\"in\" type=\"ay\"/>" \
50 " <arg name=\"configuration\" direction=\"out\" type=\"ay\"/>" \
52 " <method name=\"ClearConfiguration\">" \
54 " <method name=\"Release\">" \
57 " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
58 " <method name=\"Introspect\">" \
59 " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
64 struct pa_bluetooth_discovery
{
68 pa_dbus_connection
*connection
;
69 PA_LLIST_HEAD(pa_dbus_pending
, pending
);
72 pa_bool_t filter_added
;
75 static void get_properties_reply(DBusPendingCall
*pending
, void *userdata
);
76 static pa_dbus_pending
* send_and_add_to_pending(pa_bluetooth_discovery
*y
, DBusMessage
*m
, DBusPendingCallNotifyFunction func
, void *call_data
);
77 static void found_adapter(pa_bluetooth_discovery
*y
, const char *path
);
78 static pa_bluetooth_device
*found_device(pa_bluetooth_discovery
*y
, const char* path
);
80 pa_bt_audio_state_t
pa_bt_audio_state_from_string(const char* value
) {
83 if (pa_streq(value
, "disconnected"))
84 return PA_BT_AUDIO_STATE_DISCONNECTED
;
85 else if (pa_streq(value
, "connecting"))
86 return PA_BT_AUDIO_STATE_CONNECTING
;
87 else if (pa_streq(value
, "connected"))
88 return PA_BT_AUDIO_STATE_CONNECTED
;
89 else if (pa_streq(value
, "playing"))
90 return PA_BT_AUDIO_STATE_PLAYING
;
92 return PA_BT_AUDIO_STATE_INVALID
;
95 static pa_bluetooth_uuid
*uuid_new(const char *uuid
) {
98 u
= pa_xnew(pa_bluetooth_uuid
, 1);
99 u
->uuid
= pa_xstrdup(uuid
);
100 PA_LLIST_INIT(pa_bluetooth_uuid
, u
);
105 static void uuid_free(pa_bluetooth_uuid
*u
) {
112 static pa_bluetooth_device
* device_new(const char *path
) {
113 pa_bluetooth_device
*d
;
115 d
= pa_xnew(pa_bluetooth_device
, 1);
119 d
->device_info_valid
= 0;
122 d
->path
= pa_xstrdup(path
);
123 d
->transports
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
126 d
->device_connected
= -1;
127 PA_LLIST_HEAD_INIT(pa_bluetooth_uuid
, d
->uuids
);
132 d
->audio_state
= PA_BT_AUDIO_STATE_INVALID
;
133 d
->audio_sink_state
= PA_BT_AUDIO_STATE_INVALID
;
134 d
->audio_source_state
= PA_BT_AUDIO_STATE_INVALID
;
135 d
->headset_state
= PA_BT_AUDIO_STATE_INVALID
;
136 d
->hfgw_state
= PA_BT_AUDIO_STATE_INVALID
;
141 static void transport_free(pa_bluetooth_transport
*t
) {
146 for (i
= 0; i
< PA_BLUETOOTH_TRANSPORT_HOOK_MAX
; i
++)
147 pa_hook_done(&t
->hooks
[i
]);
154 static void device_free(pa_bluetooth_device
*d
) {
155 pa_bluetooth_uuid
*u
;
156 pa_bluetooth_transport
*t
;
160 while ((t
= pa_hashmap_steal_first(d
->transports
)))
163 pa_hashmap_free(d
->transports
, NULL
, NULL
);
165 while ((u
= d
->uuids
)) {
166 PA_LLIST_REMOVE(pa_bluetooth_uuid
, d
->uuids
, u
);
173 pa_xfree(d
->address
);
177 static pa_bool_t
device_is_audio(pa_bluetooth_device
*d
) {
181 d
->device_info_valid
&& (d
->hfgw_state
!= PA_BT_AUDIO_STATE_INVALID
||
182 (d
->audio_state
!= PA_BT_AUDIO_STATE_INVALID
&&
183 (d
->audio_sink_state
!= PA_BT_AUDIO_STATE_INVALID
||
184 d
->audio_source_state
!= PA_BT_AUDIO_STATE_INVALID
||
185 d
->headset_state
!= PA_BT_AUDIO_STATE_INVALID
)));
188 static const char *check_variant_property(DBusMessageIter
*i
) {
193 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
194 pa_log("Property name not a string.");
198 dbus_message_iter_get_basic(i
, &key
);
200 if (!dbus_message_iter_next(i
)) {
201 pa_log("Property value missing");
205 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
206 pa_log("Property value not a variant.");
213 static int parse_manager_property(pa_bluetooth_discovery
*y
, DBusMessageIter
*i
) {
215 DBusMessageIter variant_i
;
219 key
= check_variant_property(i
);
223 dbus_message_iter_recurse(i
, &variant_i
);
225 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
227 case DBUS_TYPE_ARRAY
: {
230 dbus_message_iter_recurse(&variant_i
, &ai
);
232 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_OBJECT_PATH
&&
233 pa_streq(key
, "Adapters")) {
235 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
238 dbus_message_iter_get_basic(&ai
, &value
);
240 found_adapter(y
, value
);
242 dbus_message_iter_next(&ai
);
253 static int parse_adapter_property(pa_bluetooth_discovery
*y
, DBusMessageIter
*i
) {
255 DBusMessageIter variant_i
;
259 key
= check_variant_property(i
);
263 dbus_message_iter_recurse(i
, &variant_i
);
265 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
267 case DBUS_TYPE_ARRAY
: {
270 dbus_message_iter_recurse(&variant_i
, &ai
);
272 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_OBJECT_PATH
&&
273 pa_streq(key
, "Devices")) {
275 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
278 dbus_message_iter_get_basic(&ai
, &value
);
280 found_device(y
, value
);
282 dbus_message_iter_next(&ai
);
293 static int parse_device_property(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessageIter
*i
) {
295 DBusMessageIter variant_i
;
300 key
= check_variant_property(i
);
304 dbus_message_iter_recurse(i
, &variant_i
);
306 /* pa_log_debug("Parsing property org.bluez.Device.%s", key); */
308 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
310 case DBUS_TYPE_STRING
: {
313 dbus_message_iter_get_basic(&variant_i
, &value
);
315 if (pa_streq(key
, "Name")) {
317 d
->name
= pa_xstrdup(value
);
318 } else if (pa_streq(key
, "Alias")) {
320 d
->alias
= pa_xstrdup(value
);
321 } else if (pa_streq(key
, "Address")) {
322 pa_xfree(d
->address
);
323 d
->address
= pa_xstrdup(value
);
326 /* pa_log_debug("Value %s", value); */
331 case DBUS_TYPE_BOOLEAN
: {
334 dbus_message_iter_get_basic(&variant_i
, &value
);
336 if (pa_streq(key
, "Paired"))
338 else if (pa_streq(key
, "Connected"))
339 d
->device_connected
= !!value
;
340 else if (pa_streq(key
, "Trusted"))
341 d
->trusted
= !!value
;
343 /* pa_log_debug("Value %s", pa_yes_no(value)); */
348 case DBUS_TYPE_UINT32
: {
351 dbus_message_iter_get_basic(&variant_i
, &value
);
353 if (pa_streq(key
, "Class"))
354 d
->class = (int) value
;
356 /* pa_log_debug("Value %u", (unsigned) value); */
361 case DBUS_TYPE_ARRAY
: {
364 dbus_message_iter_recurse(&variant_i
, &ai
);
366 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_STRING
&&
367 pa_streq(key
, "UUIDs")) {
369 pa_bool_t has_audio
= FALSE
;
371 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
372 pa_bluetooth_uuid
*node
;
375 dbus_message_iter_get_basic(&ai
, &value
);
376 node
= uuid_new(value
);
377 PA_LLIST_PREPEND(pa_bluetooth_uuid
, d
->uuids
, node
);
379 /* Vudentz said the interfaces are here when the UUIDs are announced */
380 if (strcasecmp(HSP_AG_UUID
, value
) == 0 || strcasecmp(HFP_AG_UUID
, value
) == 0) {
381 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.HandsfreeGateway", "GetProperties"));
382 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
384 } else if (strcasecmp(HSP_HS_UUID
, value
) == 0 || strcasecmp(HFP_HS_UUID
, value
) == 0) {
385 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.Headset", "GetProperties"));
386 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
388 } else if (strcasecmp(A2DP_SINK_UUID
, value
) == 0) {
389 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.AudioSink", "GetProperties"));
390 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
392 } else if (strcasecmp(A2DP_SOURCE_UUID
, value
) == 0) {
393 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.AudioSource", "GetProperties"));
394 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
398 dbus_message_iter_next(&ai
);
401 /* 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 */
403 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.Audio", "GetProperties"));
404 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
415 static int parse_audio_property(pa_bluetooth_discovery
*u
, int *state
, DBusMessageIter
*i
) {
417 DBusMessageIter variant_i
;
422 key
= check_variant_property(i
);
426 dbus_message_iter_recurse(i
, &variant_i
);
428 /* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|AudioSource|Headset}.%s", key); */
430 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
432 case DBUS_TYPE_STRING
: {
435 dbus_message_iter_get_basic(&variant_i
, &value
);
437 if (pa_streq(key
, "State")) {
438 *state
= pa_bt_audio_state_from_string(value
);
439 pa_log_debug("dbus: property 'State' changed to value '%s'", value
);
449 static void run_callback(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, pa_bool_t dead
) {
453 if (!device_is_audio(d
))
457 pa_hook_fire(&y
->hook
, d
);
460 static void remove_all_devices(pa_bluetooth_discovery
*y
) {
461 pa_bluetooth_device
*d
;
465 while ((d
= pa_hashmap_steal_first(y
->devices
))) {
466 run_callback(y
, d
, TRUE
);
471 static pa_bluetooth_device
*found_device(pa_bluetooth_discovery
*y
, const char* path
) {
473 pa_bluetooth_device
*d
;
478 d
= pa_hashmap_get(y
->devices
, path
);
482 d
= device_new(path
);
484 pa_hashmap_put(y
->devices
, d
->path
, d
);
486 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Device", "GetProperties"));
487 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
489 /* Before we read the other properties (Audio, AudioSink, AudioSource,
490 * Headset) we wait that the UUID is read */
494 static void get_properties_reply(DBusPendingCall
*pending
, void *userdata
) {
496 DBusMessageIter arg_i
, element_i
;
498 pa_bluetooth_device
*d
;
499 pa_bluetooth_discovery
*y
;
502 pa_assert_se(p
= userdata
);
503 pa_assert_se(y
= p
->context_data
);
504 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
506 /* pa_log_debug("Got %s.GetProperties response for %s", */
507 /* dbus_message_get_interface(p->message), */
508 /* dbus_message_get_path(p->message)); */
510 /* We don't use p->call_data here right-away since the device
511 * might already be invalidated at this point */
513 if (dbus_message_has_interface(p
->message
, "org.bluez.Manager") ||
514 dbus_message_has_interface(p
->message
, "org.bluez.Adapter"))
517 d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(p
->message
));
519 pa_assert(p
->call_data
== d
);
521 valid
= dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
? -1 : 1;
523 if (dbus_message_is_method_call(p
->message
, "org.bluez.Device", "GetProperties"))
524 d
->device_info_valid
= valid
;
526 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
527 pa_log_debug("Bluetooth daemon is apparently not available.");
528 remove_all_devices(y
);
532 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
533 pa_log("%s.GetProperties() failed: %s: %s", dbus_message_get_interface(p
->message
), dbus_message_get_error_name(r
), pa_dbus_get_error_message(r
));
537 if (!dbus_message_iter_init(r
, &arg_i
)) {
538 pa_log("GetProperties reply has no arguments.");
542 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
543 pa_log("GetProperties argument is not an array.");
547 dbus_message_iter_recurse(&arg_i
, &element_i
);
548 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
550 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
551 DBusMessageIter dict_i
;
553 dbus_message_iter_recurse(&element_i
, &dict_i
);
555 if (dbus_message_has_interface(p
->message
, "org.bluez.Manager")) {
556 if (parse_manager_property(y
, &dict_i
) < 0)
559 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Adapter")) {
560 if (parse_adapter_property(y
, &dict_i
) < 0)
563 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Device")) {
564 if (parse_device_property(y
, d
, &dict_i
) < 0)
567 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Audio")) {
568 if (parse_audio_property(y
, &d
->audio_state
, &dict_i
) < 0)
571 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Headset")) {
572 if (parse_audio_property(y
, &d
->headset_state
, &dict_i
) < 0)
575 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSink")) {
576 if (parse_audio_property(y
, &d
->audio_sink_state
, &dict_i
) < 0)
579 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSource")) {
580 if (parse_audio_property(y
, &d
->audio_source_state
, &dict_i
) < 0)
583 } else if (dbus_message_has_interface(p
->message
, "org.bluez.HandsfreeGateway")) {
584 if (parse_audio_property(y
, &d
->hfgw_state
, &dict_i
) < 0)
590 dbus_message_iter_next(&element_i
);
595 run_callback(y
, d
, FALSE
);
598 dbus_message_unref(r
);
600 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
601 pa_dbus_pending_free(p
);
604 static pa_dbus_pending
* send_and_add_to_pending(pa_bluetooth_discovery
*y
, DBusMessage
*m
, DBusPendingCallNotifyFunction func
, void *call_data
) {
606 DBusPendingCall
*call
;
611 pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y
->connection
), m
, &call
, -1));
613 p
= pa_dbus_pending_new(pa_dbus_connection_get(y
->connection
), m
, call
, y
, call_data
);
614 PA_LLIST_PREPEND(pa_dbus_pending
, y
->pending
, p
);
615 dbus_pending_call_set_notify(call
, func
, p
, NULL
);
620 static void register_endpoint_reply(DBusPendingCall
*pending
, void *userdata
) {
624 pa_bluetooth_discovery
*y
;
631 pa_assert_se(p
= userdata
);
632 pa_assert_se(y
= p
->context_data
);
633 pa_assert_se(endpoint
= p
->call_data
);
634 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
636 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
637 pa_log_debug("Bluetooth daemon is apparently not available.");
638 remove_all_devices(y
);
642 if (dbus_message_is_error(r
, PA_BLUETOOTH_ERROR_NOT_SUPPORTED
)) {
643 pa_log_info("Couldn't register endpoint %s, because BlueZ is configured to disable the endpoint type.", endpoint
);
647 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
648 pa_log("org.bluez.Media.RegisterEndpoint() failed: %s: %s", dbus_message_get_error_name(r
), pa_dbus_get_error_message(r
));
653 dbus_message_unref(r
);
655 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
656 pa_dbus_pending_free(p
);
661 static void register_endpoint(pa_bluetooth_discovery
*y
, const char *path
, const char *endpoint
, const char *uuid
) {
663 DBusMessageIter i
, d
;
666 pa_log_debug("Registering %s on adapter %s.", endpoint
, path
);
668 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Media", "RegisterEndpoint"));
670 dbus_message_iter_init_append(m
, &i
);
672 dbus_message_iter_append_basic(&i
, DBUS_TYPE_OBJECT_PATH
, &endpoint
);
674 dbus_message_iter_open_container(&i
, DBUS_TYPE_ARRAY
, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
675 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING
,
678 pa_dbus_append_basic_variant_dict_entry(&d
, "UUID", DBUS_TYPE_STRING
, &uuid
);
680 pa_dbus_append_basic_variant_dict_entry(&d
, "Codec", DBUS_TYPE_BYTE
, &codec
);
682 if (pa_streq(uuid
, HFP_AG_UUID
) || pa_streq(uuid
, HFP_HS_UUID
)) {
683 uint8_t capability
= 0;
684 pa_dbus_append_basic_array_variant_dict_entry(&d
, "Capabilities", DBUS_TYPE_BYTE
, &capability
, 1);
686 a2dp_sbc_t capabilities
;
688 capabilities
.channel_mode
= SBC_CHANNEL_MODE_MONO
| SBC_CHANNEL_MODE_DUAL_CHANNEL
|
689 SBC_CHANNEL_MODE_STEREO
| SBC_CHANNEL_MODE_JOINT_STEREO
;
690 capabilities
.frequency
= SBC_SAMPLING_FREQ_16000
| SBC_SAMPLING_FREQ_32000
|
691 SBC_SAMPLING_FREQ_44100
| SBC_SAMPLING_FREQ_48000
;
692 capabilities
.allocation_method
= SBC_ALLOCATION_SNR
| SBC_ALLOCATION_LOUDNESS
;
693 capabilities
.subbands
= SBC_SUBBANDS_4
| SBC_SUBBANDS_8
;
694 capabilities
.block_length
= SBC_BLOCK_LENGTH_4
| SBC_BLOCK_LENGTH_8
|
695 SBC_BLOCK_LENGTH_12
| SBC_BLOCK_LENGTH_16
;
696 capabilities
.min_bitpool
= MIN_BITPOOL
;
697 capabilities
.max_bitpool
= MAX_BITPOOL
;
699 pa_dbus_append_basic_array_variant_dict_entry(&d
, "Capabilities", DBUS_TYPE_BYTE
, &capabilities
, sizeof(capabilities
));
702 dbus_message_iter_close_container(&i
, &d
);
704 send_and_add_to_pending(y
, m
, register_endpoint_reply
, pa_xstrdup(endpoint
));
707 static void found_adapter(pa_bluetooth_discovery
*y
, const char *path
) {
710 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Adapter", "GetProperties"));
711 send_and_add_to_pending(y
, m
, get_properties_reply
, NULL
);
713 register_endpoint(y
, path
, HFP_AG_ENDPOINT
, HFP_AG_UUID
);
714 register_endpoint(y
, path
, HFP_HS_ENDPOINT
, HFP_HS_UUID
);
715 register_endpoint(y
, path
, A2DP_SOURCE_ENDPOINT
, A2DP_SOURCE_UUID
);
716 register_endpoint(y
, path
, A2DP_SINK_ENDPOINT
, A2DP_SINK_UUID
);
719 static void list_adapters(pa_bluetooth_discovery
*y
) {
723 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "GetProperties"));
724 send_and_add_to_pending(y
, m
, get_properties_reply
, NULL
);
727 int pa_bluetooth_transport_parse_property(pa_bluetooth_transport
*t
, DBusMessageIter
*i
)
730 DBusMessageIter variant_i
;
732 key
= check_variant_property(i
);
736 dbus_message_iter_recurse(i
, &variant_i
);
738 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
740 case DBUS_TYPE_BOOLEAN
: {
743 dbus_message_iter_get_basic(&variant_i
, &value
);
745 if (pa_streq(key
, "NREC") && t
->nrec
!= value
) {
747 pa_log_debug("Transport %s: Property 'NREC' changed to %s.", t
->path
, t
->nrec
? "True" : "False");
748 pa_hook_fire(&t
->hooks
[PA_BLUETOOTH_TRANSPORT_HOOK_NREC_CHANGED
], NULL
);
758 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*m
, void *userdata
) {
760 pa_bluetooth_discovery
*y
;
765 pa_assert_se(y
= userdata
);
767 dbus_error_init(&err
);
769 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
770 dbus_message_get_interface(m
),
771 dbus_message_get_path(m
),
772 dbus_message_get_member(m
));
774 if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceRemoved")) {
776 pa_bluetooth_device
*d
;
778 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
779 pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err
.message
);
783 pa_log_debug("Device %s removed", path
);
785 if ((d
= pa_hashmap_remove(y
->devices
, path
))) {
786 run_callback(y
, d
, TRUE
);
790 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
792 } else if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceCreated")) {
795 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
796 pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err
.message
);
800 pa_log_debug("Device %s created", path
);
802 found_device(y
, path
);
803 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
805 } else if (dbus_message_is_signal(m
, "org.bluez.Manager", "AdapterAdded")) {
808 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
809 pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err
.message
);
813 pa_log_debug("Adapter %s created", path
);
815 found_adapter(y
, path
);
816 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
818 } else if (dbus_message_is_signal(m
, "org.bluez.Audio", "PropertyChanged") ||
819 dbus_message_is_signal(m
, "org.bluez.Headset", "PropertyChanged") ||
820 dbus_message_is_signal(m
, "org.bluez.AudioSink", "PropertyChanged") ||
821 dbus_message_is_signal(m
, "org.bluez.AudioSource", "PropertyChanged") ||
822 dbus_message_is_signal(m
, "org.bluez.HandsfreeGateway", "PropertyChanged") ||
823 dbus_message_is_signal(m
, "org.bluez.Device", "PropertyChanged")) {
825 pa_bluetooth_device
*d
;
827 if ((d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(m
)))) {
828 DBusMessageIter arg_i
;
830 if (!dbus_message_iter_init(m
, &arg_i
)) {
831 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
835 if (dbus_message_has_interface(m
, "org.bluez.Device")) {
836 if (parse_device_property(y
, d
, &arg_i
) < 0)
839 } else if (dbus_message_has_interface(m
, "org.bluez.Audio")) {
840 if (parse_audio_property(y
, &d
->audio_state
, &arg_i
) < 0)
843 } else if (dbus_message_has_interface(m
, "org.bluez.Headset")) {
844 if (parse_audio_property(y
, &d
->headset_state
, &arg_i
) < 0)
847 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSink")) {
848 if (parse_audio_property(y
, &d
->audio_sink_state
, &arg_i
) < 0)
851 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSource")) {
852 if (parse_audio_property(y
, &d
->audio_source_state
, &arg_i
) < 0)
855 } else if (dbus_message_has_interface(m
, "org.bluez.HandsfreeGateway")) {
856 if (parse_audio_property(y
, &d
->hfgw_state
, &arg_i
) < 0)
860 run_callback(y
, d
, FALSE
);
863 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
865 } else if (dbus_message_is_signal(m
, "org.freedesktop.DBus", "NameOwnerChanged")) {
866 const char *name
, *old_owner
, *new_owner
;
868 if (!dbus_message_get_args(m
, &err
,
869 DBUS_TYPE_STRING
, &name
,
870 DBUS_TYPE_STRING
, &old_owner
,
871 DBUS_TYPE_STRING
, &new_owner
,
872 DBUS_TYPE_INVALID
)) {
873 pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err
.message
);
877 if (pa_streq(name
, "org.bluez")) {
878 if (old_owner
&& *old_owner
) {
879 pa_log_debug("Bluetooth daemon disappeared.");
880 remove_all_devices(y
);
883 if (new_owner
&& *new_owner
) {
884 pa_log_debug("Bluetooth daemon appeared.");
889 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
890 } else if (dbus_message_is_signal(m
, "org.bluez.MediaTransport", "PropertyChanged")) {
891 pa_bluetooth_device
*d
;
892 pa_bluetooth_transport
*t
= NULL
;
894 DBusMessageIter arg_i
;
896 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
897 if ((t
= pa_hashmap_get(d
->transports
, dbus_message_get_path(m
))))
903 if (!dbus_message_iter_init(m
, &arg_i
)) {
904 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
908 if (pa_bluetooth_transport_parse_property(t
, &arg_i
) < 0)
911 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
915 dbus_error_free(&err
);
917 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
920 const pa_bluetooth_device
* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery
*y
, const char* address
) {
921 pa_bluetooth_device
*d
;
925 pa_assert(PA_REFCNT_VALUE(y
) > 0);
928 if (!pa_hook_is_firing(&y
->hook
))
929 pa_bluetooth_discovery_sync(y
);
931 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
932 if (pa_streq(d
->address
, address
))
933 return device_is_audio(d
) ? d
: NULL
;
938 const pa_bluetooth_device
* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery
*y
, const char* path
) {
939 pa_bluetooth_device
*d
;
942 pa_assert(PA_REFCNT_VALUE(y
) > 0);
945 if (!pa_hook_is_firing(&y
->hook
))
946 pa_bluetooth_discovery_sync(y
);
948 if ((d
= pa_hashmap_get(y
->devices
, path
)))
949 if (device_is_audio(d
))
955 pa_bluetooth_transport
* pa_bluetooth_discovery_get_transport(pa_bluetooth_discovery
*y
, const char *path
) {
956 pa_bluetooth_device
*d
;
957 pa_bluetooth_transport
*t
;
961 pa_assert(PA_REFCNT_VALUE(y
) > 0);
964 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
965 if ((t
= pa_hashmap_get(d
->transports
, path
)))
971 const pa_bluetooth_transport
* pa_bluetooth_device_get_transport(const pa_bluetooth_device
*d
, enum profile profile
) {
972 pa_bluetooth_transport
*t
;
977 while ((t
= pa_hashmap_iterate(d
->transports
, &state
, NULL
)))
978 if (t
->profile
== profile
)
984 int pa_bluetooth_transport_acquire(const pa_bluetooth_transport
*t
, const char *accesstype
, size_t *imtu
, size_t *omtu
) {
993 dbus_error_init(&err
);
995 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", t
->path
, "org.bluez.MediaTransport", "Acquire"));
996 pa_assert_se(dbus_message_append_args(m
, DBUS_TYPE_STRING
, &accesstype
, DBUS_TYPE_INVALID
));
997 r
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t
->y
->connection
), m
, -1, &err
);
999 if (dbus_error_is_set(&err
) || !r
) {
1000 dbus_error_free(&err
);
1004 if (!dbus_message_get_args(r
, &err
, DBUS_TYPE_UNIX_FD
, &ret
, DBUS_TYPE_UINT16
, &i
, DBUS_TYPE_UINT16
, &o
, DBUS_TYPE_INVALID
)) {
1005 pa_log("Failed to parse org.bluez.MediaTransport.Acquire(): %s", err
.message
);
1007 dbus_error_free(&err
);
1018 dbus_message_unref(r
);
1022 void pa_bluetooth_transport_release(const pa_bluetooth_transport
*t
, const char *accesstype
) {
1029 dbus_error_init(&err
);
1031 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", t
->path
, "org.bluez.MediaTransport", "Release"));
1032 pa_assert_se(dbus_message_append_args(m
, DBUS_TYPE_STRING
, &accesstype
, DBUS_TYPE_INVALID
));
1033 dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t
->y
->connection
), m
, -1, &err
);
1035 if (dbus_error_is_set(&err
)) {
1036 pa_log("Failed to release transport %s: %s", t
->path
, err
.message
);
1037 dbus_error_free(&err
);
1039 pa_log_info("Transport %s released", t
->path
);
1042 static int setup_dbus(pa_bluetooth_discovery
*y
) {
1045 dbus_error_init(&err
);
1047 y
->connection
= pa_dbus_bus_get(y
->core
, DBUS_BUS_SYSTEM
, &err
);
1049 if (dbus_error_is_set(&err
) || !y
->connection
) {
1050 pa_log("Failed to get D-Bus connection: %s", err
.message
);
1051 dbus_error_free(&err
);
1058 static pa_bluetooth_transport
*transport_new(pa_bluetooth_discovery
*y
, const char *path
, enum profile p
, const uint8_t *config
, int size
) {
1059 pa_bluetooth_transport
*t
;
1062 t
= pa_xnew0(pa_bluetooth_transport
, 1);
1064 t
->path
= pa_xstrdup(path
);
1066 t
->config_size
= size
;
1069 t
->config
= pa_xnew(uint8_t, size
);
1070 memcpy(t
->config
, config
, size
);
1073 for (i
= 0; i
< PA_BLUETOOTH_TRANSPORT_HOOK_MAX
; i
++)
1074 pa_hook_init(&t
->hooks
[i
], t
);
1079 static DBusMessage
*endpoint_set_configuration(DBusConnection
*conn
, DBusMessage
*m
, void *userdata
) {
1080 pa_bluetooth_discovery
*y
= userdata
;
1081 pa_bluetooth_device
*d
;
1082 pa_bluetooth_transport
*t
;
1083 const char *path
, *dev_path
= NULL
, *uuid
= NULL
;
1084 uint8_t *config
= NULL
;
1086 pa_bool_t nrec
= FALSE
;
1088 DBusMessageIter args
, props
;
1091 dbus_message_iter_init(m
, &args
);
1093 dbus_message_iter_get_basic(&args
, &path
);
1094 if (!dbus_message_iter_next(&args
))
1097 dbus_message_iter_recurse(&args
, &props
);
1098 if (dbus_message_iter_get_arg_type(&props
) != DBUS_TYPE_DICT_ENTRY
)
1101 /* Read transport properties */
1102 while (dbus_message_iter_get_arg_type(&props
) == DBUS_TYPE_DICT_ENTRY
) {
1104 DBusMessageIter value
, entry
;
1107 dbus_message_iter_recurse(&props
, &entry
);
1108 dbus_message_iter_get_basic(&entry
, &key
);
1110 dbus_message_iter_next(&entry
);
1111 dbus_message_iter_recurse(&entry
, &value
);
1113 var
= dbus_message_iter_get_arg_type(&value
);
1114 if (strcasecmp(key
, "UUID") == 0) {
1115 if (var
!= DBUS_TYPE_STRING
)
1117 dbus_message_iter_get_basic(&value
, &uuid
);
1118 } else if (strcasecmp(key
, "Device") == 0) {
1119 if (var
!= DBUS_TYPE_OBJECT_PATH
)
1121 dbus_message_iter_get_basic(&value
, &dev_path
);
1122 } else if (strcasecmp(key
, "NREC") == 0) {
1123 dbus_bool_t tmp_boolean
;
1124 if (var
!= DBUS_TYPE_BOOLEAN
)
1126 dbus_message_iter_get_basic(&value
, &tmp_boolean
);
1128 } else if (strcasecmp(key
, "Configuration") == 0) {
1129 DBusMessageIter array
;
1130 if (var
!= DBUS_TYPE_ARRAY
)
1132 dbus_message_iter_recurse(&value
, &array
);
1133 dbus_message_iter_get_fixed_array(&array
, &config
, &size
);
1136 dbus_message_iter_next(&props
);
1139 d
= found_device(y
, dev_path
);
1143 if (dbus_message_has_path(m
, HFP_AG_ENDPOINT
))
1145 else if (dbus_message_has_path(m
, HFP_HS_ENDPOINT
))
1147 else if (dbus_message_has_path(m
, A2DP_SOURCE_ENDPOINT
))
1150 p
= PROFILE_A2DP_SOURCE
;
1152 t
= transport_new(y
, path
, p
, config
, size
);
1155 pa_hashmap_put(d
->transports
, t
->path
, t
);
1157 pa_log_debug("Transport %s profile %d available", t
->path
, t
->profile
);
1159 pa_assert_se(r
= dbus_message_new_method_return(m
));
1164 pa_log("org.bluez.MediaEndpoint.SetConfiguration: invalid arguments");
1165 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1166 "Unable to set configuration")));
1170 static DBusMessage
*endpoint_clear_configuration(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1171 pa_bluetooth_discovery
*y
= userdata
;
1172 pa_bluetooth_device
*d
;
1173 pa_bluetooth_transport
*t
;
1179 dbus_error_init(&e
);
1181 if (!dbus_message_get_args(m
, &e
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
1182 pa_log("org.bluez.MediaEndpoint.ClearConfiguration: %s", e
.message
);
1183 dbus_error_free(&e
);
1187 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
))) {
1188 if ((t
= pa_hashmap_get(d
->transports
, path
))) {
1189 pa_log_debug("Clearing transport %s profile %d", t
->path
, t
->profile
);
1190 pa_hashmap_remove(d
->transports
, t
->path
);
1196 pa_assert_se(r
= dbus_message_new_method_return(m
));
1201 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1202 "Unable to clear configuration")));
1206 static uint8_t a2dp_default_bitpool(uint8_t freq
, uint8_t mode
) {
1209 case SBC_SAMPLING_FREQ_16000
:
1210 case SBC_SAMPLING_FREQ_32000
:
1213 case SBC_SAMPLING_FREQ_44100
:
1216 case SBC_CHANNEL_MODE_MONO
:
1217 case SBC_CHANNEL_MODE_DUAL_CHANNEL
:
1220 case SBC_CHANNEL_MODE_STEREO
:
1221 case SBC_CHANNEL_MODE_JOINT_STEREO
:
1225 pa_log_warn("Invalid channel mode %u", mode
);
1229 case SBC_SAMPLING_FREQ_48000
:
1232 case SBC_CHANNEL_MODE_MONO
:
1233 case SBC_CHANNEL_MODE_DUAL_CHANNEL
:
1236 case SBC_CHANNEL_MODE_STEREO
:
1237 case SBC_CHANNEL_MODE_JOINT_STEREO
:
1241 pa_log_warn("Invalid channel mode %u", mode
);
1246 pa_log_warn("Invalid sampling freq %u", freq
);
1251 static DBusMessage
*endpoint_select_configuration(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1252 pa_bluetooth_discovery
*y
= userdata
;
1253 a2dp_sbc_t
*cap
, config
;
1254 uint8_t *pconf
= (uint8_t *) &config
;
1259 static const struct {
1263 { 16000U, SBC_SAMPLING_FREQ_16000
},
1264 { 32000U, SBC_SAMPLING_FREQ_32000
},
1265 { 44100U, SBC_SAMPLING_FREQ_44100
},
1266 { 48000U, SBC_SAMPLING_FREQ_48000
}
1269 dbus_error_init(&e
);
1271 if (!dbus_message_get_args(m
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_BYTE
, &cap
, &size
, DBUS_TYPE_INVALID
)) {
1272 pa_log("org.bluez.MediaEndpoint.SelectConfiguration: %s", e
.message
);
1273 dbus_error_free(&e
);
1277 if (dbus_message_has_path(m
, HFP_AG_ENDPOINT
) || dbus_message_has_path(m
, HFP_HS_ENDPOINT
))
1280 pa_assert(size
== sizeof(config
));
1282 memset(&config
, 0, sizeof(config
));
1284 /* Find the lowest freq that is at least as high as the requested
1286 for (i
= 0; (unsigned) i
< PA_ELEMENTSOF(freq_table
); i
++)
1287 if (freq_table
[i
].rate
>= y
->core
->default_sample_spec
.rate
&& (cap
->frequency
& freq_table
[i
].cap
)) {
1288 config
.frequency
= freq_table
[i
].cap
;
1292 if ((unsigned) i
== PA_ELEMENTSOF(freq_table
)) {
1293 for (--i
; i
>= 0; i
--) {
1294 if (cap
->frequency
& freq_table
[i
].cap
) {
1295 config
.frequency
= freq_table
[i
].cap
;
1301 pa_log("Not suitable sample rate");
1306 pa_assert((unsigned) i
< PA_ELEMENTSOF(freq_table
));
1308 if (y
->core
->default_sample_spec
.channels
<= 1) {
1309 if (cap
->channel_mode
& SBC_CHANNEL_MODE_MONO
)
1310 config
.channel_mode
= SBC_CHANNEL_MODE_MONO
;
1313 if (y
->core
->default_sample_spec
.channels
>= 2) {
1314 if (cap
->channel_mode
& SBC_CHANNEL_MODE_JOINT_STEREO
)
1315 config
.channel_mode
= SBC_CHANNEL_MODE_JOINT_STEREO
;
1316 else if (cap
->channel_mode
& SBC_CHANNEL_MODE_STEREO
)
1317 config
.channel_mode
= SBC_CHANNEL_MODE_STEREO
;
1318 else if (cap
->channel_mode
& SBC_CHANNEL_MODE_DUAL_CHANNEL
)
1319 config
.channel_mode
= SBC_CHANNEL_MODE_DUAL_CHANNEL
;
1320 else if (cap
->channel_mode
& SBC_CHANNEL_MODE_MONO
) {
1321 config
.channel_mode
= SBC_CHANNEL_MODE_MONO
;
1323 pa_log("No supported channel modes");
1328 if (cap
->block_length
& SBC_BLOCK_LENGTH_16
)
1329 config
.block_length
= SBC_BLOCK_LENGTH_16
;
1330 else if (cap
->block_length
& SBC_BLOCK_LENGTH_12
)
1331 config
.block_length
= SBC_BLOCK_LENGTH_12
;
1332 else if (cap
->block_length
& SBC_BLOCK_LENGTH_8
)
1333 config
.block_length
= SBC_BLOCK_LENGTH_8
;
1334 else if (cap
->block_length
& SBC_BLOCK_LENGTH_4
)
1335 config
.block_length
= SBC_BLOCK_LENGTH_4
;
1337 pa_log_error("No supported block lengths");
1341 if (cap
->subbands
& SBC_SUBBANDS_8
)
1342 config
.subbands
= SBC_SUBBANDS_8
;
1343 else if (cap
->subbands
& SBC_SUBBANDS_4
)
1344 config
.subbands
= SBC_SUBBANDS_4
;
1346 pa_log_error("No supported subbands");
1350 if (cap
->allocation_method
& SBC_ALLOCATION_LOUDNESS
)
1351 config
.allocation_method
= SBC_ALLOCATION_LOUDNESS
;
1352 else if (cap
->allocation_method
& SBC_ALLOCATION_SNR
)
1353 config
.allocation_method
= SBC_ALLOCATION_SNR
;
1355 config
.min_bitpool
= (uint8_t) PA_MAX(MIN_BITPOOL
, cap
->min_bitpool
);
1356 config
.max_bitpool
= (uint8_t) PA_MIN(a2dp_default_bitpool(config
.frequency
, config
.channel_mode
), cap
->max_bitpool
);
1359 pa_assert_se(r
= dbus_message_new_method_return(m
));
1361 pa_assert_se(dbus_message_append_args(
1363 DBUS_TYPE_ARRAY
, DBUS_TYPE_BYTE
, &pconf
, size
,
1364 DBUS_TYPE_INVALID
));
1369 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1370 "Unable to select configuration")));
1374 static DBusHandlerResult
endpoint_handler(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1375 struct pa_bluetooth_discovery
*y
= userdata
;
1376 DBusMessage
*r
= NULL
;
1382 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
1383 dbus_message_get_interface(m
),
1384 dbus_message_get_path(m
),
1385 dbus_message_get_member(m
));
1387 path
= dbus_message_get_path(m
);
1388 dbus_error_init(&e
);
1390 if (!pa_streq(path
, A2DP_SOURCE_ENDPOINT
) && !pa_streq(path
, A2DP_SINK_ENDPOINT
) && !pa_streq(path
, HFP_AG_ENDPOINT
) && !pa_streq(path
, HFP_HS_ENDPOINT
))
1391 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1393 if (dbus_message_is_method_call(m
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1394 const char *xml
= ENDPOINT_INTROSPECT_XML
;
1396 pa_assert_se(r
= dbus_message_new_method_return(m
));
1397 pa_assert_se(dbus_message_append_args(
1399 DBUS_TYPE_STRING
, &xml
,
1400 DBUS_TYPE_INVALID
));
1402 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "SetConfiguration")) {
1403 r
= endpoint_set_configuration(c
, m
, userdata
);
1404 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "SelectConfiguration")) {
1405 r
= endpoint_select_configuration(c
, m
, userdata
);
1406 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "ClearConfiguration"))
1407 r
= endpoint_clear_configuration(c
, m
, userdata
);
1409 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1412 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y
->connection
), r
, NULL
));
1413 dbus_message_unref(r
);
1416 return DBUS_HANDLER_RESULT_HANDLED
;
1419 pa_bluetooth_discovery
* pa_bluetooth_discovery_get(pa_core
*c
) {
1421 pa_bluetooth_discovery
*y
;
1422 static const DBusObjectPathVTable vtable_endpoint
= {
1423 .message_function
= endpoint_handler
,
1428 dbus_error_init(&err
);
1430 if ((y
= pa_shared_get(c
, "bluetooth-discovery")))
1431 return pa_bluetooth_discovery_ref(y
);
1433 y
= pa_xnew0(pa_bluetooth_discovery
, 1);
1436 y
->devices
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
1437 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
->pending
);
1438 pa_hook_init(&y
->hook
, y
);
1439 pa_shared_set(c
, "bluetooth-discovery", y
);
1441 if (setup_dbus(y
) < 0)
1444 /* dynamic detection of bluetooth audio devices */
1445 if (!dbus_connection_add_filter(pa_dbus_connection_get(y
->connection
), filter_cb
, y
, NULL
)) {
1446 pa_log_error("Failed to add filter function");
1449 y
->filter_added
= TRUE
;
1451 if (pa_dbus_add_matches(
1452 pa_dbus_connection_get(y
->connection
), &err
,
1453 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1454 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1455 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1456 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1457 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1458 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1459 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1460 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1461 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1462 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1463 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
1465 pa_log("Failed to add D-Bus matches: %s", err
.message
);
1469 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), HFP_AG_ENDPOINT
, &vtable_endpoint
, y
));
1470 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), HFP_HS_ENDPOINT
, &vtable_endpoint
, y
));
1471 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SOURCE_ENDPOINT
, &vtable_endpoint
, y
));
1472 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SINK_ENDPOINT
, &vtable_endpoint
, y
));
1481 pa_bluetooth_discovery_unref(y
);
1483 dbus_error_free(&err
);
1488 pa_bluetooth_discovery
* pa_bluetooth_discovery_ref(pa_bluetooth_discovery
*y
) {
1490 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1497 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery
*y
) {
1499 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1501 if (PA_REFCNT_DEC(y
) > 0)
1504 pa_dbus_free_pending_list(&y
->pending
);
1507 remove_all_devices(y
);
1508 pa_hashmap_free(y
->devices
, NULL
, NULL
);
1511 if (y
->connection
) {
1512 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), HFP_AG_ENDPOINT
);
1513 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), HFP_HS_ENDPOINT
);
1514 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SOURCE_ENDPOINT
);
1515 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SINK_ENDPOINT
);
1516 pa_dbus_remove_matches(pa_dbus_connection_get(y
->connection
),
1517 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1518 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1519 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
1520 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1521 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1522 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1523 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1524 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1525 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1526 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1527 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1528 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
1531 if (y
->filter_added
)
1532 dbus_connection_remove_filter(pa_dbus_connection_get(y
->connection
), filter_cb
, y
);
1534 pa_dbus_connection_unref(y
->connection
);
1537 pa_hook_done(&y
->hook
);
1540 pa_shared_remove(y
->core
, "bluetooth-discovery");
1545 void pa_bluetooth_discovery_sync(pa_bluetooth_discovery
*y
) {
1547 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1549 pa_dbus_sync_pending_list(&y
->pending
);
1552 pa_hook
* pa_bluetooth_discovery_hook(pa_bluetooth_discovery
*y
) {
1554 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1559 const char*pa_bluetooth_get_form_factor(uint32_t class) {
1563 static const char * const table
[] = {
1574 if (((class >> 8) & 31) != 4)
1577 if ((i
= (class >> 2) & 63) > PA_ELEMENTSOF(table
))
1583 pa_log_debug("Unknown Bluetooth minor device class %u", i
);
1588 char *pa_bluetooth_cleanup_name(const char *name
) {
1590 pa_bool_t space
= FALSE
;
1594 while ((*name
>= 1 && *name
<= 32) || *name
>= 127)
1597 t
= pa_xstrdup(name
);
1599 for (s
= d
= t
; *s
; s
++) {
1601 if (*s
<= 32 || *s
>= 127 || *s
== '_') {
1619 pa_bool_t
pa_bluetooth_uuid_has(pa_bluetooth_uuid
*uuids
, const char *uuid
) {
1623 if (strcasecmp(uuids
->uuid
, uuid
) == 0)
1626 uuids
= uuids
->next
;