OSDN Git Service

SHIM: Plumb UI for security
authorMartin Brabham <optedoblivion@google.com>
Wed, 19 Feb 2020 03:15:56 +0000 (19:15 -0800)
committerMartin Brabham <optedoblivion@google.com>
Fri, 21 Feb 2020 22:17:15 +0000 (14:17 -0800)
Bug:
Test: bluetooth_test_gd
Change-Id: I1b10f3886b61ae5d37d8c9fa655e7e01c961a43e

btif/src/btif_dm.cc
gd/security/pairing/classic_pairing_handler.cc
gd/security/pairing/classic_pairing_handler_unittest.cc
main/shim/Android.bp
main/shim/btif_dm.cc [new file with mode: 0644]
main/shim/btif_dm.h [new file with mode: 0644]
main/shim/btm.cc

index 572bad1..03df7c0 100644 (file)
@@ -64,6 +64,8 @@
 #include "device/include/controller.h"
 #include "device/include/interop.h"
 #include "internal_include/stack_config.h"
+#include "main/shim/btif_dm.h"
+#include "main/shim/shim.h"
 #include "osi/include/allocator.h"
 #include "osi/include/log.h"
 #include "osi/include/osi.h"
@@ -288,7 +290,24 @@ static void btif_dm_data_free(uint16_t event, tBTA_DM_SEC* dm_sec) {
     osi_free_and_reset((void**)&dm_sec->ble_key.p_key_value);
 }
 
-void btif_dm_init(uid_set_t* set) { uid_set = set; }
+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);
+          //
+        });
+  }
+}
 
 void btif_dm_cleanup(void) {
   if (uid_set) {
index 93c0865..feccaa9 100644 (file)
@@ -24,26 +24,31 @@ namespace security {
 namespace pairing {
 
 void ClassicPairingHandler::NotifyUiDisplayYesNo(uint32_t numeric_value) {
+  ASSERT(user_interface_handler_ != nullptr);
   user_interface_handler_->Post(common::BindOnce(&UI::DisplayConfirmValue, common::Unretained(user_interface_),
                                                  GetRecord()->GetPseudoAddress(), device_name_, numeric_value));
 }
 
 void ClassicPairingHandler::NotifyUiDisplayYesNo() {
+  ASSERT(user_interface_handler_ != nullptr);
   user_interface_handler_->Post(common::BindOnce(&UI::DisplayYesNoDialog, common::Unretained(user_interface_),
                                                  GetRecord()->GetPseudoAddress(), device_name_));
 }
 
 void ClassicPairingHandler::NotifyUiDisplayPasskey(uint32_t passkey) {
+  ASSERT(user_interface_handler_ != nullptr);
   user_interface_handler_->Post(common::BindOnce(&UI::DisplayPasskey, common::Unretained(user_interface_),
                                                  GetRecord()->GetPseudoAddress(), device_name_, passkey));
 }
 
 void ClassicPairingHandler::NotifyUiDisplayPasskeyInput() {
+  ASSERT(user_interface_handler_ != nullptr);
   user_interface_handler_->Post(common::BindOnce(&UI::DisplayEnterPasskeyDialog, common::Unretained(user_interface_),
                                                  GetRecord()->GetPseudoAddress(), device_name_));
 }
 
 void ClassicPairingHandler::NotifyUiDisplayCancel() {
+  ASSERT(user_interface_handler_ != nullptr);
   user_interface_handler_->Post(
       common::BindOnce(&UI::Cancel, common::Unretained(user_interface_), GetRecord()->GetPseudoAddress()));
 }
index d134ff8..dfbf368 100644 (file)
@@ -102,13 +102,6 @@ class SecurityManagerChannelCallback : public ISecurityManagerChannelListener {
 
 static void pairing_complete_callback(bluetooth::hci::Address address, PairingResultOrFailure status) {
   ASSERT(std::holds_alternative<PairingResult>(status));
-  // auto result = std::get<PairingResult>(status);
-  //  if (std::holds_alternative<PairingResult>(status)) {
-  //    auto result = status::get<PairingResult>(status);
-  //  }
-  //  if (std::holds_alternative<PairingFailure>(status)) {
-  //    auto failure = status::get<PairingFailure>(status);
-  //  }
 }
 
 class ClassicPairingHandlerTest : public ::testing::Test {
index 9b21e8b..961e113 100644 (file)
@@ -1,6 +1,7 @@
 filegroup {
     name: "LibBluetoothShimSources",
     srcs: [
+        "btif_dm.cc",
         "btm.cc",
         "btm_api.cc",
         "controller.cc",
diff --git a/main/shim/btif_dm.cc b/main/shim/btif_dm.cc
new file mode 100644 (file)
index 0000000..2a32e46
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#define LOG_TAG "bt_shim_btif_dm"
+
+#include "main/shim/btif_dm.h"
+
+#include "main/shim/entry.h"
+#include "osi/include/log.h"
+#include "security/security_module.h"
+#include "security/ui.h"
+
+using ::bluetooth::shim::GetSecurityModule;
+
+namespace bluetooth {
+namespace shim {
+
+class ShimUi : public security::UI {
+ public:
+  ~ShimUi() {}
+  void DisplayPairingPrompt(const bluetooth::hci::AddressWithType& address,
+                            std::string name) {
+    LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__);
+  }
+  void Cancel(const bluetooth::hci::AddressWithType& address) {
+    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);
+  }
+  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);
+  }
+  void DisplayEnterPasskeyDialog(const bluetooth::hci::AddressWithType& address,
+                                 std::string name) {
+    LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__);
+  }
+  void DisplayPasskey(const bluetooth::hci::AddressWithType& address,
+                      std::string name, uint32_t passkey) {
+    LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__);
+  }
+
+  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,
+                     uint32_t)>
+      callback_;
+};
+
+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();
+  ui.SetLegacyCallback(callback);
+  security_manager->SetUserInterfaceHandler(
+      &ui, bluetooth::shim::GetGdShimHandler());
+}
+
+}  // namespace shim
+}  // namespace bluetooth
diff --git a/main/shim/btif_dm.h b/main/shim/btif_dm.h
new file mode 100644 (file)
index 0000000..650d6ec
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <cstdint>
+#include <functional>
+
+#include "include/hardware/bluetooth.h"
+#include "types/raw_address.h"
+
+namespace bluetooth {
+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);
+
+}  // namespace shim
+}  // namespace bluetooth
index 12d73e4..c2cce79 100644 (file)
@@ -826,5 +826,7 @@ 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;
 }