]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/module-bluetooth-discover.c
Merge commit 'origin/master-tx'
[pulseaudio] / 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
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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
17 License 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/core-util.h>
33 #include <pulsecore/modargs.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/llist.h>
36 #include <pulsecore/core-util.h>
37 #include <modules/dbus-util.h>
38
39 #include "module-bluetooth-discover-symdef.h"
40 #include "bluetooth-util.h"
41
42 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
43 PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
44 PA_MODULE_VERSION(PACKAGE_VERSION);
45 PA_MODULE_USAGE("sco_sink=<name of sink> "
46 "sco_source=<name of source>"
47 "async=<Asynchronous initialization?>");
48
49 static const char* const valid_modargs[] = {
50 "sco_sink",
51 "sco_source",
52 "async",
53 NULL
54 };
55
56 struct userdata {
57 pa_module *module;
58 pa_modargs *modargs;
59 pa_core *core;
60 pa_dbus_connection *connection;
61 pa_bluetooth_discovery *discovery;
62 };
63
64 static void load_module_for_device(struct userdata *u, pa_bluetooth_device *d, pa_bool_t good) {
65 pa_assert(u);
66 pa_assert(d);
67
68 if (good &&
69 d->device_connected > 0 &&
70 (d->audio_sink_connected > 0 || d->headset_connected > 0)) {
71
72 if (((uint32_t) PA_PTR_TO_UINT(d->data))-1 == PA_INVALID_INDEX) {
73 pa_module *m = NULL;
74 char *args;
75
76 /* Oh, awesome, a new device has shown up and been connected! */
77
78 args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
79
80 if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
81 pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
82 char *tmp;
83
84 tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
85 pa_modargs_get_value(u->modargs, "sco_sink", NULL),
86 pa_modargs_get_value(u->modargs, "sco_source", NULL));
87 pa_xfree(args);
88 args = tmp;
89 }
90
91 pa_log_debug("Loading module-bluetooth-device %s", args);
92 m = pa_module_load(u->module->core, "module-bluetooth-device", args);
93 pa_xfree(args);
94
95 if (m)
96 d->data = PA_UINT_TO_PTR((uint32_t) (m->index+1));
97 else
98 pa_log_debug("Failed to load module for device %s", d->path);
99 }
100
101 } else {
102
103 if (((uint32_t) PA_PTR_TO_UINT(d->data))-1 != PA_INVALID_INDEX) {
104
105 /* Hmm, disconnection? Then let's unload our module */
106
107 pa_log_debug("Unloading module for %s", d->path);
108 pa_module_unload_request_by_index(u->core, (uint32_t) (PA_PTR_TO_UINT(d->data))-1, TRUE);
109 d->data = NULL;
110 }
111 }
112 }
113
114 static int setup_dbus(struct userdata *u) {
115 DBusError err;
116
117 dbus_error_init(&err);
118
119 u->connection = pa_dbus_bus_get(u->core, DBUS_BUS_SYSTEM, &err);
120
121 if (dbus_error_is_set(&err) || !u->connection) {
122 pa_log("Failed to get D-Bus connection: %s", err.message);
123 dbus_error_free(&err);
124 return -1;
125 }
126
127 return 0;
128 }
129
130 int pa__init(pa_module* m) {
131 struct userdata *u;
132 pa_modargs *ma = NULL;
133 pa_bool_t async = FALSE;
134
135 pa_assert(m);
136
137 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
138 pa_log("Failed to parse module arguments");
139 goto fail;
140 }
141
142 if (pa_modargs_get_value_boolean(ma, "async", &async) < 0) {
143 pa_log("Failed to parse async argument.");
144 goto fail;
145 }
146
147 m->userdata = u = pa_xnew0(struct userdata, 1);
148 u->module = m;
149 u->core = m->core;
150 u->modargs = ma;
151 ma = NULL;
152
153 if (setup_dbus(u) < 0)
154 goto fail;
155
156 if (!(u->discovery = pa_bluetooth_discovery_new(pa_dbus_connection_get(u->connection), load_module_for_device, u)))
157 goto fail;
158
159 if (!async)
160 pa_bluetooth_discovery_sync(u->discovery);
161
162 return 0;
163
164 fail:
165 pa__done(m);
166
167 if (ma)
168 pa_modargs_free(ma);
169
170 return -1;
171 }
172
173 void pa__done(pa_module* m) {
174 struct userdata *u;
175
176 pa_assert(m);
177
178 if (!(u = m->userdata))
179 return;
180
181 if (u->discovery)
182 pa_bluetooth_discovery_free(u->discovery);
183
184 if (u->connection)
185 pa_dbus_connection_unref(u->connection);
186
187 if (u->modargs)
188 pa_modargs_free(u->modargs);
189
190 pa_xfree(u);
191 }