| 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/location.h" | 8 #include "base/location.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "base/test/test_pending_task.h" | 11 #include "base/test/test_pending_task.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "device/bluetooth/bluetooth_adapter_win.h" | 13 #include "device/bluetooth/bluetooth_adapter_win.h" |
| 14 #include "device/bluetooth/bluetooth_low_energy_win.h" | 14 #include "device/bluetooth/bluetooth_low_energy_win.h" |
| 15 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" | 15 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h" |
| 16 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_win.h" |
| 16 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" | 17 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( | 21 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( |
| 21 std::string device_address) { | 22 std::string device_address) { |
| 22 BLUETOOTH_ADDRESS win_addr; | 23 BLUETOOTH_ADDRESS win_addr; |
| 23 unsigned int data[6]; | 24 unsigned int data[6]; |
| 24 int result = | 25 int result = |
| 25 sscanf_s(device_address.c_str(), "%02X:%02X:%02X:%02X:%02X:%02X", | 26 sscanf_s(device_address.c_str(), "%02X:%02X:%02X:%02X:%02X:%02X", |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 win::BluetoothClassicWrapper::SetInstanceForTest(fake_bt_classic_wrapper_); | 107 win::BluetoothClassicWrapper::SetInstanceForTest(fake_bt_classic_wrapper_); |
| 107 win::BluetoothLowEnergyWrapper::SetInstanceForTest(fake_bt_le_wrapper_); | 108 win::BluetoothLowEnergyWrapper::SetInstanceForTest(fake_bt_le_wrapper_); |
| 108 fake_bt_classic_wrapper_->SimulateARadio( | 109 fake_bt_classic_wrapper_->SimulateARadio( |
| 109 base::SysUTF8ToWide(kTestAdapterName), | 110 base::SysUTF8ToWide(kTestAdapterName), |
| 110 CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress)); | 111 CanonicalStringToBLUETOOTH_ADDRESS(kTestAdapterAddress)); |
| 111 | 112 |
| 112 adapter_ = new BluetoothAdapterWin(base::Bind( | 113 adapter_ = new BluetoothAdapterWin(base::Bind( |
| 113 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); | 114 &BluetoothTestWin::AdapterInitCallback, base::Unretained(this))); |
| 114 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); | 115 adapter_win_ = static_cast<BluetoothAdapterWin*>(adapter_.get()); |
| 115 adapter_win_->InitForTest(nullptr, bluetooth_task_runner_); | 116 adapter_win_->InitForTest(nullptr, bluetooth_task_runner_); |
| 116 adapter_win_->GetWinBluetoothTaskManager()->AddObserver(this); | |
| 117 FinishPendingTasks(); | 117 FinishPendingTasks(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 bool BluetoothTestWin::DenyPermission() { | 120 bool BluetoothTestWin::DenyPermission() { |
| 121 return false; | 121 return false; |
| 122 } | 122 } |
| 123 | 123 |
| 124 void BluetoothTestWin::StartLowEnergyDiscoverySession() { | 124 void BluetoothTestWin::StartLowEnergyDiscoverySession() { |
| 125 __super ::StartLowEnergyDiscoverySession(); | 125 __super ::StartLowEnergyDiscoverySession(); |
| 126 FinishPendingTasks(); | 126 FinishPendingTasks(); |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 311 |
| 312 RunPendingTasksUntilCallback(); | 312 RunPendingTasksUntilCallback(); |
| 313 } | 313 } |
| 314 | 314 |
| 315 void BluetoothTestWin::SimulateGattCharacteristicReadError( | 315 void BluetoothTestWin::SimulateGattCharacteristicReadError( |
| 316 BluetoothGattCharacteristic* characteristic, | 316 BluetoothGattCharacteristic* characteristic, |
| 317 BluetoothGattService::GattErrorCode error_code) { | 317 BluetoothGattService::GattErrorCode error_code) { |
| 318 win::GattCharacteristic* target_characteristic = | 318 win::GattCharacteristic* target_characteristic = |
| 319 GetSimulatedCharacteristic(characteristic); | 319 GetSimulatedCharacteristic(characteristic); |
| 320 CHECK(target_characteristic); | 320 CHECK(target_characteristic); |
| 321 HRESULT hr = ERROR_SEM_TIMEOUT; | 321 HRESULT hr = HRESULT_FROM_WIN32(ERROR_SEM_TIMEOUT); |
| 322 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) | 322 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) |
| 323 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; | 323 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; |
| 324 fake_bt_le_wrapper_->SimulateGattCharacteristicReadError( | 324 fake_bt_le_wrapper_->SimulateGattCharacteristicReadError( |
| 325 target_characteristic, hr); | 325 target_characteristic, hr); |
| 326 | 326 |
| 327 FinishPendingTasks(); | 327 FinishPendingTasks(); |
| 328 } | 328 } |
| 329 | 329 |
| 330 void BluetoothTestWin::SimulateGattCharacteristicWrite( | 330 void BluetoothTestWin::SimulateGattCharacteristicWrite( |
| 331 BluetoothGattCharacteristic* characteristic) { | 331 BluetoothGattCharacteristic* characteristic) { |
| 332 RunPendingTasksUntilCallback(); | 332 RunPendingTasksUntilCallback(); |
| 333 } | 333 } |
| 334 | 334 |
| 335 void BluetoothTestWin::SimulateGattCharacteristicWriteError( | 335 void BluetoothTestWin::SimulateGattCharacteristicWriteError( |
| 336 BluetoothGattCharacteristic* characteristic, | 336 BluetoothGattCharacteristic* characteristic, |
| 337 BluetoothGattService::GattErrorCode error_code) { | 337 BluetoothGattService::GattErrorCode error_code) { |
| 338 win::GattCharacteristic* target_characteristic = | 338 win::GattCharacteristic* target_characteristic = |
| 339 GetSimulatedCharacteristic(characteristic); | 339 GetSimulatedCharacteristic(characteristic); |
| 340 CHECK(target_characteristic); | 340 CHECK(target_characteristic); |
| 341 HRESULT hr = ERROR_SEM_TIMEOUT; | 341 HRESULT hr = HRESULT_FROM_WIN32(ERROR_SEM_TIMEOUT); |
| 342 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) | 342 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) |
| 343 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; | 343 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; |
| 344 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError( | 344 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError( |
| 345 target_characteristic, hr); | 345 target_characteristic, hr); |
| 346 | 346 |
| 347 FinishPendingTasks(); | 347 FinishPendingTasks(); |
| 348 } | 348 } |
| 349 | 349 |
| 350 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) { | 350 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) { |
| 351 CHECK(device); | 351 CHECK(device); |
| 352 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress()); | 352 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress()); |
| 353 } | 353 } |
| 354 | 354 |
| 355 void BluetoothTestWin::SimulateGattDescriptor( | 355 void BluetoothTestWin::SimulateGattDescriptor( |
| 356 BluetoothGattCharacteristic* characteristic, | 356 BluetoothGattCharacteristic* characteristic, |
| 357 const std::string& uuid) { | 357 const std::string& uuid) { |
| 358 win::GattCharacteristic* target_characteristic = | 358 win::GattCharacteristic* target_characteristic = |
| 359 GetSimulatedCharacteristic(characteristic); | 359 GetSimulatedCharacteristic(characteristic); |
| 360 CHECK(target_characteristic); | 360 CHECK(target_characteristic); |
| 361 fake_bt_le_wrapper_->SimulateGattDescriptor( | 361 fake_bt_le_wrapper_->SimulateGattDescriptor( |
| 362 characteristic->GetService()->GetDevice()->GetAddress(), | 362 characteristic->GetService()->GetDevice()->GetAddress(), |
| 363 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); | 363 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); |
| 364 ForceRefreshDevice(); | 364 ForceRefreshDevice(); |
| 365 } | 365 } |
| 366 | 366 |
| 367 void BluetoothTestWin::OnAttemptReadGattCharacteristic() { | 367 void BluetoothTestWin::SimulateGattNotifySessionStarted( |
| 368 BluetoothGattCharacteristic* characteristic) { |
| 369 FinishPendingTasks(); |
| 370 } |
| 371 |
| 372 void BluetoothTestWin::SimulateGattCharacteristicChanged( |
| 373 BluetoothGattCharacteristic* characteristic, |
| 374 const std::vector<uint8_t>& value) { |
| 375 BluetoothGattCharacteristic* target_characteristic = characteristic; |
| 376 if (target_characteristic == nullptr) |
| 377 target_characteristic = remembered_characteristic_; |
| 378 CHECK(target_characteristic); |
| 379 |
| 380 win::GattCharacteristic* simulated_characteristic = |
| 381 GetSimulatedCharacteristic(target_characteristic); |
| 382 CHECK(simulated_characteristic); |
| 383 fake_bt_le_wrapper_->SimulateGattCharacteristicValue(simulated_characteristic, |
| 384 value); |
| 385 fake_bt_le_wrapper_->SimulateCharacteristicValueChangeNotification( |
| 386 simulated_characteristic); |
| 387 |
| 388 FinishPendingTasks(); |
| 389 } |
| 390 |
| 391 void BluetoothTestWin::OnReadGattCharacteristicValue() { |
| 368 gatt_read_characteristic_attempts_++; | 392 gatt_read_characteristic_attempts_++; |
| 369 } | 393 } |
| 370 | 394 |
| 371 void BluetoothTestWin::OnAttemptWriteGattCharacteristic() { | 395 void BluetoothTestWin::OnWriteGattCharacteristicValue( |
| 396 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) { |
| 372 gatt_write_characteristic_attempts_++; | 397 gatt_write_characteristic_attempts_++; |
| 373 } | |
| 374 | |
| 375 void BluetoothTestWin::onWriteGattCharacteristicValue( | |
| 376 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) { | |
| 377 last_write_value_.clear(); | 398 last_write_value_.clear(); |
| 378 for (ULONG i = 0; i < value->DataSize; i++) | 399 for (ULONG i = 0; i < value->DataSize; i++) |
| 379 last_write_value_.push_back(value->Data[i]); | 400 last_write_value_.push_back(value->Data[i]); |
| 380 } | 401 } |
| 381 | 402 |
| 403 void BluetoothTestWin::OnStartCharacteristicNotification() { |
| 404 gatt_notify_characteristic_attempts_++; |
| 405 } |
| 406 |
| 382 win::GattService* BluetoothTestWin::GetSimulatedService( | 407 win::GattService* BluetoothTestWin::GetSimulatedService( |
| 383 win::BLEDevice* device, | 408 win::BLEDevice* device, |
| 384 BluetoothGattService* service) { | 409 BluetoothGattService* service) { |
| 385 CHECK(device); | 410 CHECK(device); |
| 386 CHECK(service); | 411 CHECK(service); |
| 387 | 412 |
| 388 std::vector<std::string> chain_of_att_handles; | 413 std::vector<std::string> chain_of_att_handles; |
| 389 BluetoothRemoteGattServiceWin* win_service = | 414 BluetoothRemoteGattServiceWin* win_service = |
| 390 static_cast<BluetoothRemoteGattServiceWin*>(service); | 415 static_cast<BluetoothRemoteGattServiceWin*>(service); |
| 391 chain_of_att_handles.insert( | 416 chain_of_att_handles.insert( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 void BluetoothTestWin::ForceRefreshDevice() { | 471 void BluetoothTestWin::ForceRefreshDevice() { |
| 447 adapter_win_->force_update_device_for_test_ = true; | 472 adapter_win_->force_update_device_for_test_ = true; |
| 448 FinishPendingTasks(); | 473 FinishPendingTasks(); |
| 449 } | 474 } |
| 450 | 475 |
| 451 void BluetoothTestWin::FinishPendingTasks() { | 476 void BluetoothTestWin::FinishPendingTasks() { |
| 452 bluetooth_task_runner_->RunPendingTasks(); | 477 bluetooth_task_runner_->RunPendingTasks(); |
| 453 base::RunLoop().RunUntilIdle(); | 478 base::RunLoop().RunUntilIdle(); |
| 454 } | 479 } |
| 455 } | 480 } |
| OLD | NEW |