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