public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
From: "Frédéric Danis" <frederic.danis@collabora.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v2] tools/btpclient: Add GATT support to get Device name
Date: Wed, 11 Feb 2026 10:47:26 +0100	[thread overview]
Message-ID: <20260211094726.198317-1-frederic.danis@collabora.com> (raw)

The device name is retrieved during GATT connection, so no need to
send a specific request, just need to send the name to PTS when the
property is updated.

This allows to pass GAP/IDLE/NAMP/BV-01-C.
---
v1 -> v2: Fix memory leak

 src/shared/btp.h  |  18 +++++++
 tools/btpclient.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 139 insertions(+)

diff --git a/src/shared/btp.h b/src/shared/btp.h
index ad18cfb3a..cf921e64a 100644
--- a/src/shared/btp.h
+++ b/src/shared/btp.h
@@ -285,6 +285,24 @@ struct btp_gap_identity_resolved_ev {
 	bdaddr_t identity_address;
 } __packed;
 
+#define BTP_OP_GATT_READ_SUPPORTED_COMMANDS	0x01
+
+#define BTP_OP_GATT_READ_UUID			0x12
+struct btp_gatt_read_uuid_cp {
+	uint8_t address_type;
+	bdaddr_t address;
+	uint16_t start_handle;
+	uint16_t end_handle;
+	uint8_t uuid_len;
+	uint8_t uuid[];
+} __packed;
+
+struct btp_gatt_read_uuid_rp {
+	uint8_t status;
+	uint8_t count;
+	uint8_t data[];
+} __packed;
+
 struct btp;
 
 typedef void (*btp_destroy_func_t)(void *user_data);
diff --git a/tools/btpclient.c b/tools/btpclient.c
index b70e2b573..8466d0606 100644
--- a/tools/btpclient.c
+++ b/tools/btpclient.c
@@ -22,6 +22,7 @@
 #include <ell/ell.h>
 
 #include "bluetooth/bluetooth.h"
+#include "bluetooth/uuid.h"
 #include "src/shared/btp.h"
 
 #define AD_PATH "/org/bluez/advertising"
@@ -41,6 +42,7 @@
 #define AD_TYPE_MANUFACTURER_DATA		0xff
 
 static void register_gap_service(void);
+static void register_gatt_service(void);
 
 static struct l_dbus *dbus;
 
@@ -65,6 +67,9 @@ static char *socket_path;
 static struct btp *btp;
 
 static bool gap_service_registered;
+static bool gatt_service_registered;
+
+static bool gatt_uuid_name_in_progress;
 
 struct ad_data {
 	uint8_t data[25];
@@ -2769,6 +2774,72 @@ static void register_gap_service(void)
 					btp_gap_confirm_entry_rsp, NULL, NULL);
 }
 
+static void btp_gatt_read_commands(uint8_t index, const void *param,
+					uint16_t length, void *user_data)
+{
+	uint16_t commands = 0;
+
+	if (index != BTP_INDEX_NON_CONTROLLER) {
+		btp_send_error(btp, BTP_GATT_SERVICE, index,
+						BTP_ERROR_INVALID_INDEX);
+		return;
+	}
+
+	commands |= (1 << BTP_OP_GATT_READ_SUPPORTED_COMMANDS);
+	commands |= (1 << BTP_OP_GATT_READ_UUID);
+
+	commands = L_CPU_TO_LE16(commands);
+
+	btp_send(btp, BTP_GATT_SERVICE, BTP_OP_GATT_READ_SUPPORTED_COMMANDS,
+			BTP_INDEX_NON_CONTROLLER, sizeof(commands), &commands);
+}
+
+static void btp_gatt_read_uuid(uint8_t index, const void *param,
+					uint16_t length, void *user_data)
+{
+	struct btp_adapter *adapter = find_adapter_by_index(index);
+	const struct btp_gatt_read_uuid_cp *cp = param;
+	uint8_t status = BTP_ERROR_FAIL;
+	bool prop;
+
+	if (!adapter) {
+		status = BTP_ERROR_INVALID_INDEX;
+		goto failed;
+	}
+
+	/* Adapter needs to be powered to be able to read UUID */
+	if (!l_dbus_proxy_get_property(adapter->proxy, "Powered", "b",
+					&prop) || !prop) {
+		goto failed;
+	}
+
+	if (cp->uuid_len == 2) {
+		uint16_t uuid = cp->uuid[0] | (cp->uuid[1] << 8);
+
+		/* Name UUID is automatically retrieved during connection */
+		if (uuid == GATT_CHARAC_DEVICE_NAME) {
+			gatt_uuid_name_in_progress = true;
+			return;
+		}
+	}
+
+	/* TODO: Process other UUIDs */
+
+	return;
+
+failed:
+	btp_send_error(btp, BTP_GATT_SERVICE, index, status);
+}
+
+static void register_gatt_service(void)
+{
+	btp_register(btp, BTP_GATT_SERVICE, BTP_OP_GATT_READ_SUPPORTED_COMMANDS,
+					btp_gatt_read_commands, NULL, NULL);
+
+	btp_register(btp, BTP_GATT_SERVICE, BTP_OP_GATT_READ_UUID,
+					btp_gatt_read_uuid, NULL, NULL);
+}
+
 static void btp_core_read_commands(uint8_t index, const void *param,
 					uint16_t length, void *user_data)
 {
@@ -2833,6 +2904,16 @@ static void btp_core_register(uint8_t index, const void *param,
 
 		return;
 	case BTP_GATT_SERVICE:
+		if (gatt_service_registered)
+			goto failed;
+
+		register_gatt_service();
+		gatt_service_registered = true;
+
+		btp_send(btp, BTP_CORE_SERVICE, BTP_OP_CORE_REGISTER,
+					BTP_INDEX_NON_CONTROLLER, 0, NULL);
+
+		return;
 	case BTP_L2CAP_SERVICE:
 	case BTP_MESH_NODE_SERVICE:
 	case BTP_CORE_SERVICE:
@@ -2871,6 +2952,12 @@ static void btp_core_unregister(uint8_t index, const void *param,
 		gap_service_registered = false;
 		break;
 	case BTP_GATT_SERVICE:
+		if (!gatt_service_registered)
+			goto failed;
+
+		btp_unregister_service(btp, BTP_GATT_SERVICE);
+		gatt_service_registered = false;
+		break;
 	case BTP_L2CAP_SERVICE:
 	case BTP_MESH_NODE_SERVICE:
 	case BTP_CORE_SERVICE:
@@ -3162,6 +3249,40 @@ static void property_changed(struct l_dbus_proxy *proxy, const char *name,
 			 * type.
 			 */
 			btp_identity_resolved_ev(proxy);
+		} else if (!strcmp(name, "Name")) {
+			struct btp_device *device;
+			struct btp_adapter *adapter;
+			const char *pts_name;
+			uint8_t pts_name_len;
+			struct btp_gatt_read_uuid_rp *rp;
+
+			if (!gatt_uuid_name_in_progress)
+				return;
+
+			device = find_device_by_proxy(proxy);
+			adapter = find_adapter_by_device(device);
+
+			if (!l_dbus_message_get_arguments(msg, "s", &pts_name))
+				return;
+
+			pts_name_len = strlen(pts_name);
+			rp = malloc(sizeof(struct btp_gatt_read_uuid_rp) + 3 +
+								pts_name_len);
+
+			rp->status = 0;
+			rp->count = 1;
+			bt_put_le16(GATT_CHARAC_DEVICE_NAME, rp->data);
+			rp->data[2] = pts_name_len;
+			memcpy(rp->data+3, pts_name, pts_name_len);
+
+			btp_send(btp, BTP_GATT_SERVICE, BTP_OP_GATT_READ_UUID,
+					adapter->index,
+					sizeof(struct btp_gatt_read_uuid_rp) +
+					3 + pts_name_len, rp);
+
+			gatt_uuid_name_in_progress = false;
+
+			free(rp);
 		}
 	}
 }
-- 
2.43.0


             reply	other threads:[~2026-02-11  9:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-11  9:47 Frédéric Danis [this message]
2026-02-11 11:21 ` [BlueZ,v2] tools/btpclient: Add GATT support to get Device name bluez.test.bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260211094726.198317-1-frederic.danis@collabora.com \
    --to=frederic.danis@collabora.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox