]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/bluetooth-util.c
bluetooth: recognize only those BT devices that implement both the Audio and either...
[pulseaudio] / src / modules / bluetooth / bluetooth-util.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 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
32 struct pa_bluetooth_discovery {
33 PA_REFCNT_DECLARE;
34
35 pa_core *core;
36 pa_dbus_connection *connection;
37 PA_LLIST_HEAD(pa_dbus_pending, pending);
38 pa_hashmap *devices;
39 pa_hook hook;
40 };
41
42 static void get_properties_reply(DBusPendingCall *pending, void *userdata);
43 static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessage *m, DBusPendingCallNotifyFunction func);
44
45 static pa_bt_audio_state_t pa_bt_audio_state_from_string(const char* value) {
46 pa_assert(value);
47
48 if (pa_streq(value, "disconnected"))
49 return PA_BT_AUDIO_STATE_DISCONNECTED;
50 else if (pa_streq(value, "connecting"))
51 return PA_BT_AUDIO_STATE_CONNECTING;
52 else if (pa_streq(value, "connected"))
53 return PA_BT_AUDIO_STATE_CONNECTED;
54 else if (pa_streq(value, "playing"))
55 return PA_BT_AUDIO_STATE_PLAYING;
56
57 return PA_BT_AUDIO_STATE_INVALID;
58 }
59
60 static pa_bluetooth_uuid *uuid_new(const char *uuid) {
61 pa_bluetooth_uuid *u;
62
63 u = pa_xnew(pa_bluetooth_uuid, 1);
64 u->uuid = pa_xstrdup(uuid);
65 PA_LLIST_INIT(pa_bluetooth_uuid, u);
66
67 return u;
68 }
69
70 static void uuid_free(pa_bluetooth_uuid *u) {
71 pa_assert(u);
72
73 pa_xfree(u->uuid);
74 pa_xfree(u);
75 }
76
77 static pa_bluetooth_device* device_new(const char *path) {
78 pa_bluetooth_device *d;
79
80 d = pa_xnew(pa_bluetooth_device, 1);
81
82 d->dead = FALSE;
83
84 d->device_info_valid = 0;
85
86 d->name = NULL;
87 d->path = pa_xstrdup(path);
88 d->paired = -1;
89 d->alias = NULL;
90 d->device_connected = -1;
91 PA_LLIST_HEAD_INIT(pa_bluetooth_uuid, d->uuids);
92 d->address = NULL;
93 d->class = -1;
94 d->trusted = -1;
95
96 d->audio_state = PA_BT_AUDIO_STATE_INVALID;
97 d->audio_sink_state = PA_BT_AUDIO_STATE_INVALID;
98 d->headset_state = PA_BT_AUDIO_STATE_INVALID;
99
100 return d;
101 }
102
103 static void device_free(pa_bluetooth_device *d) {
104 pa_bluetooth_uuid *u;
105
106 pa_assert(d);
107
108 while ((u = d->uuids)) {
109 PA_LLIST_REMOVE(pa_bluetooth_uuid, d->uuids, u);
110 uuid_free(u);
111 }
112
113 pa_xfree(d->name);
114 pa_xfree(d->path);
115 pa_xfree(d->alias);
116 pa_xfree(d->address);
117 pa_xfree(d);
118 }
119
120 static pa_bool_t device_is_audio(pa_bluetooth_device *d) {
121 pa_assert(d);
122
123 return
124 d->device_info_valid &&
125 (d->audio_state != PA_BT_AUDIO_STATE_INVALID &&
126 (d->audio_sink_state != PA_BT_AUDIO_STATE_INVALID ||
127 d->headset_state != PA_BT_AUDIO_STATE_INVALID));
128 }
129
130 static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessageIter *i) {
131 const char *key;
132 DBusMessageIter variant_i;
133
134 pa_assert(y);
135 pa_assert(d);
136 pa_assert(i);
137
138 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
139 pa_log("Property name not a string.");
140 return -1;
141 }
142
143 dbus_message_iter_get_basic(i, &key);
144
145 if (!dbus_message_iter_next(i)) {
146 pa_log("Property value missing");
147 return -1;
148 }
149
150 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
151 pa_log("Property value not a variant.");
152 return -1;
153 }
154
155 dbus_message_iter_recurse(i, &variant_i);
156
157 /* pa_log_debug("Parsing property org.bluez.Device.%s", key); */
158
159 switch (dbus_message_iter_get_arg_type(&variant_i)) {
160
161 case DBUS_TYPE_STRING: {
162
163 const char *value;
164 dbus_message_iter_get_basic(&variant_i, &value);
165
166 if (pa_streq(key, "Name")) {
167 pa_xfree(d->name);
168 d->name = pa_xstrdup(value);
169 } else if (pa_streq(key, "Alias")) {
170 pa_xfree(d->alias);
171 d->alias = pa_xstrdup(value);
172 } else if (pa_streq(key, "Address")) {
173 pa_xfree(d->address);
174 d->address = pa_xstrdup(value);
175 }
176
177 /* pa_log_debug("Value %s", value); */
178
179 break;
180 }
181
182 case DBUS_TYPE_BOOLEAN: {
183
184 dbus_bool_t value;
185 dbus_message_iter_get_basic(&variant_i, &value);
186
187 if (pa_streq(key, "Paired"))
188 d->paired = !!value;
189 else if (pa_streq(key, "Connected"))
190 d->device_connected = !!value;
191 else if (pa_streq(key, "Trusted"))
192 d->trusted = !!value;
193
194 /* pa_log_debug("Value %s", pa_yes_no(value)); */
195
196 break;
197 }
198
199 case DBUS_TYPE_UINT32: {
200
201 uint32_t value;
202 dbus_message_iter_get_basic(&variant_i, &value);
203
204 if (pa_streq(key, "Class"))
205 d->class = (int) value;
206
207 /* pa_log_debug("Value %u", (unsigned) value); */
208
209 break;
210 }
211
212 case DBUS_TYPE_ARRAY: {
213
214 DBusMessageIter ai;
215 dbus_message_iter_recurse(&variant_i, &ai);
216
217 if (dbus_message_iter_get_arg_type(&ai) == DBUS_TYPE_STRING &&
218 pa_streq(key, "UUIDs")) {
219
220 while (dbus_message_iter_get_arg_type(&ai) != DBUS_TYPE_INVALID) {
221 pa_bluetooth_uuid *node;
222 const char *value;
223 DBusMessage *m;
224
225 dbus_message_iter_get_basic(&ai, &value);
226 node = uuid_new(value);
227 PA_LLIST_PREPEND(pa_bluetooth_uuid, d->uuids, node);
228
229 /* 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 */
230 pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.Audio", "GetProperties"));
231 send_and_add_to_pending(y, d, m, get_properties_reply);
232
233 /* Vudentz said the interfaces are here when the UUIDs are announced */
234 if (strcasecmp(HSP_HS_UUID, value) == 0 || strcasecmp(HFP_HS_UUID, value) == 0) {
235 pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.Headset", "GetProperties"));
236 send_and_add_to_pending(y, d, m, get_properties_reply);
237 } else if (strcasecmp(A2DP_SINK_UUID, value) == 0) {
238 pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.AudioSink", "GetProperties"));
239 send_and_add_to_pending(y, d, m, get_properties_reply);
240 }
241
242 if (!dbus_message_iter_next(&ai))
243 break;
244 }
245 }
246
247 break;
248 }
249 }
250
251 return 0;
252 }
253
254 static int parse_audio_property(pa_bluetooth_discovery *u, int *state, DBusMessageIter *i) {
255 const char *key;
256 DBusMessageIter variant_i;
257
258 pa_assert(u);
259 pa_assert(state);
260 pa_assert(i);
261
262 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
263 pa_log("Property name not a string.");
264 return -1;
265 }
266
267 dbus_message_iter_get_basic(i, &key);
268
269 if (!dbus_message_iter_next(i)) {
270 pa_log("Property value missing");
271 return -1;
272 }
273
274 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
275 pa_log("Property value not a variant.");
276 return -1;
277 }
278
279 dbus_message_iter_recurse(i, &variant_i);
280
281 /* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|Headset}.%s", key); */
282
283 switch (dbus_message_iter_get_arg_type(&variant_i)) {
284
285 case DBUS_TYPE_STRING: {
286
287 const char *value;
288 dbus_message_iter_get_basic(&variant_i, &value);
289
290 if (pa_streq(key, "State"))
291 *state = pa_bt_audio_state_from_string(value);
292 /* pa_log_debug("Value %s", value); */
293
294 break;
295 }
296 }
297
298 return 0;
299 }
300
301 static void run_callback(pa_bluetooth_discovery *y, pa_bluetooth_device *d, pa_bool_t dead) {
302 pa_assert(y);
303 pa_assert(d);
304
305 if (!device_is_audio(d))
306 return;
307
308 d->dead = dead;
309 pa_hook_fire(&y->hook, d);
310 }
311
312 static void remove_all_devices(pa_bluetooth_discovery *y) {
313 pa_bluetooth_device *d;
314
315 pa_assert(y);
316
317 while ((d = pa_hashmap_steal_first(y->devices))) {
318 run_callback(y, d, TRUE);
319 device_free(d);
320 }
321 }
322
323 static void get_properties_reply(DBusPendingCall *pending, void *userdata) {
324 DBusMessage *r;
325 DBusMessageIter arg_i, element_i;
326 pa_dbus_pending *p;
327 pa_bluetooth_device *d;
328 pa_bluetooth_discovery *y;
329 int valid;
330
331 pa_assert_se(p = userdata);
332 pa_assert_se(y = p->context_data);
333 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
334
335 /* pa_log_debug("Got %s.GetProperties response for %s", */
336 /* dbus_message_get_interface(p->message), */
337 /* dbus_message_get_path(p->message)); */
338
339 d = p->call_data;
340
341 valid = dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR ? -1 : 1;
342
343 if (dbus_message_is_method_call(p->message, "org.bluez.Device", "GetProperties"))
344 d->device_info_valid = valid;
345
346 if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
347 pa_log_debug("Bluetooth daemon is apparently not available.");
348 remove_all_devices(y);
349 goto finish2;
350 }
351
352 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
353
354 if (!dbus_message_is_error(r, DBUS_ERROR_UNKNOWN_METHOD))
355 pa_log("Error from GetProperties reply: %s", dbus_message_get_error_name(r));
356
357 goto finish;
358 }
359
360 if (!dbus_message_iter_init(r, &arg_i)) {
361 pa_log("GetProperties reply has no arguments.");
362 goto finish;
363 }
364
365 if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) {
366 pa_log("GetProperties argument is not an array.");
367 goto finish;
368 }
369
370 dbus_message_iter_recurse(&arg_i, &element_i);
371 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
372
373 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
374 DBusMessageIter dict_i;
375
376 dbus_message_iter_recurse(&element_i, &dict_i);
377
378 if (dbus_message_has_interface(p->message, "org.bluez.Device")) {
379 if (parse_device_property(y, d, &dict_i) < 0)
380 goto finish;
381
382 } else if (dbus_message_has_interface(p->message, "org.bluez.Audio")) {
383 if (parse_audio_property(y, &d->audio_state, &dict_i) < 0)
384 goto finish;
385
386 } else if (dbus_message_has_interface(p->message, "org.bluez.Headset")) {
387 if (parse_audio_property(y, &d->headset_state, &dict_i) < 0)
388 goto finish;
389
390 } else if (dbus_message_has_interface(p->message, "org.bluez.AudioSink")) {
391 if (parse_audio_property(y, &d->audio_sink_state, &dict_i) < 0)
392 goto finish;
393 }
394 }
395
396 if (!dbus_message_iter_next(&element_i))
397 break;
398 }
399
400 finish:
401 run_callback(y, d, FALSE);
402
403 finish2:
404 dbus_message_unref(r);
405
406 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
407 pa_dbus_pending_free(p);
408 }
409
410 static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessage *m, DBusPendingCallNotifyFunction func) {
411 pa_dbus_pending *p;
412 DBusPendingCall *call;
413
414 pa_assert(y);
415 pa_assert(m);
416
417 pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y->connection), m, &call, -1));
418
419 p = pa_dbus_pending_new(pa_dbus_connection_get(y->connection), m, call, y, d);
420 PA_LLIST_PREPEND(pa_dbus_pending, y->pending, p);
421 dbus_pending_call_set_notify(call, func, p, NULL);
422
423 return p;
424 }
425
426 static void found_device(pa_bluetooth_discovery *y, const char* path) {
427 DBusMessage *m;
428 pa_bluetooth_device *d;
429
430 pa_assert(y);
431 pa_assert(path);
432
433 if (pa_hashmap_get(y->devices, path))
434 return;
435
436 d = device_new(path);
437
438 pa_hashmap_put(y->devices, d->path, d);
439
440 pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Device", "GetProperties"));
441 send_and_add_to_pending(y, d, m, get_properties_reply);
442
443 /* Before we read the other properties (Audio, AudioSink, Headset) we wait
444 * that the UUID is read */
445 }
446
447 static void list_devices_reply(DBusPendingCall *pending, void *userdata) {
448 DBusError e;
449 DBusMessage *r;
450 char **paths = NULL;
451 int num = -1;
452 pa_dbus_pending *p;
453 pa_bluetooth_discovery *y;
454
455 pa_assert(pending);
456
457 dbus_error_init(&e);
458
459 pa_assert_se(p = userdata);
460 pa_assert_se(y = p->context_data);
461 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
462
463 if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
464 pa_log_debug("Bluetooth daemon is apparently not available.");
465 remove_all_devices(y);
466 goto finish;
467 }
468
469 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
470 pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r));
471 goto finish;
472 }
473
474 if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) {
475 pa_log("org.bluez.Adapter.ListDevices returned an error: '%s'\n", e.message);
476 dbus_error_free(&e);
477 } else {
478 int i;
479
480 for (i = 0; i < num; ++i)
481 found_device(y, paths[i]);
482 }
483
484 finish:
485 if (paths)
486 dbus_free_string_array (paths);
487
488 dbus_message_unref(r);
489
490 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
491 pa_dbus_pending_free(p);
492 }
493
494 static void found_adapter(pa_bluetooth_discovery *y, const char *path) {
495 DBusMessage *m;
496
497 pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Adapter", "ListDevices"));
498 send_and_add_to_pending(y, NULL, m, list_devices_reply);
499 }
500
501 static void list_adapters_reply(DBusPendingCall *pending, void *userdata) {
502 DBusError e;
503 DBusMessage *r;
504 char **paths = NULL;
505 int num = -1;
506 pa_dbus_pending *p;
507 pa_bluetooth_discovery *y;
508
509 pa_assert(pending);
510
511 dbus_error_init(&e);
512
513 pa_assert_se(p = userdata);
514 pa_assert_se(y = p->context_data);
515 pa_assert_se(r = dbus_pending_call_steal_reply(pending));
516
517 if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) {
518 pa_log_debug("Bluetooth daemon is apparently not available.");
519 remove_all_devices(y);
520 goto finish;
521 }
522
523 if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
524 pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r));
525 goto finish;
526 }
527
528 if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) {
529 pa_log("org.bluez.Manager.ListAdapters returned an error: %s", e.message);
530 dbus_error_free(&e);
531 } else {
532 int i;
533
534 for (i = 0; i < num; ++i)
535 found_adapter(y, paths[i]);
536 }
537
538 finish:
539 if (paths)
540 dbus_free_string_array (paths);
541
542 dbus_message_unref(r);
543
544 PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
545 pa_dbus_pending_free(p);
546 }
547
548 static void list_adapters(pa_bluetooth_discovery *y) {
549 DBusMessage *m;
550 pa_assert(y);
551
552 pa_assert_se(m = dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
553 send_and_add_to_pending(y, NULL, m, list_adapters_reply);
554 }
555
556 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
557 DBusError err;
558 pa_bluetooth_discovery *y;
559
560 pa_assert(bus);
561 pa_assert(m);
562
563 pa_assert_se(y = userdata);
564
565 dbus_error_init(&err);
566
567 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
568 dbus_message_get_interface(m),
569 dbus_message_get_path(m),
570 dbus_message_get_member(m));
571
572 if (dbus_message_is_signal(m, "org.bluez.Adapter", "DeviceRemoved")) {
573 const char *path;
574 pa_bluetooth_device *d;
575
576 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
577 pa_log("Failed to parse org.bluez.Adapter.DeviceRemoved: %s", err.message);
578 goto fail;
579 }
580
581 pa_log_debug("Device %s removed", path);
582
583 if ((d = pa_hashmap_remove(y->devices, path))) {
584 run_callback(y, d, TRUE);
585 device_free(d);
586 }
587
588 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
589
590 } else if (dbus_message_is_signal(m, "org.bluez.Adapter", "DeviceCreated")) {
591 const char *path;
592
593 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
594 pa_log("Failed to parse org.bluez.Adapter.DeviceCreated: %s", err.message);
595 goto fail;
596 }
597
598 pa_log_debug("Device %s created", path);
599
600 found_device(y, path);
601 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
602
603 } else if (dbus_message_is_signal(m, "org.bluez.Manager", "AdapterAdded")) {
604 const char *path;
605
606 if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
607 pa_log("Failed to parse org.bluez.Manager.AdapterAdded: %s", err.message);
608 goto fail;
609 }
610
611 pa_log_debug("Adapter %s created", path);
612
613 found_adapter(y, path);
614 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
615
616 } else if (dbus_message_is_signal(m, "org.bluez.Audio", "PropertyChanged") ||
617 dbus_message_is_signal(m, "org.bluez.Headset", "PropertyChanged") ||
618 dbus_message_is_signal(m, "org.bluez.AudioSink", "PropertyChanged") ||
619 dbus_message_is_signal(m, "org.bluez.Device", "PropertyChanged")) {
620
621 pa_bluetooth_device *d;
622
623 if ((d = pa_hashmap_get(y->devices, dbus_message_get_path(m)))) {
624 DBusMessageIter arg_i;
625
626 if (!dbus_message_iter_init(m, &arg_i)) {
627 pa_log("Failed to parse PropertyChanged: %s", err.message);
628 goto fail;
629 }
630
631 if (dbus_message_has_interface(m, "org.bluez.Device")) {
632 if (parse_device_property(y, d, &arg_i) < 0)
633 goto fail;
634
635 } else if (dbus_message_has_interface(m, "org.bluez.Audio")) {
636 if (parse_audio_property(y, &d->audio_state, &arg_i) < 0)
637 goto fail;
638
639 } else if (dbus_message_has_interface(m, "org.bluez.Headset")) {
640 if (parse_audio_property(y, &d->headset_state, &arg_i) < 0)
641 goto fail;
642
643 } else if (dbus_message_has_interface(m, "org.bluez.AudioSink")) {
644 if (parse_audio_property(y, &d->audio_sink_state, &arg_i) < 0)
645 goto fail;
646 }
647
648 run_callback(y, d, FALSE);
649 }
650
651 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
652
653 } else if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
654 const char *name, *old_owner, *new_owner;
655
656 if (!dbus_message_get_args(m, &err,
657 DBUS_TYPE_STRING, &name,
658 DBUS_TYPE_STRING, &old_owner,
659 DBUS_TYPE_STRING, &new_owner,
660 DBUS_TYPE_INVALID)) {
661 pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
662 goto fail;
663 }
664
665 if (pa_streq(name, "org.bluez")) {
666 if (old_owner && *old_owner) {
667 pa_log_debug("Bluetooth daemon disappeared.");
668 remove_all_devices(y);
669 }
670
671 if (new_owner && *new_owner) {
672 pa_log_debug("Bluetooth daemon appeared.");
673 list_adapters(y);
674 }
675 }
676
677 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
678 }
679
680 fail:
681 dbus_error_free(&err);
682
683 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
684 }
685
686 const pa_bluetooth_device* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery *y, const char* address) {
687 pa_bluetooth_device *d;
688 void *state = NULL;
689
690 pa_assert(y);
691 pa_assert(PA_REFCNT_VALUE(y) > 0);
692 pa_assert(address);
693
694 if (!pa_hook_is_firing(&y->hook))
695 pa_bluetooth_discovery_sync(y);
696
697 while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
698 if (pa_streq(d->address, address))
699 return d;
700
701 return NULL;
702 }
703
704 const pa_bluetooth_device* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery *y, const char* path) {
705 pa_assert(y);
706 pa_assert(PA_REFCNT_VALUE(y) > 0);
707 pa_assert(path);
708
709 if (!pa_hook_is_firing(&y->hook))
710 pa_bluetooth_discovery_sync(y);
711
712 return pa_hashmap_get(y->devices, path);
713 }
714
715 static int setup_dbus(pa_bluetooth_discovery *y) {
716 DBusError err;
717
718 dbus_error_init(&err);
719
720 y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err);
721
722 if (dbus_error_is_set(&err) || !y->connection) {
723 pa_log("Failed to get D-Bus connection: %s", err.message);
724 dbus_error_free(&err);
725 return -1;
726 }
727
728 return 0;
729 }
730
731 pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
732 DBusError err;
733 pa_bluetooth_discovery *y;
734
735 pa_assert(c);
736
737 dbus_error_init(&err);
738
739 if ((y = pa_shared_get(c, "bluetooth-discovery")))
740 return pa_bluetooth_discovery_ref(y);
741
742 y = pa_xnew0(pa_bluetooth_discovery, 1);
743 PA_REFCNT_INIT(y);
744 y->core = c;
745 y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
746 PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
747 pa_hook_init(&y->hook, y);
748 pa_shared_set(c, "bluetooth-discovery", y);
749
750 if (setup_dbus(y) < 0)
751 goto fail;
752
753 /* dynamic detection of bluetooth audio devices */
754 if (!dbus_connection_add_filter(pa_dbus_connection_get(y->connection), filter_cb, y, NULL)) {
755 pa_log_error("Failed to add filter function");
756 goto fail;
757 }
758
759 if (pa_dbus_add_matches(
760 pa_dbus_connection_get(y->connection), &err,
761 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'",
762 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
763 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
764 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
765 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
766 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
767 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
768 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL) < 0) {
769 pa_log("Failed to add D-Bus matches: %s", err.message);
770 goto fail;
771 }
772
773 list_adapters(y);
774
775 return y;
776
777 fail:
778
779 if (y)
780 pa_bluetooth_discovery_unref(y);
781
782 dbus_error_free(&err);
783
784 return NULL;
785 }
786
787 pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
788 pa_assert(y);
789 pa_assert(PA_REFCNT_VALUE(y) > 0);
790
791 PA_REFCNT_INC(y);
792
793 return y;
794 }
795
796 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
797 pa_assert(y);
798 pa_assert(PA_REFCNT_VALUE(y) > 0);
799
800 if (PA_REFCNT_DEC(y) > 0)
801 return;
802
803 pa_dbus_free_pending_list(&y->pending);
804
805 if (y->devices) {
806 remove_all_devices(y);
807 pa_hashmap_free(y->devices, NULL, NULL);
808 }
809
810 if (y->connection) {
811 pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
812 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'",
813 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'",
814 "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'",
815 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'",
816 "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'",
817 "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'",
818 "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'",
819 "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'",
820 "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL);
821
822 dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
823
824 pa_dbus_connection_unref(y->connection);
825 }
826
827 pa_hook_done(&y->hook);
828
829 if (y->core)
830 pa_shared_remove(y->core, "bluetooth-discovery");
831
832 pa_xfree(y);
833 }
834
835 void pa_bluetooth_discovery_sync(pa_bluetooth_discovery *y) {
836 pa_assert(y);
837 pa_assert(PA_REFCNT_VALUE(y) > 0);
838
839 pa_dbus_sync_pending_list(&y->pending);
840 }
841
842 pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y) {
843 pa_assert(y);
844 pa_assert(PA_REFCNT_VALUE(y) > 0);
845
846 return &y->hook;
847 }
848
849 const char*pa_bluetooth_get_form_factor(uint32_t class) {
850 unsigned i;
851 const char *r;
852
853 static const char * const table[] = {
854 [1] = "headset",
855 [2] = "hands-free",
856 [4] = "microphone",
857 [5] = "speaker",
858 [6] = "headphone",
859 [7] = "portable",
860 [8] = "car",
861 [10] = "hifi"
862 };
863
864 if (((class >> 8) & 31) != 4)
865 return NULL;
866
867 if ((i = (class >> 2) & 63) > PA_ELEMENTSOF(table))
868 r = NULL;
869 else
870 r = table[i];
871
872 if (!r)
873 pa_log_debug("Unknown Bluetooth minor device class %u", i);
874
875 return r;
876 }
877
878 char *pa_bluetooth_cleanup_name(const char *name) {
879 char *t, *s, *d;
880 pa_bool_t space = FALSE;
881
882 pa_assert(name);
883
884 while ((*name >= 1 && *name <= 32) || *name >= 127)
885 name++;
886
887 t = pa_xstrdup(name);
888
889 for (s = d = t; *s; s++) {
890
891 if (*s <= 32 || *s >= 127 || *s == '_') {
892 space = TRUE;
893 continue;
894 }
895
896 if (space) {
897 *(d++) = ' ';
898 space = FALSE;
899 }
900
901 *(d++) = *s;
902 }
903
904 *d = 0;
905
906 return t;
907 }
908
909 pa_bool_t pa_bluetooth_uuid_has(pa_bluetooth_uuid *uuids, const char *uuid) {
910 pa_assert(uuid);
911
912 while (uuids) {
913 if (strcasecmp(uuids->uuid, uuid) == 0)
914 return TRUE;
915
916 uuids = uuids->next;
917 }
918
919 return FALSE;
920 }