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 <libudev.h> | 5 #include <linux/hidraw.h> |
| 6 #include <sys/ioctl.h> |
| 7 |
6 #include <stdint.h> | 8 #include <stdint.h> |
7 | 9 |
8 #include <string> | 10 #include <string> |
9 #include <vector> | |
10 | 11 |
| 12 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 13 #include "base/logging.h" |
12 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
13 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
14 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_piece.h" | 17 #include "base/strings/string_piece.h" |
16 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
17 #include "device/hid/hid_connection_linux.h" | 19 #include "device/hid/hid_connection_linux.h" |
18 #include "device/hid/hid_device_info.h" | 20 #include "device/hid/hid_device_info.h" |
| 21 #include "device/hid/hid_report_descriptor.h" |
19 #include "device/hid/hid_service_linux.h" | 22 #include "device/hid/hid_service_linux.h" |
20 | 23 |
21 namespace device { | 24 namespace device { |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
25 const char kUdevName[] = "udev"; | 28 const char kUdevName[] = "udev"; |
26 const char kUdevActionAdd[] = "add"; | 29 const char kUdevActionAdd[] = "add"; |
27 const char kUdevActionRemove[] = "remove"; | 30 const char kUdevActionRemove[] = "remove"; |
28 const char kHIDSubSystem[] = "hid"; | 31 const char kHIDSubSystem[] = "hid"; |
29 | 32 const char kHidrawSubsystem[] = "hidraw"; |
30 const char kHIDID[] = "HID_ID"; | 33 const char kHIDID[] = "HID_ID"; |
31 const char kHIDName[] = "HID_NAME"; | 34 const char kHIDName[] = "HID_NAME"; |
32 const char kHIDUnique[] = "HID_UNIQ"; | 35 const char kHIDUnique[] = "HID_UNIQ"; |
33 | 36 |
34 } // namespace | 37 } // namespace |
35 | 38 |
36 HidServiceLinux::HidServiceLinux() { | 39 HidServiceLinux::HidServiceLinux() { |
37 udev_.reset(udev_new()); | 40 udev_.reset(udev_new()); |
38 if (!udev_) { | 41 if (!udev_) { |
39 LOG(ERROR) << "Failed to create udev."; | 42 LOG(ERROR) << "Failed to create udev."; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 } | 80 } |
78 | 81 |
79 scoped_refptr<HidConnection> HidServiceLinux::Connect( | 82 scoped_refptr<HidConnection> HidServiceLinux::Connect( |
80 const HidDeviceId& device_id) { | 83 const HidDeviceId& device_id) { |
81 HidDeviceInfo device_info; | 84 HidDeviceInfo device_info; |
82 if (!GetDeviceInfo(device_id, &device_info)) | 85 if (!GetDeviceInfo(device_id, &device_info)) |
83 return NULL; | 86 return NULL; |
84 | 87 |
85 ScopedUdevDevicePtr hid_device( | 88 ScopedUdevDevicePtr hid_device( |
86 udev_device_new_from_syspath(udev_.get(), device_info.device_id.c_str())); | 89 udev_device_new_from_syspath(udev_.get(), device_info.device_id.c_str())); |
| 90 |
87 if (hid_device) { | 91 if (hid_device) { |
88 return new HidConnectionLinux(device_info, hid_device.Pass()); | 92 std::string dev_node; |
| 93 if (!FindHidrawDevNode(hid_device.get(), &dev_node)) { |
| 94 LOG(ERROR) << "Cannot open HID device as hidraw device."; |
| 95 return NULL; |
| 96 } |
| 97 return new HidConnectionLinux(device_info, dev_node); |
89 } | 98 } |
90 return NULL; | 99 return NULL; |
91 } | 100 } |
92 | 101 |
93 void HidServiceLinux::OnFileCanReadWithoutBlocking(int fd) { | 102 void HidServiceLinux::OnFileCanReadWithoutBlocking(int fd) { |
94 DCHECK_EQ(monitor_fd_, fd); | 103 DCHECK_EQ(monitor_fd_, fd); |
95 | 104 |
96 ScopedUdevDevicePtr dev(udev_monitor_receive_device(monitor_.get())); | 105 ScopedUdevDevicePtr dev(udev_monitor_receive_device(monitor_.get())); |
97 if (!dev) | 106 if (!dev) |
98 return; | 107 return; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 } | 185 } |
177 | 186 |
178 str_property = udev_device_get_property_value(device, kHIDUnique); | 187 str_property = udev_device_get_property_value(device, kHIDUnique); |
179 if (str_property != NULL) | 188 if (str_property != NULL) |
180 device_info.serial_number = str_property; | 189 device_info.serial_number = str_property; |
181 | 190 |
182 str_property = udev_device_get_property_value(device, kHIDName); | 191 str_property = udev_device_get_property_value(device, kHIDName); |
183 if (str_property != NULL) | 192 if (str_property != NULL) |
184 device_info.product_name = str_property; | 193 device_info.product_name = str_property; |
185 | 194 |
| 195 std::string dev_node; |
| 196 if (!FindHidrawDevNode(device, &dev_node)) { |
| 197 LOG(ERROR) << "Cannot open HID device as hidraw device."; |
| 198 return; |
| 199 } |
| 200 |
| 201 int flags = base::File::FLAG_OPEN | |
| 202 base::File::FLAG_READ; |
| 203 |
| 204 base::File device_file(base::FilePath(dev_node), flags); |
| 205 if (!device_file.IsValid()) { |
| 206 LOG(ERROR) << device_file.error_details(); |
| 207 return; |
| 208 } |
| 209 |
| 210 int desc_size = 0; |
| 211 int res = ioctl(device_file.GetPlatformFile(), HIDIOCGRDESCSIZE, &desc_size); |
| 212 if (res < 0) |
| 213 { |
| 214 LOG(ERROR) << "HIDIOCGRDESCSIZE failed."; |
| 215 device_file.Close(); |
| 216 return; |
| 217 } |
| 218 |
| 219 hidraw_report_descriptor rpt_desc; |
| 220 rpt_desc.size = desc_size; |
| 221 |
| 222 res = ioctl(device_file.GetPlatformFile(), HIDIOCGRDESC, &rpt_desc); |
| 223 if (res < 0) |
| 224 { |
| 225 LOG(ERROR) << "HIDIOCGRDESC failed."; |
| 226 device_file.Close(); |
| 227 return; |
| 228 } |
| 229 |
| 230 device_file.Close(); |
| 231 |
| 232 HidReportDescriptor report_descriptor(rpt_desc.value, rpt_desc.size); |
| 233 |
| 234 device_info.usages = report_descriptor.topLevelCollections(); |
| 235 |
186 AddDevice(device_info); | 236 AddDevice(device_info); |
187 } | 237 } |
188 | 238 |
189 void HidServiceLinux::PlatformRemoveDevice(udev_device* raw_dev) { | 239 void HidServiceLinux::PlatformRemoveDevice(udev_device* raw_dev) { |
190 const char* device_path = NULL; | 240 const char* device_path = NULL; |
191 device_path = udev_device_get_syspath(raw_dev); | 241 device_path = udev_device_get_syspath(raw_dev); |
192 if (device_path == NULL) | 242 if (device_path == NULL) |
193 return; | 243 return; |
194 RemoveDevice(device_path); | 244 RemoveDevice(device_path); |
195 } | 245 } |
196 | 246 |
| 247 bool HidServiceLinux::FindHidrawDevNode(udev_device* parent, |
| 248 std::string* result) { |
| 249 udev* udev = udev_device_get_udev(parent); |
| 250 if (!udev) { |
| 251 return false; |
| 252 } |
| 253 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev)); |
| 254 if (!enumerate) { |
| 255 return false; |
| 256 } |
| 257 if (udev_enumerate_add_match_subsystem(enumerate.get(), kHidrawSubsystem)) { |
| 258 return false; |
| 259 } |
| 260 if (udev_enumerate_scan_devices(enumerate.get())) { |
| 261 return false; |
| 262 } |
| 263 std::string parent_path(udev_device_get_devpath(parent)); |
| 264 if (parent_path.length() == 0 || *parent_path.rbegin() != '/') |
| 265 parent_path += '/'; |
| 266 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get()); |
| 267 for (udev_list_entry* i = devices; i != NULL; |
| 268 i = udev_list_entry_get_next(i)) { |
| 269 ScopedUdevDevicePtr hid_dev( |
| 270 udev_device_new_from_syspath(udev, udev_list_entry_get_name(i))); |
| 271 const char* raw_path = udev_device_get_devnode(hid_dev.get()); |
| 272 std::string device_path = udev_device_get_devpath(hid_dev.get()); |
| 273 if (raw_path && |
| 274 !device_path.compare(0, parent_path.length(), parent_path)) { |
| 275 std::string sub_path = device_path.substr(parent_path.length()); |
| 276 if (sub_path.substr(0, sizeof(kHidrawSubsystem)-1) == kHidrawSubsystem) { |
| 277 *result = raw_path; |
| 278 return true; |
| 279 } |
| 280 } |
| 281 } |
| 282 |
| 283 return false; |
| 284 } |
| 285 |
197 } // namespace dev | 286 } // namespace dev |
OLD | NEW |