Bluetooth: tester: Add BTP event encryption change

Report encryption change event when callback `le_security_changed` is
triggered.

Signed-off-by: Lyle Zhu <lyle.zhu@nxp.com>
This commit is contained in:
Lyle Zhu 2025-03-17 10:56:14 +08:00 committed by Benjamin Cabé
commit 065762cd2d
2 changed files with 26 additions and 13 deletions

View file

@ -471,6 +471,13 @@ struct btp_gap_ev_periodic_transfer_received_ev {
uint8_t data[]; uint8_t data[];
} __packed; } __packed;
#define BTP_GAP_EV_ENCRYPTION_CHANGE 0x91
struct btp_gap_encryption_change_ev {
bt_addr_le_t address;
uint8_t enabled;
uint8_t key_size;
} __packed;
#if defined(CONFIG_BT_EXT_ADV) #if defined(CONFIG_BT_EXT_ADV)
struct bt_le_per_adv_param; struct bt_le_per_adv_param;
struct bt_le_per_adv_sync_param; struct bt_le_per_adv_sync_param;

View file

@ -223,27 +223,34 @@ static void le_security_changed(struct bt_conn *conn, bt_security_t level,
{ {
struct btp_gap_sec_level_changed_ev sec_ev; struct btp_gap_sec_level_changed_ev sec_ev;
struct btp_gap_bond_lost_ev bond_ev; struct btp_gap_bond_lost_ev bond_ev;
struct btp_gap_encryption_change_ev enc_ev;
struct bt_conn_info info; struct bt_conn_info info;
if (bt_conn_is_type(conn, BT_CONN_TYPE_LE)) { if (bt_conn_get_info(conn, &info) != 0) {
const bt_addr_le_t *addr; LOG_WRN("Failed to get connection info of %p", conn);
return;
}
addr = bt_conn_get_dst(conn); if (info.type == BT_CONN_TYPE_LE) {
bt_addr_le_copy(&sec_ev.address, addr); bt_addr_le_copy(&sec_ev.address, info.le.dst);
bt_addr_le_copy(&bond_ev.address, addr); bt_addr_le_copy(&bond_ev.address, info.le.dst);
} else if (IS_ENABLED(CONFIG_BT_CLASSIC) && bt_conn_is_type(conn, BT_CONN_TYPE_BR)) { bt_addr_le_copy(&enc_ev.address, info.le.dst);
const bt_addr_t *br_addr; } else if (IS_ENABLED(CONFIG_BT_CLASSIC) && (info.type == BT_CONN_TYPE_BR)) {
br_addr = bt_conn_get_dst_br(conn);
sec_ev.address.type = BTP_BR_ADDRESS_TYPE; sec_ev.address.type = BTP_BR_ADDRESS_TYPE;
bt_addr_copy(&sec_ev.address.a, br_addr); bt_addr_copy(&sec_ev.address.a, info.br.dst);
bond_ev.address.type = BTP_BR_ADDRESS_TYPE; bond_ev.address.type = BTP_BR_ADDRESS_TYPE;
bt_addr_copy(&bond_ev.address.a, br_addr); bt_addr_copy(&bond_ev.address.a, info.br.dst);
enc_ev.address.type = BTP_BR_ADDRESS_TYPE;
bt_addr_copy(&enc_ev.address.a, info.br.dst);
} else { } else {
LOG_WRN("Unsupported transport"); LOG_WRN("Unsupported transport");
return; return;
} }
enc_ev.enabled = (err == BT_SECURITY_ERR_SUCCESS) ? true : false;
enc_ev.key_size = info.security.enc_key_size;
tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_ENCRYPTION_CHANGE, &enc_ev, sizeof(enc_ev));
switch (err) { switch (err) {
case BT_SECURITY_ERR_SUCCESS: case BT_SECURITY_ERR_SUCCESS:
/* enum matches BTP values */ /* enum matches BTP values */
@ -258,8 +265,7 @@ static void le_security_changed(struct bt_conn *conn, bt_security_t level,
* *
* This means bond is lost and we restart pairing to re-bond * This means bond is lost and we restart pairing to re-bond
*/ */
if (bt_conn_get_info(conn, &info) == 0 && if (info.role == BT_CONN_ROLE_CENTRAL) {
info.role == BT_CONN_ROLE_CENTRAL) {
LOG_DBG("Bond lost"); LOG_DBG("Bond lost");
tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_BOND_LOST, tester_event(BTP_SERVICE_ID_GAP, BTP_GAP_EV_BOND_LOST,