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

Side by Side Diff: device/bluetooth/test/fake_peripheral.cc

Issue 2874873003: bluetooth: Implement simulateGATTConnectionResponse() (Closed)
Patch Set: clean up Created 3 years, 6 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 unified diff | Download patch
OLDNEW
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
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 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
96 } 106 }
97 107
98 bool FakePeripheral::IsConnectable() const { 108 bool FakePeripheral::IsConnectable() const {
99 NOTREACHED(); 109 NOTREACHED();
100 return false; 110 return false;
101 } 111 }
102 112
103 bool FakePeripheral::IsConnecting() const { 113 bool FakePeripheral::IsConnecting() const {
104 NOTREACHED(); 114 NOTREACHED();
105 return false; 115 return false;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 NOTREACHED(); 187 NOTREACHED();
178 } 188 }
179 189
180 void FakePeripheral::ConnectToServiceInsecurely( 190 void FakePeripheral::ConnectToServiceInsecurely(
181 const device::BluetoothUUID& uuid, 191 const device::BluetoothUUID& uuid,
182 const ConnectToServiceCallback& callback, 192 const ConnectToServiceCallback& callback,
183 const ConnectToServiceErrorCallback& error_callback) { 193 const ConnectToServiceErrorCallback& error_callback) {
184 NOTREACHED(); 194 NOTREACHED();
185 } 195 }
186 196
197 void FakePeripheral::CreateGattConnection(
198 const GattConnectionCallback& callback,
199 const ConnectErrorCallback& error_callback) {
200 create_gatt_connection_success_callbacks_.push_back(callback);
201 create_gatt_connection_error_callbacks_.push_back(error_callback);
202
203 // TODO(crbug.com/630581): Use BluetoothDevice::CreateGattConnection when
204 // system connected devices are supported.
205 if (gatt_connected_)
206 return DidConnectGatt();
207
208 CreateGattConnectionImpl();
209 }
210
187 void FakePeripheral::CreateGattConnectionImpl() { 211 void FakePeripheral::CreateGattConnectionImpl() {
188 NOTREACHED(); 212 base::ThreadTaskRunnerHandle::Get()->PostTask(
213 FROM_HERE, base::Bind(&FakePeripheral::DispatchConnectionResponse,
214 weak_ptr_factory_.GetWeakPtr()));
215 }
216
217 void FakePeripheral::DispatchConnectionResponse() {
218 DCHECK(!!next_connection_response_);
219
220 uint16_t code = next_connection_response_.value();
221 next_connection_response_.reset();
222
223 if (code == mojom::kHCISuccess) {
224 gatt_connected_ = true;
225 DidConnectGatt();
226 } else if (code == mojom::kHCIConnectionTimeout) {
227 DidFailToConnectGatt(ERROR_FAILED);
228 } else {
229 DidFailToConnectGatt(ERROR_UNKNOWN);
230 }
189 } 231 }
190 232
191 void FakePeripheral::DisconnectGatt() { 233 void FakePeripheral::DisconnectGatt() {
192 NOTREACHED();
193 } 234 }
235
194 } // namespace bluetooth 236 } // namespace bluetooth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698