]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/module-bluetooth-discover.c
bluetooth: Use stdbool for pa_bool_t
[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("sco_sink=<name of sink> "
44 "sco_source=<name of source> ");
45 PA_MODULE_LOAD_ONCE(true);
46
47 static const char* const valid_modargs[] = {
48 "sco_sink",
49 "sco_source",
50 "async", /* deprecated */
51 NULL
52 };
53
54 struct userdata {
55 pa_module *module;
56 pa_modargs *modargs;
57 pa_core *core;
58 pa_bluetooth_discovery *discovery;
59 pa_hook_slot *slot;
60 pa_hashmap *hashmap;
61 };
62
63 struct module_info {
64 char *path;
65 uint32_t module;
66 };
67
68 static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) {
69 struct module_info *mi;
70
71 pa_assert(u);
72 pa_assert(d);
73
74 mi = pa_hashmap_get(u->hashmap, d->path);
75
76 if (pa_bluetooth_device_any_audio_connected(d)) {
77
78 if (!mi) {
79 pa_module *m = NULL;
80 char *args;
81
82 /* Oh, awesome, a new device has shown up and been connected! */
83
84 args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path);
85
86 if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
87 pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
88 char *tmp;
89
90 tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
91 pa_modargs_get_value(u->modargs, "sco_sink", NULL),
92 pa_modargs_get_value(u->modargs, "sco_source", NULL));
93 pa_xfree(args);
94 args = tmp;
95 }
96
97 pa_log_debug("Loading module-bluetooth-device %s", args);
98 m = pa_module_load(u->module->core, "module-bluetooth-device", args);
99 pa_xfree(args);
100
101 if (m) {
102 mi = pa_xnew(struct module_info, 1);
103 mi->module = m->index;
104 mi->path = pa_xstrdup(d->path);
105
106 pa_hashmap_put(u->hashmap, mi->path, mi);
107 } else
108 pa_log_debug("Failed to load module for device %s", d->path);
109 }
110
111 } else {
112
113 if (mi) {
114
115 /* Hmm, disconnection? Then the module unloads itself */
116
117 pa_log_debug("Unregistering module for %s", d->path);
118 pa_hashmap_remove(u->hashmap, mi->path);
119 pa_xfree(mi->path);
120 pa_xfree(mi);
121 }
122 }
123
124 return PA_HOOK_OK;
125 }
126
127 int pa__init(pa_module* m) {
128 struct userdata *u;
129 pa_modargs *ma = NULL;
130
131 pa_assert(m);
132
133 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
134 pa_log("Failed to parse module arguments");
135 goto fail;
136 }
137
138 if (pa_modargs_get_value(ma, "async", NULL))
139 pa_log_warn("The 'async' argument is deprecated and does nothing.");
140
141 m->userdata = u = pa_xnew0(struct userdata, 1);
142 u->module = m;
143 u->core = m->core;
144 u->modargs = ma;
145 ma = NULL;
146 u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
147
148 if (!(u->discovery = pa_bluetooth_discovery_get(u->core)))
149 goto fail;
150
151 u->slot = pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED),
152 PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u);
153
154 return 0;
155
156 fail:
157 pa__done(m);
158
159 if (ma)
160 pa_modargs_free(ma);
161
162 return -1;
163 }
164
165 void pa__done(pa_module* m) {
166 struct userdata *u;
167
168 pa_assert(m);
169
170 if (!(u = m->userdata))
171 return;
172
173 if (u->slot)
174 pa_hook_slot_free(u->slot);
175
176 if (u->discovery)
177 pa_bluetooth_discovery_unref(u->discovery);
178
179 if (u->hashmap) {
180 struct module_info *mi;
181
182 while ((mi = pa_hashmap_steal_first(u->hashmap))) {
183 pa_xfree(mi->path);
184 pa_xfree(mi);
185 }
186
187 pa_hashmap_free(u->hashmap, NULL, NULL);
188 }
189
190 if (u->modargs)
191 pa_modargs_free(u->modargs);
192
193 pa_xfree(u);
194 }