support for system specific error numbers

git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@253 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
spandi
2007-08-11 15:02:40 +00:00
parent 8dc3f8c515
commit 2a13255fd0
5 changed files with 109 additions and 8 deletions

View File

@@ -22,8 +22,6 @@ public class Device {
private static final Logger logger = LogUtil.getLogger("ch.ntb.usb");
private static final int TIMEOUT_ERROR_CODE = -116;
private int maxPacketSize;
private int idVendor, idProduct, dev_configuration, dev_interface,
@@ -284,7 +282,7 @@ public class Device {
int lenWritten = LibusbJava.usb_bulk_write(usbDevHandle,
out_ep_address, data, size, timeout);
if (lenWritten < 0) {
if (lenWritten == TIMEOUT_ERROR_CODE) {
if (lenWritten == LibusbJava.ERROR_TIMEDOUT) {
// try to reopen the device and send the data again
if (reopenOnTimeout) {
logger.info("try to reopen");
@@ -344,7 +342,7 @@ public class Device {
int lenRead = LibusbJava.usb_bulk_read(usbDevHandle, in_ep_address,
data, size, timeout);
if (lenRead < 0) {
if (lenRead == TIMEOUT_ERROR_CODE) {
if (lenRead == LibusbJava.ERROR_TIMEDOUT) {
// try to reopen the device and send the data again
if (reopenOnTimeout) {
logger.info("try to reopen");
@@ -404,7 +402,7 @@ public class Device {
int lenWritten = LibusbJava.usb_interrupt_write(usbDevHandle,
out_ep_address, data, size, timeout);
if (lenWritten < 0) {
if (lenWritten == TIMEOUT_ERROR_CODE) {
if (lenWritten == LibusbJava.ERROR_TIMEDOUT) {
// try to reopen the device and send the data again
if (reopenOnTimeout) {
logger.info("try to reopen");
@@ -465,7 +463,7 @@ public class Device {
int lenRead = LibusbJava.usb_interrupt_read(usbDevHandle,
in_ep_address, data, size, timeout);
if (lenRead < 0) {
if (lenRead == TIMEOUT_ERROR_CODE) {
if (lenRead == LibusbJava.ERROR_TIMEDOUT) {
// try to reopen the device and send the data again
if (reopenOnTimeout) {
logger.info("try to reopen");
@@ -541,7 +539,7 @@ public class Device {
int len = LibusbJava.usb_control_msg(usbDevHandle, requestType,
request, value, index, data, size, timeout);
if (len < 0) {
if (len == TIMEOUT_ERROR_CODE) {
if (len == LibusbJava.ERROR_TIMEDOUT) {
// try to reopen the device and send the data again
if (reopenOnTimeout) {
logger.info("try to reopen");

View File

@@ -32,6 +32,16 @@ package ch.ntb.usb;
*/
public class LibusbJava {
/**
* System error codes.<br>
* 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;;
/**
* Sets the debugging level of libusb.<br>
*
@@ -330,6 +340,25 @@ public class LibusbJava {
/** **************************************************************** */
/**
* Maps the Java error code to the system error code.<br>
* <br>
* Note that not all error codes are be mapped by this method. For more
* error codes see the file 'errno.h' on your system.<br>
* <br>
* 1: EBADF: Bad file descriptor.<br>
* 2: ENXIO: No such device or address.<br>
* 3: EBUSY: Device or resource busy.<br>
* 4: EINVAL: Invalid argument.<br>
* 5: ETIMEDOUT: Connection timed out.<br>
* 6: EIO: I/O error.<br>
* 7: ENOMEM: Not enough memory.<br>
*
*
* @return the system error code or 100000 if no mapping has been found.
*/
private static native int usb_error_no(int value);
static {
String os = System.getProperty("os.name");
if (os.contains("Windows")) {
@@ -337,5 +366,14 @@ public class LibusbJava {
} else {
System.loadLibrary("usbJava");
}
// define the error codes
ERROR_SUCCESS = 0;
ERROR_BAD_FILE_DESCRIPTOR = -usb_error_no(1);
ERROR_NO_SUCH_DEVICE_OR_ADDRESS = -usb_error_no(2);
ERROR_BUSY = -usb_error_no(3);
ERROR_INVALID_PARAMETER = -usb_error_no(4);
ERROR_TIMEDOUT = -usb_error_no(5);
ERROR_IO_ERROR = -usb_error_no(6);
ERROR_NOT_ENOUGH_MEMORY = -usb_error_no(7);
}
}