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"
34 #include "a2dp-codecs.h"
36 #define HFP_AG_ENDPOINT "/MediaEndpoint/HFPAG"
37 #define HFP_HS_ENDPOINT "/MediaEndpoint/HFPHS"
38 #define A2DP_SOURCE_ENDPOINT "/MediaEndpoint/A2DPSource"
39 #define A2DP_SINK_ENDPOINT "/MediaEndpoint/A2DPSink"
41 #define ENDPOINT_INTROSPECT_XML \
42 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
44 " <interface name=\"org.bluez.MediaEndpoint\">" \
45 " <method name=\"SetConfiguration\">" \
46 " <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
47 " <arg name=\"configuration\" direction=\"in\" type=\"ay\"/>" \
49 " <method name=\"SelectConfiguration\">" \
50 " <arg name=\"capabilities\" direction=\"in\" type=\"ay\"/>" \
51 " <arg name=\"configuration\" direction=\"out\" type=\"ay\"/>" \
53 " <method name=\"ClearConfiguration\">" \
55 " <method name=\"Release\">" \
58 " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
59 " <method name=\"Introspect\">" \
60 " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
65 struct pa_bluetooth_discovery
{
69 pa_dbus_connection
*connection
;
70 PA_LLIST_HEAD(pa_dbus_pending
, pending
);
73 pa_bool_t filter_added
;
76 static void get_properties_reply(DBusPendingCall
*pending
, void *userdata
);
77 static pa_dbus_pending
* send_and_add_to_pending(pa_bluetooth_discovery
*y
, DBusMessage
*m
, DBusPendingCallNotifyFunction func
, void *call_data
);
79 pa_bt_audio_state_t
pa_bt_audio_state_from_string(const char* value
) {
82 if (pa_streq(value
, "disconnected"))
83 return PA_BT_AUDIO_STATE_DISCONNECTED
;
84 else if (pa_streq(value
, "connecting"))
85 return PA_BT_AUDIO_STATE_CONNECTING
;
86 else if (pa_streq(value
, "connected"))
87 return PA_BT_AUDIO_STATE_CONNECTED
;
88 else if (pa_streq(value
, "playing"))
89 return PA_BT_AUDIO_STATE_PLAYING
;
91 return PA_BT_AUDIO_STATE_INVALID
;
94 static pa_bluetooth_uuid
*uuid_new(const char *uuid
) {
97 u
= pa_xnew(pa_bluetooth_uuid
, 1);
98 u
->uuid
= pa_xstrdup(uuid
);
99 PA_LLIST_INIT(pa_bluetooth_uuid
, u
);
104 static void uuid_free(pa_bluetooth_uuid
*u
) {
111 static pa_bluetooth_device
* device_new(const char *path
) {
112 pa_bluetooth_device
*d
;
114 d
= pa_xnew(pa_bluetooth_device
, 1);
118 d
->device_info_valid
= 0;
121 d
->path
= pa_xstrdup(path
);
122 d
->transports
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
125 d
->device_connected
= -1;
126 PA_LLIST_HEAD_INIT(pa_bluetooth_uuid
, d
->uuids
);
131 d
->audio_state
= PA_BT_AUDIO_STATE_INVALID
;
132 d
->audio_sink_state
= PA_BT_AUDIO_STATE_INVALID
;
133 d
->audio_source_state
= PA_BT_AUDIO_STATE_INVALID
;
134 d
->headset_state
= PA_BT_AUDIO_STATE_INVALID
;
135 d
->hfgw_state
= PA_BT_AUDIO_STATE_INVALID
;
140 static void transport_free(pa_bluetooth_transport
*t
) {
145 for (i
= 0; i
< PA_BLUETOOTH_TRANSPORT_HOOK_MAX
; i
++)
146 pa_hook_done(&t
->hooks
[i
]);
153 static void device_free(pa_bluetooth_device
*d
) {
154 pa_bluetooth_uuid
*u
;
155 pa_bluetooth_transport
*t
;
159 while ((t
= pa_hashmap_steal_first(d
->transports
)))
162 pa_hashmap_free(d
->transports
, NULL
, NULL
);
164 while ((u
= d
->uuids
)) {
165 PA_LLIST_REMOVE(pa_bluetooth_uuid
, d
->uuids
, u
);
172 pa_xfree(d
->address
);
176 static pa_bool_t
device_is_audio(pa_bluetooth_device
*d
) {
180 d
->device_info_valid
&& (d
->hfgw_state
!= PA_BT_AUDIO_STATE_INVALID
||
181 (d
->audio_state
!= PA_BT_AUDIO_STATE_INVALID
&&
182 (d
->audio_sink_state
!= PA_BT_AUDIO_STATE_INVALID
||
183 d
->audio_source_state
!= PA_BT_AUDIO_STATE_INVALID
||
184 d
->headset_state
!= PA_BT_AUDIO_STATE_INVALID
)));
187 static int parse_device_property(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessageIter
*i
) {
189 DBusMessageIter variant_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.");
212 dbus_message_iter_recurse(i
, &variant_i
);
214 /* pa_log_debug("Parsing property org.bluez.Device.%s", key); */
216 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
218 case DBUS_TYPE_STRING
: {
221 dbus_message_iter_get_basic(&variant_i
, &value
);
223 if (pa_streq(key
, "Name")) {
225 d
->name
= pa_xstrdup(value
);
226 } else if (pa_streq(key
, "Alias")) {
228 d
->alias
= pa_xstrdup(value
);
229 } else if (pa_streq(key
, "Address")) {
230 pa_xfree(d
->address
);
231 d
->address
= pa_xstrdup(value
);
234 /* pa_log_debug("Value %s", value); */
239 case DBUS_TYPE_BOOLEAN
: {
242 dbus_message_iter_get_basic(&variant_i
, &value
);
244 if (pa_streq(key
, "Paired"))
246 else if (pa_streq(key
, "Connected"))
247 d
->device_connected
= !!value
;
248 else if (pa_streq(key
, "Trusted"))
249 d
->trusted
= !!value
;
251 /* pa_log_debug("Value %s", pa_yes_no(value)); */
256 case DBUS_TYPE_UINT32
: {
259 dbus_message_iter_get_basic(&variant_i
, &value
);
261 if (pa_streq(key
, "Class"))
262 d
->class = (int) value
;
264 /* pa_log_debug("Value %u", (unsigned) value); */
269 case DBUS_TYPE_ARRAY
: {
272 dbus_message_iter_recurse(&variant_i
, &ai
);
274 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_STRING
&&
275 pa_streq(key
, "UUIDs")) {
277 pa_bool_t has_audio
= FALSE
;
279 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
280 pa_bluetooth_uuid
*node
;
283 dbus_message_iter_get_basic(&ai
, &value
);
284 node
= uuid_new(value
);
285 PA_LLIST_PREPEND(pa_bluetooth_uuid
, d
->uuids
, node
);
287 /* Vudentz said the interfaces are here when the UUIDs are announced */
288 if (strcasecmp(HSP_AG_UUID
, value
) == 0 || strcasecmp(HFP_AG_UUID
, value
) == 0) {
289 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.HandsfreeGateway", "GetProperties"));
290 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
292 } else if (strcasecmp(HSP_HS_UUID
, value
) == 0 || strcasecmp(HFP_HS_UUID
, value
) == 0) {
293 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.Headset", "GetProperties"));
294 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
296 } else if (strcasecmp(A2DP_SINK_UUID
, value
) == 0) {
297 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.AudioSink", "GetProperties"));
298 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
300 } else if (strcasecmp(A2DP_SOURCE_UUID
, value
) == 0) {
301 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.AudioSource", "GetProperties"));
302 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
306 if (!dbus_message_iter_next(&ai
))
310 /* 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 */
312 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.Audio", "GetProperties"));
313 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
324 static int parse_audio_property(pa_bluetooth_discovery
*u
, int *state
, DBusMessageIter
*i
) {
326 DBusMessageIter variant_i
;
332 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
333 pa_log("Property name not a string.");
337 dbus_message_iter_get_basic(i
, &key
);
339 if (!dbus_message_iter_next(i
)) {
340 pa_log("Property value missing");
344 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
345 pa_log("Property value not a variant.");
349 dbus_message_iter_recurse(i
, &variant_i
);
351 /* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|AudioSource|Headset}.%s", key); */
353 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
355 case DBUS_TYPE_STRING
: {
358 dbus_message_iter_get_basic(&variant_i
, &value
);
360 if (pa_streq(key
, "State")) {
361 *state
= pa_bt_audio_state_from_string(value
);
362 pa_log_debug("dbus: property 'State' changed to value '%s'", value
);
372 static void run_callback(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, pa_bool_t dead
) {
376 if (!device_is_audio(d
))
380 pa_hook_fire(&y
->hook
, d
);
383 static void remove_all_devices(pa_bluetooth_discovery
*y
) {
384 pa_bluetooth_device
*d
;
388 while ((d
= pa_hashmap_steal_first(y
->devices
))) {
389 run_callback(y
, d
, TRUE
);
394 static pa_bluetooth_device
*found_device(pa_bluetooth_discovery
*y
, const char* path
) {
396 pa_bluetooth_device
*d
;
401 d
= pa_hashmap_get(y
->devices
, path
);
405 d
= device_new(path
);
407 pa_hashmap_put(y
->devices
, d
->path
, d
);
409 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Device", "GetProperties"));
410 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
412 /* Before we read the other properties (Audio, AudioSink, AudioSource,
413 * Headset) we wait that the UUID is read */
417 static void get_properties_reply(DBusPendingCall
*pending
, void *userdata
) {
419 DBusMessageIter arg_i
, element_i
;
421 pa_bluetooth_device
*d
;
422 pa_bluetooth_discovery
*y
;
425 pa_assert_se(p
= userdata
);
426 pa_assert_se(y
= p
->context_data
);
427 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
429 /* pa_log_debug("Got %s.GetProperties response for %s", */
430 /* dbus_message_get_interface(p->message), */
431 /* dbus_message_get_path(p->message)); */
433 /* We don't use p->call_data here right-away since the device
434 * might already be invalidated at this point */
436 if (!(d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(p
->message
))))
439 pa_assert(p
->call_data
== d
);
441 valid
= dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
? -1 : 1;
443 if (dbus_message_is_method_call(p
->message
, "org.bluez.Device", "GetProperties"))
444 d
->device_info_valid
= valid
;
446 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
447 pa_log_debug("Bluetooth daemon is apparently not available.");
448 remove_all_devices(y
);
452 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
453 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
));
457 if (!dbus_message_iter_init(r
, &arg_i
)) {
458 pa_log("GetProperties reply has no arguments.");
462 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
463 pa_log("GetProperties argument is not an array.");
467 dbus_message_iter_recurse(&arg_i
, &element_i
);
468 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
470 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
471 DBusMessageIter dict_i
;
473 dbus_message_iter_recurse(&element_i
, &dict_i
);
475 if (dbus_message_has_interface(p
->message
, "org.bluez.Device")) {
476 if (parse_device_property(y
, d
, &dict_i
) < 0)
479 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Audio")) {
480 if (parse_audio_property(y
, &d
->audio_state
, &dict_i
) < 0)
483 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Headset")) {
484 if (parse_audio_property(y
, &d
->headset_state
, &dict_i
) < 0)
487 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSink")) {
488 if (parse_audio_property(y
, &d
->audio_sink_state
, &dict_i
) < 0)
491 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSource")) {
492 if (parse_audio_property(y
, &d
->audio_source_state
, &dict_i
) < 0)
495 } else if (dbus_message_has_interface(p
->message
, "org.bluez.HandsfreeGateway")) {
496 if (parse_audio_property(y
, &d
->hfgw_state
, &dict_i
) < 0)
502 if (!dbus_message_iter_next(&element_i
))
507 run_callback(y
, d
, FALSE
);
510 dbus_message_unref(r
);
512 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
513 pa_dbus_pending_free(p
);
516 static pa_dbus_pending
* send_and_add_to_pending(pa_bluetooth_discovery
*y
, DBusMessage
*m
, DBusPendingCallNotifyFunction func
, void *call_data
) {
518 DBusPendingCall
*call
;
523 pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y
->connection
), m
, &call
, -1));
525 p
= pa_dbus_pending_new(pa_dbus_connection_get(y
->connection
), m
, call
, y
, call_data
);
526 PA_LLIST_PREPEND(pa_dbus_pending
, y
->pending
, p
);
527 dbus_pending_call_set_notify(call
, func
, p
, NULL
);
532 static void register_endpoint_reply(DBusPendingCall
*pending
, void *userdata
) {
536 pa_bluetooth_discovery
*y
;
543 pa_assert_se(p
= userdata
);
544 pa_assert_se(y
= p
->context_data
);
545 pa_assert_se(endpoint
= p
->call_data
);
546 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
548 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
549 pa_log_debug("Bluetooth daemon is apparently not available.");
550 remove_all_devices(y
);
554 if (dbus_message_is_error(r
, PA_BLUETOOTH_ERROR_NOT_SUPPORTED
)) {
555 pa_log_info("Couldn't register endpoint %s, because BlueZ is configured to disable the endpoint type.", endpoint
);
559 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
560 pa_log("org.bluez.Media.RegisterEndpoint() failed: %s: %s", dbus_message_get_error_name(r
), pa_dbus_get_error_message(r
));
565 dbus_message_unref(r
);
567 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
568 pa_dbus_pending_free(p
);
573 static void list_devices_reply(DBusPendingCall
*pending
, void *userdata
) {
579 pa_bluetooth_discovery
*y
;
585 pa_assert_se(p
= userdata
);
586 pa_assert_se(y
= p
->context_data
);
587 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
589 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
590 pa_log_debug("Bluetooth daemon is apparently not available.");
591 remove_all_devices(y
);
595 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
596 pa_log("org.bluez.Adapter.ListDevices() failed: %s: %s", dbus_message_get_error_name(r
), pa_dbus_get_error_message(r
));
600 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_OBJECT_PATH
, &paths
, &num
, DBUS_TYPE_INVALID
)) {
601 pa_log("org.bluez.Adapter.ListDevices returned an error: '%s'\n", e
.message
);
606 for (i
= 0; i
< num
; ++i
)
607 found_device(y
, paths
[i
]);
612 dbus_free_string_array(paths
);
614 dbus_message_unref(r
);
616 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
617 pa_dbus_pending_free(p
);
620 static void register_endpoint(pa_bluetooth_discovery
*y
, const char *path
, const char *endpoint
, const char *uuid
) {
622 DBusMessageIter i
, d
;
625 pa_log_debug("Registering %s on adapter %s.", endpoint
, path
);
627 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Media", "RegisterEndpoint"));
629 dbus_message_iter_init_append(m
, &i
);
631 dbus_message_iter_append_basic(&i
, DBUS_TYPE_OBJECT_PATH
, &endpoint
);
633 dbus_message_iter_open_container(&i
, DBUS_TYPE_ARRAY
, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
634 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING
,
637 pa_dbus_append_basic_variant_dict_entry(&d
, "UUID", DBUS_TYPE_STRING
, &uuid
);
639 pa_dbus_append_basic_variant_dict_entry(&d
, "Codec", DBUS_TYPE_BYTE
, &codec
);
641 if (pa_streq(uuid
, HFP_AG_UUID
) || pa_streq(uuid
, HFP_HS_UUID
)) {
642 uint8_t capability
= 0;
643 pa_dbus_append_basic_array_variant_dict_entry(&d
, "Capabilities", DBUS_TYPE_BYTE
, &capability
, 1);
645 a2dp_sbc_t capabilities
;
647 capabilities
.channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
| BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
|
648 BT_A2DP_CHANNEL_MODE_STEREO
| BT_A2DP_CHANNEL_MODE_JOINT_STEREO
;
649 capabilities
.frequency
= BT_SBC_SAMPLING_FREQ_16000
| BT_SBC_SAMPLING_FREQ_32000
|
650 BT_SBC_SAMPLING_FREQ_44100
| BT_SBC_SAMPLING_FREQ_48000
;
651 capabilities
.allocation_method
= BT_A2DP_ALLOCATION_SNR
| BT_A2DP_ALLOCATION_LOUDNESS
;
652 capabilities
.subbands
= BT_A2DP_SUBBANDS_4
| BT_A2DP_SUBBANDS_8
;
653 capabilities
.block_length
= BT_A2DP_BLOCK_LENGTH_4
| BT_A2DP_BLOCK_LENGTH_8
|
654 BT_A2DP_BLOCK_LENGTH_12
| BT_A2DP_BLOCK_LENGTH_16
;
655 capabilities
.min_bitpool
= MIN_BITPOOL
;
656 capabilities
.max_bitpool
= MAX_BITPOOL
;
658 pa_dbus_append_basic_array_variant_dict_entry(&d
, "Capabilities", DBUS_TYPE_BYTE
, &capabilities
, sizeof(capabilities
));
661 dbus_message_iter_close_container(&i
, &d
);
663 send_and_add_to_pending(y
, m
, register_endpoint_reply
, pa_xstrdup(endpoint
));
666 static void found_adapter(pa_bluetooth_discovery
*y
, const char *path
) {
669 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Adapter", "ListDevices"));
670 send_and_add_to_pending(y
, m
, list_devices_reply
, NULL
);
672 register_endpoint(y
, path
, HFP_AG_ENDPOINT
, HFP_AG_UUID
);
673 register_endpoint(y
, path
, HFP_HS_ENDPOINT
, HFP_HS_UUID
);
674 register_endpoint(y
, path
, A2DP_SOURCE_ENDPOINT
, A2DP_SOURCE_UUID
);
675 register_endpoint(y
, path
, A2DP_SINK_ENDPOINT
, A2DP_SINK_UUID
);
678 static void list_adapters_reply(DBusPendingCall
*pending
, void *userdata
) {
684 pa_bluetooth_discovery
*y
;
690 pa_assert_se(p
= userdata
);
691 pa_assert_se(y
= p
->context_data
);
692 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
694 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
695 pa_log_debug("Bluetooth daemon is apparently not available.");
696 remove_all_devices(y
);
700 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
701 pa_log("org.bluez.Manager.ListAdapters() failed: %s: %s", dbus_message_get_error_name(r
), pa_dbus_get_error_message(r
));
705 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_OBJECT_PATH
, &paths
, &num
, DBUS_TYPE_INVALID
)) {
706 pa_log("org.bluez.Manager.ListAdapters returned an error: %s", e
.message
);
711 for (i
= 0; i
< num
; ++i
)
712 found_adapter(y
, paths
[i
]);
717 dbus_free_string_array(paths
);
719 dbus_message_unref(r
);
721 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
722 pa_dbus_pending_free(p
);
725 static void list_adapters(pa_bluetooth_discovery
*y
) {
729 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
730 send_and_add_to_pending(y
, m
, list_adapters_reply
, NULL
);
733 int pa_bluetooth_transport_parse_property(pa_bluetooth_transport
*t
, DBusMessageIter
*i
)
736 DBusMessageIter variant_i
;
738 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
739 pa_log("Property name not a string.");
743 dbus_message_iter_get_basic(i
, &key
);
745 if (!dbus_message_iter_next(i
)) {
746 pa_log("Property value missing");
750 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
751 pa_log("Property value not a variant.");
755 dbus_message_iter_recurse(i
, &variant_i
);
757 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
759 case DBUS_TYPE_BOOLEAN
: {
762 dbus_message_iter_get_basic(&variant_i
, &value
);
764 if (pa_streq(key
, "NREC") && t
->nrec
!= value
) {
766 pa_log_debug("Transport %s: Property 'NREC' changed to %s.", t
->path
, t
->nrec
? "True" : "False");
767 pa_hook_fire(&t
->hooks
[PA_BLUETOOTH_TRANSPORT_HOOK_NREC_CHANGED
], NULL
);
777 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*m
, void *userdata
) {
779 pa_bluetooth_discovery
*y
;
784 pa_assert_se(y
= userdata
);
786 dbus_error_init(&err
);
788 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
789 dbus_message_get_interface(m
),
790 dbus_message_get_path(m
),
791 dbus_message_get_member(m
));
793 if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceRemoved")) {
795 pa_bluetooth_device
*d
;
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.DeviceRemoved: %s", err
.message
);
802 pa_log_debug("Device %s removed", path
);
804 if ((d
= pa_hashmap_remove(y
->devices
, path
))) {
805 run_callback(y
, d
, TRUE
);
809 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
811 } else if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceCreated")) {
814 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
815 pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err
.message
);
819 pa_log_debug("Device %s created", path
);
821 found_device(y
, path
);
822 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
824 } else if (dbus_message_is_signal(m
, "org.bluez.Manager", "AdapterAdded")) {
827 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
828 pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err
.message
);
832 pa_log_debug("Adapter %s created", path
);
834 found_adapter(y
, path
);
835 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
837 } else if (dbus_message_is_signal(m
, "org.bluez.Audio", "PropertyChanged") ||
838 dbus_message_is_signal(m
, "org.bluez.Headset", "PropertyChanged") ||
839 dbus_message_is_signal(m
, "org.bluez.AudioSink", "PropertyChanged") ||
840 dbus_message_is_signal(m
, "org.bluez.AudioSource", "PropertyChanged") ||
841 dbus_message_is_signal(m
, "org.bluez.HandsfreeGateway", "PropertyChanged") ||
842 dbus_message_is_signal(m
, "org.bluez.Device", "PropertyChanged")) {
844 pa_bluetooth_device
*d
;
846 if ((d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(m
)))) {
847 DBusMessageIter arg_i
;
849 if (!dbus_message_iter_init(m
, &arg_i
)) {
850 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
854 if (dbus_message_has_interface(m
, "org.bluez.Device")) {
855 if (parse_device_property(y
, d
, &arg_i
) < 0)
858 } else if (dbus_message_has_interface(m
, "org.bluez.Audio")) {
859 if (parse_audio_property(y
, &d
->audio_state
, &arg_i
) < 0)
862 } else if (dbus_message_has_interface(m
, "org.bluez.Headset")) {
863 if (parse_audio_property(y
, &d
->headset_state
, &arg_i
) < 0)
866 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSink")) {
867 if (parse_audio_property(y
, &d
->audio_sink_state
, &arg_i
) < 0)
870 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSource")) {
871 if (parse_audio_property(y
, &d
->audio_source_state
, &arg_i
) < 0)
874 } else if (dbus_message_has_interface(m
, "org.bluez.HandsfreeGateway")) {
875 if (parse_audio_property(y
, &d
->hfgw_state
, &arg_i
) < 0)
879 run_callback(y
, d
, FALSE
);
882 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
884 } else if (dbus_message_is_signal(m
, "org.bluez.Device", "DisconnectRequested")) {
885 pa_bluetooth_device
*d
;
887 if ((d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(m
)))) {
888 /* Device will disconnect in 2 sec */
889 d
->audio_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
890 d
->audio_sink_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
891 d
->audio_source_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
892 d
->headset_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
893 d
->hfgw_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
895 run_callback(y
, d
, FALSE
);
898 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
900 } else if (dbus_message_is_signal(m
, "org.freedesktop.DBus", "NameOwnerChanged")) {
901 const char *name
, *old_owner
, *new_owner
;
903 if (!dbus_message_get_args(m
, &err
,
904 DBUS_TYPE_STRING
, &name
,
905 DBUS_TYPE_STRING
, &old_owner
,
906 DBUS_TYPE_STRING
, &new_owner
,
907 DBUS_TYPE_INVALID
)) {
908 pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err
.message
);
912 if (pa_streq(name
, "org.bluez")) {
913 if (old_owner
&& *old_owner
) {
914 pa_log_debug("Bluetooth daemon disappeared.");
915 remove_all_devices(y
);
918 if (new_owner
&& *new_owner
) {
919 pa_log_debug("Bluetooth daemon appeared.");
924 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
925 } else if (dbus_message_is_signal(m
, "org.bluez.MediaTransport", "PropertyChanged")) {
926 pa_bluetooth_device
*d
;
927 pa_bluetooth_transport
*t
= NULL
;
929 DBusMessageIter arg_i
;
931 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
932 if ((t
= pa_hashmap_get(d
->transports
, dbus_message_get_path(m
))))
938 if (!dbus_message_iter_init(m
, &arg_i
)) {
939 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
943 if (pa_bluetooth_transport_parse_property(t
, &arg_i
) < 0)
946 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
950 dbus_error_free(&err
);
952 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
955 const pa_bluetooth_device
* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery
*y
, const char* address
) {
956 pa_bluetooth_device
*d
;
960 pa_assert(PA_REFCNT_VALUE(y
) > 0);
963 if (!pa_hook_is_firing(&y
->hook
))
964 pa_bluetooth_discovery_sync(y
);
966 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
967 if (pa_streq(d
->address
, address
))
968 return device_is_audio(d
) ? d
: NULL
;
973 const pa_bluetooth_device
* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery
*y
, const char* path
) {
974 pa_bluetooth_device
*d
;
977 pa_assert(PA_REFCNT_VALUE(y
) > 0);
980 if (!pa_hook_is_firing(&y
->hook
))
981 pa_bluetooth_discovery_sync(y
);
983 if ((d
= pa_hashmap_get(y
->devices
, path
)))
984 if (device_is_audio(d
))
990 pa_bluetooth_transport
* pa_bluetooth_discovery_get_transport(pa_bluetooth_discovery
*y
, const char *path
) {
991 pa_bluetooth_device
*d
;
992 pa_bluetooth_transport
*t
;
996 pa_assert(PA_REFCNT_VALUE(y
) > 0);
999 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
1000 if ((t
= pa_hashmap_get(d
->transports
, path
)))
1006 const pa_bluetooth_transport
* pa_bluetooth_device_get_transport(const pa_bluetooth_device
*d
, enum profile profile
) {
1007 pa_bluetooth_transport
*t
;
1012 while ((t
= pa_hashmap_iterate(d
->transports
, &state
, NULL
)))
1013 if (t
->profile
== profile
)
1019 int pa_bluetooth_transport_acquire(const pa_bluetooth_transport
*t
, const char *accesstype
, size_t *imtu
, size_t *omtu
) {
1028 dbus_error_init(&err
);
1030 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", t
->path
, "org.bluez.MediaTransport", "Acquire"));
1031 pa_assert_se(dbus_message_append_args(m
, DBUS_TYPE_STRING
, &accesstype
, DBUS_TYPE_INVALID
));
1032 r
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t
->y
->connection
), m
, -1, &err
);
1034 if (dbus_error_is_set(&err
) || !r
) {
1035 pa_log("Failed to acquire transport fd: %s", err
.message
);
1036 dbus_error_free(&err
);
1040 if (!dbus_message_get_args(r
, &err
, DBUS_TYPE_UNIX_FD
, &ret
, DBUS_TYPE_UINT16
, &i
, DBUS_TYPE_UINT16
, &o
, DBUS_TYPE_INVALID
)) {
1041 pa_log("Failed to parse org.bluez.MediaTransport.Acquire(): %s", err
.message
);
1043 dbus_error_free(&err
);
1054 dbus_message_unref(r
);
1058 void pa_bluetooth_transport_release(const pa_bluetooth_transport
*t
, const char *accesstype
) {
1065 dbus_error_init(&err
);
1067 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", t
->path
, "org.bluez.MediaTransport", "Release"));
1068 pa_assert_se(dbus_message_append_args(m
, DBUS_TYPE_STRING
, &accesstype
, DBUS_TYPE_INVALID
));
1069 dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t
->y
->connection
), m
, -1, &err
);
1071 if (dbus_error_is_set(&err
)) {
1072 pa_log("Failed to release transport %s: %s", t
->path
, err
.message
);
1073 dbus_error_free(&err
);
1075 pa_log_info("Transport %s released", t
->path
);
1078 static int setup_dbus(pa_bluetooth_discovery
*y
) {
1081 dbus_error_init(&err
);
1083 y
->connection
= pa_dbus_bus_get(y
->core
, DBUS_BUS_SYSTEM
, &err
);
1085 if (dbus_error_is_set(&err
) || !y
->connection
) {
1086 pa_log("Failed to get D-Bus connection: %s", err
.message
);
1087 dbus_error_free(&err
);
1094 static pa_bluetooth_transport
*transport_new(pa_bluetooth_discovery
*y
, const char *path
, enum profile p
, const uint8_t *config
, int size
) {
1095 pa_bluetooth_transport
*t
;
1098 t
= pa_xnew0(pa_bluetooth_transport
, 1);
1100 t
->path
= pa_xstrdup(path
);
1102 t
->config_size
= size
;
1105 t
->config
= pa_xnew(uint8_t, size
);
1106 memcpy(t
->config
, config
, size
);
1109 for (i
= 0; i
< PA_BLUETOOTH_TRANSPORT_HOOK_MAX
; i
++)
1110 pa_hook_init(&t
->hooks
[i
], t
);
1115 static DBusMessage
*endpoint_set_configuration(DBusConnection
*conn
, DBusMessage
*m
, void *userdata
) {
1116 pa_bluetooth_discovery
*y
= userdata
;
1117 pa_bluetooth_device
*d
;
1118 pa_bluetooth_transport
*t
;
1119 const char *path
, *dev_path
= NULL
, *uuid
= NULL
;
1120 uint8_t *config
= NULL
;
1122 pa_bool_t nrec
= FALSE
;
1124 DBusMessageIter args
, props
;
1127 dbus_message_iter_init(m
, &args
);
1129 dbus_message_iter_get_basic(&args
, &path
);
1130 if (!dbus_message_iter_next(&args
))
1133 dbus_message_iter_recurse(&args
, &props
);
1134 if (dbus_message_iter_get_arg_type(&props
) != DBUS_TYPE_DICT_ENTRY
)
1137 /* Read transport properties */
1138 while (dbus_message_iter_get_arg_type(&props
) == DBUS_TYPE_DICT_ENTRY
) {
1140 DBusMessageIter value
, entry
;
1143 dbus_message_iter_recurse(&props
, &entry
);
1144 dbus_message_iter_get_basic(&entry
, &key
);
1146 dbus_message_iter_next(&entry
);
1147 dbus_message_iter_recurse(&entry
, &value
);
1149 var
= dbus_message_iter_get_arg_type(&value
);
1150 if (strcasecmp(key
, "UUID") == 0) {
1151 if (var
!= DBUS_TYPE_STRING
)
1153 dbus_message_iter_get_basic(&value
, &uuid
);
1154 } else if (strcasecmp(key
, "Device") == 0) {
1155 if (var
!= DBUS_TYPE_OBJECT_PATH
)
1157 dbus_message_iter_get_basic(&value
, &dev_path
);
1158 } else if (strcasecmp(key
, "NREC") == 0) {
1159 dbus_bool_t tmp_boolean
;
1160 if (var
!= DBUS_TYPE_BOOLEAN
)
1162 dbus_message_iter_get_basic(&value
, &tmp_boolean
);
1164 } else if (strcasecmp(key
, "Configuration") == 0) {
1165 DBusMessageIter array
;
1166 if (var
!= DBUS_TYPE_ARRAY
)
1168 dbus_message_iter_recurse(&value
, &array
);
1169 dbus_message_iter_get_fixed_array(&array
, &config
, &size
);
1172 dbus_message_iter_next(&props
);
1175 d
= found_device(y
, dev_path
);
1179 if (dbus_message_has_path(m
, HFP_AG_ENDPOINT
))
1181 else if (dbus_message_has_path(m
, HFP_HS_ENDPOINT
))
1183 else if (dbus_message_has_path(m
, A2DP_SOURCE_ENDPOINT
))
1186 p
= PROFILE_A2DP_SOURCE
;
1188 t
= transport_new(y
, path
, p
, config
, size
);
1191 pa_hashmap_put(d
->transports
, t
->path
, t
);
1193 pa_log_debug("Transport %s profile %d available", t
->path
, t
->profile
);
1195 pa_assert_se(r
= dbus_message_new_method_return(m
));
1200 pa_log("org.bluez.MediaEndpoint.SetConfiguration: invalid arguments");
1201 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1202 "Unable to set configuration")));
1206 static DBusMessage
*endpoint_clear_configuration(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1207 pa_bluetooth_discovery
*y
= userdata
;
1208 pa_bluetooth_device
*d
;
1209 pa_bluetooth_transport
*t
;
1215 dbus_error_init(&e
);
1217 if (!dbus_message_get_args(m
, &e
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
1218 pa_log("org.bluez.MediaEndpoint.ClearConfiguration: %s", e
.message
);
1219 dbus_error_free(&e
);
1223 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
))) {
1224 if ((t
= pa_hashmap_get(d
->transports
, path
))) {
1225 pa_log_debug("Clearing transport %s profile %d", t
->path
, t
->profile
);
1226 pa_hashmap_remove(d
->transports
, t
->path
);
1232 pa_assert_se(r
= dbus_message_new_method_return(m
));
1237 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1238 "Unable to clear configuration")));
1242 static uint8_t a2dp_default_bitpool(uint8_t freq
, uint8_t mode
) {
1245 case BT_SBC_SAMPLING_FREQ_16000
:
1246 case BT_SBC_SAMPLING_FREQ_32000
:
1249 case BT_SBC_SAMPLING_FREQ_44100
:
1252 case BT_A2DP_CHANNEL_MODE_MONO
:
1253 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
1256 case BT_A2DP_CHANNEL_MODE_STEREO
:
1257 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
1261 pa_log_warn("Invalid channel mode %u", mode
);
1265 case BT_SBC_SAMPLING_FREQ_48000
:
1268 case BT_A2DP_CHANNEL_MODE_MONO
:
1269 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
1272 case BT_A2DP_CHANNEL_MODE_STEREO
:
1273 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
1277 pa_log_warn("Invalid channel mode %u", mode
);
1282 pa_log_warn("Invalid sampling freq %u", freq
);
1287 static DBusMessage
*endpoint_select_configuration(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1288 pa_bluetooth_discovery
*y
= userdata
;
1289 a2dp_sbc_t
*cap
, config
;
1290 uint8_t *pconf
= (uint8_t *) &config
;
1295 static const struct {
1299 { 16000U, BT_SBC_SAMPLING_FREQ_16000
},
1300 { 32000U, BT_SBC_SAMPLING_FREQ_32000
},
1301 { 44100U, BT_SBC_SAMPLING_FREQ_44100
},
1302 { 48000U, BT_SBC_SAMPLING_FREQ_48000
}
1305 dbus_error_init(&e
);
1307 if (!dbus_message_get_args(m
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_BYTE
, &cap
, &size
, DBUS_TYPE_INVALID
)) {
1308 pa_log("org.bluez.MediaEndpoint.SelectConfiguration: %s", e
.message
);
1309 dbus_error_free(&e
);
1313 if (dbus_message_has_path(m
, HFP_AG_ENDPOINT
) || dbus_message_has_path(m
, HFP_HS_ENDPOINT
))
1316 pa_assert(size
== sizeof(config
));
1318 memset(&config
, 0, sizeof(config
));
1320 /* Find the lowest freq that is at least as high as the requested
1322 for (i
= 0; (unsigned) i
< PA_ELEMENTSOF(freq_table
); i
++)
1323 if (freq_table
[i
].rate
>= y
->core
->default_sample_spec
.rate
&& (cap
->frequency
& freq_table
[i
].cap
)) {
1324 config
.frequency
= freq_table
[i
].cap
;
1328 if ((unsigned) i
== PA_ELEMENTSOF(freq_table
)) {
1329 for (--i
; i
>= 0; i
--) {
1330 if (cap
->frequency
& freq_table
[i
].cap
) {
1331 config
.frequency
= freq_table
[i
].cap
;
1337 pa_log("Not suitable sample rate");
1342 pa_assert((unsigned) i
< PA_ELEMENTSOF(freq_table
));
1344 if (y
->core
->default_sample_spec
.channels
<= 1) {
1345 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
)
1346 config
.channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
;
1349 if (y
->core
->default_sample_spec
.channels
>= 2) {
1350 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
1351 config
.channel_mode
= BT_A2DP_CHANNEL_MODE_JOINT_STEREO
;
1352 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
1353 config
.channel_mode
= BT_A2DP_CHANNEL_MODE_STEREO
;
1354 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
1355 config
.channel_mode
= BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
;
1356 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
) {
1357 config
.channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
;
1359 pa_log("No supported channel modes");
1364 if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_16
)
1365 config
.block_length
= BT_A2DP_BLOCK_LENGTH_16
;
1366 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_12
)
1367 config
.block_length
= BT_A2DP_BLOCK_LENGTH_12
;
1368 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_8
)
1369 config
.block_length
= BT_A2DP_BLOCK_LENGTH_8
;
1370 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_4
)
1371 config
.block_length
= BT_A2DP_BLOCK_LENGTH_4
;
1373 pa_log_error("No supported block lengths");
1377 if (cap
->subbands
& BT_A2DP_SUBBANDS_8
)
1378 config
.subbands
= BT_A2DP_SUBBANDS_8
;
1379 else if (cap
->subbands
& BT_A2DP_SUBBANDS_4
)
1380 config
.subbands
= BT_A2DP_SUBBANDS_4
;
1382 pa_log_error("No supported subbands");
1386 if (cap
->allocation_method
& BT_A2DP_ALLOCATION_LOUDNESS
)
1387 config
.allocation_method
= BT_A2DP_ALLOCATION_LOUDNESS
;
1388 else if (cap
->allocation_method
& BT_A2DP_ALLOCATION_SNR
)
1389 config
.allocation_method
= BT_A2DP_ALLOCATION_SNR
;
1391 config
.min_bitpool
= (uint8_t) PA_MAX(MIN_BITPOOL
, cap
->min_bitpool
);
1392 config
.max_bitpool
= (uint8_t) PA_MIN(a2dp_default_bitpool(config
.frequency
, config
.channel_mode
), cap
->max_bitpool
);
1395 pa_assert_se(r
= dbus_message_new_method_return(m
));
1397 pa_assert_se(dbus_message_append_args(
1399 DBUS_TYPE_ARRAY
, DBUS_TYPE_BYTE
, &pconf
, size
,
1400 DBUS_TYPE_INVALID
));
1405 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1406 "Unable to select configuration")));
1410 static DBusHandlerResult
endpoint_handler(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1411 struct pa_bluetooth_discovery
*y
= userdata
;
1412 DBusMessage
*r
= NULL
;
1418 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
1419 dbus_message_get_interface(m
),
1420 dbus_message_get_path(m
),
1421 dbus_message_get_member(m
));
1423 path
= dbus_message_get_path(m
);
1424 dbus_error_init(&e
);
1426 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
))
1427 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1429 if (dbus_message_is_method_call(m
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1430 const char *xml
= ENDPOINT_INTROSPECT_XML
;
1432 pa_assert_se(r
= dbus_message_new_method_return(m
));
1433 pa_assert_se(dbus_message_append_args(
1435 DBUS_TYPE_STRING
, &xml
,
1436 DBUS_TYPE_INVALID
));
1438 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "SetConfiguration")) {
1439 r
= endpoint_set_configuration(c
, m
, userdata
);
1440 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "SelectConfiguration")) {
1441 r
= endpoint_select_configuration(c
, m
, userdata
);
1442 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "ClearConfiguration"))
1443 r
= endpoint_clear_configuration(c
, m
, userdata
);
1445 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1448 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y
->connection
), r
, NULL
));
1449 dbus_message_unref(r
);
1452 return DBUS_HANDLER_RESULT_HANDLED
;
1455 pa_bluetooth_discovery
* pa_bluetooth_discovery_get(pa_core
*c
) {
1457 pa_bluetooth_discovery
*y
;
1458 static const DBusObjectPathVTable vtable_endpoint
= {
1459 .message_function
= endpoint_handler
,
1464 dbus_error_init(&err
);
1466 if ((y
= pa_shared_get(c
, "bluetooth-discovery")))
1467 return pa_bluetooth_discovery_ref(y
);
1469 y
= pa_xnew0(pa_bluetooth_discovery
, 1);
1472 y
->devices
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
1473 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
->pending
);
1474 pa_hook_init(&y
->hook
, y
);
1475 pa_shared_set(c
, "bluetooth-discovery", y
);
1477 if (setup_dbus(y
) < 0)
1480 /* dynamic detection of bluetooth audio devices */
1481 if (!dbus_connection_add_filter(pa_dbus_connection_get(y
->connection
), filter_cb
, y
, NULL
)) {
1482 pa_log_error("Failed to add filter function");
1485 y
->filter_added
= TRUE
;
1487 if (pa_dbus_add_matches(
1488 pa_dbus_connection_get(y
->connection
), &err
,
1489 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1490 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1491 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1492 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1493 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1494 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'",
1495 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1496 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1497 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1498 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1499 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1500 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
1502 pa_log("Failed to add D-Bus matches: %s", err
.message
);
1506 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), HFP_AG_ENDPOINT
, &vtable_endpoint
, y
));
1507 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), HFP_HS_ENDPOINT
, &vtable_endpoint
, y
));
1508 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SOURCE_ENDPOINT
, &vtable_endpoint
, y
));
1509 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SINK_ENDPOINT
, &vtable_endpoint
, y
));
1518 pa_bluetooth_discovery_unref(y
);
1520 dbus_error_free(&err
);
1525 pa_bluetooth_discovery
* pa_bluetooth_discovery_ref(pa_bluetooth_discovery
*y
) {
1527 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1534 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery
*y
) {
1536 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1538 if (PA_REFCNT_DEC(y
) > 0)
1541 pa_dbus_free_pending_list(&y
->pending
);
1544 remove_all_devices(y
);
1545 pa_hashmap_free(y
->devices
, NULL
, NULL
);
1548 if (y
->connection
) {
1549 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), HFP_AG_ENDPOINT
);
1550 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), HFP_HS_ENDPOINT
);
1551 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SOURCE_ENDPOINT
);
1552 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SINK_ENDPOINT
);
1553 pa_dbus_remove_matches(pa_dbus_connection_get(y
->connection
),
1554 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1555 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1556 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
1557 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1558 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1559 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1560 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'",
1561 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1562 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1563 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1564 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1565 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1566 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
1569 if (y
->filter_added
)
1570 dbus_connection_remove_filter(pa_dbus_connection_get(y
->connection
), filter_cb
, y
);
1572 pa_dbus_connection_unref(y
->connection
);
1575 pa_hook_done(&y
->hook
);
1578 pa_shared_remove(y
->core
, "bluetooth-discovery");
1583 void pa_bluetooth_discovery_sync(pa_bluetooth_discovery
*y
) {
1585 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1587 pa_dbus_sync_pending_list(&y
->pending
);
1590 pa_hook
* pa_bluetooth_discovery_hook(pa_bluetooth_discovery
*y
) {
1592 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1597 const char*pa_bluetooth_get_form_factor(uint32_t class) {
1601 static const char * const table
[] = {
1612 if (((class >> 8) & 31) != 4)
1615 if ((i
= (class >> 2) & 63) > PA_ELEMENTSOF(table
))
1621 pa_log_debug("Unknown Bluetooth minor device class %u", i
);
1626 char *pa_bluetooth_cleanup_name(const char *name
) {
1628 pa_bool_t space
= FALSE
;
1632 while ((*name
>= 1 && *name
<= 32) || *name
>= 127)
1635 t
= pa_xstrdup(name
);
1637 for (s
= d
= t
; *s
; s
++) {
1639 if (*s
<= 32 || *s
>= 127 || *s
== '_') {
1657 pa_bool_t
pa_bluetooth_uuid_has(pa_bluetooth_uuid
*uuids
, const char *uuid
) {
1661 if (strcasecmp(uuids
->uuid
, uuid
) == 0)
1664 uuids
= uuids
->next
;