2 This file is part of PulseAudio.
4 Copyright 2008-2013 João 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.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/dbus-shared.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/refcnt.h>
34 #include <pulsecore/shared.h>
36 #include "a2dp-codecs.h"
38 #include "bluez5-util.h"
40 #define BLUEZ_SERVICE "org.bluez"
41 #define BLUEZ_ADAPTER_INTERFACE BLUEZ_SERVICE ".Adapter1"
42 #define BLUEZ_DEVICE_INTERFACE BLUEZ_SERVICE ".Device1"
43 #define BLUEZ_MEDIA_INTERFACE BLUEZ_SERVICE ".Media1"
44 #define BLUEZ_MEDIA_ENDPOINT_INTERFACE BLUEZ_SERVICE ".MediaEndpoint1"
45 #define BLUEZ_MEDIA_TRANSPORT_INTERFACE BLUEZ_SERVICE ".MediaTransport1"
47 #define BLUEZ_ERROR_NOT_SUPPORTED "org.bluez.Error.NotSupported"
49 #define A2DP_SOURCE_ENDPOINT "/MediaEndpoint/A2DPSource"
50 #define A2DP_SINK_ENDPOINT "/MediaEndpoint/A2DPSink"
52 #define ENDPOINT_INTROSPECT_XML \
53 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
55 " <interface name=\"" BLUEZ_MEDIA_ENDPOINT_INTERFACE "\">" \
56 " <method name=\"SetConfiguration\">" \
57 " <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
58 " <arg name=\"properties\" direction=\"in\" type=\"ay\"/>" \
60 " <method name=\"SelectConfiguration\">" \
61 " <arg name=\"capabilities\" direction=\"in\" type=\"ay\"/>" \
62 " <arg name=\"configuration\" direction=\"out\" type=\"ay\"/>" \
64 " <method name=\"ClearConfiguration\">" \
65 " <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
67 " <method name=\"Release\">" \
70 " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
71 " <method name=\"Introspect\">" \
72 " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
77 struct pa_bluetooth_discovery {
81 pa_dbus_connection *connection;
85 pa_hook hooks[PA_BLUETOOTH_HOOK_MAX];
88 pa_hashmap *transports;
90 PA_LLIST_HEAD(pa_dbus_pending, pending);
93 static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, DBusMessage *m,
94 DBusPendingCallNotifyFunction func, void *call_data) {
96 DBusPendingCall *call;
101 pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y->connection), m, &call, -1));
103 p = pa_dbus_pending_new(pa_dbus_connection_get(y->connection), m, call, y, call_data);
104 PA_LLIST_PREPEND(pa_dbus_pending, y->pending, p);
105 dbus_pending_call_set_notify(call, func, p, NULL);
110 static const char *check_variant_property(DBusMessageIter *i) {
115 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
116 pa_log_error("Property name not a string.");
120 dbus_message_iter_get_basic(i, &key);
122 if (!dbus_message_iter_next(i)) {
123 pa_log_error("Property value missing");
127 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
128 pa_log_error("Property value not a variant.");
135 pa_bluetooth_transport *pa_bluetooth_transport_new(pa_bluetooth_device *d, const char *owner, const char *path,
136 pa_bluetooth_profile_t p, const uint8_t *config, size_t size) {
137 pa_bluetooth_transport *t;
139 t = pa_xnew0(pa_bluetooth_transport, 1);
141 t->owner = pa_xstrdup(owner);
142 t->path = pa_xstrdup(path);
144 t->config_size = size;
147 t->config = pa_xnew(uint8_t, size);
148 memcpy(t->config, config, size);
151 pa_assert_se(pa_hashmap_put(d->discovery->transports, t->path, t) >= 0);
156 static const char *transport_state_to_string(pa_bluetooth_transport_state_t state) {
158 case PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED:
159 return "disconnected";
160 case PA_BLUETOOTH_TRANSPORT_STATE_IDLE:
162 case PA_BLUETOOTH_TRANSPORT_STATE_PLAYING:
169 static void transport_state_changed(pa_bluetooth_transport *t, pa_bluetooth_transport_state_t state) {
170 bool old_any_connected;
174 if (t->state == state)
177 old_any_connected = pa_bluetooth_device_any_transport_connected(t->device);
179 pa_log_debug("Transport %s state changed from %s to %s",
180 t->path, transport_state_to_string(t->state), transport_state_to_string(state));
183 if (state == PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)
184 t->device->transports[t->profile] = NULL;
186 pa_hook_fire(&t->device->discovery->hooks[PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED], t);
188 if (old_any_connected != pa_bluetooth_device_any_transport_connected(t->device))
189 pa_hook_fire(&t->device->discovery->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], t->device);
192 void pa_bluetooth_transport_put(pa_bluetooth_transport *t) {
193 transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_IDLE);
196 void pa_bluetooth_transport_free(pa_bluetooth_transport *t) {
199 pa_hashmap_remove(t->device->discovery->transports, t->path);
206 static int bluez5_transport_acquire_cb(pa_bluetooth_transport *t, bool optional, size_t *imtu, size_t *omtu) {
211 const char *method = optional ? "TryAcquire" : "Acquire";
214 pa_assert(t->device);
215 pa_assert(t->device->discovery);
217 pa_assert_se(m = dbus_message_new_method_call(t->owner, t->path, BLUEZ_MEDIA_TRANSPORT_INTERFACE, method));
219 dbus_error_init(&err);
221 r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->device->discovery->connection), m, -1, &err);
223 if (optional && pa_streq(err.name, "org.bluez.Error.NotAvailable"))
224 pa_log_info("Failed optional acquire of unavailable transport %s", t->path);
226 pa_log_error("Transport %s() failed for transport %s (%s)", method, t->path, err.message);
228 dbus_error_free(&err);
232 if (!dbus_message_get_args(r, &err, DBUS_TYPE_UNIX_FD, &ret, DBUS_TYPE_UINT16, &i, DBUS_TYPE_UINT16, &o,
233 DBUS_TYPE_INVALID)) {
234 pa_log_error("Failed to parse %s() reply: %s", method, err.message);
235 dbus_error_free(&err);
247 dbus_message_unref(r);
251 static void bluez5_transport_release_cb(pa_bluetooth_transport *t) {
256 pa_assert(t->device);
257 pa_assert(t->device->discovery);
259 dbus_error_init(&err);
261 if (t->state <= PA_BLUETOOTH_TRANSPORT_STATE_IDLE) {
262 pa_log_info("Transport %s auto-released by BlueZ or already released", t->path);
266 pa_assert_se(m = dbus_message_new_method_call(t->owner, t->path, BLUEZ_MEDIA_TRANSPORT_INTERFACE, "Release"));
267 dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->device->discovery->connection), m, -1, &err);
269 if (dbus_error_is_set(&err)) {
270 pa_log_error("Failed to release transport %s: %s", t->path, err.message);
271 dbus_error_free(&err);
273 pa_log_info("Transport %s released", t->path);
276 bool pa_bluetooth_device_any_transport_connected(const pa_bluetooth_device *d) {
281 if (d->device_info_valid != 1)
284 for (i = 0; i < PA_BLUETOOTH_PROFILE_COUNT; i++)
285 if (d->transports[i] && d->transports[i]->state != PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)
291 static int transport_state_from_string(const char* value, pa_bluetooth_transport_state_t *state) {
295 if (pa_streq(value, "idle"))
296 *state = PA_BLUETOOTH_TRANSPORT_STATE_IDLE;
297 else if (pa_streq(value, "pending") || pa_streq(value, "active"))
298 *state = PA_BLUETOOTH_TRANSPORT_STATE_PLAYING;
305 static void parse_transport_property(pa_bluetooth_transport *t, DBusMessageIter *i) {
307 DBusMessageIter variant_i;
309 key = check_variant_property(i);
313 dbus_message_iter_recurse(i, &variant_i);
315 switch (dbus_message_iter_get_arg_type(&variant_i)) {
317 case DBUS_TYPE_STRING: {
320 dbus_message_iter_get_basic(&variant_i, &value);
322 if (pa_streq(key, "State")) {
323 pa_bluetooth_transport_state_t state;
325 if (transport_state_from_string(value, &state) < 0) {
326 pa_log_error("Invalid state received: %s", value);
330 transport_state_changed(t, state);
340 static int parse_transport_properties(pa_bluetooth_transport *t, DBusMessageIter *i) {
341 DBusMessageIter element_i;
343 dbus_message_iter_recurse(i, &element_i);
345 while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
346 DBusMessageIter dict_i;
348 dbus_message_iter_recurse(&element_i, &dict_i);
350 parse_transport_property(t, &dict_i);
352 dbus_message_iter_next(&element_i);
358 static pa_bluetooth_device* device_create(pa_bluetooth_discovery *y, const char *path) {
359 pa_bluetooth_device *d;
364 d = pa_xnew0(pa_bluetooth_device, 1);
366 d->path = pa_xstrdup(path);
367 d->uuids = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, pa_xfree);
369 pa_hashmap_put(y->devices, d->path, d);
374 pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_path(pa_bluetooth_discovery *y, const char *path) {
375 pa_bluetooth_device *d;
378 pa_assert(PA_REFCNT_VALUE(y) > 0);
381 if ((d = pa_hashmap_get(y->devices, path)))
382 if (d->device_info_valid == 1)
388 pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_address(pa_bluetooth_discovery *y, const char *remote, const char *local) {
389 pa_bluetooth_device *d;
393 pa_assert(PA_REFCNT_VALUE(y) > 0);
397 while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
398 if (pa_streq(d->address, remote) && pa_streq(d->adapter->address, local))
399 return d->device_info_valid == 1 ? d : NULL;
404 static void device_free(pa_bluetooth_device *d) {
409 for (i = 0; i < PA_BLUETOOTH_PROFILE_COUNT; i++) {
410 pa_bluetooth_transport *t;
412 if (!(t = d->transports[i]))
415 transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED);
416 pa_bluetooth_transport_free(t);
420 pa_hashmap_free(d->uuids);
426 pa_xfree(d->address);
430 static void device_remove(pa_bluetooth_discovery *y, const char *path) {
431 pa_bluetooth_device *d;
433 if (!(d = pa_hashmap_remove(y->devices, path)))
434 pa_log_warn("Unknown device removed %s", path);
436 pa_log_debug("Device %s removed", path);
441 static void device_remove_all(pa_bluetooth_discovery *y) {
442 pa_bluetooth_device *d;
446 while ((d = pa_hashmap_steal_first(y->devices))) {
447 d->device_info_valid = -1;
448 pa_hook_fire(&y->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], d);
453 static pa_bluetooth_adapter* adapter_create(pa_bluetooth_discovery *y, const char *path) {
454 pa_bluetooth_adapter *a;
459 a = pa_xnew0(pa_bluetooth_adapter, 1);
461 a->path = pa_xstrdup(path);
463 pa_hashmap_put(y->adapters, a->path, a);
468 static void adapter_free(pa_bluetooth_adapter *a) {
469 pa_bluetooth_device *d;
473 pa_assert(a->discovery);
475 PA_HASHMAP_FOREACH(d, a->discovery->devices, state)
480 pa_xfree(a->address);
484 static void adapter_remove(pa_bluetooth_discovery *y, const char *path) {
485 pa_bluetooth_adapter *a;
487 if (!(a = pa_hashmap_remove(y->adapters, path)))
488 pa_log_warn("Unknown adapter removed %s", path);
490 pa_log_debug("Adapter %s removed", path);
495 static void adapter_remove_all(pa_bluetooth_discovery *y) {
496 pa_bluetooth_adapter *a;
500 /* When this function is called all devices have already been freed */
502 while ((a = pa_hashmap_steal_first(y->adapters)))
506 static void parse_device_property(pa_bluetooth_device *d, DBusMessageIter *i, bool is_property_change) {
508 DBusMessageIter variant_i;
512 key = check_variant_property(i);
514 pa_log_error("Received invalid property for device %s", d->path);
518 dbus_message_iter_recurse(i, &variant_i);
520 switch (dbus_message_iter_get_arg_type(&variant_i)) {
522 case DBUS_TYPE_STRING: {
524 dbus_message_iter_get_basic(&variant_i, &value);
526 if (pa_streq(key, "Alias")) {
528 d->alias = pa_xstrdup(value);
529 pa_log_debug("%s: %s", key, value);
530 } else if (pa_streq(key, "Address")) {
531 if (is_property_change) {
532 pa_log_warn("Device property 'Address' expected to be constant but changed for %s, ignoring", d->path);
537 pa_log_warn("Device %s: Received a duplicate 'Address' property, ignoring", d->path);
541 d->address = pa_xstrdup(value);
542 pa_log_debug("%s: %s", key, value);
548 case DBUS_TYPE_OBJECT_PATH: {
550 dbus_message_iter_get_basic(&variant_i, &value);
552 if (pa_streq(key, "Adapter")) {
554 if (is_property_change) {
555 pa_log_warn("Device property 'Adapter' expected to be constant but changed for %s, ignoring", d->path);
559 if (d->adapter_path) {
560 pa_log_warn("Device %s: Received a duplicate 'Adapter' property, ignoring", d->path);
564 d->adapter_path = pa_xstrdup(value);
566 d->adapter = pa_hashmap_get(d->discovery->adapters, value);
568 pa_log_info("Device %s: 'Adapter' property references an unknown adapter %s.", d->path, value);
570 pa_log_debug("%s: %s", key, value);
576 case DBUS_TYPE_UINT32: {
578 dbus_message_iter_get_basic(&variant_i, &value);
580 if (pa_streq(key, "Class")) {
581 d->class_of_device = value;
582 pa_log_debug("%s: %d", key, value);
588 case DBUS_TYPE_ARRAY: {
590 dbus_message_iter_recurse(&variant_i, &ai);
592 if (dbus_message_iter_get_arg_type(&ai) == DBUS_TYPE_STRING && pa_streq(key, "UUIDs")) {
593 /* bluetoothd never removes UUIDs from a device object so there
594 * is no need to handle it here. */
595 while (dbus_message_iter_get_arg_type(&ai) != DBUS_TYPE_INVALID) {
599 dbus_message_iter_get_basic(&ai, &value);
601 if (pa_hashmap_get(d->uuids, value)) {
602 dbus_message_iter_next(&ai);
606 uuid = pa_xstrdup(value);
607 pa_hashmap_put(d->uuids, uuid, uuid);
609 pa_log_debug("%s: %s", key, value);
610 dbus_message_iter_next(&ai);
619 static int parse_device_properties(pa_bluetooth_device *d, DBusMessageIter *i, bool is_property_change) {
620 DBusMessageIter element_i;
623 dbus_message_iter_recurse(i, &element_i);
625 while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
626 DBusMessageIter dict_i;
628 dbus_message_iter_recurse(&element_i, &dict_i);
629 parse_device_property(d, &dict_i, is_property_change);
630 dbus_message_iter_next(&element_i);
633 if (!d->address || !d->adapter_path || !d->alias) {
634 pa_log_error("Non-optional information missing for device %s", d->path);
635 d->device_info_valid = -1;
639 d->device_info_valid = 1;
643 static void parse_adapter_properties(pa_bluetooth_adapter *a, DBusMessageIter *i, bool is_property_change) {
644 DBusMessageIter element_i;
648 dbus_message_iter_recurse(i, &element_i);
650 while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
651 DBusMessageIter dict_i, variant_i;
654 dbus_message_iter_recurse(&element_i, &dict_i);
656 key = check_variant_property(&dict_i);
658 pa_log_error("Received invalid property for adapter %s", a->path);
662 dbus_message_iter_recurse(&dict_i, &variant_i);
664 if (dbus_message_iter_get_arg_type(&variant_i) == DBUS_TYPE_STRING && pa_streq(key, "Address")) {
667 if (is_property_change) {
668 pa_log_warn("Adapter property 'Address' expected to be constant but changed for %s, ignoring", a->path);
673 pa_log_warn("Adapter %s received a duplicate 'Address' property, ignoring", a->path);
677 dbus_message_iter_get_basic(&variant_i, &value);
678 a->address = pa_xstrdup(value);
681 dbus_message_iter_next(&element_i);
685 static void register_endpoint_reply(DBusPendingCall *pending, void *userdata) {
688 pa_bluetooth_discovery *y;
692 pa_assert_se(p = userdata);
693 pa_assert_se(y = p->context_data);
694 pa_assert_se(endpoint = p->call_data);
695 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
697 if (dbus_message_is_error(r, BLUEZ_ERROR_NOT_SUPPORTED)) {
698 pa_log_info("Couldn't register endpoint %s because it is disabled in BlueZ", endpoint);
702 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
703 pa_log_error(BLUEZ_MEDIA_INTERFACE ".RegisterEndpoint() failed: %s: %s", dbus_message_get_error_name(r),
704 pa_dbus_get_error_message(r));
709 dbus_message_unref(r);
711 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
712 pa_dbus_pending_free(p);
717 static void register_endpoint(pa_bluetooth_discovery *y, const char *path, const char *endpoint, const char *uuid) {
719 DBusMessageIter i, d;
722 pa_log_debug("Registering %s on adapter %s", endpoint, path);
724 pa_assert_se(m = dbus_message_new_method_call(BLUEZ_SERVICE, path, BLUEZ_MEDIA_INTERFACE, "RegisterEndpoint"));
726 dbus_message_iter_init_append(m, &i);
727 dbus_message_iter_append_basic(&i, DBUS_TYPE_OBJECT_PATH, &endpoint);
728 dbus_message_iter_open_container(&i, DBUS_TYPE_ARRAY, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING
729 DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &d);
730 pa_dbus_append_basic_variant_dict_entry(&d, "UUID", DBUS_TYPE_STRING, &uuid);
731 pa_dbus_append_basic_variant_dict_entry(&d, "Codec", DBUS_TYPE_BYTE, &codec);
733 if (pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SOURCE) || pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SINK)) {
734 a2dp_sbc_t capabilities;
736 capabilities.channel_mode = SBC_CHANNEL_MODE_MONO | SBC_CHANNEL_MODE_DUAL_CHANNEL | SBC_CHANNEL_MODE_STEREO |
737 SBC_CHANNEL_MODE_JOINT_STEREO;
738 capabilities.frequency = SBC_SAMPLING_FREQ_16000 | SBC_SAMPLING_FREQ_32000 | SBC_SAMPLING_FREQ_44100 |
739 SBC_SAMPLING_FREQ_48000;
740 capabilities.allocation_method = SBC_ALLOCATION_SNR | SBC_ALLOCATION_LOUDNESS;
741 capabilities.subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8;
742 capabilities.block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 | SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16;
743 capabilities.min_bitpool = MIN_BITPOOL;
744 capabilities.max_bitpool = MAX_BITPOOL;
746 pa_dbus_append_basic_array_variant_dict_entry(&d, "Capabilities", DBUS_TYPE_BYTE, &capabilities, sizeof(capabilities));
749 dbus_message_iter_close_container(&i, &d);
751 send_and_add_to_pending(y, m, register_endpoint_reply, pa_xstrdup(endpoint));
754 static void parse_interfaces_and_properties(pa_bluetooth_discovery *y, DBusMessageIter *dict_i) {
755 DBusMessageIter element_i;
758 pa_bluetooth_device *d;
760 pa_assert(dbus_message_iter_get_arg_type(dict_i) == DBUS_TYPE_OBJECT_PATH);
761 dbus_message_iter_get_basic(dict_i, &path);
763 pa_assert_se(dbus_message_iter_next(dict_i));
764 pa_assert(dbus_message_iter_get_arg_type(dict_i) == DBUS_TYPE_ARRAY);
766 dbus_message_iter_recurse(dict_i, &element_i);
768 while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
769 DBusMessageIter iface_i;
770 const char *interface;
772 dbus_message_iter_recurse(&element_i, &iface_i);
774 pa_assert(dbus_message_iter_get_arg_type(&iface_i) == DBUS_TYPE_STRING);
775 dbus_message_iter_get_basic(&iface_i, &interface);
777 pa_assert_se(dbus_message_iter_next(&iface_i));
778 pa_assert(dbus_message_iter_get_arg_type(&iface_i) == DBUS_TYPE_ARRAY);
780 if (pa_streq(interface, BLUEZ_ADAPTER_INTERFACE)) {
781 pa_bluetooth_adapter *a;
783 if ((a = pa_hashmap_get(y->adapters, path))) {
784 pa_log_error("Found duplicated D-Bus path for device %s", path);
787 a = adapter_create(y, path);
789 pa_log_debug("Adapter %s found", path);
791 parse_adapter_properties(a, &iface_i, false);
795 register_endpoint(y, path, A2DP_SOURCE_ENDPOINT, PA_BLUETOOTH_UUID_A2DP_SOURCE);
796 register_endpoint(y, path, A2DP_SINK_ENDPOINT, PA_BLUETOOTH_UUID_A2DP_SINK);
798 } else if (pa_streq(interface, BLUEZ_DEVICE_INTERFACE)) {
800 if ((d = pa_hashmap_get(y->devices, path))) {
801 if (d->device_info_valid != 0) {
802 pa_log_error("Found duplicated D-Bus path for device %s", path);
806 d = device_create(y, path);
808 pa_log_debug("Device %s found", d->path);
810 parse_device_properties(d, &iface_i, false);
813 pa_log_debug("Unknown interface %s found, skipping", interface);
815 dbus_message_iter_next(&element_i);
818 PA_HASHMAP_FOREACH(d, y->devices, state)
819 if (!d->adapter && d->adapter_path) {
820 d->adapter = pa_hashmap_get(d->discovery->adapters, d->adapter_path);
822 pa_log_error("Device %s is child of nonexistent adapter %s", d->path, d->adapter_path);
823 d->device_info_valid = -1;
830 static void get_managed_objects_reply(DBusPendingCall *pending, void *userdata) {
832 pa_bluetooth_discovery *y;
834 DBusMessageIter arg_i, element_i;
836 pa_assert_se(p = userdata);
837 pa_assert_se(y = p->context_data);
838 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
840 if (dbus_message_is_error(r, DBUS_ERROR_UNKNOWN_METHOD)) {
841 pa_log_warn("BlueZ D-Bus ObjectManager not available");
845 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
846 pa_log_error("GetManagedObjects() failed: %s: %s", dbus_message_get_error_name(r), pa_dbus_get_error_message(r));
850 if (!dbus_message_iter_init(r, &arg_i) || !pa_streq(dbus_message_get_signature(r), "a{oa{sa{sv}}}")) {
851 pa_log_error("Invalid reply signature for GetManagedObjects()");
855 dbus_message_iter_recurse(&arg_i, &element_i);
856 while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
857 DBusMessageIter dict_i;
859 dbus_message_iter_recurse(&element_i, &dict_i);
861 parse_interfaces_and_properties(y, &dict_i);
863 dbus_message_iter_next(&element_i);
866 y->objects_listed = true;
869 dbus_message_unref(r);
871 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
872 pa_dbus_pending_free(p);
875 static void get_managed_objects(pa_bluetooth_discovery *y) {
880 pa_assert_se(m = dbus_message_new_method_call(BLUEZ_SERVICE, "/", "org.freedesktop.DBus.ObjectManager",
881 "GetManagedObjects"));
882 send_and_add_to_pending(y, m, get_managed_objects_reply, NULL);
885 pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook) {
887 pa_assert(PA_REFCNT_VALUE(y) > 0);
889 return &y->hooks[hook];
892 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
893 pa_bluetooth_discovery *y;
898 pa_assert_se(y = userdata);
900 dbus_error_init(&err);
902 if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
903 const char *name, *old_owner, *new_owner;
905 if (!dbus_message_get_args(m, &err,
906 DBUS_TYPE_STRING, &name,
907 DBUS_TYPE_STRING, &old_owner,
908 DBUS_TYPE_STRING, &new_owner,
909 DBUS_TYPE_INVALID)) {
910 pa_log_error("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
914 if (pa_streq(name, BLUEZ_SERVICE)) {
915 if (old_owner && *old_owner) {
916 pa_log_debug("Bluetooth daemon disappeared");
917 device_remove_all(y);
918 adapter_remove_all(y);
919 y->objects_listed = false;
922 if (new_owner && *new_owner) {
923 pa_log_debug("Bluetooth daemon appeared");
924 get_managed_objects(y);
928 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
929 } else if (dbus_message_is_signal(m, "org.freedesktop.DBus.ObjectManager", "InterfacesAdded")) {
930 DBusMessageIter arg_i;
932 if (!y->objects_listed)
933 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; /* No reply received yet from GetManagedObjects */
935 if (!dbus_message_iter_init(m, &arg_i) || !pa_streq(dbus_message_get_signature(m), "oa{sa{sv}}")) {
936 pa_log_error("Invalid signature found in InterfacesAdded");
940 parse_interfaces_and_properties(y, &arg_i);
942 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
943 } else if (dbus_message_is_signal(m, "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved")) {
945 DBusMessageIter arg_i;
946 DBusMessageIter element_i;
948 if (!y->objects_listed)
949 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; /* No reply received yet from GetManagedObjects */
951 if (!dbus_message_iter_init(m, &arg_i) || !pa_streq(dbus_message_get_signature(m), "oas")) {
952 pa_log_error("Invalid signature found in InterfacesRemoved");
956 dbus_message_iter_get_basic(&arg_i, &p);
958 pa_assert_se(dbus_message_iter_next(&arg_i));
959 pa_assert(dbus_message_iter_get_arg_type(&arg_i) == DBUS_TYPE_ARRAY);
961 dbus_message_iter_recurse(&arg_i, &element_i);
963 while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_STRING) {
966 dbus_message_iter_get_basic(&element_i, &iface);
968 if (pa_streq(iface, BLUEZ_DEVICE_INTERFACE))
970 else if (pa_streq(iface, BLUEZ_ADAPTER_INTERFACE))
971 adapter_remove(y, p);
973 dbus_message_iter_next(&element_i);
976 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
978 } else if (dbus_message_is_signal(m, "org.freedesktop.DBus.Properties", "PropertiesChanged")) {
979 DBusMessageIter arg_i;
982 if (!y->objects_listed)
983 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; /* No reply received yet from GetManagedObjects */
985 if (!dbus_message_iter_init(m, &arg_i) || !pa_streq(dbus_message_get_signature(m), "sa{sv}as")) {
986 pa_log_error("Invalid signature found in PropertiesChanged");
990 dbus_message_iter_get_basic(&arg_i, &iface);
992 pa_assert_se(dbus_message_iter_next(&arg_i));
993 pa_assert(dbus_message_iter_get_arg_type(&arg_i) == DBUS_TYPE_ARRAY);
995 if (pa_streq(iface, BLUEZ_ADAPTER_INTERFACE)) {
996 pa_bluetooth_adapter *a;
998 pa_log_debug("Properties changed in adapter %s", dbus_message_get_path(m));
1000 if (!(a = pa_hashmap_get(y->adapters, dbus_message_get_path(m)))) {
1001 pa_log_warn("Properties changed in unknown adapter");
1002 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1005 parse_adapter_properties(a, &arg_i, true);
1007 } else if (pa_streq(iface, BLUEZ_DEVICE_INTERFACE)) {
1008 pa_bluetooth_device *d;
1010 pa_log_debug("Properties changed in device %s", dbus_message_get_path(m));
1012 if (!(d = pa_hashmap_get(y->devices, dbus_message_get_path(m)))) {
1013 pa_log_warn("Properties changed in unknown device");
1014 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1017 if (d->device_info_valid != 1) {
1018 pa_log_warn("Properties changed in a device which information is unknown or invalid");
1019 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1022 parse_device_properties(d, &arg_i, true);
1023 } else if (pa_streq(iface, BLUEZ_MEDIA_TRANSPORT_INTERFACE)) {
1024 pa_bluetooth_transport *t;
1026 pa_log_debug("Properties changed in transport %s", dbus_message_get_path(m));
1028 if (!(t = pa_hashmap_get(y->transports, dbus_message_get_path(m))))
1029 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1031 parse_transport_properties(t, &arg_i);
1034 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1038 dbus_error_free(&err);
1040 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1043 static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
1044 /* These bitpool values were chosen based on the A2DP spec recommendation */
1046 case SBC_SAMPLING_FREQ_16000:
1047 case SBC_SAMPLING_FREQ_32000:
1050 case SBC_SAMPLING_FREQ_44100:
1053 case SBC_CHANNEL_MODE_MONO:
1054 case SBC_CHANNEL_MODE_DUAL_CHANNEL:
1057 case SBC_CHANNEL_MODE_STEREO:
1058 case SBC_CHANNEL_MODE_JOINT_STEREO:
1062 pa_log_warn("Invalid channel mode %u", mode);
1065 case SBC_SAMPLING_FREQ_48000:
1068 case SBC_CHANNEL_MODE_MONO:
1069 case SBC_CHANNEL_MODE_DUAL_CHANNEL:
1072 case SBC_CHANNEL_MODE_STEREO:
1073 case SBC_CHANNEL_MODE_JOINT_STEREO:
1077 pa_log_warn("Invalid channel mode %u", mode);
1081 pa_log_warn("Invalid sampling freq %u", freq);
1085 const char *pa_bluetooth_profile_to_string(pa_bluetooth_profile_t profile) {
1087 case PA_BLUETOOTH_PROFILE_A2DP_SINK:
1089 case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
1090 return "a2dp_source";
1091 case PA_BLUETOOTH_PROFILE_OFF:
1098 static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
1099 pa_bluetooth_discovery *y = userdata;
1100 pa_bluetooth_device *d;
1101 pa_bluetooth_transport *t;
1102 const char *sender, *path, *endpoint_path, *dev_path = NULL, *uuid = NULL;
1103 const uint8_t *config = NULL;
1105 pa_bluetooth_profile_t p = PA_BLUETOOTH_PROFILE_OFF;
1106 DBusMessageIter args, props;
1109 if (!dbus_message_iter_init(m, &args) || !pa_streq(dbus_message_get_signature(m), "oa{sv}")) {
1110 pa_log_error("Invalid signature for method SetConfiguration()");
1114 dbus_message_iter_get_basic(&args, &path);
1116 if (pa_hashmap_get(y->transports, path)) {
1117 pa_log_error("Endpoint SetConfiguration(): Transport %s is already configured.", path);
1121 pa_assert_se(dbus_message_iter_next(&args));
1123 dbus_message_iter_recurse(&args, &props);
1124 if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
1127 /* Read transport properties */
1128 while (dbus_message_iter_get_arg_type(&props) == DBUS_TYPE_DICT_ENTRY) {
1130 DBusMessageIter value, entry;
1133 dbus_message_iter_recurse(&props, &entry);
1134 dbus_message_iter_get_basic(&entry, &key);
1136 dbus_message_iter_next(&entry);
1137 dbus_message_iter_recurse(&entry, &value);
1139 var = dbus_message_iter_get_arg_type(&value);
1141 if (pa_streq(key, "UUID")) {
1142 if (var != DBUS_TYPE_STRING) {
1143 pa_log_error("Property %s of wrong type %c", key, (char)var);
1147 dbus_message_iter_get_basic(&value, &uuid);
1149 endpoint_path = dbus_message_get_path(m);
1150 if (pa_streq(endpoint_path, A2DP_SOURCE_ENDPOINT)) {
1151 if (pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SOURCE))
1152 p = PA_BLUETOOTH_PROFILE_A2DP_SINK;
1153 } else if (pa_streq(endpoint_path, A2DP_SINK_ENDPOINT)) {
1154 if (pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SINK))
1155 p = PA_BLUETOOTH_PROFILE_A2DP_SOURCE;
1158 if (p == PA_BLUETOOTH_PROFILE_OFF) {
1159 pa_log_error("UUID %s of transport %s incompatible with endpoint %s", uuid, path, endpoint_path);
1162 } else if (pa_streq(key, "Device")) {
1163 if (var != DBUS_TYPE_OBJECT_PATH) {
1164 pa_log_error("Property %s of wrong type %c", key, (char)var);
1168 dbus_message_iter_get_basic(&value, &dev_path);
1169 } else if (pa_streq(key, "Configuration")) {
1170 DBusMessageIter array;
1173 if (var != DBUS_TYPE_ARRAY) {
1174 pa_log_error("Property %s of wrong type %c", key, (char)var);
1178 dbus_message_iter_recurse(&value, &array);
1179 var = dbus_message_iter_get_arg_type(&array);
1180 if (var != DBUS_TYPE_BYTE) {
1181 pa_log_error("%s is an array of wrong type %c", key, (char)var);
1185 dbus_message_iter_get_fixed_array(&array, &config, &size);
1186 if (size != sizeof(a2dp_sbc_t)) {
1187 pa_log_error("Configuration array of invalid size");
1191 c = (a2dp_sbc_t *) config;
1193 if (c->frequency != SBC_SAMPLING_FREQ_16000 && c->frequency != SBC_SAMPLING_FREQ_32000 &&
1194 c->frequency != SBC_SAMPLING_FREQ_44100 && c->frequency != SBC_SAMPLING_FREQ_48000) {
1195 pa_log_error("Invalid sampling frequency in configuration");
1199 if (c->channel_mode != SBC_CHANNEL_MODE_MONO && c->channel_mode != SBC_CHANNEL_MODE_DUAL_CHANNEL &&
1200 c->channel_mode != SBC_CHANNEL_MODE_STEREO && c->channel_mode != SBC_CHANNEL_MODE_JOINT_STEREO) {
1201 pa_log_error("Invalid channel mode in configuration");
1205 if (c->allocation_method != SBC_ALLOCATION_SNR && c->allocation_method != SBC_ALLOCATION_LOUDNESS) {
1206 pa_log_error("Invalid allocation method in configuration");
1210 if (c->subbands != SBC_SUBBANDS_4 && c->subbands != SBC_SUBBANDS_8) {
1211 pa_log_error("Invalid SBC subbands in configuration");
1215 if (c->block_length != SBC_BLOCK_LENGTH_4 && c->block_length != SBC_BLOCK_LENGTH_8 &&
1216 c->block_length != SBC_BLOCK_LENGTH_12 && c->block_length != SBC_BLOCK_LENGTH_16) {
1217 pa_log_error("Invalid block length in configuration");
1222 dbus_message_iter_next(&props);
1225 if ((d = pa_hashmap_get(y->devices, dev_path))) {
1226 if (d->device_info_valid == -1) {
1227 pa_log_error("Information about device %s is invalid", dev_path);
1231 /* InterfacesAdded signal is probably on it's way, device_info_valid is kept as 0. */
1232 pa_log_warn("SetConfiguration() received for unknown device %s", dev_path);
1233 d = device_create(y, dev_path);
1236 if (d->transports[p] != NULL) {
1237 pa_log_error("Cannot configure transport %s because profile %s is already used", path, pa_bluetooth_profile_to_string(p));
1241 sender = dbus_message_get_sender(m);
1243 pa_assert_se(r = dbus_message_new_method_return(m));
1244 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
1245 dbus_message_unref(r);
1247 d->transports[p] = t = pa_bluetooth_transport_new(d, sender, path, p, config, size);
1248 t->acquire = bluez5_transport_acquire_cb;
1249 t->release = bluez5_transport_release_cb;
1250 pa_bluetooth_transport_put(t);
1252 pa_log_debug("Transport %s available for profile %s", t->path, pa_bluetooth_profile_to_string(t->profile));
1257 pa_log_error("Endpoint SetConfiguration(): invalid arguments");
1260 pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to set configuration"));
1264 static DBusMessage *endpoint_select_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
1265 pa_bluetooth_discovery *y = userdata;
1266 a2dp_sbc_t *cap, config;
1267 uint8_t *pconf = (uint8_t *) &config;
1272 static const struct {
1276 { 16000U, SBC_SAMPLING_FREQ_16000 },
1277 { 32000U, SBC_SAMPLING_FREQ_32000 },
1278 { 44100U, SBC_SAMPLING_FREQ_44100 },
1279 { 48000U, SBC_SAMPLING_FREQ_48000 }
1282 dbus_error_init(&err);
1284 if (!dbus_message_get_args(m, &err, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &cap, &size, DBUS_TYPE_INVALID)) {
1285 pa_log_error("Endpoint SelectConfiguration(): %s", err.message);
1286 dbus_error_free(&err);
1290 if (size != sizeof(config)) {
1291 pa_log_error("Capabilities array has invalid size");
1297 /* Find the lowest freq that is at least as high as the requested sampling rate */
1298 for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
1299 if (freq_table[i].rate >= y->core->default_sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
1300 config.frequency = freq_table[i].cap;
1304 if ((unsigned) i == PA_ELEMENTSOF(freq_table)) {
1305 for (--i; i >= 0; i--) {
1306 if (cap->frequency & freq_table[i].cap) {
1307 config.frequency = freq_table[i].cap;
1313 pa_log_error("Not suitable sample rate");
1318 pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
1320 if (y->core->default_sample_spec.channels <= 1) {
1321 if (cap->channel_mode & SBC_CHANNEL_MODE_MONO)
1322 config.channel_mode = SBC_CHANNEL_MODE_MONO;
1323 else if (cap->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
1324 config.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
1325 else if (cap->channel_mode & SBC_CHANNEL_MODE_STEREO)
1326 config.channel_mode = SBC_CHANNEL_MODE_STEREO;
1327 else if (cap->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
1328 config.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
1330 pa_log_error("No supported channel modes");
1335 if (y->core->default_sample_spec.channels >= 2) {
1336 if (cap->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
1337 config.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
1338 else if (cap->channel_mode & SBC_CHANNEL_MODE_STEREO)
1339 config.channel_mode = SBC_CHANNEL_MODE_STEREO;
1340 else if (cap->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
1341 config.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
1342 else if (cap->channel_mode & SBC_CHANNEL_MODE_MONO)
1343 config.channel_mode = SBC_CHANNEL_MODE_MONO;
1345 pa_log_error("No supported channel modes");
1350 if (cap->block_length & SBC_BLOCK_LENGTH_16)
1351 config.block_length = SBC_BLOCK_LENGTH_16;
1352 else if (cap->block_length & SBC_BLOCK_LENGTH_12)
1353 config.block_length = SBC_BLOCK_LENGTH_12;
1354 else if (cap->block_length & SBC_BLOCK_LENGTH_8)
1355 config.block_length = SBC_BLOCK_LENGTH_8;
1356 else if (cap->block_length & SBC_BLOCK_LENGTH_4)
1357 config.block_length = SBC_BLOCK_LENGTH_4;
1359 pa_log_error("No supported block lengths");
1363 if (cap->subbands & SBC_SUBBANDS_8)
1364 config.subbands = SBC_SUBBANDS_8;
1365 else if (cap->subbands & SBC_SUBBANDS_4)
1366 config.subbands = SBC_SUBBANDS_4;
1368 pa_log_error("No supported subbands");
1372 if (cap->allocation_method & SBC_ALLOCATION_LOUDNESS)
1373 config.allocation_method = SBC_ALLOCATION_LOUDNESS;
1374 else if (cap->allocation_method & SBC_ALLOCATION_SNR)
1375 config.allocation_method = SBC_ALLOCATION_SNR;
1377 config.min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
1378 config.max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(config.frequency, config.channel_mode), cap->max_bitpool);
1380 if (config.min_bitpool > config.max_bitpool)
1383 pa_assert_se(r = dbus_message_new_method_return(m));
1384 pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &pconf, size, DBUS_TYPE_INVALID));
1389 pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to select configuration"));
1393 static DBusMessage *endpoint_clear_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
1394 pa_bluetooth_discovery *y = userdata;
1395 pa_bluetooth_transport *t;
1400 dbus_error_init(&err);
1402 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
1403 pa_log_error("Endpoint ClearConfiguration(): %s", err.message);
1404 dbus_error_free(&err);
1408 if ((t = pa_hashmap_get(y->transports, path))) {
1409 pa_log_debug("Clearing transport %s profile %s", t->path, pa_bluetooth_profile_to_string(t->profile));
1410 transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED);
1411 pa_bluetooth_transport_free(t);
1414 pa_assert_se(r = dbus_message_new_method_return(m));
1419 pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to clear configuration"));
1423 static DBusMessage *endpoint_release(DBusConnection *conn, DBusMessage *m, void *userdata) {
1426 pa_assert_se(r = dbus_message_new_error(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE ".Error.NotImplemented",
1427 "Method not implemented"));
1432 static DBusHandlerResult endpoint_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
1433 struct pa_bluetooth_discovery *y = userdata;
1434 DBusMessage *r = NULL;
1435 const char *path, *interface, *member;
1439 path = dbus_message_get_path(m);
1440 interface = dbus_message_get_interface(m);
1441 member = dbus_message_get_member(m);
1443 pa_log_debug("dbus: path=%s, interface=%s, member=%s", path, interface, member);
1445 if (!pa_streq(path, A2DP_SOURCE_ENDPOINT) && !pa_streq(path, A2DP_SINK_ENDPOINT))
1446 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1448 if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1449 const char *xml = ENDPOINT_INTROSPECT_XML;
1451 pa_assert_se(r = dbus_message_new_method_return(m));
1452 pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID));
1454 } else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "SetConfiguration"))
1455 r = endpoint_set_configuration(c, m, userdata);
1456 else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "SelectConfiguration"))
1457 r = endpoint_select_configuration(c, m, userdata);
1458 else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "ClearConfiguration"))
1459 r = endpoint_clear_configuration(c, m, userdata);
1460 else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "Release"))
1461 r = endpoint_release(c, m, userdata);
1463 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1466 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
1467 dbus_message_unref(r);
1470 return DBUS_HANDLER_RESULT_HANDLED;
1473 static void endpoint_init(pa_bluetooth_discovery *y, pa_bluetooth_profile_t profile) {
1474 static const DBusObjectPathVTable vtable_endpoint = {
1475 .message_function = endpoint_handler,
1481 case PA_BLUETOOTH_PROFILE_A2DP_SINK:
1482 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT,
1483 &vtable_endpoint, y));
1485 case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
1486 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT,
1487 &vtable_endpoint, y));
1490 pa_assert_not_reached();
1495 static void endpoint_done(pa_bluetooth_discovery *y, pa_bluetooth_profile_t profile) {
1499 case PA_BLUETOOTH_PROFILE_A2DP_SINK:
1500 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT);
1502 case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
1503 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT);
1506 pa_assert_not_reached();
1511 pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
1512 pa_bluetooth_discovery *y;
1514 DBusConnection *conn;
1517 if ((y = pa_shared_get(c, "bluetooth-discovery")))
1518 return pa_bluetooth_discovery_ref(y);
1520 y = pa_xnew0(pa_bluetooth_discovery, 1);
1523 y->adapters = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
1524 y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
1525 y->transports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
1526 PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
1528 for (i = 0; i < PA_BLUETOOTH_HOOK_MAX; i++)
1529 pa_hook_init(&y->hooks[i], y);
1531 pa_shared_set(c, "bluetooth-discovery", y);
1533 dbus_error_init(&err);
1535 if (!(y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err))) {
1536 pa_log_error("Failed to get D-Bus connection: %s", err.message);
1540 conn = pa_dbus_connection_get(y->connection);
1542 /* dynamic detection of bluetooth audio devices */
1543 if (!dbus_connection_add_filter(conn, filter_cb, y, NULL)) {
1544 pa_log_error("Failed to add filter function");
1547 y->filter_added = true;
1549 if (pa_dbus_add_matches(conn, &err,
1550 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'"
1551 ",arg0='" BLUEZ_SERVICE "'",
1552 "type='signal',sender='" BLUEZ_SERVICE "',interface='org.freedesktop.DBus.ObjectManager',member='InterfacesAdded'",
1553 "type='signal',sender='" BLUEZ_SERVICE "',interface='org.freedesktop.DBus.ObjectManager',"
1554 "member='InterfacesRemoved'",
1555 "type='signal',sender='" BLUEZ_SERVICE "',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'"
1556 ",arg0='" BLUEZ_ADAPTER_INTERFACE "'",
1557 "type='signal',sender='" BLUEZ_SERVICE "',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'"
1558 ",arg0='" BLUEZ_DEVICE_INTERFACE "'",
1559 "type='signal',sender='" BLUEZ_SERVICE "',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'"
1560 ",arg0='" BLUEZ_MEDIA_TRANSPORT_INTERFACE "'",
1562 pa_log_error("Failed to add D-Bus matches: %s", err.message);
1565 y->matches_added = true;
1567 endpoint_init(y, PA_BLUETOOTH_PROFILE_A2DP_SINK);
1568 endpoint_init(y, PA_BLUETOOTH_PROFILE_A2DP_SOURCE);
1570 get_managed_objects(y);
1575 pa_bluetooth_discovery_unref(y);
1576 dbus_error_free(&err);
1581 pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
1583 pa_assert(PA_REFCNT_VALUE(y) > 0);
1590 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
1592 pa_assert(PA_REFCNT_VALUE(y) > 0);
1594 if (PA_REFCNT_DEC(y) > 0)
1597 pa_dbus_free_pending_list(&y->pending);
1600 device_remove_all(y);
1601 pa_hashmap_free(y->devices);
1605 adapter_remove_all(y);
1606 pa_hashmap_free(y->adapters);
1609 if (y->transports) {
1610 pa_assert(pa_hashmap_isempty(y->transports));
1611 pa_hashmap_free(y->transports);
1614 if (y->connection) {
1616 if (y->matches_added)
1617 pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
1618 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',"
1619 "arg0='" BLUEZ_SERVICE "'",
1620 "type='signal',sender='" BLUEZ_SERVICE "',interface='org.freedesktop.DBus.ObjectManager',"
1621 "member='InterfacesAdded'",
1622 "type='signal',sender='" BLUEZ_SERVICE "',interface='org.freedesktop.DBus.ObjectManager',"
1623 "member='InterfacesRemoved'",
1624 "type='signal',sender='" BLUEZ_SERVICE "',interface='org.freedesktop.DBus.Properties',"
1625 "member='PropertiesChanged',arg0='" BLUEZ_ADAPTER_INTERFACE "'",
1626 "type='signal',sender='" BLUEZ_SERVICE "',interface='org.freedesktop.DBus.Properties',"
1627 "member='PropertiesChanged',arg0='" BLUEZ_DEVICE_INTERFACE "'",
1628 "type='signal',sender='" BLUEZ_SERVICE "',interface='org.freedesktop.DBus.Properties',"
1629 "member='PropertiesChanged',arg0='" BLUEZ_MEDIA_TRANSPORT_INTERFACE "'",
1632 if (y->filter_added)
1633 dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
1635 endpoint_done(y, PA_BLUETOOTH_PROFILE_A2DP_SINK);
1636 endpoint_done(y, PA_BLUETOOTH_PROFILE_A2DP_SOURCE);
1638 pa_dbus_connection_unref(y->connection);
1641 pa_shared_remove(y->core, "bluetooth-discovery");