def test_hid(test_info, hid): """Smoke test of the HID endpoint""" hid.set_idle() report = hid.get_descriptor(hid.DESC_TYPE_REPORT, 0) test_info.info("Report descriptor: %s" % report) # Send CMSIS-DAP vendor command to get the serial number data = bytearray(64) data[0] = 0x80 hid.set_report(data) resp = hid.get_report(64) length = resp[1] test_info.info("CMSIS-DAP response: %s" % bytearray(resp[1:1 + length]).decode("utf-8"))
def test_msd(test_info, msd): """MSD endpoint tests""" # Simple read mbr = msd.scsi_read10(0, 1) test_info.info("MBR[0:16]: %s" % mbr[0:16]) # Test FAT filesystem fat = Fat(msd) print(fat.mbr) # Grab entries in the root directory root_dir = fat.root_dir for entry in root_dir: if entry["DIR_Name"][0] != "\0": print(entry) # Trigger a remount dir_idx = root_dir.find_free_entry_index() root_dir[dir_idx]["DIR_Name"] = "REFRESH ACT" root_dir_data = root_dir.pack() msd.scsi_write10(root_dir.sector, root_dir_data) test_info.info("Added file to root directory") start = time.time() while time.time() - start < DISMOUNT_TIME_S: try: msd.scsi_read10(0, 1) except msd.SCSIError: test_info.info("Dismount detected") break else: test_info.failure("Device failed to dismount") start = time.time() while time.time() - start < DISMOUNT_TIME_S: try: msd.scsi_read10(0, 1) test_info.info("Mount detected") break except msd.SCSIError: pass else: test_info.failure("Device failed to mount")
def test_cdc(test_info, cdc): """Smoke test of the CDC endpoint""" cdc.set_line_coding(115200) baud, fmt, parity, databits = cdc.get_line_coding() test_info.info("Baud %i, fmt %i, parity %i, databits %i" % (baud, fmt, parity, databits)) cdc.send_break(cdc.SEND_BREAK_ON) cdc.send_break(cdc.SEND_BREAK_OFF) data = cdc.read(1024) test_info.info("Serial port data: %s" % bytearray(data)) cdc.write("Hello world") data = cdc.read(1024) test_info.info("Serial port data2: %s" % bytearray(data))
def test_control(test_info, dev, cdc, hid, msd): """Test for the control endpoint""" test_info.info("testing control transfer with size a multiple of 256") request_type = 0x80 request = 0x06 # Get descriptor value = 0x200 # Configuration descriptor index = 0 # Always 0 for this request resp = dev.ctrl_transfer(request_type, request, value, index, 256) assert len(resp) > 0 test_info.info("testing control commands") # Test various patterns of control transfers # # Some devices have had problems with back-to-back # control transfers. Intentionally send these sequences # to make sure they are properly handled. for _ in range(100): # Control transfer with a data in stage cdc.get_line_coding() for _ in range(100): # Control transfer with a data out stage followed # by a control transfer with a data in stage cdc.set_line_coding(115200) cdc.get_line_coding() for _ in range(100): # Control transfer with a data out stage cdc.set_line_coding(115200) test_info.info("testing endpoint clearing") cdc.ep_data_out.clear_halt() cdc.ep_data_out.write('') # DATA0 cdc.ep_data_out.clear_halt() cdc.ep_data_out.write('') # DATA0 cdc.ep_data_out.clear_halt() cdc.ep_data_out.write('') # DATA 0 cdc.ep_data_out.write('') # DATA 1 cdc.ep_data_out.clear_halt() cdc.ep_data_out.write('') # DATA 0
def test_msd_stall(test_info, msd): """Test stalls coming at various times in the middle of MSD xfers""" fat = Fat(msd) root_dir = fat.root_dir dir_idx = root_dir.find_free_entry_index() root_dir[dir_idx]["DIR_Name"] = "REFRESH ACT" root_dir_data = root_dir.pack() # Test that a write fails if media is removed after the CBW # stage but before the CSW stage msd.scsi_write10(root_dir.sector, root_dir_data) msd.delay_cbw_to_data = 1.0 retval = msd.CSW_STATUS_PASSED try: msd.scsi_write10(0, bytearray(512)) test_info.failure("Device failed to stall data stage") except msd.SCSIError as error: retval = error.value msd.delay_cbw_to_data = 0 # Make sure device still works as expected time.sleep(3) msd.scsi_read10(0, 1) msd.scsi_write10(0, bytearray(512)) if retval == msd.CSW_STATUS_FAILED: test_info.info("Test CBW,Stall,Data OUT - Pass") else: test_info.failure("Device returned wrong status") # Test that a write succeeds even if media is removed # after the OUT stage but before the CSW stage msd.scsi_write10(root_dir.sector, root_dir_data) msd.delay_data_to_csw = 1.0 msd.scsi_write10(0, bytearray(512)) msd.delay_data_to_csw = 0 # Make sure device still works as expected time.sleep(3) msd.scsi_read10(0, 1) msd.scsi_write10(0, bytearray(512)) test_info.info("Test DATA OUT,Stall,CSW - Pass") # Test that a read succeeds even if media is removed # after the IN stage but before the CSW stage msd.scsi_write10(root_dir.sector, root_dir_data) msd.delay_data_to_csw = 1.0 resp = msd.scsi_read10(0, 1) assert len(resp) == 512 msd.delay_data_to_csw = 0 # Make sure device still works as expected time.sleep(3) msd.scsi_read10(0, 1) msd.scsi_write10(0, bytearray(512)) test_info.info("Test DATA IN,Stall,CSW - Pass") # Test that a test unit ready succeeds even if media is removed # after the CBW stage but before the CSW stage msd.scsi_write10(root_dir.sector, root_dir_data) msd.delay_data_to_csw = 1.0 resp = msd.scsi_test_unit_ready() msd.delay_data_to_csw = 0 # Make sure device still works as expected time.sleep(3) msd.scsi_read10(0, 1) msd.scsi_write10(0, bytearray(512)) if resp == msd.CSW_STATUS_PASSED: test_info.info("Test CBW,Stall,CSW - Pass") else: test_info.failure("Test CBW,Stall,CSW - Failed") # Test that a test unit ready succeeds even if media is removed # after the CBW stage but before the CSW stage msd.scsi_write10(root_dir.sector, root_dir_data) time.sleep(1.0) resp = msd.scsi_test_unit_ready() # Make sure device still works as expected time.sleep(3) msd.scsi_read10(0, 1) msd.scsi_write10(0, bytearray(512)) if resp == msd.CSW_STATUS_FAILED: test_info.info("Test CBW,Stall,CSW - Pass") else: test_info.failure("Test CBW,Stall,CSW - Failed")