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