]> code.delx.au - pulseaudio/blob - src/modules/bluetooth/bluez5-util.c
bluetooth: Create pa_bluetooth_transport for BlueZ 5 support
[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 #define BLUEZ_MEDIA_TRANSPORT_INTERFACE BLUEZ_SERVICE ".MediaTransport1"
40
41 struct pa_bluetooth_discovery {
42 PA_REFCNT_DECLARE;
43
44 pa_core *core;
45 pa_dbus_connection *connection;
46 bool filter_added;
47 bool matches_added;
48 pa_hook hooks[PA_BLUETOOTH_HOOK_MAX];
49 pa_hashmap *adapters;
50 pa_hashmap *devices;
51 pa_hashmap *transports;
52 };
53
54 pa_bluetooth_transport *pa_bluetooth_transport_new(pa_bluetooth_device *d, const char *owner, const char *path,
55 pa_bluetooth_profile_t p, const uint8_t *config, size_t size) {
56 pa_bluetooth_transport *t;
57
58 t = pa_xnew0(pa_bluetooth_transport, 1);
59 t->device = d;
60 t->owner = pa_xstrdup(owner);
61 t->path = pa_xstrdup(path);
62 t->profile = p;
63 t->config_size = size;
64
65 if (size > 0) {
66 t->config = pa_xnew(uint8_t, size);
67 memcpy(t->config, config, size);
68 }
69
70 pa_assert_se(pa_hashmap_put(d->discovery->transports, t->path, t) >= 0);
71
72 return t;
73 }
74
75 static const char *transport_state_to_string(pa_bluetooth_transport_state_t state) {
76 switch(state) {
77 case PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED:
78 return "disconnected";
79 case PA_BLUETOOTH_TRANSPORT_STATE_IDLE:
80 return "idle";
81 case PA_BLUETOOTH_TRANSPORT_STATE_PLAYING:
82 return "playing";
83 }
84
85 return "invalid";
86 }
87
88 static void transport_state_changed(pa_bluetooth_transport *t, pa_bluetooth_transport_state_t state) {
89 bool old_any_connected;
90
91 pa_assert(t);
92
93 if (t->state == state)
94 return;
95
96 old_any_connected = pa_bluetooth_device_any_transport_connected(t->device);
97
98 pa_log_debug("Transport %s state changed from %s to %s",
99 t->path, transport_state_to_string(t->state), transport_state_to_string(state));
100
101 t->state = state;
102 if (state == PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)
103 t->device->transports[t->profile] = NULL;
104
105 pa_hook_fire(&t->device->discovery->hooks[PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED], t);
106
107 if (old_any_connected != pa_bluetooth_device_any_transport_connected(t->device))
108 pa_hook_fire(&t->device->discovery->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], t->device);
109 }
110
111 void pa_bluetooth_transport_put(pa_bluetooth_transport *t) {
112 transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_IDLE);
113 }
114
115 void pa_bluetooth_transport_free(pa_bluetooth_transport *t) {
116 pa_assert(t);
117
118 pa_hashmap_remove(t->device->discovery->transports, t->path);
119 pa_xfree(t->owner);
120 pa_xfree(t->path);
121 pa_xfree(t->config);
122 pa_xfree(t);
123 }
124
125 static int bluez5_transport_acquire_cb(pa_bluetooth_transport *t, bool optional, size_t *imtu, size_t *omtu) {
126 DBusMessage *m, *r;
127 DBusError err;
128 int ret;
129 uint16_t i, o;
130 const char *method = optional ? "TryAcquire" : "Acquire";
131
132 pa_assert(t);
133 pa_assert(t->device);
134 pa_assert(t->device->discovery);
135
136 pa_assert_se(m = dbus_message_new_method_call(t->owner, t->path, BLUEZ_MEDIA_TRANSPORT_INTERFACE, method));
137
138 dbus_error_init(&err);
139
140 r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->device->discovery->connection), m, -1, &err);
141 if (!r) {
142 if (optional && pa_streq(err.name, "org.bluez.Error.NotAvailable"))
143 pa_log_info("Failed optional acquire of unavailable transport %s", t->path);
144 else
145 pa_log_error("Transport %s() failed for transport %s (%s)", method, t->path, err.message);
146
147 dbus_error_free(&err);
148 return -1;
149 }
150
151 if (!dbus_message_get_args(r, &err, DBUS_TYPE_UNIX_FD, &ret, DBUS_TYPE_UINT16, &i, DBUS_TYPE_UINT16, &o,
152 DBUS_TYPE_INVALID)) {
153 pa_log_error("Failed to parse %s() reply: %s", method, err.message);
154 dbus_error_free(&err);
155 ret = -1;
156 goto finish;
157 }
158
159 if (imtu)
160 *imtu = i;
161
162 if (omtu)
163 *omtu = o;
164
165 finish:
166 dbus_message_unref(r);
167 return ret;
168 }
169
170 static void bluez5_transport_release_cb(pa_bluetooth_transport *t) {
171 DBusMessage *m;
172 DBusError err;
173
174 pa_assert(t);
175 pa_assert(t->device);
176 pa_assert(t->device->discovery);
177
178 dbus_error_init(&err);
179
180 if (t->state <= PA_BLUETOOTH_TRANSPORT_STATE_IDLE) {
181 pa_log_info("Transport %s auto-released by BlueZ or already released", t->path);
182 return;
183 }
184
185 pa_assert_se(m = dbus_message_new_method_call(t->owner, t->path, BLUEZ_MEDIA_TRANSPORT_INTERFACE, "Release"));
186 dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->device->discovery->connection), m, -1, &err);
187
188 if (dbus_error_is_set(&err)) {
189 pa_log_error("Failed to release transport %s: %s", t->path, err.message);
190 dbus_error_free(&err);
191 } else
192 pa_log_info("Transport %s released", t->path);
193 }
194
195 bool pa_bluetooth_device_any_transport_connected(const pa_bluetooth_device *d) {
196 unsigned i;
197
198 pa_assert(d);
199
200 if (d->device_info_valid != 1)
201 return false;
202
203 for (i = 0; i < PA_BLUETOOTH_PROFILE_COUNT; i++)
204 if (d->transports[i] && d->transports[i]->state != PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)
205 return true;
206
207 return false;
208 }
209
210 static pa_bluetooth_device* device_create(pa_bluetooth_discovery *y, const char *path) {
211 pa_bluetooth_device *d;
212
213 pa_assert(y);
214 pa_assert(path);
215
216 d = pa_xnew0(pa_bluetooth_device, 1);
217 d->discovery = y;
218 d->path = pa_xstrdup(path);
219
220 pa_hashmap_put(y->devices, d->path, d);
221
222 return d;
223 }
224
225 pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_path(pa_bluetooth_discovery *y, const char *path) {
226 pa_bluetooth_device *d;
227
228 pa_assert(y);
229 pa_assert(PA_REFCNT_VALUE(y) > 0);
230 pa_assert(path);
231
232 if ((d = pa_hashmap_get(y->devices, path)))
233 if (d->device_info_valid == 1)
234 return d;
235
236 return NULL;
237 }
238
239 pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_address(pa_bluetooth_discovery *y, const char *remote, const char *local) {
240 pa_bluetooth_device *d;
241 void *state = NULL;
242
243 pa_assert(y);
244 pa_assert(PA_REFCNT_VALUE(y) > 0);
245 pa_assert(remote);
246 pa_assert(local);
247
248 while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
249 if (pa_streq(d->address, remote) && pa_streq(d->adapter->address, local))
250 return d->device_info_valid == 1 ? d : NULL;
251
252 return NULL;
253 }
254
255 static void device_free(pa_bluetooth_device *d) {
256 unsigned i;
257
258 pa_assert(d);
259
260 for (i = 0; i < PA_BLUETOOTH_PROFILE_COUNT; i++) {
261 pa_bluetooth_transport *t;
262
263 if (!(t = d->transports[i]))
264 continue;
265
266 transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED);
267 pa_bluetooth_transport_free(t);
268 }
269
270 d->discovery = NULL;
271 d->adapter = NULL;
272 pa_xfree(d->path);
273 pa_xfree(d->alias);
274 pa_xfree(d->address);
275 pa_xfree(d);
276 }
277
278 static void device_remove(pa_bluetooth_discovery *y, const char *path) {
279 pa_bluetooth_device *d;
280
281 if (!(d = pa_hashmap_remove(y->devices, path)))
282 pa_log_warn("Unknown device removed %s", path);
283 else {
284 pa_log_debug("Device %s removed", path);
285 device_free(d);
286 }
287 }
288
289 static void device_remove_all(pa_bluetooth_discovery *y) {
290 pa_bluetooth_device *d;
291
292 pa_assert(y);
293
294 while ((d = pa_hashmap_steal_first(y->devices))) {
295 d->device_info_valid = -1;
296 pa_hook_fire(&y->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], d);
297 device_free(d);
298 }
299 }
300
301 static pa_bluetooth_adapter* adapter_create(pa_bluetooth_discovery *y, const char *path) {
302 pa_bluetooth_adapter *a;
303
304 pa_assert(y);
305 pa_assert(path);
306
307 a = pa_xnew0(pa_bluetooth_adapter, 1);
308 a->discovery = y;
309 a->path = pa_xstrdup(path);
310
311 pa_hashmap_put(y->adapters, a->path, a);
312
313 return a;
314 }
315
316 static void adapter_remove_all(pa_bluetooth_discovery *y) {
317 pa_bluetooth_adapter *a;
318
319 pa_assert(y);
320
321 /* When this function is called all devices have already been freed */
322
323 while ((a = pa_hashmap_steal_first(y->adapters))) {
324 pa_xfree(a->path);
325 pa_xfree(a->address);
326 pa_xfree(a);
327 }
328 }
329
330 pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook) {
331 pa_assert(y);
332 pa_assert(PA_REFCNT_VALUE(y) > 0);
333
334 return &y->hooks[hook];
335 }
336
337 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
338 pa_bluetooth_discovery *y;
339 DBusError err;
340
341 pa_assert(bus);
342 pa_assert(m);
343 pa_assert_se(y = userdata);
344
345 dbus_error_init(&err);
346
347 if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
348 const char *name, *old_owner, *new_owner;
349
350 if (!dbus_message_get_args(m, &err,
351 DBUS_TYPE_STRING, &name,
352 DBUS_TYPE_STRING, &old_owner,
353 DBUS_TYPE_STRING, &new_owner,
354 DBUS_TYPE_INVALID)) {
355 pa_log_error("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
356 goto fail;
357 }
358
359 if (pa_streq(name, BLUEZ_SERVICE)) {
360 if (old_owner && *old_owner) {
361 pa_log_debug("Bluetooth daemon disappeared");
362 device_remove_all(y);
363 adapter_remove_all(y);
364 }
365
366 if (new_owner && *new_owner) {
367 pa_log_debug("Bluetooth daemon appeared");
368 /* TODO: get managed objects */
369 }
370 }
371
372 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
373 }
374
375 fail:
376 dbus_error_free(&err);
377
378 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
379 }
380
381 pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
382 pa_bluetooth_discovery *y;
383 DBusError err;
384 DBusConnection *conn;
385 unsigned i;
386
387 if ((y = pa_shared_get(c, "bluetooth-discovery")))
388 return pa_bluetooth_discovery_ref(y);
389
390 y = pa_xnew0(pa_bluetooth_discovery, 1);
391 PA_REFCNT_INIT(y);
392 y->core = c;
393 y->adapters = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
394 y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
395 y->transports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
396
397 for (i = 0; i < PA_BLUETOOTH_HOOK_MAX; i++)
398 pa_hook_init(&y->hooks[i], y);
399
400 pa_shared_set(c, "bluetooth-discovery", y);
401
402 dbus_error_init(&err);
403
404 if (!(y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err))) {
405 pa_log_error("Failed to get D-Bus connection: %s", err.message);
406 goto fail;
407 }
408
409 conn = pa_dbus_connection_get(y->connection);
410
411 /* dynamic detection of bluetooth audio devices */
412 if (!dbus_connection_add_filter(conn, filter_cb, y, NULL)) {
413 pa_log_error("Failed to add filter function");
414 goto fail;
415 }
416 y->filter_added = true;
417
418 if (pa_dbus_add_matches(conn, &err,
419 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'"
420 ",arg0='" BLUEZ_SERVICE "'",
421 NULL) < 0) {
422 pa_log_error("Failed to add D-Bus matches: %s", err.message);
423 goto fail;
424 }
425 y->matches_added = true;
426
427 return y;
428
429 fail:
430 pa_bluetooth_discovery_unref(y);
431 dbus_error_free(&err);
432
433 return NULL;
434 }
435
436 pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
437 pa_assert(y);
438 pa_assert(PA_REFCNT_VALUE(y) > 0);
439
440 PA_REFCNT_INC(y);
441
442 return y;
443 }
444
445 void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
446 pa_assert(y);
447 pa_assert(PA_REFCNT_VALUE(y) > 0);
448
449 if (PA_REFCNT_DEC(y) > 0)
450 return;
451
452 if (y->devices) {
453 device_remove_all(y);
454 pa_hashmap_free(y->devices);
455 }
456
457 if (y->adapters) {
458 adapter_remove_all(y);
459 pa_hashmap_free(y->adapters);
460 }
461
462 if (y->transports) {
463 pa_assert(pa_hashmap_isempty(y->transports));
464 pa_hashmap_free(y->transports);
465 }
466
467 if (y->connection) {
468
469 if (y->matches_added)
470 pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
471 "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',"
472 "arg0='" BLUEZ_SERVICE "'",
473 NULL);
474
475 if (y->filter_added)
476 dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
477
478 pa_dbus_connection_unref(y->connection);
479 }
480
481 pa_shared_remove(y->core, "bluetooth-discovery");
482 pa_xfree(y);
483 }