Revert 281282 "Revert 281133 "chrome.hid: enrich model with repo..."
[chromium-blink-merge.git] / device / hid / hid_service_linux.cc
blob470afa82a4d4231f3cb4a5e96f7122d474e8b1e9
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
3 // found in the LICENSE file.
5 #include "device/hid/hid_service_linux.h"
7 #include <linux/hidraw.h>
8 #include <sys/ioctl.h>
9 #include <stdint.h>
11 #include <string>
13 #include "base/bind.h"
14 #include "base/files/file.h"
15 #include "base/files/file_path.h"
16 #include "base/logging.h"
17 #include "base/stl_util.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_piece.h"
20 #include "base/strings/string_split.h"
21 #include "device/hid/hid_connection_linux.h"
22 #include "device/hid/hid_device_info.h"
23 #include "device/hid/hid_report_descriptor.h"
24 #include "device/udev_linux/udev.h"
26 namespace device {
28 namespace {
30 const char kHIDSubSystem[] = "hid";
31 const char kHidrawSubsystem[] = "hidraw";
32 const char kHIDID[] = "HID_ID";
33 const char kHIDName[] = "HID_NAME";
34 const char kHIDUnique[] = "HID_UNIQ";
36 } // namespace
38 HidServiceLinux::HidServiceLinux() {
39 DeviceMonitorLinux* monitor = DeviceMonitorLinux::GetInstance();
40 monitor->AddObserver(this);
41 monitor->Enumerate(
42 base::Bind(&HidServiceLinux::OnDeviceAdded, base::Unretained(this)));
45 scoped_refptr<HidConnection> HidServiceLinux::Connect(
46 const HidDeviceId& device_id) {
47 HidDeviceInfo device_info;
48 if (!GetDeviceInfo(device_id, &device_info))
49 return NULL;
51 ScopedUdevDevicePtr device =
52 DeviceMonitorLinux::GetInstance()->GetDeviceFromPath(
53 device_info.device_id);
55 if (device) {
56 std::string dev_node;
57 if (!FindHidrawDevNode(device.get(), &dev_node)) {
58 LOG(ERROR) << "Cannot open HID device as hidraw device.";
59 return NULL;
61 return new HidConnectionLinux(device_info, dev_node);
64 return NULL;
67 HidServiceLinux::~HidServiceLinux() {
68 if (DeviceMonitorLinux::HasInstance())
69 DeviceMonitorLinux::GetInstance()->RemoveObserver(this);
72 void HidServiceLinux::OnDeviceAdded(udev_device* device) {
73 if (!device)
74 return;
76 const char* device_path = udev_device_get_syspath(device);
77 if (!device_path)
78 return;
79 const char* subsystem = udev_device_get_subsystem(device);
80 if (!subsystem || strcmp(subsystem, kHIDSubSystem) != 0)
81 return;
83 HidDeviceInfo device_info;
84 device_info.device_id = device_path;
86 uint32_t int_property = 0;
87 const char* str_property = NULL;
89 const char* hid_id = udev_device_get_property_value(device, kHIDID);
90 if (!hid_id)
91 return;
93 std::vector<std::string> parts;
94 base::SplitString(hid_id, ':', &parts);
95 if (parts.size() != 3) {
96 return;
99 if (HexStringToUInt(base::StringPiece(parts[1]), &int_property)) {
100 device_info.vendor_id = int_property;
103 if (HexStringToUInt(base::StringPiece(parts[2]), &int_property)) {
104 device_info.product_id = int_property;
107 str_property = udev_device_get_property_value(device, kHIDUnique);
108 if (str_property != NULL)
109 device_info.serial_number = str_property;
111 str_property = udev_device_get_property_value(device, kHIDName);
112 if (str_property != NULL)
113 device_info.product_name = str_property;
115 std::string dev_node;
116 if (!FindHidrawDevNode(device, &dev_node)) {
117 LOG(ERROR) << "Cannot find device node for HID device.";
118 return;
121 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
123 base::File device_file(base::FilePath(dev_node), flags);
124 if (!device_file.IsValid()) {
125 LOG(ERROR) << "Cannot open '" << dev_node << "': "
126 << base::File::ErrorToString(device_file.error_details());
127 return;
130 int desc_size = 0;
131 int res = ioctl(device_file.GetPlatformFile(), HIDIOCGRDESCSIZE, &desc_size);
132 if (res < 0) {
133 PLOG(ERROR) << "Failed to get report descriptor size";
134 device_file.Close();
135 return;
138 hidraw_report_descriptor rpt_desc;
139 rpt_desc.size = desc_size;
141 res = ioctl(device_file.GetPlatformFile(), HIDIOCGRDESC, &rpt_desc);
142 if (res < 0) {
143 PLOG(ERROR) << "Failed to get report descriptor";
144 device_file.Close();
145 return;
148 device_file.Close();
150 HidReportDescriptor report_descriptor(rpt_desc.value, rpt_desc.size);
151 report_descriptor.GetDetails(&device_info.collections,
152 &device_info.max_input_report_size,
153 &device_info.max_output_report_size,
154 &device_info.max_feature_report_size);
156 AddDevice(device_info);
159 void HidServiceLinux::OnDeviceRemoved(udev_device* device) {
160 const char* device_path = udev_device_get_syspath(device);;
161 if (device_path)
162 RemoveDevice(device_path);
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;
171 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev));
172 if (!enumerate) {
173 return false;
175 if (udev_enumerate_add_match_subsystem(enumerate.get(), kHidrawSubsystem)) {
176 return false;
178 if (udev_enumerate_scan_devices(enumerate.get())) {
179 return false;
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;
202 return false;
205 } // namespace device