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