| 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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic) | 282 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic) |
| 283 ->GetAttributeHandle()); | 283 ->GetAttributeHandle()); |
| 284 fake_bt_le_wrapper_->SimulateGattCharacteriscRemove( | 284 fake_bt_le_wrapper_->SimulateGattCharacteriscRemove( |
| 285 target_service, characteristic_att_handle); | 285 target_service, characteristic_att_handle); |
| 286 | 286 |
| 287 ForceRefreshDevice(); | 287 ForceRefreshDevice(); |
| 288 } | 288 } |
| 289 | 289 |
| 290 void BluetoothTestWin::RememberCharacteristicForSubsequentAction( | 290 void BluetoothTestWin::RememberCharacteristicForSubsequentAction( |
| 291 BluetoothGattCharacteristic* characteristic) { | 291 BluetoothGattCharacteristic* characteristic) { |
| 292 remembered_characteristic_ = | 292 CHECK(characteristic); |
| 293 BluetoothRemoteGattCharacteristicWin* win_characteristic = |
| 293 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic); | 294 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic); |
| 295 |
| 296 std::string device_address = |
| 297 win_characteristic->GetService()->GetDevice()->GetAddress(); |
| 298 win::BLEDevice* target_device = |
| 299 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address); |
| 300 CHECK(target_device); |
| 301 win::GattService* target_service = |
| 302 GetSimulatedService(target_device, win_characteristic->GetService()); |
| 303 CHECK(target_service); |
| 304 fake_bt_le_wrapper_->RememberCharacteristicForSubsequentAction( |
| 305 target_service, std::to_string(win_characteristic->GetAttributeHandle())); |
| 294 } | 306 } |
| 295 | 307 |
| 296 void BluetoothTestWin::SimulateGattCharacteristicRead( | 308 void BluetoothTestWin::SimulateGattCharacteristicRead( |
| 297 BluetoothGattCharacteristic* characteristic, | 309 BluetoothGattCharacteristic* characteristic, |
| 298 const std::vector<uint8_t>& value) { | 310 const std::vector<uint8_t>& value) { |
| 299 BluetoothGattCharacteristic* target_characteristic = characteristic; | 311 win::GattCharacteristic* target_simulated_characteristic = nullptr; |
| 300 if (target_characteristic == nullptr) | 312 if (characteristic) { |
| 301 target_characteristic = remembered_characteristic_; | 313 target_simulated_characteristic = |
| 302 CHECK(target_characteristic); | 314 GetSimulatedCharacteristic(characteristic); |
| 303 | 315 } |
| 304 win::GattCharacteristic* target_simulated_characteristic = | |
| 305 GetSimulatedCharacteristic(target_characteristic); | |
| 306 if (target_simulated_characteristic == nullptr) | |
| 307 return; | |
| 308 | 316 |
| 309 fake_bt_le_wrapper_->SimulateGattCharacteristicValue( | 317 fake_bt_le_wrapper_->SimulateGattCharacteristicValue( |
| 310 target_simulated_characteristic, value); | 318 target_simulated_characteristic, value); |
| 311 | 319 |
| 312 RunPendingTasksUntilCallback(); | 320 RunPendingTasksUntilCallback(); |
| 313 } | 321 } |
| 314 | 322 |
| 315 void BluetoothTestWin::SimulateGattCharacteristicReadError( | 323 void BluetoothTestWin::SimulateGattCharacteristicReadError( |
| 316 BluetoothGattCharacteristic* characteristic, | 324 BluetoothGattCharacteristic* characteristic, |
| 317 BluetoothGattService::GattErrorCode error_code) { | 325 BluetoothGattService::GattErrorCode error_code) { |
| 318 win::GattCharacteristic* target_characteristic = | 326 win::GattCharacteristic* target_characteristic = |
| 319 GetSimulatedCharacteristic(characteristic); | 327 GetSimulatedCharacteristic(characteristic); |
| 320 CHECK(target_characteristic); | 328 CHECK(target_characteristic); |
| 321 HRESULT hr = ERROR_SEM_TIMEOUT; | 329 HRESULT hr = HRESULT_FROM_WIN32(ERROR_SEM_TIMEOUT); |
| 322 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) | 330 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) |
| 323 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; | 331 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; |
| 324 fake_bt_le_wrapper_->SimulateGattCharacteristicReadError( | 332 fake_bt_le_wrapper_->SimulateGattCharacteristicReadError( |
| 325 target_characteristic, hr); | 333 target_characteristic, hr); |
| 326 | 334 |
| 327 FinishPendingTasks(); | 335 FinishPendingTasks(); |
| 328 } | 336 } |
| 329 | 337 |
| 330 void BluetoothTestWin::SimulateGattCharacteristicWrite( | 338 void BluetoothTestWin::SimulateGattCharacteristicWrite( |
| 331 BluetoothGattCharacteristic* characteristic) { | 339 BluetoothGattCharacteristic* characteristic) { |
| 332 RunPendingTasksUntilCallback(); | 340 RunPendingTasksUntilCallback(); |
| 333 } | 341 } |
| 334 | 342 |
| 335 void BluetoothTestWin::SimulateGattCharacteristicWriteError( | 343 void BluetoothTestWin::SimulateGattCharacteristicWriteError( |
| 336 BluetoothGattCharacteristic* characteristic, | 344 BluetoothGattCharacteristic* characteristic, |
| 337 BluetoothGattService::GattErrorCode error_code) { | 345 BluetoothGattService::GattErrorCode error_code) { |
| 338 win::GattCharacteristic* target_characteristic = | 346 win::GattCharacteristic* target_characteristic = |
| 339 GetSimulatedCharacteristic(characteristic); | 347 GetSimulatedCharacteristic(characteristic); |
| 340 CHECK(target_characteristic); | 348 CHECK(target_characteristic); |
| 341 HRESULT hr = ERROR_SEM_TIMEOUT; | 349 HRESULT hr = HRESULT_FROM_WIN32(ERROR_SEM_TIMEOUT); |
| 342 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) | 350 if (error_code == BluetoothGattService::GATT_ERROR_INVALID_LENGTH) |
| 343 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; | 351 hr = E_BLUETOOTH_ATT_INVALID_ATTRIBUTE_VALUE_LENGTH; |
| 344 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError( | 352 fake_bt_le_wrapper_->SimulateGattCharacteristicWriteError( |
| 345 target_characteristic, hr); | 353 target_characteristic, hr); |
| 346 | 354 |
| 347 FinishPendingTasks(); | 355 FinishPendingTasks(); |
| 348 } | 356 } |
| 349 | 357 |
| 350 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) { | 358 void BluetoothTestWin::DeleteDevice(BluetoothDevice* device) { |
| 351 CHECK(device); | 359 CHECK(device); |
| 352 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress()); | 360 fake_bt_le_wrapper_->RemoveSimulatedBLEDevice(device->GetAddress()); |
| 361 FinishPendingTasks(); |
| 353 } | 362 } |
| 354 | 363 |
| 355 void BluetoothTestWin::SimulateGattDescriptor( | 364 void BluetoothTestWin::SimulateGattDescriptor( |
| 356 BluetoothGattCharacteristic* characteristic, | 365 BluetoothGattCharacteristic* characteristic, |
| 357 const std::string& uuid) { | 366 const std::string& uuid) { |
| 358 win::GattCharacteristic* target_characteristic = | 367 win::GattCharacteristic* target_characteristic = |
| 359 GetSimulatedCharacteristic(characteristic); | 368 GetSimulatedCharacteristic(characteristic); |
| 360 CHECK(target_characteristic); | 369 CHECK(target_characteristic); |
| 361 fake_bt_le_wrapper_->SimulateGattDescriptor( | 370 fake_bt_le_wrapper_->SimulateGattDescriptor( |
| 362 characteristic->GetService()->GetDevice()->GetAddress(), | 371 characteristic->GetService()->GetDevice()->GetAddress(), |
| 363 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); | 372 target_characteristic, CanonicalStringToBTH_LE_UUID(uuid)); |
| 364 ForceRefreshDevice(); | 373 ForceRefreshDevice(); |
| 365 } | 374 } |
| 366 | 375 |
| 367 void BluetoothTestWin::OnAttemptReadGattCharacteristic() { | 376 void BluetoothTestWin::SimulateGattNotifySessionStarted( |
| 377 BluetoothGattCharacteristic* characteristic) { |
| 378 FinishPendingTasks(); |
| 379 } |
| 380 |
| 381 void BluetoothTestWin::SimulateGattCharacteristicChanged( |
| 382 BluetoothGattCharacteristic* characteristic, |
| 383 const std::vector<uint8_t>& value) { |
| 384 win::GattCharacteristic* target_simulated_characteristic = nullptr; |
| 385 if (characteristic) { |
| 386 target_simulated_characteristic = |
| 387 GetSimulatedCharacteristic(characteristic); |
| 388 } |
| 389 |
| 390 fake_bt_le_wrapper_->SimulateGattCharacteristicValue( |
| 391 target_simulated_characteristic, value); |
| 392 fake_bt_le_wrapper_->SimulateCharacteristicValueChangeNotification( |
| 393 target_simulated_characteristic); |
| 394 |
| 395 FinishPendingTasks(); |
| 396 } |
| 397 |
| 398 void BluetoothTestWin::OnReadGattCharacteristicValue() { |
| 368 gatt_read_characteristic_attempts_++; | 399 gatt_read_characteristic_attempts_++; |
| 369 } | 400 } |
| 370 | 401 |
| 371 void BluetoothTestWin::OnAttemptWriteGattCharacteristic() { | 402 void BluetoothTestWin::OnWriteGattCharacteristicValue( |
| 403 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) { |
| 372 gatt_write_characteristic_attempts_++; | 404 gatt_write_characteristic_attempts_++; |
| 373 } | |
| 374 | |
| 375 void BluetoothTestWin::onWriteGattCharacteristicValue( | |
| 376 const PBTH_LE_GATT_CHARACTERISTIC_VALUE value) { | |
| 377 last_write_value_.clear(); | 405 last_write_value_.clear(); |
| 378 for (ULONG i = 0; i < value->DataSize; i++) | 406 for (ULONG i = 0; i < value->DataSize; i++) |
| 379 last_write_value_.push_back(value->Data[i]); | 407 last_write_value_.push_back(value->Data[i]); |
| 380 } | 408 } |
| 381 | 409 |
| 410 void BluetoothTestWin::OnStartCharacteristicNotification() { |
| 411 gatt_notify_characteristic_attempts_++; |
| 412 } |
| 413 |
| 414 void BluetoothTestWin::OnWriteGattDescriptorValue( |
| 415 const std::vector<uint8_t>& value) { |
| 416 gatt_write_descriptor_attempts_++; |
| 417 last_write_value_.assign(value.begin(), value.end()); |
| 418 } |
| 419 |
| 382 win::GattService* BluetoothTestWin::GetSimulatedService( | 420 win::GattService* BluetoothTestWin::GetSimulatedService( |
| 383 win::BLEDevice* device, | 421 win::BLEDevice* device, |
| 384 BluetoothGattService* service) { | 422 BluetoothGattService* service) { |
| 385 CHECK(device); | 423 CHECK(device); |
| 386 CHECK(service); | 424 CHECK(service); |
| 387 | 425 |
| 388 std::vector<std::string> chain_of_att_handles; | 426 std::vector<std::string> chain_of_att_handles; |
| 389 BluetoothRemoteGattServiceWin* win_service = | 427 BluetoothRemoteGattServiceWin* win_service = |
| 390 static_cast<BluetoothRemoteGattServiceWin*>(service); | 428 static_cast<BluetoothRemoteGattServiceWin*>(service); |
| 391 chain_of_att_handles.insert( | 429 chain_of_att_handles.insert( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 void BluetoothTestWin::ForceRefreshDevice() { | 484 void BluetoothTestWin::ForceRefreshDevice() { |
| 447 adapter_win_->force_update_device_for_test_ = true; | 485 adapter_win_->force_update_device_for_test_ = true; |
| 448 FinishPendingTasks(); | 486 FinishPendingTasks(); |
| 449 } | 487 } |
| 450 | 488 |
| 451 void BluetoothTestWin::FinishPendingTasks() { | 489 void BluetoothTestWin::FinishPendingTasks() { |
| 452 bluetooth_task_runner_->RunPendingTasks(); | 490 bluetooth_task_runner_->RunPendingTasks(); |
| 453 base::RunLoop().RunUntilIdle(); | 491 base::RunLoop().RunUntilIdle(); |
| 454 } | 492 } |
| 455 } | 493 } |
| OLD | NEW |