Fix the symdef include
[pulseaudio-mirror.git] / src / modules / module-bt-discover.c
blob4f862f9c9403a7b79724a5405fab3bce2e92a1f6
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 #define VERBOSE 1
47 typedef struct adapter adapter_t;
48 typedef struct device device_t;
49 typedef struct uuid uuid_t;
51 struct uuid {
52 char *uuid;
53 uuid_t *next;
56 struct device {
57 char *name;
58 char *object_path;
59 int paired;
60 adapter_t *adapter;
61 char *alias;
62 int connected;
63 uuid_t *uuid_list;
64 char *address;
65 int class;
66 int trusted;
67 device_t *next;
70 struct adapter {
71 char *object_path;
72 char *mode;
73 char *address;
74 device_t *device_list;
75 adapter_t *next;
78 struct userdata {
79 pa_module *module;
80 pa_dbus_connection *conn;
81 adapter_t *adapter_list;
84 uuid_t *uuid_new(const char *uuid) {
85 uuid_t *node = pa_xnew0(uuid_t, 1);
86 node->uuid = pa_xstrdup(uuid);
87 node->next = NULL;
88 return node;
91 void uuid_list_append(uuid_t *node, const char *uuid) {
92 while (node->next != NULL) node = node->next;
93 node->next = uuid_new(uuid);
96 device_t *device_new(const char *device, adapter_t *adapter) {
97 device_t *node = pa_xnew0(device_t, 1);
98 node->name = NULL;
99 node->object_path = pa_xstrdup(device);
100 node->paired = -1;
101 node->adapter = adapter;
102 node->alias = NULL;
103 node->connected = -1;
104 node->uuid_list = uuid_new("UUID_HEAD");
105 node->address = NULL;
106 node->class = -1;
107 node->trusted = -1;
108 node->next = NULL;
109 return node;
112 void device_list_append(device_t *node, const char *device, adapter_t *adapter) {
113 while (node->next != NULL) node = node->next;
114 node->next = device_new(device, adapter);
117 adapter_t *adapter_new(const char *adapter) {
118 adapter_t *node = pa_xnew0(adapter_t, 1);
119 node->object_path = pa_xstrdup(adapter);
120 node->mode = NULL;
121 node->address = NULL;
122 node->device_list = device_new("/DEVICE_HEAD", NULL);
123 node->next = NULL;
124 return node;
127 void adapter_list_append(adapter_t *node, const char *adapter) {
128 while (node->next != NULL) node = node->next;
129 node->next = adapter_new(adapter);
132 void print_devices(device_t *device_list) {
133 device_t *device_list_i = device_list;
134 while (device_list_i != NULL) {
135 uuid_t *uuid_list_i = device_list_i->uuid_list;
136 if (strcmp(device_list_i->object_path, "/DEVICE_HEAD") != 0) {
137 pa_log(" [ %s ]", device_list_i->object_path);
138 pa_log(" Name = %s", device_list_i->name);
139 pa_log(" Paired = %d", device_list_i->paired);
140 pa_log(" Adapter = %s", device_list_i->adapter->object_path);
141 pa_log(" Alias = %s", device_list_i->alias);
142 pa_log(" Connected = %d", device_list_i->connected);
143 pa_log(" UUIDs = ");
144 while (uuid_list_i != NULL) {
145 if (strcmp(uuid_list_i->uuid, "UUID_HEAD") != 0) {
146 pa_log(" %s", uuid_list_i->uuid);
148 uuid_list_i = uuid_list_i->next;
150 pa_log(" Address = %s", device_list_i->address);
151 pa_log(" Class = 0x%x", device_list_i->class);
152 pa_log(" Trusted = %d", device_list_i->trusted);
154 device_list_i = device_list_i->next;
158 void print_adapters(adapter_t *adapter_list) {
159 adapter_t *adapter_list_i = adapter_list;
160 while (adapter_list_i != NULL) {
161 if (strcmp(adapter_list_i->object_path, "/ADAPTER_HEAD") != 0) {
162 pa_log("[ %s ]", adapter_list_i->object_path);
163 pa_log(" Mode = %s", adapter_list_i->mode);
164 pa_log(" Address = %s", adapter_list_i->address);
165 print_devices(adapter_list_i->device_list);
167 adapter_list_i = adapter_list_i->next;
171 DBusMessageIter call_dbus_method(pa_dbus_connection *conn, const char *destination, const char *path, const char *interface,
172 const char *method) {
173 DBusMessage *msg;
174 DBusPendingCall *pending;
175 DBusMessageIter args;
177 /* construct the DBusMessage */
178 msg = dbus_message_new_method_call(destination, path, interface, method);
180 /* send the message and get a handle for a reply */
181 if (!dbus_connection_send_with_reply(pa_dbus_connection_get(conn), msg, &pending, -1)) {
182 pa_log("Out Of Memory!");
184 if (pending == NULL) {
185 pa_log("Pending Call Null");
187 dbus_connection_flush(pa_dbus_connection_get(conn));
189 /* free msg */
190 dbus_message_unref(msg);
192 /* wait for reply */
193 dbus_pending_call_block(pending);
195 /* get the reply */
196 msg = dbus_pending_call_steal_reply(pending);
197 if (msg == NULL) {
198 pa_log("Reply Null");
201 /* free pending */
202 dbus_pending_call_unref(pending);
204 /* read the reply */
205 if (!dbus_message_iter_init(msg, &args))
206 pa_log("Reply has no arguments");
208 dbus_message_unref(msg);
210 return args;
214 void detect_adapters(adapter_t *adapter_list, pa_dbus_connection *conn) {
215 DBusMessageIter arg_i, element_i, dict_i, variant_i;
216 adapter_t *adapter_list_i;
217 const char *key, *value;
219 /* get adapters */
220 arg_i = call_dbus_method(conn, "org.bluez", "/", "org.bluez.Manager", "ListAdapters");
221 dbus_message_iter_recurse(&arg_i, &element_i);
222 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
223 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_OBJECT_PATH) {
224 dbus_message_iter_get_basic(&element_i, &value);
225 adapter_list_append(adapter_list, value);
227 dbus_message_iter_next(&element_i);
230 /* get adapter properties */
231 adapter_list_i = adapter_list->next;
232 while (adapter_list_i != NULL) {
233 arg_i = call_dbus_method(conn, "org.bluez", adapter_list_i->object_path, "org.bluez.Adapter", "GetProperties");
234 dbus_message_iter_recurse(&arg_i, &element_i);
235 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
236 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
237 dbus_message_iter_recurse(&element_i, &dict_i);
238 dbus_message_iter_get_basic(&dict_i, &key);
239 dbus_message_iter_next(&dict_i);
240 dbus_message_iter_recurse(&dict_i, &variant_i);
241 dbus_message_iter_get_basic(&variant_i, &value);
242 if (strcmp(key, "Mode") == 0) {
243 adapter_list_i->mode = pa_xstrdup(value);
245 else if (strcmp(key, "Address") == 0) {
246 adapter_list_i->address = pa_xstrdup(value);
249 dbus_message_iter_next(&element_i);
251 adapter_list_i = adapter_list_i->next;
255 void detect_devices(adapter_t *adapter_list, pa_dbus_connection *conn) {
256 DBusMessageIter arg_i, element_i, dict_i, variant_i;
257 adapter_t *adapter_list_i;
258 device_t *device_list_i, *device_list_prev_i;
259 const char *key, *value;
260 unsigned int uvalue;
262 /* get devices of each adapter */
263 adapter_list_i = adapter_list->next;
264 while (adapter_list_i != NULL) {
265 arg_i = call_dbus_method(conn, "org.bluez", adapter_list_i->object_path , "org.bluez.Adapter", "ListDevices");
266 dbus_message_iter_recurse(&arg_i, &element_i);
267 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
268 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_OBJECT_PATH) {
269 dbus_message_iter_get_basic(&element_i, &value);
270 device_list_append(adapter_list_i->device_list, value, adapter_list_i);
272 dbus_message_iter_next(&element_i);
274 adapter_list_i = adapter_list_i->next;
277 /* get device properties */
278 adapter_list_i = adapter_list->next;
279 while (adapter_list_i != NULL) {
280 device_list_prev_i = adapter_list_i->device_list;
281 device_list_i = adapter_list_i->device_list->next;
282 while (device_list_i != NULL) {
283 arg_i = call_dbus_method(conn, "org.bluez", device_list_i->object_path, "org.bluez.Device", "GetProperties");
284 dbus_message_iter_recurse(&arg_i, &element_i);
285 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
286 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
287 dbus_message_iter_recurse(&element_i, &dict_i);
288 dbus_message_iter_get_basic(&dict_i, &key);
289 dbus_message_iter_next(&dict_i);
290 dbus_message_iter_recurse(&dict_i, &variant_i);
291 if (strcmp(key, "Name") == 0) {
292 dbus_message_iter_get_basic(&variant_i, &value);
293 device_list_i->name = pa_xstrdup(value);
295 else if (strcmp(key, "Paired") == 0) {
296 dbus_message_iter_get_basic(&variant_i, &uvalue);
297 device_list_i->paired = uvalue;
299 else if (strcmp(key, "Alias") == 0) {
300 dbus_message_iter_get_basic(&variant_i, &value);
301 device_list_i->alias = pa_xstrdup(value);
303 else if (strcmp(key, "Connected") == 0) {
304 dbus_message_iter_get_basic(&variant_i, &uvalue);
305 device_list_i->connected = uvalue;
307 else if (strcmp(key, "UUIDs") == 0) {
308 DBusMessageIter uuid_i;
309 int is_audio_device = 0;
310 dbus_message_iter_recurse(&variant_i, &uuid_i);
311 while (dbus_message_iter_get_arg_type(&uuid_i) != DBUS_TYPE_INVALID) {
312 dbus_message_iter_get_basic(&uuid_i, &value);
313 if ( (strcasecmp(value, HSP_HS_UUID) == 0) || (strcasecmp(value, HFP_HS_UUID) == 0) ||
314 (strcasecmp(value, A2DP_SOURCE_UUID) == 0) || (strcasecmp(value, A2DP_SINK_UUID) == 0) )
315 is_audio_device = 1;
316 uuid_list_append(device_list_i->uuid_list, value);
317 dbus_message_iter_next(&uuid_i);
319 if (!is_audio_device) {
320 /* remove current device */
321 device_list_prev_i->next = device_list_i->next;
322 pa_xfree(device_list_i);
323 break;
326 else if (strcmp(key, "Address") == 0) {
327 dbus_message_iter_get_basic(&variant_i, &value);
328 device_list_i->address = pa_xstrdup(value);
330 else if (strcmp(key, "Class") == 0) {
331 dbus_message_iter_get_basic(&variant_i, &uvalue);
332 device_list_i->class = uvalue;
334 else if (strcmp(key, "Trusted") == 0) {
335 dbus_message_iter_get_basic(&variant_i, &uvalue);
336 device_list_i->trusted = uvalue;
339 dbus_message_iter_next(&element_i);
341 device_list_prev_i = device_list_prev_i->next;
342 if (device_list_prev_i == NULL)
343 device_list_i = NULL;
344 else
345 device_list_i = device_list_prev_i->next;
347 adapter_list_i = adapter_list_i->next;
351 void pa__done(pa_module* m) {
352 struct userdata *u;
353 adapter_t *adapter_list_i, *adapter_list_next_i;
354 device_t *device_list_i, *device_list_next_i;
356 pa_assert(m);
358 if (!(u = m->userdata))
359 return;
361 if ((adapter_list_i = u->adapter_list) != NULL) {
362 while ((adapter_list_next_i = adapter_list_i->next) != NULL) {
363 if ((device_list_i = adapter_list_i->device_list) != NULL) {
364 while ((device_list_next_i = device_list_i->next) != NULL) {
365 pa_xfree(device_list_i);
366 device_list_i = device_list_next_i;
368 pa_xfree(device_list_i);
370 pa_xfree(adapter_list_i);
371 adapter_list_i = adapter_list_next_i;
373 pa_xfree(adapter_list_i);
376 pa_dbus_connection_unref(u->conn);
377 pa_xfree(u);
378 pa_log("Unloading module-bt-discover");
379 return;
382 int pa__init(pa_module* m) {
383 pa_modargs *ma = NULL;
384 DBusError err;
385 DBusMessageIter arg_i, element_i;
386 adapter_t *adapter_list_i;
387 device_t *device_list_i;
388 const char *value;
389 unsigned int hcid_running = 0;
390 struct userdata *u;
392 pa_assert(m);
393 dbus_error_init(&err);
394 pa_log("Loading module-bt-discover");
395 m->userdata = u = pa_xnew0(struct userdata, 1);
396 u->module = m;
398 /* connect to the bus */
399 u->conn = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &err);
400 if ( dbus_error_is_set(&err) || (u->conn == NULL) ) {
401 pa_log("Failed to get D-Bus connection: %s", err.message);
402 goto fail;
405 /* check if hcid is running */
406 arg_i = call_dbus_method(u->conn, "org.freedesktop.DBus", "/org/freedesktop/DBus" , "org.freedesktop.DBus", "ListNames");
407 dbus_message_iter_recurse(&arg_i, &element_i);
408 while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
409 if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_STRING) {
410 dbus_message_iter_get_basic(&element_i, &value);
411 if (strcmp(value, "org.bluez") == 0)
412 hcid_running = 1;
414 dbus_message_iter_next(&element_i);
416 if (!hcid_running) {
417 pa_log("hcid not running");
418 goto fail;
421 /* static detection of bluetooth devices */
422 u->adapter_list = adapter_new("/ADAPTER_HEAD");
423 detect_adapters(u->adapter_list, u->conn);
424 detect_devices(u->adapter_list, u->conn);
426 if (VERBOSE) print_adapters(u->adapter_list);
428 /* load device modules */
429 adapter_list_i = u->adapter_list->next;
430 while (adapter_list_i != NULL) {
431 device_list_i = adapter_list_i->device_list->next;
432 while (device_list_i != NULL) {
433 pa_log("Loading module-bt-device for %s", device_list_i->name); /* CHECK: Should it be name or alias? */
434 /* call module */
435 device_list_i = device_list_i->next;
437 adapter_list_i = adapter_list_i->next;
440 return 0;
442 fail:
443 if (ma)
444 pa_modargs_free(ma);
445 dbus_error_free(&err);
446 pa__done(m);
447 return -1;