Linux-Bluetooth Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] bluetoothctl: Add support for discover characteristic by uuid
@ 2020-06-05 14:25 Amitsi5x
  2020-06-05 14:25 ` [PATCH] bluetoothctl:Add support for discover service " Amitsi5x
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Amitsi5x @ 2020-06-05 14:25 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: amitx.k.singh

From: amit <amitx.k.singh@intel.com>

Changes made to add support for discovering gatt characteristic
by uuid.

Signed-off-by: amit <amitx.k.singh@intel.com>
---
 client/gatt.c            |  67 +++++++++++++++++++++
 client/gatt.h            |   1 +
 client/main.c            |  17 ++++++
 src/gatt-client.c        |  69 +++++++++++++++++++++
 src/shared/gatt-client.c | 125 +++++++++++++++++++++++++++++++++++++++
 src/shared/gatt-client.h |   8 +++
 6 files changed, 287 insertions(+)

diff --git a/client/gatt.c b/client/gatt.c
index 21e251d2e..53f875050 100644
--- a/client/gatt.c
+++ b/client/gatt.c
@@ -681,6 +681,73 @@ void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[])
 	return bt_shell_noninteractive_quit(EXIT_FAILURE);
 }
 
+static void charbyuuid_reply(DBusMessage *message, void *user_data)
+{
+	DBusError error;
+	DBusMessageIter iter, array;
+	uint8_t *value;
+	int len;
+
+	dbus_error_init(&error);
+
+	if (dbus_set_error_from_message(&error, message) == TRUE) {
+		bt_shell_printf("Failed to read: %s\n", error.name);
+		dbus_error_free(&error);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	dbus_message_iter_init(message, &iter);
+	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
+		bt_shell_printf("Invalid response to read\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	dbus_message_iter_recurse(&iter, &array);
+	dbus_message_iter_get_fixed_array(&array, &value, &len);
+
+	if (len < 0) {
+		bt_shell_printf("Unable to parse value\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+}
+
+static void charbyuuid_setup(DBusMessageIter *iter, void *user_data)
+{
+	char *uuid = user_data;
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
+}
+
+static void charbyuuid_attribute(GDBusProxy *proxy, char *uuid)
+{
+	if (g_dbus_proxy_method_call(proxy, "CharByUUID", charbyuuid_setup, charbyuuid_reply,
+						uuid, NULL) == FALSE) {
+		bt_shell_printf("Failed to set uuid\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	bt_shell_printf("Attempting to read service handle %s\n", g_dbus_proxy_get_path(proxy));
+}
+
+void gatt_charbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[])
+{
+	const char *iface;
+
+	iface = g_dbus_proxy_get_interface(proxy);
+
+	if (!strcmp(iface, "org.bluez.GattCharacteristic1")) {
+		charbyuuid_attribute(proxy, argv[1]);
+		return;
+	}
+
+	bt_shell_printf("Unable to read attribute %s\n",
+						g_dbus_proxy_get_path(proxy));
+
+	return bt_shell_noninteractive_quit(EXIT_FAILURE);
+}
+
 static void servbyuuid_reply(DBusMessage *message, void *user_data)
 {
 	DBusError error;
diff --git a/client/gatt.h b/client/gatt.h
index 8757d6b48..692fb5758 100644
--- a/client/gatt.h
+++ b/client/gatt.h
@@ -34,6 +34,7 @@ void gatt_list_attributes(const char *device);
 GDBusProxy *gatt_select_attribute(GDBusProxy *parent, const char *path);
 char *gatt_attribute_generator(const char *text, int state);
 void gatt_servbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[]);
+void gatt_charbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[]);
 void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[]);
 void gatt_write_attribute(GDBusProxy *proxy, int argc, char *argv[]);
 void gatt_notify_attribute(GDBusProxy *proxy, bool enable);
diff --git a/client/main.c b/client/main.c
index 79a08728b..10e64e17b 100644
--- a/client/main.c
+++ b/client/main.c
@@ -2071,6 +2071,21 @@ static void cmd_attribute_info(int argc, char *argv[])
 	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
+static void cmd_char_by_uuid(int argc, char *argv[])
+{
+	GDBusProxy *proxy;
+
+	proxy = find_attribute(argc, argv);
+	set_default_attribute(proxy);
+
+	if (!default_attr) {
+		bt_shell_printf("No attribute selected\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	gatt_charbyuuid_attribute(default_attr, argc, argv);
+}
+
 static void cmd_primary_by_uuid(int argc, char *argv[])
 {
 	GDBusProxy *proxy;
@@ -2701,6 +2716,8 @@ static const struct bt_shell_menu gatt_menu = {
 				"List attributes", dev_generator },
 	{ "primary-by-uuid", "[UUID]", cmd_primary_by_uuid,
 				"Discover Primary Services by UUID" },
+	{ "char-by-uuid", "[UUID]", cmd_char_by_uuid,
+				"Discover Characteristic Services by UUID" },
 	{ "select-attribute", "<attribute/UUID>",  cmd_select_attribute,
 				"Select attribute", attribute_generator },
 	{ "attribute-info", "[attribute/UUID]",  cmd_attribute_info,
diff --git a/src/gatt-client.c b/src/gatt-client.c
index daedae939..da811ea4f 100644
--- a/src/gatt-client.c
+++ b/src/gatt-client.c
@@ -444,6 +444,27 @@ static struct async_dbus_op *async_dbus_op_new(DBusMessage *msg, void *data)
 	return op;
 }
 
+static struct async_dbus_op *fetch_char_by_uuid(struct bt_gatt_client *gatt,
+					DBusMessage *msg,
+					char *uuid,
+					bt_gatt_client_char_by_uuid_callback_t callback,
+					void *data)
+{
+	struct async_dbus_op *op;
+
+	op = async_dbus_op_new(msg, data);
+	op->id = bt_gatt_client_char_by_uuid(gatt, uuid, callback,
+						async_dbus_op_ref(op),
+						async_dbus_op_unref);
+
+	if (op->id)
+		return op;
+
+	async_dbus_op_free(op);
+
+	return NULL;
+}
+
 static struct async_dbus_op *fetch_service_by_uuid(struct bt_gatt_client *gatt,
 					DBusMessage *msg,
 					char *uuid,
@@ -951,6 +972,51 @@ fail:
 	chrc->read_op = NULL;
 }
 
+static void characteristic_by_uuid_cb(bool success, uint8_t att_ecode, const uint8_t *value,
+					uint16_t length, void *user_data)
+{
+	struct async_dbus_op *op = user_data;
+	struct characteristic *opchar = op->data;
+
+	if (!success)
+		goto fail;
+
+	async_dbus_op_reply(op, att_ecode, value, length);
+
+	return;
+
+fail:
+	async_dbus_op_reply(op, att_ecode, NULL, 0);
+	opchar->type_op = NULL;
+}
+
+static DBusMessage *chardiscover_by_uuid(DBusConnection *conn,
+					DBusMessage *msg, void *user_data)
+{
+	struct characteristic *chardata = user_data;
+	struct bt_gatt_client *gatt = chardata->service->client->gatt;
+	DBusMessageIter iter;
+
+	char *uuid = 0;
+
+	if (!gatt)
+		return btd_error_failed(msg, "Not connected");
+
+	dbus_message_iter_init(msg, &iter);
+
+	if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
+		dbus_message_iter_get_basic(&iter,&uuid);
+	else
+		return NULL;
+
+	chardata->type_op = fetch_char_by_uuid(gatt, msg,uuid, characteristic_by_uuid_cb, chardata);
+
+	if (!chardata->type_op)
+		return btd_error_failed(msg, "Failed to send read request");
+
+	return NULL;
+}
+
 static void serv_uuid_cb(bool success, uint8_t att_ecode, const uint8_t *value,
 					uint16_t length, void *user_data)
 {
@@ -1717,6 +1783,9 @@ static const GDBusPropertyTable characteristic_properties[] = {
 };
 
 static const GDBusMethodTable characteristic_methods[] = {
+	{ GDBUS_ASYNC_METHOD("CharByUUID", GDBUS_ARGS({ "options", "s" }),
+					GDBUS_ARGS({ "value", "ay" }),
+					chardiscover_by_uuid) },
 	{ GDBUS_ASYNC_METHOD("ReadValue", GDBUS_ARGS({ "options", "a{sv}" }),
 					GDBUS_ARGS({ "value", "ay" }),
 					characteristic_read_value) },
diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 88257c054..8a696c77f 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -131,6 +131,13 @@ struct request {
 	void (*destroy)(void *);
 };
 
+struct char_by_uuid_op
+{
+	bt_gatt_client_char_by_uuid_callback_t callback;
+	bt_gatt_client_destroy_func_t destroy;
+	void *user_data;
+};
+
 struct service_by_uuid_op
 {
 	bt_gatt_client_service_by_uuid_callback_t callback;
@@ -2585,6 +2592,16 @@ bool bt_gatt_client_cancel_all(struct bt_gatt_client *client)
 	return true;
 }
 
+static void destroy_char_by_uuid_op(void *data)
+{
+	struct char_by_uuid_op *op = data;
+
+	if (op->destroy)
+		op->destroy(op->user_data);
+
+	free(op);
+}
+
 static void destroy_service_by_uuid_op(void *data)
 {
 	struct service_by_uuid_op *op = data;
@@ -2595,6 +2612,39 @@ static void destroy_service_by_uuid_op(void *data)
 	free(op);
 }
 
+static void char_by_uuid_cb(uint8_t opcode, const void *pdu, uint16_t length,
+								void *user_data)
+{
+	struct request *req = user_data;
+	struct char_by_uuid_op *op = req->data;
+	bool success;
+
+	uint8_t att_ecode = 0;
+	const uint8_t *value = NULL;
+	uint16_t value_len = 0;
+
+	if (opcode == BT_ATT_OP_ERROR_RSP) {
+		success = false;
+		att_ecode = process_error(pdu, length);
+		goto done;
+	}
+
+	if (opcode != BT_ATT_OP_READ_BY_TYPE_RSP || (!pdu && length)) {
+		success = false;
+		goto done;
+	}
+
+	success = true;
+	value_len = length;
+
+	if (value_len)
+		value = pdu;
+
+done:
+	if (op->callback)
+		op->callback(success, att_ecode, value, length, op->user_data);
+}
+
 static void service_by_uuid_cb(uint8_t opcode, const void *pdu, uint16_t length,
 								void *user_data)
 {
@@ -2675,6 +2725,81 @@ done:
 		op->callback(success, att_ecode, value, length, op->user_data);
 }
 
+unsigned int bt_gatt_client_char_by_uuid(struct bt_gatt_client *client,
+                                              char *uuid,
+                                              bt_gatt_client_char_by_uuid_callback_t callback,
+					      void *user_data,
+                                              bt_gatt_client_destroy_func_t destroy)
+{
+	struct request *req;
+	struct char_by_uuid_op *op;
+	unsigned char *pdu;
+	uint16_t len ;
+	uint16_t start_handle = 0x0001;
+	uint16_t end_handle = 0xffff;
+	bt_uuid_t btuuid;
+	uint8_t uuid128[16];
+
+	/* Length of pdu will be vary according to uuid type
+	for 2 byte uuid total length  is 8 (start handle(2) + end handle(2)  + uuid(2))
+	for 16 byte uuid total length  is 22 (start handle(2) + end handle(2)  + uuid(16))
+	*/
+	uint16_t pdu_len_16bit_uuid = 6;
+	uint16_t pdu_len_128bit_uuid = 20;
+
+	if (bt_string_to_uuid(&btuuid, uuid) < 0) {
+		return 0;
+	}
+
+	if (btuuid.type == BT_UUID16){
+		pdu = (unsigned char *) malloc(pdu_len_16bit_uuid);
+		len = pdu_len_16bit_uuid;
+	} else {
+		pdu = (unsigned char *) malloc(pdu_len_128bit_uuid);
+		len = pdu_len_128bit_uuid;
+	}
+
+	if (!client)
+		return 0;
+
+	op = new0(struct char_by_uuid_op, 1);
+	req = request_create(client);
+
+	if (!req) {
+		free(op);
+		return 0;
+	}
+
+	op->callback = callback;
+	op->user_data = user_data;
+	op->destroy = destroy;
+	req->data = op;
+	req->destroy = destroy_char_by_uuid_op;
+
+	put_le16(start_handle, pdu);
+	put_le16(end_handle, pdu+2);
+
+	if (btuuid.type == BT_UUID16)
+		put_le16(btuuid.value.u16, pdu+4);
+	else {
+		for (int i =0 ; i<16 ; i++)
+			uuid128[15-i]=btuuid.value.u128.data[i];
+		memcpy(pdu + 4, uuid128, 16);
+	}
+
+	req->att_id = bt_att_send(client->att, BT_ATT_OP_READ_BY_TYPE_REQ,
+							pdu, len,
+							char_by_uuid_cb, req,
+							request_unref);
+	if (!req->att_id) {
+		op->destroy = NULL;
+		request_unref(req);
+		return 0;
+	}
+
+	return req->id;
+}
+
 unsigned int bt_gatt_client_service_by_uuid(struct bt_gatt_client *client,
 						char *uuid,
 						bt_gatt_client_service_by_uuid_callback_t callback,
diff --git a/src/shared/gatt-client.h b/src/shared/gatt-client.h
index 599e98556..f5d5169ce 100644
--- a/src/shared/gatt-client.h
+++ b/src/shared/gatt-client.h
@@ -45,6 +45,9 @@ typedef void (*bt_gatt_client_debug_func_t)(const char *str, void *user_data);
 typedef void (*bt_gatt_client_service_by_uuid_callback_t)(bool success, uint8_t att_ecode,
 					const uint8_t *value, uint16_t length,
 					void *user_data);
+typedef void (*bt_gatt_client_char_by_uuid_callback_t)(bool success, uint8_t att_ecode,
+					const uint8_t *value, uint16_t length,
+					void *user_data);
 typedef void (*bt_gatt_client_read_callback_t)(bool success, uint8_t att_ecode,
 					const uint8_t *value, uint16_t length,
 					void *user_data);
@@ -89,6 +92,11 @@ unsigned int bt_gatt_client_service_by_uuid(struct bt_gatt_client *client,
 					bt_gatt_client_read_callback_t callback,
 					void *user_data,
 					bt_gatt_client_destroy_func_t destroy);
+unsigned int bt_gatt_client_char_by_uuid(struct bt_gatt_client *client,
+					char *uuid,
+					bt_gatt_client_read_callback_t callback,
+					void *user_data,
+					bt_gatt_client_destroy_func_t destroy);
 unsigned int bt_gatt_client_read_value(struct bt_gatt_client *client,
 					uint16_t value_handle,
 					bt_gatt_client_read_callback_t callback,
-- 
2.17.1


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

* [PATCH] bluetoothctl:Add support for discover service by uuid
  2020-06-05 14:25 [PATCH] bluetoothctl: Add support for discover characteristic by uuid Amitsi5x
@ 2020-06-05 14:25 ` Amitsi5x
  2020-06-05 14:25 ` [PATCH] bluetoothctl:Add support for read characteristics value Amitsi5x
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 13+ messages in thread
From: Amitsi5x @ 2020-06-05 14:25 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: amitx.k.singh

From: amit <amitx.k.singh@intel.com>

Changes made to add support for discovering gatt service
by uuid in bluetoothctl.

Signed-off-by: amit <amitx.k.singh@intel.com>
---
 client/gatt.c            |  70 ++++++++++++++++++++++
 client/gatt.h            |   2 +-
 client/main.c            |  17 ++++++
 src/gatt-client.c        |  79 +++++++++++++++++++++++-
 src/shared/gatt-client.c | 126 +++++++++++++++++++++++++++++++++++++++
 src/shared/gatt-client.h |   8 +++
 6 files changed, 298 insertions(+), 4 deletions(-)

diff --git a/client/gatt.c b/client/gatt.c
index e5bab6dd0..21e251d2e 100644
--- a/client/gatt.c
+++ b/client/gatt.c
@@ -681,6 +681,76 @@ void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[])
 	return bt_shell_noninteractive_quit(EXIT_FAILURE);
 }
 
+static void servbyuuid_reply(DBusMessage *message, void *user_data)
+{
+	DBusError error;
+	DBusMessageIter iter, array;
+	uint8_t *value;
+	int len;
+
+	dbus_error_init(&error);
+
+	if (dbus_set_error_from_message(&error, message) == TRUE) {
+		bt_shell_printf("Failed to read: %s\n", error.name);
+		dbus_error_free(&error);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	dbus_message_iter_init(message, &iter);
+
+	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
+		bt_shell_printf("Invalid response to read\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	dbus_message_iter_recurse(&iter, &array);
+	dbus_message_iter_get_fixed_array(&array, &value, &len);
+
+	if (len < 0) {
+		bt_shell_printf("Unable to parse value\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	bt_shell_hexdump(value, len);
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+}
+
+static void servbyuuid_setup(DBusMessageIter *iter, void *user_data)
+{
+	char *uuid = user_data;
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
+}
+
+static void servbyuuid_attribute(GDBusProxy *proxy, char *uuid)
+{
+	if (g_dbus_proxy_method_call(proxy, "ServiceByUUID", servbyuuid_setup, servbyuuid_reply,
+						uuid, NULL) == FALSE) {
+		bt_shell_printf("Failed to set uuid\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	bt_shell_printf("Attempting to read service handle %s\n", g_dbus_proxy_get_path(proxy));
+}
+
+void gatt_servbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[])
+{
+	const char *iface;
+
+	iface = g_dbus_proxy_get_interface(proxy);
+
+	if (!strcmp(iface, "org.bluez.GattService1")) {
+		servbyuuid_attribute(proxy, argv[1]);
+		return;
+	}
+
+	bt_shell_printf("Unable to read attribute %s\n",
+						g_dbus_proxy_get_path(proxy));
+
+	return bt_shell_noninteractive_quit(EXIT_FAILURE);
+}
+
 static void write_reply(DBusMessage *message, void *user_data)
 {
 	DBusError error;
diff --git a/client/gatt.h b/client/gatt.h
index 09ca618d3..8757d6b48 100644
--- a/client/gatt.h
+++ b/client/gatt.h
@@ -33,7 +33,7 @@ void gatt_remove_descriptor(GDBusProxy *proxy);
 void gatt_list_attributes(const char *device);
 GDBusProxy *gatt_select_attribute(GDBusProxy *parent, const char *path);
 char *gatt_attribute_generator(const char *text, int state);
-
+void gatt_servbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[]);
 void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[]);
 void gatt_write_attribute(GDBusProxy *proxy, int argc, char *argv[]);
 void gatt_notify_attribute(GDBusProxy *proxy, bool enable);
diff --git a/client/main.c b/client/main.c
index 422da5593..79a08728b 100644
--- a/client/main.c
+++ b/client/main.c
@@ -2071,6 +2071,21 @@ static void cmd_attribute_info(int argc, char *argv[])
 	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
+static void cmd_primary_by_uuid(int argc, char *argv[])
+{
+	GDBusProxy *proxy;
+
+	proxy = find_attribute(argc, argv);
+	set_default_attribute(proxy);
+
+	if (!default_attr) {
+		bt_shell_printf("No attribute selected\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	gatt_servbyuuid_attribute(default_attr, argc, argv);
+}
+
 static void cmd_read(int argc, char *argv[])
 {
 	if (!default_attr) {
@@ -2684,6 +2699,8 @@ static const struct bt_shell_menu gatt_menu = {
 	.entries = {
 	{ "list-attributes", "[dev/local]", cmd_list_attributes,
 				"List attributes", dev_generator },
+	{ "primary-by-uuid", "[UUID]", cmd_primary_by_uuid,
+				"Discover Primary Services by UUID" },
 	{ "select-attribute", "<attribute/UUID>",  cmd_select_attribute,
 				"Select attribute", attribute_generator },
 	{ "attribute-info", "[attribute/UUID]",  cmd_attribute_info,
diff --git a/src/gatt-client.c b/src/gatt-client.c
index 2ae258da0..daedae939 100644
--- a/src/gatt-client.c
+++ b/src/gatt-client.c
@@ -81,6 +81,7 @@ struct service {
 	char *path;
 	struct queue *chrcs;
 	struct queue *incl_services;
+	struct async_dbus_op *type_op;
 };
 
 typedef bool (*async_dbus_op_complete_t)(void *data);
@@ -118,7 +119,7 @@ struct characteristic {
 
 	struct async_dbus_op *read_op;
 	struct async_dbus_op *write_op;
-
+	struct async_dbus_op *type_op;
 	struct queue *descs;
 
 	bool notifying;
@@ -440,10 +441,30 @@ static struct async_dbus_op *async_dbus_op_new(DBusMessage *msg, void *data)
 	op->msgs = queue_new();
 	queue_push_tail(op->msgs, dbus_message_ref(msg));
 	op->data = data;
-
 	return op;
 }
 
+static struct async_dbus_op *fetch_service_by_uuid(struct bt_gatt_client *gatt,
+					DBusMessage *msg,
+					char *uuid,
+					bt_gatt_client_service_by_uuid_callback_t callback,
+					void *data)
+{
+	struct async_dbus_op *op;
+
+	op = async_dbus_op_new(msg, data);
+	op->id = bt_gatt_client_service_by_uuid(gatt, uuid, callback,
+						async_dbus_op_ref(op),
+						async_dbus_op_unref);
+
+	if (op->id)
+		return op;
+
+	async_dbus_op_free(op);
+
+	return NULL;
+}
+
 static struct async_dbus_op *read_value(struct bt_gatt_client *gatt,
 					DBusMessage *msg, uint16_t handle,
 					uint16_t offset,
@@ -930,6 +951,51 @@ fail:
 	chrc->read_op = NULL;
 }
 
+static void serv_uuid_cb(bool success, uint8_t att_ecode, const uint8_t *value,
+					uint16_t length, void *user_data)
+{
+	struct async_dbus_op *op = user_data;
+	struct service *serv = op->data;
+
+	if (!success)
+		goto fail;
+
+	async_dbus_op_reply(op, att_ecode, value, length);
+
+	return;
+
+fail:
+	async_dbus_op_reply(op, att_ecode, NULL, 0);
+	serv->type_op = NULL;
+}
+
+static DBusMessage *service_by_uuid(DBusConnection *conn,
+					DBusMessage *msg, void *user_data)
+{
+	struct service *serv = user_data;
+	struct bt_gatt_client *gatt = serv->client->gatt;
+	DBusMessageIter iter;
+	char *uuid;
+
+	if (!gatt)
+		return btd_error_failed(msg, "Not connected");
+
+	dbus_message_iter_init(msg, &iter);
+
+	if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
+		dbus_message_iter_get_basic(&iter,&uuid);
+	else
+		return NULL;
+
+	serv->type_op = fetch_service_by_uuid(gatt, msg, uuid,
+							serv_uuid_cb, serv);
+
+	if (!serv->type_op)
+		return btd_error_failed(msg, "Failed to send service by uuid");
+
+	return NULL;
+}
+
 static DBusMessage *characteristic_read_value(DBusConnection *conn,
 					DBusMessage *msg, void *user_data)
 {
@@ -1845,6 +1911,13 @@ static const GDBusPropertyTable service_properties[] = {
 	{ }
 };
 
+static const GDBusMethodTable service_methods[] = {
+	{ GDBUS_ASYNC_METHOD("ServiceByUUID", GDBUS_ARGS({ "options", "s" }),
+					GDBUS_ARGS({ "value", "ay" }),
+					service_by_uuid) },
+	{ }
+};
+
 static void service_free(void *data)
 {
 	struct service *service = data;
@@ -1878,7 +1951,7 @@ static struct service *service_create(struct gatt_db_attribute *attr,
 
 	if (!g_dbus_register_interface(btd_get_dbus_connection(), service->path,
 						GATT_SERVICE_IFACE,
-						NULL, NULL,
+						service_methods, NULL,
 						service_properties,
 						service, service_free)) {
 		error("Unable to register GATT service with handle 0x%04x for "
diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 3cb6ae443..88257c054 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -131,6 +131,13 @@ struct request {
 	void (*destroy)(void *);
 };
 
+struct service_by_uuid_op
+{
+	bt_gatt_client_service_by_uuid_callback_t callback;
+	bt_gatt_client_destroy_func_t destroy;
+	void *user_data;
+};
+
 static struct request *request_ref(struct request *req)
 {
 	__sync_fetch_and_add(&req->ref_count, 1);
@@ -2578,6 +2585,49 @@ bool bt_gatt_client_cancel_all(struct bt_gatt_client *client)
 	return true;
 }
 
+static void destroy_service_by_uuid_op(void *data)
+{
+	struct service_by_uuid_op *op = data;
+
+	if (op->destroy)
+		op->destroy(op->user_data);
+
+	free(op);
+}
+
+static void service_by_uuid_cb(uint8_t opcode, const void *pdu, uint16_t length,
+								void *user_data)
+{
+	struct request *req = user_data;
+	struct service_by_uuid_op *op = req->data;
+	bool success;
+
+	uint8_t att_ecode = 0;
+	const uint8_t *value = NULL;
+	uint16_t value_len = 0;
+
+	if (opcode == BT_ATT_OP_ERROR_RSP) {
+		success = false;
+		att_ecode = process_error(pdu, length);
+		goto done;
+	}
+
+	if (opcode != BT_ATT_OP_FIND_BY_TYPE_RSP || (!pdu && length)) {
+		success = false;
+		goto done;
+	}
+
+	success = true;
+	value_len = length;
+
+	if (value_len)
+		value = pdu;
+
+done:
+	if (op->callback)
+		op->callback(success, att_ecode, value, length, op->user_data);
+}
+
 struct read_op {
 	bt_gatt_client_read_callback_t callback;
 	void *user_data;
@@ -2625,6 +2675,82 @@ done:
 		op->callback(success, att_ecode, value, length, op->user_data);
 }
 
+unsigned int bt_gatt_client_service_by_uuid(struct bt_gatt_client *client,
+						char *uuid,
+						bt_gatt_client_service_by_uuid_callback_t callback,
+						void *user_data,
+						bt_gatt_client_destroy_func_t destroy)
+{
+	struct request *req;
+	struct service_by_uuid_op *op;
+	unsigned char *pdu;
+	uint16_t len ;
+	uint16_t start_handle = 0x0001;
+	uint16_t end_handle = 0xffff;
+	uint16_t primart_uuid = GATT_PRIM_SVC_UUID;
+	bt_uuid_t btuuid;
+	uint8_t uuid128[16];
+
+	/* Length of pdu will be vary according to uuid type
+	for 2 byte uuid total length  is 8 (start handle(2) + end handle(2) + type(2) + uuid(2))
+	for 16 byte uuid total length  is 22 (start handle(2) + end handle(2) + type(2) + uuid(16))
+	*/
+	uint16_t pdu_len_16bit_uuid = 8;
+	uint16_t pdu_len_128bit_uuid = 22;
+
+	if (bt_string_to_uuid(&btuuid, uuid) < 0) {
+		return 0;
+	}
+
+	if (btuuid.type == BT_UUID16){
+		pdu = (unsigned char *) malloc(pdu_len_16bit_uuid);
+		len = pdu_len_16bit_uuid;
+	}
+	else {
+		pdu = (unsigned char *) malloc(pdu_len_128bit_uuid);
+		len = pdu_len_128bit_uuid;
+	}
+	if (!client)
+		return 0;
+	op = new0(struct service_by_uuid_op, 1);
+	req = request_create(client);
+	if (!req) {
+		free(op);
+		return 0;
+	}
+
+	op->callback = callback;
+	op->user_data = user_data;
+	op->destroy = destroy;
+	req->data = op;
+	req->destroy = destroy_service_by_uuid_op;
+
+	put_le16(start_handle, pdu);
+	put_le16(end_handle, pdu+2);
+	put_le16(primart_uuid, pdu+4);
+
+	/* Checking uuid type 16 bit or 128 bit , conversion as required*/
+	if (btuuid.type == BT_UUID16)
+		put_le16(btuuid.value.u16, pdu+6);
+	else {
+		bswap_128(&btuuid.value.u128.data[0], &uuid128[0]);
+		memcpy(pdu + 6, uuid128, 16);
+	}
+
+	req->att_id = bt_att_send(client->att, BT_ATT_OP_FIND_BY_TYPE_REQ,
+							pdu, len,
+							service_by_uuid_cb, req,
+							request_unref);
+
+	if (!req->att_id) {
+		op->destroy = NULL;
+		request_unref(req);
+		return 0;
+	}
+
+	return req->id;
+}
+
 unsigned int bt_gatt_client_read_value(struct bt_gatt_client *client,
 					uint16_t value_handle,
 					bt_gatt_client_read_callback_t callback,
diff --git a/src/shared/gatt-client.h b/src/shared/gatt-client.h
index 10900168b..599e98556 100644
--- a/src/shared/gatt-client.h
+++ b/src/shared/gatt-client.h
@@ -42,6 +42,9 @@ typedef void (*bt_gatt_client_destroy_func_t)(void *user_data);
 typedef void (*bt_gatt_client_callback_t)(bool success, uint8_t att_ecode,
 							void *user_data);
 typedef void (*bt_gatt_client_debug_func_t)(const char *str, void *user_data);
+typedef void (*bt_gatt_client_service_by_uuid_callback_t)(bool success, uint8_t att_ecode,
+					const uint8_t *value, uint16_t length,
+					void *user_data);
 typedef void (*bt_gatt_client_read_callback_t)(bool success, uint8_t att_ecode,
 					const uint8_t *value, uint16_t length,
 					void *user_data);
@@ -81,6 +84,11 @@ uint8_t bt_gatt_client_get_features(struct bt_gatt_client *client);
 bool bt_gatt_client_cancel(struct bt_gatt_client *client, unsigned int id);
 bool bt_gatt_client_cancel_all(struct bt_gatt_client *client);
 
+unsigned int bt_gatt_client_service_by_uuid(struct bt_gatt_client *client,
+					char *uuid,
+					bt_gatt_client_read_callback_t callback,
+					void *user_data,
+					bt_gatt_client_destroy_func_t destroy);
 unsigned int bt_gatt_client_read_value(struct bt_gatt_client *client,
 					uint16_t value_handle,
 					bt_gatt_client_read_callback_t callback,
-- 
2.17.1


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

* [PATCH] bluetoothctl:Add support for read characteristics value
  2020-06-05 14:25 [PATCH] bluetoothctl: Add support for discover characteristic by uuid Amitsi5x
  2020-06-05 14:25 ` [PATCH] bluetoothctl:Add support for discover service " Amitsi5x
@ 2020-06-05 14:25 ` Amitsi5x
  2020-06-05 17:02   ` Luiz Augusto von Dentz
  2020-06-05 14:25 ` [PATCH 1/2] bluez:load Generic access service Amitsi5x
  2020-06-05 14:25 ` [PATCH] bluez:update handle for large database Amitsi5x
  3 siblings, 1 reply; 13+ messages in thread
From: Amitsi5x @ 2020-06-05 14:25 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: amitx.k.singh

From: amit <amitx.k.singh@intel.com>

Changes made to add support for read charateristic value
by uuid in bluetoothctl.

Signed-off-by: amit <amitx.k.singh@intel.com>
---
 client/gatt.c            | 70 ++++++++++++++++++++++++++++++++
 client/gatt.h            |  1 +
 client/main.c            | 18 +++++++++
 src/gatt-client.c        | 70 ++++++++++++++++++++++++++++++++
 src/shared/gatt-client.c | 86 +++++++++++++++++++++++++++++++++++++++-
 src/shared/gatt-client.h |  5 +++
 6 files changed, 249 insertions(+), 1 deletion(-)

diff --git a/client/gatt.c b/client/gatt.c
index 53f875050..8c2844ed6 100644
--- a/client/gatt.c
+++ b/client/gatt.c
@@ -681,6 +681,76 @@ void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[])
 	return bt_shell_noninteractive_quit(EXIT_FAILURE);
 }
 
+static void charreadbyuuid_reply(DBusMessage *message, void *user_data)
+{
+	DBusError error;
+	DBusMessageIter iter, array;
+	uint8_t *value;
+	int len;
+
+	dbus_error_init(&error);
+
+	if (dbus_set_error_from_message(&error, message) == TRUE) {
+		bt_shell_printf("Failed to read: %s\n", error.name);
+		dbus_error_free(&error);
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	dbus_message_iter_init(message, &iter);
+
+	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
+		bt_shell_printf("Invalid response to read\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	dbus_message_iter_recurse(&iter, &array);
+	dbus_message_iter_get_fixed_array(&array, &value, &len);
+
+	if (len < 0) {
+		bt_shell_printf("Unable to parse value\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	bt_shell_hexdump(value, len);
+
+	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+}
+
+static void charreadbyuuid_setup(DBusMessageIter *iter, void *user_data)
+{
+	char *uuid = user_data;
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
+}
+
+static void charreadbyuuid_attribute(GDBusProxy *proxy, char *uuid)
+{
+	if (g_dbus_proxy_method_call(proxy, "CharReadByUUID", charreadbyuuid_setup, charreadbyuuid_reply,
+						uuid, NULL) == FALSE) {
+		bt_shell_printf("Failed to set uuid\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	bt_shell_printf("Attempting to read service handle %s\n", g_dbus_proxy_get_path(proxy));
+}
+
+void gatt_charreadbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[])
+{
+	const char *iface;
+
+	iface = g_dbus_proxy_get_interface(proxy);
+
+	if (!strcmp(iface, "org.bluez.GattCharacteristic1")) {
+		charreadbyuuid_attribute(proxy, argv[1]);
+		return;
+	}
+
+	bt_shell_printf("Unable to read attribute %s\n",
+						g_dbus_proxy_get_path(proxy));
+
+	return bt_shell_noninteractive_quit(EXIT_FAILURE);
+}
+
 static void charbyuuid_reply(DBusMessage *message, void *user_data)
 {
 	DBusError error;
diff --git a/client/gatt.h b/client/gatt.h
index 692fb5758..8f96d8665 100644
--- a/client/gatt.h
+++ b/client/gatt.h
@@ -35,6 +35,7 @@ GDBusProxy *gatt_select_attribute(GDBusProxy *parent, const char *path);
 char *gatt_attribute_generator(const char *text, int state);
 void gatt_servbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[]);
 void gatt_charbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[]);
+void gatt_charreadbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[]);
 void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[]);
 void gatt_write_attribute(GDBusProxy *proxy, int argc, char *argv[]);
 void gatt_notify_attribute(GDBusProxy *proxy, bool enable);
diff --git a/client/main.c b/client/main.c
index 10e64e17b..4dd1e593a 100644
--- a/client/main.c
+++ b/client/main.c
@@ -2071,6 +2071,22 @@ static void cmd_attribute_info(int argc, char *argv[])
 	return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
+static void cmd_char_read_by_uuid(int argc, char *argv[])
+{
+	GDBusProxy *proxy;
+
+	proxy = find_attribute(argc, argv);
+
+	set_default_attribute(proxy);
+
+	if (!default_attr) {
+		bt_shell_printf("No attribute selected\n");
+		return bt_shell_noninteractive_quit(EXIT_FAILURE);
+	}
+
+	gatt_charreadbyuuid_attribute(default_attr, argc, argv);
+}
+
 static void cmd_char_by_uuid(int argc, char *argv[])
 {
 	GDBusProxy *proxy;
@@ -2718,6 +2734,8 @@ static const struct bt_shell_menu gatt_menu = {
 				"Discover Primary Services by UUID" },
 	{ "char-by-uuid", "[UUID]", cmd_char_by_uuid,
 				"Discover Characteristic Services by UUID" },
+	{ "char-read-by-uuid", "[UUID]", cmd_char_read_by_uuid,
+				"Read Characteristic by UUID" },
 	{ "select-attribute", "<attribute/UUID>",  cmd_select_attribute,
 				"Select attribute", attribute_generator },
 	{ "attribute-info", "[attribute/UUID]",  cmd_attribute_info,
diff --git a/src/gatt-client.c b/src/gatt-client.c
index da811ea4f..cd6d6dfde 100644
--- a/src/gatt-client.c
+++ b/src/gatt-client.c
@@ -444,6 +444,27 @@ static struct async_dbus_op *async_dbus_op_new(DBusMessage *msg, void *data)
 	return op;
 }
 
+static struct async_dbus_op *fetch_char_read_by_uuid(struct bt_gatt_client *gatt,
+					DBusMessage *msg,
+					char *uuid,
+					bt_gatt_client_char_by_uuid_callback_t callback,
+					void *data)
+{
+	struct async_dbus_op *op;
+
+	op = async_dbus_op_new(msg, data);
+	op->id = bt_gatt_client_char_read_by_uuid(gatt, uuid, callback,
+						async_dbus_op_ref(op),
+						async_dbus_op_unref);
+
+	if (op->id)
+		return op;
+
+	async_dbus_op_free(op);
+
+	return NULL;
+}
+
 static struct async_dbus_op *fetch_char_by_uuid(struct bt_gatt_client *gatt,
 					DBusMessage *msg,
 					char *uuid,
@@ -972,6 +993,52 @@ fail:
 	chrc->read_op = NULL;
 }
 
+static void char_read_by_uuid_cb(bool success, uint8_t att_ecode, const uint8_t *value,
+					uint16_t length, void *user_data)
+{
+	struct async_dbus_op *op = user_data;
+	struct characteristic *opchar = op->data;
+
+	if (!success)
+		goto fail;
+
+	async_dbus_op_reply(op, att_ecode, value, length);
+
+	return;
+
+fail:
+	async_dbus_op_reply(op, att_ecode, NULL, 0);
+
+	opchar->type_op = NULL;
+}
+
+static DBusMessage *char_read_by_uuid(DBusConnection *conn,
+					DBusMessage *msg, void *user_data)
+{
+	struct characteristic *chardata = user_data;
+	struct bt_gatt_client *gatt = chardata->service->client->gatt;
+	DBusMessageIter iter;
+
+	char *uuid = 0;
+
+	if (!gatt)
+		return btd_error_failed(msg, "Not connected");
+
+	dbus_message_iter_init(msg, &iter);
+
+	if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
+		dbus_message_iter_get_basic(&iter,&uuid);
+	else
+		return NULL;
+
+	chardata->type_op = fetch_char_read_by_uuid(gatt, msg,uuid, char_read_by_uuid_cb, chardata);
+
+	if (!chardata->type_op)
+		return btd_error_failed(msg, "Failed to send read request");
+
+	return NULL;
+}
+
 static void characteristic_by_uuid_cb(bool success, uint8_t att_ecode, const uint8_t *value,
 					uint16_t length, void *user_data)
 {
@@ -1786,6 +1853,9 @@ static const GDBusMethodTable characteristic_methods[] = {
 	{ GDBUS_ASYNC_METHOD("CharByUUID", GDBUS_ARGS({ "options", "s" }),
 					GDBUS_ARGS({ "value", "ay" }),
 					chardiscover_by_uuid) },
+	{ GDBUS_ASYNC_METHOD("CharReadByUUID", GDBUS_ARGS({ "options", "s" }),
+					GDBUS_ARGS({ "value", "ay" }),
+					char_read_by_uuid) },
 	{ GDBUS_ASYNC_METHOD("ReadValue", GDBUS_ARGS({ "options", "a{sv}" }),
 					GDBUS_ARGS({ "value", "ay" }),
 					characteristic_read_value) },
diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 8a696c77f..7c9d25ec3 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -2725,6 +2725,90 @@ done:
 		op->callback(success, att_ecode, value, length, op->user_data);
 }
 
+unsigned int bt_gatt_client_char_read_by_uuid(struct bt_gatt_client *client,
+						char *uuid,
+						bt_gatt_client_char_by_uuid_callback_t callback,
+						void *user_data,
+						bt_gatt_client_destroy_func_t destroy)
+{
+	struct request *req;
+	struct char_by_uuid_op *op;
+	unsigned char *pdu;
+	uint16_t len ;
+	uint16_t start_handle = 0x0001;
+	uint16_t end_handle = 0xffff;
+	bt_uuid_t btuuid;
+	uint8_t uuid128[16];
+	
+	/* Length of pdu will be vary according to uuid type
+	for 2 byte uuid total length  is 8 (start handle(2) + end handle(2)  + uuid(2))
+	for 16 byte uuid total length  is 22 (start handle(2) + end handle(2)  + uuid(16))
+	*/
+	uint16_t pdu_len_16bit_uuid = 6;
+	uint16_t pdu_len_128bit_uuid = 20;
+
+	if (bt_string_to_uuid(&btuuid, uuid) < 0) {
+		return 0;
+	}
+
+	if (btuuid.type == BT_UUID16){
+		pdu = (unsigned char *) malloc(pdu_len_16bit_uuid);
+		len = pdu_len_16bit_uuid;
+	} else {
+		pdu = (unsigned char *) malloc(pdu_len_128bit_uuid);
+		len = pdu_len_128bit_uuid;
+	}
+
+	if (!client)
+		return 0;
+
+	op = new0(struct char_by_uuid_op, 1);
+	req = request_create(client);
+	if (!req) {
+		free(op);
+		return 0;
+	}
+	if (!client)
+		return 0;
+
+	op = new0(struct char_by_uuid_op, 1);
+	req = request_create(client);
+
+	if (!req) {
+		free(op);
+		return 0;
+	}
+
+	op->callback = callback;
+	op->user_data = user_data;
+	op->destroy = destroy;
+	req->data = op;
+	req->destroy = destroy_char_by_uuid_op;
+
+	put_le16(start_handle, pdu);
+	put_le16(end_handle, pdu+2);
+
+	if (btuuid.type == BT_UUID16)
+		put_le16(btuuid.value.u16, pdu+4);
+	else {
+		bswap_128(&btuuid.value.u128.data[0], &uuid128[0]);
+		memcpy(pdu + 4, uuid128, 16);
+	}
+
+	req->att_id = bt_att_send(client->att, BT_ATT_OP_READ_BY_TYPE_REQ,
+							pdu, len,
+							char_by_uuid_cb, req,
+							request_unref);
+
+	if (!req->att_id) {
+		op->destroy = NULL;
+		request_unref(req);
+		return 0;
+	}
+
+	return req->id;
+}
+
 unsigned int bt_gatt_client_char_by_uuid(struct bt_gatt_client *client,
                                               char *uuid,
                                               bt_gatt_client_char_by_uuid_callback_t callback,
@@ -2754,7 +2838,7 @@ unsigned int bt_gatt_client_char_by_uuid(struct bt_gatt_client *client,
 	if (btuuid.type == BT_UUID16){
 		pdu = (unsigned char *) malloc(pdu_len_16bit_uuid);
 		len = pdu_len_16bit_uuid;
-	} else {
+	}else {
 		pdu = (unsigned char *) malloc(pdu_len_128bit_uuid);
 		len = pdu_len_128bit_uuid;
 	}
diff --git a/src/shared/gatt-client.h b/src/shared/gatt-client.h
index f5d5169ce..50859ce52 100644
--- a/src/shared/gatt-client.h
+++ b/src/shared/gatt-client.h
@@ -97,6 +97,11 @@ unsigned int bt_gatt_client_char_by_uuid(struct bt_gatt_client *client,
 					bt_gatt_client_read_callback_t callback,
 					void *user_data,
 					bt_gatt_client_destroy_func_t destroy);
+unsigned int bt_gatt_client_char_read_by_uuid(struct bt_gatt_client *client,
+					char *uuid,
+					bt_gatt_client_read_callback_t callback,
+					void *user_data,
+					bt_gatt_client_destroy_func_t destroy);
 unsigned int bt_gatt_client_read_value(struct bt_gatt_client *client,
 					uint16_t value_handle,
 					bt_gatt_client_read_callback_t callback,
-- 
2.17.1


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

* [PATCH 1/2] bluez:load Generic access service
  2020-06-05 14:25 [PATCH] bluetoothctl: Add support for discover characteristic by uuid Amitsi5x
  2020-06-05 14:25 ` [PATCH] bluetoothctl:Add support for discover service " Amitsi5x
  2020-06-05 14:25 ` [PATCH] bluetoothctl:Add support for read characteristics value Amitsi5x
@ 2020-06-05 14:25 ` Amitsi5x
  2020-06-05 17:03   ` Luiz Augusto von Dentz
  2020-06-05 14:25 ` [PATCH] bluez:update handle for large database Amitsi5x
  3 siblings, 1 reply; 13+ messages in thread
From: Amitsi5x @ 2020-06-05 14:25 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: amitx.k.singh

From: “AmitSingh” <amitx.k.singh@intel.com>

It allow to load generic access service to database

Signed-off-by: “AmitSingh” <amitx.k.singh@intel.com>
---
 src/gatt-client.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/src/gatt-client.c b/src/gatt-client.c
index 20c3fbec2..2ae258da0 100644
--- a/src/gatt-client.c
+++ b/src/gatt-client.c
@@ -2009,9 +2009,6 @@ static void export_service(struct gatt_db_attribute *attr, void *user_data)
 	struct btd_gatt_client *client = user_data;
 	struct service *service;
 
-	if (gatt_db_service_get_claimed(attr))
-		return;
-
 	service = service_create(attr, client);
 	if (!service)
 		return;
-- 
2.17.1


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

* [PATCH] bluez:update handle for large database
  2020-06-05 14:25 [PATCH] bluetoothctl: Add support for discover characteristic by uuid Amitsi5x
                   ` (2 preceding siblings ...)
  2020-06-05 14:25 ` [PATCH 1/2] bluez:load Generic access service Amitsi5x
@ 2020-06-05 14:25 ` Amitsi5x
  2020-06-05 14:36   ` bluez.test.bot
  2020-06-05 17:16   ` [PATCH] " Luiz Augusto von Dentz
  3 siblings, 2 replies; 13+ messages in thread
From: Amitsi5x @ 2020-06-05 14:25 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: amitx.k.singh

From: amit <amitx.k.singh@intel.com>

Update handle for large database and
added condition before free to avoid double free

Signed-off-by: amit <amitx.k.singh@intel.com>
---
 src/shared/gatt-client.c | 12 +++++++-----
 src/shared/gatt-db.c     | 15 +++++++++------
 2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
index 19ff6ab65..3cb6ae443 100644
--- a/src/shared/gatt-client.c
+++ b/src/shared/gatt-client.c
@@ -1131,8 +1131,6 @@ static void discover_secondary_cb(bool success, uint8_t att_ecode,
 				success = false;
 				goto done;
 			}
-			/* Database has changed adjust last handle */
-			op->last = end;
 		}
 
 		/* Update pending list */
@@ -1392,9 +1390,13 @@ static void db_hash_read_cb(bool success, uint8_t att_ecode,
 	util_hexdump(' ', value, len, client->debug_callback,
 						client->debug_data);
 
-	/* Store ithe new hash in the db */
-	gatt_db_attribute_write(op->hash, 0, value, len, 0, NULL,
-					db_hash_write_value_cb, client);
+	/* Store the new hash in the db */
+	if(gatt_db_attribute_write(op->hash, 0, value, len, 0, NULL,
+						db_hash_write_value_cb, client)) {
+		util_debug(client->debug_callback, client->debug_data,"DB Hash match write: skipping discovery");
+		queue_remove_all(op->pending_svcs, NULL, NULL, NULL);
+	}
+
 
 discover:
 	if (!op->success) {
diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index b44f7b5e9..15af4c20a 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
@@ -344,10 +344,15 @@ static bool db_hash_update(void *user_data)
 	gatt_db_foreach_service(db, NULL, service_gen_hash_m, &hash);
 	bt_crypto_gatt_hash(db->crypto, hash.iov, db->next_handle, db->hash);
 
-	for (i = 0; i < hash.i; i++)
-		free(hash.iov[i].iov_base);
+	for (i = 0; i < hash.i; i++) {
+		if(hash.iov[i].iov_base)
+			free(hash.iov[i].iov_base);
+	}
+
+	if(hash.iov)
+		free(hash.iov);
 
-	free(hash.iov);
+	hash.iov = NULL;
 
 	return false;
 }
@@ -689,7 +694,7 @@ struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db,
 	service->num_handles = num_handles;
 
 	/* Fast-forward next_handle if the new service was added to the end */
-	db->next_handle = MAX(handle + num_handles, db->next_handle);
+	db->next_handle += num_handles;
 
 	return service->attributes[0];
 
@@ -811,8 +816,6 @@ service_insert_characteristic(struct gatt_db_service *service,
 	 * declaration. All characteristic definitions shall have a
 	 * Characteristic Value declaration.
 	 */
-	if (handle == UINT16_MAX)
-		return NULL;
 
 	i = get_attribute_index(service, 1);
 	if (!i)
-- 
2.17.1


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

* RE: bluez:update handle for large database
  2020-06-05 14:25 ` [PATCH] bluez:update handle for large database Amitsi5x
@ 2020-06-05 14:36   ` bluez.test.bot
  2020-06-05 17:16   ` [PATCH] " Luiz Augusto von Dentz
  1 sibling, 0 replies; 13+ messages in thread
From: bluez.test.bot @ 2020-06-05 14:36 UTC (permalink / raw)
  To: linux-bluetooth, amitx.k.singh


[-- Attachment #1: Type: text/plain, Size: 1612 bytes --]


This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While we are preparing for reviewing the patches, we found the following
issue/warning.

Test Result:
checkpatch Failed

Outputs:
ERROR:SPACING: space required before the open parenthesis '('
#32: FILE: src/shared/gatt-client.c:1394:
+	if(gatt_db_attribute_write(op->hash, 0, value, len, 0, NULL,

WARNING:LONG_LINE: line over 80 characters
#33: FILE: src/shared/gatt-client.c:1395:
+						db_hash_write_value_cb, client)) {

ERROR:SPACING: space required after that ',' (ctx:VxV)
#34: FILE: src/shared/gatt-client.c:1396:
+		util_debug(client->debug_callback, client->debug_data,"DB Hash match write: skipping discovery");
 		                                                     ^

ERROR:SPACING: space required before the open parenthesis '('
#52: FILE: src/shared/gatt-db.c:348:
+		if(hash.iov[i].iov_base)

ERROR:SPACING: space required before the open parenthesis '('
#56: FILE: src/shared/gatt-db.c:352:
+	if(hash.iov)

- total: 4 errors, 1 warnings, 58 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

Your patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPLIT_STRING

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.



---
Regards,
Linux Bluetooth

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

* Re: [PATCH] bluetoothctl:Add support for read characteristics value
  2020-06-05 14:25 ` [PATCH] bluetoothctl:Add support for read characteristics value Amitsi5x
@ 2020-06-05 17:02   ` Luiz Augusto von Dentz
  2020-07-16  8:40     ` Singh, AmitX K
  0 siblings, 1 reply; 13+ messages in thread
From: Luiz Augusto von Dentz @ 2020-06-05 17:02 UTC (permalink / raw)
  To: Amitsi5x; +Cc: linux-bluetooth

Hi Amit,

On Fri, Jun 5, 2020 at 7:30 AM Amitsi5x <amitx.k.singh@intel.com> wrote:
>
> From: amit <amitx.k.singh@intel.com>
>
> Changes made to add support for read charateristic value
> by uuid in bluetoothctl.
>
> Signed-off-by: amit <amitx.k.singh@intel.com>
> ---
>  client/gatt.c            | 70 ++++++++++++++++++++++++++++++++
>  client/gatt.h            |  1 +
>  client/main.c            | 18 +++++++++
>  src/gatt-client.c        | 70 ++++++++++++++++++++++++++++++++
>  src/shared/gatt-client.c | 86 +++++++++++++++++++++++++++++++++++++++-
>  src/shared/gatt-client.h |  5 +++
>  6 files changed, 249 insertions(+), 1 deletion(-)
>
> diff --git a/client/gatt.c b/client/gatt.c
> index 53f875050..8c2844ed6 100644
> --- a/client/gatt.c
> +++ b/client/gatt.c
> @@ -681,6 +681,76 @@ void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[])
>         return bt_shell_noninteractive_quit(EXIT_FAILURE);
>  }
>
> +static void charreadbyuuid_reply(DBusMessage *message, void *user_data)
> +{
> +       DBusError error;
> +       DBusMessageIter iter, array;
> +       uint8_t *value;
> +       int len;
> +
> +       dbus_error_init(&error);
> +
> +       if (dbus_set_error_from_message(&error, message) == TRUE) {
> +               bt_shell_printf("Failed to read: %s\n", error.name);
> +               dbus_error_free(&error);
> +               return bt_shell_noninteractive_quit(EXIT_FAILURE);
> +       }
> +
> +       dbus_message_iter_init(message, &iter);
> +
> +       if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
> +               bt_shell_printf("Invalid response to read\n");
> +               return bt_shell_noninteractive_quit(EXIT_FAILURE);
> +       }
> +
> +       dbus_message_iter_recurse(&iter, &array);
> +       dbus_message_iter_get_fixed_array(&array, &value, &len);
> +
> +       if (len < 0) {
> +               bt_shell_printf("Unable to parse value\n");
> +               return bt_shell_noninteractive_quit(EXIT_FAILURE);
> +       }
> +
> +       bt_shell_hexdump(value, len);
> +
> +       return bt_shell_noninteractive_quit(EXIT_SUCCESS);
> +}
> +
> +static void charreadbyuuid_setup(DBusMessageIter *iter, void *user_data)
> +{
> +       char *uuid = user_data;
> +
> +       dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
> +}
> +
> +static void charreadbyuuid_attribute(GDBusProxy *proxy, char *uuid)
> +{
> +       if (g_dbus_proxy_method_call(proxy, "CharReadByUUID", charreadbyuuid_setup, charreadbyuuid_reply,
> +                                               uuid, NULL) == FALSE) {
> +               bt_shell_printf("Failed to set uuid\n");
> +               return bt_shell_noninteractive_quit(EXIT_FAILURE);
> +       }
> +
> +       bt_shell_printf("Attempting to read service handle %s\n", g_dbus_proxy_get_path(proxy));
> +}
> +
> +void gatt_charreadbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[])
> +{
> +       const char *iface;
> +
> +       iface = g_dbus_proxy_get_interface(proxy);
> +
> +       if (!strcmp(iface, "org.bluez.GattCharacteristic1")) {
> +               charreadbyuuid_attribute(proxy, argv[1]);
> +               return;
> +       }
> +
> +       bt_shell_printf("Unable to read attribute %s\n",
> +                                               g_dbus_proxy_get_path(proxy));
> +
> +       return bt_shell_noninteractive_quit(EXIT_FAILURE);
> +}
> +
>  static void charbyuuid_reply(DBusMessage *message, void *user_data)
>  {
>         DBusError error;
> diff --git a/client/gatt.h b/client/gatt.h
> index 692fb5758..8f96d8665 100644
> --- a/client/gatt.h
> +++ b/client/gatt.h
> @@ -35,6 +35,7 @@ GDBusProxy *gatt_select_attribute(GDBusProxy *parent, const char *path);
>  char *gatt_attribute_generator(const char *text, int state);
>  void gatt_servbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[]);
>  void gatt_charbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[]);
> +void gatt_charreadbyuuid_attribute(GDBusProxy *proxy, int argc, char *argv[]);
>  void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[]);
>  void gatt_write_attribute(GDBusProxy *proxy, int argc, char *argv[]);
>  void gatt_notify_attribute(GDBusProxy *proxy, bool enable);
> diff --git a/client/main.c b/client/main.c
> index 10e64e17b..4dd1e593a 100644
> --- a/client/main.c
> +++ b/client/main.c
> @@ -2071,6 +2071,22 @@ static void cmd_attribute_info(int argc, char *argv[])
>         return bt_shell_noninteractive_quit(EXIT_SUCCESS);
>  }
>
> +static void cmd_char_read_by_uuid(int argc, char *argv[])
> +{
> +       GDBusProxy *proxy;
> +
> +       proxy = find_attribute(argc, argv);
> +
> +       set_default_attribute(proxy);
> +
> +       if (!default_attr) {
> +               bt_shell_printf("No attribute selected\n");
> +               return bt_shell_noninteractive_quit(EXIT_FAILURE);
> +       }
> +
> +       gatt_charreadbyuuid_attribute(default_attr, argc, argv);
> +}
> +
>  static void cmd_char_by_uuid(int argc, char *argv[])
>  {
>         GDBusProxy *proxy;
> @@ -2718,6 +2734,8 @@ static const struct bt_shell_menu gatt_menu = {
>                                 "Discover Primary Services by UUID" },
>         { "char-by-uuid", "[UUID]", cmd_char_by_uuid,
>                                 "Discover Characteristic Services by UUID" },
> +       { "char-read-by-uuid", "[UUID]", cmd_char_read_by_uuid,
> +                               "Read Characteristic by UUID" },
>         { "select-attribute", "<attribute/UUID>",  cmd_select_attribute,
>                                 "Select attribute", attribute_generator },
>         { "attribute-info", "[attribute/UUID]",  cmd_attribute_info,
> diff --git a/src/gatt-client.c b/src/gatt-client.c
> index da811ea4f..cd6d6dfde 100644
> --- a/src/gatt-client.c
> +++ b/src/gatt-client.c
> @@ -444,6 +444,27 @@ static struct async_dbus_op *async_dbus_op_new(DBusMessage *msg, void *data)
>         return op;
>  }
>
> +static struct async_dbus_op *fetch_char_read_by_uuid(struct bt_gatt_client *gatt,
> +                                       DBusMessage *msg,
> +                                       char *uuid,
> +                                       bt_gatt_client_char_by_uuid_callback_t callback,
> +                                       void *data)
> +{
> +       struct async_dbus_op *op;
> +
> +       op = async_dbus_op_new(msg, data);
> +       op->id = bt_gatt_client_char_read_by_uuid(gatt, uuid, callback,
> +                                               async_dbus_op_ref(op),
> +                                               async_dbus_op_unref);
> +
> +       if (op->id)
> +               return op;
> +
> +       async_dbus_op_free(op);
> +
> +       return NULL;
> +}
> +
>  static struct async_dbus_op *fetch_char_by_uuid(struct bt_gatt_client *gatt,
>                                         DBusMessage *msg,
>                                         char *uuid,
> @@ -972,6 +993,52 @@ fail:
>         chrc->read_op = NULL;
>  }
>
> +static void char_read_by_uuid_cb(bool success, uint8_t att_ecode, const uint8_t *value,
> +                                       uint16_t length, void *user_data)
> +{
> +       struct async_dbus_op *op = user_data;
> +       struct characteristic *opchar = op->data;
> +
> +       if (!success)
> +               goto fail;
> +
> +       async_dbus_op_reply(op, att_ecode, value, length);
> +
> +       return;
> +
> +fail:
> +       async_dbus_op_reply(op, att_ecode, NULL, 0);
> +
> +       opchar->type_op = NULL;
> +}
> +
> +static DBusMessage *char_read_by_uuid(DBusConnection *conn,
> +                                       DBusMessage *msg, void *user_data)
> +{
> +       struct characteristic *chardata = user_data;
> +       struct bt_gatt_client *gatt = chardata->service->client->gatt;
> +       DBusMessageIter iter;
> +
> +       char *uuid = 0;
> +
> +       if (!gatt)
> +               return btd_error_failed(msg, "Not connected");
> +
> +       dbus_message_iter_init(msg, &iter);
> +
> +       if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
> +               dbus_message_iter_get_basic(&iter,&uuid);
> +       else
> +               return NULL;
> +
> +       chardata->type_op = fetch_char_read_by_uuid(gatt, msg,uuid, char_read_by_uuid_cb, chardata);
> +
> +       if (!chardata->type_op)
> +               return btd_error_failed(msg, "Failed to send read request");
> +
> +       return NULL;
> +}
> +
>  static void characteristic_by_uuid_cb(bool success, uint8_t att_ecode, const uint8_t *value,
>                                         uint16_t length, void *user_data)
>  {
> @@ -1786,6 +1853,9 @@ static const GDBusMethodTable characteristic_methods[] = {
>         { GDBUS_ASYNC_METHOD("CharByUUID", GDBUS_ARGS({ "options", "s" }),
>                                         GDBUS_ARGS({ "value", "ay" }),
>                                         chardiscover_by_uuid) },
> +       { GDBUS_ASYNC_METHOD("CharReadByUUID", GDBUS_ARGS({ "options", "s" }),
> +                                       GDBUS_ARGS({ "value", "ay" }),
> +                                       char_read_by_uuid) },

It would have helped if you had communicated that you would be looking
into add support for this type of operation, these procedures
obviously cannot be part of Characteristic interface since that is
only available after the discovery procedure then you can just lookup
internally in the cache. So this type of procedure will probably need
to be under a Device object and lets please agree on the documentation
first before we move forward.

>         { GDBUS_ASYNC_METHOD("ReadValue", GDBUS_ARGS({ "options", "a{sv}" }),
>                                         GDBUS_ARGS({ "value", "ay" }),
>                                         characteristic_read_value) },
> diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
> index 8a696c77f..7c9d25ec3 100644
> --- a/src/shared/gatt-client.c
> +++ b/src/shared/gatt-client.c
> @@ -2725,6 +2725,90 @@ done:
>                 op->callback(success, att_ecode, value, length, op->user_data);
>  }
>
> +unsigned int bt_gatt_client_char_read_by_uuid(struct bt_gatt_client *client,
> +                                               char *uuid,
> +                                               bt_gatt_client_char_by_uuid_callback_t callback,
> +                                               void *user_data,
> +                                               bt_gatt_client_destroy_func_t destroy)
> +{
> +       struct request *req;
> +       struct char_by_uuid_op *op;
> +       unsigned char *pdu;
> +       uint16_t len ;
> +       uint16_t start_handle = 0x0001;
> +       uint16_t end_handle = 0xffff;
> +       bt_uuid_t btuuid;
> +       uint8_t uuid128[16];
> +
> +       /* Length of pdu will be vary according to uuid type
> +       for 2 byte uuid total length  is 8 (start handle(2) + end handle(2)  + uuid(2))
> +       for 16 byte uuid total length  is 22 (start handle(2) + end handle(2)  + uuid(16))
> +       */
> +       uint16_t pdu_len_16bit_uuid = 6;
> +       uint16_t pdu_len_128bit_uuid = 20;
> +
> +       if (bt_string_to_uuid(&btuuid, uuid) < 0) {
> +               return 0;
> +       }
> +
> +       if (btuuid.type == BT_UUID16){
> +               pdu = (unsigned char *) malloc(pdu_len_16bit_uuid);
> +               len = pdu_len_16bit_uuid;
> +       } else {
> +               pdu = (unsigned char *) malloc(pdu_len_128bit_uuid);
> +               len = pdu_len_128bit_uuid;
> +       }
> +
> +       if (!client)
> +               return 0;
> +
> +       op = new0(struct char_by_uuid_op, 1);
> +       req = request_create(client);
> +       if (!req) {
> +               free(op);
> +               return 0;
> +       }
> +       if (!client)
> +               return 0;
> +
> +       op = new0(struct char_by_uuid_op, 1);
> +       req = request_create(client);
> +
> +       if (!req) {
> +               free(op);
> +               return 0;
> +       }
> +
> +       op->callback = callback;
> +       op->user_data = user_data;
> +       op->destroy = destroy;
> +       req->data = op;
> +       req->destroy = destroy_char_by_uuid_op;
> +
> +       put_le16(start_handle, pdu);
> +       put_le16(end_handle, pdu+2);
> +
> +       if (btuuid.type == BT_UUID16)
> +               put_le16(btuuid.value.u16, pdu+4);
> +       else {
> +               bswap_128(&btuuid.value.u128.data[0], &uuid128[0]);
> +               memcpy(pdu + 4, uuid128, 16);
> +       }
> +
> +       req->att_id = bt_att_send(client->att, BT_ATT_OP_READ_BY_TYPE_REQ,
> +                                                       pdu, len,
> +                                                       char_by_uuid_cb, req,
> +                                                       request_unref);
> +
> +       if (!req->att_id) {
> +               op->destroy = NULL;
> +               request_unref(req);
> +               return 0;
> +       }
> +
> +       return req->id;
> +}
> +
>  unsigned int bt_gatt_client_char_by_uuid(struct bt_gatt_client *client,
>                                                char *uuid,
>                                                bt_gatt_client_char_by_uuid_callback_t callback,
> @@ -2754,7 +2838,7 @@ unsigned int bt_gatt_client_char_by_uuid(struct bt_gatt_client *client,
>         if (btuuid.type == BT_UUID16){
>                 pdu = (unsigned char *) malloc(pdu_len_16bit_uuid);
>                 len = pdu_len_16bit_uuid;
> -       } else {
> +       }else {
>                 pdu = (unsigned char *) malloc(pdu_len_128bit_uuid);
>                 len = pdu_len_128bit_uuid;
>         }
> diff --git a/src/shared/gatt-client.h b/src/shared/gatt-client.h
> index f5d5169ce..50859ce52 100644
> --- a/src/shared/gatt-client.h
> +++ b/src/shared/gatt-client.h
> @@ -97,6 +97,11 @@ unsigned int bt_gatt_client_char_by_uuid(struct bt_gatt_client *client,
>                                         bt_gatt_client_read_callback_t callback,
>                                         void *user_data,
>                                         bt_gatt_client_destroy_func_t destroy);
> +unsigned int bt_gatt_client_char_read_by_uuid(struct bt_gatt_client *client,
> +                                       char *uuid,
> +                                       bt_gatt_client_read_callback_t callback,
> +                                       void *user_data,
> +                                       bt_gatt_client_destroy_func_t destroy);
>  unsigned int bt_gatt_client_read_value(struct bt_gatt_client *client,
>                                         uint16_t value_handle,
>                                         bt_gatt_client_read_callback_t callback,
> --
> 2.17.1
>


-- 
Luiz Augusto von Dentz

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

* Re: [PATCH 1/2] bluez:load Generic access service
  2020-06-05 14:25 ` [PATCH 1/2] bluez:load Generic access service Amitsi5x
@ 2020-06-05 17:03   ` Luiz Augusto von Dentz
  2020-07-16  8:39     ` Singh, AmitX K
  0 siblings, 1 reply; 13+ messages in thread
From: Luiz Augusto von Dentz @ 2020-06-05 17:03 UTC (permalink / raw)
  To: Amitsi5x; +Cc: linux-bluetooth

Hi Amit,

On Fri, Jun 5, 2020 at 7:31 AM Amitsi5x <amitx.k.singh@intel.com> wrote:
>
> From: “AmitSingh” <amitx.k.singh@intel.com>
>
> It allow to load generic access service to database
>
> Signed-off-by: “AmitSingh” <amitx.k.singh@intel.com>
> ---
>  src/gatt-client.c | 3 ---
>  1 file changed, 3 deletions(-)
>
> diff --git a/src/gatt-client.c b/src/gatt-client.c
> index 20c3fbec2..2ae258da0 100644
> --- a/src/gatt-client.c
> +++ b/src/gatt-client.c
> @@ -2009,9 +2009,6 @@ static void export_service(struct gatt_db_attribute *attr, void *user_data)
>         struct btd_gatt_client *client = user_data;
>         struct service *service;
>
> -       if (gatt_db_service_get_claimed(attr))
> -               return;
> -

This is actually done on purpose in order to avoid extra traffic since
the daemon already exposes this information in other interfaces.

>         service = service_create(attr, client);
>         if (!service)
>                 return;
> --
> 2.17.1

-- 
Luiz Augusto von Dentz

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

* Re: [PATCH] bluez:update handle for large database
  2020-06-05 14:25 ` [PATCH] bluez:update handle for large database Amitsi5x
  2020-06-05 14:36   ` bluez.test.bot
@ 2020-06-05 17:16   ` Luiz Augusto von Dentz
  2020-07-16  8:40     ` Singh, AmitX K
  1 sibling, 1 reply; 13+ messages in thread
From: Luiz Augusto von Dentz @ 2020-06-05 17:16 UTC (permalink / raw)
  To: Amitsi5x; +Cc: linux-bluetooth

Hi Amit,

On Fri, Jun 5, 2020 at 7:30 AM Amitsi5x <amitx.k.singh@intel.com> wrote:
>
> From: amit <amitx.k.singh@intel.com>
>
> Update handle for large database and
> added condition before free to avoid double free
>
> Signed-off-by: amit <amitx.k.singh@intel.com>
> ---
>  src/shared/gatt-client.c | 12 +++++++-----
>  src/shared/gatt-db.c     | 15 +++++++++------
>  2 files changed, 16 insertions(+), 11 deletions(-)
>
> diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c
> index 19ff6ab65..3cb6ae443 100644
> --- a/src/shared/gatt-client.c
> +++ b/src/shared/gatt-client.c
> @@ -1131,8 +1131,6 @@ static void discover_secondary_cb(bool success, uint8_t att_ecode,
>                                 success = false;
>                                 goto done;
>                         }
> -                       /* Database has changed adjust last handle */
> -                       op->last = end;
>                 }
>
>                 /* Update pending list */
> @@ -1392,9 +1390,13 @@ static void db_hash_read_cb(bool success, uint8_t att_ecode,
>         util_hexdump(' ', value, len, client->debug_callback,
>                                                 client->debug_data);
>
> -       /* Store ithe new hash in the db */
> -       gatt_db_attribute_write(op->hash, 0, value, len, 0, NULL,
> -                                       db_hash_write_value_cb, client);
> +       /* Store the new hash in the db */
> +       if(gatt_db_attribute_write(op->hash, 0, value, len, 0, NULL,
> +                                               db_hash_write_value_cb, client)) {
> +               util_debug(client->debug_callback, client->debug_data,"DB Hash match write: skipping discovery");
> +               queue_remove_all(op->pending_svcs, NULL, NULL, NULL);

Not following this change, if we got to write the db hash that means
the old value did not match.

> +       }
> +
>
>  discover:
>         if (!op->success) {
> diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
> index b44f7b5e9..15af4c20a 100644
> --- a/src/shared/gatt-db.c
> +++ b/src/shared/gatt-db.c
> @@ -344,10 +344,15 @@ static bool db_hash_update(void *user_data)
>         gatt_db_foreach_service(db, NULL, service_gen_hash_m, &hash);
>         bt_crypto_gatt_hash(db->crypto, hash.iov, db->next_handle, db->hash);
>
> -       for (i = 0; i < hash.i; i++)
> -               free(hash.iov[i].iov_base);
> +       for (i = 0; i < hash.i; i++) {
> +               if(hash.iov[i].iov_base)
> +                       free(hash.iov[i].iov_base);
> +       }
> +
> +       if(hash.iov)
> +               free(hash.iov);
>
> -       free(hash.iov);
> +       hash.iov = NULL;

I believe this error was actually introduced by your changes actually,
see below.

>         return false;
>  }
> @@ -689,7 +694,7 @@ struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db,
>         service->num_handles = num_handles;
>
>         /* Fast-forward next_handle if the new service was added to the end */
> -       db->next_handle = MAX(handle + num_handles, db->next_handle);
> +       db->next_handle += num_handles;

Note that if the service was not added to the end this starts adding
gaps in between, so I'm afraid I will have to nack this change.

>         return service->attributes[0];
>
> @@ -811,8 +816,6 @@ service_insert_characteristic(struct gatt_db_service *service,
>          * declaration. All characteristic definitions shall have a
>          * Characteristic Value declaration.
>          */
> -       if (handle == UINT16_MAX)
> -               return NULL;

This perhaps is the real reason, it seems to me that you have more
than UINT16_MAX handles so the handles loop around and start over from
0 which is invalid and will most likely cause double frees etc and
they can be multiple attributes assigned to the same handle. How big
is the database you are trying to test? If that is going past
UINT16_MAX it is probably broken and nothing can be done to fix it on
the client side which is why we stop adding attributes after it.

>         i = get_attribute_index(service, 1);
>         if (!i)
> --
> 2.17.1
>


-- 
Luiz Augusto von Dentz

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

* RE: [PATCH 1/2] bluez:load Generic access service
  2020-06-05 17:03   ` Luiz Augusto von Dentz
@ 2020-07-16  8:39     ` Singh, AmitX K
  2020-07-16 16:22       ` Luiz Augusto von Dentz
  0 siblings, 1 reply; 13+ messages in thread
From: Singh, AmitX K @ 2020-07-16  8:39 UTC (permalink / raw)
  To: Luiz Augusto von Dentz; +Cc: linux-bluetooth, Panda, Bharat B

Hi Luiz,

> -----Original Message-----
> From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
> Sent: Friday, June 5, 2020 10:34 PM
> To: Singh, AmitX K <amitx.k.singh@intel.com>
> Cc: linux-bluetooth@vger.kernel.org
> Subject: Re: [PATCH 1/2] bluez:load Generic access service
> 
> Hi Amit,
> 
> On Fri, Jun 5, 2020 at 7:31 AM Amitsi5x <amitx.k.singh@intel.com> wrote:
> >
> > From: “AmitSingh” <amitx.k.singh@intel.com>
> >
> > It allow to load generic access service to database
> >
> > Signed-off-by: “AmitSingh” <amitx.k.singh@intel.com>
> > ---
> >  src/gatt-client.c | 3 ---
> >  1 file changed, 3 deletions(-)
> >
> > diff --git a/src/gatt-client.c b/src/gatt-client.c index
> > 20c3fbec2..2ae258da0 100644
> > --- a/src/gatt-client.c
> > +++ b/src/gatt-client.c
> > @@ -2009,9 +2009,6 @@ static void export_service(struct
> gatt_db_attribute *attr, void *user_data)
> >         struct btd_gatt_client *client = user_data;
> >         struct service *service;
> >
> > -       if (gatt_db_service_get_claimed(attr))
> > -               return;
> > -
> 
> This is actually done on purpose in order to avoid extra traffic since the
> daemon already exposes this information in other interfaces.
> 

GAP service is not getting stored is hash database .To verify the PTS test  case GATT/CL/GAD/BV-02-C which is GAP service discovery with '1800' UUID, verdicts as "INCONCLUSIVE" as it does not find the GAP UUID in place

> >         service = service_create(attr, client);
> >         if (!service)
> >                 return;
> > --
> > 2.17.1
> 
> --
> Luiz Augusto von Dentz

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

* RE: [PATCH] bluez:update handle for large database
  2020-06-05 17:16   ` [PATCH] " Luiz Augusto von Dentz
@ 2020-07-16  8:40     ` Singh, AmitX K
  0 siblings, 0 replies; 13+ messages in thread
From: Singh, AmitX K @ 2020-07-16  8:40 UTC (permalink / raw)
  To: Luiz Augusto von Dentz; +Cc: linux-bluetooth, Panda, Bharat B

Hi Luiz

> -----Original Message-----
> From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
> Sent: Friday, June 5, 2020 10:46 PM
> To: Singh, AmitX K <amitx.k.singh@intel.com>
> Cc: linux-bluetooth@vger.kernel.org
> Subject: Re: [PATCH] bluez:update handle for large database
> 
> Hi Amit,
> 
> On Fri, Jun 5, 2020 at 7:30 AM Amitsi5x <amitx.k.singh@intel.com> wrote:
> >
> > From: amit <amitx.k.singh@intel.com>
> >
> > Update handle for large database and
> > added condition before free to avoid double free
> >
> > Signed-off-by: amit <amitx.k.singh@intel.com>
> > ---
> >  src/shared/gatt-client.c | 12 +++++++-----
> >  src/shared/gatt-db.c     | 15 +++++++++------
> >  2 files changed, 16 insertions(+), 11 deletions(-)
> >
> > diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c index
> > 19ff6ab65..3cb6ae443 100644
> > --- a/src/shared/gatt-client.c
> > +++ b/src/shared/gatt-client.c
> > @@ -1131,8 +1131,6 @@ static void discover_secondary_cb(bool success,
> uint8_t att_ecode,
> >                                 success = false;
> >                                 goto done;
> >                         }
> > -                       /* Database has changed adjust last handle */
> > -                       op->last = end;
> >                 }
> >
> >                 /* Update pending list */ @@ -1392,9 +1390,13 @@
> > static void db_hash_read_cb(bool success, uint8_t att_ecode,
> >         util_hexdump(' ', value, len, client->debug_callback,
> >                                                 client->debug_data);
> >
> > -       /* Store ithe new hash in the db */
> > -       gatt_db_attribute_write(op->hash, 0, value, len, 0, NULL,
> > -                                       db_hash_write_value_cb, client);
> > +       /* Store the new hash in the db */
> > +       if(gatt_db_attribute_write(op->hash, 0, value, len, 0, NULL,
> > +                                               db_hash_write_value_cb, client)) {
> > +               util_debug(client->debug_callback, client->debug_data,"DB Hash
> match write: skipping discovery");
> > +               queue_remove_all(op->pending_svcs, NULL, NULL, NULL);
> 
> Not following this change, if we got to write the db hash that means the old
> value did not match.
> 

When we verify the PTS test case GATT/CL/GAD/BV-02-C test case, the Test case demands to perform multiple connections to the PTS device with different database upon each connection, where the current code does not update the database hash on each connect iteration that yields to seg fault.
Added support for remove pending service if any after updating new hash in database.

> > +       }
> > +
> >
> >  discover:
> >         if (!op->success) {
> > diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c index
> > b44f7b5e9..15af4c20a 100644
> > --- a/src/shared/gatt-db.c
> > +++ b/src/shared/gatt-db.c
> > @@ -344,10 +344,15 @@ static bool db_hash_update(void *user_data)
> >         gatt_db_foreach_service(db, NULL, service_gen_hash_m, &hash);
> >         bt_crypto_gatt_hash(db->crypto, hash.iov, db->next_handle,
> > db->hash);
> >
> > -       for (i = 0; i < hash.i; i++)
> > -               free(hash.iov[i].iov_base);
> > +       for (i = 0; i < hash.i; i++) {
> > +               if(hash.iov[i].iov_base)
> > +                       free(hash.iov[i].iov_base);
> > +       }
> > +
> > +       if(hash.iov)
> > +               free(hash.iov);
> >
> > -       free(hash.iov);
> > +       hash.iov = NULL;
> 
> I believe this error was actually introduced by your changes actually, see
> below.
> 
> >         return false;
> >  }
> > @@ -689,7 +694,7 @@ struct gatt_db_attribute
> *gatt_db_insert_service(struct gatt_db *db,
> >         service->num_handles = num_handles;
> >
> >         /* Fast-forward next_handle if the new service was added to the end
> */
> > -       db->next_handle = MAX(handle + num_handles, db->next_handle);
> > +       db->next_handle += num_handles;
> 
> Note that if the service was not added to the end this starts adding gaps in
> between, so I'm afraid I will have to nack this change.
> 
> >         return service->attributes[0];
> >
> > @@ -811,8 +816,6 @@ service_insert_characteristic(struct gatt_db_service
> *service,
> >          * declaration. All characteristic definitions shall have a
> >          * Characteristic Value declaration.
> >          */
> > -       if (handle == UINT16_MAX)
> > -               return NULL;
> 
> This perhaps is the real reason, it seems to me that you have more than
> UINT16_MAX handles so the handles loop around and start over from
> 0 which is invalid and will most likely cause double frees etc and they can be
> multiple attributes assigned to the same handle. How big is the database you
> are trying to test? If that is going past UINT16_MAX it is probably broken and
> nothing can be done to fix it on the client side which is why we stop adding
> attributes after it.
> 

When we verify the PTS test case GATT/CL/GAD/BV-02-C test case, the TC demands to perform multiple connections to the PTS device with different database upon each connection. 
In one connection setup database having handle value is UINT16_MAX,  where the current code does not able when handle is max  .

> >         i = get_attribute_index(service, 1);
> >         if (!i)
> > --
> > 2.17.1
> >
> 
> 
> --
> Luiz Augusto von Dentz

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

* RE: [PATCH] bluetoothctl:Add support for read characteristics value
  2020-06-05 17:02   ` Luiz Augusto von Dentz
@ 2020-07-16  8:40     ` Singh, AmitX K
  0 siblings, 0 replies; 13+ messages in thread
From: Singh, AmitX K @ 2020-07-16  8:40 UTC (permalink / raw)
  To: Luiz Augusto von Dentz; +Cc: linux-bluetooth, Panda, Bharat B

Hi Luiz,

> -----Original Message-----
> From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
> Sent: Friday, June 5, 2020 10:32 PM
> To: Singh, AmitX K <amitx.k.singh@intel.com>
> Cc: linux-bluetooth@vger.kernel.org
> Subject: Re: [PATCH] bluetoothctl:Add support for read characteristics value
> 
> Hi Amit,
> 
> On Fri, Jun 5, 2020 at 7:30 AM Amitsi5x <amitx.k.singh@intel.com> wrote:
> >
> > From: amit <amitx.k.singh@intel.com>
> >
> > Changes made to add support for read charateristic value by uuid in
> > bluetoothctl.
> >
> > Signed-off-by: amit <amitx.k.singh@intel.com>
> > ---
> >  client/gatt.c            | 70 ++++++++++++++++++++++++++++++++
> >  client/gatt.h            |  1 +
> >  client/main.c            | 18 +++++++++
> >  src/gatt-client.c        | 70 ++++++++++++++++++++++++++++++++
> >  src/shared/gatt-client.c | 86
> > +++++++++++++++++++++++++++++++++++++++-
> >  src/shared/gatt-client.h |  5 +++
> >  6 files changed, 249 insertions(+), 1 deletion(-)
> >
> > diff --git a/client/gatt.c b/client/gatt.c index 53f875050..8c2844ed6
> > 100644
> > --- a/client/gatt.c
> > +++ b/client/gatt.c
> > @@ -681,6 +681,76 @@ void gatt_read_attribute(GDBusProxy *proxy, int
> argc, char *argv[])
> >         return bt_shell_noninteractive_quit(EXIT_FAILURE);
> >  }
> >
> > +static void charreadbyuuid_reply(DBusMessage *message, void
> > +*user_data) {
> > +       DBusError error;
> > +       DBusMessageIter iter, array;
> > +       uint8_t *value;
> > +       int len;
> > +
> > +       dbus_error_init(&error);
> > +
> > +       if (dbus_set_error_from_message(&error, message) == TRUE) {
> > +               bt_shell_printf("Failed to read: %s\n", error.name);
> > +               dbus_error_free(&error);
> > +               return bt_shell_noninteractive_quit(EXIT_FAILURE);
> > +       }
> > +
> > +       dbus_message_iter_init(message, &iter);
> > +
> > +       if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
> > +               bt_shell_printf("Invalid response to read\n");
> > +               return bt_shell_noninteractive_quit(EXIT_FAILURE);
> > +       }
> > +
> > +       dbus_message_iter_recurse(&iter, &array);
> > +       dbus_message_iter_get_fixed_array(&array, &value, &len);
> > +
> > +       if (len < 0) {
> > +               bt_shell_printf("Unable to parse value\n");
> > +               return bt_shell_noninteractive_quit(EXIT_FAILURE);
> > +       }
> > +
> > +       bt_shell_hexdump(value, len);
> > +
> > +       return bt_shell_noninteractive_quit(EXIT_SUCCESS);
> > +}
> > +
> > +static void charreadbyuuid_setup(DBusMessageIter *iter, void
> > +*user_data) {
> > +       char *uuid = user_data;
> > +
> > +       dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &uuid);
> > +}
> > +
> > +static void charreadbyuuid_attribute(GDBusProxy *proxy, char *uuid) {
> > +       if (g_dbus_proxy_method_call(proxy, "CharReadByUUID",
> charreadbyuuid_setup, charreadbyuuid_reply,
> > +                                               uuid, NULL) == FALSE) {
> > +               bt_shell_printf("Failed to set uuid\n");
> > +               return bt_shell_noninteractive_quit(EXIT_FAILURE);
> > +       }
> > +
> > +       bt_shell_printf("Attempting to read service handle %s\n",
> > +g_dbus_proxy_get_path(proxy)); }
> > +
> > +void gatt_charreadbyuuid_attribute(GDBusProxy *proxy, int argc, char
> > +*argv[]) {
> > +       const char *iface;
> > +
> > +       iface = g_dbus_proxy_get_interface(proxy);
> > +
> > +       if (!strcmp(iface, "org.bluez.GattCharacteristic1")) {
> > +               charreadbyuuid_attribute(proxy, argv[1]);
> > +               return;
> > +       }
> > +
> > +       bt_shell_printf("Unable to read attribute %s\n",
> > +
> > + g_dbus_proxy_get_path(proxy));
> > +
> > +       return bt_shell_noninteractive_quit(EXIT_FAILURE);
> > +}
> > +
> >  static void charbyuuid_reply(DBusMessage *message, void *user_data)
> > {
> >         DBusError error;
> > diff --git a/client/gatt.h b/client/gatt.h index 692fb5758..8f96d8665
> > 100644
> > --- a/client/gatt.h
> > +++ b/client/gatt.h
> > @@ -35,6 +35,7 @@ GDBusProxy *gatt_select_attribute(GDBusProxy
> > *parent, const char *path);  char *gatt_attribute_generator(const char
> > *text, int state);  void gatt_servbyuuid_attribute(GDBusProxy *proxy,
> > int argc, char *argv[]);  void gatt_charbyuuid_attribute(GDBusProxy
> > *proxy, int argc, char *argv[]);
> > +void gatt_charreadbyuuid_attribute(GDBusProxy *proxy, int argc, char
> > +*argv[]);
> >  void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[]);
> > void gatt_write_attribute(GDBusProxy *proxy, int argc, char *argv[]);
> > void gatt_notify_attribute(GDBusProxy *proxy, bool enable); diff --git
> > a/client/main.c b/client/main.c index 10e64e17b..4dd1e593a 100644
> > --- a/client/main.c
> > +++ b/client/main.c
> > @@ -2071,6 +2071,22 @@ static void cmd_attribute_info(int argc, char
> *argv[])
> >         return bt_shell_noninteractive_quit(EXIT_SUCCESS);
> >  }
> >
> > +static void cmd_char_read_by_uuid(int argc, char *argv[]) {
> > +       GDBusProxy *proxy;
> > +
> > +       proxy = find_attribute(argc, argv);
> > +
> > +       set_default_attribute(proxy);
> > +
> > +       if (!default_attr) {
> > +               bt_shell_printf("No attribute selected\n");
> > +               return bt_shell_noninteractive_quit(EXIT_FAILURE);
> > +       }
> > +
> > +       gatt_charreadbyuuid_attribute(default_attr, argc, argv); }
> > +
> >  static void cmd_char_by_uuid(int argc, char *argv[])  {
> >         GDBusProxy *proxy;
> > @@ -2718,6 +2734,8 @@ static const struct bt_shell_menu gatt_menu = {
> >                                 "Discover Primary Services by UUID" },
> >         { "char-by-uuid", "[UUID]", cmd_char_by_uuid,
> >                                 "Discover Characteristic Services by
> > UUID" },
> > +       { "char-read-by-uuid", "[UUID]", cmd_char_read_by_uuid,
> > +                               "Read Characteristic by UUID" },
> >         { "select-attribute", "<attribute/UUID>",  cmd_select_attribute,
> >                                 "Select attribute", attribute_generator },
> >         { "attribute-info", "[attribute/UUID]",  cmd_attribute_info,
> > diff --git a/src/gatt-client.c b/src/gatt-client.c index
> > da811ea4f..cd6d6dfde 100644
> > --- a/src/gatt-client.c
> > +++ b/src/gatt-client.c
> > @@ -444,6 +444,27 @@ static struct async_dbus_op
> *async_dbus_op_new(DBusMessage *msg, void *data)
> >         return op;
> >  }
> >
> > +static struct async_dbus_op *fetch_char_read_by_uuid(struct
> bt_gatt_client *gatt,
> > +                                       DBusMessage *msg,
> > +                                       char *uuid,
> > +                                       bt_gatt_client_char_by_uuid_callback_t callback,
> > +                                       void *data) {
> > +       struct async_dbus_op *op;
> > +
> > +       op = async_dbus_op_new(msg, data);
> > +       op->id = bt_gatt_client_char_read_by_uuid(gatt, uuid, callback,
> > +                                               async_dbus_op_ref(op),
> > +                                               async_dbus_op_unref);
> > +
> > +       if (op->id)
> > +               return op;
> > +
> > +       async_dbus_op_free(op);
> > +
> > +       return NULL;
> > +}
> > +
> >  static struct async_dbus_op *fetch_char_by_uuid(struct bt_gatt_client
> *gatt,
> >                                         DBusMessage *msg,
> >                                         char *uuid, @@ -972,6 +993,52
> > @@ fail:
> >         chrc->read_op = NULL;
> >  }
> >
> > +static void char_read_by_uuid_cb(bool success, uint8_t att_ecode, const
> uint8_t *value,
> > +                                       uint16_t length, void
> > +*user_data) {
> > +       struct async_dbus_op *op = user_data;
> > +       struct characteristic *opchar = op->data;
> > +
> > +       if (!success)
> > +               goto fail;
> > +
> > +       async_dbus_op_reply(op, att_ecode, value, length);
> > +
> > +       return;
> > +
> > +fail:
> > +       async_dbus_op_reply(op, att_ecode, NULL, 0);
> > +
> > +       opchar->type_op = NULL;
> > +}
> > +
> > +static DBusMessage *char_read_by_uuid(DBusConnection *conn,
> > +                                       DBusMessage *msg, void
> > +*user_data) {
> > +       struct characteristic *chardata = user_data;
> > +       struct bt_gatt_client *gatt = chardata->service->client->gatt;
> > +       DBusMessageIter iter;
> > +
> > +       char *uuid = 0;
> > +
> > +       if (!gatt)
> > +               return btd_error_failed(msg, "Not connected");
> > +
> > +       dbus_message_iter_init(msg, &iter);
> > +
> > +       if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
> > +               dbus_message_iter_get_basic(&iter,&uuid);
> > +       else
> > +               return NULL;
> > +
> > +       chardata->type_op = fetch_char_read_by_uuid(gatt, msg,uuid,
> > + char_read_by_uuid_cb, chardata);
> > +
> > +       if (!chardata->type_op)
> > +               return btd_error_failed(msg, "Failed to send read
> > + request");
> > +
> > +       return NULL;
> > +}
> > +
> >  static void characteristic_by_uuid_cb(bool success, uint8_t att_ecode,
> const uint8_t *value,
> >                                         uint16_t length, void
> > *user_data)  { @@ -1786,6 +1853,9 @@ static const GDBusMethodTable
> > characteristic_methods[] = {
> >         { GDBUS_ASYNC_METHOD("CharByUUID", GDBUS_ARGS({ "options",
> "s" }),
> >                                         GDBUS_ARGS({ "value", "ay" }),
> >                                         chardiscover_by_uuid) },
> > +       { GDBUS_ASYNC_METHOD("CharReadByUUID", GDBUS_ARGS({
> "options", "s" }),
> > +                                       GDBUS_ARGS({ "value", "ay" }),
> > +                                       char_read_by_uuid) },
> 
> It would have helped if you had communicated that you would be looking
> into add support for this type of operation, these procedures obviously
> cannot be part of Characteristic interface since that is only available after the
> discovery procedure then you can just lookup internally in the cache. So this
> type of procedure will probably need to be under a Device object and lets
> please agree on the documentation first before we move forward.
> 

When we verify the PTS test case GATT/CL/GAR/BV-03-C "Read using characterisitic UUID", the Test case expect read request from device .
Added a code for sending read request for reading characteristic value using UUID over characteristic interface

> >         { GDBUS_ASYNC_METHOD("ReadValue", GDBUS_ARGS({ "options",
> "a{sv}" }),
> >                                         GDBUS_ARGS({ "value", "ay" }),
> >                                         characteristic_read_value) },
> > diff --git a/src/shared/gatt-client.c b/src/shared/gatt-client.c index
> > 8a696c77f..7c9d25ec3 100644
> > --- a/src/shared/gatt-client.c
> > +++ b/src/shared/gatt-client.c
> > @@ -2725,6 +2725,90 @@ done:
> >                 op->callback(success, att_ecode, value, length,
> > op->user_data);  }
> >
> > +unsigned int bt_gatt_client_char_read_by_uuid(struct bt_gatt_client
> *client,
> > +                                               char *uuid,
> > +                                               bt_gatt_client_char_by_uuid_callback_t callback,
> > +                                               void *user_data,
> > +
> > +bt_gatt_client_destroy_func_t destroy) {
> > +       struct request *req;
> > +       struct char_by_uuid_op *op;
> > +       unsigned char *pdu;
> > +       uint16_t len ;
> > +       uint16_t start_handle = 0x0001;
> > +       uint16_t end_handle = 0xffff;
> > +       bt_uuid_t btuuid;
> > +       uint8_t uuid128[16];
> > +
> > +       /* Length of pdu will be vary according to uuid type
> > +       for 2 byte uuid total length  is 8 (start handle(2) + end handle(2)  +
> uuid(2))
> > +       for 16 byte uuid total length  is 22 (start handle(2) + end handle(2)  +
> uuid(16))
> > +       */
> > +       uint16_t pdu_len_16bit_uuid = 6;
> > +       uint16_t pdu_len_128bit_uuid = 20;
> > +
> > +       if (bt_string_to_uuid(&btuuid, uuid) < 0) {
> > +               return 0;
> > +       }
> > +
> > +       if (btuuid.type == BT_UUID16){
> > +               pdu = (unsigned char *) malloc(pdu_len_16bit_uuid);
> > +               len = pdu_len_16bit_uuid;
> > +       } else {
> > +               pdu = (unsigned char *) malloc(pdu_len_128bit_uuid);
> > +               len = pdu_len_128bit_uuid;
> > +       }
> > +
> > +       if (!client)
> > +               return 0;
> > +
> > +       op = new0(struct char_by_uuid_op, 1);
> > +       req = request_create(client);
> > +       if (!req) {
> > +               free(op);
> > +               return 0;
> > +       }
> > +       if (!client)
> > +               return 0;
> > +
> > +       op = new0(struct char_by_uuid_op, 1);
> > +       req = request_create(client);
> > +
> > +       if (!req) {
> > +               free(op);
> > +               return 0;
> > +       }
> > +
> > +       op->callback = callback;
> > +       op->user_data = user_data;
> > +       op->destroy = destroy;
> > +       req->data = op;
> > +       req->destroy = destroy_char_by_uuid_op;
> > +
> > +       put_le16(start_handle, pdu);
> > +       put_le16(end_handle, pdu+2);
> > +
> > +       if (btuuid.type == BT_UUID16)
> > +               put_le16(btuuid.value.u16, pdu+4);
> > +       else {
> > +               bswap_128(&btuuid.value.u128.data[0], &uuid128[0]);
> > +               memcpy(pdu + 4, uuid128, 16);
> > +       }
> > +
> > +       req->att_id = bt_att_send(client->att,
> BT_ATT_OP_READ_BY_TYPE_REQ,
> > +                                                       pdu, len,
> > +                                                       char_by_uuid_cb, req,
> > +
> > + request_unref);
> > +
> > +       if (!req->att_id) {
> > +               op->destroy = NULL;
> > +               request_unref(req);
> > +               return 0;
> > +       }
> > +
> > +       return req->id;
> > +}
> > +
> >  unsigned int bt_gatt_client_char_by_uuid(struct bt_gatt_client *client,
> >                                                char *uuid,
> >
> > bt_gatt_client_char_by_uuid_callback_t callback, @@ -2754,7 +2838,7 @@
> unsigned int bt_gatt_client_char_by_uuid(struct bt_gatt_client *client,
> >         if (btuuid.type == BT_UUID16){
> >                 pdu = (unsigned char *) malloc(pdu_len_16bit_uuid);
> >                 len = pdu_len_16bit_uuid;
> > -       } else {
> > +       }else {
> >                 pdu = (unsigned char *) malloc(pdu_len_128bit_uuid);
> >                 len = pdu_len_128bit_uuid;
> >         }
> > diff --git a/src/shared/gatt-client.h b/src/shared/gatt-client.h index
> > f5d5169ce..50859ce52 100644
> > --- a/src/shared/gatt-client.h
> > +++ b/src/shared/gatt-client.h
> > @@ -97,6 +97,11 @@ unsigned int bt_gatt_client_char_by_uuid(struct
> bt_gatt_client *client,
> >                                         bt_gatt_client_read_callback_t callback,
> >                                         void *user_data,
> >                                         bt_gatt_client_destroy_func_t
> > destroy);
> > +unsigned int bt_gatt_client_char_read_by_uuid(struct bt_gatt_client
> *client,
> > +                                       char *uuid,
> > +                                       bt_gatt_client_read_callback_t callback,
> > +                                       void *user_data,
> > +                                       bt_gatt_client_destroy_func_t
> > +destroy);
> >  unsigned int bt_gatt_client_read_value(struct bt_gatt_client *client,
> >                                         uint16_t value_handle,
> >                                         bt_gatt_client_read_callback_t
> > callback,
> > --
> > 2.17.1
> >
> 
> 
> --
> Luiz Augusto von Dentz

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

* Re: [PATCH 1/2] bluez:load Generic access service
  2020-07-16  8:39     ` Singh, AmitX K
@ 2020-07-16 16:22       ` Luiz Augusto von Dentz
  0 siblings, 0 replies; 13+ messages in thread
From: Luiz Augusto von Dentz @ 2020-07-16 16:22 UTC (permalink / raw)
  To: Singh, AmitX K; +Cc: linux-bluetooth, Panda, Bharat B

Hi Amit,

On Thu, Jul 16, 2020 at 1:40 AM Singh, AmitX K <amitx.k.singh@intel.com> wrote:
>
> Hi Luiz,
>
> > -----Original Message-----
> > From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
> > Sent: Friday, June 5, 2020 10:34 PM
> > To: Singh, AmitX K <amitx.k.singh@intel.com>
> > Cc: linux-bluetooth@vger.kernel.org
> > Subject: Re: [PATCH 1/2] bluez:load Generic access service
> >
> > Hi Amit,
> >
> > On Fri, Jun 5, 2020 at 7:31 AM Amitsi5x <amitx.k.singh@intel.com> wrote:
> > >
> > > From: “AmitSingh” <amitx.k.singh@intel.com>
> > >
> > > It allow to load generic access service to database
> > >
> > > Signed-off-by: “AmitSingh” <amitx.k.singh@intel.com>
> > > ---
> > >  src/gatt-client.c | 3 ---
> > >  1 file changed, 3 deletions(-)
> > >
> > > diff --git a/src/gatt-client.c b/src/gatt-client.c index
> > > 20c3fbec2..2ae258da0 100644
> > > --- a/src/gatt-client.c
> > > +++ b/src/gatt-client.c
> > > @@ -2009,9 +2009,6 @@ static void export_service(struct
> > gatt_db_attribute *attr, void *user_data)
> > >         struct btd_gatt_client *client = user_data;
> > >         struct service *service;
> > >
> > > -       if (gatt_db_service_get_claimed(attr))
> > > -               return;
> > > -
> >
> > This is actually done on purpose in order to avoid extra traffic since the
> > daemon already exposes this information in other interfaces.
> >
>
> GAP service is not getting stored is hash database .To verify the PTS test  case GATT/CL/GAD/BV-02-C which is GAP service discovery with '1800' UUID, verdicts as "INCONCLUSIVE" as it does not find the GAP UUID in place

BlueZ does perform the so-called discover all service procedure,
perhaps you need to select the procedure correctly in order to pass
this test.

> > >         service = service_create(attr, client);
> > >         if (!service)
> > >                 return;
> > > --
> > > 2.17.1
> >
> > --
> > Luiz Augusto von Dentz



-- 
Luiz Augusto von Dentz

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

end of thread, back to index

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-05 14:25 [PATCH] bluetoothctl: Add support for discover characteristic by uuid Amitsi5x
2020-06-05 14:25 ` [PATCH] bluetoothctl:Add support for discover service " Amitsi5x
2020-06-05 14:25 ` [PATCH] bluetoothctl:Add support for read characteristics value Amitsi5x
2020-06-05 17:02   ` Luiz Augusto von Dentz
2020-07-16  8:40     ` Singh, AmitX K
2020-06-05 14:25 ` [PATCH 1/2] bluez:load Generic access service Amitsi5x
2020-06-05 17:03   ` Luiz Augusto von Dentz
2020-07-16  8:39     ` Singh, AmitX K
2020-07-16 16:22       ` Luiz Augusto von Dentz
2020-06-05 14:25 ` [PATCH] bluez:update handle for large database Amitsi5x
2020-06-05 14:36   ` bluez.test.bot
2020-06-05 17:16   ` [PATCH] " Luiz Augusto von Dentz
2020-07-16  8:40     ` Singh, AmitX K

Linux-Bluetooth Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-bluetooth/0 linux-bluetooth/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-bluetooth linux-bluetooth/ https://lore.kernel.org/linux-bluetooth \
		linux-bluetooth@vger.kernel.org
	public-inbox-index linux-bluetooth

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-bluetooth


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git