Chromium Code Reviews| 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_linux.h" | 5 #include "device/hid/hid_service_linux.h" |
| 6 | 6 |
| 7 #include <linux/hidraw.h> | 7 #include <linux/hidraw.h> |
| 8 #include <sys/ioctl.h> | 8 #include <sys/ioctl.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/files/file.h" | 14 #include "base/files/file.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
| 20 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
| 21 #include "device/hid/hid_connection_linux.h" | 21 #include "device/hid/hid_connection_linux.h" |
| 22 #include "device/hid/hid_device_info.h" | 22 #include "device/hid/hid_device_info.h" |
| 23 #include "device/hid/hid_report_descriptor.h" | 23 #include "device/hid/hid_report_descriptor.h" |
| 24 #include "device/udev_linux/udev.h" | 24 #include "device/udev_linux/udev.h" |
| 25 | 25 |
| 26 namespace device { | 26 namespace device { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 const char kHIDSubSystem[] = "hid"; | 30 const char kHidrawSubSystem[] = "hidraw"; |
|
mschilder1
2014/07/17 01:52:38
as const, should this be kHIDRAWSUBSYSTEM?
Reilly Grant (use Gerrit)
2014/07/17 03:22:53
Google C++ style says kHidrawSubSystem (or kHidraw
| |
| 31 const char kHidrawSubsystem[] = "hidraw"; | |
| 32 const char kHIDID[] = "HID_ID"; | 31 const char kHIDID[] = "HID_ID"; |
| 33 const char kHIDName[] = "HID_NAME"; | 32 const char kHIDName[] = "HID_NAME"; |
| 34 const char kHIDUnique[] = "HID_UNIQ"; | 33 const char kHIDUnique[] = "HID_UNIQ"; |
| 35 | 34 |
| 36 } // namespace | 35 } // namespace |
| 37 | 36 |
| 38 HidServiceLinux::HidServiceLinux() { | 37 HidServiceLinux::HidServiceLinux() { |
| 39 DeviceMonitorLinux* monitor = DeviceMonitorLinux::GetInstance(); | 38 DeviceMonitorLinux* monitor = DeviceMonitorLinux::GetInstance(); |
| 40 monitor->AddObserver(this); | 39 monitor->AddObserver(this); |
| 41 monitor->Enumerate( | 40 monitor->Enumerate( |
| 42 base::Bind(&HidServiceLinux::OnDeviceAdded, base::Unretained(this))); | 41 base::Bind(&HidServiceLinux::OnDeviceAdded, base::Unretained(this))); |
| 43 } | 42 } |
| 44 | 43 |
| 45 scoped_refptr<HidConnection> HidServiceLinux::Connect( | 44 scoped_refptr<HidConnection> HidServiceLinux::Connect( |
| 46 const HidDeviceId& device_id) { | 45 const HidDeviceId& device_id) { |
| 47 HidDeviceInfo device_info; | 46 HidDeviceInfo device_info; |
| 48 if (!GetDeviceInfo(device_id, &device_info)) | 47 if (!GetDeviceInfo(device_id, &device_info)) |
| 49 return NULL; | 48 return NULL; |
| 50 | 49 |
| 51 ScopedUdevDevicePtr device = | 50 ScopedUdevDevicePtr device = |
| 52 DeviceMonitorLinux::GetInstance()->GetDeviceFromPath( | 51 DeviceMonitorLinux::GetInstance()->GetDeviceFromPath( |
| 53 device_info.device_id); | 52 device_info.device_id); |
| 54 | 53 |
| 55 if (device) { | 54 if (device) { |
| 56 std::string dev_node; | 55 std::string dev_node = udev_device_get_devnode(device.get()); |
| 57 if (!FindHidrawDevNode(device.get(), &dev_node)) { | |
| 58 LOG(ERROR) << "Cannot open HID device as hidraw device."; | |
| 59 return NULL; | |
| 60 } | |
| 61 return new HidConnectionLinux(device_info, dev_node); | 56 return new HidConnectionLinux(device_info, dev_node); |
| 62 } | 57 } |
| 63 | 58 |
| 64 return NULL; | 59 return NULL; |
| 65 } | 60 } |
| 66 | 61 |
| 67 HidServiceLinux::~HidServiceLinux() { | 62 HidServiceLinux::~HidServiceLinux() { |
| 68 if (DeviceMonitorLinux::HasInstance()) | 63 if (DeviceMonitorLinux::HasInstance()) |
| 69 DeviceMonitorLinux::GetInstance()->RemoveObserver(this); | 64 DeviceMonitorLinux::GetInstance()->RemoveObserver(this); |
| 70 } | 65 } |
| 71 | 66 |
| 72 void HidServiceLinux::OnDeviceAdded(udev_device* device) { | 67 void HidServiceLinux::OnDeviceAdded(udev_device* device) { |
| 73 if (!device) | 68 if (!device) |
| 74 return; | 69 return; |
| 75 | 70 |
| 76 const char* device_path = udev_device_get_syspath(device); | 71 const char* device_path = udev_device_get_syspath(device); |
| 77 if (!device_path) | 72 if (!device_path) |
| 78 return; | 73 return; |
| 79 const char* subsystem = udev_device_get_subsystem(device); | 74 const char* subsystem = udev_device_get_subsystem(device); |
| 80 if (!subsystem || strcmp(subsystem, kHIDSubSystem) != 0) | 75 if (!subsystem || strcmp(subsystem, kHidrawSubSystem) != 0) |
| 81 return; | 76 return; |
| 82 | 77 |
| 83 HidDeviceInfo device_info; | 78 HidDeviceInfo device_info; |
| 84 device_info.device_id = device_path; | 79 device_info.device_id = device_path; |
| 85 | 80 |
| 86 uint32_t int_property = 0; | 81 uint32_t int_property = 0; |
| 87 const char* str_property = NULL; | 82 const char* str_property = NULL; |
| 88 | 83 |
| 89 const char* hid_id = udev_device_get_property_value(device, kHIDID); | 84 udev_device *parent = udev_device_get_parent(device); |
| 85 if (!parent) { | |
|
mschilder1
2014/07/17 01:52:38
inconsistent if(){ expr; } vs. if () expr; in this
Reilly Grant (use Gerrit)
2014/07/17 03:22:53
The rules say it is my choice. I dislike inconsist
| |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 const char* hid_id = udev_device_get_property_value(parent, kHIDID); | |
| 90 if (!hid_id) | 90 if (!hid_id) |
| 91 return; | 91 return; |
| 92 | 92 |
| 93 std::vector<std::string> parts; | 93 std::vector<std::string> parts; |
| 94 base::SplitString(hid_id, ':', &parts); | 94 base::SplitString(hid_id, ':', &parts); |
| 95 if (parts.size() != 3) { | 95 if (parts.size() != 3) { |
| 96 return; | 96 return; |
| 97 } | 97 } |
| 98 | 98 |
| 99 if (HexStringToUInt(base::StringPiece(parts[1]), &int_property)) { | 99 if (HexStringToUInt(base::StringPiece(parts[1]), &int_property)) { |
| 100 device_info.vendor_id = int_property; | 100 device_info.vendor_id = int_property; |
| 101 } | 101 } |
| 102 | 102 |
| 103 if (HexStringToUInt(base::StringPiece(parts[2]), &int_property)) { | 103 if (HexStringToUInt(base::StringPiece(parts[2]), &int_property)) { |
| 104 device_info.product_id = int_property; | 104 device_info.product_id = int_property; |
| 105 } | 105 } |
| 106 | 106 |
| 107 str_property = udev_device_get_property_value(device, kHIDUnique); | 107 str_property = udev_device_get_property_value(parent, kHIDUnique); |
| 108 if (str_property != NULL) | 108 if (str_property != NULL) |
| 109 device_info.serial_number = str_property; | 109 device_info.serial_number = str_property; |
| 110 | 110 |
| 111 str_property = udev_device_get_property_value(device, kHIDName); | 111 str_property = udev_device_get_property_value(parent, kHIDName); |
| 112 if (str_property != NULL) | 112 if (str_property != NULL) |
| 113 device_info.product_name = str_property; | 113 device_info.product_name = str_property; |
| 114 | 114 |
| 115 std::string dev_node; | 115 const std::string dev_node = udev_device_get_devnode(device); |
| 116 if (!FindHidrawDevNode(device, &dev_node)) { | 116 const int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 117 LOG(ERROR) << "Cannot find device node for HID device."; | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; | |
| 122 | 117 |
| 123 base::File device_file(base::FilePath(dev_node), flags); | 118 base::File device_file(base::FilePath(dev_node), flags); |
| 124 if (!device_file.IsValid()) { | 119 if (!device_file.IsValid()) { |
| 125 LOG(ERROR) << "Cannot open '" << dev_node << "': " | 120 LOG(ERROR) << "Cannot open '" << dev_node << "': " |
| 126 << base::File::ErrorToString(device_file.error_details()); | 121 << base::File::ErrorToString(device_file.error_details()); |
| 127 return; | 122 return; |
| 128 } | 123 } |
| 129 | 124 |
| 130 int desc_size = 0; | 125 int desc_size = 0; |
| 131 int res = ioctl(device_file.GetPlatformFile(), HIDIOCGRDESCSIZE, &desc_size); | 126 int res = ioctl(device_file.GetPlatformFile(), HIDIOCGRDESCSIZE, &desc_size); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 155 | 150 |
| 156 AddDevice(device_info); | 151 AddDevice(device_info); |
| 157 } | 152 } |
| 158 | 153 |
| 159 void HidServiceLinux::OnDeviceRemoved(udev_device* device) { | 154 void HidServiceLinux::OnDeviceRemoved(udev_device* device) { |
| 160 const char* device_path = udev_device_get_syspath(device);; | 155 const char* device_path = udev_device_get_syspath(device);; |
| 161 if (device_path) | 156 if (device_path) |
| 162 RemoveDevice(device_path); | 157 RemoveDevice(device_path); |
| 163 } | 158 } |
| 164 | 159 |
| 165 bool HidServiceLinux::FindHidrawDevNode(udev_device* parent, | |
| 166 std::string* result) { | |
| 167 udev* udev = udev_device_get_udev(parent); | |
| 168 if (!udev) { | |
| 169 return false; | |
| 170 } | |
| 171 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev)); | |
| 172 if (!enumerate) { | |
| 173 return false; | |
| 174 } | |
| 175 if (udev_enumerate_add_match_subsystem(enumerate.get(), kHidrawSubsystem)) { | |
| 176 return false; | |
| 177 } | |
| 178 if (udev_enumerate_scan_devices(enumerate.get())) { | |
| 179 return false; | |
| 180 } | |
| 181 std::string parent_path(udev_device_get_devpath(parent)); | |
| 182 if (parent_path.length() == 0 || *parent_path.rbegin() != '/') | |
| 183 parent_path += '/'; | |
| 184 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get()); | |
| 185 for (udev_list_entry* i = devices; i != NULL; | |
| 186 i = udev_list_entry_get_next(i)) { | |
| 187 ScopedUdevDevicePtr hid_dev( | |
| 188 udev_device_new_from_syspath(udev, udev_list_entry_get_name(i))); | |
| 189 const char* raw_path = udev_device_get_devnode(hid_dev.get()); | |
| 190 std::string device_path = udev_device_get_devpath(hid_dev.get()); | |
| 191 if (raw_path && | |
| 192 !device_path.compare(0, parent_path.length(), parent_path)) { | |
| 193 std::string sub_path = device_path.substr(parent_path.length()); | |
| 194 if (sub_path.substr(0, sizeof(kHidrawSubsystem) - 1) == | |
| 195 kHidrawSubsystem) { | |
| 196 *result = raw_path; | |
| 197 return true; | |
| 198 } | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 return false; | |
| 203 } | |
| 204 | |
| 205 } // namespace device | 160 } // namespace device |
| OLD | NEW |