- Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathcore.py
239 lines (204 loc) · 7.84 KB
/
core.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
# MicroPython USB host library, compatible with PyUSB.
# MIT license; Copyright (c) 2021-2024 Damien P. George
importsys
importffi
importuctypes
ifsys.maxsize>>32:
UINTPTR_SIZE=8
UINTPTR=uctypes.UINT64
else:
UINTPTR_SIZE=4
UINTPTR=uctypes.UINT32
def_align_word(x):
return (x+UINTPTR_SIZE-1) &~(UINTPTR_SIZE-1)
ptr_descriptor= (0|uctypes.ARRAY, 1|UINTPTR)
libusb_device_descriptor= {
"bLength": 0|uctypes.UINT8,
"bDescriptorType": 1|uctypes.UINT8,
"bcdUSB": 2|uctypes.UINT16,
"bDeviceClass": 4|uctypes.UINT8,
"bDeviceSubClass": 5|uctypes.UINT8,
"bDeviceProtocol": 6|uctypes.UINT8,
"bMaxPacketSize0": 7|uctypes.UINT8,
"idVendor": 8|uctypes.UINT16,
"idProduct": 10|uctypes.UINT16,
"bcdDevice": 12|uctypes.UINT16,
"iManufacturer": 14|uctypes.UINT8,
"iProduct": 15|uctypes.UINT8,
"iSerialNumber": 16|uctypes.UINT8,
"bNumConfigurations": 17|uctypes.UINT8,
}
libusb_config_descriptor= {
"bLength": 0|uctypes.UINT8,
"bDescriptorType": 1|uctypes.UINT8,
"wTotalLength": 2|uctypes.UINT16,
"bNumInterfaces": 4|uctypes.UINT8,
"bConfigurationValue": 5|uctypes.UINT8,
"iConfiguration": 6|uctypes.UINT8,
"bmAttributes": 7|uctypes.UINT8,
"MaxPower": 8|uctypes.UINT8,
"interface": _align_word(9) |UINTPTR, # array of libusb_interface
"extra": (_align_word(9) +UINTPTR_SIZE) |UINTPTR,
"extra_length": (_align_word(9) +2*UINTPTR_SIZE) |uctypes.INT,
}
libusb_interface= {
"altsetting": 0|UINTPTR, # array of libusb_interface_descriptor
"num_altsetting": UINTPTR_SIZE|uctypes.INT,
}
libusb_interface_descriptor= {
"bLength": 0|uctypes.UINT8,
"bDescriptorType": 1|uctypes.UINT8,
"bInterfaceNumber": 2|uctypes.UINT8,
"bAlternateSetting": 3|uctypes.UINT8,
"bNumEndpoints": 4|uctypes.UINT8,
"bInterfaceClass": 5|uctypes.UINT8,
"bInterfaceSubClass": 6|uctypes.UINT8,
"bInterfaceProtocol": 7|uctypes.UINT8,
"iInterface": 8|uctypes.UINT8,
"endpoint": _align_word(9) |UINTPTR,
"extra": (_align_word(9) +UINTPTR_SIZE) |UINTPTR,
"extra_length": (_align_word(9) +2*UINTPTR_SIZE) |uctypes.INT,
}
libusb=ffi.open("libusb-1.0.so")
libusb_init=libusb.func("i", "libusb_init", "p")
libusb_exit=libusb.func("v", "libusb_exit", "p")
libusb_get_device_list=libusb.func("i", "libusb_get_device_list", "pp") # return is ssize_t
libusb_free_device_list=libusb.func("v", "libusb_free_device_list", "pi")
libusb_get_device_descriptor=libusb.func("i", "libusb_get_device_descriptor", "pp")
libusb_get_config_descriptor=libusb.func("i", "libusb_get_config_descriptor", "pBp")
libusb_free_config_descriptor=libusb.func("v", "libusb_free_config_descriptor", "p")
libusb_open=libusb.func("i", "libusb_open", "pp")
libusb_set_configuration=libusb.func("i", "libusb_set_configuration", "pi")
libusb_claim_interface=libusb.func("i", "libusb_claim_interface", "pi")
libusb_control_transfer=libusb.func("i", "libusb_control_transfer", "pBBHHpHI")
def_new(sdesc):
buf=bytearray(uctypes.sizeof(sdesc))
s=uctypes.struct(uctypes.addressof(buf), sdesc)
returns
classInterface:
def__init__(self, descr):
# Public attributes.
self.bInterfaceClass=descr.bInterfaceClass
self.bInterfaceSubClass=descr.bInterfaceSubClass
self.iInterface=descr.iInterface
self.extra_descriptors=uctypes.bytes_at(descr.extra, descr.extra_length)
classConfiguration:
def__init__(self, dev, cfg_idx):
cfgs=_new(ptr_descriptor)
iflibusb_get_config_descriptor(dev._dev, cfg_idx, cfgs) !=0:
libusb_exit(0)
raiseException
descr=uctypes.struct(cfgs[0], libusb_config_descriptor)
# Extract all needed info because descr is going to be free'd at the end.
self._itfs= []
itf_array=uctypes.struct(
descr.interface, (0|uctypes.ARRAY, descr.bNumInterfaces, libusb_interface)
)
foriinrange(descr.bNumInterfaces):
itf=itf_array[i]
alt_array=uctypes.struct(
itf.altsetting,
(0|uctypes.ARRAY, itf.num_altsetting, libusb_interface_descriptor),
)
forjinrange(itf.num_altsetting):
alt=alt_array[j]
self._itfs.append(Interface(alt))
# Public attributes.
self.bNumInterfaces=descr.bNumInterfaces
self.bConfigurationValue=descr.bConfigurationValue
self.bmAttributes=descr.bmAttributes
self.bMaxPower=descr.MaxPower
self.extra_descriptors=uctypes.bytes_at(descr.extra, descr.extra_length)
# Free descr memory in the driver.
libusb_free_config_descriptor(cfgs[0])
def__iter__(self):
returniter(self._itfs)
classDevice:
_TIMEOUT_DEFAULT=1000
def__init__(self, dev, descr):
self._dev=dev
self._num_cfg=descr.bNumConfigurations
self._handle=None
self._claim_itf=set()
# Public attributes.
self.idVendor=descr.idVendor
self.idProduct=descr.idProduct
def__iter__(self):
foriinrange(self._num_cfg):
yieldConfiguration(self, i)
def__getitem__(self, i):
returnConfiguration(self, i)
def_open(self):
ifself._handleisNone:
# Open the USB device.
handle=_new(ptr_descriptor)
iflibusb_open(self._dev, handle) !=0:
libusb_exit(0)
raiseException
self._handle=handle[0]
def_claim_interface(self, i):
iflibusb_claim_interface(self._handle, i) !=0:
libusb_exit(0)
raiseException
defset_configuration(self):
# Select default configuration.
self._open()
cfg=Configuration(self, 0).bConfigurationValue
ret=libusb_set_configuration(self._handle, cfg)
ifret!=0:
libusb_exit(0)
raiseException
defctrl_transfer(
self, bmRequestType, bRequest, wValue=0, wIndex=0, data_or_wLength=None, timeout=None
):
ifdata_or_wLengthisNone:
l=0
data=bytes()
elifisinstance(data_or_wLength, int):
l=data_or_wLength
data=bytearray(l)
else:
l=len(data_or_wLength)
data=data_or_wLength
self._open()
ifwIndex&0xFFnotinself._claim_itf:
self._claim_interface(wIndex&0xFF)
self._claim_itf.add(wIndex&0xFF)
iftimeoutisNone:
timeout=self._TIMEOUT_DEFAULT
ret=libusb_control_transfer(
self._handle, bmRequestType, bRequest, wValue, wIndex, data, l, timeout*1000
)
ifret<0:
libusb_exit(0)
raiseException
ifisinstance(data_or_wLength, int):
returndata
else:
returnret
deffind(*, find_all=False, custom_match=None, idVendor=None, idProduct=None):
iflibusb_init(0) <0:
raiseException
devs=_new(ptr_descriptor)
count=libusb_get_device_list(0, devs)
ifcount<0:
libusb_exit(0)
raiseException
dev_array=uctypes.struct(devs[0], (0|uctypes.ARRAY, count|UINTPTR))
descr=_new(libusb_device_descriptor)
devices=None
foriinrange(count):
libusb_get_device_descriptor(dev_array[i], descr)
ifidVendoranddescr.idVendor!=idVendor:
continue
ifidProductanddescr.idProduct!=idProduct:
continue
device=Device(dev_array[i], descr)
ifcustom_matchandnotcustom_match(device):
continue
ifnotfind_all:
returndevice
ifnotdevices:
devices= []
devices.append(device)
returndevices