Skip to content

Commit 7ebe4b6

Browse files
zhongzhijie1hyson710
authored andcommitted
add direct profile connect/disconnect flow and API docs
bug: v/77661 - Add profile connection handler type and bt_profile_conn_handler_node_t structure. - Introduce bt_sal_profile_connect_request, bt_sal_profile_disconnect_register and bt_sal_profile_disconnect APIs. - Add internal worker APIs: bt_sal_remove_bond_internal, bt_sal_disconnect_internal. - Expand connection manager data model: - separate connecting / disconnecting manager lists - support dynamic profile_conn_handler_node per device - change profile flags to 64-bit and add profile_id -> flag helper - add async request wrapper and service-worker invocation for profile actions - Implement profile connect/disconnect orchestration: - trigger profile actions on ACL connect - ensure profile disconnect handlers run before ACL teardown - handle unpair flow by removing bond after ACL disconnect - Update A2DP/AVRCP and adapter code to use new APIs: - register profile disconnect handlers and notify profile-connected events - use bt_sal_profile_connect_request / bt_sal_profile_disconnect wrappers - invoke cm callbacks on ACL connect/disconnect paths Signed-off-by: zhongzhijie1 <[email protected]>
1 parent 013be33 commit 7ebe4b6

File tree

5 files changed

+652
-148
lines changed

5 files changed

+652
-148
lines changed

service/stacks/zephyr/include/sal_connection_manager.h

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,35 @@ typedef struct {
2222
uint8_t profile_id;
2323
} cm_data_t;
2424

25+
typedef bt_status_t (*bt_profile_conn_handler_t)(
26+
bt_controller_id_t id, bt_address_t* addr);
27+
28+
typedef struct {
29+
bt_profile_conn_handler_t handler;
30+
uint8_t profile_id;
31+
bt_controller_id_t id;
32+
} bt_profile_conn_handler_node_t;
33+
34+
bt_status_t bt_sal_profile_connect_request(bt_address_t* addr,
35+
uint8_t profile_id, bt_controller_id_t id,
36+
bt_profile_conn_handler_t handler);
37+
bt_status_t bt_sal_profile_disconnect_register(bt_address_t* addr,
38+
uint8_t profile_id, bt_controller_id_t id,
39+
bt_profile_conn_handler_t handler);
40+
bt_status_t bt_sal_profile_disconnect_request(bt_address_t* addr,
41+
uint8_t profile_id, bt_controller_id_t id,
42+
bt_profile_conn_handler_t handler);
43+
bt_status_t bt_sal_remove_bond_internal(bt_controller_id_t id,
44+
bt_address_t* addr);
45+
bt_status_t bt_sal_disconnect_internal(bt_controller_id_t id,
46+
bt_address_t* addr, uint8_t reason);
47+
2548
cm_data_t* cm_data_new(bt_address_t* addr, uint8_t profile_id);
49+
void bt_sal_cm_profile_connected_callback(cm_data_t* data);
2650
void bt_sal_cm_profile_disconnected_callback(cm_data_t* data);
51+
void bt_sal_cm_acl_connected_callback(cm_data_t* data);
2752
void bt_sal_cm_acl_disconnected_callback(cm_data_t* data);
2853

2954
void bt_sal_cm_conn_init(void);
3055
bt_status_t bt_sal_cm_try_disconnect_profiles(bt_address_t* addr, bool is_unpair);
31-
void bt_sal_cm_conn_cleanup(void);
32-
33-
bool bt_sal_a2dp_try_disconnect_a2dp_sink(bt_controller_id_t id, bt_address_t* addr);
34-
bool bt_sal_a2dp_try_disconnect_a2dp_srouce(bt_controller_id_t id, bt_address_t* addr);
35-
bool bt_sal_avrcp_try_disconnect_avrcp_control(bt_controller_id_t id, bt_address_t* addr);
56+
void bt_sal_cm_conn_cleanup(void);

service/stacks/zephyr/sal_a2dp_interface.c

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ static bt_list_t* bt_a2dp_conn = NULL;
8080

8181
static void bt_list_remove_a2dp_info(struct zblue_a2dp_info_t* a2dp_info);
8282

83+
#ifdef CONFIG_BLUETOOTH_A2DP_SOURCE
84+
static bt_status_t a2dp_source_disconnect(bt_controller_id_t id, bt_address_t* addr);
85+
#endif
86+
87+
#ifdef CONFIG_BLUETOOTH_A2DP_SINK
88+
static bt_status_t a2dp_sink_disconnect(bt_controller_id_t id, bt_address_t* addr);
89+
#endif
90+
8391
static void flag_reset(struct zblue_a2dp_info_t* a2dp_info)
8492
{
8593
a2dp_info->state = 0x00;
@@ -901,6 +909,21 @@ static void zblue_on_stream_established(struct bt_a2dp_stream* stream)
901909
}
902910
}
903911

912+
static void bt_sal_a2dp_notify_connected(struct zblue_a2dp_info_t* a2dp_info)
913+
{
914+
if (a2dp_info->role == SEP_SRC) {
915+
#ifdef CONFIG_BLUETOOTH_A2DP_SOURCE
916+
bt_sal_cm_profile_connected_callback(cm_data_new(&a2dp_info->bd_addr, PROFILE_A2DP));
917+
bt_sal_profile_disconnect_register(&a2dp_info->bd_addr, PROFILE_A2DP, PRIMARY_ADAPTER, a2dp_source_disconnect);
918+
#endif
919+
} else { /* SEP_SNK */
920+
#ifdef CONFIG_BLUETOOTH_A2DP_SINK
921+
bt_sal_cm_profile_connected_callback(cm_data_new(&a2dp_info->bd_addr, PROFILE_A2DP_SINK));
922+
bt_sal_profile_disconnect_register(&a2dp_info->bd_addr, PROFILE_A2DP_SINK, PRIMARY_ADAPTER, a2dp_sink_disconnect);
923+
#endif
924+
}
925+
}
926+
904927
static void bt_sal_a2dp_notify_disconnected(struct zblue_a2dp_info_t* a2dp_info)
905928
{
906929
if (a2dp_info->role == SEP_SRC) {
@@ -1184,6 +1207,7 @@ static void zblue_on_connected(struct bt_a2dp* a2dp, int err)
11841207
a2dp_info->selected_peer_endpoint = NULL;
11851208

11861209
bt_list_add_tail(bt_a2dp_conn, a2dp_info);
1210+
bt_sal_a2dp_notify_connected(a2dp_info);
11871211
}
11881212

11891213
static void bt_list_remove_a2dp_info(struct zblue_a2dp_info_t* a2dp_info)
@@ -1558,9 +1582,9 @@ bt_status_t bt_sal_a2dp_sink_init(uint8_t max_connections)
15581582
#endif /* CONFIG_BLUETOOTH_A2DP_SINK */
15591583
}
15601584

1561-
bt_status_t bt_sal_a2dp_source_connect(bt_controller_id_t id, bt_address_t* addr)
1562-
{
15631585
#ifdef CONFIG_BLUETOOTH_A2DP_SOURCE
1586+
static bt_status_t a2dp_source_profile_connect(bt_controller_id_t id, bt_address_t* addr)
1587+
{
15641588
struct bt_conn* conn = bt_conn_lookup_addr_br((bt_addr_t*)addr);
15651589
struct zblue_a2dp_info_t* a2dp_info;
15661590
struct bt_a2dp* a2dp;
@@ -1608,14 +1632,21 @@ bt_status_t bt_sal_a2dp_source_connect(bt_controller_id_t id, bt_address_t* addr
16081632
error:
16091633
bt_conn_unref(conn);
16101634
return BT_STATUS_FAIL;
1635+
}
1636+
#endif /* CONFIG_BLUETOOTH_A2DP_SOURCE */
1637+
1638+
bt_status_t bt_sal_a2dp_source_connect(bt_controller_id_t id, bt_address_t* addr)
1639+
{
1640+
#ifdef CONFIG_BLUETOOTH_A2DP_SOURCE
1641+
return bt_sal_profile_connect_request(addr, PROFILE_A2DP, id, a2dp_source_profile_connect);
16111642
#else
16121643
return BT_STATUS_NOT_SUPPORTED;
16131644
#endif /* CONFIG_BLUETOOTH_A2DP_SOURCE */
16141645
}
16151646

1616-
bt_status_t bt_sal_a2dp_sink_connect(bt_controller_id_t id, bt_address_t* addr)
1617-
{
16181647
#ifdef CONFIG_BLUETOOTH_A2DP_SINK
1648+
static bt_status_t a2dp_sink_profile_connect(bt_controller_id_t id, bt_address_t* addr)
1649+
{
16191650
struct bt_conn* conn = bt_conn_lookup_addr_br((bt_addr_t*)addr);
16201651
struct zblue_a2dp_info_t* a2dp_info;
16211652
struct bt_a2dp* a2dp;
@@ -1663,6 +1694,13 @@ bt_status_t bt_sal_a2dp_sink_connect(bt_controller_id_t id, bt_address_t* addr)
16631694
error:
16641695
bt_conn_unref(conn);
16651696
return BT_STATUS_FAIL;
1697+
}
1698+
#endif /* CONFIG_BLUETOOTH_A2DP_SINK */
1699+
1700+
bt_status_t bt_sal_a2dp_sink_connect(bt_controller_id_t id, bt_address_t* addr)
1701+
{
1702+
#ifdef CONFIG_BLUETOOTH_A2DP_SINK
1703+
return bt_sal_profile_connect_request(addr, PROFILE_A2DP_SINK, id, a2dp_sink_profile_connect);
16661704
#else
16671705
return BT_STATUS_NOT_SUPPORTED;
16681706
#endif /* CONFIG_BLUETOOTH_A2DP_SINK */
@@ -1690,9 +1728,9 @@ static bt_status_t bt_sal_a2dp_disconnect(struct zblue_a2dp_info_t* a2dp_info)
16901728
return BT_STATUS_SUCCESS;
16911729
}
16921730

1693-
bt_status_t bt_sal_a2dp_source_disconnect(bt_controller_id_t id, bt_address_t* addr)
1694-
{
16951731
#ifdef CONFIG_BLUETOOTH_A2DP_SOURCE
1732+
static bt_status_t a2dp_source_disconnect(bt_controller_id_t id, bt_address_t* addr)
1733+
{
16961734
struct zblue_a2dp_info_t* a2dp_info;
16971735

16981736
a2dp_info = (struct zblue_a2dp_info_t*)bt_list_find(bt_a2dp_conn, bt_a2dp_info_find_addr, addr);
@@ -1702,14 +1740,21 @@ bt_status_t bt_sal_a2dp_source_disconnect(bt_controller_id_t id, bt_address_t* a
17021740
}
17031741

17041742
return bt_sal_a2dp_disconnect(a2dp_info);
1743+
}
1744+
#endif /* CONFIG_BLUETOOTH_A2DP_SOURCE */
1745+
1746+
bt_status_t bt_sal_a2dp_source_disconnect(bt_controller_id_t id, bt_address_t* addr)
1747+
{
1748+
#ifdef CONFIG_BLUETOOTH_A2DP_SOURCE
1749+
return bt_sal_profile_disconnect_request(addr, PROFILE_A2DP, id, a2dp_source_disconnect);
17051750
#else
17061751
return BT_STATUS_NOT_SUPPORTED;
17071752
#endif /* CONFIG_BLUETOOTH_A2DP_SOURCE */
17081753
}
17091754

1710-
bt_status_t bt_sal_a2dp_sink_disconnect(bt_controller_id_t id, bt_address_t* addr)
1711-
{
17121755
#ifdef CONFIG_BLUETOOTH_A2DP_SINK
1756+
static bt_status_t a2dp_sink_disconnect(bt_controller_id_t id, bt_address_t* addr)
1757+
{
17131758
struct zblue_a2dp_info_t* a2dp_info;
17141759

17151760
a2dp_info = (struct zblue_a2dp_info_t*)bt_list_find(bt_a2dp_conn, bt_a2dp_info_find_addr, addr);
@@ -1719,51 +1764,18 @@ bt_status_t bt_sal_a2dp_sink_disconnect(bt_controller_id_t id, bt_address_t* add
17191764
}
17201765

17211766
return bt_sal_a2dp_disconnect(a2dp_info);
1722-
#else
1723-
return BT_STATUS_NOT_SUPPORTED;
1724-
#endif /* CONFIG_BLUETOOTH_A2DP_SINK */
17251767
}
1768+
#endif /* CONFIG_BLUETOOTH_A2DP_SINK */
17261769

1727-
bool bt_sal_a2dp_try_disconnect_a2dp_sink(bt_controller_id_t id, bt_address_t* addr)
1770+
bt_status_t bt_sal_a2dp_sink_disconnect(bt_controller_id_t id, bt_address_t* addr)
17281771
{
17291772
#ifdef CONFIG_BLUETOOTH_A2DP_SINK
1730-
struct zblue_a2dp_info_t* a2dp_info;
1731-
1732-
a2dp_info = (struct zblue_a2dp_info_t*)bt_list_find(bt_a2dp_conn, bt_a2dp_info_find_addr, addr);
1733-
if (!a2dp_info) {
1734-
BT_LOGW("%s, a2dp_info is NULL", __func__);
1735-
return false;
1736-
}
1737-
1738-
if (bt_sal_a2dp_disconnect(a2dp_info))
1739-
return false;
1740-
1741-
return true;
1773+
return bt_sal_profile_disconnect_request(addr, PROFILE_A2DP_SINK, id, a2dp_sink_disconnect);
17421774
#else
1743-
return false;
1775+
return BT_STATUS_NOT_SUPPORTED;
17441776
#endif /* CONFIG_BLUETOOTH_A2DP_SINK */
17451777
}
17461778

1747-
bool bt_sal_a2dp_try_disconnect_a2dp_srouce(bt_controller_id_t id, bt_address_t* addr)
1748-
{
1749-
#ifdef CONFIG_BLUETOOTH_A2DP_SOURCE
1750-
struct zblue_a2dp_info_t* a2dp_info;
1751-
1752-
a2dp_info = (struct zblue_a2dp_info_t*)bt_list_find(bt_a2dp_conn, bt_a2dp_info_find_addr, addr);
1753-
if (!a2dp_info) {
1754-
BT_LOGW("%s, a2dp_info is NULL", __func__);
1755-
return false;
1756-
}
1757-
1758-
if (bt_sal_a2dp_disconnect(a2dp_info))
1759-
return false;
1760-
1761-
return true;
1762-
#else
1763-
return false;
1764-
#endif /* CONFIG_BLUETOOTH_A2DP_SOURCE */
1765-
}
1766-
17671779
bt_status_t bt_sal_a2dp_source_start_stream(bt_controller_id_t id, bt_address_t* addr)
17681780
{
17691781
#ifdef CONFIG_BLUETOOTH_A2DP_SOURCE

service/stacks/zephyr/sal_adapter_interface.c

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,12 @@ static void zblue_on_connected(struct bt_conn* conn, uint8_t err)
246246
if (err) {
247247
state.connection_state = CONNECTION_STATE_DISCONNECTED;
248248
state.status = err;
249+
bt_sal_cm_acl_disconnected_callback(cm_data_new(&state.addr, PROFILE_UNKOWN));
249250
goto error;
250251
}
251252

252253
bt_sal_get_remote_name(BT_TRANSPORT_BREDR, &state.addr);
254+
bt_sal_cm_acl_connected_callback(cm_data_new(&state.addr, PROFILE_UNKOWN));
253255

254256
error:
255257
adapter_on_connection_state_changed(&state);
@@ -1278,6 +1280,9 @@ static void STACK_CALL(disconnect)(void* args)
12781280
{
12791281
sal_adapter_req_t* req = args;
12801282
struct bt_conn* conn = bt_conn_lookup_addr_br((bt_addr_t*)&req->addr);
1283+
if (conn == NULL) {
1284+
return;
1285+
}
12811286

12821287
SAL_CHECK(bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN), 0);
12831288
bt_conn_unref(conn);
@@ -1286,6 +1291,32 @@ static void STACK_CALL(disconnect)(void* args)
12861291

12871292
bt_status_t bt_sal_disconnect(bt_controller_id_t id, bt_address_t* addr, uint8_t reason)
12881293
{
1294+
#ifdef CONFIG_BLUETOOTH_BREDR_SUPPORT
1295+
UNUSED(id);
1296+
sal_adapter_req_t* req;
1297+
bt_status_t status;
1298+
1299+
/* disconnect profile, then disconnect acl */
1300+
status = bt_sal_cm_try_disconnect_profiles(addr, false);
1301+
1302+
if (status == BT_STATUS_SUCCESS) {
1303+
return status;
1304+
}
1305+
1306+
req = sal_adapter_req(id, addr, STACK_CALL(disconnect));
1307+
if (!req)
1308+
return BT_STATUS_NOMEM;
1309+
1310+
req->adpt.reason = reason;
1311+
1312+
return sal_send_req(req);
1313+
#else
1314+
return BT_STATUS_NOT_SUPPORTED;
1315+
#endif
1316+
}
1317+
1318+
bt_status_t bt_sal_disconnect_internal(bt_controller_id_t id, bt_address_t* addr, uint8_t reason)
1319+
{
12891320
#ifdef CONFIG_BLUETOOTH_BREDR_SUPPORT
12901321
UNUSED(id);
12911322
sal_adapter_req_t* req;
@@ -1394,13 +1425,7 @@ bt_status_t bt_sal_cancel_bond(bt_controller_id_t id, bt_address_t* addr, bt_tra
13941425
#ifdef CONFIG_BLUETOOTH_BREDR_SUPPORT
13951426
static void STACK_CALL(remove_bond)(void* args)
13961427
{
1397-
bt_status_t status;
13981428
sal_adapter_req_t* req = args;
1399-
1400-
status = bt_sal_cm_try_disconnect_profiles(&req->addr, true);
1401-
if (status == BT_STATUS_SUCCESS)
1402-
return;
1403-
14041429
SAL_CHECK(bt_br_unpair((bt_addr_t*)&req->addr), 0);
14051430
}
14061431
#endif
@@ -1410,6 +1435,13 @@ bt_status_t bt_sal_remove_bond(bt_controller_id_t id, bt_address_t* addr, bt_tra
14101435
#ifdef CONFIG_BLUETOOTH_BREDR_SUPPORT
14111436
UNUSED(id);
14121437
sal_adapter_req_t* req;
1438+
bt_status_t status;
1439+
1440+
status = bt_sal_cm_try_disconnect_profiles(addr, true);
1441+
1442+
if (status == BT_STATUS_SUCCESS) {
1443+
return status;
1444+
}
14131445

14141446
req = sal_adapter_req(id, addr, STACK_CALL(remove_bond));
14151447
if (!req)
@@ -1423,6 +1455,22 @@ bt_status_t bt_sal_remove_bond(bt_controller_id_t id, bt_address_t* addr, bt_tra
14231455
#endif
14241456
}
14251457

1458+
bt_status_t bt_sal_remove_bond_internal(bt_controller_id_t id, bt_address_t* addr)
1459+
{
1460+
#ifdef CONFIG_BLUETOOTH_BREDR_SUPPORT
1461+
UNUSED(id);
1462+
sal_adapter_req_t* req;
1463+
1464+
req = sal_adapter_req(id, addr, STACK_CALL(remove_bond));
1465+
if (!req)
1466+
return BT_STATUS_NOMEM;
1467+
1468+
return sal_send_req(req);
1469+
#else
1470+
return BT_STATUS_NOT_SUPPORTED;
1471+
#endif
1472+
}
1473+
14261474
bt_status_t bt_sal_set_remote_oob_data(bt_controller_id_t id, bt_address_t* addr,
14271475
bt_oob_data_t* p192_val, bt_oob_data_t* p256_val)
14281476
{

0 commit comments

Comments
 (0)