| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.h" | 5 #include "device/bluetooth/bluetooth_low_energy_win.h" |
| 6 | 6 |
| 7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "base/win/scoped_handle.h" | 10 #include "base/win/scoped_handle.h" |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 } | 164 } |
| 165 | 165 |
| 166 bool CollectBluetoothLowEnergyDeviceProperty( | 166 bool CollectBluetoothLowEnergyDeviceProperty( |
| 167 const ScopedDeviceInfoSetHandle& device_info_handle, | 167 const ScopedDeviceInfoSetHandle& device_info_handle, |
| 168 PSP_DEVINFO_DATA device_info_data, | 168 PSP_DEVINFO_DATA device_info_data, |
| 169 const DEVPROPKEY& key, | 169 const DEVPROPKEY& key, |
| 170 scoped_ptr<DevicePropertyValue>* value, | 170 scoped_ptr<DevicePropertyValue>* value, |
| 171 std::string* error) { | 171 std::string* error) { |
| 172 DWORD required_length; | 172 DWORD required_length; |
| 173 DEVPROPTYPE prop_type; | 173 DEVPROPTYPE prop_type; |
| 174 BOOL success = SetupDiGetDeviceProperty(device_info_handle, | 174 BOOL success = SetupDiGetDeviceProperty(device_info_handle.Get(), |
| 175 device_info_data, | 175 device_info_data, |
| 176 &key, | 176 &key, |
| 177 &prop_type, | 177 &prop_type, |
| 178 NULL, | 178 NULL, |
| 179 0, | 179 0, |
| 180 &required_length, | 180 &required_length, |
| 181 0); | 181 0); |
| 182 if (!CheckInsufficientBuffer(!!success, kDeviceInfoError, error)) | 182 if (!CheckInsufficientBuffer(!!success, kDeviceInfoError, error)) |
| 183 return false; | 183 return false; |
| 184 | 184 |
| 185 scoped_ptr<uint8_t[]> prop_value(new uint8_t[required_length]); | 185 scoped_ptr<uint8_t[]> prop_value(new uint8_t[required_length]); |
| 186 DWORD actual_length = required_length; | 186 DWORD actual_length = required_length; |
| 187 success = SetupDiGetDeviceProperty(device_info_handle, | 187 success = SetupDiGetDeviceProperty(device_info_handle.Get(), |
| 188 device_info_data, | 188 device_info_data, |
| 189 &key, | 189 &key, |
| 190 &prop_type, | 190 &prop_type, |
| 191 prop_value.get(), | 191 prop_value.get(), |
| 192 actual_length, | 192 actual_length, |
| 193 &required_length, | 193 &required_length, |
| 194 0); | 194 0); |
| 195 if (!CheckSuccess(!!success, kDeviceInfoError, error)) | 195 if (!CheckSuccess(!!success, kDeviceInfoError, error)) |
| 196 return false; | 196 return false; |
| 197 if (!CheckExpectedLength( | 197 if (!CheckExpectedLength( |
| 198 actual_length, required_length, kDeviceInfoError, error)) { | 198 actual_length, required_length, kDeviceInfoError, error)) { |
| 199 return false; | 199 return false; |
| 200 } | 200 } |
| 201 | 201 |
| 202 (*value) = scoped_ptr<DevicePropertyValue>( | 202 (*value) = scoped_ptr<DevicePropertyValue>( |
| 203 new DevicePropertyValue(prop_type, prop_value.Pass(), actual_length)); | 203 new DevicePropertyValue(prop_type, prop_value.Pass(), actual_length)); |
| 204 return true; | 204 return true; |
| 205 } | 205 } |
| 206 | 206 |
| 207 bool CollectBluetoothLowEnergyDeviceRegistryProperty( | 207 bool CollectBluetoothLowEnergyDeviceRegistryProperty( |
| 208 const ScopedDeviceInfoSetHandle& device_info_handle, | 208 const ScopedDeviceInfoSetHandle& device_info_handle, |
| 209 PSP_DEVINFO_DATA device_info_data, | 209 PSP_DEVINFO_DATA device_info_data, |
| 210 DWORD property_id, | 210 DWORD property_id, |
| 211 scoped_ptr<DeviceRegistryPropertyValue>* value, | 211 scoped_ptr<DeviceRegistryPropertyValue>* value, |
| 212 std::string* error) { | 212 std::string* error) { |
| 213 ULONG required_length = 0; | 213 ULONG required_length = 0; |
| 214 BOOL success = SetupDiGetDeviceRegistryProperty(device_info_handle, | 214 BOOL success = SetupDiGetDeviceRegistryProperty(device_info_handle.Get(), |
| 215 device_info_data, | 215 device_info_data, |
| 216 property_id, | 216 property_id, |
| 217 NULL, | 217 NULL, |
| 218 NULL, | 218 NULL, |
| 219 0, | 219 0, |
| 220 &required_length); | 220 &required_length); |
| 221 if (!CheckInsufficientBuffer(!!success, kDeviceInfoError, error)) | 221 if (!CheckInsufficientBuffer(!!success, kDeviceInfoError, error)) |
| 222 return false; | 222 return false; |
| 223 | 223 |
| 224 scoped_ptr<uint8_t[]> property_value(new uint8_t[required_length]); | 224 scoped_ptr<uint8_t[]> property_value(new uint8_t[required_length]); |
| 225 ULONG actual_length = required_length; | 225 ULONG actual_length = required_length; |
| 226 DWORD property_type; | 226 DWORD property_type; |
| 227 success = SetupDiGetDeviceRegistryProperty(device_info_handle, | 227 success = SetupDiGetDeviceRegistryProperty(device_info_handle.Get(), |
| 228 device_info_data, | 228 device_info_data, |
| 229 property_id, | 229 property_id, |
| 230 &property_type, | 230 &property_type, |
| 231 property_value.get(), | 231 property_value.get(), |
| 232 actual_length, | 232 actual_length, |
| 233 &required_length); | 233 &required_length); |
| 234 if (!CheckSuccess(!!success, kDeviceInfoError, error)) | 234 if (!CheckSuccess(!!success, kDeviceInfoError, error)) |
| 235 return false; | 235 return false; |
| 236 if (!CheckExpectedLength( | 236 if (!CheckExpectedLength( |
| 237 actual_length, required_length, kDeviceInfoError, error)) { | 237 actual_length, required_length, kDeviceInfoError, error)) { |
| 238 return false; | 238 return false; |
| 239 } | 239 } |
| 240 | 240 |
| 241 (*value) = DeviceRegistryPropertyValue::Create( | 241 (*value) = DeviceRegistryPropertyValue::Create( |
| 242 property_type, property_value.Pass(), actual_length).Pass(); | 242 property_type, property_value.Pass(), actual_length).Pass(); |
| 243 return true; | 243 return true; |
| 244 } | 244 } |
| 245 | 245 |
| 246 bool CollectBluetoothLowEnergyDeviceInstanceId( | 246 bool CollectBluetoothLowEnergyDeviceInstanceId( |
| 247 const ScopedDeviceInfoSetHandle& device_info_handle, | 247 const ScopedDeviceInfoSetHandle& device_info_handle, |
| 248 PSP_DEVINFO_DATA device_info_data, | 248 PSP_DEVINFO_DATA device_info_data, |
| 249 scoped_ptr<device::win::BluetoothLowEnergyDeviceInfo>& device_info, | 249 scoped_ptr<device::win::BluetoothLowEnergyDeviceInfo>& device_info, |
| 250 std::string* error) { | 250 std::string* error) { |
| 251 ULONG required_length = 0; | 251 ULONG required_length = 0; |
| 252 BOOL success = SetupDiGetDeviceInstanceId( | 252 BOOL success = SetupDiGetDeviceInstanceId( |
| 253 device_info_handle, device_info_data, NULL, 0, &required_length); | 253 device_info_handle.Get(), device_info_data, NULL, 0, &required_length); |
| 254 if (!CheckInsufficientBuffer(!!success, kDeviceInfoError, error)) | 254 if (!CheckInsufficientBuffer(!!success, kDeviceInfoError, error)) |
| 255 return false; | 255 return false; |
| 256 | 256 |
| 257 scoped_ptr<WCHAR[]> instance_id(new WCHAR[required_length]); | 257 scoped_ptr<WCHAR[]> instance_id(new WCHAR[required_length]); |
| 258 ULONG actual_length = required_length; | 258 ULONG actual_length = required_length; |
| 259 success = SetupDiGetDeviceInstanceId(device_info_handle, | 259 success = SetupDiGetDeviceInstanceId(device_info_handle.Get(), |
| 260 device_info_data, | 260 device_info_data, |
| 261 instance_id.get(), | 261 instance_id.get(), |
| 262 actual_length, | 262 actual_length, |
| 263 &required_length); | 263 &required_length); |
| 264 if (!CheckSuccess(!!success, kDeviceInfoError, error)) | 264 if (!CheckSuccess(!!success, kDeviceInfoError, error)) |
| 265 return false; | 265 return false; |
| 266 if (!CheckExpectedLength( | 266 if (!CheckExpectedLength( |
| 267 actual_length, required_length, kDeviceInfoError, error)) { | 267 actual_length, required_length, kDeviceInfoError, error)) { |
| 268 return false; | 268 return false; |
| 269 } | 269 } |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 return true; | 410 return true; |
| 411 } | 411 } |
| 412 | 412 |
| 413 bool CollectBluetoothLowEnergyDeviceInfo( | 413 bool CollectBluetoothLowEnergyDeviceInfo( |
| 414 const ScopedDeviceInfoSetHandle& device_info_handle, | 414 const ScopedDeviceInfoSetHandle& device_info_handle, |
| 415 PSP_DEVICE_INTERFACE_DATA device_interface_data, | 415 PSP_DEVICE_INTERFACE_DATA device_interface_data, |
| 416 scoped_ptr<device::win::BluetoothLowEnergyDeviceInfo>* device_info, | 416 scoped_ptr<device::win::BluetoothLowEnergyDeviceInfo>* device_info, |
| 417 std::string* error) { | 417 std::string* error) { |
| 418 // Retrieve required # of bytes for interface details | 418 // Retrieve required # of bytes for interface details |
| 419 ULONG required_length = 0; | 419 ULONG required_length = 0; |
| 420 BOOL success = SetupDiGetDeviceInterfaceDetail(device_info_handle, | 420 BOOL success = SetupDiGetDeviceInterfaceDetail(device_info_handle.Get(), |
| 421 device_interface_data, | 421 device_interface_data, |
| 422 NULL, | 422 NULL, |
| 423 0, | 423 0, |
| 424 &required_length, | 424 &required_length, |
| 425 NULL); | 425 NULL); |
| 426 if (!CheckInsufficientBuffer(!!success, kDeviceInfoError, error)) | 426 if (!CheckInsufficientBuffer(!!success, kDeviceInfoError, error)) |
| 427 return false; | 427 return false; |
| 428 | 428 |
| 429 scoped_ptr<uint8_t[]> interface_data(new uint8_t[required_length]); | 429 scoped_ptr<uint8_t[]> interface_data(new uint8_t[required_length]); |
| 430 ZeroMemory(interface_data.get(), required_length); | 430 ZeroMemory(interface_data.get(), required_length); |
| 431 | 431 |
| 432 PSP_DEVICE_INTERFACE_DETAIL_DATA device_interface_detail_data = | 432 PSP_DEVICE_INTERFACE_DETAIL_DATA device_interface_detail_data = |
| 433 reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(interface_data.get()); | 433 reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(interface_data.get()); |
| 434 device_interface_detail_data->cbSize = | 434 device_interface_detail_data->cbSize = |
| 435 sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); | 435 sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); |
| 436 | 436 |
| 437 SP_DEVINFO_DATA device_info_data = {0}; | 437 SP_DEVINFO_DATA device_info_data = {0}; |
| 438 device_info_data.cbSize = sizeof(SP_DEVINFO_DATA); | 438 device_info_data.cbSize = sizeof(SP_DEVINFO_DATA); |
| 439 | 439 |
| 440 ULONG actual_length = required_length; | 440 ULONG actual_length = required_length; |
| 441 success = SetupDiGetDeviceInterfaceDetail(device_info_handle, | 441 success = SetupDiGetDeviceInterfaceDetail(device_info_handle.Get(), |
| 442 device_interface_data, | 442 device_interface_data, |
| 443 device_interface_detail_data, | 443 device_interface_detail_data, |
| 444 actual_length, | 444 actual_length, |
| 445 &required_length, | 445 &required_length, |
| 446 &device_info_data); | 446 &device_info_data); |
| 447 if (!CheckSuccess(!!success, kDeviceInfoError, error)) | 447 if (!CheckSuccess(!!success, kDeviceInfoError, error)) |
| 448 return false; | 448 return false; |
| 449 if (!CheckExpectedLength( | 449 if (!CheckExpectedLength( |
| 450 actual_length, required_length, kDeviceInfoError, error)) { | 450 actual_length, required_length, kDeviceInfoError, error)) { |
| 451 return false; | 451 return false; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 479 | 479 |
| 480 DeviceInfoResult EnumerateSingleBluetoothLowEnergyDevice( | 480 DeviceInfoResult EnumerateSingleBluetoothLowEnergyDevice( |
| 481 const ScopedDeviceInfoSetHandle& device_info_handle, | 481 const ScopedDeviceInfoSetHandle& device_info_handle, |
| 482 DWORD device_index, | 482 DWORD device_index, |
| 483 scoped_ptr<device::win::BluetoothLowEnergyDeviceInfo>* device_info, | 483 scoped_ptr<device::win::BluetoothLowEnergyDeviceInfo>* device_info, |
| 484 std::string* error) { | 484 std::string* error) { |
| 485 // Enumerate device of BLUETOOTHLE_DEVICE interface class | 485 // Enumerate device of BLUETOOTHLE_DEVICE interface class |
| 486 GUID BluetoothInterfaceGUID = GUID_BLUETOOTHLE_DEVICE_INTERFACE; | 486 GUID BluetoothInterfaceGUID = GUID_BLUETOOTHLE_DEVICE_INTERFACE; |
| 487 SP_DEVICE_INTERFACE_DATA device_interface_data = {0}; | 487 SP_DEVICE_INTERFACE_DATA device_interface_data = {0}; |
| 488 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); | 488 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); |
| 489 BOOL success = ::SetupDiEnumDeviceInterfaces(device_info_handle, | 489 BOOL success = ::SetupDiEnumDeviceInterfaces(device_info_handle.Get(), |
| 490 NULL, | 490 NULL, |
| 491 &BluetoothInterfaceGUID, | 491 &BluetoothInterfaceGUID, |
| 492 device_index, | 492 device_index, |
| 493 &device_interface_data); | 493 &device_interface_data); |
| 494 if (!success) { | 494 if (!success) { |
| 495 HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); | 495 HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); |
| 496 if (hr == HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS)) { | 496 if (hr == HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS)) { |
| 497 return kNoMoreDevices; | 497 return kNoMoreDevices; |
| 498 } | 498 } |
| 499 *error = FormatBluetoothError(kDeviceInfoError, hr); | 499 *error = FormatBluetoothError(kDeviceInfoError, hr); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 | 665 |
| 666 bool ExtractBluetoothAddressFromDeviceInstanceIdForTesting( | 666 bool ExtractBluetoothAddressFromDeviceInstanceIdForTesting( |
| 667 const std::string& instance_id, | 667 const std::string& instance_id, |
| 668 BLUETOOTH_ADDRESS* btha, | 668 BLUETOOTH_ADDRESS* btha, |
| 669 std::string* error) { | 669 std::string* error) { |
| 670 return ExtractBluetoothAddressFromDeviceInstanceId(instance_id, btha, error); | 670 return ExtractBluetoothAddressFromDeviceInstanceId(instance_id, btha, error); |
| 671 } | 671 } |
| 672 | 672 |
| 673 } // namespace win | 673 } // namespace win |
| 674 } // namespace device | 674 } // namespace device |
| OLD | NEW |