This repository was archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathcommon.py
More file actions
410 lines (336 loc) · 14.1 KB
/
common.py
File metadata and controls
410 lines (336 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Common code for ADB and Fastboot.
Common usb browsing, and usb communication.
"""
import logging
import platform
import socket
import threading
import weakref
import select
import serial
import libusb1
import usb1
from adb import usb_exceptions
DEFAULT_TIMEOUT_MS = 10000
_LOG = logging.getLogger('android_usb')
def GetInterface(setting):
"""Get the class, subclass, and protocol for the given USB setting."""
return (setting.getClass(), setting.getSubClass(), setting.getProtocol())
def InterfaceMatcher(clazz, subclass, protocol):
"""Returns a matcher that returns the setting with the given interface."""
interface = (clazz, subclass, protocol)
def Matcher(device):
for setting in device.iterSettings():
if GetInterface(setting) == interface:
return setting
return Matcher
class UsbHandle(object):
"""USB communication object. Not thread-safe.
Handles reading and writing over USB with the proper endpoints, exceptions,
and interface claiming.
Important methods:
FlushBuffers()
BulkRead(int length)
BulkWrite(bytes data)
"""
_HANDLE_CACHE = weakref.WeakValueDictionary()
_HANDLE_CACHE_LOCK = threading.Lock()
def __init__(self, device, setting, usb_info=None, timeout_ms=None):
"""Initialize USB Handle.
Arguments:
device: libusb_device to connect to.
setting: libusb setting with the correct endpoints to communicate with.
usb_info: String describing the usb path/serial/device, for debugging.
timeout_ms: Timeout in milliseconds for all I/O.
"""
self._setting = setting
self._device = device
self._handle = None
self._usb_info = usb_info or ''
self._timeout_ms = timeout_ms if timeout_ms else DEFAULT_TIMEOUT_MS
self._max_read_packet_len = 0
@property
def usb_info(self):
try:
sn = self.serial_number
except libusb1.USBError:
sn = ''
if sn and sn != self._usb_info:
return '%s %s' % (self._usb_info, sn)
return self._usb_info
def Open(self):
"""Opens the USB device for this setting, and claims the interface."""
# Make sure we close any previous handle open to this usb device.
port_path = tuple(self.port_path)
with self._HANDLE_CACHE_LOCK:
old_handle = self._HANDLE_CACHE.get(port_path)
if old_handle is not None:
old_handle.Close()
self._read_endpoint = None
self._write_endpoint = None
for endpoint in self._setting.iterEndpoints():
address = endpoint.getAddress()
if address & libusb1.USB_ENDPOINT_DIR_MASK:
self._read_endpoint = address
self._max_read_packet_len = endpoint.getMaxPacketSize()
else:
self._write_endpoint = address
assert self._read_endpoint is not None
assert self._write_endpoint is not None
handle = self._device.open()
iface_number = self._setting.getNumber()
try:
if (platform.system() != 'Windows'
and handle.kernelDriverActive(iface_number)):
handle.detachKernelDriver(iface_number)
except libusb1.USBError as e:
if e.value == libusb1.LIBUSB_ERROR_NOT_FOUND:
_LOG.warning('Kernel driver not found for interface: %s.', iface_number)
else:
raise
handle.claimInterface(iface_number)
self._handle = handle
self._interface_number = iface_number
with self._HANDLE_CACHE_LOCK:
self._HANDLE_CACHE[port_path] = self
# When this object is deleted, make sure it's closed.
weakref.ref(self, self.Close)
@property
def serial_number(self):
return self._device.getSerialNumber()
@property
def port_path(self):
return [self._device.getBusNumber()] + self._device.getPortNumberList()
def Close(self):
if self._handle is None:
return
try:
self._handle.releaseInterface(self._interface_number)
self._handle.close()
except libusb1.USBError:
_LOG.info('USBError while closing handle %s: ',
self.usb_info, exc_info=True)
finally:
self._handle = None
def Timeout(self, timeout_ms):
return timeout_ms if timeout_ms is not None else self._timeout_ms
def FlushBuffers(self):
while True:
try:
self.BulkRead(self._max_read_packet_len, timeout_ms=10)
except usb_exceptions.ReadFailedError as e:
if e.usb_error.value == libusb1.LIBUSB_ERROR_TIMEOUT:
break
raise
def BulkWrite(self, data, timeout_ms=None):
if self._handle is None:
raise usb_exceptions.WriteFailedError(
'This handle has been closed, probably due to another being opened.',
None)
try:
return self._handle.bulkWrite(
self._write_endpoint, data, timeout=self.Timeout(timeout_ms))
except libusb1.USBError as e:
raise usb_exceptions.WriteFailedError(
'Could not send data to %s (timeout %sms)' % (
self.usb_info, self.Timeout(timeout_ms)), e)
def BulkRead(self, length, timeout_ms=None):
if self._handle is None:
raise usb_exceptions.ReadFailedError(
'This handle has been closed, probably due to another being opened.',
None)
try:
# python-libusb1 > 1.6 exposes bytearray()s now instead of bytes/str.
# To support older and newer versions, we ensure everything's bytearray()
# from here on out.
return bytearray(self._handle.bulkRead(
self._read_endpoint, length, timeout=self.Timeout(timeout_ms)))
except libusb1.USBError as e:
raise usb_exceptions.ReadFailedError(
'Could not receive data from %s (timeout %sms)' % (
self.usb_info, self.Timeout(timeout_ms)), e)
def BulkReadAsync(self, length, timeout_ms=None):
# See: https://pypi.python.org/pypi/libusb1 "Asynchronous I/O" section
return
@classmethod
def PortPathMatcher(cls, port_path):
"""Returns a device matcher for the given port path."""
if isinstance(port_path, str):
# Convert from sysfs path to port_path.
port_path = [int(part) for part in SYSFS_PORT_SPLIT_RE.split(port_path)]
return lambda device: device.port_path == port_path
@classmethod
def SerialMatcher(cls, serial):
"""Returns a device matcher for the given serial."""
return lambda device: device.serial_number == serial
@classmethod
def FindAndOpen(cls, setting_matcher,
port_path=None, serial=None, timeout_ms=None):
dev = cls.Find(
setting_matcher, port_path=port_path, serial=serial,
timeout_ms=timeout_ms)
dev.Open()
dev.FlushBuffers()
return dev
@classmethod
def Find(cls, setting_matcher, port_path=None, serial=None, timeout_ms=None):
"""Gets the first device that matches according to the keyword args."""
if port_path:
device_matcher = cls.PortPathMatcher(port_path)
usb_info = port_path
elif serial:
device_matcher = cls.SerialMatcher(serial)
usb_info = serial
else:
device_matcher = None
usb_info = 'first'
return cls.FindFirst(setting_matcher, device_matcher,
usb_info=usb_info, timeout_ms=timeout_ms)
@classmethod
def FindFirst(cls, setting_matcher, device_matcher=None, **kwargs):
"""Find and return the first matching device.
Args:
setting_matcher: See cls.FindDevices.
device_matcher: See cls.FindDevices.
**kwargs: See cls.FindDevices.
Returns:
An instance of UsbHandle.
Raises:
DeviceNotFoundError: Raised if the device is not available.
"""
try:
return next(cls.FindDevices(
setting_matcher, device_matcher=device_matcher, **kwargs))
except StopIteration:
raise usb_exceptions.DeviceNotFoundError(
'No device available, or it is in the wrong configuration.')
@classmethod
def FindDevices(cls, setting_matcher, device_matcher=None,
usb_info='', timeout_ms=None):
"""Find and yield the devices that match.
Args:
setting_matcher: Function that returns the setting to use given a
usb1.USBDevice, or None if the device doesn't have a valid setting.
device_matcher: Function that returns True if the given UsbHandle is
valid. None to match any device.
usb_info: Info string describing device(s).
timeout_ms: Default timeout of commands in milliseconds.
Yields:
UsbHandle instances
"""
ctx = usb1.USBContext()
for device in ctx.getDeviceList(skip_on_error=True):
setting = setting_matcher(device)
if setting is None:
continue
handle = cls(device, setting, usb_info=usb_info, timeout_ms=timeout_ms)
if device_matcher is None or device_matcher(handle):
yield handle
class SerialHandle(object):
def __init__(self, serial, timeout_ms=None):
if isinstance(serial, (bytes, bytearray)):
serial = serial.decode('utf-8')
if ',' in serial:
self.port, self.speed = serial.split(',')
else:
self.port = serial
self.speed = 115200
self._connection = None
self._serial_number = '%s,%s' % (self.port, self.speed)
self._timeout_ms = float(timeout_ms) if timeout_ms else None
self._connect()
def _connect(self):
timeout = self.TimeoutSeconds(self._timeout_ms)
self._connection = serial.Serial(self.port, self.speed, timeout=timeout)
@property
def serial_number(self):
return self._serial_number
def BulkWrite(self, data, timeout=None):
t = self.TimeoutSeconds(timeout)
_, writeable, _ = select.select([], [self._connection], [], t)
if writeable:
return self._connection.write(data)
msg = 'Sending data to {} timed out after {}s. No data was sent.'.format(
self.serial_number, t)
raise serial.SerialTimeoutException(msg)
def BulkRead(self, numbytes, timeout=None):
t = self.TimeoutSeconds(timeout)
readable, _, _ = select.select([self._connection], [], [], t)
if readable:
return self._connection.read(numbytes)
msg = 'Reading from {} timed out (Timeout {}s)'.format(
self._serial_number, t)
#raise serial.TcpTimeoutException(msg)
def Timeout(self, timeout_ms):
return float(timeout_ms) if timeout_ms is not None else self._timeout_ms
def TimeoutSeconds(self, timeout_ms):
timeout = self.Timeout(timeout_ms)
return timeout / 1000.0 if timeout is not None else timeout
def Close(self):
return self._connection.close()
class TcpHandle(object):
"""TCP connection object.
Provides same interface as UsbHandle. """
def __init__(self, serial, timeout_ms=None):
"""Initialize the TCP Handle.
Arguments:
serial: Android device serial of the form host or host:port.
Host may be an IP address or a host name.
"""
# if necessary, convert serial to a unicode string
if isinstance(serial, (bytes, bytearray)):
serial = serial.decode('utf-8')
if ':' in serial:
self.host, self.port = serial.split(':')
else:
self.host = serial
self.port = 5555
self._connection = None
self._serial_number = '%s:%s' % (self.host, self.port)
self._timeout_ms = float(timeout_ms) if timeout_ms else None
self._connect()
def _connect(self):
timeout = self.TimeoutSeconds(self._timeout_ms)
self._connection = socket.create_connection((self.host, self.port),
timeout=timeout)
if timeout:
self._connection.setblocking(0)
@property
def serial_number(self):
return self._serial_number
def BulkWrite(self, data, timeout=None):
t = self.TimeoutSeconds(timeout)
_, writeable, _ = select.select([], [self._connection], [], t)
if writeable:
return self._connection.send(data)
msg = 'Sending data to {} timed out after {}s. No data was sent.'.format(
self.serial_number, t)
raise usb_exceptions.TcpTimeoutException(msg)
def BulkRead(self, numbytes, timeout=None):
t = self.TimeoutSeconds(timeout)
readable, _, _ = select.select([self._connection], [], [], t)
if readable:
return self._connection.recv(numbytes)
msg = 'Reading from {} timed out (Timeout {}s)'.format(
self._serial_number, t)
raise usb_exceptions.TcpTimeoutException(msg)
def Timeout(self, timeout_ms):
return float(timeout_ms) if timeout_ms is not None else self._timeout_ms
def TimeoutSeconds(self, timeout_ms):
timeout = self.Timeout(timeout_ms)
return timeout / 1000.0 if timeout is not None else timeout
def Close(self):
return self._connection.close()