test class added which replicates the code from testlibusb.c
new test class for MX500 mouse allow test with only read operations and interrupt and/or bulk transfers git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@251 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
@@ -35,6 +35,8 @@ public abstract class AbstractDeviceInfo {
|
||||
|
||||
private TransferMode mode;
|
||||
|
||||
private boolean compareData = true;
|
||||
|
||||
public static enum TransferMode {
|
||||
Bulk, Interrupt
|
||||
}
|
||||
@@ -149,4 +151,11 @@ public abstract class AbstractDeviceInfo {
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
public boolean doCompareData() {
|
||||
return compareData;
|
||||
}
|
||||
|
||||
public void setDoCompareData(boolean compareData) {
|
||||
this.compareData = compareData;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,16 +141,32 @@ public class DeviceTest {
|
||||
|
||||
@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;
|
||||
@@ -451,24 +467,31 @@ public class DeviceTest {
|
||||
private void doOpenWriteReadClose() throws Exception {
|
||||
doOpen();
|
||||
doWriteRead();
|
||||
compare(testData, readData);
|
||||
doClose();
|
||||
}
|
||||
|
||||
private void doWriteRead() throws Exception {
|
||||
initTestData();
|
||||
if (devinfo.getMode().equals(TransferMode.Bulk)) {
|
||||
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
|
||||
devinfo.getTimeout(), false);
|
||||
dev.readBulk(devinfo.getInEPBulk(), readData, readData.length,
|
||||
devinfo.getTimeout(), false);
|
||||
if (devinfo.getOutEPBulk() != -1) {
|
||||
dev.writeBulk(devinfo.getOutEPBulk(), testData,
|
||||
testData.length, devinfo.getTimeout(), false);
|
||||
} else if (devinfo.getInEPBulk() != -1) {
|
||||
dev.readBulk(devinfo.getInEPBulk(), readData, readData.length,
|
||||
devinfo.getTimeout(), false);
|
||||
}
|
||||
} else if (devinfo.getMode().equals(TransferMode.Interrupt)) {
|
||||
dev.writeInterrupt(devinfo.getOutEPInt(), testData,
|
||||
testData.length, devinfo.getTimeout(), false);
|
||||
dev.readInterrupt(devinfo.getInEPInt(), readData, readData.length,
|
||||
devinfo.getTimeout(), false);
|
||||
if (devinfo.getOutEPInt() != -1) {
|
||||
dev.writeInterrupt(devinfo.getOutEPInt(), testData,
|
||||
testData.length, devinfo.getTimeout(), false);
|
||||
} else if (devinfo.getInEPInt() != -1) {
|
||||
dev.readInterrupt(devinfo.getInEPInt(), readData,
|
||||
readData.length, devinfo.getTimeout(), false);
|
||||
}
|
||||
}
|
||||
if (devinfo.doCompareData()) {
|
||||
compare(testData, readData);
|
||||
}
|
||||
compare(testData, readData);
|
||||
}
|
||||
|
||||
private static void compare(byte[] d1, byte[] d2) {
|
||||
|
||||
33
java/test/ch/ntb/usb/test/MX500.java
Normal file
33
java/test/ch/ntb/usb/test/MX500.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.usb.test;
|
||||
|
||||
import ch.ntb.usb.USB;
|
||||
import ch.ntb.usb.testApp.AbstractDeviceInfo;
|
||||
|
||||
public class MX500 extends AbstractDeviceInfo {
|
||||
|
||||
@Override
|
||||
public void initValues() {
|
||||
setIdVendor((short) 0x046d);
|
||||
setIdProduct((short) 0xc025);
|
||||
setTimeout(5000);
|
||||
setConfiguration(1);
|
||||
setInterface(0);
|
||||
setAltinterface(0);
|
||||
setOutEPInt(-1);
|
||||
setInEPInt(0x81);
|
||||
setOutEPBulk(-1);
|
||||
setInEPBulk(-1);
|
||||
setSleepTimeout(2000);
|
||||
setMaxDataSize(USB.HIGHSPEED_MAX_BULK_PACKET_SIZE);
|
||||
setMode(TransferMode.Interrupt);
|
||||
// we only read data -> don't compare
|
||||
setDoCompareData(false);
|
||||
}
|
||||
}
|
||||
240
java/test/ch/ntb/usb/test/TestLibUsbJava.java
Normal file
240
java/test/ch/ntb/usb/test/TestLibUsbJava.java
Normal 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.usb.test;
|
||||
|
||||
import ch.ntb.usb.LibusbJava;
|
||||
import ch.ntb.usb.Usb_Bus;
|
||||
import ch.ntb.usb.Usb_Config_Descriptor;
|
||||
import ch.ntb.usb.Usb_Device;
|
||||
import ch.ntb.usb.Usb_Endpoint_Descriptor;
|
||||
import ch.ntb.usb.Usb_Interface;
|
||||
import ch.ntb.usb.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) {
|
||||
int 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.
|
||||
//
|
||||
LibusbJava.usb_set_debug(0);
|
||||
|
||||
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
|
||||
@@ -4,6 +4,8 @@
|
||||
################################################################
|
||||
|
||||
# Atmel AVR AT90USB1287
|
||||
testdeviceInfo=ch.ntb.usb.test.AT90USB1287
|
||||
# testdeviceInfo=ch.ntb.usb.test.AT90USB1287
|
||||
# Cypress FX2 CY7C68013A
|
||||
# testdeviceInfo=ch.ntb.usb.test.CY7C68013A
|
||||
# MX500 Mouse
|
||||
testdeviceInfo=ch.ntb.usb.test.MX500
|
||||
|
||||
Reference in New Issue
Block a user