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"
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/threading/scoped_blocking_call.h"
20 const char kSerialSubsystem[] = "tty";
22 const char kHostPathKey[] = "DEVNAME";
23 const char kHostBusKey[] = "ID_BUS";
24 const char kVendorIDKey[] = "ID_VENDOR_ID";
25 const char kProductIDKey[] = "ID_MODEL_ID";
26 const char kProductNameKey[] = "ID_MODEL";
31 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() {
32 return std::unique_ptr<SerialDeviceEnumerator>(
33 new SerialDeviceEnumeratorLinux());
36 SerialDeviceEnumeratorLinux::SerialDeviceEnumeratorLinux() {
37 udev_.reset(udev_new());
40 SerialDeviceEnumeratorLinux::~SerialDeviceEnumeratorLinux() = default;
42 std::vector<mojom::SerialDeviceInfoPtr>
43 SerialDeviceEnumeratorLinux::GetDevices() {
44 base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK);
46 std::vector<mojom::SerialDeviceInfoPtr> devices;
47 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev_.get()));
49 LOG(ERROR) << "Serial device enumeration failed.";
52 if (udev_enumerate_add_match_subsystem(enumerate.get(), kSerialSubsystem)) {
53 LOG(ERROR) << "Serial device enumeration failed.";
56 if (udev_enumerate_scan_devices(enumerate.get())) {
57 LOG(ERROR) << "Serial device enumeration failed.";
61 udev_list_entry* entry = udev_enumerate_get_list_entry(enumerate.get());
62 for (; entry != NULL; entry = udev_list_entry_get_next(entry)) {
63 ScopedUdevDevicePtr device(udev_device_new_from_syspath(
64 udev_.get(), udev_list_entry_get_name(entry)));
65 // TODO(rockot): There may be a better way to filter serial devices here,
66 // but it's not clear what that would be. Udev will list lots of virtual
67 // devices with no real endpoint to back them anywhere. The presence of
68 // a bus identifier (e.g., "pci" or "usb") seems to be a good heuristic
69 // for detecting actual devices.
71 udev_device_get_property_value(device.get(), kHostPathKey);
72 const char* bus = udev_device_get_property_value(device.get(), kHostBusKey);
73 if (path != NULL && bus != NULL) {
74 auto info = mojom::SerialDeviceInfo::New();
77 const char* vendor_id =
78 udev_device_get_property_value(device.get(), kVendorIDKey);
79 const char* product_id =
80 udev_device_get_property_value(device.get(), kProductIDKey);
81 const char* product_name =
82 udev_device_get_property_value(device.get(), kProductNameKey);
85 if (vendor_id && base::HexStringToUInt(vendor_id, &int_value)) {
86 info->vendor_id = int_value;
87 info->has_vendor_id = true;
89 if (product_id && base::HexStringToUInt(product_id, &int_value)) {
90 info->product_id = int_value;
91 info->has_product_id = true;
94 info->display_name.emplace(product_name);
95 devices.push_back(std::move(info));
101 } // namespace device