- isOpen added
git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@183 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
@@ -1,16 +1,17 @@
|
|||||||
package ch.ntb.usb;
|
package ch.ntb.usb;
|
||||||
|
|
||||||
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import ch.ntb.usb.logger.LogUtil;
|
import ch.ntb.usb.logger.LogUtil;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class represents an USB device.<br>
|
* This class represents an USB device.<br>
|
||||||
* To get an instance of an USB device use <code>USB.getDevice(...)</code>.
|
* To get an instance of an USB device use <code>USB.getDevice(...)</code>.
|
||||||
*
|
*
|
||||||
* @author schlaepfer
|
* @author schlaepfer
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class Device {
|
public class Device {
|
||||||
|
|
||||||
@@ -29,6 +30,7 @@ public class Device {
|
|||||||
|
|
||||||
private int resetTimeout = 1000;
|
private int resetTimeout = 1000;
|
||||||
|
|
||||||
|
|
||||||
protected Device(short idVendor, short idProduct) {
|
protected Device(short idVendor, short idProduct) {
|
||||||
resetOnFirstOpen = false;
|
resetOnFirstOpen = false;
|
||||||
resetDone = false;
|
resetDone = false;
|
||||||
@@ -37,6 +39,7 @@ public class Device {
|
|||||||
this.idProduct = idProduct;
|
this.idProduct = idProduct;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens the device and claims the specified configuration, interface and
|
* Opens the device and claims the specified configuration, interface and
|
||||||
* altinterface.<br>
|
* altinterface.<br>
|
||||||
@@ -44,23 +47,19 @@ public class Device {
|
|||||||
* read and the <code>maxPacketSize</code> value is updated. If no
|
* read and the <code>maxPacketSize</code> value is updated. If no
|
||||||
* endpoints are found in the descriptors an exception is thrown.
|
* endpoints are found in the descriptors an exception is thrown.
|
||||||
*
|
*
|
||||||
* @param configuration
|
* @param configuration the configuration
|
||||||
* the configuration
|
* @param interface_ the interface
|
||||||
* @param interface_
|
* @param altinterface the alternative interface
|
||||||
* the interface
|
|
||||||
* @param altinterface
|
|
||||||
* the alternative interface
|
|
||||||
* @throws USBException
|
* @throws USBException
|
||||||
*/
|
*/
|
||||||
public void open(int configuration, int interface_, int altinterface)
|
public void open(int configuration, int interface_, int altinterface) throws USBException {
|
||||||
throws USBException {
|
|
||||||
this.dev_configuration = configuration;
|
this.dev_configuration = configuration;
|
||||||
this.dev_interface = interface_;
|
this.dev_interface = interface_;
|
||||||
this.dev_altinterface = altinterface;
|
this.dev_altinterface = altinterface;
|
||||||
|
|
||||||
Usb_Bus bus;
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,92 +69,85 @@ public class Device {
|
|||||||
LibusbWin.usb_find_devices();
|
LibusbWin.usb_find_devices();
|
||||||
|
|
||||||
bus = LibusbWin.usb_get_busses();
|
bus = LibusbWin.usb_get_busses();
|
||||||
if (bus == null) {
|
if(bus == null) {
|
||||||
throw new USBException("LibusbWin.usb_get_busses(): "
|
throw new USBException("LibusbWin.usb_get_busses(): " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
maxPacketSize = -1;
|
maxPacketSize = -1;
|
||||||
|
|
||||||
// search for device
|
// search for device
|
||||||
while (bus != null) {
|
while(bus != null) {
|
||||||
Usb_Device dev = bus.devices;
|
Usb_Device dev = bus.devices;
|
||||||
while (dev != null) {
|
while(dev != null) {
|
||||||
Usb_Device_Descriptor devDesc = dev.descriptor;
|
Usb_Device_Descriptor devDesc = dev.descriptor;
|
||||||
if ((devDesc.idVendor == idVendor)
|
if((devDesc.idVendor == idVendor) && (devDesc.idProduct == idProduct)) {
|
||||||
&& (devDesc.idProduct == idProduct)) {
|
|
||||||
logger.info("Open device: " + dev.filename);
|
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
|
// get endpoint wMaxPacketSize
|
||||||
Usb_Config_Descriptor[] confDesc = dev.config;
|
Usb_Config_Descriptor[] confDesc = dev.config;
|
||||||
for (int i = 0; i < confDesc.length; i++) {
|
for(int i = 0; i < confDesc.length; i++) {
|
||||||
Usb_Interface[] int_ = confDesc[i].interface_;
|
Usb_Interface[] int_ = confDesc[i].interface_;
|
||||||
for (int j = 0; j < int_.length; j++) {
|
for(int j = 0; j < int_.length; j++) {
|
||||||
Usb_Interface_Descriptor[] intDesc = int_[j].altsetting;
|
Usb_Interface_Descriptor[] intDesc = int_[j].altsetting;
|
||||||
for (int k = 0; k < intDesc.length; k++) {
|
for(int k = 0; k < intDesc.length; k++) {
|
||||||
Usb_Endpoint_Descriptor[] epDesc = intDesc[k].endpoint;
|
Usb_Endpoint_Descriptor[] epDesc = intDesc[k].endpoint;
|
||||||
for (int l = 0; l < epDesc.length; l++) {
|
for(int l = 0; l < epDesc.length; l++) {
|
||||||
maxPacketSize = Math.max(
|
maxPacketSize = Math.max(epDesc[l].wMaxPacketSize, maxPacketSize);
|
||||||
epDesc[l].wMaxPacketSize,
|
|
||||||
maxPacketSize);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (maxPacketSize <= 0) {
|
if(maxPacketSize <= 0) {
|
||||||
throw new USBException(
|
throw new USBException("No USB endpoints found. Check the device configuration");
|
||||||
"No USB endpoints found. Check the device configuration");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dev = dev.next;
|
dev = dev.next;
|
||||||
}
|
}
|
||||||
bus = bus.next;
|
bus = bus.next;
|
||||||
}
|
}
|
||||||
if (usbDevHandle <= 0) {
|
if(usbDevHandle <= 0) {
|
||||||
throw new USBException("USB device with idVendor 0x"
|
throw new USBException("USB device with idVendor 0x" + Integer.toHexString(idVendor & 0xFFFF) + " and idProduct 0x" + Integer.toHexString(idProduct & 0xFFFF) + " not found");
|
||||||
+ Integer.toHexString(idVendor & 0xFFFF)
|
|
||||||
+ " and idProduct 0x"
|
|
||||||
+ Integer.toHexString(idProduct & 0xFFFF) + " not found");
|
|
||||||
}
|
}
|
||||||
claim_interface(usbDevHandle, configuration, interface_, altinterface);
|
claim_interface(usbDevHandle, configuration, interface_, altinterface);
|
||||||
if (resetOnFirstOpen & !resetDone) {
|
if(resetOnFirstOpen & !resetDone) {
|
||||||
logger.info("reset on first open");
|
logger.info("reset on first open");
|
||||||
resetDone = true;
|
resetDone = true;
|
||||||
reset();
|
reset();
|
||||||
try {
|
try {
|
||||||
Thread.sleep(resetTimeout);
|
Thread.sleep(resetTimeout);
|
||||||
} catch (InterruptedException e) {
|
}
|
||||||
|
catch(InterruptedException e) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
open(configuration, interface_, altinterface);
|
open(configuration, interface_, altinterface);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Release the claimed interface and close the opened device.<br>
|
* Release the claimed interface and close the opened device.<br>
|
||||||
*
|
*
|
||||||
* @throws USBException
|
* @throws USBException
|
||||||
*/
|
*/
|
||||||
public void close() throws USBException {
|
public void close() throws USBException {
|
||||||
if (usbDevHandle <= 0) {
|
if(usbDevHandle <= 0) {
|
||||||
throw new USBException("invalid device handle");
|
throw new USBException("invalid device handle");
|
||||||
}
|
}
|
||||||
release_interface(usbDevHandle, dev_interface);
|
release_interface(usbDevHandle, dev_interface);
|
||||||
if (LibusbWin.usb_close(usbDevHandle) < 0) {
|
if(LibusbWin.usb_close(usbDevHandle) < 0) {
|
||||||
usbDevHandle = 0;
|
usbDevHandle = 0;
|
||||||
throw new USBException("LibusbWin.usb_close: "
|
throw new USBException("LibusbWin.usb_close: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
usbDevHandle = 0;
|
usbDevHandle = 0;
|
||||||
maxPacketSize = -1;
|
maxPacketSize = -1;
|
||||||
logger.info("device closed");
|
logger.info("device closed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends an USB reset to the device. The device handle will no longer be
|
* Sends an USB reset to the device. The device handle will no longer be
|
||||||
* valid. To use the device again, {@link #open(int, int, int)} must be
|
* valid. To use the device again, {@link #open(int, int, int)} must be
|
||||||
@@ -164,72 +156,61 @@ public class Device {
|
|||||||
* @throws USBException
|
* @throws USBException
|
||||||
*/
|
*/
|
||||||
public void reset() throws USBException {
|
public void reset() throws USBException {
|
||||||
if (usbDevHandle <= 0) {
|
if(usbDevHandle <= 0) {
|
||||||
throw new USBException("invalid device handle");
|
throw new USBException("invalid device handle");
|
||||||
}
|
}
|
||||||
if (LibusbWin.usb_reset(usbDevHandle) < 0) {
|
if(LibusbWin.usb_reset(usbDevHandle) < 0) {
|
||||||
usbDevHandle = 0;
|
usbDevHandle = 0;
|
||||||
throw new USBException("LibusbWin.usb_reset: "
|
throw new USBException("LibusbWin.usb_reset: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
usbDevHandle = 0;
|
usbDevHandle = 0;
|
||||||
logger.info("device reset");
|
logger.info("device reset");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write data to the device using a bulk transfer.<br>
|
* Write data to the device using a bulk transfer.<br>
|
||||||
*
|
*
|
||||||
* @param out_ep_address
|
* @param out_ep_address endpoint address to write to
|
||||||
* endpoint address to write to
|
* @param data data to write to this endpoint
|
||||||
* @param data
|
* @param length length of the data
|
||||||
* data to write to this endpoint
|
* @param timeout amount of time in ms the device will try to send the data
|
||||||
* @param length
|
|
||||||
* length of the data
|
|
||||||
* @param timeout
|
|
||||||
* amount of time in ms the device will try to send the data
|
|
||||||
* until a timeout exception is thrown
|
* until a timeout exception is thrown
|
||||||
* @param reopenOnTimeout
|
* @param reopenOnTimeout if set to true, the device will try to open the
|
||||||
* if set to true, the device will try to open the connection and
|
* connection and send the data again before a timeout exception
|
||||||
* send the data again before a timeout exception is thrown
|
* is thrown
|
||||||
* @return the actual number of bytes written
|
* @return the actual number of bytes written
|
||||||
* @throws USBException
|
* @throws USBException
|
||||||
*/
|
*/
|
||||||
public int writeBulk(int out_ep_address, byte[] data, int length,
|
public int writeBulk(int out_ep_address, byte[] data, int length, int timeout, boolean reopenOnTimeout) throws USBException {
|
||||||
int timeout, boolean reopenOnTimeout) throws USBException {
|
if(usbDevHandle <= 0) {
|
||||||
if (usbDevHandle <= 0) {
|
|
||||||
throw new USBException("invalid device handle");
|
throw new USBException("invalid device handle");
|
||||||
}
|
}
|
||||||
if (data == null) {
|
if(data == null) {
|
||||||
throw new USBException("data must not be null");
|
throw new USBException("data must not be null");
|
||||||
}
|
}
|
||||||
if (length <= 0) {
|
if(length <= 0) {
|
||||||
throw new USBException("size must be > 0");
|
throw new USBException("size must be > 0");
|
||||||
}
|
}
|
||||||
int lenWritten = LibusbWin.usb_bulk_write(usbDevHandle, out_ep_address,
|
int lenWritten = LibusbWin.usb_bulk_write(usbDevHandle, out_ep_address, data, length, timeout);
|
||||||
data, length, timeout);
|
if(lenWritten < 0) {
|
||||||
if (lenWritten < 0) {
|
if(lenWritten == TIMEOUT_ERROR_CODE) {
|
||||||
if (lenWritten == TIMEOUT_ERROR_CODE) {
|
|
||||||
// try to reopen the device and send the data again
|
// try to reopen the device and send the data again
|
||||||
if (reopenOnTimeout) {
|
if(reopenOnTimeout) {
|
||||||
logger.info("try to reopen");
|
logger.info("try to reopen");
|
||||||
reset();
|
reset();
|
||||||
open(dev_configuration, dev_interface, dev_altinterface);
|
open(dev_configuration, dev_interface, dev_altinterface);
|
||||||
return writeBulk(out_ep_address, data, length, timeout,
|
return writeBulk(out_ep_address, data, length, timeout, false);
|
||||||
false);
|
|
||||||
}
|
}
|
||||||
throw new USBTimeoutException("LibusbWin.usb_bulk_write: "
|
throw new USBTimeoutException("LibusbWin.usb_bulk_write: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
throw new USBException("LibusbWin.usb_bulk_write: "
|
throw new USBException("LibusbWin.usb_bulk_write: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("length written: " + lenWritten);
|
logger.info("length written: " + lenWritten);
|
||||||
if (logger.isLoggable(Level.FINEST)) {
|
if(logger.isLoggable(Level.FINEST)) {
|
||||||
StringBuffer sb = new StringBuffer("bulkwrite, ep 0x"
|
StringBuffer sb = new StringBuffer("bulkwrite, ep 0x" + Integer.toHexString(out_ep_address) + ": " + lenWritten + " Bytes sent: ");
|
||||||
+ Integer.toHexString(out_ep_address) + ": " + lenWritten
|
for(int i = 0; i < lenWritten; i++) {
|
||||||
+ " Bytes sent: ");
|
|
||||||
for (int i = 0; i < lenWritten; i++) {
|
|
||||||
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
|
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
|
||||||
}
|
}
|
||||||
logger.info(sb.toString());
|
logger.info(sb.toString());
|
||||||
@@ -237,59 +218,50 @@ public class Device {
|
|||||||
return lenWritten;
|
return lenWritten;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read data from the device using a bulk transfer.<br>
|
* Read data from the device using a bulk transfer.<br>
|
||||||
*
|
*
|
||||||
* @param in_ep_address
|
* @param in_ep_address endpoint address to read from
|
||||||
* endpoint address to read from
|
* @param data data buffer for the data to be read
|
||||||
* @param data
|
* @param size the maximum requested data size
|
||||||
* data buffer for the data to be read
|
* @param timeout amount of time in ms the device will try to receive data
|
||||||
* @param size
|
* until a timeout exception is thrown
|
||||||
* the maximum requested data size
|
* @param reopenOnTimeout if set to true, the device will try to open the
|
||||||
* @param timeout
|
* connection and receive the data again before a timeout
|
||||||
* amount of time in ms the device will try to receive data until
|
* exception is thrown
|
||||||
* a timeout exception is thrown
|
|
||||||
* @param reopenOnTimeout
|
|
||||||
* if set to true, the device will try to open the connection and
|
|
||||||
* receive the data again before a timeout exception is thrown
|
|
||||||
* @return the actual number of bytes read
|
* @return the actual number of bytes read
|
||||||
* @throws USBException
|
* @throws USBException
|
||||||
*/
|
*/
|
||||||
public int readBulk(int in_ep_address, byte[] data, int size, int timeout,
|
public int readBulk(int in_ep_address, byte[] data, int size, int timeout, boolean reopenOnTimeout) throws USBException {
|
||||||
boolean reopenOnTimeout) throws USBException {
|
if(usbDevHandle <= 0) {
|
||||||
if (usbDevHandle <= 0) {
|
|
||||||
throw new USBException("invalid device handle");
|
throw new USBException("invalid device handle");
|
||||||
}
|
}
|
||||||
if (data == null) {
|
if(data == null) {
|
||||||
throw new USBException("data must not be null");
|
throw new USBException("data must not be null");
|
||||||
}
|
}
|
||||||
if (size <= 0) {
|
if(size <= 0) {
|
||||||
throw new USBException("size must be > 0");
|
throw new USBException("size must be > 0");
|
||||||
}
|
}
|
||||||
int lenRead = LibusbWin.usb_bulk_read(usbDevHandle, in_ep_address,
|
int lenRead = LibusbWin.usb_bulk_read(usbDevHandle, in_ep_address, data, size, timeout);
|
||||||
data, size, timeout);
|
if(lenRead < 0) {
|
||||||
if (lenRead < 0) {
|
if(lenRead == TIMEOUT_ERROR_CODE) {
|
||||||
if (lenRead == TIMEOUT_ERROR_CODE) {
|
|
||||||
// try to reopen the device and send the data again
|
// try to reopen the device and send the data again
|
||||||
if (reopenOnTimeout) {
|
if(reopenOnTimeout) {
|
||||||
logger.info("try to reopen");
|
logger.info("try to reopen");
|
||||||
reset();
|
reset();
|
||||||
open(dev_configuration, dev_interface, dev_altinterface);
|
open(dev_configuration, dev_interface, dev_altinterface);
|
||||||
return readBulk(in_ep_address, data, size, timeout, false);
|
return readBulk(in_ep_address, data, size, timeout, false);
|
||||||
}
|
}
|
||||||
throw new USBTimeoutException("LibusbWin.usb_bulk_read: "
|
throw new USBTimeoutException("LibusbWin.usb_bulk_read: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
throw new USBException("LibusbWin.usb_bulk_read: "
|
throw new USBException("LibusbWin.usb_bulk_read: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("length read: " + lenRead);
|
logger.info("length read: " + lenRead);
|
||||||
if (logger.isLoggable(Level.FINEST)) {
|
if(logger.isLoggable(Level.FINEST)) {
|
||||||
StringBuffer sb = new StringBuffer("bulkread, ep 0x"
|
StringBuffer sb = new StringBuffer("bulkread, ep 0x" + Integer.toHexString(in_ep_address) + ": " + lenRead + " Bytes received: ");
|
||||||
+ Integer.toHexString(in_ep_address) + ": " + lenRead
|
for(int i = 0; i < lenRead; i++) {
|
||||||
+ " Bytes received: ");
|
|
||||||
for (int i = 0; i < lenRead; i++) {
|
|
||||||
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
|
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
|
||||||
}
|
}
|
||||||
logger.info(sb.toString());
|
logger.info(sb.toString());
|
||||||
@@ -297,60 +269,50 @@ public class Device {
|
|||||||
return lenRead;
|
return lenRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write data to the device using a interrupt transfer.<br>
|
* Write data to the device using a interrupt transfer.<br>
|
||||||
*
|
*
|
||||||
* @param out_ep_address
|
* @param out_ep_address endpoint address to write to
|
||||||
* endpoint address to write to
|
* @param data data to write to this endpoint
|
||||||
* @param data
|
* @param length length of the data
|
||||||
* data to write to this endpoint
|
* @param timeout amount of time in ms the device will try to send the data
|
||||||
* @param length
|
|
||||||
* length of the data
|
|
||||||
* @param timeout
|
|
||||||
* amount of time in ms the device will try to send the data
|
|
||||||
* until a timeout exception is thrown
|
* until a timeout exception is thrown
|
||||||
* @param reopenOnTimeout
|
* @param reopenOnTimeout if set to true, the device will try to open the
|
||||||
* if set to true, the device will try to open the connection and
|
* connection and send the data again before a timeout exception
|
||||||
* send the data again before a timeout exception is thrown
|
* is thrown
|
||||||
* @return the actual number of bytes written
|
* @return the actual number of bytes written
|
||||||
* @throws USBException
|
* @throws USBException
|
||||||
*/
|
*/
|
||||||
public int writeInterrupt(int out_ep_address, byte[] data, int length,
|
public int writeInterrupt(int out_ep_address, byte[] data, int length, int timeout, boolean reopenOnTimeout) throws USBException {
|
||||||
int timeout, boolean reopenOnTimeout) throws USBException {
|
if(usbDevHandle <= 0) {
|
||||||
if (usbDevHandle <= 0) {
|
|
||||||
throw new USBException("invalid device handle");
|
throw new USBException("invalid device handle");
|
||||||
}
|
}
|
||||||
if (data == null) {
|
if(data == null) {
|
||||||
throw new USBException("data must not be null");
|
throw new USBException("data must not be null");
|
||||||
}
|
}
|
||||||
if (length <= 0) {
|
if(length <= 0) {
|
||||||
throw new USBException("size must be > 0");
|
throw new USBException("size must be > 0");
|
||||||
}
|
}
|
||||||
int lenWritten = LibusbWin.usb_interrupt_write(usbDevHandle,
|
int lenWritten = LibusbWin.usb_interrupt_write(usbDevHandle, out_ep_address, data, length, timeout);
|
||||||
out_ep_address, data, length, timeout);
|
if(lenWritten < 0) {
|
||||||
if (lenWritten < 0) {
|
if(lenWritten == TIMEOUT_ERROR_CODE) {
|
||||||
if (lenWritten == TIMEOUT_ERROR_CODE) {
|
|
||||||
// try to reopen the device and send the data again
|
// try to reopen the device and send the data again
|
||||||
if (reopenOnTimeout) {
|
if(reopenOnTimeout) {
|
||||||
logger.info("try to reopen");
|
logger.info("try to reopen");
|
||||||
reset();
|
reset();
|
||||||
open(dev_configuration, dev_interface, dev_altinterface);
|
open(dev_configuration, dev_interface, dev_altinterface);
|
||||||
return writeInterrupt(out_ep_address, data, length,
|
return writeInterrupt(out_ep_address, data, length, timeout, false);
|
||||||
timeout, false);
|
|
||||||
}
|
}
|
||||||
throw new USBTimeoutException("LibusbWin.usb_bulk_write: "
|
throw new USBTimeoutException("LibusbWin.usb_bulk_write: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
throw new USBException("LibusbWin.usb_bulk_write: "
|
throw new USBException("LibusbWin.usb_bulk_write: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("length written: " + lenWritten);
|
logger.info("length written: " + lenWritten);
|
||||||
if (logger.getLevel().intValue() <= Level.FINEST.intValue()) {
|
if(logger.getLevel().intValue() <= Level.FINEST.intValue()) {
|
||||||
StringBuffer sb = new StringBuffer("bulkwrite, ep 0x"
|
StringBuffer sb = new StringBuffer("bulkwrite, ep 0x" + Integer.toHexString(out_ep_address) + ": " + lenWritten + " Bytes sent: ");
|
||||||
+ Integer.toHexString(out_ep_address) + ": " + lenWritten
|
for(int i = 0; i < lenWritten; i++) {
|
||||||
+ " Bytes sent: ");
|
|
||||||
for (int i = 0; i < lenWritten; i++) {
|
|
||||||
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
|
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
|
||||||
}
|
}
|
||||||
logger.info(sb.toString());
|
logger.info(sb.toString());
|
||||||
@@ -358,60 +320,50 @@ public class Device {
|
|||||||
return lenWritten;
|
return lenWritten;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read data from the device using a interrupt transfer.<br>
|
* Read data from the device using a interrupt transfer.<br>
|
||||||
*
|
*
|
||||||
* @param in_ep_address
|
* @param in_ep_address endpoint address to read from
|
||||||
* endpoint address to read from
|
* @param data data buffer for the data to be read
|
||||||
* @param data
|
* @param size the maximum requested data size
|
||||||
* data buffer for the data to be read
|
* @param timeout amount of time in ms the device will try to receive data
|
||||||
* @param size
|
* until a timeout exception is thrown
|
||||||
* the maximum requested data size
|
* @param reopenOnTimeout if set to true, the device will try to open the
|
||||||
* @param timeout
|
* connection and receive the data again before a timeout
|
||||||
* amount of time in ms the device will try to receive data until
|
* exception is thrown
|
||||||
* a timeout exception is thrown
|
|
||||||
* @param reopenOnTimeout
|
|
||||||
* if set to true, the device will try to open the connection and
|
|
||||||
* receive the data again before a timeout exception is thrown
|
|
||||||
* @return the actual number of bytes read
|
* @return the actual number of bytes read
|
||||||
* @throws USBException
|
* @throws USBException
|
||||||
*/
|
*/
|
||||||
public int readInterrupt(int in_ep_address, byte[] data, int size,
|
public int readInterrupt(int in_ep_address, byte[] data, int size, int timeout, boolean reopenOnTimeout) throws USBException {
|
||||||
int timeout, boolean reopenOnTimeout) throws USBException {
|
if(usbDevHandle <= 0) {
|
||||||
if (usbDevHandle <= 0) {
|
|
||||||
throw new USBException("invalid device handle");
|
throw new USBException("invalid device handle");
|
||||||
}
|
}
|
||||||
if (data == null) {
|
if(data == null) {
|
||||||
throw new USBException("data must not be null");
|
throw new USBException("data must not be null");
|
||||||
}
|
}
|
||||||
if (size <= 0) {
|
if(size <= 0) {
|
||||||
throw new USBException("size must be > 0");
|
throw new USBException("size must be > 0");
|
||||||
}
|
}
|
||||||
int lenRead = LibusbWin.usb_interrupt_read(usbDevHandle, in_ep_address,
|
int lenRead = LibusbWin.usb_interrupt_read(usbDevHandle, in_ep_address, data, size, timeout);
|
||||||
data, size, timeout);
|
if(lenRead < 0) {
|
||||||
if (lenRead < 0) {
|
if(lenRead == TIMEOUT_ERROR_CODE) {
|
||||||
if (lenRead == TIMEOUT_ERROR_CODE) {
|
|
||||||
// try to reopen the device and send the data again
|
// try to reopen the device and send the data again
|
||||||
if (reopenOnTimeout) {
|
if(reopenOnTimeout) {
|
||||||
logger.info("try to reopen");
|
logger.info("try to reopen");
|
||||||
reset();
|
reset();
|
||||||
open(dev_configuration, dev_interface, dev_altinterface);
|
open(dev_configuration, dev_interface, dev_altinterface);
|
||||||
return readInterrupt(in_ep_address, data, size, timeout,
|
return readInterrupt(in_ep_address, data, size, timeout, false);
|
||||||
false);
|
|
||||||
}
|
}
|
||||||
throw new USBTimeoutException("LibusbWin.usb_bulk_read: "
|
throw new USBTimeoutException("LibusbWin.usb_bulk_read: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
throw new USBException("LibusbWin.usb_bulk_read: "
|
throw new USBException("LibusbWin.usb_bulk_read: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("length read: " + lenRead);
|
logger.info("length read: " + lenRead);
|
||||||
if (logger.getLevel().intValue() <= Level.FINEST.intValue()) {
|
if(logger.getLevel().intValue() <= Level.FINEST.intValue()) {
|
||||||
StringBuffer sb = new StringBuffer("bulkread, ep 0x"
|
StringBuffer sb = new StringBuffer("bulkread, ep 0x" + Integer.toHexString(in_ep_address) + ": " + lenRead + " Bytes received: ");
|
||||||
+ Integer.toHexString(in_ep_address) + ": " + lenRead
|
for(int i = 0; i < lenRead; i++) {
|
||||||
+ " Bytes received: ");
|
|
||||||
for (int i = 0; i < lenRead; i++) {
|
|
||||||
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
|
sb.append("0x" + String.format("%1$02X", data[i]) + " ");
|
||||||
}
|
}
|
||||||
logger.info(sb.toString());
|
logger.info(sb.toString());
|
||||||
@@ -419,57 +371,46 @@ public class Device {
|
|||||||
return lenRead;
|
return lenRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Claim an interface to send and receive USB data.<br>
|
* Claim an interface to send and receive USB data.<br>
|
||||||
*
|
*
|
||||||
* @param usb_dev_handle
|
* @param usb_dev_handle the handle of the device <b>(MUST BE VALID)</b>
|
||||||
* the handle of the device <b>(MUST BE VALID)</b>
|
* @param configuration the configuration to use
|
||||||
* @param configuration
|
* @param interface_ the interface to claim
|
||||||
* the configuration to use
|
* @param altinterface the alternative interface to use
|
||||||
* @param interface_
|
* @throws USBException throws an USBException if the action fails
|
||||||
* the interface to claim
|
|
||||||
* @param altinterface
|
|
||||||
* the alternative interface to use
|
|
||||||
* @throws USBException
|
|
||||||
* throws an USBException if the action fails
|
|
||||||
*/
|
*/
|
||||||
private void claim_interface(int usb_dev_handle, int configuration,
|
private void claim_interface(int usb_dev_handle, int configuration, int interface_, int altinterface) throws USBException {
|
||||||
int interface_, int altinterface) throws USBException {
|
if(LibusbWin.usb_set_configuration(usb_dev_handle, configuration) < 0) {
|
||||||
if (LibusbWin.usb_set_configuration(usb_dev_handle, configuration) < 0) {
|
throw new USBException("LibusbWin.usb_set_configuration: " + LibusbWin.usb_strerror());
|
||||||
throw new USBException("LibusbWin.usb_set_configuration: "
|
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
if (LibusbWin.usb_claim_interface(usb_dev_handle, interface_) < 0) {
|
if(LibusbWin.usb_claim_interface(usb_dev_handle, interface_) < 0) {
|
||||||
throw new USBException("LibusbWin.usb_claim_interface: "
|
throw new USBException("LibusbWin.usb_claim_interface: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
if (LibusbWin.usb_set_altinterface(usb_dev_handle, altinterface) < 0) {
|
if(LibusbWin.usb_set_altinterface(usb_dev_handle, altinterface) < 0) {
|
||||||
throw new USBException("LibusbWin.usb_set_altinterface: "
|
throw new USBException("LibusbWin.usb_set_altinterface: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
logger.info("interface claimed");
|
logger.info("interface claimed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Release a previously claimed interface.<br>
|
* Release a previously claimed interface.<br>
|
||||||
*
|
*
|
||||||
* @param dev_handle
|
* @param dev_handle the handle of the device <b>(MUST BE VALID)</b>
|
||||||
* the handle of the device <b>(MUST BE VALID)</b>
|
* @param interface_ the interface to claim
|
||||||
* @param interface_
|
* @throws USBException throws an USBException if the action fails
|
||||||
* the interface to claim
|
|
||||||
* @throws USBException
|
|
||||||
* throws an USBException if the action fails
|
|
||||||
*/
|
*/
|
||||||
private void release_interface(int dev_handle, int interface_)
|
private void release_interface(int dev_handle, int interface_) throws USBException {
|
||||||
throws USBException {
|
if(LibusbWin.usb_release_interface(dev_handle, interface_) < 0) {
|
||||||
if (LibusbWin.usb_release_interface(dev_handle, interface_) < 0) {
|
|
||||||
usbDevHandle = 0;
|
usbDevHandle = 0;
|
||||||
throw new USBException("LibusbWin.usb_release_interface: "
|
throw new USBException("LibusbWin.usb_release_interface: " + LibusbWin.usb_strerror());
|
||||||
+ LibusbWin.usb_strerror());
|
|
||||||
}
|
}
|
||||||
logger.info("interface released");
|
logger.info("interface released");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the product ID of the device.<br>
|
* Returns the product ID of the device.<br>
|
||||||
*
|
*
|
||||||
@@ -479,6 +420,7 @@ public class Device {
|
|||||||
return idProduct;
|
return idProduct;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the vendor ID of the device.<br>
|
* Returns the vendor ID of the device.<br>
|
||||||
*
|
*
|
||||||
@@ -488,6 +430,7 @@ public class Device {
|
|||||||
return idVendor;
|
return idVendor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the alternative interface. This value is only valid after opening
|
* Returns the alternative interface. This value is only valid after opening
|
||||||
* the device.<br>
|
* the device.<br>
|
||||||
@@ -499,6 +442,7 @@ public class Device {
|
|||||||
return dev_altinterface;
|
return dev_altinterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current configuration used. This value is only valid after
|
* Returns the current configuration used. This value is only valid after
|
||||||
* opening the device.<br>
|
* opening the device.<br>
|
||||||
@@ -510,6 +454,7 @@ public class Device {
|
|||||||
return dev_configuration;
|
return dev_configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current interface. This value is only valid after opening the
|
* Returns the current interface. This value is only valid after opening the
|
||||||
* device.<br>
|
* device.<br>
|
||||||
@@ -521,6 +466,7 @@ public class Device {
|
|||||||
return dev_interface;
|
return dev_interface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current device handle. This value is only valid after opening
|
* Returns the current device handle. This value is only valid after opening
|
||||||
* the device.<br>
|
* the device.<br>
|
||||||
@@ -532,6 +478,7 @@ public class Device {
|
|||||||
return usbDevHandle;
|
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>
|
||||||
@@ -547,6 +494,19 @@ public class Device {
|
|||||||
return maxPacketSize;
|
return maxPacketSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the device is open.<br>
|
||||||
|
* This checks only for a valid device handle. It doesn't check if the
|
||||||
|
* device is still attached or working.
|
||||||
|
*
|
||||||
|
* @return true if the device is open
|
||||||
|
*/
|
||||||
|
public boolean isOpen() {
|
||||||
|
return usbDevHandle > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If enabled, the device is reset when first opened. <br>
|
* If enabled, the device is reset when first opened. <br>
|
||||||
* This will only happen once. When the application is started, the device
|
* This will only happen once. When the application is started, the device
|
||||||
@@ -555,10 +515,8 @@ public class Device {
|
|||||||
* <br>
|
* <br>
|
||||||
* This feature is disabled by default.
|
* This feature is disabled by default.
|
||||||
*
|
*
|
||||||
* @param enable
|
* @param enable true if the device should be reset when first opened
|
||||||
* true if the device should be reset when first opened
|
* @param timeout the timeout between the reset and the reopening
|
||||||
* @param timeout
|
|
||||||
* the timeout between the reset and the reopening
|
|
||||||
*/
|
*/
|
||||||
public void setResetOnFirstOpen(boolean enable, int timeout) {
|
public void setResetOnFirstOpen(boolean enable, int timeout) {
|
||||||
resetOnFirstOpen = enable;
|
resetOnFirstOpen = enable;
|
||||||
|
|||||||
Reference in New Issue
Block a user