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