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

Commit 721be9e4 authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

vc: Disconnect profile when link key is not found

Bug: 312691809
Test: atest bluetooth_vc_test
Tag: #feature
Change-Id: Ie0227c163f22be40051ba31d807e0b7ccd0d3f23
parent bdcea72e
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 * limitations under the License.
 */

#include <base/logging.h>

#include <map>
#include <vector>

@@ -22,11 +24,10 @@
#include "bta_gatt_queue.h"
#include "devices.h"
#include "gatt_api.h"
#include "gd/common/strings.h"
#include "stack/btm/btm_sec.h"
#include "types/bluetooth/uuid.h"

#include <base/logging.h>

using namespace bluetooth::vc::internal;

void VolumeControlDevice::DeregisterNotifications(tGATT_IF gatt_if) {
@@ -416,8 +417,10 @@ bool VolumeControlDevice::IsEncryptionEnabled() {
  return BTM_IsEncrypted(address, BT_TRANSPORT_LE);
}

void VolumeControlDevice::EnableEncryption() {
bool VolumeControlDevice::EnableEncryption() {
  int result = BTM_SetEncryption(address, BT_TRANSPORT_LE, nullptr, nullptr,
                                 BTM_BLE_SEC_ENCRYPT);
  LOG(INFO) << __func__ << ": result=" << +result;
  LOG_INFO("%s: result=0x%02x", ADDRESS_TO_LOGGABLE_CSTR(address), result);

  return result != BTM_ERR_KEY_MISSING;
}
+1 −1
Original line number Diff line number Diff line
@@ -137,7 +137,7 @@ class VolumeControlDevice : public bluetooth::common::IRedactableLoggable {
                                        GATT_WRITE_OP_CB cb, void* cb_data);
  bool IsEncryptionEnabled();

  void EnableEncryption();
  bool EnableEncryption();

  bool EnqueueInitialRequests(tGATT_IF gatt_if, GATT_READ_OP_CB chrc_read_cb,
                              GATT_WRITE_OP_CB cccd_write_cb);
+5 −1
Original line number Diff line number Diff line
@@ -184,7 +184,11 @@ class VolumeControlImpl : public VolumeControl {
      return;
    }

    device->EnableEncryption();
    if (!device->EnableEncryption()) {
      LOG_ERROR("Link key is not known for %s, disconnect profile",
                ADDRESS_TO_LOGGABLE_CSTR(address));
      device->Disconnect(gatt_if_);
    }
  }

  void OnEncryptionComplete(const RawAddress& address, uint8_t success) {
+25 −0
Original line number Diff line number Diff line
@@ -462,6 +462,8 @@ class VolumeControlTest : public ::testing::Test {
  void SetEncryptionResult(const RawAddress& address, bool success) {
    ON_CALL(btm_interface, BTM_IsEncrypted(address, _))
        .WillByDefault(DoAll(Return(false)));
    ON_CALL(btm_interface, IsLinkKeyKnown(address, _))
        .WillByDefault(DoAll(Return(true)));
    ON_CALL(btm_interface, SetEncryption(address, _, _, _, BTM_BLE_SEC_ENCRYPT))
        .WillByDefault(Invoke(
            [&success, this](const RawAddress& bd_addr, tBT_TRANSPORT transport,
@@ -731,6 +733,27 @@ TEST_F(VolumeControlTest, test_disconnected_while_autoconnect) {
  TestAppUnregister();
}

TEST_F(VolumeControlTest, test_disconnect_when_link_key_gone) {
  const RawAddress test_address = GetTestAddress(0);
  TestAppRegister();
  TestAddFromStorage(test_address);

  ON_CALL(btm_interface, BTM_IsEncrypted(test_address, _))
      .WillByDefault(DoAll(Return(false)));
  ON_CALL(btm_interface,
          SetEncryption(test_address, _, _, _, BTM_BLE_SEC_ENCRYPT))
      .WillByDefault(Return(BTM_ERR_KEY_MISSING));

  // autoconnect - don't indicate disconnection
  EXPECT_CALL(*callbacks,
              OnConnectionState(ConnectionState::DISCONNECTED, test_address))
      .Times(0);
  EXPECT_CALL(gatt_interface, Close(1));
  GetConnectedEvent(test_address, 1);
  Mock::VerifyAndClearExpectations(&btm_interface);
  TestAppUnregister();
}

TEST_F(VolumeControlTest, test_reconnect_after_encryption_failed) {
  const RawAddress test_address = GetTestAddress(0);
  TestAppRegister();
@@ -755,6 +778,8 @@ TEST_F(VolumeControlTest, test_service_discovery_completed_before_encryption) {

  ON_CALL(btm_interface, BTM_IsEncrypted(test_address, _))
      .WillByDefault(DoAll(Return(false)));
  ON_CALL(btm_interface, IsLinkKeyKnown(test_address, _))
      .WillByDefault(DoAll(Return(true)));
  ON_CALL(btm_interface, SetEncryption(test_address, _, _, _, _))
      .WillByDefault(Return(BTM_SUCCESS));

+4 −1
Original line number Diff line number Diff line
@@ -26,7 +26,10 @@

using namespace bluetooth::vc::internal;

void VolumeControlDevice::EnableEncryption() { inc_func_call_count(__func__); }
bool VolumeControlDevice::EnableEncryption() {
  inc_func_call_count(__func__);
  return true;
}
bool VolumeControlDevice::EnqueueInitialRequests(
    tGATT_IF gatt_if, GATT_READ_OP_CB chrc_read_cb,
    GATT_WRITE_OP_CB cccd_write_cb) {