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

Commit 5560dc33 authored by Martin Brabham's avatar Martin Brabham Committed by Myles Watson
Browse files

Return matching device in CreateDatabase when called twice

Bug: N/A
Test: atest --host -t bluetooth_test_gd:DeviceTest bluetooth_test_gd:DeviceDatabaseTest bluetooth_test_gd:DualDeviceTest
Change-Id: Ic8df7da1f3f003994acbdde773e2131b614d3def
parent f6f3fdcf
Loading
Loading
Loading
Loading
+6 −14
Original line number Diff line number Diff line
@@ -30,20 +30,16 @@ using namespace bluetooth::hci;
std::shared_ptr<ClassicDevice> DeviceDatabase::CreateClassicDevice(Address address) {
  ClassicDevice device(address);
  const std::string uuid = device.GetUuid();
  if (AddDeviceToMap(std::move(device))) {
  AddDeviceToMap(std::move(device));
  return GetClassicDevice(uuid);
}
  return std::shared_ptr<ClassicDevice>();
}

std::shared_ptr<LeDevice> DeviceDatabase::CreateLeDevice(Address address) {
  LeDevice device(address);
  const std::string uuid = device.GetUuid();
  if (AddDeviceToMap(std::move(device))) {
  AddDeviceToMap(std::move(device));
  return GetLeDevice(uuid);
}
  return std::shared_ptr<LeDevice>();
}

std::shared_ptr<DualDevice> DeviceDatabase::CreateDualDevice(Address address) {
  auto classic = CreateClassicDevice(address);
@@ -51,10 +47,9 @@ std::shared_ptr<DualDevice> DeviceDatabase::CreateDualDevice(Address address) {
  if (classic && le) {
    DualDevice device(address, classic, le);
    std::string uuid = device.GetUuid();
    if (AddDeviceToMap(std::move(device))) {
    AddDeviceToMap(std::move(device));
    return GetDualDevice(uuid);
  }
  }
  LOG_WARN("Attempting to instert a DUAL device that already exists!");
  return std::shared_ptr<DualDevice>();
}
@@ -189,7 +184,6 @@ bool DeviceDatabase::AddDeviceToMap(ClassicDevice&& device) {
    auto it = classic_device_map_.find(device.GetUuid());
    // If we have a record with the same key
    if (it != classic_device_map_.end()) {
      LOG_ERROR("Attempt to re-insert classic device '%s' object with same UUID", uuid.c_str());
      // We don't want to insert and overwrite
      return false;
    }
@@ -217,7 +211,6 @@ bool DeviceDatabase::AddDeviceToMap(LeDevice&& device) {
    auto it = le_device_map_.find(device.GetUuid());
    // If we have a record with the same key
    if (it != le_device_map_.end()) {
      LOG_ERROR("Attempt to re-insert LE device '%s' object with same UUID", uuid.c_str());
      // We don't want to insert and overwrite
      return false;
    }
@@ -244,7 +237,6 @@ bool DeviceDatabase::AddDeviceToMap(DualDevice&& device) {
    auto it = dual_device_map_.find(device.GetUuid());
    // If we have a record with the same key
    if (it != dual_device_map_.end()) {
      LOG_ERROR("Attempt to re-insert dual device '%s' object with same UUID", uuid.c_str());
      // We don't want to insert and overwrite
      return false;
    }
+4 −3
Original line number Diff line number Diff line
@@ -61,12 +61,13 @@ TEST_F(DeviceDatabaseTest, create_dual_device) {
  ASSERT_EQ(address_str, dual_device->GetUuid());
}

// Shouldn't fail when creating twice.  Should just get back a s_ptr the same device
TEST_F(DeviceDatabaseTest, create_classic_device_twice) {
  auto classic_device = device_database_.CreateClassicDevice(address);
  ASSERT_TRUE(classic_device);
  ASSERT_EQ(CLASSIC, classic_device->GetDeviceType());
  ASSERT_EQ(address_str, classic_device->GetUuid());
  ASSERT_FALSE(device_database_.CreateClassicDevice(address));
  ASSERT_TRUE(device_database_.CreateClassicDevice(address));
}

TEST_F(DeviceDatabaseTest, create_le_device_twice) {
@@ -74,7 +75,7 @@ TEST_F(DeviceDatabaseTest, create_le_device_twice) {
  ASSERT_TRUE(le_device);
  ASSERT_EQ(LE, le_device->GetDeviceType());
  ASSERT_EQ(address_str, le_device->GetUuid());
  ASSERT_FALSE(device_database_.CreateLeDevice(address));
  ASSERT_TRUE(device_database_.CreateLeDevice(address));
}

TEST_F(DeviceDatabaseTest, create_dual_device_twice) {
@@ -93,7 +94,7 @@ TEST_F(DeviceDatabaseTest, create_dual_device_twice) {
  ASSERT_EQ(DUAL, dual_device->GetLeDevice()->GetDeviceType());
  ASSERT_EQ(address_str, dual_device->GetLeDevice()->GetUuid());

  ASSERT_FALSE(device_database_.CreateDualDevice(address));
  ASSERT_TRUE(device_database_.CreateDualDevice(address));
}

TEST_F(DeviceDatabaseTest, remove_device) {
+1 −1
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ namespace bluetooth::hci {
/**
 * TODO(optedoblivion): Build out AddressType getter/setter
 */
enum AddressType {};
// enum AddressType {};

/**
 * A device representing a LE device.