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