| 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/run_loop.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "device/bluetooth/bluetooth_adapter_win.h" | 10 #include "device/bluetooth/bluetooth_adapter_win.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } | 97 } |
| 98 | 98 |
| 99 void BluetoothTestWin::InitWithFakeAdapter() { | 99 void BluetoothTestWin::InitWithFakeAdapter() { |
| 100 fake_bt_classic_wrapper_ = new win::BluetoothClassicWrapperFake(); | 100 fake_bt_classic_wrapper_ = new win::BluetoothClassicWrapperFake(); |
| 101 fake_bt_le_wrapper_ = new win::BluetoothLowEnergyWrapperFake(); | 101 fake_bt_le_wrapper_ = new win::BluetoothLowEnergyWrapperFake(); |
| 102 win::BluetoothClassicWrapper::SetInstanceForTest(fake_bt_classic_wrapper_); | 102 win::BluetoothClassicWrapper::SetInstanceForTest(fake_bt_classic_wrapper_); |
| 103 win::BluetoothLowEnergyWrapper::SetInstanceForTest(fake_bt_le_wrapper_); | 103 win::BluetoothLowEnergyWrapper::SetInstanceForTest(fake_bt_le_wrapper_); |
| 104 fake_bt_classic_wrapper_->SimulateARadio( | 104 fake_bt_classic_wrapper_->SimulateARadio( |
| 105 base::SysUTF8ToWide(kTestAdapterName), | 105 base::SysUTF8ToWide(kTestAdapterName), |
| 106 CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress)); | 106 CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress)); |
| 107 fake_bt_le_wrapper_->AddObserver(this); |
| 107 | 108 |
| 108 adapter_ = new BluetoothAdapterWin(base::Bind( | 109 adapter_ = new BluetoothAdapterWin(base::Bind( |
| 109 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); | 110 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); |
| 110 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); | 111 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); |
| 111 adapter_win_->InitForTest(ui_task_runner_, bluetooth_task_runner_); | |
| 112 adapter_win_->InitForTest(nullptr, bluetooth_task_runner_); | 112 adapter_win_->InitForTest(nullptr, bluetooth_task_runner_); |
| 113 adapter_win_->GetWinBluetoothTaskManager()->AddObserver(this); |
| 113 FinishPendingTasks(); | 114 FinishPendingTasks(); |
| 114 } | 115 } |
| 115 | 116 |
| 116 bool BluetoothTestWin::DenyPermission() { | 117 bool BluetoothTestWin::DenyPermission() { |
| 117 return false; | 118 return false; |
| 118 } | 119 } |
| 119 | 120 |
| 120 void BluetoothTestWin::StartLowEnergyDiscoverySession() { | 121 void BluetoothTestWin::StartLowEnergyDiscoverySession() { |
| 121 __super ::StartLowEnergyDiscoverySession(); | 122 __super ::StartLowEnergyDiscoverySession(); |
| 122 FinishPendingTasks(); | 123 FinishPendingTasks(); |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 | 277 |
| 277 std::string characteristic_att_handle = std::to_string( | 278 std::string characteristic_att_handle = std::to_string( |
| 278 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic) | 279 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic) |
| 279 ->GetAttributeHandle()); | 280 ->GetAttributeHandle()); |
| 280 fake_bt_le_wrapper_->SimulateGattCharacteriscRemove( | 281 fake_bt_le_wrapper_->SimulateGattCharacteriscRemove( |
| 281 target_service, characteristic_att_handle); | 282 target_service, characteristic_att_handle); |
| 282 | 283 |
| 283 ForceRefreshDevice(); | 284 ForceRefreshDevice(); |
| 284 } | 285 } |
| 285 | 286 |
| 287 void BluetoothTestWin::RememberCharacteristicForSubsequentAction( |
| 288 BluetoothGattCharacteristic* characteristic) { |
| 289 remembered_characteristic_ = |
| 290 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic); |
| 291 } |
| 292 |
| 293 void BluetoothTestWin::SimulateGattCharacteristicRead( |
| 294 BluetoothGattCharacteristic* characteristic, |
| 295 const std::vector<uint8_t>& value) { |
| 296 BluetoothGattCharacteristic* target_characteristic = characteristic; |
| 297 if (target_characteristic == nullptr) |
| 298 target_characteristic = remembered_characteristic_; |
| 299 CHECK(target_characteristic); |
| 300 |
| 301 win::GattCharacteristic* target_simulated_characteristic = |
| 302 GetSimulatedCharacteristic(target_characteristic); |
| 303 if (target_simulated_characteristic != nullptr) { |
| 304 fake_bt_le_wrapper_->SimulateGattCharacteristicValue( |
| 305 target_simulated_characteristic, value); |
| 306 } |
| 307 |
| 308 FinishPendingTasks(); |
| 309 } |
| 310 |
| 311 void BluetoothTestWin::SimulateGattCharacteristicReadError( |
| 312 BluetoothGattCharacteristic* characteristic, |
| 313 BluetoothGattService::GattErrorCode error_code) { |
| 314 win::GattCharacteristic* target_characteristic = |
| 315 GetSimulatedCharacteristic(characteristic); |
| 316 CHECK(target_characteristic); |
| 317 HRESULT hr = ERROR_SEM_TIMEOUT; |
| 318 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) |
| 319 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; |
| 320 fake_bt_le_wrapper_->SimulateGattCharacteristicReadError( |
| 321 target_characteristic, hr); |
| 322 |
| 323 FinishPendingTasks(); |
| 324 } |
| 325 |
| 326 void BluetoothTestWin::SimulateGattCharacteristicWrite( |
| 327 BluetoothGattCharacteristic* characteristic) { |
| 328 FinishPendingTasks(); |
| 329 } |
| 330 |
| 331 void BluetoothTestWin::SimulateGattCharacteristicWriteError( |
| 332 BluetoothGattCharacteristic* characteristic, |
| 333 BluetoothGattService::GattErrorCode error_code) { |
| 334 win::GattCharacteristic* target_characteristic = |
| 335 GetSimulatedCharacteristic(characteristic); |
| 336 CHECK(target_characteristic); |
| 337 HRESULT hr = ERROR_SEM_TIMEOUT; |
| 338 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) |
| 339 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; |
| 340 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError( |
| 341 target_characteristic, hr); |
| 342 |
| 343 FinishPendingTasks(); |
| 344 } |
| 345 |
| 346 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) { |
| 347 CHECK(device); |
| 348 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress()); |
| 349 } |
| 350 |
| 286 void BluetoothTestWin::SimulateGattDescriptor( | 351 void BluetoothTestWin::SimulateGattDescriptor( |
| 287 BluetoothGattCharacteristic* characteristic, | 352 BluetoothGattCharacteristic* characteristic, |
| 288 const std::string& uuid) { | 353 const std::string& uuid) { |
| 289 win::GattCharacteristic* target_characteristic = | 354 win::GattCharacteristic* target_characteristic = |
| 290 GetSimulatedCharacteristic(characteristic); | 355 GetSimulatedCharacteristic(characteristic); |
| 291 CHECK(target_characteristic); | 356 CHECK(target_characteristic); |
| 292 fake_bt_le_wrapper_->SimulateGattDescriptor( | 357 fake_bt_le_wrapper_->SimulateGattDescriptor( |
| 293 characteristic->GetService()->GetDevice()->GetAddress(), | 358 characteristic->GetService()->GetDevice()->GetAddress(), |
| 294 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); | 359 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); |
| 295 ForceRefreshDevice(); | 360 ForceRefreshDevice(); |
| 296 } | 361 } |
| 297 | 362 |
| 363 void BluetoothTestWin::OnBluetoothAttemptReadGattCharacteristic() { |
| 364 gatt_read_characteristic_attempts_++; |
| 365 } |
| 366 |
| 367 void BluetoothTestWin::OnBluetoothAttemptWriteGattCharacteristic() { |
| 368 gatt_write_characteristic_attempts_++; |
| 369 } |
| 370 |
| 371 void BluetoothTestWin::onWriteGattCharacteristicValue( |
| 372 PBTH_LE_GATT_CHARACTERISTIC_VALUE value) { |
| 373 last_write_value_.clear(); |
| 374 for (ULONG i = 0; i < value->DataSize; i++) |
| 375 last_write_value_.push_back(value->Data[i]); |
| 376 } |
| 377 |
| 298 win::GattService* BluetoothTestWin::GetSimulatedService( | 378 win::GattService* BluetoothTestWin::GetSimulatedService( |
| 299 win::BLEDevice* device, | 379 win::BLEDevice* device, |
| 300 BluetoothGattService* service) { | 380 BluetoothGattService* service) { |
| 301 CHECK(device); | 381 CHECK(device); |
| 302 CHECK(service); | 382 CHECK(service); |
| 303 | 383 |
| 304 std::vector<std::string> chain_of_att_handles; | 384 std::vector<std::string> chain_of_att_handles; |
| 305 BluetoothRemoteGattServiceWin* win_service = | 385 BluetoothRemoteGattServiceWin* win_service = |
| 306 static_cast<BluetoothRemoteGattServiceWin*>(service); | 386 static_cast<BluetoothRemoteGattServiceWin*>(service); |
| 307 chain_of_att_handles.insert( | 387 chain_of_att_handles.insert( |
| (...skipping 29 matching lines...) Expand all Loading... |
| 337 void BluetoothTestWin::ForceRefreshDevice() { | 417 void BluetoothTestWin::ForceRefreshDevice() { |
| 338 adapter_win_->force_update_device_for_test_ = true; | 418 adapter_win_->force_update_device_for_test_ = true; |
| 339 FinishPendingTasks(); | 419 FinishPendingTasks(); |
| 340 } | 420 } |
| 341 | 421 |
| 342 void BluetoothTestWin::FinishPendingTasks() { | 422 void BluetoothTestWin::FinishPendingTasks() { |
| 343 bluetooth_task_runner_->RunPendingTasks(); | 423 bluetooth_task_runner_->RunPendingTasks(); |
| 344 base::RunLoop().RunUntilIdle(); | 424 base::RunLoop().RunUntilIdle(); |
| 345 } | 425 } |
| 346 } | 426 } |
| OLD | NEW |