| OLD | NEW |
| 1 // Copyright (c) 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 <libudev.h> | 5 #include <libudev.h> |
| 6 #include <stdint.h> |
| 7 |
| 6 #include <string> | 8 #include <string> |
| 7 #include <vector> | 9 #include <vector> |
| 8 | 10 |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/bind.h" | |
| 11 #include "base/callback.h" | |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "base/memory/scoped_vector.h" | |
| 14 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
| 13 #include "base/stl_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
| 17 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 18 #include "base/threading/thread_restrictions.h" | |
| 19 #include "device/hid/hid_connection.h" | |
| 20 #include "device/hid/hid_connection_linux.h" | 17 #include "device/hid/hid_connection_linux.h" |
| 21 #include "device/hid/hid_device_info.h" | 18 #include "device/hid/hid_device_info.h" |
| 22 #include "device/hid/hid_service_linux.h" | 19 #include "device/hid/hid_service_linux.h" |
| 23 | 20 |
| 24 namespace device { | 21 namespace device { |
| 25 | 22 |
| 26 namespace { | 23 namespace { |
| 27 | 24 |
| 28 const char kUdevName[] = "udev"; | 25 const char kUdevName[] = "udev"; |
| 29 const char kUdevActionAdd[] = "add"; | 26 const char kUdevActionAdd[] = "add"; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 monitor_fd_, | 69 monitor_fd_, |
| 73 true, | 70 true, |
| 74 base::MessageLoopForIO::WATCH_READ, | 71 base::MessageLoopForIO::WATCH_READ, |
| 75 &monitor_watcher_, | 72 &monitor_watcher_, |
| 76 this)) | 73 this)) |
| 77 return; | 74 return; |
| 78 | 75 |
| 79 Enumerate(); | 76 Enumerate(); |
| 80 } | 77 } |
| 81 | 78 |
| 79 scoped_refptr<HidConnection> HidServiceLinux::Connect( |
| 80 const HidDeviceId& device_id) { |
| 81 HidDeviceInfo device_info; |
| 82 if (!GetDeviceInfo(device_id, &device_info)) |
| 83 return NULL; |
| 84 |
| 85 ScopedUdevDevicePtr hid_device( |
| 86 udev_device_new_from_syspath(udev_.get(), device_info.device_id.c_str())); |
| 87 if (hid_device) { |
| 88 return new HidConnectionLinux(device_info, hid_device.Pass()); |
| 89 } |
| 90 return NULL; |
| 91 } |
| 92 |
| 93 void HidServiceLinux::OnFileCanReadWithoutBlocking(int fd) { |
| 94 DCHECK_EQ(monitor_fd_, fd); |
| 95 |
| 96 ScopedUdevDevicePtr dev(udev_monitor_receive_device(monitor_.get())); |
| 97 if (!dev) |
| 98 return; |
| 99 |
| 100 std::string action(udev_device_get_action(dev.get())); |
| 101 if (action == kUdevActionAdd) { |
| 102 PlatformAddDevice(dev.get()); |
| 103 } else if (action == kUdevActionRemove) { |
| 104 PlatformRemoveDevice(dev.get()); |
| 105 } |
| 106 } |
| 107 |
| 108 void HidServiceLinux::OnFileCanWriteWithoutBlocking(int fd) {} |
| 109 |
| 82 HidServiceLinux::~HidServiceLinux() { | 110 HidServiceLinux::~HidServiceLinux() { |
| 83 monitor_watcher_.StopWatchingFileDescriptor(); | 111 monitor_watcher_.StopWatchingFileDescriptor(); |
| 84 close(monitor_fd_); | 112 close(monitor_fd_); |
| 85 } | 113 } |
| 86 | 114 |
| 87 void HidServiceLinux::Enumerate() { | 115 void HidServiceLinux::Enumerate() { |
| 88 scoped_ptr<udev_enumerate, UdevEnumerateDeleter> enumerate( | 116 scoped_ptr<udev_enumerate, UdevEnumerateDeleter> enumerate( |
| 89 udev_enumerate_new(udev_.get())); | 117 udev_enumerate_new(udev_.get())); |
| 90 | 118 |
| 91 if (!enumerate) { | 119 if (!enumerate) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 103 return; | 131 return; |
| 104 } | 132 } |
| 105 | 133 |
| 106 // This list is managed by |enumerate|. | 134 // This list is managed by |enumerate|. |
| 107 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get()); | 135 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get()); |
| 108 for (udev_list_entry* i = devices; i != NULL; | 136 for (udev_list_entry* i = devices; i != NULL; |
| 109 i = udev_list_entry_get_next(i)) { | 137 i = udev_list_entry_get_next(i)) { |
| 110 ScopedUdevDevicePtr hid_dev( | 138 ScopedUdevDevicePtr hid_dev( |
| 111 udev_device_new_from_syspath(udev_.get(), udev_list_entry_get_name(i))); | 139 udev_device_new_from_syspath(udev_.get(), udev_list_entry_get_name(i))); |
| 112 if (hid_dev) { | 140 if (hid_dev) { |
| 113 PlatformDeviceAdd(hid_dev.get()); | 141 PlatformAddDevice(hid_dev.get()); |
| 114 } | 142 } |
| 115 } | 143 } |
| 116 | |
| 117 initialized_ = true; | |
| 118 } | 144 } |
| 119 | 145 |
| 120 void HidServiceLinux::PlatformDeviceAdd(udev_device* device) { | 146 void HidServiceLinux::PlatformAddDevice(udev_device* device) { |
| 121 if (!device) | 147 if (!device) |
| 122 return; | 148 return; |
| 123 | 149 |
| 124 const char* device_id = udev_device_get_syspath(device); | 150 const char* device_path = udev_device_get_syspath(device); |
| 125 if (!device_id) | 151 if (!device_path) |
| 126 return; | 152 return; |
| 127 | 153 |
| 154 HidDeviceInfo device_info; |
| 155 device_info.device_id = device_path; |
| 128 | 156 |
| 129 HidDeviceInfo device_info; | 157 uint32_t int_property = 0; |
| 130 device_info.device_id = device_id; | |
| 131 | |
| 132 uint32 int_property = 0; | |
| 133 const char* str_property = NULL; | 158 const char* str_property = NULL; |
| 134 | 159 |
| 135 const char* hid_id = udev_device_get_property_value(device, kHIDID); | 160 const char* hid_id = udev_device_get_property_value(device, kHIDID); |
| 136 if (!hid_id) | 161 if (!hid_id) |
| 137 return; | 162 return; |
| 138 | 163 |
| 139 std::vector<std::string> parts; | 164 std::vector<std::string> parts; |
| 140 base::SplitString(hid_id, ':', &parts); | 165 base::SplitString(hid_id, ':', &parts); |
| 141 if (parts.size() != 3) { | 166 if (parts.size() != 3) { |
| 142 return; | 167 return; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 154 if (str_property != NULL) | 179 if (str_property != NULL) |
| 155 device_info.serial_number = str_property; | 180 device_info.serial_number = str_property; |
| 156 | 181 |
| 157 str_property = udev_device_get_property_value(device, kHIDName); | 182 str_property = udev_device_get_property_value(device, kHIDName); |
| 158 if (str_property != NULL) | 183 if (str_property != NULL) |
| 159 device_info.product_name = str_property; | 184 device_info.product_name = str_property; |
| 160 | 185 |
| 161 AddDevice(device_info); | 186 AddDevice(device_info); |
| 162 } | 187 } |
| 163 | 188 |
| 164 void HidServiceLinux::PlatformDeviceRemove(udev_device* raw_dev) { | 189 void HidServiceLinux::PlatformRemoveDevice(udev_device* raw_dev) { |
| 165 // The returned the device is not referenced. | 190 // The returned the device is not referenced. |
| 166 udev_device* hid_dev = | 191 udev_device* hid_dev = |
| 167 udev_device_get_parent_with_subsystem_devtype(raw_dev, "hid", NULL); | 192 udev_device_get_parent_with_subsystem_devtype(raw_dev, "hid", NULL); |
| 168 | 193 |
| 169 if (!hid_dev) | 194 if (!hid_dev) |
| 170 return; | 195 return; |
| 171 | 196 |
| 172 const char* device_id = NULL; | 197 const char* device_path = NULL; |
| 173 device_id = udev_device_get_syspath(hid_dev); | 198 device_path = udev_device_get_syspath(hid_dev); |
| 174 if (device_id == NULL) | 199 if (device_path == NULL) |
| 175 return; | 200 return; |
| 176 | 201 |
| 177 RemoveDevice(device_id); | 202 RemoveDevice(device_path); |
| 178 } | 203 } |
| 179 | 204 |
| 180 scoped_refptr<HidConnection> HidServiceLinux::Connect(std::string device_id) { | |
| 181 if (!ContainsKey(devices_, device_id)) | |
| 182 return NULL; | |
| 183 ScopedUdevDevicePtr hid_device( | |
| 184 udev_device_new_from_syspath(udev_.get(), device_id.c_str())); | |
| 185 if (hid_device) { | |
| 186 scoped_refptr<HidConnectionLinux> connection = | |
| 187 new HidConnectionLinux(devices_[device_id], hid_device.Pass()); | |
| 188 if (connection->initialized()) | |
| 189 return connection; | |
| 190 } | |
| 191 return NULL; | |
| 192 } | |
| 193 | |
| 194 void HidServiceLinux::OnFileCanReadWithoutBlocking(int fd) { | |
| 195 DCHECK_EQ(monitor_fd_, fd); | |
| 196 | |
| 197 ScopedUdevDevicePtr dev(udev_monitor_receive_device(monitor_.get())); | |
| 198 if (!dev) | |
| 199 return; | |
| 200 | |
| 201 std::string action(udev_device_get_action(dev.get())); | |
| 202 if (action == kUdevActionAdd) { | |
| 203 PlatformDeviceAdd(dev.get()); | |
| 204 } else if (action == kUdevActionRemove) { | |
| 205 PlatformDeviceRemove(dev.get()); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 void HidServiceLinux::OnFileCanWriteWithoutBlocking(int fd) {} | |
| 210 | |
| 211 } // namespace dev | 205 } // namespace dev |
| OLD | NEW |