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

@@ -10,9 +10,15 @@
#include <stddef.h>
#include <string.h>
#include <locale.h>
#include <errno.h>
#include <usb.h>
#include "LibusbJava.h"
// Windows specific stuff
#ifdef WIN32
#include <error.h>
#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;
}
}

View File

@@ -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

23
LibusbJava/error.h Normal file
View File

@@ -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_ */

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);
}
}