Chromium Code Reviews| 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/bluetooth_low_energy_win_fake.h" | 5 #include "device/bluetooth/bluetooth_low_energy_win_fake.h" |
| 6 | 6 |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "device/bluetooth/bluetooth_low_energy_defs_win.h" | 8 #include "device/bluetooth/bluetooth_low_energy_defs_win.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 const char kPlatformNotSupported[] = | 11 const char kPlatformNotSupported[] = |
| 12 "Bluetooth Low energy is only supported on Windows 8 and later."; | 12 "Bluetooth Low energy is only supported on Windows 8 and later."; |
| 13 } // namespace | 13 } // namespace |
| 14 | 14 |
| 15 namespace device { | 15 namespace device { |
| 16 namespace win { | 16 namespace win { |
| 17 | 17 |
| 18 BLEDevice::BLEDevice() {} | 18 BLEDevice::BLEDevice() {} |
| 19 BLEDevice::~BLEDevice() {} | 19 BLEDevice::~BLEDevice() {} |
| 20 | 20 |
| 21 GattService::GattService() {} | 21 GattService::GattService() {} |
| 22 GattService::~GattService() {} | 22 GattService::~GattService() {} |
| 23 | 23 |
| 24 GattCharacteristic::GattCharacteristic() {} | 24 GattCharacteristic::GattCharacteristic() {} |
| 25 GattCharacteristic::~GattCharacteristic() {} | 25 GattCharacteristic::~GattCharacteristic() {} |
| 26 | 26 |
| 27 GattDescriptor::GattDescriptor() {} | 27 GattDescriptor::GattDescriptor() {} |
| 28 GattDescriptor::~GattDescriptor() {} | 28 GattDescriptor::~GattDescriptor() {} |
| 29 | 29 |
| 30 GattCharacteristicObserver::GattCharacteristicObserver() {} | |
| 31 GattCharacteristicObserver::~GattCharacteristicObserver() {} | |
| 32 | |
| 30 BluetoothLowEnergyWrapperFake::BluetoothLowEnergyWrapperFake() | 33 BluetoothLowEnergyWrapperFake::BluetoothLowEnergyWrapperFake() |
| 31 : observer_(nullptr) {} | 34 : observer_(nullptr) {} |
| 32 BluetoothLowEnergyWrapperFake::~BluetoothLowEnergyWrapperFake() {} | 35 BluetoothLowEnergyWrapperFake::~BluetoothLowEnergyWrapperFake() {} |
| 33 | 36 |
| 34 bool BluetoothLowEnergyWrapperFake::IsBluetoothLowEnergySupported() { | 37 bool BluetoothLowEnergyWrapperFake::IsBluetoothLowEnergySupported() { |
| 35 return true; | 38 return true; |
| 36 } | 39 } |
| 37 | 40 |
| 38 bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyDevices( | 41 bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyDevices( |
| 39 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, | 42 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 return hr; | 200 return hr; |
| 198 } | 201 } |
| 199 | 202 |
| 200 PBTH_LE_GATT_CHARACTERISTIC_VALUE ret_value = | 203 PBTH_LE_GATT_CHARACTERISTIC_VALUE ret_value = |
| 201 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( | 204 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( |
| 202 new UCHAR[sizeof(ULONG) + target_characteristic->value->DataSize]); | 205 new UCHAR[sizeof(ULONG) + target_characteristic->value->DataSize]); |
| 203 ret_value->DataSize = target_characteristic->value->DataSize; | 206 ret_value->DataSize = target_characteristic->value->DataSize; |
| 204 for (ULONG i = 0; i < ret_value->DataSize; i++) | 207 for (ULONG i = 0; i < ret_value->DataSize; i++) |
| 205 ret_value->Data[i] = target_characteristic->value->Data[i]; | 208 ret_value->Data[i] = target_characteristic->value->Data[i]; |
| 206 out_value->reset(ret_value); | 209 out_value->reset(ret_value); |
| 210 if (observer_) | |
| 211 observer_->OnReadGattCharacteristicValue(); | |
| 207 return S_OK; | 212 return S_OK; |
| 208 } | 213 } |
| 209 | 214 |
| 210 HRESULT BluetoothLowEnergyWrapperFake::WriteCharacteristicValue( | 215 HRESULT BluetoothLowEnergyWrapperFake::WriteCharacteristicValue( |
| 211 base::FilePath& service_path, | 216 base::FilePath& service_path, |
| 212 const PBTH_LE_GATT_CHARACTERISTIC characteristic, | 217 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 213 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) { | 218 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) { |
| 214 GattCharacteristic* target_characteristic = | 219 GattCharacteristic* target_characteristic = |
| 215 GetSimulatedGattCharacteristic(service_path, characteristic); | 220 GetSimulatedGattCharacteristic(service_path, characteristic); |
| 216 if (target_characteristic == nullptr) | 221 if (target_characteristic == nullptr) |
| 217 return ERROR_NOT_FOUND; | 222 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND); |
| 218 | 223 |
| 219 // Return error simulated by SimulateGattCharacteristicWriteError. | 224 // Return error simulated by SimulateGattCharacteristicWriteError. |
| 220 if (target_characteristic->write_errors.size()) { | 225 if (target_characteristic->write_errors.size()) { |
| 221 HRESULT hr = *(target_characteristic->write_errors.begin()); | 226 HRESULT hr = *(target_characteristic->write_errors.begin()); |
| 222 target_characteristic->write_errors.erase( | 227 target_characteristic->write_errors.erase( |
| 223 target_characteristic->write_errors.begin()); | 228 target_characteristic->write_errors.begin()); |
| 224 return hr; | 229 return hr; |
| 225 } | 230 } |
| 226 | 231 |
| 227 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = | 232 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = |
| 228 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( | 233 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( |
| 229 new UCHAR[new_value->DataSize + sizeof(ULONG)]); | 234 new UCHAR[new_value->DataSize + sizeof(ULONG)]); |
| 230 for (ULONG i = 0; i < new_value->DataSize; i++) | 235 for (ULONG i = 0; i < new_value->DataSize; i++) |
| 231 win_value->Data[i] = new_value->Data[i]; | 236 win_value->Data[i] = new_value->Data[i]; |
| 232 win_value->DataSize = new_value->DataSize; | 237 win_value->DataSize = new_value->DataSize; |
| 233 target_characteristic->value.reset(win_value); | 238 target_characteristic->value.reset(win_value); |
| 234 if (observer_) | 239 if (observer_) |
| 235 observer_->onWriteGattCharacteristicValue(win_value); | 240 observer_->OnWriteGattCharacteristicValue(win_value); |
| 236 return S_OK; | 241 return S_OK; |
| 237 } | 242 } |
| 238 | 243 |
| 244 HRESULT BluetoothLowEnergyWrapperFake::RegisterGattEvents( | |
| 245 base::FilePath& service_path, | |
| 246 BTH_LE_GATT_EVENT_TYPE type, | |
| 247 PVOID event_parameter, | |
| 248 PFNBLUETOOTH_GATT_EVENT_CALLBACK callback, | |
| 249 PVOID context, | |
| 250 BLUETOOTH_GATT_EVENT_HANDLE* out_handle) { | |
| 251 // Right now, only CharacteristicValueChangedEvent is supported. | |
| 252 CHECK(CharacteristicValueChangedEvent == type); | |
| 253 | |
| 254 base::string16 device_address = | |
|
ortuno
2016/03/15 02:55:31
Why do you go through all the trouble of retrievin
gogerald1
2016/03/15 18:28:22
Done. Residual of removing CCCD
| |
| 255 ExtractDeviceAddressFromDevicePath(service_path.value()); | |
| 256 BLEDevice* target_device = GetSimulatedBLEDevice( | |
| 257 std::string(device_address.begin(), device_address.end())); | |
| 258 if (target_device == nullptr) | |
| 259 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND); | |
| 260 const std::vector<std::string> service_att_handles = | |
| 261 ExtractServiceAttributeHandlesFromDevicePath(service_path.value()); | |
| 262 GattService* target_service = | |
| 263 GetSimulatedGattService(target_device, service_att_handles); | |
| 264 if (target_service == nullptr) | |
| 265 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND); | |
| 266 | |
| 267 scoped_ptr<GattCharacteristicObserver> observer( | |
| 268 new GattCharacteristicObserver()); | |
| 269 observer->callback = callback; | |
| 270 observer->context = context; | |
| 271 *out_handle = (BLUETOOTH_GATT_EVENT_HANDLE)observer.get(); | |
| 272 gatt_characteristic_observers_[*out_handle] = std::move(observer); | |
| 273 | |
| 274 PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION parameter = | |
| 275 (PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION)event_parameter; | |
| 276 for (USHORT i = 0; i < parameter->NumCharacteristics; i++) { | |
| 277 GattCharacteristic* target_characteristic = GetSimulatedGattCharacteristic( | |
| 278 target_service, | |
| 279 std::to_string(parameter->Characteristics[i].AttributeHandle)); | |
| 280 CHECK(target_characteristic); | |
| 281 target_characteristic->observers.push_back(*out_handle); | |
| 282 } | |
| 283 | |
| 284 if (observer_) | |
| 285 observer_->OnStartCharacteristicNotification(); | |
| 286 | |
| 287 return S_OK; | |
| 288 } | |
| 289 | |
| 290 HRESULT BluetoothLowEnergyWrapperFake::UnregisterGattEvent( | |
| 291 BLUETOOTH_GATT_EVENT_HANDLE event_handle) { | |
| 292 gatt_characteristic_observers_.erase(event_handle); | |
| 293 return S_OK; | |
| 294 } | |
| 295 | |
| 239 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice( | 296 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice( |
| 240 std::string device_name, | 297 std::string device_name, |
| 241 BLUETOOTH_ADDRESS device_address) { | 298 BLUETOOTH_ADDRESS device_address) { |
| 242 BLEDevice* device = new BLEDevice(); | 299 BLEDevice* device = new BLEDevice(); |
| 243 BluetoothLowEnergyDeviceInfo* device_info = | 300 BluetoothLowEnergyDeviceInfo* device_info = |
| 244 new BluetoothLowEnergyDeviceInfo(); | 301 new BluetoothLowEnergyDeviceInfo(); |
| 245 std::string string_device_address = | 302 std::string string_device_address = |
| 246 BluetoothAddressToCanonicalString(device_address); | 303 BluetoothAddressToCanonicalString(device_address); |
| 247 device_info->path = | 304 device_info->path = |
| 248 base::FilePath(GenerateBLEDevicePath(string_device_address)); | 305 base::FilePath(GenerateBLEDevicePath(string_device_address)); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 CHECK(characteristic); | 427 CHECK(characteristic); |
| 371 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = | 428 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = |
| 372 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( | 429 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( |
| 373 new UCHAR[value.size() + sizeof(ULONG)]); | 430 new UCHAR[value.size() + sizeof(ULONG)]); |
| 374 win_value->DataSize = (ULONG)value.size(); | 431 win_value->DataSize = (ULONG)value.size(); |
| 375 for (std::size_t i = 0; i < value.size(); i++) | 432 for (std::size_t i = 0; i < value.size(); i++) |
| 376 win_value->Data[i] = value[i]; | 433 win_value->Data[i] = value[i]; |
| 377 characteristic->value.reset(win_value); | 434 characteristic->value.reset(win_value); |
| 378 } | 435 } |
| 379 | 436 |
| 437 void BluetoothLowEnergyWrapperFake:: | |
| 438 SimulateCharacteristicValueChangeNotification( | |
| 439 GattCharacteristic* characteristic) { | |
| 440 for (const auto& observer : characteristic->observers) { | |
| 441 GattCharacteristicObserverTable::const_iterator it = | |
| 442 gatt_characteristic_observers_.find(observer); | |
| 443 // Check if |observer| has been unregistered by UnregisterGattEvent. | |
| 444 if (it != gatt_characteristic_observers_.end()) { | |
| 445 BLUETOOTH_GATT_VALUE_CHANGED_EVENT event; | |
| 446 event.ChangedAttributeHandle = | |
| 447 characteristic->characteristic_info->AttributeHandle; | |
| 448 event.CharacteristicValueDataSize = | |
| 449 characteristic->value->DataSize + sizeof(ULONG); | |
|
ortuno
2016/03/15 02:55:31
Why isn't event.CharacteristicValueDataSize == cha
gogerald1
2016/03/15 18:28:22
BTH_LE_GATT_CHARACTERISTIC_VALUE is a structure, i
| |
| 450 event.CharacteristicValue = characteristic->value.get(); | |
| 451 it->second->callback(CharacteristicValueChangedEvent, &event, | |
| 452 it->second->context); | |
| 453 } | |
| 454 } | |
| 455 } | |
| 456 | |
| 380 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicReadError( | 457 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicReadError( |
| 381 GattCharacteristic* characteristic, | 458 GattCharacteristic* characteristic, |
| 382 HRESULT error) { | 459 HRESULT error) { |
| 383 CHECK(characteristic); | 460 CHECK(characteristic); |
| 384 characteristic->read_errors.push_back(error); | 461 characteristic->read_errors.push_back(error); |
| 385 } | 462 } |
| 386 | 463 |
| 387 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicWriteError( | 464 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicWriteError( |
| 388 GattCharacteristic* characteristic, | 465 GattCharacteristic* characteristic, |
| 389 HRESULT error) { | 466 HRESULT error) { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 508 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( | 585 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( |
| 509 const BLUETOOTH_ADDRESS& btha) { | 586 const BLUETOOTH_ADDRESS& btha) { |
| 510 std::string result = base::StringPrintf( | 587 std::string result = base::StringPrintf( |
| 511 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4], | 588 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4], |
| 512 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]); | 589 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]); |
| 513 return result; | 590 return result; |
| 514 } | 591 } |
| 515 | 592 |
| 516 } // namespace win | 593 } // namespace win |
| 517 } // namespace device | 594 } // namespace device |
| OLD | NEW |