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