Git init
[framework/multimedia/pulseaudio.git] / src / modules / bluetooth / module-bluetooth-discover.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2008-2009 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 && d->device_connected > 0 &&
87                 (/*d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED*/
88         d->audio_sink_state >= PA_BT_AUDIO_STATE_CONNECTED ||
89          d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED ||
90          d->hfgw_state > PA_BT_AUDIO_STATE_CONNECTED)) {
91
92         if (!mi) {
93             pa_module *m = NULL;
94             char *args;
95
96             /* Oh, awesome, a new device has shown up and been connected! */
97
98             args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
99 #ifdef BT_FULL_AUDIO_FEATURE
100             /* This is in case we have to use hsp immediately, without waiting for .Audio.State = Connected */
101             if (d->headset_state >= PA_BT_AUDIO_STATE_CONNECTED && somecondition) {
102                 char *tmp;
103                 tmp = pa_sprintf_malloc("%s profile=\"hsp\"", args);
104                 pa_xfree(args);
105                 args = tmp;
106             }
107 #endif
108
109 #ifdef NOKIA
110             if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
111                 pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
112                 char *tmp;
113
114                 tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
115                                         pa_modargs_get_value(u->modargs, "sco_sink", NULL),
116                                         pa_modargs_get_value(u->modargs, "sco_source", NULL));
117                 pa_xfree(args);
118                 args = tmp;
119             }
120 #endif
121
122 #ifdef BT_FULL_AUDIO_FEATURE
123
124             if (d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED)
125                 args = pa_sprintf_malloc("%s profile=\"a2dp_source\" auto_connect=no", args);
126
127             if (d->hfgw_state > PA_BT_AUDIO_STATE_CONNECTED)
128                 args = pa_sprintf_malloc("%s profile=\"hfgw\"", args);
129 #endif
130                         if (d->audio_sink_state >= PA_BT_AUDIO_STATE_CONNECTED)
131                                 args = pa_sprintf_malloc("%s profile=\"a2dp\"",args );
132
133             pa_log_debug("Loading module-bluetooth-device %s", args);
134             m = pa_module_load(u->module->core, "module-bluetooth-device", args);
135             pa_xfree(args);
136
137             if (m) {
138                 mi = pa_xnew(struct module_info, 1);
139                 mi->module = m->index;
140                 mi->path = pa_xstrdup(d->path);
141
142                 pa_hashmap_put(u->hashmap, mi->path, mi);
143             } else
144                 pa_log_debug("Failed to load module for device %s", d->path);
145         }
146
147     } else {
148
149         if (mi) {
150
151             /* Hmm, disconnection? Then let's unload our module */
152
153             pa_log_debug("Unloading module for %s", d->path);
154             pa_module_unload_request_by_index(u->core, mi->module, TRUE);
155
156             pa_hashmap_remove(u->hashmap, mi->path);
157             pa_xfree(mi->path);
158             pa_xfree(mi);
159         }
160     }
161
162     return PA_HOOK_OK;
163 }
164
165 int pa__init(pa_module* m) {
166     struct userdata *u;
167     pa_modargs *ma = NULL;
168     pa_bool_t async = FALSE;
169
170     pa_assert(m);
171
172     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
173         pa_log("Failed to parse module arguments");
174         goto fail;
175     }
176
177     if (pa_modargs_get_value_boolean(ma, "async", &async) < 0) {
178         pa_log("Failed to parse async argument.");
179         goto fail;
180     }
181
182     m->userdata = u = pa_xnew0(struct userdata, 1);
183     u->module = m;
184     u->core = m->core;
185     u->modargs = ma;
186     ma = NULL;
187     u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
188
189     if (!(u->discovery = pa_bluetooth_discovery_get(u->core)))
190         goto fail;
191
192     u->slot = pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery), PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u);
193
194     if (!async)
195         pa_bluetooth_discovery_sync(u->discovery);
196
197     return 0;
198
199 fail:
200     pa__done(m);
201
202     if (ma)
203         pa_modargs_free(ma);
204
205     return -1;
206 }
207
208 void pa__done(pa_module* m) {
209     struct userdata *u;
210
211     pa_assert(m);
212
213     if (!(u = m->userdata))
214         return;
215
216     if (u->slot)
217         pa_hook_slot_free(u->slot);
218
219     if (u->discovery)
220         pa_bluetooth_discovery_unref(u->discovery);
221
222     if (u->hashmap) {
223         struct module_info *mi;
224
225         while ((mi = pa_hashmap_steal_first(u->hashmap))) {
226             pa_xfree(mi->path);
227             pa_xfree(mi);
228         }
229
230         pa_hashmap_free(u->hashmap, NULL, NULL);
231     }
232
233     if (u->modargs)
234         pa_modargs_free(u->modargs);
235
236     pa_xfree(u);
237 }