OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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/strings/string_number_conversions.h" |
| 9 |
| 10 namespace device { |
| 11 |
| 12 namespace { |
| 13 |
| 14 const char kSerialSubsystem[] = "tty"; |
| 15 |
| 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"; |
| 21 |
| 22 struct UdevEnumerateDeleter { |
| 23 void operator()(udev_enumerate* enumerate) { |
| 24 udev_enumerate_unref(enumerate); |
| 25 } |
| 26 }; |
| 27 |
| 28 struct UdevDeviceDeleter { |
| 29 void operator()(udev_device* device) { |
| 30 udev_device_unref(device); |
| 31 } |
| 32 }; |
| 33 |
| 34 typedef scoped_ptr<udev_enumerate, UdevEnumerateDeleter> ScopedUdevEnumeratePtr; |
| 35 typedef scoped_ptr<udev_device, UdevDeviceDeleter> ScopedUdevDevicePtr; |
| 36 |
| 37 } |
| 38 |
| 39 // static |
| 40 scoped_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { |
| 41 return scoped_ptr<SerialDeviceEnumerator>(new SerialDeviceEnumeratorLinux()); |
| 42 } |
| 43 |
| 44 SerialDeviceEnumeratorLinux::SerialDeviceEnumeratorLinux() { |
| 45 udev_.reset(udev_new()); |
| 46 } |
| 47 |
| 48 SerialDeviceEnumeratorLinux::~SerialDeviceEnumeratorLinux() { |
| 49 } |
| 50 |
| 51 void SerialDeviceEnumeratorLinux::GetDevices( |
| 52 std::vector<SerialDeviceInfo>* devices) { |
| 53 devices->clear(); |
| 54 |
| 55 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev_.get())); |
| 56 if (!enumerate) { |
| 57 LOG(ERROR) << "Serial device enumeration failed."; |
| 58 return; |
| 59 } |
| 60 if (udev_enumerate_add_match_subsystem(enumerate.get(), kSerialSubsystem)) { |
| 61 LOG(ERROR) << "Serial device enumeration failed."; |
| 62 return; |
| 63 } |
| 64 if (udev_enumerate_scan_devices(enumerate.get())) { |
| 65 LOG(ERROR) << "Serial device enumeration failed."; |
| 66 return; |
| 67 } |
| 68 |
| 69 udev_list_entry* entry = udev_enumerate_get_list_entry(enumerate.get()); |
| 70 for (; entry != NULL; entry = udev_list_entry_get_next(entry)) { |
| 71 ScopedUdevDevicePtr device( |
| 72 udev_device_new_from_syspath( |
| 73 udev_.get(), udev_list_entry_get_name(entry))); |
| 74 // TODO(rockot): There may be a better way to filter serial devices here, |
| 75 // but it's not clear what that would be. Udev will list lots of virtual |
| 76 // devices with no real endpoint to back them anywhere. The presence of |
| 77 // a bus identifier (e.g., "pci" or "usb") seems to be a good heuristic |
| 78 // for detecting actual devices. |
| 79 const char *path = |
| 80 udev_device_get_property_value(device.get(), kHostPathKey); |
| 81 const char *bus = |
| 82 udev_device_get_property_value(device.get(), kHostBusKey); |
| 83 if (path != NULL && bus != NULL) { |
| 84 SerialDeviceInfo info; |
| 85 info.path = std::string(path); |
| 86 |
| 87 const char *vendor_id = |
| 88 udev_device_get_property_value(device.get(), kVendorIDKey); |
| 89 const char *product_id = |
| 90 udev_device_get_property_value(device.get(), kProductIDKey); |
| 91 const char *product_name = |
| 92 udev_device_get_property_value(device.get(), kProductNameKey); |
| 93 |
| 94 uint32 int_value; |
| 95 if (vendor_id && base::HexStringToUInt(vendor_id, &int_value)) |
| 96 info.vendor_id = int_value; |
| 97 if (product_id && base::HexStringToUInt(product_id, &int_value)) |
| 98 info.product_id = int_value; |
| 99 if (product_name) |
| 100 info.display_name = product_name; |
| 101 |
| 102 devices->push_back(info); |
| 103 } |
| 104 } |
| 105 } |
| 106 |
| 107 void SerialDeviceEnumeratorLinux::UdevDeleter::operator()(udev* handle) { |
| 108 udev_unref(handle); |
| 109 } |
| 110 |
| 111 } // namespace device |
OLD | NEW |