@@ -916,6 +916,11 @@ def control_stress_test(dev, log):
916
916
917
917
918
918
def find_ep_pair (intf , endpoint_type ):
919
+ """Find an OUT and IN endpoint pair.
920
+
921
+ Raise a RuntimeError if any endpoint could not be found
922
+ or wMaxPacketSize is not equal for both endpoints.
923
+ """
919
924
ep_out = usb .util .find_descriptor (
920
925
intf , custom_match = lambda e :
921
926
usb .util .endpoint_type (e .bmAttributes ) == endpoint_type and
@@ -934,13 +939,24 @@ def find_ep_pair(intf, endpoint_type):
934
939
935
940
936
941
def loopback_ep_test (ep_out , ep_in , payload_size ):
942
+ """Send and receive random data using OUT/IN endpoint pair.
943
+
944
+ Verify that data received from IN endpoint is equal to
945
+ data sent to OUT endpoint.
946
+ Raise a RuntimeError if data does not match.
947
+ """
937
948
payload_out = array .array ('B' , (random .randint (0x00 , 0xff ) for _ in range (payload_size )))
938
949
ep_out .write (payload_out )
939
950
payload_in = ep_in .read (ep_in .wMaxPacketSize )
940
951
raise_if_different (payload_out , payload_in , lineno (), 'Payloads mismatch.' )
941
952
942
953
943
954
def random_size_loopback_ep_test (ep_out , ep_in , failure , error , seconds , log , min_payload_size = 1 ):
955
+ """Repeat data transfer test for OUT/IN endpoint pair for a given time.
956
+
957
+ Set a failure Event if OUT/IN data verification fails.
958
+ Set an error Event if unexpected USB error occurs.
959
+ """
944
960
end_ts = time .time () + seconds
945
961
while time .time () < end_ts and not failure .is_set () and not error .is_set ():
946
962
payload_size = random .randint (min_payload_size , ep_out .wMaxPacketSize )
@@ -958,6 +974,12 @@ def random_size_loopback_ep_test(ep_out, ep_in, failure, error, seconds, log, mi
958
974
959
975
960
976
def halt_ep_test (dev , ep_out , ep_in , ep_to_halt , log ):
977
+ """OUT/IN endpoint halt test.
978
+
979
+ Verify that halting an endpoint at a random point of OUT or IN transfer
980
+ raises a USBError.
981
+ Raise a RuntimeError if halt fails or any unexpected error occurs.
982
+ """
961
983
MIN_HALT_DELAY = 0.01
962
984
MAX_HALT_DELAY = 0.1
963
985
delay = random .uniform (MIN_HALT_DELAY , MAX_HALT_DELAY )
@@ -969,6 +991,7 @@ def halt_ep_test(dev, ep_out, ep_in, ep_to_halt, log):
969
991
ctrl_error = Event ()
970
992
971
993
def timer_handler ():
994
+ """Halt an endpoint using a USB control request."""
972
995
try :
973
996
dev .ctrl_transfer (** ctrl_kwargs )
974
997
except Exception as err :
@@ -995,10 +1018,19 @@ def timer_handler():
995
1018
996
1019
USB_ERROR_FMT = str ('Got {0!r} while testing endpoints '
997
1020
'{1.bEndpointAddress:#04x}({1.wMaxPacketSize:02}) and '
998
- '{2.bEndpointAddress:#04x}({2.wMaxPacketSize:02}) with a random payload of {3} B.' )
1021
+ '{2.bEndpointAddress:#04x}({2.wMaxPacketSize:02}) with '
1022
+ 'a random payload of {3} B.' )
999
1023
1000
1024
1001
1025
def ep_test_data_correctness (dev , log , verbose = False ):
1026
+ """Test data correctness for every OUT/IN endpoint pair.
1027
+
1028
+ Given a USB device with multiple OUT/IN endpoint pairs
1029
+ When the host sends random payloads up to wMaxPacketSize in size
1030
+ to an OUT endpoint of the device,
1031
+ and then the device sends data back to host using an IN endpoint
1032
+ Then data sent and received by host is equal for every endpoint pair
1033
+ """
1002
1034
cfg = dev .get_active_configuration ()
1003
1035
for intf in cfg :
1004
1036
log ('interface {}, alt {} -- ' .format (intf .bInterfaceNumber , intf .bAlternateSetting ), end = '' )
@@ -1047,6 +1079,13 @@ def ep_test_data_correctness(dev, log, verbose=False):
1047
1079
1048
1080
1049
1081
def ep_test_halt (dev , log , verbose = False ):
1082
+ """Test endpoint halt for every OUT/IN endpoint pair.
1083
+
1084
+ Given a USB device with multiple OUT/IN endpoint pairs
1085
+ When the host issues an endpoint halt control request at a random point
1086
+ of OUT or IN transfer
1087
+ Then the endpoint is stalled and all further transfers fail
1088
+ """
1050
1089
cfg = dev .get_active_configuration ()
1051
1090
for intf in cfg :
1052
1091
log ('interface {}, alt {} -- ' .format (intf .bInterfaceNumber , intf .bAlternateSetting ), end = '' )
@@ -1092,6 +1131,13 @@ def ep_test_halt(dev, log, verbose=False):
1092
1131
1093
1132
1094
1133
def ep_test_parallel_transfers (dev , log , verbose = False ):
1134
+ """Test simultaneous data transfers for multiple OUT/IN endpoint pairs.
1135
+
1136
+ Given a USB device with multiple OUT/IN endpoint pairs
1137
+ When multiple OUT and IN endpoints are used to transfer random test data
1138
+ Then all transfers succeed
1139
+ and data received equals data sent for every endpoint pair
1140
+ """
1095
1141
cfg = dev .get_active_configuration ()
1096
1142
for intf in cfg :
1097
1143
log ('interface {}, alt {} -- ' .format (intf .bInterfaceNumber , intf .bAlternateSetting ), end = '' )
@@ -1145,6 +1191,14 @@ def ep_test_parallel_transfers(dev, log, verbose=False):
1145
1191
1146
1192
1147
1193
def ep_test_parallel_transfers_ctrl (dev , log , verbose = False ):
1194
+ """Test simultaneous data transfers in parallel with control transfers.
1195
+
1196
+ Given a USB device with multiple OUT/IN endpoint pairs
1197
+ When multiple OUT and IN endpoints are used to transfer random data
1198
+ and control requests are processed in parallel
1199
+ Then all transfers succeed
1200
+ and for every endpoint pair, data received by host equals data sent by host
1201
+ """
1148
1202
cfg = dev .get_active_configuration ()
1149
1203
for intf in cfg :
1150
1204
log ('interface {}, alt {} -- ' .format (intf .bInterfaceNumber , intf .bAlternateSetting ), end = '' )
@@ -1201,6 +1255,12 @@ def ep_test_parallel_transfers_ctrl(dev, log, verbose=False):
1201
1255
1202
1256
1203
1257
def ep_test_abort (dev , log , verbose = False ):
1258
+ """Test aborting data transfer for every OUT/IN endpoint pair.
1259
+
1260
+ Given a USB device with multiple OUT/IN endpoint pairs
1261
+ When a device aborts an in progress data transfer
1262
+ Then no more data is transmitted
1263
+ """
1204
1264
NUM_PACKETS_UNTIL_ABORT = 2
1205
1265
NUM_PACKETS_AFTER_ABORT = 8
1206
1266
cfg = dev .get_active_configuration ()
0 commit comments