2 This file is part of PulseAudio.
4 Copyright 2008 Joao Paulo Rechi Vita
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.
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.
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
28 #include <pulse/xmalloc.h>
29 #include <pulsecore/module.h>
30 #include <pulsecore/modargs.h>
31 #include <pulsecore/macro.h>
32 #include <pulsecore/llist.h>
34 #include "dbus-util.h"
35 #include "module-bt-discover-symdef.h"
37 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
38 PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
39 PA_MODULE_VERSION(PACKAGE_VERSION
);
42 #define HSP_HS_UUID "00001108-0000-1000-8000-00805F9B34FB"
43 #define HFP_HS_UUID "0000111E-0000-1000-8000-00805F9B34FB"
44 #define A2DP_SOURCE_UUID "0000110A-0000-1000-8000-00805F9B34FB"
45 #define A2DP_SINK_UUID "0000110B-0000-1000-8000-00805F9B34FB"
49 PA_LLIST_FIELDS(struct uuid
);
56 struct adapter
*adapter
;
59 PA_LLIST_HEAD(struct uuid
, uuid_list
);
63 PA_LLIST_FIELDS(struct device
);
70 PA_LLIST_HEAD(struct device
, device_list
);
71 PA_LLIST_FIELDS(struct adapter
);
76 pa_dbus_connection
*conn
;
77 PA_LLIST_HEAD(struct adapter
, adapter_list
);
80 static struct uuid
*uuid_new(const char *uuid
) {
81 struct uuid
*node
= pa_xnew(struct uuid
, 1);
82 node
->uuid
= pa_xstrdup(uuid
);
83 PA_LLIST_INIT(struct uuid
, node
);
87 static void uuid_free(struct uuid
*uuid
) {
91 static struct device
*device_new(const char *device
, struct adapter
*adapter
) {
92 struct device
*node
= pa_xnew(struct device
, 1);
94 node
->object_path
= pa_xstrdup(device
);
96 node
->adapter
= adapter
;
99 PA_LLIST_HEAD_INIT(struct uuid
, node
->uuid_list
);
100 node
->address
= NULL
;
103 PA_LLIST_INIT(struct device
, node
);
107 static void device_free(struct device
*device
) {
108 struct uuid
*uuid_list_i
;
109 while (device
->uuid_list
) {
110 uuid_list_i
= device
->uuid_list
;
111 PA_LLIST_REMOVE(struct uuid
, device
->uuid_list
, uuid_list_i
);
112 uuid_free(uuid_list_i
);
117 static struct adapter
*adapter_new(const char *adapter
) {
118 struct adapter
*node
= pa_xnew(struct adapter
, 1);
119 node
->object_path
= pa_xstrdup(adapter
);
121 node
->address
= NULL
;
122 PA_LLIST_HEAD_INIT(struct device
, node
->device_list
);
123 PA_LLIST_INIT(struct adapter
, node
);
127 static void adapter_free(struct adapter
*adapter
) {
128 struct device
*device_list_i
;
129 while (adapter
->device_list
) {
130 device_list_i
= adapter
->device_list
;
131 PA_LLIST_REMOVE(struct device
, adapter
->device_list
, device_list_i
);
132 device_free(device_list_i
);
137 static void print_devices(struct device
*device_list
) {
138 struct device
*device_list_i
= device_list
;
139 while (device_list_i
!= NULL
) {
140 struct uuid
*uuid_list_i
= device_list_i
->uuid_list
;
141 if (strcmp(device_list_i
->object_path
, "/DEVICE_HEAD") != 0) {
142 pa_log(" [ %s ]", device_list_i
->object_path
);
143 pa_log(" Name = %s", device_list_i
->name
);
144 pa_log(" Paired = %d", device_list_i
->paired
);
145 pa_log(" Adapter = %s", device_list_i
->adapter
->object_path
);
146 pa_log(" Alias = %s", device_list_i
->alias
);
147 pa_log(" Connected = %d", device_list_i
->connected
);
149 while (uuid_list_i
!= NULL
) {
150 if (strcmp(uuid_list_i
->uuid
, "UUID_HEAD") != 0)
151 pa_log(" %s", uuid_list_i
->uuid
);
152 uuid_list_i
= uuid_list_i
->next
;
154 pa_log(" Address = %s", device_list_i
->address
);
155 pa_log(" Class = 0x%x", device_list_i
->class);
156 pa_log(" Trusted = %d", device_list_i
->trusted
);
158 device_list_i
= device_list_i
->next
;
162 static void print_adapters(struct adapter
*adapter_list
) {
163 struct adapter
*adapter_list_i
= adapter_list
;
164 while (adapter_list_i
!= NULL
) {
165 if (strcmp(adapter_list_i
->object_path
, "/ADAPTER_HEAD") != 0) {
166 pa_log("[ %s ]", adapter_list_i
->object_path
);
167 pa_log(" Mode = %s", adapter_list_i
->mode
);
168 pa_log(" Address = %s", adapter_list_i
->address
);
169 print_devices(adapter_list_i
->device_list
);
171 adapter_list_i
= adapter_list_i
->next
;
175 static void detect_adapters(struct userdata
*u
) {
177 DBusMessage
*m
= NULL
, *r
= NULL
;
178 DBusMessageIter arg_i
, element_i
, dict_i
, variant_i
;
179 struct adapter
*adapter_list_i
;
180 const char *key
, *value
;
186 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", "/", "org.bluez.Manager", "ListAdapters"));
187 r
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u
->conn
), m
, -1, &e
);
189 pa_log("org.bluez.Manager.ListAdapters failed: %s", e
.message
);
192 if (!dbus_message_iter_init(r
, &arg_i
)) {
193 pa_log("org.bluez.Manager.ListAdapters reply has no arguments");
196 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
197 pa_log("org.bluez.Manager.ListAdapters argument is not an array");
200 dbus_message_iter_recurse(&arg_i
, &element_i
);
201 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
202 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_OBJECT_PATH
) {
203 struct adapter
*node
;
204 dbus_message_iter_get_basic(&element_i
, &value
);
205 node
= adapter_new(value
);
206 PA_LLIST_PREPEND(struct adapter
, u
->adapter_list
, node
);
208 dbus_message_iter_next(&element_i
);
211 /* get adapter properties */
212 adapter_list_i
= u
->adapter_list
;
213 while (adapter_list_i
!= NULL
) {
214 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", adapter_list_i
->object_path
, "org.bluez.Adapter", "GetProperties"));
215 r
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u
->conn
), m
, -1, &e
);
217 pa_log("org.bluez.Adapter.GetProperties failed: %s", e
.message
);
220 if (!dbus_message_iter_init(r
, &arg_i
)) {
221 pa_log("org.bluez.Adapter.GetProperties reply has no arguments");
224 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
225 pa_log("org.bluez.Adapter.GetProperties argument is not an array");
228 dbus_message_iter_recurse(&arg_i
, &element_i
);
229 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
230 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
231 dbus_message_iter_recurse(&element_i
, &dict_i
);
232 dbus_message_iter_get_basic(&dict_i
, &key
);
233 dbus_message_iter_next(&dict_i
);
234 dbus_message_iter_recurse(&dict_i
, &variant_i
);
235 dbus_message_iter_get_basic(&variant_i
, &value
);
236 if (strcmp(key
, "Mode") == 0)
237 adapter_list_i
->mode
= pa_xstrdup(value
);
238 else if (strcmp(key
, "Address") == 0)
239 adapter_list_i
->address
= pa_xstrdup(value
);
241 dbus_message_iter_next(&element_i
);
243 adapter_list_i
= adapter_list_i
->next
;
248 dbus_message_unref(m
);
250 dbus_message_unref(r
);
254 static void detect_devices(struct userdata
*u
) {
256 DBusMessage
*m
= NULL
, *r
= NULL
;
257 DBusMessageIter arg_i
, element_i
, dict_i
, variant_i
;
258 struct adapter
*adapter_list_i
;
259 struct device
*device_list_i
;
260 const char *key
, *value
;
266 /* get devices of each adapter */
267 adapter_list_i
= u
->adapter_list
;
268 while (adapter_list_i
!= NULL
) {
269 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", adapter_list_i
->object_path
, "org.bluez.Adapter", "ListDevices"));
270 r
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u
->conn
), m
, -1, &e
);
272 pa_log("org.bluez.Adapter.ListDevices failed: %s", e
.message
);
275 if (!dbus_message_iter_init(r
, &arg_i
)) {
276 pa_log("org.bluez.Adapter.ListDevices reply has no arguments");
279 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
280 pa_log("org.bluez.Adapter.ListDevices argument is not an array");
283 dbus_message_iter_recurse(&arg_i
, &element_i
);
284 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
285 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_OBJECT_PATH
) {
287 dbus_message_iter_get_basic(&element_i
, &value
);
288 node
= device_new(value
, adapter_list_i
);
289 PA_LLIST_PREPEND(struct device
, adapter_list_i
->device_list
, node
);
291 dbus_message_iter_next(&element_i
);
293 adapter_list_i
= adapter_list_i
->next
;
296 /* get device properties */
297 adapter_list_i
= u
->adapter_list
;
298 while (adapter_list_i
!= NULL
) {
299 device_list_i
= adapter_list_i
->device_list
;
300 while (device_list_i
!= NULL
) {
301 pa_assert_se(m
= dbus_message_new_method_call("org.bluez", device_list_i
->object_path
, "org.bluez.Device", "GetProperties"));
302 r
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u
->conn
), m
, -1, &e
);
304 pa_log("org.bluez.Device.GetProperties failed: %s", e
.message
);
307 if (!dbus_message_iter_init(r
, &arg_i
)) {
308 pa_log("org.bluez.Device.GetProperties reply has no arguments");
311 if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_ARRAY
) {
312 pa_log("org.bluez.Device.GetProperties argument is not an array");
315 dbus_message_iter_recurse(&arg_i
, &element_i
);
316 while (dbus_message_iter_get_arg_type(&element_i
) != DBUS_TYPE_INVALID
) {
317 if (dbus_message_iter_get_arg_type(&element_i
) == DBUS_TYPE_DICT_ENTRY
) {
318 dbus_message_iter_recurse(&element_i
, &dict_i
);
319 dbus_message_iter_get_basic(&dict_i
, &key
);
320 dbus_message_iter_next(&dict_i
);
321 dbus_message_iter_recurse(&dict_i
, &variant_i
);
322 if (strcmp(key
, "Name") == 0) {
323 dbus_message_iter_get_basic(&variant_i
, &value
);
324 device_list_i
->name
= pa_xstrdup(value
);
326 else if (strcmp(key
, "Paired") == 0) {
327 dbus_message_iter_get_basic(&variant_i
, &uvalue
);
328 device_list_i
->paired
= uvalue
;
330 else if (strcmp(key
, "Alias") == 0) {
331 dbus_message_iter_get_basic(&variant_i
, &value
);
332 device_list_i
->alias
= pa_xstrdup(value
);
334 else if (strcmp(key
, "Connected") == 0) {
335 dbus_message_iter_get_basic(&variant_i
, &uvalue
);
336 device_list_i
->connected
= uvalue
;
338 else if (strcmp(key
, "UUIDs") == 0) {
339 DBusMessageIter uuid_i
;
340 pa_bool_t is_audio_device
= FALSE
;
341 dbus_message_iter_recurse(&variant_i
, &uuid_i
);
342 while (dbus_message_iter_get_arg_type(&uuid_i
) != DBUS_TYPE_INVALID
) {
344 dbus_message_iter_get_basic(&uuid_i
, &value
);
345 node
= uuid_new(value
);
346 PA_LLIST_PREPEND(struct uuid
, device_list_i
->uuid_list
, node
);
347 if ( (strcasecmp(value
, HSP_HS_UUID
) == 0) || (strcasecmp(value
, HFP_HS_UUID
) == 0) ||
348 (strcasecmp(value
, A2DP_SOURCE_UUID
) == 0) || (strcasecmp(value
, A2DP_SINK_UUID
) == 0) )
349 is_audio_device
= TRUE
;
350 dbus_message_iter_next(&uuid_i
);
352 if (!is_audio_device
) {
353 /* remove the current device from the list */
354 PA_LLIST_REMOVE(struct device
, adapter_list_i
->device_list
, device_list_i
);
355 pa_xfree(device_list_i
);
359 else if (strcmp(key
, "Address") == 0) {
360 dbus_message_iter_get_basic(&variant_i
, &value
);
361 device_list_i
->address
= pa_xstrdup(value
);
363 else if (strcmp(key
, "Class") == 0) {
364 dbus_message_iter_get_basic(&variant_i
, &uvalue
);
365 device_list_i
->class = uvalue
;
367 else if (strcmp(key
, "Trusted") == 0) {
368 dbus_message_iter_get_basic(&variant_i
, &uvalue
);
369 device_list_i
->trusted
= uvalue
;
372 dbus_message_iter_next(&element_i
);
374 device_list_i
= device_list_i
->next
;
376 adapter_list_i
= adapter_list_i
->next
;
381 dbus_message_unref(m
);
383 dbus_message_unref(r
);
387 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*msg
, void *userdata
) {
388 DBusMessageIter arg_i
;
397 dbus_error_init(&err
);
399 pa_log("dbus: interface=%s, path=%s, member=%s\n",
400 dbus_message_get_interface(msg
),
401 dbus_message_get_path(msg
),
402 dbus_message_get_member(msg
));
404 if (dbus_message_is_signal(msg
, "org.bluez.Manager", "AdapterAdded")) {
405 if (!dbus_message_iter_init(msg
, &arg_i
))
406 pa_log("dbus: message has no parameters");
407 else if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_OBJECT_PATH
)
408 pa_log("dbus: argument is not object path");
410 dbus_message_iter_get_basic(&arg_i
, &value
);
411 pa_log("hcid: adapter %s added", value
);
414 else if (dbus_message_is_signal(msg
, "org.bluez.Manager", "AdapterRemoved")) {
415 if (!dbus_message_iter_init(msg
, &arg_i
))
416 pa_log("dbus: message has no parameters");
417 else if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_OBJECT_PATH
)
418 pa_log("dbus: argument is not object path");
420 dbus_message_iter_get_basic(&arg_i
, &value
);
421 pa_log("hcid: adapter %s removed", value
);
424 else if (dbus_message_is_signal(msg
, "org.bluez.Adapter", "DeviceCreated")) {
425 if (!dbus_message_iter_init(msg
, &arg_i
))
426 pa_log("dbus: message has no parameters");
427 else if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_OBJECT_PATH
)
428 pa_log("dbus: argument is not object path");
430 dbus_message_iter_get_basic(&arg_i
, &value
);
431 pa_log("hcid: device %s created", value
);
434 else if (dbus_message_is_signal(msg
, "org.bluez.Adapter", "DeviceRemoved")) {
435 if (!dbus_message_iter_init(msg
, &arg_i
))
436 pa_log("dbus: message has no parameters");
437 else if (dbus_message_iter_get_arg_type(&arg_i
) != DBUS_TYPE_OBJECT_PATH
)
438 pa_log("dbus: argument is not object path");
440 dbus_message_iter_get_basic(&arg_i
, &value
);
441 pa_log("hcid: device %s removed", value
);
445 dbus_error_free(&err
);
446 return DBUS_HANDLER_RESULT_HANDLED
;
449 void pa__done(pa_module
* m
) {
451 struct adapter
*adapter_list_i
;
455 if (!(u
= m
->userdata
))
458 while (u
->adapter_list
) {
459 adapter_list_i
= u
->adapter_list
;
460 PA_LLIST_REMOVE(struct adapter
, u
->adapter_list
, adapter_list_i
);
461 adapter_free(adapter_list_i
);
464 pa_dbus_connection_unref(u
->conn
);
466 pa_log("Unloading module-bt-discover");
470 int pa__init(pa_module
* m
) {
471 pa_modargs
*ma
= NULL
;
473 struct adapter
*adapter_list_i
;
474 struct device
*device_list_i
;
478 pa_log("Loading module-bt-discover");
479 dbus_error_init(&err
);
480 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
482 PA_LLIST_HEAD_INIT(struct adapter
, u
->adapter_list
);
484 /* connect to the bus */
485 u
->conn
= pa_dbus_bus_get(m
->core
, DBUS_BUS_SYSTEM
, &err
);
486 if ( dbus_error_is_set(&err
) || (u
->conn
== NULL
) ) {
487 pa_log("Failed to get D-Bus connection: %s", err
.message
);
491 /* static detection of bluetooth audio devices */
495 print_adapters(u
->adapter_list
);
497 /* load device modules */
498 adapter_list_i
= u
->adapter_list
;
499 while (adapter_list_i
!= NULL
) {
500 device_list_i
= adapter_list_i
->device_list
;
501 while (device_list_i
!= NULL
) {
502 pa_log("Loading module-bt-device for %s", device_list_i
->name
);
503 /* TODO: call module */
504 device_list_i
= device_list_i
->next
;
506 adapter_list_i
= adapter_list_i
->next
;
509 /* dynamic detection of bluetooth audio devices */
510 if (!dbus_connection_add_filter(pa_dbus_connection_get(u
->conn
), filter_cb
, u
, NULL
)) {
511 pa_log_error("Failed to add filter function");
514 dbus_connection_flush(pa_dbus_connection_get(u
->conn
));
515 dbus_bus_add_match(pa_dbus_connection_get(u
->conn
), "type='signal',interface='org.bluez.Manager'", &err
);
516 dbus_connection_flush(pa_dbus_connection_get(u
->conn
));
517 if (dbus_error_is_set(&err
)) {
518 pa_log_error("Unable to subscribe to org.bluez.Manager signals: %s: %s", err
.name
, err
.message
);
521 dbus_bus_add_match(pa_dbus_connection_get(u
->conn
), "type='signal',interface='org.bluez.Adapter'", &err
);
522 dbus_connection_flush(pa_dbus_connection_get(u
->conn
));
523 if (dbus_error_is_set(&err
)) {
524 pa_log_error("Unable to subscribe to org.bluez.Adapter signals: %s: %s", err
.name
, err
.message
);
533 dbus_error_free(&err
);