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 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
);
78 static pa_bt_audio_state_t
pa_bt_audio_state_from_string(const char* value
) {
81 if (pa_streq(value
, "disconnected"))
82 return PA_BT_AUDIO_STATE_DISCONNECTED
;
83 else if (pa_streq(value
, "connecting"))
84 return PA_BT_AUDIO_STATE_CONNECTING
;
85 else if (pa_streq(value
, "connected"))
86 return PA_BT_AUDIO_STATE_CONNECTED
;
87 else if (pa_streq(value
, "playing"))
88 return PA_BT_AUDIO_STATE_PLAYING
;
90 return PA_BT_AUDIO_STATE_INVALID
;
93 static pa_bluetooth_uuid
*uuid_new(const char *uuid
) {
96 u
= pa_xnew(pa_bluetooth_uuid
, 1);
97 u
->uuid
= pa_xstrdup(uuid
);
98 PA_LLIST_INIT(pa_bluetooth_uuid
, u
);
103 static void uuid_free(pa_bluetooth_uuid
*u
) {
110 static pa_bluetooth_device
* device_new(const char *path
) {
111 pa_bluetooth_device
*d
;
113 d
= pa_xnew(pa_bluetooth_device
, 1);
117 d
->device_info_valid
= 0;
120 d
->path
= pa_xstrdup(path
);
121 d
->transports
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
124 d
->device_connected
= -1;
125 PA_LLIST_HEAD_INIT(pa_bluetooth_uuid
, d
->uuids
);
130 d
->audio_state
= PA_BT_AUDIO_STATE_INVALID
;
131 d
->audio_sink_state
= PA_BT_AUDIO_STATE_INVALID
;
132 d
->audio_source_state
= PA_BT_AUDIO_STATE_INVALID
;
133 d
->headset_state
= PA_BT_AUDIO_STATE_INVALID
;
134 d
->hfgw_state
= PA_BT_AUDIO_STATE_INVALID
;
139 static void transport_free(pa_bluetooth_transport
*t
) {
147 static void device_free(pa_bluetooth_device
*d
) {
148 pa_bluetooth_uuid
*u
;
149 pa_bluetooth_transport
*t
;
153 while ((t
= pa_hashmap_steal_first(d
->transports
)))
156 pa_hashmap_free(d
->transports
, NULL
, NULL
);
158 while ((u
= d
->uuids
)) {
159 PA_LLIST_REMOVE(pa_bluetooth_uuid
, d
->uuids
, u
);
166 pa_xfree(d
->address
);
170 static pa_bool_t
device_is_audio(pa_bluetooth_device
*d
) {
174 d
->device_info_valid
&& (d
->hfgw_state
!= PA_BT_AUDIO_STATE_INVALID
||
175 (d
->audio_state
!= PA_BT_AUDIO_STATE_INVALID
&&
176 (d
->audio_sink_state
!= PA_BT_AUDIO_STATE_INVALID
||
177 d
->audio_source_state
!= PA_BT_AUDIO_STATE_INVALID
||
178 d
->headset_state
!= PA_BT_AUDIO_STATE_INVALID
)));
181 static int parse_device_property(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, DBusMessageIter
*i
) {
183 DBusMessageIter variant_i
;
189 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
190 pa_log("Property name not a string.");
194 dbus_message_iter_get_basic(i
, &key
);
196 if (!dbus_message_iter_next(i
)) {
197 pa_log("Property value missing");
201 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
202 pa_log("Property value not a variant.");
206 dbus_message_iter_recurse(i
, &variant_i
);
208 /* pa_log_debug("Parsing property org.bluez.Device.%s", key); */
210 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
212 case DBUS_TYPE_STRING
: {
215 dbus_message_iter_get_basic(&variant_i
, &value
);
217 if (pa_streq(key
, "Name")) {
219 d
->name
= pa_xstrdup(value
);
220 } else if (pa_streq(key
, "Alias")) {
222 d
->alias
= pa_xstrdup(value
);
223 } else if (pa_streq(key
, "Address")) {
224 pa_xfree(d
->address
);
225 d
->address
= pa_xstrdup(value
);
228 /* pa_log_debug("Value %s", value); */
233 case DBUS_TYPE_BOOLEAN
: {
236 dbus_message_iter_get_basic(&variant_i
, &value
);
238 if (pa_streq(key
, "Paired"))
240 else if (pa_streq(key
, "Connected"))
241 d
->device_connected
= !!value
;
242 else if (pa_streq(key
, "Trusted"))
243 d
->trusted
= !!value
;
245 /* pa_log_debug("Value %s", pa_yes_no(value)); */
250 case DBUS_TYPE_UINT32
: {
253 dbus_message_iter_get_basic(&variant_i
, &value
);
255 if (pa_streq(key
, "Class"))
256 d
->class = (int) value
;
258 /* pa_log_debug("Value %u", (unsigned) value); */
263 case DBUS_TYPE_ARRAY
: {
266 dbus_message_iter_recurse(&variant_i
, &ai
);
268 if (dbus_message_iter_get_arg_type(&ai
) == DBUS_TYPE_STRING
&&
269 pa_streq(key
, "UUIDs")) {
271 while (dbus_message_iter_get_arg_type(&ai
) != DBUS_TYPE_INVALID
) {
272 pa_bluetooth_uuid
*node
;
276 dbus_message_iter_get_basic(&ai
, &value
);
277 node
= uuid_new(value
);
278 PA_LLIST_PREPEND(pa_bluetooth_uuid
, d
->uuids
, node
);
280 /* Vudentz said the interfaces are here when the UUIDs are announced */
281 if (strcasecmp(HSP_AG_UUID
, value
) == 0 || strcasecmp(HFP_AG_UUID
, value
) == 0) {
282 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.HandsfreeGateway", "GetProperties"));
283 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
284 } else if (strcasecmp(HSP_HS_UUID
, value
) == 0 || strcasecmp(HFP_HS_UUID
, value
) == 0) {
285 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.Headset", "GetProperties"));
286 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
287 } else if (strcasecmp(A2DP_SINK_UUID
, value
) == 0) {
288 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.AudioSink", "GetProperties"));
289 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
290 } else if (strcasecmp(A2DP_SOURCE_UUID
, value
) == 0) {
291 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.AudioSource", "GetProperties"));
292 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
295 /* 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 */
296 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", d
->path
, "org.bluez.Audio", "GetProperties"));
297 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
299 if (!dbus_message_iter_next(&ai
))
311 static int parse_audio_property(pa_bluetooth_discovery
*u
, int *state
, DBusMessageIter
*i
) {
313 DBusMessageIter variant_i
;
319 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
320 pa_log("Property name not a string.");
324 dbus_message_iter_get_basic(i
, &key
);
326 if (!dbus_message_iter_next(i
)) {
327 pa_log("Property value missing");
331 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
332 pa_log("Property value not a variant.");
336 dbus_message_iter_recurse(i
, &variant_i
);
338 /* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|AudioSource|Headset}.%s", key); */
340 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
342 case DBUS_TYPE_STRING
: {
345 dbus_message_iter_get_basic(&variant_i
, &value
);
347 if (pa_streq(key
, "State")) {
348 *state
= pa_bt_audio_state_from_string(value
);
349 pa_log_debug("dbus: property 'State' changed to value '%s'", value
);
359 static void run_callback(pa_bluetooth_discovery
*y
, pa_bluetooth_device
*d
, pa_bool_t dead
) {
363 if (!device_is_audio(d
))
367 pa_hook_fire(&y
->hook
, d
);
370 static void remove_all_devices(pa_bluetooth_discovery
*y
) {
371 pa_bluetooth_device
*d
;
375 while ((d
= pa_hashmap_steal_first(y
->devices
))) {
376 run_callback(y
, d
, TRUE
);
381 static pa_bluetooth_device
*found_device(pa_bluetooth_discovery
*y
, const char* path
) {
383 pa_bluetooth_device
*d
;
388 d
= pa_hashmap_get(y
->devices
, path
);
392 d
= device_new(path
);
394 pa_hashmap_put(y
->devices
, d
->path
, d
);
396 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Device", "GetProperties"));
397 send_and_add_to_pending(y
, m
, get_properties_reply
, d
);
399 /* Before we read the other properties (Audio, AudioSink, AudioSource,
400 * Headset) we wait that the UUID is read */
404 static void get_properties_reply(DBusPendingCall
*pending
, void *userdata
) {
406 DBusMessageIter arg_i
, element_i
;
408 pa_bluetooth_device
*d
;
409 pa_bluetooth_discovery
*y
;
412 pa_assert_se(p
= userdata
);
413 pa_assert_se(y
= p
->context_data
);
414 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
416 /* pa_log_debug("Got %s.GetProperties response for %s", */
417 /* dbus_message_get_interface(p->message), */
418 /* dbus_message_get_path(p->message)); */
420 /* We don't use p->call_data here right-away since the device
421 * might already be invalidated at this point */
423 if (!(d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(p
->message
))))
426 pa_assert(p
->call_data
== d
);
428 valid
= dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
? -1 : 1;
430 if (dbus_message_is_method_call(p
->message
, "org.bluez.Device", "GetProperties"))
431 d
->device_info_valid
= valid
;
433 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
434 pa_log_debug("Bluetooth daemon is apparently not available.");
435 remove_all_devices(y
);
439 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
441 if (!dbus_message_is_error(r
, DBUS_ERROR_UNKNOWN_METHOD
))
442 pa_log("Error from GetProperties reply: %s", dbus_message_get_error_name(r
));
447 if (!dbus_message_iter_init(r
, &arg_i
)) {
448 pa_log("GetProperties reply has no arguments.");
452 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
453 pa_log("GetProperties argument is not an array.");
457 dbus_message_iter_recurse(&arg_i
, &element_i
);
458 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
460 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
461 DBusMessageIter dict_i
;
463 dbus_message_iter_recurse(&element_i
, &dict_i
);
465 if (dbus_message_has_interface(p
->message
, "org.bluez.Device")) {
466 if (parse_device_property(y
, d
, &dict_i
) < 0)
469 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Audio")) {
470 if (parse_audio_property(y
, &d
->audio_state
, &dict_i
) < 0)
473 } else if (dbus_message_has_interface(p
->message
, "org.bluez.Headset")) {
474 if (parse_audio_property(y
, &d
->headset_state
, &dict_i
) < 0)
477 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSink")) {
478 if (parse_audio_property(y
, &d
->audio_sink_state
, &dict_i
) < 0)
481 } else if (dbus_message_has_interface(p
->message
, "org.bluez.AudioSource")) {
482 if (parse_audio_property(y
, &d
->audio_source_state
, &dict_i
) < 0)
485 } else if (dbus_message_has_interface(p
->message
, "org.bluez.HandsfreeGateway")) {
486 if (parse_audio_property(y
, &d
->hfgw_state
, &arg_i
) < 0)
492 if (!dbus_message_iter_next(&element_i
))
497 run_callback(y
, d
, FALSE
);
500 dbus_message_unref(r
);
502 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
503 pa_dbus_pending_free(p
);
506 static pa_dbus_pending
* send_and_add_to_pending(pa_bluetooth_discovery
*y
, DBusMessage
*m
, DBusPendingCallNotifyFunction func
, void *call_data
) {
508 DBusPendingCall
*call
;
513 pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y
->connection
), m
, &call
, -1));
515 p
= pa_dbus_pending_new(pa_dbus_connection_get(y
->connection
), m
, call
, y
, call_data
);
516 PA_LLIST_PREPEND(pa_dbus_pending
, y
->pending
, p
);
517 dbus_pending_call_set_notify(call
, func
, p
, NULL
);
522 static void register_endpoint_reply(DBusPendingCall
*pending
, void *userdata
) {
526 pa_bluetooth_discovery
*y
;
533 pa_assert_se(p
= userdata
);
534 pa_assert_se(y
= p
->context_data
);
535 pa_assert_se(endpoint
= p
->call_data
);
536 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
538 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
539 pa_log_debug("Bluetooth daemon is apparently not available.");
540 remove_all_devices(y
);
544 if (dbus_message_is_error(r
, PA_BLUETOOTH_ERROR_NOT_SUPPORTED
)) {
545 pa_log_info("Couldn't register endpoint %s, because BlueZ is configured to disable the endpoint type.", endpoint
);
549 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
550 pa_log("Error from RegisterEndpoint reply: %s", dbus_message_get_error_name(r
));
555 dbus_message_unref(r
);
557 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
558 pa_dbus_pending_free(p
);
563 static void list_devices_reply(DBusPendingCall
*pending
, void *userdata
) {
569 pa_bluetooth_discovery
*y
;
575 pa_assert_se(p
= userdata
);
576 pa_assert_se(y
= p
->context_data
);
577 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
579 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
580 pa_log_debug("Bluetooth daemon is apparently not available.");
581 remove_all_devices(y
);
585 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
586 pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r
));
590 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_OBJECT_PATH
, &paths
, &num
, DBUS_TYPE_INVALID
)) {
591 pa_log("org.bluez.Adapter.ListDevices returned an error: '%s'\n", e
.message
);
596 for (i
= 0; i
< num
; ++i
)
597 found_device(y
, paths
[i
]);
602 dbus_free_string_array(paths
);
604 dbus_message_unref(r
);
606 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
607 pa_dbus_pending_free(p
);
610 static void register_endpoint(pa_bluetooth_discovery
*y
, const char *path
, const char *endpoint
, const char *uuid
) {
612 DBusMessageIter i
, d
;
615 pa_log_debug("Registering %s on adapter %s.", endpoint
, path
);
617 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Media", "RegisterEndpoint"));
619 dbus_message_iter_init_append(m
, &i
);
621 dbus_message_iter_append_basic(&i
, DBUS_TYPE_OBJECT_PATH
, &endpoint
);
623 dbus_message_iter_open_container(&i
, DBUS_TYPE_ARRAY
, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
624 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING
,
627 pa_dbus_append_basic_variant_dict_entry(&d
, "UUID", DBUS_TYPE_STRING
, &uuid
);
629 pa_dbus_append_basic_variant_dict_entry(&d
, "Codec", DBUS_TYPE_BYTE
, &codec
);
631 if (pa_streq(uuid
, HFP_AG_UUID
)) {
632 uint8_t capability
= 0;
633 pa_dbus_append_basic_array_variant_dict_entry(&d
, "Capabilities", DBUS_TYPE_BYTE
, &capability
, 1);
635 a2dp_sbc_t capabilities
;
637 capabilities
.channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
| BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
|
638 BT_A2DP_CHANNEL_MODE_STEREO
| BT_A2DP_CHANNEL_MODE_JOINT_STEREO
;
639 capabilities
.frequency
= BT_SBC_SAMPLING_FREQ_16000
| BT_SBC_SAMPLING_FREQ_32000
|
640 BT_SBC_SAMPLING_FREQ_44100
| BT_SBC_SAMPLING_FREQ_48000
;
641 capabilities
.allocation_method
= BT_A2DP_ALLOCATION_SNR
| BT_A2DP_ALLOCATION_LOUDNESS
;
642 capabilities
.subbands
= BT_A2DP_SUBBANDS_4
| BT_A2DP_SUBBANDS_8
;
643 capabilities
.block_length
= BT_A2DP_BLOCK_LENGTH_4
| BT_A2DP_BLOCK_LENGTH_8
|
644 BT_A2DP_BLOCK_LENGTH_12
| BT_A2DP_BLOCK_LENGTH_16
;
645 capabilities
.min_bitpool
= MIN_BITPOOL
;
646 capabilities
.max_bitpool
= MAX_BITPOOL
;
648 pa_dbus_append_basic_array_variant_dict_entry(&d
, "Capabilities", DBUS_TYPE_BYTE
, &capabilities
, sizeof(capabilities
));
651 dbus_message_iter_close_container(&i
, &d
);
653 send_and_add_to_pending(y
, m
, register_endpoint_reply
, pa_xstrdup(endpoint
));
656 static void found_adapter(pa_bluetooth_discovery
*y
, const char *path
) {
659 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", path
, "org.bluez.Adapter", "ListDevices"));
660 send_and_add_to_pending(y
, m
, list_devices_reply
, NULL
);
662 register_endpoint(y
, path
, HFP_AG_ENDPOINT
, HFP_AG_UUID
);
663 register_endpoint(y
, path
, A2DP_SOURCE_ENDPOINT
, A2DP_SOURCE_UUID
);
664 register_endpoint(y
, path
, A2DP_SINK_ENDPOINT
, A2DP_SINK_UUID
);
667 static void list_adapters_reply(DBusPendingCall
*pending
, void *userdata
) {
673 pa_bluetooth_discovery
*y
;
679 pa_assert_se(p
= userdata
);
680 pa_assert_se(y
= p
->context_data
);
681 pa_assert_se(r
= dbus_pending_call_steal_reply(pending
));
683 if (dbus_message_is_error(r
, DBUS_ERROR_SERVICE_UNKNOWN
)) {
684 pa_log_debug("Bluetooth daemon is apparently not available.");
685 remove_all_devices(y
);
689 if (dbus_message_get_type(r
) == DBUS_MESSAGE_TYPE_ERROR
) {
690 pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r
));
694 if (!dbus_message_get_args(r
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_OBJECT_PATH
, &paths
, &num
, DBUS_TYPE_INVALID
)) {
695 pa_log("org.bluez.Manager.ListAdapters returned an error: %s", e
.message
);
700 for (i
= 0; i
< num
; ++i
)
701 found_adapter(y
, paths
[i
]);
706 dbus_free_string_array(paths
);
708 dbus_message_unref(r
);
710 PA_LLIST_REMOVE(pa_dbus_pending
, y
->pending
, p
);
711 pa_dbus_pending_free(p
);
714 static void list_adapters(pa_bluetooth_discovery
*y
) {
718 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
719 send_and_add_to_pending(y
, m
, list_adapters_reply
, NULL
);
722 int pa_bluetooth_transport_parse_property(pa_bluetooth_transport
*t
, DBusMessageIter
*i
)
725 DBusMessageIter variant_i
;
727 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_STRING
) {
728 pa_log("Property name not a string.");
732 dbus_message_iter_get_basic(i
, &key
);
734 if (!dbus_message_iter_next(i
)) {
735 pa_log("Property value missing");
739 if (dbus_message_iter_get_arg_type(i
) != DBUS_TYPE_VARIANT
) {
740 pa_log("Property value not a variant.");
744 dbus_message_iter_recurse(i
, &variant_i
);
746 switch (dbus_message_iter_get_arg_type(&variant_i
)) {
748 case DBUS_TYPE_BOOLEAN
: {
751 dbus_message_iter_get_basic(&variant_i
, &value
);
753 if (pa_streq(key
, "NREC"))
763 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*m
, void *userdata
) {
765 pa_bluetooth_discovery
*y
;
770 pa_assert_se(y
= userdata
);
772 dbus_error_init(&err
);
774 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
775 dbus_message_get_interface(m
),
776 dbus_message_get_path(m
),
777 dbus_message_get_member(m
));
779 if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceRemoved")) {
781 pa_bluetooth_device
*d
;
783 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
784 pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err
.message
);
788 pa_log_debug("Device %s removed", path
);
790 if ((d
= pa_hashmap_remove(y
->devices
, path
))) {
791 run_callback(y
, d
, TRUE
);
795 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
797 } else if (dbus_message_is_signal(m
, "org.bluez.Adapter", "DeviceCreated")) {
800 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
801 pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err
.message
);
805 pa_log_debug("Device %s created", path
);
807 found_device(y
, path
);
808 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
810 } else if (dbus_message_is_signal(m
, "org.bluez.Manager", "AdapterAdded")) {
813 if (!dbus_message_get_args(m
, &err
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
814 pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err
.message
);
818 pa_log_debug("Adapter %s created", path
);
820 found_adapter(y
, path
);
821 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
823 } else if (dbus_message_is_signal(m
, "org.bluez.Audio", "PropertyChanged") ||
824 dbus_message_is_signal(m
, "org.bluez.Headset", "PropertyChanged") ||
825 dbus_message_is_signal(m
, "org.bluez.AudioSink", "PropertyChanged") ||
826 dbus_message_is_signal(m
, "org.bluez.AudioSource", "PropertyChanged") ||
827 dbus_message_is_signal(m
, "org.bluez.HandsfreeGateway", "PropertyChanged") ||
828 dbus_message_is_signal(m
, "org.bluez.Device", "PropertyChanged")) {
830 pa_bluetooth_device
*d
;
832 if ((d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(m
)))) {
833 DBusMessageIter arg_i
;
835 if (!dbus_message_iter_init(m
, &arg_i
)) {
836 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
840 if (dbus_message_has_interface(m
, "org.bluez.Device")) {
841 if (parse_device_property(y
, d
, &arg_i
) < 0)
844 } else if (dbus_message_has_interface(m
, "org.bluez.Audio")) {
845 if (parse_audio_property(y
, &d
->audio_state
, &arg_i
) < 0)
848 } else if (dbus_message_has_interface(m
, "org.bluez.Headset")) {
849 if (parse_audio_property(y
, &d
->headset_state
, &arg_i
) < 0)
852 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSink")) {
853 if (parse_audio_property(y
, &d
->audio_sink_state
, &arg_i
) < 0)
856 } else if (dbus_message_has_interface(m
, "org.bluez.AudioSource")) {
857 if (parse_audio_property(y
, &d
->audio_source_state
, &arg_i
) < 0)
860 } else if (dbus_message_has_interface(m
, "org.bluez.HandsfreeGateway")) {
861 if (parse_audio_property(y
, &d
->hfgw_state
, &arg_i
) < 0)
865 run_callback(y
, d
, FALSE
);
868 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
870 } else if (dbus_message_is_signal(m
, "org.bluez.Device", "DisconnectRequested")) {
871 pa_bluetooth_device
*d
;
873 if ((d
= pa_hashmap_get(y
->devices
, dbus_message_get_path(m
)))) {
874 /* Device will disconnect in 2 sec */
875 d
->audio_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
876 d
->audio_sink_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
877 d
->audio_source_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
878 d
->headset_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
879 d
->hfgw_state
= PA_BT_AUDIO_STATE_DISCONNECTED
;
881 run_callback(y
, d
, FALSE
);
884 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
886 } else if (dbus_message_is_signal(m
, "org.freedesktop.DBus", "NameOwnerChanged")) {
887 const char *name
, *old_owner
, *new_owner
;
889 if (!dbus_message_get_args(m
, &err
,
890 DBUS_TYPE_STRING
, &name
,
891 DBUS_TYPE_STRING
, &old_owner
,
892 DBUS_TYPE_STRING
, &new_owner
,
893 DBUS_TYPE_INVALID
)) {
894 pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err
.message
);
898 if (pa_streq(name
, "org.bluez")) {
899 if (old_owner
&& *old_owner
) {
900 pa_log_debug("Bluetooth daemon disappeared.");
901 remove_all_devices(y
);
904 if (new_owner
&& *new_owner
) {
905 pa_log_debug("Bluetooth daemon appeared.");
910 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
911 } else if (dbus_message_is_signal(m
, "org.bluez.MediaTransport", "PropertyChanged")) {
912 pa_bluetooth_device
*d
;
913 pa_bluetooth_transport
*t
= NULL
;
915 DBusMessageIter arg_i
;
917 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
918 if ((t
= pa_hashmap_get(d
->transports
, dbus_message_get_path(m
))))
924 if (!dbus_message_iter_init(m
, &arg_i
)) {
925 pa_log("Failed to parse PropertyChanged: %s", err
.message
);
929 if (pa_bluetooth_transport_parse_property(t
, &arg_i
) < 0)
932 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
936 dbus_error_free(&err
);
938 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
941 const pa_bluetooth_device
* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery
*y
, const char* address
) {
942 pa_bluetooth_device
*d
;
946 pa_assert(PA_REFCNT_VALUE(y
) > 0);
949 if (!pa_hook_is_firing(&y
->hook
))
950 pa_bluetooth_discovery_sync(y
);
952 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
953 if (pa_streq(d
->address
, address
))
954 return device_is_audio(d
) ? d
: NULL
;
959 const pa_bluetooth_device
* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery
*y
, const char* path
) {
960 pa_bluetooth_device
*d
;
963 pa_assert(PA_REFCNT_VALUE(y
) > 0);
966 if (!pa_hook_is_firing(&y
->hook
))
967 pa_bluetooth_discovery_sync(y
);
969 if ((d
= pa_hashmap_get(y
->devices
, path
)))
970 if (device_is_audio(d
))
976 const pa_bluetooth_transport
* pa_bluetooth_discovery_get_transport(pa_bluetooth_discovery
*y
, const char *path
) {
977 pa_bluetooth_device
*d
;
978 pa_bluetooth_transport
*t
;
982 pa_assert(PA_REFCNT_VALUE(y
) > 0);
985 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
)))
986 if ((t
= pa_hashmap_get(d
->transports
, path
)))
992 const pa_bluetooth_transport
* pa_bluetooth_device_get_transport(const pa_bluetooth_device
*d
, enum profile profile
) {
993 pa_bluetooth_transport
*t
;
998 while ((t
= pa_hashmap_iterate(d
->transports
, &state
, NULL
)))
999 if (t
->profile
== profile
)
1005 int pa_bluetooth_transport_acquire(const pa_bluetooth_transport
*t
, const char *accesstype
, size_t *imtu
, size_t *omtu
) {
1014 dbus_error_init(&err
);
1016 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", t
->path
, "org.bluez.MediaTransport", "Acquire"));
1017 pa_assert_se(dbus_message_append_args(m
, DBUS_TYPE_STRING
, &accesstype
, DBUS_TYPE_INVALID
));
1018 r
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t
->y
->connection
), m
, -1, &err
);
1020 if (dbus_error_is_set(&err
) || !r
) {
1021 pa_log("Failed to acquire transport fd: %s", err
.message
);
1022 dbus_error_free(&err
);
1026 if (!dbus_message_get_args(r
, &err
, DBUS_TYPE_UNIX_FD
, &ret
, DBUS_TYPE_UINT16
, &i
, DBUS_TYPE_UINT16
, &o
, DBUS_TYPE_INVALID
)) {
1027 pa_log("Failed to parse org.bluez.MediaTransport.Acquire(): %s", err
.message
);
1029 dbus_error_free(&err
);
1040 dbus_message_unref(r
);
1044 void pa_bluetooth_transport_release(const pa_bluetooth_transport
*t
, const char *accesstype
) {
1051 dbus_error_init(&err
);
1053 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", t
->path
, "org.bluez.MediaTransport", "Release"));
1054 pa_assert_se(dbus_message_append_args(m
, DBUS_TYPE_STRING
, &accesstype
, DBUS_TYPE_INVALID
));
1055 dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t
->y
->connection
), m
, -1, &err
);
1057 if (dbus_error_is_set(&err
)) {
1058 pa_log("Failed to release transport %s: %s", t
->path
, err
.message
);
1059 dbus_error_free(&err
);
1061 pa_log_info("Transport %s released", t
->path
);
1064 static int setup_dbus(pa_bluetooth_discovery
*y
) {
1067 dbus_error_init(&err
);
1069 y
->connection
= pa_dbus_bus_get(y
->core
, DBUS_BUS_SYSTEM
, &err
);
1071 if (dbus_error_is_set(&err
) || !y
->connection
) {
1072 pa_log("Failed to get D-Bus connection: %s", err
.message
);
1073 dbus_error_free(&err
);
1080 static pa_bluetooth_transport
*transport_new(pa_bluetooth_discovery
*y
, const char *path
, enum profile p
, const uint8_t *config
, int size
) {
1081 pa_bluetooth_transport
*t
;
1083 t
= pa_xnew0(pa_bluetooth_transport
, 1);
1085 t
->path
= pa_xstrdup(path
);
1087 t
->config_size
= size
;
1090 t
->config
= pa_xnew(uint8_t, size
);
1091 memcpy(t
->config
, config
, size
);
1097 static DBusMessage
*endpoint_set_configuration(DBusConnection
*conn
, DBusMessage
*m
, void *userdata
) {
1098 pa_bluetooth_discovery
*y
= userdata
;
1099 pa_bluetooth_device
*d
;
1100 pa_bluetooth_transport
*t
;
1101 const char *path
, *dev_path
= NULL
, *uuid
= NULL
;
1102 uint8_t *config
= NULL
;
1104 pa_bool_t nrec
= FALSE
;
1106 DBusMessageIter args
, props
;
1109 dbus_message_iter_init(m
, &args
);
1111 dbus_message_iter_get_basic(&args
, &path
);
1112 if (!dbus_message_iter_next(&args
))
1115 dbus_message_iter_recurse(&args
, &props
);
1116 if (dbus_message_iter_get_arg_type(&props
) != DBUS_TYPE_DICT_ENTRY
)
1119 /* Read transport properties */
1120 while (dbus_message_iter_get_arg_type(&props
) == DBUS_TYPE_DICT_ENTRY
) {
1122 DBusMessageIter value
, entry
;
1125 dbus_message_iter_recurse(&props
, &entry
);
1126 dbus_message_iter_get_basic(&entry
, &key
);
1128 dbus_message_iter_next(&entry
);
1129 dbus_message_iter_recurse(&entry
, &value
);
1131 var
= dbus_message_iter_get_arg_type(&value
);
1132 if (strcasecmp(key
, "UUID") == 0) {
1133 if (var
!= DBUS_TYPE_STRING
)
1135 dbus_message_iter_get_basic(&value
, &uuid
);
1136 } else if (strcasecmp(key
, "Device") == 0) {
1137 if (var
!= DBUS_TYPE_OBJECT_PATH
)
1139 dbus_message_iter_get_basic(&value
, &dev_path
);
1140 } else if (strcasecmp(key
, "NREC") == 0) {
1141 if (var
!= DBUS_TYPE_BOOLEAN
)
1143 dbus_message_iter_get_basic(&value
, &nrec
);
1144 } else if (strcasecmp(key
, "Configuration") == 0) {
1145 DBusMessageIter array
;
1146 if (var
!= DBUS_TYPE_ARRAY
)
1148 dbus_message_iter_recurse(&value
, &array
);
1149 dbus_message_iter_get_fixed_array(&array
, &config
, &size
);
1152 dbus_message_iter_next(&props
);
1155 d
= found_device(y
, dev_path
);
1159 if (dbus_message_has_path(m
, HFP_AG_ENDPOINT
))
1161 else if (dbus_message_has_path(m
, A2DP_SOURCE_ENDPOINT
))
1164 p
= PROFILE_A2DP_SOURCE
;
1166 t
= transport_new(y
, path
, p
, config
, size
);
1169 pa_hashmap_put(d
->transports
, t
->path
, t
);
1171 pa_log_debug("Transport %s profile %d available", t
->path
, t
->profile
);
1173 pa_assert_se(r
= dbus_message_new_method_return(m
));
1178 pa_log("org.bluez.MediaEndpoint.SetConfiguration: invalid arguments");
1179 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1180 "Unable to set configuration")));
1184 static DBusMessage
*endpoint_clear_configuration(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1185 pa_bluetooth_discovery
*y
= userdata
;
1186 pa_bluetooth_device
*d
;
1187 pa_bluetooth_transport
*t
;
1193 dbus_error_init(&e
);
1195 if (!dbus_message_get_args(m
, &e
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
1196 pa_log("org.bluez.MediaEndpoint.ClearConfiguration: %s", e
.message
);
1197 dbus_error_free(&e
);
1201 while ((d
= pa_hashmap_iterate(y
->devices
, &state
, NULL
))) {
1202 if ((t
= pa_hashmap_get(d
->transports
, path
))) {
1203 pa_log_debug("Clearing transport %s profile %d", t
->path
, t
->profile
);
1204 pa_hashmap_remove(d
->transports
, t
->path
);
1210 pa_assert_se(r
= dbus_message_new_method_return(m
));
1215 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1216 "Unable to clear configuration")));
1220 static uint8_t a2dp_default_bitpool(uint8_t freq
, uint8_t mode
) {
1223 case BT_SBC_SAMPLING_FREQ_16000
:
1224 case BT_SBC_SAMPLING_FREQ_32000
:
1227 case BT_SBC_SAMPLING_FREQ_44100
:
1230 case BT_A2DP_CHANNEL_MODE_MONO
:
1231 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
1234 case BT_A2DP_CHANNEL_MODE_STEREO
:
1235 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
1239 pa_log_warn("Invalid channel mode %u", mode
);
1243 case BT_SBC_SAMPLING_FREQ_48000
:
1246 case BT_A2DP_CHANNEL_MODE_MONO
:
1247 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
:
1250 case BT_A2DP_CHANNEL_MODE_STEREO
:
1251 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO
:
1255 pa_log_warn("Invalid channel mode %u", mode
);
1260 pa_log_warn("Invalid sampling freq %u", freq
);
1265 static DBusMessage
*endpoint_select_configuration(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1266 pa_bluetooth_discovery
*y
= userdata
;
1267 a2dp_sbc_t
*cap
, config
;
1268 uint8_t *pconf
= (uint8_t *) &config
;
1273 static const struct {
1277 { 16000U, BT_SBC_SAMPLING_FREQ_16000
},
1278 { 32000U, BT_SBC_SAMPLING_FREQ_32000
},
1279 { 44100U, BT_SBC_SAMPLING_FREQ_44100
},
1280 { 48000U, BT_SBC_SAMPLING_FREQ_48000
}
1283 dbus_error_init(&e
);
1285 if (!dbus_message_get_args(m
, &e
, DBUS_TYPE_ARRAY
, DBUS_TYPE_BYTE
, &cap
, &size
, DBUS_TYPE_INVALID
)) {
1286 pa_log("org.bluez.MediaEndpoint.SelectConfiguration: %s", e
.message
);
1287 dbus_error_free(&e
);
1291 if (dbus_message_has_path(m
, HFP_AG_ENDPOINT
))
1294 pa_assert(size
== sizeof(config
));
1296 memset(&config
, 0, sizeof(config
));
1298 /* Find the lowest freq that is at least as high as the requested
1300 for (i
= 0; (unsigned) i
< PA_ELEMENTSOF(freq_table
); i
++)
1301 if (freq_table
[i
].rate
>= y
->core
->default_sample_spec
.rate
&& (cap
->frequency
& freq_table
[i
].cap
)) {
1302 config
.frequency
= freq_table
[i
].cap
;
1306 if ((unsigned) i
== PA_ELEMENTSOF(freq_table
)) {
1307 for (--i
; i
>= 0; i
--) {
1308 if (cap
->frequency
& freq_table
[i
].cap
) {
1309 config
.frequency
= freq_table
[i
].cap
;
1315 pa_log("Not suitable sample rate");
1320 pa_assert((unsigned) i
< PA_ELEMENTSOF(freq_table
));
1322 if (y
->core
->default_sample_spec
.channels
<= 1) {
1323 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
)
1324 config
.channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
;
1327 if (y
->core
->default_sample_spec
.channels
>= 2) {
1328 if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_JOINT_STEREO
)
1329 config
.channel_mode
= BT_A2DP_CHANNEL_MODE_JOINT_STEREO
;
1330 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_STEREO
)
1331 config
.channel_mode
= BT_A2DP_CHANNEL_MODE_STEREO
;
1332 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
)
1333 config
.channel_mode
= BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL
;
1334 else if (cap
->channel_mode
& BT_A2DP_CHANNEL_MODE_MONO
) {
1335 config
.channel_mode
= BT_A2DP_CHANNEL_MODE_MONO
;
1337 pa_log("No supported channel modes");
1342 if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_16
)
1343 config
.block_length
= BT_A2DP_BLOCK_LENGTH_16
;
1344 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_12
)
1345 config
.block_length
= BT_A2DP_BLOCK_LENGTH_12
;
1346 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_8
)
1347 config
.block_length
= BT_A2DP_BLOCK_LENGTH_8
;
1348 else if (cap
->block_length
& BT_A2DP_BLOCK_LENGTH_4
)
1349 config
.block_length
= BT_A2DP_BLOCK_LENGTH_4
;
1351 pa_log_error("No supported block lengths");
1355 if (cap
->subbands
& BT_A2DP_SUBBANDS_8
)
1356 config
.subbands
= BT_A2DP_SUBBANDS_8
;
1357 else if (cap
->subbands
& BT_A2DP_SUBBANDS_4
)
1358 config
.subbands
= BT_A2DP_SUBBANDS_4
;
1360 pa_log_error("No supported subbands");
1364 if (cap
->allocation_method
& BT_A2DP_ALLOCATION_LOUDNESS
)
1365 config
.allocation_method
= BT_A2DP_ALLOCATION_LOUDNESS
;
1366 else if (cap
->allocation_method
& BT_A2DP_ALLOCATION_SNR
)
1367 config
.allocation_method
= BT_A2DP_ALLOCATION_SNR
;
1369 config
.min_bitpool
= (uint8_t) PA_MAX(MIN_BITPOOL
, cap
->min_bitpool
);
1370 config
.max_bitpool
= (uint8_t) PA_MIN(a2dp_default_bitpool(config
.frequency
, config
.channel_mode
), cap
->max_bitpool
);
1373 pa_assert_se(r
= dbus_message_new_method_return(m
));
1375 pa_assert_se(dbus_message_append_args(
1377 DBUS_TYPE_ARRAY
, DBUS_TYPE_BYTE
, &pconf
, size
,
1378 DBUS_TYPE_INVALID
));
1383 pa_assert_se(r
= (dbus_message_new_error(m
, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1384 "Unable to select configuration")));
1388 static DBusHandlerResult
endpoint_handler(DBusConnection
*c
, DBusMessage
*m
, void *userdata
) {
1389 struct pa_bluetooth_discovery
*y
= userdata
;
1390 DBusMessage
*r
= NULL
;
1396 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
1397 dbus_message_get_interface(m
),
1398 dbus_message_get_path(m
),
1399 dbus_message_get_member(m
));
1401 path
= dbus_message_get_path(m
);
1402 dbus_error_init(&e
);
1404 if (!pa_streq(path
, A2DP_SOURCE_ENDPOINT
) && !pa_streq(path
, A2DP_SINK_ENDPOINT
) && !pa_streq(path
, HFP_AG_ENDPOINT
))
1405 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1407 if (dbus_message_is_method_call(m
, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1408 const char *xml
= ENDPOINT_INTROSPECT_XML
;
1410 pa_assert_se(r
= dbus_message_new_method_return(m
));
1411 pa_assert_se(dbus_message_append_args(
1413 DBUS_TYPE_STRING
, &xml
,
1414 DBUS_TYPE_INVALID
));
1416 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "SetConfiguration")) {
1417 r
= endpoint_set_configuration(c
, m
, userdata
);
1418 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "SelectConfiguration")) {
1419 r
= endpoint_select_configuration(c
, m
, userdata
);
1420 } else if (dbus_message_is_method_call(m
, "org.bluez.MediaEndpoint", "ClearConfiguration"))
1421 r
= endpoint_clear_configuration(c
, m
, userdata
);
1423 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
1426 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y
->connection
), r
, NULL
));
1427 dbus_message_unref(r
);
1430 return DBUS_HANDLER_RESULT_HANDLED
;
1433 pa_bluetooth_discovery
* pa_bluetooth_discovery_get(pa_core
*c
) {
1435 pa_bluetooth_discovery
*y
;
1436 static const DBusObjectPathVTable vtable_endpoint
= {
1437 .message_function
= endpoint_handler
,
1442 dbus_error_init(&err
);
1444 if ((y
= pa_shared_get(c
, "bluetooth-discovery")))
1445 return pa_bluetooth_discovery_ref(y
);
1447 y
= pa_xnew0(pa_bluetooth_discovery
, 1);
1450 y
->devices
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
1451 PA_LLIST_HEAD_INIT(pa_dbus_pending
, y
->pending
);
1452 pa_hook_init(&y
->hook
, y
);
1453 pa_shared_set(c
, "bluetooth-discovery", y
);
1455 if (setup_dbus(y
) < 0)
1458 /* dynamic detection of bluetooth audio devices */
1459 if (!dbus_connection_add_filter(pa_dbus_connection_get(y
->connection
), filter_cb
, y
, NULL
)) {
1460 pa_log_error("Failed to add filter function");
1463 y
->filter_added
= TRUE
;
1465 if (pa_dbus_add_matches(
1466 pa_dbus_connection_get(y
->connection
), &err
,
1467 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1468 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1469 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1470 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1471 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1472 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'",
1473 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1474 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1475 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1476 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1477 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1478 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
1480 pa_log("Failed to add D-Bus matches: %s", err
.message
);
1484 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), HFP_AG_ENDPOINT
, &vtable_endpoint
, y
));
1485 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SOURCE_ENDPOINT
, &vtable_endpoint
, y
));
1486 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SINK_ENDPOINT
, &vtable_endpoint
, y
));
1495 pa_bluetooth_discovery_unref(y
);
1497 dbus_error_free(&err
);
1502 pa_bluetooth_discovery
* pa_bluetooth_discovery_ref(pa_bluetooth_discovery
*y
) {
1504 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1511 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery
*y
) {
1513 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1515 if (PA_REFCNT_DEC(y
) > 0)
1518 pa_dbus_free_pending_list(&y
->pending
);
1521 remove_all_devices(y
);
1522 pa_hashmap_free(y
->devices
, NULL
, NULL
);
1525 if (y
->connection
) {
1526 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), HFP_AG_ENDPOINT
);
1527 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SOURCE_ENDPOINT
);
1528 dbus_connection_unregister_object_path(pa_dbus_connection_get(y
->connection
), A2DP_SINK_ENDPOINT
);
1529 pa_dbus_remove_matches(pa_dbus_connection_get(y
->connection
),
1530 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1531 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1532 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
1533 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1534 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1535 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1536 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'",
1537 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1538 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1539 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1540 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1541 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1542 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
1545 if (y
->filter_added
)
1546 dbus_connection_remove_filter(pa_dbus_connection_get(y
->connection
), filter_cb
, y
);
1548 pa_dbus_connection_unref(y
->connection
);
1551 pa_hook_done(&y
->hook
);
1554 pa_shared_remove(y
->core
, "bluetooth-discovery");
1559 void pa_bluetooth_discovery_sync(pa_bluetooth_discovery
*y
) {
1561 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1563 pa_dbus_sync_pending_list(&y
->pending
);
1566 pa_hook
* pa_bluetooth_discovery_hook(pa_bluetooth_discovery
*y
) {
1568 pa_assert(PA_REFCNT_VALUE(y
) > 0);
1573 const char*pa_bluetooth_get_form_factor(uint32_t class) {
1577 static const char * const table
[] = {
1588 if (((class >> 8) & 31) != 4)
1591 if ((i
= (class >> 2) & 63) > PA_ELEMENTSOF(table
))
1597 pa_log_debug("Unknown Bluetooth minor device class %u", i
);
1602 char *pa_bluetooth_cleanup_name(const char *name
) {
1604 pa_bool_t space
= FALSE
;
1608 while ((*name
>= 1 && *name
<= 32) || *name
>= 127)
1611 t
= pa_xstrdup(name
);
1613 for (s
= d
= t
; *s
; s
++) {
1615 if (*s
<= 32 || *s
>= 127 || *s
== '_') {
1633 pa_bool_t
pa_bluetooth_uuid_has(pa_bluetooth_uuid
*uuids
, const char *uuid
) {
1637 if (strcasecmp(uuids
->uuid
, uuid
) == 0)
1640 uuids
= uuids
->next
;