OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/browser/devtools/adb/android_usb_device.h" | 5 #include "chrome/browser/devtools/adb/android_usb_device.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "chrome/browser/devtools/adb/android_rsa.h" | 13 #include "chrome/browser/devtools/adb/android_rsa.h" |
14 #include "chrome/browser/devtools/adb/android_usb_socket.h" | 14 #include "chrome/browser/devtools/adb/android_usb_socket.h" |
15 #include "chrome/browser/usb/usb_interface.h" | 15 #include "chrome/browser/usb/usb_interface.h" |
16 #include "chrome/browser/usb/usb_service.h" | 16 #include "chrome/browser/usb/usb_service.h" |
17 #include "chrome/browser/usb/usb_service_factory.h" | 17 #include "chrome/browser/usb/usb_service_factory.h" |
18 #include "crypto/rsa_private_key.h" | 18 #include "crypto/rsa_private_key.h" |
19 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
21 #include "net/socket/stream_socket.h" | 21 #include "net/socket/stream_socket.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 void Noop() {} | |
26 void BoolNoop(bool success) {} | |
27 | |
28 const size_t kHeaderSize = 24; | 25 const size_t kHeaderSize = 24; |
29 | 26 |
30 const int kAdbClass = 0xff; | 27 const int kAdbClass = 0xff; |
31 const int kAdbSubclass = 0x42; | 28 const int kAdbSubclass = 0x42; |
32 const int kAdbProtocol = 0x1; | 29 const int kAdbProtocol = 0x1; |
33 | 30 |
34 const int kUsbTimeout = 0; | 31 const int kUsbTimeout = 0; |
35 | 32 |
36 const uint32 kMaxPayload = 4096; | 33 const uint32 kMaxPayload = 4096; |
37 const uint32 kVersion = 0x01000000; | 34 const uint32 kVersion = 0x01000000; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 int j; | 83 int j; |
87 for (j = 1; j < res; ++j) | 84 for (j = 1; j < res; ++j) |
88 serial[j - 1] = buffer[j]; | 85 serial[j - 1] = buffer[j]; |
89 serial[j - 1] = '\0'; | 86 serial[j - 1] = '\0'; |
90 return std::string(serial, j); | 87 return std::string(serial, j); |
91 } | 88 } |
92 } | 89 } |
93 return std::string(); | 90 return std::string(); |
94 } | 91 } |
95 | 92 |
96 static void InterfaceClaimed(crypto::RSAPrivateKey* rsa_key, | |
97 scoped_refptr<UsbDeviceHandle> usb_device, | |
98 int inbound_address, | |
99 int outbound_address, | |
100 int zero_mask, | |
101 AndroidUsbDevices* devices, | |
102 bool success) { | |
103 if (!success) | |
104 return; | |
105 | |
106 std::string serial = ReadSerialNumSync(usb_device->handle()); | |
107 scoped_refptr<AndroidUsbDevice> device = | |
108 new AndroidUsbDevice(rsa_key, usb_device, serial, inbound_address, | |
109 outbound_address, zero_mask); | |
110 devices->push_back(device); | |
111 } | |
112 | |
113 static void ClaimInterface( | 93 static void ClaimInterface( |
114 crypto::RSAPrivateKey* rsa_key, | 94 crypto::RSAPrivateKey* rsa_key, |
115 scoped_refptr<UsbDeviceHandle> usb_device, | 95 scoped_refptr<UsbDeviceHandle> usb_device, |
116 const UsbInterface* interface, | 96 const UsbInterface* interface, |
117 AndroidUsbDevices* devices) { | 97 AndroidUsbDevices* devices) { |
118 if (interface->GetNumAltSettings() == 0) | 98 if (interface->GetNumAltSettings() == 0) |
119 return; | 99 return; |
120 | 100 |
121 scoped_refptr<const UsbInterfaceDescriptor> idesc = | 101 scoped_refptr<const UsbInterfaceDescriptor> idesc = |
122 interface->GetAltSetting(0).get(); | 102 interface->GetAltSetting(0).get(); |
(...skipping 17 matching lines...) Expand all Loading... |
140 if (edesc->GetDirection() == USB_DIRECTION_INBOUND) | 120 if (edesc->GetDirection() == USB_DIRECTION_INBOUND) |
141 inbound_address = edesc->GetAddress(); | 121 inbound_address = edesc->GetAddress(); |
142 else | 122 else |
143 outbound_address = edesc->GetAddress(); | 123 outbound_address = edesc->GetAddress(); |
144 zero_mask = edesc->GetMaximumPacketSize() - 1; | 124 zero_mask = edesc->GetMaximumPacketSize() - 1; |
145 } | 125 } |
146 | 126 |
147 if (inbound_address == 0 || outbound_address == 0) | 127 if (inbound_address == 0 || outbound_address == 0) |
148 return; | 128 return; |
149 | 129 |
150 usb_device->ClaimInterface(1, base::Bind(&InterfaceClaimed, | 130 if (!usb_device->ClaimInterface(1)) |
151 rsa_key, usb_device, | 131 return; |
152 inbound_address, outbound_address, | |
153 zero_mask, devices)); | |
154 } | |
155 | 132 |
156 static void InterfacesListed( | 133 std::string serial = ReadSerialNumSync(usb_device->handle()); |
157 crypto::RSAPrivateKey* rsa_key, | 134 scoped_refptr<AndroidUsbDevice> device = |
158 scoped_refptr<UsbDeviceHandle> usb_device, | 135 new AndroidUsbDevice(rsa_key, usb_device, serial, inbound_address, |
159 scoped_refptr<UsbConfigDescriptor> config, | 136 outbound_address, zero_mask); |
160 AndroidUsbDevices* devices, | 137 devices->push_back(device); |
161 bool success) { | |
162 if (!success) | |
163 return; | |
164 for (size_t j = 0; j < config->GetNumInterfaces(); ++j) { | |
165 ClaimInterface(rsa_key, usb_device, config->GetInterface(j), | |
166 devices); | |
167 } | |
168 } | 138 } |
169 | 139 |
170 static uint32 Checksum(const std::string& data) { | 140 static uint32 Checksum(const std::string& data) { |
171 unsigned char* x = (unsigned char*)data.data(); | 141 unsigned char* x = (unsigned char*)data.data(); |
172 int count = data.length(); | 142 int count = data.length(); |
173 uint32 sum = 0; | 143 uint32 sum = 0; |
174 while (count-- > 0) | 144 while (count-- > 0) |
175 sum += *x++; | 145 sum += *x++; |
176 return sum; | 146 return sum; |
177 } | 147 } |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 ++it; | 220 ++it; |
251 } | 221 } |
252 | 222 |
253 // Add new devices. | 223 // Add new devices. |
254 for (UsbDevices::iterator it = usb_devices.begin(); it != usb_devices.end(); | 224 for (UsbDevices::iterator it = usb_devices.begin(); it != usb_devices.end(); |
255 ++it) { | 225 ++it) { |
256 scoped_refptr<UsbDeviceHandle> usb_device = *it; | 226 scoped_refptr<UsbDeviceHandle> usb_device = *it; |
257 if (claimed_devices.find(usb_device.get()) != claimed_devices.end()) | 227 if (claimed_devices.find(usb_device.get()) != claimed_devices.end()) |
258 continue; | 228 continue; |
259 scoped_refptr<UsbConfigDescriptor> config = new UsbConfigDescriptor(); | 229 scoped_refptr<UsbConfigDescriptor> config = new UsbConfigDescriptor(); |
260 usb_device->ListInterfaces(config.get(), | 230 bool success = usb_device->ListInterfaces(config.get()); |
261 base::Bind(&InterfacesListed, rsa_key, | 231 if (!success) |
262 usb_device, config, | 232 continue; |
263 &g_devices.Get())); | 233 for (size_t j = 0; j < config->GetNumInterfaces(); ++j) { |
| 234 ClaimInterface(rsa_key, usb_device, config->GetInterface(j), |
| 235 &g_devices.Get()); |
| 236 } |
264 } | 237 } |
265 callback.Run(g_devices.Get()); | 238 callback.Run(g_devices.Get()); |
266 } | 239 } |
267 | 240 |
268 AndroidUsbDevice::AndroidUsbDevice(crypto::RSAPrivateKey* rsa_key, | 241 AndroidUsbDevice::AndroidUsbDevice(crypto::RSAPrivateKey* rsa_key, |
269 scoped_refptr<UsbDeviceHandle> usb_device, | 242 scoped_refptr<UsbDeviceHandle> usb_device, |
270 const std::string& serial, | 243 const std::string& serial, |
271 int inbound_address, | 244 int inbound_address, |
272 int outbound_address, | 245 int outbound_address, |
273 int zero_mask) | 246 int zero_mask) |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 | 499 |
527 terminated_ = true; | 500 terminated_ = true; |
528 | 501 |
529 // Iterate over copy. | 502 // Iterate over copy. |
530 AndroidUsbSockets sockets(sockets_); | 503 AndroidUsbSockets sockets(sockets_); |
531 for (AndroidUsbSockets::iterator it = sockets.begin(); | 504 for (AndroidUsbSockets::iterator it = sockets.begin(); |
532 it != sockets.end(); ++it) { | 505 it != sockets.end(); ++it) { |
533 it->second->Terminated(); | 506 it->second->Terminated(); |
534 } | 507 } |
535 | 508 |
536 usb_device_->ReleaseInterface(1, base::Bind(&BoolNoop)); | 509 usb_device_->ReleaseInterface(1); |
537 usb_device_->Close(base::Bind(&Noop)); | 510 usb_device_->Close(); |
538 } | 511 } |
539 | 512 |
540 void AndroidUsbDevice::SocketDeleted(uint32 socket_id) { | 513 void AndroidUsbDevice::SocketDeleted(uint32 socket_id) { |
541 sockets_.erase(socket_id); | 514 sockets_.erase(socket_id); |
542 } | 515 } |
OLD | NEW |