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