Skip to content

Commit 091647e

Browse files
maciejbocianski0xc0170
authored andcommitted
pyusb_basic test add helper functions
1 parent 094f317 commit 091647e

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

TESTS/host_tests/pyusb_basic.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,58 @@ def repeated_construction_destruction_test(log):
894894
yield
895895

896896

897+
def release_interfaces(dev):
898+
""" Releases interfaces to allow configuration switch
899+
900+
Fixes error while configuration change(on Windows machines):
901+
USBError: [Errno None] libusb0-dll:err [set_configuration] can't change configuration, an interface is still in use (claimed)
902+
"""
903+
cfg = dev.get_active_configuration()
904+
for i in range(0, cfg.bNumInterfaces):
905+
usb.util.release_interface(dev, i)
906+
907+
908+
def restore_default_configuration(dev):
909+
cfg = dev[1]
910+
cfg.set()
911+
912+
913+
def get_status(dev, recipient, index = 0):
914+
""" Get status of the recipient
915+
916+
Args:
917+
dev - pyusb device
918+
recipient - CTRL_RECIPIENT_DEVICE/CTRL_RECIPIENT_INTERFACE/CTRL_RECIPIENT_ENDPOINT
919+
index - 0 if recipient is device, interface index if recipient is interface, endpoint index if recipient is endpoint
920+
921+
Returns:
922+
status flag 32b int
923+
"""
924+
request_type = build_request_type(CTRL_IN, CTRL_TYPE_STANDARD,
925+
recipient)
926+
request = REQUEST_GET_STATUS
927+
value = 0 # Always 0 for this request
928+
index = index # recipient index
929+
length = 2 # Always 2 for this request (size of return data)
930+
ret = dev.ctrl_transfer(request_type, request, value, index, length)
931+
ret = ret[0] | (ret[1] << 8)
932+
933+
return ret
934+
935+
936+
def get_descriptor(dev, type_index, lang_id, length):
937+
# Control IN GET_DESCRIPTOR - device
938+
request_type = build_request_type(CTRL_IN, CTRL_TYPE_STANDARD,
939+
CTRL_RECIPIENT_DEVICE)
940+
request = REQUEST_GET_DESCRIPTOR
941+
value = type_index # Descriptor Type (H) and Descriptor Index (L)
942+
index = lang_id # 0 or Language ID for this request
943+
length = length # Descriptor Length
944+
ret = dev.ctrl_transfer(request_type, request, value, index, length)
945+
946+
return ret
947+
948+
897949
"""
898950
For documentation purpose until test writing finished
899951
TODO: remove this if not needed anymore

0 commit comments

Comments
 (0)