Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / device / serial / serial_device_enumerator_linux.cc
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.
4
5 #include "device/serial/serial_device_enumerator_linux.h"
6
7 #include <stdint.h>
8
9 #include <memory>
10 #include <utility>
11
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/threading/thread_restrictions.h"
15
16 namespace device {
17
18 namespace {
19
20 const char kSerialSubsystem[] = "tty";
21
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";
27
28 }  // namespace
29
30 // static
31 std::unique_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() {
32   return std::unique_ptr<SerialDeviceEnumerator>(
33       new SerialDeviceEnumeratorLinux());
34 }
35
36 SerialDeviceEnumeratorLinux::SerialDeviceEnumeratorLinux() {
37   udev_.reset(udev_new());
38 }
39
40 SerialDeviceEnumeratorLinux::~SerialDeviceEnumeratorLinux() = default;
41
42 std::vector<mojom::SerialDeviceInfoPtr>
43 SerialDeviceEnumeratorLinux::GetDevices() {
44   base::AssertBlockingAllowed();
45
46   std::vector<mojom::SerialDeviceInfoPtr> devices;
47   ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev_.get()));
48   if (!enumerate) {
49     LOG(ERROR) << "Serial device enumeration failed.";
50     return devices;
51   }
52   if (udev_enumerate_add_match_subsystem(enumerate.get(), kSerialSubsystem)) {
53     LOG(ERROR) << "Serial device enumeration failed.";
54     return devices;
55   }
56   if (udev_enumerate_scan_devices(enumerate.get())) {
57     LOG(ERROR) << "Serial device enumeration failed.";
58     return devices;
59   }
60
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.
70     const char* path =
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();
75       info->path = path;
76
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);
83
84       uint32_t int_value;
85       if (vendor_id && base::HexStringToUInt(vendor_id, &int_value)) {
86         info->vendor_id = int_value;
87         info->has_vendor_id = true;
88       }
89       if (product_id && base::HexStringToUInt(product_id, &int_value)) {
90         info->product_id = int_value;
91         info->has_product_id = true;
92       }
93       if (product_name)
94         info->display_name.emplace(product_name);
95       devices.push_back(std::move(info));
96     }
97   }
98   return devices;
99 }
100
101 }  // namespace device