- associate descriptors with device
- update and get descriptors git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@198 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
@@ -26,9 +26,14 @@ public class Device {
|
|||||||
|
|
||||||
private boolean resetOnFirstOpen, resetDone;
|
private boolean resetOnFirstOpen, resetDone;
|
||||||
|
|
||||||
private int resetTimeout = 1000;
|
private int resetTimeout = 2000;
|
||||||
|
|
||||||
|
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;
|
||||||
@@ -36,6 +41,111 @@ public class Device {
|
|||||||
this.idProduct = idProduct;
|
this.idProduct = idProduct;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void initUSB() {
|
||||||
|
LibusbWin.usb_init();
|
||||||
|
initUSBDone = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Usb_Bus initBus() throws USBException {
|
||||||
|
LibusbWin.usb_find_busses();
|
||||||
|
LibusbWin.usb_find_devices();
|
||||||
|
|
||||||
|
Usb_Bus bus = LibusbWin.usb_get_busses();
|
||||||
|
if (bus == null) {
|
||||||
|
throw new USBException("LibusbWin.usb_get_busses(): "
|
||||||
|
+ LibusbWin.usb_strerror());
|
||||||
|
}
|
||||||
|
|
||||||
|
return bus;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateMaxPacketSize(Usb_Device device) throws USBException {
|
||||||
|
maxPacketSize = -1;
|
||||||
|
Usb_Config_Descriptor[] confDesc = device.config;
|
||||||
|
for (int i = 0; i < confDesc.length; i++) {
|
||||||
|
Usb_Interface[] int_ = confDesc[i].interface_;
|
||||||
|
for (int j = 0; j < int_.length; j++) {
|
||||||
|
Usb_Interface_Descriptor[] intDesc = int_[j].altsetting;
|
||||||
|
for (int k = 0; k < intDesc.length; k++) {
|
||||||
|
Usb_Endpoint_Descriptor[] epDesc = intDesc[k].endpoint;
|
||||||
|
for (int l = 0; l < epDesc.length; l++) {
|
||||||
|
maxPacketSize = Math.max(epDesc[l].wMaxPacketSize,
|
||||||
|
maxPacketSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (maxPacketSize <= 0) {
|
||||||
|
throw new USBException(
|
||||||
|
"No USB endpoints found. Check the device configuration");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Usb_Device initDevice() throws USBException {
|
||||||
|
Usb_Bus bus = initBus();
|
||||||
|
|
||||||
|
Usb_Device device = null;
|
||||||
|
// search for device
|
||||||
|
while (bus != null) {
|
||||||
|
device = bus.devices;
|
||||||
|
while (device != null) {
|
||||||
|
Usb_Device_Descriptor devDesc = device.descriptor;
|
||||||
|
if ((devDesc.idVendor == idVendor)
|
||||||
|
&& (devDesc.idProduct == idProduct)) {
|
||||||
|
logger.info("Device found: " + device.filename);
|
||||||
|
updateMaxPacketSize(device);
|
||||||
|
return device;
|
||||||
|
}
|
||||||
|
device = device.next;
|
||||||
|
}
|
||||||
|
bus = bus.next;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the device and descriptor information from the bus.<br>
|
||||||
|
* The descriptors can be read with {@link #getDeviceDescriptor()} and
|
||||||
|
* {@link #getConfigDescriptors()}.
|
||||||
|
*
|
||||||
|
* @throws USBException
|
||||||
|
*/
|
||||||
|
public void updateDescriptors() throws USBException {
|
||||||
|
if (!initUSBDone)
|
||||||
|
initUSB();
|
||||||
|
dev = initDevice();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the device descriptor associated with this device.<br>
|
||||||
|
* The descriptor is updated by calling {@link #updateDescriptors()} or
|
||||||
|
* {@link #open(int, int, int)}.
|
||||||
|
*
|
||||||
|
* @return the device descriptor associated with this device or
|
||||||
|
* <code>null</code>
|
||||||
|
*/
|
||||||
|
public Usb_Device_Descriptor getDeviceDescriptor() {
|
||||||
|
if (dev == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return dev.descriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the configuration descriptors associated with this device.<br>
|
||||||
|
* The descriptors are updated by calling {@link #updateDescriptors()} or
|
||||||
|
* {@link #open(int, int, int)}.
|
||||||
|
*
|
||||||
|
* @return the configuration descriptors associated with this device or
|
||||||
|
* <code>null</code>
|
||||||
|
*/
|
||||||
|
public Usb_Config_Descriptor[] getConfigDescriptors() {
|
||||||
|
if (dev == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return dev.config;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens the device and claims the specified configuration, interface and
|
* Opens the device and claims the specified configuration, interface and
|
||||||
* altinterface.<br>
|
* altinterface.<br>
|
||||||
@@ -58,65 +168,24 @@ public class Device {
|
|||||||
this.dev_interface = interface_;
|
this.dev_interface = interface_;
|
||||||
this.dev_altinterface = altinterface;
|
this.dev_altinterface = altinterface;
|
||||||
|
|
||||||
Usb_Bus bus;
|
|
||||||
|
|
||||||
if (usbDevHandle > 0) {
|
if (usbDevHandle > 0) {
|
||||||
throw new USBException("device opened, close or reset first");
|
throw new USBException("device opened, close or reset first");
|
||||||
}
|
}
|
||||||
|
|
||||||
// open bus
|
initUSB();
|
||||||
LibusbWin.usb_init();
|
|
||||||
LibusbWin.usb_find_busses();
|
|
||||||
LibusbWin.usb_find_devices();
|
|
||||||
|
|
||||||
bus = LibusbWin.usb_get_busses();
|
dev = initDevice();
|
||||||
if (bus == null) {
|
|
||||||
throw new USBException("LibusbWin.usb_get_busses(): "
|
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
|
||||||
|
|
||||||
maxPacketSize = -1;
|
if (dev != null) {
|
||||||
|
|
||||||
// search for device
|
|
||||||
while (bus != null) {
|
|
||||||
Usb_Device dev = bus.devices;
|
|
||||||
while (dev != null) {
|
|
||||||
Usb_Device_Descriptor devDesc = dev.descriptor;
|
|
||||||
if ((devDesc.idVendor == idVendor)
|
|
||||||
&& (devDesc.idProduct == idProduct)) {
|
|
||||||
logger.info("Open device: " + dev.filename);
|
|
||||||
int res = LibusbWin.usb_open(dev);
|
int res = LibusbWin.usb_open(dev);
|
||||||
if (res <= 0) {
|
if (res <= 0) {
|
||||||
throw new USBException("LibusbWin.usb_open: "
|
throw new USBException("LibusbWin.usb_open: "
|
||||||
+ LibusbWin.usb_strerror());
|
+ LibusbWin.usb_strerror());
|
||||||
}
|
}
|
||||||
usbDevHandle = res;
|
usbDevHandle = res;
|
||||||
// get endpoint wMaxPacketSize
|
|
||||||
Usb_Config_Descriptor[] confDesc = dev.config;
|
|
||||||
for (int i = 0; i < confDesc.length; i++) {
|
|
||||||
Usb_Interface[] int_ = confDesc[i].interface_;
|
|
||||||
for (int j = 0; j < int_.length; j++) {
|
|
||||||
Usb_Interface_Descriptor[] intDesc = int_[j].altsetting;
|
|
||||||
for (int k = 0; k < intDesc.length; k++) {
|
|
||||||
Usb_Endpoint_Descriptor[] epDesc = intDesc[k].endpoint;
|
|
||||||
for (int l = 0; l < epDesc.length; l++) {
|
|
||||||
maxPacketSize = Math.max(
|
|
||||||
epDesc[l].wMaxPacketSize,
|
|
||||||
maxPacketSize);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
if (dev == null || usbDevHandle <= 0) {
|
||||||
}
|
|
||||||
if (maxPacketSize <= 0) {
|
|
||||||
throw new USBException(
|
|
||||||
"No USB endpoints found. Check the device configuration");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dev = dev.next;
|
|
||||||
}
|
|
||||||
bus = bus.next;
|
|
||||||
}
|
|
||||||
if (usbDevHandle <= 0) {
|
|
||||||
throw new USBException("USB device with idVendor 0x"
|
throw new USBException("USB device with idVendor 0x"
|
||||||
+ Integer.toHexString(idVendor & 0xFFFF)
|
+ Integer.toHexString(idVendor & 0xFFFF)
|
||||||
+ " and idProduct 0x"
|
+ " and idProduct 0x"
|
||||||
@@ -492,8 +561,8 @@ public class Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the alternative interface. This value is only valid after opening
|
* Returns the alternative interface.<br>
|
||||||
* the device.<br>
|
* This value is only valid after opening the device.
|
||||||
*
|
*
|
||||||
* @return the alternative interface. This value is only valid after opening
|
* @return the alternative interface. This value is only valid after opening
|
||||||
* the device.
|
* the device.
|
||||||
@@ -503,8 +572,8 @@ public class Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current configuration used. This value is only valid after
|
* Returns the current configuration used.<br>
|
||||||
* opening the device.<br>
|
* This value is only valid after opening the device.
|
||||||
*
|
*
|
||||||
* @return the current configuration used. This value is only valid after
|
* @return the current configuration used. This value is only valid after
|
||||||
* opening the device.
|
* opening the device.
|
||||||
@@ -514,8 +583,8 @@ public class Device {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current interface. This value is only valid after opening the
|
* Returns the current interface.<br>
|
||||||
* device.<br>
|
* This value is only valid after opening the device.
|
||||||
*
|
*
|
||||||
* @return the current interface. This value is only valid after opening the
|
* @return the current interface. This value is only valid after opening the
|
||||||
* device.
|
* device.
|
||||||
@@ -524,17 +593,6 @@ public class Device {
|
|||||||
return dev_interface;
|
return dev_interface;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the current device handle. This value is only valid after opening
|
|
||||||
* the device.<br>
|
|
||||||
*
|
|
||||||
* @return the current device handle. This value is only valid after opening
|
|
||||||
* the device.
|
|
||||||
*/
|
|
||||||
public int getUsbDevHandle() {
|
|
||||||
return usbDevHandle;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the maximum packet size in bytes which is allowed to be
|
* Returns the maximum packet size in bytes which is allowed to be
|
||||||
* transmitted at once.<br>
|
* transmitted at once.<br>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package ch.ntb.usb.test;
|
package ch.ntb.usb.test;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -16,6 +17,8 @@ import ch.ntb.usb.Device;
|
|||||||
import ch.ntb.usb.LibusbWin;
|
import ch.ntb.usb.LibusbWin;
|
||||||
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_Config_Descriptor;
|
||||||
|
import ch.ntb.usb.Usb_Device_Descriptor;
|
||||||
import ch.ntb.usb.test.AbstractDeviceInfo.WriteMode;
|
import ch.ntb.usb.test.AbstractDeviceInfo.WriteMode;
|
||||||
|
|
||||||
public class DeviceTest {
|
public class DeviceTest {
|
||||||
@@ -32,6 +35,7 @@ public class DeviceTest {
|
|||||||
|
|
||||||
private static Device dev;
|
private static Device dev;
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUp() throws Exception {
|
public static void setUp() throws Exception {
|
||||||
// load the device info class with the key
|
// load the device info class with the key
|
||||||
@@ -56,6 +60,19 @@ public class DeviceTest {
|
|||||||
dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct());
|
dev = USB.getDevice(devinfo.getIdVendor(), devinfo.getIdProduct());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("null")
|
||||||
|
@Test
|
||||||
|
public void getDescriptors() throws Exception {
|
||||||
|
dev.updateDescriptors();
|
||||||
|
Usb_Device_Descriptor devDescriptor = dev.getDeviceDescriptor();
|
||||||
|
assertTrue(devDescriptor != null);
|
||||||
|
assertEquals(devinfo.getIdProduct(), devDescriptor.idProduct);
|
||||||
|
assertEquals(devinfo.getIdVendor(), devDescriptor.idVendor);
|
||||||
|
Usb_Config_Descriptor confDescriptors[] = dev.getConfigDescriptors();
|
||||||
|
assertTrue(confDescriptors != null);
|
||||||
|
assertTrue(confDescriptors[0].interface_.length > 0);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void initalReset() throws Exception {
|
public void initalReset() throws Exception {
|
||||||
doOpen();
|
doOpen();
|
||||||
@@ -233,8 +250,7 @@ public class DeviceTest {
|
|||||||
private static void compare(byte[] d1, byte[] d2) {
|
private static void compare(byte[] d1, byte[] d2) {
|
||||||
int minLength = Math.min(d1.length, d2.length);
|
int minLength = Math.min(d1.length, d2.length);
|
||||||
for (int i = 0; i < minLength; i++) {
|
for (int i = 0; i < minLength; i++) {
|
||||||
if (d1[i] != d2[i])
|
assertEquals(d1[i], d2[i]);
|
||||||
fail("received data not equal to sent data");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user