bluetooth: add discover of bluetooth sources
[pulseaudio-mirror.git] / src / modules / bluetooth / module-bluetooth-discover.c
blob7571e48a09b5edef6c2397763e0be6ce10bd9429
1 /***
2 This file is part of PulseAudio.
4 Copyright 2008-2009 Joao Paulo Rechi Vita
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
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>
39 #include "module-bluetooth-discover-symdef.h"
40 #include "bluetooth-util.h"
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);
49 #ifdef NOKIA
50 "sco_sink=<name of sink> "
51 "sco_source=<name of source>"
52 #endif
55 static const char* const valid_modargs[] = {
56 #ifdef NOKIA
57 "sco_sink",
58 "sco_source",
59 #endif
60 "async",
61 NULL
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;
73 struct module_info {
74 char *path;
75 uint32_t module;
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;
81 pa_assert(u);
82 pa_assert(d);
84 mi = pa_hashmap_get(u->hashmap, d->path);
86 pa_log("dead: %d, device_connected: %d, audio_state: %d, audio_source_state: %d", d->dead, d->device_connected, d->audio_state, d->audio_source_state);
87 if (!d->dead &&
88 d->device_connected > 0 && (d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED || d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED)) {
90 if (!mi) {
91 pa_module *m = NULL;
92 char *args;
94 /* Oh, awesome, a new device has shown up and been connected! */
96 args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
97 #if 0
98 /* This is in case we have to use hsp immediately, without waiting for .Audio.State = Connected */
99 if (d->headset_state >= PA_BT_AUDIO_STATE_CONNECTED && somecondition) {
100 char *tmp;
101 tmp = pa_sprintf_malloc("%s profile=\"hsp\"", args);
102 pa_xfree(args);
103 args = tmp;
105 #endif
107 #ifdef NOKIA
108 if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
109 pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
110 char *tmp;
112 tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
113 pa_modargs_get_value(u->modargs, "sco_sink", NULL),
114 pa_modargs_get_value(u->modargs, "sco_source", NULL));
115 pa_xfree(args);
116 args = tmp;
118 #endif
120 if (d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED)
121 args = pa_sprintf_malloc("%s profile=\"a2dp_source\"", args);
123 pa_log_debug("Loading module-bluetooth-device %s", args);
124 m = pa_module_load(u->module->core, "module-bluetooth-device", args);
125 pa_xfree(args);
127 if (m) {
128 mi = pa_xnew(struct module_info, 1);
129 mi->module = m->index;
130 mi->path = pa_xstrdup(d->path);
132 pa_hashmap_put(u->hashmap, mi->path, mi);
133 } else
134 pa_log_debug("Failed to load module for device %s", d->path);
137 } else {
139 if (mi) {
141 /* Hmm, disconnection? Then let's unload our module */
143 pa_log_debug("Unloading module for %s", d->path);
144 pa_module_unload_request_by_index(u->core, mi->module, TRUE);
146 pa_hashmap_remove(u->hashmap, mi->path);
147 pa_xfree(mi->path);
148 pa_xfree(mi);
152 return PA_HOOK_OK;
155 int pa__init(pa_module* m) {
156 struct userdata *u;
157 pa_modargs *ma = NULL;
158 pa_bool_t async = FALSE;
160 pa_assert(m);
162 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
163 pa_log("Failed to parse module arguments");
164 goto fail;
167 if (pa_modargs_get_value_boolean(ma, "async", &async) < 0) {
168 pa_log("Failed to parse async argument.");
169 goto fail;
172 m->userdata = u = pa_xnew0(struct userdata, 1);
173 u->module = m;
174 u->core = m->core;
175 u->modargs = ma;
176 ma = NULL;
177 u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
179 if (!(u->discovery = pa_bluetooth_discovery_get(u->core)))
180 goto fail;
182 u->slot = pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery), PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u);
184 if (!async)
185 pa_bluetooth_discovery_sync(u->discovery);
187 return 0;
189 fail:
190 pa__done(m);
192 if (ma)
193 pa_modargs_free(ma);
195 return -1;
198 void pa__done(pa_module* m) {
199 struct userdata *u;
201 pa_assert(m);
203 if (!(u = m->userdata))
204 return;
206 if (u->slot)
207 pa_hook_slot_free(u->slot);
209 if (u->discovery)
210 pa_bluetooth_discovery_unref(u->discovery);
212 if (u->hashmap) {
213 struct module_info *mi;
215 while ((mi = pa_hashmap_steal_first(u->hashmap))) {
216 pa_xfree(mi->path);
217 pa_xfree(mi);
220 pa_hashmap_free(u->hashmap, NULL, NULL);
223 if (u->modargs)
224 pa_modargs_free(u->modargs);
226 pa_xfree(u);