Skip to content

Commit 43ed4cc

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 fa67a55 commit 43ed4cc

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

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

Lines changed: 79 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,83 @@ 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+
struct bt_conn_info info;
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+
/* 0 - Change own Role to Central for this BD_ADDR. */
1036+
role = 0;
1037+
} else if (!strcmp(action, "peripheral")) {
1038+
/* 1 - Change own Role to Peripheral for this BD_ADDR. */
1039+
role = 1;
1040+
} else {
1041+
shell_help(sh);
1042+
return SHELL_CMD_HELP_PRINTED;
1043+
}
1044+
1045+
err = bt_conn_get_info(default_conn, &info);
1046+
if (err) {
1047+
shell_error(sh, "Failed to get info (err %d)", err);
1048+
return err;
1049+
}
1050+
1051+
buf = bt_hci_cmd_create(BT_HCI_OP_SWITCH_ROLE, sizeof(*cp));
1052+
if (buf != NULL)
1053+
{
1054+
cp = net_buf_add(buf, sizeof(*cp));
1055+
memcpy(&cp->bdaddr, &info.br.dst->val[0], sizeof(cp->bdaddr));
1056+
cp->role = role;
1057+
err = bt_hci_cmd_send_sync(BT_HCI_OP_SWITCH_ROLE, buf, NULL);
1058+
if (err) {
1059+
shell_print(sh, "fail to send hci cmd (err %d)", err);
1060+
}
1061+
}
1062+
else
1063+
{
1064+
shell_error(sh, "hi cmd fail to create");
1065+
}
1066+
1067+
return 0;
1068+
}
1069+
9921070
static int cmd_default_handler(const struct shell *sh, size_t argc, char **argv)
9931071
{
9941072
if (argc == 1) {
@@ -1041,6 +1119,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(br_cmds,
10411119
SHELL_CMD_ARG(oob, NULL, NULL, cmd_oob, 1, 0),
10421120
SHELL_CMD_ARG(pscan, NULL, "<value: on, off>", cmd_connectable, 2, 0),
10431121
SHELL_CMD_ARG(sdp-find, NULL, "<HFPAG, HFPHF>", cmd_sdp_find_record, 2, 0),
1122+
SHELL_CMD_ARG(switch-role, NULL, "<central, peripheral", cmd_switch_role, 2, 0),
10441123
SHELL_SUBCMD_SET_END
10451124
);
10461125

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)