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
))) {
161 pa_hook_fire(&t
->hooks
[PA_BLUETOOTH_TRANSPORT_HOOK_REMOVED
], NULL
);
165 pa_hashmap_free(d
->transports
, NULL
, NULL
);
167 while ((u
= d
->uuids
)) {
168 PA_LLIST_REMOVE(pa_bluetooth_uuid
, d
->uuids
, u
);
175 pa_xfree(d
->address
);
179 static pa_bool_t
device_is_audio(pa_bluetooth_device
*d
) {
183 d
->device_info_valid
&& (d
->hfgw_state
!= PA_BT_AUDIO_STATE_INVALID
||
184 (d
->audio_state
!= PA_BT_AUDIO_STATE_INVALID
&&
185 (d
->audio_sink_state
!= PA_BT_AUDIO_STATE_INVALID
||
186 d
->audio_source_state
!= PA_BT_AUDIO_STATE_INVALID
||
187 d
->headset_state
!= PA_BT_AUDIO_STATE_INVALID
)));
190 static const char *check_variant_property(DBusMessageIter
*i
) {
195 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
196 pa_log("Property name not a string.");
200 dbus_message_iter_get_basic(i
, &key
);
202 if (!dbus_message_iter_next(i
)) {
203 pa_log("Property value missing");
207 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
208 pa_log("Property value not a variant.");
215 static int parse_manager_property(pa_bluetooth_discovery
*y
, DBusMessageIter
*i
) {
217 DBusMessageIter variant_i
;
221 key
= check_variant_property(i
);
225 dbus_message_iter_recurse(i
, &variant_i
);
227 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
229 case DBUS_TYPE_ARRAY
: {
232 dbus_message_iter_recurse(&variant_i
, &ai
);
234 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_OBJECT_PATH
&&
235 pa_streq(key
, "Adapters")) {
237 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
240 dbus_message_iter_get_basic(&ai
, &value
);
242 found_adapter(y
, value
);
244 dbus_message_iter_next(&ai
);
255 static int parse_adapter_property(pa_bluetooth_discovery
*y
, DBusMessageIter
*i
) {
257 DBusMessageIter variant_i
;
261 key
= check_variant_property(i
);
265 dbus_message_iter_recurse(i
, &variant_i
);
267 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
269 case DBUS_TYPE_ARRAY
: {
272 dbus_message_iter_recurse(&variant_i
, &ai
);
274 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_OBJECT_PATH
&&
275 pa_streq(key
, "Devices")) {
277 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
280 dbus_message_iter_get_basic(&ai
, &value
);
282 found_device(y
, value
);
284 dbus_message_iter_next(&ai
);
295 static int parse_device_property(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessageIter
*i
) {
297 DBusMessageIter variant_i
;
302 key
= check_variant_property(i
);
306 dbus_message_iter_recurse(i
, &variant_i
);
308 /* pa_log_debug("Parsing property org.bluez.Device.%s", key); */
310 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
312 case DBUS_TYPE_STRING
: {
315 dbus_message_iter_get_basic(&variant_i
, &value
);
317 if (pa_streq(key
, "Name")) {
319 d
->name
= pa_xstrdup(value
);
320 } else if (pa_streq(key
, "Alias")) {
322 d
->alias
= pa_xstrdup(value
);
323 } else if (pa_streq(key
, "Address")) {
324 pa_xfree(d
->address
);
325 d
->address
= pa_xstrdup(value
);
328 /* pa_log_debug("Value %s", value); */
333 case DBUS_TYPE_BOOLEAN
: {
336 dbus_message_iter_get_basic(&variant_i
, &value
);
338 if (pa_streq(key
, "Paired"))
340 else if (pa_streq(key
, "Connected"))
341 d
->device_connected
= !!value
;
342 else if (pa_streq(key
, "Trusted"))
343 d
->trusted
= !!value
;
345 /* pa_log_debug("Value %s", pa_yes_no(value)); */
350 case DBUS_TYPE_UINT32
: {
353 dbus_message_iter_get_basic(&variant_i
, &value
);
355 if (pa_streq(key
, "Class"))
356 d
->class = (int) value
;
358 /* pa_log_debug("Value %u", (unsigned) value); */
363 case DBUS_TYPE_ARRAY
: {
366 dbus_message_iter_recurse(&variant_i
, &ai
);
368 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_STRING
&&
369 pa_streq(key
, "UUIDs")) {
371 pa_bool_t has_audio
= FALSE
;
373 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
374 pa_bluetooth_uuid
*node
;
377 dbus_message_iter_get_basic(&ai
, &value
);
378 node
= uuid_new(value
);
379 PA_LLIST_PREPEND(pa_bluetooth_uuid
, d
->uuids
, node
);
381 /* Vudentz said the interfaces are here when the UUIDs are announced */
382 if (strcasecmp(HSP_AG_UUID
, value
) == 0 || strcasecmp(HFP_AG_UUID
, value
) == 0) {
383 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.HandsfreeGateway", "GetProperties"));
384 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
386 } else if (strcasecmp(HSP_HS_UUID
, value
) == 0 || strcasecmp(HFP_HS_UUID
, value
) == 0) {
387 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.Headset", "GetProperties"));
388 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
390 } else if (strcasecmp(A2DP_SINK_UUID
, value
) == 0) {
391 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.AudioSink", "GetProperties"));
392 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
394 } else if (strcasecmp(A2DP_SOURCE_UUID
, value
) == 0) {
395 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.AudioSource", "GetProperties"));
396 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
400 dbus_message_iter_next(&ai
);
403 /* 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 */
405 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.Audio", "GetProperties"));
406 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
417 static int parse_audio_property(pa_bluetooth_discovery
*u
, int *state
, DBusMessageIter
*i
) {
419 DBusMessageIter variant_i
;
424 key
= check_variant_property(i
);
428 dbus_message_iter_recurse(i
, &variant_i
);
430 /* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|AudioSource|Headset}.%s", key); */
432 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
434 case DBUS_TYPE_STRING
: {
437 dbus_message_iter_get_basic(&variant_i
, &value
);
439 if (pa_streq(key
, "State")) {
440 *state
= pa_bt_audio_state_from_string(value
);
441 pa_log_debug("dbus: property 'State' changed to value '%s'", value
);
451 static void run_callback(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, pa_bool_t dead
) {
455 if (!device_is_audio(d
))
459 pa_hook_fire(&y
->hook
, d
);
462 static void remove_all_devices(pa_bluetooth_discovery
*y
) {
463 pa_bluetooth_device
*d
;
467 while ((d
= pa_hashmap_steal_first(y
->devices
))) {
468 run_callback(y
, d
, TRUE
);
473 static pa_bluetooth_device
*found_device(pa_bluetooth_discovery
*y
, const char* path
) {
475 pa_bluetooth_device
*d
;
480 d
= pa_hashmap_get(y
->devices
, path
);
484 d
= device_new(path
);
486 pa_hashmap_put(y
->devices
, d
->path
, d
);
488 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Device", "GetProperties"));
489 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
491 /* Before we read the other properties (Audio, AudioSink, AudioSource,
492 * Headset) we wait that the UUID is read */
496 static void get_properties_reply(DBusPendingCall
*pending
, void *userdata
) {
498 DBusMessageIter arg_i
, element_i
;
500 pa_bluetooth_device
*d
;
501 pa_bluetooth_discovery
*y
;
504 pa_assert_se(p
= userdata
);
505 pa_assert_se(y
= p
->context_data
);
506 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
508 /* pa_log_debug("Got %s.GetProperties response for %s", */
509 /* dbus_message_get_interface(p->message), */
510 /* dbus_message_get_path(p->message)); */
512 /* We don't use p->call_data here right-away since the device
513 * might already be invalidated at this point */
515 if (dbus_message_has_interface(p
->message
, "org.bluez.Manager") ||
516 dbus_message_has_interface(p
->message
, "org.bluez.Adapter"))
519 d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(p
->message
));
521 pa_assert(p
->call_data
== d
);
523 valid
= dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
? -1 : 1;
525 if (dbus_message_is_method_call(p
->message
, "org.bluez.Device", "GetProperties"))
526 d
->device_info_valid
= valid
;
528 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
529 pa_log_debug("Bluetooth daemon is apparently not available.");
530 remove_all_devices(y
);
534 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
535 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
));
539 if (!dbus_message_iter_init(r
, &arg_i
)) {
540 pa_log("GetProperties reply has no arguments.");
544 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
545 pa_log("GetProperties argument is not an array.");
549 dbus_message_iter_recurse(&arg_i
, &element_i
);
550 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
552 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
553 DBusMessageIter dict_i
;
555 dbus_message_iter_recurse(&element_i
, &dict_i
);
557 if (dbus_message_has_interface(p
->message
, "org.bluez.Manager")) {
558 if (parse_manager_property(y
, &dict_i
) < 0)
561 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Adapter")) {
562 if (parse_adapter_property(y
, &dict_i
) < 0)
565 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Device")) {
566 if (parse_device_property(y
, d
, &dict_i
) < 0)
569 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Audio")) {
570 if (parse_audio_property(y
, &d
->audio_state
, &dict_i
) < 0)
573 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Headset")) {
574 if (parse_audio_property(y
, &d
->headset_state
, &dict_i
) < 0)
577 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSink")) {
578 if (parse_audio_property(y
, &d
->audio_sink_state
, &dict_i
) < 0)
581 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSource")) {
582 if (parse_audio_property(y
, &d
->audio_source_state
, &dict_i
) < 0)
585 } else if (dbus_message_has_interface(p
->message
, "org.bluez.HandsfreeGateway")) {
586 if (parse_audio_property(y
, &d
->hfgw_state
, &dict_i
) < 0)
592 dbus_message_iter_next(&element_i
);
597 run_callback(y
, d
, FALSE
);
600 dbus_message_unref(r
);
602 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
603 pa_dbus_pending_free(p
);
606 static pa_dbus_pending
* send_and_add_to_pending(pa_bluetooth_discovery
*y
, DBusMessage
*m
, DBusPendingCallNotifyFunction func
, void *call_data
) {
608 DBusPendingCall
*call
;
613 pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y
->connection
), m
, &call
, -1));
615 p
= pa_dbus_pending_new(pa_dbus_connection_get(y
->connection
), m
, call
, y
, call_data
);
616 PA_LLIST_PREPEND(pa_dbus_pending
, y
->pending
, p
);
617 dbus_pending_call_set_notify(call
, func
, p
, NULL
);
622 static void register_endpoint_reply(DBusPendingCall
*pending
, void *userdata
) {
626 pa_bluetooth_discovery
*y
;
633 pa_assert_se(p
= userdata
);
634 pa_assert_se(y
= p
->context_data
);
635 pa_assert_se(endpoint
= p
->call_data
);
636 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
638 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
639 pa_log_debug("Bluetooth daemon is apparently not available.");
640 remove_all_devices(y
);
644 if (dbus_message_is_error(r
, PA_BLUETOOTH_ERROR_NOT_SUPPORTED
)) {
645 pa_log_info("Couldn't register endpoint %s, because BlueZ is configured to disable the endpoint type.", endpoint
);
649 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
650 pa_log("org.bluez.Media.RegisterEndpoint() failed: %s: %s", dbus_message_get_error_name(r
), pa_dbus_get_error_message(r
));
655 dbus_message_unref(r
);
657 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
658 pa_dbus_pending_free(p
);
663 static void register_endpoint(pa_bluetooth_discovery
*y
, const char *path
, const char *endpoint
, const char *uuid
) {
665 DBusMessageIter i
, d
;
668 pa_log_debug("Registering %s on adapter %s.", endpoint
, path
);
670 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Media", "RegisterEndpoint"));
672 dbus_message_iter_init_append(m
, &i
);
674 dbus_message_iter_append_basic(&i
, DBUS_TYPE_OBJECT_PATH
, &endpoint
);
676 dbus_message_iter_open_container(&i
, DBUS_TYPE_ARRAY
, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
677 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING
,
680 pa_dbus_append_basic_variant_dict_entry(&d
, "UUID", DBUS_TYPE_STRING
, &uuid
);
682 pa_dbus_append_basic_variant_dict_entry(&d
, "Codec", DBUS_TYPE_BYTE
, &codec
);
684 if (pa_streq(uuid
, HFP_AG_UUID
) || pa_streq(uuid
, HFP_HS_UUID
)) {
685 uint8_t capability
= 0;
686 pa_dbus_append_basic_array_variant_dict_entry(&d
, "Capabilities", DBUS_TYPE_BYTE
, &capability
, 1);
688 a2dp_sbc_t capabilities
;
690 capabilities
.channel_mode
= SBC_CHANNEL_MODE_MONO
| SBC_CHANNEL_MODE_DUAL_CHANNEL
|
691 SBC_CHANNEL_MODE_STEREO
| SBC_CHANNEL_MODE_JOINT_STEREO
;
692 capabilities
.frequency
= SBC_SAMPLING_FREQ_16000
| SBC_SAMPLING_FREQ_32000
|
693 SBC_SAMPLING_FREQ_44100
| SBC_SAMPLING_FREQ_48000
;
694 capabilities
.allocation_method
= SBC_ALLOCATION_SNR
| SBC_ALLOCATION_LOUDNESS
;
695 capabilities
.subbands
= SBC_SUBBANDS_4
| SBC_SUBBANDS_8
;
696 capabilities
.block_length
= SBC_BLOCK_LENGTH_4
| SBC_BLOCK_LENGTH_8
|
697 SBC_BLOCK_LENGTH_12
| SBC_BLOCK_LENGTH_16
;
698 capabilities
.min_bitpool
= MIN_BITPOOL
;
699 capabilities
.max_bitpool
= MAX_BITPOOL
;
701 pa_dbus_append_basic_array_variant_dict_entry(&d
, "Capabilities", DBUS_TYPE_BYTE
, &capabilities
, sizeof(capabilities
));
704 dbus_message_iter_close_container(&i
, &d
);
706 send_and_add_to_pending(y
, m
, register_endpoint_reply
, pa_xstrdup(endpoint
));
709 static void found_adapter(pa_bluetooth_discovery
*y
, const char *path
) {
712 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Adapter", "GetProperties"));
713 send_and_add_to_pending(y
, m
, get_properties_reply
, NULL
);
715 register_endpoint(y
, path
, HFP_AG_ENDPOINT
, HFP_AG_UUID
);
716 register_endpoint(y
, path
, HFP_HS_ENDPOINT
, HFP_HS_UUID
);
717 register_endpoint(y
, path
, A2DP_SOURCE_ENDPOINT
, A2DP_SOURCE_UUID
);
718 register_endpoint(y
, path
, A2DP_SINK_ENDPOINT
, A2DP_SINK_UUID
);
721 static void list_adapters(pa_bluetooth_discovery
*y
) {
725 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "GetProperties"));
726 send_and_add_to_pending(y
, m
, get_properties_reply
, NULL
);
729 int pa_bluetooth_transport_parse_property(pa_bluetooth_transport
*t
, DBusMessageIter
*i
)
732 DBusMessageIter variant_i
;
734 key
= check_variant_property(i
);
738 dbus_message_iter_recurse(i
, &variant_i
);
740 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
742 case DBUS_TYPE_BOOLEAN
: {
745 dbus_message_iter_get_basic(&variant_i
, &value
);
747 if (pa_streq(key
, "NREC") && t
->nrec
!= value
) {
749 pa_log_debug("Transport %s: Property 'NREC' changed to %s.", t
->path
, t
->nrec
? "True" : "False");
750 pa_hook_fire(&t
->hooks
[PA_BLUETOOTH_TRANSPORT_HOOK_NREC_CHANGED
], NULL
);
760 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*m
, void *userdata
) {
762 pa_bluetooth_discovery
*y
;
767 pa_assert_se(y
= userdata
);
769 dbus_error_init(&err
);
771 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
772 dbus_message_get_interface(m
),
773 dbus_message_get_path(m
),
774 dbus_message_get_member(m
));
776 if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceRemoved")) {
778 pa_bluetooth_device
*d
;
780 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
781 pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err
.message
);
785 pa_log_debug("Device %s removed", path
);
787 if ((d
= pa_hashmap_remove(y
->devices
, path
))) {
788 run_callback(y
, d
, TRUE
);
792 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
794 } else if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceCreated")) {
797 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
798 pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err
.message
);
802 pa_log_debug("Device %s created", path
);
804 found_device(y
, path
);
805 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
807 } else if (dbus_message_is_signal(m
, "org.bluez.Manager", "AdapterAdded")) {
810 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
811 pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err
.message
);
815 pa_log_debug("Adapter %s created", path
);
817 found_adapter(y
, path
);
818 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
820 } else if (dbus_message_is_signal(m
, "org.bluez.Audio", "PropertyChanged") ||
821 dbus_message_is_signal(m
, "org.bluez.Headset", "PropertyChanged") ||
822 dbus_message_is_signal(m
, "org.bluez.AudioSink", "PropertyChanged") ||
823 dbus_message_is_signal(m
, "org.bluez.AudioSource", "PropertyChanged") ||
824 dbus_message_is_signal(m
, "org.bluez.HandsfreeGateway", "PropertyChanged") ||
825 dbus_message_is_signal(m
, "org.bluez.Device", "PropertyChanged")) {
827 pa_bluetooth_device
*d
;
829 if ((d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(m
)))) {
830 DBusMessageIter arg_i
;
832 if (!dbus_message_iter_init(m
, &arg_i
)) {
833 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
837 if (dbus_message_has_interface(m
, "org.bluez.Device")) {
838 if (parse_device_property(y
, d
, &arg_i
) < 0)
841 } else if (dbus_message_has_interface(m
, "org.bluez.Audio")) {
842 if (parse_audio_property(y
, &d
->audio_state
, &arg_i
) < 0)
845 } else if (dbus_message_has_interface(m
, "org.bluez.Headset")) {
846 if (parse_audio_property(y
, &d
->headset_state
, &arg_i
) < 0)
849 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSink")) {
850 if (parse_audio_property(y
, &d
->audio_sink_state
, &arg_i
) < 0)
853 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSource")) {
854 if (parse_audio_property(y
, &d
->audio_source_state
, &arg_i
) < 0)
857 } else if (dbus_message_has_interface(m
, "org.bluez.HandsfreeGateway")) {
858 if (parse_audio_property(y
, &d
->hfgw_state
, &arg_i
) < 0)
862 run_callback(y
, d
, FALSE
);
865 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
867 } else if (dbus_message_is_signal(m
, "org.freedesktop.DBus", "NameOwnerChanged")) {
868 const char *name
, *old_owner
, *new_owner
;
870 if (!dbus_message_get_args(m
, &err
,
871 DBUS_TYPE_STRING
, &name
,
872 DBUS_TYPE_STRING
, &old_owner
,
873 DBUS_TYPE_STRING
, &new_owner
,
874 DBUS_TYPE_INVALID
)) {
875 pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err
.message
);
879 if (pa_streq(name
, "org.bluez")) {
880 if (old_owner
&& *old_owner
) {
881 pa_log_debug("Bluetooth daemon disappeared.");
882 remove_all_devices(y
);
885 if (new_owner
&& *new_owner
) {
886 pa_log_debug("Bluetooth daemon appeared.");
891 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
892 } else if (dbus_message_is_signal(m
, "org.bluez.MediaTransport", "PropertyChanged")) {
893 pa_bluetooth_device
*d
;
894 pa_bluetooth_transport
*t
= NULL
;
896 DBusMessageIter arg_i
;
898 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
899 if ((t
= pa_hashmap_get(d
->transports
, dbus_message_get_path(m
))))
905 if (!dbus_message_iter_init(m
, &arg_i
)) {
906 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
910 if (pa_bluetooth_transport_parse_property(t
, &arg_i
) < 0)
913 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
917 dbus_error_free(&err
);
919 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
922 const pa_bluetooth_device
* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery
*y
, const char* address
) {
923 pa_bluetooth_device
*d
;
927 pa_assert(PA_REFCNT_VALUE(y
) > 0);
930 if (!pa_hook_is_firing(&y
->hook
))
931 pa_bluetooth_discovery_sync(y
);
933 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
934 if (pa_streq(d
->address
, address
))
935 return device_is_audio(d
) ? d
: NULL
;
940 const pa_bluetooth_device
* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery
*y
, const char* path
) {
941 pa_bluetooth_device
*d
;
944 pa_assert(PA_REFCNT_VALUE(y
) > 0);
947 if (!pa_hook_is_firing(&y
->hook
))
948 pa_bluetooth_discovery_sync(y
);
950 if ((d
= pa_hashmap_get(y
->devices
, path
)))
951 if (device_is_audio(d
))
957 pa_bluetooth_transport
* pa_bluetooth_discovery_get_transport(pa_bluetooth_discovery
*y
, const char *path
) {
958 pa_bluetooth_device
*d
;
959 pa_bluetooth_transport
*t
;
963 pa_assert(PA_REFCNT_VALUE(y
) > 0);
966 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
967 if ((t
= pa_hashmap_get(d
->transports
, path
)))
973 pa_bluetooth_transport
* pa_bluetooth_device_get_transport(const pa_bluetooth_device
*d
, enum profile profile
) {
974 pa_bluetooth_transport
*t
;
979 while ((t
= pa_hashmap_iterate(d
->transports
, &state
, NULL
)))
980 if (t
->profile
== profile
)
986 int pa_bluetooth_transport_acquire(pa_bluetooth_transport
*t
, const char *accesstype
, size_t *imtu
, size_t *omtu
) {
995 dbus_error_init(&err
);
997 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", t
->path
, "org.bluez.MediaTransport", "Acquire"));
998 pa_assert_se(dbus_message_append_args(m
, DBUS_TYPE_STRING
, &accesstype
, DBUS_TYPE_INVALID
));
999 r
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t
->y
->connection
), m
, -1, &err
);
1001 if (dbus_error_is_set(&err
) || !r
) {
1002 dbus_error_free(&err
);
1006 if (!dbus_message_get_args(r
, &err
, DBUS_TYPE_UNIX_FD
, &ret
, DBUS_TYPE_UINT16
, &i
, DBUS_TYPE_UINT16
, &o
, DBUS_TYPE_INVALID
)) {
1007 pa_log("Failed to parse org.bluez.MediaTransport.Acquire(): %s", err
.message
);
1009 dbus_error_free(&err
);
1020 dbus_message_unref(r
);
1024 void pa_bluetooth_transport_release(pa_bluetooth_transport
*t
, const char *accesstype
) {
1031 dbus_error_init(&err
);
1033 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", t
->path
, "org.bluez.MediaTransport", "Release"));
1034 pa_assert_se(dbus_message_append_args(m
, DBUS_TYPE_STRING
, &accesstype
, DBUS_TYPE_INVALID
));
1035 dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t
->y
->connection
), m
, -1, &err
);
1037 if (dbus_error_is_set(&err
)) {
1038 pa_log("Failed to release transport %s: %s", t
->path
, err
.message
);
1039 dbus_error_free(&err
);
1041 pa_log_info("Transport %s released", t
->path
);
1044 static int setup_dbus(pa_bluetooth_discovery
*y
) {
1047 dbus_error_init(&err
);
1049 y
->connection
= pa_dbus_bus_get(y
->core
, DBUS_BUS_SYSTEM
, &err
);
1051 if (dbus_error_is_set(&err
) || !y
->connection
) {
1052 pa_log("Failed to get D-Bus connection: %s", err
.message
);
1053 dbus_error_free(&err
);
1060 static pa_bluetooth_transport
*transport_new(pa_bluetooth_discovery
*y
, const char *path
, enum profile p
, const uint8_t *config
, int size
) {
1061 pa_bluetooth_transport
*t
;
1064 t
= pa_xnew0(pa_bluetooth_transport
, 1);
1066 t
->path
= pa_xstrdup(path
);
1068 t
->config_size
= size
;
1071 t
->config
= pa_xnew(uint8_t, size
);
1072 memcpy(t
->config
, config
, size
);
1075 for (i
= 0; i
< PA_BLUETOOTH_TRANSPORT_HOOK_MAX
; i
++)
1076 pa_hook_init(&t
->hooks
[i
], t
);
1081 static DBusMessage
*endpoint_set_configuration(DBusConnection
*conn
, DBusMessage
*m
, void *userdata
) {
1082 pa_bluetooth_discovery
*y
= userdata
;
1083 pa_bluetooth_device
*d
;
1084 pa_bluetooth_transport
*t
;
1085 const char *path
, *dev_path
= NULL
, *uuid
= NULL
;
1086 uint8_t *config
= NULL
;
1088 pa_bool_t nrec
= FALSE
;
1090 DBusMessageIter args
, props
;
1093 dbus_message_iter_init(m
, &args
);
1095 dbus_message_iter_get_basic(&args
, &path
);
1096 if (!dbus_message_iter_next(&args
))
1099 dbus_message_iter_recurse(&args
, &props
);
1100 if (dbus_message_iter_get_arg_type(&props
) != DBUS_TYPE_DICT_ENTRY
)
1103 /* Read transport properties */
1104 while (dbus_message_iter_get_arg_type(&props
) == DBUS_TYPE_DICT_ENTRY
) {
1106 DBusMessageIter value
, entry
;
1109 dbus_message_iter_recurse(&props
, &entry
);
1110 dbus_message_iter_get_basic(&entry
, &key
);
1112 dbus_message_iter_next(&entry
);
1113 dbus_message_iter_recurse(&entry
, &value
);
1115 var
= dbus_message_iter_get_arg_type(&value
);
1116 if (strcasecmp(key
, "UUID") == 0) {
1117 if (var
!= DBUS_TYPE_STRING
)
1119 dbus_message_iter_get_basic(&value
, &uuid
);
1120 } else if (strcasecmp(key
, "Device") == 0) {
1121 if (var
!= DBUS_TYPE_OBJECT_PATH
)
1123 dbus_message_iter_get_basic(&value
, &dev_path
);
1124 } else if (strcasecmp(key
, "NREC") == 0) {
1125 dbus_bool_t tmp_boolean
;
1126 if (var
!= DBUS_TYPE_BOOLEAN
)
1128 dbus_message_iter_get_basic(&value
, &tmp_boolean
);
1130 } else if (strcasecmp(key
, "Configuration") == 0) {
1131 DBusMessageIter array
;
1132 if (var
!= DBUS_TYPE_ARRAY
)
1134 dbus_message_iter_recurse(&value
, &array
);
1135 dbus_message_iter_get_fixed_array(&array
, &config
, &size
);
1138 dbus_message_iter_next(&props
);
1141 d
= found_device(y
, dev_path
);
1145 if (dbus_message_has_path(m
, HFP_AG_ENDPOINT
))
1147 else if (dbus_message_has_path(m
, HFP_HS_ENDPOINT
))
1149 else if (dbus_message_has_path(m
, A2DP_SOURCE_ENDPOINT
))
1152 p
= PROFILE_A2DP_SOURCE
;
1154 t
= transport_new(y
, path
, p
, config
, size
);
1157 pa_hashmap_put(d
->transports
, t
->path
, t
);
1159 pa_log_debug("Transport %s profile %d available", t
->path
, t
->profile
);
1161 pa_assert_se(r
= dbus_message_new_method_return(m
));
1166 pa_log("org.bluez.MediaEndpoint.SetConfiguration: invalid arguments");
1167 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1168 "Unable to set configuration")));
1172 static DBusMessage
*endpoint_clear_configuration(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1173 pa_bluetooth_discovery
*y
= userdata
;
1174 pa_bluetooth_device
*d
;
1175 pa_bluetooth_transport
*t
;
1181 dbus_error_init(&e
);
1183 if (!dbus_message_get_args(m
, &e
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
1184 pa_log("org.bluez.MediaEndpoint.ClearConfiguration: %s", e
.message
);
1185 dbus_error_free(&e
);
1189 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
))) {
1190 if ((t
= pa_hashmap_get(d
->transports
, path
))) {
1191 pa_log_debug("Clearing transport %s profile %d", t
->path
, t
->profile
);
1192 pa_hashmap_remove(d
->transports
, t
->path
);
1193 pa_hook_fire(&t
->hooks
[PA_BLUETOOTH_TRANSPORT_HOOK_REMOVED
], NULL
);
1199 pa_assert_se(r
= dbus_message_new_method_return(m
));
1204 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1205 "Unable to clear configuration")));
1209 static uint8_t a2dp_default_bitpool(uint8_t freq
, uint8_t mode
) {
1212 case SBC_SAMPLING_FREQ_16000
:
1213 case SBC_SAMPLING_FREQ_32000
:
1216 case SBC_SAMPLING_FREQ_44100
:
1219 case SBC_CHANNEL_MODE_MONO
:
1220 case SBC_CHANNEL_MODE_DUAL_CHANNEL
:
1223 case SBC_CHANNEL_MODE_STEREO
:
1224 case SBC_CHANNEL_MODE_JOINT_STEREO
:
1228 pa_log_warn("Invalid channel mode %u", mode
);
1232 case SBC_SAMPLING_FREQ_48000
:
1235 case SBC_CHANNEL_MODE_MONO
:
1236 case SBC_CHANNEL_MODE_DUAL_CHANNEL
:
1239 case SBC_CHANNEL_MODE_STEREO
:
1240 case SBC_CHANNEL_MODE_JOINT_STEREO
:
1244 pa_log_warn("Invalid channel mode %u", mode
);
1249 pa_log_warn("Invalid sampling freq %u", freq
);
1254 static DBusMessage
*endpoint_select_configuration(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1255 pa_bluetooth_discovery
*y
= userdata
;
1256 a2dp_sbc_t
*cap
, config
;
1257 uint8_t *pconf
= (uint8_t *) &config
;
1262 static const struct {
1266 { 16000U, SBC_SAMPLING_FREQ_16000
},
1267 { 32000U, SBC_SAMPLING_FREQ_32000
},
1268 { 44100U, SBC_SAMPLING_FREQ_44100
},
1269 { 48000U, SBC_SAMPLING_FREQ_48000
}
1272 dbus_error_init(&e
);
1274 if (!dbus_message_get_args(m
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_BYTE
, &cap
, &size
, DBUS_TYPE_INVALID
)) {
1275 pa_log("org.bluez.MediaEndpoint.SelectConfiguration: %s", e
.message
);
1276 dbus_error_free(&e
);
1280 if (dbus_message_has_path(m
, HFP_AG_ENDPOINT
) || dbus_message_has_path(m
, HFP_HS_ENDPOINT
))
1283 pa_assert(size
== sizeof(config
));
1285 memset(&config
, 0, sizeof(config
));
1287 /* Find the lowest freq that is at least as high as the requested
1289 for (i
= 0; (unsigned) i
< PA_ELEMENTSOF(freq_table
); i
++)
1290 if (freq_table
[i
].rate
>= y
->core
->default_sample_spec
.rate
&& (cap
->frequency
& freq_table
[i
].cap
)) {
1291 config
.frequency
= freq_table
[i
].cap
;
1295 if ((unsigned) i
== PA_ELEMENTSOF(freq_table
)) {
1296 for (--i
; i
>= 0; i
--) {
1297 if (cap
->frequency
& freq_table
[i
].cap
) {
1298 config
.frequency
= freq_table
[i
].cap
;
1304 pa_log("Not suitable sample rate");
1309 pa_assert((unsigned) i
< PA_ELEMENTSOF(freq_table
));
1311 if (y
->core
->default_sample_spec
.channels
<= 1) {
1312 if (cap
->channel_mode
& SBC_CHANNEL_MODE_MONO
)
1313 config
.channel_mode
= SBC_CHANNEL_MODE_MONO
;
1316 if (y
->core
->default_sample_spec
.channels
>= 2) {
1317 if (cap
->channel_mode
& SBC_CHANNEL_MODE_JOINT_STEREO
)
1318 config
.channel_mode
= SBC_CHANNEL_MODE_JOINT_STEREO
;
1319 else if (cap
->channel_mode
& SBC_CHANNEL_MODE_STEREO
)
1320 config
.channel_mode
= SBC_CHANNEL_MODE_STEREO
;
1321 else if (cap
->channel_mode
& SBC_CHANNEL_MODE_DUAL_CHANNEL
)
1322 config
.channel_mode
= SBC_CHANNEL_MODE_DUAL_CHANNEL
;
1323 else if (cap
->channel_mode
& SBC_CHANNEL_MODE_MONO
) {
1324 config
.channel_mode
= SBC_CHANNEL_MODE_MONO
;
1326 pa_log("No supported channel modes");
1331 if (cap
->block_length
& SBC_BLOCK_LENGTH_16
)
1332 config
.block_length
= SBC_BLOCK_LENGTH_16
;
1333 else if (cap
->block_length
& SBC_BLOCK_LENGTH_12
)
1334 config
.block_length
= SBC_BLOCK_LENGTH_12
;
1335 else if (cap
->block_length
& SBC_BLOCK_LENGTH_8
)
1336 config
.block_length
= SBC_BLOCK_LENGTH_8
;
1337 else if (cap
->block_length
& SBC_BLOCK_LENGTH_4
)
1338 config
.block_length
= SBC_BLOCK_LENGTH_4
;
1340 pa_log_error("No supported block lengths");
1344 if (cap
->subbands
& SBC_SUBBANDS_8
)
1345 config
.subbands
= SBC_SUBBANDS_8
;
1346 else if (cap
->subbands
& SBC_SUBBANDS_4
)
1347 config
.subbands
= SBC_SUBBANDS_4
;
1349 pa_log_error("No supported subbands");
1353 if (cap
->allocation_method
& SBC_ALLOCATION_LOUDNESS
)
1354 config
.allocation_method
= SBC_ALLOCATION_LOUDNESS
;
1355 else if (cap
->allocation_method
& SBC_ALLOCATION_SNR
)
1356 config
.allocation_method
= SBC_ALLOCATION_SNR
;
1358 config
.min_bitpool
= (uint8_t) PA_MAX(MIN_BITPOOL
, cap
->min_bitpool
);
1359 config
.max_bitpool
= (uint8_t) PA_MIN(a2dp_default_bitpool(config
.frequency
, config
.channel_mode
), cap
->max_bitpool
);
1362 pa_assert_se(r
= dbus_message_new_method_return(m
));
1364 pa_assert_se(dbus_message_append_args(
1366 DBUS_TYPE_ARRAY
, DBUS_TYPE_BYTE
, &pconf
, size
,
1367 DBUS_TYPE_INVALID
));
1372 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1373 "Unable to select configuration")));
1377 static DBusHandlerResult
endpoint_handler(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1378 struct pa_bluetooth_discovery
*y
= userdata
;
1379 DBusMessage
*r
= NULL
;
1385 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
1386 dbus_message_get_interface(m
),
1387 dbus_message_get_path(m
),
1388 dbus_message_get_member(m
));
1390 path
= dbus_message_get_path(m
);
1391 dbus_error_init(&e
);
1393 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
))
1394 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1396 if (dbus_message_is_method_call(m
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1397 const char *xml
= ENDPOINT_INTROSPECT_XML
;
1399 pa_assert_se(r
= dbus_message_new_method_return(m
));
1400 pa_assert_se(dbus_message_append_args(
1402 DBUS_TYPE_STRING
, &xml
,
1403 DBUS_TYPE_INVALID
));
1405 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "SetConfiguration")) {
1406 r
= endpoint_set_configuration(c
, m
, userdata
);
1407 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "SelectConfiguration")) {
1408 r
= endpoint_select_configuration(c
, m
, userdata
);
1409 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "ClearConfiguration"))
1410 r
= endpoint_clear_configuration(c
, m
, userdata
);
1412 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1415 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y
->connection
), r
, NULL
));
1416 dbus_message_unref(r
);
1419 return DBUS_HANDLER_RESULT_HANDLED
;
1422 pa_bluetooth_discovery
* pa_bluetooth_discovery_get(pa_core
*c
) {
1424 pa_bluetooth_discovery
*y
;
1425 static const DBusObjectPathVTable vtable_endpoint
= {
1426 .message_function
= endpoint_handler
,
1431 dbus_error_init(&err
);
1433 if ((y
= pa_shared_get(c
, "bluetooth-discovery")))
1434 return pa_bluetooth_discovery_ref(y
);
1436 y
= pa_xnew0(pa_bluetooth_discovery
, 1);
1439 y
->devices
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
1440 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
->pending
);
1441 pa_hook_init(&y
->hook
, y
);
1442 pa_shared_set(c
, "bluetooth-discovery", y
);
1444 if (setup_dbus(y
) < 0)
1447 /* dynamic detection of bluetooth audio devices */
1448 if (!dbus_connection_add_filter(pa_dbus_connection_get(y
->connection
), filter_cb
, y
, NULL
)) {
1449 pa_log_error("Failed to add filter function");
1452 y
->filter_added
= TRUE
;
1454 if (pa_dbus_add_matches(
1455 pa_dbus_connection_get(y
->connection
), &err
,
1456 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1457 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1458 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1459 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1460 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1461 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1462 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1463 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1464 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1465 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1466 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
1468 pa_log("Failed to add D-Bus matches: %s", err
.message
);
1472 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), HFP_AG_ENDPOINT
, &vtable_endpoint
, y
));
1473 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), HFP_HS_ENDPOINT
, &vtable_endpoint
, y
));
1474 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SOURCE_ENDPOINT
, &vtable_endpoint
, y
));
1475 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SINK_ENDPOINT
, &vtable_endpoint
, y
));
1484 pa_bluetooth_discovery_unref(y
);
1486 dbus_error_free(&err
);
1491 pa_bluetooth_discovery
* pa_bluetooth_discovery_ref(pa_bluetooth_discovery
*y
) {
1493 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1500 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery
*y
) {
1502 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1504 if (PA_REFCNT_DEC(y
) > 0)
1507 pa_dbus_free_pending_list(&y
->pending
);
1510 remove_all_devices(y
);
1511 pa_hashmap_free(y
->devices
, NULL
, NULL
);
1514 if (y
->connection
) {
1515 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), HFP_AG_ENDPOINT
);
1516 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), HFP_HS_ENDPOINT
);
1517 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SOURCE_ENDPOINT
);
1518 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SINK_ENDPOINT
);
1519 pa_dbus_remove_matches(pa_dbus_connection_get(y
->connection
),
1520 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1521 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1522 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
1523 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1524 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1525 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1526 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1527 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1528 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1529 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1530 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1531 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
1534 if (y
->filter_added
)
1535 dbus_connection_remove_filter(pa_dbus_connection_get(y
->connection
), filter_cb
, y
);
1537 pa_dbus_connection_unref(y
->connection
);
1540 pa_hook_done(&y
->hook
);
1543 pa_shared_remove(y
->core
, "bluetooth-discovery");
1548 void pa_bluetooth_discovery_sync(pa_bluetooth_discovery
*y
) {
1550 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1552 pa_dbus_sync_pending_list(&y
->pending
);
1555 pa_hook
* pa_bluetooth_discovery_hook(pa_bluetooth_discovery
*y
) {
1557 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1562 const char*pa_bluetooth_get_form_factor(uint32_t class) {
1566 static const char * const table
[] = {
1577 if (((class >> 8) & 31) != 4)
1580 if ((i
= (class >> 2) & 63) > PA_ELEMENTSOF(table
))
1586 pa_log_debug("Unknown Bluetooth minor device class %u", i
);
1591 char *pa_bluetooth_cleanup_name(const char *name
) {
1593 pa_bool_t space
= FALSE
;
1597 while ((*name
>= 1 && *name
<= 32) || *name
>= 127)
1600 t
= pa_xstrdup(name
);
1602 for (s
= d
= t
; *s
; s
++) {
1604 if (*s
<= 32 || *s
>= 127 || *s
== '_') {
1622 pa_bool_t
pa_bluetooth_uuid_has(pa_bluetooth_uuid
*uuids
, const char *uuid
) {
1626 if (strcasecmp(uuids
->uuid
, uuid
) == 0)
1629 uuids
= uuids
->next
;