]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/module-bluetooth-discover.c
bluetooth: Remove commented out code.
[pulseaudio] / 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
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/module.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/modargs.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/dbus-shared.h>
36
37 #include "module-bluetooth-discover-symdef.h"
38 #include "bluetooth-util.h"
39
40 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita");
41 PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers");
42 PA_MODULE_VERSION(PACKAGE_VERSION);
43 PA_MODULE_USAGE("async=<Asynchronous initialization?> "
44 "sco_sink=<name of sink> "
45 "sco_source=<name of source> ");
46 PA_MODULE_LOAD_ONCE(TRUE);
47
48 static const char* const valid_modargs[] = {
49 "sco_sink",
50 "sco_source",
51 "async",
52 NULL
53 };
54
55 struct userdata {
56 pa_module *module;
57 pa_modargs *modargs;
58 pa_core *core;
59 pa_bluetooth_discovery *discovery;
60 pa_hook_slot *slot;
61 pa_hashmap *hashmap;
62 };
63
64 struct module_info {
65 char *path;
66 uint32_t module;
67 };
68
69 static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) {
70 struct module_info *mi;
71
72 pa_assert(u);
73 pa_assert(d);
74
75 mi = pa_hashmap_get(u->hashmap, d->path);
76
77 if (!d->dead && d->device_connected > 0 &&
78 (d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED ||
79 d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED ||
80 d->hfgw_state >= PA_BT_AUDIO_STATE_CONNECTED)) {
81
82 if (!mi) {
83 pa_module *m = NULL;
84 char *args;
85
86 /* Oh, awesome, a new device has shown up and been connected! */
87
88 args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
89
90 if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
91 pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
92 char *tmp;
93
94 tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
95 pa_modargs_get_value(u->modargs, "sco_sink", NULL),
96 pa_modargs_get_value(u->modargs, "sco_source", NULL));
97 pa_xfree(args);
98 args = tmp;
99 }
100
101 pa_log_debug("Loading module-bluetooth-device %s", args);
102 m = pa_module_load(u->module->core, "module-bluetooth-device", args);
103 pa_xfree(args);
104
105 if (m) {
106 mi = pa_xnew(struct module_info, 1);
107 mi->module = m->index;
108 mi->path = pa_xstrdup(d->path);
109
110 pa_hashmap_put(u->hashmap, mi->path, mi);
111 } else
112 pa_log_debug("Failed to load module for device %s", d->path);
113 }
114
115 } else {
116
117 if (mi) {
118
119 /* Hmm, disconnection? Then let's unload our module */
120
121 pa_log_debug("Unloading module for %s", d->path);
122 pa_module_unload_request_by_index(u->core, mi->module, TRUE);
123
124 pa_hashmap_remove(u->hashmap, mi->path);
125 pa_xfree(mi->path);
126 pa_xfree(mi);
127 }
128 }
129
130 return PA_HOOK_OK;
131 }
132
133 int pa__init(pa_module* m) {
134 struct userdata *u;
135 pa_modargs *ma = NULL;
136 pa_bool_t async = FALSE;
137
138 pa_assert(m);
139
140 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
141 pa_log("Failed to parse module arguments");
142 goto fail;
143 }
144
145 if (pa_modargs_get_value_boolean(ma, "async", &async) < 0) {
146 pa_log("Failed to parse async argument.");
147 goto fail;
148 }
149
150 m->userdata = u = pa_xnew0(struct userdata, 1);
151 u->module = m;
152 u->core = m->core;
153 u->modargs = ma;
154 ma = NULL;
155 u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
156
157 if (!(u->discovery = pa_bluetooth_discovery_get(u->core)))
158 goto fail;
159
160 u->slot = pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery), PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u);
161
162 if (!async)
163 pa_bluetooth_discovery_sync(u->discovery);
164
165 return 0;
166
167 fail:
168 pa__done(m);
169
170 if (ma)
171 pa_modargs_free(ma);
172
173 return -1;
174 }
175
176 void pa__done(pa_module* m) {
177 struct userdata *u;
178
179 pa_assert(m);
180
181 if (!(u = m->userdata))
182 return;
183
184 if (u->slot)
185 pa_hook_slot_free(u->slot);
186
187 if (u->discovery)
188 pa_bluetooth_discovery_unref(u->discovery);
189
190 if (u->hashmap) {
191 struct module_info *mi;
192
193 while ((mi = pa_hashmap_steal_first(u->hashmap))) {
194 pa_xfree(mi->path);
195 pa_xfree(mi);
196 }
197
198 pa_hashmap_free(u->hashmap, NULL, NULL);
199 }
200
201 if (u->modargs)
202 pa_modargs_free(u->modargs);
203
204 pa_xfree(u);
205 }