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

Commit 06d538bc authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

groups: Extend RemoveDevice with group id

If a group_id provided to this function is kGroupUnknown, then groups
will remove device from all the groups. Otherwise device will be removed
only for a single group.

Bug: 150670922
Tag: #feature
Test: atest --host bluetooth_test_groups
Sponsor: jpawlowski@

Change-Id: I97317d232bc7c3036701d2cb060fe8dc7fd65b16
parent 3678af77
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -123,7 +123,10 @@ class DeviceGroupsImpl : public DeviceGroups {
    return group->GetGroupId();
  }

  void RemoveDevice(const RawAddress& addr) override {
  void RemoveDevice(const RawAddress& addr, int group_id) override {
    int num_of_groups_dev_belongs = 0;

    /* Remove from all the groups. Usually happens on unbond */
    for (auto it = groups_.begin(); it != groups_.end();) {
      auto& [id, g] = *it;
      if (!g.Contains(addr)) {
@@ -131,6 +134,15 @@ class DeviceGroupsImpl : public DeviceGroups {
        continue;
      }

      num_of_groups_dev_belongs++;

      if ((group_id != bluetooth::groups::kGroupUnknown) && (group_id != id)) {
        ++it;
        continue;
      }

      num_of_groups_dev_belongs--;

      g.Remove(addr);
      for (auto c : callbacks_) {
        c->OnGroupMemberRemoved(addr, id);
@@ -147,6 +159,9 @@ class DeviceGroupsImpl : public DeviceGroups {
    }

    btif_storage_remove_groups(addr);
    if (num_of_groups_dev_belongs > 0) {
      btif_storage_add_groups(addr);
    }
  }

  bool SerializeGroups(const RawAddress& addr,
+21 −0
Original line number Diff line number Diff line
@@ -186,6 +186,27 @@ TEST_F(GroupsTest, test_remove_multiple_groups) {
  DeviceGroups::CleanUp(callbacks.get());
}

TEST_F(GroupsTest, test_remove_device_fo_devices) {
  Uuid uuid1 = Uuid::GetRandom();
  Uuid uuid2 = Uuid::GetRandom();
  EXPECT_CALL(*callbacks, OnGroupAdded(_, _, _)).Times(2);
  DeviceGroups::Initialize(callbacks.get());
  DeviceGroups::Get()->AddDevice(GetTestAddress(1), uuid1, 8);
  DeviceGroups::Get()->AddDevice(GetTestAddress(1), uuid2, 9);

  EXPECT_CALL(*callbacks, OnGroupRemoved(uuid1, 8));
  EXPECT_CALL(*callbacks, OnGroupRemoved(uuid2, 9)).Times(0);

  DeviceGroups::Get()->RemoveDevice(GetTestAddress(1), 8);

  Mock::VerifyAndClearExpectations(&callbacks);

  EXPECT_CALL(*callbacks, OnGroupRemoved(uuid1, 8)).Times(0);
  EXPECT_CALL(*callbacks, OnGroupRemoved(uuid2, 9));

  DeviceGroups::Get()->RemoveDevice(GetTestAddress(1), 9);
}

TEST_F(GroupsTest, test_add_devices_different_group_id) {
  DeviceGroups::Initialize(callbacks.get());
  DeviceGroups::Get()->AddDevice(GetTestAddress(2), Uuid::kEmpty, 10);
+3 −1
Original line number Diff line number Diff line
@@ -70,7 +70,9 @@ class DeviceGroups {
  virtual int GetGroupId(
      const RawAddress& addr,
      bluetooth::Uuid uuid = bluetooth::groups::kGenericContextUuid) const = 0;
  virtual void RemoveDevice(const RawAddress& addr) = 0;
  virtual void RemoveDevice(
      const RawAddress& addr,
      int group_id = bluetooth::groups::kGroupUnknown) = 0;
};

}  // namespace groups