| 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/hid/hid_service_win.h" | 5 #include "device/hid/hid_service_win.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 memset(&devinfo_data, 0, sizeof(SP_DEVINFO_DATA)); | 59 memset(&devinfo_data, 0, sizeof(SP_DEVINFO_DATA)); |
| 60 devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA); | 60 devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA); |
| 61 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); | 61 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); |
| 62 | 62 |
| 63 device_info_set = SetupDiGetClassDevs( | 63 device_info_set = SetupDiGetClassDevs( |
| 64 &GUID_DEVINTERFACE_HID, | 64 &GUID_DEVINTERFACE_HID, |
| 65 NULL, | 65 NULL, |
| 66 NULL, | 66 NULL, |
| 67 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); | 67 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); |
| 68 | 68 |
| 69 if (device_info_set == INVALID_HANDLE_VALUE) | 69 std::set<std::string> connected_devices; |
| 70 return; | |
| 71 | 70 |
| 72 for (int device_index = 0; | 71 if (device_info_set != INVALID_HANDLE_VALUE) { |
| 73 SetupDiEnumDeviceInterfaces(device_info_set, | 72 for (int device_index = 0; |
| 74 NULL, | 73 SetupDiEnumDeviceInterfaces(device_info_set, |
| 75 &GUID_DEVINTERFACE_HID, | 74 NULL, |
| 76 device_index, | 75 &GUID_DEVINTERFACE_HID, |
| 77 &device_interface_data); | 76 device_index, |
| 78 ++device_index) { | 77 &device_interface_data); |
| 79 DWORD required_size = 0; | 78 ++device_index) { |
| 79 DWORD required_size = 0; |
| 80 | 80 |
| 81 // Determime the required size of detail struct. | 81 // Determime the required size of detail struct. |
| 82 SetupDiGetDeviceInterfaceDetailA(device_info_set, | 82 SetupDiGetDeviceInterfaceDetailA(device_info_set, |
| 83 &device_interface_data, | 83 &device_interface_data, |
| 84 NULL, | 84 NULL, |
| 85 0, | 85 0, |
| 86 &required_size, | 86 &required_size, |
| 87 NULL); | 87 NULL); |
| 88 | 88 |
| 89 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA_A, base::FreeDeleter> | 89 scoped_ptr<SP_DEVICE_INTERFACE_DETAIL_DATA_A, base::FreeDeleter> |
| 90 device_interface_detail_data( | 90 device_interface_detail_data( |
| 91 static_cast<SP_DEVICE_INTERFACE_DETAIL_DATA_A*>( | 91 static_cast<SP_DEVICE_INTERFACE_DETAIL_DATA_A*>( |
| 92 malloc(required_size))); | 92 malloc(required_size))); |
| 93 device_interface_detail_data->cbSize = | 93 device_interface_detail_data->cbSize = |
| 94 sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A); | 94 sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A); |
| 95 | 95 |
| 96 // Get the detailed data for this device. | 96 // Get the detailed data for this device. |
| 97 res = SetupDiGetDeviceInterfaceDetailA(device_info_set, | 97 res = SetupDiGetDeviceInterfaceDetailA(device_info_set, |
| 98 &device_interface_data, | 98 &device_interface_data, |
| 99 device_interface_detail_data.get(), | 99 device_interface_detail_data.get(), |
| 100 required_size, | 100 required_size, |
| 101 NULL, | 101 NULL, |
| 102 NULL); | 102 NULL); |
| 103 if (!res) | 103 if (!res) |
| 104 continue; | 104 continue; |
| 105 | 105 |
| 106 // Enumerate device info. Looking for Setup Class "HIDClass". | 106 // Enumerate device info. Looking for Setup Class "HIDClass". |
| 107 for (DWORD i = 0; | 107 for (DWORD i = 0; |
| 108 SetupDiEnumDeviceInfo(device_info_set, i, &devinfo_data); | 108 SetupDiEnumDeviceInfo(device_info_set, i, &devinfo_data); |
| 109 i++) { | 109 i++) { |
| 110 char class_name[256] = {0}; | 110 char class_name[256] = {0}; |
| 111 res = SetupDiGetDeviceRegistryPropertyA(device_info_set, | |
| 112 &devinfo_data, | |
| 113 SPDRP_CLASS, | |
| 114 NULL, | |
| 115 (PBYTE) class_name, | |
| 116 sizeof(class_name) - 1, | |
| 117 NULL); | |
| 118 if (!res) | |
| 119 break; | |
| 120 if (memcmp(class_name, kHIDClass, sizeof(kHIDClass)) == 0) { | |
| 121 char driver_name[256] = {0}; | |
| 122 // Get bounded driver. | |
| 123 res = SetupDiGetDeviceRegistryPropertyA(device_info_set, | 111 res = SetupDiGetDeviceRegistryPropertyA(device_info_set, |
| 124 &devinfo_data, | 112 &devinfo_data, |
| 125 SPDRP_DRIVER, | 113 SPDRP_CLASS, |
| 126 NULL, | 114 NULL, |
| 127 (PBYTE) driver_name, | 115 (PBYTE) class_name, |
| 128 sizeof(driver_name) - 1, | 116 sizeof(class_name) - 1, |
| 129 NULL); | 117 NULL); |
| 130 if (res) { | 118 if (!res) |
| 131 // Found the driver. | |
| 132 break; | 119 break; |
| 120 if (memcmp(class_name, kHIDClass, sizeof(kHIDClass)) == 0) { |
| 121 char driver_name[256] = {0}; |
| 122 // Get bounded driver. |
| 123 res = SetupDiGetDeviceRegistryPropertyA(device_info_set, |
| 124 &devinfo_data, |
| 125 SPDRP_DRIVER, |
| 126 NULL, |
| 127 (PBYTE) driver_name, |
| 128 sizeof(driver_name) - 1, |
| 129 NULL); |
| 130 if (res) { |
| 131 // Found the driver. |
| 132 break; |
| 133 } |
| 133 } | 134 } |
| 134 } | 135 } |
| 136 |
| 137 if (!res) |
| 138 continue; |
| 139 |
| 140 PlatformAddDevice(device_interface_detail_data->DevicePath); |
| 141 connected_devices.insert(device_interface_detail_data->DevicePath); |
| 135 } | 142 } |
| 143 } |
| 136 | 144 |
| 137 if (!res) | 145 // Find disconnected devices. |
| 138 continue; | 146 std::vector<std::string> disconnected_devices; |
| 147 for (DeviceMap::iterator it = devices_.begin(); it != devices_.end(); ++it) { |
| 148 if (!ContainsKey(connected_devices, it->first)) { |
| 149 disconnected_devices.push_back(it->first); |
| 150 } |
| 151 } |
| 139 | 152 |
| 140 PlatformAddDevice(device_interface_detail_data->DevicePath); | 153 // Remove disconnected devices. |
| 154 for (size_t i = 0; i < disconnected_devices.size(); ++i) { |
| 155 PlatformRemoveDevice(disconnected_devices[i]); |
| 141 } | 156 } |
| 142 } | 157 } |
| 143 | 158 |
| 144 void HidServiceWin::PlatformAddDevice(const std::string& device_path) { | 159 void HidServiceWin::PlatformAddDevice(const std::string& device_path) { |
| 145 HidDeviceInfo device_info; | 160 HidDeviceInfo device_info; |
| 146 device_info.device_id = device_path; | 161 device_info.device_id = device_path; |
| 147 | 162 |
| 148 // Try to open the device. | 163 // Try to open the device. |
| 149 base::win::ScopedHandle device_handle( | 164 base::win::ScopedHandle device_handle( |
| 150 CreateFileA(device_path.c_str(), | 165 CreateFileA(device_path.c_str(), |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 return NULL; | 268 return NULL; |
| 254 scoped_refptr<HidConnectionWin> connection(new HidConnectionWin(device_info)); | 269 scoped_refptr<HidConnectionWin> connection(new HidConnectionWin(device_info)); |
| 255 if (!connection->available()) { | 270 if (!connection->available()) { |
| 256 PLOG(ERROR) << "Failed to open device."; | 271 PLOG(ERROR) << "Failed to open device."; |
| 257 return NULL; | 272 return NULL; |
| 258 } | 273 } |
| 259 return connection; | 274 return connection; |
| 260 } | 275 } |
| 261 | 276 |
| 262 } // namespace device | 277 } // namespace device |
| OLD | NEW |