Skip to contentSkip to conversation

core: Fix connection order of Device.Connect

PATCH In review Luiz Augusto von Dentz 2 participants 7 messages Nov 21, 2012 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/6 core: Fix connection order of Device.Connect

From: Luiz Augusto von Dentz <[email protected]>

We should with higher priority first.

---
 src/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/device.c +1 -1
index 0053098..fd29a7f 100644
@@ -1299,7 +1299,7 @@ static gint profile_prio_cmp(gconstpointer a, gconstpointer b)
 {
 	const struct btd_profile *p1 = a, *p2 = b;
 
-	return p1->priority - p2->priority;
+	return p2->priority - p1->priority;
 }
 
 static DBusMessage *connect_profiles(struct btd_device *dev, DBusMessage *msg,
Signature · 1 line
1.7.11.7
author maintainer
#1 lore raw

PATCH 2/6 audio: Split A2DP into two btd_profile

From: Mikel Astiz <[email protected]>

Merging the three audio profiles (AVDTP, A2DP sink and A2DP source) into one was convenient in the past was doesn't fit very well the new btd_profile approach. The split is also more consistent with other existing profiles.

---
 profiles/audio/a2dp.c    | 80 +++++++++++++++++++++++------------------------
 profiles/audio/a2dp.h    |  3 +-
 profiles/audio/manager.c | 81 +++++++++++++++++++++++++++++++-----------------
 3 files changed, 92 insertions(+), 72 deletions(-)
profiles/audio/a2dp.c +38 -42
index 50c0f43..177f653 100644
@@ -1166,63 +1166,59 @@ static struct a2dp_server *find_server(GSList *list, const bdaddr_t *src)
 	return NULL;
 }
 
-int a2dp_register(const bdaddr_t *src, GKeyFile *config)
+static struct a2dp_server *a2dp_server_register(const bdaddr_t *src,
+							GKeyFile *config)
 {
-	gboolean source = TRUE, sink = FALSE;
-	char *str;
-	GError *err = NULL;
 	struct a2dp_server *server;
+	int av_err;
 
-	if (!config)
-		goto proceed;
+	server = g_new0(struct a2dp_server, 1);
 
-	str = g_key_file_get_string(config, "General", "Enable", &err);
-
-	if (err) {
-		DBG("audio.conf: %s", err->message);
-		g_clear_error(&err);
-	} else {
-		if (strstr(str, "Sink"))
-			source = TRUE;
-		if (strstr(str, "Source"))
-			sink = TRUE;
-		g_free(str);
+	av_err = avdtp_init(src, config);
+	if (av_err < 0) {
+		DBG("AVDTP not registered");
+		g_free(server);
+		return NULL;
 	}
 
-	str = g_key_file_get_string(config, "General", "Disable", &err);
+	bacpy(&server->src, src);
+	servers = g_slist_append(servers, server);
 
-	if (err) {
-		DBG("audio.conf: %s", err->message);
-		g_clear_error(&err);
-	} else {
-		if (strstr(str, "Sink"))
-			source = FALSE;
-		if (strstr(str, "Source"))
-			sink = FALSE;
-		g_free(str);
-	}
+	return server;
+}
 
-proceed:
+int a2dp_source_register(const bdaddr_t *src, GKeyFile *config)
+{
+	struct a2dp_server *server;
 
 	server = find_server(servers, src);
-	if (!server) {
-		int av_err;
+	if (server != NULL)
+		goto done;
 
-		server = g_new0(struct a2dp_server, 1);
+	server = a2dp_server_register(src, config);
+	if (server == NULL)
+		return -EPROTONOSUPPORT;
 
-		av_err = avdtp_init(src, config);
-		if (av_err < 0) {
-			g_free(server);
-			return av_err;
-		}
+done:
+	server->source_enabled = TRUE;
 
-		bacpy(&server->src, src);
-		servers = g_slist_append(servers, server);
-	}
+	return 0;
+}
 
-	server->source_enabled = source;
+int a2dp_sink_register(const bdaddr_t *src, GKeyFile *config)
+{
+	struct a2dp_server *server;
 
-	server->sink_enabled = sink;
+	server = find_server(servers, src);
+	if (server != NULL)
+		goto done;
+
+	server = a2dp_server_register(src, config);
+	if (server == NULL)
+		return -EPROTONOSUPPORT;
+
+done:
+	server->sink_enabled = TRUE;
 
 	return 0;
 }
profiles/audio/a2dp.h +2 -1
index 736bc66..ded1060 100644
@@ -64,7 +64,8 @@ typedef void (*a2dp_stream_cb_t) (struct avdtp *session,
 					struct avdtp_error *err,
 					void *user_data);
 
-int a2dp_register(const bdaddr_t *src, GKeyFile *config);
+int a2dp_source_register(const bdaddr_t *src, GKeyFile *config);
+int a2dp_sink_register(const bdaddr_t *src, GKeyFile *config);
 void a2dp_unregister(const bdaddr_t *src);
 
 struct a2dp_sep *a2dp_add_sep(const bdaddr_t *src, uint8_t type,
profiles/audio/manager.c +52 -29

143 lines not on this page · raw message

Signature · 1 line
1.7.11.7
author maintainer
#2 lore raw

PATCH 3/6 source: Add profile .connect and .disconnect

In reply to Luiz Augusto von Dentz

From: Mikel Astiz <[email protected]>

Add the connection and disconnection hooks to the a2dp_source btd_profile.

---
 profiles/audio/manager.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
profiles/audio/manager.c +90 -0
index 2f36efd..eaae018 100644
@@ -74,6 +74,12 @@ struct audio_adapter {
 	gint ref;
 };
 
+struct profile_req {
+	struct btd_device	*device;
+	struct btd_profile	*profile;
+	btd_profile_cb		cb;
+};
+
 static gboolean auto_connect = TRUE;
 static int max_connected_headsets = 1;
 static GKeyFile *config = NULL;
@@ -176,6 +182,86 @@ static int avrcp_probe(struct btd_profile *p, struct btd_device *device,
 	return 0;
 }
 
+static struct profile_req *new_profile_request(struct btd_device *dev,
+						struct btd_profile *profile,
+						btd_profile_cb cb)
+{
+	struct profile_req *req;
+
+	req  = g_new0(struct profile_req, 1);
+	req->device = dev;
+	req->profile = profile;
+	req->cb = cb;
+
+	return req;
+}
+
+static void profile_cb(struct audio_device *dev, int err, void *data)
+{
+	struct profile_req *req = data;
+
+	if (req->cb)
+		req->cb(req->profile, req->device, err);
+
+	g_free(req);
+}
+
+static int a2dp_source_connect(struct btd_device *dev,
+						struct btd_profile *profile,
+						btd_profile_cb cb)
+{
+	const gchar *path = device_get_path(dev);
+	struct audio_device *audio_dev;
+	struct profile_req *req;
+	int err;
+
+	DBG("path %s", path);
+
+	audio_dev = get_audio_dev(dev);
+	if (!audio_dev) {
+		DBG("unable to get a device object");
+		return -1;
+	}
+
+	req = new_profile_request(dev, profile, cb);
+
+	err = source_connect(audio_dev, profile_cb, req);
+	if (err < 0) {
+		g_free(req);
+		return err;
+	}
+
+	return 0;
+}
+
+static int a2dp_source_disconnect(struct btd_device *dev,
+						struct btd_profile *profile,
+						btd_profile_cb cb)
+{
+	const gchar *path = device_get_path(dev);
+	struct audio_device *audio_dev;
+	struct profile_req *req;
+	int err;
+
+	DBG("path %s", path);
+
+	audio_dev = get_audio_dev(dev);
+	if (!audio_dev) {
+		DBG("unable to get a device object");
+		return -1;
+	}
+
+	req = new_profile_request(dev, profile, cb);
+
+	err = source_disconnect(audio_dev, FALSE, profile_cb, req);
+	if (err < 0) {
+		g_free(req);
+		return err;
+	}
+
+	return 0;
+}
+
 static struct audio_adapter *audio_adapter_ref(struct audio_adapter *adp)
 {
 	adp->ref++;
@@ -335,6 +421,10 @@ static struct btd_profile a2dp_source_profile = {
 	.device_probe	= a2dp_source_probe,
 	.device_remove	= audio_remove,
 
+	.auto_connect	= true,
+	.connect	= a2dp_source_connect,
+	.disconnect	= a2dp_source_disconnect,
+
 	.adapter_probe	= a2dp_source_server_probe,
 };
 
Signature · 1 line
1.7.11.7
author maintainer
#3 lore raw

PATCH 4/6 sink: Add profile .connect and .disconnect

In reply to Luiz Augusto von Dentz

From: Mikel Astiz <[email protected]>

Add the connection and disconnection hooks to the a2dp_sink btd_profile.

---
 profiles/audio/manager.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
profiles/audio/manager.c +60 -0
index eaae018..dad46a9 100644
@@ -262,6 +262,62 @@ static int a2dp_source_disconnect(struct btd_device *dev,
 	return 0;
 }
 
+static int a2dp_sink_connect(struct btd_device *dev,
+						struct btd_profile *profile,
+						btd_profile_cb cb)
+{
+	const gchar *path = device_get_path(dev);
+	struct audio_device *audio_dev;
+	struct profile_req *req;
+	int err;
+
+	DBG("path %s", path);
+
+	audio_dev = get_audio_dev(dev);
+	if (!audio_dev) {
+		DBG("unable to get a device object");
+		return -1;
+	}
+
+	req = new_profile_request(dev, profile, cb);
+
+	err = sink_connect(audio_dev, profile_cb, req);
+	if (err < 0) {
+		g_free(req);
+		return err;
+	}
+
+	return 0;
+}
+
+static int a2dp_sink_disconnect(struct btd_device *dev,
+						struct btd_profile *profile,
+						btd_profile_cb cb)
+{
+	const gchar *path = device_get_path(dev);
+	struct audio_device *audio_dev;
+	struct profile_req *req;
+	int err;
+
+	DBG("path %s", path);
+
+	audio_dev = get_audio_dev(dev);
+	if (!audio_dev) {
+		DBG("unable to get a device object");
+		return -1;
+	}
+
+	req = new_profile_request(dev, profile, cb);
+
+	err = sink_disconnect(audio_dev, FALSE, profile_cb, req);
+	if (err < 0) {
+		g_free(req);
+		return err;
+	}
+
+	return 0;
+}
+
 static struct audio_adapter *audio_adapter_ref(struct audio_adapter *adp)
 {
 	adp->ref++;
@@ -436,6 +492,10 @@ static struct btd_profile a2dp_sink_profile = {
 	.device_probe	= a2dp_sink_probe,
 	.device_remove	= audio_remove,
 
+	.auto_connect	= true,
+	.connect	= a2dp_sink_connect,
+	.disconnect	= a2dp_sink_disconnect,
+
 	.adapter_probe	= a2dp_sink_server_probe,
 };
 
Signature · 1 line
1.7.11.7
author maintainer
#4 lore raw

PATCH 5/6 control: Expose internal connection API

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Separate the D-Bus code from the internal connection handling code, exposing an internal API in case some internal codepath/plugin is interested in using it.

---
 profiles/audio/control.c | 118 ++++++++++++++++++++++++++++++++++++-----------
 profiles/audio/control.h |   4 ++
 2 files changed, 96 insertions(+), 26 deletions(-)
profiles/audio/control.c +92 -26
index 1620128..b2bfb0a 100644
@@ -61,12 +61,28 @@
 
 static unsigned int avctp_id = 0;
 
+struct pending_request {
+	audio_device_cb cb;
+	void *data;
+	unsigned int id;
+};
+
 struct control {
 	struct avctp *session;
 	gboolean target;
-	DBusMessage *connect;
+	struct pending_request *connect;
 };
 
+static void pending_request_free(struct audio_device *dev,
+					struct pending_request *pending,
+					int err)
+{
+	if (pending->cb)
+		pending->cb(dev, err, pending->data);
+
+	g_free(pending);
+}
+
 static void state_changed(struct audio_device *dev, avctp_state_t old_state,
 				avctp_state_t new_state, void *user_data)
 {
@@ -79,11 +95,7 @@ static void state_changed(struct audio_device *dev, avctp_state_t old_state,
 		control->session = NULL;
 
 		if (control->connect) {
-			DBusMessage *reply = btd_error_failed(control->connect,
-							"Unable to connect");
-
-			g_dbus_send_message(btd_get_dbus_connection(), reply);
-			dbus_message_unref(control->connect);
+			pending_request_free(dev, control->connect, -EIO);
 			control->connect = NULL;
 		}
 
@@ -105,9 +117,7 @@ static void state_changed(struct audio_device *dev, avctp_state_t old_state,
 		break;
 	case AVCTP_STATE_CONNECTED:
 		if (control->connect) {
-			g_dbus_send_reply(conn, control->connect,
-							DBUS_TYPE_INVALID);
-			dbus_message_unref(control->connect);
+			pending_request_free(dev, control->connect, 0);
 			control->connect = NULL;
 		}
 
@@ -143,41 +153,97 @@ static DBusMessage *control_is_connected(DBusConnection *conn,
 	return reply;
 }
 
-static DBusMessage *control_connect(DBusConnection *conn, DBusMessage *msg,
-								void *data)
+int control_connect(struct audio_device *dev, audio_device_cb cb, void *data)
 {
-	struct audio_device *device = data;
-	struct control *control = device->control;
+	struct control *control = dev->control;
+	struct pending_request *pending;
 
 	if (control->session)
-		return btd_error_already_connected(msg);
+		return -EALREADY;
 
 	if (!control->target)
-		return btd_error_not_supported(msg);
+		return -ENOTSUP;
 
 	if (control->connect)
-		return btd_error_in_progress(msg);
+		return -EINPROGRESS;
 
-	control->session = avctp_connect(&device->src, &device->dst);
+	control->session = avctp_connect(&dev->src, &dev->dst);
 	if (!control->session)
-		return btd_error_failed(msg, "Unable to connect");
+		return -EIO;
 
-	control->connect = dbus_message_ref(msg);
+	pending = g_new0(struct pending_request, 1);
+	pending->cb = cb;
+	pending->data = data;
+	control->connect = pending;
 
-	return NULL;
+	return 0;
 }
 
-static DBusMessage *control_disconnect(DBusConnection *conn, DBusMessage *msg,
+static void generic_cb(struct audio_device *dev, int err, void *data)
+{
+	DBusMessage *msg = data;
+	DBusMessage *reply;
+
+	if (err < 0) {
+		reply = btd_error_failed(msg, strerror(-err));
+		g_dbus_send_message(btd_get_dbus_connection(), reply);
+		dbus_message_unref(msg);
+		return;
+	}
+
+	g_dbus_send_reply(btd_get_dbus_connection(), msg, DBUS_TYPE_INVALID);
+
+	dbus_message_unref(msg);
+}
+
+static DBusMessage *connect_control(DBusConnection *conn, DBusMessage *msg,
 								void *data)
 {
 	struct audio_device *device = data;
-	struct control *control = device->control;
+	int err;
+
+	err = control_connect(device, generic_cb, msg);
+	if (err < 0)
+		return btd_error_failed(msg, strerror(-err));
+
+	dbus_message_ref(msg);
+
+	return NULL;
+}
+
+int control_disconnect(struct audio_device *dev, audio_device_cb cb,
+								void *data)
+{
+	struct control *control = dev->control;
 
 	if (!control->session)
-		return btd_error_not_connected(msg);
+		return -ENOTCONN;
+
+	/* cancel pending connect */
+	if (control->connect) {
+		pending_request_free(dev, control->connect, -ECANCELED);
+		control->connect = NULL;
+	}
 
 	avctp_disconnect(control->session);
 
+	if (cb)
+		cb(dev, 0, data);
+
+	return 0;
+
+}
+
+static DBusMessage *disconnect_control(DBusConnection *conn, DBusMessage *msg,
+								void *data)
+{
+	struct audio_device *device = data;
+	int err;
+
+	err = control_disconnect(device, NULL, NULL);
+	if (err < 0)
+		return btd_error_failed(msg, strerror(-err));
+
 	return dbus_message_new_method_return(msg);
 }
 
@@ -259,8 +325,8 @@ static const GDBusMethodTable control_methods[] = {
 	{ GDBUS_DEPRECATED_METHOD("IsConnected",
 				NULL, GDBUS_ARGS({ "connected", "b" }),
 				control_is_connected) },
-	{ GDBUS_ASYNC_METHOD("Connect", NULL, NULL, control_connect) },
-	{ GDBUS_METHOD("Disconnect", NULL, NULL, control_disconnect) },
+	{ GDBUS_ASYNC_METHOD("Connect", NULL, NULL, connect_control) },
+	{ GDBUS_METHOD("Disconnect", NULL, NULL, disconnect_control) },
 	{ GDBUS_METHOD("Play", NULL, NULL, control_play) },
 	{ GDBUS_METHOD("Pause", NULL, NULL, control_pause) },
 	{ GDBUS_METHOD("Stop", NULL, NULL, control_stop) },
@@ -294,7 +360,7 @@ static void path_unregister(void *data)
 		avctp_disconnect(control->session);
 
 	if (control->connect)
-		dbus_message_unref(control->connect);
+		pending_request_free(dev, control->connect, -ECANCELED);
 
 	g_free(control);
 	dev->control = NULL;
profiles/audio/control.h +4 -0
index 5f4f728..800a151 100644
@@ -28,3 +28,7 @@ struct control *control_init(struct audio_device *dev, GSList *uuids);
 void control_update(struct control *control, GSList *uuids);
 void control_unregister(struct audio_device *dev);
 gboolean control_is_active(struct audio_device *dev);
+
+int control_connect(struct audio_device *dev, audio_device_cb cb, void *data);
+int control_disconnect(struct audio_device *dev, audio_device_cb cb,
+								void *data);
Signature · 1 line
1.7.11.7
author maintainer
#5 lore raw

PATCH 6/6 control: Add profile .connect and .disconnect

In reply to Luiz Augusto von Dentz

From: Luiz Augusto von Dentz <[email protected]>

Add the connection and disconnection hooks to the avrcp btd_profile.

---
 profiles/audio/manager.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
profiles/audio/manager.c +60 -0
index dad46a9..714489a 100644
@@ -318,6 +318,62 @@ static int a2dp_sink_disconnect(struct btd_device *dev,
 	return 0;
 }
 
+static int avrcp_control_connect(struct btd_device *dev,
+						struct btd_profile *profile,
+						btd_profile_cb cb)
+{
+	const gchar *path = device_get_path(dev);
+	struct audio_device *audio_dev;
+	struct profile_req *req;
+	int err;
+
+	DBG("path %s", path);
+
+	audio_dev = get_audio_dev(dev);
+	if (!audio_dev) {
+		DBG("unable to get a device object");
+		return -1;
+	}
+
+	req = new_profile_request(dev, profile, cb);
+
+	err = control_connect(audio_dev, profile_cb, req);
+	if (err < 0) {
+		g_free(req);
+		return err;
+	}
+
+	return 0;
+}
+
+static int avrcp_control_disconnect(struct btd_device *dev,
+						struct btd_profile *profile,
+						btd_profile_cb cb)
+{
+	const gchar *path = device_get_path(dev);
+	struct audio_device *audio_dev;
+	struct profile_req *req;
+	int err;
+
+	DBG("path %s", path);
+
+	audio_dev = get_audio_dev(dev);
+	if (!audio_dev) {
+		DBG("unable to get a device object");
+		return -1;
+	}
+
+	req = new_profile_request(dev, profile, cb);
+
+	err = control_disconnect(audio_dev, profile_cb, req);
+	if (err < 0) {
+		g_free(req);
+		return err;
+	}
+
+	return 0;
+}
+
 static struct audio_adapter *audio_adapter_ref(struct audio_adapter *adp)
 {
 	adp->ref++;
@@ -506,6 +562,10 @@ static struct btd_profile avrcp_profile = {
 	.device_probe	= avrcp_probe,
 	.device_remove	= audio_remove,
 
+	.auto_connect	= true,
+	.connect	= avrcp_control_connect,
+	.disconnect	= avrcp_control_disconnect,
+
 	.adapter_probe	= avrcp_server_probe,
 	.adapter_remove = avrcp_server_remove,
 };
Signature · 1 line
1.7.11.7
#6 lore raw
In reply to Luiz Augusto von Dentz

Hi Luiz,

Quoted 4 lines · Luiz Augusto von Dentz

We should with higher priority first.

---
 src/device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

All patches in this set have been applied. Thanks.

Johan