libusb-java-0.8+ztex20090101/0000755000175000017500000000000011522245175015247 5ustar moellermoellerlibusb-java-0.8+ztex20090101/build-w32.sh0000755000175000017500000000102011522227626017310 0ustar moellermoellerexport PATH=/drv_t1/mingw/w32/bin:$PATH triple=i686-pc-mingw32 JAVAPREFIX=/usr/local/java TARGET=libusbJava32.dll $triple-gcc -m32 -shared -std=c99 -Wall -Wno-pointer-to-int-cast -D_JNI_IMPLEMENTATION_ -Wl,--kill-at \ -I$JAVAPREFIX/include -I$JAVAPREFIX/include/win32 -I../libusb-win32 -L../libusb-win32/x86 LibusbJava.c -lusb0_x86 -o libusbJava32.dll # -I$JAVAPREFIX/include -I$JAVAPREFIX/include/win32 -I../libusb-win32 LibusbJava.c ../libusb-win32/libusb.a -o libusbJava.dll $triple-strip $TARGET chmod -x $TARGET libusb-java-0.8+ztex20090101/LibusbJava.c0000644000175000017500000011060611517665666017460 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ #include #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) struct usb_bus *busses; // global flag for loading all class, method and field ID references int java_references_loaded = 0; // if > 0 an LibusbJava specific error string is set char *libusbJavaError = NULL; // macros to set and clear LibusbJava specific errors #define setLibusbJavaError(error) libusbJavaError = error #define clearLibusbJavaError() libusbJavaError = NULL // class references jclass usb_busClazz, usb_devClazz, usb_devDescClazz, usb_confDescClazz, \ usb_intClazz, usb_intDescClazz, usb_epDescClazz; // method ID references jmethodID usb_busMid, usb_devMid, usb_devDescMid, usb_confDescMid, \ usb_intMid, usb_intDescMid, usb_epDescMid; // field ID references // usb_bus jfieldID usb_busFID_next, usb_busFID_prev, usb_busFID_dirname, \ usb_busFID_devices, usb_busFID_location, usb_busFID_root_dev; // usb_device jfieldID usb_devFID_next, usb_devFID_prev, usb_devFID_filename, \ usb_devFID_bus, usb_devFID_descriptor, usb_devFID_config, \ usb_devFID_devnum, usb_devFID_num_children, usb_devFID_children, \ usb_devFID_devStructAddr; // usb_deviceDescriptor jfieldID usb_devDescFID_bLength, usb_devDescFID_bDescriptorType, \ usb_devDescFID_bcdUSB, usb_devDescFID_bDeviceClass, \ usb_devDescFID_bDeviceSubClass, usb_devDescFID_bDeviceProtocol, \ usb_devDescFID_bMaxPacketSize0, usb_devDescFID_idVendor, \ usb_devDescFID_idProduct, usb_devDescFID_bcdDevice, \ usb_devDescFID_iManufacturer, usb_devDescFID_iProduct, \ usb_devDescFID_iSerialNumber, usb_devDescFID_bNumConfigurations; // usb_configurationDescriptor jfieldID usb_confDescFID_bLength, usb_confDescFID_bDescriptorType, usb_confDescFID_wTotalLength, \ usb_confDescFID_bNumInterfaces, usb_confDescFID_bConfigurationValue, \ usb_confDescFID_iConfiguration, usb_confDescFID_bmAttributes, usb_confDescFID_MaxPower, \ usb_confDescFID_interface_, usb_confDescFID_extra, usb_confDescFID_extralen; // usb_interface jfieldID usb_intFID_altsetting, usb_intFID_num_altsetting; // usb_intDesc jfieldID usb_intDescFID_bLength, usb_intDescFID_bDescriptorType, \ usb_intDescFID_bInterfaceNumber, usb_intDescFID_bAlternateSetting, \ usb_intDescFID_bNumEndpoints, usb_intDescFID_bInterfaceClass, \ usb_intDescFID_bInterfaceSubClass, usb_intDescFID_bInterfaceProtocol, \ usb_intDescFID_iInterface, usb_intDescFID_endpoint, usb_intDescFID_extra, \ usb_intDescFID_extralen; // usb_endpointDescriptor jfieldID usb_epDescFID_bLength, usb_epDescFID_bDescriptorType, \ usb_epDescFID_bEndpointAddress, usb_epDescFID_bmAttributes, \ usb_epDescFID_wMaxPacketSize, usb_epDescFID_bInterval, \ usb_epDescFID_bRefresh, usb_epDescFID_bSynchAddress, usb_epDescFID_extra, \ usb_epDescFID_extralen; /* * Class: ch_ntb_usb_LibusbJava * Method: usb_set_debug * Signature: (B)V */ JNIEXPORT void JNICALL Java_ch_ntb_usb_LibusbJava_usb_1set_1debug (JNIEnv *env, jclass obj, jint level) { clearLibusbJavaError(); usb_set_debug(level); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_init * Signature: ()V */ JNIEXPORT void JNICALL Java_ch_ntb_usb_LibusbJava_usb_1init (JNIEnv *env, jclass obj) { clearLibusbJavaError(); usb_init(); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_find_busses * Signature: ()I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1find_1busses (JNIEnv *env, jclass obj) { clearLibusbJavaError(); return usb_find_busses(); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_find_devices * Signature: ()I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1find_1devices (JNIEnv *env, jclass obj) { clearLibusbJavaError(); return usb_find_devices(); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_get_busses * Signature: ()Lch/ntb/usb/Usb_Bus; */ JNIEXPORT jobject JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1busses (JNIEnv *env, jclass obj) { clearLibusbJavaError(); // only load class, method and field ID references once if (!java_references_loaded) { // find classes and field ids // usb_bus usb_busClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Bus"); if (usb_busClazz == NULL) { return NULL; /* exception thrown */ } usb_busMid = (*env)->GetMethodID(env, usb_busClazz, "","()V"); if (usb_busMid == NULL) { return NULL; } usb_busFID_next = (*env)->GetFieldID(env, usb_busClazz, "next", "Lch/ntb/usb/Usb_Bus;"); usb_busFID_prev = (*env)->GetFieldID(env, usb_busClazz, "prev", "Lch/ntb/usb/Usb_Bus;"); usb_busFID_dirname = (*env)->GetFieldID(env, usb_busClazz, "dirname", "Ljava/lang/String;"); usb_busFID_devices = (*env)->GetFieldID(env, usb_busClazz, "devices", "Lch/ntb/usb/Usb_Device;"); usb_busFID_location = (*env)->GetFieldID(env, usb_busClazz, "location", "J"); usb_busFID_root_dev = (*env)->GetFieldID(env, usb_busClazz, "root_dev", "Lch/ntb/usb/Usb_Device;"); // usb_device usb_devClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Device"); if (usb_devClazz == NULL) { return NULL; /* exception thrown */ } usb_devMid = (*env)->GetMethodID(env, usb_devClazz, "","()V"); if (usb_devMid == NULL) { return NULL; } usb_devFID_next = (*env)->GetFieldID(env, usb_devClazz, "next", "Lch/ntb/usb/Usb_Device;"); usb_devFID_prev = (*env)->GetFieldID(env, usb_devClazz, "prev", "Lch/ntb/usb/Usb_Device;"); usb_devFID_filename = (*env)->GetFieldID(env, usb_devClazz, "filename", "Ljava/lang/String;"); usb_devFID_bus = (*env)->GetFieldID(env, usb_devClazz, "bus", "Lch/ntb/usb/Usb_Bus;"); usb_devFID_descriptor = (*env)->GetFieldID(env, usb_devClazz, "descriptor", "Lch/ntb/usb/Usb_Device_Descriptor;"); usb_devFID_config = (*env)->GetFieldID(env, usb_devClazz, "config", "[Lch/ntb/usb/Usb_Config_Descriptor;"); usb_devFID_devnum = (*env)->GetFieldID(env, usb_devClazz, "devnum", "B"); usb_devFID_num_children = (*env)->GetFieldID(env, usb_devClazz, "num_children", "B"); usb_devFID_children = (*env)->GetFieldID(env, usb_devClazz, "children", "Lch/ntb/usb/Usb_Device;"); usb_devFID_devStructAddr = (*env)->GetFieldID(env, usb_devClazz, "devStructAddr", "J"); // usb_device_descriptor usb_devDescClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Device_Descriptor"); if (usb_devDescClazz == NULL) { return NULL; /* exception thrown */ } usb_devDescMid = (*env)->GetMethodID(env, usb_devDescClazz, "","()V"); if (usb_devDescMid == NULL) { return NULL; } usb_devDescFID_bLength = (*env)->GetFieldID(env, usb_devDescClazz, "bLength", "B"); usb_devDescFID_bDescriptorType = (*env)->GetFieldID(env, usb_devDescClazz, "bDescriptorType", "B"); usb_devDescFID_bcdUSB = (*env)->GetFieldID(env, usb_devDescClazz, "bcdUSB", "S"); usb_devDescFID_bDeviceClass = (*env)->GetFieldID(env, usb_devDescClazz, "bDeviceClass", "B"); usb_devDescFID_bDeviceSubClass = (*env)->GetFieldID(env, usb_devDescClazz, "bDeviceSubClass", "B"); usb_devDescFID_bDeviceProtocol = (*env)->GetFieldID(env, usb_devDescClazz, "bDeviceProtocol", "B"); usb_devDescFID_bMaxPacketSize0 = (*env)->GetFieldID(env, usb_devDescClazz, "bMaxPacketSize0", "B"); usb_devDescFID_idVendor = (*env)->GetFieldID(env, usb_devDescClazz, "idVendor", "S"); usb_devDescFID_idProduct = (*env)->GetFieldID(env, usb_devDescClazz, "idProduct", "S"); usb_devDescFID_bcdDevice = (*env)->GetFieldID(env, usb_devDescClazz, "bcdDevice", "S"); usb_devDescFID_iManufacturer = (*env)->GetFieldID(env, usb_devDescClazz, "iManufacturer", "B"); usb_devDescFID_iProduct = (*env)->GetFieldID(env, usb_devDescClazz, "iProduct", "B"); usb_devDescFID_iSerialNumber = (*env)->GetFieldID(env, usb_devDescClazz, "iSerialNumber", "B"); usb_devDescFID_bNumConfigurations = (*env)->GetFieldID(env, usb_devDescClazz, "bNumConfigurations", "B"); // usb_configuration_descriptor usb_confDescClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Config_Descriptor"); if (usb_confDescClazz == NULL) { return NULL; /* exception thrown */ } usb_confDescMid = (*env)->GetMethodID(env, usb_confDescClazz, "","()V"); if (usb_confDescMid == NULL) { return NULL; } usb_confDescFID_bLength = (*env)->GetFieldID(env, usb_confDescClazz, "bLength", "B"); usb_confDescFID_bDescriptorType = (*env)->GetFieldID(env, usb_confDescClazz, "bDescriptorType", "B"); usb_confDescFID_wTotalLength = (*env)->GetFieldID(env, usb_confDescClazz, "wTotalLength", "S"); usb_confDescFID_bNumInterfaces = (*env)->GetFieldID(env, usb_confDescClazz, "bNumInterfaces", "B"); usb_confDescFID_bConfigurationValue = (*env)->GetFieldID(env, usb_confDescClazz, "bConfigurationValue", "B"); usb_confDescFID_iConfiguration = (*env)->GetFieldID(env, usb_confDescClazz, "iConfiguration", "B"); usb_confDescFID_bmAttributes = (*env)->GetFieldID(env, usb_confDescClazz, "bmAttributes", "B"); usb_confDescFID_MaxPower = (*env)->GetFieldID(env, usb_confDescClazz, "MaxPower", "B"); usb_confDescFID_interface_ = (*env)->GetFieldID(env, usb_confDescClazz, "interface_", "[Lch/ntb/usb/Usb_Interface;"); usb_confDescFID_extra = (*env)->GetFieldID(env, usb_confDescClazz, "extra", "[B"); usb_confDescFID_extralen = (*env)->GetFieldID(env, usb_confDescClazz, "extralen", "I"); // usb_interface usb_intClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Interface"); if (usb_intClazz == NULL) { return NULL; /* exception thrown */ } usb_intMid = (*env)->GetMethodID(env, usb_intClazz, "","()V"); if (usb_intMid == NULL) { return NULL; } usb_intFID_altsetting = (*env)->GetFieldID(env, usb_intClazz, "altsetting", "[Lch/ntb/usb/Usb_Interface_Descriptor;"); usb_intFID_num_altsetting = (*env)->GetFieldID(env, usb_intClazz, "num_altsetting", "I"); // usb_interface_descriptor usb_intDescClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Interface_Descriptor"); if (usb_intDescClazz == NULL) { return NULL; /* exception thrown */ } usb_intDescMid = (*env)->GetMethodID(env, usb_intDescClazz, "","()V"); if (usb_intDescMid == NULL) { return NULL; } usb_intDescFID_bLength = (*env)->GetFieldID(env, usb_intDescClazz, "bLength", "B"); usb_intDescFID_bDescriptorType = (*env)->GetFieldID(env, usb_intDescClazz, "bDescriptorType", "B"); usb_intDescFID_bInterfaceNumber = (*env)->GetFieldID(env, usb_intDescClazz, "bInterfaceNumber", "B"); usb_intDescFID_bAlternateSetting = (*env)->GetFieldID(env, usb_intDescClazz, "bAlternateSetting", "B"); usb_intDescFID_bNumEndpoints = (*env)->GetFieldID(env, usb_intDescClazz, "bNumEndpoints", "B"); usb_intDescFID_bInterfaceClass = (*env)->GetFieldID(env, usb_intDescClazz, "bInterfaceClass", "B"); usb_intDescFID_bInterfaceSubClass = (*env)->GetFieldID(env, usb_intDescClazz, "bInterfaceSubClass", "B"); usb_intDescFID_bInterfaceProtocol = (*env)->GetFieldID(env, usb_intDescClazz, "bInterfaceProtocol", "B"); usb_intDescFID_iInterface = (*env)->GetFieldID(env, usb_intDescClazz, "iInterface", "B"); usb_intDescFID_endpoint = (*env)->GetFieldID(env, usb_intDescClazz, "endpoint", "[Lch/ntb/usb/Usb_Endpoint_Descriptor;"); usb_intDescFID_extra = (*env)->GetFieldID(env, usb_intDescClazz, "extra", "[B"); usb_intDescFID_extralen = (*env)->GetFieldID(env, usb_intDescClazz, "extralen", "I"); // usb_endpoint_descriptor usb_epDescClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Endpoint_Descriptor"); if (usb_epDescClazz == NULL) { return NULL; /* exception thrown */ } usb_epDescMid = (*env)->GetMethodID(env, usb_epDescClazz, "","()V"); if (usb_epDescMid == NULL) { return NULL; } usb_epDescFID_bLength = (*env)->GetFieldID(env, usb_epDescClazz, "bLength", "B"); usb_epDescFID_bDescriptorType = (*env)->GetFieldID(env, usb_epDescClazz, "bDescriptorType", "B"); usb_epDescFID_bEndpointAddress = (*env)->GetFieldID(env, usb_epDescClazz, "bEndpointAddress", "B"); usb_epDescFID_bmAttributes = (*env)->GetFieldID(env, usb_epDescClazz, "bmAttributes", "B"); usb_epDescFID_wMaxPacketSize = (*env)->GetFieldID(env, usb_epDescClazz, "wMaxPacketSize", "S"); usb_epDescFID_bInterval = (*env)->GetFieldID(env, usb_epDescClazz, "bInterval", "B"); usb_epDescFID_bRefresh = (*env)->GetFieldID(env, usb_epDescClazz, "bRefresh", "B"); usb_epDescFID_bSynchAddress = (*env)->GetFieldID(env, usb_epDescClazz, "bSynchAddress", "B"); usb_epDescFID_extra = (*env)->GetFieldID(env, usb_epDescClazz, "extra", "[B"); usb_epDescFID_extralen = (*env)->GetFieldID(env, usb_epDescClazz, "extralen", "I"); #ifdef DEBUGON printf("usb_get_busses: Field initialization done (1)\n"); #endif } //************************************************************************// struct usb_device *dev; struct usb_bus *bus; busses = usb_get_busses(); bus = busses; if (!bus){ return NULL; } // objects jobject main_usb_busObj, usb_busObj, usb_busObj_next, usb_busObj_prev, \ main_usb_devObj, usb_devObj, usb_devObj_next, usb_devObj_prev, \ usb_devDescObj, usb_confDescObj, usb_intObj, usb_intDescObj, \ usb_epDescObj; jobjectArray usb_confDescObjArray, usb_intObjArray, usb_intDescObjArray, usb_epDescObjArray; usb_busObj = NULL; usb_busObj_prev = NULL; main_usb_busObj = NULL; #ifdef DEBUGON printf("usb_get_busses: usb_get_busses done (2)\n"); #endif while (bus){ #ifdef DEBUGON printf("\tusb_get_busses: bus %x (3)\n", bus); #endif // create a new object for every bus if (!usb_busObj) { usb_busObj = (*env)->NewObject(env, usb_busClazz, usb_busMid); if (!usb_busObj) { setLibusbJavaError("shared library error: Error NewObject (usb_busObj)"); return NULL; } main_usb_busObj = usb_busObj; } // fill the fields of the object usb_busObj_next = NULL; if (bus->next){ usb_busObj_next = (*env)->NewObject(env, usb_busClazz, usb_busMid); if (!usb_busObj_next) { setLibusbJavaError("shared library error: Error NewObject (usb_busObj_next)"); return NULL; } } (*env)->SetObjectField(env, usb_busObj, usb_busFID_next, usb_busObj_next); (*env)->SetObjectField(env, usb_busObj, usb_busFID_prev, usb_busObj_prev); (*env)->SetObjectField(env, usb_busObj, usb_busFID_dirname, (*env)->NewStringUTF(env, bus->dirname)); (*env)->SetLongField(env, usb_busObj, usb_busFID_location, bus->location); dev = bus->devices; usb_devObj = NULL; usb_devObj_prev = NULL; main_usb_devObj = NULL; while (dev){ #ifdef DEBUGON printf("\tusb_get_busses: dev %x (4)\n", dev); #endif // create a new object for every device if (!usb_devObj){ usb_devObj = (*env)->NewObject(env, usb_devClazz, usb_devMid); if (!usb_devObj) { setLibusbJavaError("shared library error: Error NewObject (usb_devObj)"); return NULL; } main_usb_devObj = usb_devObj; } // fill the fields of the object usb_devObj_next = NULL; if (dev->next){ usb_devObj_next = (*env)->NewObject(env, usb_devClazz, usb_devMid); if (!usb_devObj_next) { setLibusbJavaError("shared library error: Error NewObject (usb_devObj_next)"); return NULL; } } (*env)->SetObjectField(env, usb_devObj, usb_devFID_next, usb_devObj_next); (*env)->SetObjectField(env, usb_devObj, usb_devFID_prev, usb_devObj_prev); (*env)->SetObjectField(env, usb_devObj, usb_devFID_bus, usb_busObj); (*env)->SetObjectField(env, usb_devObj, usb_devFID_filename, (*env)->NewStringUTF(env, dev->filename)); (*env)->SetByteField(env, usb_devObj, usb_devFID_devnum, dev->devnum); (*env)->SetByteField(env, usb_devObj, usb_devFID_num_children, dev->num_children); (*env)->SetLongField(env, usb_devObj, usb_devFID_devStructAddr, (jlong) dev); // device descriptor usb_devDescObj = (*env)->NewObject(env, usb_devDescClazz, usb_devDescMid); if (!usb_devDescObj) { setLibusbJavaError("shared library error: Error NewObject (usb_devDescObj)"); return NULL; } (*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bLength, dev->descriptor.bLength); (*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bDescriptorType, dev->descriptor.bDescriptorType); (*env)->SetShortField(env, usb_devDescObj, usb_devDescFID_bcdUSB, dev->descriptor.bcdUSB); (*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bDeviceClass, dev->descriptor.bDeviceClass); (*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bDeviceSubClass, dev->descriptor.bDeviceSubClass); (*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bDeviceProtocol, dev->descriptor.bDeviceProtocol); (*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bMaxPacketSize0, dev->descriptor.bMaxPacketSize0); (*env)->SetShortField(env, usb_devDescObj, usb_devDescFID_idVendor, dev->descriptor.idVendor); (*env)->SetShortField(env, usb_devDescObj, usb_devDescFID_idProduct, dev->descriptor.idProduct); (*env)->SetShortField(env, usb_devDescObj, usb_devDescFID_bcdDevice, dev->descriptor.bcdDevice); (*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_iManufacturer, dev->descriptor.iManufacturer); (*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_iProduct, dev->descriptor.iProduct); (*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_iSerialNumber, dev->descriptor.iSerialNumber); (*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bNumConfigurations, dev->descriptor.bNumConfigurations); (*env)->SetObjectField(env, usb_devObj, usb_devFID_descriptor, usb_devDescObj); // configuration descriptor // Loop through all of the configurations usb_confDescObjArray = (jobjectArray) (*env)->NewObjectArray(env, dev->descriptor.bNumConfigurations, usb_confDescClazz, NULL); if (!usb_confDescObjArray) { setLibusbJavaError("shared library error: Error NewObject 6"); return NULL; } for (int c = 0; c < dev->descriptor.bNumConfigurations; c++){ #ifdef DEBUGON printf("\t\tusb_get_busses: configuration %x (5)\n", c); #endif if (dev->config == NULL) { // this shouldn't happen, but it did occasionally (maybe this is (or probably was) a libusb bug) setLibusbJavaError("shared library error: dev->config == NULL"); return main_usb_busObj; } usb_confDescObj = (*env)->NewObject(env, usb_confDescClazz, usb_confDescMid); if (!usb_confDescObj) { setLibusbJavaError("shared library error: Error NewObject (usb_confDescObj)"); return NULL; } (*env)->SetObjectArrayElement(env, usb_confDescObjArray, c, usb_confDescObj); (*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_bLength, dev->config[c].bLength); (*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_bDescriptorType, dev->config[c].bDescriptorType); (*env)->SetShortField(env, usb_confDescObj, usb_confDescFID_wTotalLength, dev->config[c].wTotalLength); (*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_bNumInterfaces, dev->config[c].bNumInterfaces); (*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_bConfigurationValue, dev->config[c].bConfigurationValue); (*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_iConfiguration, dev->config[c].iConfiguration); (*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_bmAttributes, dev->config[c].bmAttributes); (*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_MaxPower, dev->config[c].MaxPower); (*env)->SetIntField(env, usb_confDescObj, usb_confDescFID_extralen, dev->config[c].extralen); if (dev->config[c].extra){ jbyteArray jbExtraDesc = (*env)->NewByteArray(env, dev->config[c].extralen); (*env)->SetByteArrayRegion(env, jbExtraDesc, 0, dev->config[c].extralen, (jbyte *) dev->config[c].extra); (*env)->SetObjectField(env, usb_confDescObj, usb_confDescFID_extra, jbExtraDesc); } else { (*env)->SetObjectField(env, usb_confDescObj, usb_confDescFID_extra, NULL); } // interface usb_intObjArray = (jobjectArray) (*env)->NewObjectArray(env, dev->config[c].bNumInterfaces, usb_intClazz, NULL); if (!usb_intObjArray) { setLibusbJavaError("shared library error: Error NewObject (usb_intObjArray)"); return NULL; } for (int i = 0; i < dev->config[c].bNumInterfaces; i++){ #ifdef DEBUGON printf("\t\t\tusb_get_busses: interface %x (6)\n", i); #endif if (dev->config[c].interface == NULL) { // this shouldn't happen printf("dev->config[c].interface == NULL"); return main_usb_busObj; } usb_intObj = (*env)->NewObject(env, usb_intClazz, usb_intMid); if (!usb_intObj) { setLibusbJavaError("shared library error: Error NewObject (usb_intObj)"); return NULL; } (*env)->SetObjectArrayElement(env, usb_intObjArray, i, usb_intObj); (*env)->SetIntField(env, usb_intObj, usb_intFID_num_altsetting, dev->config[c].interface[i].num_altsetting); // interface descriptor usb_intDescObjArray = (jobjectArray) (*env)->NewObjectArray(env, dev->config[c].interface[i].num_altsetting, usb_intDescClazz, NULL); if (!usb_intDescObjArray) { setLibusbJavaError("shared library error: Error NewObject (usb_intDescObjArray)"); return NULL; } for (int a = 0; a < dev->config[c].interface[i].num_altsetting; a++){ #ifdef DEBUGON printf("\t\t\t\tusb_get_busses: interface descriptor %x (7)\n", a); #endif if (dev->config[c].interface[i].altsetting == NULL) { // this shouldn't happen printf("LibusbJava: usb_get_busses: dev->config[c].interface[i].altsetting == NULL\n"); return main_usb_busObj; } usb_intDescObj = (*env)->NewObject(env, usb_intDescClazz, usb_intDescMid); if (!usb_intDescObj) { setLibusbJavaError("shared library error: Error NewObject (usb_intDescObj)"); return NULL; } (*env)->SetObjectArrayElement(env, usb_intDescObjArray, a, usb_intDescObj); (*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bLength, dev->config[c].interface[i].altsetting[a].bLength); (*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bDescriptorType, dev->config[c].interface[i].altsetting[a].bDescriptorType); (*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bInterfaceNumber, dev->config[c].interface[i].altsetting[a].bInterfaceNumber); (*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bAlternateSetting, dev->config[c].interface[i].altsetting[a].bAlternateSetting); (*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bNumEndpoints, dev->config[c].interface[i].altsetting[a].bNumEndpoints); (*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bInterfaceClass, dev->config[c].interface[i].altsetting[a].bInterfaceClass); (*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bInterfaceSubClass, dev->config[c].interface[i].altsetting[a].bInterfaceSubClass); (*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bInterfaceProtocol, dev->config[c].interface[i].altsetting[a].bInterfaceProtocol); (*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_iInterface, dev->config[c].interface[i].altsetting[a].iInterface); (*env)->SetIntField(env, usb_intDescObj, usb_intDescFID_extralen, dev->config[c].interface[i].altsetting[a].extralen); if (dev->config[c].interface[i].altsetting[a].extra){ jbyteArray jbExtraDesc = (*env)->NewByteArray(env, dev->config[c].interface[i].altsetting[a].extralen); (*env)->SetByteArrayRegion(env, jbExtraDesc, 0, dev->config[c].interface[i].altsetting[a].extralen, (jbyte *) dev->config[c].interface[i].altsetting[a].extra); (*env)->SetObjectField(env, usb_intDescObj, usb_intDescFID_extra, jbExtraDesc); } else { (*env)->SetObjectField(env, usb_intDescObj, usb_intDescFID_extra, NULL); } // endpoint descriptor usb_epDescObjArray = (jobjectArray) (*env)->NewObjectArray(env, dev->config[c].interface[i].altsetting[a].bNumEndpoints, usb_epDescClazz, NULL); if (!usb_epDescObjArray) { setLibusbJavaError("shared library error: Error NewObject (usb_epDescObjArray)"); return NULL; } for (int e = 0; e < dev->config[c].interface[i].altsetting[a].bNumEndpoints; e++){ #ifdef DEBUGON printf("\t\t\t\t\tusb_get_busses: endpoint descriptor %x (8)\n", e); #endif if (dev->config[c].interface[i].altsetting[a].endpoint == NULL) { printf("LibusbJava: usb_get_busses: dev->config[c].interface[i].altsetting[a].endpoint == NULL\n"); return main_usb_busObj; } usb_epDescObj = (*env)->NewObject(env, usb_epDescClazz, usb_epDescMid); if (!usb_epDescObj) { setLibusbJavaError("shared library error: Error NewObject (usb_epDescObj)"); return NULL; } (*env)->SetObjectArrayElement(env, usb_epDescObjArray, e, usb_epDescObj); (*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bLength, dev->config[c].interface[i].altsetting[a].endpoint[e].bLength); (*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bDescriptorType, dev->config[c].interface[i].altsetting[a].endpoint[e].bDescriptorType); (*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bEndpointAddress, dev->config[c].interface[i].altsetting[a].endpoint[e].bEndpointAddress); (*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bmAttributes, dev->config[c].interface[i].altsetting[a].endpoint[e].bmAttributes); (*env)->SetShortField(env, usb_epDescObj, usb_epDescFID_wMaxPacketSize, dev->config[c].interface[i].altsetting[a].endpoint[e].wMaxPacketSize); (*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bInterval, dev->config[c].interface[i].altsetting[a].endpoint[e].bInterval); (*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bRefresh, dev->config[c].interface[i].altsetting[a].endpoint[e].bRefresh); (*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bSynchAddress, dev->config[c].interface[i].altsetting[a].endpoint[e].bSynchAddress); (*env)->SetIntField(env, usb_epDescObj, usb_epDescFID_extralen, dev->config[c].interface[i].altsetting[a].endpoint[e].extralen); if (dev->config[c].interface[i].altsetting[a].endpoint[e].extra){ jbyteArray jbExtraDesc = (*env)->NewByteArray(env, dev->config[c].interface[i].altsetting[a].endpoint[e].extralen); (*env)->SetByteArrayRegion(env, jbExtraDesc, 0, dev->config[c].interface[i].altsetting[a].endpoint[e].extralen, (jbyte *) dev->config[c].interface[i].altsetting[a].endpoint[e].extra); (*env)->SetObjectField(env, usb_epDescObj, usb_epDescFID_extra, jbExtraDesc); } else { (*env)->SetObjectField(env, usb_epDescObj, usb_epDescFID_extra, NULL); } } (*env)->SetObjectField(env, usb_intDescObj, usb_intDescFID_endpoint, usb_epDescObjArray); } (*env)->SetObjectField(env, usb_intObj, usb_intFID_altsetting, usb_intDescObjArray); } (*env)->SetObjectField(env, usb_confDescObj, usb_confDescFID_interface_, usb_intObjArray); } (*env)->SetObjectField(env, usb_devObj, usb_devFID_config, usb_confDescObjArray); usb_devObj_prev = usb_devObj; usb_devObj = usb_devObj_next; dev = dev->next; } (*env)->SetObjectField(env, usb_busObj, usb_busFID_devices, main_usb_devObj); (*env)->SetObjectField(env, usb_busObj, usb_busFID_root_dev, main_usb_devObj); usb_busObj_prev = usb_busObj; usb_busObj = usb_busObj_next; bus = bus->next; } #ifdef DEBUGON printf("usb_get_busses: done\n"); #endif return main_usb_busObj; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_open * Signature: (Lch/ntb/usb/Usb_Device;)I */ JNIEXPORT jlong JNICALL Java_ch_ntb_usb_LibusbJava_usb_1open (JNIEnv *env, jclass obj, jobject dev) { clearLibusbJavaError(); if (busses == NULL) { setLibusbJavaError("shared library error: busses is null"); return 0; } jlong usb_device_cmp_addr = (*env)->GetLongField(env, dev, usb_devFID_devStructAddr); struct usb_bus *tmpBus; for (tmpBus = busses; tmpBus; tmpBus = tmpBus->next) { struct usb_device *device; for (device = tmpBus->devices; device; device = device->next) { if ((jlong) device == usb_device_cmp_addr){ return (jlong) usb_open(device); } } } setLibusbJavaError("shared library error: no device with dev.devnum found on busses"); return 0; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_close * Signature: (J)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1close (JNIEnv *env, jclass obj, jlong dev_handle) { clearLibusbJavaError(); return (jint) usb_close((usb_dev_handle *) dev_handle); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_set_configuration * Signature: (JI)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1set_1configuration (JNIEnv *env, jclass obj, jlong dev_handle, jint configuration) { clearLibusbJavaError(); return usb_set_configuration((usb_dev_handle *) dev_handle, configuration); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_set_altinterface * Signature: (JI)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1set_1altinterface (JNIEnv *env, jclass obj, jlong dev_handle, jint alternate) { clearLibusbJavaError(); return usb_set_altinterface((usb_dev_handle *) dev_handle, alternate); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_clear_halt * Signature: (JI)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1clear_1halt (JNIEnv *env, jclass obj, jlong dev_handle, jint ep) { clearLibusbJavaError(); return usb_clear_halt((usb_dev_handle *) dev_handle, (unsigned) ep); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_reset * Signature: (J)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1reset (JNIEnv *env, jclass obj, jlong dev_handle) { clearLibusbJavaError(); return usb_reset((usb_dev_handle *) dev_handle); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_claim_interface * Signature: (JI)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1claim_1interface (JNIEnv *env, jclass obj, jlong dev_handle, jint interface) { clearLibusbJavaError(); return usb_claim_interface((usb_dev_handle *) dev_handle, interface); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_release_interface * Signature: (JI)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1release_1interface (JNIEnv *env, jclass obj, jlong dev_handle, jint interface) { clearLibusbJavaError(); return usb_release_interface((usb_dev_handle *) dev_handle, interface); } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_control_msg * Signature: (JIIII[BII)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1control_1msg (JNIEnv *env, jclass obj, jlong dev_handle, jint requesttype, jint request, jint value, jint index, jbyteArray jbytes, jint size, jint timeout) { clearLibusbJavaError(); jbyte *bytes = (*env)->GetByteArrayElements(env, jbytes, NULL); int num_bytes = usb_control_msg((usb_dev_handle *) dev_handle, requesttype, request, value, index, (char *) bytes, size, timeout); (*env)->SetByteArrayRegion(env, jbytes, 0, size, bytes); (*env)->ReleaseByteArrayElements(env, jbytes, bytes, 0); return num_bytes; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_get_string * Signature: (JII)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1string (JNIEnv *env, jclass obj, jlong dev_handle, jint index, jint langid) { clearLibusbJavaError(); char string[256]; int retVal = usb_get_string((usb_dev_handle *) dev_handle, index, langid, string, 256); if (retVal > 0) return (*env)->NewStringUTF(env, string); return 0; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_get_string_simple * Signature: (JI)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1string_1simple (JNIEnv *env, jclass obj, jlong dev_handle, jint index) { clearLibusbJavaError(); char string[256]; int retVal = usb_get_string_simple((usb_dev_handle *) dev_handle, index, string, 256); if (retVal > 0) return (*env)->NewStringUTF(env, string); return 0; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_get_descriptor * Signature: (JBBI)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1descriptor (JNIEnv *env, jclass obj, jlong dev_handle, jbyte type, jbyte index, jint size) { clearLibusbJavaError(); char *string = (char *) malloc(size * sizeof(char)); int retVal = usb_get_descriptor((usb_dev_handle *) dev_handle, (unsigned) type, (unsigned) index, string, size); if (retVal > 0) return (*env)->NewStringUTF(env, string); return 0; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_get_descriptor_by_endpoint * Signature: (JIBBI)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1descriptor_1by_1endpoint (JNIEnv *env, jclass obj, jlong dev_handle, jint ep, jbyte type, jbyte index, jint size) { clearLibusbJavaError(); char *string = (char *) malloc(size * sizeof(char)); int retVal = usb_get_descriptor_by_endpoint((usb_dev_handle *) dev_handle, ep, (unsigned) type, (unsigned) index, string, size); if (retVal > 0) return (*env)->NewStringUTF(env, string); return 0; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_bulk_write * Signature: (JI[BII)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1bulk_1write (JNIEnv *env, jclass obj, jlong dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout) { clearLibusbJavaError(); jbyte *bytes = (*env)->GetByteArrayElements(env, jbytes, NULL); int num_bytes = usb_bulk_write((usb_dev_handle *) dev_handle, ep, (char *) bytes, size, timeout); (*env)->ReleaseByteArrayElements(env, jbytes, bytes, 0); return num_bytes; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_bulk_read * Signature: (JI[BII)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1bulk_1read (JNIEnv *env, jclass obj, jlong dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout) { clearLibusbJavaError(); char *bytes = (char *) malloc(size * sizeof(char)); int num_bytes = usb_bulk_read((usb_dev_handle *) dev_handle, ep, bytes, size, timeout); if (!bytes) { return num_bytes; } (*env)->SetByteArrayRegion(env, jbytes, 0, size, (jbyte *) bytes); free(bytes); return num_bytes; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_interrupt_write * Signature: (JI[BII)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1interrupt_1write (JNIEnv *env, jclass obj, jlong dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout) { clearLibusbJavaError(); jbyte *bytes = (*env)->GetByteArrayElements(env, jbytes, NULL); int num_bytes = usb_interrupt_write( (usb_dev_handle *) dev_handle, ep, (char *) bytes, size, timeout); (*env)->ReleaseByteArrayElements(env, jbytes, bytes, 0); return num_bytes; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_interrupt_read * Signature: (JI[BII)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1interrupt_1read (JNIEnv *env, jclass obj, jlong dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout) { clearLibusbJavaError(); char *bytes = (char *) malloc(size * sizeof(char)); int num_bytes = usb_interrupt_read((usb_dev_handle *) dev_handle, ep, bytes, size, timeout); if (!bytes) { return num_bytes; } (*env)->SetByteArrayRegion(env, jbytes, 0, size, (jbyte *) bytes); free(bytes); return num_bytes; } /* * Class: ch_ntb_usb_LibusbJava * Method: usb_strerror * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1strerror (JNIEnv *env, jclass obj){ char *str; // check for LibusbJava specific errors first if (libusbJavaError != NULL) { str = libusbJavaError; clearLibusbJavaError(); } else { str = usb_strerror(); } return (*env)->NewStringUTF(env, 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; } } libusb-java-0.8+ztex20090101/build-w64.sh0000755000175000017500000000060711522221202017307 0ustar moellermoellerexport PATH=/drv_t1/mingw/w64/bin:$PATH triple=x86_64-w64-mingw32 JAVAPREFIX=/usr/local/java TARGET=libusbJava64.dll $triple-gcc -m64 -shared -std=c99 -Wall -Wno-pointer-to-int-cast -D_JNI_IMPLEMENTATION_ -Wl,--kill-at \ -I$JAVAPREFIX/include -I$JAVAPREFIX/include/win32 -I../libusb-win32 -L../libusb-win32/amd64 LibusbJava.c -lusb0 -o $TARGET $triple-strip $TARGET chmod -x $TARGET libusb-java-0.8+ztex20090101/Makefile0000644000175000017500000000512511522245171016706 0ustar moellermoeller# Makefile for libusbJava, a Java libusb wrapper # Copyright (C) 2009 ZTEX e.K. # http://www.ztex.de # # This Makefile ia a part of the reorganized version of the libusbJava source tree # with proper makefiles. (SVN version of Apr. 6, 2009). # # Please visit http://libusbjava.sourceforge.net for more information about the # original project. ######################### # Configuration section # ######################### # $(JAVAPREFIX)/include should contain jni.h JAVAPREFIX=/usr/local/java ############################### # this should not be modified # ############################### GCC=gcc STRIP=strip CHMOD=chmod -x JAVAC=javac RM=rm -f INSTALL=install INSTALLDIR=$(INSTALL) -d INSTALLEXE=$(INSTALL) -m 0755 INSTALLFILE=$(INSTALL) -m 0644 OBJEXTRADEPS=LibusbJava.h LIBSRCS=LibusbJava.o LIBSRCS64=LibusbJava.c JAVASRCS=$(shell echo ch/ntb/usb/*.java) $(shell echo ch/ntb/usb/logger/*.java) LIBTARGET_SH=libusbJavaSh.so LIBTARGET_ST=libusbJavaSt.so LIBTARGET_64=libusbJava64.so LIBLIBS=-lusb LIBINCS=-I $(JAVAPREFIX)/include -I $(JAVAPREFIX)/include/linux .PHONY: all libs classes install2 clean distclean all: libs classes classes: classes.made libs: $(LIBTARGET_SH) $(LIBTARGET_ST) $(LIBTARGET_64) %.o: %.c LibusbJava.h $(GCC) -fPIC -g -c -std=c99 -Wall -Wno-pointer-to-int-cast $(LIBINCS) $< -o$@ $(LIBTARGET_ST): $(LIBSRCS) # $(GCC) -shared -Wl,-static,-soname,$(LIBTARGET_ST) $(LIBINCS) -static $(LIBSRCS) -o $(LIBTARGET_ST) $(LIBLIBS) $(GCC) -shared -Wl,-soname,$(LIBTARGET_ST),-static $(LIBINCS) $(LIBSRCS) -static -o $(LIBTARGET_ST) $(LIBLIBS) $(STRIP) $(LIBTARGET_ST) $(CHMOD) $(LIBTARGET_ST) $(LIBTARGET_SH): $(LIBSRCS) $(GCC) -fPIC -shared -Wl,-soname,$(LIBTARGET_SH) $(LIBINCS) $(LIBSRCS) -o $(LIBTARGET_SH) $(LIBLIBS) $(STRIP) $(LIBTARGET_SH) $(CHMOD) $(LIBTARGET_SH) $(LIBTARGET_64): $(LIBSRCS64) $(GCC) -fPIC -m64 -shared -std=c99 -Wall -Wno-pointer-to-int-cast -Wl,-soname,$(LIBTARGET_64) $(LIBINCS) $(LIBSRCS64) $(LIBLIBS) -o $(LIBTARGET_64) $(STRIP) $(LIBTARGET_64) $(CHMOD) $(LIBTARGET_64) classes.made: $(JAVASRCS) $(JAVAC) $(JAVASRCS) echo > classes.made doc: javadoc ch.ntb.usb -d ../docs/libusbJava install2: all $(INSTALLDIR) ../libusbJava/ch/ntb/usb/logger $(INSTALLFILE) $(LIBTARGET_SH) ../libusbJava $(INSTALLFILE) $(LIBTARGET_ST) ../libusbJava $(INSTALLFILE) $(LIBTARGET_64) ../libusbJava $(INSTALLFILE) ch/ntb/usb/*.class ../libusbJava/ch/ntb/usb $(INSTALLFILE) ch/ntb/usb/logger/*.class ../libusbJava/ch/ntb/usb/logger clean: distclean: $(RM) *.o $(LIBTARGET_SH) $(LIBTARGET_ST) $(LIBTARGET_64) libusbJava*.dll ch/ntb/usb/*.class ch/ntb/usb/logger/*.class classes.made libusb-java-0.8+ztex20090101/LibusbJava.h0000644000175000017500000001211611517665564017457 0ustar moellermoeller/* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class ch_ntb_usb_LibusbJava */ #ifndef _Included_ch_ntb_usb_LibusbJava #define _Included_ch_ntb_usb_LibusbJava /* * Class: ch_ntb_usb_LibusbJava * Method: usb_set_debug * Signature: (I)V */ JNIEXPORT void JNICALL Java_ch_ntb_usb_LibusbJava_usb_1set_1debug (JNIEnv *, jclass, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_init * Signature: ()V */ JNIEXPORT void JNICALL Java_ch_ntb_usb_LibusbJava_usb_1init (JNIEnv *, jclass); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_find_busses * Signature: ()I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1find_1busses (JNIEnv *, jclass); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_find_devices * Signature: ()I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1find_1devices (JNIEnv *, jclass); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_get_busses * Signature: ()Lch/ntb/usb/Usb_Bus; */ JNIEXPORT jobject JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1busses (JNIEnv *, jclass); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_open * Signature: (Lch/ntb/usb/Usb_Device;)J */ JNIEXPORT jlong JNICALL Java_ch_ntb_usb_LibusbJava_usb_1open (JNIEnv *, jclass, jobject); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_close * Signature: (J)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1close (JNIEnv *, jclass, jlong); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_set_configuration * Signature: (JI)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1set_1configuration (JNIEnv *, jclass, jlong, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_set_altinterface * Signature: (JI)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1set_1altinterface (JNIEnv *, jclass, jlong, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_clear_halt * Signature: (JI)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1clear_1halt (JNIEnv *, jclass, jlong, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_reset * Signature: (J)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1reset (JNIEnv *, jclass, jlong); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_claim_interface * Signature: (JI)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1claim_1interface (JNIEnv *, jclass, jlong, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_release_interface * Signature: (JI)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1release_1interface (JNIEnv *, jclass, jlong, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_control_msg * Signature: (JIIII[BII)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1control_1msg (JNIEnv *, jclass, jlong, jint, jint, jint, jint, jbyteArray, jint, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_get_string * Signature: (JII)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1string (JNIEnv *, jclass, jlong, jint, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_get_string_simple * Signature: (JI)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1string_1simple (JNIEnv *, jclass, jlong, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_get_descriptor * Signature: (JBBI)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1descriptor (JNIEnv *, jclass, jlong, jbyte, jbyte, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_get_descriptor_by_endpoint * Signature: (JIBBI)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1descriptor_1by_1endpoint (JNIEnv *, jclass, jlong, jint, jbyte, jbyte, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_bulk_write * Signature: (JI[BII)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1bulk_1write (JNIEnv *, jclass, jlong, jint, jbyteArray, jint, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_bulk_read * Signature: (JI[BII)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1bulk_1read (JNIEnv *, jclass, jlong, jint, jbyteArray, jint, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_interrupt_write * Signature: (JI[BII)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1interrupt_1write (JNIEnv *, jclass, jlong, jint, jbyteArray, jint, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_interrupt_read * Signature: (JI[BII)I */ JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1interrupt_1read (JNIEnv *, jclass, jlong, jint, jbyteArray, jint, jint); /* * Class: ch_ntb_usb_LibusbJava * Method: usb_strerror * Signature: ()Ljava/lang/String; */ 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); #endif libusb-java-0.8+ztex20090101/Readme0000644000175000017500000000032011166441665016370 0ustar moellermoellerThis is a reorganized version of libusbJava source tree with proper makefiles. (SVN version of Apr. 6, 2009). Please visit http://libusbjava.sourceforge.net for more information about the original project. libusb-java-0.8+ztex20090101/ch/0000755000175000017500000000000011166414653015644 5ustar moellermoellerlibusb-java-0.8+ztex20090101/ch/ntb/0000755000175000017500000000000011166414657016433 5ustar moellermoellerlibusb-java-0.8+ztex20090101/ch/ntb/usb/0000755000175000017500000000000011522351517017213 5ustar moellermoellerlibusb-java-0.8+ztex20090101/ch/ntb/usb/Usb_Device.java0000644000175000017500000000470611166457332022103 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; /** * Represents an USB device.
* An USB device has one device descriptor and it may have multiple * configuration descriptors. * */ public class Usb_Device { private Usb_Device next, prev; private String filename; private Usb_Bus bus; private Usb_Device_Descriptor descriptor; private Usb_Config_Descriptor[] config; private byte devnum; private byte num_children; private Usb_Device children; /** * The address of the device structure to be passed to usb_open. This value * is used only internally so we don't use getter or setter methods. */ @SuppressWarnings("unused") private long devStructAddr; /** * Returns the reference to the bus to which this device is connected.
* * @return the reference to the bus to which this device is connected */ public Usb_Bus getBus() { return bus; } /** * Returns a reference to the first child.
* * @return a reference to the first child */ public Usb_Device getChildren() { return children; } /** * Returns the USB config descriptors.
* * @return the USB config descriptors */ public Usb_Config_Descriptor[] getConfig() { return config; } /** * Returns the USB device descriptor.
* * @return the USB device descriptor */ public Usb_Device_Descriptor getDescriptor() { return descriptor; } /** * Returns the number assigned to this device.
* * @return the number assigned to this device */ public byte getDevnum() { return devnum; } /** * Returns the systems String representation.
* * @return the systems String representation */ public String getFilename() { return filename; } /** * Returns the pointer to the next device.
* * @return the pointer to the next device or null */ public Usb_Device getNext() { return next; } /** * Returns the number of children of this device.
* * @return the number of children of this device */ public byte getNumChildren() { return num_children; } /** * Returns the pointer to the previous device.
* * @return the pointer to the previous device or null */ public Usb_Device getPrev() { return prev; } @Override public String toString() { return "Usb_Device " + filename; } }libusb-java-0.8+ztex20090101/ch/ntb/usb/LibusbJava.java0000644000175000017500000003072211522351460022101 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; /** * This class represents the Java Native Interface to the shared library which * is (with some exceptions) a one-to-one representation of the libusb API.
*
*

Project Description

* Java libusb is a Java wrapper for the libusb and libusb-win32 USB library. * * libusb aim is to create a * library for use by user level applications to access USB devices regardless * of OS.
* Libusb-win32 is a port of * the USB library libusb to the * Windows operating systems. The library allows user space applications to * access any USB device on Windows in a generic way without writing any line of * kernel driver code.
*
* The API description of this class has been copied from the libusb documentation * and adapted where neccessary.
* */ 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.
* * The range is from 0 to 255, where 0 disables debug output and 255 enables * all output. On application start, debugging is disabled (0). * * @param level * 0 to 255 */ public static native void usb_set_debug(int level); // Core /** * Just like the name implies, usb_init sets up some internal * structures. usb_init must be called before any other * libusb functions. */ public static native void usb_init(); /** * usb_find_busses will find all of the busses on the system. * * @return the number of changes since previous call to this function (total * of new busses and busses removed). */ public static native int usb_find_busses(); /** * usb_find_devices will find all of the devices on each bus. * This should be called after usb_find_busses. * * @return the number of changes since the previous call to this function * (total of new device and devices removed). */ public static native int usb_find_devices(); /** * usb_get_busses returns a tree of descriptor objects.
* The tree represents the bus structure with devices, configurations, * interfaces and endpoints. Note that this is only a copy. To refresh the * information, usb_get_busses() must be called again.
* The name of the objects contained in the tree is starting with * Usb_. * * @return the structure of all busses and devices. Note: The * java objects are copies of the C structs. */ public static native Usb_Bus usb_get_busses(); // Device Operations /** * usb_open is to be used to open up a device for use. * usb_open must be called before attempting to perform any * operations to the device. * * @param dev * The device to open. * @return a handle used in future communication with the device. 0 if an * error has occurred. */ public static native long usb_open(Usb_Device dev); /** * usb_close closes a device opened with * usb_open. * * @param dev_handle * The handle to the device. * @return 0 on success or < 0 on error. */ public static native int usb_close(long dev_handle); /** * Sets the active configuration of a device * * @param dev_handle * The handle to the device. * @param configuration * The value as specified in the descriptor field * bConfigurationValue. * @return 0 on success or < 0 on error. */ public static native int usb_set_configuration(long dev_handle, int configuration); /** * Sets the active alternate setting of the current interface * * @param dev_handle * The handle to the device. * @param alternate * The value as specified in the descriptor field * bAlternateSetting. * @return 0 on success or < 0 on error. */ public static native int usb_set_altinterface(long dev_handle, int alternate); /** * Clears any halt status on an endpoint. * * @param dev_handle * The handle to the device. * @param ep * The value specified in the descriptor field bEndpointAddress. * @return 0 on success or < 0 on error. */ public static native int usb_clear_halt(long dev_handle, int ep); /** * Resets a device by sending a RESET down the port it is connected to.
*
* Causes re-enumeration: After calling usb_reset, * the device will need to re-enumerate and thusly, requires you to find the * new device and open a new handle. The handle used to call * usb_reset will no longer work. * * @param dev_handle * The handle to the device. * @return 0 on success or < 0 on error. */ public static native int usb_reset(long dev_handle); /** * Claim an interface of a device.
*
* Must be called!: usb_claim_interface must be * called before you perform any operations related to this interface (like * usb_set_altinterface, usb_bulk_write, etc). * * @param dev_handle * The handle to the device. * @param interface_ * The value as specified in the descriptor field * bInterfaceNumber. * @return 0 on success or < 0 on error. */ public static native int usb_claim_interface(long dev_handle, int interface_); /** * Releases a previously claimed interface * * @param dev_handle * The handle to the device. * @param interface_ * The value as specified in the descriptor field * bInterfaceNumber. * @return 0 on success or < 0 on error. */ public static native int usb_release_interface(long dev_handle, int interface_); // Control Transfers /** * Performs a control request to the default control pipe on a device. The * parameters mirror the types of the same name in the USB specification. * * @param dev_handle * The handle to the device. * @param requesttype * @param request * @param value * @param index * @param bytes * @param size * @param timeout * @return the number of bytes written/read or < 0 on error. */ public static native int usb_control_msg(long dev_handle, int requesttype, int request, int value, int index, byte[] bytes, int size, int timeout); /** * Retrieves the string descriptor specified by index and langid from a * device. * * @param dev_handle * The handle to the device. * @param index * @param langid * @return the descriptor String or null */ public static native String usb_get_string(long dev_handle, int index, int langid); /** * usb_get_string_simple is a wrapper around * usb_get_string that retrieves the string description * specified by index in the first language for the descriptor. * * @param dev_handle * The handle to the device. * @param index * @return the descriptor String or null */ public static native String usb_get_string_simple(long dev_handle, int index); /** * Retrieves a descriptor from the device identified by the type and index * of the descriptor from the default control pipe.
*
* See {@link #usb_get_descriptor_by_endpoint(long, int, byte, byte, int)} * for a function that allows the control endpoint to be specified. * * @param dev_handle * The handle to the device. * @param type * @param index * @param size * number of charactes which will be retrieved (the length of the * resulting String) * @return the descriptor String or null */ public static native String usb_get_descriptor(long dev_handle, byte type, byte index, int size); /** * Retrieves a descriptor from the device identified by the type and index * of the descriptor from the control pipe identified by ep. * * @param dev_handle * The handle to the device. * @param ep * @param type * @param index * @param size * number of charactes which will be retrieved (the length of the * resulting String) * @return the descriptor String or null */ public static native String usb_get_descriptor_by_endpoint(long dev_handle, int ep, byte type, byte index, int size); // Bulk Transfers /** * Performs a bulk write request to the endpoint specified by ep. * * @param dev_handle * The handle to the device. * @param ep * @param bytes * @param size * @param timeout * @return the number of bytes written on success or < 0 on error. */ public static native int usb_bulk_write(long dev_handle, int ep, byte[] bytes, int size, int timeout); /** * Performs a bulk read request to the endpoint specified by ep. * * @param dev_handle * The handle to the device. * @param ep * @param bytes * @param size * @param timeout * @return the number of bytes read on success or < 0 on error. */ public static native int usb_bulk_read(long dev_handle, int ep, byte[] bytes, int size, int timeout); // Interrupt Transfers /** * Performs an interrupt write request to the endpoint specified by ep. * * @param dev_handle * The handle to the device. * @param ep * @param bytes * @param size * @param timeout * @return the number of bytes written on success or < 0 on error. */ public static native int usb_interrupt_write(long dev_handle, int ep, byte[] bytes, int size, int timeout); /** * Performs a interrupt read request to the endpoint specified by ep. * * @param dev_handle * The handle to the device. * @param ep * @param bytes * @param size * @param timeout * @return the number of bytes read on success or < 0 on error. */ public static native int usb_interrupt_read(long dev_handle, int ep, byte[] bytes, int size, int timeout); /** * Returns the error string after an error occured. * * @return the last error sring. */ public static native String usb_strerror(); /** **************************************************************** */ /** * 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 { // System.out.println("os.name: " + System.getProperty("os.name")); // System.out.println("os.arch: " + System.getProperty("os.arch")); String os = System.getProperty("os.name"); if (os.contains("Windows")) { if ( System.getProperty("os.arch").equalsIgnoreCase("amd64") ) { LibLoader.load( "libusbJava64" ); } else { LibLoader.load( "libusbJava32" ); } } else if ( System.getProperty("os.arch").equalsIgnoreCase("amd64") ) { LibLoader.load( "usbJava64" ); // System.err.println("loaded libusbJava64"); } else { try { LibLoader.load( "usbJavaSh" ); // System.err.println("loaded libusbJavaSh"); } catch ( UnsatisfiedLinkError e ) { LibLoader.load( "usbJavaSt" ); // System.err.println("loaded libusbJavaSt"); } } // 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); } }libusb-java-0.8+ztex20090101/ch/ntb/usb/Device.java0000644000175000017500000005711511517667007021276 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; import java.util.logging.Level; import java.util.logging.Logger; import ch.ntb.usb.logger.LogUtil; /** * This class represents an USB device.
* To get an instance of an USB device use USB.getDevice(...). * */ public class Device { private static final Logger logger = LogUtil.getLogger("ch.ntb.usb"); private int maxPacketSize; /** * Mandatory identification values for the device. */ private int idVendor, idProduct; /** * Optional identification value for the device (e.g. if there are multiple * devices with the same vendor and product id). */ private String filename; private int dev_configuration, dev_interface, dev_altinterface; private long usbDevHandle; private boolean resetOnFirstOpen, resetDone; private int resetTimeout = 2000; private Usb_Device dev; protected Device(short idVendor, short idProduct) { resetOnFirstOpen = false; resetDone = false; maxPacketSize = -1; this.idVendor = idVendor; this.idProduct = idProduct; this.filename = null; } protected Device(short idVendor, short idProduct, String filename) { resetOnFirstOpen = false; resetDone = false; maxPacketSize = -1; this.idVendor = idVendor; this.idProduct = idProduct; this.filename = filename; } private void updateMaxPacketSize(Usb_Device device) throws USBException { maxPacketSize = -1; Usb_Config_Descriptor[] confDesc = device.getConfig(); for (int i = 0; i < confDesc.length; i++) { Usb_Interface[] int_ = confDesc[i].getInterface(); for (int j = 0; j < int_.length; j++) { Usb_Interface_Descriptor[] intDesc = int_[j].getAltsetting(); for (int k = 0; k < intDesc.length; k++) { Usb_Endpoint_Descriptor[] epDesc = intDesc[k].getEndpoint(); for (int l = 0; l < epDesc.length; l++) { maxPacketSize = Math.max(epDesc[l].getWMaxPacketSize(), maxPacketSize); } } } } if (maxPacketSize <= 0) { throw new USBException( "No USB endpoints found. Check the device configuration"); } } /** * Initializes the device. The parameters idVendor and * idProduct are mandatory. The parameter * filename is optional. */ private Usb_Device initDevice(int idVendorParam, int idProductParam, String filename) throws USBException { Usb_Bus bus = USB.getBus(); Usb_Device device = null; // search for device while (bus != null) { device = bus.getDevices(); while (device != null) { Usb_Device_Descriptor devDesc = device.getDescriptor(); if (filename != null && filename.compareTo(device.getFilename()) == 0 && devDesc.getIdVendor() == idVendorParam && devDesc.getIdProduct() == idProductParam) { // idVendor, idProduct and filename logger.info("Device found: " + device.getFilename()); updateMaxPacketSize(device); return device; } else if (devDesc.getIdVendor() == idVendorParam && devDesc.getIdProduct() == idProductParam) { // only idVendor and idProduct logger.info("Device found: " + device.getFilename()); updateMaxPacketSize(device); return device; } device = device.getNext(); } bus = bus.getNext(); } return null; } /** * Updates the device and descriptor information from the bus.
* The descriptors can be read with {@link #getDeviceDescriptor()} and * {@link #getConfigDescriptors()}. * * @throws USBException */ public void updateDescriptors() throws USBException { dev = initDevice(idVendor, idProduct, filename); } /** * Returns the device descriptor associated with this device.
* The descriptor is updated by calling {@link #updateDescriptors()} or * {@link #open(int, int, int)}. * * @return the device descriptor associated with this device or * null */ public Usb_Device_Descriptor getDeviceDescriptor() { if (dev == null) { return null; } return dev.getDescriptor(); } /** * Returns the configuration descriptors associated with this device.
* The descriptors are updated by calling {@link #updateDescriptors()} or * {@link #open(int, int, int)}. * * @return the configuration descriptors associated with this device or * null */ public Usb_Config_Descriptor[] getConfigDescriptors() { if (dev == null) { return null; } return dev.getConfig(); } /** * Opens the device and claims the specified configuration, interface and * altinterface.
* First the bus is enumerated. If the device is found its descriptors are * read and the maxPacketSize value is updated. If no * endpoints are found in the descriptors an exception is thrown. * * @param configuration * the configuration, see * {@link Usb_Config_Descriptor#getBConfigurationValue()} * @param interface_ * the interface, see * {@link Usb_Interface_Descriptor#getBInterfaceNumber()} * @param altinterface * the alternate interface, see * {@link Usb_Interface_Descriptor#getBAlternateSetting()}. If * no alternate interface must be set -1 can be used. * @throws USBException */ public void open(int configuration, int interface_, int altinterface) throws USBException { this.dev_configuration = configuration; this.dev_interface = interface_; this.dev_altinterface = altinterface; if (usbDevHandle != 0) { throw new USBException("device opened, close or reset first"); } dev = initDevice(idVendor, idProduct, filename); if (dev != null) { long res = LibusbJava.usb_open(dev); if (res == 0) { throw new USBException("LibusbJava.usb_open: " + LibusbJava.usb_strerror()); } usbDevHandle = res; } if (dev == null || usbDevHandle == 0) { throw new USBException("USB device with idVendor 0x" + Integer.toHexString(idVendor & 0xFFFF) + " and idProduct 0x" + Integer.toHexString(idProduct & 0xFFFF) + " not found"); } claim_interface(usbDevHandle, configuration, interface_, altinterface); if (resetOnFirstOpen & !resetDone) { logger.info("reset on first open"); resetDone = true; reset(); try { Thread.sleep(resetTimeout); } catch (InterruptedException e) { // } open(configuration, interface_, altinterface); } } /** * Release the claimed interface and close the opened device.
* * @throws USBException */ public void close() throws USBException { if (usbDevHandle == 0) { throw new USBException("invalid device handle"); } release_interface(usbDevHandle, dev_interface); if (LibusbJava.usb_close(usbDevHandle) < 0) { usbDevHandle = 0; throw new USBException("LibusbJava.usb_close: " + LibusbJava.usb_strerror()); } usbDevHandle = 0; maxPacketSize = -1; logger.info("device closed"); } /** * 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 * called. * * @throws USBException */ public void reset() throws USBException { if (usbDevHandle == 0) { throw new USBException("invalid device handle"); } release_interface(usbDevHandle, dev_interface); if (LibusbJava.usb_reset(usbDevHandle) < 0) { usbDevHandle = 0; throw new USBException("LibusbJava.usb_reset: " + LibusbJava.usb_strerror()); } usbDevHandle = 0; logger.info("device reset"); } /** * Write data to the device using a bulk transfer.
* * @param out_ep_address * endpoint address to write to * @param data * data to write to this endpoint * @param size * size of the data * @param timeout * amount of time in ms the device will try to send the data * until a timeout exception is thrown * @param reopenOnTimeout * if set to true, the device will try to open the connection and * send the data again before a timeout exception is thrown * @return the actual number of bytes written * @throws USBException */ public int writeBulk(int out_ep_address, byte[] data, int size, int timeout, boolean reopenOnTimeout) throws USBException { if (usbDevHandle == 0) { throw new USBException("invalid device handle"); } if (data == null) { throw new USBException("data must not be null"); } if (size <= 0 || size > data.length) { throw new ArrayIndexOutOfBoundsException("invalid size: " + size); } int lenWritten = LibusbJava.usb_bulk_write(usbDevHandle, out_ep_address, data, size, timeout); if (lenWritten < 0) { if (lenWritten == LibusbJava.ERROR_TIMEDOUT) { // try to reopen the device and send the data again if (reopenOnTimeout) { logger.info("try to reopen"); reset(); open(dev_configuration, dev_interface, dev_altinterface); return writeBulk(out_ep_address, data, size, timeout, false); } throw new USBTimeoutException("LibusbJava.usb_bulk_write: " + LibusbJava.usb_strerror()); } throw new USBException("LibusbJava.usb_bulk_write: " + LibusbJava.usb_strerror()); } logger.info("length written: " + lenWritten); if (logger.isLoggable(Level.FINEST)) { StringBuffer sb = new StringBuffer("bulkwrite, ep 0x" + Integer.toHexString(out_ep_address) + ": " + lenWritten + " Bytes sent: "); for (int i = 0; i < lenWritten; i++) { sb.append("0x" + String.format("%1$02X", data[i]) + " "); } logger.info(sb.toString()); } return lenWritten; } /** * Read data from the device using a bulk transfer.
* * @param in_ep_address * endpoint address to read from * @param data * data buffer for the data to be read * @param size * the maximum requested data size * @param timeout * amount of time in ms the device will try to receive data until * 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 * @throws USBException */ public int readBulk(int in_ep_address, byte[] data, int size, int timeout, boolean reopenOnTimeout) throws USBException { if (usbDevHandle == 0) { throw new USBException("invalid device handle"); } if (data == null) { throw new USBException("data must not be null"); } if (size <= 0 || size > data.length) { throw new ArrayIndexOutOfBoundsException("invalid size: " + size); } int lenRead = LibusbJava.usb_bulk_read(usbDevHandle, in_ep_address, data, size, timeout); if (lenRead < 0) { if (lenRead == LibusbJava.ERROR_TIMEDOUT) { // try to reopen the device and send the data again if (reopenOnTimeout) { logger.info("try to reopen"); reset(); open(dev_configuration, dev_interface, dev_altinterface); return readBulk(in_ep_address, data, size, timeout, false); } throw new USBTimeoutException("LibusbJava.usb_bulk_read: " + LibusbJava.usb_strerror()); } throw new USBException("LibusbJava.usb_bulk_read: " + LibusbJava.usb_strerror()); } logger.info("length read: " + lenRead); if (logger.isLoggable(Level.FINEST)) { StringBuffer sb = new StringBuffer("bulkread, ep 0x" + Integer.toHexString(in_ep_address) + ": " + lenRead + " Bytes received: "); for (int i = 0; i < lenRead; i++) { sb.append("0x" + String.format("%1$02X", data[i]) + " "); } logger.info(sb.toString()); } return lenRead; } /** * Write data to the device using a interrupt transfer.
* * @param out_ep_address * endpoint address to write to * @param data * data to write to this endpoint * @param size * size of the data * @param timeout * amount of time in ms the device will try to send the data * until a timeout exception is thrown * @param reopenOnTimeout * if set to true, the device will try to open the connection and * send the data again before a timeout exception is thrown * @return the actual number of bytes written * @throws USBException */ public int writeInterrupt(int out_ep_address, byte[] data, int size, int timeout, boolean reopenOnTimeout) throws USBException { if (usbDevHandle == 0) { throw new USBException("invalid device handle"); } if (data == null) { throw new USBException("data must not be null"); } if (size <= 0 || size > data.length) { throw new ArrayIndexOutOfBoundsException("invalid size: " + size); } int lenWritten = LibusbJava.usb_interrupt_write(usbDevHandle, out_ep_address, data, size, timeout); if (lenWritten < 0) { if (lenWritten == LibusbJava.ERROR_TIMEDOUT) { // try to reopen the device and send the data again if (reopenOnTimeout) { logger.info("try to reopen"); reset(); open(dev_configuration, dev_interface, dev_altinterface); return writeInterrupt(out_ep_address, data, size, timeout, false); } throw new USBTimeoutException( "LibusbJava.usb_interrupt_write: " + LibusbJava.usb_strerror()); } throw new USBException("LibusbJava.usb_interrupt_write: " + LibusbJava.usb_strerror()); } logger.info("length written: " + lenWritten); if (logger.isLoggable(Level.FINEST)) { StringBuffer sb = new StringBuffer("interruptwrite, ep 0x" + Integer.toHexString(out_ep_address) + ": " + lenWritten + " Bytes sent: "); for (int i = 0; i < lenWritten; i++) { sb.append("0x" + String.format("%1$02X", data[i]) + " "); } logger.info(sb.toString()); } return lenWritten; } /** * Read data from the device using a interrupt transfer.
* * @param in_ep_address * endpoint address to read from * @param data * data buffer for the data to be read * @param size * the maximum requested data size * @param timeout * amount of time in ms the device will try to receive data until * 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 * @throws USBException */ public int readInterrupt(int in_ep_address, byte[] data, int size, int timeout, boolean reopenOnTimeout) throws USBException { if (usbDevHandle == 0) { throw new USBException("invalid device handle"); } if (data == null) { throw new USBException("data must not be null"); } if (size <= 0 || size > data.length) { throw new ArrayIndexOutOfBoundsException("invalid size: " + size); } int lenRead = LibusbJava.usb_interrupt_read(usbDevHandle, in_ep_address, data, size, timeout); if (lenRead < 0) { if (lenRead == LibusbJava.ERROR_TIMEDOUT) { // try to reopen the device and send the data again if (reopenOnTimeout) { logger.info("try to reopen"); reset(); open(dev_configuration, dev_interface, dev_altinterface); return readInterrupt(in_ep_address, data, size, timeout, false); } throw new USBTimeoutException("LibusbJava.usb_interrupt_read: " + LibusbJava.usb_strerror()); } throw new USBException("LibusbJava.usb_interrupt_read: " + LibusbJava.usb_strerror()); } logger.info("length read: " + lenRead); if (logger.isLoggable(Level.FINEST)) { StringBuffer sb = new StringBuffer("interrupt, ep 0x" + Integer.toHexString(in_ep_address) + ": " + lenRead + " Bytes received: "); for (int i = 0; i < lenRead; i++) { sb.append("0x" + String.format("%1$02X", data[i]) + " "); } logger.info(sb.toString()); } return lenRead; } /** * Performs a control request to the default control pipe on a device.
* The parameters mirror the types of the same name in the USB * specification. * * @param requestType * USB device request type (USB specification 9.3, * bmRequestType). Use constants from {@link ch.ntb.usb.USB} * (REQ_TYPE_xxx). * @param request * specific request (USB specification 9.4, bRequest). Use * constants from {@link ch.ntb.usb.USB} (REQ_xxx). * @param value * field that varies according to request (USB specification 9.4, * wValue) * @param index * field that varies according to request (USB specification 9.4, * wIndex) * @param data * the send/receive buffer * @param size * the buffer size. 0 is a valid value, but there must still be a * dummy data buffer provided. * @param timeout * amount of time in ms the device will try to send/receive data * until a timeout exception is thrown * @param reopenOnTimeout * if set to true, the device will try to open the connection and * send/receive the data again before a timeout exception is * thrown * @return the number of bytes written/read * @throws USBException */ public int controlMsg(int requestType, int request, int value, int index, byte[] data, int size, int timeout, boolean reopenOnTimeout) throws USBException { if (usbDevHandle == 0) { throw new USBException("invalid device handle"); } if (data == null) { throw new USBException("data must not be null"); } if (size < 0 || size > data.length) { throw new ArrayIndexOutOfBoundsException("invalid size: " + size); } int len = LibusbJava.usb_control_msg(usbDevHandle, requestType, request, value, index, data, size, timeout); if (len < 0) { if (len == LibusbJava.ERROR_TIMEDOUT) { // try to reopen the device and send the data again if (reopenOnTimeout) { logger.info("try to reopen"); reset(); open(dev_configuration, dev_interface, dev_altinterface); return controlMsg(requestType, request, value, index, data, size, timeout, false); } throw new USBTimeoutException("LibusbJava.controlMsg: " + LibusbJava.usb_strerror()); } throw new USBException("LibusbJava.controlMsg: " + LibusbJava.usb_strerror()); } logger.info("length read/written: " + len); if (logger.isLoggable(Level.FINEST)) { StringBuffer sb = new StringBuffer("controlMsg: " + len + " Bytes received(written: "); for (int i = 0; i < len; i++) { sb.append("0x" + String.format("%1$02X", data[i]) + " "); } logger.info(sb.toString()); } return len; } /** * Claim an interface to send and receive USB data.
* * @param usb_dev_handle * the handle of the device (MUST BE VALID) * @param configuration * the configuration to use * @param interface_ * the interface to claim * @param altinterface * the alternate interface to use. If no alternate interface must * be set -1 can be used. * @throws USBException * throws an USBException if the action fails */ private void claim_interface(long usb_dev_handle, int configuration, int interface_, int altinterface) throws USBException { if (LibusbJava.usb_set_configuration(usb_dev_handle, configuration) < 0) { usbDevHandle = 0; throw new USBException("LibusbJava.usb_set_configuration: " + LibusbJava.usb_strerror()); } if (LibusbJava.usb_claim_interface(usb_dev_handle, interface_) < 0) { usbDevHandle = 0; throw new USBException("LibusbJava.usb_claim_interface: " + LibusbJava.usb_strerror()); } if (altinterface >= 0) { if (LibusbJava.usb_set_altinterface(usb_dev_handle, altinterface) < 0) { try { release_interface(usb_dev_handle, interface_); } catch (USBException e) { // ignore } usbDevHandle = 0; throw new USBException("LibusbJava.usb_set_altinterface: " + LibusbJava.usb_strerror()); } } logger.info("interface claimed"); } /** * Release a previously claimed interface.
* * @param dev_handle * the handle of the device (MUST BE VALID) * @param interface_ * the interface to claim * @throws USBException * throws an USBException if the action fails */ private void release_interface(long dev_handle, int interface_) throws USBException { if (LibusbJava.usb_release_interface(dev_handle, interface_) < 0) { usbDevHandle = 0; throw new USBException("LibusbJava.usb_release_interface: " + LibusbJava.usb_strerror()); } logger.info("interface released"); } /** * Returns the product ID of the device.
* * @return the product ID of the device. */ public int getIdProduct() { return idProduct; } /** * Returns the vendor ID of the device.
* * @return the vendor ID of the device. */ public int getIdVendor() { return idVendor; } /** * Returns the alternative interface.
* This value is only valid after opening the device. * * @return the alternative interface. This value is only valid after opening * the device. */ public int getAltinterface() { return dev_altinterface; } /** * Returns the current configuration used.
* This value is only valid after opening the device. * * @return the current configuration used. This value is only valid after * opening the device. */ public int getConfiguration() { return dev_configuration; } /** * Returns the current interface.
* This value is only valid after opening the device. * * @return the current interface. This value is only valid after opening the * device. */ public int getInterface() { return dev_interface; } /** * Returns the maximum packet size in bytes which is allowed to be * transmitted at once.
* The value is determined by reading the endpoint descriptor(s) when * opening the device. It is invalid before the device is opened! Note that * if some endpoints use different packet sizes the maximum packet size is * return. This value may be used to determine if a device is opened in * fullspeed or highspeed mode. * * @return the maximum packet size */ public int getMaxPacketSize() { return maxPacketSize; } /** * Check if the device is open.
* 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.
* This will only happen once. When the application is started, the device * state is unknown. If the device is not reset, read or write may result in * a {@link USBTimeoutException}.
*
* This feature is disabled by default. * * @param enable * true if the device should be reset when first opened * @param timeout * the timeout between the reset and the reopening */ public void setResetOnFirstOpen(boolean enable, int timeout) { resetOnFirstOpen = enable; resetTimeout = timeout; } /** * Returns the optional filename which is set when there are multiple * devices with the same vendor and product id. See * {@link USB#getDevice(short, short, String)}. Use * {@link Usb_Device#getFilename()} to read the filename of a device. * * @return the filename if set or null */ protected String getFilename() { return filename; } /** * Returns the Usb_Device instance associated with this device. This value * is only valid after opening the device. * * @return the Usb_Device instance associated with this device. */ public Usb_Device getDevice() { return dev; } } libusb-java-0.8+ztex20090101/ch/ntb/usb/LibLoader.java0000644000175000017500000000514711306756035021726 0ustar moellermoeller/* * library loader with the ability to load libraries as system resource (e.g. form the current .jar file) * Copyright (c) 2007-2009, ZTEX e.K. * http://www.ztex.de * * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; import java.io.*; /** * This class allows to load libraries in the normal way or as a system resource (e.g. form the current .jar file). * See below for a further description.
* * @author Stefan Ziegenbalg * */ public class LibLoader { /** * Loads a library. This is done in three steps.
* 1. The library is tried to be load from the path list specified by the java.library.path property.
* 2. The library is tried to be load from the current directory.
* 3. The library is searched as a system resource (e.g. in the current .jar file), * copied to to temporary directory and loaded from there. Afterwards the temporary library is deleted. * The copying is necessary because libraries can't be loaded directly from .jar files.
* * @param libName Library name (e.g. usbJava) * * @throws UnsatisfiedLinkError * */ public static void load( String libName ) { // Step 1: Normal way try { System.loadLibrary( libName ); } catch ( Throwable e1 ) { // Step 2: From the current directory String basename = System.mapLibraryName( libName ); try { System.load( System.getProperty("user.dir") + System.getProperty("file.separator") + basename ); } catch ( Throwable e2 ) { // Step 2: As system ressource String libFileName = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + basename; try { InputStream inputStream = ClassLoader.getSystemResourceAsStream( basename ); if ( inputStream == null ) { throw new Exception(); } File libFile = new File( libFileName ); FileOutputStream outputStream = new FileOutputStream( libFile ); byte[] buf = new byte[65536]; int bytesRead = -1; while ( (bytesRead = inputStream.read(buf)) > 0 ) { outputStream.write(buf, 0, bytesRead); } outputStream.close(); inputStream.close(); System.load( libFileName ); try { libFile.delete(); } catch (Exception e3) { // System.err.println( "Warning: Cannot delete temporary library file `" + libFileName + "'" ); } } catch (Exception e3) { throw new UnsatisfiedLinkError ("Library `"+basename+"' cannot be loaded as system resource, from current directory or from java.library.path (java.library.path=" + System.getProperty("java.library.path")+")" ); } } } } } libusb-java-0.8+ztex20090101/ch/ntb/usb/Usb_Bus.java0000644000175000017500000000321711166441464021430 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; /** * Represents an USB bus.
* This is the root class for the representation of the libusb USB structure. * Zero or more devices may be connected to an USB bus. * */ public class Usb_Bus { private Usb_Bus next, prev; private String dirname; private Usb_Device devices; private long location; private Usb_Device root_dev; /** * Get the first device ojects of the devices linked list.
* * @return the first device ojects of the devices linked list or null */ public Usb_Device getDevices() { return devices; } /** * Returns the systems String representation of the bus.
* * @return the systems String representation of the bus */ public String getDirname() { return dirname; } /** * Returns the next bus object.
* * @return Returns the next bus object or null */ public Usb_Bus getNext() { return next; } /** * Returns the previous bus object.
* * @return Returns the previous bus object or null */ public Usb_Bus getPrev() { return prev; } /** * Get the root device of this bus.
* * @return the root device oject or null */ public Usb_Device getRootDev() { return root_dev; } /** * Returns the location in the USB bus linked list.
* * @return the location in the USB bus linked list */ public long getLocation() { return location; } @Override public String toString() { return "Usb_Bus " + dirname; } }libusb-java-0.8+ztex20090101/ch/ntb/usb/Usb_Endpoint_Descriptor.java0000644000175000017500000000744111166441464024660 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; /** * Represents the descriptor of an USB endpoint.
* Endpoint descriptors are used to describe endpoints other than endpoint zero. * Endpoint zero is always assumed to be a control endpoint and is configured * before any descriptors are even requested. The host will use the information * returned from these descriptors to determine the bandwidth requirements of * the bus.
*
* The length of the configuration descriptor is * {@link ch.ntb.usb.Usb_Descriptor#USB_DT_ENDPOINT_SIZE} and the type is * {@link ch.ntb.usb.Usb_Descriptor#USB_DT_ENDPOINT}. * */ public class Usb_Endpoint_Descriptor extends Usb_Descriptor { /** * Maximum number of endpoints */ public static final int USB_MAXENDPOINTS = 32; /** * Endpoint address mask (in bEndpointAddress). */ public static final int USB_ENDPOINT_ADDRESS_MASK = 0x0f, USB_ENDPOINT_DIR_MASK = 0x80; /** * Endpoint type mask (in bmAttributes). */ public static final int USB_ENDPOINT_TYPE_MASK = 0x03; /** * Possible endpoint types (in bmAttributes). */ public static final int USB_ENDPOINT_TYPE_CONTROL = 0, USB_ENDPOINT_TYPE_ISOCHRONOUS = 1, USB_ENDPOINT_TYPE_BULK = 2, USB_ENDPOINT_TYPE_INTERRUPT = 3; private byte bEndpointAddress; private byte bmAttributes; private short wMaxPacketSize; private byte bInterval; private byte bRefresh; private byte bSynchAddress; private byte[] extra; /* Extra descriptors */ private int extralen; /** * Returns the endpoint address.
*
* Bits 3..0: Endpoint number
* Bits 6..4: Reserved. Set to zero
* Bit 7: Direction. 0 = Out, 1 = In (ignored for control endpoints)
* * @return the endpoint address */ public byte getBEndpointAddress() { return bEndpointAddress; } /** * Returns the intervall for polling endpoint data transfers.
* Value in frame counts. Ignored for Bulk & Control eEndpoints. Isochronous * endpoints must equal 1 and field may range from 1 to 255 for interrupt * endpoints. * * @return the intervall for polling endpoint data transfers */ public byte getBInterval() { return bInterval; } /** * Returns the attributes of this endpoint.
* * Bits 1..0: Transfer Type (see USB_ENDPOINT_TYPE_XXX).
* Bits 7..2: Reserved.
* *
	 * 	If isochronous endpoint:
	 * 		Bits 3..2: Synchronisation type
	 *  		00 = No synchronisation
	 * 			01 = Asynchronous
	 *          10 = Adaptive
	 *          11 = Synchronous
	 *     	Bits 5..4: Usage Type
	 *      	00 = Data endpoint
	 *      	01 = Feedback endpoint
	 *      	10 = Explicit feedback data endpoint
	 *      	11 = Reserved
	 * 
* * @return the attributes of this endpoint */ public byte getBmAttributes() { return bmAttributes; } public byte getBRefresh() { return bRefresh; } public byte getBSynchAddress() { return bSynchAddress; } /** * Returns the data of extra descriptor(s) if available.
* * @return null or a byte array with the extra descriptor data */ public byte[] getExtra() { return extra; } /** * Returns the number of bytes of the extra descriptor.
* * @return the number of bytes of the extra descriptor */ public int getExtralen() { return extralen; } /** * Returns the maximum packet size of this endpoint is capable of sending or * receiving.
* * @return the maximum packet size */ public short getWMaxPacketSize() { return wMaxPacketSize; } @Override public String toString() { return "Usb_Endpoint_Descriptor bEndpointAddress: 0x" + Integer.toHexString(bEndpointAddress & 0xFF); } } libusb-java-0.8+ztex20090101/ch/ntb/usb/USBException.java0000644000175000017500000000071511166441464022376 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; import java.io.IOException; public class USBException extends IOException { public USBException(String string) { super(string); } /** * */ private static final long serialVersionUID = 1690857437804284710L; } libusb-java-0.8+ztex20090101/ch/ntb/usb/package.html0000644000175000017500000000301511310173166021470 0ustar moellermoeller A Java wrapper for the libusb 0.1 and libusb-win32 USB library.

The Java libusb wrapper is known to work on Linux, Windows and Mac, but should work on every operating system which supports libusb and Java.

This software has originally been developed by members of the Computer Science Laboratory at the University of Applied Sciences of Technology NTB, Buchs, Switzerland.

The main project page is http://libusbjava.sourceforge.net.

This version is distributed together with the Java API of the ZTEX EZ-USB SDK and bases on the SVN version of Apr. 6, 2009. It is extended by the ability to load the (OS dependent) libraries form system ressource (e.g. in the current .jar file), see the LibLoader Class.

LibusbJava.java is the JNI class to the libusbJava.so and LibusbJava.dll, respectively. Every class starting with Usb_ represents a descriptor of the bus structure which is retrieved by calling LibusbJava.usb_get_busses().

Related Resources

For more information about this project visit http://libusbjava.sourceforge.net. libusb-java-0.8+ztex20090101/ch/ntb/usb/Usb_Interface_Descriptor.java0000644000175000017500000000634611522351517024776 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; /** * Represents the descriptor of a USB interface.
* The interface descriptor could be seen as a header or grouping of the * endpoints into a functional group performing a single feature of the device.
*
* The length of the interface descriptor is * {@link ch.ntb.usb.Usb_Descriptor#USB_DT_INTERFACE_SIZE} and the type is * {@link ch.ntb.usb.Usb_Descriptor#USB_DT_INTERFACE}. * */ public class Usb_Interface_Descriptor extends Usb_Descriptor { /** * Maximum number of interfaces */ public static final int USB_MAXINTERFACES = 32; private byte bInterfaceNumber; private byte bAlternateSetting; private byte bNumEndpoints; private byte bInterfaceClass; private byte bInterfaceSubClass; private byte bInterfaceProtocol; private byte iInterface; private Usb_Endpoint_Descriptor[] endpoint; private byte[] extra; /* Extra descriptors */ private int extralen; @Override public String toString() { return "Usb_Interface_Descriptor bNumEndpoints: 0x" + Integer.toHexString(bNumEndpoints); } /** * Returns the value used to select the alternate setting ({@link LibusbJava#usb_set_altinterface(long, int)}).
* * @return the alternate setting */ public byte getBAlternateSetting() { return bAlternateSetting; } /** * Returns the class code (Assigned by www.usb.org).
* * @return the class code */ public byte getBInterfaceClass() { return bInterfaceClass; } /** * Returns the number (identifier) of this interface.
* * @return the number (identifier) of this interface */ public byte getBInterfaceNumber() { return bInterfaceNumber; } /** * Returns the protocol code (Assigned by www.usb.org).
* * @return the protocol code */ public byte getBInterfaceProtocol() { return bInterfaceProtocol; } /** * Returns the subclass code (Assigned by www.usb.org).
* * @return the subclass code */ public byte getBInterfaceSubClass() { return bInterfaceSubClass; } /** * Returns the number of endpoints used for this interface.
* * @return the number of endpoints used for this interface */ public byte getBNumEndpoints() { return bNumEndpoints; } /** * Returns an array of endpoint descriptors.
* * @return an array of endpoint descriptors */ public Usb_Endpoint_Descriptor[] getEndpoint() { return endpoint; } /** * Returns the data of extra descriptor(s) if available.
* * @return null or a byte array with the extra descriptor data */ public byte[] getExtra() { return extra; } /** * Returns the number of bytes of the extra descriptor.
* * @return the number of bytes of the extra descriptor */ public int getExtralen() { return extralen; } /** * Returns the index of the String descriptor describing this interface.
* * @return the index of the String descriptor */ public byte getIInterface() { return iInterface; } } libusb-java-0.8+ztex20090101/ch/ntb/usb/Usb_Descriptor.java0000644000175000017500000000242111473027014023001 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; /** * Common USB descriptor values.
* */ public class Usb_Descriptor { /** * Descriptor types. */ public static final int USB_DT_DEVICE = 0x01, USB_DT_CONFIG = 0x02, USB_DT_STRING = 0x03, USB_DT_INTERFACE = 0x04, USB_DT_ENDPOINT = 0x05; /** * Descriptor types. */ public static final int USB_DT_HID = 0x21, USB_DT_REPORT = 0x22, USB_DT_PHYSICAL = 0x23, USB_DT_HUB = 0x29; /** * Descriptor sizes per descriptor type. */ public static final int USB_DT_DEVICE_SIZE = 18, USB_DT_CONFIG_SIZE = 9, USB_DT_INTERFACE_SIZE = 9, USB_DT_ENDPOINT_SIZE = 7, USB_DT_ENDPOINT_AUDIO_SIZE = 9 /* Audio extension */, USB_DT_HUB_NONVAR_SIZE = 7; private byte bLength; private byte bDescriptorType; /** * Get the type of this descriptor.
* * @return the type of this descriptor */ public byte getBDescriptorType() { return bDescriptorType; } /** * Get the size of this descriptor in bytes.
* * @return the size of this descriptor in bytes */ public byte getBLength() { return bLength; } } libusb-java-0.8+ztex20090101/ch/ntb/usb/USBTimeoutException.java0000644000175000017500000000070011166441464023737 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; public class USBTimeoutException extends USBException { public USBTimeoutException(String string) { super(string); } /** * */ private static final long serialVersionUID = -1065328371159778249L; } libusb-java-0.8+ztex20090101/ch/ntb/usb/Utils.java0000644000175000017500000000326111166441464021165 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; import java.io.PrintStream; public class Utils { public static void logBus(Usb_Bus bus) { logBus(bus, System.out); } public static void logBus(Usb_Bus bus, PrintStream out) { Usb_Bus usb_Bus = bus; while (usb_Bus != null) { out.println(usb_Bus.toString()); Usb_Device dev = usb_Bus.getDevices(); while (dev != null) { out.println("\t" + dev.toString()); // Usb_Device_Descriptor Usb_Device_Descriptor defDesc = dev.getDescriptor(); out.println("\t\t" + defDesc.toString()); // Usb_Config_Descriptor Usb_Config_Descriptor[] confDesc = dev.getConfig(); for (int i = 0; i < confDesc.length; i++) { out.println("\t\t" + confDesc[i].toString()); Usb_Interface[] int_ = confDesc[i].getInterface(); if (int_ != null) { for (int j = 0; j < int_.length; j++) { out.println("\t\t\t" + int_[j].toString()); Usb_Interface_Descriptor[] intDesc = int_[j] .getAltsetting(); if (intDesc != null) { for (int k = 0; k < intDesc.length; k++) { out.println("\t\t\t\t" + intDesc[k].toString()); Usb_Endpoint_Descriptor[] epDesc = intDesc[k] .getEndpoint(); if (epDesc != null) { for (int e = 0; e < epDesc.length; e++) { out.println("\t\t\t\t\t" + epDesc[e].toString()); } } } } } } } dev = dev.getNext(); } usb_Bus = usb_Bus.getNext(); } } } libusb-java-0.8+ztex20090101/ch/ntb/usb/USB.java0000644000175000017500000002036411166441464020521 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; import java.util.Iterator; import java.util.LinkedList; import java.util.logging.Logger; import ch.ntb.usb.logger.LogUtil; /** * This class manages all USB devices and defines some USB specific constants.
* */ public class USB { // Standard requests (USB spec 9.4) /** * This request returns status for the specified recipient (USB spec 9.4.5). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_GET_STATUS = 0x00; /** * This request is used to clear or disable a specific feature (USB spec * 9.4.1). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_CLEAR_FEATURE = 0x01; // 0x02 is reserved /** * This request is used to set or enable a specific feature (USB spec * 9.4.9). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_SET_FEATURE = 0x03; // 0x04 is reserved /** * This request sets the device address for all future device accesses (USB * spec 9.4.6). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_SET_ADDRESS = 0x05; /** * This request returns the specified descriptor if the descriptor exists * (USB spec 9.4.3). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_GET_DESCRIPTOR = 0x06; /** * This request is optional and may be used to update existing descriptors * or new descriptors may be added (USB spec 9.4.8). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_SET_DESCRIPTOR = 0x07; /** * This request returns the current device configuration value (USB spec * 9.4.2). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_GET_CONFIGURATION = 0x08; /** * This request sets the device configuration (USB spec 9.4.7). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_SET_CONFIGURATION = 0x09; /** * This request returns the selected alternate setting for the specified * interface (USB spec 9.4.4). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_GET_INTERFACE = 0x0A; /** * This request allows the host to select an alternate setting for the * specified interface (USB spec 9.4.10). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_SET_INTERFACE = 0x0B; /** * This request is used to set and then report an endpoint’s synchronization * frame (USB spec 9.4.11). * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_SYNCH_FRAME = 0x0C; // data transfer direction (USB spec 9.3) /** * Identifies the direction of data transfer in the second phase of the * control transfer.
* The state of the Direction bit is ignored if the wLength field is zero, * signifying there is no Data stage.
* Specifies bit 7 of bmRequestType. * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_TYPE_DIR_HOST_TO_DEVICE = (0x00 << 7), REQ_TYPE_DIR_DEVICE_TO_HOST = (0x01 << 7); // request types (USB spec 9.3) /** * Specifies the type of the request.
* Specifies bits 6..5 of bmRequestType. * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_TYPE_TYPE_STANDARD = (0x00 << 5), REQ_TYPE_TYPE_CLASS = (0x01 << 5), REQ_TYPE_TYPE_VENDOR = (0x02 << 5), REQ_TYPE_TYPE_RESERVED = (0x03 << 5); // request recipient (USB spec 9.3) /** * Specifies the intended recipient of the request.
* Requests may be directed to the device, an interface on the device, or a * specific endpoint on a device. When an interface or endpoint is * specified, the wIndex field identifies the interface or endpoint.
* Specifies bits 4..0 of bmRequestType. * * @see ch.ntb.usb.Device#controlMsg(int, int, int, int, byte[], int, int, * boolean) */ public static final int REQ_TYPE_RECIP_DEVICE = 0x00, REQ_TYPE_RECIP_INTERFACE = 0x01, REQ_TYPE_RECIP_ENDPOINT = 0x02, REQ_TYPE_RECIP_OTHER = 0x03; /** * The maximum packet size of a bulk transfer when operating in highspeed * (480 MB/s) mode. */ public static int HIGHSPEED_MAX_BULK_PACKET_SIZE = 512; /** * The maximum packet size of a bulk transfer when operating in fullspeed * (12 MB/s) mode. */ public static int FULLSPEED_MAX_BULK_PACKET_SIZE = 64; private static final Logger logger = LogUtil.getLogger("ch.ntb.usb"); private static LinkedList devices = new LinkedList(); private static boolean initUSBDone = false; /** * Create a new device an register it in a device queue. If the device is * already registered, a reference to it will be returned.
* * @param idVendor * the vendor id of the USB device * @param idProduct * the product id of the USB device * @param filename * an optional filename which can be used to distinguish multiple * devices with the same vendor and product id. * @return a newly created device or an already registered device */ public static Device getDevice(short idVendor, short idProduct, String filename) { // check if this device is already registered Device dev = getRegisteredDevice(idVendor, idProduct, filename); if (dev != null) { logger.info("return already registered device"); return dev; } dev = new Device(idVendor, idProduct, filename); logger.info("create new device"); devices.add(dev); return dev; } /** * See {@link #getDevice(short, short, String)}. The parameter * filename is set to null. * * @param idVendor * @param idProduct * @return a newly created device or an already registered device */ public static Device getDevice(short idVendor, short idProduct) { return getDevice(idVendor, idProduct, null); } /** * Get an already registered device or null if the device does not exist.
* * @param idVendor * the vendor id of the USB device * @param idProduct * the product id of the USB device * @param filename * an optional filename which can be used to distinguish multiple * devices with the same vendor and product id. * @return the device or null */ private static Device getRegisteredDevice(short idVendor, short idProduct, String filename) { for (Iterator iter = devices.iterator(); iter.hasNext();) { Device dev = iter.next(); if (filename != null && dev.getFilename() != null && filename.compareTo(dev.getFilename()) == 0 && dev.getIdVendor() == idVendor && dev.getIdProduct() == idProduct) { return dev; } else if (dev.getIdVendor() == idVendor && dev.getIdProduct() == idProduct) { return dev; } } return null; } /** * Returns the root {@link Usb_Bus} element. * * @return the root {@link Usb_Bus} element * @throws USBException */ public static Usb_Bus getBus() throws USBException { if (!initUSBDone) { init(); } LibusbJava.usb_find_busses(); LibusbJava.usb_find_devices(); Usb_Bus bus = LibusbJava.usb_get_busses(); if (bus == null) { throw new USBException("LibusbJava.usb_get_busses(): " + LibusbJava.usb_strerror()); } return bus; } /** * Explicitly calls {@link LibusbJava#usb_init()}. Note that you don't need * to call this procedure as it is called implicitly when creating a new * device with {@link USB#getDevice(short, short, String)}. */ public static void init() { LibusbJava.usb_init(); initUSBDone = true; } } libusb-java-0.8+ztex20090101/ch/ntb/usb/Usb_Device_Descriptor.java0000644000175000017500000001120211166441464024265 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; /** * Represents the descriptor of a USB device.
* A USB device can only have one device descriptor. It specifies some basic, * yet important information about the device.
*
* The length of the device descriptor is * {@link ch.ntb.usb.Usb_Descriptor#USB_DT_DEVICE_SIZE} and the type is * {@link ch.ntb.usb.Usb_Descriptor#USB_DT_DEVICE}. * */ public class Usb_Device_Descriptor extends Usb_Descriptor { /** * Device and/or interface class codes. */ public static final int USB_CLASS_PER_INTERFACE = 0, USB_CLASS_AUDIO = 1, USB_CLASS_COMM = 2, USB_CLASS_HID = 3, USB_CLASS_PRINTER = 7, USB_CLASS_MASS_STORAGE = 8, USB_CLASS_HUB = 9, USB_CLASS_DATA = 10, USB_CLASS_VENDOR_SPEC = 0xff; private short bcdUSB; private byte bDeviceClass; private byte bDeviceSubClass; private byte bDeviceProtocol; private byte bMaxPacketSize0; private short idVendor; private short idProduct; private short bcdDevice; private byte iManufacturer; private byte iProduct; private byte iSerialNumber; private byte bNumConfigurations; /** * Returns the device release number.
* Assigned by the manufacturer of the device. * * @return the device release number */ public short getBcdDevice() { return bcdDevice; } /** * Returns the USB specification number to which the device complies to.
* This field reports the highest version of USB the device supports. The * value is in binary coded decimal with a format of 0xJJMN where JJ is the * major version number, M is the minor version number and N is the sub * minor version number.
* Examples: USB 2.0 is reported as 0x0200, USB 1.1 as 0x0110 and USB 1.0 as * 0x100 * * @return the USB specification number to which the device complies to */ public short getBcdUSB() { return bcdUSB; } /** * Returns the class code (Assigned by www.usb.org)
* If equal to zero, each interface specifies it's own class code. If equal * to 0xFF, the class code is vendor specified. Otherwise the field is a * valid class code. * * @return the class code */ public byte getBDeviceClass() { return bDeviceClass; } /** * Returns the protocol code (Assigned by www.usb.org)
* * @return the protocol code */ public byte getBDeviceProtocol() { return bDeviceProtocol; } /** * Returns the subclass code (Assigned by www.usb.org)
* * @return the subclass code */ public byte getBDeviceSubClass() { return bDeviceSubClass; } /** * Returns the maximum packet size for endpoint zero.
* Valid sizes are 8, 16, 32, 64. * * @return the maximum packet size for endpoint zero */ public byte getBMaxPacketSize0() { return bMaxPacketSize0; } /** * Returns the number of possible configurations supported at its current * speed.
* * @return the number of possible configurations supported at its current * speed */ public byte getBNumConfigurations() { return bNumConfigurations; } /** * Returns the product ID (Assigned by www.usb.org)
* * @return the product ID */ public short getIdProduct() { return idProduct; } /** * Returns the Vendor ID (Assigned by www.usb.org)
* * @return the Vendor ID */ public short getIdVendor() { return idVendor; } /** * Returns the index of the manufacturer string descriptor.
* If this value is 0, no string descriptor is used. * * @return the index of the manufacturer string descriptor */ public byte getIManufacturer() { return iManufacturer; } /** * Returns the index of the product string descriptor.
* If this value is 0, no string descriptor is used. * * @return the index of the product string descriptor */ public byte getIProduct() { return iProduct; } /** * Returns the index of serial number string descriptor.
* If this value is 0, no string descriptor is used. * * @return the index of serial number string descriptor */ public byte getISerialNumber() { return iSerialNumber; } @Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append("Usb_Device_Descriptor idVendor: 0x" + Integer.toHexString(idVendor & 0xFFFF) + ", idProduct: 0x" + Integer.toHexString(idProduct & 0xFFFF)); return sb.toString(); } }libusb-java-0.8+ztex20090101/ch/ntb/usb/Usb_Interface.java0000644000175000017500000000207011166441464022573 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; /** * Represents an USB interface.
* An interface is a group of alternate settings of a configuration.
* */ public class Usb_Interface { /** * Maximal number of alternate settings */ public static final int USB_MAXALTSETTING = 128; /* Hard limit */ private Usb_Interface_Descriptor[] altsetting; private int num_altsetting; @Override public String toString() { return "Usb_Interface num_altsetting: 0x" + Integer.toHexString(num_altsetting); } /** * Retuns an array of interface descriptors.
* * @return an array of interface descriptors */ public Usb_Interface_Descriptor[] getAltsetting() { return altsetting; } /** * Returns the number of alternate settings.
* * @return the number of alternate settings */ public int getNumAltsetting() { return num_altsetting; } } libusb-java-0.8+ztex20090101/ch/ntb/usb/logger/0000755000175000017500000000000011522245137020472 5ustar moellermoellerlibusb-java-0.8+ztex20090101/ch/ntb/usb/logger/package.html0000644000175000017500000000045711166432211022754 0ustar moellermoeller Logging related classes.

Related Resources

For more information about this project visit http://libusbjava.sourceforge.net . libusb-java-0.8+ztex20090101/ch/ntb/usb/logger/LogUtil.java0000644000175000017500000000776311166432211022724 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb.logger; import java.io.InputStream; import java.util.Properties; import java.util.logging.ConsoleHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; public class LogUtil { // debug this class private static final boolean debugLogUtil = false; private static final String PLUGIN_ID = "ch.ntb.usb"; private static final String PROPERTIES_FILE = ".configure"; private static final String LOGGER_WARNING = "Warning in class " + LogUtil.class.getName() + ": could not load the logger properties file " + PROPERTIES_FILE; private static boolean debugEnabled; static { createLoggersFromProperties(); } private static void debugMsg(String method, String message) { if (debugLogUtil) { System.out.println(method + ": " + message); } } public static void setLevel(Logger logger, Level loglevel) { Handler[] h = logger.getHandlers(); for (int i = 0; i < h.length; i++) { System.out.println("setLevel " + loglevel.toString()); h[i].setLevel(loglevel); } logger.setLevel(loglevel); } public static Logger getLogger(String name) { debugMsg("getLogger", name); LogManager manager = LogManager.getLogManager(); // check if logger is already registered Logger logger = manager.getLogger(name); if (logger == null) { logger = Logger.getLogger(name); setLevel(logger, Level.OFF); manager.addLogger(logger); debugMsg("getLogger", "creating new logger"); } if (logger.getLevel() == null) { debugMsg("getLogger", "level == null -> setLevel to OFF "); setLevel(logger, Level.OFF); } debugMsg("getLogger", "logLevel " + logger.getLevel().getName()); return logger; } private static void initLevel(Logger logger, Level loglevel) { Handler[] h = logger.getHandlers(); for (int i = 0; i < h.length; i++) { logger.removeHandler(h[i]); } Handler console = new ConsoleHandler(); console.setLevel(loglevel); logger.addHandler(console); logger.setLevel(loglevel); logger.setUseParentHandlers(false); } private static void createLoggersFromProperties() { try { debugMsg(LogUtil.class.getName(), "createLoggersFromProperties"); InputStream is = LogUtil.class.getClassLoader() .getResourceAsStream(PROPERTIES_FILE); if (is == null) { System.err.println(LOGGER_WARNING); } else { Properties prop = new Properties(); prop.load(is); debugMsg("createLoggersFromProperties", "properties file loaded: " + PROPERTIES_FILE); debugMsg("createLoggersFromProperties", "file content:\n" + prop.toString()); // get global debug enable flag debugEnabled = Boolean.parseBoolean(prop.getProperty(PLUGIN_ID + "/debug")); debugMsg("createLoggersFromProperties", "debuging enabled: " + debugEnabled); // get and configure loggers boolean moreLoggers = true; int loggerCount = 0; while (moreLoggers) { String loggerProp = prop.getProperty(PLUGIN_ID + "/debug/logger" + loggerCount); loggerCount++; if (loggerProp != null) { // parse string and get logger name and log level int slashIndex = loggerProp.indexOf('/'); String loggerName = loggerProp.substring(0, slashIndex) .trim(); String logLevel = loggerProp.substring(slashIndex + 1, loggerProp.length()); // register logger Level level; if (debugEnabled) { level = Level.parse(logLevel); } else { level = Level.OFF; } Logger logger = getLogger(loggerName); initLevel(logger, level); debugMsg("createLoggersFromProperties", "create logger " + loggerName + " with level " + level.toString()); } else { moreLoggers = false; } } } } catch (Exception e) { e.printStackTrace(); } } } libusb-java-0.8+ztex20090101/ch/ntb/usb/Usb_Config_Descriptor.java0000644000175000017500000000636511522351476024310 0ustar moellermoeller/* * Java libusb wrapper * Copyright (c) 2005-2006 Andreas Schläpfer * * http://libusbjava.sourceforge.net * This library is covered by the LGPL, read LGPL.txt for details. */ package ch.ntb.usb; /** * Represents the descriptor of a USB configuration.
* A USB device can have several different configuration.
*
* The length of the configuration descriptor is * {@link ch.ntb.usb.Usb_Descriptor#USB_DT_CONFIG_SIZE} and the type is * {@link ch.ntb.usb.Usb_Descriptor#USB_DT_CONFIG}. * */ public class Usb_Config_Descriptor extends Usb_Descriptor { /** * Maximum number of configurations per device */ public static final int USB_MAXCONFIG = 8; private short wTotalLength; private byte bNumInterfaces; private byte bConfigurationValue; private byte iConfiguration; private byte bmAttributes; private byte MaxPower; private Usb_Interface[] interface_; private byte[] extra; /* Extra descriptors */ private int extralen; /** * Returns the value to use as an argument to select this configuration ({@link LibusbJava#usb_set_configuration(long, int)}). * * @return the value to use as an argument to select this configuration */ public byte getBConfigurationValue() { return bConfigurationValue; } /** * Returns the power parameters for this configuration.
*
* Bit 7: Reserved, set to 1 (USB 1.0 Bus Powered)
* Bit 6: Self Powered
* Bit 5: Remote Wakeup
* Bit 4..0: Reserved, set to 0 * * @return the power parameters for this configuration */ public byte getBmAttributes() { return bmAttributes; } /** * Returns the number of interfaces.
* * @return the number of interfaces */ public byte getBNumInterfaces() { return bNumInterfaces; } /** * Returns the data of extra descriptor(s) if available.
* * @return null or a byte array with the extra descriptor data */ public byte[] getExtra() { return extra; } /** * Returns the number of bytes of the extra descriptor.
* * @return the number of bytes of the extra descriptor */ public int getExtralen() { return extralen; } /** * Returns the index of the String descriptor describing this configuration.
* * @return the index of the String descriptor */ public byte getIConfiguration() { return iConfiguration; } /** * Returns the USB interface descriptors.
* * @return the USB interface descriptors */ public Usb_Interface[] getInterface() { return interface_; } /** * Returns the maximum power consumption in 2mA units.
* * @return the maximum power consumption in 2mA units */ public byte getMaxPower() { return MaxPower; } /** * Returns the total length in bytes of all descriptors.
* When the configuration descriptor is read, it returns the entire * configuration hierarchy which includes all related interface and endpoint * descriptors. The wTotalLength field reflects the number of * bytes in the hierarchy. * * @return the total length in bytes of all descriptors */ public short getWTotalLength() { return wTotalLength; } @Override public String toString() { return "Usb_Config_Descriptor bNumInterfaces: 0x" + Integer.toHexString(bNumInterfaces); } }