Get rid of Security shim

Bug: 149757450
Change-Id: Id7137d64059cf5d15cd94419255c4b1f051bee60
diff --git a/system/gd/facade/grpc_root_server.cc b/system/gd/facade/grpc_root_server.cc
index 88b49c7..449e6bb 100644
--- a/system/gd/facade/grpc_root_server.cc
+++ b/system/gd/facade/grpc_root_server.cc
@@ -34,6 +34,7 @@
 #include "os/log.h"
 #include "os/thread.h"
 #include "security/facade.h"
+#include "security/security_module.h"
 #include "shim/advertising.h"
 #include "shim/connectability.h"
 #include "shim/dumpsys.h"
@@ -43,7 +44,6 @@
 #include "shim/name.h"
 #include "shim/page.h"
 #include "shim/scanning.h"
-#include "shim/security.h"
 #include "shim/storage.h"
 #include "stack_manager.h"
 
@@ -115,7 +115,7 @@
         modules.add<::bluetooth::shim::Name>();
         modules.add<::bluetooth::shim::Page>();
         modules.add<::bluetooth::shim::Scanning>();
-        modules.add<::bluetooth::shim::Security>();
+        modules.add<::bluetooth::security::SecurityModule>();
         modules.add<::bluetooth::shim::Storage>();
         break;
       default:
diff --git a/system/gd/shim/Android.bp b/system/gd/shim/Android.bp
index b1d5f4a..058b433 100644
--- a/system/gd/shim/Android.bp
+++ b/system/gd/shim/Android.bp
@@ -11,7 +11,6 @@
             "name_db.cc",
             "page.cc",
             "scanning.cc",
-            "security.cc",
             "stack.cc",
             "storage.cc",
     ]
diff --git a/system/gd/shim/only_include_this_file_into_legacy_stack___ever.h b/system/gd/shim/only_include_this_file_into_legacy_stack___ever.h
index 41415e3..30c772e 100644
--- a/system/gd/shim/only_include_this_file_into_legacy_stack___ever.h
+++ b/system/gd/shim/only_include_this_file_into_legacy_stack___ever.h
@@ -38,7 +38,6 @@
 class NameDb;
 class Page;
 class Scanning;
-class Security;
 class SecurityModule;
 class Storage;
 }  // namespace shim
diff --git a/system/gd/shim/security.cc b/system/gd/shim/security.cc
deleted file mode 100644
index 0e2b069..0000000
--- a/system/gd/shim/security.cc
+++ /dev/null
@@ -1,214 +0,0 @@
-/*
- * Copyright 2019 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_gd_shim"
-
-#include "shim/security.h"
-
-#include <functional>
-#include <memory>
-#include <string>
-
-#include "common/bind.h"
-#include "hci/address.h"
-#include "module.h"
-#include "os/handler.h"
-#include "os/log.h"
-#include "security/security_manager.h"
-#include "security/security_module.h"
-
-namespace bluetooth {
-namespace shim {
-
-namespace {
-constexpr char kModuleName[] = "shim::Security";
-
-constexpr uint8_t kLegacyAddressTypePublic = 0;
-constexpr uint8_t kLegacyAddressTypeRandom = 1;
-constexpr uint8_t kLegacyAddressTypePublicIdentity = 2;
-constexpr uint8_t kLegacyAddressTypeRandomIdentity = 3;
-
-// TOOD: implement properly, have it passed from above shim ?
-class UIHandler : public ::bluetooth::security::UI {
- public:
-  void DisplayPairingPrompt(const hci::AddressWithType& address, std::string name) override {}
-  void Cancel(const hci::AddressWithType& address) override {}
-  void DisplayConfirmValue(const hci::AddressWithType& address, std::string name, uint32_t numeric_value) override {}
-  void DisplayYesNoDialog(const bluetooth::hci::AddressWithType& address, std::string name) override {}
-  void DisplayEnterPasskeyDialog(const hci::AddressWithType& address, std::string name) override {}
-  void DisplayPasskey(const hci::AddressWithType& address, std::string name, uint32_t passkey) override {}
-};
-UIHandler static_ui_handler;
-
-}  // namespace
-
-struct Security::impl : public security::ISecurityManagerListener {
-  void OnDeviceBonded(bluetooth::hci::AddressWithType device) override {
-    LOG_DEBUG("UNIMPLEMENTED %s", __func__);
-  }
-
-  void OnDeviceUnbonded(bluetooth::hci::AddressWithType device) override {
-    LOG_DEBUG("UNIMPLEMENTED %s", __func__);
-  }
-
-  void OnDeviceBondFailed(bluetooth::hci::AddressWithType device) override {
-    LOG_DEBUG("UNIMPLEMENTED %s", __func__);
-  }
-
-  void CreateBond(hci::AddressWithType bdaddr);
-  void CreateBondLe(hci::AddressWithType bdaddr);
-  void CancelBond(hci::AddressWithType bdaddr);
-  void RemoveBond(hci::AddressWithType bdaddr);
-
-  os::Handler* Handler() /*override*/;
-
-  void SetSimplePairingCallback(SimplePairingCallback callback);
-
-  impl(bluetooth::security::SecurityModule* security_module, os::Handler* handler);
-  ~impl();
-
-  void Start();
-  void Stop();
-
- private:
-  SimplePairingCallback simple_pairing_callback_;
-
-  std::unique_ptr<bluetooth::security::SecurityManager> security_manager_{nullptr};
-  os::Handler* handler_;
-};
-
-const ModuleFactory Security::Factory = ModuleFactory([]() { return new Security(); });
-
-Security::impl::impl(bluetooth::security::SecurityModule* security_module, os::Handler* handler)
-    : security_manager_(security_module->GetSecurityManager()), handler_(handler) {}
-
-Security::impl::~impl() {}
-
-os::Handler* Security::impl::Handler() {
-  return handler_;
-}
-
-void Security::impl::CreateBond(hci::AddressWithType bdaddr) {
-  security_manager_->CreateBond(bdaddr);
-}
-
-void Security::impl::CreateBondLe(hci::AddressWithType bdaddr) {
-  security_manager_->CreateBondLe(bdaddr);
-}
-
-void Security::impl::CancelBond(hci::AddressWithType bdaddr) {
-  security_manager_->CancelBond(bdaddr);
-}
-
-void Security::impl::RemoveBond(hci::AddressWithType bdaddr) {
-  security_manager_->RemoveBond(bdaddr);
-}
-
-void Security::impl::SetSimplePairingCallback(SimplePairingCallback callback) {
-  ASSERT(!simple_pairing_callback_);
-  simple_pairing_callback_ = callback;
-}
-
-void Security::impl::Start() {
-  LOG_DEBUG("Starting security manager shim");
-  security_manager_->SetUserInterfaceHandler(&static_ui_handler, handler_);
-  security_manager_->RegisterCallbackListener(this, handler_);
-}
-
-void Security::impl::Stop() {
-  security_manager_->UnregisterCallbackListener(this);
-  LOG_DEBUG("Stopping security manager shim");
-}
-
-void Security::CreateBond(std::string string_address) {
-  hci::Address address;
-  if (!hci::Address::FromString(string_address, address)) {
-    LOG_ERROR("%s bad address: %s, aborting", __func__, address.ToString().c_str());
-    return;
-  }
-  pimpl_->CreateBond(hci::AddressWithType{address, hci::AddressType::PUBLIC_DEVICE_ADDRESS});
-}
-
-void Security::CreateBondLe(std::string string_address, uint8_t type) {
-  hci::AddressType address_type;
-  switch (type) {
-    case kLegacyAddressTypePublic:
-    default:
-      address_type = hci::AddressType::PUBLIC_DEVICE_ADDRESS;
-      break;
-    case kLegacyAddressTypeRandom:
-      address_type = hci::AddressType::RANDOM_DEVICE_ADDRESS;
-      break;
-    case kLegacyAddressTypePublicIdentity:
-      address_type = hci::AddressType::PUBLIC_IDENTITY_ADDRESS;
-      break;
-    case kLegacyAddressTypeRandomIdentity:
-      address_type = hci::AddressType::RANDOM_IDENTITY_ADDRESS;
-      break;
-  }
-
-  hci::Address address;
-  if (!hci::Address::FromString(string_address, address)) {
-    LOG_ERROR("%s bad address: %s, aborting", __func__, address.ToString().c_str());
-    return;
-  }
-  pimpl_->CreateBondLe(hci::AddressWithType{address, address_type});
-}
-
-void Security::CancelBond(std::string string_address) {
-  hci::Address address;
-  if (!hci::Address::FromString(string_address, address)) {
-    LOG_ERROR("%s bad address: %s, aborting", __func__, address.ToString().c_str());
-    return;
-  }
-  pimpl_->CancelBond(hci::AddressWithType{address, hci::AddressType::PUBLIC_DEVICE_ADDRESS});
-}
-
-void Security::RemoveBond(std::string string_address) {
-  hci::Address address;
-  if (!hci::Address::FromString(string_address, address)) {
-    LOG_ERROR("%s bad address: %s, aborting", __func__, address.ToString().c_str());
-    return;
-  }
-  pimpl_->RemoveBond(hci::AddressWithType{address, hci::AddressType::PUBLIC_DEVICE_ADDRESS});
-}
-
-void Security::SetSimplePairingCallback(SimplePairingCallback callback) {
-  pimpl_->SetSimplePairingCallback(callback);
-}
-
-/**
- * Module methods
- */
-void Security::ListDependencies(ModuleList* list) {
-  list->add<bluetooth::security::SecurityModule>();
-}
-
-void Security::Start() {
-  pimpl_ = std::make_unique<impl>(GetDependency<bluetooth::security::SecurityModule>(), GetHandler());
-  pimpl_->Start();
-}
-
-void Security::Stop() {
-  pimpl_->Stop();
-  pimpl_.reset();
-}
-
-std::string Security::ToString() const {
-  return kModuleName;
-}
-
-}  // namespace shim
-}  // namespace bluetooth
diff --git a/system/gd/shim/security.h b/system/gd/shim/security.h
deleted file mode 100644
index 9670d99..0000000
--- a/system/gd/shim/security.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright 2019 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 <memory>
-#include <string>
-
-#include "module.h"
-
-namespace bluetooth {
-namespace shim {
-
-using SimplePairingCallback = std::function<bool(std::string address, uint32_t value, bool just_works)>;
-
-class Security : public bluetooth::Module {
- public:
-  Security() = default;
-  ~Security() = default;
-
-  void CreateBond(std::string address);
-  void CreateBondLe(std::string address, uint8_t address_type);
-  void CancelBond(std::string address);
-  void RemoveBond(std::string address);
-
-  void SetSimplePairingCallback(SimplePairingCallback callback);
-
-  static const ModuleFactory Factory;
-
- protected:
-  void ListDependencies(ModuleList* list) override;  // Module
-  void Start() override;                             // Module
-  void Stop() override;                              // Module
-  std::string ToString() const override;             // Module
-
- private:
-  struct impl;
-  std::unique_ptr<impl> pimpl_;
-  DISALLOW_COPY_AND_ASSIGN(Security);
-};
-
-}  // namespace shim
-}  // namespace bluetooth
diff --git a/system/gd/shim/stack.cc b/system/gd/shim/stack.cc
index 9983029..9492be7 100644
--- a/system/gd/shim/stack.cc
+++ b/system/gd/shim/stack.cc
@@ -45,7 +45,6 @@
 #include "shim/name_db.h"
 #include "shim/page.h"
 #include "shim/scanning.h"
-#include "shim/security.h"
 #include "shim/storage.h"
 #include "stack_manager.h"
 #include "storage/legacy.h"
@@ -86,7 +85,6 @@
     modules.add<::bluetooth::shim::L2cap>();
     modules.add<::bluetooth::shim::Page>();
     modules.add<::bluetooth::shim::Scanning>();
-    modules.add<::bluetooth::shim::Security>();
     modules.add<::bluetooth::shim::Storage>();
 
     stack_thread_ = new Thread("gd_stack_thread", Thread::Priority::NORMAL);
diff --git a/system/gd/shim/stack.h b/system/gd/shim/stack.h
index 478fcd0..fcf2490 100644
--- a/system/gd/shim/stack.h
+++ b/system/gd/shim/stack.h
@@ -29,7 +29,6 @@
 #include "shim/name.h"
 #include "shim/page.h"
 #include "shim/scanning.h"
-#include "shim/security.h"
 #include "shim/storage.h"
 #include "stack_manager.h"
 
diff --git a/system/main/shim/btm.cc b/system/main/shim/btm.cc
index daecf37..2cf4611 100644
--- a/system/main/shim/btm.cc
+++ b/system/main/shim/btm.cc
@@ -31,7 +31,9 @@
 #include "types/class_of_device.h"
 #include "types/raw_address.h"
 
+#include "main/shim/helpers.h"
 #include "neighbor/discoverability.h"
+#include "security/security_module.h"
 #include "shim/advertising.h"
 #include "shim/connectability.h"
 #include "shim/controller.h"
@@ -39,7 +41,6 @@
 #include "shim/name.h"
 #include "shim/page.h"
 #include "shim/scanning.h"
-#include "shim/security.h"
 
 extern tBTM_CB btm_cb;
 
@@ -615,13 +616,15 @@
                                              tBT_TRANSPORT transport,
                                              uint8_t pin_len, uint8_t* p_pin,
                                              uint32_t trusted_mask[]) {
+  auto security_manager =
+      bluetooth::shim::GetSecurityModule()->GetSecurityManager();
   switch (transport) {
     case BT_TRANSPORT_BR_EDR:
-      bluetooth::shim::GetSecurity()->CreateBond(bd_addr.ToString());
+      security_manager->CreateBond(
+          ToAddressWithType(bd_addr.address, BLE_ADDR_PUBLIC));
       break;
     case BT_TRANSPORT_LE:
-      bluetooth::shim::GetSecurity()->CreateBondLe(bd_addr.ToString(),
-                                                   addr_type);
+      security_manager->CreateBondLe(ToAddressWithType(bd_addr, addr_type));
       break;
     default:
       return bluetooth::shim::BTM_ILLEGAL_VALUE;
@@ -630,38 +633,23 @@
 }
 
 bool bluetooth::shim::Btm::CancelBond(const RawAddress& bd_addr) {
-  bluetooth::shim::GetSecurity()->CancelBond(bd_addr.ToString());
+  auto security_manager =
+      bluetooth::shim::GetSecurityModule()->GetSecurityManager();
+  security_manager->CancelBond(ToAddressWithType(bd_addr, BLE_ADDR_PUBLIC));
   return true;
 }
 
 bool bluetooth::shim::Btm::RemoveBond(const RawAddress& bd_addr) {
   // TODO(cmanton) Check if acl is connected
-  bluetooth::shim::GetSecurity()->RemoveBond(bd_addr.ToString());
+  auto security_manager =
+      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();
   simple_pairing_callback_ = callback;
-
-  bluetooth::shim::GetSecurity()->SetSimplePairingCallback(
-      [this](std::string address_string, uint32_t value, bool just_works) {
-        RawAddress address;
-        RawAddress::FromString(address_string, address);
-
-        tBTM_SP_EVT event = BTM_SP_CFM_REQ_EVT;
-        tBTM_SP_EVT_DATA data;
-
-        data.cfm_req.bd_addr = address;
-        data.cfm_req.num_val = value;
-        data.cfm_req.just_works = just_works;
-        switch (simple_pairing_callback_(event, &data)) {
-          case BTM_SUCCESS:
-          case BTM_SUCCESS_NO_SECURITY:
-            return true;
-          default:
-            break;
-        }
-        return false;
-      });
 }
diff --git a/system/main/shim/entry.cc b/system/main/shim/entry.cc
index dcd3fcf..31e0b19 100644
--- a/system/main/shim/entry.cc
+++ b/system/main/shim/entry.cc
@@ -119,12 +119,6 @@
       ->GetInstance<bluetooth::shim::Scanning>();
 }
 
-bluetooth::shim::Security* bluetooth::shim::GetSecurity() {
-  return GetGabeldorscheStack()
-      ->GetStackManager()
-      ->GetInstance<bluetooth::shim::Security>();
-}
-
 bluetooth::security::SecurityModule* bluetooth::shim::GetSecurityModule() {
   return GetGabeldorscheStack()
       ->GetStackManager()
diff --git a/system/main/shim/entry.h b/system/main/shim/entry.h
index 900eede..71f4bed 100644
--- a/system/main/shim/entry.h
+++ b/system/main/shim/entry.h
@@ -60,7 +60,6 @@
 NameDb* GetNameDb();
 Page* GetPage();
 Scanning* GetScanning();
-Security* GetSecurity();
 bluetooth::security::SecurityModule* GetSecurityModule();
 Storage* GetStorage();
 
diff --git a/system/main/shim/helpers.h b/system/main/shim/helpers.h
new file mode 100644
index 0000000..5beb09f
--- /dev/null
+++ b/system/main/shim/helpers.h
@@ -0,0 +1,43 @@
+/*
+ * 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.
+ */
+
+#include "hci/address_with_type.h"
+
+namespace bluetooth {
+
+hci::AddressWithType ToAddressWithType(const RawAddress& legacy_address,
+                                       tBLE_ADDR_TYPE legacy_type) {
+  // Address and RawAddress are binary equivalent;
+  hci::Address address(legacy_address.address);
+
+  hci::AddressType type;
+  if (legacy_type == BLE_ADDR_PUBLIC)
+    type = hci::AddressType::PUBLIC_DEVICE_ADDRESS;
+  else if (legacy_type == BLE_ADDR_RANDOM)
+    type = hci::AddressType::RANDOM_DEVICE_ADDRESS;
+  else if (legacy_type == BLE_ADDR_PUBLIC_ID)
+    type = hci::AddressType::PUBLIC_IDENTITY_ADDRESS;
+  else if (legacy_type == BLE_ADDR_RANDOM_ID)
+    type = hci::AddressType::RANDOM_IDENTITY_ADDRESS;
+  else {
+    LOG_ALWAYS_FATAL("Bad address type");
+    return hci::AddressWithType{address,
+                                hci::AddressType::PUBLIC_DEVICE_ADDRESS};
+  }
+
+  return hci::AddressWithType{address, type};
+}
+}  // namespace bluetooth
\ No newline at end of file