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/serial/serial_device_enumerator_linux.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
14 const char kSerialSubsystem[] = "tty";
16 const char kHostPathKey[] = "DEVNAME";
17 const char kHostBusKey[] = "ID_BUS";
18 const char kVendorIDKey[] = "ID_VENDOR_ID";
19 const char kProductIDKey[] = "ID_MODEL_ID";
20 const char kProductNameKey[] = "ID_MODEL";
22 struct UdevEnumerateDeleter {
23 void operator()(udev_enumerate* enumerate) {
24 udev_enumerate_unref(enumerate);
28 struct UdevDeviceDeleter {
29 void operator()(udev_device* device) { udev_device_unref(device); }
32 typedef scoped_ptr<udev_enumerate, UdevEnumerateDeleter> ScopedUdevEnumeratePtr;
33 typedef scoped_ptr<udev_device, UdevDeviceDeleter> ScopedUdevDevicePtr;
37 scoped_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() {
38 return scoped_ptr<SerialDeviceEnumerator>(new SerialDeviceEnumeratorLinux());
41 SerialDeviceEnumeratorLinux::SerialDeviceEnumeratorLinux() {
42 udev_.reset(udev_new());
45 SerialDeviceEnumeratorLinux::~SerialDeviceEnumeratorLinux() {}
47 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorLinux::GetDevices() {
48 mojo::Array<serial::DeviceInfoPtr> devices(0);
49 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev_.get()));
51 LOG(ERROR) << "Serial device enumeration failed.";
52 return devices.Pass();
54 if (udev_enumerate_add_match_subsystem(enumerate.get(), kSerialSubsystem)) {
55 LOG(ERROR) << "Serial device enumeration failed.";
56 return devices.Pass();
58 if (udev_enumerate_scan_devices(enumerate.get())) {
59 LOG(ERROR) << "Serial device enumeration failed.";
60 return devices.Pass();
63 udev_list_entry* entry = udev_enumerate_get_list_entry(enumerate.get());
64 for (; entry != NULL; entry = udev_list_entry_get_next(entry)) {
65 ScopedUdevDevicePtr device(udev_device_new_from_syspath(
66 udev_.get(), udev_list_entry_get_name(entry)));
67 // TODO(rockot): There may be a better way to filter serial devices here,
68 // but it's not clear what that would be. Udev will list lots of virtual
69 // devices with no real endpoint to back them anywhere. The presence of
70 // a bus identifier (e.g., "pci" or "usb") seems to be a good heuristic
71 // for detecting actual devices.
73 udev_device_get_property_value(device.get(), kHostPathKey);
74 const char* bus = udev_device_get_property_value(device.get(), kHostBusKey);
75 if (path != NULL && bus != NULL) {
76 serial::DeviceInfoPtr info(serial::DeviceInfo::New());
79 const char* vendor_id =
80 udev_device_get_property_value(device.get(), kVendorIDKey);
81 const char* product_id =
82 udev_device_get_property_value(device.get(), kProductIDKey);
83 const char* product_name =
84 udev_device_get_property_value(device.get(), kProductNameKey);
87 if (vendor_id && base::HexStringToUInt(vendor_id, &int_value)) {
88 info->vendor_id = int_value;
89 info->has_vendor_id = true;
91 if (product_id && base::HexStringToUInt(product_id, &int_value)) {
92 info->product_id = int_value;
93 info->has_product_id = true;
96 info->display_name = product_name;
97 devices.push_back(info.Pass());
100 return devices.Pass();
103 void SerialDeviceEnumeratorLinux::UdevDeleter::operator()(udev* handle) {
107 } // namespace device