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