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