2013-08-13 01:54:00 -03:00
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
|
|
|
|
|
|
|
|
|
Copyright 2008-2013 João Paulo Rechi Vita
|
|
|
|
|
|
|
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
License along with PulseAudio; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <pulse/xmalloc.h>
|
|
|
|
|
|
|
|
|
|
#include <pulsecore/core.h>
|
2013-09-18 16:16:55 -05:00
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/dbus-shared.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2013-08-13 01:54:00 -03:00
|
|
|
#include <pulsecore/macro.h>
|
|
|
|
|
#include <pulsecore/refcnt.h>
|
|
|
|
|
#include <pulsecore/shared.h>
|
|
|
|
|
|
2013-09-24 19:45:24 -03:00
|
|
|
#include "a2dp-codecs.h"
|
|
|
|
|
|
2013-08-13 01:54:00 -03:00
|
|
|
#include "bluez5-util.h"
|
|
|
|
|
|
2013-09-18 16:16:55 -05:00
|
|
|
#define BLUEZ_SERVICE "org.bluez"
|
2013-09-24 19:45:23 -03:00
|
|
|
#define BLUEZ_MEDIA_ENDPOINT_INTERFACE BLUEZ_SERVICE ".MediaEndpoint1"
|
2013-09-24 19:45:22 -03:00
|
|
|
#define BLUEZ_MEDIA_TRANSPORT_INTERFACE BLUEZ_SERVICE ".MediaTransport1"
|
2013-09-18 16:16:55 -05:00
|
|
|
|
2013-09-24 19:45:23 -03:00
|
|
|
#define A2DP_SOURCE_ENDPOINT "/MediaEndpoint/A2DPSource"
|
|
|
|
|
#define A2DP_SINK_ENDPOINT "/MediaEndpoint/A2DPSink"
|
|
|
|
|
|
|
|
|
|
#define ENDPOINT_INTROSPECT_XML \
|
|
|
|
|
DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
|
|
|
|
|
"<node>" \
|
|
|
|
|
" <interface name=\"" BLUEZ_MEDIA_ENDPOINT_INTERFACE "\">" \
|
|
|
|
|
" <method name=\"SetConfiguration\">" \
|
|
|
|
|
" <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
|
|
|
|
|
" <arg name=\"properties\" direction=\"in\" type=\"ay\"/>" \
|
|
|
|
|
" </method>" \
|
|
|
|
|
" <method name=\"SelectConfiguration\">" \
|
|
|
|
|
" <arg name=\"capabilities\" direction=\"in\" type=\"ay\"/>" \
|
|
|
|
|
" <arg name=\"configuration\" direction=\"out\" type=\"ay\"/>" \
|
|
|
|
|
" </method>" \
|
|
|
|
|
" <method name=\"ClearConfiguration\">" \
|
|
|
|
|
" <arg name=\"transport\" direction=\"in\" type=\"o\"/>" \
|
|
|
|
|
" </method>" \
|
|
|
|
|
" <method name=\"Release\">" \
|
|
|
|
|
" </method>" \
|
|
|
|
|
" </interface>" \
|
|
|
|
|
" <interface name=\"org.freedesktop.DBus.Introspectable\">" \
|
|
|
|
|
" <method name=\"Introspect\">" \
|
|
|
|
|
" <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
|
|
|
|
|
" </method>" \
|
|
|
|
|
" </interface>" \
|
|
|
|
|
"</node>"
|
|
|
|
|
|
2013-08-13 01:54:00 -03:00
|
|
|
struct pa_bluetooth_discovery {
|
|
|
|
|
PA_REFCNT_DECLARE;
|
|
|
|
|
|
|
|
|
|
pa_core *core;
|
2013-09-18 16:16:55 -05:00
|
|
|
pa_dbus_connection *connection;
|
|
|
|
|
bool filter_added;
|
|
|
|
|
bool matches_added;
|
2013-09-24 19:45:28 -03:00
|
|
|
bool objects_listed;
|
2013-09-18 16:16:56 -05:00
|
|
|
pa_hook hooks[PA_BLUETOOTH_HOOK_MAX];
|
2013-09-18 16:16:57 -05:00
|
|
|
pa_hashmap *adapters;
|
2013-09-18 16:16:58 -05:00
|
|
|
pa_hashmap *devices;
|
2013-09-24 19:45:22 -03:00
|
|
|
pa_hashmap *transports;
|
2013-09-24 19:45:27 -03:00
|
|
|
|
|
|
|
|
PA_LLIST_HEAD(pa_dbus_pending, pending);
|
2013-08-13 01:54:00 -03:00
|
|
|
};
|
|
|
|
|
|
2013-09-24 19:45:27 -03:00
|
|
|
static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, DBusMessage *m,
|
|
|
|
|
DBusPendingCallNotifyFunction func, void *call_data) {
|
|
|
|
|
pa_dbus_pending *p;
|
|
|
|
|
DBusPendingCall *call;
|
|
|
|
|
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
pa_assert(m);
|
|
|
|
|
|
|
|
|
|
pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y->connection), m, &call, -1));
|
|
|
|
|
|
|
|
|
|
p = pa_dbus_pending_new(pa_dbus_connection_get(y->connection), m, call, y, call_data);
|
|
|
|
|
PA_LLIST_PREPEND(pa_dbus_pending, y->pending, p);
|
|
|
|
|
dbus_pending_call_set_notify(call, func, p, NULL);
|
|
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 19:45:22 -03:00
|
|
|
pa_bluetooth_transport *pa_bluetooth_transport_new(pa_bluetooth_device *d, const char *owner, const char *path,
|
|
|
|
|
pa_bluetooth_profile_t p, const uint8_t *config, size_t size) {
|
|
|
|
|
pa_bluetooth_transport *t;
|
|
|
|
|
|
|
|
|
|
t = pa_xnew0(pa_bluetooth_transport, 1);
|
|
|
|
|
t->device = d;
|
|
|
|
|
t->owner = pa_xstrdup(owner);
|
|
|
|
|
t->path = pa_xstrdup(path);
|
|
|
|
|
t->profile = p;
|
|
|
|
|
t->config_size = size;
|
|
|
|
|
|
|
|
|
|
if (size > 0) {
|
|
|
|
|
t->config = pa_xnew(uint8_t, size);
|
|
|
|
|
memcpy(t->config, config, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_assert_se(pa_hashmap_put(d->discovery->transports, t->path, t) >= 0);
|
|
|
|
|
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char *transport_state_to_string(pa_bluetooth_transport_state_t state) {
|
|
|
|
|
switch(state) {
|
|
|
|
|
case PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED:
|
|
|
|
|
return "disconnected";
|
|
|
|
|
case PA_BLUETOOTH_TRANSPORT_STATE_IDLE:
|
|
|
|
|
return "idle";
|
|
|
|
|
case PA_BLUETOOTH_TRANSPORT_STATE_PLAYING:
|
|
|
|
|
return "playing";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "invalid";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void transport_state_changed(pa_bluetooth_transport *t, pa_bluetooth_transport_state_t state) {
|
|
|
|
|
bool old_any_connected;
|
|
|
|
|
|
|
|
|
|
pa_assert(t);
|
|
|
|
|
|
|
|
|
|
if (t->state == state)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
old_any_connected = pa_bluetooth_device_any_transport_connected(t->device);
|
|
|
|
|
|
|
|
|
|
pa_log_debug("Transport %s state changed from %s to %s",
|
|
|
|
|
t->path, transport_state_to_string(t->state), transport_state_to_string(state));
|
|
|
|
|
|
|
|
|
|
t->state = state;
|
|
|
|
|
if (state == PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)
|
|
|
|
|
t->device->transports[t->profile] = NULL;
|
|
|
|
|
|
|
|
|
|
pa_hook_fire(&t->device->discovery->hooks[PA_BLUETOOTH_HOOK_TRANSPORT_STATE_CHANGED], t);
|
|
|
|
|
|
|
|
|
|
if (old_any_connected != pa_bluetooth_device_any_transport_connected(t->device))
|
|
|
|
|
pa_hook_fire(&t->device->discovery->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], t->device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_bluetooth_transport_put(pa_bluetooth_transport *t) {
|
|
|
|
|
transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_IDLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_bluetooth_transport_free(pa_bluetooth_transport *t) {
|
|
|
|
|
pa_assert(t);
|
|
|
|
|
|
|
|
|
|
pa_hashmap_remove(t->device->discovery->transports, t->path);
|
|
|
|
|
pa_xfree(t->owner);
|
|
|
|
|
pa_xfree(t->path);
|
|
|
|
|
pa_xfree(t->config);
|
|
|
|
|
pa_xfree(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int bluez5_transport_acquire_cb(pa_bluetooth_transport *t, bool optional, size_t *imtu, size_t *omtu) {
|
|
|
|
|
DBusMessage *m, *r;
|
|
|
|
|
DBusError err;
|
|
|
|
|
int ret;
|
|
|
|
|
uint16_t i, o;
|
|
|
|
|
const char *method = optional ? "TryAcquire" : "Acquire";
|
|
|
|
|
|
|
|
|
|
pa_assert(t);
|
|
|
|
|
pa_assert(t->device);
|
|
|
|
|
pa_assert(t->device->discovery);
|
|
|
|
|
|
|
|
|
|
pa_assert_se(m = dbus_message_new_method_call(t->owner, t->path, BLUEZ_MEDIA_TRANSPORT_INTERFACE, method));
|
|
|
|
|
|
|
|
|
|
dbus_error_init(&err);
|
|
|
|
|
|
|
|
|
|
r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->device->discovery->connection), m, -1, &err);
|
|
|
|
|
if (!r) {
|
|
|
|
|
if (optional && pa_streq(err.name, "org.bluez.Error.NotAvailable"))
|
|
|
|
|
pa_log_info("Failed optional acquire of unavailable transport %s", t->path);
|
|
|
|
|
else
|
|
|
|
|
pa_log_error("Transport %s() failed for transport %s (%s)", method, t->path, err.message);
|
|
|
|
|
|
|
|
|
|
dbus_error_free(&err);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_get_args(r, &err, DBUS_TYPE_UNIX_FD, &ret, DBUS_TYPE_UINT16, &i, DBUS_TYPE_UINT16, &o,
|
|
|
|
|
DBUS_TYPE_INVALID)) {
|
|
|
|
|
pa_log_error("Failed to parse %s() reply: %s", method, err.message);
|
|
|
|
|
dbus_error_free(&err);
|
|
|
|
|
ret = -1;
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (imtu)
|
|
|
|
|
*imtu = i;
|
|
|
|
|
|
|
|
|
|
if (omtu)
|
|
|
|
|
*omtu = o;
|
|
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
dbus_message_unref(r);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bluez5_transport_release_cb(pa_bluetooth_transport *t) {
|
|
|
|
|
DBusMessage *m;
|
|
|
|
|
DBusError err;
|
|
|
|
|
|
|
|
|
|
pa_assert(t);
|
|
|
|
|
pa_assert(t->device);
|
|
|
|
|
pa_assert(t->device->discovery);
|
|
|
|
|
|
|
|
|
|
dbus_error_init(&err);
|
|
|
|
|
|
|
|
|
|
if (t->state <= PA_BLUETOOTH_TRANSPORT_STATE_IDLE) {
|
|
|
|
|
pa_log_info("Transport %s auto-released by BlueZ or already released", t->path);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_assert_se(m = dbus_message_new_method_call(t->owner, t->path, BLUEZ_MEDIA_TRANSPORT_INTERFACE, "Release"));
|
|
|
|
|
dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(t->device->discovery->connection), m, -1, &err);
|
|
|
|
|
|
|
|
|
|
if (dbus_error_is_set(&err)) {
|
|
|
|
|
pa_log_error("Failed to release transport %s: %s", t->path, err.message);
|
|
|
|
|
dbus_error_free(&err);
|
|
|
|
|
} else
|
|
|
|
|
pa_log_info("Transport %s released", t->path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool pa_bluetooth_device_any_transport_connected(const pa_bluetooth_device *d) {
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
pa_assert(d);
|
|
|
|
|
|
|
|
|
|
if (d->device_info_valid != 1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < PA_BLUETOOTH_PROFILE_COUNT; i++)
|
|
|
|
|
if (d->transports[i] && d->transports[i]->state != PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 16:16:58 -05:00
|
|
|
static pa_bluetooth_device* device_create(pa_bluetooth_discovery *y, const char *path) {
|
|
|
|
|
pa_bluetooth_device *d;
|
|
|
|
|
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
pa_assert(path);
|
|
|
|
|
|
|
|
|
|
d = pa_xnew0(pa_bluetooth_device, 1);
|
|
|
|
|
d->discovery = y;
|
|
|
|
|
d->path = pa_xstrdup(path);
|
|
|
|
|
|
|
|
|
|
pa_hashmap_put(y->devices, d->path, d);
|
|
|
|
|
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_path(pa_bluetooth_discovery *y, const char *path) {
|
|
|
|
|
pa_bluetooth_device *d;
|
|
|
|
|
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
pa_assert(PA_REFCNT_VALUE(y) > 0);
|
|
|
|
|
pa_assert(path);
|
|
|
|
|
|
|
|
|
|
if ((d = pa_hashmap_get(y->devices, path)))
|
|
|
|
|
if (d->device_info_valid == 1)
|
|
|
|
|
return d;
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_bluetooth_device* pa_bluetooth_discovery_get_device_by_address(pa_bluetooth_discovery *y, const char *remote, const char *local) {
|
|
|
|
|
pa_bluetooth_device *d;
|
|
|
|
|
void *state = NULL;
|
|
|
|
|
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
pa_assert(PA_REFCNT_VALUE(y) > 0);
|
|
|
|
|
pa_assert(remote);
|
|
|
|
|
pa_assert(local);
|
|
|
|
|
|
|
|
|
|
while ((d = pa_hashmap_iterate(y->devices, &state, NULL)))
|
|
|
|
|
if (pa_streq(d->address, remote) && pa_streq(d->adapter->address, local))
|
|
|
|
|
return d->device_info_valid == 1 ? d : NULL;
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void device_free(pa_bluetooth_device *d) {
|
2013-09-24 19:45:22 -03:00
|
|
|
unsigned i;
|
|
|
|
|
|
2013-09-18 16:16:58 -05:00
|
|
|
pa_assert(d);
|
|
|
|
|
|
2013-09-24 19:45:22 -03:00
|
|
|
for (i = 0; i < PA_BLUETOOTH_PROFILE_COUNT; i++) {
|
|
|
|
|
pa_bluetooth_transport *t;
|
|
|
|
|
|
|
|
|
|
if (!(t = d->transports[i]))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED);
|
|
|
|
|
pa_bluetooth_transport_free(t);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 16:16:58 -05:00
|
|
|
d->discovery = NULL;
|
|
|
|
|
d->adapter = NULL;
|
|
|
|
|
pa_xfree(d->path);
|
|
|
|
|
pa_xfree(d->alias);
|
|
|
|
|
pa_xfree(d->address);
|
|
|
|
|
pa_xfree(d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void device_remove(pa_bluetooth_discovery *y, const char *path) {
|
|
|
|
|
pa_bluetooth_device *d;
|
|
|
|
|
|
|
|
|
|
if (!(d = pa_hashmap_remove(y->devices, path)))
|
|
|
|
|
pa_log_warn("Unknown device removed %s", path);
|
|
|
|
|
else {
|
|
|
|
|
pa_log_debug("Device %s removed", path);
|
|
|
|
|
device_free(d);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void device_remove_all(pa_bluetooth_discovery *y) {
|
|
|
|
|
pa_bluetooth_device *d;
|
|
|
|
|
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
|
|
|
|
|
while ((d = pa_hashmap_steal_first(y->devices))) {
|
|
|
|
|
d->device_info_valid = -1;
|
|
|
|
|
pa_hook_fire(&y->hooks[PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED], d);
|
|
|
|
|
device_free(d);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 16:16:57 -05:00
|
|
|
static pa_bluetooth_adapter* adapter_create(pa_bluetooth_discovery *y, const char *path) {
|
|
|
|
|
pa_bluetooth_adapter *a;
|
|
|
|
|
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
pa_assert(path);
|
|
|
|
|
|
|
|
|
|
a = pa_xnew0(pa_bluetooth_adapter, 1);
|
|
|
|
|
a->discovery = y;
|
|
|
|
|
a->path = pa_xstrdup(path);
|
|
|
|
|
|
|
|
|
|
pa_hashmap_put(y->adapters, a->path, a);
|
|
|
|
|
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void adapter_remove_all(pa_bluetooth_discovery *y) {
|
|
|
|
|
pa_bluetooth_adapter *a;
|
|
|
|
|
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
|
2013-09-18 16:16:58 -05:00
|
|
|
/* When this function is called all devices have already been freed */
|
|
|
|
|
|
2013-09-18 16:16:57 -05:00
|
|
|
while ((a = pa_hashmap_steal_first(y->adapters))) {
|
|
|
|
|
pa_xfree(a->path);
|
|
|
|
|
pa_xfree(a->address);
|
|
|
|
|
pa_xfree(a);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 19:45:28 -03:00
|
|
|
static void get_managed_objects_reply(DBusPendingCall *pending, void *userdata) {
|
|
|
|
|
pa_dbus_pending *p;
|
|
|
|
|
pa_bluetooth_discovery *y;
|
|
|
|
|
DBusMessage *r;
|
|
|
|
|
DBusMessageIter arg_i, element_i;
|
|
|
|
|
|
|
|
|
|
pa_assert_se(p = userdata);
|
|
|
|
|
pa_assert_se(y = p->context_data);
|
|
|
|
|
pa_assert_se(r = dbus_pending_call_steal_reply(pending));
|
|
|
|
|
|
|
|
|
|
if (dbus_message_is_error(r, DBUS_ERROR_UNKNOWN_METHOD)) {
|
|
|
|
|
pa_log_warn("BlueZ D-Bus ObjectManager not available");
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) {
|
|
|
|
|
pa_log_error("GetManagedObjects() failed: %s: %s", dbus_message_get_error_name(r), pa_dbus_get_error_message(r));
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_iter_init(r, &arg_i) || !pa_streq(dbus_message_get_signature(r), "a{oa{sa{sv}}}")) {
|
|
|
|
|
pa_log_error("Invalid reply signature for GetManagedObjects()");
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&arg_i, &element_i);
|
|
|
|
|
while (dbus_message_iter_get_arg_type(&element_i) == DBUS_TYPE_DICT_ENTRY) {
|
|
|
|
|
DBusMessageIter dict_i;
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&element_i, &dict_i);
|
|
|
|
|
|
|
|
|
|
/* TODO: parse interfaces and properties */
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_next(&element_i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
y->objects_listed = true;
|
|
|
|
|
|
|
|
|
|
finish:
|
|
|
|
|
dbus_message_unref(r);
|
|
|
|
|
|
|
|
|
|
PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p);
|
|
|
|
|
pa_dbus_pending_free(p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void get_managed_objects(pa_bluetooth_discovery *y) {
|
|
|
|
|
DBusMessage *m;
|
|
|
|
|
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
|
|
|
|
|
pa_assert_se(m = dbus_message_new_method_call(BLUEZ_SERVICE, "/", "org.freedesktop.DBus.ObjectManager",
|
|
|
|
|
"GetManagedObjects"));
|
|
|
|
|
send_and_add_to_pending(y, m, get_managed_objects_reply, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 16:16:56 -05:00
|
|
|
pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y, pa_bluetooth_hook_t hook) {
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
pa_assert(PA_REFCNT_VALUE(y) > 0);
|
|
|
|
|
|
|
|
|
|
return &y->hooks[hook];
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 16:16:55 -05:00
|
|
|
static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) {
|
|
|
|
|
pa_bluetooth_discovery *y;
|
|
|
|
|
DBusError err;
|
|
|
|
|
|
|
|
|
|
pa_assert(bus);
|
|
|
|
|
pa_assert(m);
|
|
|
|
|
pa_assert_se(y = userdata);
|
|
|
|
|
|
|
|
|
|
dbus_error_init(&err);
|
|
|
|
|
|
|
|
|
|
if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) {
|
|
|
|
|
const char *name, *old_owner, *new_owner;
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_get_args(m, &err,
|
|
|
|
|
DBUS_TYPE_STRING, &name,
|
|
|
|
|
DBUS_TYPE_STRING, &old_owner,
|
|
|
|
|
DBUS_TYPE_STRING, &new_owner,
|
|
|
|
|
DBUS_TYPE_INVALID)) {
|
|
|
|
|
pa_log_error("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pa_streq(name, BLUEZ_SERVICE)) {
|
|
|
|
|
if (old_owner && *old_owner) {
|
|
|
|
|
pa_log_debug("Bluetooth daemon disappeared");
|
2013-09-24 19:45:21 -03:00
|
|
|
device_remove_all(y);
|
|
|
|
|
adapter_remove_all(y);
|
2013-09-24 19:45:28 -03:00
|
|
|
y->objects_listed = false;
|
2013-09-18 16:16:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (new_owner && *new_owner) {
|
|
|
|
|
pa_log_debug("Bluetooth daemon appeared");
|
2013-09-24 19:45:28 -03:00
|
|
|
get_managed_objects(y);
|
2013-09-18 16:16:55 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
dbus_error_free(&err);
|
|
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 19:45:25 -03:00
|
|
|
static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) {
|
|
|
|
|
/* These bitpool values were chosen based on the A2DP spec recommendation */
|
|
|
|
|
switch (freq) {
|
|
|
|
|
case SBC_SAMPLING_FREQ_16000:
|
|
|
|
|
case SBC_SAMPLING_FREQ_32000:
|
|
|
|
|
return 53;
|
|
|
|
|
|
|
|
|
|
case SBC_SAMPLING_FREQ_44100:
|
|
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case SBC_CHANNEL_MODE_MONO:
|
|
|
|
|
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
|
|
|
|
|
return 31;
|
|
|
|
|
|
|
|
|
|
case SBC_CHANNEL_MODE_STEREO:
|
|
|
|
|
case SBC_CHANNEL_MODE_JOINT_STEREO:
|
|
|
|
|
return 53;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_log_warn("Invalid channel mode %u", mode);
|
|
|
|
|
return 53;
|
|
|
|
|
|
|
|
|
|
case SBC_SAMPLING_FREQ_48000:
|
|
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
|
case SBC_CHANNEL_MODE_MONO:
|
|
|
|
|
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
|
|
|
|
|
return 29;
|
|
|
|
|
|
|
|
|
|
case SBC_CHANNEL_MODE_STEREO:
|
|
|
|
|
case SBC_CHANNEL_MODE_JOINT_STEREO:
|
|
|
|
|
return 51;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_log_warn("Invalid channel mode %u", mode);
|
|
|
|
|
return 51;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_log_warn("Invalid sampling freq %u", freq);
|
|
|
|
|
return 53;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 19:45:24 -03:00
|
|
|
const char *pa_bluetooth_profile_to_string(pa_bluetooth_profile_t profile) {
|
|
|
|
|
switch(profile) {
|
|
|
|
|
case PA_BLUETOOTH_PROFILE_A2DP_SINK:
|
|
|
|
|
return "a2dp_sink";
|
|
|
|
|
case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
|
|
|
|
|
return "a2dp_source";
|
|
|
|
|
case PA_BLUETOOTH_PROFILE_OFF:
|
|
|
|
|
return "off";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 19:45:23 -03:00
|
|
|
static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
|
2013-09-24 19:45:24 -03:00
|
|
|
pa_bluetooth_discovery *y = userdata;
|
|
|
|
|
pa_bluetooth_device *d;
|
|
|
|
|
pa_bluetooth_transport *t;
|
|
|
|
|
const char *sender, *path, *endpoint_path, *dev_path = NULL, *uuid = NULL;
|
|
|
|
|
const uint8_t *config = NULL;
|
|
|
|
|
int size = 0;
|
|
|
|
|
pa_bluetooth_profile_t p = PA_BLUETOOTH_PROFILE_OFF;
|
|
|
|
|
DBusMessageIter args, props;
|
2013-09-24 19:45:23 -03:00
|
|
|
DBusMessage *r;
|
|
|
|
|
|
2013-09-24 19:45:24 -03:00
|
|
|
if (!dbus_message_iter_init(m, &args) || !pa_streq(dbus_message_get_signature(m), "oa{sv}")) {
|
|
|
|
|
pa_log_error("Invalid signature for method SetConfiguration()");
|
|
|
|
|
goto fail2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&args, &path);
|
|
|
|
|
|
|
|
|
|
if (pa_hashmap_get(y->transports, path)) {
|
|
|
|
|
pa_log_error("Endpoint SetConfiguration(): Transport %s is already configured.", path);
|
|
|
|
|
goto fail2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_assert_se(dbus_message_iter_next(&args));
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&args, &props);
|
|
|
|
|
if (dbus_message_iter_get_arg_type(&props) != DBUS_TYPE_DICT_ENTRY)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
/* Read transport properties */
|
|
|
|
|
while (dbus_message_iter_get_arg_type(&props) == DBUS_TYPE_DICT_ENTRY) {
|
|
|
|
|
const char *key;
|
|
|
|
|
DBusMessageIter value, entry;
|
|
|
|
|
int var;
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&props, &entry);
|
|
|
|
|
dbus_message_iter_get_basic(&entry, &key);
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_next(&entry);
|
|
|
|
|
dbus_message_iter_recurse(&entry, &value);
|
|
|
|
|
|
|
|
|
|
var = dbus_message_iter_get_arg_type(&value);
|
|
|
|
|
|
|
|
|
|
if (pa_streq(key, "UUID")) {
|
|
|
|
|
if (var != DBUS_TYPE_STRING) {
|
|
|
|
|
pa_log_error("Property %s of wrong type %c", key, (char)var);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&value, &uuid);
|
|
|
|
|
|
|
|
|
|
endpoint_path = dbus_message_get_path(m);
|
|
|
|
|
if (pa_streq(endpoint_path, A2DP_SOURCE_ENDPOINT)) {
|
|
|
|
|
if (pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SOURCE))
|
|
|
|
|
p = PA_BLUETOOTH_PROFILE_A2DP_SINK;
|
|
|
|
|
} else if (pa_streq(endpoint_path, A2DP_SINK_ENDPOINT)) {
|
|
|
|
|
if (pa_streq(uuid, PA_BLUETOOTH_UUID_A2DP_SINK))
|
|
|
|
|
p = PA_BLUETOOTH_PROFILE_A2DP_SOURCE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (p == PA_BLUETOOTH_PROFILE_OFF) {
|
|
|
|
|
pa_log_error("UUID %s of transport %s incompatible with endpoint %s", uuid, path, endpoint_path);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
} else if (pa_streq(key, "Device")) {
|
|
|
|
|
if (var != DBUS_TYPE_OBJECT_PATH) {
|
|
|
|
|
pa_log_error("Property %s of wrong type %c", key, (char)var);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_get_basic(&value, &dev_path);
|
|
|
|
|
} else if (pa_streq(key, "Configuration")) {
|
|
|
|
|
DBusMessageIter array;
|
|
|
|
|
a2dp_sbc_t *c;
|
|
|
|
|
|
|
|
|
|
if (var != DBUS_TYPE_ARRAY) {
|
|
|
|
|
pa_log_error("Property %s of wrong type %c", key, (char)var);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&value, &array);
|
|
|
|
|
var = dbus_message_iter_get_arg_type(&array);
|
|
|
|
|
if (var != DBUS_TYPE_BYTE) {
|
|
|
|
|
pa_log_error("%s is an array of wrong type %c", key, (char)var);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_get_fixed_array(&array, &config, &size);
|
|
|
|
|
if (size != sizeof(a2dp_sbc_t)) {
|
|
|
|
|
pa_log_error("Configuration array of invalid size");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c = (a2dp_sbc_t *) config;
|
|
|
|
|
|
|
|
|
|
if (c->frequency != SBC_SAMPLING_FREQ_16000 && c->frequency != SBC_SAMPLING_FREQ_32000 &&
|
|
|
|
|
c->frequency != SBC_SAMPLING_FREQ_44100 && c->frequency != SBC_SAMPLING_FREQ_48000) {
|
|
|
|
|
pa_log_error("Invalid sampling frequency in configuration");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->channel_mode != SBC_CHANNEL_MODE_MONO && c->channel_mode != SBC_CHANNEL_MODE_DUAL_CHANNEL &&
|
|
|
|
|
c->channel_mode != SBC_CHANNEL_MODE_STEREO && c->channel_mode != SBC_CHANNEL_MODE_JOINT_STEREO) {
|
|
|
|
|
pa_log_error("Invalid channel mode in configuration");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->allocation_method != SBC_ALLOCATION_SNR && c->allocation_method != SBC_ALLOCATION_LOUDNESS) {
|
|
|
|
|
pa_log_error("Invalid allocation method in configuration");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->subbands != SBC_SUBBANDS_4 && c->subbands != SBC_SUBBANDS_8) {
|
|
|
|
|
pa_log_error("Invalid SBC subbands in configuration");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->block_length != SBC_BLOCK_LENGTH_4 && c->block_length != SBC_BLOCK_LENGTH_8 &&
|
|
|
|
|
c->block_length != SBC_BLOCK_LENGTH_12 && c->block_length != SBC_BLOCK_LENGTH_16) {
|
|
|
|
|
pa_log_error("Invalid block length in configuration");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_next(&props);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((d = pa_hashmap_get(y->devices, dev_path))) {
|
|
|
|
|
if (d->device_info_valid == -1) {
|
|
|
|
|
pa_log_error("Information about device %s is invalid", dev_path);
|
|
|
|
|
goto fail2;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* InterfacesAdded signal is probably on it's way, device_info_valid is kept as 0. */
|
|
|
|
|
pa_log_warn("SetConfiguration() received for unknown device %s", dev_path);
|
|
|
|
|
d = device_create(y, dev_path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (d->transports[p] != NULL) {
|
|
|
|
|
pa_log_error("Cannot configure transport %s because profile %s is already used", path, pa_bluetooth_profile_to_string(p));
|
|
|
|
|
goto fail2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sender = dbus_message_get_sender(m);
|
|
|
|
|
|
|
|
|
|
pa_assert_se(r = dbus_message_new_method_return(m));
|
|
|
|
|
pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
|
|
|
|
|
dbus_message_unref(r);
|
|
|
|
|
|
|
|
|
|
d->transports[p] = t = pa_bluetooth_transport_new(d, sender, path, p, config, size);
|
|
|
|
|
t->acquire = bluez5_transport_acquire_cb;
|
|
|
|
|
t->release = bluez5_transport_release_cb;
|
|
|
|
|
pa_bluetooth_transport_put(t);
|
|
|
|
|
|
|
|
|
|
pa_log_debug("Transport %s available for profile %s", t->path, pa_bluetooth_profile_to_string(t->profile));
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
pa_log_error("Endpoint SetConfiguration(): invalid arguments");
|
2013-09-24 19:45:23 -03:00
|
|
|
|
2013-09-24 19:45:24 -03:00
|
|
|
fail2:
|
|
|
|
|
pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to set configuration"));
|
2013-09-24 19:45:23 -03:00
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DBusMessage *endpoint_select_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
|
2013-09-24 19:45:25 -03:00
|
|
|
pa_bluetooth_discovery *y = userdata;
|
|
|
|
|
a2dp_sbc_t *cap, config;
|
|
|
|
|
uint8_t *pconf = (uint8_t *) &config;
|
|
|
|
|
int i, size;
|
2013-09-24 19:45:23 -03:00
|
|
|
DBusMessage *r;
|
2013-09-24 19:45:25 -03:00
|
|
|
DBusError err;
|
2013-09-24 19:45:23 -03:00
|
|
|
|
2013-09-24 19:45:25 -03:00
|
|
|
static const struct {
|
|
|
|
|
uint32_t rate;
|
|
|
|
|
uint8_t cap;
|
|
|
|
|
} freq_table[] = {
|
|
|
|
|
{ 16000U, SBC_SAMPLING_FREQ_16000 },
|
|
|
|
|
{ 32000U, SBC_SAMPLING_FREQ_32000 },
|
|
|
|
|
{ 44100U, SBC_SAMPLING_FREQ_44100 },
|
|
|
|
|
{ 48000U, SBC_SAMPLING_FREQ_48000 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dbus_error_init(&err);
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_get_args(m, &err, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &cap, &size, DBUS_TYPE_INVALID)) {
|
|
|
|
|
pa_log_error("Endpoint SelectConfiguration(): %s", err.message);
|
|
|
|
|
dbus_error_free(&err);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (size != sizeof(config)) {
|
|
|
|
|
pa_log_error("Capabilities array has invalid size");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_zero(config);
|
|
|
|
|
|
|
|
|
|
/* Find the lowest freq that is at least as high as the requested sampling rate */
|
|
|
|
|
for (i = 0; (unsigned) i < PA_ELEMENTSOF(freq_table); i++)
|
|
|
|
|
if (freq_table[i].rate >= y->core->default_sample_spec.rate && (cap->frequency & freq_table[i].cap)) {
|
|
|
|
|
config.frequency = freq_table[i].cap;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((unsigned) i == PA_ELEMENTSOF(freq_table)) {
|
|
|
|
|
for (--i; i >= 0; i--) {
|
|
|
|
|
if (cap->frequency & freq_table[i].cap) {
|
|
|
|
|
config.frequency = freq_table[i].cap;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-24 19:45:23 -03:00
|
|
|
|
2013-09-24 19:45:25 -03:00
|
|
|
if (i < 0) {
|
|
|
|
|
pa_log_error("Not suitable sample rate");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table));
|
|
|
|
|
|
|
|
|
|
if (y->core->default_sample_spec.channels <= 1) {
|
|
|
|
|
if (cap->channel_mode & SBC_CHANNEL_MODE_MONO)
|
|
|
|
|
config.channel_mode = SBC_CHANNEL_MODE_MONO;
|
|
|
|
|
else if (cap->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
|
|
|
|
|
config.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
|
|
|
|
|
else if (cap->channel_mode & SBC_CHANNEL_MODE_STEREO)
|
|
|
|
|
config.channel_mode = SBC_CHANNEL_MODE_STEREO;
|
|
|
|
|
else if (cap->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
|
|
|
|
|
config.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
|
|
|
|
|
else {
|
|
|
|
|
pa_log_error("No supported channel modes");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (y->core->default_sample_spec.channels >= 2) {
|
|
|
|
|
if (cap->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
|
|
|
|
|
config.channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
|
|
|
|
|
else if (cap->channel_mode & SBC_CHANNEL_MODE_STEREO)
|
|
|
|
|
config.channel_mode = SBC_CHANNEL_MODE_STEREO;
|
|
|
|
|
else if (cap->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
|
|
|
|
|
config.channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
|
|
|
|
|
else if (cap->channel_mode & SBC_CHANNEL_MODE_MONO)
|
|
|
|
|
config.channel_mode = SBC_CHANNEL_MODE_MONO;
|
|
|
|
|
else {
|
|
|
|
|
pa_log_error("No supported channel modes");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cap->block_length & SBC_BLOCK_LENGTH_16)
|
|
|
|
|
config.block_length = SBC_BLOCK_LENGTH_16;
|
|
|
|
|
else if (cap->block_length & SBC_BLOCK_LENGTH_12)
|
|
|
|
|
config.block_length = SBC_BLOCK_LENGTH_12;
|
|
|
|
|
else if (cap->block_length & SBC_BLOCK_LENGTH_8)
|
|
|
|
|
config.block_length = SBC_BLOCK_LENGTH_8;
|
|
|
|
|
else if (cap->block_length & SBC_BLOCK_LENGTH_4)
|
|
|
|
|
config.block_length = SBC_BLOCK_LENGTH_4;
|
|
|
|
|
else {
|
|
|
|
|
pa_log_error("No supported block lengths");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cap->subbands & SBC_SUBBANDS_8)
|
|
|
|
|
config.subbands = SBC_SUBBANDS_8;
|
|
|
|
|
else if (cap->subbands & SBC_SUBBANDS_4)
|
|
|
|
|
config.subbands = SBC_SUBBANDS_4;
|
|
|
|
|
else {
|
|
|
|
|
pa_log_error("No supported subbands");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cap->allocation_method & SBC_ALLOCATION_LOUDNESS)
|
|
|
|
|
config.allocation_method = SBC_ALLOCATION_LOUDNESS;
|
|
|
|
|
else if (cap->allocation_method & SBC_ALLOCATION_SNR)
|
|
|
|
|
config.allocation_method = SBC_ALLOCATION_SNR;
|
|
|
|
|
|
|
|
|
|
config.min_bitpool = (uint8_t) PA_MAX(MIN_BITPOOL, cap->min_bitpool);
|
|
|
|
|
config.max_bitpool = (uint8_t) PA_MIN(a2dp_default_bitpool(config.frequency, config.channel_mode), cap->max_bitpool);
|
|
|
|
|
|
|
|
|
|
if (config.min_bitpool > config.max_bitpool)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
pa_assert_se(r = dbus_message_new_method_return(m));
|
|
|
|
|
pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &pconf, size, DBUS_TYPE_INVALID));
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to select configuration"));
|
2013-09-24 19:45:23 -03:00
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DBusMessage *endpoint_clear_configuration(DBusConnection *conn, DBusMessage *m, void *userdata) {
|
2013-09-24 19:45:26 -03:00
|
|
|
pa_bluetooth_discovery *y = userdata;
|
|
|
|
|
pa_bluetooth_transport *t;
|
2013-09-24 19:45:23 -03:00
|
|
|
DBusMessage *r;
|
2013-09-24 19:45:26 -03:00
|
|
|
DBusError err;
|
|
|
|
|
const char *path;
|
2013-09-24 19:45:23 -03:00
|
|
|
|
2013-09-24 19:45:26 -03:00
|
|
|
dbus_error_init(&err);
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_get_args(m, &err, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
|
|
|
|
|
pa_log_error("Endpoint ClearConfiguration(): %s", err.message);
|
|
|
|
|
dbus_error_free(&err);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2013-09-24 19:45:23 -03:00
|
|
|
|
2013-09-24 19:45:26 -03:00
|
|
|
if ((t = pa_hashmap_get(y->transports, path))) {
|
|
|
|
|
pa_log_debug("Clearing transport %s profile %s", t->path, pa_bluetooth_profile_to_string(t->profile));
|
|
|
|
|
transport_state_changed(t, PA_BLUETOOTH_TRANSPORT_STATE_DISCONNECTED);
|
|
|
|
|
pa_bluetooth_transport_free(t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_assert_se(r = dbus_message_new_method_return(m));
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
pa_assert_se(r = dbus_message_new_error(m, "org.bluez.Error.InvalidArguments", "Unable to clear configuration"));
|
2013-09-24 19:45:23 -03:00
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DBusMessage *endpoint_release(DBusConnection *conn, DBusMessage *m, void *userdata) {
|
|
|
|
|
DBusMessage *r;
|
|
|
|
|
|
|
|
|
|
pa_assert_se(r = dbus_message_new_error(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE ".Error.NotImplemented",
|
|
|
|
|
"Method not implemented"));
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DBusHandlerResult endpoint_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
|
|
|
|
|
struct pa_bluetooth_discovery *y = userdata;
|
|
|
|
|
DBusMessage *r = NULL;
|
|
|
|
|
const char *path, *interface, *member;
|
|
|
|
|
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
|
|
|
|
|
path = dbus_message_get_path(m);
|
|
|
|
|
interface = dbus_message_get_interface(m);
|
|
|
|
|
member = dbus_message_get_member(m);
|
|
|
|
|
|
|
|
|
|
pa_log_debug("dbus: path=%s, interface=%s, member=%s", path, interface, member);
|
|
|
|
|
|
|
|
|
|
if (!pa_streq(path, A2DP_SOURCE_ENDPOINT) && !pa_streq(path, A2DP_SINK_ENDPOINT))
|
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
|
|
|
|
|
|
if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
|
|
|
|
|
const char *xml = ENDPOINT_INTROSPECT_XML;
|
|
|
|
|
|
|
|
|
|
pa_assert_se(r = dbus_message_new_method_return(m));
|
|
|
|
|
pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID));
|
|
|
|
|
|
|
|
|
|
} else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "SetConfiguration"))
|
|
|
|
|
r = endpoint_set_configuration(c, m, userdata);
|
|
|
|
|
else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "SelectConfiguration"))
|
|
|
|
|
r = endpoint_select_configuration(c, m, userdata);
|
|
|
|
|
else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "ClearConfiguration"))
|
|
|
|
|
r = endpoint_clear_configuration(c, m, userdata);
|
|
|
|
|
else if (dbus_message_is_method_call(m, BLUEZ_MEDIA_ENDPOINT_INTERFACE, "Release"))
|
|
|
|
|
r = endpoint_release(c, m, userdata);
|
|
|
|
|
else
|
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
|
|
|
|
|
|
if (r) {
|
|
|
|
|
pa_assert_se(dbus_connection_send(pa_dbus_connection_get(y->connection), r, NULL));
|
|
|
|
|
dbus_message_unref(r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void endpoint_init(pa_bluetooth_discovery *y, pa_bluetooth_profile_t profile) {
|
|
|
|
|
static const DBusObjectPathVTable vtable_endpoint = {
|
|
|
|
|
.message_function = endpoint_handler,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
|
|
|
|
|
switch(profile) {
|
|
|
|
|
case PA_BLUETOOTH_PROFILE_A2DP_SINK:
|
|
|
|
|
pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT,
|
|
|
|
|
&vtable_endpoint, y));
|
|
|
|
|
break;
|
|
|
|
|
case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
|
|
|
|
|
pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT,
|
|
|
|
|
&vtable_endpoint, y));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pa_assert_not_reached();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void endpoint_done(pa_bluetooth_discovery *y, pa_bluetooth_profile_t profile) {
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
|
|
|
|
|
switch(profile) {
|
|
|
|
|
case PA_BLUETOOTH_PROFILE_A2DP_SINK:
|
|
|
|
|
dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SOURCE_ENDPOINT);
|
|
|
|
|
break;
|
|
|
|
|
case PA_BLUETOOTH_PROFILE_A2DP_SOURCE:
|
|
|
|
|
dbus_connection_unregister_object_path(pa_dbus_connection_get(y->connection), A2DP_SINK_ENDPOINT);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
pa_assert_not_reached();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 01:54:00 -03:00
|
|
|
pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
|
|
|
|
|
pa_bluetooth_discovery *y;
|
2013-09-18 16:16:55 -05:00
|
|
|
DBusError err;
|
|
|
|
|
DBusConnection *conn;
|
2013-09-18 16:16:56 -05:00
|
|
|
unsigned i;
|
2013-08-13 01:54:00 -03:00
|
|
|
|
|
|
|
|
if ((y = pa_shared_get(c, "bluetooth-discovery")))
|
|
|
|
|
return pa_bluetooth_discovery_ref(y);
|
|
|
|
|
|
|
|
|
|
y = pa_xnew0(pa_bluetooth_discovery, 1);
|
|
|
|
|
PA_REFCNT_INIT(y);
|
|
|
|
|
y->core = c;
|
2013-09-18 16:16:57 -05:00
|
|
|
y->adapters = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
|
2013-09-18 16:16:58 -05:00
|
|
|
y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
|
2013-09-24 19:45:22 -03:00
|
|
|
y->transports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
|
2013-09-24 19:45:27 -03:00
|
|
|
PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending);
|
2013-08-13 01:54:00 -03:00
|
|
|
|
2013-09-18 16:16:56 -05:00
|
|
|
for (i = 0; i < PA_BLUETOOTH_HOOK_MAX; i++)
|
|
|
|
|
pa_hook_init(&y->hooks[i], y);
|
|
|
|
|
|
2013-08-13 01:54:00 -03:00
|
|
|
pa_shared_set(c, "bluetooth-discovery", y);
|
|
|
|
|
|
2013-09-18 16:16:55 -05:00
|
|
|
dbus_error_init(&err);
|
|
|
|
|
|
|
|
|
|
if (!(y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err))) {
|
|
|
|
|
pa_log_error("Failed to get D-Bus connection: %s", err.message);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn = pa_dbus_connection_get(y->connection);
|
|
|
|
|
|
|
|
|
|
/* dynamic detection of bluetooth audio devices */
|
|
|
|
|
if (!dbus_connection_add_filter(conn, filter_cb, y, NULL)) {
|
|
|
|
|
pa_log_error("Failed to add filter function");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
y->filter_added = true;
|
|
|
|
|
|
|
|
|
|
if (pa_dbus_add_matches(conn, &err,
|
|
|
|
|
"type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'"
|
|
|
|
|
",arg0='" BLUEZ_SERVICE "'",
|
|
|
|
|
NULL) < 0) {
|
|
|
|
|
pa_log_error("Failed to add D-Bus matches: %s", err.message);
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
y->matches_added = true;
|
|
|
|
|
|
2013-09-24 19:45:23 -03:00
|
|
|
endpoint_init(y, PA_BLUETOOTH_PROFILE_A2DP_SINK);
|
|
|
|
|
endpoint_init(y, PA_BLUETOOTH_PROFILE_A2DP_SOURCE);
|
|
|
|
|
|
2013-09-24 19:45:28 -03:00
|
|
|
get_managed_objects(y);
|
|
|
|
|
|
2013-08-13 01:54:00 -03:00
|
|
|
return y;
|
2013-09-18 16:16:55 -05:00
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
pa_bluetooth_discovery_unref(y);
|
|
|
|
|
dbus_error_free(&err);
|
|
|
|
|
|
|
|
|
|
return NULL;
|
2013-08-13 01:54:00 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) {
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
pa_assert(PA_REFCNT_VALUE(y) > 0);
|
|
|
|
|
|
|
|
|
|
PA_REFCNT_INC(y);
|
|
|
|
|
|
|
|
|
|
return y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
|
|
|
|
|
pa_assert(y);
|
|
|
|
|
pa_assert(PA_REFCNT_VALUE(y) > 0);
|
|
|
|
|
|
|
|
|
|
if (PA_REFCNT_DEC(y) > 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-09-24 19:45:27 -03:00
|
|
|
pa_dbus_free_pending_list(&y->pending);
|
|
|
|
|
|
2013-09-18 16:16:58 -05:00
|
|
|
if (y->devices) {
|
|
|
|
|
device_remove_all(y);
|
|
|
|
|
pa_hashmap_free(y->devices);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 16:16:57 -05:00
|
|
|
if (y->adapters) {
|
|
|
|
|
adapter_remove_all(y);
|
|
|
|
|
pa_hashmap_free(y->adapters);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 19:45:22 -03:00
|
|
|
if (y->transports) {
|
|
|
|
|
pa_assert(pa_hashmap_isempty(y->transports));
|
|
|
|
|
pa_hashmap_free(y->transports);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-18 16:16:55 -05:00
|
|
|
if (y->connection) {
|
|
|
|
|
|
|
|
|
|
if (y->matches_added)
|
|
|
|
|
pa_dbus_remove_matches(pa_dbus_connection_get(y->connection),
|
|
|
|
|
"type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',"
|
|
|
|
|
"arg0='" BLUEZ_SERVICE "'",
|
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
if (y->filter_added)
|
|
|
|
|
dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y);
|
|
|
|
|
|
2013-09-24 19:45:23 -03:00
|
|
|
endpoint_done(y, PA_BLUETOOTH_PROFILE_A2DP_SINK);
|
|
|
|
|
endpoint_done(y, PA_BLUETOOTH_PROFILE_A2DP_SOURCE);
|
|
|
|
|
|
2013-09-18 16:16:55 -05:00
|
|
|
pa_dbus_connection_unref(y->connection);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 01:54:00 -03:00
|
|
|
pa_shared_remove(y->core, "bluetooth-discovery");
|
|
|
|
|
pa_xfree(y);
|
|
|
|
|
}
|