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

Side by Side Diff: device/serial/serial_device_enumerator_linux.cc

Issue 158063002: Use udev for serial device enumeration on Linux (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: -___- Created 6 years, 10 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 "base/logging.h"
8 #include "base/memory/linked_ptr.h"
9 #include "base/strings/string_number_conversions.h"
10
11 namespace device {
12
13 namespace {
14
15 const char kSerialSubsystem[] = "tty";
16
17 const char kHostPathKey[] = "DEVNAME";
18 const char kHostBusKey[] = "ID_BUS";
19 const char kVendorIDKey[] = "ID_VENDOR_ID";
20 const char kProductIDKey[] = "ID_MODEL_ID";
21 const char kProductNameKey[] = "ID_MODEL";
22
23 struct UdevEnumerateDeleter {
24 void operator()(udev_enumerate* enumerate) {
25 udev_enumerate_unref(enumerate);
26 }
27 };
28
29 struct UdevDeviceDeleter {
30 void operator()(udev_device* device) { udev_device_unref(device); }
31 };
32
33 typedef scoped_ptr<udev_enumerate, UdevEnumerateDeleter> ScopedUdevEnumeratePtr;
34 typedef scoped_ptr<udev_device, UdevDeviceDeleter> ScopedUdevDevicePtr;
35 }
36
37 // static
38 scoped_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() {
39 return scoped_ptr<SerialDeviceEnumerator>(new SerialDeviceEnumeratorLinux());
40 }
41
42 SerialDeviceEnumeratorLinux::SerialDeviceEnumeratorLinux() {
43 udev_.reset(udev_new());
44 }
45
46 SerialDeviceEnumeratorLinux::~SerialDeviceEnumeratorLinux() {}
47
48 void SerialDeviceEnumeratorLinux::GetDevices(SerialDeviceInfoList* devices) {
49 devices->clear();
50
51 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev_.get()));
52 if (!enumerate) {
53 LOG(ERROR) << "Serial device enumeration failed.";
54 return;
55 }
56 if (udev_enumerate_add_match_subsystem(enumerate.get(), kSerialSubsystem)) {
57 LOG(ERROR) << "Serial device enumeration failed.";
58 return;
59 }
60 if (udev_enumerate_scan_devices(enumerate.get())) {
61 LOG(ERROR) << "Serial device enumeration failed.";
62 return;
63 }
64
65 udev_list_entry* entry = udev_enumerate_get_list_entry(enumerate.get());
66 for (; entry != NULL; entry = udev_list_entry_get_next(entry)) {
67 ScopedUdevDevicePtr device(udev_device_new_from_syspath(
68 udev_.get(), udev_list_entry_get_name(entry)));
69 // TODO(rockot): There may be a better way to filter serial devices here,
70 // but it's not clear what that would be. Udev will list lots of virtual
71 // devices with no real endpoint to back them anywhere. The presence of
72 // a bus identifier (e.g., "pci" or "usb") seems to be a good heuristic
73 // for detecting actual devices.
74 const char* path =
75 udev_device_get_property_value(device.get(), kHostPathKey);
76 const char* bus = udev_device_get_property_value(device.get(), kHostBusKey);
77 if (path != NULL && bus != NULL) {
78 linked_ptr<SerialDeviceInfo> info(new SerialDeviceInfo());
79 info->path = std::string(path);
80
81 const char* vendor_id =
82 udev_device_get_property_value(device.get(), kVendorIDKey);
83 const char* product_id =
84 udev_device_get_property_value(device.get(), kProductIDKey);
85 const char* product_name =
86 udev_device_get_property_value(device.get(), kProductNameKey);
87
88 uint32 int_value;
89 if (vendor_id && base::HexStringToUInt(vendor_id, &int_value))
90 info->vendor_id.reset(new uint16(int_value));
91 if (product_id && base::HexStringToUInt(product_id, &int_value))
92 info->product_id.reset(new uint16(int_value));
93 if (product_name)
94 info->display_name.reset(new std::string(product_name));
95
96 devices->push_back(info);
97 }
98 }
99 }
100
101 void SerialDeviceEnumeratorLinux::UdevDeleter::operator()(udev* handle) {
102 udev_unref(handle);
103 }
104
105 } // namespace device
OLDNEW
« no previous file with comments | « device/serial/serial_device_enumerator_linux.h ('k') | device/serial/serial_device_enumerator_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698