summaryrefslogtreecommitdiffstats
path: root/src/modules/module-bt-discover.c
blob: f1dcd2ec97e04a1dc73414e4175d45ac7ba52ee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/***
    This file is part of PulseAudio.

    Copyright 2008 Joao Paulo Rechi Vita

    PulseAudio is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2 of the License,
    or (at your option) any later version.

    PulseAudio is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with PulseAudio; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
    USA.
***/

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pulse/xmalloc.h>
#include <pulsecore/module.h>
#include <pulsecore/modargs.h>

#include "dbus-util.h"
#include "module-bt-discover-symdef.h"

PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
PA_MODULE_VERSION(PACKAGE_VERSION);
PA_MODULE_USAGE("");

#define HSP_HS_UUID             "00001108-0000-1000-8000-00805F9B34FB"
#define HFP_HS_UUID             "0000111E-0000-1000-8000-00805F9B34FB"
#define A2DP_SOURCE_UUID        "0000110A-0000-1000-8000-00805F9B34FB"
#define A2DP_SINK_UUID          "0000110B-0000-1000-8000-00805F9B34FB"

typedef struct adapter adapter_t;
typedef struct device device_t;
typedef struct uuid uuid_t;

struct uuid {
    char *uuid;
    uuid_t *next;
};

struct device {
    char *name;
    char *object_path;
    int paired;
    adapter_t *adapter;
    char *alias;
    int connected;
    uuid_t *uuid_list;
    char *address;
    int class;
    int trusted;
    device_t *next;
};

struct adapter {
    char *object_path;
    char *mode;
    char *address;
    device_t *device_list;
    adapter_t *next;
};

struct userdata {
    pa_module *module;
    pa_dbus_connection *conn;
    adapter_t *adapter_list;
};

static uuid_t *uuid_new(const char *uuid) {
    uuid_t *node = pa_xnew0(uuid_t, 1);
    node->uuid = pa_xstrdup(uuid);
    node->next = NULL;
    return node;
}

static void uuid_list_append(uuid_t *node, const char *uuid) {
    while (node->next != NULL) node = node->next;
    node->next = uuid_new(uuid);
}

static device_t *device_new(const char *device, adapter_t *adapter) {
    device_t *node = pa_xnew0(device_t, 1);
    node->name = NULL;
    node->object_path = pa_xstrdup(device);
    node->paired = -1;
    node->adapter = adapter;
    node->alias = NULL;
    node->connected = -1;
    node->uuid_list = uuid_new("UUID_HEAD");
    node->address = NULL;
    node->class = -1;
    node->trusted = -1;
    node->next = NULL;
    return node;
}

static void device_list_append(device_t *node, const char *device, adapter_t *adapter) {
    while (node->next != NULL) node = node->next;
    node->next = device_new(device, adapter);
}

static adapter_t *adapter_new(const char *adapter) {
    adapter_t *node = pa_xnew0(adapter_t, 1);
    node->object_path = pa_xstrdup(adapter);
    node->mode = NULL;
    node->address = NULL;
    node->device_list = device_new("/DEVICE_HEAD", NULL);
    node->next = NULL;
    return node;
}

static void adapter_list_append(adapter_t *node, const char *adapter) {
    while (node->next != NULL) node = node->next;
    node->next = adapter_new(adapter);
}

static void print_devices(device_t *device_list) {
    device_t *device_list_i = device_list;
    while (device_list_i != NULL) {
        uuid_t *uuid_list_i = device_list_i->uuid_list;
        if (strcmp(device_list_i->object_path, "/DEVICE_HEAD") != 0) {
            pa_log("    [ %s ]", device_list_i->object_path);
            pa_log("        Name = %s", device_list_i->name);
            pa_log("        Paired = %d", device_list_i->paired);
            pa_log("        Adapter = %s", device_list_i->adapter->object_path);
            pa_log("        Alias = %s", device_list_i->alias);
            pa_log("        Connected = %d", device_list_i->connected);
            pa_log("        UUIDs = ");
            while (uuid_list_i != NULL) {
                if (strcmp(uuid_list_i->uuid, "UUID_HEAD") != 0) {
                    pa_log("            %s", uuid_list_i->uuid);
                }
                uuid_list_i = uuid_list_i->next;
            }
            pa_log("        Address = %s", device_list_i->address);
            pa_log("        Class = 0x%x", device_list_i->class);
            pa_log("        Trusted = %d", device_list_i->trusted);
        }
        device_list_i = device_list_i->next;
    }
}

static void print_adapters(adapter_t *adapter_list) {
    adapter_t *adapter_list_i = adapter_list;
    while (adapter_list_i != NULL) {
        if (strcmp(adapter_list_i->object_path, "/ADAPTER_HEAD") != 0) {
            pa_log("[ %s ]", adapter_list_i->object_path);
            pa_log("    Mode = %s", adapter_list_i->mode);
            pa_log("    Address = %s", adapter_list_i->address);
            print_devices(adapter_list_i->device_list);
        }
        adapter_list_i = adapter_list_i->next;
    }
}

static DBusMessageIter call_dbus_method(pa_dbus_connection *conn, const char *destination, const char *path, const char *interface,
        const char *method) {
    DBusMessage *msg;
    DBusPendingCall *pending;
    DBusMessageIter args;

    /* construct the DBusMessage */
    msg = dbus_message_new_method_call(destination, path, interface, method);

    /* send the message and get a handle for a reply */
    if (!dbus_connection_send_with_reply(pa_dbus_connection_get(conn), msg, &pending, -1)) { 
        pa_log("Out Of Memory!"); 
    }
    if (pending == NULL) { 
        pa_log("Pending Call Null"); 
    }
    dbus_connection_flush(pa_dbus_connection_get(conn));

    /* free msg */
    dbus_message_unref(msg);

    /* wait for reply */
    dbus_pending_call_block(pending);

    /* get the reply */
    msg = dbus_pending_call_steal_reply(pending);
    if (msg == NULL) {
        pa_log("Reply Null");
    }

    /* free pending */
    dbus_pending_call_unref(pending);

    /* read the reply */
    if (!dbus_message_iter_init(msg, &args))
        pa_log("Reply has no arguments");

    dbus_message_unref(msg);

    return args;

}

static void detect_adapters(adapter_t *adapter_list, pa_dbus_connection *conn) {
    DBusMessageIter arg_i, element_i, dict_i, variant_i;
    adapter_t *adapter_list_i;
    const char *key, *value;

    /* get adapters */
    arg_i = call_dbus_method(conn, "org.bluez", "/", "org.bluez.Manager", "ListAdapters");
    dbus_message_iter_recurse(&arg_i, &element_i);
    while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
        if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_OBJECT_PATH) {
            dbus_message_iter_get_basic(&element_i, &value);
            adapter_list_append(adapter_list, value);
        }
        dbus_message_iter_next(&element_i);
    }

    /* get adapter properties */
    adapter_list_i = adapter_list->next;
    while (adapter_list_i != NULL) {
        arg_i = call_dbus_method(conn, "org.bluez", adapter_list_i->object_path, "org.bluez.Adapter", "GetProperties");
        dbus_message_iter_recurse(&arg_i, &element_i);
        while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
            if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
                dbus_message_iter_recurse(&element_i, &dict_i);
                dbus_message_iter_get_basic(&dict_i, &key);
                dbus_message_iter_next(&dict_i);
                dbus_message_iter_recurse(&dict_i, &variant_i);
                dbus_message_iter_get_basic(&variant_i, &value);
                if (strcmp(key, "Mode") == 0) {
                    adapter_list_i->mode = pa_xstrdup(value);
                }
                else if (strcmp(key, "Address") == 0) {
                    adapter_list_i->address = pa_xstrdup(value);
                }
            }
            dbus_message_iter_next(&element_i);
        }
        adapter_list_i = adapter_list_i->next;
    }
}

static void detect_devices(adapter_t *adapter_list, pa_dbus_connection *conn) {
    DBusMessageIter arg_i, element_i, dict_i, variant_i;
    adapter_t *adapter_list_i;
    device_t *device_list_i, *device_list_prev_i;
    const char *key, *value;
    unsigned int uvalue;

    /* get devices of each adapter */
    adapter_list_i = adapter_list->next;
    while (adapter_list_i != NULL) {
        arg_i = call_dbus_method(conn, "org.bluez", adapter_list_i->object_path , "org.bluez.Adapter", "ListDevices");
        dbus_message_iter_recurse(&arg_i, &element_i);
        while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
            if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_OBJECT_PATH) {
                dbus_message_iter_get_basic(&element_i, &value);
                device_list_append(adapter_list_i->device_list, value, adapter_list_i);
            }
            dbus_message_iter_next(&element_i);
        }
        adapter_list_i = adapter_list_i->next;
    }

    /* get device properties */
    adapter_list_i = adapter_list->next;
    while (adapter_list_i != NULL) {
        device_list_prev_i = adapter_list_i->device_list;
        device_list_i = adapter_list_i->device_list->next;
        while (device_list_i != NULL) {
            arg_i = call_dbus_method(conn, "org.bluez", device_list_i->object_path, "org.bluez.Device", "GetProperties");
            dbus_message_iter_recurse(&arg_i, &element_i);
            while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
                if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
                    dbus_message_iter_recurse(&element_i, &dict_i);
                    dbus_message_iter_get_basic(&dict_i, &key);
                    dbus_message_iter_next(&dict_i);
                    dbus_message_iter_recurse(&dict_i, &variant_i);
                    if (strcmp(key, "Name") == 0) {
                        dbus_message_iter_get_basic(&variant_i, &value);
                        device_list_i->name = pa_xstrdup(value);
                    }
                    else if (strcmp(key, "Paired") == 0) {
                        dbus_message_iter_get_basic(&variant_i, &uvalue);
                        device_list_i->paired = uvalue;
                    }
                    else if (strcmp(key, "Alias") == 0) {
                        dbus_message_iter_get_basic(&variant_i, &value);
                        device_list_i->alias = pa_xstrdup(value);
                    }
                    else if (strcmp(key, "Connected") == 0) {
                        dbus_message_iter_get_basic(&variant_i, &uvalue);
                        device_list_i->connected = uvalue;
                    }
                    else if (strcmp(key, "UUIDs") == 0) {
                        DBusMessageIter uuid_i;
                        int is_audio_device = 0;
                        dbus_message_iter_recurse(&variant_i, &uuid_i);
                        while (dbus_message_iter_get_arg_type(&uuid_i) != DBUS_TYPE_INVALID) {
                            dbus_message_iter_get_basic(&uuid_i, &value);
                            if ( (strcasecmp(value, HSP_HS_UUID) == 0) || (strcasecmp(value, HFP_HS_UUID) == 0) ||
                                (strcasecmp(value, A2DP_SOURCE_UUID) == 0) || (strcasecmp(value, A2DP_SINK_UUID) == 0) )
                                is_audio_device = 1;
                            uuid_list_append(device_list_i->uuid_list, value);
                            dbus_message_iter_next(&uuid_i);
                        }
                        if (!is_audio_device) {
                            /* remove current device */
                            device_list_prev_i->next = device_list_i->next;
                            pa_xfree(device_list_i);
                            break;
                        }
                    }
                    else if (strcmp(key, "Address") == 0) {
                        dbus_message_iter_get_basic(&variant_i, &value);
                        device_list_i->address = pa_xstrdup(value);
                    }
                    else if (strcmp(key, "Class") == 0) {
                        dbus_message_iter_get_basic(&variant_i, &uvalue);
                        device_list_i->class = uvalue;
                    }
                    else if (strcmp(key, "Trusted") == 0) {
                        dbus_message_iter_get_basic(&variant_i, &uvalue);
                        device_list_i->trusted = uvalue;
                    }
                }
                dbus_message_iter_next(&element_i);
            }
            device_list_prev_i = device_list_prev_i->next;
            if (device_list_prev_i == NULL)
                device_list_i = NULL;
            else
                device_list_i = device_list_prev_i->next;
        }
        adapter_list_i = adapter_list_i->next;
    }
}

static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void *userdata) {
    DBusMessageIter arg_i;
    DBusError err;
    const char *value;
    struct userdata *u;

    pa_assert(bus);
    pa_assert(msg);
    pa_assert(userdata);
    u = userdata;
    dbus_error_init(&err);

    pa_log("dbus: interface=%s, path=%s, member=%s\n",
            dbus_message_get_interface(msg),
            dbus_message_get_path(msg),
            dbus_message_get_member(msg));

    if (dbus_message_is_signal(msg, "org.bluez.Manager", "AdapterAdded")) {
        if (!dbus_message_iter_init(msg, &arg_i))
            pa_log("dbus: message has no parameters");
        else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH)
            pa_log("dbus: argument is not object path");
        else {
            dbus_message_iter_get_basic(&arg_i, &value);
            pa_log("hcid: adapter %s added", value);
        }
    }
    else if (dbus_message_is_signal(msg, "org.bluez.Manager", "AdapterRemoved")) {
        if (!dbus_message_iter_init(msg, &arg_i))
            pa_log("dbus: message has no parameters");
        else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH)
            pa_log("dbus: argument is not object path");
        else {
            dbus_message_iter_get_basic(&arg_i, &value);
            pa_log("hcid: adapter %s removed", value);
        }
    }
    else if (dbus_message_is_signal(msg, "org.bluez.Adapter", "DeviceCreated")) {
        if (!dbus_message_iter_init(msg, &arg_i))
            pa_log("dbus: message has no parameters");
        else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH)
            pa_log("dbus: argument is not object path");
        else {
            dbus_message_iter_get_basic(&arg_i, &value);
            pa_log("hcid: device %s created", value);
        }
    }
    else if (dbus_message_is_signal(msg, "org.bluez.Adapter", "DeviceRemoved")) {
        if (!dbus_message_iter_init(msg, &arg_i))
            pa_log("dbus: message has no parameters");
        else if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_OBJECT_PATH)
            pa_log("dbus: argument is not object path");
        else {
            dbus_message_iter_get_basic(&arg_i, &value);
            pa_log("hcid: device %s removed", value);
        }
    }

finish:
    dbus_error_free(&err);
    return DBUS_HANDLER_RESULT_HANDLED;
}

void pa__done(pa_module* m) {
    struct userdata *u;
    adapter_t *adapter_list_i, *adapter_list_next_i;
    device_t *device_list_i, *device_list_next_i;

    pa_assert(m);

    if (!(u = m->userdata))
        return;

    if ((adapter_list_i = u->adapter_list) != NULL) {
        while ((adapter_list_next_i = adapter_list_i->next) != NULL) {
            if ((device_list_i = adapter_list_i->device_list) != NULL) {
                while ((device_list_next_i = device_list_i->next) != NULL) {
                    pa_xfree(device_list_i);
                    device_list_i = device_list_next_i;
                }
                pa_xfree(device_list_i);
            }
            pa_xfree(adapter_list_i);
            adapter_list_i = adapter_list_next_i;
        }
        pa_xfree(adapter_list_i);
    }

    pa_dbus_connection_unref(u->conn);
    pa_xfree(u);
    pa_log("Unloading module-bt-discover");
    return;
}

int pa__init(pa_module* m) {
    pa_modargs *ma = NULL;
    DBusError err;
    DBusMessageIter arg_i, element_i;
    adapter_t *adapter_list_i;
    device_t *device_list_i;
    const char *value;
    unsigned int hcid_running = 0;
    struct userdata *u;

    pa_assert(m);
    dbus_error_init(&err);
    pa_log("Loading module-bt-discover");
    m->userdata = u = pa_xnew0(struct userdata, 1);
    u->module = m;

    /* connect to the bus */
    u->conn = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &err);
    if ( dbus_error_is_set(&err) || (u->conn == NULL) ) {
        pa_log("Failed to get D-Bus connection: %s", err.message);
        goto fail;
    }

    /* check if hcid is running */
    arg_i = call_dbus_method(u->conn, "org.freedesktop.DBus", "/org/freedesktop/DBus" , "org.freedesktop.DBus", "ListNames");
    dbus_message_iter_recurse(&arg_i, &element_i);
    while (dbus_message_iter_get_arg_type(&element_i) != DBUS_TYPE_INVALID) {
        if (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_STRING) {
            dbus_message_iter_get_basic(&element_i, &value);
            if (strcmp(value, "org.bluez") == 0)
                hcid_running = 1;
        }
        dbus_message_iter_next(&element_i);
    }
    if (!hcid_running) {
        pa_log("hcid not running");
        goto fail;
    }

    /* static detection of bluetooth audio devices */
    u->adapter_list = adapter_new("/ADAPTER_HEAD");
    detect_adapters(u->adapter_list, u->conn);
    detect_devices(u->adapter_list, u->conn);

    print_adapters(u->adapter_list);

    /* load device modules */
    adapter_list_i = u->adapter_list->next;
    while (adapter_list_i != NULL) {
        device_list_i = adapter_list_i->device_list->next;
        while (device_list_i != NULL) {
            pa_log("Loading module-bt-device for %s", device_list_i->name); /* CHECK: Should it be name or alias? */
            /* call module */
            device_list_i = device_list_i->next;
        }
        adapter_list_i = adapter_list_i->next;
    }

    /* dynamic detection of bluetooth audio devices */
    if (!dbus_connection_add_filter(pa_dbus_connection_get(u->conn), filter_cb, u, NULL)) {
        pa_log_error("Failed to add filter function");
        goto fail;
    }
    dbus_connection_flush(pa_dbus_connection_get(u->conn));
    dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',interface='org.bluez.Manager'", &err);
    dbus_connection_flush(pa_dbus_connection_get(u->conn));
    if (dbus_error_is_set(&err)) {
        pa_log_error("Unable to subscribe to org.bluez.Manager signals: %s: %s", err.name, err.message);
        goto fail;
    }
    dbus_bus_add_match(pa_dbus_connection_get(u->conn), "type='signal',interface='org.bluez.Adapter'", &err);
    dbus_connection_flush(pa_dbus_connection_get(u->conn));
    if (dbus_error_is_set(&err)) {
        pa_log_error("Unable to subscribe to org.bluez.Adapter signals: %s: %s", err.name, err.message);
        goto fail;
    }

    return 0;

fail:
    if (ma)
        pa_modargs_free(ma);
    dbus_error_free(&err);
    pa__done(m);
    return -1;
}