Linux bluetooth development
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v1 1/2] emulator: Add helpers to hold SMP and send LE credits
Date: Thu,  3 Dec 2026 13:14:41 -0500	[thread overview]
Message-ID: <20261203181442.774979-2-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261203181442.774979-1-luiz.dentz@gmail.com>

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

Add bthost_hold_smp() and bthost_release_smp() to hold the first SMP
PDU received and only process it when released, so tests can act while
a security procedure is pending, and bthost_send_le_credits() to send
an LE Flow Control Credit packet for a given channel.

Assisted-by: OpenCode:claude-opus-5.5
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
 emulator/bthost.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++
 emulator/bthost.h |  4 ++++
 2 files changed, 61 insertions(+)

diff --git a/emulator/bthost.c b/emulator/bthost.c
index afc83891778e..e0c70a3babc8 100644
--- a/emulator/bthost.c
+++ b/emulator/bthost.c
@@ -170,6 +170,8 @@ struct btconn {
 	struct iso_hook *iso_hook;
 	struct btconn *next;
 	void *smp_data;
+	void *smp_held;
+	uint16_t smp_held_len;
 	uint16_t recv_len;
 	uint16_t data_len;
 	void *recv_data;
@@ -276,6 +278,7 @@ struct bthost {
 	bool reject_user_confirm;
 	bool hold_auth_request;
 	bool auth_request_held;
+	bool hold_smp;
 	bool auth_request_pin;
 	bool l2cap_rsp_pending;
 	uint8_t auth_request_bdaddr[6];
@@ -330,6 +333,8 @@ static void btconn_free(struct btconn *conn)
 	if (conn->smp_data)
 		smp_conn_del(conn->smp_data);
 
+	free(conn->smp_held);
+
 	while (conn->l2conns) {
 		struct l2conn *l2conn = conn->l2conns;
 
@@ -3339,6 +3344,14 @@ static void process_l2cap(struct bthost *bthost, struct btconn *conn,
 		l2cap_le_sig(bthost, conn, data.iov_base, data.iov_len);
 		break;
 	case 0x0006:
+		/* Hold the first SMP PDU until bthost_release_smp() */
+		if (bthost->hold_smp && !conn->smp_held) {
+			conn->smp_held = util_memdup(data.iov_base,
+							data.iov_len);
+			conn->smp_held_len = data.iov_len;
+			break;
+		}
+
 		smp_data(conn->smp_data, data.iov_base, data.iov_len);
 		break;
 	case 0x0007:
@@ -4346,6 +4359,50 @@ void bthost_release_auth_request(struct bthost *bthost)
 	}
 }
 
+void bthost_send_le_credits(struct bthost *bthost, uint16_t handle,
+						uint16_t cid, uint16_t credits)
+{
+	struct bt_l2cap_pdu_le_flowctl_creds creds;
+	struct btconn *conn;
+	struct l2conn *l2conn;
+
+	conn = bthost_find_conn(bthost, handle);
+	if (!conn)
+		return;
+
+	l2conn = btconn_find_l2cap_conn_by_dcid(conn, cid);
+	if (!l2conn)
+		return;
+
+	creds.cid = cpu_to_le16(l2conn->scid);
+	creds.credits = cpu_to_le16(credits);
+	l2cap_sig_send(bthost, conn, BT_L2CAP_PDU_LE_FLOWCTL_CREDS, 0,
+							&creds, sizeof(creds));
+}
+
+void bthost_hold_smp(struct bthost *bthost, bool hold)
+{
+	bthost->hold_smp = hold;
+}
+
+void bthost_release_smp(struct bthost *bthost, uint16_t handle)
+{
+	struct btconn *conn;
+	void *pdu;
+
+	bthost->hold_smp = false;
+
+	conn = bthost_find_conn(bthost, handle);
+	if (!conn || !conn->smp_held)
+		return;
+
+	pdu = conn->smp_held;
+	conn->smp_held = NULL;
+
+	smp_data(conn->smp_data, pdu, conn->smp_held_len);
+	free(pdu);
+}
+
 void bthost_set_l2cap_rsp_pending(struct bthost *bthost, bool enable)
 {
 	bthost->l2cap_rsp_pending = enable;
diff --git a/emulator/bthost.h b/emulator/bthost.h
index 92aaf42305da..1253a24a367e 100644
--- a/emulator/bthost.h
+++ b/emulator/bthost.h
@@ -208,6 +208,10 @@ void bthost_set_auth_req(struct bthost *bthost, uint8_t auth_req);
 uint8_t bthost_get_auth_req(struct bthost *bthost);
 void bthost_hold_auth_request(struct bthost *bthost, bool hold);
 void bthost_release_auth_request(struct bthost *bthost);
+void bthost_hold_smp(struct bthost *bthost, bool hold);
+void bthost_release_smp(struct bthost *bthost, uint16_t handle);
+void bthost_send_le_credits(struct bthost *bthost, uint16_t handle,
+						uint16_t cid, uint16_t credits);
 void bthost_set_l2cap_rsp_pending(struct bthost *bthost, bool enable);
 void bthost_set_conn_encrypt(struct bthost *bthost, uint16_t handle,
 							uint8_t encr_mode);
-- 
2.56.1


  reply	other threads:[~2026-12-03 18:14 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 18:14 [PATCH BlueZ v1 0/2] l2cap-tester: Add tests for changing LE security with SMP pending Luiz Augusto von Dentz
2026-12-03 18:14 ` Luiz Augusto von Dentz [this message]
2026-12-03 18:14 ` [PATCH BlueZ v1 2/2] " Luiz Augusto von Dentz

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=20261203181442.774979-2-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.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