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

Commit 91b06909 authored by Krzysztof Kopyściński's avatar Krzysztof Kopyściński
Browse files

has_client.cc: Connect shall connect only requested device

`Connect` method should only connect single device, defined by
`address` parameter. Otherwise, fail on connection of other device in
group will lead to disconnection of already connected ones. Allow
Java layer to handle connecting all group members, as other profiles do.

Bug: 361014973
Flag: com.android.bluetooth.flags.hap_connect_only_requested_device
Test: atest --host --no-bazel-mode bluetooth_has_test
Change-Id: I26ebab5c6105c803865bdce49e008e9c997f3537
parent 5db7f576
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -1238,6 +1238,7 @@ cc_test {
        "liblog",
    ],
    static_libs: [
        "bluetooth_flags_c_lib_for_test",
        "libbluetooth-types",
        "libbluetooth_crypto_toolbox",
        "libbluetooth_gd",
@@ -1245,6 +1246,7 @@ cc_test {
        "libbt-common",
        "libchrome",
        "libgmock",
        "server_configurable_flags",
    ],
    sanitize: {
        cfi: true,
+35 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <base/functional/callback.h>
#include <base/strings/string_number_conversions.h>
#include <bluetooth/log.h>
#include <com_android_bluetooth_flags.h>
#include <hardware/bt_gatt_types.h>
#include <hardware/bt_has.h>

@@ -153,6 +154,22 @@ public:
      return;
    }

    if (com::android::bluetooth::flags::hap_connect_only_requested_device()) {
      auto device =
              std::find_if(devices_.begin(), devices_.end(), HasDevice::MatchAddress(address));
      if (device == devices_.end()) {
        devices_.emplace_back(address, true);
        BTA_GATTC_Open(gatt_if_, address, BTM_BLE_DIRECT_CONNECTION, false);

      } else {
        device->is_connecting_actively = true;
        if (!device->IsConnected()) {
          BTA_GATTC_Open(gatt_if_, address, BTM_BLE_DIRECT_CONNECTION, false);
        }
      }
      return;
    }

    std::vector<RawAddress> addresses = {address};
    auto csis_api = CsisClient::Get();
    if (csis_api != nullptr) {
@@ -202,6 +219,24 @@ public:
  void Disconnect(const RawAddress& address) override {
    log::debug("{}", address);

    if (com::android::bluetooth::flags::hap_connect_only_requested_device()) {
      auto device =
              std::find_if(devices_.begin(), devices_.end(), HasDevice::MatchAddress(address));
      auto conn_id = device->conn_id;
      auto is_connecting_actively = device->is_connecting_actively;
      if (conn_id != GATT_INVALID_CONN_ID) {
        BTA_GATTC_Close(conn_id);
        callbacks_->OnConnectionState(ConnectionState::DISCONNECTED, address);
      } else {
        /* Removes active connection. */
        if (is_connecting_actively) {
          BTA_GATTC_CancelOpen(gatt_if_, address, true);
          callbacks_->OnConnectionState(ConnectionState::DISCONNECTED, address);
        }
      }
      return;
    }

    std::vector<RawAddress> addresses = {address};
    auto csis_api = CsisClient::Get();
    if (csis_api != nullptr) {