OSDN Git Service

Wire Security UI callbacks
authorJakub Pawlowski <jpawlowski@google.com>
Wed, 26 Feb 2020 13:24:10 +0000 (14:24 +0100)
committerJakub Pawlowski <jpawlowski@google.com>
Thu, 27 Feb 2020 07:40:08 +0000 (07:40 +0000)
Bug: 147086339
Change-Id: I719459314dd33d922a575126b74ee133f8ac2289

btif/src/btif_dm.cc
main/shim/btif_dm.cc
main/shim/btif_dm.h
main/shim/btm.cc
main/shim/btm.h
main/shim/btm_api.cc
main/shim/helpers.h

index d538473..52cb294 100644 (file)
@@ -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
index 2a32e46..a625c4e 100644 (file)
  */
 #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<void(RawAddress*, bt_bdname_t*, uint32_t,
-                                            bt_ssp_variant_t, uint32_t)>
-                             callback) {
+  void SetLegacyCallback(std::function<void(RawAddress, bt_bdname_t, uint32_t, bt_ssp_variant_t, uint32_t)> callback) {
     callback_ = callback;
   }
 
  private:
-  std::function<void(RawAddress*, bt_bdname_t*, uint32_t, bt_ssp_variant_t,
+  std::function<void(RawAddress, bt_bdname_t, uint32_t, bt_ssp_variant_t,
                      uint32_t)>
       callback_;
 };
@@ -81,16 +80,28 @@ ShimUi ui;
 /**
  * Sets handler to SecurityModule and provides callback to handler
  */
-void BTIF_DM_SetUiCallback(
-    std::function<void(RawAddress*, bt_bdname_t*, uint32_t, bt_ssp_variant_t,
-                       uint32_t)>
-        callback) {
-  LOG_WARN(LOG_TAG, "%s called", __func__);
-  auto security_manager =
-      bluetooth::shim::GetSecurityModule()->GetSecurityManager();
+void BTIF_DM_SetUiCallback(std::function<void(RawAddress, bt_bdname_t, uint32_t, bt_ssp_variant_t, uint32_t)> 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
index 650d6ec..c1305fd 100644 (file)
@@ -28,10 +28,7 @@ namespace shim {
 /**
  * Sets handler to SecurityModule and provides callback to handler
  */
-void BTIF_DM_SetUiCallback(
-    std::function<void(RawAddress*, bt_bdname_t*, uint32_t, bt_ssp_variant_t,
-                       uint32_t)>
-        callback);
-
+void BTIF_DM_SetUiCallback(std::function<void(RawAddress, bt_bdname_t, uint32_t, bt_ssp_variant_t, uint32_t)> callback);
+void BTIF_DM_ssp_reply(const RawAddress bd_addr, uint8_t, bt_ssp_variant_t variant, uint8_t accept);
 }  // namespace shim
 }  // namespace bluetooth
index efda7f5..0c4969a 100644 (file)
@@ -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
index 15e0ad4..ef96736 100644 (file)
@@ -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
index fa43994..a392ba4 100644 (file)
@@ -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;
 }
 
index 5beb09f..e6fb3b7 100644 (file)
  * 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};
   }