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 if (observer_) |
| 234 observer_->onWriteGattCharacteristicValue(win_value); |
| 235 return S_OK; |
| 236 } |
| 237 |
180 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice( | 238 BLEDevice* BluetoothLowEnergyWrapperFake::SimulateBLEDevice( |
181 std::string device_name, | 239 std::string device_name, |
182 BLUETOOTH_ADDRESS device_address) { | 240 BLUETOOTH_ADDRESS device_address) { |
183 BLEDevice* device = new BLEDevice(); | 241 BLEDevice* device = new BLEDevice(); |
184 BluetoothLowEnergyDeviceInfo* device_info = | 242 BluetoothLowEnergyDeviceInfo* device_info = |
185 new BluetoothLowEnergyDeviceInfo(); | 243 new BluetoothLowEnergyDeviceInfo(); |
186 std::string string_device_address = | 244 std::string string_device_address = |
187 BluetoothAddressToCanonicalString(device_address); | 245 BluetoothAddressToCanonicalString(device_address); |
188 device_info->path = | 246 device_info->path = |
189 base::FilePath(GenerateBLEDevicePath(string_device_address)); | 247 base::FilePath(GenerateBLEDevicePath(string_device_address)); |
190 device_info->friendly_name = device_name; | 248 device_info->friendly_name = device_name; |
191 device_info->address = device_address; | 249 device_info->address = device_address; |
192 device->device_info.reset(device_info); | 250 device->device_info.reset(device_info); |
193 simulated_devices_[string_device_address] = make_scoped_ptr(device); | 251 simulated_devices_[string_device_address] = make_scoped_ptr(device); |
194 return device; | 252 return device; |
195 } | 253 } |
196 | 254 |
197 BLEDevice* BluetoothLowEnergyWrapperFake::GetSimulatedBLEDevice( | 255 BLEDevice* BluetoothLowEnergyWrapperFake::GetSimulatedBLEDevice( |
198 std::string device_address) { | 256 std::string device_address) { |
199 BLEDevicesMap::iterator it_d = simulated_devices_.find(device_address); | 257 BLEDevicesMap::iterator it_d = simulated_devices_.find(device_address); |
200 if (it_d == simulated_devices_.end()) | 258 if (it_d == simulated_devices_.end()) |
201 return nullptr; | 259 return nullptr; |
202 return it_d->second.get(); | 260 return it_d->second.get(); |
203 } | 261 } |
204 | 262 |
| 263 void BluetoothLowEnergyWrapperFake::RemoveSimulatedBLEDevice( |
| 264 std::string device_address) { |
| 265 simulated_devices_.erase(device_address); |
| 266 } |
| 267 |
205 GattService* BluetoothLowEnergyWrapperFake::SimulateGattService( | 268 GattService* BluetoothLowEnergyWrapperFake::SimulateGattService( |
206 BLEDevice* device, | 269 BLEDevice* device, |
207 GattService* parent_service, | 270 GattService* parent_service, |
208 const BTH_LE_UUID& uuid) { | 271 const BTH_LE_UUID& uuid) { |
209 CHECK(device); | 272 CHECK(device); |
210 | 273 |
211 GattService* service = new GattService(); | 274 GattService* service = new GattService(); |
212 PBTH_LE_GATT_SERVICE service_info = new BTH_LE_GATT_SERVICE[1]; | 275 PBTH_LE_GATT_SERVICE service_info = new BTH_LE_GATT_SERVICE[1]; |
213 std::string string_device_address = | 276 std::string string_device_address = |
214 BluetoothAddressToCanonicalString(device->device_info->address); | 277 BluetoothAddressToCanonicalString(device->device_info->address); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 GattService* parent_service, | 356 GattService* parent_service, |
294 std::string attribute_handle) { | 357 std::string attribute_handle) { |
295 CHECK(parent_service); | 358 CHECK(parent_service); |
296 GattCharacteristicsMap::iterator it = | 359 GattCharacteristicsMap::iterator it = |
297 parent_service->included_characteristics.find(attribute_handle); | 360 parent_service->included_characteristics.find(attribute_handle); |
298 if (it != parent_service->included_characteristics.end()) | 361 if (it != parent_service->included_characteristics.end()) |
299 return it->second.get(); | 362 return it->second.get(); |
300 return nullptr; | 363 return nullptr; |
301 } | 364 } |
302 | 365 |
| 366 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicValue( |
| 367 GattCharacteristic* characteristic, |
| 368 const std::vector<uint8_t>& value) { |
| 369 CHECK(characteristic); |
| 370 PBTH_LE_GATT_CHARACTERISTIC_VALUE win_value = |
| 371 (PBTH_LE_GATT_CHARACTERISTIC_VALUE)( |
| 372 new UCHAR[value.size() + sizeof(ULONG)]); |
| 373 win_value->DataSize = value.size(); |
| 374 for (std::size_t i = 0; i < value.size(); i++) |
| 375 win_value->Data[i] = value[i]; |
| 376 characteristic->value.reset(win_value); |
| 377 } |
| 378 |
| 379 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicReadError( |
| 380 GattCharacteristic* characteristic, |
| 381 HRESULT error) { |
| 382 CHECK(characteristic); |
| 383 characteristic->read_errors.push_back(error); |
| 384 } |
| 385 |
| 386 void BluetoothLowEnergyWrapperFake::SimulateGattCharacteristicWriteError( |
| 387 GattCharacteristic* characteristic, |
| 388 HRESULT error) { |
| 389 CHECK(characteristic); |
| 390 characteristic->write_errors.push_back(error); |
| 391 } |
| 392 |
303 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor( | 393 void BluetoothLowEnergyWrapperFake::SimulateGattDescriptor( |
304 std::string device_address, | 394 std::string device_address, |
305 GattCharacteristic* characteristic, | 395 GattCharacteristic* characteristic, |
306 const BTH_LE_UUID& uuid) { | 396 const BTH_LE_UUID& uuid) { |
307 GattDescriptor* descriptor = new GattDescriptor(); | 397 GattDescriptor* descriptor = new GattDescriptor(); |
308 BTH_LE_GATT_DESCRIPTOR* win_descriptors_info = new BTH_LE_GATT_DESCRIPTOR[1]; | 398 BTH_LE_GATT_DESCRIPTOR* win_descriptors_info = new BTH_LE_GATT_DESCRIPTOR[1]; |
309 win_descriptors_info->DescriptorUuid = uuid; | 399 win_descriptors_info->DescriptorUuid = uuid; |
310 win_descriptors_info->AttributeHandle = | 400 win_descriptors_info->AttributeHandle = |
311 GenerateAUniqueAttributeHandle(device_address); | 401 GenerateAUniqueAttributeHandle(device_address); |
312 descriptor->descriptor_info.reset(win_descriptors_info); | 402 descriptor->descriptor_info.reset(win_descriptors_info); |
313 characteristic->included_descriptors[std::to_string( | 403 characteristic->included_descriptors[std::to_string( |
314 win_descriptors_info->AttributeHandle)] = make_scoped_ptr(descriptor); | 404 win_descriptors_info->AttributeHandle)] = make_scoped_ptr(descriptor); |
315 } | 405 } |
316 | 406 |
| 407 void BluetoothLowEnergyWrapperFake::AddObserver(Observer* observer) { |
| 408 observer_ = observer; |
| 409 } |
| 410 |
317 GattCharacteristic* | 411 GattCharacteristic* |
318 BluetoothLowEnergyWrapperFake::GetSimulatedGattCharacteristic( | 412 BluetoothLowEnergyWrapperFake::GetSimulatedGattCharacteristic( |
319 base::FilePath& service_path, | 413 base::FilePath& service_path, |
320 const PBTH_LE_GATT_CHARACTERISTIC characteristic) { | 414 const PBTH_LE_GATT_CHARACTERISTIC characteristic) { |
321 base::string16 device_address = | 415 base::string16 device_address = |
322 ExtractDeviceAddressFromDevicePath(service_path.value()); | 416 ExtractDeviceAddressFromDevicePath(service_path.value()); |
323 BLEDevice* target_device = GetSimulatedBLEDevice( | 417 BLEDevice* target_device = GetSimulatedBLEDevice( |
324 std::string(device_address.begin(), device_address.end())); | 418 std::string(device_address.begin(), device_address.end())); |
325 if (target_device == nullptr) | 419 if (target_device == nullptr) |
326 return nullptr; | 420 return nullptr; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( | 508 std::string BluetoothLowEnergyWrapperFake::BluetoothAddressToCanonicalString( |
415 const BLUETOOTH_ADDRESS& btha) { | 509 const BLUETOOTH_ADDRESS& btha) { |
416 std::string result = base::StringPrintf( | 510 std::string result = base::StringPrintf( |
417 "%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], |
418 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]); |
419 return result; | 513 return result; |
420 } | 514 } |
421 | 515 |
422 } // namespace win | 516 } // namespace win |
423 } // namespace device | 517 } // namespace device |
OLD | NEW |