2012-09-11 13:55:26

by Mikel Astiz

[permalink] [raw]
Subject: [PATCH v0 0/4] Audio profile authorization

From: Mikel Astiz <[email protected]>

The agent-based profile authorization makes a special consideration for audio profiles: they are processed as if they all belonged to one single profile. This includes several internal policies that are inconvenient for IVI use-cases.

This patchset removes this exception by making use of the conventional authorization mechanism also for audio profiles.

The new approach is not straightforward since devices might send several connection requets in parallel (i.e. HFP, A2DP, AVRCP). This cannot be neither automatically rejected (EBUSY) nor forwarded in parallel to the agent, so a queue was added to store the pending authorization requests. These will be sent to the agent sequentially.

Mikel Astiz (4):
adapter: Replace device authorizing flag
adapter: Use authorization id for cancelling
adapter: Queue parallel authorization requests
audio: Drop audio-specific authorization mechanism

audio/avctp.c | 29 +++----
audio/avdtp.c | 25 ++++--
audio/device.c | 144 ------------------------------------
audio/device.h | 12 +---
audio/manager.c | 18 +++--
plugins/service.c | 18 +++--
profiles/input/server.c | 2 +-
src/adapter.c | 188 +++++++++++++++++++++++++++++------------------
src/adapter.h | 2 +-
src/device.c | 11 ---
src/device.h | 2 -
11 files changed, 170 insertions(+), 281 deletions(-)

--
1.7.7.6



2012-09-20 12:20:24

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH v0 1/4] adapter: Replace device authorizing flag

Hi Mikel,

On Tue, Sep 11, 2012 at 4:55 PM, Mikel Astiz <[email protected]> wrote:
> From: Mikel Astiz <[email protected]>
>
> Refactor code to drop the device authorizing flag by replacing it with a
> private authorization pointer in btd_adapter. After all, no more than
> one authorization can be ongoing, so the code is easier to follow if
> this is made explicit.
> ---
> src/adapter.c | 48 ++++++++++++++++++++++++++++++++----------------
> src/device.c | 11 -----------
> src/device.h | 2 --
> 3 files changed, 32 insertions(+), 29 deletions(-)
>
> diff --git a/src/adapter.c b/src/adapter.c
> index 4b675e8..14c5322 100644
> --- a/src/adapter.c
> +++ b/src/adapter.c
> @@ -128,7 +128,8 @@ struct btd_adapter {
> GSList *found_devices;
> GSList *oor_devices; /* out of range device list */
> struct agent *agent; /* For the new API */
> - guint auth_idle_id; /* Ongoing authorization */
> + guint auth_idle_id; /* Ongoing authorization (trusted) */
> + struct service_auth *auth; /* Ongoing authorization */

You should probably store the id directly inside struct service_auth
as they depend on each other then when you free auth you also remove
the id if not set to 0.

--
Luiz Augusto von Dentz

2012-09-11 13:55:27

by Mikel Astiz

[permalink] [raw]
Subject: [PATCH v0 1/4] adapter: Replace device authorizing flag

From: Mikel Astiz <[email protected]>

Refactor code to drop the device authorizing flag by replacing it with a
private authorization pointer in btd_adapter. After all, no more than
one authorization can be ongoing, so the code is easier to follow if
this is made explicit.
---
src/adapter.c | 48 ++++++++++++++++++++++++++++++++----------------
src/device.c | 11 -----------
src/device.h | 2 --
3 files changed, 32 insertions(+), 29 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 4b675e8..14c5322 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -128,7 +128,8 @@ struct btd_adapter {
GSList *found_devices;
GSList *oor_devices; /* out of range device list */
struct agent *agent; /* For the new API */
- guint auth_idle_id; /* Ongoing authorization */
+ guint auth_idle_id; /* Ongoing authorization (trusted) */
+ struct service_auth *auth; /* Ongoing authorization */
GSList *connections; /* Connected devices */
GSList *devices; /* Devices structure pointers */
GSList *mode_sessions; /* Request Mode sessions */
@@ -977,8 +978,10 @@ void adapter_remove_device(DBusConnection *conn, struct btd_adapter *adapter,

agent = device_get_agent(device);

- if (agent && device_is_authorizing(device))
+ if (agent && adapter->auth && adapter->auth->device == device) {
+ adapter->auth = NULL;
agent_cancel(agent);
+ }

device_remove(device, remove_storage);
}
@@ -2411,6 +2414,9 @@ static void adapter_free(gpointer user_data)
if (adapter->auth_idle_id)
g_source_remove(adapter->auth_idle_id);

+ if (adapter->auth)
+ g_free(adapter->auth);
+
if (adapter->off_timer)
off_timer_remove(adapter);

@@ -3147,18 +3153,22 @@ void btd_unregister_adapter_driver(struct btd_adapter_driver *driver)
static void agent_auth_cb(struct agent *agent, DBusError *derr,
void *user_data)
{
- struct service_auth *auth = user_data;
+ struct btd_adapter *adapter = user_data;
+ struct service_auth *auth = adapter->auth;

- device_set_authorizing(auth->device, FALSE);
+ adapter->auth = NULL;

auth->cb(derr, auth->user_data);
+
+ g_free(auth);
}

static gboolean auth_idle_cb(gpointer user_data)
{
- struct service_auth *auth = user_data;
- struct btd_adapter *adapter = auth->adapter;
+ struct btd_adapter *adapter = user_data;
+ struct service_auth *auth = adapter->auth;

+ adapter->auth = NULL;
adapter->auth_idle_id = 0;

auth->cb(NULL, auth->user_data);
@@ -3186,7 +3196,7 @@ static int adapter_authorize(struct btd_adapter *adapter, const bdaddr_t *dst,
if (!g_slist_find(adapter->connections, device))
error("Authorization request for non-connected device!?");

- if (adapter->auth_idle_id)
+ if (adapter->auth)
return -EBUSY;

auth = g_try_new0(struct service_auth, 1);
@@ -3200,9 +3210,9 @@ static int adapter_authorize(struct btd_adapter *adapter, const bdaddr_t *dst,

if (device_is_trusted(device) == TRUE) {
adapter->auth_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
- auth_idle_cb, auth,
- g_free);
- return 0;
+ auth_idle_cb, adapter,
+ NULL);
+ goto done;
}

agent = device_get_agent(device);
@@ -3214,13 +3224,16 @@ static int adapter_authorize(struct btd_adapter *adapter, const bdaddr_t *dst,

dev_path = device_get_path(device);

- err = agent_authorize(agent, dev_path, uuid, agent_auth_cb, auth, g_free);
- if (err < 0)
+ err = agent_authorize(agent, dev_path, uuid, agent_auth_cb, adapter,
+ NULL);
+ if (err < 0) {
g_free(auth);
- else
- device_set_authorizing(device, TRUE);
+ return err;
+ }

- return err;
+done:
+ adapter->auth = auth;
+ return 0;
}

int btd_request_authorization(const bdaddr_t *src, const bdaddr_t *dst,
@@ -3273,6 +3286,9 @@ int btd_cancel_authorization(const bdaddr_t *src, const bdaddr_t *dst)
return 0;
}

+ if (!adapter->auth || adapter->auth->device != device)
+ return -EPERM;
+
/*
* FIXME: Cancel fails if authorization is requested to adapter's
* agent and in the meanwhile CreatePairedDevice is called.
@@ -3285,7 +3301,7 @@ int btd_cancel_authorization(const bdaddr_t *src, const bdaddr_t *dst)
err = agent_cancel(agent);

if (err == 0)
- device_set_authorizing(device, FALSE);
+ adapter->auth = NULL;

return err;
}
diff --git a/src/device.c b/src/device.c
index 02ef35e..fd0b7f9 100644
--- a/src/device.c
+++ b/src/device.c
@@ -164,7 +164,6 @@ struct btd_device {
gboolean bonded;
gboolean auto_connect;

- gboolean authorizing;
gint ref;

GIOChannel *att_io;
@@ -2963,16 +2962,6 @@ gboolean device_is_authenticating(struct btd_device *device)
return (device->authr != NULL);
}

-gboolean device_is_authorizing(struct btd_device *device)
-{
- return device->authorizing;
-}
-
-void device_set_authorizing(struct btd_device *device, gboolean auth)
-{
- device->authorizing = auth;
-}
-
void device_register_services(DBusConnection *conn, struct btd_device *device,
GSList *prim_list, int psm)
{
diff --git a/src/device.h b/src/device.h
index f1d95c6..cb3a982 100644
--- a/src/device.h
+++ b/src/device.h
@@ -131,8 +131,6 @@ int device_notify_pincode(struct btd_device *device, gboolean secure,
const char *pincode, void *cb);
void device_cancel_authentication(struct btd_device *device, gboolean aborted);
gboolean device_is_authenticating(struct btd_device *device);
-gboolean device_is_authorizing(struct btd_device *device);
-void device_set_authorizing(struct btd_device *device, gboolean auth);
void device_add_connection(struct btd_device *device, DBusConnection *conn);
void device_remove_connection(struct btd_device *device, DBusConnection *conn);
void device_request_disconnect(struct btd_device *device, DBusMessage *msg);
--
1.7.7.6


2012-09-11 13:55:28

by Mikel Astiz

[permalink] [raw]
Subject: [PATCH v0 2/4] adapter: Use authorization id for cancelling

From: Mikel Astiz <[email protected]>

Return a request id in btd_request_authorization() in order to be used
when the request needs to be cancelled. This id alone will be enough to
use btd_cancel_authorization().
---
audio/device.c | 12 +++++++--
plugins/service.c | 18 ++++++++-----
profiles/input/server.c | 2 +-
src/adapter.c | 62 +++++++++++++++++++++++++++++++----------------
src/adapter.h | 2 +-
5 files changed, 63 insertions(+), 33 deletions(-)

diff --git a/audio/device.c b/audio/device.c
index 4952b57..ca64f14 100644
--- a/audio/device.c
+++ b/audio/device.c
@@ -83,6 +83,7 @@ struct dev_priv {
sink_state_t sink_state;
avctp_state_t avctp_state;
GSList *auths;
+ int auth_id;

DBusMessage *conn_req;
DBusMessage *dc_req;
@@ -741,6 +742,8 @@ static void auth_cb(DBusError *derr, void *user_data)
struct audio_device *dev = user_data;
struct dev_priv *priv = dev->priv;

+ priv->auth_id = 0;
+
if (derr == NULL)
priv->authorized = TRUE;

@@ -824,7 +827,8 @@ int audio_device_request_authorization(struct audio_device *dev,
if (err < 0) {
priv->auths = g_slist_remove(priv->auths, auth);
g_free(auth);
- }
+ } else
+ priv->auth_id = err;

return err;
}
@@ -854,8 +858,10 @@ int audio_device_cancel_authorization(struct audio_device *dev,
if (priv->auth_idle_id > 0) {
g_source_remove(priv->auth_idle_id);
priv->auth_idle_id = 0;
- } else
- btd_cancel_authorization(&dev->src, &dev->dst);
+ } else {
+ btd_cancel_authorization(priv->auth_id);
+ priv->auth_id = 0;
+ }
}

return 0;
diff --git a/plugins/service.c b/plugins/service.c
index f16abe7..506a0cd 100644
--- a/plugins/service.c
+++ b/plugins/service.c
@@ -67,6 +67,7 @@ struct pending_auth {
char *sender;
bdaddr_t dst;
char uuid[MAX_LEN_UUID_STR];
+ int auth_id;
};

struct service_adapter {
@@ -560,8 +561,9 @@ done:
else
bacpy(&src, BDADDR_ANY);

- btd_request_authorization(&src, &auth->dst,
- auth->uuid, auth_cb, serv_adapter);
+ auth->auth_id = btd_request_authorization(&src, &auth->dst,
+ auth->uuid, auth_cb,
+ serv_adapter);
}

static DBusMessage *request_authorization(DBusConnection *conn,
@@ -637,8 +639,9 @@ static DBusMessage *request_authorization(DBusConnection *conn,
else
bacpy(&src, BDADDR_ANY);

- if (btd_request_authorization(&src, &auth->dst, auth->uuid, auth_cb,
- serv_adapter) < 0) {
+ auth->auth_id = btd_request_authorization(&src, &auth->dst, auth->uuid,
+ auth_cb, serv_adapter);
+ if (auth->auth_id < 0) {
serv_adapter->pending_list = g_slist_remove(serv_adapter->pending_list,
auth);
g_free(auth);
@@ -668,7 +671,7 @@ static DBusMessage *cancel_authorization(DBusConnection *conn,
else
bacpy(&src, BDADDR_ANY);

- btd_cancel_authorization(&src, &auth->dst);
+ btd_cancel_authorization(auth->auth_id);

reply = btd_error_not_authorized(auth->msg);
dbus_message_unref(auth->msg);
@@ -689,8 +692,9 @@ static DBusMessage *cancel_authorization(DBusConnection *conn,
else
bacpy(&src, BDADDR_ANY);

- btd_request_authorization(&src, &auth->dst,
- auth->uuid, auth_cb, serv_adapter);
+ auth->auth_id = btd_request_authorization(&src, &auth->dst,
+ auth->uuid, auth_cb,
+ serv_adapter);

done:
return dbus_message_new_method_return(msg);
diff --git a/profiles/input/server.c b/profiles/input/server.c
index 0464c4a..0385012 100644
--- a/profiles/input/server.c
+++ b/profiles/input/server.c
@@ -175,7 +175,7 @@ static void confirm_event_cb(GIOChannel *chan, gpointer user_data)

ret = btd_request_authorization(&src, &dst, HID_UUID,
auth_callback, server);
- if (ret == 0)
+ if (ret >= 0)
return;

ba2str(&src, addr);
diff --git a/src/adapter.c b/src/adapter.c
index 14c5322..de5d1c9 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -85,6 +85,8 @@

static DBusConnection *connection = NULL;
static GSList *adapter_drivers = NULL;
+static int service_auth_id = 0;
+static GHashTable *service_auths = NULL;

struct session_req {
struct btd_adapter *adapter;
@@ -98,6 +100,7 @@ struct session_req {
};

struct service_auth {
+ int id;
service_auth_cb cb;
void *user_data;
struct btd_device *device;
@@ -208,6 +211,15 @@ static uint8_t get_mode(const bdaddr_t *bdaddr, const char *mode)
return MODE_UNKNOWN;
}

+static void service_auth_free(gpointer data)
+{
+ struct service_auth *auth = data;
+
+ g_hash_table_remove(service_auths, GUINT_TO_POINTER(auth->id));
+
+ g_free(auth);
+}
+
static struct session_req *session_ref(struct session_req *req)
{
req->refcount++;
@@ -2415,7 +2427,7 @@ static void adapter_free(gpointer user_data)
g_source_remove(adapter->auth_idle_id);

if (adapter->auth)
- g_free(adapter->auth);
+ service_auth_free(adapter->auth);

if (adapter->off_timer)
off_timer_remove(adapter);
@@ -2429,6 +2441,8 @@ static void adapter_free(gpointer user_data)
g_free(adapter->path);
g_free(adapter->name);
g_free(adapter);
+
+ g_hash_table_unref(service_auths);
}

struct btd_adapter *btd_adapter_ref(struct btd_adapter *adapter)
@@ -2523,6 +2537,11 @@ struct btd_adapter *adapter_create(DBusConnection *conn, int id)
return NULL;
}

+ if (!service_auths)
+ service_auths = g_hash_table_new(g_direct_hash, g_direct_equal);
+ else
+ g_hash_table_ref(service_auths);
+
return btd_adapter_ref(adapter);
}

@@ -3160,7 +3179,7 @@ static void agent_auth_cb(struct agent *agent, DBusError *derr,

auth->cb(derr, auth->user_data);

- g_free(auth);
+ service_auth_free(auth);
}

static gboolean auth_idle_cb(gpointer user_data)
@@ -3207,6 +3226,9 @@ static int adapter_authorize(struct btd_adapter *adapter, const bdaddr_t *dst,
auth->user_data = user_data;
auth->device = device;
auth->adapter = adapter;
+ auth->id = service_auth_id++;
+
+ g_hash_table_insert(service_auths, GUINT_TO_POINTER(auth->id), auth);

if (device_is_trusted(device) == TRUE) {
adapter->auth_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
@@ -3218,7 +3240,7 @@ static int adapter_authorize(struct btd_adapter *adapter, const bdaddr_t *dst,
agent = device_get_agent(device);
if (!agent) {
warn("Can't find device agent");
- g_free(auth);
+ service_auth_free(auth);
return -EPERM;
}

@@ -3227,13 +3249,13 @@ static int adapter_authorize(struct btd_adapter *adapter, const bdaddr_t *dst,
err = agent_authorize(agent, dev_path, uuid, agent_auth_cb, adapter,
NULL);
if (err < 0) {
- g_free(auth);
+ service_auth_free(auth);
return err;
}

done:
adapter->auth = auth;
- return 0;
+ return auth->id;
}

int btd_request_authorization(const bdaddr_t *src, const bdaddr_t *dst,
@@ -3252,49 +3274,47 @@ int btd_request_authorization(const bdaddr_t *src, const bdaddr_t *dst,
}

for (l = manager_get_adapters(); l != NULL; l = g_slist_next(l)) {
- int err;
+ int auth_id;

adapter = l->data;

- err = adapter_authorize(adapter, dst, uuid, cb, user_data);
- if (err == 0)
- return 0;
+ auth_id = adapter_authorize(adapter, dst, uuid, cb, user_data);
+ if (auth_id >= 0)
+ return auth_id;
}

return -EPERM;
}

-int btd_cancel_authorization(const bdaddr_t *src, const bdaddr_t *dst)
+int btd_cancel_authorization(int auth_id)
{
- struct btd_adapter *adapter = manager_find_adapter(src);
- struct btd_device *device;
+ struct service_auth *auth;
+ struct btd_adapter *adapter;
struct agent *agent;
- char address[18];
int err;

- if (!adapter)
+ if (!service_auths)
return -EPERM;

- ba2str(dst, address);
- device = adapter_find_device(adapter, address);
- if (!device)
+ auth = g_hash_table_lookup(service_auths, GUINT_TO_POINTER(auth_id));
+ if (!auth)
return -EPERM;

+ adapter = auth->adapter;
+
if (adapter->auth_idle_id) {
g_source_remove(adapter->auth_idle_id);
adapter->auth_idle_id = 0;
+ adapter->auth = NULL;
return 0;
}

- if (!adapter->auth || adapter->auth->device != device)
- return -EPERM;
-
/*
* FIXME: Cancel fails if authorization is requested to adapter's
* agent and in the meanwhile CreatePairedDevice is called.
*/

- agent = device_get_agent(device);
+ agent = device_get_agent(auth->device);
if (!agent)
return -EPERM;

diff --git a/src/adapter.h b/src/adapter.h
index cd37b15..2993715 100644
--- a/src/adapter.h
+++ b/src/adapter.h
@@ -149,7 +149,7 @@ int btd_register_adapter_driver(struct btd_adapter_driver *driver);
void btd_unregister_adapter_driver(struct btd_adapter_driver *driver);
int btd_request_authorization(const bdaddr_t *src, const bdaddr_t *dst,
const char *uuid, service_auth_cb cb, void *user_data);
-int btd_cancel_authorization(const bdaddr_t *src, const bdaddr_t *dst);
+int btd_cancel_authorization(int auth_id);

const char *adapter_any_get_path(void);

--
1.7.7.6


2012-09-11 13:55:29

by Mikel Astiz

[permalink] [raw]
Subject: [PATCH v0 3/4] adapter: Queue parallel authorization requests

From: Mikel Astiz <[email protected]>

Remote device could try to connect several profiles in parallel, or
several devices could be trying to connect services. Instead of
returning EBUSY, this patch adds a queue to each adapter such that the
authorization requests will be queued and processed sequentially.
---
src/adapter.c | 150 ++++++++++++++++++++++++++++++--------------------------
1 files changed, 80 insertions(+), 70 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index de5d1c9..14fe02a 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -103,8 +103,10 @@ struct service_auth {
int id;
service_auth_cb cb;
void *user_data;
+ const char *uuid;
struct btd_device *device;
struct btd_adapter *adapter;
+ struct agent *agent; /* NULL for queued auths */
};

struct btd_adapter {
@@ -131,8 +133,8 @@ struct btd_adapter {
GSList *found_devices;
GSList *oor_devices; /* out of range device list */
struct agent *agent; /* For the new API */
- guint auth_idle_id; /* Ongoing authorization (trusted) */
- struct service_auth *auth; /* Ongoing authorization */
+ guint auth_idle_id; /* Pending authorization dequeue */
+ GQueue *auths; /* Ongoing and pending auths */
GSList *connections; /* Connected devices */
GSList *devices; /* Devices structure pointers */
GSList *mode_sessions; /* Request Mode sessions */
@@ -160,6 +162,8 @@ struct btd_adapter {
GSList *profiles;
};

+static gboolean process_auth_queue(gpointer user_data);
+
static void dev_info_free(void *data)
{
struct remote_dev_info *dev = data;
@@ -971,16 +975,34 @@ static struct btd_device *adapter_create_device(DBusConnection *conn,
return device;
}

+static void service_auth_cancel(struct service_auth *auth)
+{
+ DBusError derr;
+
+ dbus_error_init(&derr);
+ dbus_set_error_const(&derr, "org.bluez.Error.Canceled", NULL);
+
+ auth->cb(&derr, auth->user_data);
+
+ dbus_error_free(&derr);
+
+ if (auth->agent != NULL)
+ agent_cancel(auth->agent);
+
+ service_auth_free(auth);
+}
+
void adapter_remove_device(DBusConnection *conn, struct btd_adapter *adapter,
struct btd_device *device,
gboolean remove_storage)
{
const gchar *dev_path = device_get_path(device);
- struct agent *agent;

adapter->devices = g_slist_remove(adapter->devices, device);
adapter->connections = g_slist_remove(adapter->connections, device);

+ g_queue_foreach(adapter->auths, (GFunc) service_auth_cancel, NULL);
+
adapter_update_devices(adapter);

g_dbus_emit_signal(conn, adapter->path,
@@ -988,13 +1010,6 @@ void adapter_remove_device(DBusConnection *conn, struct btd_adapter *adapter,
DBUS_TYPE_OBJECT_PATH, &dev_path,
DBUS_TYPE_INVALID);

- agent = device_get_agent(device);
-
- if (agent && adapter->auth && adapter->auth->device == device) {
- adapter->auth = NULL;
- agent_cancel(agent);
- }
-
device_remove(device, remove_storage);
}

@@ -2426,8 +2441,8 @@ static void adapter_free(gpointer user_data)
if (adapter->auth_idle_id)
g_source_remove(adapter->auth_idle_id);

- if (adapter->auth)
- service_auth_free(adapter->auth);
+ g_queue_foreach(adapter->auths, (GFunc) service_auth_free, NULL);
+ g_queue_free(adapter->auths);

if (adapter->off_timer)
off_timer_remove(adapter);
@@ -2525,6 +2540,7 @@ struct btd_adapter *adapter_create(DBusConnection *conn, int id)
}

adapter->dev_id = id;
+ adapter->auths = g_queue_new();

snprintf(path, sizeof(path), "%s/hci%d", base_path, id);
adapter->path = g_strdup(path);
@@ -3173,24 +3189,60 @@ static void agent_auth_cb(struct agent *agent, DBusError *derr,
void *user_data)
{
struct btd_adapter *adapter = user_data;
- struct service_auth *auth = adapter->auth;
+ struct service_auth *auth = adapter->auths->head->data;

- adapter->auth = NULL;
+ g_queue_pop_head(adapter->auths);

auth->cb(derr, auth->user_data);

service_auth_free(auth);
+
+ adapter->auth_idle_id = g_idle_add(process_auth_queue, adapter);
}

-static gboolean auth_idle_cb(gpointer user_data)
+static gboolean process_auth_queue(gpointer user_data)
{
struct btd_adapter *adapter = user_data;
- struct service_auth *auth = adapter->auth;
+ DBusError err;

- adapter->auth = NULL;
adapter->auth_idle_id = 0;

- auth->cb(NULL, auth->user_data);
+ dbus_error_init(&err);
+ dbus_set_error_const(&err, "org.bluez.Error.Rejected", NULL);
+
+ while (!g_queue_is_empty(adapter->auths)) {
+ struct service_auth *auth = adapter->auths->head->data;
+ struct btd_device *device = auth->device;
+ const gchar *dev_path;
+
+ if (device_is_trusted(device) == TRUE) {
+ auth->cb(NULL, auth->user_data);
+ goto next;
+ }
+
+ auth->agent = device_get_agent(device);
+ if (auth->agent == NULL) {
+ warn("Can't find device agent");
+ auth->cb(&err, auth->user_data);
+ goto next;
+ }
+
+ dev_path = device_get_path(device);
+
+ if (agent_authorize(auth->agent, dev_path, auth->uuid,
+ agent_auth_cb, adapter, NULL) < 0) {
+ auth->cb(&err, auth->user_data);
+ goto next;
+ }
+
+ break;
+
+next:
+ service_auth_free(auth);
+ g_queue_pop_head(adapter->auths);
+ }
+
+ dbus_error_free(&err);

return FALSE;
}
@@ -3201,10 +3253,7 @@ static int adapter_authorize(struct btd_adapter *adapter, const bdaddr_t *dst,
{
struct service_auth *auth;
struct btd_device *device;
- struct agent *agent;
char address[18];
- const gchar *dev_path;
- int err;

ba2str(dst, address);
device = adapter_find_device(adapter, address);
@@ -3215,46 +3264,29 @@ static int adapter_authorize(struct btd_adapter *adapter, const bdaddr_t *dst,
if (!g_slist_find(adapter->connections, device))
error("Authorization request for non-connected device!?");

- if (adapter->auth)
- return -EBUSY;
-
auth = g_try_new0(struct service_auth, 1);
if (!auth)
return -ENOMEM;

auth->cb = cb;
auth->user_data = user_data;
+ auth->uuid = uuid;
auth->device = device;
auth->adapter = adapter;
auth->id = service_auth_id++;

g_hash_table_insert(service_auths, GUINT_TO_POINTER(auth->id), auth);

- if (device_is_trusted(device) == TRUE) {
- adapter->auth_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE,
- auth_idle_cb, adapter,
- NULL);
- goto done;
- }
+ g_queue_push_tail(adapter->auths, auth);

- agent = device_get_agent(device);
- if (!agent) {
- warn("Can't find device agent");
- service_auth_free(auth);
- return -EPERM;
- }
+ if (adapter->auths->length != 1)
+ return auth->id;

- dev_path = device_get_path(device);
+ if (adapter->auth_idle_id != 0)
+ return auth->id;

- err = agent_authorize(agent, dev_path, uuid, agent_auth_cb, adapter,
- NULL);
- if (err < 0) {
- service_auth_free(auth);
- return err;
- }
+ adapter->auth_idle_id = g_idle_add(process_auth_queue, adapter);

-done:
- adapter->auth = auth;
return auth->id;
}

@@ -3289,9 +3321,6 @@ int btd_request_authorization(const bdaddr_t *src, const bdaddr_t *dst,
int btd_cancel_authorization(int auth_id)
{
struct service_auth *auth;
- struct btd_adapter *adapter;
- struct agent *agent;
- int err;

if (!service_auths)
return -EPERM;
@@ -3300,30 +3329,11 @@ int btd_cancel_authorization(int auth_id)
if (!auth)
return -EPERM;

- adapter = auth->adapter;
-
- if (adapter->auth_idle_id) {
- g_source_remove(adapter->auth_idle_id);
- adapter->auth_idle_id = 0;
- adapter->auth = NULL;
- return 0;
- }
-
- /*
- * FIXME: Cancel fails if authorization is requested to adapter's
- * agent and in the meanwhile CreatePairedDevice is called.
- */
-
- agent = device_get_agent(auth->device);
- if (!agent)
- return -EPERM;
-
- err = agent_cancel(agent);
+ g_queue_remove(auth->adapter->auths, auth);

- if (err == 0)
- adapter->auth = NULL;
+ service_auth_cancel(auth);

- return err;
+ return 0;
}

static gchar *adapter_any_path = NULL;
--
1.7.7.6


2012-09-11 13:55:30

by Mikel Astiz

[permalink] [raw]
Subject: [PATCH v0 4/4] audio: Drop audio-specific authorization mechanism

From: Mikel Astiz <[email protected]>

Remove the audio-specific service authorization mechanism in favor of
using the conventional one.

The main difference is that audio profiles will be authorized
independently. Therefore a single connection might result in several
profile authorization requests to the agent (i.e. HFP, A2DP and AVRCP).

This removes any internal policy that would skip the authorization
procedure, making it simpler and more convenient for IVI use-cases.

Agents interested in simulating the old behavior are encouraged to
either set the device as trusted or just reply to the additional
authorization requests automatically without user intervention.
---
audio/avctp.c | 29 ++++------
audio/avdtp.c | 25 ++++++---
audio/device.c | 150 -------------------------------------------------------
audio/device.h | 12 +----
audio/manager.c | 18 ++++---
5 files changed, 40 insertions(+), 194 deletions(-)

diff --git a/audio/avctp.c b/audio/avctp.c
index 4e0e8dc..40e85ed 100644
--- a/audio/avctp.c
+++ b/audio/avctp.c
@@ -139,6 +139,7 @@ struct avctp {

GIOChannel *io;
guint io_id;
+ int service_auth_id;

uint16_t mtu;

@@ -337,13 +338,9 @@ static void avctp_disconnected(struct avctp *session)
g_source_remove(session->io_id);
session->io_id = 0;

- if (session->state == AVCTP_STATE_CONNECTING) {
- struct audio_device *dev;
-
- dev = manager_get_device(&session->server->src,
- &session->dst, FALSE);
- audio_device_cancel_authorization(dev, auth_cb,
- session);
+ if (session->service_auth_id > 0) {
+ btd_cancel_authorization(session->service_auth_id);
+ session->service_auth_id = 0;
}
}

@@ -386,15 +383,7 @@ static void avctp_set_state(struct avctp *session, avctp_state_t new_state)
switch (new_state) {
case AVCTP_STATE_DISCONNECTED:
DBG("AVCTP Disconnected");
-
avctp_disconnected(session);
-
- if (old_state != AVCTP_STATE_CONNECTED)
- break;
-
- if (!audio_device_is_active(dev, NULL))
- audio_device_set_authorized(dev, FALSE);
-
break;
case AVCTP_STATE_CONNECTING:
DBG("AVCTP Connecting");
@@ -657,6 +646,8 @@ static void auth_cb(DBusError *derr, void *user_data)
struct avctp *session = user_data;
GError *err = NULL;

+ session->service_auth_id = 0;
+
if (session->io_id) {
g_source_remove(session->io_id);
session->io_id = 0;
@@ -736,6 +727,7 @@ static void avctp_confirm_cb(GIOChannel *chan, gpointer data)
struct audio_device *dev;
char address[18];
bdaddr_t src, dst;
+ int auth_id;
GError *err = NULL;

bt_io_get(chan, &err,
@@ -780,12 +772,15 @@ static void avctp_confirm_cb(GIOChannel *chan, gpointer data)
avctp_set_state(session, AVCTP_STATE_CONNECTING);
session->io = g_io_channel_ref(chan);

- if (audio_device_request_authorization(dev, AVRCP_TARGET_UUID,
- auth_cb, session) < 0)
+ auth_id = btd_request_authorization(&dev->src, &dev->dst,
+ AVRCP_TARGET_UUID,
+ auth_cb, session);
+ if (auth_id < 0)
goto drop;

session->io_id = g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
session_cb, session);
+ session->service_auth_id = auth_id;
return;

drop:
diff --git a/audio/avdtp.c b/audio/avdtp.c
index d44c504..3137094 100644
--- a/audio/avdtp.c
+++ b/audio/avdtp.c
@@ -400,6 +400,8 @@ struct avdtp {
/* True if the entire device is being disconnected */
gboolean device_disconnect;

+ int service_auth_id;
+
GIOChannel *io;
guint io_id;

@@ -1129,16 +1131,21 @@ static void release_stream(struct avdtp_stream *stream, struct avdtp *session)

static int avdtp_cancel_authorization(struct avdtp *session)
{
- struct audio_device *dev;
+ int err;

if (session->state != AVDTP_SESSION_STATE_CONNECTING)
return 0;

- dev = manager_get_device(&session->server->src, &session->dst, FALSE);
- if (dev == NULL)
- return -ENODEV;
+ if (!session->service_auth_id)
+ return 0;

- return audio_device_cancel_authorization(dev, auth_cb, session);
+ err = btd_cancel_authorization(session->service_auth_id);
+ if (err < 0)
+ return err;
+
+ session->service_auth_id = 0;
+
+ return 0;
}

static void connection_lost(struct avdtp *session, int err)
@@ -2507,7 +2514,7 @@ static void avdtp_confirm_cb(GIOChannel *chan, gpointer data)
struct audio_device *dev;
char address[18];
bdaddr_t src, dst;
- int perr;
+ int auth_id;
GError *err = NULL;

bt_io_get(chan, &err,
@@ -2566,15 +2573,17 @@ static void avdtp_confirm_cb(GIOChannel *chan, gpointer data)
session->io_id = g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
(GIOFunc) session_cb, session);

- perr = audio_device_request_authorization(dev, ADVANCED_AUDIO_UUID,
+ auth_id = btd_request_authorization(&dev->src, &dev->dst,
+ ADVANCED_AUDIO_UUID,
auth_cb, session);
- if (perr < 0) {
+ if (auth_id < 0) {
avdtp_set_state(session, AVDTP_SESSION_STATE_DISCONNECTED);
avdtp_unref(session);
goto drop;
}

dev->auto_connect = auto_connect;
+ session->service_auth_id = auth_id;

return;

diff --git a/audio/device.c b/audio/device.c
index ca64f14..072b0ce 100644
--- a/audio/device.c
+++ b/audio/device.c
@@ -71,19 +71,12 @@ typedef enum {
AUDIO_STATE_CONNECTED,
} audio_state_t;

-struct service_auth {
- service_auth_cb cb;
- void *user_data;
-};
-
struct dev_priv {
audio_state_t state;

headset_state_t hs_state;
sink_state_t sink_state;
avctp_state_t avctp_state;
- GSList *auths;
- int auth_id;

DBusMessage *conn_req;
DBusMessage *dc_req;
@@ -94,8 +87,6 @@ struct dev_priv {
guint dc_id;

gboolean disconnecting;
- gboolean authorized;
- guint auth_idle_id;
};

static unsigned int sink_callback_id = 0;
@@ -113,8 +104,6 @@ static void device_free(struct audio_device *dev)
btd_device_unref(dev->btd_dev);

if (priv) {
- if (priv->auths)
- audio_device_cancel_authorization(dev, NULL, NULL);
if (priv->control_timer)
g_source_remove(priv->control_timer);
if (priv->avdtp_timer)
@@ -240,8 +229,6 @@ static void device_set_state(struct audio_device *dev, audio_state_t new_state)
return;

if (new_state == AUDIO_STATE_DISCONNECTED) {
- priv->authorized = FALSE;
-
if (priv->dc_id) {
device_remove_disconnect_watch(dev->btd_dev,
priv->dc_id);
@@ -736,140 +723,3 @@ void audio_device_unregister(struct audio_device *device)

device_free(device);
}
-
-static void auth_cb(DBusError *derr, void *user_data)
-{
- struct audio_device *dev = user_data;
- struct dev_priv *priv = dev->priv;
-
- priv->auth_id = 0;
-
- if (derr == NULL)
- priv->authorized = TRUE;
-
- while (priv->auths) {
- struct service_auth *auth = priv->auths->data;
-
- auth->cb(derr, auth->user_data);
- priv->auths = g_slist_remove(priv->auths, auth);
- g_free(auth);
- }
-}
-
-static gboolean auth_idle_cb(gpointer user_data)
-{
- struct audio_device *dev = user_data;
- struct dev_priv *priv = dev->priv;
-
- priv->auth_idle_id = 0;
-
- auth_cb(NULL, dev);
-
- return FALSE;
-}
-
-static gboolean audio_device_is_connected(struct audio_device *dev)
-{
- if (dev->headset) {
- headset_state_t state = headset_get_state(dev);
-
- if (state == HEADSET_STATE_CONNECTED ||
- state == HEADSET_STATE_PLAY_IN_PROGRESS ||
- state == HEADSET_STATE_PLAYING)
- return TRUE;
- }
-
- if (dev->sink) {
- sink_state_t state = sink_get_state(dev);
-
- if (state == SINK_STATE_CONNECTED ||
- state == SINK_STATE_PLAYING)
- return TRUE;
- }
-
- if (dev->source) {
- source_state_t state = source_get_state(dev);
-
- if (state == SOURCE_STATE_CONNECTED ||
- state == SOURCE_STATE_PLAYING)
- return TRUE;
- }
-
- return FALSE;
-}
-
-int audio_device_request_authorization(struct audio_device *dev,
- const char *uuid, service_auth_cb cb,
- void *user_data)
-{
- struct dev_priv *priv = dev->priv;
- struct service_auth *auth;
- int err;
-
- auth = g_try_new0(struct service_auth, 1);
- if (!auth)
- return -ENOMEM;
-
- auth->cb = cb;
- auth->user_data = user_data;
-
- priv->auths = g_slist_append(priv->auths, auth);
- if (g_slist_length(priv->auths) > 1)
- return 0;
-
- if (priv->authorized || audio_device_is_connected(dev)) {
- priv->auth_idle_id = g_idle_add(auth_idle_cb, dev);
- return 0;
- }
-
- err = btd_request_authorization(&dev->src, &dev->dst, uuid, auth_cb,
- dev);
- if (err < 0) {
- priv->auths = g_slist_remove(priv->auths, auth);
- g_free(auth);
- } else
- priv->auth_id = err;
-
- return err;
-}
-
-int audio_device_cancel_authorization(struct audio_device *dev,
- authorization_cb cb, void *user_data)
-{
- struct dev_priv *priv = dev->priv;
- GSList *l, *next;
-
- for (l = priv->auths; l != NULL; l = next) {
- struct service_auth *auth = l->data;
-
- next = g_slist_next(l);
-
- if (cb && auth->cb != cb)
- continue;
-
- if (user_data && auth->user_data != user_data)
- continue;
-
- priv->auths = g_slist_remove(priv->auths, auth);
- g_free(auth);
- }
-
- if (g_slist_length(priv->auths) == 0) {
- if (priv->auth_idle_id > 0) {
- g_source_remove(priv->auth_idle_id);
- priv->auth_idle_id = 0;
- } else {
- btd_cancel_authorization(priv->auth_id);
- priv->auth_id = 0;
- }
- }
-
- return 0;
-}
-
-void audio_device_set_authorized(struct audio_device *dev, gboolean auth)
-{
- struct dev_priv *priv = dev->priv;
-
- priv->authorized = auth;
-}
diff --git a/audio/device.h b/audio/device.h
index 75f1da9..49c6829 100644
--- a/audio/device.h
+++ b/audio/device.h
@@ -48,6 +48,7 @@ struct audio_device {
struct target *target;

guint hs_preauth_id;
+ int hs_service_auth_id;

struct dev_priv *priv;
};
@@ -61,14 +62,3 @@ void audio_device_unregister(struct audio_device *device);

gboolean audio_device_is_active(struct audio_device *dev,
const char *interface);
-
-typedef void (*authorization_cb) (DBusError *derr, void *user_data);
-
-int audio_device_cancel_authorization(struct audio_device *dev,
- authorization_cb cb, void *user_data);
-
-int audio_device_request_authorization(struct audio_device *dev,
- const char *uuid, authorization_cb cb,
- void *user_data);
-
-void audio_device_set_authorized(struct audio_device *dev, gboolean auth);
diff --git a/audio/manager.c b/audio/manager.c
index 5c81957..0222d67 100644
--- a/audio/manager.c
+++ b/audio/manager.c
@@ -328,7 +328,7 @@ static gboolean hs_preauth_cb(GIOChannel *chan, GIOCondition cond,

DBG("Headset disconnected during authorization");

- audio_device_cancel_authorization(device, headset_auth_cb, device);
+ btd_cancel_authorization(device->hs_service_auth_id);

headset_set_state(device, HEADSET_STATE_DISCONNECTED);

@@ -343,7 +343,7 @@ static void ag_confirm(GIOChannel *chan, gpointer data)
struct audio_device *device;
gboolean hfp_active;
bdaddr_t src, dst;
- int perr;
+ int auth_id;
GError *err = NULL;
uint8_t ch;

@@ -398,10 +398,11 @@ static void ag_confirm(GIOChannel *chan, gpointer data)

headset_set_state(device, HEADSET_STATE_CONNECTING);

- perr = audio_device_request_authorization(device, server_uuid,
- headset_auth_cb, device);
- if (perr < 0) {
- DBG("Authorization denied: %s", strerror(-perr));
+ auth_id = btd_request_authorization(&device->src, &device->dst,
+ server_uuid, headset_auth_cb,
+ device);
+ if (auth_id < 0) {
+ DBG("Authorization denied: %s", strerror(-auth_id));
headset_set_state(device, HEADSET_STATE_DISCONNECTED);
return;
}
@@ -480,8 +481,9 @@ static void hf_io_cb(GIOChannel *chan, gpointer data)
goto drop;
}

- perr = audio_device_request_authorization(device, server_uuid,
- gateway_auth_cb, device);
+ perr = btd_request_authorization(&device->src, &device->dst,
+ server_uuid, gateway_auth_cb,
+ device);
if (perr < 0) {
DBG("Authorization denied: %s", strerror(-perr));
gateway_set_state(device, GATEWAY_STATE_DISCONNECTED);
--
1.7.7.6