forked from google/python-adb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.py
289 lines (249 loc) · 9.45 KB
/
common.py
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
import logging
import platform
import re
import select
import socket
import threading
import weakref
import libusb1
import usb1
try:
from libusb1 import LIBUSB_ERROR_NOT_FOUND, LIBUSB_ERROR_TIMEOUT
except ImportError:
LIBUSB_ERROR_NOT_FOUND = "LIBUSB_ERROR_NOT_FOUND"
LIBUSB_ERROR_TIMEOUT = "LIBUSB_ERROR_TIMEOUT"
from adb import usb_exceptions
DEFAULT_TIMEOUT_MS = 10000
SYSFS_PORT_SPLIT_RE = re.compile("[,/:.-]")
_LOG = logging.getLogger("android_usb")
def GetInterface(setting):
return (setting.getClass(), setting.getSubClass(), setting.getProtocol())
def InterfaceMatcher(clazz, subclass, protocol):
interface = (clazz, subclass, protocol)
def Matcher(device):
for setting in device.iterSettings():
if GetInterface(setting) == interface:
return setting
return None
return Matcher
class UsbHandle(object):
_HANDLE_CACHE = weakref.WeakValueDictionary()
_HANDLE_CACHE_LOCK = threading.Lock()
def __init__(self, device, setting, usb_info=None, timeout_ms=None):
self._setting = setting
self._device = device
self._handle = None
self._interface_number = None
self._read_endpoint = None
self._write_endpoint = 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):
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 == 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
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 == 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:
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):
raise NotImplementedError
@classmethod
def PortPathMatcher(cls, port_path):
if isinstance(port_path, str):
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):
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):
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):
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
):
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 TcpHandle(object):
def __init__(self, serial, timeout_ms=None):
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()