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 <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 BluetoothLowEnergyWrapperFake::BluetoothLowEnergyWrapperFake() | 36 BluetoothLowEnergyWrapperFake::BluetoothLowEnergyWrapperFake() |
37 : observer_(nullptr) {} | 37 : observer_(nullptr) {} |
38 BluetoothLowEnergyWrapperFake::~BluetoothLowEnergyWrapperFake() {} | 38 BluetoothLowEnergyWrapperFake::~BluetoothLowEnergyWrapperFake() {} |
39 | 39 |
40 bool BluetoothLowEnergyWrapperFake::IsBluetoothLowEnergySupported() { | 40 bool BluetoothLowEnergyWrapperFake::IsBluetoothLowEnergySupported() { |
41 return true; | 41 return true; |
42 } | 42 } |
43 | 43 |
44 bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyDevices( | 44 bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyDevices( |
45 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, | 45 std::vector<std::unique_ptr<BluetoothLowEnergyDeviceInfo>>* devices, |
46 std::string* error) { | 46 std::string* error) { |
47 if (!IsBluetoothLowEnergySupported()) { | 47 if (!IsBluetoothLowEnergySupported()) { |
48 *error = kPlatformNotSupported; | 48 *error = kPlatformNotSupported; |
49 return false; | 49 return false; |
50 } | 50 } |
51 | 51 |
52 for (auto& device : simulated_devices_) { | 52 for (auto& device : simulated_devices_) { |
53 if (device.second->marked_as_deleted) | 53 if (device.second->marked_as_deleted) |
54 continue; | 54 continue; |
55 BluetoothLowEnergyDeviceInfo* device_info = | 55 auto device_info = base::MakeUnique<BluetoothLowEnergyDeviceInfo>(); |
56 new BluetoothLowEnergyDeviceInfo(); | |
57 *device_info = *(device.second->device_info); | 56 *device_info = *(device.second->device_info); |
58 devices->push_back(device_info); | 57 devices->push_back(std::move(device_info)); |
59 } | 58 } |
60 return true; | 59 return true; |
61 } | 60 } |
62 | 61 |
63 bool BluetoothLowEnergyWrapperFake:: | 62 bool BluetoothLowEnergyWrapperFake:: |
64 EnumerateKnownBluetoothLowEnergyGattServiceDevices( | 63 EnumerateKnownBluetoothLowEnergyGattServiceDevices( |
65 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, | 64 std::vector<std::unique_ptr<BluetoothLowEnergyDeviceInfo>>* devices, |
66 std::string* error) { | 65 std::string* error) { |
67 if (!IsBluetoothLowEnergySupported()) { | 66 if (!IsBluetoothLowEnergySupported()) { |
68 *error = kPlatformNotSupported; | 67 *error = kPlatformNotSupported; |
69 return false; | 68 return false; |
70 } | 69 } |
71 | 70 |
72 for (auto& device : simulated_devices_) { | 71 for (auto& device : simulated_devices_) { |
73 for (auto& service : device.second->primary_services) { | 72 for (auto& service : device.second->primary_services) { |
74 BluetoothLowEnergyDeviceInfo* device_info = | 73 auto device_info = base::MakeUnique<BluetoothLowEnergyDeviceInfo>(); |
75 new BluetoothLowEnergyDeviceInfo(); | |
76 *device_info = *(device.second->device_info); | 74 *device_info = *(device.second->device_info); |
77 base::string16 path = GenerateGattServiceDevicePath( | 75 base::string16 path = GenerateGattServiceDevicePath( |
78 device.second->device_info->path.value(), | 76 device.second->device_info->path.value(), |
79 service.second->service_info->AttributeHandle); | 77 service.second->service_info->AttributeHandle); |
80 device_info->path = base::FilePath(path); | 78 device_info->path = base::FilePath(path); |
81 devices->push_back(device_info); | 79 devices->push_back(std::move(device_info)); |
82 } | 80 } |
83 } | 81 } |
84 return true; | 82 return true; |
85 } | 83 } |
86 | 84 |
87 bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyServices( | 85 bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyServices( |
88 const base::FilePath& device_path, | 86 const base::FilePath& device_path, |
89 ScopedVector<BluetoothLowEnergyServiceInfo>* services, | 87 std::vector<std::unique_ptr<BluetoothLowEnergyServiceInfo>>* services, |
90 std::string* error) { | 88 std::string* error) { |
91 if (!IsBluetoothLowEnergySupported()) { | 89 if (!IsBluetoothLowEnergySupported()) { |
92 *error = kPlatformNotSupported; | 90 *error = kPlatformNotSupported; |
93 return false; | 91 return false; |
94 } | 92 } |
95 | 93 |
96 base::string16 device_address = | 94 base::string16 device_address = |
97 ExtractDeviceAddressFromDevicePath(device_path.value()); | 95 ExtractDeviceAddressFromDevicePath(device_path.value()); |
98 std::vector<std::string> service_attribute_handles = | 96 std::vector<std::string> service_attribute_handles = |
99 ExtractServiceAttributeHandlesFromDevicePath(device_path.value()); | 97 ExtractServiceAttributeHandlesFromDevicePath(device_path.value()); |
100 | 98 |
101 BLEDevicesMap::iterator it_d = simulated_devices_.find( | 99 BLEDevicesMap::iterator it_d = simulated_devices_.find( |
102 std::string(device_address.begin(), device_address.end())); | 100 std::string(device_address.begin(), device_address.end())); |
103 CHECK(it_d != simulated_devices_.end()); | 101 CHECK(it_d != simulated_devices_.end()); |
104 | 102 |
105 // |service_attribute_handles| is empty means |device_path| is a BLE device | 103 // |service_attribute_handles| is empty means |device_path| is a BLE device |
106 // path, otherwise it is a BLE GATT service device path. | 104 // path, otherwise it is a BLE GATT service device path. |
107 if (service_attribute_handles.empty()) { | 105 if (service_attribute_handles.empty()) { |
108 // Return all primary services for BLE device. | 106 // Return all primary services for BLE device. |
109 for (auto& primary_service : it_d->second->primary_services) { | 107 for (auto& primary_service : it_d->second->primary_services) { |
110 BluetoothLowEnergyServiceInfo* service_info = | 108 auto service_info = base::MakeUnique<BluetoothLowEnergyServiceInfo>(); |
111 new BluetoothLowEnergyServiceInfo(); | |
112 service_info->uuid = primary_service.second->service_info->ServiceUuid; | 109 service_info->uuid = primary_service.second->service_info->ServiceUuid; |
113 service_info->attribute_handle = | 110 service_info->attribute_handle = |
114 primary_service.second->service_info->AttributeHandle; | 111 primary_service.second->service_info->AttributeHandle; |
115 services->push_back(service_info); | 112 services->push_back(std::move(service_info)); |
116 } | 113 } |
117 } else { | 114 } else { |
118 // Return corresponding GATT service for BLE GATT service device. | 115 // Return corresponding GATT service for BLE GATT service device. |
119 GattService* target_service = | 116 GattService* target_service = |
120 GetSimulatedGattService(it_d->second.get(), service_attribute_handles); | 117 GetSimulatedGattService(it_d->second.get(), service_attribute_handles); |
121 BluetoothLowEnergyServiceInfo* service_info = | 118 auto service_info = base::MakeUnique<BluetoothLowEnergyServiceInfo>(); |
122 new BluetoothLowEnergyServiceInfo(); | |
123 service_info->uuid = target_service->service_info->ServiceUuid; | 119 service_info->uuid = target_service->service_info->ServiceUuid; |
124 service_info->attribute_handle = | 120 service_info->attribute_handle = |
125 target_service->service_info->AttributeHandle; | 121 target_service->service_info->AttributeHandle; |
126 services->push_back(service_info); | 122 services->push_back(std::move(service_info)); |
127 } | 123 } |
128 | 124 |
129 return true; | 125 return true; |
130 } | 126 } |
131 | 127 |
132 HRESULT BluetoothLowEnergyWrapperFake::ReadCharacteristicsOfAService( | 128 HRESULT BluetoothLowEnergyWrapperFake::ReadCharacteristicsOfAService( |
133 base::FilePath& service_path, | 129 base::FilePath& service_path, |
134 const PBTH_LE_GATT_SERVICE service, | 130 const PBTH_LE_GATT_SERVICE service, |
135 std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC>* out_included_characteristics, | 131 std::unique_ptr<BTH_LE_GATT_CHARACTERISTIC>* out_included_characteristics, |
136 USHORT* out_counts) { | 132 USHORT* out_counts) { |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 CHECK(parent_service); | 512 CHECK(parent_service); |
517 remembered_characteristic_ = | 513 remembered_characteristic_ = |
518 parent_service->included_characteristics[attribute_handle].get(); | 514 parent_service->included_characteristics[attribute_handle].get(); |
519 CHECK(remembered_characteristic_); | 515 CHECK(remembered_characteristic_); |
520 } | 516 } |
521 | 517 |
522 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor( | 518 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor( |
523 std::string device_address, | 519 std::string device_address, |
524 GattCharacteristic* characteristic, | 520 GattCharacteristic* characteristic, |
525 const BTH_LE_UUID& uuid) { | 521 const BTH_LE_UUID& uuid) { |
526 std::unique_ptr<GattDescriptor> descriptor(new GattDescriptor()); | 522 auto descriptor = base::MakeUnique<GattDescriptor>(); |
527 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]); | 523 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]); |
528 descriptor->descriptor_info->DescriptorUuid = uuid; | 524 descriptor->descriptor_info->DescriptorUuid = uuid; |
529 descriptor->descriptor_info->AttributeHandle = | 525 descriptor->descriptor_info->AttributeHandle = |
530 GenerateAUniqueAttributeHandle(device_address); | 526 GenerateAUniqueAttributeHandle(device_address); |
531 characteristic->included_descriptors[std::to_string( | 527 characteristic->included_descriptors[std::to_string( |
532 descriptor->descriptor_info->AttributeHandle)] = std::move(descriptor); | 528 descriptor->descriptor_info->AttributeHandle)] = std::move(descriptor); |
533 } | 529 } |
534 | 530 |
535 void BluetoothLowEnergyWrapperFake::AddObserver(Observer* observer) { | 531 void BluetoothLowEnergyWrapperFake::AddObserver(Observer* observer) { |
536 observer_ = observer; | 532 observer_ = observer; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( | 632 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( |
637 const BLUETOOTH_ADDRESS& btha) { | 633 const BLUETOOTH_ADDRESS& btha) { |
638 std::string result = base::StringPrintf( | 634 std::string result = base::StringPrintf( |
639 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4], | 635 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4], |
640 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]); | 636 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]); |
641 return result; | 637 return result; |
642 } | 638 } |
643 | 639 |
644 } // namespace win | 640 } // namespace win |
645 } // namespace device | 641 } // namespace device |
OLD | NEW |