Index: device/bluetooth/test/fake_peripheral.cc |
diff --git a/device/bluetooth/test/fake_peripheral.cc b/device/bluetooth/test/fake_peripheral.cc |
index acf93c736d8f69ce17be05c4cf339d7072f23f9d..335ff13f89913dc03369fe439a3956995cc9103f 100644 |
--- a/device/bluetooth/test/fake_peripheral.cc |
+++ b/device/bluetooth/test/fake_peripheral.cc |
@@ -4,13 +4,17 @@ |
#include "device/bluetooth/test/fake_peripheral.h" |
+#include "base/memory/weak_ptr.h" |
+ |
namespace bluetooth { |
FakePeripheral::FakePeripheral(FakeCentral* fake_central, |
const std::string& address) |
: device::BluetoothDevice(fake_central), |
address_(address), |
- gatt_connected_(false) {} |
+ system_connected_(false), |
+ gatt_connected_(false), |
+ weak_ptr_factory_(this) {} |
FakePeripheral::~FakePeripheral() {} |
@@ -18,14 +22,20 @@ void FakePeripheral::SetName(base::Optional<std::string> name) { |
name_ = std::move(name); |
} |
-void FakePeripheral::SetGattConnected(bool connected) { |
- gatt_connected_ = connected; |
+void FakePeripheral::SetSystemConnected(bool connected) { |
+ system_connected_ = connected; |
} |
void FakePeripheral::SetServiceUUIDs(UUIDSet service_uuids) { |
service_uuids_ = std::move(service_uuids); |
} |
+void FakePeripheral::SetNextGATTConnectionResponse(uint16_t code) { |
+ DCHECK(!next_connection_response_); |
+ DCHECK(create_gatt_connection_error_callbacks_.empty()); |
+ next_connection_response_ = code; |
+} |
+ |
uint32_t FakePeripheral::GetBluetoothClass() const { |
NOTREACHED(); |
return 0; |
@@ -92,7 +102,7 @@ bool FakePeripheral::IsConnected() const { |
} |
bool FakePeripheral::IsGattConnected() const { |
- return gatt_connected_; |
+ return system_connected_ || gatt_connected_; |
scheib
2017/05/30 20:54:13
Our Android code at least doesn't work this way. W
ortuno
2017/05/31 05:47:27
Unsure what you mean by Android doesn't work this
scheib
2017/05/31 17:22:21
OK. We discussed this some earlier. I think this i
scheib
2017/06/01 05:03:29
We've just had a VC chat. I understand more contex
|
} |
bool FakePeripheral::IsConnectable() const { |
@@ -184,11 +194,43 @@ void FakePeripheral::ConnectToServiceInsecurely( |
NOTREACHED(); |
} |
+void FakePeripheral::CreateGattConnection( |
+ const GattConnectionCallback& callback, |
+ const ConnectErrorCallback& error_callback) { |
+ create_gatt_connection_success_callbacks_.push_back(callback); |
+ create_gatt_connection_error_callbacks_.push_back(error_callback); |
+ |
+ // TODO(crbug.com/630581): Use BluetoothDevice::CreateGattConnection when |
+ // system connected devices are supported. |
+ if (gatt_connected_) |
+ return DidConnectGatt(); |
+ |
+ CreateGattConnectionImpl(); |
+} |
+ |
void FakePeripheral::CreateGattConnectionImpl() { |
- NOTREACHED(); |
+ base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, base::Bind(&FakePeripheral::DispatchConnectionResponse, |
+ weak_ptr_factory_.GetWeakPtr())); |
+} |
+ |
+void FakePeripheral::DispatchConnectionResponse() { |
+ DCHECK(!!next_connection_response_); |
+ |
+ uint16_t code = next_connection_response_.value(); |
+ next_connection_response_.reset(); |
+ |
+ if (code == mojom::kHCISuccess) { |
+ gatt_connected_ = true; |
+ DidConnectGatt(); |
+ } else if (code == mojom::kHCIConnectionTimeout) { |
+ DidFailToConnectGatt(ERROR_FAILED); |
+ } else { |
+ DidFailToConnectGatt(ERROR_UNKNOWN); |
+ } |
} |
void FakePeripheral::DisconnectGatt() { |
- NOTREACHED(); |
} |
+ |
} // namespace bluetooth |