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

Unified Diff: device/hid/hid_service_linux.cc

Issue 225513005: chrome.hid : enrich device info with Top-Level collections usages (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: enrich device info with hid usages Created 6 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: device/hid/hid_service_linux.cc
diff --git a/device/hid/hid_service_linux.cc b/device/hid/hid_service_linux.cc
index 04a3c160664468d68db60c74e28f4925e52ec63f..cede20f54d56e3726ea7158dd0382e5ab72969d0 100644
--- a/device/hid/hid_service_linux.cc
+++ b/device/hid/hid_service_linux.cc
@@ -2,12 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include <libudev.h>
+#include <linux/hidraw.h>
+#include <sys/ioctl.h>
+
#include <stdint.h>
#include <string>
-#include <vector>
+#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/platform_file.h"
#include "base/stl_util.h"
@@ -16,6 +18,7 @@
#include "base/strings/string_split.h"
#include "device/hid/hid_connection_linux.h"
#include "device/hid/hid_device_info.h"
+#include "device/hid/hid_report_descriptor.h"
#include "device/hid/hid_service_linux.h"
namespace device {
@@ -26,7 +29,7 @@ const char kUdevName[] = "udev";
const char kUdevActionAdd[] = "add";
const char kUdevActionRemove[] = "remove";
const char kHIDSubSystem[] = "hid";
-
+const char kHidrawSubsystem[] = "hidraw";
const char kHIDID[] = "HID_ID";
const char kHIDName[] = "HID_NAME";
const char kHIDUnique[] = "HID_UNIQ";
@@ -84,8 +87,14 @@ scoped_refptr<HidConnection> HidServiceLinux::Connect(
ScopedUdevDevicePtr hid_device(
udev_device_new_from_syspath(udev_.get(), device_info.device_id.c_str()));
+
if (hid_device) {
- return new HidConnectionLinux(device_info, hid_device.Pass());
+ std::string dev_node;
+ if (!FindHidrawDevNode(hid_device.get(), &dev_node)) {
+ LOG(ERROR) << "Cannot open HID device as hidraw device.";
+ return NULL;
+ }
+ return new HidConnectionLinux(device_info, dev_node);
}
return NULL;
}
@@ -183,6 +192,47 @@ void HidServiceLinux::PlatformAddDevice(udev_device* device) {
if (str_property != NULL)
device_info.product_name = str_property;
+ std::string dev_node;
+ if (!FindHidrawDevNode(device, &dev_node)) {
+ LOG(ERROR) << "Cannot open HID device as hidraw device.";
+ return;
+ }
+
+ int flags = base::File::FLAG_OPEN |
+ base::File::FLAG_READ;
+
+ base::File device_file(base::FilePath(dev_node), flags);
+ if (!device_file.IsValid()) {
+ LOG(ERROR) << device_file.error_details();
+ return;
+ }
+
+ int desc_size = 0;
+ int res = ioctl(device_file.GetPlatformFile(), HIDIOCGRDESCSIZE, &desc_size);
+ if (res < 0)
+ {
+ LOG(ERROR) << "HIDIOCGRDESCSIZE failed.";
+ device_file.Close();
+ return;
+ }
+
+ hidraw_report_descriptor rpt_desc;
+ rpt_desc.size = desc_size;
+
+ res = ioctl(device_file.GetPlatformFile(), HIDIOCGRDESC, &rpt_desc);
+ if (res < 0)
+ {
+ LOG(ERROR) << "HIDIOCGRDESC failed.";
+ device_file.Close();
+ return;
+ }
+
+ device_file.Close();
+
+ HidReportDescriptor report_descriptor(rpt_desc.value, rpt_desc.size);
+
+ device_info.usages = report_descriptor.topLevelCollections();
+
AddDevice(device_info);
}
@@ -194,4 +244,43 @@ void HidServiceLinux::PlatformRemoveDevice(udev_device* raw_dev) {
RemoveDevice(device_path);
}
+bool HidServiceLinux::FindHidrawDevNode(udev_device* parent,
+ std::string* result) {
+ udev* udev = udev_device_get_udev(parent);
+ if (!udev) {
+ return false;
+ }
+ ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev));
+ if (!enumerate) {
+ return false;
+ }
+ if (udev_enumerate_add_match_subsystem(enumerate.get(), kHidrawSubsystem)) {
+ return false;
+ }
+ if (udev_enumerate_scan_devices(enumerate.get())) {
+ return false;
+ }
+ std::string parent_path(udev_device_get_devpath(parent));
+ if (parent_path.length() == 0 || *parent_path.rbegin() != '/')
+ parent_path += '/';
+ udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get());
+ for (udev_list_entry* i = devices; i != NULL;
+ i = udev_list_entry_get_next(i)) {
+ ScopedUdevDevicePtr hid_dev(
+ udev_device_new_from_syspath(udev, udev_list_entry_get_name(i)));
+ const char* raw_path = udev_device_get_devnode(hid_dev.get());
+ std::string device_path = udev_device_get_devpath(hid_dev.get());
+ if (raw_path &&
+ !device_path.compare(0, parent_path.length(), parent_path)) {
+ std::string sub_path = device_path.substr(parent_path.length());
+ if (sub_path.substr(0, sizeof(kHidrawSubsystem)-1) == kHidrawSubsystem) {
+ *result = raw_path;
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
} // namespace dev

Powered by Google App Engine
This is Rietveld 408576698