Skip to content

Feature/add role change cb #90126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MarkWangChinese
Copy link
Collaborator

add role_changed to struct bt_conn_cb to notify the HCI_Role_Change event to application.
add BT_HCI_OP_SWITCH_ROLE and struct bt_hci_cp_switch_role.
add br switch-role shell cmd and test the 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]>
@MarkWangChinese MarkWangChinese force-pushed the feature/add_role_change_cb branch from 43ed4cc to 2009ed9 Compare May 19, 2025 07:18
@Thalley Thalley removed their request for review May 19, 2025 07:32

if (!strcmp(action, "central")) {
/* 0 - Change own Role to Central for this BD_ADDR. */
role = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not readable. I suggest defining two marcos.

#define BT_HCI_SWITCH_ROLE_CENTRAL    0
#define BT_HCI_SWITCH_ROLE_PERIPHERAL 1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Comment on lines 1045 to 1047
err = bt_conn_get_info(default_conn, &info);
if (err) {
shell_error(sh, "Failed to get info (err %d)", err);
return err;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a function bt_conn_get_dst_br() can be used to get the peer address.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

if (buf != NULL) {
cp = net_buf_add(buf, sizeof(*cp));
memcpy(&cp->bdaddr, &info.br.dst->val[0], sizeof(cp->bdaddr));
cp->role = role;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the new role is the same as the old one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my test, the controller gives error for the result.

Comment on lines 1051 to 1060
buf = bt_hci_cmd_create(BT_HCI_OP_SWITCH_ROLE, sizeof(*cp));
if (buf != NULL) {
cp = net_buf_add(buf, sizeof(*cp));
memcpy(&cp->bdaddr, &info.br.dst->val[0], sizeof(cp->bdaddr));
cp->role = role;
err = bt_hci_cmd_send_sync(BT_HCI_OP_SWITCH_ROLE, buf, NULL);
if (err) {
shell_print(sh, "fail to send hci cmd (err %d)", err);
}
} else {
shell_error(sh, "hi cmd fail to create");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If can we add an API for BR role switch function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to add an API. I think another pr for the API is better because the current pr is mainly for adding new callback.

@@ -1041,6 +1116,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(br_cmds,
SHELL_CMD_ARG(oob, NULL, NULL, cmd_oob, 1, 0),
SHELL_CMD_ARG(pscan, NULL, "<value: on, off>", cmd_connectable, 2, 0),
SHELL_CMD_ARG(sdp-find, NULL, "<HFPAG, HFPHF>", cmd_sdp_find_record, 2, 0),
SHELL_CMD_ARG(switch-role, NULL, "<central, peripheral", cmd_switch_role, 2, 0),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> is missing in <central, peripheral.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -478,6 +478,10 @@ void bt_conn_set_state(struct bt_conn *conn, bt_conn_state_t state);

void bt_conn_connected(struct bt_conn *conn);

#if defined(CONFIG_BT_CLASSIC)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As my understand, function declarations do not need to be controlled by conditional compilation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add BT_HCI_OP_SWITCH_ROLE and struct bt_hci_cp_switch_role

Signed-off-by: Mark Wang <[email protected]>
add br switch-role shell cmd and test the role_changed callback.

Signed-off-by: Mark Wang <[email protected]>
(cherry picked from commit e8eef61b1203dc14a45baeac34163df3b1f0573d)
@MarkWangChinese MarkWangChinese force-pushed the feature/add_role_change_cb branch from 2009ed9 to 7c6719d Compare May 19, 2025 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Bluetooth Classic Bluetooth Classic (BR/EDR) area: Bluetooth Host Bluetooth Host (excluding BR/EDR) area: Bluetooth
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants