Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e96e028d authored by Martin Brabham's avatar Martin Brabham Committed by Automerger Merge Worker
Browse files

BTIF DM Shim: Remove obsolete components am: 9815c769 am: f1085a48

Original change: https://android-review.googlesource.com/c/platform/system/bt/+/1414122

Change-Id: I798f0d4847bf814fd7e7b4633904d43a4c7eb6dd
parents 1ae78d41 f1085a48
Loading
Loading
Loading
Loading
+0 −75
Original line number Original line Diff line number Diff line
@@ -66,7 +66,6 @@
#include "device/include/controller.h"
#include "device/include/controller.h"
#include "device/include/interop.h"
#include "device/include/interop.h"
#include "internal_include/stack_config.h"
#include "internal_include/stack_config.h"
#include "main/shim/btif_dm.h"
#include "main/shim/shim.h"
#include "main/shim/shim.h"
#include "osi/include/allocator.h"
#include "osi/include/allocator.h"
#include "osi/include/log.h"
#include "osi/include/log.h"
@@ -248,63 +247,8 @@ static bool is_bonding_or_sdp() {
         (pairing_cb.state == BT_BOND_STATE_BONDED && pairing_cb.sdp_attempts);
         (pairing_cb.state == BT_BOND_STATE_BONDED && pairing_cb.sdp_attempts);
}
}


static void btif_dm_send_bond_state_changed(RawAddress address, bt_bond_state_t bond_state) {
  btif_stats_add_bond_event(address, BTIF_DM_FUNC_BOND_STATE_CHANGED,
                            bond_state);

  if (bond_state == BT_BOND_STATE_NONE) {
    MetricIdAllocator::GetInstance().ForgetDevice(address);
  } else if (bond_state == BT_BOND_STATE_BONDED) {
    MetricIdAllocator::GetInstance().AllocateId(address);
    if (!MetricIdAllocator::GetInstance().SaveDevice(address)) {
      LOG(FATAL) << __func__ << ": Fail to save metric id for device "
                 << address;
    }
  }

  invoke_bond_state_changed_cb(BT_STATUS_SUCCESS, address, bond_state);
  if (bluetooth::shim::is_gd_shim_enabled()) {
    // TODO(b/165394095): Clean up the callback path and find the uniform place
    // to start service discovery
    btif_dm_get_remote_services(address, BT_TRANSPORT_UNKNOWN);
  }
}

void btif_dm_init(uid_set_t* set) {
void btif_dm_init(uid_set_t* set) {
  uid_set = set;
  uid_set = set;
  if (bluetooth::shim::is_gd_shim_enabled()) {
    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) {
      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
      invoke_bond_state_changed_cb(BT_STATUS_SUCCESS, address,
                                   BT_BOND_STATE_BONDING);

      if (pairing_variant == BT_SSP_VARIANT_PASSKEY_ENTRY) {
        // For passkey entry we must actually use pin request, due to
        // BluetoothPairingController (in Settings)
        invoke_pin_request_cb(address, bd_name, cod, false);
        return;
      }

      invoke_ssp_request_cb(address, bd_name, cod, pairing_variant, pass_key);
    });

    bluetooth::shim::BTIF_RegisterBondStateChangeListener(
        [](RawAddress address) {
          btif_dm_send_bond_state_changed(address, BT_BOND_STATE_BONDING);
        },
        [](RawAddress address) {
          btif_dm_send_bond_state_changed(address, BT_BOND_STATE_BONDED);
        },
        [](RawAddress address) {
          btif_dm_send_bond_state_changed(address, BT_BOND_STATE_NONE);
        });
  }
}
}


void btif_dm_cleanup(void) {
void btif_dm_cleanup(void) {
@@ -2000,16 +1944,6 @@ void btif_dm_pin_reply(const RawAddress bd_addr, uint8_t accept,
                       uint8_t pin_len, bt_pin_code_t pin_code) {
                       uint8_t pin_len, bt_pin_code_t pin_code) {
  BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept);
  BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept);


  if (bluetooth::shim::is_gd_shim_enabled()) {
    uint8_t tmp_dev_type = 0;
    tBLE_ADDR_TYPE tmp_addr_type = BLE_ADDR_PUBLIC;
    BTM_ReadDevInfo(bd_addr, &tmp_dev_type, &tmp_addr_type);

    bluetooth::shim::BTIF_DM_pin_reply(bd_addr, tmp_addr_type, accept, pin_len,
                                       pin_code);
    return;
  }

  if (pairing_cb.is_le_only) {
  if (pairing_cb.is_le_only) {
    int i;
    int i;
    uint32_t passkey = 0;
    uint32_t passkey = 0;
@@ -2036,15 +1970,6 @@ void btif_dm_pin_reply(const RawAddress bd_addr, uint8_t accept,
 ******************************************************************************/
 ******************************************************************************/
void btif_dm_ssp_reply(const RawAddress bd_addr, bt_ssp_variant_t variant,
void btif_dm_ssp_reply(const RawAddress bd_addr, bt_ssp_variant_t variant,
                       uint8_t accept) {
                       uint8_t accept) {
  if (bluetooth::shim::is_gd_shim_enabled()) {
    tBT_DEVICE_TYPE tmp_dev_type;
    tBLE_ADDR_TYPE tmp_addr_type = BLE_ADDR_PUBLIC;
    BTM_ReadDevInfo(bd_addr, &tmp_dev_type, &tmp_addr_type);

    bluetooth::shim::BTIF_DM_ssp_reply(bd_addr, tmp_addr_type, variant, accept);
    return;
  }

  BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept);
  BTIF_TRACE_EVENT("%s: accept=%d", __func__, accept);
  if (pairing_cb.is_le_only) {
  if (pairing_cb.is_le_only) {
    if (pairing_cb.is_le_nc) {
    if (pairing_cb.is_le_nc) {
+0 −1
Original line number Original line Diff line number Diff line
@@ -3,7 +3,6 @@ filegroup {
    srcs: [
    srcs: [
        "acl.cc",
        "acl.cc",
        "acl_api.cc",
        "acl_api.cc",
        "btif_dm.cc",
        "btm.cc",
        "btm.cc",
        "btm_api.cc",
        "btm_api.cc",
        "controller.cc",
        "controller.cc",

system/main/shim/btif_dm.cc

deleted100644 → 0
+0 −205
Original line number Original line Diff line number Diff line
/*
 * 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 "osi/include/log.h"

#include "main/shim/btif_dm.h"
#include "main/shim/entry.h"
#include "main/shim/helpers.h"
#include "security/security_module.h"
#include "security/ui.h"

using ::bluetooth::shim::GetSecurityModule;

namespace bluetooth {
namespace shim {

namespace {
bool waiting_for_pairing_prompt = false;
}

class ShimUi : public security::UI {
 public:
  ~ShimUi() {}
  void DisplayPairingPrompt(const bluetooth::hci::AddressWithType& address,
                            std::string name) {
    waiting_for_pairing_prompt = true;
    bt_bdname_t legacy_name{0};
    memcpy(legacy_name.name, name.data(), name.length());
    callback_(ToRawAddress(address.GetAddress()), legacy_name,
              ((0x1F) << 8) /* COD_UNCLASSIFIED*/, BT_SSP_VARIANT_CONSENT, 0);
  }

  void Cancel(const bluetooth::hci::AddressWithType& address) {
    LOG(WARNING) << " ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ " << __func__;
  }

  void DisplayConfirmValue(const bluetooth::hci::AddressWithType& address,
                           std::string name, uint32_t numeric_value) {
    waiting_for_pairing_prompt = false;
    bt_bdname_t legacy_name{0};
    memcpy(legacy_name.name, name.data(), name.length());
    callback_(ToRawAddress(address.GetAddress()), legacy_name,
              ((0x1F) << 8) /* COD_UNCLASSIFIED*/,
              BT_SSP_VARIANT_PASSKEY_CONFIRMATION, numeric_value);
  }

  void DisplayYesNoDialog(const bluetooth::hci::AddressWithType& address,
                          std::string name) {
    waiting_for_pairing_prompt = false;
    bt_bdname_t legacy_name{0};
    memcpy(legacy_name.name, name.data(), name.length());
    callback_(ToRawAddress(address.GetAddress()), legacy_name,
              ((0x1F) << 8) /* COD_UNCLASSIFIED*/, BT_SSP_VARIANT_CONSENT, 0);
  }

  void DisplayEnterPasskeyDialog(const bluetooth::hci::AddressWithType& address, std::string name) {
    waiting_for_pairing_prompt = false;
    bt_bdname_t legacy_name{0};
    memcpy(legacy_name.name, name.data(), name.length());
    callback_(ToRawAddress(address.GetAddress()), 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) {
    waiting_for_pairing_prompt = false;
    bt_bdname_t legacy_name{0};
    memcpy(legacy_name.name, name.data(), name.length());
    callback_(ToRawAddress(address.GetAddress()), 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) {
    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) {
  auto security_manager = bluetooth::shim::GetSecurityModule()->GetSecurityManager();
  ui.SetLegacyCallback(callback);
  security_manager->SetUserInterfaceHandler(&ui, bluetooth::shim::GetGdShimHandler());
}

class ShimBondListener : public security::ISecurityManagerListener {
 public:
  void SetLegacyCallbacks(std::function<void(RawAddress)> bond_state_bonding_cb,
                          std::function<void(RawAddress)> bond_state_bonded_cb,
                          std::function<void(RawAddress)> bond_state_none_cb) {
    bond_state_bonding_cb_ = bond_state_bonding_cb;
    bond_state_bonded_cb_ = bond_state_bonded_cb;
    bond_state_none_cb_ = bond_state_none_cb;
  }

  void OnDeviceBonded(bluetooth::hci::AddressWithType device) override {
    bond_state_bonded_cb_(ToRawAddress(device.GetAddress()));
  }

  void OnDeviceUnbonded(bluetooth::hci::AddressWithType device) override {
    bond_state_none_cb_(ToRawAddress(device.GetAddress()));
  }

  void OnDeviceBondFailed(bluetooth::hci::AddressWithType device) override {
    bond_state_none_cb_(ToRawAddress(device.GetAddress()));
  }

  void OnEncryptionStateChanged(
      EncryptionChangeView encryption_change_view) override {}

  std::function<void(RawAddress)> bond_state_bonding_cb_;
  std::function<void(RawAddress)> bond_state_bonded_cb_;
  std::function<void(RawAddress)> bond_state_none_cb_;
};

ShimBondListener shim_bond_listener;

void BTIF_RegisterBondStateChangeListener(
    std::function<void(RawAddress)> bonding_cb,
    std::function<void(RawAddress)> bonded_cb,
    std::function<void(RawAddress)> none_cb) {
  auto security_manager =
      bluetooth::shim::GetSecurityModule()->GetSecurityManager();
  shim_bond_listener.SetLegacyCallbacks(bonding_cb, bonded_cb, none_cb);
  security_manager->RegisterCallbackListener(
      &shim_bond_listener, bluetooth::shim::GetGdShimHandler());
}

void BTIF_DM_ssp_reply(const RawAddress bd_addr, uint8_t addr_type, bt_ssp_variant_t variant, uint8_t accept) {
  // TODO: GD expects to receive correct address type.
  // pass addr_type once it's properly set in btif layer
  hci::AddressWithType address = ToAddressWithType(bd_addr, BLE_ADDR_PUBLIC);
  hci::AddressWithType address2 = ToAddressWithType(bd_addr, BLE_ADDR_RANDOM);
  auto security_manager = bluetooth::shim::GetSecurityModule()->GetSecurityManager();

  if (variant == BT_SSP_VARIANT_PASSKEY_CONFIRMATION) {
    if (waiting_for_pairing_prompt) {
      LOG(INFO) << "interpreting confirmation as pairing accept " << address;
      security_manager->OnPairingPromptAccepted(address, accept);
      security_manager->OnPairingPromptAccepted(address2, accept);
      waiting_for_pairing_prompt = false;
    } else {
      LOG(INFO) << "interpreting confirmation as yes/no confirmation "
                << address;
      security_manager->OnConfirmYesNo(address, accept);
      security_manager->OnConfirmYesNo(address2, accept);
    }
  } else if (variant == BT_SSP_VARIANT_CONSENT) {
    LOG(INFO) << "consent ";
    security_manager->OnConfirmYesNo(address, accept);
    security_manager->OnConfirmYesNo(address2, accept);
  } else {
    //TODO:
    //  void OnPasskeyEntry(const bluetooth::hci::AddressWithType& address, uint32_t passkey) override;
    LOG(WARNING) << "Variant not implemented yet %02x" << +variant;
  }
}

void BTIF_DM_pin_reply(const RawAddress bd_addr, uint8_t addr_type, uint8_t accept, uint8_t pin_len, bt_pin_code_t pin_code) {
  // TODO: GD expects to receive correct address type.
  // pass addr_type once it's properly set in btif layer
  hci::AddressWithType address = ToAddressWithType(bd_addr, BLE_ADDR_PUBLIC);
  hci::AddressWithType address2 = ToAddressWithType(bd_addr, BLE_ADDR_RANDOM);
  auto security_manager = bluetooth::shim::GetSecurityModule()->GetSecurityManager();

  if (!accept) {
    LOG_WARN("This case is not implemented!!");
    return;
  }

  uint32_t passkey = 0;
  int multi[] = {100000, 10000, 1000, 100, 10, 1};
  for (int i = 0; i < pin_len; i++) {
    passkey += (multi[i] * (pin_code.pin[i] - '0'));
  }

  security_manager->OnPasskeyEntry(address, passkey);
  security_manager->OnPasskeyEntry(address2, passkey);
}

}  // namespace shim
}  // namespace bluetooth

system/main/shim/btif_dm.h

deleted100644 → 0
+0 −39
Original line number Original line Diff line number Diff line
/*
 * 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);
void BTIF_DM_ssp_reply(const RawAddress bd_addr, uint8_t, bt_ssp_variant_t variant, uint8_t accept);
void BTIF_DM_pin_reply(const RawAddress bd_addr, uint8_t, uint8_t, uint8_t, bt_pin_code_t);
void BTIF_RegisterBondStateChangeListener(
    std::function<void(RawAddress)> bond_state_bonding_cb,
    std::function<void(RawAddress)> bond_state_bonded_cb,
    std::function<void(RawAddress)> bond_state_none_cb);
}  // namespace shim
}  // namespace bluetooth