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 BluetoothLowEnergyWrapperFake::BluetoothLowEnergyWrapperFake() {} | 30 BluetoothLowEnergyWrapperFake::BluetoothLowEnergyWrapperFake() |
| 31 : observer_(nullptr) {} |
31 BluetoothLowEnergyWrapperFake::~BluetoothLowEnergyWrapperFake() {} | 32 BluetoothLowEnergyWrapperFake::~BluetoothLowEnergyWrapperFake() {} |
32 | 33 |
33 bool BluetoothLowEnergyWrapperFake::IsBluetoothLowEnergySupported() { | 34 bool BluetoothLowEnergyWrapperFake::IsBluetoothLowEnergySupported() { |
34 return true; | 35 return true; |
35 } | 36 } |
36 | 37 |
37 bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyDevices( | 38 bool BluetoothLowEnergyWrapperFake::EnumerateKnownBluetoothLowEnergyDevices( |
38 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, | 39 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, |
39 std::string* error) { | 40 std::string* error) { |
40 if (!IsBluetoothLowEnergySupported()) { | 41 if (!IsBluetoothLowEnergySupported()) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 return true; | 121 return true; |
121 } | 122 } |
122 | 123 |
123 HRESULT BluetoothLowEnergyWrapperFake::ReadCharacteristicsOfAService( | 124 HRESULT BluetoothLowEnergyWrapperFake::ReadCharacteristicsOfAService( |
124 base::FilePath& service_path, | 125 base::FilePath& service_path, |
125 const PBTH_LE_GATT_SERVICE service, | 126 const PBTH_LE_GATT_SERVICE service, |
126 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC>* out_included_characteristics, | 127 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC>* out_included_characteristics, |
127 USHORT* out_counts) { | 128 USHORT* out_counts) { |
128 base::string16 device_address = | 129 base::string16 device_address = |
129 ExtractDeviceAddressFromDevicePath(service_path.value()); | 130 ExtractDeviceAddressFromDevicePath(service_path.value()); |
| 131 BLEDevice* target_device = GetSimulatedBLEDevice( |
| 132 std::string(device_address.begin(), device_address.end())); |
| 133 if (target_device == nullptr) |
| 134 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND); |
130 const std::vector<std::string> service_att_handles = | 135 const std::vector<std::string> service_att_handles = |
131 ExtractServiceAttributeHandlesFromDevicePath(service_path.value()); | 136 ExtractServiceAttributeHandlesFromDevicePath(service_path.value()); |
132 GattService* target_service = GetSimulatedGattService( | 137 GattService* target_service = |
133 GetSimulatedBLEDevice( | 138 GetSimulatedGattService(target_device, service_att_handles); |
134 std::string(device_address.begin(), device_address.end())), | |
135 service_att_handles); | |
136 if (target_service == nullptr) | 139 if (target_service == nullptr) |
137 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND); | 140 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND); |
138 | 141 |
139 std::size_t number_of_included_characteristic = | 142 std::size_t number_of_included_characteristic = |
140 target_service->included_characteristics.size(); | 143 target_service->included_characteristics.size(); |
141 if (number_of_included_characteristic) { | 144 if (number_of_included_characteristic) { |
142 *out_counts = (USHORT)number_of_included_characteristic; | 145 *out_counts = (USHORT)number_of_included_characteristic; |
143 out_included_characteristics->reset( | 146 out_included_characteristics->reset( |
144 new BTH_LE_GATT_CHARACTERISTIC[number_of_included_characteristic]); | 147 new BTH_LE_GATT_CHARACTERISTIC[number_of_included_characteristic]); |
145 std::size_t i = 0; | 148 std::size_t i = 0; |
(...skipping 24 matching lines...) Expand all Loading... |
170 *out_counts = USHORT(number_of_included_descriptors); | 173 *out_counts = USHORT(number_of_included_descriptors); |
171 std::size_t i = 0; | 174 std::size_t i = 0; |
172 for (const auto& d : target_characteristic->included_descriptors) { | 175 for (const auto& d : target_characteristic->included_descriptors) { |
173 win_descriptors_info[i] = *(d.second->descriptor_info); | 176 win_descriptors_info[i] = *(d.second->descriptor_info); |
174 i++; | 177 i++; |
175 } | 178 } |
176 out_included_descriptors->reset(win_descriptors_info); | 179 out_included_descriptors->reset(win_descriptors_info); |
177 return S_OK; | 180 return S_OK; |
178 } | 181 } |
179 | 182 |
| 183 HRESULT BluetoothLowEnergyWrapperFake::ReadTheValueOfACharacteristic( |
| 184 base::FilePath& service_path, |
| 185 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 186 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC_VALUE>* out_value) { |
| 187 GattCharacteristic* target_characteristic = |
| 188 GetSimulatedGattCharacteristic(service_path, characteristic); |
| 189 if (target_characteristic == nullptr) |
| 190 return HRESULT_FROM_WIN32(ERROR_NOT_FOUND); |
| 191 |
| 192 // Return error simulated by SimulateGattCharacteristicReadError. |
| 193 if (target_characteristic->read_errors.size()) { |
| 194 HRESULT hr = target_characteristic->read_errors[0]; |
| 195 target_characteristic->read_errors.erase( |
| 196 target_characteristic->read_errors.begin()); |
| 197 return hr; |
| 198 } |
| 199 |
| 200 PBTH_LE_GATT_CHARACTERISTIC_VALUE ret_value = |
| 201 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( |
| 202 new UCHAR[sizeof(ULONG) + target_characteristic->value->DataSize]); |
| 203 ret_value->DataSize = target_characteristic->value->DataSize; |
| 204 for (ULONG i = 0; i < ret_value->DataSize; i++) |
| 205 ret_value->Data[i] = target_characteristic->value->Data[i]; |
| 206 out_value->reset(ret_value); |
| 207 return S_OK; |
| 208 } |
| 209 |
| 210 HRESULT BluetoothLowEnergyWrapperFake::WriteTheValueOfACharacteristic( |
| 211 base::FilePath& service_path, |
| 212 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 213 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value) { |
| 214 GattCharacteristic* target_characteristic = |
| 215 GetSimulatedGattCharacteristic(service_path, characteristic); |
| 216 if (target_characteristic == nullptr) |
| 217 return ERROR_NOT_FOUND; |
| 218 |
| 219 // Return error simulated by SimulateGattCharacteristicWriteError. |
| 220 if (target_characteristic->write_errors.size()) { |
| 221 HRESULT hr = *(target_characteristic->write_errors.begin()); |
| 222 target_characteristic->write_errors.erase( |
| 223 target_characteristic->write_errors.begin()); |
| 224 return hr; |
| 225 } |
| 226 |
| 227 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = |
| 228 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( |
| 229 new UCHAR[new_value->DataSize + sizeof(ULONG)]); |
| 230 for (ULONG i = 0; i < new_value->DataSize; i++) |
| 231 win_value->Data[i] = new_value->Data[i]; |
| 232 win_value->DataSize = new_value->DataSize; |
| 233 target_characteristic->value.reset(win_value); |
| 234 if (observer_) |
| 235 observer_->onWriteGattCharacteristicValue(win_value); |
| 236 return S_OK; |
| 237 } |
| 238 |
180 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice( | 239 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice( |
181 std::string device_name, | 240 std::string device_name, |
182 BLUETOOTH_ADDRESS device_address) { | 241 BLUETOOTH_ADDRESS device_address) { |
183 BLEDevice* device = new BLEDevice(); | 242 BLEDevice* device = new BLEDevice(); |
184 BluetoothLowEnergyDeviceInfo* device_info = | 243 BluetoothLowEnergyDeviceInfo* device_info = |
185 new BluetoothLowEnergyDeviceInfo(); | 244 new BluetoothLowEnergyDeviceInfo(); |
186 std::string string_device_address = | 245 std::string string_device_address = |
187 BluetoothAddressToCanonicalString(device_address); | 246 BluetoothAddressToCanonicalString(device_address); |
188 device_info->path = | 247 device_info->path = |
189 base::FilePath(GenerateBLEDevicePath(string_device_address)); | 248 base::FilePath(GenerateBLEDevicePath(string_device_address)); |
190 device_info->friendly_name = device_name; | 249 device_info->friendly_name = device_name; |
191 device_info->address = device_address; | 250 device_info->address = device_address; |
192 device->device_info.reset(device_info); | 251 device->device_info.reset(device_info); |
193 simulated_devices_[string_device_address] = make_scoped_ptr(device); | 252 simulated_devices_[string_device_address] = make_scoped_ptr(device); |
194 return device; | 253 return device; |
195 } | 254 } |
196 | 255 |
197 BLEDevice* BluetoothLowEnergyWrapperFake::GetSimulatedBLEDevice( | 256 BLEDevice* BluetoothLowEnergyWrapperFake::GetSimulatedBLEDevice( |
198 std::string device_address) { | 257 std::string device_address) { |
199 BLEDevicesMap::iterator it_d = simulated_devices_.find(device_address); | 258 BLEDevicesMap::iterator it_d = simulated_devices_.find(device_address); |
200 if (it_d == simulated_devices_.end()) | 259 if (it_d == simulated_devices_.end()) |
201 return nullptr; | 260 return nullptr; |
202 return it_d->second.get(); | 261 return it_d->second.get(); |
203 } | 262 } |
204 | 263 |
| 264 void BluetoothLowEnergyWrapperFake::RemoveSimulatedBLEDevice( |
| 265 std::string device_address) { |
| 266 simulated_devices_.erase(device_address); |
| 267 } |
| 268 |
205 GattService* BluetoothLowEnergyWrapperFake::SimulateGattService( | 269 GattService* BluetoothLowEnergyWrapperFake::SimulateGattService( |
206 BLEDevice* device, | 270 BLEDevice* device, |
207 GattService* parent_service, | 271 GattService* parent_service, |
208 const BTH_LE_UUID& uuid) { | 272 const BTH_LE_UUID& uuid) { |
209 CHECK(device); | 273 CHECK(device); |
210 | 274 |
211 GattService* service = new GattService(); | 275 GattService* service = new GattService(); |
212 PBTH_LE_GATT_SERVICE service_info = new BTH_LE_GATT_SERVICE[1]; | 276 PBTH_LE_GATT_SERVICE service_info = new BTH_LE_GATT_SERVICE[1]; |
213 std::string string_device_address = | 277 std::string string_device_address = |
214 BluetoothAddressToCanonicalString(device->device_info->address); | 278 BluetoothAddressToCanonicalString(device->device_info->address); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 GattService* parent_service, | 357 GattService* parent_service, |
294 std::string attribute_handle) { | 358 std::string attribute_handle) { |
295 CHECK(parent_service); | 359 CHECK(parent_service); |
296 GattCharacteristicsMap::iterator it = | 360 GattCharacteristicsMap::iterator it = |
297 parent_service->included_characteristics.find(attribute_handle); | 361 parent_service->included_characteristics.find(attribute_handle); |
298 if (it != parent_service->included_characteristics.end()) | 362 if (it != parent_service->included_characteristics.end()) |
299 return it->second.get(); | 363 return it->second.get(); |
300 return nullptr; | 364 return nullptr; |
301 } | 365 } |
302 | 366 |
| 367 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicValue( |
| 368 GattCharacteristic* characteristic, |
| 369 const std::vector<uint8_t>& value) { |
| 370 CHECK(characteristic); |
| 371 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = |
| 372 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( |
| 373 new UCHAR[value.size() + sizeof(ULONG)]); |
| 374 win_value->DataSize = (ULONG)value.size(); |
| 375 for (std::size_t i = 0; i < value.size(); i++) |
| 376 win_value->Data[i] = value[i]; |
| 377 characteristic->value.reset(win_value); |
| 378 } |
| 379 |
| 380 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicReadError( |
| 381 GattCharacteristic* characteristic, |
| 382 HRESULT error) { |
| 383 CHECK(characteristic); |
| 384 characteristic->read_errors.push_back(error); |
| 385 } |
| 386 |
| 387 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicWriteError( |
| 388 GattCharacteristic* characteristic, |
| 389 HRESULT error) { |
| 390 CHECK(characteristic); |
| 391 characteristic->write_errors.push_back(error); |
| 392 } |
| 393 |
303 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor( | 394 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor( |
304 std::string device_address, | 395 std::string device_address, |
305 GattCharacteristic* characteristic, | 396 GattCharacteristic* characteristic, |
306 const BTH_LE_UUID& uuid) { | 397 const BTH_LE_UUID& uuid) { |
307 scoped_ptr<GattDescriptor> descriptor(new GattDescriptor()); | 398 scoped_ptr<GattDescriptor> descriptor(new GattDescriptor()); |
308 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]); | 399 descriptor->descriptor_info.reset(new BTH_LE_GATT_DESCRIPTOR[1]); |
309 descriptor->descriptor_info->DescriptorUuid = uuid; | 400 descriptor->descriptor_info->DescriptorUuid = uuid; |
310 descriptor->descriptor_info->AttributeHandle = | 401 descriptor->descriptor_info->AttributeHandle = |
311 GenerateAUniqueAttributeHandle(device_address); | 402 GenerateAUniqueAttributeHandle(device_address); |
312 characteristic->included_descriptors[std::to_string( | 403 characteristic->included_descriptors[std::to_string( |
313 descriptor->descriptor_info->AttributeHandle)] = std::move(descriptor); | 404 descriptor->descriptor_info->AttributeHandle)] = std::move(descriptor); |
314 } | 405 } |
315 | 406 |
| 407 void BluetoothLowEnergyWrapperFake::AddObserver(Observer* observer) { |
| 408 observer_ = observer; |
| 409 } |
| 410 |
316 GattCharacteristic* | 411 GattCharacteristic* |
317 BluetoothLowEnergyWrapperFake::GetSimulatedGattCharacteristic( | 412 BluetoothLowEnergyWrapperFake::GetSimulatedGattCharacteristic( |
318 base::FilePath& service_path, | 413 base::FilePath& service_path, |
319 const PBTH_LE_GATT_CHARACTERISTIC characteristic) { | 414 const PBTH_LE_GATT_CHARACTERISTIC characteristic) { |
320 base::string16 device_address = | 415 base::string16 device_address = |
321 ExtractDeviceAddressFromDevicePath(service_path.value()); | 416 ExtractDeviceAddressFromDevicePath(service_path.value()); |
322 BLEDevice* target_device = GetSimulatedBLEDevice( | 417 BLEDevice* target_device = GetSimulatedBLEDevice( |
323 std::string(device_address.begin(), device_address.end())); | 418 std::string(device_address.begin(), device_address.end())); |
324 if (target_device == nullptr) | 419 if (target_device == nullptr) |
325 return nullptr; | 420 return nullptr; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( | 508 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( |
414 const BLUETOOTH_ADDRESS& btha) { | 509 const BLUETOOTH_ADDRESS& btha) { |
415 std::string result = base::StringPrintf( | 510 std::string result = base::StringPrintf( |
416 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4], | 511 "%02X:%02X:%02X:%02X:%02X:%02X", btha.rgBytes[5], btha.rgBytes[4], |
417 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]); | 512 btha.rgBytes[3], btha.rgBytes[2], btha.rgBytes[1], btha.rgBytes[0]); |
418 return result; | 513 return result; |
419 } | 514 } |
420 | 515 |
421 } // namespace win | 516 } // namespace win |
422 } // namespace device | 517 } // namespace device |
OLD | NEW |