]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/bluez5-util.c
bluetooth: Get managed objects
[pulseaudio] / src / modules / bluetooth / bluez5-util.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008-2013 João Paulo Rechi Vita
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulse/xmalloc.h>
27
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>
35
36 #include "a2dp-codecs.h"
37
38 #include "bluez5-util.h"
39
40 #define BLUEZ_SERVICE "org.bluez"
41 #define BLUEZ_MEDIA_ENDPOINT_INTERFACE BLUEZ_SERVICE ".MediaEndpoint1"
42 #define BLUEZ_MEDIA_TRANSPORT_INTERFACE BLUEZ_SERVICE ".MediaTransport1"
43
44 #define A2DP_SOURCE_ENDPOINT "/MediaEndpoint/A2DPSource"
45 #define A2DP_SINK_ENDPOINT "/MediaEndpoint/A2DPSink"
46
47 #define ENDPOINT_INTROSPECT_XML \
48 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
49 "<node>" \
50 " <interface name=\"" BLUEZ_MEDIA_ENDPOINT_INTERFACE "\">" \
51 " <method name=\"SetConfiguration\">" \
52 " <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
53 " <arg name=\"properties\" direction=\"in\" type=\"ay\"/>" \
54 " </method>" \
55 " <method name=\"SelectConfiguration\">" \
56 " <arg name=\"capabilities\" direction=\"in\" type=\"ay\"/>" \
57 " <arg name=\"configuration\" direction=\"out\" type=\"ay\"/>" \
58 " </method>" \
59 " <method name=\"ClearConfiguration\">" \
60 " <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
61 " </method>" \
62 " <method name=\"Release\">" \
63 " </method>" \
64 " </interface>" \
65 " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
66 " <method name=\"Introspect\">" \
67 " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
68 " </method>" \
69 " </interface>" \
70 "</node>"
71
72 struct pa_bluetooth_discovery {
73 PA_REFCNT_DECLARE;
74
75 pa_core *core;
76 pa_dbus_connection *connection;
77 bool filter_added;
78 bool matches_added;
79 bool objects_listed;
80 pa_hook hooks[PA_BLUETOOTH_HOOK_MAX];
81 pa_hashmap *adapters;
82 pa_hashmap *devices;
83 pa_hashmap *transports;
84
85 PA_LLIST_HEAD(pa_dbus_pending, pending);
86 };
87
88 static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, DBusMessage *m,
89 DBusPendingCallNotifyFunction func, void *call_data) {
90 pa_dbus_pending *p;
91 DBusPendingCall *call;
92
93 pa_assert(y);
94 pa_assert(m);
95
96 pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y->connection), m, &call, -1));
97
98 p = pa_dbus_pending_new(pa_dbus_connection_get(y->connection), m, call, y, call_data);
99 PA_LLIST_PREPEND(pa_dbus_pending, y->pending, p);
100 dbus_pending_call_set_notify(call, func, p, NULL);
101
102 return p;
103 }
104
105 pa_bluetooth_transport *pa_bluetooth_transport_new(pa_bluetooth_device *d, const char *owner, const char *path,
106 pa_bluetooth_profile_t p, const uint8_t *config, size_t size) {
107 pa_bluetooth_transport *t;
108
109 t = pa_xnew0(pa_bluetooth_transport, 1);
110 t->device = d;
111 t->owner = pa_xstrdup(owner);
112 t->path = pa_xstrdup(path);
113 t->profile = p;
114 t->config_size = size;
115
116 if (size > 0) {
117 t->config = pa_xnew(uint8_t, size);
118 memcpy(t->config, config, size);
119 }
120
121 pa_assert_se(pa_hashmap_put(d->discovery->transports, t->path, t) >= 0);
122
123 return t;
124 }
125
126 static const char *transport_state_to_string(pa_bluetooth_transport_state_t state) {
127 switch(state) {
128 case PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED:
129 return "disconnected";
130 case PA_BLUETOOTH_TRANSPORT_STATE_IDLE:
131 return "idle";
132 case PA_BLUETOOTH_TRANSPORT_STATE_PLAYING:
133 return "playing";
134 }
135
136 return "invalid";
137 }
138
139 static void transport_state_changed(pa_bluetooth_transport *t, pa_bluetooth_transport_state_t state) {
140 bool old_any_connected;
141
142 pa_assert(t);
143
144 if (t->state == state)
145 return;
146
147 old_any_connected = pa_bluetooth_device_any_transport_connected(t->device);
148
149 pa_log_debug("Transport %s state changed from %s to %s",
150 t->path, transport_state_to_string(t->state), transport_state_to_string(state));
151
152 t->state = state;
153 if (state == PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)
154 t->device->transports[t->profile] = NULL;
155
156 pa_hook_fire(&t->device->discovery->hooks[PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED], t);
157
158 if (old_any_connected != pa_bluetooth_device_any_transport_connected(t->device))
159 pa_hook_fire(&t->device->discovery->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], t->device);
160 }
161
162 void pa_bluetooth_transport_put(pa_bluetooth_transport *t) {
163 transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_IDLE);
164 }
165
166 void pa_bluetooth_transport_free(pa_bluetooth_transport *t) {
167 pa_assert(t);
168
169 pa_hashmap_remove(t->device->discovery->transports, t->path);
170 pa_xfree(t->owner);
171 pa_xfree(t->path);
172 pa_xfree(t->config);
173 pa_xfree(t);
174 }
175
176 static int bluez5_transport_acquire_cb(pa_bluetooth_transport *t, bool optional, size_t *imtu, size_t *omtu) {
177 DBusMessage *m, *r;
178 DBusError err;
179 int ret;
180 uint16_t i, o;
181 const char *method = optional ? "TryAcquire" : "Acquire";
182
183 pa_assert(t);
184 pa_assert(t->device);
185 pa_assert(t->device->discovery);
186
187 pa_assert_se(m = dbus_message_new_method_call(t->owner, t->path, BLUEZ_MEDIA_TRANSPORT_INTERFACE, method));
188
189 dbus_error_init(&err);
190
191 r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->device->discovery->connection), m, -1, &err);
192 if (!r) {
193 if (optional && pa_streq(err.name, "org.bluez.Error.NotAvailable"))
194 pa_log_info("Failed optional acquire of unavailable transport %s", t->path);
195 else
196 pa_log_error("Transport %s() failed for transport %s (%s)", method, t->path, err.message);
197
198 dbus_error_free(&err);
199 return -1;
200 }
201
202 if (!dbus_message_get_args(r, &err, DBUS_TYPE_UNIX_FD, &ret, DBUS_TYPE_UINT16, &i, DBUS_TYPE_UINT16, &o,
203 DBUS_TYPE_INVALID)) {
204 pa_log_error("Failed to parse %s() reply: %s", method, err.message);
205 dbus_error_free(&err);
206 ret = -1;
207 goto finish;
208 }
209
210 if (imtu)
211 *imtu = i;
212
213 if (omtu)
214 *omtu = o;
215
216 finish:
217 dbus_message_unref(r);
218 return ret;
219 }
220
221 static void bluez5_transport_release_cb(pa_bluetooth_transport *t) {
222 DBusMessage *m;
223 DBusError err;
224
225 pa_assert(t);
226 pa_assert(t->device);
227 pa_assert(t->device->discovery);
228
229 dbus_error_init(&err);
230
231 if (t->state <= PA_BLUETOOTH_TRANSPORT_STATE_IDLE) {
232 pa_log_info("Transport %s auto-released by BlueZ or already released", t->path);
233 return;
234 }
235
236 pa_assert_se(m = dbus_message_new_method_call(t->owner, t->path, BLUEZ_MEDIA_TRANSPORT_INTERFACE, "Release"));
237 dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->device->discovery->connection), m, -1, &err);
238
239 if (dbus_error_is_set(&err)) {
240 pa_log_error("Failed to release transport %s: %s", t->path, err.message);
241 dbus_error_free(&err);
242 } else
243 pa_log_info("Transport %s released", t->path);
244 }
245
246 bool pa_bluetooth_device_any_transport_connected(const pa_bluetooth_device *d) {
247 unsigned i;
248
249 pa_assert(d);
250
251 if (d->device_info_valid != 1)
252 return false;
253
254 for (i = 0; i < PA_BLUETOOTH_PROFILE_COUNT; i++)
255 if (d->transports[i] && d->transports[i]->state != PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)
256 return true;
257
258 return false;
259 }
260
261 static pa_bluetooth_device* device_create(pa_bluetooth_discovery *y, const char *path) {
262 pa_bluetooth_device *d;
263
264 pa_assert(y);
265 pa_assert(path);
266
267 d = pa_xnew0(pa_bluetooth_device, 1);
268 d->discovery = y;
269 d->path = pa_xstrdup(path);
270
271 pa_hashmap_put(y->devices, d->path, d);
272
273 return d;
274 }
275
276 pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_path(pa_bluetooth_discovery *y, const char *path) {
277 pa_bluetooth_device *d;
278
279 pa_assert(y);
280 pa_assert(PA_REFCNT_VALUE(y) > 0);
281 pa_assert(path);
282
283 if ((d = pa_hashmap_get(y->devices, path)))
284 if (d->device_info_valid == 1)
285 return d;
286
287 return NULL;
288 }
289
290 pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_address(pa_bluetooth_discovery *y, const char *remote, const char *local) {
291 pa_bluetooth_device *d;
292 void *state = NULL;
293
294 pa_assert(y);
295 pa_assert(PA_REFCNT_VALUE(y) > 0);
296 pa_assert(remote);
297 pa_assert(local);
298
299 while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
300 if (pa_streq(d->address, remote) && pa_streq(d->adapter->address, local))
301 return d->device_info_valid == 1 ? d : NULL;
302
303 return NULL;
304 }
305
306 static void device_free(pa_bluetooth_device *d) {
307 unsigned i;
308
309 pa_assert(d);
310
311 for (i = 0; i < PA_BLUETOOTH_PROFILE_COUNT; i++) {
312 pa_bluetooth_transport *t;
313
314 if (!(t = d->transports[i]))
315 continue;
316
317 transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED);
318 pa_bluetooth_transport_free(t);
319 }
320
321 d->discovery = NULL;
322 d->adapter = NULL;
323 pa_xfree(d->path);
324 pa_xfree(d->alias);
325 pa_xfree(d->address);
326 pa_xfree(d);
327 }
328
329 static void device_remove(pa_bluetooth_discovery *y, const char *path) {
330 pa_bluetooth_device *d;
331
332 if (!(d = pa_hashmap_remove(y->devices, path)))
333 pa_log_warn("Unknown device removed %s", path);
334 else {
335 pa_log_debug("Device %s removed", path);
336 device_free(d);
337 }
338 }
339
340 static void device_remove_all(pa_bluetooth_discovery *y) {
341 pa_bluetooth_device *d;
342
343 pa_assert(y);
344
345 while ((d = pa_hashmap_steal_first(y->devices))) {
346 d->device_info_valid = -1;
347 pa_hook_fire(&y->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], d);
348 device_free(d);
349 }
350 }
351
352 static pa_bluetooth_adapter* adapter_create(pa_bluetooth_discovery *y, const char *path) {
353 pa_bluetooth_adapter *a;
354
355 pa_assert(y);
356 pa_assert(path);
357
358 a = pa_xnew0(pa_bluetooth_adapter, 1);
359 a->discovery = y;
360 a->path = pa_xstrdup(path);
361
362 pa_hashmap_put(y->adapters, a->path, a);
363
364 return a;
365 }
366
367 static void adapter_remove_all(pa_bluetooth_discovery *y) {
368 pa_bluetooth_adapter *a;
369
370 pa_assert(y);
371
372 /* When this function is called all devices have already been freed */
373
374 while ((a = pa_hashmap_steal_first(y->adapters))) {
375 pa_xfree(a->path);
376 pa_xfree(a->address);
377 pa_xfree(a);
378 }
379 }
380
381 static void get_managed_objects_reply(DBusPendingCall *pending, void *userdata) {
382 pa_dbus_pending *p;
383 pa_bluetooth_discovery *y;
384 DBusMessage *r;
385 DBusMessageIter arg_i, element_i;
386
387 pa_assert_se(p = userdata);
388 pa_assert_se(y = p->context_data);
389 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
390
391 if (dbus_message_is_error(r, DBUS_ERROR_UNKNOWN_METHOD)) {
392 pa_log_warn("BlueZ D-Bus ObjectManager not available");
393 goto finish;
394 }
395
396 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
397 pa_log_error("GetManagedObjects() failed: %s: %s", dbus_message_get_error_name(r), pa_dbus_get_error_message(r));
398 goto finish;
399 }
400
401 if (!dbus_message_iter_init(r, &arg_i) || !pa_streq(dbus_message_get_signature(r), "a{oa{sa{sv}}}")) {
402 pa_log_error("Invalid reply signature for GetManagedObjects()");
403 goto finish;
404 }
405
406 dbus_message_iter_recurse(&arg_i, &element_i);
407 while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
408 DBusMessageIter dict_i;
409
410 dbus_message_iter_recurse(&element_i, &dict_i);
411
412 /* TODO: parse interfaces and properties */
413
414 dbus_message_iter_next(&element_i);
415 }
416
417 y->objects_listed = true;
418
419 finish:
420 dbus_message_unref(r);
421
422 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
423 pa_dbus_pending_free(p);
424 }
425
426 static void get_managed_objects(pa_bluetooth_discovery *y) {
427 DBusMessage *m;
428
429 pa_assert(y);
430
431 pa_assert_se(m = dbus_message_new_method_call(BLUEZ_SERVICE, "/", "org.freedesktop.DBus.ObjectManager",
432 "GetManagedObjects"));
433 send_and_add_to_pending(y, m, get_managed_objects_reply, NULL);
434 }
435
436 pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook) {
437 pa_assert(y);
438 pa_assert(PA_REFCNT_VALUE(y) > 0);
439
440 return &y->hooks[hook];
441 }
442
443 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
444 pa_bluetooth_discovery *y;
445 DBusError err;
446
447 pa_assert(bus);
448 pa_assert(m);
449 pa_assert_se(y = userdata);
450
451 dbus_error_init(&err);
452
453 if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
454 const char *name, *old_owner, *new_owner;
455
456 if (!dbus_message_get_args(m, &err,
457 DBUS_TYPE_STRING, &name,
458 DBUS_TYPE_STRING, &old_owner,
459 DBUS_TYPE_STRING, &new_owner,
460 DBUS_TYPE_INVALID)) {
461 pa_log_error("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
462 goto fail;
463 }
464
465 if (pa_streq(name, BLUEZ_SERVICE)) {
466 if (old_owner && *old_owner) {
467 pa_log_debug("Bluetooth daemon disappeared");
468 device_remove_all(y);
469 adapter_remove_all(y);
470 y->objects_listed = false;
471 }
472
473 if (new_owner && *new_owner) {
474 pa_log_debug("Bluetooth daemon appeared");
475 get_managed_objects(y);
476 }
477 }
478
479 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
480 }
481
482 fail:
483 dbus_error_free(&err);
484
485 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
486 }
487
488 static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
489 /* These bitpool values were chosen based on the A2DP spec recommendation */
490 switch (freq) {
491 case SBC_SAMPLING_FREQ_16000:
492 case SBC_SAMPLING_FREQ_32000:
493 return 53;
494
495 case SBC_SAMPLING_FREQ_44100:
496
497 switch (mode) {
498 case SBC_CHANNEL_MODE_MONO:
499 case SBC_CHANNEL_MODE_DUAL_CHANNEL:
500 return 31;
501
502 case SBC_CHANNEL_MODE_STEREO:
503 case SBC_CHANNEL_MODE_JOINT_STEREO:
504 return 53;
505 }
506
507 pa_log_warn("Invalid channel mode %u", mode);
508 return 53;
509
510 case SBC_SAMPLING_FREQ_48000:
511
512 switch (mode) {
513 case SBC_CHANNEL_MODE_MONO:
514 case SBC_CHANNEL_MODE_DUAL_CHANNEL:
515 return 29;
516
517 case SBC_CHANNEL_MODE_STEREO:
518 case SBC_CHANNEL_MODE_JOINT_STEREO:
519 return 51;
520 }
521
522 pa_log_warn("Invalid channel mode %u", mode);
523 return 51;
524 }
525
526 pa_log_warn("Invalid sampling freq %u", freq);
527 return 53;
528 }
529
530 const char *pa_bluetooth_profile_to_string(pa_bluetooth_profile_t profile) {
531 switch(profile) {
532 case PA_BLUETOOTH_PROFILE_A2DP_SINK:
533 return "a2dp_sink";
534 case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
535 return "a2dp_source";
536 case PA_BLUETOOTH_PROFILE_OFF:
537 return "off";
538 }
539
540 return NULL;
541 }
542
543 static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
544 pa_bluetooth_discovery *y = userdata;
545 pa_bluetooth_device *d;
546 pa_bluetooth_transport *t;
547 const char *sender, *path, *endpoint_path, *dev_path = NULL, *uuid = NULL;
548 const uint8_t *config = NULL;
549 int size = 0;
550 pa_bluetooth_profile_t p = PA_BLUETOOTH_PROFILE_OFF;
551 DBusMessageIter args, props;
552 DBusMessage *r;
553
554 if (!dbus_message_iter_init(m, &args) || !pa_streq(dbus_message_get_signature(m), "oa{sv}")) {
555 pa_log_error("Invalid signature for method SetConfiguration()");
556 goto fail2;
557 }
558
559 dbus_message_iter_get_basic(&args, &path);
560
561 if (pa_hashmap_get(y->transports, path)) {
562 pa_log_error("Endpoint SetConfiguration(): Transport %s is already configured.", path);
563 goto fail2;
564 }
565
566 pa_assert_se(dbus_message_iter_next(&args));
567
568 dbus_message_iter_recurse(&args, &props);
569 if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
570 goto fail;
571
572 /* Read transport properties */
573 while (dbus_message_iter_get_arg_type(&props) == DBUS_TYPE_DICT_ENTRY) {
574 const char *key;
575 DBusMessageIter value, entry;
576 int var;
577
578 dbus_message_iter_recurse(&props, &entry);
579 dbus_message_iter_get_basic(&entry, &key);
580
581 dbus_message_iter_next(&entry);
582 dbus_message_iter_recurse(&entry, &value);
583
584 var = dbus_message_iter_get_arg_type(&value);
585
586 if (pa_streq(key, "UUID")) {
587 if (var != DBUS_TYPE_STRING) {
588 pa_log_error("Property %s of wrong type %c", key, (char)var);
589 goto fail;
590 }
591
592 dbus_message_iter_get_basic(&value, &uuid);
593
594 endpoint_path = dbus_message_get_path(m);
595 if (pa_streq(endpoint_path, A2DP_SOURCE_ENDPOINT)) {
596 if (pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SOURCE))
597 p = PA_BLUETOOTH_PROFILE_A2DP_SINK;
598 } else if (pa_streq(endpoint_path, A2DP_SINK_ENDPOINT)) {
599 if (pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SINK))
600 p = PA_BLUETOOTH_PROFILE_A2DP_SOURCE;
601 }
602
603 if (p == PA_BLUETOOTH_PROFILE_OFF) {
604 pa_log_error("UUID %s of transport %s incompatible with endpoint %s", uuid, path, endpoint_path);
605 goto fail;
606 }
607 } else if (pa_streq(key, "Device")) {
608 if (var != DBUS_TYPE_OBJECT_PATH) {
609 pa_log_error("Property %s of wrong type %c", key, (char)var);
610 goto fail;
611 }
612
613 dbus_message_iter_get_basic(&value, &dev_path);
614 } else if (pa_streq(key, "Configuration")) {
615 DBusMessageIter array;
616 a2dp_sbc_t *c;
617
618 if (var != DBUS_TYPE_ARRAY) {
619 pa_log_error("Property %s of wrong type %c", key, (char)var);
620 goto fail;
621 }
622
623 dbus_message_iter_recurse(&value, &array);
624 var = dbus_message_iter_get_arg_type(&array);
625 if (var != DBUS_TYPE_BYTE) {
626 pa_log_error("%s is an array of wrong type %c", key, (char)var);
627 goto fail;
628 }
629
630 dbus_message_iter_get_fixed_array(&array, &config, &size);
631 if (size != sizeof(a2dp_sbc_t)) {
632 pa_log_error("Configuration array of invalid size");
633 goto fail;
634 }
635
636 c = (a2dp_sbc_t *) config;
637
638 if (c->frequency != SBC_SAMPLING_FREQ_16000 && c->frequency != SBC_SAMPLING_FREQ_32000 &&
639 c->frequency != SBC_SAMPLING_FREQ_44100 && c->frequency != SBC_SAMPLING_FREQ_48000) {
640 pa_log_error("Invalid sampling frequency in configuration");
641 goto fail;
642 }
643
644 if (c->channel_mode != SBC_CHANNEL_MODE_MONO && c->channel_mode != SBC_CHANNEL_MODE_DUAL_CHANNEL &&
645 c->channel_mode != SBC_CHANNEL_MODE_STEREO && c->channel_mode != SBC_CHANNEL_MODE_JOINT_STEREO) {
646 pa_log_error("Invalid channel mode in configuration");
647 goto fail;
648 }
649
650 if (c->allocation_method != SBC_ALLOCATION_SNR && c->allocation_method != SBC_ALLOCATION_LOUDNESS) {
651 pa_log_error("Invalid allocation method in configuration");
652 goto fail;
653 }
654
655 if (c->subbands != SBC_SUBBANDS_4 && c->subbands != SBC_SUBBANDS_8) {
656 pa_log_error("Invalid SBC subbands in configuration");
657 goto fail;
658 }
659
660 if (c->block_length != SBC_BLOCK_LENGTH_4 && c->block_length != SBC_BLOCK_LENGTH_8 &&
661 c->block_length != SBC_BLOCK_LENGTH_12 && c->block_length != SBC_BLOCK_LENGTH_16) {
662 pa_log_error("Invalid block length in configuration");
663 goto fail;
664 }
665 }
666
667 dbus_message_iter_next(&props);
668 }
669
670 if ((d = pa_hashmap_get(y->devices, dev_path))) {
671 if (d->device_info_valid == -1) {
672 pa_log_error("Information about device %s is invalid", dev_path);
673 goto fail2;
674 }
675 } else {
676 /* InterfacesAdded signal is probably on it's way, device_info_valid is kept as 0. */
677 pa_log_warn("SetConfiguration() received for unknown device %s", dev_path);
678 d = device_create(y, dev_path);
679 }
680
681 if (d->transports[p] != NULL) {
682 pa_log_error("Cannot configure transport %s because profile %s is already used", path, pa_bluetooth_profile_to_string(p));
683 goto fail2;
684 }
685
686 sender = dbus_message_get_sender(m);
687
688 pa_assert_se(r = dbus_message_new_method_return(m));
689 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
690 dbus_message_unref(r);
691
692 d->transports[p] = t = pa_bluetooth_transport_new(d, sender, path, p, config, size);
693 t->acquire = bluez5_transport_acquire_cb;
694 t->release = bluez5_transport_release_cb;
695 pa_bluetooth_transport_put(t);
696
697 pa_log_debug("Transport %s available for profile %s", t->path, pa_bluetooth_profile_to_string(t->profile));
698
699 return NULL;
700
701 fail:
702 pa_log_error("Endpoint SetConfiguration(): invalid arguments");
703
704 fail2:
705 pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to set configuration"));
706 return r;
707 }
708
709 static DBusMessage *endpoint_select_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
710 pa_bluetooth_discovery *y = userdata;
711 a2dp_sbc_t *cap, config;
712 uint8_t *pconf = (uint8_t *) &config;
713 int i, size;
714 DBusMessage *r;
715 DBusError err;
716
717 static const struct {
718 uint32_t rate;
719 uint8_t cap;
720 } freq_table[] = {
721 { 16000U, SBC_SAMPLING_FREQ_16000 },
722 { 32000U, SBC_SAMPLING_FREQ_32000 },
723 { 44100U, SBC_SAMPLING_FREQ_44100 },
724 { 48000U, SBC_SAMPLING_FREQ_48000 }
725 };
726
727 dbus_error_init(&err);
728
729 if (!dbus_message_get_args(m, &err, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &cap, &size, DBUS_TYPE_INVALID)) {
730 pa_log_error("Endpoint SelectConfiguration(): %s", err.message);
731 dbus_error_free(&err);
732 goto fail;
733 }
734
735 if (size != sizeof(config)) {
736 pa_log_error("Capabilities array has invalid size");
737 goto fail;
738 }
739
740 pa_zero(config);
741
742 /* Find the lowest freq that is at least as high as the requested sampling rate */
743 for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
744 if (freq_table[i].rate >= y->core->default_sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
745 config.frequency = freq_table[i].cap;
746 break;
747 }
748
749 if ((unsigned) i == PA_ELEMENTSOF(freq_table)) {
750 for (--i; i >= 0; i--) {
751 if (cap->frequency & freq_table[i].cap) {
752 config.frequency = freq_table[i].cap;
753 break;
754 }
755 }
756
757 if (i < 0) {
758 pa_log_error("Not suitable sample rate");
759 goto fail;
760 }
761 }
762
763 pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
764
765 if (y->core->default_sample_spec.channels <= 1) {
766 if (cap->channel_mode & SBC_CHANNEL_MODE_MONO)
767 config.channel_mode = SBC_CHANNEL_MODE_MONO;
768 else if (cap->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
769 config.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
770 else if (cap->channel_mode & SBC_CHANNEL_MODE_STEREO)
771 config.channel_mode = SBC_CHANNEL_MODE_STEREO;
772 else if (cap->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
773 config.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
774 else {
775 pa_log_error("No supported channel modes");
776 goto fail;
777 }
778 }
779
780 if (y->core->default_sample_spec.channels >= 2) {
781 if (cap->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
782 config.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
783 else if (cap->channel_mode & SBC_CHANNEL_MODE_STEREO)
784 config.channel_mode = SBC_CHANNEL_MODE_STEREO;
785 else if (cap->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
786 config.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
787 else if (cap->channel_mode & SBC_CHANNEL_MODE_MONO)
788 config.channel_mode = SBC_CHANNEL_MODE_MONO;
789 else {
790 pa_log_error("No supported channel modes");
791 goto fail;
792 }
793 }
794
795 if (cap->block_length & SBC_BLOCK_LENGTH_16)
796 config.block_length = SBC_BLOCK_LENGTH_16;
797 else if (cap->block_length & SBC_BLOCK_LENGTH_12)
798 config.block_length = SBC_BLOCK_LENGTH_12;
799 else if (cap->block_length & SBC_BLOCK_LENGTH_8)
800 config.block_length = SBC_BLOCK_LENGTH_8;
801 else if (cap->block_length & SBC_BLOCK_LENGTH_4)
802 config.block_length = SBC_BLOCK_LENGTH_4;
803 else {
804 pa_log_error("No supported block lengths");
805 goto fail;
806 }
807
808 if (cap->subbands & SBC_SUBBANDS_8)
809 config.subbands = SBC_SUBBANDS_8;
810 else if (cap->subbands & SBC_SUBBANDS_4)
811 config.subbands = SBC_SUBBANDS_4;
812 else {
813 pa_log_error("No supported subbands");
814 goto fail;
815 }
816
817 if (cap->allocation_method & SBC_ALLOCATION_LOUDNESS)
818 config.allocation_method = SBC_ALLOCATION_LOUDNESS;
819 else if (cap->allocation_method & SBC_ALLOCATION_SNR)
820 config.allocation_method = SBC_ALLOCATION_SNR;
821
822 config.min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
823 config.max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(config.frequency, config.channel_mode), cap->max_bitpool);
824
825 if (config.min_bitpool > config.max_bitpool)
826 goto fail;
827
828 pa_assert_se(r = dbus_message_new_method_return(m));
829 pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &pconf, size, DBUS_TYPE_INVALID));
830
831 return r;
832
833 fail:
834 pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to select configuration"));
835 return r;
836 }
837
838 static DBusMessage *endpoint_clear_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
839 pa_bluetooth_discovery *y = userdata;
840 pa_bluetooth_transport *t;
841 DBusMessage *r;
842 DBusError err;
843 const char *path;
844
845 dbus_error_init(&err);
846
847 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
848 pa_log_error("Endpoint ClearConfiguration(): %s", err.message);
849 dbus_error_free(&err);
850 goto fail;
851 }
852
853 if ((t = pa_hashmap_get(y->transports, path))) {
854 pa_log_debug("Clearing transport %s profile %s", t->path, pa_bluetooth_profile_to_string(t->profile));
855 transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED);
856 pa_bluetooth_transport_free(t);
857 }
858
859 pa_assert_se(r = dbus_message_new_method_return(m));
860
861 return r;
862
863 fail:
864 pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to clear configuration"));
865 return r;
866 }
867
868 static DBusMessage *endpoint_release(DBusConnection *conn, DBusMessage *m, void *userdata) {
869 DBusMessage *r;
870
871 pa_assert_se(r = dbus_message_new_error(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE ".Error.NotImplemented",
872 "Method not implemented"));
873
874 return r;
875 }
876
877 static DBusHandlerResult endpoint_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
878 struct pa_bluetooth_discovery *y = userdata;
879 DBusMessage *r = NULL;
880 const char *path, *interface, *member;
881
882 pa_assert(y);
883
884 path = dbus_message_get_path(m);
885 interface = dbus_message_get_interface(m);
886 member = dbus_message_get_member(m);
887
888 pa_log_debug("dbus: path=%s, interface=%s, member=%s", path, interface, member);
889
890 if (!pa_streq(path, A2DP_SOURCE_ENDPOINT) && !pa_streq(path, A2DP_SINK_ENDPOINT))
891 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
892
893 if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
894 const char *xml = ENDPOINT_INTROSPECT_XML;
895
896 pa_assert_se(r = dbus_message_new_method_return(m));
897 pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID));
898
899 } else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "SetConfiguration"))
900 r = endpoint_set_configuration(c, m, userdata);
901 else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "SelectConfiguration"))
902 r = endpoint_select_configuration(c, m, userdata);
903 else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "ClearConfiguration"))
904 r = endpoint_clear_configuration(c, m, userdata);
905 else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "Release"))
906 r = endpoint_release(c, m, userdata);
907 else
908 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
909
910 if (r) {
911 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
912 dbus_message_unref(r);
913 }
914
915 return DBUS_HANDLER_RESULT_HANDLED;
916 }
917
918 static void endpoint_init(pa_bluetooth_discovery *y, pa_bluetooth_profile_t profile) {
919 static const DBusObjectPathVTable vtable_endpoint = {
920 .message_function = endpoint_handler,
921 };
922
923 pa_assert(y);
924
925 switch(profile) {
926 case PA_BLUETOOTH_PROFILE_A2DP_SINK:
927 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT,
928 &vtable_endpoint, y));
929 break;
930 case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
931 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT,
932 &vtable_endpoint, y));
933 break;
934 default:
935 pa_assert_not_reached();
936 break;
937 }
938 }
939
940 static void endpoint_done(pa_bluetooth_discovery *y, pa_bluetooth_profile_t profile) {
941 pa_assert(y);
942
943 switch(profile) {
944 case PA_BLUETOOTH_PROFILE_A2DP_SINK:
945 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT);
946 break;
947 case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
948 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT);
949 break;
950 default:
951 pa_assert_not_reached();
952 break;
953 }
954 }
955
956 pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
957 pa_bluetooth_discovery *y;
958 DBusError err;
959 DBusConnection *conn;
960 unsigned i;
961
962 if ((y = pa_shared_get(c, "bluetooth-discovery")))
963 return pa_bluetooth_discovery_ref(y);
964
965 y = pa_xnew0(pa_bluetooth_discovery, 1);
966 PA_REFCNT_INIT(y);
967 y->core = c;
968 y->adapters = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
969 y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
970 y->transports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
971 PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
972
973 for (i = 0; i < PA_BLUETOOTH_HOOK_MAX; i++)
974 pa_hook_init(&y->hooks[i], y);
975
976 pa_shared_set(c, "bluetooth-discovery", y);
977
978 dbus_error_init(&err);
979
980 if (!(y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err))) {
981 pa_log_error("Failed to get D-Bus connection: %s", err.message);
982 goto fail;
983 }
984
985 conn = pa_dbus_connection_get(y->connection);
986
987 /* dynamic detection of bluetooth audio devices */
988 if (!dbus_connection_add_filter(conn, filter_cb, y, NULL)) {
989 pa_log_error("Failed to add filter function");
990 goto fail;
991 }
992 y->filter_added = true;
993
994 if (pa_dbus_add_matches(conn, &err,
995 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'"
996 ",arg0='" BLUEZ_SERVICE "'",
997 NULL) < 0) {
998 pa_log_error("Failed to add D-Bus matches: %s", err.message);
999 goto fail;
1000 }
1001 y->matches_added = true;
1002
1003 endpoint_init(y, PA_BLUETOOTH_PROFILE_A2DP_SINK);
1004 endpoint_init(y, PA_BLUETOOTH_PROFILE_A2DP_SOURCE);
1005
1006 get_managed_objects(y);
1007
1008 return y;
1009
1010 fail:
1011 pa_bluetooth_discovery_unref(y);
1012 dbus_error_free(&err);
1013
1014 return NULL;
1015 }
1016
1017 pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
1018 pa_assert(y);
1019 pa_assert(PA_REFCNT_VALUE(y) > 0);
1020
1021 PA_REFCNT_INC(y);
1022
1023 return y;
1024 }
1025
1026 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
1027 pa_assert(y);
1028 pa_assert(PA_REFCNT_VALUE(y) > 0);
1029
1030 if (PA_REFCNT_DEC(y) > 0)
1031 return;
1032
1033 pa_dbus_free_pending_list(&y->pending);
1034
1035 if (y->devices) {
1036 device_remove_all(y);
1037 pa_hashmap_free(y->devices);
1038 }
1039
1040 if (y->adapters) {
1041 adapter_remove_all(y);
1042 pa_hashmap_free(y->adapters);
1043 }
1044
1045 if (y->transports) {
1046 pa_assert(pa_hashmap_isempty(y->transports));
1047 pa_hashmap_free(y->transports);
1048 }
1049
1050 if (y->connection) {
1051
1052 if (y->matches_added)
1053 pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
1054 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',"
1055 "arg0='" BLUEZ_SERVICE "'",
1056 NULL);
1057
1058 if (y->filter_added)
1059 dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
1060
1061 endpoint_done(y, PA_BLUETOOTH_PROFILE_A2DP_SINK);
1062 endpoint_done(y, PA_BLUETOOTH_PROFILE_A2DP_SOURCE);
1063
1064 pa_dbus_connection_unref(y->connection);
1065 }
1066
1067 pa_shared_remove(y->core, "bluetooth-discovery");
1068 pa_xfree(y);
1069 }