new parameter added to be able to open a specific device based on the filename if there are multiple devices with the same vendor/product id
procedure added to get Usb_Bus object from USB class git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@268 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
@@ -23,8 +23,18 @@ public class Device {
|
|||||||
|
|
||||||
private int maxPacketSize;
|
private int maxPacketSize;
|
||||||
|
|
||||||
private int idVendor, idProduct, dev_configuration, dev_interface,
|
/**
|
||||||
dev_altinterface;
|
* Mandatory identification values for the device.
|
||||||
|
*/
|
||||||
|
private int idVendor, idProduct;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional identification value for the device (e.g. if there are multiple
|
||||||
|
* devices with the same vendor and product id).
|
||||||
|
*/
|
||||||
|
private String filename;
|
||||||
|
|
||||||
|
private int dev_configuration, dev_interface, dev_altinterface;
|
||||||
|
|
||||||
private long usbDevHandle;
|
private long usbDevHandle;
|
||||||
|
|
||||||
@@ -34,32 +44,22 @@ public class Device {
|
|||||||
|
|
||||||
private Usb_Device dev;
|
private Usb_Device dev;
|
||||||
|
|
||||||
private boolean initUSBDone;
|
|
||||||
|
|
||||||
protected Device(short idVendor, short idProduct) {
|
protected Device(short idVendor, short idProduct) {
|
||||||
initUSBDone = false;
|
|
||||||
resetOnFirstOpen = false;
|
resetOnFirstOpen = false;
|
||||||
resetDone = false;
|
resetDone = false;
|
||||||
maxPacketSize = -1;
|
maxPacketSize = -1;
|
||||||
this.idVendor = idVendor;
|
this.idVendor = idVendor;
|
||||||
this.idProduct = idProduct;
|
this.idProduct = idProduct;
|
||||||
|
this.filename = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initUSB() {
|
protected Device(short idVendor, short idProduct, String filename) {
|
||||||
LibusbJava.usb_init();
|
resetOnFirstOpen = false;
|
||||||
initUSBDone = true;
|
resetDone = false;
|
||||||
}
|
maxPacketSize = -1;
|
||||||
|
this.idVendor = idVendor;
|
||||||
private Usb_Bus initBus() throws USBException {
|
this.idProduct = idProduct;
|
||||||
LibusbJava.usb_find_busses();
|
this.filename = filename;
|
||||||
LibusbJava.usb_find_devices();
|
|
||||||
|
|
||||||
Usb_Bus bus = LibusbJava.usb_get_busses();
|
|
||||||
if (bus == null) {
|
|
||||||
throw new USBException("LibusbJava.usb_get_busses(): "
|
|
||||||
+ LibusbJava.usb_strerror());
|
|
||||||
}
|
|
||||||
return bus;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateMaxPacketSize(Usb_Device device) throws USBException {
|
private void updateMaxPacketSize(Usb_Device device) throws USBException {
|
||||||
@@ -84,8 +84,14 @@ public class Device {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Usb_Device initDevice() throws USBException {
|
/**
|
||||||
Usb_Bus bus = initBus();
|
* Initializes the device. The parameters <code>idVendor</code> and
|
||||||
|
* <code>idProduct</code> are mandatory. The parameter
|
||||||
|
* <code>filename</code> is optional.
|
||||||
|
*/
|
||||||
|
private Usb_Device initDevice(int idVendorParam, int idProductParam,
|
||||||
|
String filename) throws USBException {
|
||||||
|
Usb_Bus bus = USB.getBus();
|
||||||
|
|
||||||
Usb_Device device = null;
|
Usb_Device device = null;
|
||||||
// search for device
|
// search for device
|
||||||
@@ -93,8 +99,17 @@ public class Device {
|
|||||||
device = bus.getDevices();
|
device = bus.getDevices();
|
||||||
while (device != null) {
|
while (device != null) {
|
||||||
Usb_Device_Descriptor devDesc = device.getDescriptor();
|
Usb_Device_Descriptor devDesc = device.getDescriptor();
|
||||||
if ((devDesc.getIdVendor() == idVendor)
|
if (filename != null
|
||||||
&& (devDesc.getIdProduct() == idProduct)) {
|
&& filename.compareTo(device.getFilename()) == 0
|
||||||
|
&& devDesc.getIdVendor() == idVendorParam
|
||||||
|
&& devDesc.getIdProduct() == idProductParam) {
|
||||||
|
// idVendor, idProduct and filename
|
||||||
|
logger.info("Device found: " + device.getFilename());
|
||||||
|
updateMaxPacketSize(device);
|
||||||
|
return device;
|
||||||
|
} else if (devDesc.getIdVendor() == idVendorParam
|
||||||
|
&& devDesc.getIdProduct() == idProductParam) {
|
||||||
|
// only idVendor and idProduct
|
||||||
logger.info("Device found: " + device.getFilename());
|
logger.info("Device found: " + device.getFilename());
|
||||||
updateMaxPacketSize(device);
|
updateMaxPacketSize(device);
|
||||||
return device;
|
return device;
|
||||||
@@ -114,9 +129,7 @@ public class Device {
|
|||||||
* @throws USBException
|
* @throws USBException
|
||||||
*/
|
*/
|
||||||
public void updateDescriptors() throws USBException {
|
public void updateDescriptors() throws USBException {
|
||||||
if (!initUSBDone)
|
dev = initDevice(idVendor, idProduct, filename);
|
||||||
initUSB();
|
|
||||||
dev = initDevice();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -175,9 +188,7 @@ public class Device {
|
|||||||
throw new USBException("device opened, close or reset first");
|
throw new USBException("device opened, close or reset first");
|
||||||
}
|
}
|
||||||
|
|
||||||
initUSB();
|
dev = initDevice(idVendor, idProduct, filename);
|
||||||
|
|
||||||
dev = initDevice();
|
|
||||||
|
|
||||||
if (dev != null) {
|
if (dev != null) {
|
||||||
long res = LibusbJava.usb_open(dev);
|
long res = LibusbJava.usb_open(dev);
|
||||||
@@ -724,4 +735,26 @@ public class Device {
|
|||||||
resetOnFirstOpen = enable;
|
resetOnFirstOpen = enable;
|
||||||
resetTimeout = timeout;
|
resetTimeout = timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the optional filename which is set when there are multiple
|
||||||
|
* devices with the same vendor and product id. See
|
||||||
|
* {@link USB#getDevice(short, short, String)}. Use
|
||||||
|
* {@link Usb_Device#getFilename()} to read the filename of a device.
|
||||||
|
*
|
||||||
|
* @return the filename if set or null
|
||||||
|
*/
|
||||||
|
protected String getFilename() {
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Usb_Device instance associated with this device. This value
|
||||||
|
* is only valid after opening the device.
|
||||||
|
*
|
||||||
|
* @return the Usb_Device instance associated with this device.
|
||||||
|
*/
|
||||||
|
public Usb_Device getDevice() {
|
||||||
|
return dev;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -167,6 +167,8 @@ public class USB {
|
|||||||
|
|
||||||
private static LinkedList<Device> devices = new LinkedList<Device>();
|
private static LinkedList<Device> devices = new LinkedList<Device>();
|
||||||
|
|
||||||
|
private static boolean initUSBDone = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new device an register it in a device queue. If the device is
|
* Create a new device an register it in a device queue. If the device is
|
||||||
* already registered, a reference to it will be returned.<br>
|
* already registered, a reference to it will be returned.<br>
|
||||||
@@ -175,22 +177,38 @@ public class USB {
|
|||||||
* the vendor id of the USB device
|
* the vendor id of the USB device
|
||||||
* @param idProduct
|
* @param idProduct
|
||||||
* the product id of the USB device
|
* the product id of the USB device
|
||||||
|
* @param filename
|
||||||
|
* an optional filename which can be used to distinguish multiple
|
||||||
|
* devices with the same vendor and product id.
|
||||||
* @return a newly created device or an already registered device
|
* @return a newly created device or an already registered device
|
||||||
*/
|
*/
|
||||||
public static Device getDevice(short idVendor, short idProduct) {
|
public static Device getDevice(short idVendor, short idProduct,
|
||||||
|
String filename) {
|
||||||
|
|
||||||
// check if this device is already registered
|
// check if this device is already registered
|
||||||
Device dev = getRegisteredDevice(idVendor, idProduct);
|
Device dev = getRegisteredDevice(idVendor, idProduct, filename);
|
||||||
if (dev != null) {
|
if (dev != null) {
|
||||||
logger.info("return already registered device");
|
logger.info("return already registered device");
|
||||||
return dev;
|
return dev;
|
||||||
}
|
}
|
||||||
dev = new Device(idVendor, idProduct);
|
dev = new Device(idVendor, idProduct, filename);
|
||||||
logger.info("create new device");
|
logger.info("create new device");
|
||||||
devices.add(dev);
|
devices.add(dev);
|
||||||
return dev;
|
return dev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See {@link #getDevice(short, short, String)}. The parameter
|
||||||
|
* <code>filename</code> is set to null.
|
||||||
|
*
|
||||||
|
* @param idVendor
|
||||||
|
* @param idProduct
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Device getDevice(short idVendor, short idProduct) {
|
||||||
|
return getDevice(idVendor, idProduct, null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an already registered device or null if the device does not exist.<br>
|
* Get an already registered device or null if the device does not exist.<br>
|
||||||
*
|
*
|
||||||
@@ -198,16 +216,55 @@ public class USB {
|
|||||||
* the vendor id of the USB device
|
* the vendor id of the USB device
|
||||||
* @param idProduct
|
* @param idProduct
|
||||||
* the product id of the USB device
|
* the product id of the USB device
|
||||||
|
* @param filename
|
||||||
|
* an optional filename which can be used to distinguish multiple
|
||||||
|
* devices with the same vendor and product id.
|
||||||
* @return the device or null
|
* @return the device or null
|
||||||
*/
|
*/
|
||||||
private static Device getRegisteredDevice(short idVendor, short idProduct) {
|
private static Device getRegisteredDevice(short idVendor, short idProduct,
|
||||||
|
String filename) {
|
||||||
for (Iterator<Device> iter = devices.iterator(); iter.hasNext();) {
|
for (Iterator<Device> iter = devices.iterator(); iter.hasNext();) {
|
||||||
Device dev = iter.next();
|
Device dev = iter.next();
|
||||||
if ((dev.getIdVendor() == idVendor)
|
if (filename != null && filename.compareTo(dev.getFilename()) == 0
|
||||||
&& (dev.getIdProduct() == idProduct)) {
|
&& dev.getIdVendor() == idVendor
|
||||||
|
&& dev.getIdProduct() == idProduct) {
|
||||||
|
return dev;
|
||||||
|
} else if (dev.getIdVendor() == idVendor
|
||||||
|
&& dev.getIdProduct() == idProduct) {
|
||||||
return dev;
|
return dev;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the root {@link Usb_Bus} element.
|
||||||
|
*
|
||||||
|
* @return the root {@link Usb_Bus} element
|
||||||
|
* @throws USBException
|
||||||
|
*/
|
||||||
|
public static Usb_Bus getBus() throws USBException {
|
||||||
|
if (!initUSBDone) {
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
LibusbJava.usb_find_busses();
|
||||||
|
LibusbJava.usb_find_devices();
|
||||||
|
|
||||||
|
Usb_Bus bus = LibusbJava.usb_get_busses();
|
||||||
|
if (bus == null) {
|
||||||
|
throw new USBException("LibusbJava.usb_get_busses(): "
|
||||||
|
+ LibusbJava.usb_strerror());
|
||||||
|
}
|
||||||
|
return bus;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Explicitly calls {@link LibusbJava#usb_init()}. Note that you don't need
|
||||||
|
* to call this procedure as it is called implicitly when creating a new
|
||||||
|
* device with {@link USB#getDevice(short, short, String)}.
|
||||||
|
*/
|
||||||
|
public static void init() {
|
||||||
|
LibusbJava.usb_init();
|
||||||
|
initUSBDone = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,35 +7,43 @@
|
|||||||
*/
|
*/
|
||||||
package ch.ntb.usb;
|
package ch.ntb.usb;
|
||||||
|
|
||||||
|
import java.io.PrintStream;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
|
||||||
public static void logBus(Usb_Bus bus) {
|
public static void logBus(Usb_Bus bus) {
|
||||||
|
logBus(bus, System.out);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void logBus(Usb_Bus bus, PrintStream out) {
|
||||||
Usb_Bus usb_Bus = bus;
|
Usb_Bus usb_Bus = bus;
|
||||||
while (usb_Bus != null) {
|
while (usb_Bus != null) {
|
||||||
System.out.println(usb_Bus.toString());
|
out.println(usb_Bus.toString());
|
||||||
Usb_Device dev = usb_Bus.getDevices();
|
Usb_Device dev = usb_Bus.getDevices();
|
||||||
while (dev != null) {
|
while (dev != null) {
|
||||||
System.out.println("\t" + dev.toString());
|
out.println("\t" + dev.toString());
|
||||||
// Usb_Device_Descriptor
|
// Usb_Device_Descriptor
|
||||||
Usb_Device_Descriptor defDesc = dev.getDescriptor();
|
Usb_Device_Descriptor defDesc = dev.getDescriptor();
|
||||||
System.out.println("\t\t" + defDesc.toString());
|
out.println("\t\t" + defDesc.toString());
|
||||||
// Usb_Config_Descriptor
|
// Usb_Config_Descriptor
|
||||||
Usb_Config_Descriptor[] confDesc = dev.getConfig();
|
Usb_Config_Descriptor[] confDesc = dev.getConfig();
|
||||||
for (int i = 0; i < confDesc.length; i++) {
|
for (int i = 0; i < confDesc.length; i++) {
|
||||||
System.out.println("\t\t" + confDesc[i].toString());
|
out.println("\t\t" + confDesc[i].toString());
|
||||||
Usb_Interface[] int_ = confDesc[i].getInterface();
|
Usb_Interface[] int_ = confDesc[i].getInterface();
|
||||||
if (int_ != null) {
|
if (int_ != null) {
|
||||||
for (int j = 0; j < int_.length; j++) {
|
for (int j = 0; j < int_.length; j++) {
|
||||||
System.out.println("\t\t\t" + int_[j].toString());
|
out.println("\t\t\t" + int_[j].toString());
|
||||||
Usb_Interface_Descriptor[] intDesc = int_[j].getAltsetting();
|
Usb_Interface_Descriptor[] intDesc = int_[j]
|
||||||
|
.getAltsetting();
|
||||||
if (intDesc != null) {
|
if (intDesc != null) {
|
||||||
for (int k = 0; k < intDesc.length; k++) {
|
for (int k = 0; k < intDesc.length; k++) {
|
||||||
System.out.println("\t\t\t\t"
|
out.println("\t\t\t\t"
|
||||||
+ intDesc[k].toString());
|
+ intDesc[k].toString());
|
||||||
Usb_Endpoint_Descriptor[] epDesc = intDesc[k].getEndpoint();
|
Usb_Endpoint_Descriptor[] epDesc = intDesc[k]
|
||||||
|
.getEndpoint();
|
||||||
if (epDesc != null) {
|
if (epDesc != null) {
|
||||||
for (int e = 0; e < epDesc.length; e++) {
|
for (int e = 0; e < epDesc.length; e++) {
|
||||||
System.out.println("\t\t\t\t\t"
|
out.println("\t\t\t\t\t"
|
||||||
+ epDesc[e].toString());
|
+ epDesc[e].toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,17 @@
|
|||||||
package ch.ntb.usb.test;
|
package ch.ntb.usb.test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
@@ -26,8 +30,10 @@ import ch.ntb.usb.Device;
|
|||||||
import ch.ntb.usb.LibusbJava;
|
import ch.ntb.usb.LibusbJava;
|
||||||
import ch.ntb.usb.USB;
|
import ch.ntb.usb.USB;
|
||||||
import ch.ntb.usb.USBException;
|
import ch.ntb.usb.USBException;
|
||||||
|
import ch.ntb.usb.Usb_Bus;
|
||||||
import ch.ntb.usb.Usb_Config_Descriptor;
|
import ch.ntb.usb.Usb_Config_Descriptor;
|
||||||
import ch.ntb.usb.Usb_Device_Descriptor;
|
import ch.ntb.usb.Usb_Device_Descriptor;
|
||||||
|
import ch.ntb.usb.Utils;
|
||||||
import ch.ntb.usb.testApp.AbstractDeviceInfo;
|
import ch.ntb.usb.testApp.AbstractDeviceInfo;
|
||||||
import ch.ntb.usb.testApp.AbstractDeviceInfo.TransferMode;
|
import ch.ntb.usb.testApp.AbstractDeviceInfo.TransferMode;
|
||||||
|
|
||||||
@@ -48,6 +54,8 @@ public class DeviceTest {
|
|||||||
|
|
||||||
private static Device dev;
|
private static Device dev;
|
||||||
|
|
||||||
|
private static Logger log = Logger.getLogger(DeviceTest.class.getName());
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUp() throws Exception {
|
public static void setUp() throws Exception {
|
||||||
@@ -71,6 +79,17 @@ public class DeviceTest {
|
|||||||
// initialise the device
|
// initialise the device
|
||||||
LibusbJava.usb_set_debug(255);
|
LibusbJava.usb_set_debug(255);
|
||||||
dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct());
|
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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("null")
|
@SuppressWarnings("null")
|
||||||
@@ -78,11 +97,11 @@ public class DeviceTest {
|
|||||||
public void getDescriptors() throws Exception {
|
public void getDescriptors() throws Exception {
|
||||||
dev.updateDescriptors();
|
dev.updateDescriptors();
|
||||||
Usb_Device_Descriptor devDescriptor = dev.getDeviceDescriptor();
|
Usb_Device_Descriptor devDescriptor = dev.getDeviceDescriptor();
|
||||||
assertTrue(devDescriptor != null);
|
assertNotNull(devDescriptor);
|
||||||
assertEquals(devinfo.getIdProduct(), devDescriptor.getIdProduct());
|
assertEquals(devinfo.getIdProduct(), devDescriptor.getIdProduct());
|
||||||
assertEquals(devinfo.getIdVendor(), devDescriptor.getIdVendor());
|
assertEquals(devinfo.getIdVendor(), devDescriptor.getIdVendor());
|
||||||
Usb_Config_Descriptor confDescriptors[] = dev.getConfigDescriptors();
|
Usb_Config_Descriptor confDescriptors[] = dev.getConfigDescriptors();
|
||||||
assertTrue(confDescriptors != null);
|
assertNotNull(confDescriptors);
|
||||||
assertTrue(confDescriptors[0].getInterface().length > 0);
|
assertTrue(confDescriptors[0].getInterface().length > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,6 +158,20 @@ public class DeviceTest {
|
|||||||
doClose();
|
doClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void openWithFilename() throws Exception {
|
||||||
|
// get device by filename
|
||||||
|
doOpen();
|
||||||
|
assertNotNull(dev.getDevice());
|
||||||
|
String oldFilename = dev.getDevice().getFilename();
|
||||||
|
assertNotNull(oldFilename);
|
||||||
|
log.info("Filename: " + oldFilename);
|
||||||
|
Device dev2 = USB.getDevice(devinfo.getIdVendor(), devinfo
|
||||||
|
.getIdProduct(), oldFilename);
|
||||||
|
assertEquals(dev, dev2);
|
||||||
|
doClose();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void bulkWriteRead() throws Exception {
|
public void bulkWriteRead() throws Exception {
|
||||||
checkBulkEndpoints();
|
checkBulkEndpoints();
|
||||||
@@ -224,8 +257,8 @@ public class DeviceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void controlMsg() throws Exception {
|
public void controlMsg() throws Exception {
|
||||||
try {
|
try {
|
||||||
dev.open(devinfo.getConfiguration(), devinfo.getInterface(), devinfo
|
dev.open(devinfo.getConfiguration(), devinfo.getInterface(),
|
||||||
.getAltinterface());
|
devinfo.getAltinterface());
|
||||||
// GET STATUS (device)
|
// GET STATUS (device)
|
||||||
byte[] data = getTestData(2);
|
byte[] data = getTestData(2);
|
||||||
int length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
|
int length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
|
||||||
@@ -237,10 +270,11 @@ public class DeviceTest {
|
|||||||
assertEquals((byte) 0x00, data[1]);
|
assertEquals((byte) 0x00, data[1]);
|
||||||
// GET STATUS (interface)
|
// GET STATUS (interface)
|
||||||
data = getTestData(2);
|
data = getTestData(2);
|
||||||
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
|
length = dev.controlMsg(
|
||||||
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_INTERFACE,
|
USB.REQ_TYPE_DIR_DEVICE_TO_HOST
|
||||||
USB.REQ_GET_STATUS, 0, 0, data, data.length, devinfo
|
| USB.REQ_TYPE_TYPE_STANDARD
|
||||||
.getTimeout(), false);
|
| USB.REQ_TYPE_RECIP_INTERFACE, USB.REQ_GET_STATUS,
|
||||||
|
0, 0, data, data.length, devinfo.getTimeout(), false);
|
||||||
assertTrue(length > 0);
|
assertTrue(length > 0);
|
||||||
assertEquals((byte) 0x00, data[0]);
|
assertEquals((byte) 0x00, data[0]);
|
||||||
assertEquals((byte) 0x00, data[1]);
|
assertEquals((byte) 0x00, data[1]);
|
||||||
@@ -265,7 +299,8 @@ public class DeviceTest {
|
|||||||
// data = byte[1];
|
// data = byte[1];
|
||||||
// length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
|
// length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
|
||||||
// | USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_INTERFACE,
|
// | USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_INTERFACE,
|
||||||
// USB.REQ_GET_INTERFACE, 0, devinfo.getInterface(), data, data.length,
|
// USB.REQ_GET_INTERFACE, 0, devinfo.getInterface(), data,
|
||||||
|
// data.length,
|
||||||
// devinfo
|
// devinfo
|
||||||
// .getTimeout(), false);
|
// .getTimeout(), false);
|
||||||
// logData(data, length);
|
// logData(data, length);
|
||||||
@@ -273,8 +308,8 @@ public class DeviceTest {
|
|||||||
data = getTestData(128);
|
data = getTestData(128);
|
||||||
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
|
length = dev.controlMsg(USB.REQ_TYPE_DIR_DEVICE_TO_HOST
|
||||||
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
|
| USB.REQ_TYPE_TYPE_STANDARD | USB.REQ_TYPE_RECIP_DEVICE,
|
||||||
USB.REQ_GET_DESCRIPTOR, 1 << 8, 0, data, data.length, devinfo
|
USB.REQ_GET_DESCRIPTOR, 1 << 8, 0, data, data.length,
|
||||||
.getTimeout(), false);
|
devinfo.getTimeout(), false);
|
||||||
validateDeviceDescriptor(data, length);
|
validateDeviceDescriptor(data, length);
|
||||||
// GET DESCRIPTOR (string descriptor (1))
|
// GET DESCRIPTOR (string descriptor (1))
|
||||||
data = getTestData(128);
|
data = getTestData(128);
|
||||||
@@ -369,14 +404,15 @@ public class DeviceTest {
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
private void logData(byte[] data, int length) {
|
private void logData(byte[] data, int length) {
|
||||||
if (length > 0) {
|
if (length > 0) {
|
||||||
System.out.println("length: " + length);
|
log.info("length: " + length);
|
||||||
|
String logData = "";
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
System.out.print("0x" + Integer.toHexString(data[i] & 0xff)
|
logData += "0x" + Integer.toHexString(data[i] & 0xff) + "\t";
|
||||||
+ "\t");
|
|
||||||
}
|
}
|
||||||
System.out.println();
|
log.info(logData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,8 +435,7 @@ public class DeviceTest {
|
|||||||
devinfo.getAltinterface());
|
devinfo.getAltinterface());
|
||||||
fail("USBException expected");
|
fail("USBException expected");
|
||||||
} catch (USBException e) {
|
} catch (USBException e) {
|
||||||
System.err.println("INFO: " + getClass()
|
log.severe("could not set config "
|
||||||
+ ": error expected: could not set config "
|
|
||||||
+ (devinfo.getConfiguration() + 5));
|
+ (devinfo.getConfiguration() + 5));
|
||||||
}
|
}
|
||||||
doOpenWriteReadClose();
|
doOpenWriteReadClose();
|
||||||
@@ -413,8 +448,7 @@ public class DeviceTest {
|
|||||||
devinfo.getAltinterface());
|
devinfo.getAltinterface());
|
||||||
fail("USBException expected");
|
fail("USBException expected");
|
||||||
} catch (USBException e) {
|
} catch (USBException e) {
|
||||||
System.err.println("INFO: " + getClass()
|
log.severe("could not claim interface "
|
||||||
+ ": error expected: could not claim interface "
|
|
||||||
+ (devinfo.getInterface() + 5));
|
+ (devinfo.getInterface() + 5));
|
||||||
}
|
}
|
||||||
doOpenWriteReadClose();
|
doOpenWriteReadClose();
|
||||||
@@ -427,8 +461,7 @@ public class DeviceTest {
|
|||||||
devinfo.getAltinterface() + 5);
|
devinfo.getAltinterface() + 5);
|
||||||
fail("USBException expected");
|
fail("USBException expected");
|
||||||
} catch (USBException e) {
|
} catch (USBException e) {
|
||||||
System.err.println("INFO: " + getClass()
|
log.severe("could not set alt interface "
|
||||||
+ ": error expected: could not set alt interface "
|
|
||||||
+ (devinfo.getAltinterface() + 5));
|
+ (devinfo.getAltinterface() + 5));
|
||||||
}
|
}
|
||||||
doOpenWriteReadClose();
|
doOpenWriteReadClose();
|
||||||
@@ -436,33 +469,33 @@ public class DeviceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetIdProduct() {
|
public void testGetIdProduct() {
|
||||||
Assert.assertEquals(dev.getIdProduct(), devinfo.getIdProduct());
|
Assert.assertEquals(devinfo.getIdProduct(), dev.getIdProduct());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetIdVendor() {
|
public void testGetIdVendor() {
|
||||||
Assert.assertEquals(dev.getIdVendor(), devinfo.getIdVendor());
|
Assert.assertEquals(devinfo.getIdVendor(), dev.getIdVendor());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAltinterface() {
|
public void testGetAltinterface() {
|
||||||
Assert.assertEquals(dev.getAltinterface(), devinfo.getAltinterface());
|
Assert.assertEquals(devinfo.getAltinterface(), dev.getAltinterface());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetConfiguration() {
|
public void testGetConfiguration() {
|
||||||
Assert.assertEquals(dev.getConfiguration(), devinfo.getConfiguration());
|
Assert.assertEquals(devinfo.getConfiguration(), dev.getConfiguration());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetInterface() {
|
public void testGetInterface() {
|
||||||
Assert.assertEquals(dev.getInterface(), devinfo.getInterface());
|
Assert.assertEquals(devinfo.getInterface(), dev.getInterface());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetMaxPacketSize() throws USBException {
|
public void testGetMaxPacketSize() throws USBException {
|
||||||
doOpen();
|
doOpen();
|
||||||
Assert.assertEquals(dev.getMaxPacketSize(), devinfo.getMaxDataSize());
|
Assert.assertEquals(devinfo.getMaxDataSize(), dev.getMaxPacketSize());
|
||||||
doClose();
|
doClose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,7 +528,8 @@ public class DeviceTest {
|
|||||||
if (devinfo.getOutEPBulk() != -1) {
|
if (devinfo.getOutEPBulk() != -1) {
|
||||||
dev.writeBulk(devinfo.getOutEPBulk(), testData,
|
dev.writeBulk(devinfo.getOutEPBulk(), testData,
|
||||||
testData.length, devinfo.getTimeout(), false);
|
testData.length, devinfo.getTimeout(), false);
|
||||||
} else if (devinfo.getInEPBulk() != -1) {
|
}
|
||||||
|
if (devinfo.getInEPBulk() != -1) {
|
||||||
dev.readBulk(devinfo.getInEPBulk(), readData,
|
dev.readBulk(devinfo.getInEPBulk(), readData,
|
||||||
readData.length, devinfo.getTimeout(), false);
|
readData.length, devinfo.getTimeout(), false);
|
||||||
}
|
}
|
||||||
@@ -503,18 +537,22 @@ public class DeviceTest {
|
|||||||
if (devinfo.getOutEPInt() != -1) {
|
if (devinfo.getOutEPInt() != -1) {
|
||||||
dev.writeInterrupt(devinfo.getOutEPInt(), testData,
|
dev.writeInterrupt(devinfo.getOutEPInt(), testData,
|
||||||
testData.length, devinfo.getTimeout(), false);
|
testData.length, devinfo.getTimeout(), false);
|
||||||
} else if (devinfo.getInEPInt() != -1) {
|
}
|
||||||
|
if (devinfo.getInEPInt() != -1) {
|
||||||
dev.readInterrupt(devinfo.getInEPInt(), readData,
|
dev.readInterrupt(devinfo.getInEPInt(), readData,
|
||||||
readData.length, devinfo.getTimeout(), false);
|
readData.length, devinfo.getTimeout(), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (devinfo.doCompareData()) {
|
||||||
|
compare(testData, readData);
|
||||||
|
}
|
||||||
|
} catch (AssertionError e) {
|
||||||
|
closeOnException();
|
||||||
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
closeOnException();
|
closeOnException();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
if (devinfo.doCompareData()) {
|
|
||||||
compare(testData, readData);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void compare(byte[] d1, byte[] d2) {
|
private static void compare(byte[] d1, byte[] d2) {
|
||||||
|
|||||||
Reference in New Issue
Block a user