Changing all private functions to static
[pulseaudio-mirror.git] / src / modules / module-bt-discover.c
blobf1dcd2ec97e04a1dc73414e4175d45ac7ba52ee5
1 /***
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
19 USA.
20 ***/
22 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include <pulse/xmalloc.h>
29 #include <pulsecore/module.h>
30 #include <pulsecore/modargs.h>
32 #include "dbus-util.h"
33 #include "module-bt-discover-symdef.h"
35 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
36 PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
37 PA_MODULE_VERSION(PACKAGE_VERSION);
38 PA_MODULE_USAGE("");
40 #define HSP_HS_UUID "00001108-0000-1000-8000-00805F9B34FB"
41 #define HFP_HS_UUID "0000111E-0000-1000-8000-00805F9B34FB"
42 #define A2DP_SOURCE_UUID "0000110A-0000-1000-8000-00805F9B34FB"
43 #define A2DP_SINK_UUID "0000110B-0000-1000-8000-00805F9B34FB"
45 typedef struct adapter adapter_t;
46 typedef struct device device_t;
47 typedef struct uuid uuid_t;
49 struct uuid {
50 char *uuid;
51 uuid_t *next;
54 struct device {
55 char *name;
56 char *object_path;
57 int paired;
58 adapter_t *adapter;
59 char *alias;
60 int connected;
61 uuid_t *uuid_list;
62 char *address;
63 int class;
64 int trusted;
65 device_t *next;
68 struct adapter {
69 char *object_path;
70 char *mode;
71 char *address;
72 device_t *device_list;
73 adapter_t *next;
76 struct userdata {
77 pa_module *module;
78 pa_dbus_connection *conn;
79 adapter_t *adapter_list;
82 static uuid_t *uuid_new(const char *uuid) {
83 uuid_t *node = pa_xnew0(uuid_t, 1);
84 node->uuid = pa_xstrdup(uuid);
85 node->next = NULL;
86 return node;
89 static void uuid_list_append(uuid_t *node, const char *uuid) {
90 while (node->next != NULL) node = node->next;
91 node->next = uuid_new(uuid);
94 static device_t *device_new(const char *device, adapter_t *adapter) {
95 device_t *node = pa_xnew0(device_t, 1);
96 node->name = NULL;
97 node->object_path = pa_xstrdup(device);
98 node->paired = -1;
99 node->adapter = adapter;
100 node->alias = NULL;
101 node->connected = -1;
102 node->uuid_list = uuid_new("UUID_HEAD");
103 node->address = NULL;
104 node->class = -1;
105 node->trusted = -1;
106 node->next = NULL;
107 return node;
110 static void device_list_append(device_t *node, const char *device, adapter_t *adapter) {
111 while (node->next != NULL) node = node->next;
112 node->next = device_new(device, adapter);
115 static adapter_t *adapter_new(const char *adapter) {
116 adapter_t *node = pa_xnew0(adapter_t, 1);
117 node->object_path = pa_xstrdup(adapter);
118 node->mode = NULL;
119 node->address = NULL;
120 node->device_list = device_new("/DEVICE_HEAD", NULL);
121 node->next = NULL;
122 return node;
125 static void adapter_list_append(adapter_t *node, const char *adapter) {
126 while (node->next != NULL) node = node->next;
127 node->next = adapter_new(adapter);
130 static void print_devices(device_t *device_list) {
131 device_t *device_list_i = device_list;
132 while (device_list_i != NULL) {
133 uuid_t *uuid_list_i = device_list_i->uuid_list;
134 if (strcmp(device_list_i->object_path, "/DEVICE_HEAD") != 0) {
135 pa_log(" [ %s ]", device_list_i->object_path);
136 pa_log(" Name = %s", device_list_i->name);
137 pa_log(" Paired = %d", device_list_i->paired);
138 pa_log(" Adapter = %s", device_list_i->adapter->object_path);
139 pa_log(" Alias = %s", device_list_i->alias);
140 pa_log(" Connected = %d", device_list_i->connected);
141 pa_log(" UUIDs = ");
142 while (uuid_list_i != NULL) {
143 if (strcmp(uuid_list_i->uuid, "UUID_HEAD") != 0) {
144 pa_log(" %s", uuid_list_i->uuid);
146 uuid_list_i = uuid_list_i->next;
148 pa_log(" Address = %s", device_list_i->address);
149 pa_log(" Class = 0x%x", device_list_i->class);
150 pa_log(" Trusted = %d", device_list_i->trusted);
152 device_list_i = device_list_i->next;
156 static void print_adapters(adapter_t *adapter_list) {
157 adapter_t *adapter_list_i = adapter_list;
158 while (adapter_list_i != NULL) {
159 if (strcmp(adapter_list_i->object_path, "/ADAPTER_HEAD") != 0) {
160 pa_log("[ %s ]", adapter_list_i->object_path);
161 pa_log(" Mode = %s", adapter_list_i->mode);
162 pa_log(" Address = %s", adapter_list_i->address);
163 print_devices(adapter_list_i->device_list);
165 adapter_list_i = adapter_list_i->next;
169 static DBusMessageIter call_dbus_method(pa_dbus_connection *conn, const char *destination, const char *path, const char *interface,
170 const char *method) {
171 DBusMessage *msg;
172 DBusPendingCall *pending;
173 DBusMessageIter args;
175 /* construct the DBusMessage */
176 msg = dbus_message_new_method_call(destination, path, interface, method);
178 /* send the message and get a handle for a reply */
179 if (!dbus_connection_send_with_reply(pa_dbus_connection_get(conn), msg, &pending, -1)) {
180 pa_log("Out Of Memory!");
182 if (pending == NULL) {
183 pa_log("Pending Call Null");
185 dbus_connection_flush(pa_dbus_connection_get(conn));
187 /* free msg */
188 dbus_message_unref(msg);
190 /* wait for reply */
191 dbus_pending_call_block(pending);
193 /* get the reply */
194 msg = dbus_pending_call_steal_reply(pending);
195 if (msg == NULL) {
196 pa_log("Reply Null");
199 /* free pending */
200 dbus_pending_call_unref(pending);
202 /* read the reply */
203 if (!dbus_message_iter_init(msg, &args))
204 pa_log("Reply has no arguments");
206 dbus_message_unref(msg);
208 return args;
212 static void detect_adapters(adapter_t *adapter_list, pa_dbus_connection *conn) {
213 DBusMessageIter arg_i, element_i, dict_i, variant_i;
214 adapter_t *adapter_list_i;
215 const char *key, *value;
217 /* get adapters */
218 arg_i = call_dbus_method(conn, "org.bluez", "/", "org.bluez.Manager", "ListAdapters");
219 dbus_message_iter_recurse(&arg_i, &element_i);
220 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
221 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_OBJECT_PATH) {
222 dbus_message_iter_get_basic(&element_i, &value);
223 adapter_list_append(adapter_list, value);
225 dbus_message_iter_next(&element_i);
228 /* get adapter properties */
229 adapter_list_i = adapter_list->next;
230 while (adapter_list_i != NULL) {
231 arg_i = call_dbus_method(conn, "org.bluez", adapter_list_i->object_path, "org.bluez.Adapter", "GetProperties");
232 dbus_message_iter_recurse(&arg_i, &element_i);
233 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
234 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
235 dbus_message_iter_recurse(&element_i, &dict_i);
236 dbus_message_iter_get_basic(&dict_i, &key);
237 dbus_message_iter_next(&dict_i);
238 dbus_message_iter_recurse(&dict_i, &variant_i);
239 dbus_message_iter_get_basic(&variant_i, &value);
240 if (strcmp(key, "Mode") == 0) {
241 adapter_list_i->mode = pa_xstrdup(value);
243 else if (strcmp(key, "Address") == 0) {
244 adapter_list_i->address = pa_xstrdup(value);
247 dbus_message_iter_next(&element_i);
249 adapter_list_i = adapter_list_i->next;
253 static void detect_devices(adapter_t *adapter_list, pa_dbus_connection *conn) {
254 DBusMessageIter arg_i, element_i, dict_i, variant_i;
255 adapter_t *adapter_list_i;
256 device_t *device_list_i, *device_list_prev_i;
257 const char *key, *value;
258 unsigned int uvalue;
260 /* get devices of each adapter */
261 adapter_list_i = adapter_list->next;
262 while (adapter_list_i != NULL) {
263 arg_i = call_dbus_method(conn, "org.bluez", adapter_list_i->object_path , "org.bluez.Adapter", "ListDevices");
264 dbus_message_iter_recurse(&arg_i, &element_i);
265 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
266 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_OBJECT_PATH) {
267 dbus_message_iter_get_basic(&element_i, &value);
268 device_list_append(adapter_list_i->device_list, value, adapter_list_i);
270 dbus_message_iter_next(&element_i);
272 adapter_list_i = adapter_list_i->next;
275 /* get device properties */
276 adapter_list_i = adapter_list->next;
277 while (adapter_list_i != NULL) {
278 device_list_prev_i = adapter_list_i->device_list;
279 device_list_i = adapter_list_i->device_list->next;
280 while (device_list_i != NULL) {
281 arg_i = call_dbus_method(conn, "org.bluez", device_list_i->object_path, "org.bluez.Device", "GetProperties");
282 dbus_message_iter_recurse(&arg_i, &element_i);
283 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
284 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
285 dbus_message_iter_recurse(&element_i, &dict_i);
286 dbus_message_iter_get_basic(&dict_i, &key);
287 dbus_message_iter_next(&dict_i);
288 dbus_message_iter_recurse(&dict_i, &variant_i);
289 if (strcmp(key, "Name") == 0) {
290 dbus_message_iter_get_basic(&variant_i, &value);
291 device_list_i->name = pa_xstrdup(value);
293 else if (strcmp(key, "Paired") == 0) {
294 dbus_message_iter_get_basic(&variant_i, &uvalue);
295 device_list_i->paired = uvalue;
297 else if (strcmp(key, "Alias") == 0) {
298 dbus_message_iter_get_basic(&variant_i, &value);
299 device_list_i->alias = pa_xstrdup(value);
301 else if (strcmp(key, "Connected") == 0) {
302 dbus_message_iter_get_basic(&variant_i, &uvalue);
303 device_list_i->connected = uvalue;
305 else if (strcmp(key, "UUIDs") == 0) {
306 DBusMessageIter uuid_i;
307 int is_audio_device = 0;
308 dbus_message_iter_recurse(&variant_i, &uuid_i);
309 while (dbus_message_iter_get_arg_type(&uuid_i) != DBUS_TYPE_INVALID) {
310 dbus_message_iter_get_basic(&uuid_i, &value);
311 if ( (strcasecmp(value, HSP_HS_UUID) == 0) || (strcasecmp(value, HFP_HS_UUID) == 0) ||
312 (strcasecmp(value, A2DP_SOURCE_UUID) == 0) || (strcasecmp(value, A2DP_SINK_UUID) == 0) )
313 is_audio_device = 1;
314 uuid_list_append(device_list_i->uuid_list, value);
315 dbus_message_iter_next(&uuid_i);
317 if (!is_audio_device) {
318 /* remove current device */
319 device_list_prev_i->next = device_list_i->next;
320 pa_xfree(device_list_i);
321 break;
324 else if (strcmp(key, "Address") == 0) {
325 dbus_message_iter_get_basic(&variant_i, &value);
326 device_list_i->address = pa_xstrdup(value);
328 else if (strcmp(key, "Class") == 0) {
329 dbus_message_iter_get_basic(&variant_i, &uvalue);
330 device_list_i->class = uvalue;
332 else if (strcmp(key, "Trusted") == 0) {
333 dbus_message_iter_get_basic(&variant_i, &uvalue);
334 device_list_i->trusted = uvalue;
337 dbus_message_iter_next(&element_i);
339 device_list_prev_i = device_list_prev_i->next;
340 if (device_list_prev_i == NULL)
341 device_list_i = NULL;
342 else
343 device_list_i = device_list_prev_i->next;
345 adapter_list_i = adapter_list_i->next;
349 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void *userdata) {
350 DBusMessageIter arg_i;
351 DBusError err;
352 const char *value;
353 struct userdata *u;
355 pa_assert(bus);
356 pa_assert(msg);
357 pa_assert(userdata);
358 u = userdata;
359 dbus_error_init(&err);
361 pa_log("dbus: interface=%s, path=%s, member=%s\n",
362 dbus_message_get_interface(msg),
363 dbus_message_get_path(msg),
364 dbus_message_get_member(msg));
366 if (dbus_message_is_signal(msg, "org.bluez.Manager", "AdapterAdded")) {
367 if (!dbus_message_iter_init(msg, &arg_i))
368 pa_log("dbus: message has no parameters");
369 else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH)
370 pa_log("dbus: argument is not object path");
371 else {
372 dbus_message_iter_get_basic(&arg_i, &value);
373 pa_log("hcid: adapter %s added", value);
376 else if (dbus_message_is_signal(msg, "org.bluez.Manager", "AdapterRemoved")) {
377 if (!dbus_message_iter_init(msg, &arg_i))
378 pa_log("dbus: message has no parameters");
379 else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH)
380 pa_log("dbus: argument is not object path");
381 else {
382 dbus_message_iter_get_basic(&arg_i, &value);
383 pa_log("hcid: adapter %s removed", value);
386 else if (dbus_message_is_signal(msg, "org.bluez.Adapter", "DeviceCreated")) {
387 if (!dbus_message_iter_init(msg, &arg_i))
388 pa_log("dbus: message has no parameters");
389 else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH)
390 pa_log("dbus: argument is not object path");
391 else {
392 dbus_message_iter_get_basic(&arg_i, &value);
393 pa_log("hcid: device %s created", value);
396 else if (dbus_message_is_signal(msg, "org.bluez.Adapter", "DeviceRemoved")) {
397 if (!dbus_message_iter_init(msg, &arg_i))
398 pa_log("dbus: message has no parameters");
399 else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH)
400 pa_log("dbus: argument is not object path");
401 else {
402 dbus_message_iter_get_basic(&arg_i, &value);
403 pa_log("hcid: device %s removed", value);
407 finish:
408 dbus_error_free(&err);
409 return DBUS_HANDLER_RESULT_HANDLED;
412 void pa__done(pa_module* m) {
413 struct userdata *u;
414 adapter_t *adapter_list_i, *adapter_list_next_i;
415 device_t *device_list_i, *device_list_next_i;
417 pa_assert(m);
419 if (!(u = m->userdata))
420 return;
422 if ((adapter_list_i = u->adapter_list) != NULL) {
423 while ((adapter_list_next_i = adapter_list_i->next) != NULL) {
424 if ((device_list_i = adapter_list_i->device_list) != NULL) {
425 while ((device_list_next_i = device_list_i->next) != NULL) {
426 pa_xfree(device_list_i);
427 device_list_i = device_list_next_i;
429 pa_xfree(device_list_i);
431 pa_xfree(adapter_list_i);
432 adapter_list_i = adapter_list_next_i;
434 pa_xfree(adapter_list_i);
437 pa_dbus_connection_unref(u->conn);
438 pa_xfree(u);
439 pa_log("Unloading module-bt-discover");
440 return;
443 int pa__init(pa_module* m) {
444 pa_modargs *ma = NULL;
445 DBusError err;
446 DBusMessageIter arg_i, element_i;
447 adapter_t *adapter_list_i;
448 device_t *device_list_i;
449 const char *value;
450 unsigned int hcid_running = 0;
451 struct userdata *u;
453 pa_assert(m);
454 dbus_error_init(&err);
455 pa_log("Loading module-bt-discover");
456 m->userdata = u = pa_xnew0(struct userdata, 1);
457 u->module = m;
459 /* connect to the bus */
460 u->conn = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &err);
461 if ( dbus_error_is_set(&err) || (u->conn == NULL) ) {
462 pa_log("Failed to get D-Bus connection: %s", err.message);
463 goto fail;
466 /* check if hcid is running */
467 arg_i = call_dbus_method(u->conn, "org.freedesktop.DBus", "/org/freedesktop/DBus" , "org.freedesktop.DBus", "ListNames");
468 dbus_message_iter_recurse(&arg_i, &element_i);
469 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
470 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_STRING) {
471 dbus_message_iter_get_basic(&element_i, &value);
472 if (strcmp(value, "org.bluez") == 0)
473 hcid_running = 1;
475 dbus_message_iter_next(&element_i);
477 if (!hcid_running) {
478 pa_log("hcid not running");
479 goto fail;
482 /* static detection of bluetooth audio devices */
483 u->adapter_list = adapter_new("/ADAPTER_HEAD");
484 detect_adapters(u->adapter_list, u->conn);
485 detect_devices(u->adapter_list, u->conn);
487 print_adapters(u->adapter_list);
489 /* load device modules */
490 adapter_list_i = u->adapter_list->next;
491 while (adapter_list_i != NULL) {
492 device_list_i = adapter_list_i->device_list->next;
493 while (device_list_i != NULL) {
494 pa_log("Loading module-bt-device for %s", device_list_i->name); /* CHECK: Should it be name or alias? */
495 /* call module */
496 device_list_i = device_list_i->next;
498 adapter_list_i = adapter_list_i->next;
501 /* dynamic detection of bluetooth audio devices */
502 if (!dbus_connection_add_filter(pa_dbus_connection_get(u->conn), filter_cb, u, NULL)) {
503 pa_log_error("Failed to add filter function");
504 goto fail;
506 dbus_connection_flush(pa_dbus_connection_get(u->conn));
507 dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',interface='org.bluez.Manager'", &err);
508 dbus_connection_flush(pa_dbus_connection_get(u->conn));
509 if (dbus_error_is_set(&err)) {
510 pa_log_error("Unable to subscribe to org.bluez.Manager signals: %s: %s", err.name, err.message);
511 goto fail;
513 dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',interface='org.bluez.Adapter'", &err);
514 dbus_connection_flush(pa_dbus_connection_get(u->conn));
515 if (dbus_error_is_set(&err)) {
516 pa_log_error("Unable to subscribe to org.bluez.Adapter signals: %s: %s", err.name, err.message);
517 goto fail;
520 return 0;
522 fail:
523 if (ma)
524 pa_modargs_free(ma);
525 dbus_error_free(&err);
526 pa__done(m);
527 return -1;