Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: device/bluetooth/bluetooth_low_energy_win_fake.cc

Issue 1749403002: Implement BluetoothRemoteGattCharacteristicWin::StartNotifySession and related unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: move comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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,
40 std::string* error) { 43 std::string* error) {
41 if (!IsBluetoothLowEnergySupported()) { 44 if (!IsBluetoothLowEnergySupported()) {
42 *error = kPlatformNotSupported; 45 *error = kPlatformNotSupported;
43 return false; 46 return false;
44 } 47 }
45 48
46 for (auto& device : simulated_devices_) { 49 for (auto& device : simulated_devices_) {
50 if (device.second->marked_as_deleted)
51 continue;
47 BluetoothLowEnergyDeviceInfo* device_info = 52 BluetoothLowEnergyDeviceInfo* device_info =
48 new BluetoothLowEnergyDeviceInfo(); 53 new BluetoothLowEnergyDeviceInfo();
49 *device_info = *(device.second->device_info); 54 *device_info = *(device.second->device_info);
50 devices->push_back(device_info); 55 devices->push_back(device_info);
51 } 56 }
52 return true; 57 return true;
53 } 58 }
54 59
55 bool BluetoothLowEnergyWrapperFake:: 60 bool BluetoothLowEnergyWrapperFake::
56 EnumerateKnownBluetoothLowEnergyGattServiceDevices( 61 EnumerateKnownBluetoothLowEnergyGattServiceDevices(
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 return hr; 202 return hr;
198 } 203 }
199 204
200 PBTH_LE_GATT_CHARACTERISTIC_VALUE ret_value = 205 PBTH_LE_GATT_CHARACTERISTIC_VALUE ret_value =
201 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 206 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
202 new UCHAR[sizeof(ULONG) + target_characteristic->value->DataSize]); 207 new UCHAR[sizeof(ULONG) + target_characteristic->value->DataSize]);
203 ret_value->DataSize = target_characteristic->value->DataSize; 208 ret_value->DataSize = target_characteristic->value->DataSize;
204 for (ULONG i = 0; i < ret_value->DataSize; i++) 209 for (ULONG i = 0; i < ret_value->DataSize; i++)
205 ret_value->Data[i] = target_characteristic->value->Data[i]; 210 ret_value->Data[i] = target_characteristic->value->Data[i];
206 out_value->reset(ret_value); 211 out_value->reset(ret_value);
212 if (observer_)
213 observer_->OnReadGattCharacteristicValue();
207 return S_OK; 214 return S_OK;
208 } 215 }
209 216
210 HRESULT BluetoothLowEnergyWrapperFake::WriteCharacteristicValue( 217 HRESULT BluetoothLowEnergyWrapperFake::WriteCharacteristicValue(
211 base::FilePath& service_path, 218 base::FilePath& service_path,
212 const PBTH_LE_GATT_CHARACTERISTIC characteristic, 219 const PBTH_LE_GATT_CHARACTERISTIC characteristic,
213 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) { 220 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) {
214 GattCharacteristic* target_characteristic = 221 GattCharacteristic* target_characteristic =
215 GetSimulatedGattCharacteristic(service_path, characteristic); 222 GetSimulatedGattCharacteristic(service_path, characteristic);
216 if (target_characteristic == nullptr) 223 if (target_characteristic == nullptr)
217 return ERROR_NOT_FOUND; 224 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
218 225
219 // Return error simulated by SimulateGattCharacteristicWriteError. 226 // Return error simulated by SimulateGattCharacteristicWriteError.
220 if (target_characteristic->write_errors.size()) { 227 if (target_characteristic->write_errors.size()) {
221 HRESULT hr = *(target_characteristic->write_errors.begin()); 228 HRESULT hr = *(target_characteristic->write_errors.begin());
222 target_characteristic->write_errors.erase( 229 target_characteristic->write_errors.erase(
223 target_characteristic->write_errors.begin()); 230 target_characteristic->write_errors.begin());
224 return hr; 231 return hr;
225 } 232 }
226 233
227 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = 234 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value =
228 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 235 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
229 new UCHAR[new_value->DataSize + sizeof(ULONG)]); 236 new UCHAR[new_value->DataSize + sizeof(ULONG)]);
230 for (ULONG i = 0; i < new_value->DataSize; i++) 237 for (ULONG i = 0; i < new_value->DataSize; i++)
231 win_value->Data[i] = new_value->Data[i]; 238 win_value->Data[i] = new_value->Data[i];
232 win_value->DataSize = new_value->DataSize; 239 win_value->DataSize = new_value->DataSize;
233 target_characteristic->value.reset(win_value); 240 target_characteristic->value.reset(win_value);
234 if (observer_) 241 if (observer_)
235 observer_->onWriteGattCharacteristicValue(win_value); 242 observer_->OnWriteGattCharacteristicValue(win_value);
236 return S_OK; 243 return S_OK;
237 } 244 }
238 245
246 HRESULT BluetoothLowEnergyWrapperFake::RegisterGattEvents(
247 base::FilePath& service_path,
248 BTH_LE_GATT_EVENT_TYPE type,
249 PVOID event_parameter,
250 PFNBLUETOOTH_GATT_EVENT_CALLBACK callback,
251 PVOID context,
252 BLUETOOTH_GATT_EVENT_HANDLE* out_handle) {
253 // Right now, only CharacteristicValueChangedEvent is supported.
254 CHECK(CharacteristicValueChangedEvent == type);
255
256 scoped_ptr<GattCharacteristicObserver> observer(
257 new GattCharacteristicObserver());
258 observer->callback = callback;
259 observer->context = context;
260 *out_handle = (BLUETOOTH_GATT_EVENT_HANDLE)observer.get();
261 gatt_characteristic_observers_[*out_handle] = std::move(observer);
262
263 PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION parameter =
264 (PBLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION)event_parameter;
265 for (USHORT i = 0; i < parameter->NumCharacteristics; i++) {
266 GattCharacteristic* target_characteristic = GetSimulatedGattCharacteristic(
267 service_path, &parameter->Characteristics[i]);
268 CHECK(target_characteristic);
269 target_characteristic->observers.push_back(*out_handle);
270 }
271
272 if (observer_)
273 observer_->OnStartCharacteristicNotification();
274
275 return S_OK;
276 }
277
278 HRESULT BluetoothLowEnergyWrapperFake::UnregisterGattEvent(
279 BLUETOOTH_GATT_EVENT_HANDLE event_handle) {
280 gatt_characteristic_observers_.erase(event_handle);
281 return S_OK;
282 }
283
284 HRESULT BluetoothLowEnergyWrapperFake::WriteDescriptorValue(
285 base::FilePath& service_path,
286 const PBTH_LE_GATT_DESCRIPTOR descriptor,
287 PBTH_LE_GATT_DESCRIPTOR_VALUE new_value) {
288 if (new_value->DescriptorType == ClientCharacteristicConfiguration) {
289 // Simulate the value the OS will write.
290 std::vector<UCHAR> write_value;
291 if (new_value->ClientCharacteristicConfiguration
292 .IsSubscribeToNotification) {
293 write_value.push_back(1);
294 } else if (new_value->ClientCharacteristicConfiguration
295 .IsSubscribeToIndication) {
296 write_value.push_back(2);
297 }
298 write_value.push_back(0);
299 if (observer_)
300 observer_->OnWriteGattDescriptorValue(write_value);
301 }
302 return S_OK;
303 }
304
239 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice( 305 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice(
240 std::string device_name, 306 std::string device_name,
241 BLUETOOTH_ADDRESS device_address) { 307 BLUETOOTH_ADDRESS device_address) {
242 BLEDevice* device = new BLEDevice(); 308 BLEDevice* device = new BLEDevice();
243 BluetoothLowEnergyDeviceInfo* device_info = 309 BluetoothLowEnergyDeviceInfo* device_info =
244 new BluetoothLowEnergyDeviceInfo(); 310 new BluetoothLowEnergyDeviceInfo();
245 std::string string_device_address = 311 std::string string_device_address =
246 BluetoothAddressToCanonicalString(device_address); 312 BluetoothAddressToCanonicalString(device_address);
247 device_info->path = 313 device_info->path =
248 base::FilePath(GenerateBLEDevicePath(string_device_address)); 314 base::FilePath(GenerateBLEDevicePath(string_device_address));
249 device_info->friendly_name = device_name; 315 device_info->friendly_name = device_name;
250 device_info->address = device_address; 316 device_info->address = device_address;
251 device->device_info.reset(device_info); 317 device->device_info.reset(device_info);
318 device->marked_as_deleted = false;
252 simulated_devices_[string_device_address] = make_scoped_ptr(device); 319 simulated_devices_[string_device_address] = make_scoped_ptr(device);
253 return device; 320 return device;
254 } 321 }
255 322
256 BLEDevice* BluetoothLowEnergyWrapperFake::GetSimulatedBLEDevice( 323 BLEDevice* BluetoothLowEnergyWrapperFake::GetSimulatedBLEDevice(
257 std::string device_address) { 324 std::string device_address) {
258 BLEDevicesMap::iterator it_d = simulated_devices_.find(device_address); 325 BLEDevicesMap::iterator it_d = simulated_devices_.find(device_address);
259 if (it_d == simulated_devices_.end()) 326 if (it_d == simulated_devices_.end())
260 return nullptr; 327 return nullptr;
261 return it_d->second.get(); 328 return it_d->second.get();
262 } 329 }
263 330
264 void BluetoothLowEnergyWrapperFake::RemoveSimulatedBLEDevice( 331 void BluetoothLowEnergyWrapperFake::RemoveSimulatedBLEDevice(
265 std::string device_address) { 332 std::string device_address) {
266 simulated_devices_.erase(device_address); 333 simulated_devices_[device_address]->marked_as_deleted = true;
267 } 334 }
268 335
269 GattService* BluetoothLowEnergyWrapperFake::SimulateGattService( 336 GattService* BluetoothLowEnergyWrapperFake::SimulateGattService(
270 BLEDevice* device, 337 BLEDevice* device,
271 GattService* parent_service, 338 GattService* parent_service,
272 const BTH_LE_UUID& uuid) { 339 const BTH_LE_UUID& uuid) {
273 CHECK(device); 340 CHECK(device);
274 341
275 GattService* service = new GattService(); 342 GattService* service = new GattService();
276 PBTH_LE_GATT_SERVICE service_info = new BTH_LE_GATT_SERVICE[1]; 343 PBTH_LE_GATT_SERVICE service_info = new BTH_LE_GATT_SERVICE[1];
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 GattCharacteristic* win_characteristic = new GattCharacteristic(); 402 GattCharacteristic* win_characteristic = new GattCharacteristic();
336 PBTH_LE_GATT_CHARACTERISTIC win_characteristic_info = 403 PBTH_LE_GATT_CHARACTERISTIC win_characteristic_info =
337 new BTH_LE_GATT_CHARACTERISTIC[1]; 404 new BTH_LE_GATT_CHARACTERISTIC[1];
338 *win_characteristic_info = characteristic; 405 *win_characteristic_info = characteristic;
339 (win_characteristic->characteristic_info).reset(win_characteristic_info); 406 (win_characteristic->characteristic_info).reset(win_characteristic_info);
340 win_characteristic->characteristic_info->AttributeHandle = 407 win_characteristic->characteristic_info->AttributeHandle =
341 GenerateAUniqueAttributeHandle(device_address); 408 GenerateAUniqueAttributeHandle(device_address);
342 parent_service->included_characteristics[std::to_string( 409 parent_service->included_characteristics[std::to_string(
343 win_characteristic->characteristic_info->AttributeHandle)] = 410 win_characteristic->characteristic_info->AttributeHandle)] =
344 make_scoped_ptr(win_characteristic); 411 make_scoped_ptr(win_characteristic);
412 // Set default empty value.
413 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value =
414 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
415 new UCHAR[sizeof(BTH_LE_GATT_CHARACTERISTIC_VALUE)]);
416 win_value->DataSize = 0;
417 win_characteristic->value.reset(win_value);
345 return win_characteristic; 418 return win_characteristic;
346 } 419 }
347 420
348 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteriscRemove( 421 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteriscRemove(
349 GattService* parent_service, 422 GattService* parent_service,
350 std::string attribute_handle) { 423 std::string attribute_handle) {
351 CHECK(parent_service); 424 CHECK(parent_service);
352 parent_service->included_characteristics.erase(attribute_handle); 425 parent_service->included_characteristics.erase(attribute_handle);
353 } 426 }
354 427
355 GattCharacteristic* 428 GattCharacteristic*
356 BluetoothLowEnergyWrapperFake::GetSimulatedGattCharacteristic( 429 BluetoothLowEnergyWrapperFake::GetSimulatedGattCharacteristic(
357 GattService* parent_service, 430 GattService* parent_service,
358 std::string attribute_handle) { 431 std::string attribute_handle) {
359 CHECK(parent_service); 432 CHECK(parent_service);
360 GattCharacteristicsMap::iterator it = 433 GattCharacteristicsMap::iterator it =
361 parent_service->included_characteristics.find(attribute_handle); 434 parent_service->included_characteristics.find(attribute_handle);
362 if (it != parent_service->included_characteristics.end()) 435 if (it != parent_service->included_characteristics.end())
363 return it->second.get(); 436 return it->second.get();
364 return nullptr; 437 return nullptr;
365 } 438 }
366 439
367 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicValue( 440 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicValue(
368 GattCharacteristic* characteristic, 441 GattCharacteristic* characteristic,
369 const std::vector<uint8_t>& value) { 442 const std::vector<uint8_t>& value) {
370 CHECK(characteristic); 443 GattCharacteristic* target_characteristic = characteristic;
444 if (target_characteristic == nullptr)
445 target_characteristic = remembered_characteristic_;
446 CHECK(target_characteristic);
447
371 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = 448 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value =
372 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( 449 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)(
373 new UCHAR[value.size() + sizeof(ULONG)]); 450 new UCHAR[value.size() + sizeof(ULONG)]);
374 win_value->DataSize = (ULONG)value.size(); 451 win_value->DataSize = (ULONG)value.size();
375 for (std::size_t i = 0; i < value.size(); i++) 452 for (std::size_t i = 0; i < value.size(); i++)
376 win_value->Data[i] = value[i]; 453 win_value->Data[i] = value[i];
377 characteristic->value.reset(win_value); 454 target_characteristic->value.reset(win_value);
455 }
456
457 void BluetoothLowEnergyWrapperFake::
458 SimulateCharacteristicValueChangeNotification(
459 GattCharacteristic* characteristic) {
460 GattCharacteristic* target_characteristic = characteristic;
461 if (target_characteristic == nullptr)
462 target_characteristic = remembered_characteristic_;
463 CHECK(target_characteristic);
464 for (const auto& observer : target_characteristic->observers) {
465 GattCharacteristicObserverTable::const_iterator it =
466 gatt_characteristic_observers_.find(observer);
467 // Check if |observer| has been unregistered by UnregisterGattEvent.
468 if (it != gatt_characteristic_observers_.end()) {
469 BLUETOOTH_GATT_VALUE_CHANGED_EVENT event;
470 event.ChangedAttributeHandle =
471 target_characteristic->characteristic_info->AttributeHandle;
472 event.CharacteristicValueDataSize =
473 target_characteristic->value->DataSize + sizeof(ULONG);
474 event.CharacteristicValue = target_characteristic->value.get();
475 it->second->callback(CharacteristicValueChangedEvent, &event,
476 it->second->context);
477 }
478 }
378 } 479 }
379 480
380 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicReadError( 481 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicReadError(
381 GattCharacteristic* characteristic, 482 GattCharacteristic* characteristic,
382 HRESULT error) { 483 HRESULT error) {
383 CHECK(characteristic); 484 CHECK(characteristic);
384 characteristic->read_errors.push_back(error); 485 characteristic->read_errors.push_back(error);
385 } 486 }
386 487
387 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicWriteError( 488 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicWriteError(
388 GattCharacteristic* characteristic, 489 GattCharacteristic* characteristic,
389 HRESULT error) { 490 HRESULT error) {
390 CHECK(characteristic); 491 CHECK(characteristic);
391 characteristic->write_errors.push_back(error); 492 characteristic->write_errors.push_back(error);
392 } 493 }
393 494
495 void BluetoothLowEnergyWrapperFake::RememberCharacteristicForSubsequentAction(
496 GattService* parent_service,
497 std::string attribute_handle) {
498 CHECK(parent_service);
499 remembered_characteristic_ =
500 parent_service->included_characteristics[attribute_handle].get();
501 CHECK(remembered_characteristic_);
502 }
503
394 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor( 504 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor(
395 std::string device_address, 505 std::string device_address,
396 GattCharacteristic* characteristic, 506 GattCharacteristic* characteristic,
397 const BTH_LE_UUID& uuid) { 507 const BTH_LE_UUID& uuid) {
398 scoped_ptr<GattDescriptor> descriptor(new GattDescriptor()); 508 scoped_ptr<GattDescriptor> descriptor(new GattDescriptor());
399 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]); 509 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]);
400 descriptor->descriptor_info->DescriptorUuid = uuid; 510 descriptor->descriptor_info->DescriptorUuid = uuid;
401 descriptor->descriptor_info->AttributeHandle = 511 descriptor->descriptor_info->AttributeHandle =
402 GenerateAUniqueAttributeHandle(device_address); 512 GenerateAUniqueAttributeHandle(device_address);
403 characteristic->included_descriptors[std::to_string( 513 characteristic->included_descriptors[std::to_string(
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( 618 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString(
509 const BLUETOOTH_ADDRESS& btha) { 619 const BLUETOOTH_ADDRESS& btha) {
510 std::string result = base::StringPrintf( 620 std::string result = base::StringPrintf(
511 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4], 621 "%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]); 622 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]);
513 return result; 623 return result;
514 } 624 }
515 625
516 } // namespace win 626 } // namespace win
517 } // namespace device 627 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_low_energy_win_fake.h ('k') | device/bluetooth/bluetooth_remote_gatt_characteristic_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698