]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/bluez5-util.c
bluetooth: Remove all devices and adapters when org.bluez goes away
[pulseaudio] / src / modules / bluetooth / bluez5-util.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008-2013 João 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 <pulse/xmalloc.h>
27
28 #include <pulsecore/core.h>
29 #include <pulsecore/core-util.h>
30 #include <pulsecore/dbus-shared.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/refcnt.h>
34 #include <pulsecore/shared.h>
35
36 #include "bluez5-util.h"
37
38 #define BLUEZ_SERVICE "org.bluez"
39
40 struct pa_bluetooth_discovery {
41 PA_REFCNT_DECLARE;
42
43 pa_core *core;
44 pa_dbus_connection *connection;
45 bool filter_added;
46 bool matches_added;
47 pa_hook hooks[PA_BLUETOOTH_HOOK_MAX];
48 pa_hashmap *adapters;
49 pa_hashmap *devices;
50 };
51
52 static pa_bluetooth_device* device_create(pa_bluetooth_discovery *y, const char *path) {
53 pa_bluetooth_device *d;
54
55 pa_assert(y);
56 pa_assert(path);
57
58 d = pa_xnew0(pa_bluetooth_device, 1);
59 d->discovery = y;
60 d->path = pa_xstrdup(path);
61
62 pa_hashmap_put(y->devices, d->path, d);
63
64 return d;
65 }
66
67 pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_path(pa_bluetooth_discovery *y, const char *path) {
68 pa_bluetooth_device *d;
69
70 pa_assert(y);
71 pa_assert(PA_REFCNT_VALUE(y) > 0);
72 pa_assert(path);
73
74 if ((d = pa_hashmap_get(y->devices, path)))
75 if (d->device_info_valid == 1)
76 return d;
77
78 return NULL;
79 }
80
81 pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_address(pa_bluetooth_discovery *y, const char *remote, const char *local) {
82 pa_bluetooth_device *d;
83 void *state = NULL;
84
85 pa_assert(y);
86 pa_assert(PA_REFCNT_VALUE(y) > 0);
87 pa_assert(remote);
88 pa_assert(local);
89
90 while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
91 if (pa_streq(d->address, remote) && pa_streq(d->adapter->address, local))
92 return d->device_info_valid == 1 ? d : NULL;
93
94 return NULL;
95 }
96
97 static void device_free(pa_bluetooth_device *d) {
98 pa_assert(d);
99
100 d->discovery = NULL;
101 d->adapter = NULL;
102 pa_xfree(d->path);
103 pa_xfree(d->alias);
104 pa_xfree(d->address);
105 pa_xfree(d);
106 }
107
108 static void device_remove(pa_bluetooth_discovery *y, const char *path) {
109 pa_bluetooth_device *d;
110
111 if (!(d = pa_hashmap_remove(y->devices, path)))
112 pa_log_warn("Unknown device removed %s", path);
113 else {
114 pa_log_debug("Device %s removed", path);
115 device_free(d);
116 }
117 }
118
119 static void device_remove_all(pa_bluetooth_discovery *y) {
120 pa_bluetooth_device *d;
121
122 pa_assert(y);
123
124 while ((d = pa_hashmap_steal_first(y->devices))) {
125 d->device_info_valid = -1;
126 pa_hook_fire(&y->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], d);
127 device_free(d);
128 }
129 }
130
131 static pa_bluetooth_adapter* adapter_create(pa_bluetooth_discovery *y, const char *path) {
132 pa_bluetooth_adapter *a;
133
134 pa_assert(y);
135 pa_assert(path);
136
137 a = pa_xnew0(pa_bluetooth_adapter, 1);
138 a->discovery = y;
139 a->path = pa_xstrdup(path);
140
141 pa_hashmap_put(y->adapters, a->path, a);
142
143 return a;
144 }
145
146 static void adapter_remove_all(pa_bluetooth_discovery *y) {
147 pa_bluetooth_adapter *a;
148
149 pa_assert(y);
150
151 /* When this function is called all devices have already been freed */
152
153 while ((a = pa_hashmap_steal_first(y->adapters))) {
154 pa_xfree(a->path);
155 pa_xfree(a->address);
156 pa_xfree(a);
157 }
158 }
159
160 pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook) {
161 pa_assert(y);
162 pa_assert(PA_REFCNT_VALUE(y) > 0);
163
164 return &y->hooks[hook];
165 }
166
167 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
168 pa_bluetooth_discovery *y;
169 DBusError err;
170
171 pa_assert(bus);
172 pa_assert(m);
173 pa_assert_se(y = userdata);
174
175 dbus_error_init(&err);
176
177 if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
178 const char *name, *old_owner, *new_owner;
179
180 if (!dbus_message_get_args(m, &err,
181 DBUS_TYPE_STRING, &name,
182 DBUS_TYPE_STRING, &old_owner,
183 DBUS_TYPE_STRING, &new_owner,
184 DBUS_TYPE_INVALID)) {
185 pa_log_error("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
186 goto fail;
187 }
188
189 if (pa_streq(name, BLUEZ_SERVICE)) {
190 if (old_owner && *old_owner) {
191 pa_log_debug("Bluetooth daemon disappeared");
192 device_remove_all(y);
193 adapter_remove_all(y);
194 }
195
196 if (new_owner && *new_owner) {
197 pa_log_debug("Bluetooth daemon appeared");
198 /* TODO: get managed objects */
199 }
200 }
201
202 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
203 }
204
205 fail:
206 dbus_error_free(&err);
207
208 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
209 }
210
211 pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
212 pa_bluetooth_discovery *y;
213 DBusError err;
214 DBusConnection *conn;
215 unsigned i;
216
217 if ((y = pa_shared_get(c, "bluetooth-discovery")))
218 return pa_bluetooth_discovery_ref(y);
219
220 y = pa_xnew0(pa_bluetooth_discovery, 1);
221 PA_REFCNT_INIT(y);
222 y->core = c;
223 y->adapters = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
224 y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
225
226 for (i = 0; i < PA_BLUETOOTH_HOOK_MAX; i++)
227 pa_hook_init(&y->hooks[i], y);
228
229 pa_shared_set(c, "bluetooth-discovery", y);
230
231 dbus_error_init(&err);
232
233 if (!(y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err))) {
234 pa_log_error("Failed to get D-Bus connection: %s", err.message);
235 goto fail;
236 }
237
238 conn = pa_dbus_connection_get(y->connection);
239
240 /* dynamic detection of bluetooth audio devices */
241 if (!dbus_connection_add_filter(conn, filter_cb, y, NULL)) {
242 pa_log_error("Failed to add filter function");
243 goto fail;
244 }
245 y->filter_added = true;
246
247 if (pa_dbus_add_matches(conn, &err,
248 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'"
249 ",arg0='" BLUEZ_SERVICE "'",
250 NULL) < 0) {
251 pa_log_error("Failed to add D-Bus matches: %s", err.message);
252 goto fail;
253 }
254 y->matches_added = true;
255
256 return y;
257
258 fail:
259 pa_bluetooth_discovery_unref(y);
260 dbus_error_free(&err);
261
262 return NULL;
263 }
264
265 pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
266 pa_assert(y);
267 pa_assert(PA_REFCNT_VALUE(y) > 0);
268
269 PA_REFCNT_INC(y);
270
271 return y;
272 }
273
274 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
275 pa_assert(y);
276 pa_assert(PA_REFCNT_VALUE(y) > 0);
277
278 if (PA_REFCNT_DEC(y) > 0)
279 return;
280
281 if (y->devices) {
282 device_remove_all(y);
283 pa_hashmap_free(y->devices);
284 }
285
286 if (y->adapters) {
287 adapter_remove_all(y);
288 pa_hashmap_free(y->adapters);
289 }
290
291 if (y->connection) {
292
293 if (y->matches_added)
294 pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
295 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',"
296 "arg0='" BLUEZ_SERVICE "'",
297 NULL);
298
299 if (y->filter_added)
300 dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
301
302 pa_dbus_connection_unref(y->connection);
303 }
304
305 pa_shared_remove(y->core, "bluetooth-discovery");
306 pa_xfree(y);
307 }