Linux bluetooth development
 help / color / mirror / Atom feed
From: Simon Mikuda <simon.mikuda@streamunlimited.com>
To: linux-bluetooth@vger.kernel.org
Cc: Simon Mikuda <simon.mikuda@streamunlimited.com>
Subject: [PATCH BlueZ 8/8] device: Connect GATT over BR/EDR with ConnectProfile
Date: Tue, 24 Nov 2026 15:46:42 +0100	[thread overview]
Message-ID: <20261124144642.298366-9-simon.mikuda@streamunlimited.com> (raw)
In-Reply-To: <20261124144642.298366-1-simon.mikuda@streamunlimited.com>

bluetoothd runs GATT over BR/EDR only when the remote opens the ATT
channel, so the GATT services a BR/EDR device lists in SDP can't be
discovered from our side, as the TODO in search_cb() says.

Add a gatt-bredr profile for a device whose SDP has the GATT service:
ConnectProfile and DisconnectProfile with the GATT UUID open and close
ATT over BR/EDR, and its state follows an ATT channel the remote opens.
It isn't connected automatically, so Connect() works as before.

  bluetoothctl connect <address> 00001801-0000-1000-8000-00805f9b34fb

Assisted-by: Claude:claude-opus-5-5
---
 src/device.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)

diff --git a/src/device.c b/src/device.c
index 8508fed81..be3278c19 100644
--- a/src/device.c
+++ b/src/device.c
@@ -394,6 +394,14 @@ static GSList *find_service_with_uuid(GSList *list, char *uuid)
 	return NULL;
 }
 
+/* The GATT service of a BR/EDR device, connected while ATT runs over BR/EDR */
+static struct btd_service *gatt_bredr_service(struct btd_device *dev)
+{
+	GSList *l = find_service_with_uuid(dev->services, GATT_UUID);
+
+	return l ? l->data : NULL;
+}
+
 static const char *device_prefer_bearer_str(struct btd_device *device)
 {
 	/* Check if both BR/EDR and LE bearer are supported */
@@ -6373,6 +6381,7 @@ static void disconnect_gatt_service(gpointer data, gpointer user_data)
 static void att_disconnected_cb(int err, void *user_data)
 {
 	struct btd_device *device = user_data;
+	struct btd_service *service;
 
 	DBG("");
 
@@ -6399,6 +6408,10 @@ static void att_disconnected_cb(int err, void *user_data)
 		adapter_connect_list_add(device->adapter, device);
 
 done:
+	service = gatt_bredr_service(device);
+	if (service)
+		btd_service_disconnecting_complete(service, 0);
+
 	attio_cleanup(device);
 }
 
@@ -6774,6 +6787,14 @@ bool device_attach_att(struct btd_device *dev, GIOChannel *io)
 	gatt_client_init(dev);
 	gatt_server_init(dev, database);
 
+	/* ATT over BR/EDR, opened by either side, is the GATT service */
+	if (bt_att_get_link_type(dev->att) == BT_ATT_BREDR) {
+		struct btd_service *service = gatt_bredr_service(dev);
+
+		if (service)
+			btd_service_connecting_complete(service, 0);
+	}
+
 	/*
 	 * Remove the device from the connect_list and give the passive
 	 * scanning another chance to be restarted in case there are
@@ -6861,6 +6882,95 @@ done:
 	}
 }
 
+static void gatt_bredr_connect_cb(GIOChannel *io, GError *gerr,
+							gpointer user_data)
+{
+	struct btd_device *dev = user_data;
+	struct btd_service *service = gatt_bredr_service(dev);
+
+	g_io_channel_unref(dev->att_io);
+	dev->att_io = NULL;
+
+	if (gerr) {
+		DBG("%s", gerr->message);
+		if (service)
+			btd_service_connecting_complete(service, -EIO);
+		return;
+	}
+
+	if (!device_attach_att(dev, io) && service)
+		btd_service_connecting_complete(service, -EIO);
+}
+
+static int gatt_bredr_probe(struct btd_service *service)
+{
+	struct btd_device *dev = btd_service_get_device(service);
+
+	/* Only a device that lists the GATT service in SDP */
+	if (!btd_device_get_record(dev, GATT_UUID))
+		return -ENOTSUP;
+
+	return 0;
+}
+
+static void gatt_bredr_remove(struct btd_service *service)
+{
+}
+
+static int gatt_bredr_connect(struct btd_service *service)
+{
+	struct btd_device *dev = btd_service_get_device(service);
+	GError *gerr = NULL;
+	char addr[18];
+
+	/* A device has one ATT bearer, over LE or BR/EDR */
+	if (dev->att || dev->att_io)
+		return -EALREADY;
+
+	ba2str(&dev->bdaddr, addr);
+	DBG("Connection attempt to: %s", addr);
+
+	dev->att_io = bt_io_connect(gatt_bredr_connect_cb, dev, NULL, &gerr,
+				BT_IO_OPT_SOURCE_BDADDR,
+				btd_adapter_get_address(dev->adapter),
+				BT_IO_OPT_DEST_BDADDR, &dev->bdaddr,
+				BT_IO_OPT_PSM, BT_ATT_PSM,
+				BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_MEDIUM,
+				BT_IO_OPT_MTU, btd_opts.gatt_mtu,
+				BT_IO_OPT_INVALID);
+	if (!dev->att_io) {
+		error("ATT bt_io_connect(%s): %s", addr, gerr->message);
+		g_error_free(gerr);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int gatt_bredr_disconnect(struct btd_service *service)
+{
+	struct btd_device *dev = btd_service_get_device(service);
+
+	if (!dev->att || bt_att_get_link_type(dev->att) != BT_ATT_BREDR)
+		return -ENOTCONN;
+
+	/* Clean up as on a remote disconnection, which closes the channel */
+	att_disconnected_cb(0, dev);
+
+	return 0;
+}
+
+/* GATT over BR/EDR is only connected on request, with ConnectProfile */
+static struct btd_profile gatt_bredr_profile = {
+	.name		= "gatt-bredr",
+	.bearer		= BTD_PROFILE_BEARER_BREDR,
+	.remote_uuid	= GATT_UUID,
+	.device_probe	= gatt_bredr_probe,
+	.device_remove	= gatt_bredr_remove,
+	.connect	= gatt_bredr_connect,
+	.disconnect	= gatt_bredr_disconnect,
+};
+
 int device_connect_le(struct btd_device *dev)
 {
 	struct btd_adapter *adapter = dev->adapter;
@@ -8504,11 +8614,15 @@ void btd_device_init(void)
 	dbus_conn = btd_get_dbus_connection();
 	service_state_cb_id = btd_service_add_state_cb(
 						service_state_changed, NULL);
+
+	btd_profile_register(&gatt_bredr_profile);
 }
 
 void btd_device_cleanup(void)
 {
 	btd_service_remove_state_cb(service_state_cb_id);
+
+	btd_profile_unregister(&gatt_bredr_profile);
 }
 
 void btd_device_set_volume(struct btd_device *device, int8_t volume)
-- 
2.43.0


      parent reply	other threads:[~2026-11-24 14:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 14:46 [PATCH BlueZ 0/8] Fix GATT over BR/EDR Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 1/8] tools/btgatt-client: Add BR/EDR support Simon Mikuda
2026-11-24 20:44   ` Fix GATT over BR/EDR bluez.test.bot
2026-11-24 14:46 ` [PATCH BlueZ 2/8] shared/att: Use the smaller L2CAP MTU as ATT MTU Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 3/8] device: Don't override the ATT MTU over BR/EDR Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 4/8] device: Don't resolve services on GATT " Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 5/8] gatt-client: Don't connect EATT on an ATT bearer " Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 6/8] profiles: Set the LE bearer for TMAP, GMAP and RAP Simon Mikuda
2026-11-24 14:46 ` [PATCH BlueZ 7/8] device: Don't accept LE profiles over ATT on BR/EDR Simon Mikuda
2026-11-24 14:46 ` Simon Mikuda [this message]

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=20261124144642.298366-9-simon.mikuda@streamunlimited.com \
    --to=simon.mikuda@streamunlimited.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