Skip to contentSkip to conversation

gobex: Add g_obex_disconnect

PATCH In review Luiz Augusto von Dentz 1 participant 4 messages Mar 21, 2014 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/3 gobex: Add g_obex_disconnect

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

This adds g_obex_disconnect function which can be used to send OBEX Disconnect command.

---
 gobex/gobex.c | 12 ++++++++++++
 gobex/gobex.h |  3 +++
 2 files changed, 15 insertions(+)
gobex/gobex.c +12 -0
index d7d325b..ca15941 100644
@@ -1445,6 +1445,18 @@ guint g_obex_connect(GObex *obex, GObexResponseFunc func, gpointer user_data,
 	return g_obex_send_req(obex, req, -1, func, user_data, err);
 }
 
+guint g_obex_disconnect(GObex *obex, GObexResponseFunc func, gpointer user_data,
+								GError **err)
+{
+	GObexPacket *req;
+
+	g_obex_debug(G_OBEX_DEBUG_COMMAND, "");
+
+	req = g_obex_packet_new(G_OBEX_OP_DISCONNECT, TRUE, G_OBEX_HDR_INVALID);
+
+	return g_obex_send_req(obex, req, -1, func, user_data, err);
+}
+
 guint g_obex_setpath(GObex *obex, const char *path, GObexResponseFunc func,
 					gpointer user_data, GError **err)
 {
gobex/gobex.h +3 -0
index 76a224e..7c47590 100644
@@ -75,6 +75,9 @@ void g_obex_unref(GObex *obex);
 guint g_obex_connect(GObex *obex, GObexResponseFunc func, gpointer user_data,
 				GError **err, guint8 first_hdr_id, ...);
 
+guint g_obex_disconnect(GObex *obex, GObexResponseFunc func, gpointer user_data,
+								GError **err);
+
 guint g_obex_setpath(GObex *obex, const char *path, GObexResponseFunc func,
 					gpointer user_data, GError **err);
 
Signature · 1 line
1.8.5.3
author maintainer
#1 lore raw

PATCH 2/3 unit/test-gobex: Add test for g_obex_disconnect

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

---
 unit/test-gobex.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
unit/test-gobex.c +41 -0
index ded83dd..2834c77 100644
@@ -45,6 +45,10 @@ static uint8_t pkt_connect_req[] = { G_OBEX_OP_CONNECT | FINAL_BIT,
 static uint8_t pkt_connect_rsp[] = { 0x20 | FINAL_BIT, 0x00, 0x07,
 					0x10, 0x00, 0x10, 0x00 };
 
+static uint8_t pkt_disconnect_req[] = { G_OBEX_OP_DISCONNECT | FINAL_BIT,
+					0x00, 0x03 };
+static uint8_t pkt_disconnect_rsp[] = { 0x20 | FINAL_BIT, 0x00, 0x03 };
+
 static uint8_t pkt_setpath_req[] = { G_OBEX_OP_SETPATH | FINAL_BIT, 0x00, 0x10,
 					0x02, 0x00,
 					G_OBEX_HDR_NAME, 0x00, 0x0b,
@@ -883,6 +887,42 @@ static void test_connect(void)
 	g_assert_no_error(d.err);
 }
 
+static void test_obex_disconnect(void)
+{
+	GIOChannel *io;
+	GIOCondition cond;
+	guint io_id, timer_id;
+	GObex *obex;
+	struct test_data d = { 0, NULL, {
+			{ pkt_disconnect_req, sizeof(pkt_disconnect_req) } }, {
+			{ pkt_disconnect_rsp, sizeof(pkt_disconnect_rsp) } } };
+
+	create_endpoints(&obex, &io, SOCK_STREAM);
+
+	cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
+	io_id = g_io_add_watch(io, cond, test_io_cb, &d);
+
+	d.mainloop = g_main_loop_new(NULL, FALSE);
+
+	timer_id = g_timeout_add_seconds(1, test_timeout, &d);
+
+	g_obex_disconnect(obex, req_complete, &d, &d.err);
+	g_assert_no_error(d.err);
+
+	g_main_loop_run(d.mainloop);
+
+	g_assert_cmpuint(d.count, ==, 1);
+
+	g_main_loop_unref(d.mainloop);
+
+	g_source_remove(timer_id);
+	g_io_channel_unref(io);
+	g_source_remove(io_id);
+	g_obex_unref(obex);
+
+	g_assert_no_error(d.err);
+}
+
 static void test_setpath(void)
 {
 	GIOChannel *io;
@@ -1188,6 +1228,7 @@ int main(int argc, char *argv[])
 					test_cancel_req_delay_pkt);
 
 	g_test_add_func("/gobex/test_connect", test_connect);
+	g_test_add_func("/gobex/test_obex_disconnect", test_obex_disconnect);
 
 	g_test_add_func("/gobex/test_setpath", test_setpath);
 	g_test_add_func("/gobex/test_setpath_up", test_setpath_up);
Signature · 1 line
1.8.5.3
author maintainer
#2 lore raw

PATCH 3/3 obexd/client: Fix not sending OBEX Disconnect when disconnecting

In reply to Luiz Augusto von Dentz

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

Sending OBEX Disconnect command before disconnecting the transport is mandatory.

---
 obexd/client/session.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
obexd/client/session.c +32 -2
index 8138b1e..cb176e4 100644
@@ -240,8 +240,6 @@ static void session_free(struct obc_session *session)
 	if (session->p)
 		pending_request_free(session->p);
 
-	sessions = g_slist_remove(sessions, session);
-
 	g_free(session->path);
 	g_free(session->owner);
 	g_free(session->source);
@@ -250,6 +248,25 @@ static void session_free(struct obc_session *session)
 	g_free(session);
 }
 
+static void disconnect_complete(GObex *obex, GError *err, GObexPacket *rsp,
+							void *user_data)
+{
+	struct obc_session *session = user_data;
+
+	DBG("");
+
+	if (err)
+		error("%s", err->message);
+
+	/* Disconnect transport */
+	if (session->id > 0 && session->transport != NULL) {
+		session->transport->disconnect(session->id);
+		session->id = 0;
+	}
+
+	session_free(session);
+}
+
 void obc_session_unref(struct obc_session *session)
 {
 	int refs;
@@ -261,6 +278,19 @@ void obc_session_unref(struct obc_session *session)
 	if (refs > 0)
 		return;
 
+	sessions = g_slist_remove(sessions, session);
+
+	if (!session->obex)
+		goto disconnect;
+
+	/* Wait OBEX Disconnect to complete if command succeed otherwise
+	 * proceed with transport disconnection since there is nothing else to
+	 * be done */
+	if (g_obex_disconnect(session->obex, disconnect_complete, session,
+									NULL))
+		return;
+
+disconnect:
 	/* Disconnect transport */
 	if (session->id > 0 && session->transport != NULL) {
 		session->transport->disconnect(session->id);
Signature · 1 line
1.8.5.3
author maintainer
#3 lore raw
In reply to Luiz Augusto von Dentz

Hi,

Quoted 47 lines · Luiz Augusto von Dentz

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

This adds g_obex_disconnect function which can be used to send OBEX Disconnect command.

---
 gobex/gobex.c | 12 ++++++++++++
 gobex/gobex.h |  3 +++
 2 files changed, 15 insertions(+)
gobex/gobex.c +12 -0
index d7d325b..ca15941 100644
@@ -1445,6 +1445,18 @@ guint g_obex_connect(GObex *obex, GObexResponseFunc func, gpointer user_data,
        return g_obex_send_req(obex, req, -1, func, user_data, err);
 }
 
+guint g_obex_disconnect(GObex *obex, GObexResponseFunc func, gpointer user_data,
+                                                               GError **err)
+{
+       GObexPacket *req;
+
+       g_obex_debug(G_OBEX_DEBUG_COMMAND, "");
+
+       req = g_obex_packet_new(G_OBEX_OP_DISCONNECT, TRUE, G_OBEX_HDR_INVALID);
+
+       return g_obex_send_req(obex, req, -1, func, user_data, err);
+}
+
 guint g_obex_setpath(GObex *obex, const char *path, GObexResponseFunc func,
                                        gpointer user_data, GError **err)
 {
gobex/gobex.h +3 -1
index 76a224e..7c47590 100644
@@ -75,6 +75,9 @@ void g_obex_unref(GObex *obex);
 guint g_obex_connect(GObex *obex, GObexResponseFunc func, gpointer user_data,
                                GError **err, guint8 first_hdr_id, ...);
 
+guint g_obex_disconnect(GObex *obex, GObexResponseFunc func, gpointer user_data,
+                                                               GError **err);
+
 guint g_obex_setpath(GObex *obex, const char *path, GObexResponseFunc func,
                                        gpointer user_data, GError **err);
 
--
Quoted 1 line

1.8.5.3

Pushed.

Signature · 1 line
Luiz Augusto von Dentz