- Corrected wrong package name in test directory

- Moved demo classes from the test tree to the application tree

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@279 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
uniederer
2012-03-16 14:56:59 +00:00
parent 957a4e193a
commit ce4ef62456
9 changed files with 9 additions and 9 deletions

View File

@@ -0,0 +1,625 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Logger;
import junit.framework.Assert;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ch.ntb.inf.libusbJava.Device;
import ch.ntb.inf.libusbJava.LibusbJava;
import ch.ntb.inf.libusbJava.USB;
import ch.ntb.inf.libusbJava.USBException;
import ch.ntb.inf.libusbJava.Usb_Bus;
import ch.ntb.inf.libusbJava.Usb_Config_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Device_Descriptor;
import ch.ntb.inf.libusbJava.Utils;
import ch.ntb.inf.libusbJava.testApp.AbstractDeviceInfo;
import ch.ntb.inf.libusbJava.testApp.AbstractDeviceInfo.TransferMode;
public class DeviceTest {
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(DeviceTest.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();
// devinfo = new CY7C68013A();
// 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(),
devinfo.getBusName(), devinfo.getFilename());
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 getDescriptors() throws Exception {
dev.updateDescriptors();
Usb_Device_Descriptor devDescriptor = dev.getDeviceDescriptor();
assertNotNull(devDescriptor);
assertEquals(devinfo.getIdProduct(), devDescriptor.getIdProduct());
assertEquals(devinfo.getIdVendor(), devDescriptor.getIdVendor());
Usb_Config_Descriptor confDescriptors[] = dev.getConfigDescriptors();
assertNotNull(confDescriptors);
assertTrue(confDescriptors[0].getInterface().length > 0);
}
@Test
public void initalReset() throws Exception {
doOpen();
// this may change the bus and file name of the device
dev.reset();
timeout();
}
@Test(expected = USBException.class)
public void testClose() throws Exception {
try {
// this calls must not throw an exception
doOpen();
doClose();
} catch (USBException e) {
e.printStackTrace();
fail(e.getMessage());
}
// this call must throw an exception, because the device is closed
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
devinfo.getTimeout(), false);
}
@Test(expected = USBException.class)
public void testReset1() throws Exception {
try {
// this calls must not throw an exception
doOpen();
dev.reset();
timeout();
} catch (USBException e) {
e.printStackTrace();
fail(e.getMessage());
}
// this call must throw an exception, because the device is closed
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
devinfo.getTimeout(), false);
}
@Test(expected = USBException.class)
public void testReset2() throws Exception {
try {
// this calls must not throw an exception
doOpen();
dev.reset();
timeout();
} catch (USBException e) {
e.printStackTrace();
fail(e.getMessage());
}
// this call must throw an exception, because the device can't be closed
doClose();
}
@Test
public void open() throws Exception {
// get device by busname
doOpen();
assertNotNull(dev.getDevice());
String oldFilename = dev.getDevice().getFilename();
String oldBusName = dev.getDevice().getBus().getDirname();
assertNotNull(oldFilename);
assertNotNull(oldBusName);
log.info("filename: " + oldFilename + ", busName: " + oldBusName);
Device dev2 = USB.getDevice(devinfo.getIdVendor(), devinfo
.getIdProduct());
doClose();
assertEquals(dev, dev2);
}
@Test
public void openWithBusName() throws Exception {
// get device by busname
doOpen();
assertNotNull(dev.getDevice());
String oldFilename = dev.getDevice().getFilename();
String oldBusName = dev.getDevice().getBus().getDirname();
assertNotNull(oldFilename);
assertNotNull(oldBusName);
log.info("filename: " + oldFilename + ", busName: " + oldBusName);
Device dev2 = USB.getDevice(devinfo.getIdVendor(), devinfo
.getIdProduct(), oldBusName, null);
doClose();
assertEquals(dev, dev2);
}
@Test
public void openWithFilename() throws Exception {
// get device by busname
doOpen();
assertNotNull(dev.getDevice());
String oldFilename = dev.getDevice().getFilename();
String oldBusName = dev.getDevice().getBus().getDirname();
assertNotNull(oldFilename);
assertNotNull(oldBusName);
log.info("filename: " + oldFilename + ", busName: " + oldBusName);
Device dev2 = USB.getDevice(devinfo.getIdVendor(), devinfo
.getIdProduct(), null, oldFilename);
doClose();
assertEquals(dev, dev2);
}
@Test
public void openWithBusAndFilename() throws Exception {
// get device by busname and filename
doOpen();
assertNotNull(dev.getDevice());
String oldFilename = dev.getDevice().getFilename();
String oldBusName = dev.getDevice().getBus().getDirname();
assertNotNull(oldFilename);
assertNotNull(oldBusName);
log.info("filename: " + oldFilename + ", busName: " + oldBusName);
Device dev2 = USB.getDevice(devinfo.getIdVendor(), devinfo
.getIdProduct(), oldBusName, oldFilename);
doClose();
assertEquals(dev, dev2);
}
@Test
public void bulkWriteRead() throws Exception {
checkBulkEndpoints();
devinfo.setMode(TransferMode.Bulk);
doOpenWriteReadClose();
}
private void checkBulkEndpoints() {
if (devinfo.getInEPBulk() == -1 && devinfo.getOutEPBulk() == -1) {
throw new UnsupportedOperationException(
"no bulk endpoints defined in test device definition");
}
}
@Test
public void interruptWriteRead() throws Exception {
checkInterruptEndpoints();
devinfo.setMode(TransferMode.Interrupt);
doOpenWriteReadClose();
}
private void checkInterruptEndpoints() {
if (devinfo.getInEPInt() == -1 && devinfo.getOutEPInt() == -1) {
throw new UnsupportedOperationException(
"no interrupt endpoints defined in test device definition");
}
}
@Test
public void bulkWriteReadMultiple() throws Exception {
final int NumberOfIterations = 100;
devinfo.setMode(TransferMode.Bulk);
doOpen();
for (int i = 0; i < NumberOfIterations; i++) {
doWriteRead();
}
doClose();
}
@Test
public void multipleOpenCloseWithBulkWrite() throws Exception {
devinfo.setMode(TransferMode.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
public void bulkAndInterrupt() throws Exception {
doOpen();
// BULK
devinfo.setMode(TransferMode.Bulk);
doWriteRead();
// INTERRUPT
devinfo.setMode(TransferMode.Interrupt);
doWriteRead();
doClose();
}
@Test
public void bulkAndInterruptMultiple() throws Exception {
for (int i = 0; i < 20; i++) {
devinfo.setMode(TransferMode.Bulk);
doOpenWriteReadClose();
devinfo.setMode(TransferMode.Interrupt);
doOpenWriteReadClose();
}
}
@Test
public void controlMsg() throws Exception {
try {
dev.open(devinfo.getConfiguration(), devinfo.getInterface(),
devinfo.getAltinterface());
// GET STATUS (device)
byte[] data = getTestData(2);
int length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_STATUS, 0, 0, data, data.length, devinfo
.getTimeout(), false);
assertTrue(length > 0);
assertEquals((byte) 0x01, data[0]);
assertEquals((byte) 0x00, data[1]);
// GET STATUS (interface)
data = getTestData(2);
length = dev.controlMsg(
USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD
| USB.REQ_TYPE_RECIP_INTERFACE, USB.REQ_GET_STATUS,
0, 0, data, data.length, devinfo.getTimeout(), false);
assertTrue(length > 0);
assertEquals((byte) 0x00, data[0]);
assertEquals((byte) 0x00, data[1]);
// GET STATUS (endpoint)
data = getTestData(2);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_ENDPOINT,
USB.REQ_GET_STATUS, 0, 0, data, data.length, devinfo
.getTimeout(), false);
assertTrue(length > 0);
assertEquals((byte) 0x00, data[0]);
assertEquals((byte) 0x00, data[1]);
// GET CONFIGURATION
data = getTestData(1);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_CONFIGURATION, 0, 0, data, data.length, devinfo
.getTimeout(), false);
assertTrue(length > 0);
assertEquals((byte) devinfo.getConfiguration(), data[0]);
// // GET INTERFACE
// data = byte[1];
// length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
// | USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_INTERFACE,
// USB.REQ_GET_INTERFACE, 0, devinfo.getInterface(), data,
// data.length,
// devinfo
// .getTimeout(), false);
// logData(data, length);
// GET DESCRIPTOR (device descriptor)
data = getTestData(128);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_DESCRIPTOR, 1 << 8, 0, data, data.length,
devinfo.getTimeout(), false);
validateDeviceDescriptor(data, length);
// GET DESCRIPTOR (string descriptor (1))
data = getTestData(128);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_DESCRIPTOR, (3 << 8) + 1, 0, data, data.length,
devinfo.getTimeout(), false);
String s = getString(data, length);
assertEquals(s, devinfo.getManufacturer());
// GET DESCRIPTOR (string descriptor (2))
data = getTestData(128);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_DESCRIPTOR, (3 << 8) + 2, 0, data, data.length,
devinfo.getTimeout(), false);
s = getString(data, length);
assertEquals(s, devinfo.getProduct());
// GET DESCRIPTOR (string descriptor (3))
data = getTestData(128);
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
USB.REQ_GET_DESCRIPTOR, (3 << 8) + 3, 0, data, data.length,
devinfo.getTimeout(), false);
s = getString(data, length);
assertEquals(s, devinfo.getSerialVersion());
// close the device
dev.close();
} catch (Exception e) {
closeOnException();
throw e;
} catch (AssertionError e) {
closeOnException();
throw e;
}
}
private void closeOnException() {
try {
dev.close();
} catch (USBException e1) {
// ignore exceptions
}
}
private void validateDeviceDescriptor(byte[] data, int length) {
// length read
assertEquals(18, length);
// descriptor length
assertEquals((byte) 18, data[0]);
// descriptor type
assertEquals((byte) 1, data[1]);
// USB specification number LSB
assertEquals((byte) 0, data[2]);
// USB specification number MSB
assertEquals((byte) 0x02, data[3]);
// device class (vendor specific)
assertEquals((byte) 0xff, data[4]);
// device subclass (vendor specific)
assertEquals((byte) 0xff, data[5]);
// device protocol (vendor specific)
assertEquals((byte) 0xff, data[6]);
// maximum packet size for endpoint zero
assertEquals((byte) 64, data[7]);
// Vendor ID (NTB) LSB
assertEquals((byte) 0x35, data[8]);
// Vendor ID (NTB) MSB
assertEquals((byte) 0x82, data[9]);
// Product ID (JUnit test board) LSB
assertEquals((byte) 0x22, data[10]);
// Product ID (JUnit test board) MSB
assertEquals((byte) 0x02, data[11]);
// Device release number LSB
assertEquals((byte) 0x00, data[12]);
// Device release number MSB
assertEquals((byte) 0x10, data[13]);
// Index of manufacturer string descriptor
assertEquals((byte) 0x01, data[14]);
// Index of product string descriptor
assertEquals((byte) 0x02, data[15]);
// Index of serial number string descriptor
assertEquals((byte) 0x03, data[16]);
// Number of possible configurations
assertEquals((byte) 0x01, data[17]);
}
private byte[] getTestData(int length) {
byte[] b = new byte[length];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) (Math.random() * 256);
}
return b;
}
@SuppressWarnings("unused")
private void logData(byte[] data, int length) {
if (length > 0) {
log.info("length: " + length);
String logData = "";
for (int i = 0; i < length; i++) {
logData += "0x" + Integer.toHexString(data[i] & 0xff) + "\t";
}
log.info(logData);
}
}
private String getString(byte[] data, int length)
throws UnsupportedEncodingException {
// data length
assertTrue(length > 2);
// string length
assertTrue(data[0] > 2);
// string descriptor ident
assertEquals((byte) 3, data[1]);
// create string from data
return new String(data, 2, length - 2, "UTF-16LE");
}
@Test
public void invalidConfig() throws Exception {
try {
dev.open(devinfo.getConfiguration() + 5, devinfo.getInterface(),
devinfo.getAltinterface());
fail("USBException expected");
} catch (USBException e) {
log.severe("could not set config "
+ (devinfo.getConfiguration() + 5));
}
doOpenWriteReadClose();
}
@Test
public void invalidInterface() throws Exception {
try {
dev.open(devinfo.getConfiguration(), devinfo.getInterface() + 5,
devinfo.getAltinterface());
fail("USBException expected");
} catch (USBException e) {
log.severe("could not claim interface "
+ (devinfo.getInterface() + 5));
}
doOpenWriteReadClose();
}
@Test
public void invalidAltinterface() throws Exception {
try {
dev.open(devinfo.getConfiguration(), devinfo.getInterface(),
devinfo.getAltinterface() + 5);
fail("USBException expected");
} catch (USBException e) {
log.severe("could not set alt interface "
+ (devinfo.getAltinterface() + 5));
}
doOpenWriteReadClose();
}
@Test
public void testGetIdProduct() {
Assert.assertEquals(devinfo.getIdProduct(), dev.getIdProduct());
}
@Test
public void testGetIdVendor() {
Assert.assertEquals(devinfo.getIdVendor(), dev.getIdVendor());
}
@Test
public void testGetAltinterface() {
Assert.assertEquals(devinfo.getAltinterface(), dev.getAltinterface());
}
@Test
public void testGetConfiguration() {
Assert.assertEquals(devinfo.getConfiguration(), dev.getConfiguration());
}
@Test
public void testGetInterface() {
Assert.assertEquals(devinfo.getInterface(), dev.getInterface());
}
@Test
public void testGetMaxPacketSize() throws USBException {
doOpen();
Assert.assertEquals(devinfo.getMaxDataSize(), dev.getMaxPacketSize());
doClose();
}
@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 doOpenWriteReadClose() throws Exception {
doOpen();
doWriteRead();
doClose();
}
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) {
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;
}
}
}

View File

@@ -0,0 +1,199 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2008 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.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.inf.libusbJava.Device;
import ch.ntb.inf.libusbJava.LibusbJava;
import ch.ntb.inf.libusbJava.USB;
import ch.ntb.inf.libusbJava.USBException;
import ch.ntb.inf.libusbJava.Usb_Bus;
import ch.ntb.inf.libusbJava.Utils;
import ch.ntb.inf.libusbJava.testApp.AbstractDeviceInfo;
import ch.ntb.inf.libusbJava.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 = 3000;
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 = 3000;
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;
}
}
}

View File

@@ -0,0 +1,240 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2007 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.test;
import ch.ntb.inf.libusbJava.LibusbJava;
import ch.ntb.inf.libusbJava.Usb_Bus;
import ch.ntb.inf.libusbJava.Usb_Config_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Device;
import ch.ntb.inf.libusbJava.Usb_Endpoint_Descriptor;
import ch.ntb.inf.libusbJava.Usb_Interface;
import ch.ntb.inf.libusbJava.Usb_Interface_Descriptor;
/**
* This class replicates the code from testlibusb.c supplied in the
* libusb-0.1.12 release.
*/
public class TestLibUsbJava {
static boolean verbose;
/**
* prints out endpoint info
*
* @param endpoint
* The end point.
*/
private static void printEndpoint(Usb_Endpoint_Descriptor endpoint) {
System.out.print(String.format(" bEndpointAddress: %02xh\n",
endpoint.getBEndpointAddress()));
System.out.print(String.format(" bmAttributes: %02xh\n",
endpoint.getBmAttributes()));
System.out.print(String.format(" wMaxPacketSize: %d\n", endpoint
.getWMaxPacketSize()));
System.out.print(String.format(" bInterval: %d\n", endpoint
.getBInterval()));
System.out.print(String.format(" bRefresh: %d\n", endpoint
.getBRefresh()));
System.out.print(String.format(" bSynchAddress: %d\n", endpoint
.getBSynchAddress()));
}
/**
* prints out the interface descriptor
*
* @param interfaceDescript
* The interface descriptor.
*/
private static void printAltsetting(
Usb_Interface_Descriptor interfaceDescript) {
System.out.print(String.format(" bInterfaceNumber: %d\n",
interfaceDescript.getBInterfaceNumber()));
System.out.print(String.format(" bAlternateSetting: %d\n",
interfaceDescript.getBAlternateSetting()));
System.out.print(String.format(" bNumEndpoints: %d\n",
interfaceDescript.getBNumEndpoints()));
System.out.print(String.format(" bInterfaceClass: %d\n",
interfaceDescript.getBInterfaceClass()));
System.out.print(String.format(" bInterfaceSubClass: %d\n",
interfaceDescript.getBInterfaceSubClass()));
System.out.print(String.format(" bInterfaceProtocol: %d\n",
interfaceDescript.getBInterfaceProtocol()));
System.out.print(String.format(" iInterface: %d\n",
interfaceDescript.getIInterface()));
for (int i = 0; i < interfaceDescript.getBNumEndpoints(); i++) {
printEndpoint(interfaceDescript.getEndpoint()[i]);
}
}
/**
* prints out interface
*
* @param usbInterface
* The interface.
*/
private static void printInterface(Usb_Interface usbInterface) {
for (int i = 0; i < usbInterface.getNumAltsetting(); i++) {
printAltsetting(usbInterface.getAltsetting()[i]);
}
}
/**
* prints out configuration
*
* @param config
* The configuration.
*/
private static void printConfiguration(Usb_Config_Descriptor config) {
System.out.print(String.format(" wTotalLength: %d\n", config
.getWTotalLength()));
System.out.print(String.format(" bNumInterfaces: %d\n", config
.getBNumInterfaces()));
System.out.print(String.format(" bConfigurationValue: %d\n", config
.getBConfigurationValue()));
System.out.print(String.format(" iConfiguration: %d\n", config
.getIConfiguration()));
System.out.print(String.format(" bmAttributes: %02xh\n",
config.getBmAttributes()));
System.out.print(String.format(" MaxPower: %d\n", config
.getMaxPower()));
for (int i = 0; i < config.getBNumInterfaces(); i++) {
printInterface(config.getInterface()[i]);
}
}
private static int printDevice(Usb_Device dev, int level) {
long udev;
String mfr;
String product;
String sn;
String spaces;
String descript;
spaces = " ";
udev = LibusbJava.usb_open(dev);
if (udev != 0) {
if (dev.getDescriptor().getIManufacturer() != 0) {
mfr = LibusbJava.usb_get_string_simple(udev, dev
.getDescriptor().getIManufacturer());
if (mfr != null) {
descript = String.format("%s - ", mfr);
} else {
descript = String.format("%04X - ", dev.getDescriptor()
.getIdVendor());
}
} else {
descript = String.format("%04X - ", dev.getDescriptor()
.getIdVendor());
}
if (dev.getDescriptor().getIProduct() != 0) {
product = LibusbJava.usb_get_string_simple(udev, dev
.getDescriptor().getIProduct());
if (product != null) {
descript = descript + String.format("%s", product);
} else {
descript = descript
+ String.format("%04X", dev.getDescriptor()
.getIdProduct());
}
} else {
descript = descript
+ String.format("%04X", dev.getDescriptor()
.getIdProduct());
}
} else {
descript = String.format("%04X - %04X", dev.getDescriptor()
.getIdVendor(), dev.getDescriptor().getIdProduct());
}
System.out.print(String.format("%sDev #%d: %s\n", spaces.substring(0,
level * 2), dev.getDevnum(), descript));
if ((udev != 0) && verbose) {
if (dev.getDescriptor().getISerialNumber() != 0) {
sn = LibusbJava.usb_get_string_simple(udev, dev.getDescriptor()
.getISerialNumber());
if (sn != null) {
System.out.print(String.format("%s - Serial Number: %s\n",
spaces.substring(0, level * 2), sn));
}
}
}
if (udev != 0) {
LibusbJava.usb_close(udev);
}
if (verbose) {
if (dev.getConfig().length == 0) {
System.out.print(" Couldn't retrieve descriptors\n");
return 0;
}
for (int i = 0; i < dev.getDescriptor().getBNumConfigurations(); i++) {
printConfiguration(dev.getConfig()[i]);
}
} else {
Usb_Device childDev = null;
for (int i = 0; i < dev.getNumChildren(); i++) {
if (i == 0) {
childDev = dev.getChildren();
} else {
childDev = childDev.getNext();
}
printDevice(childDev, level + 1);
}
}
return 0;
} // end of printDevice method
/**
* The main method.
*
* @param args
* The command line arguments.
*/
public static void main(String args[]) throws Exception {
if ((args.length > 0) && (args[0].equals("-v"))) {
verbose = true;
} else {
verbose = false;
}
// used for debugging. 0 = no debugging, 255 = with debugging
//
LibusbJava.usb_set_debug(255);
LibusbJava.usb_init();
LibusbJava.usb_find_busses();
LibusbJava.usb_find_devices();
for (Usb_Bus bus = LibusbJava.usb_get_busses(); bus != null; bus = bus
.getNext()) {
if ((bus.getRootDev() != null) && !verbose) {
printDevice(bus.getRootDev(), 0);
} else {
for (Usb_Device dev = bus.getDevices(); dev != null; dev = dev
.getNext()) {
printDevice(dev, 0);
}
}
}
} // end main
} // end of TestLibUsbJava class

View File

@@ -0,0 +1,34 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.test.devices;
import ch.ntb.inf.libusbJava.USB;
import ch.ntb.inf.libusbJava.testApp.AbstractDeviceInfo;
public class AT90USB1287 extends AbstractDeviceInfo {
@Override
public void initValues() {
setIdVendor((short) 0x8235);
setIdProduct((short) 0x0222);
setTimeout(2000);
setConfiguration(1);
setInterface(0);
setAltinterface(-1);
setOutEPBulk(0x01);
setInEPBulk(0x82);
setOutEPInt(0x03);
setInEPInt(0x84);
setSleepTimeout(2000);
setMaxDataSize(USB.FULLSPEED_MAX_BULK_PACKET_SIZE);
setMode(TransferMode.Bulk);
setManufacturer("inf.ntb.ch");
setProduct("JUnit Test Board");
setSerialVersion("00.10.00");
}
}

View File

@@ -0,0 +1,31 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.test.devices;
import ch.ntb.inf.libusbJava.USB;
import ch.ntb.inf.libusbJava.testApp.AbstractDeviceInfo;
public class CY7C68013A extends AbstractDeviceInfo {
@Override
public void initValues() {
setIdVendor((short) 0x8235);
setIdProduct((short) 0x0222);
setTimeout(2000);
setConfiguration(1);
setInterface(0);
setAltinterface(-1);
setOutEPInt(0x02);
setInEPInt(0x86);
setOutEPBulk(0x04);
setInEPBulk(0x88);
setSleepTimeout(2000);
setMaxDataSize(USB.HIGHSPEED_MAX_BULK_PACKET_SIZE);
setMode(TransferMode.Bulk);
}
}

View File

@@ -0,0 +1,32 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.test.devices;
import ch.ntb.inf.libusbJava.testApp.AbstractDeviceInfo;
public class MX500 extends AbstractDeviceInfo {
@Override
public void initValues() {
setIdVendor((short) 0x046d);
setIdProduct((short) 0xc025);
setTimeout(2000);
setConfiguration(1);
setInterface(0);
setAltinterface(0);
setOutEPInt(-1);
setInEPInt(0x81);
setOutEPBulk(-1);
setInEPBulk(-1);
setSleepTimeout(5000);
setMaxDataSize(5);
setMode(TransferMode.Interrupt);
// we only read data -> don't compare
setDoCompareData(false);
}
}

View File

@@ -0,0 +1,34 @@
/*
* Java libusb wrapper
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <spandi at users.sourceforge.net>
*
* http://libusbjava.sourceforge.net
* This library is covered by the LGPL, read LGPL.txt for details.
*/
package ch.ntb.inf.libusbJava.test.devices;
import ch.ntb.inf.libusbJava.testApp.AbstractDeviceInfo;
public class MousePlus extends AbstractDeviceInfo {
@Override
public void initValues() {
setIdVendor((short) 0x046d);
setIdProduct((short) 0xc016);
setBusName("bus-0");
setFilename("\\\\.\\libusb0-0001--0x046d-0xc016");
setTimeout(2000);
setConfiguration(1);
setInterface(0);
setAltinterface(0);
setOutEPInt(-1);
setInEPInt(0x81);
setOutEPBulk(-1);
setInEPBulk(-1);
setSleepTimeout(5000);
setMaxDataSize(4);
setMode(TransferMode.Interrupt);
// we only read data -> don't compare
setDoCompareData(false);
}
}