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/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 mojo::Array<serial::DeviceInfoPtr> SerialDeviceEnumeratorLinux::GetDevices() { 48 mojo::Array<serial::DeviceInfoPtr> devices(0); 49 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev_.get())); 50 if (!enumerate) { 51 LOG(ERROR) << "Serial device enumeration failed."; 52 return devices.Pass(); 53 } 54 if (udev_enumerate_add_match_subsystem(enumerate.get(), kSerialSubsystem)) { 55 LOG(ERROR) << "Serial device enumeration failed."; 56 return devices.Pass(); 57 } 58 if (udev_enumerate_scan_devices(enumerate.get())) { 59 LOG(ERROR) << "Serial device enumeration failed."; 60 return devices.Pass(); 61 } 62 63 udev_list_entry* entry = udev_enumerate_get_list_entry(enumerate.get()); 64 for (; entry != NULL; entry = udev_list_entry_get_next(entry)) { 65 ScopedUdevDevicePtr device(udev_device_new_from_syspath( 66 udev_.get(), udev_list_entry_get_name(entry))); 67 // TODO(rockot): There may be a better way to filter serial devices here, 68 // but it's not clear what that would be. Udev will list lots of virtual 69 // devices with no real endpoint to back them anywhere. The presence of 70 // a bus identifier (e.g., "pci" or "usb") seems to be a good heuristic 71 // for detecting actual devices. 72 const char* path = 73 udev_device_get_property_value(device.get(), kHostPathKey); 74 const char* bus = udev_device_get_property_value(device.get(), kHostBusKey); 75 if (path != NULL && bus != NULL) { 76 serial::DeviceInfoPtr info(serial::DeviceInfo::New()); 77 info->path = path; 78 79 const char* vendor_id = 80 udev_device_get_property_value(device.get(), kVendorIDKey); 81 const char* product_id = 82 udev_device_get_property_value(device.get(), kProductIDKey); 83 const char* product_name = 84 udev_device_get_property_value(device.get(), kProductNameKey); 85 86 uint32 int_value; 87 if (vendor_id && base::HexStringToUInt(vendor_id, &int_value)) { 88 info->vendor_id = int_value; 89 info->has_vendor_id = true; 90 } 91 if (product_id && base::HexStringToUInt(product_id, &int_value)) { 92 info->product_id = int_value; 93 info->has_product_id = true; 94 } 95 if (product_name) 96 info->display_name = product_name; 97 devices.push_back(info.Pass()); 98 } 99 } 100 return devices.Pass(); 101 } 102 103 void SerialDeviceEnumeratorLinux::UdevDeleter::operator()(udev* handle) { 104 udev_unref(handle); 105 } 106 107 } // namespace device 108