Skip to content

Commit 5fba281

Browse files
bluetooth: classic: add role changed callback
add `role_changed` to `struct bt_conn_cb` to notify the HCI_Role_Change event to application. Signed-off-by: Mark Wang <[email protected]>
1 parent 8ab712a commit 5fba281

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

include/zephyr/bluetooth/conn.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,17 @@ struct bt_conn_cb {
19751975

19761976
#endif
19771977

1978+
#if defined(CONFIG_BT_CLASSIC)
1979+
/** @brief The role of the connection has changed.
1980+
*
1981+
* This callback notifies the application that the role switch procedure has completed.
1982+
*
1983+
* @param conn Connection object.
1984+
* @param status HCI status of role change event.
1985+
*/
1986+
void (*role_changed)(struct bt_conn *conn, uint8_t status);
1987+
#endif
1988+
19781989
/** @internal Internally used field for list handling */
19791990
sys_snode_t _node;
19801991
};

subsys/bluetooth/host/classic/br.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -681,22 +681,22 @@ void bt_hci_role_change(struct net_buf *buf)
681681

682682
LOG_DBG("status 0x%02x role %u addr %s", evt->status, evt->role, bt_addr_str(&evt->bdaddr));
683683

684-
if (evt->status) {
685-
return;
686-
}
687-
688684
conn = bt_conn_lookup_addr_br(&evt->bdaddr);
689685
if (!conn) {
690686
LOG_ERR("Can't find conn for %s", bt_addr_str(&evt->bdaddr));
691687
return;
692688
}
693689

694-
if (evt->role) {
695-
conn->role = BT_CONN_ROLE_PERIPHERAL;
696-
} else {
697-
conn->role = BT_CONN_ROLE_CENTRAL;
690+
if (!evt->status) {
691+
if (evt->role) {
692+
conn->role = BT_CONN_ROLE_PERIPHERAL;
693+
} else {
694+
conn->role = BT_CONN_ROLE_CENTRAL;
695+
}
698696
}
699697

698+
bt_conn_role_changed(conn, evt->status);
699+
700700
bt_conn_unref(conn);
701701
}
702702

subsys/bluetooth/host/conn.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,25 @@ void bt_conn_connected(struct bt_conn *conn)
18441844
notify_connected(conn);
18451845
}
18461846

1847+
#if defined(CONFIG_BT_CLASSIC)
1848+
void bt_conn_role_changed(struct bt_conn *conn, uint8_t status)
1849+
{
1850+
struct bt_conn_cb *callback;
1851+
1852+
SYS_SLIST_FOR_EACH_CONTAINER(&conn_cbs, callback, _node) {
1853+
if (callback->role_changed) {
1854+
callback->role_changed(conn, status);
1855+
}
1856+
}
1857+
1858+
STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
1859+
if (cb->role_changed) {
1860+
cb->role_changed(conn, status);
1861+
}
1862+
}
1863+
}
1864+
#endif
1865+
18471866
static int conn_disconnect(struct bt_conn *conn, uint8_t reason)
18481867
{
18491868
int err;

subsys/bluetooth/host/conn_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state);
478478

479479
void bt_conn_connected(struct bt_conn *conn);
480480

481+
void bt_conn_role_changed(struct bt_conn *conn, uint8_t status);
482+
481483
int bt_conn_le_conn_update(struct bt_conn *conn,
482484
const struct bt_le_conn_param *param);
483485

0 commit comments

Comments
 (0)