|
6 | 6 |
|
7 | 7 | /*
|
8 | 8 | * Copyright (c) 2018 Intel Corporation
|
| 9 | + * Copyright 2025 NXP |
9 | 10 | *
|
10 | 11 | * SPDX-License-Identifier: Apache-2.0
|
11 | 12 | */
|
@@ -989,6 +990,78 @@ static int cmd_clear(const struct shell *sh, size_t argc, char *argv[])
|
989 | 990 | return err;
|
990 | 991 | }
|
991 | 992 |
|
| 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 | + |
992 | 1065 | static int cmd_default_handler(const struct shell *sh, size_t argc, char **argv)
|
993 | 1066 | {
|
994 | 1067 | if (argc == 1) {
|
@@ -1041,6 +1114,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(br_cmds,
|
1041 | 1114 | SHELL_CMD_ARG(oob, NULL, NULL, cmd_oob, 1, 0),
|
1042 | 1115 | SHELL_CMD_ARG(pscan, NULL, "<value: on, off>", cmd_connectable, 2, 0),
|
1043 | 1116 | 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), |
1044 | 1118 | SHELL_SUBCMD_SET_END
|
1045 | 1119 | );
|
1046 | 1120 |
|
|
0 commit comments