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 |
7 namespace bluetooth { | 9 namespace bluetooth { |
8 | 10 |
9 FakePeripheral::FakePeripheral(FakeCentral* fake_central, | 11 FakePeripheral::FakePeripheral(FakeCentral* fake_central, |
10 const std::string& address) | 12 const std::string& address) |
11 : device::BluetoothDevice(fake_central), | 13 : device::BluetoothDevice(fake_central), |
12 address_(address), | 14 address_(address), |
13 gatt_connected_(false) {} | 15 system_connected_(false), |
| 16 gatt_connected_(false), |
| 17 weak_ptr_factory_(this) {} |
14 | 18 |
15 FakePeripheral::~FakePeripheral() {} | 19 FakePeripheral::~FakePeripheral() {} |
16 | 20 |
17 void FakePeripheral::SetName(base::Optional<std::string> name) { | 21 void FakePeripheral::SetName(base::Optional<std::string> name) { |
18 name_ = std::move(name); | 22 name_ = std::move(name); |
19 } | 23 } |
20 | 24 |
21 void FakePeripheral::SetGattConnected(bool connected) { | 25 void FakePeripheral::SetSystemConnected(bool connected) { |
22 gatt_connected_ = connected; | 26 system_connected_ = connected; |
23 } | 27 } |
24 | 28 |
25 void FakePeripheral::SetServiceUUIDs(UUIDSet service_uuids) { | 29 void FakePeripheral::SetServiceUUIDs(UUIDSet service_uuids) { |
26 service_uuids_ = std::move(service_uuids); | 30 service_uuids_ = std::move(service_uuids); |
27 } | 31 } |
28 | 32 |
| 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 |
29 uint32_t FakePeripheral::GetBluetoothClass() const { | 39 uint32_t FakePeripheral::GetBluetoothClass() const { |
30 NOTREACHED(); | 40 NOTREACHED(); |
31 return 0; | 41 return 0; |
32 } | 42 } |
33 | 43 |
34 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 44 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
35 device::BluetoothTransport FakePeripheral::GetType() const { | 45 device::BluetoothTransport FakePeripheral::GetType() const { |
36 NOTREACHED(); | 46 NOTREACHED(); |
37 return device::BLUETOOTH_TRANSPORT_INVALID; | 47 return device::BLUETOOTH_TRANSPORT_INVALID; |
38 } | 48 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 NOTREACHED(); | 95 NOTREACHED(); |
86 return false; | 96 return false; |
87 } | 97 } |
88 | 98 |
89 bool FakePeripheral::IsConnected() const { | 99 bool FakePeripheral::IsConnected() const { |
90 NOTREACHED(); | 100 NOTREACHED(); |
91 return false; | 101 return false; |
92 } | 102 } |
93 | 103 |
94 bool FakePeripheral::IsGattConnected() const { | 104 bool FakePeripheral::IsGattConnected() const { |
95 return gatt_connected_; | 105 // TODO(crbug.com/728870): Return gatt_connected_ only once system connected |
| 106 // peripherals are supported and Web Bluetooth uses them. See issue for more |
| 107 // details. |
| 108 return system_connected_ || gatt_connected_; |
96 } | 109 } |
97 | 110 |
98 bool FakePeripheral::IsConnectable() const { | 111 bool FakePeripheral::IsConnectable() const { |
99 NOTREACHED(); | 112 NOTREACHED(); |
100 return false; | 113 return false; |
101 } | 114 } |
102 | 115 |
103 bool FakePeripheral::IsConnecting() const { | 116 bool FakePeripheral::IsConnecting() const { |
104 NOTREACHED(); | 117 NOTREACHED(); |
105 return false; | 118 return false; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 NOTREACHED(); | 190 NOTREACHED(); |
178 } | 191 } |
179 | 192 |
180 void FakePeripheral::ConnectToServiceInsecurely( | 193 void FakePeripheral::ConnectToServiceInsecurely( |
181 const device::BluetoothUUID& uuid, | 194 const device::BluetoothUUID& uuid, |
182 const ConnectToServiceCallback& callback, | 195 const ConnectToServiceCallback& callback, |
183 const ConnectToServiceErrorCallback& error_callback) { | 196 const ConnectToServiceErrorCallback& error_callback) { |
184 NOTREACHED(); | 197 NOTREACHED(); |
185 } | 198 } |
186 | 199 |
| 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 |
187 void FakePeripheral::CreateGattConnectionImpl() { | 214 void FakePeripheral::CreateGattConnectionImpl() { |
188 NOTREACHED(); | 215 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 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 } |
189 } | 234 } |
190 | 235 |
191 void FakePeripheral::DisconnectGatt() { | 236 void FakePeripheral::DisconnectGatt() { |
192 NOTREACHED(); | |
193 } | 237 } |
| 238 |
194 } // namespace bluetooth | 239 } // namespace bluetooth |
OLD | NEW |