From 05f3aab743f097d1f8f10de77201e579e29d110a Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Mon, 6 Oct 2025 01:17:39 +0300 Subject: [PATCH] bthost: handle L2CAP_DISCONN_RSP and remove l2cap_conns Handle L2CAP disconnection response. On receiving disconnection request or response, remove the associated connection. Change disconnect handler signature to take also the handle and CID. --- emulator/bthost.c | 61 +++++++++++++++++++++++++++++++++++++++++++- emulator/bthost.h | 6 ++++- tools/l2cap-tester.c | 3 ++- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/emulator/bthost.c b/emulator/bthost.c index c85f751cc..93023331e 100644 --- a/emulator/bthost.c +++ b/emulator/bthost.c @@ -403,6 +403,25 @@ static struct l2conn *bthost_add_l2cap_conn(struct bthost *bthost, return l2conn; } +static void btconn_detach_l2cap_conn(struct btconn *conn, struct l2conn *l2conn) +{ + struct l2conn *c; + + if (conn->l2conns == l2conn) { + conn->l2conns = l2conn->next; + l2conn->next = NULL; + return; + } + + for (c = conn->l2conns; c != NULL; c = c->next) { + if (c->next == l2conn) { + c->next = l2conn->next; + l2conn->next = NULL; + return; + } + } +} + static struct rcconn *bthost_add_rfcomm_conn(struct bthost *bthost, struct btconn *conn, struct l2conn *l2conn, @@ -2142,11 +2161,41 @@ static bool l2cap_disconn_req(struct bthost *bthost, struct btconn *conn, if (!l2conn) return true; + btconn_detach_l2cap_conn(conn, l2conn); + cb_data = bthost_find_l2cap_cb_by_psm(bthost, l2conn->psm); if (cb_data && cb_data->disconn_func) - cb_data->disconn_func(cb_data->user_data); + cb_data->disconn_func(conn->handle, l2conn->dcid, + cb_data->user_data); + l2conn_free(l2conn); + return true; +} + +static bool l2cap_disconn_rsp(struct bthost *bthost, struct btconn *conn, + uint8_t ident, const void *data, uint16_t len) +{ + const struct bt_l2cap_pdu_disconn_rsp *rsp = data; + struct l2cap_conn_cb_data *cb_data; + struct l2conn *l2conn; + + if (len < sizeof(*rsp)) + return false; + + l2conn = btconn_find_l2cap_conn_by_scid(conn, rsp->dcid); + if (!l2conn) + return true; + + btconn_detach_l2cap_conn(conn, l2conn); + + cb_data = bthost_find_l2cap_cb_by_psm(bthost, l2conn->psm); + + if (cb_data && cb_data->disconn_func) + cb_data->disconn_func(conn->handle, l2conn->dcid, + cb_data->user_data); + + l2conn_free(l2conn); return true; } @@ -2302,6 +2351,11 @@ static void l2cap_sig(struct bthost *bthost, struct btconn *conn, data + sizeof(*hdr), hdr_len); break; + case BT_L2CAP_PDU_DISCONN_RSP: + ret = l2cap_disconn_rsp(bthost, conn, hdr->ident, + data + sizeof(*hdr), hdr_len); + break; + case BT_L2CAP_PDU_INFO_REQ: ret = l2cap_info_req(bthost, conn, hdr->ident, data + sizeof(*hdr), hdr_len); @@ -2536,6 +2590,11 @@ static void l2cap_le_sig(struct bthost *bthost, struct btconn *conn, data + sizeof(*hdr), hdr_len); break; + case BT_L2CAP_PDU_DISCONN_RSP: + ret = l2cap_disconn_rsp(bthost, conn, hdr->ident, + data + sizeof(*hdr), hdr_len); + break; + case BT_L2CAP_PDU_CONN_PARAM_REQ: ret = l2cap_conn_param_req(bthost, conn, hdr->ident, data + sizeof(*hdr), hdr_len); diff --git a/emulator/bthost.h b/emulator/bthost.h index 743615838..d60111d25 100644 --- a/emulator/bthost.h +++ b/emulator/bthost.h @@ -99,6 +99,9 @@ void bthost_send_iso(struct bthost *bthost, uint16_t handle, bool ts, uint16_t sn, uint32_t timestamp, uint8_t pkt_status, const struct iovec *iov, int iovcnt); +void bthost_disconnect_cid(struct bthost *bthost, uint16_t handle, + uint16_t cid); + typedef void (*bthost_l2cap_rsp_cb) (uint8_t code, const void *data, uint16_t len, void *user_data); @@ -145,7 +148,8 @@ void bthost_le_start_encrypt(struct bthost *bthost, uint16_t handle, const uint8_t ltk[16]); typedef void (*bthost_l2cap_connect_cb) (uint16_t handle, uint16_t cid, void *user_data); -typedef void (*bthost_l2cap_disconnect_cb) (void *user_data); +typedef void (*bthost_l2cap_disconnect_cb) (uint16_t handle, uint16_t cid, + void *user_data); void bthost_add_l2cap_server(struct bthost *bthost, uint16_t psm, bthost_l2cap_connect_cb func, diff --git a/tools/l2cap-tester.c b/tools/l2cap-tester.c index 208772527..a9ab8b051 100644 --- a/tools/l2cap-tester.c +++ b/tools/l2cap-tester.c @@ -1726,7 +1726,8 @@ static void client_l2cap_connect_cb(uint16_t handle, uint16_t cid, data->handle = handle; } -static void client_l2cap_disconnect_cb(void *user_data) +static void client_l2cap_disconnect_cb(uint16_t handle, uint16_t cid, + void *user_data) { struct test_data *data = user_data; -- 2.47.3