Skip to content

Commit 22ea55c

Browse files
lylezhu2012kartben
authored andcommitted
Bluetooth: Classic: Move classic functions from conn.c to conn_br.c
Move functions `bt_sco_cleanup()`, `bt_conn_create_br()`, `bt_hci_connect_br_cancel()`, and `bt_conn_get_dst_br()` from `conn.c` to `conn_br.c`. Change the function `bt_hci_connect_br_cancel()` to internal public function. Add a header file `conn_br_internal.h`. And declare the function `bt_hci_connect_br_cancel()` in the header file. Signed-off-by: Lyle Zhu <[email protected]>
1 parent 0e1ffc7 commit 22ea55c

File tree

4 files changed

+159
-103
lines changed

4 files changed

+159
-103
lines changed

subsys/bluetooth/host/classic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ zephyr_library_sources_ifdef(CONFIG_BT_RFCOMM rfcomm.c)
1414
zephyr_library_sources_ifdef(
1515
CONFIG_BT_CLASSIC
1616
br.c
17+
conn_br.c
1718
keys_br.c
1819
l2cap_br.c
1920
sdp.c
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/* conn_br.c - Bluetooth Classic connection handling */
2+
3+
/*
4+
* Copyright (c) 2015-2016 Intel Corporation
5+
* Copyright (c) 2025 Nordic Semiconductor ASA
6+
*
7+
* SPDX-License-Identifier: Apache-2.0
8+
*/
9+
10+
#include <zephyr/bluetooth/iso.h>
11+
#include <zephyr/kernel.h>
12+
#include <string.h>
13+
#include <errno.h>
14+
#include <stdbool.h>
15+
#include <zephyr/sys/atomic.h>
16+
#include <zephyr/sys/byteorder.h>
17+
#include <zephyr/sys/check.h>
18+
#include <zephyr/sys/iterable_sections.h>
19+
#include <zephyr/sys/util.h>
20+
#include <zephyr/sys/util_macro.h>
21+
#include <zephyr/sys/slist.h>
22+
#include <zephyr/debug/stack.h>
23+
#include <zephyr/sys/__assert.h>
24+
25+
#include <zephyr/bluetooth/hci.h>
26+
#include <zephyr/bluetooth/bluetooth.h>
27+
#include <zephyr/bluetooth/direction.h>
28+
#include <zephyr/bluetooth/conn.h>
29+
#include <zephyr/bluetooth/hci_vs.h>
30+
#include <zephyr/bluetooth/att.h>
31+
32+
#include "common/assert.h"
33+
#include "common/bt_str.h"
34+
35+
#include "host/conn_internal.h"
36+
#include "host/l2cap_internal.h"
37+
#include "host/keys.h"
38+
#include "host/smp.h"
39+
#include "ssp.h"
40+
#include "sco_internal.h"
41+
42+
#define LOG_LEVEL CONFIG_BT_CONN_LOG_LEVEL
43+
#include <zephyr/logging/log.h>
44+
LOG_MODULE_REGISTER(bt_conn_br);
45+
46+
void bt_sco_cleanup(struct bt_conn *sco_conn)
47+
{
48+
bt_sco_cleanup_acl(sco_conn);
49+
bt_conn_unref(sco_conn);
50+
}
51+
52+
struct bt_conn *bt_conn_create_br(const bt_addr_t *peer,
53+
const struct bt_br_conn_param *param)
54+
{
55+
struct bt_hci_cp_connect *cp;
56+
struct bt_conn *conn;
57+
struct net_buf *buf;
58+
59+
conn = bt_conn_lookup_addr_br(peer);
60+
if (conn) {
61+
switch (conn->state) {
62+
case BT_CONN_INITIATING:
63+
case BT_CONN_CONNECTED:
64+
return conn;
65+
default:
66+
bt_conn_unref(conn);
67+
return NULL;
68+
}
69+
}
70+
71+
conn = bt_conn_add_br(peer);
72+
if (!conn) {
73+
return NULL;
74+
}
75+
76+
buf = bt_hci_cmd_create(BT_HCI_OP_CONNECT, sizeof(*cp));
77+
if (!buf) {
78+
bt_conn_unref(conn);
79+
return NULL;
80+
}
81+
82+
cp = net_buf_add(buf, sizeof(*cp));
83+
84+
(void)memset(cp, 0, sizeof(*cp));
85+
86+
memcpy(&cp->bdaddr, peer, sizeof(cp->bdaddr));
87+
cp->packet_type = sys_cpu_to_le16(0xcc18); /* DM1 DH1 DM3 DH5 DM5 DH5 */
88+
cp->pscan_rep_mode = 0x02; /* R2 */
89+
cp->allow_role_switch = param->allow_role_switch ? 0x01 : 0x00;
90+
cp->clock_offset = 0x0000; /* TODO used cached clock offset */
91+
92+
if (bt_hci_cmd_send_sync(BT_HCI_OP_CONNECT, buf, NULL) < 0) {
93+
bt_conn_unref(conn);
94+
return NULL;
95+
}
96+
97+
bt_conn_set_state(conn, BT_CONN_INITIATING);
98+
conn->role = BT_CONN_ROLE_CENTRAL;
99+
100+
return conn;
101+
}
102+
103+
int bt_hci_connect_br_cancel(struct bt_conn *conn)
104+
{
105+
struct bt_hci_cp_connect_cancel *cp;
106+
struct bt_hci_rp_connect_cancel *rp;
107+
struct net_buf *buf, *rsp;
108+
int err;
109+
110+
buf = bt_hci_cmd_create(BT_HCI_OP_CONNECT_CANCEL, sizeof(*cp));
111+
if (!buf) {
112+
return -ENOBUFS;
113+
}
114+
115+
cp = net_buf_add(buf, sizeof(*cp));
116+
memcpy(&cp->bdaddr, &conn->br.dst, sizeof(cp->bdaddr));
117+
118+
err = bt_hci_cmd_send_sync(BT_HCI_OP_CONNECT_CANCEL, buf, &rsp);
119+
if (err) {
120+
return err;
121+
}
122+
123+
rp = (void *)rsp->data;
124+
125+
err = rp->status ? -EIO : 0;
126+
127+
net_buf_unref(rsp);
128+
129+
return err;
130+
}
131+
132+
const bt_addr_t *bt_conn_get_dst_br(const struct bt_conn *conn)
133+
{
134+
if (conn == NULL) {
135+
LOG_DBG("Invalid connect");
136+
return NULL;
137+
}
138+
139+
if (!bt_conn_is_type(conn, BT_CONN_TYPE_BR)) {
140+
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
141+
return NULL;
142+
}
143+
144+
return &conn->br.dst;
145+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/** @file
2+
* @brief Internal APIs for Bluetooth classic connection handling.
3+
*/
4+
5+
/*
6+
* Copyright (c) 2015 Intel Corporation
7+
* Copyright (c) 2021 Nordic Semiconductor ASA
8+
*
9+
* SPDX-License-Identifier: Apache-2.0
10+
*/
11+
12+
int bt_hci_connect_br_cancel(struct bt_conn *conn);

subsys/bluetooth/host/conn.c

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "iso_internal.h"
4848
#include "direction_internal.h"
4949
#include "classic/sco_internal.h"
50+
#include "classic/conn_br_internal.h"
5051

5152
#define LOG_LEVEL CONFIG_BT_CONN_LOG_LEVEL
5253
#include <zephyr/logging/log.h>
@@ -112,8 +113,6 @@ static sys_slist_t conn_cbs = SYS_SLIST_STATIC_INIT(&conn_cbs);
112113
static struct bt_conn_tx conn_tx[CONFIG_BT_CONN_TX_MAX];
113114

114115
#if defined(CONFIG_BT_CLASSIC)
115-
static int bt_hci_connect_br_cancel(struct bt_conn *conn);
116-
117116
static struct bt_conn sco_conns[CONFIG_BT_MAX_SCO_CONN];
118117
#endif /* CONFIG_BT_CLASSIC */
119118
#endif /* CONFIG_BT_CONN */
@@ -2298,68 +2297,11 @@ static struct bt_conn *acl_conn_new(void)
22982297
}
22992298

23002299
#if defined(CONFIG_BT_CLASSIC)
2301-
void bt_sco_cleanup(struct bt_conn *sco_conn)
2302-
{
2303-
bt_sco_cleanup_acl(sco_conn);
2304-
bt_conn_unref(sco_conn);
2305-
}
2306-
23072300
static struct bt_conn *sco_conn_new(void)
23082301
{
23092302
return bt_conn_new(sco_conns, ARRAY_SIZE(sco_conns));
23102303
}
23112304

2312-
struct bt_conn *bt_conn_create_br(const bt_addr_t *peer,
2313-
const struct bt_br_conn_param *param)
2314-
{
2315-
struct bt_hci_cp_connect *cp;
2316-
struct bt_conn *conn;
2317-
struct net_buf *buf;
2318-
2319-
conn = bt_conn_lookup_addr_br(peer);
2320-
if (conn) {
2321-
switch (conn->state) {
2322-
case BT_CONN_INITIATING:
2323-
case BT_CONN_CONNECTED:
2324-
return conn;
2325-
default:
2326-
bt_conn_unref(conn);
2327-
return NULL;
2328-
}
2329-
}
2330-
2331-
conn = bt_conn_add_br(peer);
2332-
if (!conn) {
2333-
return NULL;
2334-
}
2335-
2336-
buf = bt_hci_cmd_create(BT_HCI_OP_CONNECT, sizeof(*cp));
2337-
if (!buf) {
2338-
bt_conn_unref(conn);
2339-
return NULL;
2340-
}
2341-
2342-
cp = net_buf_add(buf, sizeof(*cp));
2343-
2344-
(void)memset(cp, 0, sizeof(*cp));
2345-
2346-
memcpy(&cp->bdaddr, peer, sizeof(cp->bdaddr));
2347-
cp->packet_type = sys_cpu_to_le16(0xcc18); /* DM1 DH1 DM3 DH5 DM5 DH5 */
2348-
cp->pscan_rep_mode = 0x02; /* R2 */
2349-
cp->allow_role_switch = param->allow_role_switch ? 0x01 : 0x00;
2350-
cp->clock_offset = 0x0000; /* TODO used cached clock offset */
2351-
2352-
if (bt_hci_cmd_send_sync(BT_HCI_OP_CONNECT, buf, NULL) < 0) {
2353-
bt_conn_unref(conn);
2354-
return NULL;
2355-
}
2356-
2357-
bt_conn_set_state(conn, BT_CONN_INITIATING);
2358-
conn->role = BT_CONN_ROLE_CENTRAL;
2359-
2360-
return conn;
2361-
}
2362-
23632305
struct bt_conn *bt_conn_lookup_addr_sco(const bt_addr_t *peer)
23642306
{
23652307
int i;
@@ -2467,50 +2409,6 @@ struct bt_conn *bt_conn_add_br(const bt_addr_t *peer)
24672409

24682410
return conn;
24692411
}
2470-
2471-
static int bt_hci_connect_br_cancel(struct bt_conn *conn)
2472-
{
2473-
struct bt_hci_cp_connect_cancel *cp;
2474-
struct bt_hci_rp_connect_cancel *rp;
2475-
struct net_buf *buf, *rsp;
2476-
int err;
2477-
2478-
buf = bt_hci_cmd_create(BT_HCI_OP_CONNECT_CANCEL, sizeof(*cp));
2479-
if (!buf) {
2480-
return -ENOBUFS;
2481-
}
2482-
2483-
cp = net_buf_add(buf, sizeof(*cp));
2484-
memcpy(&cp->bdaddr, &conn->br.dst, sizeof(cp->bdaddr));
2485-
2486-
err = bt_hci_cmd_send_sync(BT_HCI_OP_CONNECT_CANCEL, buf, &rsp);
2487-
if (err) {
2488-
return err;
2489-
}
2490-
2491-
rp = (void *)rsp->data;
2492-
2493-
err = rp->status ? -EIO : 0;
2494-
2495-
net_buf_unref(rsp);
2496-
2497-
return err;
2498-
}
2499-
2500-
const bt_addr_t *bt_conn_get_dst_br(const struct bt_conn *conn)
2501-
{
2502-
if (conn == NULL) {
2503-
LOG_DBG("Invalid connect");
2504-
return NULL;
2505-
}
2506-
2507-
if (!bt_conn_is_type(conn, BT_CONN_TYPE_BR)) {
2508-
LOG_DBG("Invalid connection type: %u for %p", conn->type, conn);
2509-
return NULL;
2510-
}
2511-
2512-
return &conn->br.dst;
2513-
}
25142412
#endif /* CONFIG_BT_CLASSIC */
25152413

25162414
#if defined(CONFIG_BT_SMP)

0 commit comments

Comments
 (0)