]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/module-bluetooth-discover.c
Unload module-bluetooth-device if the remote device disconnects.
[pulseaudio] / src / modules / bluetooth / module-bluetooth-discover.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 published
8 by the Free Software Foundation; either version 2 of the License,
9 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 License
17 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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <pulse/xmalloc.h>
31 #include <pulsecore/module.h>
32 #include <pulsecore/modargs.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/llist.h>
35 #include <pulsecore/core-util.h>
36
37 #include "../dbus-util.h"
38 #include "module-bluetooth-discover-symdef.h"
39
40 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
41 PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
42 PA_MODULE_VERSION(PACKAGE_VERSION);
43 PA_MODULE_USAGE("");
44
45 struct module {
46 char *profile;
47 pa_module *pa_m;
48 PA_LLIST_FIELDS(struct module);
49 };
50
51 struct uuid {
52 char *uuid;
53 PA_LLIST_FIELDS(struct uuid);
54 };
55
56 struct device {
57 char *name;
58 char *object_path;
59 int paired;
60 char *alias;
61 int connected;
62 PA_LLIST_HEAD(struct uuid, uuid_list);
63 char *address;
64 int class;
65 int trusted;
66 PA_LLIST_HEAD(struct module, module_list);
67 PA_LLIST_FIELDS(struct device);
68 };
69
70 struct userdata {
71 pa_module *module;
72 pa_dbus_connection *conn;
73 PA_LLIST_HEAD(struct device, device_list);
74 };
75
76 static struct module *module_new(const char *profile, pa_module *pa_m) {
77 struct module *m;
78
79 m = pa_xnew(struct module, 1);
80 m->profile = pa_xstrdup(profile);
81 m->pa_m = pa_m;
82 PA_LLIST_INIT(struct module, m);
83
84 return m;
85 }
86
87 static void module_free(struct module *m) {
88 pa_assert(m);
89
90 pa_xfree(m->profile);
91 pa_xfree(m);
92 }
93
94 static struct module* module_find(struct device *d, const char *profile) {
95 struct module *m;
96
97 for (m = d->module_list; d; d = d->next)
98 if (pa_streq(m->profile, profile))
99 return m;
100
101 return NULL;
102 }
103
104 static struct uuid *uuid_new(const char *uuid) {
105 struct uuid *node;
106
107 node = pa_xnew(struct uuid, 1);
108 node->uuid = pa_xstrdup(uuid);
109 PA_LLIST_INIT(struct uuid, node);
110
111 return node;
112 }
113
114 static void uuid_free(struct uuid *uuid) {
115 pa_assert(uuid);
116
117 pa_xfree(uuid->uuid);
118 pa_xfree(uuid);
119 }
120
121 static struct device *device_new(const char *object_path) {
122 struct device *node;
123
124 node = pa_xnew(struct device, 1);
125 node->name = NULL;
126 node->object_path = pa_xstrdup(object_path);
127 node->paired = -1;
128 node->alias = NULL;
129 node->connected = -1;
130 PA_LLIST_HEAD_INIT(struct uuid, node->uuid_list);
131 node->address = NULL;
132 node->class = -1;
133 node->trusted = -1;
134 PA_LLIST_HEAD_INIT(struct module, node->module_list);
135 PA_LLIST_INIT(struct device, node);
136
137 return node;
138 }
139
140 static void device_free(struct device *device) {
141 struct module *m;
142 struct uuid *i;
143
144 pa_assert(device);
145
146 while ((m = device->module_list)) {
147 PA_LLIST_REMOVE(struct module, device->module_list, m);
148 module_free(m);
149 }
150
151 while ((i = device->uuid_list)) {
152 PA_LLIST_REMOVE(struct uuid, device->uuid_list, i);
153 uuid_free(i);
154 }
155
156 pa_xfree(device->name);
157 pa_xfree(device->object_path);
158 pa_xfree(device->alias);
159 pa_xfree(device->address);
160 pa_xfree(device);
161 }
162
163 static struct device* device_find(struct userdata *u, const char *path) {
164 struct device *i;
165
166 for (i = u->device_list; i; i = i->next)
167 if (pa_streq(i->object_path, path))
168 return i;
169
170 return NULL;
171 }
172
173 static int parse_device_property(struct userdata *u, struct device *d, DBusMessageIter *i) {
174 const char *key;
175 DBusMessageIter variant_i;
176
177 pa_assert(u);
178 pa_assert(d);
179 pa_assert(i);
180
181 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) {
182 pa_log("Property name not a string.");
183 return -1;
184 }
185
186 dbus_message_iter_get_basic(i, &key);
187
188 if (!dbus_message_iter_next(i)) {
189 pa_log("Property value missing");
190 return -1;
191 }
192
193 if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_VARIANT) {
194 pa_log("Property value not a variant.");
195 return -1;
196 }
197
198 dbus_message_iter_recurse(i, &variant_i);
199
200 pa_log_debug("Parsing device property %s", key);
201
202 switch (dbus_message_iter_get_arg_type(&variant_i)) {
203
204 case DBUS_TYPE_STRING: {
205
206 const char *value;
207 dbus_message_iter_get_basic(&variant_i, &value);
208
209 if (pa_streq(key, "Name")) {
210 pa_xfree(d->name);
211 d->name = pa_xstrdup(value);
212 } else if (pa_streq(key, "Alias")) {
213 pa_xfree(d->alias);
214 d->alias = pa_xstrdup(value);
215 } else if (pa_streq(key, "Address")) {
216 pa_xfree(d->address);
217 d->address = pa_xstrdup(value);
218 }
219
220 break;
221 }
222
223 case DBUS_TYPE_BOOLEAN: {
224
225 dbus_bool_t value;
226 dbus_message_iter_get_basic(&variant_i, &value);
227
228 if (pa_streq(key, "Paired"))
229 d->paired = !!value;
230 else if (pa_streq(key, "Connected"))
231 d->connected = !!value;
232 else if (pa_streq(key, "Trusted"))
233 d->trusted = !!value;
234
235 break;
236 }
237
238 case DBUS_TYPE_UINT32: {
239
240 uint32_t value;
241 dbus_message_iter_get_basic(&variant_i, &value);
242
243 if (pa_streq(key, "Class"))
244 d->class = (int) value;
245
246 break;
247 }
248
249 case DBUS_TYPE_ARRAY: {
250
251 DBusMessageIter ai;
252 dbus_message_iter_recurse(&variant_i, &ai);
253
254 if (dbus_message_iter_get_arg_type(&ai) == DBUS_TYPE_STRING &&
255 pa_streq(key, "UUIDs")) {
256
257 while (dbus_message_iter_get_arg_type(&ai) != DBUS_TYPE_INVALID) {
258 struct uuid *node;
259 const char *value;
260
261 dbus_message_iter_get_basic(&ai, &value);
262 node = uuid_new(value);
263 PA_LLIST_PREPEND(struct uuid, d->uuid_list, node);
264
265 if (!dbus_message_iter_next(&ai))
266 break;
267 }
268 }
269
270 break;
271 }
272 }
273
274 return 0;
275 }
276
277 static int get_device_properties(struct userdata *u, struct device *d) {
278 DBusError e;
279 DBusMessage *m = NULL, *r = NULL;
280 DBusMessageIter arg_i, element_i;
281 int ret = -1;
282
283 pa_assert(u);
284 pa_assert(d);
285
286 dbus_error_init(&e);
287
288 pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->object_path, "org.bluez.Device", "GetProperties"));
289
290 r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e);
291
292 if (!r) {
293 pa_log("org.bluez.Device.GetProperties failed: %s", e.message);
294 goto finish;
295 }
296
297 if (!dbus_message_iter_init(r, &arg_i)) {
298 pa_log("org.bluez.Device.GetProperties reply has no arguments");
299 goto finish;
300 }
301
302 if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_ARRAY) {
303 pa_log("org.bluez.Device.GetProperties argument is not an array");
304 goto finish;
305 }
306
307 dbus_message_iter_recurse(&arg_i, &element_i);
308 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
309
310 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
311 DBusMessageIter dict_i;
312
313 dbus_message_iter_recurse(&element_i, &dict_i);
314
315 if (parse_device_property(u, d, &dict_i) < 0)
316 goto finish;
317 }
318
319 if (!dbus_message_iter_next(&element_i))
320 break;
321 }
322
323 ret = 0;
324
325 finish:
326 if (m)
327 dbus_message_unref(m);
328 if (r)
329 dbus_message_unref(r);
330
331 dbus_error_free(&e);
332
333 return ret;
334 }
335
336 static void load_module_for_device(struct userdata *u, struct device *d, const char *profile) {
337 char *args;
338 pa_module *pa_m;
339 struct module *m;
340
341 pa_assert(u);
342 pa_assert(d);
343
344 get_device_properties(u, d);
345 args = pa_sprintf_malloc("sink_name=\"%s\" address=\"%s\" profile=\"%s\"", d->name, d->address, profile);
346 pa_m = pa_module_load(u->module->core, "module-bluetooth-device", args);
347 pa_xfree(args);
348
349 if (!m) {
350 pa_log_debug("Failed to load module for device %s", d->object_path);
351 return;
352 }
353
354 m = module_new(profile, pa_m);
355 PA_LLIST_PREPEND(struct module, d->module_list, m);
356 }
357
358 static void unload_module_for_device(struct userdata *u, struct device *d, const char *profile) {
359 struct module *m;
360
361 pa_assert(u);
362 pa_assert(d);
363
364 if (!(m = module_find(d, profile)))
365 return;
366
367 pa_module_unload_request(m->pa_m, TRUE);
368
369 PA_LLIST_REMOVE(struct module, d->module_list, m);
370 module_free(m);
371 }
372
373 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void *userdata) {
374 DBusMessageIter arg_i;
375 DBusError err;
376 const char *value;
377 struct userdata *u;
378
379 pa_assert(bus);
380 pa_assert(msg);
381 pa_assert(userdata);
382 u = userdata;
383
384 dbus_error_init(&err);
385
386 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
387 dbus_message_get_interface(msg),
388 dbus_message_get_path(msg),
389 dbus_message_get_member(msg));
390
391 if (dbus_message_is_signal(msg, "org.bluez.Adapter", "DeviceRemoved")) {
392
393 if (!dbus_message_iter_init(msg, &arg_i))
394 pa_log("dbus: message has no parameters");
395 else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH)
396 pa_log("dbus: argument is not object path");
397 else {
398 struct device *d;
399
400 dbus_message_iter_get_basic(&arg_i, &value);
401 pa_log_debug("hcid: device %s removed", value);
402
403 if ((d = device_find(u, value))) {
404 PA_LLIST_REMOVE(struct device, u->device_list, d);
405 device_free(d);
406 }
407 }
408
409 } else if (dbus_message_is_signal(msg, "org.bluez.Headset", "PropertyChanged") ||
410 dbus_message_is_signal(msg, "org.bluez.AudioSink", "PropertyChanged")) {
411
412 struct device *d;
413 const char *profile;
414 DBusMessageIter variant_i;
415 dbus_bool_t connected;
416
417 if (!dbus_message_iter_init(msg, &arg_i)) {
418 pa_log("dbus: message has no parameters");
419 goto done;
420 }
421
422 if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_STRING) {
423 pa_log("Property name not a string.");
424 goto done;
425 }
426
427 dbus_message_iter_get_basic(&arg_i, &value);
428
429 if (!pa_streq(value, "Connected"))
430 goto done;
431
432 if (!dbus_message_iter_next(&arg_i)) {
433 pa_log("Property value missing");
434 goto done;
435 }
436
437 if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_VARIANT) {
438 pa_log("Property value not a variant.");
439 goto done;
440 }
441
442 dbus_message_iter_recurse(&arg_i, &variant_i);
443
444 if (dbus_message_iter_get_arg_type(&variant_i) != DBUS_TYPE_BOOLEAN) {
445 pa_log("Property value not a boolean.");
446 goto done;
447 }
448
449 dbus_message_iter_get_basic(&variant_i, &connected);
450
451 if (dbus_message_is_signal(msg, "org.bluez.Headset", "PropertyChanged"))
452 profile = "hsp";
453 else
454 profile = "a2dp";
455
456 d = device_find(u, dbus_message_get_path(msg));
457
458 if (connected) {
459 if (!d) {
460 d = device_new(dbus_message_get_path(msg));
461 PA_LLIST_PREPEND(struct device, u->device_list, d);
462 }
463
464 load_module_for_device(u, d, profile);
465 } else if (d)
466 unload_module_for_device(u, d, profile);
467 }
468
469 done:
470 dbus_error_free(&err);
471 return DBUS_HANDLER_RESULT_HANDLED;
472 }
473
474 void pa__done(pa_module* m) {
475 struct userdata *u;
476 struct device *i;
477
478 pa_assert(m);
479
480 if (!(u = m->userdata))
481 return;
482
483 while ((i = u->device_list)) {
484 PA_LLIST_REMOVE(struct device, u->device_list, i);
485 device_free(i);
486 }
487
488 if (u->conn)
489 pa_dbus_connection_unref(u->conn);
490
491 pa_xfree(u);
492 }
493
494 int pa__init(pa_module* m) {
495 DBusError err;
496 struct userdata *u;
497
498 pa_assert(m);
499 dbus_error_init(&err);
500
501 m->userdata = u = pa_xnew(struct userdata, 1);
502 u->module = m;
503 PA_LLIST_HEAD_INIT(struct device, u->device_list);
504
505 /* connect to the bus */
506 u->conn = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &err);
507 if (dbus_error_is_set(&err) || (u->conn == NULL) ) {
508 pa_log("Failed to get D-Bus connection: %s", err.message);
509 goto fail;
510 }
511
512 /* dynamic detection of bluetooth audio devices */
513 if (!dbus_connection_add_filter(pa_dbus_connection_get(u->conn), filter_cb, u, NULL)) {
514 pa_log_error("Failed to add filter function");
515 goto fail;
516 }
517
518 dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", &err);
519 if (dbus_error_is_set(&err)) {
520 pa_log_error("Unable to subscribe to org.bluez.Adapter signals: %s: %s", err.name, err.message);
521 goto fail;
522 }
523
524 dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'", &err);
525 if (dbus_error_is_set(&err)) {
526 pa_log_error("Unable to subscribe to org.bluez.Headset signals: %s: %s", err.name, err.message);
527 goto fail;
528 }
529
530 dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", &err);
531 if (dbus_error_is_set(&err)) {
532 pa_log_error("Unable to subscribe to org.bluez.AudioSink signals: %s: %s", err.name, err.message);
533 goto fail;
534 }
535
536 return 0;
537
538 fail:
539 dbus_error_free(&err);
540 pa__done(m);
541
542 return -1;
543 }