| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 return; | 100 return; |
| 104 } | 101 } |
| 105 | 102 |
| 106 // This list is managed by |enumerate|. | 103 // This list is managed by |enumerate|. |
| 107 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get()); | 104 udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate.get()); |
| 108 for (udev_list_entry* i = devices; i != NULL; | 105 for (udev_list_entry* i = devices; i != NULL; |
| 109 i = udev_list_entry_get_next(i)) { | 106 i = udev_list_entry_get_next(i)) { |
| 110 ScopedUdevDevicePtr hid_dev( | 107 ScopedUdevDevicePtr hid_dev( |
| 111 udev_device_new_from_syspath(udev_.get(), udev_list_entry_get_name(i))); | 108 udev_device_new_from_syspath(udev_.get(), udev_list_entry_get_name(i))); |
| 112 if (hid_dev) { | 109 if (hid_dev) { |
| 113 PlatformDeviceAdd(hid_dev.get()); | 110 PlatformAddDevice(hid_dev.get()); |
| 114 } | 111 } |
| 115 } | 112 } |
| 116 | |
| 117 initialized_ = true; | |
| 118 } | 113 } |
| 119 | 114 |
| 120 void HidServiceLinux::PlatformDeviceAdd(udev_device* device) { | 115 void HidServiceLinux::PlatformAddDevice(udev_device* device) { |
| 121 if (!device) | 116 if (!device) |
| 122 return; | 117 return; |
| 123 | 118 |
| 124 const char* device_id = udev_device_get_syspath(device); | 119 const char* device_path = udev_device_get_syspath(device); |
| 125 if (!device_id) | 120 if (!device_path) |
| 126 return; | 121 return; |
| 127 | 122 |
| 123 HidDeviceInfo device_info; |
| 124 device_info.device_id = GenerateDeviceId(); |
| 128 | 125 |
| 129 HidDeviceInfo device_info; | 126 uint32_t int_property = 0; |
| 130 device_info.device_id = device_id; | |
| 131 | |
| 132 uint32 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; |
| 143 } | 137 } |
| 144 | 138 |
| 145 if (HexStringToUInt(base::StringPiece(parts[1]), &int_property)) { | 139 if (HexStringToUInt(base::StringPiece(parts[1]), &int_property)) { |
| 146 device_info.vendor_id = int_property; | 140 device_info.vendor_id = int_property; |
| 147 } | 141 } |
| 148 | 142 |
| 149 if (HexStringToUInt(base::StringPiece(parts[2]), &int_property)) { | 143 if (HexStringToUInt(base::StringPiece(parts[2]), &int_property)) { |
| 150 device_info.product_id = int_property; | 144 device_info.product_id = int_property; |
| 151 } | 145 } |
| 152 | 146 |
| 153 str_property = udev_device_get_property_value(device, kHIDUnique); | 147 str_property = udev_device_get_property_value(device, kHIDUnique); |
| 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 |
| 155 if (!platform_resource_map_.Add(device_info.device_id, device_path)) |
| 156 return; |
| 157 |
| 161 AddDevice(device_info); | 158 AddDevice(device_info); |
| 162 } | 159 } |
| 163 | 160 |
| 164 void HidServiceLinux::PlatformDeviceRemove(udev_device* raw_dev) { | 161 void HidServiceLinux::PlatformRemoveDevice(udev_device* raw_dev) { |
| 165 // The returned the device is not referenced. | 162 // The returned the device is not referenced. |
| 166 udev_device* hid_dev = | 163 udev_device* hid_dev = |
| 167 udev_device_get_parent_with_subsystem_devtype(raw_dev, "hid", NULL); | 164 udev_device_get_parent_with_subsystem_devtype(raw_dev, "hid", NULL); |
| 168 | 165 |
| 169 if (!hid_dev) | 166 if (!hid_dev) |
| 170 return; | 167 return; |
| 171 | 168 |
| 172 const char* device_id = NULL; | 169 const char* device_path = NULL; |
| 173 device_id = udev_device_get_syspath(hid_dev); | 170 device_path = udev_device_get_syspath(hid_dev); |
| 174 if (device_id == NULL) | 171 if (device_path == NULL) |
| 175 return; | 172 return; |
| 176 | 173 |
| 174 HidDeviceId device_id; |
| 175 if (!platform_resource_map_.GetIdByResource(device_path, &device_id)) |
| 176 return; |
| 177 platform_resource_map_.RemoveById(device_id); |
| 178 |
| 177 RemoveDevice(device_id); | 179 RemoveDevice(device_id); |
| 178 } | 180 } |
| 179 | 181 |
| 180 scoped_refptr<HidConnection> HidServiceLinux::Connect(std::string device_id) { | 182 scoped_refptr<HidConnection> HidServiceLinux::Connect(HidDeviceId device_id) { |
| 181 if (!ContainsKey(devices_, device_id)) | 183 HidDeviceInfo device_info; |
| 184 if (!GetInfo(device_id, &device_info)) |
| 182 return NULL; | 185 return NULL; |
| 186 |
| 187 std::string device_path; |
| 188 if (!platform_resource_map_.GetResourceById(device_id, &device_path)) |
| 189 return NULL; |
| 190 |
| 183 ScopedUdevDevicePtr hid_device( | 191 ScopedUdevDevicePtr hid_device( |
| 184 udev_device_new_from_syspath(udev_.get(), device_id.c_str())); | 192 udev_device_new_from_syspath(udev_.get(), device_path.c_str())); |
| 185 if (hid_device) { | 193 if (hid_device) { |
| 186 scoped_refptr<HidConnectionLinux> connection = | 194 scoped_refptr<HidConnectionLinux> connection = |
| 187 new HidConnectionLinux(devices_[device_id], hid_device.Pass()); | 195 new HidConnectionLinux(device_info, hid_device.Pass()); |
| 188 if (connection->initialized()) | 196 if (connection->initialized()) |
| 189 return connection; | 197 return connection; |
| 190 } | 198 } |
| 191 return NULL; | 199 return NULL; |
| 192 } | 200 } |
| 193 | 201 |
| 194 void HidServiceLinux::OnFileCanReadWithoutBlocking(int fd) { | 202 void HidServiceLinux::OnFileCanReadWithoutBlocking(int fd) { |
| 195 DCHECK_EQ(monitor_fd_, fd); | 203 DCHECK_EQ(monitor_fd_, fd); |
| 196 | 204 |
| 197 ScopedUdevDevicePtr dev(udev_monitor_receive_device(monitor_.get())); | 205 ScopedUdevDevicePtr dev(udev_monitor_receive_device(monitor_.get())); |
| 198 if (!dev) | 206 if (!dev) |
| 199 return; | 207 return; |
| 200 | 208 |
| 201 std::string action(udev_device_get_action(dev.get())); | 209 std::string action(udev_device_get_action(dev.get())); |
| 202 if (action == kUdevActionAdd) { | 210 if (action == kUdevActionAdd) { |
| 203 PlatformDeviceAdd(dev.get()); | 211 PlatformAddDevice(dev.get()); |
| 204 } else if (action == kUdevActionRemove) { | 212 } else if (action == kUdevActionRemove) { |
| 205 PlatformDeviceRemove(dev.get()); | 213 PlatformRemoveDevice(dev.get()); |
| 206 } | 214 } |
| 207 } | 215 } |
| 208 | 216 |
| 209 void HidServiceLinux::OnFileCanWriteWithoutBlocking(int fd) {} | 217 void HidServiceLinux::OnFileCanWriteWithoutBlocking(int fd) {} |
| 210 | 218 |
| 211 } // namespace dev | 219 } // namespace dev |
| OLD | NEW |