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