- Bugfix: LibusbJava.cpp, Ln 1288 removed the manipulation of the return value
- Bugfix: Guaranteeing '\0'-Termination of the retrieved C-Strings for all libusb_get_string_descriptor* calls - Change: The whole interface makes now use of exceptions to pass errors into the java environment instead encoding them into return values. - Change: Allowing Descriptor strings to have a length of 0 git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@292 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
@@ -223,9 +223,9 @@ public class Device {
|
||||
|
||||
if (dev != null) {
|
||||
long res = LibusbJava.usb_open(dev);
|
||||
if (res <= 0) {
|
||||
if (res == 0) {
|
||||
throw new USBException("LibusbJava.usb_open: "
|
||||
+ LibusbJava.usb_strerror() + " (" + res + ")");
|
||||
+ LibusbJava.usb_strerror());
|
||||
}
|
||||
usbDevHandle = res;
|
||||
}
|
||||
|
||||
@@ -6,11 +6,14 @@
|
||||
* This library is covered by the LGPL, read LGPL.txt for details.
|
||||
*
|
||||
* Changes:
|
||||
* 12.04.2012 NTB / Ueli Niederer implemented exception handling
|
||||
* 18.10.2010 NTB / Roger Millischer change from native interface to compatibility layer
|
||||
*
|
||||
*/
|
||||
package ch.ntb.inf.libusbJava;
|
||||
|
||||
import ch.ntb.inf.libusbJava.exceptions.LibusbError;
|
||||
|
||||
/**
|
||||
* This class is used as compatibility layer for libusb 0.1 projects. For new
|
||||
* projects use {@link LibusbJava1}
|
||||
@@ -26,10 +29,12 @@ public class LibusbJava {
|
||||
* This list is not complete! For more error codes see the file 'errorno.h'
|
||||
* on your system.
|
||||
*/
|
||||
public static int ERROR_SUCCESS, ERROR_BAD_FILE_DESCRIPTOR,
|
||||
ERROR_NO_SUCH_DEVICE_OR_ADDRESS, ERROR_BUSY,
|
||||
ERROR_INVALID_PARAMETER, ERROR_TIMEDOUT, ERROR_IO_ERROR,
|
||||
ERROR_NOT_ENOUGH_MEMORY;;
|
||||
public static final int ERROR_SUCCESS = LibusbError.ERROR_NONE;
|
||||
public static final int ERROR_BUSY = LibusbError.ERROR_BUSY;
|
||||
public static final int ERROR_INVALID_PARAMETER = LibusbError.ERROR_INVALID_PARAM;
|
||||
public static final int ERROR_TIMEDOUT = LibusbError.ERROR_TIMEOUT;
|
||||
public static final int ERROR_IO_ERROR = LibusbError.ERROR_IO;
|
||||
public static final int ERROR_NOT_ENOUGH_MEMORY = LibusbError.ERROR_NO_MEM;
|
||||
|
||||
/**
|
||||
* Sets the debugging level of libusb.<br>
|
||||
@@ -51,17 +56,18 @@ public class LibusbJava {
|
||||
* functions.
|
||||
*/
|
||||
public static void usb_init() {
|
||||
if (defaultCTX > 0) {
|
||||
if (defaultCTX != 0) {
|
||||
return;
|
||||
}
|
||||
defaultCTX = LibusbJava1.libusb_init();
|
||||
try {
|
||||
defaultCTX = LibusbJava1.libusb_init();
|
||||
} catch (LibusbError e) {
|
||||
System.err.println("LibusbJava-1.0 init failed with errorcode: "
|
||||
+ e.getMessage());
|
||||
e.printStackTrace();
|
||||
defaultCTX = 0;
|
||||
}
|
||||
LibusbJava1.libusb_set_debug(0, 0);
|
||||
if (defaultCTX < 0) {
|
||||
System.out.println("LibusbJava-1.0 init failed with errorcode: "
|
||||
+ defaultCTX);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,7 +84,7 @@ public class LibusbJava {
|
||||
boolean found = false;
|
||||
Usb_Device devices = null;
|
||||
devices = LibusbJava1.libusb_get_device_list(0);
|
||||
|
||||
|
||||
// no busses
|
||||
if (devices.getDevnum() == -1) {
|
||||
while (busses != null) {
|
||||
@@ -286,8 +292,19 @@ public class LibusbJava {
|
||||
* error has occurred.
|
||||
*/
|
||||
public static long usb_open(Usb_Device dev) {
|
||||
// return LibusbJava1.libusb_open_device_with_vid_pid(defaultCTX, dev.getDescriptor().getIdVendor(), dev.getDescriptor().getIdProduct());
|
||||
return LibusbJava1.libusb_open(dev);
|
||||
long handle = 0;
|
||||
|
||||
try {
|
||||
handle = LibusbJava1.libusb_open(dev);
|
||||
}
|
||||
catch (LibusbError e) {
|
||||
System.err.println("LibusbJava-1.0 init failed with errorcode: "
|
||||
+ e.getMessage());
|
||||
e.printStackTrace();
|
||||
handle = 0;
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -313,7 +330,15 @@ public class LibusbJava {
|
||||
* @return 0 on success or < 0 on error.
|
||||
*/
|
||||
public static int usb_set_configuration(long dev_handle, int configuration) {
|
||||
return LibusbJava1.libusb_set_configuration(dev_handle, configuration);
|
||||
int result = 0;
|
||||
|
||||
try {
|
||||
LibusbJava1.libusb_set_configuration(dev_handle, configuration);
|
||||
} catch (LibusbError e) {
|
||||
result = -1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -330,21 +355,37 @@ public class LibusbJava {
|
||||
Usb_Device dev = LibusbJava1.libusb_get_device(dev_handle);
|
||||
int nofInterfaces = LibusbJava1
|
||||
.libusb_get_active_config_descriptor(dev).getBNumInterfaces();
|
||||
int interface_number, success = -1;
|
||||
int interface_number, success = 0;
|
||||
for (interface_number = 0; interface_number < nofInterfaces; interface_number++) {
|
||||
success = LibusbJava1.libusb_release_interface(dev_handle,
|
||||
interface_number);
|
||||
if (success >= 0) {
|
||||
success = LibusbJava1.libusb_claim_interface(dev_handle,
|
||||
interface_number);
|
||||
if (success < 0) {
|
||||
return success;
|
||||
try
|
||||
{
|
||||
LibusbJava1.libusb_release_interface(dev_handle, interface_number);
|
||||
|
||||
try
|
||||
{
|
||||
LibusbJava1.libusb_claim_interface(dev_handle, interface_number);
|
||||
}
|
||||
catch (LibusbError e)
|
||||
{
|
||||
return e.getErrorCode();
|
||||
}
|
||||
break;
|
||||
}
|
||||
catch (LibusbError e)
|
||||
{
|
||||
/* Move ahead. */
|
||||
}
|
||||
}
|
||||
return LibusbJava1.libusb_set_interface_alt_setting(dev_handle,
|
||||
interface_number, alternate);
|
||||
|
||||
try {
|
||||
LibusbJava1.libusb_set_interface_alt_setting(dev_handle, interface_number, alternate);
|
||||
success = 0;
|
||||
}
|
||||
catch (LibusbError e) {
|
||||
success = -1;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -357,8 +398,15 @@ public class LibusbJava {
|
||||
* @return 0 on success or < 0 on error.
|
||||
*/
|
||||
public static int usb_clear_halt(long dev_handle, int ep) {
|
||||
|
||||
return LibusbJava1.libusb_clear_halt(dev_handle, (short) ep);
|
||||
int result = 0;
|
||||
|
||||
try {
|
||||
LibusbJava1.libusb_clear_halt(dev_handle, (short) ep);
|
||||
} catch (LibusbError e) {
|
||||
result = e.getErrorCode();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -374,10 +422,22 @@ public class LibusbJava {
|
||||
* @return 0 on success or < 0 on error.
|
||||
*/
|
||||
public static int usb_reset(long dev_handle) {
|
||||
LibusbJava1.libusb_claim_interface(dev_handle, 0);
|
||||
int res = LibusbJava1.libusb_reset_device(dev_handle);
|
||||
LibusbJava1.libusb_release_interface(dev_handle, 0);
|
||||
LibusbJava1.libusb_close(dev_handle);
|
||||
int res = 0;
|
||||
|
||||
try {
|
||||
LibusbJava1.libusb_claim_interface(dev_handle, 0);
|
||||
try {
|
||||
LibusbJava1.libusb_reset_device(dev_handle);
|
||||
}
|
||||
catch (LibusbError e) {
|
||||
res = e.getErrorCode();
|
||||
}
|
||||
LibusbJava1.libusb_release_interface(dev_handle, 0);
|
||||
LibusbJava1.libusb_close(dev_handle);
|
||||
} catch (LibusbError e) {
|
||||
/* Ignore all errors of these calls */
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
@@ -397,7 +457,15 @@ public class LibusbJava {
|
||||
* @return 0 on success or < 0 on error.
|
||||
*/
|
||||
public static int usb_claim_interface(long dev_handle, int interface_) {
|
||||
return LibusbJava1.libusb_claim_interface(dev_handle, interface_);
|
||||
int result = 0;
|
||||
|
||||
try {
|
||||
LibusbJava1.libusb_claim_interface(dev_handle, interface_);
|
||||
} catch (LibusbError e) {
|
||||
result = e.getErrorCode();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -411,7 +479,15 @@ public class LibusbJava {
|
||||
* @return 0 on success or < 0 on error.
|
||||
*/
|
||||
public static int usb_release_interface(long dev_handle, int interface_) {
|
||||
return LibusbJava1.libusb_release_interface(dev_handle, interface_);
|
||||
int result = 0;
|
||||
|
||||
try {
|
||||
LibusbJava1.libusb_release_interface(dev_handle, interface_);
|
||||
} catch (LibusbError e) {
|
||||
result = e.getErrorCode();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Control Transfers
|
||||
@@ -448,8 +524,15 @@ public class LibusbJava {
|
||||
* @return the descriptor String or null
|
||||
*/
|
||||
public static String usb_get_string(long dev_handle, int index, int langid) {
|
||||
|
||||
return LibusbJava1.libusb_get_string_descriptor(dev_handle, (short) index, langid, 255);
|
||||
String result;
|
||||
|
||||
try {
|
||||
result = LibusbJava1.libusb_get_string_descriptor(dev_handle, (short) index, langid, 255);
|
||||
} catch (LibusbError e) {
|
||||
result = null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -463,7 +546,15 @@ public class LibusbJava {
|
||||
* @return the descriptor String or null
|
||||
*/
|
||||
public static String usb_get_string_simple(long dev_handle, int index) {
|
||||
return LibusbJava1.libusb_get_string_descriptor_ascii(dev_handle,(short) index, 255);
|
||||
String result = null;
|
||||
|
||||
try {
|
||||
result = LibusbJava1.libusb_get_string_descriptor_ascii(dev_handle,(short) index, 255);
|
||||
} catch (LibusbError e) {
|
||||
result = null;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -484,7 +575,14 @@ public class LibusbJava {
|
||||
*/
|
||||
public static byte[] usb_get_descriptor(long dev_handle, byte type,
|
||||
byte index, int size) {
|
||||
return LibusbJava1.libusb_get_descriptor(dev_handle, type, index, size);
|
||||
byte[] result = null;
|
||||
|
||||
try {
|
||||
result = LibusbJava1.libusb_get_descriptor(dev_handle, type, index, size);
|
||||
} catch (LibusbError e) {
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -534,8 +632,16 @@ public class LibusbJava {
|
||||
*/
|
||||
public static int usb_bulk_write(long dev_handle, int ep, byte[] bytes,
|
||||
int size, int timeout) {
|
||||
return LibusbJava1.libusb_bulk_transfer(dev_handle, (byte) ep, bytes,
|
||||
size, timeout);
|
||||
int result = LibusbError.ERROR_OTHER;
|
||||
|
||||
try {
|
||||
result = LibusbJava1.libusb_bulk_transfer(dev_handle, (byte) ep,
|
||||
bytes, size, timeout);
|
||||
} catch (LibusbError e) {
|
||||
result = e.getErrorCode();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -551,8 +657,16 @@ public class LibusbJava {
|
||||
*/
|
||||
public static int usb_bulk_read(long dev_handle, int ep, byte[] bytes,
|
||||
int size, int timeout) {
|
||||
return LibusbJava1.libusb_bulk_transfer(dev_handle, (byte) ep, bytes,
|
||||
size, timeout);
|
||||
int result = LibusbError.ERROR_OTHER;
|
||||
|
||||
try {
|
||||
result = LibusbJava1.libusb_bulk_transfer(dev_handle, (byte) ep,
|
||||
bytes, size, timeout);
|
||||
} catch (LibusbError e) {
|
||||
result = e.getErrorCode();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Interrupt Transfers
|
||||
@@ -569,8 +683,16 @@ public class LibusbJava {
|
||||
*/
|
||||
public static int usb_interrupt_write(long dev_handle, int ep,
|
||||
byte[] bytes, int size, int timeout) {
|
||||
return LibusbJava1.libusb_interrupt_transfer(dev_handle, (byte) ep,
|
||||
bytes, size, timeout);
|
||||
int result = LibusbError.ERROR_OTHER;
|
||||
|
||||
try {
|
||||
result = LibusbJava1.libusb_interrupt_transfer(dev_handle, (byte) ep,
|
||||
bytes, size, timeout);
|
||||
} catch (LibusbError e) {
|
||||
result = e.getErrorCode();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -586,8 +708,16 @@ public class LibusbJava {
|
||||
*/
|
||||
public static int usb_interrupt_read(long dev_handle, int ep, byte[] bytes,
|
||||
int size, int timeout) {
|
||||
return LibusbJava1.libusb_interrupt_transfer(dev_handle, (byte) ep,
|
||||
bytes, size, timeout);
|
||||
int result = LibusbError.ERROR_OTHER;
|
||||
|
||||
try {
|
||||
result = LibusbJava1.libusb_interrupt_transfer(dev_handle, (byte) ep,
|
||||
bytes, size, timeout);
|
||||
} catch (LibusbError e) {
|
||||
result = e.getErrorCode();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
*/
|
||||
package ch.ntb.inf.libusbJava;
|
||||
|
||||
import ch.ntb.inf.libusbJava.exceptions.LibusbError;
|
||||
|
||||
/**
|
||||
* This class represents the Java Native Interface to the shared library which
|
||||
* is (with some exceptions) a one-to-one representation of the libusb1.0 API.<br>
|
||||
@@ -28,27 +30,6 @@ package ch.ntb.inf.libusbJava;
|
||||
*
|
||||
*/
|
||||
public class LibusbJava1 {
|
||||
|
||||
/**
|
||||
* System error codes.<br>
|
||||
* This list is not complete! For more error codes see the file 'errorno.h'
|
||||
* on your system.
|
||||
*/
|
||||
public static final int LIBUSB_SUCCESS = 0;
|
||||
public static final int LIBUSB_ERROR_IO = -1;
|
||||
public static final int LIBUSB_ERROR_INVALID_PARAM = -2;
|
||||
public static final int LIBUSB_ERROR_ACCESS = -3;
|
||||
public static final int LIBUSB_ERROR_NO_DEVICE = -4;
|
||||
public static final int LIBUSB_ERROR_NOT_FOUND = -5;
|
||||
public static final int LIBUSB_ERROR_BUSY = -6;
|
||||
public static final int LIBUSB_ERROR_TIMEOUT = -7;
|
||||
public static final int LIBUSB_ERROR_OVERFLOW = -8;
|
||||
public static final int LIBUSB_ERROR_PIPE = -9;
|
||||
public static final int LIBUSB_ERROR_INTERRUPTED = -10;
|
||||
public static final int LIBUSB_ERROR_NO_MEM = -11;
|
||||
public static final int LIBUSB_ERROR_NOT_SUPPORTED = -12;
|
||||
public static final int LIBUSB_ERROR_OTHER = -99;
|
||||
|
||||
/**
|
||||
* Set message verbosity.
|
||||
* <ul>
|
||||
@@ -95,7 +76,7 @@ public class LibusbJava1 {
|
||||
* @return a context to operate on<br>
|
||||
* or a LIBUSB_ERROR code on failure
|
||||
*/
|
||||
public static native long libusb_init();
|
||||
public static native long libusb_init() throws LibusbError;
|
||||
|
||||
/**
|
||||
* Deinitialize libusb.<br>
|
||||
@@ -126,9 +107,10 @@ public class LibusbJava1 {
|
||||
public static native Usb_Device libusb_get_device_list(long ctx);
|
||||
|
||||
/**
|
||||
* Get the number of the bus that a device is connected to.
|
||||
* Get the number of the bus that a device is connected to.
|
||||
*
|
||||
* @param dev a device
|
||||
* @param dev
|
||||
* a device
|
||||
* @return the bus number
|
||||
*/
|
||||
public static native short libusb_get_bus_number(Usb_Device dev);
|
||||
@@ -210,9 +192,17 @@ public class LibusbJava1 {
|
||||
*
|
||||
* @return a device handler
|
||||
*
|
||||
* @throws LibusbError
|
||||
* in case of an error<br>
|
||||
* Possible causes are:<br>
|
||||
* - ERROR_NO_MEM on memory allocation failure - ERROR_ACCESS if
|
||||
* the user has insufficient permissions - ERROR_NO_DEVICE if
|
||||
* the device has been disconnected - another ERROR code on
|
||||
* other failure
|
||||
*
|
||||
* @see #libusb_get_device(long)
|
||||
*/
|
||||
public static native long libusb_open(Usb_Device dev);
|
||||
public static native long libusb_open(Usb_Device dev) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Convenience function for finding a device with a particular
|
||||
@@ -286,9 +276,14 @@ public class LibusbJava1 {
|
||||
* @param dev_handle
|
||||
* a device handle
|
||||
* @return bConfigurationValue of the currently active configuration
|
||||
*
|
||||
* @throws LibusbError
|
||||
* in case of an error<br>
|
||||
* Possible error causes are:<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected -
|
||||
* another ERROR code on other failure
|
||||
*/
|
||||
public static native byte libusb_get_configuration(long dev_handle);
|
||||
public static native int libusb_get_configuration(long dev_handle)
|
||||
throws LibusbError;
|
||||
|
||||
/**
|
||||
* Set the active configuration for a device.<br>
|
||||
@@ -327,15 +322,17 @@ public class LibusbJava1 {
|
||||
* the bConfigurationValue of the configuration you wish to
|
||||
* activate, or -1 if you wish to put the device in unconfigured
|
||||
* state
|
||||
* @return 0 on success<br>
|
||||
* LIBUSB_ERROR_NOT_FOUND if the requested configuration does not
|
||||
* exist<br>
|
||||
* LIBUSB_ERROR_BUSY if interfaces are currently claimed<br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* another LIBUSB_ERROR code on other failure<br>
|
||||
* @throws LibusbError
|
||||
* in case of an error<br>
|
||||
* Possible error causes are:<br>
|
||||
* - ERROR_NOT_FOUND if the requested configuration does not
|
||||
* exist<br>
|
||||
* - ERROR_BUSY if interfaces are currently claimed<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* - another LIBUSB_ERROR code on other failure<br>
|
||||
*/
|
||||
public static native int libusb_set_configuration(long dev_handle,
|
||||
int configuration);
|
||||
public static native void libusb_set_configuration(long dev_handle,
|
||||
int configuration) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Claim an interface on a given device handle.<br>
|
||||
@@ -358,15 +355,15 @@ public class LibusbJava1 {
|
||||
* a device handle
|
||||
* @param interface_number
|
||||
* the bInterfaceNumber of the interface you wish to claim
|
||||
* @return 0 on success<br>
|
||||
* LIBUSB_ERROR_NOT_FOUND if the interface was not claimed<br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* LIBUSB_ERROR_BUSY if another program or driver has claimed the
|
||||
* interface<br>
|
||||
* another LIBUSB_ERROR code on other failure<br>
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* Possible causes for errors are:<br>
|
||||
* - ERROR_NOT_FOUND if the interface was not claimed<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* - ERROR_BUSY if another program or driver has claimed the interface<br>
|
||||
* - another LIBUSB_ERROR code on other failure<br>
|
||||
*/
|
||||
public static native int libusb_claim_interface(long dev_handle,
|
||||
int interface_number);
|
||||
public static native void libusb_claim_interface(long dev_handle,
|
||||
int interface_number) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Release an interface previously claimed with
|
||||
@@ -382,19 +379,20 @@ public class LibusbJava1 {
|
||||
* a device handle
|
||||
* @param interface_number
|
||||
* the bInterfaceNumber of the previously-claimed interface
|
||||
* @return 0 on success<br>
|
||||
* LIBUSB_ERROR_NOT_FOUND if the interface was not claimed<br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* another LIBUSB_ERROR code on other failure<br>
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* Possible causes for errors are:<br>
|
||||
* - ERROR_NOT_FOUND if the interface was not claimed<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* - another ERROR code on other failure<br>
|
||||
*/
|
||||
public static native int libusb_release_interface(long dev_handle,
|
||||
int interface_number);
|
||||
public static native void libusb_release_interface(long dev_handle,
|
||||
int interface_number) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Activate an alternate setting for an interface.<br>
|
||||
* <br>
|
||||
* The interface must have been previously claimed with
|
||||
*{@link #libusb_claim_interface(long, int)}.<br>
|
||||
* {@link #libusb_claim_interface(long, int)}.<br>
|
||||
* <br>
|
||||
* You should always use this function rather than formulating your own
|
||||
* SET_INTERFACE control request. This is because the underlying operating
|
||||
@@ -409,14 +407,14 @@ public class LibusbJava1 {
|
||||
* the bInterfaceNumber of the previously-claimed interface
|
||||
* @param alternate_setting
|
||||
* the bAlternateSetting of the alternate setting to activate
|
||||
* @return 0 on success<br>
|
||||
* LIBUSB_ERROR_NOT_FOUND if the interface was not claimed, or the
|
||||
* requested alternate setting does not exist<br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* another LIBUSB_ERROR code on other failure<br>
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* Possible causes for errors are:<br>
|
||||
* - ERROR_NOT_FOUND if the interface was not claimed, or the requested alternate setting does not exist<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* - another LIBUSB_ERROR code on other failure<br>
|
||||
*/
|
||||
public static native int libusb_set_interface_alt_setting(long dev_handle,
|
||||
int interface_number, int alternate_setting);
|
||||
public static native void libusb_set_interface_alt_setting(long dev_handle,
|
||||
int interface_number, int alternate_setting) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Clear the halt/stall condition for an endpoint.<br>
|
||||
@@ -434,12 +432,13 @@ public class LibusbJava1 {
|
||||
* a device handle
|
||||
* @param endpoint
|
||||
* the endpoint to clear halt status
|
||||
* @return 0 on success<br>
|
||||
* LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist<br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* another LIBUSB_ERROR code on other failure<br>
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* Possible causes for errors are:<br>
|
||||
* - ERROR_NOT_FOUND if the endpoint does not exist<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* - another LIBUSB_ERROR code on other failure<br>
|
||||
*/
|
||||
public static native int libusb_clear_halt(long dev_handle, short endpoint);
|
||||
public static native void libusb_clear_halt(long dev_handle, short endpoint) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Perform a USB port reset to reinitialize a device.<br>
|
||||
@@ -458,12 +457,12 @@ public class LibusbJava1 {
|
||||
*
|
||||
* @param dev_handle
|
||||
* a handle of the device to reset
|
||||
* @return 0 on success <br>
|
||||
* LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the
|
||||
* device has been disconnected<br>
|
||||
* another LIBUSB_ERROR code on other failure<br>
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* Possible causes for errors are:<br>
|
||||
* - ERROR_NOT_FOUND if re-enumeration is required, or if the device has been disconnected<br>
|
||||
* - another LIBUSB_ERROR code on other failure<br>
|
||||
*/
|
||||
public static native int libusb_reset_device(long dev_handle);
|
||||
public static native void libusb_reset_device(long dev_handle) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Determine if a kernel driver is active on an interface.<br>
|
||||
@@ -478,12 +477,14 @@ public class LibusbJava1 {
|
||||
* the interface to check
|
||||
* @return 0 if no kernel driver is active<br>
|
||||
* 1 if a kernel driver is active<br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* another LIBUSB_ERROR code on other failure<br>
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* Possible causes for errors are:<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* - another LIBUSB_ERROR code on other failure<br>
|
||||
* @see #libusb_detach_kernel_driver(long, int)
|
||||
*/
|
||||
public static native int libusb_kernel_driver_active(long dev_handle,
|
||||
int interface_number);
|
||||
int interface_number)throws LibusbError;
|
||||
|
||||
/**
|
||||
* Detach a kernel driver from an interface.<br>
|
||||
@@ -496,15 +497,16 @@ public class LibusbJava1 {
|
||||
* a device handle
|
||||
* @param interface_number
|
||||
* the interface to detach the driver from
|
||||
* @return 0 on success<br>
|
||||
* LIBUSB_ERROR_NOT_FOUND if no kernel driver was active<br>
|
||||
* LIBUSB_ERROR_INVALID_PARAM if the interface does not exist<br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* another LIBUSB_ERROR code on other failure<br>
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* Possible causes for errors are:<br>
|
||||
* - ERROR_NOT_FOUND if no kernel driver was active<br>
|
||||
* - ERROR_INVALID_PARAM if the interface does not exist<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* - another ERROR code on other failure<br>
|
||||
* @see #libusb_kernel_driver_active(long, int)
|
||||
*/
|
||||
public static native int libusb_detach_kernel_driver(long dev_handle,
|
||||
int interface_number);
|
||||
public static native void libusb_detach_kernel_driver(long dev_handle,
|
||||
int interface_number) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Re-attach an interface's kernel driver, which was previously detached
|
||||
@@ -514,17 +516,17 @@ public class LibusbJava1 {
|
||||
* a device handle
|
||||
* @param interface_number
|
||||
* the interface to attach the driver from
|
||||
* @return 0 on success<br>
|
||||
* LIBUSB_ERROR_NOT_FOUND if no kernel driver was active<br>
|
||||
* LIBUSB_ERROR_INVALID_PARAM if the interface does not exist<br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* LIBUSB_ERROR_BUSY if the driver cannot be attached because the
|
||||
* interface is claimed by a program or driver<br>
|
||||
* another LIBUSB_ERROR code on other failure<br>
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* Possible causes for errors are:<br>
|
||||
* - ERROR_NOT_FOUND if no kernel driver was active<br>
|
||||
* - ERROR_INVALID_PARAM if the interface does not exist<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* - ERROR_BUSY if the driver cannot be attached because the interface is claimed by a program or driver<br>
|
||||
* - another ERROR code on other failure<br>
|
||||
* @see #libusb_kernel_driver_active(long, int)
|
||||
*/
|
||||
public static native int libusb_attach_kernel_driver(long dev_handle,
|
||||
int interface_number);
|
||||
public static native void libusb_attach_kernel_driver(long dev_handle,
|
||||
int interface_number) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Get the USB configuration descriptor for the currently active
|
||||
@@ -556,9 +558,10 @@ public class LibusbJava1 {
|
||||
* number of charactes which will be retrieved (the length of the
|
||||
* resulting String)
|
||||
* @return a string which contains the string descriptor
|
||||
* @throws LibusbError in case of an error<br>
|
||||
*/
|
||||
public static native String libusb_get_string_descriptor_ascii(
|
||||
long dev_handle, short desc_index, int size);
|
||||
long dev_handle, short desc_index, int size) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Retrieve a descriptor from the default control pipe.<br>
|
||||
@@ -576,10 +579,11 @@ public class LibusbJava1 {
|
||||
* number of bytes which will be retrieved (the length of the
|
||||
* resulting byte[])
|
||||
* @return a byte[] which contains the descriptor or null on failure
|
||||
* @throws LibusbError in case of an error<br>
|
||||
*
|
||||
*/
|
||||
public static native byte[] libusb_get_descriptor(long dev_handle,
|
||||
int desc_type, short desc_index, int size);
|
||||
int desc_type, short desc_index, int size) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Retrieve a descriptor from a device.<br>
|
||||
@@ -598,10 +602,11 @@ public class LibusbJava1 {
|
||||
* number of charactes which will be retrieved (the length of the
|
||||
* resulting String)
|
||||
* @return a string which contains the string descriptor
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* @see #libusb_get_string_descriptor_ascii(long, short, int)
|
||||
*/
|
||||
public static native String libusb_get_string_descriptor(long dev_handle,
|
||||
short desc_index, int langid, int size);
|
||||
short desc_index, int langid, int size) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Allocate a libusb transfer with a specified number of isochronous packet
|
||||
@@ -656,12 +661,13 @@ public class LibusbJava1 {
|
||||
*
|
||||
* @param transfernumber
|
||||
* the transfer to submit
|
||||
* @return 0 on success<br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* LIBUSB_ERROR_BUSY if the transfer has already been submitted. <br>
|
||||
* another LIBUSB_ERROR code on other failure <br>
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* Possible causes for errors are:<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected<br>
|
||||
* - ERROR_BUSY if the transfer has already been submitted. <br>
|
||||
* - another LIBUSB_ERROR code on other failure <br>
|
||||
*/
|
||||
public static native int libusb_submit_transfer(long transfernumber);
|
||||
public static native void libusb_submit_transfer(long transfernumber) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Asynchronously cancel a previously submitted transfer.<br>
|
||||
@@ -672,12 +678,12 @@ public class LibusbJava1 {
|
||||
*
|
||||
* @param transfernumber
|
||||
* a transfer
|
||||
* @return 0 on success <br>
|
||||
* LIBUSB_ERROR_NOT_FOUND if the transfer is already complete or
|
||||
* cancelled. <br>
|
||||
* a LIBUSB_ERROR code on failure <br>
|
||||
* @throws LibusbError in case of an error<br>
|
||||
* Possible causes for errors are:<br>
|
||||
* - ERROR_NOT_FOUND if the transfer is already complete or cancelled. <br>
|
||||
* - a LIBUSB_ERROR code on failure <br>
|
||||
*/
|
||||
public static native int libusb_cancel_transfer(long transfernumber);
|
||||
public static native void libusb_cancel_transfer(long transfernumber) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Get the data section of a control transfer.<br>
|
||||
@@ -691,7 +697,7 @@ public class LibusbJava1 {
|
||||
*
|
||||
* @param transfernumber
|
||||
* a transfer
|
||||
* @return the data section from the transfer
|
||||
* @return the data section from the transfer, null if the transfer number was invalid.
|
||||
*/
|
||||
public static native byte[] libusb_control_transfer_get_data(
|
||||
long transfernumber);
|
||||
@@ -708,7 +714,7 @@ public class LibusbJava1 {
|
||||
*
|
||||
* @param transfernumber
|
||||
* a transfer
|
||||
* @return the setup packet from the transfer
|
||||
* @return the setup packet from the transfer, null if the transfer number was invalid.
|
||||
*/
|
||||
public static native byte[] libusb_control_transfer_get_setup(
|
||||
long transfernumber);
|
||||
@@ -1060,9 +1066,9 @@ public class LibusbJava1 {
|
||||
* @param timeval
|
||||
* the maximum time to block waiting for events, or zero for
|
||||
* non-blocking mode
|
||||
* @return 0 on success, or a LIBUSB_ERROR code on failure
|
||||
* @throws LibusbError in case of an error<br>
|
||||
*/
|
||||
public static native int libusb_handle_events_timeout(long ctx, long timeval);
|
||||
public static native void libusb_handle_events_timeout(long ctx, long timeval) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Handle any pending events in blocking mode with a sensible timeout.<br>
|
||||
@@ -1074,9 +1080,9 @@ public class LibusbJava1 {
|
||||
*
|
||||
* @param ctx
|
||||
* the context to operate on, or NULL for the default context
|
||||
* @return 0 on success, or a LIBUSB_ERROR code on failure
|
||||
* @throws LibusbError in case of an error<br>
|
||||
*/
|
||||
public static native int libusb_handle_events(long ctx);
|
||||
public static native void libusb_handle_events(long ctx) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Handle any pending events by polling file descriptors, without checking
|
||||
@@ -1097,9 +1103,9 @@ public class LibusbJava1 {
|
||||
* @param timeval
|
||||
* the maximum time in seconds to block waiting for events, or
|
||||
* zero for non-blocking mode
|
||||
* @return 0 on success, or a LIBUSB_ERROR code on failure
|
||||
* @throws LibusbError in case of an error<br>
|
||||
*/
|
||||
public static native int libusb_handle_events_locked(long ctx, long timeval);
|
||||
public static native void libusb_handle_events_locked(long ctx, long timeval) throws LibusbError;
|
||||
|
||||
/**
|
||||
* Determines whether your application must apply special timing
|
||||
@@ -1151,18 +1157,20 @@ public class LibusbJava1 {
|
||||
* This function may return an zero timevalue. If this is the case, it
|
||||
* indicates that libusb has a timeout that has already expired so you
|
||||
* should call libusb_handle_events_timeout() or similar immediately. A
|
||||
* return code of -999 indicates that there are no pending timeouts.<br>
|
||||
* return code of -1 indicates that there are no pending timeouts.<br>
|
||||
* <br>
|
||||
* On some platforms, this function will always returns -999 (no pending
|
||||
* On some platforms, this function will always returns -1 (no pending
|
||||
* timeouts).
|
||||
*
|
||||
* @param ctx
|
||||
* the context to operate on, or NULL for the default context
|
||||
* @return -999 if there are no pending timeouts<br>
|
||||
* time to next timeout<br>
|
||||
* LIBUSB_ERROR_OTHER on failure
|
||||
* @return time to next timeout<br>
|
||||
* -1 if there are no pending timeouts<br>
|
||||
* @throws LibusbError
|
||||
* If an error is occured in libusb
|
||||
*/
|
||||
public static native int libusb_get_next_timeout(long ctx);
|
||||
public static native int libusb_get_next_timeout(long ctx)
|
||||
throws LibusbError;
|
||||
|
||||
/**
|
||||
* Register notification functions for file descriptor additions/removals.<br>
|
||||
@@ -1271,15 +1279,20 @@ public class LibusbJava1 {
|
||||
* timeout (in millseconds) that this function should wait before
|
||||
* giving up due to no response being received. For an unlimited
|
||||
* timeout, use value 0.
|
||||
* @return on success, the number of bytes actually transferred <br>
|
||||
* LIBUSB_ERROR_TIMEOUT if the transfer timed out <br>
|
||||
* LIBUSB_ERROR_PIPE if the control request was not supported by the
|
||||
* device <br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected <br>
|
||||
* another LIBUSB_ERROR code on other failures.
|
||||
* @return The number of bytes actually transferred
|
||||
* @throws LibusbError
|
||||
* in case an error occured<br>
|
||||
* Possible causes are:<br>
|
||||
* - ERROR_TIMEOUT if the transfer timed out <br>
|
||||
* - ERROR_PIPE if the control request was not supported by the
|
||||
* device <br>
|
||||
* - ERROR_OVERFLOW if the device offered more data<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected <br>
|
||||
* - another code on other failures.
|
||||
*/
|
||||
public static native int libusb_bulk_transfer(long dev_handle,
|
||||
byte endpoint, byte[] data, int length, int timeout);
|
||||
byte endpoint, byte[] data, int length, int timeout)
|
||||
throws LibusbError;
|
||||
|
||||
/**
|
||||
* Perform a USB interrupt transfer.<br>
|
||||
@@ -1311,15 +1324,20 @@ public class LibusbJava1 {
|
||||
* timeout (in millseconds) that this function should wait before
|
||||
* giving up due to no response being received. For an unlimited
|
||||
* timeout, use value 0.
|
||||
* @return on success, the number of bytes actually transferred <br>
|
||||
* LIBUSB_ERROR_TIMEOUT if the transfer timed out <br>
|
||||
* LIBUSB_ERROR_PIPE if the control request was not supported by the
|
||||
* device <br>
|
||||
* LIBUSB_ERROR_NO_DEVICE if the device has been disconnected <br>
|
||||
* another LIBUSB_ERROR code on other failures
|
||||
* @return The number of bytes actually transferred <br>
|
||||
* @throws LibusbError
|
||||
* in case an error occured<br>
|
||||
* Possible causes are:<br>
|
||||
* - ERROR_TIMEOUT if the transfer timed out <br>
|
||||
* - ERROR_PIPE if the control request was not supported by the
|
||||
* device <br>
|
||||
* - ERROR_OVERFLOW if the device offered more data<br>
|
||||
* - ERROR_NO_DEVICE if the device has been disconnected <br>
|
||||
* - another ERROR code on other failures
|
||||
*/
|
||||
public static native int libusb_interrupt_transfer(long dev_handle,
|
||||
byte endpoint, byte[] data, int length, int timeout);
|
||||
byte endpoint, byte[] data, int length, int timeout)
|
||||
throws LibusbError;
|
||||
|
||||
/**
|
||||
* Returns the error string after an error occured.
|
||||
@@ -1332,8 +1350,20 @@ public class LibusbJava1 {
|
||||
String os = System.getProperty("os.name");
|
||||
if (os.contains("Windows")) {
|
||||
System.loadLibrary("LibusbJava-1_0");
|
||||
} else {
|
||||
} else {
|
||||
System.loadLibrary("usbJava-1.0");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is only used for testing the DLL-code that throws exceptions
|
||||
* in the java environment.
|
||||
*
|
||||
* @param code
|
||||
* Code of the error to be simulated and hence the code of the
|
||||
* exception that shall be thrown.
|
||||
*
|
||||
* @throws LibusbError
|
||||
*/
|
||||
public static native void libusb_exceptionTest(int code) throws LibusbError;
|
||||
}
|
||||
|
||||
129
java/src/ch/ntb/inf/libusbJava/exceptions/LibusbError.java
Normal file
129
java/src/ch/ntb/inf/libusbJava/exceptions/LibusbError.java
Normal file
@@ -0,0 +1,129 @@
|
||||
package ch.ntb.inf.libusbJava.exceptions;
|
||||
|
||||
public class LibusbError extends Exception {
|
||||
private static final long serialVersionUID = 9096323614080207236L;
|
||||
|
||||
/**
|
||||
* libusb error codes
|
||||
*/
|
||||
public static final int ERROR_NONE = 0;
|
||||
public static final int ERROR_IO = -1;
|
||||
public static final int ERROR_INVALID_PARAM = -2;
|
||||
public static final int ERROR_ACCESS = -3;
|
||||
public static final int ERROR_NO_DEVICE = -4;
|
||||
public static final int ERROR_NOT_FOUND = -5;
|
||||
public static final int ERROR_BUSY = -6;
|
||||
public static final int ERROR_TIMEOUT = -7;
|
||||
public static final int ERROR_OVERFLOW = -8;
|
||||
public static final int ERROR_PIPE = -9;
|
||||
public static final int ERROR_INTERRUPTED = -10;
|
||||
public static final int ERROR_NO_MEM = -11;
|
||||
public static final int ERROR_NOT_SUPPORTED = -12;
|
||||
public static final int ERROR_OTHER = -99;
|
||||
|
||||
private int code = ERROR_NONE;
|
||||
|
||||
public LibusbError(int code)
|
||||
{
|
||||
super("libusb result: " + getStringFromCode(code));
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public int getErrorCode()
|
||||
{
|
||||
return code;
|
||||
}
|
||||
|
||||
public static String getStringFromCode(int code)
|
||||
{
|
||||
String result;
|
||||
|
||||
switch (code)
|
||||
{
|
||||
case ERROR_IO:
|
||||
{
|
||||
result = "ERROR_IO";
|
||||
break;
|
||||
}
|
||||
case ERROR_INVALID_PARAM:
|
||||
{
|
||||
result = "ERROR_INVALID_PARAM";
|
||||
break;
|
||||
}
|
||||
case ERROR_ACCESS:
|
||||
{
|
||||
result = "ERROR_ACCESS";
|
||||
break;
|
||||
}
|
||||
case ERROR_NO_DEVICE:
|
||||
{
|
||||
result = "ERROR_NO_DEVICE";
|
||||
break;
|
||||
}
|
||||
case ERROR_NOT_FOUND:
|
||||
{
|
||||
result = "ERROR_NOT_FOUND";
|
||||
break;
|
||||
}
|
||||
case ERROR_BUSY:
|
||||
{
|
||||
result = "ERROR_BUSY";
|
||||
break;
|
||||
}
|
||||
|
||||
case ERROR_TIMEOUT:
|
||||
{
|
||||
result = "ERROR_TIMEOUT";
|
||||
break;
|
||||
}
|
||||
|
||||
case ERROR_OVERFLOW:
|
||||
{
|
||||
result = "ERROR_OVERFLOW";
|
||||
break;
|
||||
}
|
||||
|
||||
case ERROR_PIPE:
|
||||
{
|
||||
result = "ERROR_PIPE";
|
||||
break;
|
||||
}
|
||||
|
||||
case ERROR_INTERRUPTED:
|
||||
{
|
||||
result = "ERROR_INTERRUPTED";
|
||||
break;
|
||||
}
|
||||
|
||||
case ERROR_NO_MEM:
|
||||
{
|
||||
result = "ERROR_NO_MEM";
|
||||
break;
|
||||
}
|
||||
|
||||
case ERROR_NOT_SUPPORTED:
|
||||
{
|
||||
result = "ERROR_NOT_SUPPORTED";
|
||||
break;
|
||||
}
|
||||
|
||||
case ERROR_OTHER:
|
||||
{
|
||||
result = "ERROR_OTHER";
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
result = "ERROR_UNKNWON (" + code + ")";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getErrorString() {
|
||||
return getStringFromCode(getErrorCode());
|
||||
}
|
||||
}
|
||||
@@ -274,7 +274,7 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
|
||||
if (tmpDevDesc.equals(devDesc)) {
|
||||
long handle = LibusbJava.usb_open(dev);
|
||||
sb.append("\nString descriptors\n");
|
||||
if (handle <= 0) {
|
||||
if (handle == 0) {
|
||||
sb.append("\terror opening the device\n");
|
||||
break;
|
||||
}
|
||||
@@ -348,7 +348,7 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
|
||||
&& (confDesc.getIConfiguration() > 0)) {
|
||||
long handle = LibusbJava.usb_open(dev);
|
||||
sb.append("\nString descriptors\n");
|
||||
if (handle <= 0) {
|
||||
if (handle == 0) {
|
||||
sb.append("\terror opening the device\n");
|
||||
break;
|
||||
}
|
||||
@@ -426,7 +426,7 @@ public class UsbTreeModel implements TreeModel, TreeSelectionListener {
|
||||
&& (intDesc.getIInterface() > 0)) {
|
||||
long handle = LibusbJava.usb_open(dev);
|
||||
sb.append("\nString descriptors\n");
|
||||
if (handle <= 0) {
|
||||
if (handle == 0) {
|
||||
sb
|
||||
.append("\terror opening the device\n");
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user