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) { udev_device_unref(device); } |
| 30 }; |
| 31 |
| 32 typedef scoped_ptr<udev_enumerate, UdevEnumerateDeleter> ScopedUdevEnumeratePtr; |
| 33 typedef scoped_ptr<udev_device, UdevDeviceDeleter> ScopedUdevDevicePtr; |
| 34 } |
| 35 |
| 36 // static |
| 37 scoped_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { |
| 38 return scoped_ptr<SerialDeviceEnumerator>(new SerialDeviceEnumeratorLinux()); |
| 39 } |
| 40 |
| 41 SerialDeviceEnumeratorLinux::SerialDeviceEnumeratorLinux() { |
| 42 udev_.reset(udev_new()); |
| 43 } |
| 44 |
| 45 SerialDeviceEnumeratorLinux::~SerialDeviceEnumeratorLinux() {} |
| 46 |
| 47 void SerialDeviceEnumeratorLinux::GetDevices( |
| 48 std::vector<SerialDeviceInfo>* 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 SerialDeviceInfo info; |
| 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 = int_value; |
| 91 if (product_id && base::HexStringToUInt(product_id, &int_value)) |
| 92 info.product_id = int_value; |
| 93 if (product_name) |
| 94 info.display_name = 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 |
OLD | NEW |