| 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::RunUntilIdle() { |
| 125 FinishPendingTasks(); |
| 126 } |
| 127 |
| 124 void BluetoothTestWin::StartLowEnergyDiscoverySession() { | 128 void BluetoothTestWin::StartLowEnergyDiscoverySession() { |
| 125 __super ::StartLowEnergyDiscoverySession(); | 129 __super ::StartLowEnergyDiscoverySession(); |
| 126 FinishPendingTasks(); | 130 FinishPendingTasks(); |
| 127 } | 131 } |
| 128 | 132 |
| 129 BluetoothDevice* BluetoothTestWin::DiscoverLowEnergyDevice(int device_ordinal) { | 133 BluetoothDevice* BluetoothTestWin::DiscoverLowEnergyDevice(int device_ordinal) { |
| 130 if (device_ordinal > 4 || device_ordinal < 1) | 134 if (device_ordinal > 4 || device_ordinal < 1) |
| 131 return nullptr; | 135 return nullptr; |
| 132 | 136 |
| 133 std::string device_name = kTestDeviceName; | 137 std::string device_name = kTestDeviceName; |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 | 315 |
| 312 RunPendingTasksUntilCallback(); | 316 RunPendingTasksUntilCallback(); |
| 313 } | 317 } |
| 314 | 318 |
| 315 void BluetoothTestWin::SimulateGattCharacteristicReadError( | 319 void BluetoothTestWin::SimulateGattCharacteristicReadError( |
| 316 BluetoothGattCharacteristic* characteristic, | 320 BluetoothGattCharacteristic* characteristic, |
| 317 BluetoothGattService::GattErrorCode error_code) { | 321 BluetoothGattService::GattErrorCode error_code) { |
| 318 win::GattCharacteristic* target_characteristic = | 322 win::GattCharacteristic* target_characteristic = |
| 319 GetSimulatedCharacteristic(characteristic); | 323 GetSimulatedCharacteristic(characteristic); |
| 320 CHECK(target_characteristic); | 324 CHECK(target_characteristic); |
| 321 HRESULT hr = ERROR_SEM_TIMEOUT; | 325 HRESULT hr = HRESULT_FROM_WIN32(ERROR_SEM_TIMEOUT); |
| 322 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) | 326 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) |
| 323 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; | 327 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; |
| 324 fake_bt_le_wrapper_->SimulateGattCharacteristicReadError( | 328 fake_bt_le_wrapper_->SimulateGattCharacteristicReadError( |
| 325 target_characteristic, hr); | 329 target_characteristic, hr); |
| 326 | 330 |
| 327 FinishPendingTasks(); | 331 FinishPendingTasks(); |
| 328 } | 332 } |
| 329 | 333 |
| 330 void BluetoothTestWin::SimulateGattCharacteristicWrite( | 334 void BluetoothTestWin::SimulateGattCharacteristicWrite( |
| 331 BluetoothGattCharacteristic* characteristic) { | 335 BluetoothGattCharacteristic* characteristic) { |
| 332 RunPendingTasksUntilCallback(); | 336 RunPendingTasksUntilCallback(); |
| 333 } | 337 } |
| 334 | 338 |
| 335 void BluetoothTestWin::SimulateGattCharacteristicWriteError( | 339 void BluetoothTestWin::SimulateGattCharacteristicWriteError( |
| 336 BluetoothGattCharacteristic* characteristic, | 340 BluetoothGattCharacteristic* characteristic, |
| 337 BluetoothGattService::GattErrorCode error_code) { | 341 BluetoothGattService::GattErrorCode error_code) { |
| 338 win::GattCharacteristic* target_characteristic = | 342 win::GattCharacteristic* target_characteristic = |
| 339 GetSimulatedCharacteristic(characteristic); | 343 GetSimulatedCharacteristic(characteristic); |
| 340 CHECK(target_characteristic); | 344 CHECK(target_characteristic); |
| 341 HRESULT hr = ERROR_SEM_TIMEOUT; | 345 HRESULT hr = HRESULT_FROM_WIN32(ERROR_SEM_TIMEOUT); |
| 342 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) | 346 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) |
| 343 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; | 347 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; |
| 344 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError( | 348 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError( |
| 345 target_characteristic, hr); | 349 target_characteristic, hr); |
| 346 | 350 |
| 347 FinishPendingTasks(); | 351 FinishPendingTasks(); |
| 348 } | 352 } |
| 349 | 353 |
| 354 void BluetoothTestWin:: |
| 355 SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( |
| 356 BluetoothGattCharacteristic* characteristic) { |
| 357 win::GattCharacteristic* target_characteristic = |
| 358 GetSimulatedCharacteristic(characteristic); |
| 359 CHECK(target_characteristic); |
| 360 fake_bt_le_wrapper_->SimulateGattCharacteristicSetNotifyError( |
| 361 target_characteristic, E_BLUETOOTH_ATT_UNKNOWN_ERROR); |
| 362 } |
| 363 |
| 350 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) { | 364 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) { |
| 351 CHECK(device); | 365 CHECK(device); |
| 352 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress()); | 366 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress()); |
| 353 } | 367 } |
| 354 | 368 |
| 355 void BluetoothTestWin::SimulateGattDescriptor( | 369 void BluetoothTestWin::SimulateGattDescriptor( |
| 356 BluetoothGattCharacteristic* characteristic, | 370 BluetoothGattCharacteristic* characteristic, |
| 357 const std::string& uuid) { | 371 const std::string& uuid) { |
| 358 win::GattCharacteristic* target_characteristic = | 372 win::GattCharacteristic* target_characteristic = |
| 359 GetSimulatedCharacteristic(characteristic); | 373 GetSimulatedCharacteristic(characteristic); |
| 360 CHECK(target_characteristic); | 374 CHECK(target_characteristic); |
| 361 fake_bt_le_wrapper_->SimulateGattDescriptor( | 375 fake_bt_le_wrapper_->SimulateGattDescriptor( |
| 362 characteristic->GetService()->GetDevice()->GetAddress(), | 376 characteristic->GetService()->GetDevice()->GetAddress(), |
| 363 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); | 377 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); |
| 364 ForceRefreshDevice(); | 378 ForceRefreshDevice(); |
| 365 } | 379 } |
| 366 | 380 |
| 367 void BluetoothTestWin::OnAttemptReadGattCharacteristic() { | 381 void BluetoothTestWin::SimulateGattDescriptorWriteWillFailSynchronouslyOnce( |
| 382 BluetoothGattDescriptor* descriptor) { |
| 383 BluetoothRemoteGattDescriptorWin* win_descriptor = |
| 384 (BluetoothRemoteGattDescriptorWin*)descriptor; |
| 385 win::GattCharacteristic* target_characteristic = |
| 386 GetSimulatedCharacteristic(win_descriptor->GetCharacteristic()); |
| 387 CHECK(target_characteristic); |
| 388 win::GattDescriptor* target_descriptor = |
| 389 fake_bt_le_wrapper_->GetSimulatedDescriptor( |
| 390 target_characteristic, |
| 391 std::to_string(win_descriptor->GetAttributeHandle())); |
| 392 CHECK(target_descriptor); |
| 393 fake_bt_le_wrapper_->SimulateGattDescriptorWriteError( |
| 394 target_descriptor, E_BLUETOOTH_ATT_REQUEST_NOT_SUPPORTED); |
| 395 } |
| 396 |
| 397 void BluetoothTestWin::SimulateGattNotifySessionStarted( |
| 398 BluetoothGattCharacteristic* characteristic) { |
| 399 FinishPendingTasks(); |
| 400 } |
| 401 |
| 402 void BluetoothTestWin::SimulateGattCharacteristicChanged( |
| 403 BluetoothGattCharacteristic* characteristic, |
| 404 const std::vector<uint8_t>& value) { |
| 405 BluetoothGattCharacteristic* target_characteristic = characteristic; |
| 406 if (target_characteristic == nullptr) |
| 407 target_characteristic = remembered_characteristic_; |
| 408 CHECK(target_characteristic); |
| 409 |
| 410 win::GattCharacteristic* simulated_characteristic = |
| 411 GetSimulatedCharacteristic(target_characteristic); |
| 412 CHECK(simulated_characteristic); |
| 413 fake_bt_le_wrapper_->SimulateGattCharacteristicValue(simulated_characteristic, |
| 414 value); |
| 415 std::string device_address = |
| 416 characteristic->GetService()->GetDevice()->GetAddress(); |
| 417 win::BLEDevice* target_device = |
| 418 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address); |
| 419 CHECK(target_device); |
| 420 win::GattService* parent_service = |
| 421 GetSimulatedService(target_device, characteristic->GetService()); |
| 422 CHECK(parent_service); |
| 423 fake_bt_le_wrapper_->SimulateCharacteristicValueChangeNotification( |
| 424 parent_service, simulated_characteristic); |
| 425 |
| 426 FinishPendingTasks(); |
| 427 } |
| 428 |
| 429 void BluetoothTestWin::OnReadGattCharacteristicValue() { |
| 368 gatt_read_characteristic_attempts_++; | 430 gatt_read_characteristic_attempts_++; |
| 369 } | 431 } |
| 370 | 432 |
| 371 void BluetoothTestWin::OnAttemptWriteGattCharacteristic() { | 433 void BluetoothTestWin::OnWriteGattCharacteristicValue( |
| 434 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) { |
| 372 gatt_write_characteristic_attempts_++; | 435 gatt_write_characteristic_attempts_++; |
| 373 } | |
| 374 | |
| 375 void BluetoothTestWin::onWriteGattCharacteristicValue( | |
| 376 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) { | |
| 377 last_write_value_.clear(); | 436 last_write_value_.clear(); |
| 378 for (ULONG i = 0; i < value->DataSize; i++) | 437 for (ULONG i = 0; i < value->DataSize; i++) |
| 379 last_write_value_.push_back(value->Data[i]); | 438 last_write_value_.push_back(value->Data[i]); |
| 380 } | 439 } |
| 381 | 440 |
| 441 void BluetoothTestWin::OnWriteGattDescriptorValue( |
| 442 const PBTH_LE_GATT_DESCRIPTOR_VALUE value) { |
| 443 gatt_write_descriptor_attempts_++; |
| 444 last_write_value_.clear(); |
| 445 for (ULONG i = 0; i < value->DataSize; i++) |
| 446 last_write_value_.push_back(value->Data[i]); |
| 447 } |
| 448 |
| 449 void BluetoothTestWin::OnStartCharacteristicNotification() { |
| 450 gatt_notify_characteristic_attempts_++; |
| 451 } |
| 452 |
| 382 win::GattService* BluetoothTestWin::GetSimulatedService( | 453 win::GattService* BluetoothTestWin::GetSimulatedService( |
| 383 win::BLEDevice* device, | 454 win::BLEDevice* device, |
| 384 BluetoothGattService* service) { | 455 BluetoothGattService* service) { |
| 385 CHECK(device); | 456 CHECK(device); |
| 386 CHECK(service); | 457 CHECK(service); |
| 387 | 458 |
| 388 std::vector<std::string> chain_of_att_handles; | 459 std::vector<std::string> chain_of_att_handles; |
| 389 BluetoothRemoteGattServiceWin* win_service = | 460 BluetoothRemoteGattServiceWin* win_service = |
| 390 static_cast<BluetoothRemoteGattServiceWin*>(service); | 461 static_cast<BluetoothRemoteGattServiceWin*>(service); |
| 391 chain_of_att_handles.insert( | 462 chain_of_att_handles.insert( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 void BluetoothTestWin::ForceRefreshDevice() { | 517 void BluetoothTestWin::ForceRefreshDevice() { |
| 447 adapter_win_->force_update_device_for_test_ = true; | 518 adapter_win_->force_update_device_for_test_ = true; |
| 448 FinishPendingTasks(); | 519 FinishPendingTasks(); |
| 449 } | 520 } |
| 450 | 521 |
| 451 void BluetoothTestWin::FinishPendingTasks() { | 522 void BluetoothTestWin::FinishPendingTasks() { |
| 452 bluetooth_task_runner_->RunPendingTasks(); | 523 bluetooth_task_runner_->RunPendingTasks(); |
| 453 base::RunLoop().RunUntilIdle(); | 524 base::RunLoop().RunUntilIdle(); |
| 454 } | 525 } |
| 455 } | 526 } |
| OLD | NEW |