All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new
@ 2014-10-02  9:51 Luiz Augusto von Dentz
  2014-10-02  9:51 ` [PATCH BlueZ v3 2/5] unit/test-gatt: Add TP/GAC/CL/BV-01-C test Luiz Augusto von Dentz
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2014-10-02  9:51 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This split bt_gatt_client_unref into bt_gatt_client_free so it can be
used within bt_gatt_client_new when freeing the data.
---
v3: Add changes to handle ATT disconnection

 src/shared/gatt-client.c | 87 +++++++++++++++++++++---------------------------
 1 file changed, 38 insertions(+), 49 deletions(-)

diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 6dc8e95..782e6b3 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -1211,6 +1211,29 @@ static void notify_cb(uint8_t opcode, const void *pdu, uint16_t length,
 	bt_gatt_client_unref(client);
 }
 
+static void long_write_op_unref(void *data);
+
+static void bt_gatt_client_free(struct bt_gatt_client *client)
+{
+	if (client->ready_destroy)
+		client->ready_destroy(client->ready_data);
+
+	if (client->debug_destroy)
+		client->debug_destroy(client->debug_data);
+
+	bt_att_unregister(client->att, client->notify_id);
+	bt_att_unregister(client->att, client->ind_id);
+
+	queue_destroy(client->svc_chngd_queue, free);
+	queue_destroy(client->long_write_queue, long_write_op_unref);
+	queue_destroy(client->notify_list, notify_data_unref);
+
+	gatt_client_clear_services(client);
+
+	bt_att_unref(client->att);
+	free(client);
+}
+
 struct bt_gatt_client *bt_gatt_client_new(struct bt_att *att, uint16_t mtu)
 {
 	struct bt_gatt_client *client;
@@ -1223,52 +1246,36 @@ struct bt_gatt_client *bt_gatt_client_new(struct bt_att *att, uint16_t mtu)
 		return NULL;
 
 	client->long_write_queue = queue_new();
-	if (!client->long_write_queue) {
-		free(client);
-		return NULL;
-	}
+	if (!client->long_write_queue)
+		goto fail;
 
 	client->svc_chngd_queue = queue_new();
-	if (!client->svc_chngd_queue) {
-		queue_destroy(client->long_write_queue, NULL);
-		free(client);
-		return NULL;
-	}
+	if (!client->svc_chngd_queue)
+		goto fail;
 
 	client->notify_list = queue_new();
-	if (!client->notify_list) {
-		queue_destroy(client->svc_chngd_queue, NULL);
-		queue_destroy(client->long_write_queue, NULL);
-		free(client);
-		return NULL;
-	}
+	if (!client->notify_list)
+		goto fail;
 
 	client->notify_id = bt_att_register(att, BT_ATT_OP_HANDLE_VAL_NOT,
 						notify_cb, client, NULL);
-	if (!client->notify_id) {
-		queue_destroy(client->notify_list, NULL);
-		queue_destroy(client->svc_chngd_queue, NULL);
-		queue_destroy(client->long_write_queue, NULL);
-		free(client);
-		return NULL;
-	}
+	if (!client->notify_id)
+		goto fail;
 
 	client->ind_id = bt_att_register(att, BT_ATT_OP_HANDLE_VAL_IND,
 						notify_cb, client, NULL);
-	if (!client->ind_id) {
-		bt_att_unregister(att, client->notify_id);
-		queue_destroy(client->notify_list, NULL);
-		queue_destroy(client->svc_chngd_queue, NULL);
-		queue_destroy(client->long_write_queue, NULL);
-		free(client);
-		return NULL;
-	}
+	if (!client->ind_id)
+		goto fail;
 
 	client->att = bt_att_ref(att);
 
 	gatt_client_init(client, mtu);
 
 	return bt_gatt_client_ref(client);
+
+fail:
+	bt_gatt_client_free(client);
+	return NULL;
 }
 
 struct bt_gatt_client *bt_gatt_client_ref(struct bt_gatt_client *client)
@@ -1281,8 +1288,6 @@ struct bt_gatt_client *bt_gatt_client_ref(struct bt_gatt_client *client)
 	return client;
 }
 
-static void long_write_op_unref(void *data);
-
 void bt_gatt_client_unref(struct bt_gatt_client *client)
 {
 	if (!client)
@@ -1291,23 +1296,7 @@ void bt_gatt_client_unref(struct bt_gatt_client *client)
 	if (__sync_sub_and_fetch(&client->ref_count, 1))
 		return;
 
-	if (client->ready_destroy)
-		client->ready_destroy(client->ready_data);
-
-	if (client->debug_destroy)
-		client->debug_destroy(client->debug_data);
-
-	bt_att_unregister(client->att, client->notify_id);
-	bt_att_unregister(client->att, client->ind_id);
-
-	queue_destroy(client->svc_chngd_queue, free);
-	queue_destroy(client->long_write_queue, long_write_op_unref);
-	queue_destroy(client->notify_list, notify_data_unref);
-
-	gatt_client_clear_services(client);
-
-	bt_att_unref(client->att);
-	free(client);
+	bt_gatt_client_free(client);
 }
 
 bool bt_gatt_client_is_ready(struct bt_gatt_client *client)
-- 
1.9.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH BlueZ v3 2/5] unit/test-gatt: Add TP/GAC/CL/BV-01-C test
  2014-10-02  9:51 [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new Luiz Augusto von Dentz
@ 2014-10-02  9:51 ` Luiz Augusto von Dentz
  2014-10-02  9:51 ` [PATCH BlueZ v3 3/5] shared/gatt-client: Fix crash on bt_gatt_client_unref Luiz Augusto von Dentz
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2014-10-02  9:51 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Verify that a Generic Attribute Profile client can generate an
Exchange MTU Request command.
---
 Makefile.am      |   7 ++
 unit/test-gatt.c | 266 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 273 insertions(+)
 create mode 100644 unit/test-gatt.c

diff --git a/Makefile.am b/Makefile.am
index ebd5b44..eba594d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -363,6 +363,13 @@ unit_tests += unit/test-lib
 unit_test_lib_SOURCES = unit/test-lib.c
 unit_test_lib_LDADD = lib/libbluetooth-internal.la @GLIB_LIBS@
 
+unit_tests += unit/test-gatt
+
+unit_test_gatt_SOURCES = unit/test-gatt.c
+unit_test_gatt_LDADD = src/libshared-glib.la lib/libbluetooth-internal.la \
+			@GLIB_LIBS@
+
+
 noinst_PROGRAMS += $(unit_tests)
 
 TESTS = $(unit_tests)
diff --git a/unit/test-gatt.c b/unit/test-gatt.c
new file mode 100644
index 0000000..bbbf9a5
--- /dev/null
+++ b/unit/test-gatt.c
@@ -0,0 +1,266 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2014  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/socket.h>
+
+#include <glib.h>
+
+#include "src/shared/util.h"
+#include "src/shared/att.h"
+#include "src/shared/gatt-client.h"
+
+struct test_pdu {
+	bool valid;
+	const uint8_t *data;
+	size_t size;
+};
+
+struct test_data {
+	char *test_name;
+	struct test_pdu *pdu_list;
+};
+
+struct context {
+	GMainLoop *main_loop;
+	struct bt_gatt_client *client;
+	guint source;
+	guint process;
+	int fd;
+	unsigned int pdu_offset;
+	const struct test_data *data;
+};
+
+#define data(args...) ((const unsigned char[]) { args })
+
+#define raw_pdu(args...)					\
+	{							\
+		.valid = true,					\
+		.data = data(args),				\
+		.size = sizeof(data(args)),			\
+	}
+
+#define define_test(name, function, args...)				\
+	do {								\
+		const struct test_pdu pdus[] = {			\
+			args, { }					\
+		};							\
+		static struct test_data data;				\
+		data.test_name = g_strdup(name);			\
+		data.pdu_list = g_malloc(sizeof(pdus));			\
+		memcpy(data.pdu_list, pdus, sizeof(pdus));		\
+		g_test_add_data_func(name, &data, function);		\
+	} while (0)
+
+static void test_debug(const char *str, void *user_data)
+{
+	const char *prefix = user_data;
+
+	g_print("%s%s\n", prefix, str);
+}
+
+static void test_free(gconstpointer user_data)
+{
+	const struct test_data *data = user_data;
+
+	g_free(data->test_name);
+	g_free(data->pdu_list);
+}
+
+static gboolean context_quit(gpointer user_data)
+{
+	struct context *context = user_data;
+
+	if (context->process > 0)
+		g_source_remove(context->process);
+
+	g_main_loop_quit(context->main_loop);
+
+	return FALSE;
+}
+
+static gboolean send_pdu(gpointer user_data)
+{
+	struct context *context = user_data;
+	const struct test_pdu *pdu;
+	ssize_t len;
+
+	pdu = &context->data->pdu_list[context->pdu_offset++];
+
+	len = write(context->fd, pdu->data, pdu->size);
+
+	if (g_test_verbose())
+		util_hexdump('<', pdu->data, len, test_debug, "GATT: ");
+
+	g_assert_cmpint(len, ==, pdu->size);
+
+	context->process = 0;
+	return FALSE;
+}
+
+static void context_process(struct context *context)
+{
+	if (!context->data->pdu_list[context->pdu_offset].valid) {
+		context_quit(context);
+		return;
+	}
+
+	context->process = g_idle_add(send_pdu, context);
+}
+
+static gboolean test_handler(GIOChannel *channel, GIOCondition cond,
+							gpointer user_data)
+{
+	struct context *context = user_data;
+	const struct test_pdu *pdu;
+	unsigned char buf[512];
+	ssize_t len;
+	int fd;
+
+	pdu = &context->data->pdu_list[context->pdu_offset++];
+
+	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
+		context->source = 0;
+		g_print("%s: cond %x\n", __func__, cond);
+		return FALSE;
+	}
+
+	fd = g_io_channel_unix_get_fd(channel);
+
+	len = read(fd, buf, sizeof(buf));
+
+	g_assert(len > 0);
+
+	if (g_test_verbose())
+		util_hexdump('>', buf, len, test_debug, "GATT: ");
+
+	g_assert_cmpint(len, ==, pdu->size);
+
+	g_assert(memcmp(buf, pdu->data, pdu->size) == 0);
+
+	context_process(context);
+
+	return TRUE;
+}
+
+static void gatt_debug(const char *str, void *user_data)
+{
+	const char *prefix = user_data;
+
+	g_print("%s%s\n", prefix, str);
+}
+
+static struct context *create_context(uint16_t mtu, gconstpointer data)
+{
+	struct context *context = g_new0(struct context, 1);
+	GIOChannel *channel;
+	int err, sv[2];
+	struct bt_att *att;
+
+	context->main_loop = g_main_loop_new(NULL, FALSE);
+	g_assert(context->main_loop);
+
+	err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
+	g_assert(err == 0);
+
+	att = bt_att_new(sv[0]);
+	g_assert(att);
+
+	context->client = bt_gatt_client_new(att, mtu);
+	g_assert(context->client);
+
+	if (g_test_verbose())
+		bt_gatt_client_set_debug(context->client, gatt_debug, "gatt:",
+									NULL);
+
+	channel = g_io_channel_unix_new(sv[1]);
+
+	g_io_channel_set_close_on_unref(channel, TRUE);
+	g_io_channel_set_encoding(channel, NULL, NULL);
+	g_io_channel_set_buffered(channel, FALSE);
+
+	context->source = g_io_add_watch(channel,
+				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+				test_handler, context);
+	g_assert(context->source > 0);
+
+	g_io_channel_unref(channel);
+	bt_att_unref(att);
+
+	context->fd = sv[1];
+	context->data = data;
+
+	return context;
+}
+
+static void destroy_context(struct context *context)
+{
+	if (context->source > 0)
+		g_source_remove(context->source);
+
+	bt_gatt_client_unref(context->client);
+
+	g_main_loop_unref(context->main_loop);
+
+	test_free(context->data);
+	g_free(context);
+}
+
+static void execute_context(struct context *context)
+{
+	g_main_loop_run(context->main_loop);
+
+	destroy_context(context);
+}
+
+static void test_client(gconstpointer data)
+{
+	struct context *context = create_context(512, data);
+
+	execute_context(context);
+}
+
+int main(int argc, char *argv[])
+{
+	g_test_init(&argc, &argv, NULL);
+
+	/*
+	 * Server Configuration
+	 *
+	 * The test group objective is to verify Generic Attribute Profile
+	 * Server Configuration.
+	 */
+	define_test("/TP/GAC/CL/BV-01-C", test_client,
+				raw_pdu(0x02, 0x00, 0x02));
+
+	return g_test_run();
+}
-- 
1.9.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH BlueZ v3 3/5] shared/gatt-client: Fix crash on bt_gatt_client_unref
  2014-10-02  9:51 [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new Luiz Augusto von Dentz
  2014-10-02  9:51 ` [PATCH BlueZ v3 2/5] unit/test-gatt: Add TP/GAC/CL/BV-01-C test Luiz Augusto von Dentz
@ 2014-10-02  9:51 ` Luiz Augusto von Dentz
  2014-10-02  9:51 ` [PATCH BlueZ v3 4/5] shared/gatt-client: Fix gatt_client_init Luiz Augusto von Dentz
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2014-10-02  9:51 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Calling gatt_client_clear_services after notify_list is destroyed cause
the following backtrace:

Invalid read of size 8
   at 0x404CC9: queue_remove_all (queue.c:312)
   by 0x401FC6: gatt_client_remove_all_notify_in_range (gatt-client.c:350)
   by 0x403170: bt_gatt_client_free (gatt-client.c:357)
   by 0x401A93: test_client (test-gatt.c:224)
   by 0x4E9E5E0: ??? (in /usr/lib64/libglib-2.0.so.0.3800.2)
   by 0x4E9E7A5: ??? (in /usr/lib64/libglib-2.0.so.0.3800.2)
   by 0x4E9E7A5: ??? (in /usr/lib64/libglib-2.0.so.0.3800.2)
   by 0x4E9E7A5: ??? (in /usr/lib64/libglib-2.0.so.0.3800.2)
   by 0x4E9EB1A: g_test_run_suite (in /usr/lib64/libglib-2.0.so.0.3800.2)
   by 0x4015EE: main (test-gatt.c:259)
 Address 0x5752718 is 8 bytes inside a block of size 32 free'd
   at 0x4C28577: free (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
   by 0x40315E: bt_gatt_client_free (gatt-client.c:1233)
   by 0x401A93: test_client (test-gatt.c:224)
   by 0x4E9E5E0: ??? (in /usr/lib64/libglib-2.0.so.0.3800.2)
   by 0x4E9E7A5: ??? (in /usr/lib64/libglib-2.0.so.0.3800.2)
   by 0x4E9E7A5: ??? (in /usr/lib64/libglib-2.0.so.0.3800.2)
   by 0x4E9E7A5: ??? (in /usr/lib64/libglib-2.0.so.0.3800.2)
   by 0x4E9EB1A: g_test_run_suite (in /usr/lib64/libglib-2.0.so.0.3800.2)
   by 0x4015EE: main (test-gatt.c:259)
---
 src/shared/gatt-client.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 782e6b3..ddedaf0 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -1224,12 +1224,12 @@ static void bt_gatt_client_free(struct bt_gatt_client *client)
 	bt_att_unregister(client->att, client->notify_id);
 	bt_att_unregister(client->att, client->ind_id);
 
+	gatt_client_clear_services(client);
+
 	queue_destroy(client->svc_chngd_queue, free);
 	queue_destroy(client->long_write_queue, long_write_op_unref);
 	queue_destroy(client->notify_list, notify_data_unref);
 
-	gatt_client_clear_services(client);
-
 	bt_att_unref(client->att);
 	free(client);
 }
-- 
1.9.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH BlueZ v3 4/5] shared/gatt-client: Fix gatt_client_init
  2014-10-02  9:51 [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new Luiz Augusto von Dentz
  2014-10-02  9:51 ` [PATCH BlueZ v3 2/5] unit/test-gatt: Add TP/GAC/CL/BV-01-C test Luiz Augusto von Dentz
  2014-10-02  9:51 ` [PATCH BlueZ v3 3/5] shared/gatt-client: Fix crash on bt_gatt_client_unref Luiz Augusto von Dentz
@ 2014-10-02  9:51 ` Luiz Augusto von Dentz
  2014-10-02  9:51 ` [PATCH BlueZ v3 5/5] shared/gatt-client: Handle ATT disconnection Luiz Augusto von Dentz
  2014-10-03  7:36 ` [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new Luiz Augusto von Dentz
  4 siblings, 0 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2014-10-02  9:51 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This function is only called within bt_gatt_client_new therefore it is
not possible to have a ready callback and should return false if
bt_gatt_exchange_mtu fails.
---
 src/shared/gatt-client.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index ddedaf0..5833e7a 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -1005,10 +1005,8 @@ static bool gatt_client_init(struct bt_gatt_client *client, uint16_t mtu)
 							exchange_mtu_cb,
 							discovery_op_ref(op),
 							discovery_op_unref)) {
-		if (client->ready_callback)
-			client->ready_callback(false, 0, client->ready_data);
-
 		free(op);
+		return false;
 	}
 
 	client->in_init = true;
@@ -1269,7 +1267,8 @@ struct bt_gatt_client *bt_gatt_client_new(struct bt_att *att, uint16_t mtu)
 
 	client->att = bt_att_ref(att);
 
-	gatt_client_init(client, mtu);
+	if (!gatt_client_init(client, mtu))
+		goto fail;
 
 	return bt_gatt_client_ref(client);
 
-- 
1.9.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH BlueZ v3 5/5] shared/gatt-client: Handle ATT disconnection
  2014-10-02  9:51 [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new Luiz Augusto von Dentz
                   ` (2 preceding siblings ...)
  2014-10-02  9:51 ` [PATCH BlueZ v3 4/5] shared/gatt-client: Fix gatt_client_init Luiz Augusto von Dentz
@ 2014-10-02  9:51 ` Luiz Augusto von Dentz
  2014-10-03  7:36 ` [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new Luiz Augusto von Dentz
  4 siblings, 0 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2014-10-02  9:51 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

If ATT disconnects reset ready flag and propagate to the callback, also
since there is no use for bt_att after that unref it.
---
 src/shared/gatt-client.c | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 5833e7a..03d722f 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -101,10 +101,10 @@ struct bt_gatt_client {
 	struct queue *long_write_queue;
 	bool in_long_write;
 
-	/* List of registered notification/indication callbacks */
+	/* List of registered disconnect/notification/indication callbacks */
 	struct queue *notify_list;
 	int next_reg_id;
-	unsigned int notify_id, ind_id;
+	unsigned int disc_id, notify_id, ind_id;
 	bool in_notify;
 	bool need_notify_cleanup;
 
@@ -1219,8 +1219,12 @@ static void bt_gatt_client_free(struct bt_gatt_client *client)
 	if (client->debug_destroy)
 		client->debug_destroy(client->debug_data);
 
-	bt_att_unregister(client->att, client->notify_id);
-	bt_att_unregister(client->att, client->ind_id);
+	if (client->att) {
+		bt_att_unregister_disconnect(client->att, client->disc_id);
+		bt_att_unregister(client->att, client->notify_id);
+		bt_att_unregister(client->att, client->ind_id);
+		bt_att_unref(client->att);
+	}
 
 	gatt_client_clear_services(client);
 
@@ -1228,10 +1232,25 @@ static void bt_gatt_client_free(struct bt_gatt_client *client)
 	queue_destroy(client->long_write_queue, long_write_op_unref);
 	queue_destroy(client->notify_list, notify_data_unref);
 
-	bt_att_unref(client->att);
 	free(client);
 }
 
+static void att_disconnect_cb(void *user_data)
+{
+	struct bt_gatt_client *client = user_data;
+
+	client->disc_id = 0;
+
+	bt_att_unref(client->att);
+	client->att = NULL;
+
+	client->in_init = false;
+	client->ready = false;
+
+	if (client->ready_callback)
+		client->ready_callback(false, 0, client->ready_data);
+}
+
 struct bt_gatt_client *bt_gatt_client_new(struct bt_att *att, uint16_t mtu)
 {
 	struct bt_gatt_client *client;
@@ -1243,6 +1262,11 @@ struct bt_gatt_client *bt_gatt_client_new(struct bt_att *att, uint16_t mtu)
 	if (!client)
 		return NULL;
 
+	client->disc_id = bt_att_register_disconnect(att, att_disconnect_cb,
+								client, NULL);
+	if (!client->disc_id)
+		goto fail;
+
 	client->long_write_queue = queue_new();
 	if (!client->long_write_queue)
 		goto fail;
-- 
1.9.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new
  2014-10-02  9:51 [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new Luiz Augusto von Dentz
                   ` (3 preceding siblings ...)
  2014-10-02  9:51 ` [PATCH BlueZ v3 5/5] shared/gatt-client: Handle ATT disconnection Luiz Augusto von Dentz
@ 2014-10-03  7:36 ` Luiz Augusto von Dentz
  4 siblings, 0 replies; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2014-10-03  7:36 UTC (permalink / raw)
  To: linux-bluetooth

Hi,

On Thu, Oct 2, 2014 at 12:51 PM, Luiz Augusto von Dentz
<luiz.dentz@gmail.com> wrote:
> From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
>
> This split bt_gatt_client_unref into bt_gatt_client_free so it can be
> used within bt_gatt_client_new when freeing the data.
> ---
> v3: Add changes to handle ATT disconnection
>
>  src/shared/gatt-client.c | 87 +++++++++++++++++++++---------------------------
>  1 file changed, 38 insertions(+), 49 deletions(-)
>
> diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
> index 6dc8e95..782e6b3 100644
> --- a/src/shared/gatt-client.c
> +++ b/src/shared/gatt-client.c
> @@ -1211,6 +1211,29 @@ static void notify_cb(uint8_t opcode, const void *pdu, uint16_t length,
>         bt_gatt_client_unref(client);
>  }
>
> +static void long_write_op_unref(void *data);
> +
> +static void bt_gatt_client_free(struct bt_gatt_client *client)
> +{
> +       if (client->ready_destroy)
> +               client->ready_destroy(client->ready_data);
> +
> +       if (client->debug_destroy)
> +               client->debug_destroy(client->debug_data);
> +
> +       bt_att_unregister(client->att, client->notify_id);
> +       bt_att_unregister(client->att, client->ind_id);
> +
> +       queue_destroy(client->svc_chngd_queue, free);
> +       queue_destroy(client->long_write_queue, long_write_op_unref);
> +       queue_destroy(client->notify_list, notify_data_unref);
> +
> +       gatt_client_clear_services(client);
> +
> +       bt_att_unref(client->att);
> +       free(client);
> +}
> +
>  struct bt_gatt_client *bt_gatt_client_new(struct bt_att *att, uint16_t mtu)
>  {
>         struct bt_gatt_client *client;
> @@ -1223,52 +1246,36 @@ struct bt_gatt_client *bt_gatt_client_new(struct bt_att *att, uint16_t mtu)
>                 return NULL;
>
>         client->long_write_queue = queue_new();
> -       if (!client->long_write_queue) {
> -               free(client);
> -               return NULL;
> -       }
> +       if (!client->long_write_queue)
> +               goto fail;
>
>         client->svc_chngd_queue = queue_new();
> -       if (!client->svc_chngd_queue) {
> -               queue_destroy(client->long_write_queue, NULL);
> -               free(client);
> -               return NULL;
> -       }
> +       if (!client->svc_chngd_queue)
> +               goto fail;
>
>         client->notify_list = queue_new();
> -       if (!client->notify_list) {
> -               queue_destroy(client->svc_chngd_queue, NULL);
> -               queue_destroy(client->long_write_queue, NULL);
> -               free(client);
> -               return NULL;
> -       }
> +       if (!client->notify_list)
> +               goto fail;
>
>         client->notify_id = bt_att_register(att, BT_ATT_OP_HANDLE_VAL_NOT,
>                                                 notify_cb, client, NULL);
> -       if (!client->notify_id) {
> -               queue_destroy(client->notify_list, NULL);
> -               queue_destroy(client->svc_chngd_queue, NULL);
> -               queue_destroy(client->long_write_queue, NULL);
> -               free(client);
> -               return NULL;
> -       }
> +       if (!client->notify_id)
> +               goto fail;
>
>         client->ind_id = bt_att_register(att, BT_ATT_OP_HANDLE_VAL_IND,
>                                                 notify_cb, client, NULL);
> -       if (!client->ind_id) {
> -               bt_att_unregister(att, client->notify_id);
> -               queue_destroy(client->notify_list, NULL);
> -               queue_destroy(client->svc_chngd_queue, NULL);
> -               queue_destroy(client->long_write_queue, NULL);
> -               free(client);
> -               return NULL;
> -       }
> +       if (!client->ind_id)
> +               goto fail;
>
>         client->att = bt_att_ref(att);
>
>         gatt_client_init(client, mtu);
>
>         return bt_gatt_client_ref(client);
> +
> +fail:
> +       bt_gatt_client_free(client);
> +       return NULL;
>  }
>
>  struct bt_gatt_client *bt_gatt_client_ref(struct bt_gatt_client *client)
> @@ -1281,8 +1288,6 @@ struct bt_gatt_client *bt_gatt_client_ref(struct bt_gatt_client *client)
>         return client;
>  }
>
> -static void long_write_op_unref(void *data);
> -
>  void bt_gatt_client_unref(struct bt_gatt_client *client)
>  {
>         if (!client)
> @@ -1291,23 +1296,7 @@ void bt_gatt_client_unref(struct bt_gatt_client *client)
>         if (__sync_sub_and_fetch(&client->ref_count, 1))
>                 return;
>
> -       if (client->ready_destroy)
> -               client->ready_destroy(client->ready_data);
> -
> -       if (client->debug_destroy)
> -               client->debug_destroy(client->debug_data);
> -
> -       bt_att_unregister(client->att, client->notify_id);
> -       bt_att_unregister(client->att, client->ind_id);
> -
> -       queue_destroy(client->svc_chngd_queue, free);
> -       queue_destroy(client->long_write_queue, long_write_op_unref);
> -       queue_destroy(client->notify_list, notify_data_unref);
> -
> -       gatt_client_clear_services(client);
> -
> -       bt_att_unref(client->att);
> -       free(client);
> +       bt_gatt_client_free(client);
>  }
>
>  bool bt_gatt_client_is_ready(struct bt_gatt_client *client)
> --
> 1.9.3

Applied.


-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-10-03  7:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-02  9:51 [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new Luiz Augusto von Dentz
2014-10-02  9:51 ` [PATCH BlueZ v3 2/5] unit/test-gatt: Add TP/GAC/CL/BV-01-C test Luiz Augusto von Dentz
2014-10-02  9:51 ` [PATCH BlueZ v3 3/5] shared/gatt-client: Fix crash on bt_gatt_client_unref Luiz Augusto von Dentz
2014-10-02  9:51 ` [PATCH BlueZ v3 4/5] shared/gatt-client: Fix gatt_client_init Luiz Augusto von Dentz
2014-10-02  9:51 ` [PATCH BlueZ v3 5/5] shared/gatt-client: Handle ATT disconnection Luiz Augusto von Dentz
2014-10-03  7:36 ` [PATCH BlueZ v3 1/5] shared/gatt-client: Simplify bt_gatt_client_new Luiz Augusto von Dentz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.