]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/module-bluetooth-discover.c
788fee004a00028642d6f856285a3edd3eeb147b
[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 <pulsecore/dbus-shared.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("async=<Asynchronous initialization?>");
46 PA_MODULE_LOAD_ONCE(TRUE);
47
48 /*
49 #ifdef NOKIA
50 "sco_sink=<name of sink> "
51 "sco_source=<name of source>"
52 #endif
53 */
54
55 static const char* const valid_modargs[] = {
56 #ifdef NOKIA
57 "sco_sink",
58 "sco_source",
59 #endif
60 "async",
61 NULL
62 };
63
64 struct userdata {
65 pa_module *module;
66 pa_modargs *modargs;
67 pa_core *core;
68 pa_bluetooth_discovery *discovery;
69 pa_hook_slot *slot;
70 pa_hashmap *hashmap;
71 };
72
73 struct module_info {
74 char *path;
75 uint32_t module;
76 };
77
78 static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) {
79 struct module_info *mi;
80
81 pa_assert(u);
82 pa_assert(d);
83
84 mi = pa_hashmap_get(u->hashmap, d->path);
85
86 if (!d->dead &&
87 d->device_connected > 0 && d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED) {
88
89 if (!mi) {
90 pa_module *m = NULL;
91 char *args;
92
93 /* Oh, awesome, a new device has shown up and been connected! */
94
95 args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
96 #if 0
97 /* This is in case we have to use hsp immediately, without waiting for .Audio.State = Connected */
98 if (d->headset_state >= PA_BT_AUDIO_STATE_CONNECTED && somecondition) {
99 char *tmp;
100 tmp = pa_sprintf_malloc("%s profile=\"hsp\"", args);
101 pa_xfree(args);
102 args = tmp;
103 }
104 #endif
105
106 #ifdef NOKIA
107 if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
108 pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
109 char *tmp;
110
111 tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
112 pa_modargs_get_value(u->modargs, "sco_sink", NULL),
113 pa_modargs_get_value(u->modargs, "sco_source", NULL));
114 pa_xfree(args);
115 args = tmp;
116 }
117 #endif
118
119 pa_log_debug("Loading module-bluetooth-device %s", args);
120 m = pa_module_load(u->module->core, "module-bluetooth-device", args);
121 pa_xfree(args);
122
123 if (m) {
124 mi = pa_xnew(struct module_info, 1);
125 mi->module = m->index;
126 mi->path = pa_xstrdup(d->path);
127
128 pa_hashmap_put(u->hashmap, mi->path, mi);
129 } else
130 pa_log_debug("Failed to load module for device %s", d->path);
131 }
132
133 } else {
134
135 if (mi) {
136
137 /* Hmm, disconnection? Then let's unload our module */
138
139 pa_log_debug("Unloading module for %s", d->path);
140 pa_module_unload_request_by_index(u->core, mi->module, TRUE);
141
142 pa_hashmap_remove(u->hashmap, mi->path);
143 pa_xfree(mi->path);
144 pa_xfree(mi);
145 }
146 }
147
148 return PA_HOOK_OK;
149 }
150
151 int pa__init(pa_module* m) {
152 struct userdata *u;
153 pa_modargs *ma = NULL;
154 pa_bool_t async = FALSE;
155
156 pa_assert(m);
157
158 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
159 pa_log("Failed to parse module arguments");
160 goto fail;
161 }
162
163 if (pa_modargs_get_value_boolean(ma, "async", &async) < 0) {
164 pa_log("Failed to parse async argument.");
165 goto fail;
166 }
167
168 m->userdata = u = pa_xnew0(struct userdata, 1);
169 u->module = m;
170 u->core = m->core;
171 u->modargs = ma;
172 ma = NULL;
173 u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
174
175 if (!(u->discovery = pa_bluetooth_discovery_get(u->core)))
176 goto fail;
177
178 u->slot = pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery), PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u);
179
180 if (!async)
181 pa_bluetooth_discovery_sync(u->discovery);
182
183 return 0;
184
185 fail:
186 pa__done(m);
187
188 if (ma)
189 pa_modargs_free(ma);
190
191 return -1;
192 }
193
194 void pa__done(pa_module* m) {
195 struct userdata *u;
196
197 pa_assert(m);
198
199 if (!(u = m->userdata))
200 return;
201
202 if (u->slot)
203 pa_hook_slot_free(u->slot);
204
205 if (u->discovery)
206 pa_bluetooth_discovery_unref(u->discovery);
207
208 if (u->hashmap) {
209 struct module_info *mi;
210
211 while ((mi = pa_hashmap_steal_first(u->hashmap))) {
212 pa_xfree(mi->path);
213 pa_xfree(mi);
214 }
215
216 pa_hashmap_free(u->hashmap, NULL, NULL);
217 }
218
219 if (u->modargs)
220 pa_modargs_free(u->modargs);
221
222 pa_xfree(u);
223 }