Commit 0dc78f92 authored by Eli Ribble's avatar Eli Ribble Committed by Commit Bot

Add GattClientManager::GetConnectedDevices

This will eventually take the place of
GattClientManager::GetNumConnected. This moves bluetooth closer to being
fully async

Bug: 76155468
Test: chromecast/device/bluetooth/le/gatt_client_manager_impl_test.cc
Change-Id: I53d7eb0b2d5743165c1df5fe64903b86012a0346
Reviewed-on: https://chromium-review.googlesource.com/1147879Reviewed-by: default avatarBailey Forrest <bcf@chromium.org>
Reviewed-by: default avatarStephen Lanham <slan@chromium.org>
Commit-Queue: Eli Ribble <eliribble@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577748}
parent 56525d2a
...@@ -54,9 +54,6 @@ class GattClientManager { ...@@ -54,9 +54,6 @@ class GattClientManager {
virtual void AddObserver(Observer* o) = 0; virtual void AddObserver(Observer* o) = 0;
virtual void RemoveObserver(Observer* o) = 0; virtual void RemoveObserver(Observer* o) = 0;
// TODO(bcf/slan): Add new method:
// void GetDevices(Callback<void(vector<scoped_refptr<RemoteDevice>>)> cb);
// Get a RemoteDevice object corresponding to |addr| for performing GATT // Get a RemoteDevice object corresponding to |addr| for performing GATT
// operations. |cb| will be run on the callers thread. Callbacks passed into // operations. |cb| will be run on the callers thread. Callbacks passed into
// methods on RemoteDevice and its subobjects (RemoteService, // methods on RemoteDevice and its subobjects (RemoteService,
...@@ -70,7 +67,13 @@ class GattClientManager { ...@@ -70,7 +67,13 @@ class GattClientManager {
virtual scoped_refptr<RemoteDevice> GetDeviceSync( virtual scoped_refptr<RemoteDevice> GetDeviceSync(
const bluetooth_v2_shlib::Addr& addr) = 0; const bluetooth_v2_shlib::Addr& addr) = 0;
// Returns the currently connected devices.
using GetConnectDevicesCallback =
base::OnceCallback<void(std::vector<scoped_refptr<RemoteDevice>>)>;
virtual void GetConnectedDevices(GetConnectDevicesCallback cb) = 0;
// Returns the number of devices which are currently connected. // Returns the number of devices which are currently connected.
// TODO(bcf): Deprecated in favor of |GetConnectedDevices|.
virtual void GetNumConnected(base::OnceCallback<void(size_t)> cb) const = 0; virtual void GetNumConnected(base::OnceCallback<void(size_t)> cb) const = 0;
virtual void NotifyConnect(const bluetooth_v2_shlib::Addr& addr) = 0; virtual void NotifyConnect(const bluetooth_v2_shlib::Addr& addr) = 0;
......
...@@ -94,6 +94,19 @@ scoped_refptr<RemoteDevice> GattClientManagerImpl::GetDeviceSync( ...@@ -94,6 +94,19 @@ scoped_refptr<RemoteDevice> GattClientManagerImpl::GetDeviceSync(
return new_device; return new_device;
} }
void GattClientManagerImpl::GetConnectedDevices(GetConnectDevicesCallback cb) {
MAKE_SURE_IO_THREAD(GetConnectedDevices,
BindToCurrentSequence(std::move(cb)));
std::vector<scoped_refptr<RemoteDevice>> devices;
for (const auto& device : addr_to_device_) {
if (device.second->IsConnected()) {
devices.push_back(device.second);
}
}
std::move(cb).Run(std::move(devices));
}
void GattClientManagerImpl::GetNumConnected( void GattClientManagerImpl::GetNumConnected(
base::OnceCallback<void(size_t)> cb) const { base::OnceCallback<void(size_t)> cb) const {
MAKE_SURE_IO_THREAD(GetNumConnected, BindToCurrentSequence(std::move(cb))); MAKE_SURE_IO_THREAD(GetNumConnected, BindToCurrentSequence(std::move(cb)));
......
...@@ -37,6 +37,7 @@ class GattClientManagerImpl ...@@ -37,6 +37,7 @@ class GattClientManagerImpl
base::OnceCallback<void(scoped_refptr<RemoteDevice>)> cb) override; base::OnceCallback<void(scoped_refptr<RemoteDevice>)> cb) override;
scoped_refptr<RemoteDevice> GetDeviceSync( scoped_refptr<RemoteDevice> GetDeviceSync(
const bluetooth_v2_shlib::Addr& addr) override; const bluetooth_v2_shlib::Addr& addr) override;
void GetConnectedDevices(GetConnectDevicesCallback cb) override;
void GetNumConnected(base::OnceCallback<void(size_t)> cb) const override; void GetNumConnected(base::OnceCallback<void(size_t)> cb) const override;
void NotifyConnect(const bluetooth_v2_shlib::Addr& addr) override; void NotifyConnect(const bluetooth_v2_shlib::Addr& addr) override;
scoped_refptr<base::SingleThreadTaskRunner> task_runner() override; scoped_refptr<base::SingleThreadTaskRunner> task_runner() override;
......
...@@ -214,10 +214,12 @@ TEST_F(GattClientManagerTest, RemoteDeviceConnect) { ...@@ -214,10 +214,12 @@ TEST_F(GattClientManagerTest, RemoteDeviceConnect) {
EXPECT_TRUE(device->IsConnected()); EXPECT_TRUE(device->IsConnected());
base::MockCallback<base::OnceCallback<void(size_t)>> base::MockCallback<
get_num_connected_callback; base::OnceCallback<void(std::vector<scoped_refptr<RemoteDevice>>)>>
EXPECT_CALL(get_num_connected_callback, Run(1)); get_connected_callback;
gatt_client_manager_->GetNumConnected(get_num_connected_callback.Get()); const std::vector<scoped_refptr<RemoteDevice>> kExpectedDevices({device});
EXPECT_CALL(get_connected_callback, Run(kExpectedDevices));
gatt_client_manager_->GetConnectedDevices(get_connected_callback.Get());
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef CHROMECAST_DEVICE_BLUETOOTH_LE_MOCK_GATT_CLIENT_MANAGER_H_ #ifndef CHROMECAST_DEVICE_BLUETOOTH_LE_MOCK_GATT_CLIENT_MANAGER_H_
#define CHROMECAST_DEVICE_BLUETOOTH_LE_MOCK_GATT_CLIENT_MANAGER_H_ #define CHROMECAST_DEVICE_BLUETOOTH_LE_MOCK_GATT_CLIENT_MANAGER_H_
#include <vector>
#include "base/observer_list.h" #include "base/observer_list.h"
#include "chromecast/device/bluetooth/le/gatt_client_manager.h" #include "chromecast/device/bluetooth/le/gatt_client_manager.h"
#include "chromecast/device/bluetooth/le/mock_remote_device.h" #include "chromecast/device/bluetooth/le/mock_remote_device.h"
...@@ -29,9 +31,16 @@ class MockGattClientManager : public GattClientManager { ...@@ -29,9 +31,16 @@ class MockGattClientManager : public GattClientManager {
base::OnceCallback<void(scoped_refptr<RemoteDevice>)> cb) override { base::OnceCallback<void(scoped_refptr<RemoteDevice>)> cb) override {
std::move(cb).Run(GetDevice(addr)); std::move(cb).Run(GetDevice(addr));
} }
MOCK_METHOD1( MOCK_METHOD1(
GetDeviceSync, GetDeviceSync,
scoped_refptr<RemoteDevice>(const bluetooth_v2_shlib::Addr& addr)); scoped_refptr<RemoteDevice>(const bluetooth_v2_shlib::Addr& addr));
MOCK_METHOD0(GetConnectedDevices, std::vector<scoped_refptr<RemoteDevice>>());
void GetConnectedDevices(GetConnectDevicesCallback cb) {
std::move(cb).Run(GetConnectedDevices());
}
MOCK_CONST_METHOD1(GetNumConnected, MOCK_CONST_METHOD1(GetNumConnected,
void(base::OnceCallback<void(size_t)> cb)); void(base::OnceCallback<void(size_t)> cb));
MOCK_METHOD1(NotifyConnect, void(const bluetooth_v2_shlib::Addr& addr)); MOCK_METHOD1(NotifyConnect, void(const bluetooth_v2_shlib::Addr& addr));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment