@@ -85,13 +85,9 @@ def get_interface(dev, interface, alternate=0):
8585FEATURE_ENDPOINT_HALT = 0
8686FEATURE_DEVICE_REMOTE_WAKEUP = 1
8787
88-
89-
9088DEVICE_QUALIFIER_DESC_SIZE = 10
91-
9289DESC_TYPE_DEVICE_QUALIFIER = 0x06
9390
94-
9591DEVICE_DESC_SIZE = 18
9692device_descriptor_parser = struct .Struct ('BBHBBBBHHHBBBB' )
9793device_descriptor_keys = ['bLength' , 'bDescriptorType' , 'bcdUSB' , 'bDeviceClass' ,
@@ -352,11 +348,9 @@ def setup(self):
352348
353349 self .register_callback ('reset_support' , self ._callback_reset_support )
354350
355-
356351 def result (self ):
357352 return self .__result
358353
359-
360354 def teardown (self ):
361355 pass
362356
@@ -415,10 +409,9 @@ def get_set_configuration_test(dev, log):
415409 # check if dafault(1) configuration set
416410 try :
417411 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.' )
419413 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 ()))
422415
423416 cfg = dev .get_active_configuration ()
424417 for intf in cfg :
@@ -428,35 +421,31 @@ def get_set_configuration_test(dev, log):
428421 try :
429422 ret = dev .set_configuration (0 )
430423 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 ()))
433425
434426 # check if deconfigured
435427 try :
436428 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.' )
438430 print ("device deconfigured - OK" )
439431 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 ()))
442433
443434 # for every configuration
444435 for cfg in dev :
445436 try :
446437 # set configuration
447438 ret = cfg .set ()
448439 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 ()))
451441
452442 # check if configured
453443 try :
454444 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.' )
456446 print ("configuration {} set - OK " .format (cfg .bConfigurationValue ))
457447 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 ()))
460449 # test control data transfer after configuration set
461450 control_data_test (dev , [64 , 256 ], log )
462451 print ("" ) # new line
@@ -477,7 +466,7 @@ def get_set_interface_test(dev, log):
477466 cfg .set ()
478467 # for every interface
479468 for intf in cfg :
480- intf .set_altsetting ();
469+ intf .set_altsetting ()
481470 altsett = usb .control .get_interface (dev , intf .bInterfaceNumber )
482471 raise_if_different (intf .bAlternateSetting , altsett , lineno (), text = 'Wrong alternate setting for interface {}' .format (intf .bInterfaceNumber ))
483472 print ("cfg({}) inteface {}.{} set - OK" .format (cfg .bConfigurationValue , intf .bInterfaceNumber , intf .bAlternateSetting ))
@@ -583,15 +572,13 @@ def set_clear_feature_test(dev, log):
583572 try :
584573 usb .control .set_feature (dev , FEATURE_ENDPOINT_HALT , ep )
585574 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 ()))
588576
589577 # check if endpoint was halted
590578 try :
591579 ret = usb .control .get_status (dev , ep )
592580 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 ()))
595582 if (ret != 1 ):
596583 raise_unconditionally (lineno (), "endpoint {} was not halted" .format (ep .bEndpointAddress ))
597584 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):
600587 try :
601588 usb .control .clear_feature (dev , FEATURE_ENDPOINT_HALT , ep )
602589 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 ()))
605591
606592 # check if endpoint was unhalted
607593 ret = usb .control .get_status (dev , ep )
@@ -635,31 +621,31 @@ def get_descriptor_test(dev, vendor_id, product_id, log):
635621 try :
636622 ret = get_descriptor (dev , (DESC_TYPE_DEVICE << 8 ) | (0 << 0 ), 0 , DEVICE_DESC_SIZE )
637623 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. ' )
641627 except usb .core .USBError :
642628 raise_unconditionally (lineno (), "Requesting device descriptor failed" )
643629
644630 # configuration descriptor
645631 try :
646632 ret = get_descriptor (dev , (DESC_TYPE_CONFIG << 8 ) | (0 << 0 ), 0 , CONFIGURATION_DESC_SIZE )
647633 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. ' )
649635 except usb .core .USBError :
650636 raise_unconditionally (lineno (), "Requesting configuration descriptor failed" )
651637
652638 # interface descriptor
653639 try :
654640 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. " )
656642 except usb .core .USBError :
657643 log ("interface descriptor is not directly accessible - OK" )
658644
659645 # endpoint descriptor
660646 try :
661647 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. " )
663649 except usb .core .USBError :
664650 log ("endpoint descriptor is not directly accessible - OK" )
665651 print ("" ) # new line
@@ -687,7 +673,7 @@ def set_descriptor_test(dev, log):
687673 data = bytearray (DEVICE_DESC_SIZE ) # Descriptor data
688674 try :
689675 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" )
691677 except usb .core .USBError :
692678 log ("SET_DESCRIPTOR is unsupported - OK" )
693679 print ("" ) # new line
@@ -715,7 +701,7 @@ def synch_frame_test(dev, log):
715701 ret = ret [0 ] | (ret [1 ] << 8 )
716702 log ("synch frame ret: %d" % (ret ))
717703 except usb .core .USBError :
718- raise_unconditionally (lineno (), "SYNCH_FRAME failed" )
704+ raise_unconditionally (lineno (), "SYNCH_FRAME request failed" )
719705 print ("" ) # new line
720706
721707
@@ -847,7 +833,7 @@ def control_data_test(dev, sizes_list, log):
847833 ret = dev .ctrl_transfer (request_type , request , value , index , length , 5000 )
848834 raise_if_different (i , len (ret ), lineno (), "send/receive data is the wrong size" )
849835 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 " )
851837 except usb .core .USBError :
852838 raise_unconditionally (lineno (), "VENDOR_TEST_CTRL_IN_SIZES failed" )
853839 count += 1
@@ -1001,7 +987,7 @@ def timer_handler():
1001987 raise RuntimeError ('Invalid endpoint status after halt operation' )
1002988 except Exception as err :
1003989 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 ))
1005991 # Whether the halt operation was successful or not,
1006992 # wait a bit so the main thread has a chance to run into a USBError
1007993 # or report the failure of halt operation.
@@ -1015,27 +1001,32 @@ def timer_handler():
10151001 try :
10161002 while delayed_halt .is_alive ():
10171003 if ctrl_error .is_set ():
1018- raise_unconditionally (lineno (), 'Halting endpoint {0.bEndpointAddress:#04x} failed'
1019- .format (ep_to_halt ))
1004+ break
10201005 try :
10211006 loopback_ep_test (ep_out , ep_in , ep_out .wMaxPacketSize )
10221007 except usb .core .USBError as err :
1008+ if ctrl_error .is_set ():
1009+ break
10231010 try :
10241011 ep_status = usb .control .get_status (dev , ep_to_halt )
10251012 except usb .core .USBError as err :
1013+ if ctrl_error .is_set ():
1014+ break
10261015 raise_unconditionally (lineno (), 'Unable to get endpoint status ({!r}).' .format (err ))
10271016 if ep_status == 1 :
10281017 # OK, got USBError because of endpoint halt
10291018 return
10301019 else :
10311020 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 ))
10341024 finally :
10351025 # Always wait for the Timer thread created above.
10361026 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 ()
10391030 raise_unconditionally (lineno (), 'Halting endpoint {0.bEndpointAddress:#04x}'
10401031 ' during transmission did not raise USBError.'
10411032 .format (ep_to_halt ))
0 commit comments