Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Unified Diff: device/bluetooth/test/fake_peripheral.cc

Issue 2874873003: bluetooth: Implement simulateGATTConnectionResponse() (Closed)
Patch Set: Improve comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..eb891240dad8e7fd5463b2f6fbd2f3f6325ff8e3 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,10 @@ bool FakePeripheral::IsConnected() const {
}
bool FakePeripheral::IsGattConnected() const {
- return gatt_connected_;
+ // TODO(crbug.com/728870): Return gatt_connected_ only once system connected
+ // peripherals are supported and Web Bluetooth uses them. See issue for more
+ // details.
+ return system_connected_ || gatt_connected_;
}
bool FakePeripheral::IsConnectable() const {
@@ -184,11 +197,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/728870): Stop overriding CreateGattConnection once
+ // IsGattConnected() is fixed. See issue for more details.
+ 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_);
dcheng 2017/06/02 16:07:35 Random: To me, !! reads a bit odd, since it isn't
ortuno 2017/06/05 01:36:09 Done.
+
+ 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

Powered by Google App Engine
This is Rietveld 408576698