Skip to content

Commit 7c6719d

Browse files
bluetooth: shell: add role_changed test
add br switch-role shell cmd and test the role_changed callback. Signed-off-by: Mark Wang <[email protected]> (cherry picked from commit e8eef61b1203dc14a45baeac34163df3b1f0573d)
1 parent 6e78c6e commit 7c6719d

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

subsys/bluetooth/host/classic/shell/bredr.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/*
88
* Copyright (c) 2018 Intel Corporation
9+
* Copyright 2025 NXP
910
*
1011
* SPDX-License-Identifier: Apache-2.0
1112
*/
@@ -989,6 +990,78 @@ static int cmd_clear(const struct shell *sh, size_t argc, char *argv[])
989990
return err;
990991
}
991992

993+
static const char *get_conn_role_str(uint8_t role)
994+
{
995+
switch (role) {
996+
case BT_CONN_ROLE_CENTRAL: return "central";
997+
case BT_CONN_ROLE_PERIPHERAL: return "peripheral";
998+
default: return "Invalid";
999+
}
1000+
}
1001+
1002+
void role_changed(struct bt_conn *conn, uint8_t status)
1003+
{
1004+
struct bt_conn_info info;
1005+
int err;
1006+
1007+
bt_shell_print("Role changed (HCI status 0x%02x)", status);
1008+
1009+
err = bt_conn_get_info(conn, &info);
1010+
if (err) {
1011+
bt_shell_print("Failed to get info");
1012+
return;
1013+
}
1014+
1015+
bt_shell_print("Role: %s", get_conn_role_str(info.role));
1016+
}
1017+
1018+
static int cmd_switch_role(const struct shell *sh, size_t argc, char *argv[])
1019+
{
1020+
struct net_buf *buf = NULL;
1021+
struct bt_hci_cp_switch_role *cp;
1022+
const bt_addr_t *dest;
1023+
const char *action;
1024+
int err;
1025+
uint8_t role;
1026+
1027+
if (!default_conn) {
1028+
shell_print(sh, "Not connected");
1029+
return -ENOEXEC;
1030+
}
1031+
1032+
action = argv[1];
1033+
1034+
if (!strcmp(action, "central")) {
1035+
role = BT_HCI_SWITCH_ROLE_CENTRAL;
1036+
} else if (!strcmp(action, "peripheral")) {
1037+
role = BT_HCI_SWITCH_ROLE_PERIPHERAL;
1038+
} else {
1039+
shell_help(sh);
1040+
return SHELL_CMD_HELP_PRINTED;
1041+
}
1042+
1043+
dest = bt_conn_get_dst_br(default_conn);
1044+
if (!dest) {
1045+
shell_error(sh, "Failed to get br addr");
1046+
return -EINVAL;
1047+
}
1048+
1049+
buf = bt_hci_cmd_create(BT_HCI_OP_SWITCH_ROLE, sizeof(*cp));
1050+
if (buf != NULL) {
1051+
cp = net_buf_add(buf, sizeof(*cp));
1052+
memcpy(&cp->bdaddr, &dest->val[0], sizeof(cp->bdaddr));
1053+
cp->role = role;
1054+
err = bt_hci_cmd_send_sync(BT_HCI_OP_SWITCH_ROLE, buf, NULL);
1055+
if (err) {
1056+
shell_print(sh, "fail to send hci cmd (err %d)", err);
1057+
}
1058+
} else {
1059+
shell_error(sh, "hi cmd fail to create");
1060+
}
1061+
1062+
return 0;
1063+
}
1064+
9921065
static int cmd_default_handler(const struct shell *sh, size_t argc, char **argv)
9931066
{
9941067
if (argc == 1) {
@@ -1041,6 +1114,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(br_cmds,
10411114
SHELL_CMD_ARG(oob, NULL, NULL, cmd_oob, 1, 0),
10421115
SHELL_CMD_ARG(pscan, NULL, "<value: on, off>", cmd_connectable, 2, 0),
10431116
SHELL_CMD_ARG(sdp-find, NULL, "<HFPAG, HFPHF>", cmd_sdp_find_record, 2, 0),
1117+
SHELL_CMD_ARG(switch-role, NULL, "<central, peripheral>", cmd_switch_role, 2, 0),
10441118
SHELL_SUBCMD_SET_END
10451119
);
10461120

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/** @file
2+
* @brief Bluetooth shell functions
3+
*
4+
* This is not to be included by the application.
5+
*/
6+
7+
/*
8+
* Copyright 2025 NXP
9+
*
10+
* SPDX-License-Identifier: Apache-2.0
11+
*/
12+
13+
#ifndef __BREDR_H
14+
#define __BREDR_H
15+
#include <stddef.h>
16+
#include <stdint.h>
17+
18+
void role_changed(struct bt_conn *conn, uint8_t status);
19+
20+
#endif /* __BREDR_H */

subsys/bluetooth/host/shell/bt.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
#endif /* CONFIG_BT_LL_SW_SPLIT */
5555
#include "host/shell/bt.h"
5656
#include "mesh/shell/hci.h"
57+
#if defined(CONFIG_BT_CLASSIC)
58+
#include "host/classic/shell/bredr.h"
59+
#endif
5760

5861
static bool no_settings_load;
5962

@@ -1182,6 +1185,9 @@ static struct bt_conn_cb conn_callbacks = {
11821185
.le_cs_config_complete = le_cs_config_created,
11831186
.le_cs_config_removed = le_cs_config_removed,
11841187
#endif
1188+
#if defined(CONFIG_BT_CLASSIC)
1189+
.role_changed = role_changed,
1190+
#endif
11851191
};
11861192
#endif /* CONFIG_BT_CONN */
11871193

0 commit comments

Comments
 (0)