| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/bluetooth/test/fake_peripheral.h" | 5 #include "device/bluetooth/test/fake_peripheral.h" |
| 6 | 6 |
| 7 #include "base/memory/weak_ptr.h" | |
| 8 | |
| 9 namespace bluetooth { | 7 namespace bluetooth { |
| 10 | 8 |
| 11 FakePeripheral::FakePeripheral(FakeCentral* fake_central, | 9 FakePeripheral::FakePeripheral(FakeCentral* fake_central, |
| 12 const std::string& address) | 10 const std::string& address) |
| 13 : device::BluetoothDevice(fake_central), | 11 : device::BluetoothDevice(fake_central), |
| 14 address_(address), | 12 address_(address), |
| 15 system_connected_(false), | 13 gatt_connected_(false) {} |
| 16 gatt_connected_(false), | |
| 17 weak_ptr_factory_(this) {} | |
| 18 | 14 |
| 19 FakePeripheral::~FakePeripheral() {} | 15 FakePeripheral::~FakePeripheral() {} |
| 20 | 16 |
| 21 void FakePeripheral::SetName(base::Optional<std::string> name) { | 17 void FakePeripheral::SetName(base::Optional<std::string> name) { |
| 22 name_ = std::move(name); | 18 name_ = std::move(name); |
| 23 } | 19 } |
| 24 | 20 |
| 25 void FakePeripheral::SetSystemConnected(bool connected) { | 21 void FakePeripheral::SetGattConnected(bool connected) { |
| 26 system_connected_ = connected; | 22 gatt_connected_ = connected; |
| 27 } | 23 } |
| 28 | 24 |
| 29 void FakePeripheral::SetServiceUUIDs(UUIDSet service_uuids) { | 25 void FakePeripheral::SetServiceUUIDs(UUIDSet service_uuids) { |
| 30 service_uuids_ = std::move(service_uuids); | 26 service_uuids_ = std::move(service_uuids); |
| 31 } | 27 } |
| 32 | 28 |
| 33 void FakePeripheral::SetNextGATTConnectionResponse(uint16_t code) { | |
| 34 DCHECK(!next_connection_response_); | |
| 35 DCHECK(create_gatt_connection_error_callbacks_.empty()); | |
| 36 next_connection_response_ = code; | |
| 37 } | |
| 38 | |
| 39 uint32_t FakePeripheral::GetBluetoothClass() const { | 29 uint32_t FakePeripheral::GetBluetoothClass() const { |
| 40 NOTREACHED(); | 30 NOTREACHED(); |
| 41 return 0; | 31 return 0; |
| 42 } | 32 } |
| 43 | 33 |
| 44 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 34 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 45 device::BluetoothTransport FakePeripheral::GetType() const { | 35 device::BluetoothTransport FakePeripheral::GetType() const { |
| 46 NOTREACHED(); | 36 NOTREACHED(); |
| 47 return device::BLUETOOTH_TRANSPORT_INVALID; | 37 return device::BLUETOOTH_TRANSPORT_INVALID; |
| 48 } | 38 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 NOTREACHED(); | 85 NOTREACHED(); |
| 96 return false; | 86 return false; |
| 97 } | 87 } |
| 98 | 88 |
| 99 bool FakePeripheral::IsConnected() const { | 89 bool FakePeripheral::IsConnected() const { |
| 100 NOTREACHED(); | 90 NOTREACHED(); |
| 101 return false; | 91 return false; |
| 102 } | 92 } |
| 103 | 93 |
| 104 bool FakePeripheral::IsGattConnected() const { | 94 bool FakePeripheral::IsGattConnected() const { |
| 105 // TODO(crbug.com/728870): Return gatt_connected_ only once system connected | 95 return gatt_connected_; |
| 106 // peripherals are supported and Web Bluetooth uses them. See issue for more | |
| 107 // details. | |
| 108 return system_connected_ || gatt_connected_; | |
| 109 } | 96 } |
| 110 | 97 |
| 111 bool FakePeripheral::IsConnectable() const { | 98 bool FakePeripheral::IsConnectable() const { |
| 112 NOTREACHED(); | 99 NOTREACHED(); |
| 113 return false; | 100 return false; |
| 114 } | 101 } |
| 115 | 102 |
| 116 bool FakePeripheral::IsConnecting() const { | 103 bool FakePeripheral::IsConnecting() const { |
| 117 NOTREACHED(); | 104 NOTREACHED(); |
| 118 return false; | 105 return false; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 NOTREACHED(); | 177 NOTREACHED(); |
| 191 } | 178 } |
| 192 | 179 |
| 193 void FakePeripheral::ConnectToServiceInsecurely( | 180 void FakePeripheral::ConnectToServiceInsecurely( |
| 194 const device::BluetoothUUID& uuid, | 181 const device::BluetoothUUID& uuid, |
| 195 const ConnectToServiceCallback& callback, | 182 const ConnectToServiceCallback& callback, |
| 196 const ConnectToServiceErrorCallback& error_callback) { | 183 const ConnectToServiceErrorCallback& error_callback) { |
| 197 NOTREACHED(); | 184 NOTREACHED(); |
| 198 } | 185 } |
| 199 | 186 |
| 200 void FakePeripheral::CreateGattConnection( | |
| 201 const GattConnectionCallback& callback, | |
| 202 const ConnectErrorCallback& error_callback) { | |
| 203 create_gatt_connection_success_callbacks_.push_back(callback); | |
| 204 create_gatt_connection_error_callbacks_.push_back(error_callback); | |
| 205 | |
| 206 // TODO(crbug.com/728870): Stop overriding CreateGattConnection once | |
| 207 // IsGattConnected() is fixed. See issue for more details. | |
| 208 if (gatt_connected_) | |
| 209 return DidConnectGatt(); | |
| 210 | |
| 211 CreateGattConnectionImpl(); | |
| 212 } | |
| 213 | |
| 214 void FakePeripheral::CreateGattConnectionImpl() { | 187 void FakePeripheral::CreateGattConnectionImpl() { |
| 215 base::ThreadTaskRunnerHandle::Get()->PostTask( | 188 NOTREACHED(); |
| 216 FROM_HERE, base::Bind(&FakePeripheral::DispatchConnectionResponse, | |
| 217 weak_ptr_factory_.GetWeakPtr())); | |
| 218 } | |
| 219 | |
| 220 void FakePeripheral::DispatchConnectionResponse() { | |
| 221 DCHECK(next_connection_response_); | |
| 222 | |
| 223 uint16_t code = next_connection_response_.value(); | |
| 224 next_connection_response_.reset(); | |
| 225 | |
| 226 if (code == mojom::kHCISuccess) { | |
| 227 gatt_connected_ = true; | |
| 228 DidConnectGatt(); | |
| 229 } else if (code == mojom::kHCIConnectionTimeout) { | |
| 230 DidFailToConnectGatt(ERROR_FAILED); | |
| 231 } else { | |
| 232 DidFailToConnectGatt(ERROR_UNKNOWN); | |
| 233 } | |
| 234 } | 189 } |
| 235 | 190 |
| 236 void FakePeripheral::DisconnectGatt() { | 191 void FakePeripheral::DisconnectGatt() { |
| 192 NOTREACHED(); |
| 237 } | 193 } |
| 238 | |
| 239 } // namespace bluetooth | 194 } // namespace bluetooth |
| OLD | NEW |