- tests for AT90USB1287 and CY7C6813A added
git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@192 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
21
java/test/ch/ntb/usb/AT90USB1287.java
Normal file
21
java/test/ch/ntb/usb/AT90USB1287.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package ch.ntb.usb;
|
||||||
|
|
||||||
|
public class AT90USB1287 extends AbstractDeviceInfo {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initValues() {
|
||||||
|
setIdVendor((short) 0x8235);
|
||||||
|
setIdProduct((short) 0x0222);
|
||||||
|
setTimeout(2000);
|
||||||
|
setConfiguration(1);
|
||||||
|
setInterface(0);
|
||||||
|
setAltinterface(-1);
|
||||||
|
setOUT_EP_BULK(0x01);
|
||||||
|
setIN_EP_BULK(0x82);
|
||||||
|
setOUT_EP_INT(0x03);
|
||||||
|
setIN_EP_INT(0x84);
|
||||||
|
setSleepTimeout(2000);
|
||||||
|
setMaxDataSize(USB.FULLSPEED_MAX_BULK_PACKET_SIZE);
|
||||||
|
setMode(WriteMode.Bulk);
|
||||||
|
}
|
||||||
|
}
|
||||||
145
java/test/ch/ntb/usb/AbstractDeviceInfo.java
Normal file
145
java/test/ch/ntb/usb/AbstractDeviceInfo.java
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
package ch.ntb.usb;
|
||||||
|
|
||||||
|
public abstract class AbstractDeviceInfo {
|
||||||
|
|
||||||
|
private short IdVendor;
|
||||||
|
|
||||||
|
private short IdProduct;
|
||||||
|
|
||||||
|
private int Timeout;
|
||||||
|
|
||||||
|
private int Configuration;
|
||||||
|
|
||||||
|
private int Interface;
|
||||||
|
|
||||||
|
private int Altinterface;
|
||||||
|
|
||||||
|
private int OUT_EP_BULK;
|
||||||
|
|
||||||
|
private int IN_EP_BULK;
|
||||||
|
|
||||||
|
private int OUT_EP_INT;
|
||||||
|
|
||||||
|
private int IN_EP_INT;
|
||||||
|
|
||||||
|
private int SleepTimeout;
|
||||||
|
|
||||||
|
private int MaxDataSize;
|
||||||
|
|
||||||
|
private WriteMode mode;
|
||||||
|
|
||||||
|
public static enum WriteMode {
|
||||||
|
Bulk, Interrupt
|
||||||
|
}
|
||||||
|
|
||||||
|
public AbstractDeviceInfo() {
|
||||||
|
initValues();
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract public void initValues();
|
||||||
|
|
||||||
|
public int getAltinterface() {
|
||||||
|
return Altinterface;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getConfiguration() {
|
||||||
|
return Configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public short getIdProduct() {
|
||||||
|
return IdProduct;
|
||||||
|
}
|
||||||
|
|
||||||
|
public short getIdVendor() {
|
||||||
|
return IdVendor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIN_EP_BULK() {
|
||||||
|
return IN_EP_BULK;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIN_EP_INT() {
|
||||||
|
return IN_EP_INT;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getInterface() {
|
||||||
|
return Interface;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMaxDataSize() {
|
||||||
|
return MaxDataSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOUT_EP_BULK() {
|
||||||
|
return OUT_EP_BULK;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOUT_EP_INT() {
|
||||||
|
return OUT_EP_INT;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSleepTimeout() {
|
||||||
|
return SleepTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getTimeout() {
|
||||||
|
return Timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAltinterface(int altinterface) {
|
||||||
|
Altinterface = altinterface;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConfiguration(int configuration) {
|
||||||
|
Configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdProduct(short idProduct) {
|
||||||
|
IdProduct = idProduct;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdVendor(short idVendor) {
|
||||||
|
IdVendor = idVendor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIN_EP_BULK(int in_ep_bulk) {
|
||||||
|
IN_EP_BULK = in_ep_bulk;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIN_EP_INT(int in_ep_int) {
|
||||||
|
IN_EP_INT = in_ep_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInterface(int interface1) {
|
||||||
|
Interface = interface1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxDataSize(int maxDataSize) {
|
||||||
|
MaxDataSize = maxDataSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOUT_EP_BULK(int out_ep_bulk) {
|
||||||
|
OUT_EP_BULK = out_ep_bulk;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOUT_EP_INT(int out_ep_int) {
|
||||||
|
OUT_EP_INT = out_ep_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSleepTimeout(int sleepTimeout) {
|
||||||
|
SleepTimeout = sleepTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimeout(int timeout) {
|
||||||
|
Timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WriteMode getMode() {
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMode(WriteMode mode) {
|
||||||
|
this.mode = mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
21
java/test/ch/ntb/usb/CY7C68013A.java
Normal file
21
java/test/ch/ntb/usb/CY7C68013A.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package ch.ntb.usb;
|
||||||
|
|
||||||
|
public class CY7C68013A extends AbstractDeviceInfo {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initValues() {
|
||||||
|
setIdVendor((short) 0x8235);
|
||||||
|
setIdProduct((short) 0x0222);
|
||||||
|
setTimeout(2000);
|
||||||
|
setConfiguration(1);
|
||||||
|
setInterface(0);
|
||||||
|
setAltinterface(-1);
|
||||||
|
setOUT_EP_INT(0x02);
|
||||||
|
setIN_EP_INT(0x86);
|
||||||
|
setOUT_EP_BULK(0x04);
|
||||||
|
setIN_EP_BULK(0x88);
|
||||||
|
setSleepTimeout(2000);
|
||||||
|
setMaxDataSize(USB.HIGHSPEED_MAX_BULK_PACKET_SIZE);
|
||||||
|
setMode(WriteMode.Bulk);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,198 +7,203 @@ import org.junit.AfterClass;
|
|||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import ch.ntb.usb.AbstractDeviceInfo.WriteMode;
|
||||||
|
|
||||||
public class DeviceTest {
|
public class DeviceTest {
|
||||||
|
|
||||||
private static final short IdVendor = (short) 0x8235;
|
private static AbstractDeviceInfo devinfo;
|
||||||
|
|
||||||
private static final short IdProduct = 0x0222;
|
|
||||||
|
|
||||||
private static final int Timeout = 20000;
|
|
||||||
|
|
||||||
private static final int Configuration = 1;
|
|
||||||
|
|
||||||
private static final int Interface = 0;
|
|
||||||
|
|
||||||
private static final int Altinterface = 0;
|
|
||||||
|
|
||||||
private static final int OUT_EP_BULK = 0x01;
|
|
||||||
|
|
||||||
private static int IN_EP_BULK = 0x82;
|
|
||||||
|
|
||||||
private static final int OUT_EP_INT = 0x03;
|
|
||||||
|
|
||||||
private static final int IN_EP_INT = 0x84;
|
|
||||||
|
|
||||||
private static final int Sleep_Timeout = 2000;
|
|
||||||
|
|
||||||
private static byte[] testData;
|
private static byte[] testData;
|
||||||
|
|
||||||
private static byte[] readData;
|
private static byte[] readData;
|
||||||
|
|
||||||
private static enum WriteMode {
|
|
||||||
Bulk, Interrupt
|
|
||||||
}
|
|
||||||
|
|
||||||
private static WriteMode mode;
|
|
||||||
|
|
||||||
private static Device dev;
|
private static Device dev;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUp() throws Exception {
|
public static void setUp() throws Exception {
|
||||||
|
// select the device
|
||||||
|
devinfo = new AT90USB1287();
|
||||||
|
// devinfo = new CY7C68013A();
|
||||||
// setup test data
|
// setup test data
|
||||||
testData = new byte[USB.FULLSPEED_MAX_BULK_PACKET_SIZE];
|
testData = new byte[devinfo.getMaxDataSize()];
|
||||||
readData = new byte[testData.length];
|
readData = new byte[testData.length];
|
||||||
// initialise the device
|
// initialise the device
|
||||||
dev = USB.getDevice(IdVendor, IdProduct);
|
LibusbWin.usb_set_debug(255);
|
||||||
dev.open(Configuration, Interface, Altinterface);
|
dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct());
|
||||||
dev.reset();
|
|
||||||
timeout();
|
|
||||||
mode = WriteMode.Bulk;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=USBException.class)
|
@Test
|
||||||
|
public void initalReset() throws Exception {
|
||||||
|
doOpen();
|
||||||
|
dev.reset();
|
||||||
|
timeout();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = USBException.class)
|
||||||
public void testClose() throws Exception {
|
public void testClose() throws Exception {
|
||||||
dev.open(Configuration, Interface, Altinterface);
|
doOpen();
|
||||||
dev.close();
|
doClose();
|
||||||
// this call must throw an exception, because the device is closed
|
// this call must throw an exception, because the device is closed
|
||||||
dev.writeBulk(OUT_EP_BULK, testData, testData.length, Timeout, false);
|
dev.writeBulk(devinfo.getOUT_EP_BULK(), testData, testData.length,
|
||||||
|
devinfo.getTimeout(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = USBException.class)
|
||||||
public void testReset() throws Exception {
|
public void testReset1() throws Exception {
|
||||||
dev.open(Configuration, Interface, Altinterface);
|
doOpen();
|
||||||
dev.reset();
|
dev.reset();
|
||||||
try {
|
|
||||||
// this call must throw an exception, because the device is closed
|
|
||||||
dev.writeBulk(OUT_EP_BULK, testData, testData.length, Timeout,
|
|
||||||
false);
|
|
||||||
fail();
|
|
||||||
} catch (Exception e) {
|
|
||||||
// must throw an exception
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
// this call must throw an exception, because the device can't be
|
|
||||||
// closed
|
|
||||||
dev.close();
|
|
||||||
fail();
|
|
||||||
} catch (Exception e) {
|
|
||||||
// must throw an exception
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBulk() throws Exception {
|
|
||||||
mode = WriteMode.Bulk;
|
|
||||||
timeout();
|
timeout();
|
||||||
testOpenWriteClose();
|
// this call must throw an exception, because the device is closed
|
||||||
|
dev.writeBulk(devinfo.getOUT_EP_BULK(), testData, testData.length,
|
||||||
|
devinfo.getTimeout(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = USBException.class)
|
||||||
|
public void testReset2() throws Exception {
|
||||||
|
doOpen();
|
||||||
|
dev.reset();
|
||||||
|
timeout();
|
||||||
|
// this call must throw an exception, because the device can't be closed
|
||||||
|
doClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBulkMultiple() throws Exception {
|
public void bulkWriteRead() throws Exception {
|
||||||
final int NumberOfIterations = 20;
|
devinfo.setMode(WriteMode.Bulk);
|
||||||
|
doOpenWriteReadClose();
|
||||||
try {
|
}
|
||||||
timeout();
|
|
||||||
dev.open(Configuration, Interface, Altinterface);
|
@Test
|
||||||
for (int i = 0; i < NumberOfIterations; i++) {
|
public void interruptWriteRead() throws Exception {
|
||||||
initTestData();
|
devinfo.setMode(WriteMode.Interrupt);
|
||||||
dev.writeBulk(OUT_EP_BULK, testData, testData.length, Timeout, true);
|
doOpenWriteReadClose();
|
||||||
dev.readBulk(IN_EP_BULK, readData, readData.length, Timeout, true);
|
}
|
||||||
compare(testData, readData);
|
|
||||||
}
|
@Test
|
||||||
dev.close();
|
public void bulkWriteReadMultiple() throws Exception {
|
||||||
} catch (USBException e) {
|
final int NumberOfIterations = 100;
|
||||||
dev.close();
|
|
||||||
throw new USBException(e.getMessage());
|
devinfo.setMode(WriteMode.Bulk);
|
||||||
|
doOpen();
|
||||||
|
for (int i = 0; i < NumberOfIterations; i++) {
|
||||||
|
initTestData();
|
||||||
|
doWriteRead();
|
||||||
|
compare(testData, readData);
|
||||||
|
}
|
||||||
|
doClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void multipleOpenCloseWithBulkWrite() throws Exception {
|
||||||
|
devinfo.setMode(WriteMode.Bulk);
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
doOpen();
|
||||||
|
doClose();
|
||||||
|
}
|
||||||
|
doOpenWriteReadClose();
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
doOpen();
|
||||||
|
doWriteRead();
|
||||||
|
doClose();
|
||||||
|
}
|
||||||
|
doOpenWriteReadClose();
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
doOpen();
|
||||||
|
doClose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInterrupt() throws Exception {
|
public void bulkAndInterrupt() throws Exception {
|
||||||
mode = WriteMode.Interrupt;
|
doOpen();
|
||||||
testOpenWriteClose();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBulkAndInterrupt() throws Exception {
|
|
||||||
timeout();
|
|
||||||
dev.open(Configuration, Interface, Altinterface);
|
|
||||||
// BULK
|
// BULK
|
||||||
initTestData();
|
devinfo.setMode(WriteMode.Bulk);
|
||||||
dev.writeBulk(OUT_EP_BULK, testData, testData.length, Timeout, false);
|
doWriteRead();
|
||||||
dev.readBulk(IN_EP_BULK, readData, readData.length, Timeout, false);
|
|
||||||
compare(testData, readData);
|
|
||||||
// INTERRUPT
|
// INTERRUPT
|
||||||
initTestData();
|
devinfo.setMode(WriteMode.Interrupt);
|
||||||
dev.writeInterrupt(OUT_EP_INT, testData, testData.length, Timeout,
|
doWriteRead();
|
||||||
false);
|
doClose();
|
||||||
// TODO change to readInterrupt
|
|
||||||
dev.readBulk(IN_EP_INT, readData, readData.length, Timeout, false);
|
|
||||||
compare(testData, readData);
|
|
||||||
dev.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBulkAndInterruptMultiple() {
|
public void bulkAndInterruptMultiple() throws Exception {
|
||||||
mode = WriteMode.Bulk;
|
for (int i = 0; i < 20; i++) {
|
||||||
timeout();
|
devinfo.setMode(WriteMode.Bulk);
|
||||||
|
doOpenWriteReadClose();
|
||||||
|
devinfo.setMode(WriteMode.Interrupt);
|
||||||
|
doOpenWriteReadClose();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetIdProduct() {
|
public void testGetIdProduct() {
|
||||||
Assert.assertTrue(dev.getIdProduct() == IdProduct);
|
Assert.assertEquals(dev.getIdProduct(), devinfo.getIdProduct());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetIdVendor() {
|
public void testGetIdVendor() {
|
||||||
Assert.assertTrue(dev.getIdVendor() == IdVendor);
|
Assert.assertEquals(dev.getIdVendor(), devinfo.getIdVendor());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAltinterface() {
|
public void testGetAltinterface() {
|
||||||
Assert.assertTrue(dev.getAltinterface() == Altinterface);
|
Assert.assertEquals(dev.getAltinterface(), devinfo.getAltinterface());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetConfiguration() {
|
public void testGetConfiguration() {
|
||||||
Assert.assertTrue(dev.getConfiguration() == Configuration);
|
Assert.assertEquals(dev.getConfiguration(), devinfo.getConfiguration());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetInterface() {
|
public void testGetInterface() {
|
||||||
Assert.assertTrue(dev.getInterface() == Interface);
|
Assert.assertEquals(dev.getInterface(), devinfo.getInterface());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetMaxPacketSize() {
|
public void testGetMaxPacketSize() throws USBException {
|
||||||
Assert
|
doOpen();
|
||||||
.assertTrue(dev.getMaxPacketSize() == USB.FULLSPEED_MAX_BULK_PACKET_SIZE);
|
Assert.assertEquals(dev.getMaxPacketSize(), devinfo.getMaxDataSize());
|
||||||
|
doClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void tearDown() {
|
public static void tearDown() throws Exception {
|
||||||
try {
|
if (dev.isOpen()) {
|
||||||
dev.close();
|
dev.close();
|
||||||
} catch (USBException e) {
|
|
||||||
// ignore Exceptions
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testOpenWriteClose() throws Exception {
|
private void doOpen() throws USBException {
|
||||||
dev.open(Configuration, Interface, Altinterface);
|
dev.open(devinfo.getConfiguration(), devinfo.getInterface(), devinfo
|
||||||
|
.getAltinterface());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doClose() throws USBException {
|
||||||
|
dev.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doOpenWriteReadClose() throws Exception {
|
||||||
|
doOpen();
|
||||||
|
doWriteRead();
|
||||||
|
compare(testData, readData);
|
||||||
|
doClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doWriteRead() throws Exception {
|
||||||
initTestData();
|
initTestData();
|
||||||
if (mode == WriteMode.Bulk) {
|
if (devinfo.getMode().equals(WriteMode.Bulk)) {
|
||||||
dev.writeBulk(OUT_EP_BULK, testData, testData.length, Timeout,
|
dev.writeBulk(devinfo.getOUT_EP_BULK(), testData, testData.length,
|
||||||
false);
|
devinfo.getTimeout(), false);
|
||||||
dev.readBulk(IN_EP_BULK, readData, readData.length, Timeout, false);
|
dev.readBulk(devinfo.getIN_EP_BULK(), readData, readData.length,
|
||||||
} else if (mode == WriteMode.Interrupt) {
|
devinfo.getTimeout(), false);
|
||||||
dev.writeInterrupt(OUT_EP_INT, testData, testData.length, Timeout,
|
} else if (devinfo.getMode().equals(WriteMode.Interrupt)) {
|
||||||
false);
|
dev.writeInterrupt(devinfo.getOUT_EP_INT(), testData,
|
||||||
// TODO change to readInterrupt
|
testData.length, devinfo.getTimeout(), false);
|
||||||
dev.readBulk(IN_EP_INT, readData, readData.length, Timeout, false);
|
dev.readInterrupt(devinfo.getIN_EP_INT(), readData,
|
||||||
|
readData.length, devinfo.getTimeout(), false);
|
||||||
}
|
}
|
||||||
compare(testData, readData);
|
compare(testData, readData);
|
||||||
dev.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void compare(byte[] d1, byte[] d2) {
|
private static void compare(byte[] d1, byte[] d2) {
|
||||||
@@ -211,7 +216,7 @@ public class DeviceTest {
|
|||||||
|
|
||||||
private static void timeout() {
|
private static void timeout() {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(Sleep_Timeout);
|
Thread.sleep(devinfo.getSleepTimeout());
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -223,5 +228,4 @@ public class DeviceTest {
|
|||||||
readData[i] = 0;
|
readData[i] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user