Skip to contentSkip to conversation

android/AVDTP: Add avdtp_add_disconnect_cb and avdtp_remove_disconnect_cb

PATCH In review Luiz Augusto von Dentz 2 participants 5 messages Dec 19, 2013 – Dec 20, 2013 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/4 android/AVDTP: Add avdtp_add_disconnect_cb and avdtp_remove_disconnect_cb

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

With these functions the user application can detect when the AVDTP has been disconnected and cleanup properly.

---
 android/avdtp.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 android/avdtp.h |  6 ++++++
 2 files changed, 64 insertions(+), 3 deletions(-)
android/avdtp.c +58 -3
index 3a3380a..b2d77a4 100644
@@ -336,6 +336,12 @@ struct discover_callback {
 	void *user_data;
 };
 
+struct disconnect_callback {
+	unsigned int id;
+	avdtp_disconnect_cb_t cb;
+	void *user_data;
+};
+
 struct avdtp_stream {
 	GIOChannel *io;
 	uint16_t imtu;
@@ -390,6 +396,8 @@ struct avdtp {
 
 	struct discover_callback *discover;
 	struct pending_req *req;
+
+	GSList *disconnect;
 };
 
 static GSList *lseps = NULL;
@@ -970,12 +978,22 @@ static void avdtp_free(void *data)
 	g_slist_free_full(session->req_queue, pending_req_free);
 	g_slist_free_full(session->prio_queue, pending_req_free);
 	g_slist_free_full(session->seps, sep_free);
+	g_slist_free_full(session->disconnect, g_free);
 
 	g_free(session->buf);
 
 	g_free(session);
 }
 
+static void process_disconnect(void *data)
+{
+	struct disconnect_callback *callback = data;
+
+	callback->cb(callback->user_data);
+
+	g_free(callback);
+}
+
 static void connection_lost(struct avdtp *session, int err)
 {
 	DBG("Disconnected: %s (%d)", strerror(err), err);
@@ -983,12 +1001,14 @@ static void connection_lost(struct avdtp *session, int err)
 	g_slist_foreach(session->streams, (GFunc) release_stream, session);
 	session->streams = NULL;
 
+	avdtp_ref(session);
+
 	finalize_discovery(session, err);
 
-	if (session->ref > 0)
-		return;
+	g_slist_free_full(session->disconnect, process_disconnect);
+	session->disconnect = NULL;
 
-	avdtp_free(session);
+	avdtp_unref(session);
 }
 
 void avdtp_unref(struct avdtp *session)
@@ -2047,6 +2067,41 @@ struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version)
 	return avdtp_ref(session);
 }
 
+unsigned int avdtp_add_disconnect_cb(struct avdtp *session,
+						avdtp_disconnect_cb_t cb,
+						void *user_data)
+{
+	struct disconnect_callback *callback;
+	static unsigned int id = 0;
+
+	callback = g_new0(struct disconnect_callback, 1);
+	callback->id = ++id;
+	callback->cb = cb;
+	callback->user_data = user_data;
+	session->disconnect = g_slist_append(session->disconnect, callback);
+
+	return id;
+}
+
+gboolean avdtp_remove_disconnect_cb(struct avdtp *session, unsigned int id)
+{
+	GSList *l;
+
+	for (l = session->disconnect; l; l = g_slist_next(l)) {
+		struct disconnect_callback *callback = l->data;
+
+		if (callback->id != id)
+			continue;
+
+		session->disconnect = g_slist_remove(session->disconnect,
+								callback);
+		g_free(callback);
+		return TRUE;
+	}
+
+	return FALSE;
+}
+
 static void queue_request(struct avdtp *session, struct pending_req *req,
 			gboolean priority)
 {
android/avdtp.h +6 -0
index 9760875..1619029 100644
@@ -200,9 +200,15 @@ struct avdtp_sep_ind {
 
 typedef void (*avdtp_discover_cb_t) (struct avdtp *session, GSList *seps,
 					struct avdtp_error *err, void *user_data);
+typedef void (*avdtp_disconnect_cb_t) (void *user_data);
 
 struct avdtp *avdtp_new(int fd, size_t imtu, size_t omtu, uint16_t version);
 
+unsigned int avdtp_add_disconnect_cb(struct avdtp *session,
+						avdtp_disconnect_cb_t cb,
+						void *user_data);
+gboolean avdtp_remove_disconnect_cb(struct avdtp *session, unsigned int id);
+
 void avdtp_unref(struct avdtp *session);
 struct avdtp *avdtp_ref(struct avdtp *session);
 
Signature · 1 line
1.8.3.1
author maintainer
#1 lore raw

PATCH 2/4 android/A2DP: Use avdtp_add_disconnect_cb to cleanup on disconnect

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

This replaces the use of g_io_add_watch with avdtp_add_disconnect_cb so connection tracking stay within AVDTP code.

---
 android/a2dp.c | 14 +++-----------
 1 file changed, 3 insertions(+), 11 deletions(-)
android/a2dp.c +3 -11
index 107cbb8..2de2f66 100644
@@ -57,7 +57,6 @@ struct a2dp_device {
 	bdaddr_t	dst;
 	uint8_t		state;
 	GIOChannel	*io;
-	guint		watch;
 	struct avdtp	*session;
 };
 
@@ -74,9 +73,6 @@ static void a2dp_device_free(struct a2dp_device *dev)
 	if (dev->session)
 		avdtp_unref(dev->session);
 
-	if (dev->watch > 0)
-		g_source_remove(dev->watch);
-
 	if (dev->io)
 		g_io_channel_unref(dev->io);
 
@@ -120,13 +116,11 @@ static void bt_a2dp_notify_state(struct a2dp_device *dev, uint8_t state)
 	a2dp_device_free(dev);
 }
 
-static gboolean watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
+static void disconnect_cb(void *user_data)
 {
-	struct a2dp_device *dev = data;
+	struct a2dp_device *dev = user_data;
 
 	bt_a2dp_notify_state(dev, HAL_A2DP_STATE_DISCONNECTED);
-
-	return FALSE;
 }
 
 static void signaling_connect_cb(GIOChannel *chan, GError *err,
@@ -157,9 +151,7 @@ static void signaling_connect_cb(GIOChannel *chan, GError *err,
 	/* FIXME: Add proper version */
 	fd = g_io_channel_unix_get_fd(chan);
 	dev->session = avdtp_new(fd, imtu, omtu, 0x0100);
-
-	dev->watch = g_io_add_watch(dev->io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
-								watch_cb, dev);
+	avdtp_add_disconnect_cb(dev->session, disconnect_cb, dev);
 
 	bt_a2dp_notify_state(dev, HAL_A2DP_STATE_CONNECTED);
 }
Signature · 1 line
1.8.3.1
author maintainer
#2 lore raw

PATCH 3/4 android/AVDTP: Add avdtp_shutdown

In reply to Luiz Augusto von Dentz

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

avdtp_shutdown can be used to disconnect closing all existing stream properly.

---
 android/avdtp.c | 19 +++++++++++++++++++
 android/avdtp.h |  2 ++
 2 files changed, 21 insertions(+)
android/avdtp.c +19 -0
index b2d77a4..7d3cb18 100644
@@ -2102,6 +2102,25 @@ gboolean avdtp_remove_disconnect_cb(struct avdtp *session, unsigned int id)
 	return FALSE;
 }
 
+void avdtp_shutdown(struct avdtp *session)
+{
+	GSList *l;
+	int sock;
+
+	if (!session->io)
+		return;
+
+	for (l = session->streams; l; l = g_slist_next(l)) {
+		struct avdtp_stream *stream = l->data;
+
+		avdtp_close(session, stream, TRUE);
+	}
+
+	sock = g_io_channel_unix_get_fd(session->io);
+
+	shutdown(sock, SHUT_RDWR);
+}
+
 static void queue_request(struct avdtp *session, struct pending_req *req,
 			gboolean priority)
 {
android/avdtp.h +2 -0
index 1619029..d371653 100644
@@ -209,6 +209,8 @@ unsigned int avdtp_add_disconnect_cb(struct avdtp *session,
 						void *user_data);
 gboolean avdtp_remove_disconnect_cb(struct avdtp *session, unsigned int id);
 
+void avdtp_shutdown(struct avdtp *session);
+
 void avdtp_unref(struct avdtp *session);
 struct avdtp *avdtp_ref(struct avdtp *session);
 
Signature · 1 line
1.8.3.1
author maintainer
#3 lore raw

PATCH 4/4 android/A2DP: Fix disconnecting after AVDTP session creation

In reply to Luiz Augusto von Dentz

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

Once the connection is handover to AVDTP code the GIOChannel can be unref but it should not close the connection.

---
 android/a2dp.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
android/a2dp.c +18 -7
index 2de2f66..9087c62 100644
@@ -73,8 +73,10 @@ static void a2dp_device_free(struct a2dp_device *dev)
 	if (dev->session)
 		avdtp_unref(dev->session);
 
-	if (dev->io)
+	if (dev->io) {
+		g_io_channel_shutdown(dev->io, FALSE, NULL);
 		g_io_channel_unref(dev->io);
+	}
 
 	devices = g_slist_remove(devices, dev);
 	g_free(dev);
@@ -148,11 +150,18 @@ static void signaling_connect_cb(GIOChannel *chan, GError *err,
 		return;
 	}
 
-	/* FIXME: Add proper version */
+	g_io_channel_set_close_on_unref(chan, FALSE);
 	fd = g_io_channel_unix_get_fd(chan);
+
+	/* FIXME: Add proper version */
 	dev->session = avdtp_new(fd, imtu, omtu, 0x0100);
 	avdtp_add_disconnect_cb(dev->session, disconnect_cb, dev);
 
+	if (dev->io) {
+		g_io_channel_unref(dev->io);
+		dev->io = NULL;
+	}
+
 	bt_a2dp_notify_state(dev, HAL_A2DP_STATE_CONNECTED);
 }
 
@@ -221,15 +230,17 @@ static void bt_a2dp_disconnect(const void *buf, uint16_t len)
 	}
 
 	dev = l->data;
+	status = HAL_STATUS_SUCCESS;
 
-	/* Wait signaling channel to HUP */
-	if (dev->io)
-		g_io_channel_shutdown(dev->io, TRUE, NULL);
+	if (dev->io) {
+		bt_a2dp_notify_state(dev, HAL_A2DP_STATE_DISCONNECTED);
+		goto failed;
+	}
 
+	/* Wait AVDTP session to shutdown */
+	avdtp_shutdown(dev->session);
 	bt_a2dp_notify_state(dev, HAL_A2DP_STATE_DISCONNECTING);
 
-	status = HAL_STATUS_SUCCESS;
-
 failed:
 	ipc_send_rsp(HAL_SERVICE_ID_A2DP, HAL_OP_A2DP_DISCONNECT, status);
 }
Signature · 1 line
1.8.3.1
#4 lore raw
In reply to Luiz Augusto von Dentz

Hi Luiz,

Quoted 6 lines · Luiz Augusto von Dentz

With these functions the user application can detect when the AVDTP has been disconnected and cleanup properly.

---
 android/avdtp.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 android/avdtp.h |  6 ++++++
 2 files changed, 64 insertions(+), 3 deletions(-)

All four patches have been applied. Thanks.

Johan