All of lore.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 v6 3/5] client/btpclient: Add BTP_EV_GAP_SEC_LEVEL_CHANGED support
Date: Thu, 26 Nov 2026 09:52:09 +0100	[thread overview]
Message-ID: <20261126085211.80204-4-frederic.danis@collabora.com> (raw)
In-Reply-To: <20261126085211.80204-1-frederic.danis@collabora.com>

When supported, send BTP_EV_GAP_SEC_LEVEL_CHANGED from org.bluez.Device1
SecurityLevel property changes, mapped to the matching BTP level, and
skip levels with no BTP equivalent (no security/encryption yet).
Postponed this BTP event after device services has been retrieved to be
in sync with auto-pts expectation.

SecurityLevel's BT_SECURITY_MEDIUM only indicates encryption is
desired, not that it is active: also check EncryptionType before
reporting this level as encrypted, and refresh the BTP event on
EncryptionType changes too.

Replay the cached security level when GAP registers, or a device
proxy is added, for a device that is already connected and resolved,
so its security state isn't missed.

This reduces connection time for the BAP tests.
---
v2->v3: Only read and report SecurityLevel when ServicesResolved becomes
  true: device_remove_connection() clears ServicesResolved without
  clearing the cached sec_level, so the ServicesResolved handler was
  emitting a stale security-level-changed event when an encrypted peer
  disconnected.
v3->v4: Fetch the device's Address/AddressType D-Bus properties instead
  of reading the btp_device struct fields directly, consistent with the
  other event builders in this file (btp_gap_device_connection_ev(),
  btp_identity_resolved_ev()).
v4->v5: BT_SECURITY_MEDIUM only means encryption is desired, not that
  it is active: now also check EncryptionType before reporting this
  level, and refresh the event on EncryptionType changes too. Replay
  the cached security level in btp_gap_device_found_ev() for devices
  that are already connected and resolved when GAP registers or the
  proxy is added, so their security state isn't missed. Moved
  btp_security_level_changed_ev() before its only caller,
  btp_gap_device_found_ev(), to drop its forward declaration.

 client/btpclient/gap.c | 75 +++++++++++++++++++++++++++++++++++++++++-
 src/shared/btp.h       | 11 +++++++
 2 files changed, 85 insertions(+), 1 deletion(-)

diff --git a/client/btpclient/gap.c b/client/btpclient/gap.c
index 33a6c1693..2731329ef 100644
--- a/client/btpclient/gap.c
+++ b/client/btpclient/gap.c
@@ -2399,6 +2399,58 @@ failed:
 	btp_send_error(btp, BTP_GAP_SERVICE, index, status);
 }
 
+static void btp_security_level_changed_ev(struct l_dbus_proxy *proxy)
+{
+	struct btp_device *device = find_device_by_proxy(proxy);
+	struct btp_adapter *adapter = find_adapter_by_device(device);
+	const char *str_addr, *str_addr_type;
+	uint8_t level, enc_type;
+	struct btp_gap_sec_level_changed_ev ev;
+
+	if (!device || !adapter)
+		return;
+
+	if (!l_dbus_proxy_get_property(proxy, "SecurityLevel", "y", &level))
+		return;
+
+	if (!l_dbus_proxy_get_property(proxy, "Address", "s", &str_addr))
+		return;
+
+	if (!l_dbus_proxy_get_property(proxy, "AddressType", "s",
+								&str_addr_type))
+		return;
+
+	str2ba(str_addr, &ev.address);
+	ev.address_type = strcmp(str_addr_type, "public") ?
+				BTP_GAP_ADDR_RANDOM : BTP_GAP_ADDR_PUBLIC;
+	switch (level) {
+	case BT_SECURITY_MEDIUM:
+		/* This level only means encryption is desired, not that it
+		 * is active yet: only report it once EncryptionType confirms
+		 * encryption is actually in use.
+		 */
+		if (!l_dbus_proxy_get_property(proxy, "EncryptionType", "y",
+								&enc_type) ||
+								!enc_type)
+			return;
+
+		ev.sec_level = BTP_GAP_SEC_LEVEL_UNAUTH_ENC;
+		break;
+	case BT_SECURITY_HIGH:
+		ev.sec_level = BTP_GAP_SEC_LEVEL_AUTH_ENC;
+		break;
+	case BT_SECURITY_FIPS:
+		ev.sec_level = BTP_GAP_SEC_LEVEL_AUTH_SC;
+		break;
+	default:
+		/* No security/encryption yet, no BTP level to report. */
+		return;
+	}
+
+	btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_SEC_LEVEL_CHANGED,
+					adapter->index, sizeof(ev), &ev);
+}
+
 static void btp_gap_device_found_ev(struct l_dbus_proxy *proxy)
 {
 	struct btp_device *device = find_device_by_proxy(proxy);
@@ -2518,6 +2570,12 @@ static void btp_gap_device_found_ev(struct l_dbus_proxy *proxy)
 
 		btp_send(btp, BTP_GAP_SERVICE, BTP_EV_GAP_DEVICE_CONNECTED,
 				adapter->index, sizeof(ev_conn), &ev_conn);
+
+		/* Replay the cached security level for devices that were
+		 * already connected and secured before GAP registered or
+		 * this proxy was added.
+		 */
+		btp_security_level_changed_ev(proxy);
 	}
 }
 
@@ -2736,6 +2794,19 @@ void gap_property_changed(struct l_dbus_proxy *proxy, const char *name,
 			 */
 			if (!prop)
 				btp_gap_device_connection_ev(proxy, prop);
+		} else if (!strcmp(name, "SecurityLevel") ||
+					!strcmp(name, "EncryptionType")) {
+			bool resolved;
+
+			/* Postpone security level changed event after services
+			 * has been resolved.
+			 */
+			if (l_dbus_proxy_get_property(proxy,
+					"ServicesResolved", "b", &resolved) &&
+					!resolved)
+				return;
+
+			btp_security_level_changed_ev(proxy);
 		} else if (!strcmp(name, "AddressType")) {
 			/* Address property change came first along with address
 			 * type.
@@ -2747,8 +2818,10 @@ void gap_property_changed(struct l_dbus_proxy *proxy, const char *name,
 			if (!l_dbus_message_get_arguments(msg, "b", &prop))
 				return;
 
-			if (prop)
+			if (prop) {
 				btp_gap_device_connection_ev(proxy, prop);
+				btp_security_level_changed_ev(proxy);
+			}
 		}
 	}
 }
diff --git a/src/shared/btp.h b/src/shared/btp.h
index ee5d7bd21..8cae0e765 100644
--- a/src/shared/btp.h
+++ b/src/shared/btp.h
@@ -296,6 +296,17 @@ struct btp_gap_identity_resolved_ev {
 	bdaddr_t identity_address;
 } __packed;
 
+#define BTP_GAP_SEC_LEVEL_UNAUTH_ENC		0x01
+#define BTP_GAP_SEC_LEVEL_AUTH_ENC		0x02
+#define BTP_GAP_SEC_LEVEL_AUTH_SC		0x03
+
+#define BTP_EV_GAP_SEC_LEVEL_CHANGED		0x89
+struct btp_gap_sec_level_changed_ev {
+	uint8_t address_type;
+	bdaddr_t address;
+	uint8_t sec_level;
+} __packed;
+
 struct btp_gatt_service {
 	uint16_t start_handle;
 	uint16_t end_handle;
-- 
2.43.0


  parent reply	other threads:[~2026-11-26  8:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  8:52 [PATCH BlueZ v6 0/5] client/btpclient: Security Level reporting Frédéric Danis
2026-11-26  8:52 ` [PATCH BlueZ v6 1/5] device: Provide Security Level props when kernel supports them Frédéric Danis
2026-11-26 15:13   ` client/btpclient: Security Level reporting bluez.test.bot
2026-12-01 15:44     ` Luiz Augusto von Dentz
2026-11-26  8:52 ` [PATCH BlueZ v6 2/5] org.bluez.Device: Make Security Level props optional Frédéric Danis
2026-11-26  8:52 ` Frédéric Danis [this message]
2026-11-26  8:52 ` [PATCH BlueZ v6 4/5] device: Reset Security Level and Encryption Type on disconnect Frédéric Danis
2026-11-26  8:52 ` [PATCH BlueZ v6 5/5] client/btpclient: Suppress duplicate BTP security-level events Frédéric Danis

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=20261126085211.80204-4-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.