diff --git a/java/build.xml b/java/build.xml index 2f40eef..20fe9d7 100644 --- a/java/build.xml +++ b/java/build.xml @@ -60,7 +60,7 @@ - + diff --git a/java/src/ch/ntb/usb/Device.java b/java/src/ch/ntb/usb/Device.java index e72f7dd..e31c406 100644 --- a/java/src/ch/ntb/usb/Device.java +++ b/java/src/ch/ntb/usb/Device.java @@ -170,12 +170,15 @@ public class Device { * endpoints are found in the descriptors an exception is thrown. * * @param configuration - * the configuration + * the configuration, see + * {@link Usb_Config_Descriptor#getBConfigurationValue()} * @param interface_ - * the interface + * the interface, see + * {@link Usb_Interface_Descriptor#getBInterfaceNumber()} * @param altinterface - * the alternate interface. If no alternate interface must be set - * -1 can be used. + * the alternate interface, see + * {@link Usb_Interface_Descriptor#getBAlternateSetting()}. If + * no alternate interface must be set -1 can be used. * @throws USBException */ public void open(int configuration, int interface_, int altinterface) diff --git a/java/src/ch/ntb/usb/USB.java b/java/src/ch/ntb/usb/USB.java index 6948f9a..5fb28a6 100644 --- a/java/src/ch/ntb/usb/USB.java +++ b/java/src/ch/ntb/usb/USB.java @@ -203,7 +203,7 @@ public class USB { * * @param idVendor * @param idProduct - * @return + * @return a newly created device or an already registered device */ public static Device getDevice(short idVendor, short idProduct) { return getDevice(idVendor, idProduct, null); @@ -225,7 +225,8 @@ public class USB { String filename) { for (Iterator iter = devices.iterator(); iter.hasNext();) { Device dev = iter.next(); - if (filename != null && filename.compareTo(dev.getFilename()) == 0 + if (filename != null && dev.getFilename() != null + && filename.compareTo(dev.getFilename()) == 0 && dev.getIdVendor() == idVendor && dev.getIdProduct() == idProduct) { return dev; diff --git a/java/src/ch/ntb/usb/testApp/AbstractDeviceInfo.java b/java/src/ch/ntb/usb/testApp/AbstractDeviceInfo.java index 94f7b7a..7dd7945 100644 --- a/java/src/ch/ntb/usb/testApp/AbstractDeviceInfo.java +++ b/java/src/ch/ntb/usb/testApp/AbstractDeviceInfo.java @@ -21,13 +21,13 @@ public abstract class AbstractDeviceInfo { private int altinterface; - private int outEPBulk; + private int outEPBulk = -1; - private int inEPBulk; + private int inEPBulk = -1; - private int outEPInt; + private int outEPInt = -1; - private int inEPInt; + private int inEPInt = -1; private int sleepTimeout; @@ -37,6 +37,12 @@ public abstract class AbstractDeviceInfo { private boolean compareData = true; + private String manufacturer = null; + + private String product = null; + + private String serialVersion = null; + public static enum TransferMode { Bulk, Interrupt } @@ -158,4 +164,28 @@ public abstract class AbstractDeviceInfo { public void setDoCompareData(boolean compareData) { this.compareData = compareData; } + + public String getManufacturer() { + return manufacturer; + } + + public void setManufacturer(String manufacturer) { + this.manufacturer = manufacturer; + } + + public String getProduct() { + return product; + } + + public void setProduct(String product) { + this.product = product; + } + + public String getSerialVersion() { + return serialVersion; + } + + public void setSerialVersion(String serialVersion) { + this.serialVersion = serialVersion; + } } diff --git a/java/test/ch/ntb/usb/test/AT90USB1287.java b/java/test/ch/ntb/usb/test/AT90USB1287.java index aa5d26c..c60e160 100644 --- a/java/test/ch/ntb/usb/test/AT90USB1287.java +++ b/java/test/ch/ntb/usb/test/AT90USB1287.java @@ -27,5 +27,8 @@ public class AT90USB1287 extends AbstractDeviceInfo { setSleepTimeout(2000); setMaxDataSize(USB.FULLSPEED_MAX_BULK_PACKET_SIZE); setMode(TransferMode.Bulk); + setManufacturer("inf.ntb.ch"); + setProduct("JUnit Test Board"); + setSerialVersion("00.10.00"); } } diff --git a/java/test/ch/ntb/usb/test/DeviceTest.java b/java/test/ch/ntb/usb/test/DeviceTest.java index 7b43925..31f4337 100644 --- a/java/test/ch/ntb/usb/test/DeviceTest.java +++ b/java/test/ch/ntb/usb/test/DeviceTest.java @@ -42,10 +42,6 @@ public class DeviceTest { private static final String testdevicePropertiesFile = "testdevice.properties"; private static final String deviceInfoKey = "testdeviceInfo"; - private static final String Manufacturer = "inf.ntb.ch"; - private static final String Product = "JUnit Test Board"; - private static final String SerialVersion = "00.10.00"; - private static AbstractDeviceInfo devinfo; private static byte[] testData; @@ -56,7 +52,6 @@ public class DeviceTest { private static Logger log = Logger.getLogger(DeviceTest.class.getName()); - @SuppressWarnings("unchecked") @BeforeClass public static void setUp() throws Exception { // load the device info class with the key @@ -70,7 +65,7 @@ public class DeviceTest { throw new Exception("property " + deviceInfoKey + " not found in file " + testdevicePropertiesFile); } - Class devInfoClazz = Class.forName(devInfoClazzName); + Class devInfoClazz = Class.forName(devInfoClazzName); devinfo = (AbstractDeviceInfo) devInfoClazz.newInstance(); // devinfo = new CY7C68013A(); // setup test data @@ -92,7 +87,6 @@ public class DeviceTest { log.info(baos.toString()); } - @SuppressWarnings("null") @Test public void getDescriptors() throws Exception { dev.updateDescriptors(); @@ -318,7 +312,7 @@ public class DeviceTest { USB.REQ_GET_DESCRIPTOR, (3 << 8) + 1, 0, data, data.length, devinfo.getTimeout(), false); String s = getString(data, length); - assertEquals(s, Manufacturer); + assertEquals(s, devinfo.getManufacturer()); // GET DESCRIPTOR (string descriptor (2)) data = getTestData(128); length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST @@ -326,7 +320,7 @@ public class DeviceTest { USB.REQ_GET_DESCRIPTOR, (3 << 8) + 2, 0, data, data.length, devinfo.getTimeout(), false); s = getString(data, length); - assertEquals(s, Product); + assertEquals(s, devinfo.getProduct()); // GET DESCRIPTOR (string descriptor (3)) data = getTestData(128); length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST @@ -334,7 +328,7 @@ public class DeviceTest { USB.REQ_GET_DESCRIPTOR, (3 << 8) + 3, 0, data, data.length, devinfo.getTimeout(), false); s = getString(data, length); - assertEquals(s, SerialVersion); + assertEquals(s, devinfo.getSerialVersion()); // close the device dev.close(); } catch (Exception e) { @@ -404,7 +398,6 @@ public class DeviceTest { return b; } - @SuppressWarnings("unused") private void logData(byte[] data, int length) { if (length > 0) { log.info("length: " + length); diff --git a/java/test/ch/ntb/usb/test/MemoryLeakTest.java b/java/test/ch/ntb/usb/test/MemoryLeakTest.java new file mode 100644 index 0000000..a879726 --- /dev/null +++ b/java/test/ch/ntb/usb/test/MemoryLeakTest.java @@ -0,0 +1,199 @@ +/* + * Java libusb wrapper + * Copyright (c) 2005-2008 Andreas Schläpfer + * + * http://libusbjava.sourceforge.net + * This library is covered by the LGPL, read LGPL.txt for details. + */ +package ch.ntb.usb.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Properties; +import java.util.logging.Logger; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import ch.ntb.usb.Device; +import ch.ntb.usb.LibusbJava; +import ch.ntb.usb.USB; +import ch.ntb.usb.USBException; +import ch.ntb.usb.Usb_Bus; +import ch.ntb.usb.Utils; +import ch.ntb.usb.testApp.AbstractDeviceInfo; +import ch.ntb.usb.testApp.AbstractDeviceInfo.TransferMode; + +public class MemoryLeakTest { + + private static final String testdevicePropertiesFile = "testdevice.properties"; + private static final String deviceInfoKey = "testdeviceInfo"; + + private static AbstractDeviceInfo devinfo; + + private static byte[] testData; + + private static byte[] readData; + + private static Device dev; + + private static Logger log = Logger + .getLogger(MemoryLeakTest.class.getName()); + + @BeforeClass + public static void setUp() throws Exception { + // load the device info class with the key + // from 'testdevice.properties' + InputStream propInputStream = new FileInputStream( + testdevicePropertiesFile); + Properties devInfoProp = new Properties(); + devInfoProp.load(propInputStream); + String devInfoClazzName = devInfoProp.getProperty(deviceInfoKey); + if (devInfoClazzName == null) { + throw new Exception("property " + deviceInfoKey + + " not found in file " + testdevicePropertiesFile); + } + Class devInfoClazz = Class.forName(devInfoClazzName); + devinfo = (AbstractDeviceInfo) devInfoClazz.newInstance(); + // setup test data + testData = new byte[devinfo.getMaxDataSize()]; + readData = new byte[testData.length]; + // initialise the device + LibusbJava.usb_set_debug(255); + dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct()); + assertNotNull(dev); + + // print the devices + LibusbJava.usb_init(); + LibusbJava.usb_find_busses(); + LibusbJava.usb_find_devices(); + Usb_Bus bus = LibusbJava.usb_get_busses(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + Utils.logBus(bus, ps); + log.info(baos.toString()); + } + + @Test + public void initalReset() throws Exception { + doOpen(); + dev.reset(); + timeout(); + } + + @Test + public void bulkWriteReadMultiple() throws Exception { + final int NumberOfIterations = 30000; + + devinfo.setMode(TransferMode.Bulk); + doOpen(); + for (int i = 0; i < NumberOfIterations; i++) { + doWriteRead(); + if (i % 1000 == 0) { + System.out.print("."); + } + } + doClose(); + } + + @Test + public void interruptWriteReadMultiple() throws Exception { + final int NumberOfIterations = 30000; + + devinfo.setMode(TransferMode.Interrupt); + doOpen(); + for (int i = 0; i < NumberOfIterations; i++) { + doWriteRead(); + if (i % 1000 == 0) { + System.out.print("."); + } + } + doClose(); + } + + private void closeOnException() { + try { + dev.close(); + } catch (USBException e1) { + // ignore exceptions + } + } + + @AfterClass + public static void tearDown() throws Exception { + if (dev != null && dev.isOpen()) { + dev.close(); + } + } + + private void doOpen() throws USBException { + dev.open(devinfo.getConfiguration(), devinfo.getInterface(), devinfo + .getAltinterface()); + } + + private void doClose() throws USBException { + dev.close(); + } + + private void doWriteRead() throws Exception { + initTestData(); + try { + if (devinfo.getMode().equals(TransferMode.Bulk)) { + if (devinfo.getOutEPBulk() != -1) { + dev.writeBulk(devinfo.getOutEPBulk(), testData, + testData.length, devinfo.getTimeout(), false); + } + if (devinfo.getInEPBulk() != -1) { + dev.readBulk(devinfo.getInEPBulk(), readData, + readData.length, devinfo.getTimeout(), false); + } + } else if (devinfo.getMode().equals(TransferMode.Interrupt)) { + if (devinfo.getOutEPInt() != -1) { + dev.writeInterrupt(devinfo.getOutEPInt(), testData, + testData.length, devinfo.getTimeout(), false); + } + if (devinfo.getInEPInt() != -1) { + dev.readInterrupt(devinfo.getInEPInt(), readData, + readData.length, devinfo.getTimeout(), false); + } + } + if (devinfo.doCompareData()) { + compare(testData, readData); + } + } catch (AssertionError e) { + closeOnException(); + throw e; + } catch (Exception e) { + closeOnException(); + throw e; + } + } + + private static void compare(byte[] d1, byte[] d2) { + final int minLength = Math.min(d1.length, d2.length); + for (int i = 0; i < minLength; i++) { + assertEquals(d1[i], d2[i]); + } + } + + private static void timeout() { + try { + Thread.sleep(devinfo.getSleepTimeout()); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + private static void initTestData() { + for (int i = 0; i < testData.length; i++) { + testData[i] = (byte) (Math.random() * 0xff); + readData[i] = 0; + } + } +} diff --git a/java/version.properties b/java/version.properties index fd4ffce..265e89c 100644 --- a/java/version.properties +++ b/java/version.properties @@ -1,4 +1,4 @@ #Thu Aug 24 14:28:28 CEST 2006 version.major=0 version.minor=5 -version.release=7 +version.release=8