diff --git a/LibusbJava/LibusbJava.cpp b/LibusbJava/LibusbJava.cpp index d3ed13d..cc7f129 100644 --- a/LibusbJava/LibusbJava.cpp +++ b/LibusbJava/LibusbJava.cpp @@ -10,9 +10,15 @@ #include #include #include +#include #include #include "LibusbJava.h" +// Windows specific stuff +#ifdef WIN32 +#include +#endif + //#define DEBUGON // global bus (updated when usb_get_busses() is called) @@ -805,4 +811,32 @@ JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1strerror return env->NewStringUTF(str); } - +/* + * Class: ch_ntb_usb_LibusbJava + * Method: usb_error_no + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1error_1no + (JNIEnv *env, jclass obj, jint java_error_no){ + + switch (java_error_no) { + case 0: + return 0; + case 1: + return EBADF; + case 2: + return ENXIO; + case 3: + return EBUSY; + case 4: + return EINVAL; + case 5: + return ETIMEDOUT; + case 6: + return EIO; + case 7: + return ENOMEM; + default: + return 100000; + } +} diff --git a/LibusbJava/LibusbJava.h b/LibusbJava/LibusbJava.h index d142c5a..560b80e 100644 --- a/LibusbJava/LibusbJava.h +++ b/LibusbJava/LibusbJava.h @@ -191,6 +191,14 @@ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1interrupt_1read JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1strerror (JNIEnv *, jclass); +/* + * Class: ch_ntb_usb_LibusbJava + * Method: usb_error_no + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1error_1no + (JNIEnv *, jclass, jint); + #ifdef __cplusplus } #endif diff --git a/LibusbJava/error.h b/LibusbJava/error.h new file mode 100644 index 0000000..a9b600c --- /dev/null +++ b/LibusbJava/error.h @@ -0,0 +1,23 @@ +// copied from libusb-win32.sourceforge.net +#ifndef __ERROR_H__ +#define __ERROR_H__ + +/* Connection timed out */ +#define ETIMEDOUT 116 + +typedef enum { + USB_ERROR_TYPE_NONE = 0, + USB_ERROR_TYPE_STRING, + USB_ERROR_TYPE_ERRNO, +} usb_error_type_t; + + +void usb_error(char *format, ...); +void usb_message(char *format, ...); +const char *usb_win_error_to_string(void); +int usb_win_error_to_errno(void); + + + +#endif /* _ERROR_H_ */ + diff --git a/java/src/ch/ntb/usb/Device.java b/java/src/ch/ntb/usb/Device.java index 6d56536..5b2a403 100644 --- a/java/src/ch/ntb/usb/Device.java +++ b/java/src/ch/ntb/usb/Device.java @@ -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"); diff --git a/java/src/ch/ntb/usb/LibusbJava.java b/java/src/ch/ntb/usb/LibusbJava.java index 58df67d..27d5f98 100644 --- a/java/src/ch/ntb/usb/LibusbJava.java +++ b/java/src/ch/ntb/usb/LibusbJava.java @@ -32,6 +32,16 @@ package ch.ntb.usb; */ public class LibusbJava { + /** + * System error codes.
+ * 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.
* @@ -330,6 +340,25 @@ public class LibusbJava { /** **************************************************************** */ + /** + * Maps the Java error code to the system error code.
+ *
+ * Note that not all error codes are be mapped by this method. For more + * error codes see the file 'errno.h' on your system.
+ *
+ * 1: EBADF: Bad file descriptor.
+ * 2: ENXIO: No such device or address.
+ * 3: EBUSY: Device or resource busy.
+ * 4: EINVAL: Invalid argument.
+ * 5: ETIMEDOUT: Connection timed out.
+ * 6: EIO: I/O error.
+ * 7: ENOMEM: Not enough memory.
+ * + * + * @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); } } \ No newline at end of file