OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/bluetooth_test_win.h" | 5 #include "device/bluetooth/test/bluetooth_test_win.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/run_loop.h" |
8 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
9 #include "device/bluetooth/bluetooth_adapter_win.h" | 10 #include "device/bluetooth/bluetooth_adapter_win.h" |
10 #include "device/bluetooth/bluetooth_low_energy_win.h" | 11 #include "device/bluetooth/bluetooth_low_energy_win.h" |
11 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" | 12 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" |
12 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" | 13 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( | 17 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( |
17 std::string device_address) { | 18 std::string device_address) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 fake_bt_le_wrapper_ = new win::BluetoothLowEnergyWrapperFake(); | 101 fake_bt_le_wrapper_ = new win::BluetoothLowEnergyWrapperFake(); |
101 win::BluetoothClassicWrapper::SetInstanceForTest(fake_bt_classic_wrapper_); | 102 win::BluetoothClassicWrapper::SetInstanceForTest(fake_bt_classic_wrapper_); |
102 win::BluetoothLowEnergyWrapper::SetInstanceForTest(fake_bt_le_wrapper_); | 103 win::BluetoothLowEnergyWrapper::SetInstanceForTest(fake_bt_le_wrapper_); |
103 fake_bt_classic_wrapper_->SimulateARadio( | 104 fake_bt_classic_wrapper_->SimulateARadio( |
104 base::SysUTF8ToWide(kTestAdapterName), | 105 base::SysUTF8ToWide(kTestAdapterName), |
105 CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress)); | 106 CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress)); |
106 | 107 |
107 adapter_ = new BluetoothAdapterWin(base::Bind( | 108 adapter_ = new BluetoothAdapterWin(base::Bind( |
108 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); | 109 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); |
109 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); | 110 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); |
110 adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); | 111 adapter_win_->InitForTest(nullptr, bluetooth_task_runner_); |
111 bluetooth_task_runner_->RunPendingTasks(); | 112 FinishPendingTasks(); |
112 ui_task_runner_->RunPendingTasks(); | |
113 } | 113 } |
114 | 114 |
115 bool BluetoothTestWin::DenyPermission() { | 115 bool BluetoothTestWin::DenyPermission() { |
116 return false; | 116 return false; |
117 } | 117 } |
118 | 118 |
119 void BluetoothTestWin::StartLowEnergyDiscoverySession() { | 119 void BluetoothTestWin::StartLowEnergyDiscoverySession() { |
120 __super ::StartLowEnergyDiscoverySession(); | 120 __super ::StartLowEnergyDiscoverySession(); |
121 bluetooth_task_runner_->RunPendingTasks(); | 121 FinishPendingTasks(); |
122 ui_task_runner_->RunPendingTasks(); | |
123 } | 122 } |
124 | 123 |
125 BluetoothDevice* BluetoothTestWin::DiscoverLowEnergyDevice(int device_ordinal) { | 124 BluetoothDevice* BluetoothTestWin::DiscoverLowEnergyDevice(int device_ordinal) { |
126 if (device_ordinal > 4 || device_ordinal < 1) | 125 if (device_ordinal > 4 || device_ordinal < 1) |
127 return nullptr; | 126 return nullptr; |
128 | 127 |
129 std::string device_name = kTestDeviceName; | 128 std::string device_name = kTestDeviceName; |
130 std::string device_address = kTestDeviceAddress1; | 129 std::string device_address = kTestDeviceAddress1; |
131 std::string service_uuid_1; | 130 std::string service_uuid_1; |
132 std::string service_uuid_2; | 131 std::string service_uuid_2; |
(...skipping 13 matching lines...) Expand all Loading... |
146 case 4: { | 145 case 4: { |
147 device_name = kTestDeviceNameEmpty; | 146 device_name = kTestDeviceNameEmpty; |
148 device_address = kTestDeviceAddress2; | 147 device_address = kTestDeviceAddress2; |
149 } break; | 148 } break; |
150 } | 149 } |
151 | 150 |
152 win::BLEDevice* simulated_device = fake_bt_le_wrapper_->SimulateBLEDevice( | 151 win::BLEDevice* simulated_device = fake_bt_le_wrapper_->SimulateBLEDevice( |
153 device_name, CanonicalStringToBLUETOOTH_ADDRESS(device_address)); | 152 device_name, CanonicalStringToBLUETOOTH_ADDRESS(device_address)); |
154 if (simulated_device != nullptr) { | 153 if (simulated_device != nullptr) { |
155 if (!service_uuid_1.empty()) { | 154 if (!service_uuid_1.empty()) { |
156 fake_bt_le_wrapper_->SimulateBLEGattService( | 155 fake_bt_le_wrapper_->SimulateGattService( |
157 simulated_device, nullptr, | 156 simulated_device, nullptr, |
158 CanonicalStringToBTH_LE_UUID(service_uuid_1)); | 157 CanonicalStringToBTH_LE_UUID(service_uuid_1)); |
159 } | 158 } |
160 if (!service_uuid_2.empty()) { | 159 if (!service_uuid_2.empty()) { |
161 fake_bt_le_wrapper_->SimulateBLEGattService( | 160 fake_bt_le_wrapper_->SimulateGattService( |
162 simulated_device, nullptr, | 161 simulated_device, nullptr, |
163 CanonicalStringToBTH_LE_UUID(service_uuid_2)); | 162 CanonicalStringToBTH_LE_UUID(service_uuid_2)); |
164 } | 163 } |
165 } | 164 } |
166 bluetooth_task_runner_->RunPendingTasks(); | 165 FinishPendingTasks(); |
167 ui_task_runner_->RunPendingTasks(); | |
168 | 166 |
169 std::vector<BluetoothDevice*> devices = adapter_win_->GetDevices(); | 167 std::vector<BluetoothDevice*> devices = adapter_win_->GetDevices(); |
170 for (auto device : devices) { | 168 for (auto device : devices) { |
171 if (device->GetAddress() == device_address) | 169 if (device->GetAddress() == device_address) |
172 return device; | 170 return device; |
173 } | 171 } |
174 | 172 |
175 return nullptr; | 173 return nullptr; |
176 } | 174 } |
177 | 175 |
178 void BluetoothTestWin::SimulateGattConnection(BluetoothDevice* device) { | 176 void BluetoothTestWin::SimulateGattConnection(BluetoothDevice* device) { |
179 bluetooth_task_runner_->RunPendingTasks(); | 177 FinishPendingTasks(); |
180 ui_task_runner_->RunPendingTasks(); | |
181 | 178 |
182 // Clear records caused by CreateGattConnection since we do not support it on | 179 // Clear records caused by CreateGattConnection since we do not support it on |
183 // Windows. | 180 // Windows. |
184 gatt_discovery_attempts_++; | 181 gatt_discovery_attempts_++; |
185 expected_success_callback_calls_--; | 182 expected_success_callback_calls_--; |
186 unexpected_error_callback_ = false; | 183 unexpected_error_callback_ = false; |
187 } | 184 } |
188 | 185 |
189 void BluetoothTestWin::SimulateGattServicesDiscovered( | 186 void BluetoothTestWin::SimulateGattServicesDiscovered( |
190 BluetoothDevice* device, | 187 BluetoothDevice* device, |
191 const std::vector<std::string>& uuids) { | 188 const std::vector<std::string>& uuids) { |
192 win::BLEDevice* simulated_device = | 189 win::BLEDevice* simulated_device = |
193 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device->GetAddress()); | 190 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device->GetAddress()); |
194 CHECK(simulated_device); | 191 CHECK(simulated_device); |
195 | 192 |
196 for (auto uuid : uuids) { | 193 for (auto uuid : uuids) { |
197 fake_bt_le_wrapper_->SimulateBLEGattService( | 194 fake_bt_le_wrapper_->SimulateGattService( |
198 simulated_device, nullptr, CanonicalStringToBTH_LE_UUID(uuid)); | 195 simulated_device, nullptr, CanonicalStringToBTH_LE_UUID(uuid)); |
199 } | 196 } |
200 | 197 |
201 bluetooth_task_runner_->RunPendingTasks(); | 198 FinishPendingTasks(); |
202 ui_task_runner_->RunPendingTasks(); | |
203 } | 199 } |
204 | 200 |
205 void BluetoothTestWin::SimulateGattServiceRemoved( | 201 void BluetoothTestWin::SimulateGattServiceRemoved( |
206 BluetoothGattService* service) { | 202 BluetoothGattService* service) { |
207 std::string device_address = service->GetDevice()->GetAddress(); | 203 std::string device_address = service->GetDevice()->GetAddress(); |
208 win::BLEDevice* target_device = | 204 win::BLEDevice* target_device = |
209 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address); | 205 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address); |
210 CHECK(target_device); | 206 CHECK(target_device); |
211 | 207 |
212 BluetoothRemoteGattServiceWin* win_service = | 208 BluetoothRemoteGattServiceWin* win_service = |
213 static_cast<BluetoothRemoteGattServiceWin*>(service); | 209 static_cast<BluetoothRemoteGattServiceWin*>(service); |
214 std::string service_att_handle = | 210 std::string service_att_handle = |
215 std::to_string(win_service->GetAttributeHandle()); | 211 std::to_string(win_service->GetAttributeHandle()); |
216 fake_bt_le_wrapper_->SimulateBLEGattServiceRemoved(target_device, nullptr, | 212 fake_bt_le_wrapper_->SimulateGattServiceRemoved(target_device, nullptr, |
217 service_att_handle); | 213 service_att_handle); |
218 | 214 |
219 ForceRefreshDevice(); | 215 ForceRefreshDevice(); |
220 } | 216 } |
221 | 217 |
222 void BluetoothTestWin::SimulateGattCharacteristic(BluetoothGattService* service, | 218 void BluetoothTestWin::SimulateGattCharacteristic(BluetoothGattService* service, |
223 const std::string& uuid, | 219 const std::string& uuid, |
224 int properties) { | 220 int properties) { |
225 std::string device_address = service->GetDevice()->GetAddress(); | 221 std::string device_address = service->GetDevice()->GetAddress(); |
226 win::BLEDevice* target_device = | 222 win::BLEDevice* target_device = |
227 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address); | 223 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address); |
228 CHECK(target_device); | 224 CHECK(target_device); |
229 win::BLEGattService* target_service = | 225 win::GattService* target_service = |
230 GetSimulatedService(target_device, service); | 226 GetSimulatedService(target_device, service); |
231 CHECK(target_service); | 227 CHECK(target_service); |
232 | 228 |
233 BTH_LE_GATT_CHARACTERISTIC win_cha_info; | 229 BTH_LE_GATT_CHARACTERISTIC win_characteristic_info; |
234 win_cha_info.CharacteristicUuid = CanonicalStringToBTH_LE_UUID(uuid); | 230 win_characteristic_info.CharacteristicUuid = |
| 231 CanonicalStringToBTH_LE_UUID(uuid); |
| 232 win_characteristic_info.IsBroadcastable = FALSE; |
| 233 win_characteristic_info.IsReadable = FALSE; |
| 234 win_characteristic_info.IsWritableWithoutResponse = FALSE; |
| 235 win_characteristic_info.IsWritable = FALSE; |
| 236 win_characteristic_info.IsNotifiable = FALSE; |
| 237 win_characteristic_info.IsIndicatable = FALSE; |
| 238 win_characteristic_info.IsSignedWritable = FALSE; |
| 239 win_characteristic_info.HasExtendedProperties = FALSE; |
235 if (properties & BluetoothGattCharacteristic::PROPERTY_BROADCAST) | 240 if (properties & BluetoothGattCharacteristic::PROPERTY_BROADCAST) |
236 win_cha_info.IsBroadcastable = TRUE; | 241 win_characteristic_info.IsBroadcastable = TRUE; |
237 if (properties & BluetoothGattCharacteristic::PROPERTY_READ) | 242 if (properties & BluetoothGattCharacteristic::PROPERTY_READ) |
238 win_cha_info.IsReadable = TRUE; | 243 win_characteristic_info.IsReadable = TRUE; |
239 if (properties & BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE) | 244 if (properties & BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE) |
240 win_cha_info.IsWritableWithoutResponse = TRUE; | 245 win_characteristic_info.IsWritableWithoutResponse = TRUE; |
241 if (properties & BluetoothGattCharacteristic::PROPERTY_WRITE) | 246 if (properties & BluetoothGattCharacteristic::PROPERTY_WRITE) |
242 win_cha_info.IsWritable = TRUE; | 247 win_characteristic_info.IsWritable = TRUE; |
243 if (properties & BluetoothGattCharacteristic::PROPERTY_NOTIFY) | 248 if (properties & BluetoothGattCharacteristic::PROPERTY_NOTIFY) |
244 win_cha_info.IsNotifiable = TRUE; | 249 win_characteristic_info.IsNotifiable = TRUE; |
245 if (properties & BluetoothGattCharacteristic::PROPERTY_INDICATE) | 250 if (properties & BluetoothGattCharacteristic::PROPERTY_INDICATE) |
246 win_cha_info.IsIndicatable = TRUE; | 251 win_characteristic_info.IsIndicatable = TRUE; |
247 if (properties & | 252 if (properties & |
248 BluetoothGattCharacteristic::PROPERTY_AUTHENTICATED_SIGNED_WRITES) | 253 BluetoothGattCharacteristic::PROPERTY_AUTHENTICATED_SIGNED_WRITES) { |
249 win_cha_info.IsSignedWritable = TRUE; | 254 win_characteristic_info.IsSignedWritable = TRUE; |
| 255 } |
250 if (properties & BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES) | 256 if (properties & BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES) |
251 win_cha_info.HasExtendedProperties = TRUE; | 257 win_characteristic_info.HasExtendedProperties = TRUE; |
252 fake_bt_le_wrapper_->SimulateBLEGattCharacterisc( | 258 |
253 device_address, target_service, win_cha_info); | 259 fake_bt_le_wrapper_->SimulateGattCharacterisc(device_address, target_service, |
| 260 win_characteristic_info); |
254 | 261 |
255 ForceRefreshDevice(); | 262 ForceRefreshDevice(); |
256 } | 263 } |
257 | 264 |
258 void BluetoothTestWin::SimulateGattCharacteristicRemoved( | 265 void BluetoothTestWin::SimulateGattCharacteristicRemoved( |
259 BluetoothGattService* service, | 266 BluetoothGattService* service, |
260 BluetoothGattCharacteristic* characteristic) { | 267 BluetoothGattCharacteristic* characteristic) { |
261 CHECK(service); | 268 CHECK(service); |
262 CHECK(characteristic); | 269 CHECK(characteristic); |
263 | 270 |
264 std::string device_address = service->GetDevice()->GetAddress(); | 271 std::string device_address = service->GetDevice()->GetAddress(); |
265 win::BLEGattService* target_service = GetSimulatedService( | 272 win::GattService* target_service = GetSimulatedService( |
266 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address), service); | 273 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address), service); |
267 CHECK(target_service); | 274 CHECK(target_service); |
268 | 275 |
269 std::string characteristic_att_handle = std::to_string( | 276 std::string characteristic_att_handle = std::to_string( |
270 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic) | 277 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic) |
271 ->GetAttributeHandle()); | 278 ->GetAttributeHandle()); |
272 fake_bt_le_wrapper_->SimulateBLEGattCharacteriscRemove( | 279 fake_bt_le_wrapper_->SimulateGattCharacteriscRemove( |
273 target_service, characteristic_att_handle); | 280 target_service, characteristic_att_handle); |
274 | 281 |
275 ForceRefreshDevice(); | 282 ForceRefreshDevice(); |
276 } | 283 } |
277 | 284 |
278 win::BLEGattService* BluetoothTestWin::GetSimulatedService( | 285 void BluetoothTestWin::SimulateGattDescriptor( |
| 286 BluetoothGattCharacteristic* characteristic, |
| 287 const std::string& uuid) { |
| 288 win::GattCharacteristic* target_characteristic = |
| 289 GetSimulatedCharacteristic(characteristic); |
| 290 CHECK(target_characteristic); |
| 291 fake_bt_le_wrapper_->SimulateGattDescriptor( |
| 292 characteristic->GetService()->GetDevice()->GetAddress(), |
| 293 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); |
| 294 ForceRefreshDevice(); |
| 295 } |
| 296 |
| 297 win::GattService* BluetoothTestWin::GetSimulatedService( |
279 win::BLEDevice* device, | 298 win::BLEDevice* device, |
280 BluetoothGattService* service) { | 299 BluetoothGattService* service) { |
281 CHECK(device); | 300 CHECK(device); |
282 CHECK(service); | 301 CHECK(service); |
283 | 302 |
284 std::vector<std::string> chain_of_att_handles; | 303 std::vector<std::string> chain_of_att_handles; |
285 BluetoothRemoteGattServiceWin* win_service = | 304 BluetoothRemoteGattServiceWin* win_service = |
286 static_cast<BluetoothRemoteGattServiceWin*>(service); | 305 static_cast<BluetoothRemoteGattServiceWin*>(service); |
287 chain_of_att_handles.insert( | 306 chain_of_att_handles.insert( |
288 chain_of_att_handles.begin(), | 307 chain_of_att_handles.begin(), |
289 std::to_string(win_service->GetAttributeHandle())); | 308 std::to_string(win_service->GetAttributeHandle())); |
290 win::BLEGattService* simulated_service = | 309 win::GattService* simulated_service = |
291 fake_bt_le_wrapper_->GetSimulatedGattService(device, | 310 fake_bt_le_wrapper_->GetSimulatedGattService(device, |
292 chain_of_att_handles); | 311 chain_of_att_handles); |
293 CHECK(simulated_service); | 312 CHECK(simulated_service); |
294 return simulated_service; | 313 return simulated_service; |
295 } | 314 } |
296 | 315 |
| 316 win::GattCharacteristic* BluetoothTestWin::GetSimulatedCharacteristic( |
| 317 BluetoothGattCharacteristic* characteristic) { |
| 318 CHECK(characteristic); |
| 319 BluetoothRemoteGattCharacteristicWin* win_characteristic = |
| 320 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic); |
| 321 |
| 322 std::string device_address = |
| 323 win_characteristic->GetService()->GetDevice()->GetAddress(); |
| 324 win::BLEDevice* target_device = |
| 325 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address); |
| 326 if (target_device == nullptr) |
| 327 return nullptr; |
| 328 win::GattService* target_service = |
| 329 GetSimulatedService(target_device, win_characteristic->GetService()); |
| 330 if (target_service == nullptr) |
| 331 return nullptr; |
| 332 return fake_bt_le_wrapper_->GetSimulatedGattCharacteristic( |
| 333 target_service, std::to_string(win_characteristic->GetAttributeHandle())); |
| 334 } |
| 335 |
297 void BluetoothTestWin::ForceRefreshDevice() { | 336 void BluetoothTestWin::ForceRefreshDevice() { |
298 adapter_win_->force_update_device_for_test_ = true; | 337 adapter_win_->force_update_device_for_test_ = true; |
| 338 FinishPendingTasks(); |
| 339 } |
| 340 |
| 341 void BluetoothTestWin::FinishPendingTasks() { |
299 bluetooth_task_runner_->RunPendingTasks(); | 342 bluetooth_task_runner_->RunPendingTasks(); |
300 ui_task_runner_->RunPendingTasks(); | 343 base::RunLoop().RunUntilIdle(); |
301 } | 344 } |
302 } | 345 } |
OLD | NEW |