]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/bluetooth-util.c
bluetooth: fix build for libdbus < 1.3
[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 <pulsecore/core-util.h>
27 #include <pulsecore/shared.h>
28 #include <pulsecore/dbus-shared.h>
29
30 #include "bluetooth-util.h"
31 #include "ipc.h"
32
33 #define HFP_AG_ENDPOINT "/MediaEndpoint/HFPAG"
34 #define A2DP_SOURCE_ENDPOINT "/MediaEndpoint/A2DPSource"
35 #define A2DP_SINK_ENDPOINT "/MediaEndpoint/A2DPSink"
36
37 #define ENDPOINT_INTROSPECT_XML \
38 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
39 "<node>" \
40 " <interface name=\"org.bluez.MediaEndpoint\">" \
41 " <method name=\"SetConfiguration\">" \
42 " <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
43 " <arg name=\"configuration\" direction=\"in\" type=\"ay\"/>" \
44 " </method>" \
45 " <method name=\"SelectConfiguration\">" \
46 " <arg name=\"capabilities\" direction=\"in\" type=\"ay\"/>" \
47 " <arg name=\"configuration\" direction=\"out\" type=\"ay\"/>" \
48 " </method>" \
49 " <method name=\"ClearConfiguration\">" \
50 " </method>" \
51 " <method name=\"Release\">" \
52 " </method>" \
53 " </interface>" \
54 " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
55 " <method name=\"Introspect\">" \
56 " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
57 " </method>" \
58 " </interface>" \
59 "</node>"
60
61 #define MAX_BITPOOL 64
62 #define MIN_BITPOOL 2U
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, pa_bluetooth_device *d, DBusMessage *m, DBusPendingCallNotifyFunction func);
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, d, m, get_properties_reply);
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, d, m, get_properties_reply);
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, d, m, get_properties_reply);
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, d, m, get_properties_reply);
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, d, m, get_properties_reply);
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, d, m, get_properties_reply);
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, pa_bluetooth_device *d, DBusMessage *m, DBusPendingCallNotifyFunction func) {
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, d);
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 static void register_endpoint_reply(DBusPendingCall *pending, void *userdata) {
523 DBusError e;
524 DBusMessage *r;
525 pa_dbus_pending *p;
526 pa_bluetooth_discovery *y;
527
528 pa_assert(pending);
529
530 dbus_error_init(&e);
531
532 pa_assert_se(p = userdata);
533 pa_assert_se(y = p->context_data);
534 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
535
536 if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
537 pa_log_debug("Bluetooth daemon is apparently not available.");
538 remove_all_devices(y);
539 goto finish;
540 }
541
542 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
543 pa_log("Error from RegisterEndpoint reply: %s", dbus_message_get_error_name(r));
544 goto finish;
545 }
546
547 finish:
548 dbus_message_unref(r);
549
550 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
551 pa_dbus_pending_free(p);
552 }
553
554 static void list_devices_reply(DBusPendingCall *pending, void *userdata) {
555 DBusError e;
556 DBusMessage *r;
557 char **paths = NULL;
558 int num = -1;
559 pa_dbus_pending *p;
560 pa_bluetooth_discovery *y;
561
562 pa_assert(pending);
563
564 dbus_error_init(&e);
565
566 pa_assert_se(p = userdata);
567 pa_assert_se(y = p->context_data);
568 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
569
570 if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
571 pa_log_debug("Bluetooth daemon is apparently not available.");
572 remove_all_devices(y);
573 goto finish;
574 }
575
576 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
577 pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r));
578 goto finish;
579 }
580
581 if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) {
582 pa_log("org.bluez.Adapter.ListDevices returned an error: '%s'\n", e.message);
583 dbus_error_free(&e);
584 } else {
585 int i;
586
587 for (i = 0; i < num; ++i)
588 found_device(y, paths[i]);
589 }
590
591 finish:
592 if (paths)
593 dbus_free_string_array (paths);
594
595 dbus_message_unref(r);
596
597 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
598 pa_dbus_pending_free(p);
599 }
600
601 static void register_endpoint(pa_bluetooth_discovery *y, const char *path, const char *endpoint, const char *uuid)
602 {
603 DBusMessage *m;
604 DBusMessageIter i, d;
605 uint8_t codec = 0;
606
607 pa_log_debug("Registering %s on adapter %s.", endpoint, path);
608
609 pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Media", "RegisterEndpoint"));
610
611 dbus_message_iter_init_append(m, &i);
612
613 dbus_message_iter_append_basic(&i, DBUS_TYPE_OBJECT_PATH, &endpoint);
614
615 dbus_message_iter_open_container(&i, DBUS_TYPE_ARRAY, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
616 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
617 &d);
618
619 pa_dbus_append_basic_variant_dict_entry(&d, "UUID", DBUS_TYPE_STRING, &uuid);
620
621 pa_dbus_append_basic_variant_dict_entry(&d, "Codec", DBUS_TYPE_BYTE, &codec);
622
623 if (pa_streq(uuid, HFP_AG_UUID)) {
624 uint8_t capability = 0;
625 uint8_t *caps = &capability;
626 pa_dbus_append_basic_array_variant_dict_entry(&d, "Capabilities", DBUS_TYPE_BYTE, &caps, 1);
627 } else {
628 sbc_capabilities_raw_t capabilities;
629 uint8_t *caps = (uint8_t *) &capabilities;
630
631 capabilities.channel_mode = BT_A2DP_CHANNEL_MODE_MONO | BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL |
632 BT_A2DP_CHANNEL_MODE_STEREO | BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
633 capabilities.frequency = BT_SBC_SAMPLING_FREQ_16000 | BT_SBC_SAMPLING_FREQ_32000 |
634 BT_SBC_SAMPLING_FREQ_44100 | BT_SBC_SAMPLING_FREQ_48000;
635 capabilities.allocation_method = BT_A2DP_ALLOCATION_SNR | BT_A2DP_ALLOCATION_LOUDNESS;
636 capabilities.subbands = BT_A2DP_SUBBANDS_4 | BT_A2DP_SUBBANDS_8;
637 capabilities.block_length = BT_A2DP_BLOCK_LENGTH_4 | BT_A2DP_BLOCK_LENGTH_8 |
638 BT_A2DP_BLOCK_LENGTH_12 | BT_A2DP_BLOCK_LENGTH_16;
639 capabilities.min_bitpool = MIN_BITPOOL;
640 capabilities.max_bitpool = MAX_BITPOOL;
641
642 pa_dbus_append_basic_array_variant_dict_entry(&d, "Capabilities", DBUS_TYPE_BYTE, &caps, sizeof(capabilities));
643 }
644
645 dbus_message_iter_close_container(&i, &d);
646
647 send_and_add_to_pending(y, NULL, m, register_endpoint_reply);
648 }
649
650 static void found_adapter(pa_bluetooth_discovery *y, const char *path) {
651 DBusMessage *m;
652
653 pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Adapter", "ListDevices"));
654 send_and_add_to_pending(y, NULL, m, list_devices_reply);
655
656 #ifdef DBUS_TYPE_UNIX_FD
657 register_endpoint(y, path, HFP_AG_ENDPOINT, HFP_AG_UUID);
658 register_endpoint(y, path, A2DP_SOURCE_ENDPOINT, A2DP_SOURCE_UUID);
659 register_endpoint(y, path, A2DP_SINK_ENDPOINT, A2DP_SINK_UUID);
660 #endif
661 }
662
663 static void list_adapters_reply(DBusPendingCall *pending, void *userdata) {
664 DBusError e;
665 DBusMessage *r;
666 char **paths = NULL;
667 int num = -1;
668 pa_dbus_pending *p;
669 pa_bluetooth_discovery *y;
670
671 pa_assert(pending);
672
673 dbus_error_init(&e);
674
675 pa_assert_se(p = userdata);
676 pa_assert_se(y = p->context_data);
677 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
678
679 if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
680 pa_log_debug("Bluetooth daemon is apparently not available.");
681 remove_all_devices(y);
682 goto finish;
683 }
684
685 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
686 pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r));
687 goto finish;
688 }
689
690 if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) {
691 pa_log("org.bluez.Manager.ListAdapters returned an error: %s", e.message);
692 dbus_error_free(&e);
693 } else {
694 int i;
695
696 for (i = 0; i < num; ++i)
697 found_adapter(y, paths[i]);
698 }
699
700 finish:
701 if (paths)
702 dbus_free_string_array (paths);
703
704 dbus_message_unref(r);
705
706 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
707 pa_dbus_pending_free(p);
708 }
709
710 static void list_adapters(pa_bluetooth_discovery *y) {
711 DBusMessage *m;
712 pa_assert(y);
713
714 pa_assert_se(m = dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
715 send_and_add_to_pending(y, NULL, m, list_adapters_reply);
716 }
717
718 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
719 DBusError err;
720 pa_bluetooth_discovery *y;
721
722 pa_assert(bus);
723 pa_assert(m);
724
725 pa_assert_se(y = userdata);
726
727 dbus_error_init(&err);
728
729 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
730 dbus_message_get_interface(m),
731 dbus_message_get_path(m),
732 dbus_message_get_member(m));
733
734 if (dbus_message_is_signal(m, "org.bluez.Adapter", "DeviceRemoved")) {
735 const char *path;
736 pa_bluetooth_device *d;
737
738 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
739 pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err.message);
740 goto fail;
741 }
742
743 pa_log_debug("Device %s removed", path);
744
745 if ((d = pa_hashmap_remove(y->devices, path))) {
746 run_callback(y, d, TRUE);
747 device_free(d);
748 }
749
750 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
751
752 } else if (dbus_message_is_signal(m, "org.bluez.Adapter", "DeviceCreated")) {
753 const char *path;
754
755 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
756 pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err.message);
757 goto fail;
758 }
759
760 pa_log_debug("Device %s created", path);
761
762 found_device(y, path);
763 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
764
765 } else if (dbus_message_is_signal(m, "org.bluez.Manager", "AdapterAdded")) {
766 const char *path;
767
768 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
769 pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err.message);
770 goto fail;
771 }
772
773 pa_log_debug("Adapter %s created", path);
774
775 found_adapter(y, path);
776 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
777
778 } else if (dbus_message_is_signal(m, "org.bluez.Audio", "PropertyChanged") ||
779 dbus_message_is_signal(m, "org.bluez.Headset", "PropertyChanged") ||
780 dbus_message_is_signal(m, "org.bluez.AudioSink", "PropertyChanged") ||
781 dbus_message_is_signal(m, "org.bluez.AudioSource", "PropertyChanged") ||
782 dbus_message_is_signal(m, "org.bluez.HandsfreeGateway", "PropertyChanged") ||
783 dbus_message_is_signal(m, "org.bluez.Device", "PropertyChanged")) {
784
785 pa_bluetooth_device *d;
786
787 if ((d = pa_hashmap_get(y->devices, dbus_message_get_path(m)))) {
788 DBusMessageIter arg_i;
789
790 if (!dbus_message_iter_init(m, &arg_i)) {
791 pa_log("Failed to parse PropertyChanged: %s", err.message);
792 goto fail;
793 }
794
795 if (dbus_message_has_interface(m, "org.bluez.Device")) {
796 if (parse_device_property(y, d, &arg_i) < 0)
797 goto fail;
798
799 } else if (dbus_message_has_interface(m, "org.bluez.Audio")) {
800 if (parse_audio_property(y, &d->audio_state, &arg_i) < 0)
801 goto fail;
802
803 } else if (dbus_message_has_interface(m, "org.bluez.Headset")) {
804 if (parse_audio_property(y, &d->headset_state, &arg_i) < 0)
805 goto fail;
806
807 } else if (dbus_message_has_interface(m, "org.bluez.AudioSink")) {
808 if (parse_audio_property(y, &d->audio_sink_state, &arg_i) < 0)
809 goto fail;
810
811 } else if (dbus_message_has_interface(m, "org.bluez.AudioSource")) {
812 if (parse_audio_property(y, &d->audio_source_state, &arg_i) < 0)
813 goto fail;
814
815 } else if (dbus_message_has_interface(m, "org.bluez.HandsfreeGateway")) {
816 if (parse_audio_property(y, &d->hfgw_state, &arg_i) < 0)
817 goto fail;
818 }
819
820 run_callback(y, d, FALSE);
821 }
822
823 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
824
825 } else if (dbus_message_is_signal(m, "org.bluez.Device", "DisconnectRequested")) {
826 pa_bluetooth_device *d;
827
828 if ((d = pa_hashmap_get(y->devices, dbus_message_get_path(m)))) {
829 /* Device will disconnect in 2 sec */
830 d->audio_state = PA_BT_AUDIO_STATE_DISCONNECTED;
831 d->audio_sink_state = PA_BT_AUDIO_STATE_DISCONNECTED;
832 d->audio_source_state = PA_BT_AUDIO_STATE_DISCONNECTED;
833 d->headset_state = PA_BT_AUDIO_STATE_DISCONNECTED;
834 d->hfgw_state = PA_BT_AUDIO_STATE_DISCONNECTED;
835
836 run_callback(y, d, FALSE);
837 }
838
839 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
840
841 } else if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
842 const char *name, *old_owner, *new_owner;
843
844 if (!dbus_message_get_args(m, &err,
845 DBUS_TYPE_STRING, &name,
846 DBUS_TYPE_STRING, &old_owner,
847 DBUS_TYPE_STRING, &new_owner,
848 DBUS_TYPE_INVALID)) {
849 pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
850 goto fail;
851 }
852
853 if (pa_streq(name, "org.bluez")) {
854 if (old_owner && *old_owner) {
855 pa_log_debug("Bluetooth daemon disappeared.");
856 remove_all_devices(y);
857 }
858
859 if (new_owner && *new_owner) {
860 pa_log_debug("Bluetooth daemon appeared.");
861 list_adapters(y);
862 }
863 }
864
865 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
866 }
867
868 fail:
869 dbus_error_free(&err);
870
871 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
872 }
873
874 const pa_bluetooth_device* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery *y, const char* address) {
875 pa_bluetooth_device *d;
876 void *state = NULL;
877
878 pa_assert(y);
879 pa_assert(PA_REFCNT_VALUE(y) > 0);
880 pa_assert(address);
881
882 if (!pa_hook_is_firing(&y->hook))
883 pa_bluetooth_discovery_sync(y);
884
885 while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
886 if (pa_streq(d->address, address))
887 return device_is_audio(d) ? d : NULL;
888
889 return NULL;
890 }
891
892 const pa_bluetooth_device* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery *y, const char* path) {
893 pa_bluetooth_device *d;
894
895 pa_assert(y);
896 pa_assert(PA_REFCNT_VALUE(y) > 0);
897 pa_assert(path);
898
899 if (!pa_hook_is_firing(&y->hook))
900 pa_bluetooth_discovery_sync(y);
901
902 if ((d = pa_hashmap_get(y->devices, path)))
903 if (device_is_audio(d))
904 return d;
905
906 return NULL;
907 }
908
909 const pa_bluetooth_transport* pa_bluetooth_discovery_get_transport(pa_bluetooth_discovery *y, const char *path) {
910 pa_bluetooth_device *d;
911 pa_bluetooth_transport *t;
912 void *state = NULL;
913
914 pa_assert(y);
915 pa_assert(PA_REFCNT_VALUE(y) > 0);
916 pa_assert(path);
917
918 while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
919 if ((t = pa_hashmap_get(d->transports, path)))
920 return t;
921
922 return NULL;
923 }
924
925 const pa_bluetooth_transport* pa_bluetooth_device_get_transport(const pa_bluetooth_device *d, enum profile profile) {
926 pa_bluetooth_transport *t;
927 void *state = NULL;
928
929 pa_assert(d);
930
931 while ((t = pa_hashmap_iterate(d->transports, &state, NULL)))
932 if (t->profile == profile)
933 return t;
934
935 return NULL;
936 }
937
938 int pa_bluetooth_transport_acquire(const pa_bluetooth_transport *t, const char *accesstype) {
939 DBusMessage *m, *r;
940 DBusError err;
941 int ret;
942
943 pa_assert(t);
944 pa_assert(t->y);
945
946 dbus_error_init(&err);
947
948 pa_assert_se(m = dbus_message_new_method_call("org.bluez", t->path, "org.bluez.MediaTransport", "Acquire"));
949 pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_STRING, &accesstype, DBUS_TYPE_INVALID));
950 r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->y->connection), m, -1, &err);
951
952 if (dbus_error_is_set(&err) || !r) {
953 pa_log("Failed to acquire transport fd: %s", err.message);
954 dbus_error_free(&err);
955 return -1;
956 }
957
958 #ifdef DBUS_TYPE_UNIX_FD
959 if (!dbus_message_get_args(r, &err, DBUS_TYPE_UNIX_FD, &ret, DBUS_TYPE_INVALID)) {
960 pa_log("Failed to parse org.bluez.MediaTransport.Acquire(): %s", err.message);
961 ret = -1;
962 dbus_error_free(&err);
963 goto fail;
964 }
965 #endif
966
967 fail:
968 dbus_message_unref(r);
969 return ret;
970 }
971
972 void pa_bluetooth_transport_release(const pa_bluetooth_transport *t, const char *accesstype) {
973 DBusMessage *m;
974 DBusError err;
975
976 pa_assert(t);
977 pa_assert(t->y);
978
979 dbus_error_init(&err);
980
981 pa_assert_se(m = dbus_message_new_method_call("org.bluez", t->path, "org.bluez.MediaTransport", "Release"));
982 pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_STRING, &accesstype, DBUS_TYPE_INVALID));
983 dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->y->connection), m, -1, &err);
984
985 if (dbus_error_is_set(&err)) {
986 pa_log("Failed to release transport %s: %s", t->path, err.message);
987 dbus_error_free(&err);
988 } else
989 pa_log_info("Transport %s released", t->path);
990 }
991
992 static int setup_dbus(pa_bluetooth_discovery *y) {
993 DBusError err;
994
995 dbus_error_init(&err);
996
997 y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err);
998
999 if (dbus_error_is_set(&err) || !y->connection) {
1000 pa_log("Failed to get D-Bus connection: %s", err.message);
1001 dbus_error_free(&err);
1002 return -1;
1003 }
1004
1005 return 0;
1006 }
1007
1008 static pa_bluetooth_transport *transport_new(pa_bluetooth_discovery *y, const char *path, enum profile p, const uint8_t *config, int size) {
1009 pa_bluetooth_transport *t;
1010
1011 t = pa_xnew0(pa_bluetooth_transport, 1);
1012 t->y = y;
1013 t->path = pa_xstrdup(path);
1014 t->profile = p;
1015 t->config_size = size;
1016
1017 if (size > 0) {
1018 t->config = pa_xnew(uint8_t, size);
1019 memcpy(t->config, config, size);
1020 }
1021
1022 return t;
1023 }
1024
1025 static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
1026 pa_bluetooth_discovery *y = userdata;
1027 pa_bluetooth_device *d;
1028 pa_bluetooth_transport *t;
1029 const char *path, *dev_path = NULL, *uuid = NULL;
1030 uint8_t *config = NULL;
1031 int size = 0;
1032 enum profile p;
1033 DBusMessageIter args, props;
1034 DBusMessage *r;
1035
1036 dbus_message_iter_init(m, &args);
1037
1038 dbus_message_iter_get_basic(&args, &path);
1039 if (!dbus_message_iter_next(&args))
1040 goto fail;
1041
1042 dbus_message_iter_recurse(&args, &props);
1043 if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
1044 goto fail;
1045
1046 /* Read transport properties */
1047 while (dbus_message_iter_get_arg_type(&props) == DBUS_TYPE_DICT_ENTRY) {
1048 const char *key;
1049 DBusMessageIter value, entry;
1050 int var;
1051
1052 dbus_message_iter_recurse(&props, &entry);
1053 dbus_message_iter_get_basic(&entry, &key);
1054
1055 dbus_message_iter_next(&entry);
1056 dbus_message_iter_recurse(&entry, &value);
1057
1058 var = dbus_message_iter_get_arg_type(&value);
1059 if (strcasecmp(key, "UUID") == 0) {
1060 if (var != DBUS_TYPE_STRING)
1061 goto fail;
1062 dbus_message_iter_get_basic(&value, &uuid);
1063 } else if (strcasecmp(key, "Device") == 0) {
1064 if (var != DBUS_TYPE_OBJECT_PATH)
1065 goto fail;
1066 dbus_message_iter_get_basic(&value, &dev_path);
1067 } else if (strcasecmp(key, "Configuration") == 0) {
1068 DBusMessageIter array;
1069 if (var != DBUS_TYPE_ARRAY)
1070 goto fail;
1071 dbus_message_iter_recurse(&value, &array);
1072 dbus_message_iter_get_fixed_array(&array, &config, &size);
1073 }
1074
1075 dbus_message_iter_next(&props);
1076 }
1077
1078 d = found_device(y, dev_path);
1079 if (!d)
1080 goto fail;
1081
1082 if (dbus_message_has_path(m, HFP_AG_ENDPOINT))
1083 p = PROFILE_HSP;
1084 else if (dbus_message_has_path(m, A2DP_SOURCE_ENDPOINT))
1085 p = PROFILE_A2DP;
1086 else
1087 p = PROFILE_A2DP_SOURCE;
1088
1089 t = transport_new(y, path, p, config, size);
1090 pa_hashmap_put(d->transports, t->path, t);
1091
1092 pa_log_debug("Transport %s profile %d available", t->path, t->profile);
1093
1094 pa_assert_se(r = dbus_message_new_method_return(m));
1095
1096 return r;
1097
1098 fail:
1099 pa_log("org.bluez.MediaEndpoint.SetConfiguration: invalid arguments");
1100 pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1101 "Unable to set configuration")));
1102 return r;
1103 }
1104
1105 static DBusMessage *endpoint_clear_configuration(DBusConnection *c, DBusMessage *m, void *userdata) {
1106 pa_bluetooth_discovery *y = userdata;
1107 pa_bluetooth_device *d;
1108 pa_bluetooth_transport *t;
1109 void *state = NULL;
1110 DBusMessage *r;
1111 DBusError e;
1112 const char *path;
1113
1114 dbus_error_init(&e);
1115
1116 if (!dbus_message_get_args(m, &e, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
1117 pa_log("org.bluez.MediaEndpoint.ClearConfiguration: %s", e.message);
1118 dbus_error_free(&e);
1119 goto fail;
1120 }
1121
1122 while ((d = pa_hashmap_iterate(y->devices, &state, NULL))) {
1123 if ((t = pa_hashmap_get(d->transports, path))) {
1124 pa_log_debug("Clearing transport %s profile %d", t->path, t->profile);
1125 pa_hashmap_remove(d->transports, t->path);
1126 transport_free(t);
1127 break;
1128 }
1129 }
1130
1131 pa_assert_se(r = dbus_message_new_method_return(m));
1132
1133 return r;
1134
1135 fail:
1136 pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1137 "Unable to clear configuration")));
1138 return r;
1139 }
1140
1141 static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
1142
1143 switch (freq) {
1144 case BT_SBC_SAMPLING_FREQ_16000:
1145 case BT_SBC_SAMPLING_FREQ_32000:
1146 return 53;
1147
1148 case BT_SBC_SAMPLING_FREQ_44100:
1149
1150 switch (mode) {
1151 case BT_A2DP_CHANNEL_MODE_MONO:
1152 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
1153 return 31;
1154
1155 case BT_A2DP_CHANNEL_MODE_STEREO:
1156 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
1157 return 53;
1158
1159 default:
1160 pa_log_warn("Invalid channel mode %u", mode);
1161 return 53;
1162 }
1163
1164 case BT_SBC_SAMPLING_FREQ_48000:
1165
1166 switch (mode) {
1167 case BT_A2DP_CHANNEL_MODE_MONO:
1168 case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL:
1169 return 29;
1170
1171 case BT_A2DP_CHANNEL_MODE_STEREO:
1172 case BT_A2DP_CHANNEL_MODE_JOINT_STEREO:
1173 return 51;
1174
1175 default:
1176 pa_log_warn("Invalid channel mode %u", mode);
1177 return 51;
1178 }
1179
1180 default:
1181 pa_log_warn("Invalid sampling freq %u", freq);
1182 return 53;
1183 }
1184 }
1185
1186 static DBusMessage *endpoint_select_configuration(DBusConnection *c, DBusMessage *m, void *userdata) {
1187 pa_bluetooth_discovery *y = userdata;
1188 sbc_capabilities_raw_t *cap, config;
1189 uint8_t *pconf = (uint8_t *) &config;
1190 int i, size;
1191 DBusMessage *r;
1192 DBusError e;
1193
1194 static const struct {
1195 uint32_t rate;
1196 uint8_t cap;
1197 } freq_table[] = {
1198 { 16000U, BT_SBC_SAMPLING_FREQ_16000 },
1199 { 32000U, BT_SBC_SAMPLING_FREQ_32000 },
1200 { 44100U, BT_SBC_SAMPLING_FREQ_44100 },
1201 { 48000U, BT_SBC_SAMPLING_FREQ_48000 }
1202 };
1203
1204 dbus_error_init(&e);
1205
1206 if (!dbus_message_get_args(m, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &cap, &size, DBUS_TYPE_INVALID)) {
1207 pa_log("org.bluez.MediaEndpoint.SelectConfiguration: %s", e.message);
1208 dbus_error_free(&e);
1209 goto fail;
1210 }
1211
1212 if (dbus_message_has_path(m, HFP_AG_ENDPOINT))
1213 goto done;
1214
1215 pa_assert(size == sizeof(config));
1216
1217 memset(&config, 0, sizeof(config));
1218
1219 /* Find the lowest freq that is at least as high as the requested
1220 * sampling rate */
1221 for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
1222 if (freq_table[i].rate >= y->core->default_sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
1223 config.frequency = freq_table[i].cap;
1224 break;
1225 }
1226
1227 if ((unsigned) i == PA_ELEMENTSOF(freq_table)) {
1228 for (--i; i >= 0; i--) {
1229 if (cap->frequency & freq_table[i].cap) {
1230 config.frequency = freq_table[i].cap;
1231 break;
1232 }
1233 }
1234
1235 if (i < 0) {
1236 pa_log("Not suitable sample rate");
1237 goto fail;
1238 }
1239 }
1240
1241 pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
1242
1243 if (y->core->default_sample_spec.channels <= 1) {
1244 if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO)
1245 config.channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
1246 }
1247
1248 if (y->core->default_sample_spec.channels >= 2) {
1249 if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO)
1250 config.channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO;
1251 else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO)
1252 config.channel_mode = BT_A2DP_CHANNEL_MODE_STEREO;
1253 else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL)
1254 config.channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL;
1255 else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) {
1256 config.channel_mode = BT_A2DP_CHANNEL_MODE_MONO;
1257 } else {
1258 pa_log("No supported channel modes");
1259 goto fail;
1260 }
1261 }
1262
1263 if (cap->block_length & BT_A2DP_BLOCK_LENGTH_16)
1264 config.block_length = BT_A2DP_BLOCK_LENGTH_16;
1265 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_12)
1266 config.block_length = BT_A2DP_BLOCK_LENGTH_12;
1267 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_8)
1268 config.block_length = BT_A2DP_BLOCK_LENGTH_8;
1269 else if (cap->block_length & BT_A2DP_BLOCK_LENGTH_4)
1270 config.block_length = BT_A2DP_BLOCK_LENGTH_4;
1271 else {
1272 pa_log_error("No supported block lengths");
1273 goto fail;
1274 }
1275
1276 if (cap->subbands & BT_A2DP_SUBBANDS_8)
1277 config.subbands = BT_A2DP_SUBBANDS_8;
1278 else if (cap->subbands & BT_A2DP_SUBBANDS_4)
1279 config.subbands = BT_A2DP_SUBBANDS_4;
1280 else {
1281 pa_log_error("No supported subbands");
1282 goto fail;
1283 }
1284
1285 if (cap->allocation_method & BT_A2DP_ALLOCATION_LOUDNESS)
1286 config.allocation_method = BT_A2DP_ALLOCATION_LOUDNESS;
1287 else if (cap->allocation_method & BT_A2DP_ALLOCATION_SNR)
1288 config.allocation_method = BT_A2DP_ALLOCATION_SNR;
1289
1290 config.min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
1291 config.max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(config.frequency, config.channel_mode), cap->max_bitpool);
1292
1293 done:
1294 pa_assert_se(r = dbus_message_new_method_return(m));
1295
1296 pa_assert_se(dbus_message_append_args(
1297 r,
1298 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &pconf, size,
1299 DBUS_TYPE_INVALID));
1300
1301 return r;
1302
1303 fail:
1304 pa_assert_se(r = (dbus_message_new_error(m, "org.bluez.MediaEndpoint.Error.InvalidArguments",
1305 "Unable to select configuration")));
1306 return r;
1307 }
1308
1309 static DBusHandlerResult endpoint_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
1310 struct pa_bluetooth_discovery *y = userdata;
1311 DBusMessage *r = NULL;
1312 DBusError e;
1313 const char *path;
1314
1315 pa_assert(y);
1316
1317 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
1318 dbus_message_get_interface(m),
1319 dbus_message_get_path(m),
1320 dbus_message_get_member(m));
1321
1322 path = dbus_message_get_path(m);
1323 dbus_error_init(&e);
1324
1325 if (!pa_streq(path, A2DP_SOURCE_ENDPOINT) && !pa_streq(path, A2DP_SINK_ENDPOINT) && !pa_streq(path, HFP_AG_ENDPOINT))
1326 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1327
1328 if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1329 const char *xml = ENDPOINT_INTROSPECT_XML;
1330
1331 pa_assert_se(r = dbus_message_new_method_return(m));
1332 pa_assert_se(dbus_message_append_args(
1333 r,
1334 DBUS_TYPE_STRING, &xml,
1335 DBUS_TYPE_INVALID));
1336
1337 } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "SetConfiguration")) {
1338 r = endpoint_set_configuration(c, m, userdata);
1339 } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "SelectConfiguration")) {
1340 r = endpoint_select_configuration(c, m, userdata);
1341 } else if (dbus_message_is_method_call(m, "org.bluez.MediaEndpoint", "ClearConfiguration"))
1342 r = endpoint_clear_configuration(c, m, userdata);
1343 else
1344 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1345
1346 if (r) {
1347 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
1348 dbus_message_unref(r);
1349 }
1350
1351 return DBUS_HANDLER_RESULT_HANDLED;
1352 }
1353
1354 pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
1355 DBusError err;
1356 pa_bluetooth_discovery *y;
1357 static const DBusObjectPathVTable vtable_endpoint = {
1358 .message_function = endpoint_handler,
1359 };
1360
1361 pa_assert(c);
1362
1363 dbus_error_init(&err);
1364
1365 if ((y = pa_shared_get(c, "bluetooth-discovery")))
1366 return pa_bluetooth_discovery_ref(y);
1367
1368 y = pa_xnew0(pa_bluetooth_discovery, 1);
1369 PA_REFCNT_INIT(y);
1370 y->core = c;
1371 y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
1372 PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
1373 pa_hook_init(&y->hook, y);
1374 pa_shared_set(c, "bluetooth-discovery", y);
1375
1376 if (setup_dbus(y) < 0)
1377 goto fail;
1378
1379 /* dynamic detection of bluetooth audio devices */
1380 if (!dbus_connection_add_filter(pa_dbus_connection_get(y->connection), filter_cb, y, NULL)) {
1381 pa_log_error("Failed to add filter function");
1382 goto fail;
1383 }
1384 y->filter_added = TRUE;
1385
1386 if (pa_dbus_add_matches(
1387 pa_dbus_connection_get(y->connection), &err,
1388 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1389 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1390 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1391 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1392 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1393 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'",
1394 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1395 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1396 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1397 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1398 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1399 NULL) < 0) {
1400 pa_log("Failed to add D-Bus matches: %s", err.message);
1401 goto fail;
1402 }
1403
1404 #ifdef DBUS_TYPE_UNIX_FD
1405 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), HFP_AG_ENDPOINT, &vtable_endpoint, y));
1406 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT, &vtable_endpoint, y));
1407 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT, &vtable_endpoint, y));
1408 #endif
1409
1410 list_adapters(y);
1411
1412 return y;
1413
1414 fail:
1415
1416 if (y)
1417 pa_bluetooth_discovery_unref(y);
1418
1419 dbus_error_free(&err);
1420
1421 return NULL;
1422 }
1423
1424 pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
1425 pa_assert(y);
1426 pa_assert(PA_REFCNT_VALUE(y) > 0);
1427
1428 PA_REFCNT_INC(y);
1429
1430 return y;
1431 }
1432
1433 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
1434 pa_assert(y);
1435 pa_assert(PA_REFCNT_VALUE(y) > 0);
1436
1437 if (PA_REFCNT_DEC(y) > 0)
1438 return;
1439
1440 pa_dbus_free_pending_list(&y->pending);
1441
1442 if (y->devices) {
1443 remove_all_devices(y);
1444 pa_hashmap_free(y->devices, NULL, NULL);
1445 }
1446
1447 if (y->connection) {
1448 #ifdef DBUS_TYPE_UNIX_FD
1449 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), HFP_AG_ENDPOINT);
1450 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT);
1451 dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT);
1452 #endif
1453 pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
1454 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
1455 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
1456 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
1457 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
1458 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
1459 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
1460 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'",
1461 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
1462 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
1463 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'",
1464 "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'",
1465 "type='signal',sender='org.bluez',interface='org.bluez.HandsfreeGateway',member='PropertyChanged'",
1466 NULL);
1467
1468 if (y->filter_added)
1469 dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
1470
1471 dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
1472
1473 pa_dbus_connection_unref(y->connection);
1474 }
1475
1476 pa_hook_done(&y->hook);
1477
1478 if (y->core)
1479 pa_shared_remove(y->core, "bluetooth-discovery");
1480
1481 pa_xfree(y);
1482 }
1483
1484 void pa_bluetooth_discovery_sync(pa_bluetooth_discovery *y) {
1485 pa_assert(y);
1486 pa_assert(PA_REFCNT_VALUE(y) > 0);
1487
1488 pa_dbus_sync_pending_list(&y->pending);
1489 }
1490
1491 pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y) {
1492 pa_assert(y);
1493 pa_assert(PA_REFCNT_VALUE(y) > 0);
1494
1495 return &y->hook;
1496 }
1497
1498 const char*pa_bluetooth_get_form_factor(uint32_t class) {
1499 unsigned i;
1500 const char *r;
1501
1502 static const char * const table[] = {
1503 [1] = "headset",
1504 [2] = "hands-free",
1505 [4] = "microphone",
1506 [5] = "speaker",
1507 [6] = "headphone",
1508 [7] = "portable",
1509 [8] = "car",
1510 [10] = "hifi"
1511 };
1512
1513 if (((class >> 8) & 31) != 4)
1514 return NULL;
1515
1516 if ((i = (class >> 2) & 63) > PA_ELEMENTSOF(table))
1517 r = NULL;
1518 else
1519 r = table[i];
1520
1521 if (!r)
1522 pa_log_debug("Unknown Bluetooth minor device class %u", i);
1523
1524 return r;
1525 }
1526
1527 char *pa_bluetooth_cleanup_name(const char *name) {
1528 char *t, *s, *d;
1529 pa_bool_t space = FALSE;
1530
1531 pa_assert(name);
1532
1533 while ((*name >= 1 && *name <= 32) || *name >= 127)
1534 name++;
1535
1536 t = pa_xstrdup(name);
1537
1538 for (s = d = t; *s; s++) {
1539
1540 if (*s <= 32 || *s >= 127 || *s == '_') {
1541 space = TRUE;
1542 continue;
1543 }
1544
1545 if (space) {
1546 *(d++) = ' ';
1547 space = FALSE;
1548 }
1549
1550 *(d++) = *s;
1551 }
1552
1553 *d = 0;
1554
1555 return t;
1556 }
1557
1558 pa_bool_t pa_bluetooth_uuid_has(pa_bluetooth_uuid *uuids, const char *uuid) {
1559 pa_assert(uuid);
1560
1561 while (uuids) {
1562 if (strcasecmp(uuids->uuid, uuid) == 0)
1563 return TRUE;
1564
1565 uuids = uuids->next;
1566 }
1567
1568 return FALSE;
1569 }