Skip to content

Commit 009cc9b

Browse files
MarkWangChineserlubos
authored andcommitted
[nrf fromtree] 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]> (cherry picked from commit 31fba83) Signed-off-by: Sean Madigan <[email protected]>
1 parent 534fd57 commit 009cc9b

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
@@ -2033,6 +2033,17 @@ struct bt_conn_cb {
20332033

20342034
#endif
20352035

2036+
#if defined(CONFIG_BT_CLASSIC)
2037+
/** @brief The role of the connection has changed.
2038+
*
2039+
* This callback notifies the application that the role switch procedure has completed.
2040+
*
2041+
* @param conn Connection object.
2042+
* @param status HCI status of role change event.
2043+
*/
2044+
void (*role_changed)(struct bt_conn *conn, uint8_t status);
2045+
#endif
2046+
20362047
/** @internal Internally used field for list handling */
20372048
sys_snode_t _node;
20382049
};

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 == 0) {
691+
if (evt->role == BT_HCI_ROLE_PERIPHERAL) {
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
@@ -1826,6 +1826,25 @@ void bt_conn_connected(struct bt_conn *conn)
18261826
notify_connected(conn);
18271827
}
18281828

1829+
#if defined(CONFIG_BT_CLASSIC)
1830+
void bt_conn_role_changed(struct bt_conn *conn, uint8_t status)
1831+
{
1832+
struct bt_conn_cb *callback;
1833+
1834+
SYS_SLIST_FOR_EACH_CONTAINER(&conn_cbs, callback, _node) {
1835+
if (callback->role_changed) {
1836+
callback->role_changed(conn, status);
1837+
}
1838+
}
1839+
1840+
STRUCT_SECTION_FOREACH(bt_conn_cb, cb) {
1841+
if (cb->role_changed) {
1842+
cb->role_changed(conn, status);
1843+
}
1844+
}
1845+
}
1846+
#endif
1847+
18291848
static int conn_disconnect(struct bt_conn *conn, uint8_t reason)
18301849
{
18311850
int err;

subsys/bluetooth/host/conn_internal.h

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

491491
void bt_conn_connected(struct bt_conn *conn);
492492

493+
void bt_conn_role_changed(struct bt_conn *conn, uint8_t status);
494+
493495
int bt_conn_le_conn_update(struct bt_conn *conn,
494496
const struct bt_le_conn_param *param);
495497

0 commit comments

Comments
 (0)