- Main Page
- Related Pages
- Data Structures
- Files

- File List
- Globals

# test/usb-tester/usbtest/testbulk.py

    00001 import unittest
    00002 import usbtest
    00003 import usb
    00004
    00005 # Must keep this in sync with the driver
    00006 BUF_SIZE        = 1024
    00007
    00008 # Number of times to repeat each loopback test
    00009 REPEAT          = 1
    00010
    00011 # Standard USB definitions not provided by PyUSB
    00012 ENDPOINT_HALT   = 0
    00013
    00014 TIMEOUT         = 1000
    00015
    00016 class TestCase(unittest.TestCase):
    00017         "Test bulk data transfers"
    00018
    00019         def setUp(self):
    00020                 for config in usbtest.dev.configurations:
    00021                         for iface in config.interfaces:
    00022                                 in_ep = None
    00023                                 out_ep = None
    00024                                 for ep in iface[0].endpoints:
    00025                                         if ep.type == usb.ENDPOINT_TYPE_BULK:
    00026                                                 if ep.address & usb.ENDPOINT_IN:
    00027                                                         in_ep = ep
    00028                                                 else:
    00029                                                         out_ep = ep
    00030                                 if in_ep and out_ep:
    00031                                         self.bulk_iface = iface[0]
    00032                                         self.in_ep = in_ep
    00033                                         self.out_ep = out_ep
    00034                                 break
    00035                         if self.bulk_iface:
    00036                                 break;
    00037
    00038                 self.dev = usbtest.dev.open()
    00039                 self.dev.claimInterface(self.bulk_iface)
    00040
    00041         def tearDown(self):
    00042                 self.dev.releaseInterface()
    00043                 del self.dev
    00044
    00045         def setHalt(self, ep):
    00046                 # What, no setHalt() method in PyUSB?
    00047                 self.dev.controlMsg(usb.ENDPOINT_OUT
    00048                                 | usb.TYPE_STANDARD | usb.RECIP_ENDPOINT,
    00049                                 usb.REQ_SET_FEATURE, 0, ENDPOINT_HALT,
    00050                                 ep.address, timeout=TIMEOUT)
    00051
    00052         def getEpStatus(self, ep):
    00053                 low, high = self.dev.controlMsg(usb.ENDPOINT_IN
    00054                                 | usb.TYPE_STANDARD | usb.RECIP_ENDPOINT,
    00055                                 usb.REQ_GET_STATUS, 2, 0, ep.address,
    00056                                 timeout=TIMEOUT)
    00057                 return (high << 8) | low
    00058
    00059         def doLoopbackTest(self, size):
    00060                 out_data = []
    00061                 for i in range(0, size):
    00062                         out_data.append(i & 0xff)
    00063                 written = self.dev.bulkWrite(self.out_ep.address, out_data,
    00064                                 TIMEOUT)
    00065                 self.failUnlessEqual(written, size)
    00066                 if (size < BUF_SIZE and size % self.out_ep.maxPacketSize == 0):
    00067                         self.dev.bulkWrite(self.out_ep.address, [], TIMEOUT)
    00068
    00069                 in_data = self.dev.bulkRead(self.in_ep.address, size, TIMEOUT)
    00070                 self.failUnlessEqual(len(in_data), size)
    00071
    00072                 for i in range(0, size):
    00073                         if in_data[i] != i & 0xff:
    00074                                 self.fail("mismatch: sent %02x, received %02x"
    00075                                                 % (i & 0xff, in_data[i]))
    00076
    00077         def testWholeBuffer(self):
    00078                 for i in range(0, REPEAT):
    00079                         self.doLoopbackTest(BUF_SIZE)
    00080
    00081         def testTwoBuffers(self):
    00082                 for i in range(0, REPEAT):
    00083                         self.doLoopbackTest(2 * BUF_SIZE)
    00084
    00085         def testShort(self):
    00086                 for i in range(0, REPEAT):
    00087                         for size in [1, 63, 64, 65, 511, 512, 513]:
    00088                                 self.doLoopbackTest(size)
    00089
    00090         def testStallOut(self):
    00091                 self.setHalt(self.out_ep)
    00092                 status = self.getEpStatus(self.out_ep)
    00093                 self.failUnless(status & (1 << ENDPOINT_HALT))
    00094                 written = self.dev.bulkWrite(self.out_ep.address, 511 * [0],
    00095                                 TIMEOUT)
    00096
    00097                 # This appears to be broken on full-speed. PyUSB
    00098                 # returns 64, but the USB analyzer shows that even
    00099                 # though 64 bytes were indeed sent, the device
    00100                 # responded with a STALL handshake, so they shouldn't
    00101                 # be counted as successfully sent.
    00102                 #
    00103                 # self.failUnlessEqual(written, 0)
    00104
    00105                 self.dev.clearHalt(self.out_ep.address)
    00106                 status = self.getEpStatus(self.out_ep)
    00107                 self.failIf(status & (1 << ENDPOINT_HALT))
    00108                 self.doLoopbackTest(511)
    00109
    00110         def testStallIn(self):
    00111                 self.setHalt(self.in_ep)
    00112                 status = self.getEpStatus(self.in_ep)
    00113                 self.failUnless(status & (1 << ENDPOINT_HALT))
    00114                 self.dev.bulkWrite(self.out_ep.address, 511 * [0], TIMEOUT)
    00115                 data = self.dev.bulkRead(self.in_ep.address, 511, TIMEOUT)
    00116                 self.failUnlessEqual(len(data), 0)
    00117
    00118                 self.dev.clearHalt(self.in_ep.address)
    00119                 status = self.getEpStatus(self.in_ep)
    00120                 self.failIf(status & (1 << ENDPOINT_HALT))
    00121                 data = self.dev.bulkRead(self.in_ep.address, 511, TIMEOUT)
    00122                 self.failUnlessEqual(len(data), 511)
    00123
    00124         def testStallOutPartial(self):
    00125                 epsize = self.out_ep.maxPacketSize
    00126
    00127                 written = self.dev.bulkWrite(self.out_ep.address, epsize * [0],
    00128                                 TIMEOUT)
    00129                 self.failUnlessEqual(written, epsize)
    00130
    00131                 self.setHalt(self.out_ep)
    00132                 status = self.getEpStatus(self.out_ep)
    00133                 self.failUnless(status & (1 << ENDPOINT_HALT))
    00134
    00135                 written = self.dev.bulkWrite(self.out_ep.address,
    00136                                 (epsize - 1) * [0], TIMEOUT)
    00137
    00138                 # This appears to be broken on full-speed. PyUSB
    00139                 # returns 64, but the USB analyzer shows that even
    00140                 # though 64 bytes were indeed sent, the device
    00141                 # responded with a STALL handshake, so they shouldn't
    00142                 # be counted as successfully sent.
    00143                 #
    00144                 # self.failUnlessEqual(written, 0)
    00145
    00146                 self.dev.clearHalt(self.out_ep.address)
    00147                 status = self.getEpStatus(self.out_ep)
    00148                 self.failIf(status & (1 << ENDPOINT_HALT))
    00149
    00150                 written = self.dev.bulkWrite(self.out_ep.address,
    00151                                 (epsize - 1) * [0], TIMEOUT)
    00152                 self.failUnlessEqual(written, epsize - 1)
    00153
    00154                 data = self.dev.bulkRead(self.in_ep.address, epsize * 2 - 1,
    00155                                 TIMEOUT)
    00156                 self.failUnlessEqual(len(data), epsize * 2 - 1)
    00157
    00158         def testStallInPartial(self):
    00159                 epsize = self.in_ep.maxPacketSize
    00160
    00161                 written = self.dev.bulkWrite(self.out_ep.address,
    00162                                 (2 * epsize - 1) * [0],
    00163                                 TIMEOUT)
    00164                 self.failUnlessEqual(written, 2 * epsize - 1)
    00165
    00166                 data = self.dev.bulkRead(self.in_ep.address, epsize,
    00167                                 TIMEOUT)
    00168                 self.failUnlessEqual(len(data), epsize)
    00169
    00170                 self.setHalt(self.in_ep)
    00171                 status = self.getEpStatus(self.in_ep)
    00172                 self.failUnless(status & (1 << ENDPOINT_HALT))
    00173
    00174                 data = self.dev.bulkRead(self.in_ep.address, epsize - 1,
    00175                                 TIMEOUT)
    00176                 self.failUnlessEqual(len(data), 0)
    00177
    00178                 self.dev.clearHalt(self.in_ep.address)
    00179                 status = self.getEpStatus(self.in_ep)
    00180                 self.failIf(status & (1 << ENDPOINT_HALT))
    00181
    00182                 data = self.dev.bulkRead(self.in_ep.address, epsize - 1,
    00183                                 TIMEOUT)
    00184                 self.failUnlessEqual(len(data), epsize - 1)
    00185
    00186 def suite():
    00187         return unittest.makeSuite(TestCase, 'test')