OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/serial/serial_device_enumerator_linux.h" | 5 #include "device/serial/serial_device_enumerator_linux.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/linked_ptr.h" | |
9 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
10 | 9 |
11 namespace device { | 10 namespace device { |
12 | 11 |
13 namespace { | 12 namespace { |
14 | 13 |
15 const char kSerialSubsystem[] = "tty"; | 14 const char kSerialSubsystem[] = "tty"; |
16 | 15 |
17 const char kHostPathKey[] = "DEVNAME"; | 16 const char kHostPathKey[] = "DEVNAME"; |
18 const char kHostBusKey[] = "ID_BUS"; | 17 const char kHostBusKey[] = "ID_BUS"; |
(...skipping 19 matching lines...) Expand all Loading... |
38 scoped_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { | 37 scoped_ptr<SerialDeviceEnumerator> SerialDeviceEnumerator::Create() { |
39 return scoped_ptr<SerialDeviceEnumerator>(new SerialDeviceEnumeratorLinux()); | 38 return scoped_ptr<SerialDeviceEnumerator>(new SerialDeviceEnumeratorLinux()); |
40 } | 39 } |
41 | 40 |
42 SerialDeviceEnumeratorLinux::SerialDeviceEnumeratorLinux() { | 41 SerialDeviceEnumeratorLinux::SerialDeviceEnumeratorLinux() { |
43 udev_.reset(udev_new()); | 42 udev_.reset(udev_new()); |
44 } | 43 } |
45 | 44 |
46 SerialDeviceEnumeratorLinux::~SerialDeviceEnumeratorLinux() {} | 45 SerialDeviceEnumeratorLinux::~SerialDeviceEnumeratorLinux() {} |
47 | 46 |
48 void SerialDeviceEnumeratorLinux::GetDevices(SerialDeviceInfoList* devices) { | 47 mojo::Array<SerialDeviceInfoPtr> SerialDeviceEnumeratorLinux::GetDevices() { |
49 devices->clear(); | 48 mojo::Array<SerialDeviceInfoPtr> devices; |
50 | |
51 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev_.get())); | 49 ScopedUdevEnumeratePtr enumerate(udev_enumerate_new(udev_.get())); |
52 if (!enumerate) { | 50 if (!enumerate) { |
53 LOG(ERROR) << "Serial device enumeration failed."; | 51 LOG(ERROR) << "Serial device enumeration failed."; |
54 return; | 52 return devices.Pass(); |
55 } | 53 } |
56 if (udev_enumerate_add_match_subsystem(enumerate.get(), kSerialSubsystem)) { | 54 if (udev_enumerate_add_match_subsystem(enumerate.get(), kSerialSubsystem)) { |
57 LOG(ERROR) << "Serial device enumeration failed."; | 55 LOG(ERROR) << "Serial device enumeration failed."; |
58 return; | 56 return devices.Pass(); |
59 } | 57 } |
60 if (udev_enumerate_scan_devices(enumerate.get())) { | 58 if (udev_enumerate_scan_devices(enumerate.get())) { |
61 LOG(ERROR) << "Serial device enumeration failed."; | 59 LOG(ERROR) << "Serial device enumeration failed."; |
62 return; | 60 return devices.Pass(); |
63 } | 61 } |
64 | 62 |
65 udev_list_entry* entry = udev_enumerate_get_list_entry(enumerate.get()); | 63 udev_list_entry* entry = udev_enumerate_get_list_entry(enumerate.get()); |
66 for (; entry != NULL; entry = udev_list_entry_get_next(entry)) { | 64 for (; entry != NULL; entry = udev_list_entry_get_next(entry)) { |
67 ScopedUdevDevicePtr device(udev_device_new_from_syspath( | 65 ScopedUdevDevicePtr device(udev_device_new_from_syspath( |
68 udev_.get(), udev_list_entry_get_name(entry))); | 66 udev_.get(), udev_list_entry_get_name(entry))); |
69 // TODO(rockot): There may be a better way to filter serial devices here, | 67 // 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 | 68 // 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 | 69 // 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 | 70 // a bus identifier (e.g., "pci" or "usb") seems to be a good heuristic |
73 // for detecting actual devices. | 71 // for detecting actual devices. |
74 const char* path = | 72 const char* path = |
75 udev_device_get_property_value(device.get(), kHostPathKey); | 73 udev_device_get_property_value(device.get(), kHostPathKey); |
76 const char* bus = udev_device_get_property_value(device.get(), kHostBusKey); | 74 const char* bus = udev_device_get_property_value(device.get(), kHostBusKey); |
77 if (path != NULL && bus != NULL) { | 75 if (path != NULL && bus != NULL) { |
78 linked_ptr<SerialDeviceInfo> info(new SerialDeviceInfo()); | 76 SerialDeviceInfoPtr info(SerialDeviceInfo::New()); |
79 info->path = std::string(path); | 77 info->path = path; |
80 | 78 |
81 const char* vendor_id = | 79 const char* vendor_id = |
82 udev_device_get_property_value(device.get(), kVendorIDKey); | 80 udev_device_get_property_value(device.get(), kVendorIDKey); |
83 const char* product_id = | 81 const char* product_id = |
84 udev_device_get_property_value(device.get(), kProductIDKey); | 82 udev_device_get_property_value(device.get(), kProductIDKey); |
85 const char* product_name = | 83 const char* product_name = |
86 udev_device_get_property_value(device.get(), kProductNameKey); | 84 udev_device_get_property_value(device.get(), kProductNameKey); |
87 | 85 |
88 uint32 int_value; | 86 uint32 int_value; |
89 if (vendor_id && base::HexStringToUInt(vendor_id, &int_value)) | 87 if (vendor_id && base::HexStringToUInt(vendor_id, &int_value)) { |
90 info->vendor_id.reset(new uint16(int_value)); | 88 info->vendor_id = int_value; |
91 if (product_id && base::HexStringToUInt(product_id, &int_value)) | 89 info->has_vendor_id = true; |
92 info->product_id.reset(new uint16(int_value)); | 90 } |
| 91 if (product_id && base::HexStringToUInt(product_id, &int_value)) { |
| 92 info->product_id = int_value; |
| 93 info->has_product_id = true; |
| 94 } |
93 if (product_name) | 95 if (product_name) |
94 info->display_name.reset(new std::string(product_name)); | 96 info->display_name = product_name; |
95 | 97 devices.push_back(info.Pass()); |
96 devices->push_back(info); | |
97 } | 98 } |
98 } | 99 } |
| 100 return devices.Pass(); |
99 } | 101 } |
100 | 102 |
101 void SerialDeviceEnumeratorLinux::UdevDeleter::operator()(udev* handle) { | 103 void SerialDeviceEnumeratorLinux::UdevDeleter::operator()(udev* handle) { |
102 udev_unref(handle); | 104 udev_unref(handle); |
103 } | 105 } |
104 | 106 |
105 } // namespace device | 107 } // namespace device |
OLD | NEW |