Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: device/hid/hid_service_win.cc

Issue 161823002: Clean up HID backend and API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 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 #include <string>
9 8
10 #include "base/callback_helpers.h"
11 #include "base/lazy_instance.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/stl_util.h" 9 #include "base/stl_util.h"
14 #include "base/strings/sys_string_conversions.h" 10 #include "base/strings/sys_string_conversions.h"
15 #include "device/hid/hid_connection.h"
16 #include "device/hid/hid_connection_win.h" 11 #include "device/hid/hid_connection_win.h"
17 #include "device/hid/hid_service.h" 12 #include "device/hid/hid_device_info.h"
18 #include "net/base/io_buffer.h" 13 #include "net/base/io_buffer.h"
19 14
20 #if defined(OS_WIN) 15 #if defined(OS_WIN)
21 16
22 #define INITGUID 17 #define INITGUID
23 18
24 #include <windows.h> 19 #include <windows.h>
25 #include <hidclass.h> 20 #include <hidclass.h>
26 21
27 extern "C" { 22 extern "C" {
(...skipping 13 matching lines...) Expand all
41 #pragma comment(lib, "setupapi.lib") 36 #pragma comment(lib, "setupapi.lib")
42 #pragma comment(lib, "hid.lib") 37 #pragma comment(lib, "hid.lib")
43 38
44 namespace device { 39 namespace device {
45 namespace { 40 namespace {
46 41
47 const char kHIDClass[] = "HIDClass"; 42 const char kHIDClass[] = "HIDClass";
48 43
49 } // namespace 44 } // namespace
50 45
51 HidServiceWin::HidServiceWin() { 46 HidServiceWin::HidServiceWin() { Enumerate(); }
52 initialized_ = Enumerate(); 47
53 }
54 HidServiceWin::~HidServiceWin() {} 48 HidServiceWin::~HidServiceWin() {}
55 49
56 bool HidServiceWin::Enumerate() { 50 void HidServiceWin::Enumerate() {
57 BOOL res; 51 BOOL res;
58 HDEVINFO device_info_set; 52 HDEVINFO device_info_set;
59 SP_DEVINFO_DATA devinfo_data; 53 SP_DEVINFO_DATA devinfo_data;
60 SP_DEVICE_INTERFACE_DATA device_interface_data; 54 SP_DEVICE_INTERFACE_DATA device_interface_data;
61 55
62 memset(&devinfo_data, 0, sizeof(SP_DEVINFO_DATA)); 56 memset(&devinfo_data, 0, sizeof(SP_DEVINFO_DATA));
63 devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA); 57 devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
64 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); 58 device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
65 59
66 device_info_set = SetupDiGetClassDevs( 60 device_info_set = SetupDiGetClassDevs(
67 &GUID_DEVINTERFACE_HID, 61 &GUID_DEVINTERFACE_HID,
68 NULL, 62 NULL,
69 NULL, 63 NULL,
70 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 64 DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
71 65
72 if (device_info_set == INVALID_HANDLE_VALUE) 66 if (device_info_set == INVALID_HANDLE_VALUE)
73 return false; 67 return;
74 68
75 for (int device_index = 0; 69 for (int device_index = 0;
76 SetupDiEnumDeviceInterfaces(device_info_set, 70 SetupDiEnumDeviceInterfaces(device_info_set,
77 NULL, 71 NULL,
78 &GUID_DEVINTERFACE_HID, 72 &GUID_DEVINTERFACE_HID,
79 device_index, 73 device_index,
80 &device_interface_data); 74 &device_interface_data);
81 device_index++) { 75 ++device_index) {
82 DWORD required_size = 0; 76 DWORD required_size = 0;
83 77
84 // Determime the required size of detail struct. 78 // Determime the required size of detail struct.
85 SetupDiGetDeviceInterfaceDetailA(device_info_set, 79 SetupDiGetDeviceInterfaceDetailA(device_info_set,
86 &device_interface_data, 80 &device_interface_data,
87 NULL, 81 NULL,
88 0, 82 0,
89 &required_size, 83 &required_size,
90 NULL); 84 NULL);
91 85
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 char driver_name[256] = {0}; 118 char driver_name[256] = {0};
125 // Get bounded driver. 119 // Get bounded driver.
126 res = SetupDiGetDeviceRegistryPropertyA(device_info_set, 120 res = SetupDiGetDeviceRegistryPropertyA(device_info_set,
127 &devinfo_data, 121 &devinfo_data,
128 SPDRP_DRIVER, 122 SPDRP_DRIVER,
129 NULL, 123 NULL,
130 (PBYTE) driver_name, 124 (PBYTE) driver_name,
131 sizeof(driver_name) - 1, 125 sizeof(driver_name) - 1,
132 NULL); 126 NULL);
133 if (res) { 127 if (res) {
134 // Found the drive. 128 // Found the driver.
135 break; 129 break;
136 } 130 }
137 } 131 }
138 } 132 }
139 133
140 if (!res) 134 if (!res)
141 continue; 135 continue;
142 136
143 PlatformAddDevice(device_interface_detail_data->DevicePath); 137 PlatformAddDevice(device_interface_detail_data->DevicePath);
144 } 138 }
145
146 return true;
147 } 139 }
148 140
149 void HidServiceWin::PlatformAddDevice(std::string device_path) { 141 void HidServiceWin::PlatformAddDevice(const std::string& device_path) {
150 HidDeviceInfo device_info; 142 HidDeviceInfo device_info;
151 device_info.device_id = device_path; 143 device_info.device_id = device_path;
152 144
153 // Try to open the device. 145 // Try to open the device.
154 base::win::ScopedHandle device_handle( 146 base::win::ScopedHandle device_handle(
155 CreateFileA(device_path.c_str(), 147 CreateFileA(device_path.c_str(),
156 0, 148 0,
157 FILE_SHARE_READ | FILE_SHARE_WRITE, 149 FILE_SHARE_READ | FILE_SHARE_WRITE,
158 NULL, 150 NULL,
159 OPEN_EXISTING, 151 OPEN_EXISTING,
(...skipping 28 matching lines...) Expand all
188 device_info.feature_report_size = capabilities.FeatureReportByteLength; 180 device_info.feature_report_size = capabilities.FeatureReportByteLength;
189 } 181 }
190 // Detect if the device supports report ids. 182 // Detect if the device supports report ids.
191 if (capabilities.NumberInputValueCaps > 0) { 183 if (capabilities.NumberInputValueCaps > 0) {
192 scoped_ptr<HIDP_VALUE_CAPS[]> value_caps( 184 scoped_ptr<HIDP_VALUE_CAPS[]> value_caps(
193 new HIDP_VALUE_CAPS[capabilities.NumberInputValueCaps]); 185 new HIDP_VALUE_CAPS[capabilities.NumberInputValueCaps]);
194 USHORT value_caps_length = capabilities.NumberInputValueCaps; 186 USHORT value_caps_length = capabilities.NumberInputValueCaps;
195 if (HidP_GetValueCaps(HidP_Input, &value_caps[0], &value_caps_length, 187 if (HidP_GetValueCaps(HidP_Input, &value_caps[0], &value_caps_length,
196 preparsed_data) == HIDP_STATUS_SUCCESS) { 188 preparsed_data) == HIDP_STATUS_SUCCESS) {
197 device_info.has_report_id = (value_caps[0].ReportID != 0); 189 device_info.has_report_id = (value_caps[0].ReportID != 0);
190 // If report IDs are supported, adjust all the expected report sizes
191 // down by one byte. This is because Windows will always provide sizes
192 // which assume the presence of a report ID.
193 if (device_info.has_report_id) {
194 if (device_info.input_report_size > 0)
195 device_info.input_report_size -= 1;
196 if (device_info.output_report_size > 0)
197 device_info.output_report_size -= 1;
198 if (device_info.feature_report_size > 0)
199 device_info.feature_report_size -= 1;
200 }
198 } 201 }
199 } 202 }
200 HidD_FreePreparsedData(preparsed_data); 203 HidD_FreePreparsedData(preparsed_data);
201 } 204 }
202 205
203 // Get the serial number 206 // Get the serial number
204 wchar_t str_property[512] = { 0 }; 207 wchar_t str_property[512] = { 0 };
205 if (HidD_GetSerialNumberString(device_handle.Get(), 208 if (HidD_GetSerialNumberString(device_handle.Get(),
206 str_property, 209 str_property,
207 sizeof(str_property))) { 210 sizeof(str_property))) {
208 device_info.serial_number = base::SysWideToUTF8(str_property); 211 device_info.serial_number = base::SysWideToUTF8(str_property);
209 } 212 }
210 213
211 if (HidD_GetProductString(device_handle.Get(), 214 if (HidD_GetProductString(device_handle.Get(),
212 str_property, 215 str_property,
213 sizeof(str_property))) { 216 sizeof(str_property))) {
214 device_info.product_name = base::SysWideToUTF8(str_property); 217 device_info.product_name = base::SysWideToUTF8(str_property);
215 } 218 }
216 219
217 HidService::AddDevice(device_info); 220 AddDevice(device_info);
218 } 221 }
219 222
220 void HidServiceWin::PlatformRemoveDevice(std::string device_path) { 223 void HidServiceWin::PlatformRemoveDevice(const std::string& device_path) {
221 HidService::RemoveDevice(device_path); 224 RemoveDevice(device_path);
222 } 225 }
223 226
224 void HidServiceWin::GetDevices(std::vector<HidDeviceInfo>* devices) { 227 void HidServiceWin::GetDevices(std::vector<HidDeviceInfo>* devices) {
225 Enumerate(); 228 Enumerate();
226 HidService::GetDevices(devices); 229 HidService::GetDevices(devices);
227 } 230 }
228 231
229 scoped_refptr<HidConnection> HidServiceWin::Connect(std::string device_id) { 232 scoped_refptr<HidConnection> HidServiceWin::Connect(
230 if (!ContainsKey(devices_, device_id)) return NULL; 233 const HidDeviceId& device_id) {
231 scoped_refptr<HidConnectionWin> connection( 234 HidDeviceInfo device_info;
232 new HidConnectionWin(devices_[device_id])); 235 if (!GetDeviceInfo(device_id, &device_info))
236 return NULL;
237 scoped_refptr<HidConnectionWin> connection(new HidConnectionWin(device_info));
233 if (!connection->available()) { 238 if (!connection->available()) {
234 LOG_GETLASTERROR(ERROR) << "Failed to open device."; 239 LOG_GETLASTERROR(ERROR) << "Failed to open device.";
235 return NULL; 240 return NULL;
236 } 241 }
237 return connection; 242 return connection;
238 } 243 }
239 244
240 } // namespace device 245 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698