]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/bluez5-util.c
bluetooth: Create a function to remove only one adapter object
[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_free(pa_bluetooth_adapter *a) {
368 pa_bluetooth_device *d;
369 void *state;
370
371 pa_assert(a);
372 pa_assert(a->discovery);
373
374 PA_HASHMAP_FOREACH(d, a->discovery->devices, state)
375 if (d->adapter == a)
376 d->adapter = NULL;
377
378 pa_xfree(a->path);
379 pa_xfree(a->address);
380 pa_xfree(a);
381 }
382
383 static void adapter_remove(pa_bluetooth_discovery *y, const char *path) {
384 pa_bluetooth_adapter *a;
385
386 if (!(a = pa_hashmap_remove(y->adapters, path)))
387 pa_log_warn("Unknown adapter removed %s", path);
388 else {
389 pa_log_debug("Adapter %s removed", path);
390 adapter_free(a);
391 }
392 }
393
394 static void adapter_remove_all(pa_bluetooth_discovery *y) {
395 pa_bluetooth_adapter *a;
396
397 pa_assert(y);
398
399 /* When this function is called all devices have already been freed */
400
401 while ((a = pa_hashmap_steal_first(y->adapters)))
402 adapter_free(a);
403 }
404
405 static void get_managed_objects_reply(DBusPendingCall *pending, void *userdata) {
406 pa_dbus_pending *p;
407 pa_bluetooth_discovery *y;
408 DBusMessage *r;
409 DBusMessageIter arg_i, element_i;
410
411 pa_assert_se(p = userdata);
412 pa_assert_se(y = p->context_data);
413 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
414
415 if (dbus_message_is_error(r, DBUS_ERROR_UNKNOWN_METHOD)) {
416 pa_log_warn("BlueZ D-Bus ObjectManager not available");
417 goto finish;
418 }
419
420 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
421 pa_log_error("GetManagedObjects() failed: %s: %s", dbus_message_get_error_name(r), pa_dbus_get_error_message(r));
422 goto finish;
423 }
424
425 if (!dbus_message_iter_init(r, &arg_i) || !pa_streq(dbus_message_get_signature(r), "a{oa{sa{sv}}}")) {
426 pa_log_error("Invalid reply signature for GetManagedObjects()");
427 goto finish;
428 }
429
430 dbus_message_iter_recurse(&arg_i, &element_i);
431 while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
432 DBusMessageIter dict_i;
433
434 dbus_message_iter_recurse(&element_i, &dict_i);
435
436 /* TODO: parse interfaces and properties */
437
438 dbus_message_iter_next(&element_i);
439 }
440
441 y->objects_listed = true;
442
443 finish:
444 dbus_message_unref(r);
445
446 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
447 pa_dbus_pending_free(p);
448 }
449
450 static void get_managed_objects(pa_bluetooth_discovery *y) {
451 DBusMessage *m;
452
453 pa_assert(y);
454
455 pa_assert_se(m = dbus_message_new_method_call(BLUEZ_SERVICE, "/", "org.freedesktop.DBus.ObjectManager",
456 "GetManagedObjects"));
457 send_and_add_to_pending(y, m, get_managed_objects_reply, NULL);
458 }
459
460 pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook) {
461 pa_assert(y);
462 pa_assert(PA_REFCNT_VALUE(y) > 0);
463
464 return &y->hooks[hook];
465 }
466
467 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
468 pa_bluetooth_discovery *y;
469 DBusError err;
470
471 pa_assert(bus);
472 pa_assert(m);
473 pa_assert_se(y = userdata);
474
475 dbus_error_init(&err);
476
477 if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
478 const char *name, *old_owner, *new_owner;
479
480 if (!dbus_message_get_args(m, &err,
481 DBUS_TYPE_STRING, &name,
482 DBUS_TYPE_STRING, &old_owner,
483 DBUS_TYPE_STRING, &new_owner,
484 DBUS_TYPE_INVALID)) {
485 pa_log_error("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
486 goto fail;
487 }
488
489 if (pa_streq(name, BLUEZ_SERVICE)) {
490 if (old_owner && *old_owner) {
491 pa_log_debug("Bluetooth daemon disappeared");
492 device_remove_all(y);
493 adapter_remove_all(y);
494 y->objects_listed = false;
495 }
496
497 if (new_owner && *new_owner) {
498 pa_log_debug("Bluetooth daemon appeared");
499 get_managed_objects(y);
500 }
501 }
502
503 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
504 }
505
506 fail:
507 dbus_error_free(&err);
508
509 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
510 }
511
512 static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
513 /* These bitpool values were chosen based on the A2DP spec recommendation */
514 switch (freq) {
515 case SBC_SAMPLING_FREQ_16000:
516 case SBC_SAMPLING_FREQ_32000:
517 return 53;
518
519 case SBC_SAMPLING_FREQ_44100:
520
521 switch (mode) {
522 case SBC_CHANNEL_MODE_MONO:
523 case SBC_CHANNEL_MODE_DUAL_CHANNEL:
524 return 31;
525
526 case SBC_CHANNEL_MODE_STEREO:
527 case SBC_CHANNEL_MODE_JOINT_STEREO:
528 return 53;
529 }
530
531 pa_log_warn("Invalid channel mode %u", mode);
532 return 53;
533
534 case SBC_SAMPLING_FREQ_48000:
535
536 switch (mode) {
537 case SBC_CHANNEL_MODE_MONO:
538 case SBC_CHANNEL_MODE_DUAL_CHANNEL:
539 return 29;
540
541 case SBC_CHANNEL_MODE_STEREO:
542 case SBC_CHANNEL_MODE_JOINT_STEREO:
543 return 51;
544 }
545
546 pa_log_warn("Invalid channel mode %u", mode);
547 return 51;
548 }
549
550 pa_log_warn("Invalid sampling freq %u", freq);
551 return 53;
552 }
553
554 const char *pa_bluetooth_profile_to_string(pa_bluetooth_profile_t profile) {
555 switch(profile) {
556 case PA_BLUETOOTH_PROFILE_A2DP_SINK:
557 return "a2dp_sink";
558 case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
559 return "a2dp_source";
560 case PA_BLUETOOTH_PROFILE_OFF:
561 return "off";
562 }
563
564 return NULL;
565 }
566
567 static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
568 pa_bluetooth_discovery *y = userdata;
569 pa_bluetooth_device *d;
570 pa_bluetooth_transport *t;
571 const char *sender, *path, *endpoint_path, *dev_path = NULL, *uuid = NULL;
572 const uint8_t *config = NULL;
573 int size = 0;
574 pa_bluetooth_profile_t p = PA_BLUETOOTH_PROFILE_OFF;
575 DBusMessageIter args, props;
576 DBusMessage *r;
577
578 if (!dbus_message_iter_init(m, &args) || !pa_streq(dbus_message_get_signature(m), "oa{sv}")) {
579 pa_log_error("Invalid signature for method SetConfiguration()");
580 goto fail2;
581 }
582
583 dbus_message_iter_get_basic(&args, &path);
584
585 if (pa_hashmap_get(y->transports, path)) {
586 pa_log_error("Endpoint SetConfiguration(): Transport %s is already configured.", path);
587 goto fail2;
588 }
589
590 pa_assert_se(dbus_message_iter_next(&args));
591
592 dbus_message_iter_recurse(&args, &props);
593 if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
594 goto fail;
595
596 /* Read transport properties */
597 while (dbus_message_iter_get_arg_type(&props) == DBUS_TYPE_DICT_ENTRY) {
598 const char *key;
599 DBusMessageIter value, entry;
600 int var;
601
602 dbus_message_iter_recurse(&props, &entry);
603 dbus_message_iter_get_basic(&entry, &key);
604
605 dbus_message_iter_next(&entry);
606 dbus_message_iter_recurse(&entry, &value);
607
608 var = dbus_message_iter_get_arg_type(&value);
609
610 if (pa_streq(key, "UUID")) {
611 if (var != DBUS_TYPE_STRING) {
612 pa_log_error("Property %s of wrong type %c", key, (char)var);
613 goto fail;
614 }
615
616 dbus_message_iter_get_basic(&value, &uuid);
617
618 endpoint_path = dbus_message_get_path(m);
619 if (pa_streq(endpoint_path, A2DP_SOURCE_ENDPOINT)) {
620 if (pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SOURCE))
621 p = PA_BLUETOOTH_PROFILE_A2DP_SINK;
622 } else if (pa_streq(endpoint_path, A2DP_SINK_ENDPOINT)) {
623 if (pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SINK))
624 p = PA_BLUETOOTH_PROFILE_A2DP_SOURCE;
625 }
626
627 if (p == PA_BLUETOOTH_PROFILE_OFF) {
628 pa_log_error("UUID %s of transport %s incompatible with endpoint %s", uuid, path, endpoint_path);
629 goto fail;
630 }
631 } else if (pa_streq(key, "Device")) {
632 if (var != DBUS_TYPE_OBJECT_PATH) {
633 pa_log_error("Property %s of wrong type %c", key, (char)var);
634 goto fail;
635 }
636
637 dbus_message_iter_get_basic(&value, &dev_path);
638 } else if (pa_streq(key, "Configuration")) {
639 DBusMessageIter array;
640 a2dp_sbc_t *c;
641
642 if (var != DBUS_TYPE_ARRAY) {
643 pa_log_error("Property %s of wrong type %c", key, (char)var);
644 goto fail;
645 }
646
647 dbus_message_iter_recurse(&value, &array);
648 var = dbus_message_iter_get_arg_type(&array);
649 if (var != DBUS_TYPE_BYTE) {
650 pa_log_error("%s is an array of wrong type %c", key, (char)var);
651 goto fail;
652 }
653
654 dbus_message_iter_get_fixed_array(&array, &config, &size);
655 if (size != sizeof(a2dp_sbc_t)) {
656 pa_log_error("Configuration array of invalid size");
657 goto fail;
658 }
659
660 c = (a2dp_sbc_t *) config;
661
662 if (c->frequency != SBC_SAMPLING_FREQ_16000 && c->frequency != SBC_SAMPLING_FREQ_32000 &&
663 c->frequency != SBC_SAMPLING_FREQ_44100 && c->frequency != SBC_SAMPLING_FREQ_48000) {
664 pa_log_error("Invalid sampling frequency in configuration");
665 goto fail;
666 }
667
668 if (c->channel_mode != SBC_CHANNEL_MODE_MONO && c->channel_mode != SBC_CHANNEL_MODE_DUAL_CHANNEL &&
669 c->channel_mode != SBC_CHANNEL_MODE_STEREO && c->channel_mode != SBC_CHANNEL_MODE_JOINT_STEREO) {
670 pa_log_error("Invalid channel mode in configuration");
671 goto fail;
672 }
673
674 if (c->allocation_method != SBC_ALLOCATION_SNR && c->allocation_method != SBC_ALLOCATION_LOUDNESS) {
675 pa_log_error("Invalid allocation method in configuration");
676 goto fail;
677 }
678
679 if (c->subbands != SBC_SUBBANDS_4 && c->subbands != SBC_SUBBANDS_8) {
680 pa_log_error("Invalid SBC subbands in configuration");
681 goto fail;
682 }
683
684 if (c->block_length != SBC_BLOCK_LENGTH_4 && c->block_length != SBC_BLOCK_LENGTH_8 &&
685 c->block_length != SBC_BLOCK_LENGTH_12 && c->block_length != SBC_BLOCK_LENGTH_16) {
686 pa_log_error("Invalid block length in configuration");
687 goto fail;
688 }
689 }
690
691 dbus_message_iter_next(&props);
692 }
693
694 if ((d = pa_hashmap_get(y->devices, dev_path))) {
695 if (d->device_info_valid == -1) {
696 pa_log_error("Information about device %s is invalid", dev_path);
697 goto fail2;
698 }
699 } else {
700 /* InterfacesAdded signal is probably on it's way, device_info_valid is kept as 0. */
701 pa_log_warn("SetConfiguration() received for unknown device %s", dev_path);
702 d = device_create(y, dev_path);
703 }
704
705 if (d->transports[p] != NULL) {
706 pa_log_error("Cannot configure transport %s because profile %s is already used", path, pa_bluetooth_profile_to_string(p));
707 goto fail2;
708 }
709
710 sender = dbus_message_get_sender(m);
711
712 pa_assert_se(r = dbus_message_new_method_return(m));
713 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
714 dbus_message_unref(r);
715
716 d->transports[p] = t = pa_bluetooth_transport_new(d, sender, path, p, config, size);
717 t->acquire = bluez5_transport_acquire_cb;
718 t->release = bluez5_transport_release_cb;
719 pa_bluetooth_transport_put(t);
720
721 pa_log_debug("Transport %s available for profile %s", t->path, pa_bluetooth_profile_to_string(t->profile));
722
723 return NULL;
724
725 fail:
726 pa_log_error("Endpoint SetConfiguration(): invalid arguments");
727
728 fail2:
729 pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to set configuration"));
730 return r;
731 }
732
733 static DBusMessage *endpoint_select_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
734 pa_bluetooth_discovery *y = userdata;
735 a2dp_sbc_t *cap, config;
736 uint8_t *pconf = (uint8_t *) &config;
737 int i, size;
738 DBusMessage *r;
739 DBusError err;
740
741 static const struct {
742 uint32_t rate;
743 uint8_t cap;
744 } freq_table[] = {
745 { 16000U, SBC_SAMPLING_FREQ_16000 },
746 { 32000U, SBC_SAMPLING_FREQ_32000 },
747 { 44100U, SBC_SAMPLING_FREQ_44100 },
748 { 48000U, SBC_SAMPLING_FREQ_48000 }
749 };
750
751 dbus_error_init(&err);
752
753 if (!dbus_message_get_args(m, &err, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &cap, &size, DBUS_TYPE_INVALID)) {
754 pa_log_error("Endpoint SelectConfiguration(): %s", err.message);
755 dbus_error_free(&err);
756 goto fail;
757 }
758
759 if (size != sizeof(config)) {
760 pa_log_error("Capabilities array has invalid size");
761 goto fail;
762 }
763
764 pa_zero(config);
765
766 /* Find the lowest freq that is at least as high as the requested sampling rate */
767 for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
768 if (freq_table[i].rate >= y->core->default_sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
769 config.frequency = freq_table[i].cap;
770 break;
771 }
772
773 if ((unsigned) i == PA_ELEMENTSOF(freq_table)) {
774 for (--i; i >= 0; i--) {
775 if (cap->frequency & freq_table[i].cap) {
776 config.frequency = freq_table[i].cap;
777 break;
778 }
779 }
780
781 if (i < 0) {
782 pa_log_error("Not suitable sample rate");
783 goto fail;
784 }
785 }
786
787 pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
788
789 if (y->core->default_sample_spec.channels <= 1) {
790 if (cap->channel_mode & SBC_CHANNEL_MODE_MONO)
791 config.channel_mode = SBC_CHANNEL_MODE_MONO;
792 else if (cap->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
793 config.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
794 else if (cap->channel_mode & SBC_CHANNEL_MODE_STEREO)
795 config.channel_mode = SBC_CHANNEL_MODE_STEREO;
796 else if (cap->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
797 config.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
798 else {
799 pa_log_error("No supported channel modes");
800 goto fail;
801 }
802 }
803
804 if (y->core->default_sample_spec.channels >= 2) {
805 if (cap->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
806 config.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
807 else if (cap->channel_mode & SBC_CHANNEL_MODE_STEREO)
808 config.channel_mode = SBC_CHANNEL_MODE_STEREO;
809 else if (cap->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
810 config.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
811 else if (cap->channel_mode & SBC_CHANNEL_MODE_MONO)
812 config.channel_mode = SBC_CHANNEL_MODE_MONO;
813 else {
814 pa_log_error("No supported channel modes");
815 goto fail;
816 }
817 }
818
819 if (cap->block_length & SBC_BLOCK_LENGTH_16)
820 config.block_length = SBC_BLOCK_LENGTH_16;
821 else if (cap->block_length & SBC_BLOCK_LENGTH_12)
822 config.block_length = SBC_BLOCK_LENGTH_12;
823 else if (cap->block_length & SBC_BLOCK_LENGTH_8)
824 config.block_length = SBC_BLOCK_LENGTH_8;
825 else if (cap->block_length & SBC_BLOCK_LENGTH_4)
826 config.block_length = SBC_BLOCK_LENGTH_4;
827 else {
828 pa_log_error("No supported block lengths");
829 goto fail;
830 }
831
832 if (cap->subbands & SBC_SUBBANDS_8)
833 config.subbands = SBC_SUBBANDS_8;
834 else if (cap->subbands & SBC_SUBBANDS_4)
835 config.subbands = SBC_SUBBANDS_4;
836 else {
837 pa_log_error("No supported subbands");
838 goto fail;
839 }
840
841 if (cap->allocation_method & SBC_ALLOCATION_LOUDNESS)
842 config.allocation_method = SBC_ALLOCATION_LOUDNESS;
843 else if (cap->allocation_method & SBC_ALLOCATION_SNR)
844 config.allocation_method = SBC_ALLOCATION_SNR;
845
846 config.min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
847 config.max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(config.frequency, config.channel_mode), cap->max_bitpool);
848
849 if (config.min_bitpool > config.max_bitpool)
850 goto fail;
851
852 pa_assert_se(r = dbus_message_new_method_return(m));
853 pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &pconf, size, DBUS_TYPE_INVALID));
854
855 return r;
856
857 fail:
858 pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to select configuration"));
859 return r;
860 }
861
862 static DBusMessage *endpoint_clear_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
863 pa_bluetooth_discovery *y = userdata;
864 pa_bluetooth_transport *t;
865 DBusMessage *r;
866 DBusError err;
867 const char *path;
868
869 dbus_error_init(&err);
870
871 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
872 pa_log_error("Endpoint ClearConfiguration(): %s", err.message);
873 dbus_error_free(&err);
874 goto fail;
875 }
876
877 if ((t = pa_hashmap_get(y->transports, path))) {
878 pa_log_debug("Clearing transport %s profile %s", t->path, pa_bluetooth_profile_to_string(t->profile));
879 transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED);
880 pa_bluetooth_transport_free(t);
881 }
882
883 pa_assert_se(r = dbus_message_new_method_return(m));
884
885 return r;
886
887 fail:
888 pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to clear configuration"));
889 return r;
890 }
891
892 static DBusMessage *endpoint_release(DBusConnection *conn, DBusMessage *m, void *userdata) {
893 DBusMessage *r;
894
895 pa_assert_se(r = dbus_message_new_error(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE ".Error.NotImplemented",
896 "Method not implemented"));
897
898 return r;
899 }
900
901 static DBusHandlerResult endpoint_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
902 struct pa_bluetooth_discovery *y = userdata;
903 DBusMessage *r = NULL;
904 const char *path, *interface, *member;
905
906 pa_assert(y);
907
908 path = dbus_message_get_path(m);
909 interface = dbus_message_get_interface(m);
910 member = dbus_message_get_member(m);
911
912 pa_log_debug("dbus: path=%s, interface=%s, member=%s", path, interface, member);
913
914 if (!pa_streq(path, A2DP_SOURCE_ENDPOINT) && !pa_streq(path, A2DP_SINK_ENDPOINT))
915 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
916
917 if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
918 const char *xml = ENDPOINT_INTROSPECT_XML;
919
920 pa_assert_se(r = dbus_message_new_method_return(m));
921 pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID));
922
923 } else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "SetConfiguration"))
924 r = endpoint_set_configuration(c, m, userdata);
925 else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "SelectConfiguration"))
926 r = endpoint_select_configuration(c, m, userdata);
927 else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "ClearConfiguration"))
928 r = endpoint_clear_configuration(c, m, userdata);
929 else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "Release"))
930 r = endpoint_release(c, m, userdata);
931 else
932 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
933
934 if (r) {
935 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
936 dbus_message_unref(r);
937 }
938
939 return DBUS_HANDLER_RESULT_HANDLED;
940 }
941
942 static void endpoint_init(pa_bluetooth_discovery *y, pa_bluetooth_profile_t profile) {
943 static const DBusObjectPathVTable vtable_endpoint = {
944 .message_function = endpoint_handler,
945 };
946
947 pa_assert(y);
948
949 switch(profile) {
950 case PA_BLUETOOTH_PROFILE_A2DP_SINK:
951 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT,
952 &vtable_endpoint, y));
953 break;
954 case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
955 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT,
956 &vtable_endpoint, y));
957 break;
958 default:
959 pa_assert_not_reached();
960 break;
961 }
962 }
963
964 static void endpoint_done(pa_bluetooth_discovery *y, pa_bluetooth_profile_t profile) {
965 pa_assert(y);
966
967 switch(profile) {
968 case PA_BLUETOOTH_PROFILE_A2DP_SINK:
969 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT);
970 break;
971 case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
972 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT);
973 break;
974 default:
975 pa_assert_not_reached();
976 break;
977 }
978 }
979
980 pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
981 pa_bluetooth_discovery *y;
982 DBusError err;
983 DBusConnection *conn;
984 unsigned i;
985
986 if ((y = pa_shared_get(c, "bluetooth-discovery")))
987 return pa_bluetooth_discovery_ref(y);
988
989 y = pa_xnew0(pa_bluetooth_discovery, 1);
990 PA_REFCNT_INIT(y);
991 y->core = c;
992 y->adapters = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
993 y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
994 y->transports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
995 PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
996
997 for (i = 0; i < PA_BLUETOOTH_HOOK_MAX; i++)
998 pa_hook_init(&y->hooks[i], y);
999
1000 pa_shared_set(c, "bluetooth-discovery", y);
1001
1002 dbus_error_init(&err);
1003
1004 if (!(y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err))) {
1005 pa_log_error("Failed to get D-Bus connection: %s", err.message);
1006 goto fail;
1007 }
1008
1009 conn = pa_dbus_connection_get(y->connection);
1010
1011 /* dynamic detection of bluetooth audio devices */
1012 if (!dbus_connection_add_filter(conn, filter_cb, y, NULL)) {
1013 pa_log_error("Failed to add filter function");
1014 goto fail;
1015 }
1016 y->filter_added = true;
1017
1018 if (pa_dbus_add_matches(conn, &err,
1019 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'"
1020 ",arg0='" BLUEZ_SERVICE "'",
1021 NULL) < 0) {
1022 pa_log_error("Failed to add D-Bus matches: %s", err.message);
1023 goto fail;
1024 }
1025 y->matches_added = true;
1026
1027 endpoint_init(y, PA_BLUETOOTH_PROFILE_A2DP_SINK);
1028 endpoint_init(y, PA_BLUETOOTH_PROFILE_A2DP_SOURCE);
1029
1030 get_managed_objects(y);
1031
1032 return y;
1033
1034 fail:
1035 pa_bluetooth_discovery_unref(y);
1036 dbus_error_free(&err);
1037
1038 return NULL;
1039 }
1040
1041 pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
1042 pa_assert(y);
1043 pa_assert(PA_REFCNT_VALUE(y) > 0);
1044
1045 PA_REFCNT_INC(y);
1046
1047 return y;
1048 }
1049
1050 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
1051 pa_assert(y);
1052 pa_assert(PA_REFCNT_VALUE(y) > 0);
1053
1054 if (PA_REFCNT_DEC(y) > 0)
1055 return;
1056
1057 pa_dbus_free_pending_list(&y->pending);
1058
1059 if (y->devices) {
1060 device_remove_all(y);
1061 pa_hashmap_free(y->devices);
1062 }
1063
1064 if (y->adapters) {
1065 adapter_remove_all(y);
1066 pa_hashmap_free(y->adapters);
1067 }
1068
1069 if (y->transports) {
1070 pa_assert(pa_hashmap_isempty(y->transports));
1071 pa_hashmap_free(y->transports);
1072 }
1073
1074 if (y->connection) {
1075
1076 if (y->matches_added)
1077 pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
1078 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',"
1079 "arg0='" BLUEZ_SERVICE "'",
1080 NULL);
1081
1082 if (y->filter_added)
1083 dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
1084
1085 endpoint_done(y, PA_BLUETOOTH_PROFILE_A2DP_SINK);
1086 endpoint_done(y, PA_BLUETOOTH_PROFILE_A2DP_SOURCE);
1087
1088 pa_dbus_connection_unref(y->connection);
1089 }
1090
1091 pa_shared_remove(y->core, "bluetooth-discovery");
1092 pa_xfree(y);
1093 }