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

Commit 097f1bdf authored by Hansong Zhang's avatar Hansong Zhang
Browse files

L2cap shim improvement

Implement ReadRemoteVersion in L2cap shim.
Notify BTA layer directly when connection is up/down.

Tag: #gd-refactor
Bug: 141555841
Test: cert/run --host
Test: Pair and connect with a headset
Change-Id: I193bb24c52f06039359aeda84d68f429d60bf33b
parent 069082cb
Loading
Loading
Loading
Loading
+36 −6
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#define LOG_TAG "bt_shim_l2cap"

#include "main/shim/l2c_api.h"
#include "bta/include/bta_dm_acl.h"
#include "gd/l2cap/classic/l2cap_classic_module.h"
#include "gd/l2cap/le/l2cap_le_module.h"
#include "gd/os/log.h"
@@ -90,6 +91,18 @@ class SecurityEnforcementShim
  intptr_t security_enforce_callback_counter_ = 100;
} security_enforcement_shim_;

struct RemoteFeature {
  uint8_t lmp_version = 0;
  uint16_t manufacturer_name = 0;
  uint16_t sub_version = 0;
  bool version_info_received = false;
  bool role_switch_supported = false;
  bool ssp_supported = false;
  bool sc_supported = false;
};

std::unordered_map<RawAddress, RemoteFeature> remote_feature_map_;

class SecurityListenerShim
    : public bluetooth::l2cap::classic::LinkSecurityInterfaceListener {
 public:
@@ -100,7 +113,8 @@ class SecurityListenerShim

    uint16_t handle = interface->GetAclHandle();
    address_to_handle_[bda] = handle;
    btm_acl_connected(bda, handle, HCI_SUCCESS, 0);
    btm_sec_connected(bda, handle, HCI_SUCCESS, 0);
    BTA_dm_acl_up(bda, BT_TRANSPORT_BR_EDR);
    address_to_interface_[bda] = std::move(interface);
  }

@@ -113,7 +127,8 @@ class SecurityListenerShim
  void OnLinkDisconnected(bluetooth::hci::Address remote) override {
    auto bda = bluetooth::ToRawAddress(remote);
    uint16_t handle = address_to_handle_[bda];
    btm_acl_disconnected(HCI_SUCCESS, handle, HCI_ERR_PEER_USER);
    btm_sec_disconnected(handle, HCI_ERR_PEER_USER);
    BTA_dm_acl_down(bda, BT_TRANSPORT_BR_EDR);
    address_to_handle_.erase(bda);
    address_to_interface_.erase(bda);
  }
@@ -130,10 +145,11 @@ class SecurityListenerShim
                                      uint16_t manufacturer_name,
                                      uint16_t sub_version) override {
    auto bda = bluetooth::ToRawAddress(remote);
    uint16_t handle = address_to_handle_[bda];

    btm_read_remote_version_complete(HCI_SUCCESS, handle, lmp_version,
                                     manufacturer_name, sub_version);
    auto& entry = remote_feature_map_[bda];
    entry.lmp_version = lmp_version;
    entry.manufacturer_name = manufacturer_name;
    entry.sub_version = sub_version;
    entry.version_info_received = true;
  }

  void OnReadRemoteExtendedFeatures(bluetooth::hci::Address remote,
@@ -171,6 +187,20 @@ class SecurityListenerShim

bluetooth::l2cap::classic::SecurityInterface* security_interface_ = nullptr;

bool bluetooth::shim::L2CA_ReadRemoteVersion(const RawAddress& addr,
                                             uint8_t* lmp_version,
                                             uint16_t* manufacturer,
                                             uint16_t* lmp_sub_version) {
  auto& entry = remote_feature_map_[addr];
  if (!entry.version_info_received) {
    return false;
  }
  *lmp_version = entry.lmp_version;
  *manufacturer = entry.manufacturer_name;
  *lmp_sub_version = entry.sub_version;
  return true;
}

void bluetooth::shim::L2CA_UseLegacySecurityModule() {
  LOG_INFO("GD L2cap is using legacy security module");
  bluetooth::shim::GetL2capClassicModule()->InjectSecurityEnforcementInterface(
+4 −0
Original line number Diff line number Diff line
@@ -516,5 +516,9 @@ void L2CA_SetBondingState(const RawAddress& p_bd_addr, bool is_bonding);
void L2CA_UseLegacySecurityModule();

void L2CA_SwitchRoleToCentral(const RawAddress& addr);

bool L2CA_ReadRemoteVersion(const RawAddress& addr, uint8_t* lmp_version,
                            uint16_t* manufacturer, uint16_t* lmp_sub_version);

}  // namespace shim
}  // namespace bluetooth
+12 −0
Original line number Diff line number Diff line
@@ -1498,8 +1498,20 @@ uint16_t BTM_GetMaxPacketSize(const RawAddress& addr) {
  return (pkt_size);
}

/*******************************************************************************
 *
 * Function         BTM_ReadRemoteVersion
 *
 * Returns          If connected report peer device info
 *
 ******************************************************************************/
bool BTM_ReadRemoteVersion(const RawAddress& addr, uint8_t* lmp_version,
                           uint16_t* manufacturer, uint16_t* lmp_sub_version) {
  if (!bluetooth::shim::is_gd_l2cap_enabled()) {
    return bluetooth::shim::L2CA_ReadRemoteVersion(
        addr, lmp_version, manufacturer, lmp_sub_version);
  }

  const tACL_CONN* p_acl = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
  if (p_acl == nullptr) {
    p_acl = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_LE);