@@ -85,13 +85,9 @@ def get_interface(dev, interface, alternate=0):
85
85
FEATURE_ENDPOINT_HALT = 0
86
86
FEATURE_DEVICE_REMOTE_WAKEUP = 1
87
87
88
-
89
-
90
88
DEVICE_QUALIFIER_DESC_SIZE = 10
91
-
92
89
DESC_TYPE_DEVICE_QUALIFIER = 0x06
93
90
94
-
95
91
DEVICE_DESC_SIZE = 18
96
92
device_descriptor_parser = struct .Struct ('BBHBBBBHHHBBBB' )
97
93
device_descriptor_keys = ['bLength' , 'bDescriptorType' , 'bcdUSB' , 'bDeviceClass' ,
@@ -352,11 +348,9 @@ def setup(self):
352
348
353
349
self .register_callback ('reset_support' , self ._callback_reset_support )
354
350
355
-
356
351
def result (self ):
357
352
return self .__result
358
353
359
-
360
354
def teardown (self ):
361
355
pass
362
356
@@ -415,10 +409,9 @@ def get_set_configuration_test(dev, log):
415
409
# check if dafault(1) configuration set
416
410
try :
417
411
ret = usb .control .get_configuration (dev )
418
- raise_if_different (1 , ret , lineno (), "FAILED - expected first configuration set" )
412
+ raise_if_different (1 , ret , lineno (), 'Invalid configuration.' )
419
413
except usb .core .USBError as error :
420
- print (error )
421
- raise_unconditionally (lineno (), "FAILED - get_configuration !!!" )
414
+ raise_unconditionally (lineno (), 'get_configuration request failed ({}).' .format (str (error ).strip ()))
422
415
423
416
cfg = dev .get_active_configuration ()
424
417
for intf in cfg :
@@ -428,35 +421,31 @@ def get_set_configuration_test(dev, log):
428
421
try :
429
422
ret = dev .set_configuration (0 )
430
423
except usb .core .USBError as error :
431
- print (error )
432
- raise_unconditionally (lineno (), "FAILED - set_configuration(0) !!!" )
424
+ raise_unconditionally (lineno (), 'set_configuration request (deconfigure) failed ({}).' .format (str (error ).strip ()))
433
425
434
426
# check if deconfigured
435
427
try :
436
428
ret = usb .control .get_configuration (dev )
437
- raise_if_different (0 , ret , lineno (), "FAILED - expected to be deconfigured" )
429
+ raise_if_different (0 , ret , lineno (), 'Invalid configuration.' )
438
430
print ("device deconfigured - OK" )
439
431
except usb .core .USBError as error :
440
- print (error )
441
- raise_unconditionally (lineno (), "FAILED - get_active_configuration !!!" )
432
+ raise_unconditionally (lineno (), 'get_configuration request failed ({}).' .format (str (error ).strip ()))
442
433
443
434
# for every configuration
444
435
for cfg in dev :
445
436
try :
446
437
# set configuration
447
438
ret = cfg .set ()
448
439
except usb .core .USBError as error :
449
- print (error )
450
- raise_unconditionally (lineno (), "FAILED - set configuration" )
440
+ raise_unconditionally (lineno (), 'set_configuration request failed ({}).' .format (str (error ).strip ()))
451
441
452
442
# check if configured
453
443
try :
454
444
ret = usb .control .get_configuration (dev )
455
- raise_if_different (cfg .bConfigurationValue , ret , lineno (), "FAILED - expected {} configuration set" . format ( cfg . bConfigurationValue ) )
445
+ raise_if_different (cfg .bConfigurationValue , ret , lineno (), 'Invalid configuration.' )
456
446
print ("configuration {} set - OK " .format (cfg .bConfigurationValue ))
457
447
except usb .core .USBError as error :
458
- print (error )
459
- raise_unconditionally (lineno (), "FAILED - get_configuration !!!" )
448
+ raise_unconditionally (lineno (), 'get_configuration request failed ({}).' .format (str (error ).strip ()))
460
449
# test control data transfer after configuration set
461
450
control_data_test (dev , [64 , 256 ], log )
462
451
print ("" ) # new line
@@ -477,7 +466,7 @@ def get_set_interface_test(dev, log):
477
466
cfg .set ()
478
467
# for every interface
479
468
for intf in cfg :
480
- intf .set_altsetting ();
469
+ intf .set_altsetting ()
481
470
altsett = usb .control .get_interface (dev , intf .bInterfaceNumber )
482
471
raise_if_different (intf .bAlternateSetting , altsett , lineno (), text = 'Wrong alternate setting for interface {}' .format (intf .bInterfaceNumber ))
483
472
print ("cfg({}) inteface {}.{} set - OK" .format (cfg .bConfigurationValue , intf .bInterfaceNumber , intf .bAlternateSetting ))
@@ -583,15 +572,13 @@ def set_clear_feature_test(dev, log):
583
572
try :
584
573
usb .control .set_feature (dev , FEATURE_ENDPOINT_HALT , ep )
585
574
except usb .core .USBError as err :
586
- print (err )
587
- raise_unconditionally (lineno (), "endpoint {} halt failed" .format (ep .bEndpointAddress ))
575
+ raise_unconditionally (lineno (), 'set_feature request (halt) failed for endpoint {} ({}).' .format (ep .bEndpointAddress , str (err ).strip ()))
588
576
589
577
# check if endpoint was halted
590
578
try :
591
579
ret = usb .control .get_status (dev , ep )
592
580
except usb .core .USBError as err :
593
- print (err )
594
- raise_unconditionally (lineno (), "endpoint status failed" .format (ep .bEndpointAddress ))
581
+ raise_unconditionally (lineno (), 'get_status request failed for endpoint {} ({}).' .format (ep .bEndpointAddress , str (err ).strip ()))
595
582
if (ret != 1 ):
596
583
raise_unconditionally (lineno (), "endpoint {} was not halted" .format (ep .bEndpointAddress ))
597
584
print ("cfg({}) intf({}.{}) ep {} halted - OK" .format (cfg .bConfigurationValue , intf .bInterfaceNumber , intf .bAlternateSetting , ep .bEndpointAddress ))
@@ -600,8 +587,7 @@ def set_clear_feature_test(dev, log):
600
587
try :
601
588
usb .control .clear_feature (dev , FEATURE_ENDPOINT_HALT , ep )
602
589
except usb .core .USBError as err :
603
- print (err )
604
- raise_unconditionally (lineno (), "endpoint {} unhalt failed" .format (ep .bEndpointAddress ))
590
+ raise_unconditionally (lineno (), "clear_feature request (unhalt) failed for endpoint {} ({})" .format (ep .bEndpointAddress , str (err ).strip ()))
605
591
606
592
# check if endpoint was unhalted
607
593
ret = usb .control .get_status (dev , ep )
@@ -635,31 +621,31 @@ def get_descriptor_test(dev, vendor_id, product_id, log):
635
621
try :
636
622
ret = get_descriptor (dev , (DESC_TYPE_DEVICE << 8 ) | (0 << 0 ), 0 , DEVICE_DESC_SIZE )
637
623
dev_desc = dict (zip (device_descriptor_keys , device_descriptor_parser .unpack (ret )))
638
- raise_if_different (DEVICE_DESC_SIZE , dev_desc ['bLength' ], lineno (), text = 'Wrong device descriptor size !!! ' )
639
- raise_if_different (vendor_id , dev_desc ['idVendor' ], lineno (), text = 'Wrong vendor id !!! ' )
640
- raise_if_different (product_id , dev_desc ['idProduct' ], lineno (), text = 'Wrong product id !!! ' )
624
+ raise_if_different (DEVICE_DESC_SIZE , dev_desc ['bLength' ], lineno (), text = 'Wrong device descriptor size. ' )
625
+ raise_if_different (vendor_id , dev_desc ['idVendor' ], lineno (), text = 'Wrong vendor ID. ' )
626
+ raise_if_different (product_id , dev_desc ['idProduct' ], lineno (), text = 'Wrong product ID. ' )
641
627
except usb .core .USBError :
642
628
raise_unconditionally (lineno (), "Requesting device descriptor failed" )
643
629
644
630
# configuration descriptor
645
631
try :
646
632
ret = get_descriptor (dev , (DESC_TYPE_CONFIG << 8 ) | (0 << 0 ), 0 , CONFIGURATION_DESC_SIZE )
647
633
conf_desc = dict (zip (configuration_descriptor_keys , configuration_descriptor_parser .unpack (ret )))
648
- raise_if_different (CONFIGURATION_DESC_SIZE , conf_desc ['bLength' ], lineno (), text = 'Wrong configuration descriptor size !!! ' )
634
+ raise_if_different (CONFIGURATION_DESC_SIZE , conf_desc ['bLength' ], lineno (), text = 'Wrong configuration descriptor size. ' )
649
635
except usb .core .USBError :
650
636
raise_unconditionally (lineno (), "Requesting configuration descriptor failed" )
651
637
652
638
# interface descriptor
653
639
try :
654
640
ret = get_descriptor (dev , (DESC_TYPE_INTERFACE << 8 ) | (0 << 0 ), 0 , INTERFACE_DESC_SIZE )
655
- raise_unconditionally (lineno (), "Requesting interface descriptor should fail since it is not directly accessible" )
641
+ raise_unconditionally (lineno (), "Requesting interface descriptor should fail since it is not directly accessible. " )
656
642
except usb .core .USBError :
657
643
log ("interface descriptor is not directly accessible - OK" )
658
644
659
645
# endpoint descriptor
660
646
try :
661
647
ret = get_descriptor (dev , (DESC_TYPE_ENDPOINT << 8 ) | (0 << 0 ), 0 , ENDPOINT_DESC_SIZE )
662
- raise_unconditionally (lineno (), "Requesting endpoint descriptor should fail since it is not directly accessible" )
648
+ raise_unconditionally (lineno (), "Requesting endpoint descriptor should fail since it is not directly accessible. " )
663
649
except usb .core .USBError :
664
650
log ("endpoint descriptor is not directly accessible - OK" )
665
651
print ("" ) # new line
@@ -687,7 +673,7 @@ def set_descriptor_test(dev, log):
687
673
data = bytearray (DEVICE_DESC_SIZE ) # Descriptor data
688
674
try :
689
675
dev .ctrl_transfer (request_type , request , value , index , data )
690
- raise_unconditionally (lineno (), "SET_DESCRIPTOR should fail since it is not implemented" )
676
+ raise_unconditionally (lineno (), "set_descriptor request should fail since it is not implemented" )
691
677
except usb .core .USBError :
692
678
log ("SET_DESCRIPTOR is unsupported - OK" )
693
679
print ("" ) # new line
@@ -715,7 +701,7 @@ def synch_frame_test(dev, log):
715
701
ret = ret [0 ] | (ret [1 ] << 8 )
716
702
log ("synch frame ret: %d" % (ret ))
717
703
except usb .core .USBError :
718
- raise_unconditionally (lineno (), "SYNCH_FRAME failed" )
704
+ raise_unconditionally (lineno (), "SYNCH_FRAME request failed" )
719
705
print ("" ) # new line
720
706
721
707
@@ -847,7 +833,7 @@ def control_data_test(dev, sizes_list, log):
847
833
ret = dev .ctrl_transfer (request_type , request , value , index , length , 5000 )
848
834
raise_if_different (i , len (ret ), lineno (), "send/receive data is the wrong size" )
849
835
for j in range (0 , i ):
850
- raise_if_different (data [j ], ret [j ], lineno (), "send/receive data not match " )
836
+ raise_if_different (data [j ], ret [j ], lineno (), "send/receive data mismatch " )
851
837
except usb .core .USBError :
852
838
raise_unconditionally (lineno (), "VENDOR_TEST_CTRL_IN_SIZES failed" )
853
839
count += 1
@@ -1001,7 +987,7 @@ def timer_handler():
1001
987
raise RuntimeError ('Invalid endpoint status after halt operation' )
1002
988
except Exception as err :
1003
989
ctrl_error .set ()
1004
- log ('Endpoint {:#04x} halt failed ({}).' .format (ep_to_halt .bEndpointAddress , err ))
990
+ log ('Endpoint {:#04x} halt failed ({!r }).' .format (ep_to_halt .bEndpointAddress , err ))
1005
991
# Whether the halt operation was successful or not,
1006
992
# wait a bit so the main thread has a chance to run into a USBError
1007
993
# or report the failure of halt operation.
@@ -1015,27 +1001,32 @@ def timer_handler():
1015
1001
try :
1016
1002
while delayed_halt .is_alive ():
1017
1003
if ctrl_error .is_set ():
1018
- raise_unconditionally (lineno (), 'Halting endpoint {0.bEndpointAddress:#04x} failed'
1019
- .format (ep_to_halt ))
1004
+ break
1020
1005
try :
1021
1006
loopback_ep_test (ep_out , ep_in , ep_out .wMaxPacketSize )
1022
1007
except usb .core .USBError as err :
1008
+ if ctrl_error .is_set ():
1009
+ break
1023
1010
try :
1024
1011
ep_status = usb .control .get_status (dev , ep_to_halt )
1025
1012
except usb .core .USBError as err :
1013
+ if ctrl_error .is_set ():
1014
+ break
1026
1015
raise_unconditionally (lineno (), 'Unable to get endpoint status ({!r}).' .format (err ))
1027
1016
if ep_status == 1 :
1028
1017
# OK, got USBError because of endpoint halt
1029
1018
return
1030
1019
else :
1031
1020
raise_unconditionally (lineno (), 'Unexpected error ({!r}).' .format (err ))
1032
- except :
1033
- raise
1021
+ if ctrl_error .is_set ():
1022
+ raise_unconditionally (lineno (), 'Halting endpoint {0.bEndpointAddress:#04x} failed'
1023
+ .format (ep_to_halt ))
1034
1024
finally :
1035
1025
# Always wait for the Timer thread created above.
1036
1026
delayed_halt .join ()
1037
- ep_out .clear_halt ()
1038
- ep_in .clear_halt ()
1027
+ if not ctrl_error .is_set ():
1028
+ ep_out .clear_halt ()
1029
+ ep_in .clear_halt ()
1039
1030
raise_unconditionally (lineno (), 'Halting endpoint {0.bEndpointAddress:#04x}'
1040
1031
' during transmission did not raise USBError.'
1041
1032
.format (ep_to_halt ))
0 commit comments