From 3ae417f6870fb5272868ea7f5ed9f1879a99db2c Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Wed, 26 Feb 2020 14:24:10 +0100 Subject: [PATCH] Wire Security UI callbacks Bug: 147086339 Change-Id: I719459314dd33d922a575126b74ee133f8ac2289 --- btif/src/btif_dm.cc | 34 +++++++++++++--------- main/shim/btif_dm.cc | 81 +++++++++++++++++++++++++++++----------------------- main/shim/btif_dm.h | 7 ++--- main/shim/btm.cc | 11 +------ main/shim/btm.h | 4 --- main/shim/btm_api.cc | 1 - main/shim/helpers.h | 7 +++-- 7 files changed, 75 insertions(+), 70 deletions(-) diff --git a/btif/src/btif_dm.cc b/btif/src/btif_dm.cc index d53847322..52cb294b6 100644 --- a/btif/src/btif_dm.cc +++ b/btif/src/btif_dm.cc @@ -293,19 +293,16 @@ static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC* dm_sec) { void btif_dm_init(uid_set_t* set) { uid_set = set; if (bluetooth::shim::is_gd_shim_enabled()) { - bluetooth::shim::BTIF_DM_SetUiCallback( - [](RawAddress* bt_addr, bt_bdname_t* bd_name, uint32_t cod, - bt_ssp_variant_t pairing_variant, uint32_t pass_key) { - LOG(ERROR) << __func__ << ": UI Callback fired!"; - // TODO(optedoblivion): Wire up HAL_CBACK - // HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &bd_addr, &bd_name, - // cod, - // (p_ssp_cfm_req->just_works ? BT_SSP_VARIANT_CONSENT - // : - // BT_SSP_VARIANT_PASSKEY_CONFIRMATION), - // p_ssp_cfm_req->num_val); - // - }); + bluetooth::shim::BTIF_DM_SetUiCallback([](RawAddress address, bt_bdname_t bd_name, uint32_t cod, bt_ssp_variant_t pairing_variant, uint32_t pass_key) { + do_in_jni_thread(FROM_HERE, base::BindOnce([](RawAddress address, bt_bdname_t bd_name, uint32_t cod, bt_ssp_variant_t pairing_variant, uint32_t pass_key) { + LOG(ERROR) << __func__ << ": UI Callback fired!"; + + //TODO: java BondStateMachine requires change into bonding state. If we ever send this event separately, consider removing this line + HAL_CBACK(bt_hal_cbacks, bond_state_changed_cb, BT_STATUS_SUCCESS, &address, BT_BOND_STATE_BONDING); + + HAL_CBACK(bt_hal_cbacks, ssp_request_cb, &address, &bd_name, cod, pairing_variant, pass_key); + }, address, bd_name, cod, pairing_variant, pass_key)); + }); } } @@ -2424,6 +2421,17 @@ bt_status_t btif_dm_pin_reply(const RawAddress* bd_addr, uint8_t accept, bt_status_t btif_dm_ssp_reply(const RawAddress* bd_addr, bt_ssp_variant_t variant, uint8_t accept, UNUSED_ATTR uint32_t passkey) { + + if (bluetooth::shim::is_gd_shim_enabled()) { + uint8_t tmp_dev_type = 0; + uint8_t tmp_addr_type = 0; + BTM_ReadDevInfo(*bd_addr, &tmp_dev_type, &tmp_addr_type); + + do_in_main_thread( + FROM_HERE, + base::Bind(&bluetooth::shim::BTIF_DM_ssp_reply, *bd_addr, tmp_addr_type, variant, accept)); + } + if (variant == BT_SSP_VARIANT_PASSKEY_ENTRY) { /* This is not implemented in the stack. * For devices with display, this is not needed diff --git a/main/shim/btif_dm.cc b/main/shim/btif_dm.cc index 2a32e4679..a625c4e53 100644 --- a/main/shim/btif_dm.cc +++ b/main/shim/btif_dm.cc @@ -15,10 +15,11 @@ */ #define LOG_TAG "bt_shim_btif_dm" -#include "main/shim/btif_dm.h" +#include "osi/include/log.h" +#include "main/shim/btif_dm.h" #include "main/shim/entry.h" -#include "osi/include/log.h" +#include "main/shim/helpers.h" #include "security/security_module.h" #include "security/ui.h" @@ -32,46 +33,44 @@ class ShimUi : public security::UI { ~ShimUi() {} void DisplayPairingPrompt(const bluetooth::hci::AddressWithType& address, std::string name) { - LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__); + LOG_WARN(LOG_TAG, "%s ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ TODO Unimplemented", __func__); } void Cancel(const bluetooth::hci::AddressWithType& address) { - LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__); + LOG_WARN(LOG_TAG, "%s ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ TODO Unimplemented", __func__); } + void DisplayConfirmValue(const bluetooth::hci::AddressWithType& address, std::string name, uint32_t numeric_value) { - LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__); - // TODO(optedoblivion): Remove and wire up to UI callback - auto security_manager = - bluetooth::shim::GetSecurityModule()->GetSecurityManager(); - security_manager->OnConfirmYesNo(address, true); - // callback(address, name, 0, 0, 0); + bt_bdname_t legacy_name{0}; + memcpy(legacy_name.name, name.data(), name.length()); + callback_(RawAddress(address.GetAddress().address), legacy_name, ((0x1F) << 8) /* COD_UNCLASSIFIED*/ , BT_SSP_VARIANT_PASSKEY_CONFIRMATION, numeric_value); } + void DisplayYesNoDialog(const bluetooth::hci::AddressWithType& address, std::string name) { - LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__); - // TODO(optedoblivion): Remove and wire up to UI callback - auto security_manager = - bluetooth::shim::GetSecurityModule()->GetSecurityManager(); - security_manager->OnConfirmYesNo(address, true); - // callback(address, name, 0, 0, 0); + bt_bdname_t legacy_name{0}; + memcpy(legacy_name.name, name.data(), name.length()); + callback_(RawAddress(address.GetAddress().address), legacy_name, ((0x1F) << 8) /* COD_UNCLASSIFIED*/ , BT_SSP_VARIANT_CONSENT, 0); } - void DisplayEnterPasskeyDialog(const bluetooth::hci::AddressWithType& address, - std::string name) { - LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__); + + void DisplayEnterPasskeyDialog(const bluetooth::hci::AddressWithType& address, std::string name) { + bt_bdname_t legacy_name{0}; + memcpy(legacy_name.name, name.data(), name.length()); + callback_(RawAddress(address.GetAddress().address), legacy_name, ((0x1F) << 8) /* COD_UNCLASSIFIED*/ , BT_SSP_VARIANT_PASSKEY_ENTRY, 0); } - void DisplayPasskey(const bluetooth::hci::AddressWithType& address, - std::string name, uint32_t passkey) { - LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__); + + void DisplayPasskey(const bluetooth::hci::AddressWithType& address, std::string name, uint32_t passkey) { + bt_bdname_t legacy_name{0}; + memcpy(legacy_name.name, name.data(), name.length()); + callback_(RawAddress(address.GetAddress().address), legacy_name, ((0x1F) << 8) /* COD_UNCLASSIFIED*/ , BT_SSP_VARIANT_PASSKEY_NOTIFICATION, passkey); } - void SetLegacyCallback(std::function - callback) { + void SetLegacyCallback(std::function callback) { callback_ = callback; } private: - std::function callback_; }; @@ -81,16 +80,28 @@ ShimUi ui; /** * Sets handler to SecurityModule and provides callback to handler */ -void BTIF_DM_SetUiCallback( - std::function - callback) { - LOG_WARN(LOG_TAG, "%s called", __func__); - auto security_manager = - bluetooth::shim::GetSecurityModule()->GetSecurityManager(); +void BTIF_DM_SetUiCallback(std::function callback) { + LOG_WARN(LOG_TAG, "%s", __func__); + auto security_manager = bluetooth::shim::GetSecurityModule()->GetSecurityManager(); ui.SetLegacyCallback(callback); - security_manager->SetUserInterfaceHandler( - &ui, bluetooth::shim::GetGdShimHandler()); + security_manager->SetUserInterfaceHandler(&ui, bluetooth::shim::GetGdShimHandler()); +} + +void BTIF_DM_ssp_reply(const RawAddress bd_addr, uint8_t addr_type, bt_ssp_variant_t variant, uint8_t accept) { + LOG_WARN(LOG_TAG, "%s", __func__); + + hci::AddressWithType address = ToAddressWithType(bd_addr, addr_type); + + auto security_manager = bluetooth::shim::GetSecurityModule()->GetSecurityManager(); + + if (variant == BT_SSP_VARIANT_PASSKEY_CONFIRMATION || variant == BT_SSP_VARIANT_CONSENT) { + security_manager->OnConfirmYesNo(address, accept); + } else { + //TODO: + // void OnPairingPromptAccepted(const bluetooth::hci::AddressWithType& address, bool confirmed) override; + // void OnPasskeyEntry(const bluetooth::hci::AddressWithType& address, uint32_t passkey) override; + LOG_WARN(LOG_TAG, "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ Variant not implemented yet %02x", variant); + } } } // namespace shim diff --git a/main/shim/btif_dm.h b/main/shim/btif_dm.h index 650d6ec6e..c1305fd9c 100644 --- a/main/shim/btif_dm.h +++ b/main/shim/btif_dm.h @@ -28,10 +28,7 @@ namespace shim { /** * Sets handler to SecurityModule and provides callback to handler */ -void BTIF_DM_SetUiCallback( - std::function - callback); - +void BTIF_DM_SetUiCallback(std::function callback); +void BTIF_DM_ssp_reply(const RawAddress bd_addr, uint8_t, bt_ssp_variant_t variant, uint8_t accept); } // namespace shim } // namespace bluetooth diff --git a/main/shim/btm.cc b/main/shim/btm.cc index efda7f5b0..0c4969a16 100644 --- a/main/shim/btm.cc +++ b/main/shim/btm.cc @@ -829,13 +829,4 @@ bool bluetooth::shim::Btm::RemoveBond(const RawAddress& bd_addr) { bluetooth::shim::GetSecurityModule()->GetSecurityManager(); security_manager->RemoveBond(ToAddressWithType(bd_addr, BLE_ADDR_PUBLIC)); return true; -} - -void bluetooth::shim::Btm::SetSimplePairingCallback( - tBTM_SP_CALLBACK* callback) { - auto security_manager = - bluetooth::shim::GetSecurityModule()->GetSecurityManager(); - // TODO(optedoblivion): security_manager->RegisterCallback(new - // ExtendedCallback(simple_pairing_callback_)) - simple_pairing_callback_ = callback; -} +} \ No newline at end of file diff --git a/main/shim/btm.h b/main/shim/btm.h index 15e0ad445..ef967361a 100644 --- a/main/shim/btm.h +++ b/main/shim/btm.h @@ -243,8 +243,6 @@ class Btm { bool CancelBond(const RawAddress& bd_addr); bool RemoveBond(const RawAddress& bd_addr); - void SetSimplePairingCallback(tBTM_SP_CALLBACK* callback); - private: ReadRemoteName le_read_remote_name_; ReadRemoteName classic_read_remote_name_; @@ -256,8 +254,6 @@ class Btm { LegacyInquiryCompleteCallback legacy_inquiry_complete_callback_{}; - tBTM_SP_CALLBACK* simple_pairing_callback_{nullptr}; - uint8_t active_inquiry_mode_ = 0; // TODO(cmanton) abort if there is no classic acl link up diff --git a/main/shim/btm_api.cc b/main/shim/btm_api.cc index fa439941c..a392ba4bd 100644 --- a/main/shim/btm_api.cc +++ b/main/shim/btm_api.cc @@ -1101,7 +1101,6 @@ bool bluetooth::shim::BTM_SecRegister(const tBTM_APPL_INFO* p_cb_info) { LOG_INFO(LOG_TAG, "UNIMPLEMENTED %s le_key_callback", __func__); } - shim_btm.SetSimplePairingCallback(p_cb_info->p_sp_callback); return true; } diff --git a/main/shim/helpers.h b/main/shim/helpers.h index 5beb09fce..e6fb3b72d 100644 --- a/main/shim/helpers.h +++ b/main/shim/helpers.h @@ -13,12 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#pragma once #include "hci/address_with_type.h" +#include "stack/include/bt_types.h" + namespace bluetooth { -hci::AddressWithType ToAddressWithType(const RawAddress& legacy_address, +inline hci::AddressWithType ToAddressWithType(const RawAddress& legacy_address, tBLE_ADDR_TYPE legacy_type) { // Address and RawAddress are binary equivalent; hci::Address address(legacy_address.address); @@ -33,7 +36,7 @@ hci::AddressWithType ToAddressWithType(const RawAddress& legacy_address, else if (legacy_type == BLE_ADDR_RANDOM_ID) type = hci::AddressType::RANDOM_IDENTITY_ADDRESS; else { - LOG_ALWAYS_FATAL("Bad address type"); + LOG_ALWAYS_FATAL("Bad address type %02x", legacy_type); return hci::AddressWithType{address, hci::AddressType::PUBLIC_DEVICE_ADDRESS}; } -- 2.11.0