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