]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/bluetooth-util.c
bluetooth: Add a pa_bluetooth_discovery pointer to pa_bluetooth_device.
[pulseaudio] / src / modules / bluetooth / bluetooth-util.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008-2009 Joao 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-util.h>
29 #include <pulsecore/shared.h>
30 #include <pulsecore/dbus-shared.h>
31
32 #include "bluetooth-util.h"
33 #include "a2dp-codecs.h"
34
35 #define HFP_AG_ENDPOINT "/MediaEndpoint/HFPAG"
36 #define HFP_HS_ENDPOINT "/MediaEndpoint/HFPHS"
37 #define A2DP_SOURCE_ENDPOINT "/MediaEndpoint/A2DPSource"
38 #define A2DP_SINK_ENDPOINT "/MediaEndpoint/A2DPSink"
39
40 #define ENDPOINT_INTROSPECT_XML \
41 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
42 "<node>" \
43 " <interface name=\"org.bluez.MediaEndpoint\">" \
44 " <method name=\"SetConfiguration\">" \
45 " <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
46 " <arg name=\"configuration\" direction=\"in\" type=\"ay\"/>" \
47 " </method>" \
48 " <method name=\"SelectConfiguration\">" \
49 " <arg name=\"capabilities\" direction=\"in\" type=\"ay\"/>" \
50 " <arg name=\"configuration\" direction=\"out\" type=\"ay\"/>" \
51 " </method>" \
52 " <method name=\"ClearConfiguration\">" \
53 " </method>" \
54 " <method name=\"Release\">" \
55 " </method>" \
56 " </interface>" \
57 " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
58 " <method name=\"Introspect\">" \
59 " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
60 " </method>" \
61 " </interface>" \
62 "</node>"
63
64 struct pa_bluetooth_discovery {
65 PA_REFCNT_DECLARE;
66
67 pa_core *core;
68 pa_dbus_connection *connection;
69 PA_LLIST_HEAD(pa_dbus_pending, pending);
70 pa_hashmap *devices;
71 pa_hook hook;
72 pa_bool_t filter_added;
73 };
74
75 static void get_properties_reply(DBusPendingCall *pending, void *userdata);
76 static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, DBusMessage *m, DBusPendingCallNotifyFunction func, void *call_data);
77 static void found_adapter(pa_bluetooth_discovery *y, const char *path);
78 static pa_bluetooth_device *found_device(pa_bluetooth_discovery *y, const char* path);
79
80 pa_bt_audio_state_t pa_bt_audio_state_from_string(const char* value) {
81 pa_assert(value);
82
83 if (pa_streq(value, "disconnected"))
84 return PA_BT_AUDIO_STATE_DISCONNECTED;
85 else if (pa_streq(value, "connecting"))
86 return PA_BT_AUDIO_STATE_CONNECTING;
87 else if (pa_streq(value, "connected"))
88 return PA_BT_AUDIO_STATE_CONNECTED;
89 else if (pa_streq(value, "playing"))
90 return PA_BT_AUDIO_STATE_PLAYING;
91
92 return PA_BT_AUDIO_STATE_INVALID;
93 }
94
95 static pa_bluetooth_uuid *uuid_new(const char *uuid) {
96 pa_bluetooth_uuid *u;
97
98 u = pa_xnew(pa_bluetooth_uuid, 1);
99 u->uuid = pa_xstrdup(uuid);
100 PA_LLIST_INIT(pa_bluetooth_uuid, u);
101
102 return u;
103 }
104
105 static void uuid_free(pa_bluetooth_uuid *u) {
106 pa_assert(u);
107
108 pa_xfree(u->uuid);
109 pa_xfree(u);
110 }
111
112 static pa_bluetooth_device* device_new(pa_bluetooth_discovery *discovery, const char *path) {
113 pa_bluetooth_device *d;
114 unsigned i;
115
116 pa_assert(discovery);
117 pa_assert(path);
118
119 d = pa_xnew(pa_bluetooth_device, 1);
120
121 d->discovery = discovery;
122 d->dead = FALSE;
123
124 d->device_info_valid = 0;
125
126 d->name = NULL;
127 d->path = pa_xstrdup(path);
128 d->transports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
129 d->paired = -1;
130 d->alias = NULL;
131 d->device_connected = -1;
132 PA_LLIST_HEAD_INIT(pa_bluetooth_uuid, d->uuids);
133 d->address = NULL;
134 d->class = -1;
135 d->trusted = -1;
136
137 d->audio_state = PA_BT_AUDIO_STATE_INVALID;
138 d->audio_sink_state = PA_BT_AUDIO_STATE_INVALID;
139 d->audio_source_state = PA_BT_AUDIO_STATE_INVALID;
140 d->headset_state = PA_BT_AUDIO_STATE_INVALID;
141 d->hfgw_state = PA_BT_AUDIO_STATE_INVALID;
142
143 for (i = 0; i < PA_BLUETOOTH_DEVICE_HOOK_MAX; i++)
144 pa_hook_init(&d->hooks[i], d);
145
146 return d;
147 }
148
149 static void transport_free(pa_bluetooth_transport *t) {
150 unsigned i;
151
152 pa_assert(t);
153
154 for (i = 0; i < PA_BLUETOOTH_TRANSPORT_HOOK_MAX; i++)
155 pa_hook_done(&t->hooks[i]);
156
157 pa_xfree(t->path);
158 pa_xfree(t->config);
159 pa_xfree(t);
160 }
161
162 static void device_free(pa_bluetooth_device *d) {
163 pa_bluetooth_uuid *u;
164 pa_bluetooth_transport *t;
165 unsigned i;
166
167 pa_assert(d);
168
169 while ((t = pa_hashmap_steal_first(d->transports))) {
170 pa_hook_fire(&t->hooks[PA_BLUETOOTH_TRANSPORT_HOOK_REMOVED], NULL);
171 transport_free(t);
172 }
173
174 pa_hashmap_free(d->transports, NULL, NULL);
175
176 for (i = 0; i < PA_BLUETOOTH_DEVICE_HOOK_MAX; i++)
177 pa_hook_done(&d->hooks[i]);
178
179 while ((u = d->uuids)) {
180 PA_LLIST_REMOVE(pa_bluetooth_uuid, d->uuids, u);
181 uuid_free(u);
182 }
183
184 pa_xfree(d->name);
185 pa_xfree(d->path);
186 pa_xfree(d->alias);
187 pa_xfree(d->address);
188 pa_xfree(d);
189 }
190
191 static pa_bool_t device_is_audio(pa_bluetooth_device *d) {
192 pa_assert(d);
193
194 return
195 d->device_info_valid && (d->hfgw_state != PA_BT_AUDIO_STATE_INVALID ||
196 (d->audio_state != PA_BT_AUDIO_STATE_INVALID &&
197 (d->audio_sink_state != PA_BT_AUDIO_STATE_INVALID ||
198 d->audio_source_state != PA_BT_AUDIO_STATE_INVALID ||
199 d->headset_state != PA_BT_AUDIO_STATE_INVALID)));
200 }
201
202 static const char *check_variant_property(DBusMessageIter *i) {
203 const char *key;
204
205 pa_assert(i);
206
207 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
208 pa_log("Property name not a string.");
209 return NULL;
210 }
211
212 dbus_message_iter_get_basic(i, &key);
213
214 if (!dbus_message_iter_next(i)) {
215 pa_log("Property value missing");
216 return NULL;
217 }
218
219 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
220 pa_log("Property value not a variant.");
221 return NULL;
222 }
223
224 return key;
225 }
226
227 static int parse_manager_property(pa_bluetooth_discovery *y, DBusMessageIter *i) {
228 const char *key;
229 DBusMessageIter variant_i;
230
231 pa_assert(y);
232
233 key = check_variant_property(i);
234 if (key == NULL)
235 return -1;
236
237 dbus_message_iter_recurse(i, &variant_i);
238
239 switch (dbus_message_iter_get_arg_type(&variant_i)) {
240
241 case DBUS_TYPE_ARRAY: {
242
243 DBusMessageIter ai;
244 dbus_message_iter_recurse(&variant_i, &ai);
245
246 if (dbus_message_iter_get_arg_type(&ai) == DBUS_TYPE_OBJECT_PATH &&
247 pa_streq(key, "Adapters")) {
248
249 while (dbus_message_iter_get_arg_type(&ai) != DBUS_TYPE_INVALID) {
250 const char *value;
251
252 dbus_message_iter_get_basic(&ai, &value);
253
254 found_adapter(y, value);
255
256 dbus_message_iter_next(&ai);
257 }
258 }
259
260 break;
261 }
262 }
263
264 return 0;
265 }
266
267 static int parse_adapter_property(pa_bluetooth_discovery *y, DBusMessageIter *i) {
268 const char *key;
269 DBusMessageIter variant_i;
270
271 pa_assert(y);
272
273 key = check_variant_property(i);
274 if (key == NULL)
275 return -1;
276
277 dbus_message_iter_recurse(i, &variant_i);
278
279 switch (dbus_message_iter_get_arg_type(&variant_i)) {
280
281 case DBUS_TYPE_ARRAY: {
282
283 DBusMessageIter ai;
284 dbus_message_iter_recurse(&variant_i, &ai);
285
286 if (dbus_message_iter_get_arg_type(&ai) == DBUS_TYPE_OBJECT_PATH &&
287 pa_streq(key, "Devices")) {
288
289 while (dbus_message_iter_get_arg_type(&ai) != DBUS_TYPE_INVALID) {
290 const char *value;
291
292 dbus_message_iter_get_basic(&ai, &value);
293
294 found_device(y, value);
295
296 dbus_message_iter_next(&ai);
297 }
298 }
299
300 break;
301 }
302 }
303
304 return 0;
305 }
306
307 static int parse_device_property(pa_bluetooth_device *d, DBusMessageIter *i) {
308 const char *key;
309 DBusMessageIter variant_i;
310
311 pa_assert(d);
312
313 key = check_variant_property(i);
314 if (key == NULL)
315 return -1;
316
317 dbus_message_iter_recurse(i, &variant_i);
318
319 /* pa_log_debug("Parsing property org.bluez.Device.%s", key); */
320
321 switch (dbus_message_iter_get_arg_type(&variant_i)) {
322
323 case DBUS_TYPE_STRING: {
324
325 const char *value;
326 dbus_message_iter_get_basic(&variant_i, &value);
327
328 if (pa_streq(key, "Name")) {
329 pa_xfree(d->name);
330 d->name = pa_xstrdup(value);
331 } else if (pa_streq(key, "Alias")) {
332 pa_xfree(d->alias);
333 d->alias = pa_xstrdup(value);
334 } else if (pa_streq(key, "Address")) {
335 pa_xfree(d->address);
336 d->address = pa_xstrdup(value);
337 }
338
339 /* pa_log_debug("Value %s", value); */
340
341 break;
342 }
343
344 case DBUS_TYPE_BOOLEAN: {
345
346 dbus_bool_t value;
347 dbus_message_iter_get_basic(&variant_i, &value);
348
349 if (pa_streq(key, "Paired"))
350 d->paired = !!value;
351 else if (pa_streq(key, "Connected"))
352 d->device_connected = !!value;
353 else if (pa_streq(key, "Trusted"))
354 d->trusted = !!value;
355
356 /* pa_log_debug("Value %s", pa_yes_no(value)); */
357
358 break;
359 }
360
361 case DBUS_TYPE_UINT32: {
362
363 uint32_t value;
364 dbus_message_iter_get_basic(&variant_i, &value);
365
366 if (pa_streq(key, "Class"))
367 d->class = (int) value;
368
369 /* pa_log_debug("Value %u", (unsigned) value); */
370
371 break;
372 }
373
374 case DBUS_TYPE_ARRAY: {
375
376 DBusMessageIter ai;
377 dbus_message_iter_recurse(&variant_i, &ai);
378
379 if (dbus_message_iter_get_arg_type(&ai) == DBUS_TYPE_STRING &&
380 pa_streq(key, "UUIDs")) {
381 DBusMessage *m;
382 pa_bool_t has_audio = FALSE;
383
384 while (dbus_message_iter_get_arg_type(&ai) != DBUS_TYPE_INVALID) {
385 pa_bluetooth_uuid *node;
386 const char *value;
387
388 dbus_message_iter_get_basic(&ai, &value);
389
390 if (pa_bluetooth_uuid_has(d->uuids, value)) {
391 dbus_message_iter_next(&ai);
392 continue;
393 }
394
395 node = uuid_new(value);
396 PA_LLIST_PREPEND(pa_bluetooth_uuid, d->uuids, node);
397
398 pa_hook_fire(&d->hooks[PA_BLUETOOTH_DEVICE_HOOK_UUID_ADDED], (char *) value);
399
400 /* Vudentz said the interfaces are here when the UUIDs are announced */
401 if (strcasecmp(HSP_AG_UUID, value) == 0 || strcasecmp(HFP_AG_UUID, value) == 0) {
402 pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.HandsfreeGateway", "GetProperties"));
403 send_and_add_to_pending(d->discovery, m, get_properties_reply, d);
404 has_audio = TRUE;
405 } else if (strcasecmp(HSP_HS_UUID, value) == 0 || strcasecmp(HFP_HS_UUID, value) == 0) {
406 pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.Headset", "GetProperties"));
407 send_and_add_to_pending(d->discovery, m, get_properties_reply, d);
408 has_audio = TRUE;
409 } else if (strcasecmp(A2DP_SINK_UUID, value) == 0) {
410 pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.AudioSink", "GetProperties"));
411 send_and_add_to_pending(d->discovery, m, get_properties_reply, d);
412 has_audio = TRUE;
413 } else if (strcasecmp(A2DP_SOURCE_UUID, value) == 0) {
414 pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.AudioSource", "GetProperties"));
415 send_and_add_to_pending(d->discovery, m, get_properties_reply, d);
416 has_audio = TRUE;
417 }
418
419 dbus_message_iter_next(&ai);
420 }
421
422 /* this might eventually be racy if .Audio is not there yet, but the State change will come anyway later, so this call is for cold-detection mostly */
423 if (has_audio) {
424 pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.Audio", "GetProperties"));
425 send_and_add_to_pending(d->discovery, m, get_properties_reply, d);
426 }
427 }
428
429 break;
430 }
431 }
432
433 return 0;
434 }
435
436 static int parse_audio_property(pa_bluetooth_discovery *u, int *state, DBusMessageIter *i) {
437 const char *key;
438 DBusMessageIter variant_i;
439
440 pa_assert(u);
441 pa_assert(state);
442
443 key = check_variant_property(i);
444 if (key == NULL)
445 return -1;
446
447 dbus_message_iter_recurse(i, &variant_i);
448
449 /* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|AudioSource|Headset}.%s", key); */
450
451 switch (dbus_message_iter_get_arg_type(&variant_i)) {
452
453 case DBUS_TYPE_STRING: {
454
455 const char *value;
456 dbus_message_iter_get_basic(&variant_i, &value);
457
458 if (pa_streq(key, "State")) {
459 *state = pa_bt_audio_state_from_string(value);
460 pa_log_debug("dbus: property 'State' changed to value '%s'", value);
461 }
462
463 break;
464 }
465 }
466
467 return 0;
468 }
469
470 static void run_callback(pa_bluetooth_device *d, pa_bool_t dead) {
471 pa_assert(d);
472
473 if (!device_is_audio(d))
474 return;
475
476 d->dead = dead;
477 pa_hook_fire(&d->discovery->hook, d);
478 }
479
480 static void remove_all_devices(pa_bluetooth_discovery *y) {
481 pa_bluetooth_device *d;
482
483 pa_assert(y);
484
485 while ((d = pa_hashmap_steal_first(y->devices))) {
486 pa_hook_fire(&d->hooks[PA_BLUETOOTH_DEVICE_HOOK_REMOVED], NULL);
487 run_callback(d, TRUE);
488 device_free(d);
489 }
490 }
491
492 static pa_bluetooth_device *found_device(pa_bluetooth_discovery *y, const char* path) {
493 DBusMessage *m;
494 pa_bluetooth_device *d;
495
496 pa_assert(y);
497 pa_assert(path);
498
499 d = pa_hashmap_get(y->devices, path);
500 if (d)
501 return d;
502
503 d = device_new(y, path);
504
505 pa_hashmap_put(y->devices, d->path, d);
506
507 pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Device", "GetProperties"));
508 send_and_add_to_pending(y, m, get_properties_reply, d);
509
510 /* Before we read the other properties (Audio, AudioSink, AudioSource,
511 * Headset) we wait that the UUID is read */
512 return d;
513 }
514
515 static void get_properties_reply(DBusPendingCall *pending, void *userdata) {
516 DBusMessage *r;
517 DBusMessageIter arg_i, element_i;
518 pa_dbus_pending *p;
519 pa_bluetooth_device *d;
520 pa_bluetooth_discovery *y;
521 int valid;
522
523 pa_assert_se(p = userdata);
524 pa_assert_se(y = p->context_data);
525 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
526
527 /* pa_log_debug("Got %s.GetProperties response for %s", */
528 /* dbus_message_get_interface(p->message), */
529 /* dbus_message_get_path(p->message)); */
530
531 /* We don't use p->call_data here right-away since the device
532 * might already be invalidated at this point */
533
534 if (dbus_message_has_interface(p->message, "org.bluez.Manager") ||
535 dbus_message_has_interface(p->message, "org.bluez.Adapter"))
536 d = NULL;
537 else
538 d = pa_hashmap_get(y->devices, dbus_message_get_path(p->message));
539
540 pa_assert(p->call_data == d);
541
542 valid = dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR ? -1 : 1;
543
544 if (dbus_message_is_method_call(p->message, "org.bluez.Device", "GetProperties"))
545 d->device_info_valid = valid;
546
547 if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
548 pa_log_debug("Bluetooth daemon is apparently not available.");
549 remove_all_devices(y);
550 goto finish2;
551 }
552
553 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
554 pa_log("%s.GetProperties() failed: %s: %s", dbus_message_get_interface(p->message), dbus_message_get_error_name(r), pa_dbus_get_error_message(r));
555 goto finish;
556 }
557
558 if (!dbus_message_iter_init(r, &arg_i)) {
559 pa_log("GetProperties reply has no arguments.");
560 goto finish;
561 }
562
563 if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) {
564 pa_log("GetProperties argument is not an array.");
565 goto finish;
566 }
567
568 dbus_message_iter_recurse(&arg_i, &element_i);
569 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
570
571 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
572 DBusMessageIter dict_i;
573
574 dbus_message_iter_recurse(&element_i, &dict_i);
575
576 if (dbus_message_has_interface(p->message, "org.bluez.Manager")) {
577 if (parse_manager_property(y, &dict_i) < 0)
578 goto finish;
579
580 } else if (dbus_message_has_interface(p->message, "org.bluez.Adapter")) {
581 if (parse_adapter_property(y, &dict_i) < 0)
582 goto finish;
583
584 } else if (dbus_message_has_interface(p->message, "org.bluez.Device")) {
585 if (parse_device_property(d, &dict_i) < 0)
586 goto finish;
587
588 } else if (dbus_message_has_interface(p->message, "org.bluez.Audio")) {
589 if (parse_audio_property(y, &d->audio_state, &dict_i) < 0)
590 goto finish;
591
592 } else if (dbus_message_has_interface(p->message, "org.bluez.Headset")) {
593 if (parse_audio_property(y, &d->headset_state, &dict_i) < 0)
594 goto finish;
595
596 } else if (dbus_message_has_interface(p->message, "org.bluez.AudioSink")) {
597 if (parse_audio_property(y, &d->audio_sink_state, &dict_i) < 0)
598 goto finish;
599
600 } else if (dbus_message_has_interface(p->message, "org.bluez.AudioSource")) {
601 if (parse_audio_property(y, &d->audio_source_state, &dict_i) < 0)
602 goto finish;
603
604 } else if (dbus_message_has_interface(p->message, "org.bluez.HandsfreeGateway")) {
605 if (parse_audio_property(y, &d->hfgw_state, &dict_i) < 0)
606 goto finish;
607
608 }
609 }
610
611 dbus_message_iter_next(&element_i);
612 }
613
614 finish:
615 if (d != NULL)
616 run_callback(d, FALSE);
617
618 finish2:
619 dbus_message_unref(r);
620
621 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
622 pa_dbus_pending_free(p);
623 }
624
625 static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, DBusMessage *m, DBusPendingCallNotifyFunction func, void *call_data) {
626 pa_dbus_pending *p;
627 DBusPendingCall *call;
628
629 pa_assert(y);
630 pa_assert(m);
631
632 pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y->connection), m, &call, -1));
633
634 p = pa_dbus_pending_new(pa_dbus_connection_get(y->connection), m, call, y, call_data);
635 PA_LLIST_PREPEND(pa_dbus_pending, y->pending, p);
636 dbus_pending_call_set_notify(call, func, p, NULL);
637
638 return p;
639 }
640
641 static void register_endpoint_reply(DBusPendingCall *pending, void *userdata) {
642 DBusError e;
643 DBusMessage *r;
644 pa_dbus_pending *p;
645 pa_bluetooth_discovery *y;
646 char *endpoint;
647
648 pa_assert(pending);
649
650 dbus_error_init(&e);
651
652 pa_assert_se(p = userdata);
653 pa_assert_se(y = p->context_data);
654 pa_assert_se(endpoint = p->call_data);
655 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
656
657 if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
658 pa_log_debug("Bluetooth daemon is apparently not available.");
659 remove_all_devices(y);
660 goto finish;
661 }
662
663 if (dbus_message_is_error(r, PA_BLUETOOTH_ERROR_NOT_SUPPORTED)) {
664 pa_log_info("Couldn't register endpoint %s, because BlueZ is configured to disable the endpoint type.", endpoint);
665 goto finish;
666 }
667
668 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
669 pa_log("org.bluez.Media.RegisterEndpoint() failed: %s: %s", dbus_message_get_error_name(r), pa_dbus_get_error_message(r));
670 goto finish;
671 }
672
673 finish:
674 dbus_message_unref(r);
675
676 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
677 pa_dbus_pending_free(p);
678
679 pa_xfree(endpoint);
680 }
681
682 static void register_endpoint(pa_bluetooth_discovery *y, const char *path, const char *endpoint, const char *uuid) {
683 DBusMessage *m;
684 DBusMessageIter i, d;
685 uint8_t codec = 0;
686
687 pa_log_debug("Registering %s on adapter %s.", endpoint, path);
688
689 pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Media", "RegisterEndpoint"));
690
691 dbus_message_iter_init_append(m, &i);
692
693 dbus_message_iter_append_basic(&i, DBUS_TYPE_OBJECT_PATH, &endpoint);
694
695 dbus_message_iter_open_container(&i, DBUS_TYPE_ARRAY, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
696 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
697 &d);
698
699 pa_dbus_append_basic_variant_dict_entry(&d, "UUID", DBUS_TYPE_STRING, &uuid);
700
701 pa_dbus_append_basic_variant_dict_entry(&d, "Codec", DBUS_TYPE_BYTE, &codec);
702
703 if (pa_streq(uuid, HFP_AG_UUID) || pa_streq(uuid, HFP_HS_UUID)) {
704 uint8_t capability = 0;
705 pa_dbus_append_basic_array_variant_dict_entry(&d, "Capabilities", DBUS_TYPE_BYTE, &capability, 1);
706 } else {
707 a2dp_sbc_t capabilities;
708
709 capabilities.channel_mode = SBC_CHANNEL_MODE_MONO | SBC_CHANNEL_MODE_DUAL_CHANNEL |
710 SBC_CHANNEL_MODE_STEREO | SBC_CHANNEL_MODE_JOINT_STEREO;
711 capabilities.frequency = SBC_SAMPLING_FREQ_16000 | SBC_SAMPLING_FREQ_32000 |
712 SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_48000;
713 capabilities.allocation_method = SBC_ALLOCATION_SNR | SBC_ALLOCATION_LOUDNESS;
714 capabilities.subbands = SBC_SUBBANDS_4 | SBC_SUBBANDS_8;
715 capabilities.block_length = SBC_BLOCK_LENGTH_4 | SBC_BLOCK_LENGTH_8 |
716 SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_16;
717 capabilities.min_bitpool = MIN_BITPOOL;
718 capabilities.max_bitpool = MAX_BITPOOL;
719
720 pa_dbus_append_basic_array_variant_dict_entry(&d, "Capabilities", DBUS_TYPE_BYTE, &capabilities, sizeof(capabilities));
721 }
722
723 dbus_message_iter_close_container(&i, &d);
724
725 send_and_add_to_pending(y, m, register_endpoint_reply, pa_xstrdup(endpoint));
726 }
727
728 static void found_adapter(pa_bluetooth_discovery *y, const char *path) {
729 DBusMessage *m;
730
731 pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Adapter", "GetProperties"));
732 send_and_add_to_pending(y, m, get_properties_reply, NULL);
733
734 register_endpoint(y, path, HFP_AG_ENDPOINT, HFP_AG_UUID);
735 register_endpoint(y, path, HFP_HS_ENDPOINT, HFP_HS_UUID);
736 register_endpoint(y, path, A2DP_SOURCE_ENDPOINT, A2DP_SOURCE_UUID);
737 register_endpoint(y, path, A2DP_SINK_ENDPOINT, A2DP_SINK_UUID);
738 }
739
740 static void list_adapters(pa_bluetooth_discovery *y) {
741 DBusMessage *m;
742 pa_assert(y);
743
744 pa_assert_se(m = dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "GetProperties"));
745 send_and_add_to_pending(y, m, get_properties_reply, NULL);
746 }
747
748 int pa_bluetooth_transport_parse_property(pa_bluetooth_transport *t, DBusMessageIter *i)
749 {
750 const char *key;
751 DBusMessageIter variant_i;
752
753 key = check_variant_property(i);
754 if (key == NULL)
755 return -1;
756
757 dbus_message_iter_recurse(i, &variant_i);
758
759 switch (dbus_message_iter_get_arg_type(&variant_i)) {
760
761 case DBUS_TYPE_BOOLEAN: {
762
763 dbus_bool_t value;
764 dbus_message_iter_get_basic(&variant_i, &value);
765
766 if (pa_streq(key, "NREC") && t->nrec != value) {
767 t->nrec = value;
768 pa_log_debug("Transport %s: Property 'NREC' changed to %s.", t->path, t->nrec ? "True" : "False");
769 pa_hook_fire(&t->hooks[PA_BLUETOOTH_TRANSPORT_HOOK_NREC_CHANGED], NULL);
770 }
771
772 break;
773 }
774 }
775
776 return 0;
777 }
778
779 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
780 DBusError err;
781 pa_bluetooth_discovery *y;
782
783 pa_assert(bus);
784 pa_assert(m);
785
786 pa_assert_se(y = userdata);
787
788 dbus_error_init(&err);
789
790 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
791 dbus_message_get_interface(m),
792 dbus_message_get_path(m),
793 dbus_message_get_member(m));
794
795 if (dbus_message_is_signal(m, "org.bluez.Adapter", "DeviceRemoved")) {
796 const char *path;
797 pa_bluetooth_device *d;
798
799 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
800 pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err.message);
801 goto fail;
802 }
803
804 pa_log_debug("Device %s removed", path);
805
806 if ((d = pa_hashmap_remove(y->devices, path))) {
807 pa_hook_fire(&d->hooks[PA_BLUETOOTH_DEVICE_HOOK_REMOVED], NULL);
808 run_callback(d, TRUE);
809 device_free(d);
810 }
811
812 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
813
814 } else if (dbus_message_is_signal(m, "org.bluez.Adapter", "DeviceCreated")) {
815 const char *path;
816
817 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
818 pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err.message);
819 goto fail;
820 }
821
822 pa_log_debug("Device %s created", path);
823
824 found_device(y, path);
825 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
826
827 } else if (dbus_message_is_signal(m, "org.bluez.Manager", "AdapterAdded")) {
828 const char *path;
829
830 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
831 pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err.message);
832 goto fail;
833 }
834
835 pa_log_debug("Adapter %s created", path);
836
837 found_adapter(y, path);
838 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
839
840 } else if (dbus_message_is_signal(m, "org.bluez.Audio", "PropertyChanged") ||
841 dbus_message_is_signal(m, "org.bluez.Headset", "PropertyChanged") ||
842 dbus_message_is_signal(m, "org.bluez.AudioSink", "PropertyChanged") ||
843 dbus_message_is_signal(m, "org.bluez.AudioSource", "PropertyChanged") ||
844 dbus_message_is_signal(m, "org.bluez.HandsfreeGateway", "PropertyChanged") ||
845 dbus_message_is_signal(m, "org.bluez.Device", "PropertyChanged")) {
846
847 pa_bluetooth_device *d;
848
849 if ((d = pa_hashmap_get(y->devices, dbus_message_get_path(m)))) {
850 DBusMessageIter arg_i;
851
852 if (!dbus_message_iter_init(m, &arg_i)) {
853 pa_log("Failed to parse PropertyChanged: %s", err.message);
854 goto fail;
855 }
856
857 if (dbus_message_has_interface(m, "org.bluez.Device")) {
858 if (parse_device_property(d, &arg_i) < 0)
859 goto fail;
860
861 } else if (dbus_message_has_interface(m, "org.bluez.Audio")) {
862 if (parse_audio_property(y, &d->audio_state, &arg_i) < 0)
863 goto fail;
864
865 } else if (dbus_message_has_interface(m, "org.bluez.Headset")) {
866 if (parse_audio_property(y, &d->headset_state, &arg_i) < 0)
867 goto fail;
868
869 } else if (dbus_message_has_interface(m, "org.bluez.AudioSink")) {
870 if (parse_audio_property(y, &d->audio_sink_state, &arg_i) < 0)
871 goto fail;
872
873 } else if (dbus_message_has_interface(m, "org.bluez.AudioSource")) {
874 if (parse_audio_property(y, &d->audio_source_state, &arg_i) < 0)
875 goto fail;
876
877 } else if (dbus_message_has_interface(m, "org.bluez.HandsfreeGateway")) {
878 if (parse_audio_property(y, &d->hfgw_state, &arg_i) < 0)
879 goto fail;
880 }
881
882 run_callback(d, FALSE);
883 }
884
885 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
886
887 } else if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
888 const char *name, *old_owner, *new_owner;
889
890 if (!dbus_message_get_args(m, &err,
891 DBUS_TYPE_STRING, &name,
892 DBUS_TYPE_STRING, &old_owner,
893 DBUS_TYPE_STRING, &new_owner,
894 DBUS_TYPE_INVALID)) {
895 pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
896 goto fail;
897 }
898
899 if (pa_streq(name, "org.bluez")) {
900 if (old_owner && *old_owner) {
901 pa_log_debug("Bluetooth daemon disappeared.");
902 remove_all_devices(y);
903 }
904
905 if (new_owner && *new_owner) {
906 pa_log_debug("Bluetooth daemon appeared.");
907 list_adapters(y);
908 }
909 }
910
911 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
912 } else if (dbus_message_is_signal(m, "org.bluez.MediaTransport", "PropertyChanged")) {
913 pa_bluetooth_device *d;
914 pa_bluetooth_transport *t = NULL;
915 void *state = NULL;
916 DBusMessageIter arg_i;
917
918 while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
919 if ((t = pa_hashmap_get(d->transports, dbus_message_get_path(m))))
920 break;
921
922 if (!t)
923 goto fail;
924
925 if (!dbus_message_iter_init(m, &arg_i)) {
926 pa_log("Failed to parse PropertyChanged: %s", err.message);
927 goto fail;
928 }
929
930 if (pa_bluetooth_transport_parse_property(t, &arg_i) < 0)
931 goto fail;
932
933 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
934 }
935
936 fail:
937 dbus_error_free(&err);
938
939 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
940 }
941
942 pa_bluetooth_device* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery *y, const char* address) {
943 pa_bluetooth_device *d;
944 void *state = NULL;
945
946 pa_assert(y);
947 pa_assert(PA_REFCNT_VALUE(y) > 0);
948 pa_assert(address);
949
950 if (!pa_hook_is_firing(&y->hook))
951 pa_bluetooth_discovery_sync(y);
952
953 while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
954 if (pa_streq(d->address, address))
955 return device_is_audio(d) ? d : NULL;
956
957 return NULL;
958 }
959
960 pa_bluetooth_device* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery *y, const char* path) {
961 pa_bluetooth_device *d;
962
963 pa_assert(y);
964 pa_assert(PA_REFCNT_VALUE(y) > 0);
965 pa_assert(path);
966
967 if (!pa_hook_is_firing(&y->hook))
968 pa_bluetooth_discovery_sync(y);
969
970 if ((d = pa_hashmap_get(y->devices, path)))
971 if (device_is_audio(d))
972 return d;
973
974 return NULL;
975 }
976
977 pa_bluetooth_transport* pa_bluetooth_discovery_get_transport(pa_bluetooth_discovery *y, const char *path) {
978 pa_bluetooth_device *d;
979 pa_bluetooth_transport *t;
980 void *state = NULL;
981
982 pa_assert(y);
983 pa_assert(PA_REFCNT_VALUE(y) > 0);
984 pa_assert(path);
985
986 while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
987 if ((t = pa_hashmap_get(d->transports, path)))
988 return t;
989
990 return NULL;
991 }
992
993 pa_bluetooth_transport* pa_bluetooth_device_get_transport(pa_bluetooth_device *d, enum profile profile) {
994 pa_bluetooth_transport *t;
995 void *state = NULL;
996
997 pa_assert(d);
998
999 while ((t = pa_hashmap_iterate(d->transports, &state, NULL)))
1000 if (t->profile == profile)
1001 return t;
1002
1003 return NULL;
1004 }
1005
1006 int pa_bluetooth_transport_acquire(pa_bluetooth_transport *t, const char *accesstype, size_t *imtu, size_t *omtu) {
1007 DBusMessage *m, *r;
1008 DBusError err;
1009 int ret;
1010 uint16_t i, o;
1011
1012 pa_assert(t);
1013 pa_assert(t->y);
1014
1015 dbus_error_init(&err);
1016
1017 pa_assert_se(m = dbus_message_new_method_call("org.bluez", t->path, "org.bluez.MediaTransport", "Acquire"));
1018 pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_STRING, &accesstype, DBUS_TYPE_INVALID));
1019 r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->y->connection), m, -1, &err);
1020
1021 if (dbus_error_is_set(&err) || !r) {
1022 dbus_error_free(&err);
1023 return -1;
1024 }
1025
1026 if (!dbus_message_get_args(r, &err, DBUS_TYPE_UNIX_FD, &ret, DBUS_TYPE_UINT16, &i, DBUS_TYPE_UINT16, &o, DBUS_TYPE_INVALID)) {
1027 pa_log("Failed to parse org.bluez.MediaTransport.Acquire(): %s", err.message);
1028 ret = -1;
1029 dbus_error_free(&err);
1030 goto fail;
1031 }
1032
1033 if (imtu)
1034 *imtu = i;
1035
1036 if (omtu)
1037 *omtu = o;
1038
1039 fail:
1040 dbus_message_unref(r);
1041 return ret;
1042 }
1043
1044 void pa_bluetooth_transport_release(pa_bluetooth_transport *t, const char *accesstype) {
1045 DBusMessage *m;
1046 DBusError err;
1047
1048 pa_assert(t);
1049 pa_assert(t->y);
1050
1051 dbus_error_init(&err);
1052
1053 pa_assert_se(m = dbus_message_new_method_call("org.bluez", t->path, "org.bluez.MediaTransport", "Release"));
1054 pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_STRING, &accesstype, DBUS_TYPE_INVALID));
1055 dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->y->connection), m, -1, &err);
1056
1057 if (dbus_error_is_set(&err)) {
1058 pa_log("Failed to release transport %s: %s", t->path, err.message);
1059 dbus_error_free(&err);
1060 } else
1061 pa_log_info("Transport %s released", t->path);
1062 }
1063
1064 static int setup_dbus(pa_bluetooth_discovery *y) {
1065 DBusError err;
1066
1067 dbus_error_init(&err);
1068
1069 y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err);
1070
1071 if (dbus_error_is_set(&err) || !y->connection) {
1072 pa_log("Failed to get D-Bus connection: %s", err.message);
1073 dbus_error_free(&err);
1074 return -1;
1075 }
1076
1077 return 0;
1078 }
1079
1080 static pa_bluetooth_transport *transport_new(pa_bluetooth_discovery *y, const char *path, enum profile p, const uint8_t *config, int size) {
1081 pa_bluetooth_transport *t;
1082 unsigned i;
1083
1084 t = pa_xnew0(pa_bluetooth_transport, 1);
1085 t->y = y;
1086 t->path = pa_xstrdup(path);
1087 t->profile = p;
1088 t->config_size = size;
1089
1090 if (size > 0) {
1091 t->config = pa_xnew(uint8_t, size);
1092 memcpy(t->config, config, size);
1093 }
1094
1095 for (i = 0; i < PA_BLUETOOTH_TRANSPORT_HOOK_MAX; i++)
1096 pa_hook_init(&t->hooks[i], t);
1097
1098 return t;
1099 }
1100
1101 static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
1102 pa_bluetooth_discovery *y = userdata;
1103 pa_bluetooth_device *d;
1104 pa_bluetooth_transport *t;
1105 const char *path, *dev_path = NULL, *uuid = NULL;
1106 uint8_t *config = NULL;
1107 int size = 0;
1108 pa_bool_t nrec = FALSE;
1109 enum profile p;
1110 DBusMessageIter args, props;
1111 DBusMessage *r;
1112
1113 dbus_message_iter_init(m, &args);
1114
1115 dbus_message_iter_get_basic(&args, &path);
1116 if (!dbus_message_iter_next(&args))
1117 goto fail;
1118
1119 dbus_message_iter_recurse(&args, &props);
1120 if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
1121 goto fail;
1122
1123 /* Read transport properties */
1124 while (dbus_message_iter_get_arg_type(&props) == DBUS_TYPE_DICT_ENTRY) {
1125 const char *key;
1126 DBusMessageIter value, entry;
1127 int var;
1128
1129 dbus_message_iter_recurse(&props, &entry);
1130 dbus_message_iter_get_basic(&entry, &key);
1131
1132 dbus_message_iter_next(&entry);
1133 dbus_message_iter_recurse(&entry, &value);
1134
1135 var = dbus_message_iter_get_arg_type(&value);
1136 if (strcasecmp(key, "UUID") == 0) {
1137 if (var != DBUS_TYPE_STRING)
1138 goto fail;
1139 dbus_message_iter_get_basic(&value, &uuid);
1140 } else if (strcasecmp(key, "Device") == 0) {
1141 if (var != DBUS_TYPE_OBJECT_PATH)
1142 goto fail;
1143 dbus_message_iter_get_basic(&value, &dev_path);
1144 } else if (strcasecmp(key, "NREC") == 0) {
1145 dbus_bool_t tmp_boolean;
1146 if (var != DBUS_TYPE_BOOLEAN)
1147 goto fail;
1148 dbus_message_iter_get_basic(&value, &tmp_boolean);
1149 nrec = tmp_boolean;
1150 } else if (strcasecmp(key, "Configuration") == 0) {
1151 DBusMessageIter array;
1152 if (var != DBUS_TYPE_ARRAY)
1153 goto fail;
1154 dbus_message_iter_recurse(&value, &array);
1155 dbus_message_iter_get_fixed_array(&array, &config, &size);
1156 }
1157
1158 dbus_message_iter_next(&props);
1159 }
1160
1161 d = found_device(y, dev_path);
1162 if (!d)
1163 goto fail;
1164
1165 if (dbus_message_has_path(m, HFP_AG_ENDPOINT))
1166 p = PROFILE_HSP;
1167 else if (dbus_message_has_path(m, HFP_HS_ENDPOINT))
1168 p = PROFILE_HFGW;
1169 else if (dbus_message_has_path(m, A2DP_SOURCE_ENDPOINT))
1170 p = PROFILE_A2DP;
1171 else
1172 p = PROFILE_A2DP_SOURCE;
1173
1174 t = transport_new(y, path, p, config, size);
1175 if (nrec)
1176 t->nrec = nrec;
1177 pa_hashmap_put(d->transports, t->path, t);
1178
1179 pa_log_debug("Transport %s profile %d available", t->path, t->profile);
1180
1181 pa_assert_se(r = dbus_message_new_method_return(m));
1182
1183 return r;
1184
1185 fail:
1186 pa_log("org.bluez.MediaEndpoint.SetConfiguration: invalid arguments");
1187 pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1188 "Unable to set configuration")));
1189 return r;
1190 }
1191
1192 static DBusMessage *endpoint_clear_configuration(DBusConnection *c, DBusMessage *m, void *userdata) {
1193 pa_bluetooth_discovery *y = userdata;
1194 pa_bluetooth_device *d;
1195 pa_bluetooth_transport *t;
1196 void *state = NULL;
1197 DBusMessage *r;
1198 DBusError e;
1199 const char *path;
1200
1201 dbus_error_init(&e);
1202
1203 if (!dbus_message_get_args(m, &e, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
1204 pa_log("org.bluez.MediaEndpoint.ClearConfiguration: %s", e.message);
1205 dbus_error_free(&e);
1206 goto fail;
1207 }
1208
1209 while ((d = pa_hashmap_iterate(y->devices, &state, NULL))) {
1210 if ((t = pa_hashmap_get(d->transports, path))) {
1211 pa_log_debug("Clearing transport %s profile %d", t->path, t->profile);
1212 pa_hashmap_remove(d->transports, t->path);
1213 pa_hook_fire(&t->hooks[PA_BLUETOOTH_TRANSPORT_HOOK_REMOVED], NULL);
1214 transport_free(t);
1215 break;
1216 }
1217 }
1218
1219 pa_assert_se(r = dbus_message_new_method_return(m));
1220
1221 return r;
1222
1223 fail:
1224 pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1225 "Unable to clear configuration")));
1226 return r;
1227 }
1228
1229 static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
1230
1231 switch (freq) {
1232 case SBC_SAMPLING_FREQ_16000:
1233 case SBC_SAMPLING_FREQ_32000:
1234 return 53;
1235
1236 case SBC_SAMPLING_FREQ_44100:
1237
1238 switch (mode) {
1239 case SBC_CHANNEL_MODE_MONO:
1240 case SBC_CHANNEL_MODE_DUAL_CHANNEL:
1241 return 31;
1242
1243 case SBC_CHANNEL_MODE_STEREO:
1244 case SBC_CHANNEL_MODE_JOINT_STEREO:
1245 return 53;
1246
1247 default:
1248 pa_log_warn("Invalid channel mode %u", mode);
1249 return 53;
1250 }
1251
1252 case SBC_SAMPLING_FREQ_48000:
1253
1254 switch (mode) {
1255 case SBC_CHANNEL_MODE_MONO:
1256 case SBC_CHANNEL_MODE_DUAL_CHANNEL:
1257 return 29;
1258
1259 case SBC_CHANNEL_MODE_STEREO:
1260 case SBC_CHANNEL_MODE_JOINT_STEREO:
1261 return 51;
1262
1263 default:
1264 pa_log_warn("Invalid channel mode %u", mode);
1265 return 51;
1266 }
1267
1268 default:
1269 pa_log_warn("Invalid sampling freq %u", freq);
1270 return 53;
1271 }
1272 }
1273
1274 static DBusMessage *endpoint_select_configuration(DBusConnection *c, DBusMessage *m, void *userdata) {
1275 pa_bluetooth_discovery *y = userdata;
1276 a2dp_sbc_t *cap, config;
1277 uint8_t *pconf = (uint8_t *) &config;
1278 int i, size;
1279 DBusMessage *r;
1280 DBusError e;
1281
1282 static const struct {
1283 uint32_t rate;
1284 uint8_t cap;
1285 } freq_table[] = {
1286 { 16000U, SBC_SAMPLING_FREQ_16000 },
1287 { 32000U, SBC_SAMPLING_FREQ_32000 },
1288 { 44100U, SBC_SAMPLING_FREQ_44100 },
1289 { 48000U, SBC_SAMPLING_FREQ_48000 }
1290 };
1291
1292 dbus_error_init(&e);
1293
1294 if (!dbus_message_get_args(m, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &cap, &size, DBUS_TYPE_INVALID)) {
1295 pa_log("org.bluez.MediaEndpoint.SelectConfiguration: %s", e.message);
1296 dbus_error_free(&e);
1297 goto fail;
1298 }
1299
1300 if (dbus_message_has_path(m, HFP_AG_ENDPOINT) || dbus_message_has_path(m, HFP_HS_ENDPOINT))
1301 goto done;
1302
1303 pa_assert(size == sizeof(config));
1304
1305 memset(&config, 0, sizeof(config));
1306
1307 /* Find the lowest freq that is at least as high as the requested
1308 * sampling rate */
1309 for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
1310 if (freq_table[i].rate >= y->core->default_sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
1311 config.frequency = freq_table[i].cap;
1312 break;
1313 }
1314
1315 if ((unsigned) i == PA_ELEMENTSOF(freq_table)) {
1316 for (--i; i >= 0; i--) {
1317 if (cap->frequency & freq_table[i].cap) {
1318 config.frequency = freq_table[i].cap;
1319 break;
1320 }
1321 }
1322
1323 if (i < 0) {
1324 pa_log("Not suitable sample rate");
1325 goto fail;
1326 }
1327 }
1328
1329 pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
1330
1331 if (y->core->default_sample_spec.channels <= 1) {
1332 if (cap->channel_mode & SBC_CHANNEL_MODE_MONO)
1333 config.channel_mode = SBC_CHANNEL_MODE_MONO;
1334 }
1335
1336 if (y->core->default_sample_spec.channels >= 2) {
1337 if (cap->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
1338 config.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
1339 else if (cap->channel_mode & SBC_CHANNEL_MODE_STEREO)
1340 config.channel_mode = SBC_CHANNEL_MODE_STEREO;
1341 else if (cap->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
1342 config.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
1343 else if (cap->channel_mode & SBC_CHANNEL_MODE_MONO) {
1344 config.channel_mode = SBC_CHANNEL_MODE_MONO;
1345 } else {
1346 pa_log("No supported channel modes");
1347 goto fail;
1348 }
1349 }
1350
1351 if (cap->block_length & SBC_BLOCK_LENGTH_16)
1352 config.block_length = SBC_BLOCK_LENGTH_16;
1353 else if (cap->block_length & SBC_BLOCK_LENGTH_12)
1354 config.block_length = SBC_BLOCK_LENGTH_12;
1355 else if (cap->block_length & SBC_BLOCK_LENGTH_8)
1356 config.block_length = SBC_BLOCK_LENGTH_8;
1357 else if (cap->block_length & SBC_BLOCK_LENGTH_4)
1358 config.block_length = SBC_BLOCK_LENGTH_4;
1359 else {
1360 pa_log_error("No supported block lengths");
1361 goto fail;
1362 }
1363
1364 if (cap->subbands & SBC_SUBBANDS_8)
1365 config.subbands = SBC_SUBBANDS_8;
1366 else if (cap->subbands & SBC_SUBBANDS_4)
1367 config.subbands = SBC_SUBBANDS_4;
1368 else {
1369 pa_log_error("No supported subbands");
1370 goto fail;
1371 }
1372
1373 if (cap->allocation_method & SBC_ALLOCATION_LOUDNESS)
1374 config.allocation_method = SBC_ALLOCATION_LOUDNESS;
1375 else if (cap->allocation_method & SBC_ALLOCATION_SNR)
1376 config.allocation_method = SBC_ALLOCATION_SNR;
1377
1378 config.min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
1379 config.max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(config.frequency, config.channel_mode), cap->max_bitpool);
1380
1381 done:
1382 pa_assert_se(r = dbus_message_new_method_return(m));
1383
1384 pa_assert_se(dbus_message_append_args(
1385 r,
1386 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &pconf, size,
1387 DBUS_TYPE_INVALID));
1388
1389 return r;
1390
1391 fail:
1392 pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1393 "Unable to select configuration")));
1394 return r;
1395 }
1396
1397 static DBusHandlerResult endpoint_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
1398 struct pa_bluetooth_discovery *y = userdata;
1399 DBusMessage *r = NULL;
1400 DBusError e;
1401 const char *path;
1402
1403 pa_assert(y);
1404
1405 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
1406 dbus_message_get_interface(m),
1407 dbus_message_get_path(m),
1408 dbus_message_get_member(m));
1409
1410 path = dbus_message_get_path(m);
1411 dbus_error_init(&e);
1412
1413 if (!pa_streq(path, A2DP_SOURCE_ENDPOINT) && !pa_streq(path, A2DP_SINK_ENDPOINT) && !pa_streq(path, HFP_AG_ENDPOINT) && !pa_streq(path, HFP_HS_ENDPOINT))
1414 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1415
1416 if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1417 const char *xml = ENDPOINT_INTROSPECT_XML;
1418
1419 pa_assert_se(r = dbus_message_new_method_return(m));
1420 pa_assert_se(dbus_message_append_args(
1421 r,
1422 DBUS_TYPE_STRING, &xml,
1423 DBUS_TYPE_INVALID));
1424
1425 } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "SetConfiguration")) {
1426 r = endpoint_set_configuration(c, m, userdata);
1427 } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "SelectConfiguration")) {
1428 r = endpoint_select_configuration(c, m, userdata);
1429 } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "ClearConfiguration"))
1430 r = endpoint_clear_configuration(c, m, userdata);
1431 else
1432 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1433
1434 if (r) {
1435 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
1436 dbus_message_unref(r);
1437 }
1438
1439 return DBUS_HANDLER_RESULT_HANDLED;
1440 }
1441
1442 pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
1443 DBusError err;
1444 pa_bluetooth_discovery *y;
1445 static const DBusObjectPathVTable vtable_endpoint = {
1446 .message_function = endpoint_handler,
1447 };
1448
1449 pa_assert(c);
1450
1451 dbus_error_init(&err);
1452
1453 if ((y = pa_shared_get(c, "bluetooth-discovery")))
1454 return pa_bluetooth_discovery_ref(y);
1455
1456 y = pa_xnew0(pa_bluetooth_discovery, 1);
1457 PA_REFCNT_INIT(y);
1458 y->core = c;
1459 y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
1460 PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
1461 pa_hook_init(&y->hook, y);
1462 pa_shared_set(c, "bluetooth-discovery", y);
1463
1464 if (setup_dbus(y) < 0)
1465 goto fail;
1466
1467 /* dynamic detection of bluetooth audio devices */
1468 if (!dbus_connection_add_filter(pa_dbus_connection_get(y->connection), filter_cb, y, NULL)) {
1469 pa_log_error("Failed to add filter function");
1470 goto fail;
1471 }
1472 y->filter_added = TRUE;
1473
1474 if (pa_dbus_add_matches(
1475 pa_dbus_connection_get(y->connection), &err,
1476 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1477 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1478 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1479 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1480 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1481 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1482 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1483 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1484 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1485 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1486 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
1487 NULL) < 0) {
1488 pa_log("Failed to add D-Bus matches: %s", err.message);
1489 goto fail;
1490 }
1491
1492 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), HFP_AG_ENDPOINT, &vtable_endpoint, y));
1493 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), HFP_HS_ENDPOINT, &vtable_endpoint, y));
1494 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT, &vtable_endpoint, y));
1495 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT, &vtable_endpoint, y));
1496
1497 list_adapters(y);
1498
1499 return y;
1500
1501 fail:
1502
1503 if (y)
1504 pa_bluetooth_discovery_unref(y);
1505
1506 dbus_error_free(&err);
1507
1508 return NULL;
1509 }
1510
1511 pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
1512 pa_assert(y);
1513 pa_assert(PA_REFCNT_VALUE(y) > 0);
1514
1515 PA_REFCNT_INC(y);
1516
1517 return y;
1518 }
1519
1520 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
1521 pa_assert(y);
1522 pa_assert(PA_REFCNT_VALUE(y) > 0);
1523
1524 if (PA_REFCNT_DEC(y) > 0)
1525 return;
1526
1527 pa_dbus_free_pending_list(&y->pending);
1528
1529 if (y->devices) {
1530 remove_all_devices(y);
1531 pa_hashmap_free(y->devices, NULL, NULL);
1532 }
1533
1534 if (y->connection) {
1535 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), HFP_AG_ENDPOINT);
1536 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), HFP_HS_ENDPOINT);
1537 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT);
1538 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT);
1539 pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
1540 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1541 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1542 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
1543 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1544 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1545 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1546 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1547 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1548 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1549 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1550 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1551 "type='signal',sender='org.bluez',interface='org.bluez.MediaTransport',member='PropertyChanged'",
1552 NULL);
1553
1554 if (y->filter_added)
1555 dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
1556
1557 pa_dbus_connection_unref(y->connection);
1558 }
1559
1560 pa_hook_done(&y->hook);
1561
1562 if (y->core)
1563 pa_shared_remove(y->core, "bluetooth-discovery");
1564
1565 pa_xfree(y);
1566 }
1567
1568 void pa_bluetooth_discovery_sync(pa_bluetooth_discovery *y) {
1569 pa_assert(y);
1570 pa_assert(PA_REFCNT_VALUE(y) > 0);
1571
1572 pa_dbus_sync_pending_list(&y->pending);
1573 }
1574
1575 pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y) {
1576 pa_assert(y);
1577 pa_assert(PA_REFCNT_VALUE(y) > 0);
1578
1579 return &y->hook;
1580 }
1581
1582 const char*pa_bluetooth_get_form_factor(uint32_t class) {
1583 unsigned i;
1584 const char *r;
1585
1586 static const char * const table[] = {
1587 [1] = "headset",
1588 [2] = "hands-free",
1589 [4] = "microphone",
1590 [5] = "speaker",
1591 [6] = "headphone",
1592 [7] = "portable",
1593 [8] = "car",
1594 [10] = "hifi"
1595 };
1596
1597 if (((class >> 8) & 31) != 4)
1598 return NULL;
1599
1600 if ((i = (class >> 2) & 63) > PA_ELEMENTSOF(table))
1601 r = NULL;
1602 else
1603 r = table[i];
1604
1605 if (!r)
1606 pa_log_debug("Unknown Bluetooth minor device class %u", i);
1607
1608 return r;
1609 }
1610
1611 char *pa_bluetooth_cleanup_name(const char *name) {
1612 char *t, *s, *d;
1613 pa_bool_t space = FALSE;
1614
1615 pa_assert(name);
1616
1617 while ((*name >= 1 && *name <= 32) || *name >= 127)
1618 name++;
1619
1620 t = pa_xstrdup(name);
1621
1622 for (s = d = t; *s; s++) {
1623
1624 if (*s <= 32 || *s >= 127 || *s == '_') {
1625 space = TRUE;
1626 continue;
1627 }
1628
1629 if (space) {
1630 *(d++) = ' ';
1631 space = FALSE;
1632 }
1633
1634 *(d++) = *s;
1635 }
1636
1637 *d = 0;
1638
1639 return t;
1640 }
1641
1642 pa_bool_t pa_bluetooth_uuid_has(pa_bluetooth_uuid *uuids, const char *uuid) {
1643 pa_assert(uuid);
1644
1645 while (uuids) {
1646 if (strcasecmp(uuids->uuid, uuid) == 0)
1647 return TRUE;
1648
1649 uuids = uuids->next;
1650 }
1651
1652 return FALSE;
1653 }