| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
""" |
| 20 |
Python ctypes bindings for libusb-1.0. |
| 21 |
|
| 22 |
You should not need to import this if you use usb1 module. |
| 23 |
|
| 24 |
Declares all constants, data structures and exported symbols. |
| 25 |
Some are only available after calling loadLibrary. |
| 26 |
""" |
| 27 |
from ctypes import ( |
| 28 |
Structure, LittleEndianStructure, |
| 29 |
Union, |
| 30 |
CFUNCTYPE, POINTER, addressof, sizeof, cast, |
| 31 |
c_short, c_int, c_uint, c_long, c_longlong, |
| 32 |
c_uint8, c_uint16, c_uint32, |
| 33 |
c_void_p, c_char_p, py_object, pointer, c_char, |
| 34 |
c_ssize_t, CDLL |
| 35 |
) |
| 36 |
import ctypes.util |
| 37 |
import errno |
| 38 |
import os.path |
| 39 |
import platform |
| 40 |
import sys |
| 41 |
from threading import Lock |
| 42 |
|
| 43 |
class Enum: |
| 44 |
def __init__(self, member_dict, scope_dict=None): |
| 45 |
if scope_dict is None: |
| 46 |
|
| 47 |
|
| 48 |
scope_dict = sys._getframe(1).f_locals |
| 49 |
|
| 50 |
forward_dict = {} |
| 51 |
reverse_dict = {} |
| 52 |
next_value = 0 |
| 53 |
for name, value in member_dict.items(): |
| 54 |
if value is None: |
| 55 |
value = next_value |
| 56 |
next_value += 1 |
| 57 |
forward_dict[name] = value |
| 58 |
if value in reverse_dict: |
| 59 |
raise ValueError( |
| 60 |
'Multiple names for value ' |
| 61 |
f'{value!r}: {reverse_dict[value]!r}, {name!r}' |
| 62 |
) |
| 63 |
reverse_dict[value] = name |
| 64 |
scope_dict[name] = value |
| 65 |
self.forward_dict = forward_dict |
| 66 |
self.reverse_dict = reverse_dict |
| 67 |
|
| 68 |
def __call__(self, value): |
| 69 |
return self.reverse_dict[value] |
| 70 |
|
| 71 |
def get(self, value, default=None): |
| 72 |
return self.reverse_dict.get(value, default) |
| 73 |
|
| 74 |
def buffer_at(address, length): |
| 75 |
""" |
| 76 |
Simular to ctypes.string_at, but zero-copy and requires an integer address. |
| 77 |
""" |
| 78 |
return bytearray((c_char * length).from_address(address)) |
| 79 |
|
| 80 |
_desc_type_dict = { |
| 81 |
'b': c_uint8, |
| 82 |
'bcd': c_uint16, |
| 83 |
'bm': c_uint8, |
| 84 |
'dw': c_uint32, |
| 85 |
'i': c_uint8, |
| 86 |
'id': c_uint16, |
| 87 |
'w': c_uint16, |
| 88 |
} |
| 89 |
|
| 90 |
def newStruct(field_name_list): |
| 91 |
""" |
| 92 |
Create a ctype structure class based on USB standard field naming |
| 93 |
(type-prefixed). |
| 94 |
""" |
| 95 |
field_list = [] |
| 96 |
append = field_list.append |
| 97 |
for field in field_name_list: |
| 98 |
type_prefix = '' |
| 99 |
for char in field: |
| 100 |
if not char.islower(): |
| 101 |
break |
| 102 |
type_prefix += char |
| 103 |
append((field, _desc_type_dict[type_prefix])) |
| 104 |
result = type('some_descriptor', (LittleEndianStructure, ), {}) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
result._pack_ = 1 |
| 111 |
result._fields_ = field_list |
| 112 |
|
| 113 |
return result |
| 114 |
|
| 115 |
def newDescriptor(field_name_list): |
| 116 |
""" |
| 117 |
Create a USB descriptor ctype structure, ie starting with bLength and |
| 118 |
bDescriptorType fields. |
| 119 |
|
| 120 |
See newStruct(). |
| 121 |
""" |
| 122 |
return newStruct(['bLength', 'bDescriptorType'] + list(field_name_list)) |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
def libusb_strerror(_unused_errcode): |
| 127 |
return None |
| 128 |
|
| 129 |
class USBError(Exception): |
| 130 |
value = None |
| 131 |
|
| 132 |
def __init__(self, value=None): |
| 133 |
Exception.__init__(self) |
| 134 |
if value is not None: |
| 135 |
self.value = value |
| 136 |
|
| 137 |
def __str__(self): |
| 138 |
return f'{libusb_error.get(self.value, "Unknown error")} [{self.value}]' |
| 139 |
|
| 140 |
def getMessage(self): |
| 141 |
""" |
| 142 |
Get user-friendly message representing the current error, and based on |
| 143 |
the current locale. |
| 144 |
|
| 145 |
Returns str, or None. |
| 146 |
If libusb has not been loaded yet, returns None. |
| 147 |
""" |
| 148 |
|
| 149 |
message = libusb_strerror(self.value) |
| 150 |
|
| 151 |
if message is not None: |
| 152 |
message = message.decode('utf-8') |
| 153 |
return message |
| 154 |
|
| 155 |
c_uchar = c_uint8 |
| 156 |
c_int_p = POINTER(c_int) |
| 157 |
intptr_t = { |
| 158 |
sizeof(x): x |
| 159 |
for x in (c_int, c_long, c_longlong) |
| 160 |
}[sizeof(c_void_p)] |
| 161 |
|
| 162 |
LITTLE_ENDIAN = sys.byteorder == 'little' |
| 163 |
|
| 164 |
class timeval(Structure): |
| 165 |
_fields_ = [('tv_sec', c_long), |
| 166 |
('tv_usec', c_long)] |
| 167 |
timeval_p = POINTER(timeval) |
| 168 |
|
| 169 |
if platform.system() == 'Windows': |
| 170 |
LIBUSB_CALL_FUNCTYPE = ctypes.WINFUNCTYPE |
| 171 |
else: |
| 172 |
LIBUSB_CALL_FUNCTYPE = CFUNCTYPE |
| 173 |
|
| 174 |
def __getLibrary(): |
| 175 |
my_dir = os.path.dirname(__file__) |
| 176 |
system = platform.system() |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
if system == 'Windows': |
| 182 |
dll_loader = ctypes.WinDLL |
| 183 |
libusb_list = [ |
| 184 |
os.path.join(my_dir, 'libusb-1.0.dll'), |
| 185 |
'libusb-1.0.dll', |
| 186 |
] |
| 187 |
find_library = None |
| 188 |
else: |
| 189 |
dll_loader = CDLL |
| 190 |
if system == 'Darwin': |
| 191 |
libusb_list = [ |
| 192 |
os.path.join(my_dir, 'libusb-1.0.dylib'), |
| 193 |
'libusb-1.0.dylib', |
| 194 |
|
| 195 |
'/opt/local/lib/libusb-1.0.dylib', |
| 196 |
|
| 197 |
'/sw/lib/libusb-1.0.dylib', |
| 198 |
|
| 199 |
'/opt/homebrew/opt/libusb/lib/libusb-1.0.dylib', |
| 200 |
] |
| 201 |
find_library = None |
| 202 |
else: |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
libusb_list = [ |
| 207 |
os.path.join(my_dir, 'libusb-1.0.so.0'), |
| 208 |
os.path.join(my_dir, 'libusb-1.0.so'), |
| 209 |
'libusb-1.0.so.0', |
| 210 |
'libusb-1.0.so', |
| 211 |
] |
| 212 |
find_library = ( |
| 213 |
|
| 214 |
|
| 215 |
'usb' |
| 216 |
if 'FreeBSD' in system else |
| 217 |
'usb-1.0' |
| 218 |
) |
| 219 |
for filename in libusb_list: |
| 220 |
try: |
| 221 |
return dll_loader(filename, use_errno=True, use_last_error=True) |
| 222 |
except OSError: |
| 223 |
pass |
| 224 |
if find_library is not None: |
| 225 |
filename = ctypes.util.find_library(find_library) |
| 226 |
if filename is not None: |
| 227 |
return dll_loader(filename, use_errno=True, use_last_error=True) |
| 228 |
raise OSError( |
| 229 |
errno.ENOENT, |
| 230 |
'cannot find a suitable libusb-1.0', |
| 231 |
libusb_list, |
| 232 |
) |
| 233 |
|
| 234 |
__load_lock = Lock() |
| 235 |
__loaded = False |
| 236 |
|
| 237 |
def loadLibrary(libusb=None): |
| 238 |
""" |
| 239 |
Load C library. |
| 240 |
|
| 241 |
libusb (ctypes.CDLL, ctypes.WinDLL, None) |
| 242 |
If None, the C library will be searched for in several platform- |
| 243 |
dependent paths. |
| 244 |
Otherwise, this is the C library to use as libusb. This is to allow |
| 245 |
customising library lookup logic. |
| 246 |
|
| 247 |
Only the first call actually loads the library. |
| 248 |
|
| 249 |
Returns True if any of the following is true: |
| 250 |
- the library was loaded by this call |
| 251 |
- libusb argument is None |
| 252 |
- libusb argument is the same object that was initially loaded. |
| 253 |
""" |
| 254 |
global __loaded |
| 255 |
if not __loaded: |
| 256 |
with __load_lock: |
| 257 |
if not __loaded: |
| 258 |
__loadLibrary(libusb) |
| 259 |
__loaded = True |
| 260 |
return True |
| 261 |
return libusb is None or globals()['libusb'] is libusb |
| 262 |
|
| 263 |
def __loadLibrary(libusb): |
| 264 |
|
| 265 |
|
| 266 |
if libusb is None: |
| 267 |
libusb = __getLibrary() |
| 268 |
|
| 269 |
_libusb_transfer_fields = [ |
| 270 |
('dev_handle', libusb_device_handle_p), |
| 271 |
('flags', c_uint8), |
| 272 |
('endpoint', c_uchar), |
| 273 |
('type', c_uchar), |
| 274 |
('timeout', c_uint), |
| 275 |
('status', c_int), |
| 276 |
('length', c_int), |
| 277 |
('actual_length', c_int), |
| 278 |
('callback', libusb_transfer_cb_fn_p), |
| 279 |
('user_data', c_void_p), |
| 280 |
('buffer', c_void_p), |
| 281 |
('num_iso_packets', c_int), |
| 282 |
('iso_packet_desc', libusb_iso_packet_descriptor) |
| 283 |
] |
| 284 |
if 'FreeBSD' in platform.system() and getattr( |
| 285 |
libusb, 'libusb_get_string_descriptor', None |
| 286 |
) is None: |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
assert _libusb_transfer_fields[2][0] == 'endpoint' |
| 291 |
_libusb_transfer_fields[2] = ('endpoint', c_uint32) |
| 292 |
assert _libusb_transfer_fields[11][0] == 'num_iso_packets' |
| 293 |
_libusb_transfer_fields.insert(11, ('os_priv', c_void_p)) |
| 294 |
|
| 295 |
|
| 296 |
libusb_transfer._fields_ = _libusb_transfer_fields |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
try: |
| 302 |
libusb_init_context = libusb.libusb_init_context |
| 303 |
except AttributeError: |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
libusb_init = libusb.libusb_init |
| 308 |
libusb_init.argtypes = [libusb_context_p_p] |
| 309 |
def libusb_init_context(ctx, _unused_options, num_options): |
| 310 |
if num_options: |
| 311 |
raise ValueError( |
| 312 |
'libusb_init_context is not available in the current ' |
| 313 |
'libusb version, initialisation options are not available', |
| 314 |
) |
| 315 |
return libusb_init(ctx) |
| 316 |
else: |
| 317 |
libusb_init_context.argtypes = [ |
| 318 |
libusb_context_p_p, |
| 319 |
POINTER(libusb_init_option), |
| 320 |
c_int, |
| 321 |
] |
| 322 |
|
| 323 |
|
| 324 |
libusb_exit = libusb.libusb_exit |
| 325 |
libusb_exit.argtypes = [libusb_context_p] |
| 326 |
libusb_exit.restype = None |
| 327 |
|
| 328 |
libusb_set_debug = libusb.libusb_set_debug |
| 329 |
libusb_set_debug.argtypes = [libusb_context_p, c_int] |
| 330 |
libusb_set_debug.restype = None |
| 331 |
|
| 332 |
try: |
| 333 |
libusb_set_log_cb = libusb.libusb_set_log_cb |
| 334 |
except AttributeError: |
| 335 |
def libusb_set_log_cb(ctx, cb, mode): |
| 336 |
pass |
| 337 |
else: |
| 338 |
libusb_set_log_cb.argtypes = [libusb_context_p, libusb_log_cb_p, c_int] |
| 339 |
libusb_set_log_cb.restype = None |
| 340 |
|
| 341 |
try: |
| 342 |
libusb_get_version = libusb.libusb_get_version |
| 343 |
except AttributeError: |
| 344 |
_dummy_version = libusb_version(0, 0, 0, 0, b'', b'') |
| 345 |
_dummy_version_p = pointer(_dummy_version) |
| 346 |
def libusb_get_version(): |
| 347 |
return _dummy_version_p |
| 348 |
else: |
| 349 |
libusb_get_version.argtypes = [] |
| 350 |
libusb_get_version.restype = POINTER(libusb_version) |
| 351 |
|
| 352 |
try: |
| 353 |
libusb_has_capability = libusb.libusb_has_capability |
| 354 |
except AttributeError: |
| 355 |
def libusb_has_capability(_): |
| 356 |
return 0 |
| 357 |
else: |
| 358 |
libusb_has_capability.argtypes = [c_uint32] |
| 359 |
libusb_has_capability.restype = c_int |
| 360 |
try: |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
libusb_error_name = libusb.libusb_error_name |
| 365 |
except AttributeError: |
| 366 |
|
| 367 |
def libusb_error_name(errcode): |
| 368 |
return None |
| 369 |
|
| 370 |
else: |
| 371 |
libusb_error_name.argtypes = [c_int] |
| 372 |
libusb_error_name.restype = c_char_p |
| 373 |
|
| 374 |
try: |
| 375 |
libusb_setlocale = libusb.libusb_setlocale |
| 376 |
except AttributeError: |
| 377 |
def libusb_setlocale(_unused_locale): |
| 378 |
pass |
| 379 |
else: |
| 380 |
libusb_setlocale.argtypes = [c_char_p] |
| 381 |
libusb_setlocale.restype = int |
| 382 |
|
| 383 |
try: |
| 384 |
|
| 385 |
libusb_strerror = libusb.libusb_strerror |
| 386 |
|
| 387 |
except AttributeError: |
| 388 |
pass |
| 389 |
else: |
| 390 |
libusb_strerror.argtypes = [c_int] |
| 391 |
libusb_strerror.restype = c_char_p |
| 392 |
|
| 393 |
|
| 394 |
libusb_get_device_list = libusb.libusb_get_device_list |
| 395 |
libusb_get_device_list.argtypes = [libusb_context_p, libusb_device_p_p_p] |
| 396 |
libusb_get_device_list.restype = c_ssize_t |
| 397 |
|
| 398 |
libusb_free_device_list = libusb.libusb_free_device_list |
| 399 |
libusb_free_device_list.argtypes = [libusb_device_p_p, c_int] |
| 400 |
libusb_free_device_list.restype = None |
| 401 |
|
| 402 |
libusb_ref_device = libusb.libusb_ref_device |
| 403 |
libusb_ref_device.argtypes = [libusb_device_p] |
| 404 |
libusb_ref_device.restype = libusb_device_p |
| 405 |
|
| 406 |
libusb_unref_device = libusb.libusb_unref_device |
| 407 |
libusb_unref_device.argtypes = [libusb_device_p] |
| 408 |
libusb_unref_device.restype = None |
| 409 |
|
| 410 |
|
| 411 |
libusb_get_configuration = libusb.libusb_get_configuration |
| 412 |
libusb_get_configuration.argtypes = [libusb_device_handle_p, c_int_p] |
| 413 |
|
| 414 |
|
| 415 |
libusb_get_device_descriptor = libusb.libusb_get_device_descriptor |
| 416 |
libusb_get_device_descriptor.argtypes = [ |
| 417 |
libusb_device_p, libusb_device_descriptor_p] |
| 418 |
|
| 419 |
|
| 420 |
libusb_get_active_config_descriptor = libusb.libusb_get_active_config_descriptor |
| 421 |
libusb_get_active_config_descriptor.argtypes = [ |
| 422 |
libusb_device_p, libusb_config_descriptor_p_p] |
| 423 |
|
| 424 |
|
| 425 |
libusb_get_config_descriptor = libusb.libusb_get_config_descriptor |
| 426 |
libusb_get_config_descriptor.argtypes = [ |
| 427 |
libusb_device_p, c_uint8, libusb_config_descriptor_p_p] |
| 428 |
|
| 429 |
|
| 430 |
libusb_get_config_descriptor_by_value = \ |
| 431 |
libusb.libusb_get_config_descriptor_by_value |
| 432 |
libusb_get_config_descriptor_by_value.argtypes = [ |
| 433 |
libusb_device_p, c_uint8, libusb_config_descriptor_p_p] |
| 434 |
|
| 435 |
libusb_free_config_descriptor = libusb.libusb_free_config_descriptor |
| 436 |
libusb_free_config_descriptor.argtypes = [libusb_config_descriptor_p] |
| 437 |
libusb_free_config_descriptor.restype = None |
| 438 |
|
| 439 |
libusb_get_bus_number = libusb.libusb_get_bus_number |
| 440 |
libusb_get_bus_number.argtypes = [libusb_device_p] |
| 441 |
libusb_get_bus_number.restype = c_uint8 |
| 442 |
try: |
| 443 |
|
| 444 |
libusb_get_port_number = libusb.libusb_get_port_number |
| 445 |
except AttributeError: |
| 446 |
pass |
| 447 |
else: |
| 448 |
libusb_get_port_number.argtypes = [libusb_device_p] |
| 449 |
libusb_get_port_number.restype = c_uint8 |
| 450 |
try: |
| 451 |
|
| 452 |
|
| 453 |
libusb_get_port_numbers = libusb.libusb_get_port_numbers |
| 454 |
except AttributeError: |
| 455 |
pass |
| 456 |
else: |
| 457 |
libusb_get_port_numbers.argtypes = [ |
| 458 |
libusb_device_p, POINTER(c_uint8), c_int] |
| 459 |
libusb_get_port_numbers.restype = c_int |
| 460 |
|
| 461 |
try: |
| 462 |
|
| 463 |
libusb_get_parent = libusb.libusb_get_parent |
| 464 |
except AttributeError: |
| 465 |
pass |
| 466 |
else: |
| 467 |
libusb_get_parent.argtypes = [libusb_device_p] |
| 468 |
libusb_get_parent.restype = libusb_device_p |
| 469 |
|
| 470 |
libusb_get_device_address = libusb.libusb_get_device_address |
| 471 |
libusb_get_device_address.argtypes = [libusb_device_p] |
| 472 |
libusb_get_device_address.restype = c_uint8 |
| 473 |
try: |
| 474 |
|
| 475 |
libusb_get_device_speed = libusb.libusb_get_device_speed |
| 476 |
except AttributeError: |
| 477 |
|
| 478 |
def libusb_get_device_speed(_): |
| 479 |
|
| 480 |
return LIBUSB_SPEED_UNKNOWN |
| 481 |
|
| 482 |
else: |
| 483 |
libusb_get_device_speed.argtypes = [libusb_device_p] |
| 484 |
|
| 485 |
libusb_get_max_packet_size = libusb.libusb_get_max_packet_size |
| 486 |
libusb_get_max_packet_size.argtypes = [libusb_device_p, c_uchar] |
| 487 |
|
| 488 |
try: |
| 489 |
libusb_get_max_iso_packet_size = libusb.libusb_get_max_iso_packet_size |
| 490 |
except AttributeError: |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
def libusb_get_max_iso_packet_size(_, __): |
| 496 |
raise NotImplementedError |
| 497 |
|
| 498 |
else: |
| 499 |
libusb_get_max_iso_packet_size.argtypes = [libusb_device_p, c_uchar] |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
try: |
| 504 |
libusb_wrap_sys_device = libusb.libusb_wrap_sys_device |
| 505 |
except AttributeError: |
| 506 |
|
| 507 |
def libusb_wrap_sys_device(_, __, ___): |
| 508 |
raise NotImplementedError |
| 509 |
|
| 510 |
else: |
| 511 |
libusb_wrap_sys_device.restype = c_int |
| 512 |
libusb_wrap_sys_device.argtypes = [ |
| 513 |
libusb_context_p, |
| 514 |
intptr_t, |
| 515 |
libusb_device_handle_p_p, |
| 516 |
] |
| 517 |
|
| 518 |
|
| 519 |
libusb_open = libusb.libusb_open |
| 520 |
libusb_open.argtypes = [libusb_device_p, libusb_device_handle_p_p] |
| 521 |
|
| 522 |
libusb_close = libusb.libusb_close |
| 523 |
libusb_close.argtypes = [libusb_device_handle_p] |
| 524 |
libusb_close.restype = None |
| 525 |
|
| 526 |
libusb_get_device = libusb.libusb_get_device |
| 527 |
libusb_get_device.argtypes = [libusb_device_handle_p] |
| 528 |
libusb_get_device.restype = libusb_device_p |
| 529 |
|
| 530 |
|
| 531 |
libusb_set_configuration = libusb.libusb_set_configuration |
| 532 |
libusb_set_configuration.argtypes = [libusb_device_handle_p, c_int] |
| 533 |
|
| 534 |
libusb_claim_interface = libusb.libusb_claim_interface |
| 535 |
libusb_claim_interface.argtypes = [libusb_device_handle_p, c_int] |
| 536 |
|
| 537 |
libusb_release_interface = libusb.libusb_release_interface |
| 538 |
libusb_release_interface.argtypes = [libusb_device_handle_p, c_int] |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
libusb_open_device_with_vid_pid = libusb.libusb_open_device_with_vid_pid |
| 543 |
libusb_open_device_with_vid_pid.argtypes = [ |
| 544 |
libusb_context_p, c_uint16, c_uint16] |
| 545 |
libusb_open_device_with_vid_pid.restype = libusb_device_handle_p |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
libusb_set_interface_alt_setting = libusb.libusb_set_interface_alt_setting |
| 550 |
libusb_set_interface_alt_setting.argtypes = [ |
| 551 |
libusb_device_handle_p, c_int, c_int] |
| 552 |
|
| 553 |
libusb_clear_halt = libusb.libusb_clear_halt |
| 554 |
libusb_clear_halt.argtypes = [libusb_device_handle_p, c_uchar] |
| 555 |
|
| 556 |
libusb_reset_device = libusb.libusb_reset_device |
| 557 |
libusb_reset_device.argtypes = [libusb_device_handle_p] |
| 558 |
|
| 559 |
|
| 560 |
libusb_kernel_driver_active = libusb.libusb_kernel_driver_active |
| 561 |
libusb_kernel_driver_active.argtypes = [libusb_device_handle_p, c_int] |
| 562 |
|
| 563 |
libusb_detach_kernel_driver = libusb.libusb_detach_kernel_driver |
| 564 |
libusb_detach_kernel_driver.argtypes = [libusb_device_handle_p, c_int] |
| 565 |
|
| 566 |
libusb_attach_kernel_driver = libusb.libusb_attach_kernel_driver |
| 567 |
libusb_attach_kernel_driver.argtypes = [libusb_device_handle_p, c_int] |
| 568 |
try: |
| 569 |
|
| 570 |
|
| 571 |
libusb_set_auto_detach_kernel_driver = \ |
| 572 |
libusb.libusb_set_auto_detach_kernel_driver |
| 573 |
except AttributeError: |
| 574 |
pass |
| 575 |
else: |
| 576 |
libusb_set_auto_detach_kernel_driver.argtypes = [ |
| 577 |
libusb_device_handle_p, c_int] |
| 578 |
libusb_set_auto_detach_kernel_driver.restype = c_int |
| 579 |
|
| 580 |
|
| 581 |
libusb_alloc_transfer = libusb.libusb_alloc_transfer |
| 582 |
libusb_alloc_transfer.argtypes = [c_int] |
| 583 |
libusb_alloc_transfer.restype = libusb_transfer_p |
| 584 |
|
| 585 |
libusb_submit_transfer = libusb.libusb_submit_transfer |
| 586 |
libusb_submit_transfer.argtypes = [libusb_transfer_p] |
| 587 |
|
| 588 |
libusb_cancel_transfer = libusb.libusb_cancel_transfer |
| 589 |
libusb_cancel_transfer.argtypes = [libusb_transfer_p] |
| 590 |
|
| 591 |
libusb_free_transfer = libusb.libusb_free_transfer |
| 592 |
libusb_free_transfer.argtypes = [libusb_transfer_p] |
| 593 |
libusb_free_transfer.restype = None |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
libusb_control_transfer = libusb.libusb_control_transfer |
| 601 |
libusb_control_transfer.argtypes = [libusb_device_handle_p, c_uint8, c_uint8, |
| 602 |
c_uint16, c_uint16, c_void_p, c_uint16, |
| 603 |
c_uint] |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
libusb_bulk_transfer = libusb.libusb_bulk_transfer |
| 609 |
libusb_bulk_transfer.argtypes = [libusb_device_handle_p, c_uchar, c_void_p, |
| 610 |
c_int, c_int_p, c_uint] |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
libusb_interrupt_transfer = libusb.libusb_interrupt_transfer |
| 616 |
libusb_interrupt_transfer.argtypes = [libusb_device_handle_p, c_uchar, |
| 617 |
c_void_p, c_int, c_int_p, c_uint] |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
libusb_get_string_descriptor_ascii = libusb.libusb_get_string_descriptor_ascii |
| 622 |
libusb_get_string_descriptor_ascii.argtypes = [libusb_device_handle_p, |
| 623 |
c_uint8, c_void_p, c_int] |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
libusb_try_lock_events = libusb.libusb_try_lock_events |
| 629 |
libusb_try_lock_events.argtypes = [libusb_context_p] |
| 630 |
|
| 631 |
libusb_lock_events = libusb.libusb_lock_events |
| 632 |
libusb_lock_events.argtypes = [libusb_context_p] |
| 633 |
|
| 634 |
libusb_unlock_events = libusb.libusb_unlock_events |
| 635 |
libusb_unlock_events.argtypes = [libusb_context_p] |
| 636 |
libusb_unlock_events.restype = None |
| 637 |
|
| 638 |
libusb_event_handling_ok = libusb.libusb_event_handling_ok |
| 639 |
libusb_event_handling_ok.argtypes = [libusb_context_p] |
| 640 |
|
| 641 |
libusb_event_handler_active = libusb.libusb_event_handler_active |
| 642 |
libusb_event_handler_active.argtypes = [libusb_context_p] |
| 643 |
|
| 644 |
libusb_lock_event_waiters = libusb.libusb_lock_event_waiters |
| 645 |
libusb_lock_event_waiters.argtypes = [libusb_context_p] |
| 646 |
libusb_lock_event_waiters.restype = None |
| 647 |
|
| 648 |
libusb_unlock_event_waiters = libusb.libusb_unlock_event_waiters |
| 649 |
libusb_unlock_event_waiters.argtypes = [] |
| 650 |
libusb_unlock_event_waiters.restype = None |
| 651 |
|
| 652 |
libusb_wait_for_event = libusb.libusb_wait_for_event |
| 653 |
libusb_wait_for_event.argtypes = [libusb_context_p, timeval_p] |
| 654 |
|
| 655 |
|
| 656 |
libusb_handle_events_timeout = libusb.libusb_handle_events_timeout |
| 657 |
libusb_handle_events_timeout.argtypes = [libusb_context_p, timeval_p] |
| 658 |
|
| 659 |
|
| 660 |
try: |
| 661 |
libusb_handle_events_timeout_completed = libusb.\ |
| 662 |
libusb_handle_events_timeout_completed |
| 663 |
except AttributeError: |
| 664 |
|
| 665 |
pass |
| 666 |
else: |
| 667 |
libusb_handle_events_timeout_completed.argtypes = [ |
| 668 |
libusb_context_p, timeval_p, c_int_p] |
| 669 |
|
| 670 |
libusb_handle_events = libusb.libusb_handle_events |
| 671 |
libusb_handle_events.argtypes = [libusb_context_p] |
| 672 |
|
| 673 |
try: |
| 674 |
libusb_handle_events_completed = libusb.libusb_handle_events_completed |
| 675 |
except AttributeError: |
| 676 |
|
| 677 |
pass |
| 678 |
else: |
| 679 |
libusb_handle_events_completed.argtypes = [libusb_context_p, c_int_p] |
| 680 |
|
| 681 |
libusb_handle_events_locked = libusb.libusb_handle_events_locked |
| 682 |
libusb_handle_events_locked.argtypes = [libusb_context_p, timeval_p] |
| 683 |
|
| 684 |
try: |
| 685 |
libusb_interrupt_event_handler = libusb.libusb_interrupt_event_handler |
| 686 |
except AttributeError: |
| 687 |
def libusb_interrupt_event_handler(_): |
| 688 |
raise NotImplementedError |
| 689 |
else: |
| 690 |
libusb_interrupt_event_handler.argtypes = [libusb_context_p] |
| 691 |
|
| 692 |
libusb_get_next_timeout = libusb.libusb_get_next_timeout |
| 693 |
libusb_get_next_timeout.argtypes = [libusb_context_p, timeval_p] |
| 694 |
|
| 695 |
|
| 696 |
libusb_get_pollfds = libusb.libusb_get_pollfds |
| 697 |
libusb_get_pollfds.argtypes = [libusb_context_p] |
| 698 |
libusb_get_pollfds.restype = libusb_pollfd_p_p |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
libusb_set_pollfd_notifiers = libusb.libusb_set_pollfd_notifiers |
| 703 |
libusb_set_pollfd_notifiers.argtypes = [libusb_context_p, |
| 704 |
libusb_pollfd_added_cb_p, |
| 705 |
libusb_pollfd_removed_cb_p, py_object] |
| 706 |
libusb_set_pollfd_notifiers.restype = None |
| 707 |
try: |
| 708 |
|
| 709 |
libusb_free_pollfds = libusb.libusb_free_pollfds |
| 710 |
libusb_free_pollfds.argtypes = [libusb_pollfd_p_p] |
| 711 |
libusb_free_pollfds.restype = None |
| 712 |
except AttributeError: |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
libusb_free_pollfds = CDLL(None).free |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
try: |
| 724 |
libusb_hotplug_register_callback = libusb.libusb_hotplug_register_callback |
| 725 |
except AttributeError: |
| 726 |
pass |
| 727 |
else: |
| 728 |
libusb_hotplug_register_callback.argtypes = [ |
| 729 |
libusb_context_p, |
| 730 |
c_int, c_int, |
| 731 |
c_int, c_int, c_int, |
| 732 |
libusb_hotplug_callback_fn_p, c_void_p, |
| 733 |
POINTER(libusb_hotplug_callback_handle), |
| 734 |
] |
| 735 |
libusb_hotplug_register_callback.restype = c_int |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
try: |
| 740 |
libusb_hotplug_deregister_callback = \ |
| 741 |
libusb.libusb_hotplug_deregister_callback |
| 742 |
except AttributeError: |
| 743 |
pass |
| 744 |
else: |
| 745 |
libusb_hotplug_deregister_callback.argtypes = [ |
| 746 |
libusb_context_p, |
| 747 |
libusb_hotplug_callback_handle, |
| 748 |
] |
| 749 |
libusb_hotplug_deregister_callback.restype = None |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
globals().update(locals()) |
| 754 |
|
| 755 |
|
| 756 |
def bswap16(x): |
| 757 |
return ((x & 0xff) << 8) | (x >> 8) |
| 758 |
|
| 759 |
if LITTLE_ENDIAN: |
| 760 |
def libusb_cpu_to_le16(x): |
| 761 |
return x |
| 762 |
def libusb_le16_to_cpu(x): |
| 763 |
return x |
| 764 |
else: |
| 765 |
libusb_cpu_to_le16 = bswap16 |
| 766 |
libusb_le16_to_cpu = bswap16 |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
libusb_class_code = Enum({ |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
'LIBUSB_CLASS_PER_INTERFACE': 0, |
| 776 |
|
| 777 |
'LIBUSB_CLASS_AUDIO': 1, |
| 778 |
|
| 779 |
'LIBUSB_CLASS_COMM': 2, |
| 780 |
|
| 781 |
'LIBUSB_CLASS_HID': 3, |
| 782 |
|
| 783 |
'LIBUSB_CLASS_PHYSICAL': 5, |
| 784 |
|
| 785 |
'LIBUSB_CLASS_PRINTER': 7, |
| 786 |
|
| 787 |
'LIBUSB_CLASS_PTP': 6, |
| 788 |
|
| 789 |
'LIBUSB_CLASS_MASS_STORAGE': 8, |
| 790 |
|
| 791 |
'LIBUSB_CLASS_HUB': 9, |
| 792 |
|
| 793 |
'LIBUSB_CLASS_DATA': 10, |
| 794 |
|
| 795 |
'LIBUSB_CLASS_SMART_CARD': 0x0b, |
| 796 |
|
| 797 |
'LIBUSB_CLASS_CONTENT_SECURITY': 0x0d, |
| 798 |
|
| 799 |
'LIBUSB_CLASS_VIDEO': 0x0e, |
| 800 |
|
| 801 |
'LIBUSB_CLASS_PERSONAL_HEALTHCARE': 0x0f, |
| 802 |
|
| 803 |
'LIBUSB_CLASS_DIAGNOSTIC_DEVICE': 0xdc, |
| 804 |
|
| 805 |
'LIBUSB_CLASS_WIRELESS': 0xe0, |
| 806 |
|
| 807 |
'LIBUSB_CLASS_APPLICATION': 0xfe, |
| 808 |
|
| 809 |
'LIBUSB_CLASS_VENDOR_SPEC': 0xff |
| 810 |
}) |
| 811 |
|
| 812 |
LIBUSB_CLASS_IMAGE = LIBUSB_CLASS_PTP |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
libusb_descriptor_type = Enum({ |
| 817 |
|
| 818 |
'LIBUSB_DT_DEVICE': 0x01, |
| 819 |
|
| 820 |
'LIBUSB_DT_CONFIG': 0x02, |
| 821 |
|
| 822 |
'LIBUSB_DT_STRING': 0x03, |
| 823 |
|
| 824 |
'LIBUSB_DT_INTERFACE': 0x04, |
| 825 |
|
| 826 |
'LIBUSB_DT_ENDPOINT': 0x05, |
| 827 |
|
| 828 |
'LIBUSB_DT_HID': 0x21, |
| 829 |
|
| 830 |
'LIBUSB_DT_REPORT': 0x22, |
| 831 |
|
| 832 |
'LIBUSB_DT_PHYSICAL': 0x23, |
| 833 |
|
| 834 |
'LIBUSB_DT_HUB': 0x29, |
| 835 |
}) |
| 836 |
|
| 837 |
|
| 838 |
LIBUSB_DT_DEVICE_SIZE = 18 |
| 839 |
LIBUSB_DT_CONFIG_SIZE = 9 |
| 840 |
LIBUSB_DT_INTERFACE_SIZE = 9 |
| 841 |
LIBUSB_DT_ENDPOINT_SIZE = 7 |
| 842 |
LIBUSB_DT_ENDPOINT_AUDIO_SIZE = 9 |
| 843 |
LIBUSB_DT_HUB_NONVAR_SIZE = 7 |
| 844 |
LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE = 6 |
| 845 |
LIBUSB_DT_BOS_SIZE = 5 |
| 846 |
LIBUSB_DT_DEVICE_CAPABILITY_SIZE = 3 |
| 847 |
LIBUSB_DT_INTERFACE_ASSOCIATION_SIZE = 8 |
| 848 |
|
| 849 |
|
| 850 |
LIBUSB_BT_USB_2_0_EXTENSION_SIZE = 7 |
| 851 |
LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE = 10 |
| 852 |
LIBUSB_BT_SSPLUS_USB_DEVICE_CAPABILITY_SIZE = 12 |
| 853 |
LIBUSB_BT_CONTAINER_ID_SIZE = 20 |
| 854 |
LIBUSB_BT_PLATFORM_DESCRIPTOR_MIN_SIZE = 20 |
| 855 |
|
| 856 |
LIBUSB_DT_BOS_MAX_SIZE = ( |
| 857 |
LIBUSB_DT_BOS_SIZE + |
| 858 |
LIBUSB_BT_USB_2_0_EXTENSION_SIZE + |
| 859 |
LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE + |
| 860 |
LIBUSB_BT_CONTAINER_ID_SIZE |
| 861 |
) |
| 862 |
|
| 863 |
LIBUSB_ENDPOINT_ADDRESS_MASK = 0x0f |
| 864 |
LIBUSB_ENDPOINT_DIR_MASK = 0x80 |
| 865 |
|
| 866 |
USB_ENDPOINT_ADDRESS_MASK = LIBUSB_ENDPOINT_ADDRESS_MASK |
| 867 |
USB_ENDPOINT_DIR_MASK = LIBUSB_ENDPOINT_DIR_MASK |
| 868 |
|
| 869 |
|
| 870 |
libusb_endpoint_direction = Enum({ |
| 871 |
|
| 872 |
'LIBUSB_ENDPOINT_IN': 0x80, |
| 873 |
|
| 874 |
'LIBUSB_ENDPOINT_OUT': 0x00 |
| 875 |
}) |
| 876 |
|
| 877 |
LIBUSB_TRANSFER_TYPE_MASK = 0x03 |
| 878 |
|
| 879 |
|
| 880 |
libusb_transfer_type = Enum({ |
| 881 |
|
| 882 |
'LIBUSB_TRANSFER_TYPE_CONTROL': 0, |
| 883 |
|
| 884 |
'LIBUSB_TRANSFER_TYPE_ISOCHRONOUS': 1, |
| 885 |
|
| 886 |
'LIBUSB_TRANSFER_TYPE_BULK': 2, |
| 887 |
|
| 888 |
'LIBUSB_TRANSFER_TYPE_INTERRUPT': 3, |
| 889 |
}) |
| 890 |
|
| 891 |
|
| 892 |
libusb_standard_request = Enum({ |
| 893 |
|
| 894 |
'LIBUSB_REQUEST_GET_STATUS': 0x00, |
| 895 |
|
| 896 |
'LIBUSB_REQUEST_CLEAR_FEATURE': 0x01, |
| 897 |
|
| 898 |
|
| 899 |
'LIBUSB_REQUEST_SET_FEATURE': 0x03, |
| 900 |
|
| 901 |
|
| 902 |
'LIBUSB_REQUEST_SET_ADDRESS': 0x05, |
| 903 |
|
| 904 |
'LIBUSB_REQUEST_GET_DESCRIPTOR': 0x06, |
| 905 |
|
| 906 |
'LIBUSB_REQUEST_SET_DESCRIPTOR': 0x07, |
| 907 |
|
| 908 |
'LIBUSB_REQUEST_GET_CONFIGURATION': 0x08, |
| 909 |
|
| 910 |
'LIBUSB_REQUEST_SET_CONFIGURATION': 0x09, |
| 911 |
|
| 912 |
'LIBUSB_REQUEST_GET_INTERFACE': 0x0a, |
| 913 |
|
| 914 |
'LIBUSB_REQUEST_SET_INTERFACE': 0x0b, |
| 915 |
|
| 916 |
'LIBUSB_REQUEST_SYNCH_FRAME': 0x0c, |
| 917 |
}) |
| 918 |
|
| 919 |
|
| 920 |
libusb_request_type = Enum({ |
| 921 |
|
| 922 |
'LIBUSB_REQUEST_TYPE_STANDARD': (0x00 << 5), |
| 923 |
|
| 924 |
'LIBUSB_REQUEST_TYPE_CLASS': (0x01 << 5), |
| 925 |
|
| 926 |
'LIBUSB_REQUEST_TYPE_VENDOR': (0x02 << 5), |
| 927 |
|
| 928 |
'LIBUSB_REQUEST_TYPE_RESERVED': (0x03 << 5), |
| 929 |
}) |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
LIBUSB_TYPE_STANDARD = LIBUSB_REQUEST_TYPE_STANDARD |
| 934 |
LIBUSB_TYPE_CLASS = LIBUSB_REQUEST_TYPE_CLASS |
| 935 |
LIBUSB_TYPE_VENDOR = LIBUSB_REQUEST_TYPE_VENDOR |
| 936 |
LIBUSB_TYPE_RESERVED = LIBUSB_REQUEST_TYPE_RESERVED |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
libusb_request_recipient = Enum({ |
| 942 |
|
| 943 |
'LIBUSB_RECIPIENT_DEVICE': 0x00, |
| 944 |
|
| 945 |
'LIBUSB_RECIPIENT_INTERFACE': 0x01, |
| 946 |
|
| 947 |
'LIBUSB_RECIPIENT_ENDPOINT': 0x02, |
| 948 |
|
| 949 |
'LIBUSB_RECIPIENT_OTHER': 0x03, |
| 950 |
}) |
| 951 |
|
| 952 |
LIBUSB_ISO_SYNC_TYPE_MASK = 0x0c |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
libusb_iso_sync_type = Enum({ |
| 957 |
|
| 958 |
'LIBUSB_ISO_SYNC_TYPE_NONE': 0, |
| 959 |
|
| 960 |
'LIBUSB_ISO_SYNC_TYPE_ASYNC': 1, |
| 961 |
|
| 962 |
'LIBUSB_ISO_SYNC_TYPE_ADAPTIVE': 2, |
| 963 |
|
| 964 |
'LIBUSB_ISO_SYNC_TYPE_SYNC': 3, |
| 965 |
}) |
| 966 |
|
| 967 |
LIBUSB_ISO_USAGE_TYPE_MASK = 0x30 |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
libusb_iso_usage_type = Enum({ |
| 972 |
|
| 973 |
'LIBUSB_ISO_USAGE_TYPE_DATA': 0, |
| 974 |
|
| 975 |
'LIBUSB_ISO_USAGE_TYPE_FEEDBACK': 1, |
| 976 |
|
| 977 |
'LIBUSB_ISO_USAGE_TYPE_IMPLICIT': 2, |
| 978 |
}) |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
class libusb_device_descriptor(Structure): |
| 984 |
_fields_ = [ |
| 985 |
|
| 986 |
('bLength', c_uint8), |
| 987 |
|
| 988 |
|
| 989 |
('bDescriptorType', c_uint8), |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
('bcdUSB', c_uint16), |
| 994 |
|
| 995 |
('bDeviceClass', c_uint8), |
| 996 |
|
| 997 |
|
| 998 |
('bDeviceSubClass', c_uint8), |
| 999 |
|
| 1000 |
|
| 1001 |
('bDeviceProtocol', c_uint8), |
| 1002 |
|
| 1003 |
('bMaxPacketSize0', c_uint8), |
| 1004 |
|
| 1005 |
('idVendor', c_uint16), |
| 1006 |
|
| 1007 |
('idProduct', c_uint16), |
| 1008 |
|
| 1009 |
('bcdDevice', c_uint16), |
| 1010 |
|
| 1011 |
('iManufacturer', c_uint8), |
| 1012 |
|
| 1013 |
('iProduct', c_uint8), |
| 1014 |
|
| 1015 |
('iSerialNumber', c_uint8), |
| 1016 |
|
| 1017 |
('bNumConfigurations', c_uint8)] |
| 1018 |
libusb_device_descriptor_p = POINTER(libusb_device_descriptor) |
| 1019 |
|
| 1020 |
class libusb_endpoint_descriptor(Structure): |
| 1021 |
_fields_ = [ |
| 1022 |
('bLength', c_uint8), |
| 1023 |
('bDescriptorType', c_uint8), |
| 1024 |
('bEndpointAddress', c_uint8), |
| 1025 |
('bmAttributes', c_uint8), |
| 1026 |
('wMaxPacketSize', c_uint16), |
| 1027 |
('bInterval', c_uint8), |
| 1028 |
('bRefresh', c_uint8), |
| 1029 |
('bSynchAddress', c_uint8), |
| 1030 |
('extra', c_void_p), |
| 1031 |
('extra_length', c_int)] |
| 1032 |
libusb_endpoint_descriptor_p = POINTER(libusb_endpoint_descriptor) |
| 1033 |
|
| 1034 |
class libusb_interface_descriptor(Structure): |
| 1035 |
_fields_ = [ |
| 1036 |
('bLength', c_uint8), |
| 1037 |
('bDescriptorType', c_uint8), |
| 1038 |
('bInterfaceNumber', c_uint8), |
| 1039 |
('bAlternateSetting', c_uint8), |
| 1040 |
('bNumEndpoints', c_uint8), |
| 1041 |
('bInterfaceClass', c_uint8), |
| 1042 |
('bInterfaceSubClass', c_uint8), |
| 1043 |
('bInterfaceProtocol', c_uint8), |
| 1044 |
('iInterface', c_uint8), |
| 1045 |
('endpoint', libusb_endpoint_descriptor_p), |
| 1046 |
('extra', c_void_p), |
| 1047 |
('extra_length', c_int)] |
| 1048 |
libusb_interface_descriptor_p = POINTER(libusb_interface_descriptor) |
| 1049 |
|
| 1050 |
class libusb_interface(Structure): |
| 1051 |
_fields_ = [('altsetting', libusb_interface_descriptor_p), |
| 1052 |
('num_altsetting', c_int)] |
| 1053 |
libusb_interface_p = POINTER(libusb_interface) |
| 1054 |
|
| 1055 |
class libusb_config_descriptor(Structure): |
| 1056 |
_fields_ = [ |
| 1057 |
('bLength', c_uint8), |
| 1058 |
('bDescriptorType', c_uint8), |
| 1059 |
('wTotalLength', c_uint16), |
| 1060 |
('bNumInterfaces', c_uint8), |
| 1061 |
('bConfigurationValue', c_uint8), |
| 1062 |
('iConfiguration', c_uint8), |
| 1063 |
('bmAttributes', c_uint8), |
| 1064 |
('MaxPower', c_uint8), |
| 1065 |
('interface', libusb_interface_p), |
| 1066 |
('extra', c_void_p), |
| 1067 |
('extra_length', c_int)] |
| 1068 |
libusb_config_descriptor_p = POINTER(libusb_config_descriptor) |
| 1069 |
libusb_config_descriptor_p_p = POINTER(libusb_config_descriptor_p) |
| 1070 |
|
| 1071 |
class libusb_control_setup(Structure): |
| 1072 |
_pack_ = 1 |
| 1073 |
_fields_ = [ |
| 1074 |
('bmRequestType', c_uint8), |
| 1075 |
('bRequest', c_uint8), |
| 1076 |
('wValue', c_uint16), |
| 1077 |
('wIndex', c_uint16), |
| 1078 |
('wLength', c_uint16)] |
| 1079 |
libusb_control_setup_p = POINTER(libusb_control_setup) |
| 1080 |
|
| 1081 |
LIBUSB_CONTROL_SETUP_SIZE = sizeof(libusb_control_setup) |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
libusb_context_p = c_void_p |
| 1102 |
libusb_context_p_p = POINTER(libusb_context_p) |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
libusb_device_p = c_void_p |
| 1121 |
libusb_device_p_p = POINTER(libusb_device_p) |
| 1122 |
libusb_device_p_p_p = POINTER(libusb_device_p_p) |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
class libusb_device_handle(Structure): |
| 1131 |
pass |
| 1132 |
libusb_device_handle_p = POINTER(libusb_device_handle) |
| 1133 |
libusb_device_handle_p_p = POINTER(libusb_device_handle_p) |
| 1134 |
|
| 1135 |
class libusb_version(Structure): |
| 1136 |
_fields_ = [ |
| 1137 |
('major', c_uint16), |
| 1138 |
('minor', c_uint16), |
| 1139 |
('micro', c_uint16), |
| 1140 |
('nano', c_uint16), |
| 1141 |
('rc', c_char_p), |
| 1142 |
('describe', c_char_p), |
| 1143 |
] |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
libusb_log_cb_p = LIBUSB_CALL_FUNCTYPE(None, libusb_context_p, c_int, c_char_p) |
| 1148 |
|
| 1149 |
class libusb_init_option_value(Union): |
| 1150 |
_fields_ = [ |
| 1151 |
('ival', c_int), |
| 1152 |
('log_cbval', libusb_log_cb_p), |
| 1153 |
] |
| 1154 |
|
| 1155 |
class libusb_init_option(Structure): |
| 1156 |
_fields_ = [ |
| 1157 |
('option', c_int), |
| 1158 |
('value', libusb_init_option_value), |
| 1159 |
] |
| 1160 |
|
| 1161 |
libusb_speed = Enum({ |
| 1162 |
|
| 1163 |
'LIBUSB_SPEED_UNKNOWN': 0, |
| 1164 |
|
| 1165 |
'LIBUSB_SPEED_LOW': 1, |
| 1166 |
|
| 1167 |
'LIBUSB_SPEED_FULL': 2, |
| 1168 |
|
| 1169 |
'LIBUSB_SPEED_HIGH': 3, |
| 1170 |
|
| 1171 |
'LIBUSB_SPEED_SUPER': 4, |
| 1172 |
|
| 1173 |
'LIBUSB_SPEED_SUPER_PLUS': 5, |
| 1174 |
|
| 1175 |
'LIBUSB_SPEED_SUPER_PLUS_X2': 6, |
| 1176 |
}) |
| 1177 |
|
| 1178 |
libusb_supported_speed = Enum({ |
| 1179 |
|
| 1180 |
'LIBUSB_LOW_SPEED_OPERATION': 1, |
| 1181 |
|
| 1182 |
'LIBUSB_FULL_SPEED_OPERATION': 2, |
| 1183 |
|
| 1184 |
'LIBUSB_HIGH_SPEED_OPERATION': 4, |
| 1185 |
|
| 1186 |
'LIBUSB_5GBPS_OPERATION': 8, |
| 1187 |
}) |
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
libusb_error = Enum({ |
| 1192 |
|
| 1193 |
'LIBUSB_SUCCESS': 0, |
| 1194 |
|
| 1195 |
'LIBUSB_ERROR_IO': -1, |
| 1196 |
|
| 1197 |
'LIBUSB_ERROR_INVALID_PARAM': -2, |
| 1198 |
|
| 1199 |
'LIBUSB_ERROR_ACCESS': -3, |
| 1200 |
|
| 1201 |
'LIBUSB_ERROR_NO_DEVICE': -4, |
| 1202 |
|
| 1203 |
'LIBUSB_ERROR_NOT_FOUND': -5, |
| 1204 |
|
| 1205 |
'LIBUSB_ERROR_BUSY': -6, |
| 1206 |
|
| 1207 |
'LIBUSB_ERROR_TIMEOUT': -7, |
| 1208 |
|
| 1209 |
'LIBUSB_ERROR_OVERFLOW': -8, |
| 1210 |
|
| 1211 |
'LIBUSB_ERROR_PIPE': -9, |
| 1212 |
|
| 1213 |
'LIBUSB_ERROR_INTERRUPTED': -10, |
| 1214 |
|
| 1215 |
'LIBUSB_ERROR_NO_MEM': -11, |
| 1216 |
|
| 1217 |
'LIBUSB_ERROR_NOT_SUPPORTED': -12, |
| 1218 |
|
| 1219 |
'LIBUSB_ERROR_OTHER': -99, |
| 1220 |
}) |
| 1221 |
|
| 1222 |
|
| 1223 |
libusb_transfer_status = Enum({ |
| 1224 |
|
| 1225 |
|
| 1226 |
'LIBUSB_TRANSFER_COMPLETED': 0, |
| 1227 |
|
| 1228 |
'LIBUSB_TRANSFER_ERROR': 1, |
| 1229 |
|
| 1230 |
'LIBUSB_TRANSFER_TIMED_OUT': 2, |
| 1231 |
|
| 1232 |
'LIBUSB_TRANSFER_CANCELLED': 3, |
| 1233 |
|
| 1234 |
|
| 1235 |
'LIBUSB_TRANSFER_STALL': 4, |
| 1236 |
|
| 1237 |
'LIBUSB_TRANSFER_NO_DEVICE': 5, |
| 1238 |
|
| 1239 |
'LIBUSB_TRANSFER_OVERFLOW': 6, |
| 1240 |
}) |
| 1241 |
|
| 1242 |
|
| 1243 |
libusb_transfer_flags = Enum({ |
| 1244 |
|
| 1245 |
'LIBUSB_TRANSFER_SHORT_NOT_OK': 1 << 0, |
| 1246 |
|
| 1247 |
'LIBUSB_TRANSFER_FREE_BUFFER': 1 << 1, |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
'LIBUSB_TRANSFER_FREE_TRANSFER': 1 << 2, |
| 1253 |
|
| 1254 |
|
| 1255 |
'LIBUSB_TRANSFER_ADD_ZERO_PACKET': 1 << 3, |
| 1256 |
}) |
| 1257 |
|
| 1258 |
|
| 1259 |
class libusb_iso_packet_descriptor(Structure): |
| 1260 |
_fields_ = [('length', c_uint), |
| 1261 |
('actual_length', c_uint), |
| 1262 |
('status', c_int)] |
| 1263 |
libusb_iso_packet_descriptor_p = POINTER(libusb_iso_packet_descriptor) |
| 1264 |
|
| 1265 |
class libusb_transfer(Structure): |
| 1266 |
pass |
| 1267 |
libusb_transfer_p = POINTER(libusb_transfer) |
| 1268 |
|
| 1269 |
libusb_transfer_cb_fn_p = LIBUSB_CALL_FUNCTYPE(None, libusb_transfer_p) |
| 1270 |
|
| 1271 |
libusb_capability = Enum({ |
| 1272 |
|
| 1273 |
'LIBUSB_CAP_HAS_CAPABILITY': 0x0000, |
| 1274 |
|
| 1275 |
'LIBUSB_CAP_HAS_HOTPLUG': 0x0001, |
| 1276 |
|
| 1277 |
'LIBUSB_CAP_HAS_HID_ACCESS': 0x0100, |
| 1278 |
|
| 1279 |
'LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER': 0x0101, |
| 1280 |
}) |
| 1281 |
|
| 1282 |
libusb_log_level = Enum({ |
| 1283 |
'LIBUSB_LOG_LEVEL_NONE': 0, |
| 1284 |
'LIBUSB_LOG_LEVEL_ERROR': 1, |
| 1285 |
'LIBUSB_LOG_LEVEL_WARNING': 2, |
| 1286 |
'LIBUSB_LOG_LEVEL_INFO': 3, |
| 1287 |
'LIBUSB_LOG_LEVEL_DEBUG': 4, |
| 1288 |
}) |
| 1289 |
|
| 1290 |
libusb_log = Enum({ |
| 1291 |
'LIBUSB_LOG_CB_GLOBAL': 1 << 0, |
| 1292 |
'LIBUSB_LOG_CB_CONTEXT': 1 << 1, |
| 1293 |
}) |
| 1294 |
|
| 1295 |
libusb_option = Enum({ |
| 1296 |
'LIBUSB_OPTION_LOG_LEVEL': 0, |
| 1297 |
'LIBUSB_OPTION_USE_USBDK': 1, |
| 1298 |
'LIBUSB_OPTION_NO_DEVICE_DISCOVERY': 2, |
| 1299 |
'LIBUSB_OPTION_LOG_CB': 3, |
| 1300 |
'LIBUSB_OPTION_MAX': 4, |
| 1301 |
}) |
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
def libusb_control_transfer_get_data(transfer_p): |
| 1315 |
transfer = transfer_p.contents |
| 1316 |
return buffer_at(transfer.buffer, transfer.length)[ |
| 1317 |
LIBUSB_CONTROL_SETUP_SIZE:] |
| 1318 |
|
| 1319 |
def libusb_control_transfer_get_setup(transfer_p): |
| 1320 |
return cast(transfer_p.contents.buffer, libusb_control_setup_p) |
| 1321 |
|
| 1322 |
def libusb_fill_control_setup( |
| 1323 |
setup_p, bmRequestType, bRequest, wValue, wIndex, wLength): |
| 1324 |
setup = cast(setup_p, libusb_control_setup_p).contents |
| 1325 |
setup.bmRequestType = bmRequestType |
| 1326 |
setup.bRequest = bRequest |
| 1327 |
setup.wValue = libusb_cpu_to_le16(wValue) |
| 1328 |
setup.wIndex = libusb_cpu_to_le16(wIndex) |
| 1329 |
setup.wLength = libusb_cpu_to_le16(wLength) |
| 1330 |
|
| 1331 |
|
| 1332 |
def libusb_fill_control_transfer( |
| 1333 |
transfer_p, dev_handle, buffer, callback, user_data, timeout): |
| 1334 |
transfer = transfer_p.contents |
| 1335 |
transfer.dev_handle = dev_handle |
| 1336 |
transfer.endpoint = 0 |
| 1337 |
|
| 1338 |
transfer.type = LIBUSB_TRANSFER_TYPE_CONTROL |
| 1339 |
|
| 1340 |
transfer.timeout = timeout |
| 1341 |
transfer.buffer = cast(buffer, c_void_p) |
| 1342 |
if buffer is not None: |
| 1343 |
setup = cast(buffer, libusb_control_setup_p).contents |
| 1344 |
|
| 1345 |
transfer.length = LIBUSB_CONTROL_SETUP_SIZE + \ |
| 1346 |
libusb_le16_to_cpu(setup.wLength) |
| 1347 |
|
| 1348 |
transfer.user_data = user_data |
| 1349 |
transfer.callback = callback |
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
def libusb_fill_bulk_transfer( |
| 1354 |
transfer_p, dev_handle, endpoint, buffer, length, |
| 1355 |
callback, user_data, timeout): |
| 1356 |
transfer = transfer_p.contents |
| 1357 |
transfer.dev_handle = dev_handle |
| 1358 |
transfer.endpoint = endpoint |
| 1359 |
|
| 1360 |
transfer.type = LIBUSB_TRANSFER_TYPE_BULK |
| 1361 |
|
| 1362 |
transfer.timeout = timeout |
| 1363 |
transfer.buffer = cast(buffer, c_void_p) |
| 1364 |
transfer.length = length |
| 1365 |
transfer.user_data = user_data |
| 1366 |
transfer.callback = callback |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
def libusb_fill_interrupt_transfer( |
| 1371 |
transfer_p, dev_handle, endpoint, buffer, |
| 1372 |
length, callback, user_data, timeout): |
| 1373 |
transfer = transfer_p.contents |
| 1374 |
transfer.dev_handle = dev_handle |
| 1375 |
transfer.endpoint = endpoint |
| 1376 |
|
| 1377 |
transfer.type = LIBUSB_TRANSFER_TYPE_INTERRUPT |
| 1378 |
|
| 1379 |
transfer.timeout = timeout |
| 1380 |
transfer.buffer = cast(buffer, c_void_p) |
| 1381 |
transfer.length = length |
| 1382 |
transfer.user_data = user_data |
| 1383 |
transfer.callback = callback |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
def libusb_fill_iso_transfer( |
| 1388 |
transfer_p, dev_handle, endpoint, buffer, length, |
| 1389 |
num_iso_packets, callback, user_data, timeout): |
| 1390 |
transfer = transfer_p.contents |
| 1391 |
transfer.dev_handle = dev_handle |
| 1392 |
transfer.endpoint = endpoint |
| 1393 |
|
| 1394 |
transfer.type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS |
| 1395 |
|
| 1396 |
transfer.timeout = timeout |
| 1397 |
transfer.buffer = cast(buffer, c_void_p) |
| 1398 |
transfer.length = length |
| 1399 |
transfer.num_iso_packets = num_iso_packets |
| 1400 |
transfer.user_data = user_data |
| 1401 |
transfer.callback = callback |
| 1402 |
|
| 1403 |
|
| 1404 |
def _get_iso_packet_list(transfer): |
| 1405 |
list_type = libusb_iso_packet_descriptor * transfer.num_iso_packets |
| 1406 |
return list_type.from_address(addressof(transfer.iso_packet_desc)) |
| 1407 |
|
| 1408 |
def get_iso_packet_list(transfer_p): |
| 1409 |
""" |
| 1410 |
Python-specific helper extracting a list of iso packet descriptors, |
| 1411 |
because it's not as straight-forward as in C. |
| 1412 |
""" |
| 1413 |
return _get_iso_packet_list(transfer_p.contents) |
| 1414 |
|
| 1415 |
def _get_iso_packet_buffer(transfer, offset, length): |
| 1416 |
return buffer_at(transfer.buffer + offset, length) |
| 1417 |
|
| 1418 |
def get_iso_packet_buffer_list(transfer_p): |
| 1419 |
""" |
| 1420 |
Python-specific helper extracting a list of iso packet buffers. |
| 1421 |
""" |
| 1422 |
transfer = transfer_p.contents |
| 1423 |
offset = 0 |
| 1424 |
result = [] |
| 1425 |
append = result.append |
| 1426 |
for iso_transfer in _get_iso_packet_list(transfer): |
| 1427 |
length = iso_transfer.length |
| 1428 |
append(_get_iso_packet_buffer(transfer, offset, length)) |
| 1429 |
offset += length |
| 1430 |
return result |
| 1431 |
|
| 1432 |
def get_extra(descriptor): |
| 1433 |
""" |
| 1434 |
Python-specific helper to access "extra" field of descriptors, |
| 1435 |
because it's not as straight-forward as in C. |
| 1436 |
Returns a list, where each entry is an individual extra descriptor. |
| 1437 |
""" |
| 1438 |
result = [] |
| 1439 |
extra_length = descriptor.extra_length |
| 1440 |
if extra_length: |
| 1441 |
extra = buffer_at(descriptor.extra, extra_length) |
| 1442 |
append = result.append |
| 1443 |
while extra: |
| 1444 |
length = extra[0] |
| 1445 |
if not 0 < length <= len(extra): |
| 1446 |
raise ValueError( |
| 1447 |
f'Extra descriptor {len(result)} is incomplete/invalid', |
| 1448 |
) |
| 1449 |
append(extra[:length]) |
| 1450 |
extra = extra[length:] |
| 1451 |
return result |
| 1452 |
|
| 1453 |
def libusb_set_iso_packet_lengths(transfer_p, length): |
| 1454 |
transfer = transfer_p.contents |
| 1455 |
for iso_packet_desc in _get_iso_packet_list(transfer): |
| 1456 |
iso_packet_desc.length = length |
| 1457 |
|
| 1458 |
def libusb_get_iso_packet_buffer(transfer_p, packet): |
| 1459 |
transfer = transfer_p.contents |
| 1460 |
offset = 0 |
| 1461 |
if packet >= transfer.num_iso_packets: |
| 1462 |
return None |
| 1463 |
iso_packet_desc_list = _get_iso_packet_list(transfer) |
| 1464 |
for i in range(packet): |
| 1465 |
offset += iso_packet_desc_list[i].length |
| 1466 |
return _get_iso_packet_buffer( |
| 1467 |
transfer, offset, iso_packet_desc_list[packet].length) |
| 1468 |
|
| 1469 |
def libusb_get_iso_packet_buffer_simple(transfer_p, packet): |
| 1470 |
transfer = transfer_p.contents |
| 1471 |
if packet >= transfer.num_iso_packets: |
| 1472 |
return None |
| 1473 |
iso_length = transfer.iso_packet_desc.length |
| 1474 |
return _get_iso_packet_buffer(transfer, iso_length * packet, iso_length) |
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
def libusb_get_descriptor(dev, desc_type, desc_index, data, length): |
| 1480 |
return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, |
| 1481 |
LIBUSB_REQUEST_GET_DESCRIPTOR, |
| 1482 |
(desc_type << 8) | desc_index, 0, data, |
| 1483 |
length, 1000) |
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
def libusb_get_string_descriptor(dev, desc_index, langid, data, length): |
| 1488 |
return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, |
| 1489 |
LIBUSB_REQUEST_GET_DESCRIPTOR, |
| 1490 |
(LIBUSB_DT_STRING << 8) | desc_index, |
| 1491 |
langid, data, length, 1000) |
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
class libusb_pollfd(Structure): |
| 1497 |
_fields_ = [ |
| 1498 |
('fd', c_int), |
| 1499 |
('events', c_short), |
| 1500 |
] |
| 1501 |
libusb_pollfd_p = POINTER(libusb_pollfd) |
| 1502 |
libusb_pollfd_p_p = POINTER(libusb_pollfd_p) |
| 1503 |
|
| 1504 |
libusb_pollfd_added_cb_p = LIBUSB_CALL_FUNCTYPE(None, c_int, c_short, py_object) |
| 1505 |
libusb_pollfd_removed_cb_p = LIBUSB_CALL_FUNCTYPE(None, c_int, py_object) |
| 1506 |
|
| 1507 |
|
| 1508 |
libusb_hotplug_callback_handle = c_int |
| 1509 |
|
| 1510 |
libusb_hotplug_flag = Enum({ |
| 1511 |
'LIBUSB_HOTPLUG_ENUMERATE': 1, |
| 1512 |
}) |
| 1513 |
|
| 1514 |
libusb_hotplug_event = Enum({ |
| 1515 |
'LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED': 0x01, |
| 1516 |
'LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT': 0x02, |
| 1517 |
}) |
| 1518 |
|
| 1519 |
LIBUSB_HOTPLUG_NO_FLAGS = 0 |
| 1520 |
LIBUSB_HOTPLUG_MATCH_ANY = -1 |
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
libusb_hotplug_callback_fn_p = LIBUSB_CALL_FUNCTYPE( |
| 1525 |
c_int, libusb_context_p, libusb_device_p, c_int, c_void_p) |
| 1526 |
|
| 1527 |
|