diff --git a/libusbdll/.cdtproject b/libusbdll/.cdtproject new file mode 100644 index 0000000..60a6b94 --- /dev/null +++ b/libusbdll/.cdtproject @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +make + +cleanDll +false +true + + +make + +testExe +false +true + + +make + +dll +false +true + + + + + diff --git a/libusbdll/.project b/libusbdll/.project new file mode 100644 index 0000000..61972a2 --- /dev/null +++ b/libusbdll/.project @@ -0,0 +1,90 @@ + + + LibusbDllJNIMinGW + + + + + + org.eclipse.cdt.make.core.makeBuilder + clean,full,incremental, + + + org.eclipse.cdt.make.core.build.arguments + + + + org.eclipse.cdt.core.errorOutputParser + org.eclipse.cdt.core.MakeErrorParser;org.eclipse.cdt.core.GCCErrorParser;org.eclipse.cdt.core.GASErrorParser;org.eclipse.cdt.core.GLDErrorParser;org.eclipse.cdt.core.VCErrorParser; + + + org.eclipse.cdt.make.core.enableAutoBuild + false + + + org.eclipse.cdt.make.core.environment + + + + org.eclipse.cdt.make.core.enableFullBuild + true + + + org.eclipse.cdt.make.core.build.target.inc + all + + + org.eclipse.cdt.make.core.enabledIncrementalBuild + true + + + org.eclipse.cdt.make.core.build.location + + + + org.eclipse.cdt.make.core.build.target.clean + clean + + + org.eclipse.cdt.make.core.build.command + make + + + org.eclipse.cdt.make.core.enableCleanBuild + true + + + org.eclipse.cdt.make.core.append_environment + true + + + org.eclipse.cdt.make.core.build.target.full + clean all + + + org.eclipse.cdt.make.core.useDefaultBuildCmd + true + + + org.eclipse.cdt.make.core.build.target.auto + all + + + org.eclipse.cdt.make.core.stopOnError + false + + + + + org.eclipse.cdt.make.core.ScannerConfigBuilder + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.make.core.makeNature + org.eclipse.cdt.make.core.ScannerConfigNature + org.eclipse.cdt.core.ccnature + + diff --git a/libusbdll/LibusbTest.cpp b/libusbdll/LibusbTest.cpp new file mode 100644 index 0000000..36ea2d6 --- /dev/null +++ b/libusbdll/LibusbTest.cpp @@ -0,0 +1,327 @@ +/* + * testlibusb.c + * + * Test suite program + */ + +#include +#include +#include "usb.h" + +#define snprintf printf + +#define ID_PRODUCT 0x1004 //0x8613 +#define ID_VENDOR 0x04B4 + +#define CONFIGURATION 1 +#define INTERFACE 0 +#define ALTINTERFACE 0 +#define TIMEOUT 2000 + +#define OUT_ENDPOINT 0x02 +#define IN_ENDPOINT 0x86 + +int verbose = 0; + +void print_endpoint(struct usb_endpoint_descriptor *endpoint) +{ + printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress); + printf(" bmAttributes: %02xh\n", endpoint->bmAttributes); + printf(" wMaxPacketSize: %d\n", endpoint->wMaxPacketSize); + printf(" bInterval: %d\n", endpoint->bInterval); + printf(" bRefresh: %d\n", endpoint->bRefresh); + printf(" bSynchAddress: %d\n", endpoint->bSynchAddress); +} + +void print_altsetting(struct usb_interface_descriptor *interface) +{ + int i; + + printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber); + printf(" bAlternateSetting: %d\n", interface->bAlternateSetting); + printf(" bNumEndpoints: %d\n", interface->bNumEndpoints); + printf(" bInterfaceClass: %d\n", interface->bInterfaceClass); + printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass); + printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol); + printf(" iInterface: %d\n", interface->iInterface); + + for (i = 0; i < interface->bNumEndpoints; i++) + print_endpoint(&interface->endpoint[i]); +} + +void print_interface(struct usb_interface *interface) +{ + int i; + + for (i = 0; i < interface->num_altsetting; i++) + print_altsetting(&interface->altsetting[i]); +} + +void print_configuration(struct usb_config_descriptor *config) +{ + int i; + + printf(" wTotalLength: %d\n", config->wTotalLength); + printf(" bNumInterfaces: %d\n", config->bNumInterfaces); + printf(" bConfigurationValue: %d\n", config->bConfigurationValue); + printf(" iConfiguration: %d\n", config->iConfiguration); + printf(" bmAttributes: %02xh\n", config->bmAttributes); + printf(" MaxPower: %d\n", config->MaxPower); + + for (i = 0; i < config->bNumInterfaces; i++) + print_interface(&config->interface[i]); +} + +int print_device(struct usb_device *dev, int level) +{ + usb_dev_handle *udev; + char description[256]; + char string[256]; + int ret, i; + + udev = usb_open(dev); + if (udev) { + if (dev->descriptor.iManufacturer) { + ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer, string, sizeof(string)); + if (ret > 0) + snprintf(description, sizeof(description), "%s - ", string); + else + snprintf(description, sizeof(description), "%04X - ", + dev->descriptor.idVendor); + } else + snprintf(description, sizeof(description), "%04X - ", + dev->descriptor.idVendor); + + if (dev->descriptor.iProduct) { + ret = usb_get_string_simple(udev, dev->descriptor.iProduct, string, sizeof(string)); + if (ret > 0) + snprintf(description + strlen(description), sizeof(description) - + strlen(description), "%s", string); + else + snprintf(description + strlen(description), sizeof(description) - + strlen(description), "%04X", dev->descriptor.idProduct); + } else + snprintf(description + strlen(description), sizeof(description) - + strlen(description), "%04X", dev->descriptor.idProduct); + + } else + snprintf(description, sizeof(description), "%04X - %04X", + dev->descriptor.idVendor, dev->descriptor.idProduct); + + printf("%.*sDev #%d: %s\n", level * 2, " ", dev->devnum, + description); + + if (udev && verbose) { + if (dev->descriptor.iSerialNumber) { + ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string)); + if (ret > 0) + printf("%.*s - Serial Number: %s\n", level * 2, + " ", string); + } + } + + if (udev) + usb_close(udev); + + if (verbose) { + if (!dev->config) { + printf(" Couldn't retrieve descriptors\n"); + return 0; + } + + for (i = 0; i < dev->descriptor.bNumConfigurations; i++) + print_configuration(&dev->config[i]); + } else { + for (i = 0; i < dev->num_children; i++) + print_device(dev->children[i], level + 1); + } + + return 0; +} + +int read(struct usb_dev_handle *handle) +{ + if (usb_claim_interface(handle, INTERFACE) < 0) { + printf("error on usb_claim_interface: %s\n", usb_strerror()); + return -1; + } + printf("usb_claim_interface successful\n"); + if (usb_set_altinterface(handle, ALTINTERFACE) < 0){ + printf("usb_set_altinterface failed: %s\n", usb_strerror()); + } + + int size = 512, res; + char *data = (char *) malloc(size*sizeof(char)); + res = usb_bulk_read(handle, IN_ENDPOINT, data, size, TIMEOUT); + if (res < 0){ + printf("usb_bulk_read failed: %s\n", usb_strerror()); + } + printf("usb_bulk_read: %d bytes read: ", res); + for (int i = 0; i < res; ++i) { + printf("%3x ", data[i]); + } + printf("\n"); + + usb_release_interface(handle, INTERFACE); +} + +int write(struct usb_dev_handle *handle) +{ + int size = 12; + char *data = (char *) malloc(size*sizeof(char)); + data[0] = 0x33; + data[1] = 0x5B; + data[2] = 0x02; + data[3] = 0x01; + data[4] = 0x00; + data[5] = 0x05; + data[6] = 0x01; + data[7] = 0x03; + data[8] = 0x07; + data[9] = 0x0F; + data[10] = 0x7F; + data[11] = 0x1F; + // data = {0x33, 0x5B, 0x02, 0x01, 0x00, 0x05, 0x01, 0x03, 0x07, 0x0F, 0x7F, 0x1F}; + + if (usb_claim_interface(handle, INTERFACE) < 0) { + printf("error on usb_claim_interface: %s\n", usb_strerror()); + return -1; + } + printf("usb_claim_interface successful\n"); + if (usb_set_altinterface(handle, ALTINTERFACE) < 0){ + printf("usb_set_altinterface failed: %s\n", usb_strerror()); + } + printf("usb_bulk_write: writing %d bytes: ", size); + for (int i = 0; i < size; ++i) { + printf("%3x ", data[i]); + } + printf("\n"); + + int res = usb_bulk_write(handle, OUT_ENDPOINT, data, size, TIMEOUT); + if (res < 0){ + printf("usb_bulk_write failed: %s\n", usb_strerror()); + return -1; + } + + printf("usb_bulk_write: %d bytes written\n", res); + + usb_release_interface(handle, INTERFACE); +} + +int main(int argc, char *argv[]) +{ + struct usb_bus *bus; + struct usb_device *dev; + struct usb_dev_handle *handle; + + bool run = true; + +// if (argc > 1 && !strcmp(argv[1], "-v")) +// verbose = 1; + + verbose = 1; + + usb_set_debug(255); + + usb_init(); + + usb_find_busses(); + usb_find_devices(); + +// for (bus = usb_get_busses(); bus; bus = bus->next) { +// if (bus->root_dev && !verbose) +// print_device(bus->root_dev, 0); +// else { +// struct usb_device *dev; +// +// for (dev = bus->devices; dev; dev = dev->next) +// print_device(dev, 0); +// } +// } + + int size = 512; + char *data = (char *) malloc(size*sizeof(char)); + + + for (bus = usb_get_busses(); bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { + if ((dev->descriptor.idProduct == ID_PRODUCT) && (dev->descriptor.idVendor == ID_VENDOR)){ + printf("Device found\n"); + handle = usb_open(dev); + if (!handle) { + printf("invalid handle: %s\n", usb_strerror()); + system("PAUSE"); + return -1; + } + if (usb_set_configuration(handle, CONFIGURATION) < 0) { + printf("error on usb_set_configuration: %s\n", usb_strerror()); + system("PAUSE"); + return -1; + } + + printf("w=write, r=read, x=exit, t=write+read:\n"); + + while (run) { + scanf("%s", data); + + switch (data[0]) { + case 'w': // write + write(handle); + break; + case 'r': // read + read(handle); + break; + case 'x': // exit + run = false; + break; + case 't': // write + read + if (write(handle)) { + read(handle); + } + break; + default: + break; + } + + // printf("type a string...\n"); + // scanf("%s", data); // Get a string + + // if (usb_claim_interface(handle, INTERFACE) < 0) { + // printf("error on usb_claim_interface: %s\n", usb_strerror()); + // system("PAUSE"); + // return -1; + // } + // printf("usb_claim_interface successful\n"); + // if (usb_set_altinterface(handle, ALTINTERFACE) < 0){ + // printf("usb_set_altinterface failed: %s\n", usb_strerror()); + // } + // + // if (usb_bulk_write(handle, OUT_ENDPOINT, data, strlen(data), 3000) < 0){ + // printf("usb_bulk_write failed: %s\n", usb_strerror()); + // system("PAUSE"); + // return -1; + // } + // + // strcpy(data, "12345678901234567890"); + // printf("%s\n", "read data"); + // if (usb_bulk_read(handle, IN_ENDPOINT, data, size, 3000) < 0){ + // printf("usb_bulk_read failed: %s\n", usb_strerror()); + // } + // printf("output %d, %s\n", size, data); + // // for (int i = 0; i < size; ++i) { + // // printf("%4x ", data[i]); + // // } + // + // usb_release_interface(handle, INTERFACE); + // } + // + // usb_close(handle); + } + printf("\ndone\n"); + } + } + } + system("PAUSE"); + + return 1; +} diff --git a/libusbdll/LibusbTest.exe b/libusbdll/LibusbTest.exe new file mode 100644 index 0000000..557c412 Binary files /dev/null and b/libusbdll/LibusbTest.exe differ diff --git a/libusbdll/LibusbWin.cpp b/libusbdll/LibusbWin.cpp new file mode 100644 index 0000000..f54e968 --- /dev/null +++ b/libusbdll/LibusbWin.cpp @@ -0,0 +1,691 @@ +#include +#include +#include +#include +#include "LibusbWin.h" +#include "usb.h" + +//#define DEBUGON + +// global bus (updated when usb_get_busses() is called) +struct usb_bus *busses; +// device class +jclass usb_devClazz; +// usb_device.devnum +jfieldID usb_devFID_devnum; + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_init + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_ch_ntb_usb_LibusbWin_usb_1init + (JNIEnv *env, jobject obj) + { + usb_init(); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_find_busses + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1find_1busses + (JNIEnv *env, jobject obj) + { + return usb_find_busses(); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_find_devices + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1find_1devices + (JNIEnv *env, jobject obj) + { + return usb_find_devices(); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_get_busses + * Signature: ()Lch/ntb/usb/Usb_Bus; + */ +JNIEXPORT jobject JNICALL Java_ch_ntb_usb_LibusbWin_usb_1get_1busses + (JNIEnv *env, jobject obj) + { + // classes + jclass usb_busClazz, /*usb_devClazz,*/ usb_devDescClazz, usb_confDescClazz, \ + usb_intClazz, usb_intDescClazz, usb_epDescClazz; + // method IDs + jmethodID usb_busMid, usb_devMid, usb_devDescMid, usb_confDescMid, \ + usb_intMid, usb_intDescMid, usb_epDescMid; + // 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_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; + + // find classes and field ids + // usb_bus + usb_busClazz = env->FindClass("ch/ntb/usb/Usb_Bus"); + if (usb_busClazz == NULL) { return NULL; /* exception thrown */ } + usb_busMid = env->GetMethodID(usb_busClazz, "","()V"); + if (usb_busMid == NULL) { return NULL; } + + usb_busFID_next = env->GetFieldID(usb_busClazz, "next", "Lch/ntb/usb/Usb_Bus;"); + usb_busFID_prev = env->GetFieldID(usb_busClazz, "prev", "Lch/ntb/usb/Usb_Bus;"); + usb_busFID_dirname = env->GetFieldID(usb_busClazz, "dirname", "Ljava/lang/String;"); + usb_busFID_devices = env->GetFieldID(usb_busClazz, "devices", "Lch/ntb/usb/Usb_Device;"); + usb_busFID_location = env->GetFieldID(usb_busClazz, "location", "J"); + usb_busFID_root_dev = env->GetFieldID(usb_busClazz, "root_dev", "Lch/ntb/usb/Usb_Device;"); + + + // usb_device + usb_devClazz = env->FindClass("ch/ntb/usb/Usb_Device"); + if (usb_devClazz == NULL) { return NULL; /* exception thrown */ } + usb_devMid = env->GetMethodID(usb_devClazz, "","()V"); + if (usb_devMid == NULL) { return NULL; } + + usb_devFID_next = env->GetFieldID(usb_devClazz, "next", "Lch/ntb/usb/Usb_Device;"); + usb_devFID_prev = env->GetFieldID(usb_devClazz, "prev", "Lch/ntb/usb/Usb_Device;"); + usb_devFID_filename = env->GetFieldID(usb_devClazz, "filename", "Ljava/lang/String;"); + usb_devFID_bus = env->GetFieldID(usb_devClazz, "bus", "Lch/ntb/usb/Usb_Bus;"); + usb_devFID_descriptor = env->GetFieldID(usb_devClazz, "descriptor", "Lch/ntb/usb/Usb_Device_Descriptor;"); + usb_devFID_config = env->GetFieldID(usb_devClazz, "config", "[Lch/ntb/usb/Usb_Config_Descriptor;"); + usb_devFID_devnum = env->GetFieldID(usb_devClazz, "devnum", "B"); + usb_devFID_num_children = env->GetFieldID(usb_devClazz, "num_children", "B"); + usb_devFID_children = env->GetFieldID(usb_devClazz, "children", "Lch/ntb/usb/Usb_Device;"); + + + // usb_device_descriptor + usb_devDescClazz = env->FindClass("ch/ntb/usb/Usb_Device_Descriptor"); + if (usb_devDescClazz == NULL) { return NULL; /* exception thrown */ } + usb_devDescMid = env->GetMethodID(usb_devDescClazz, "","()V"); + if (usb_devDescMid == NULL) { return NULL; } + + usb_devDescFID_bLength = env->GetFieldID(usb_devDescClazz, "bLength", "B"); + usb_devDescFID_bDescriptorType = env->GetFieldID(usb_devDescClazz, "bDescriptorType", "B"); + usb_devDescFID_bcdUSB = env->GetFieldID(usb_devDescClazz, "bcdUSB", "S"); + usb_devDescFID_bDeviceClass = env->GetFieldID(usb_devDescClazz, "bDeviceClass", "B"); + usb_devDescFID_bDeviceSubClass = env->GetFieldID(usb_devDescClazz, "bDeviceSubClass", "B"); + usb_devDescFID_bDeviceProtocol = env->GetFieldID(usb_devDescClazz, "bDeviceProtocol", "B"); + usb_devDescFID_bMaxPacketSize0 = env->GetFieldID(usb_devDescClazz, "bMaxPacketSize0", "B"); + usb_devDescFID_idVendor = env->GetFieldID(usb_devDescClazz, "idVendor", "S"); + usb_devDescFID_idProduct = env->GetFieldID(usb_devDescClazz, "idProduct", "S"); + usb_devDescFID_bcdDevice = env->GetFieldID(usb_devDescClazz, "bcdDevice", "S"); + usb_devDescFID_iManufacturer = env->GetFieldID(usb_devDescClazz, "iManufacturer", "B"); + usb_devDescFID_iProduct = env->GetFieldID(usb_devDescClazz, "iProduct", "B"); + usb_devDescFID_iSerialNumber = env->GetFieldID(usb_devDescClazz, "iSerialNumber", "B"); + usb_devDescFID_bNumConfigurations = env->GetFieldID(usb_devDescClazz, "bNumConfigurations", "B"); + + + // usb_configuration_descriptor + usb_confDescClazz = env->FindClass("ch/ntb/usb/Usb_Config_Descriptor"); + if (usb_confDescClazz == NULL) { return NULL; /* exception thrown */ } + usb_confDescMid = env->GetMethodID(usb_confDescClazz, "","()V"); + if (usb_confDescMid == NULL) { return NULL; } + + usb_confDescFID_bLength = env->GetFieldID(usb_confDescClazz, "bLength", "B"); + usb_confDescFID_bDescriptorType = env->GetFieldID(usb_confDescClazz, "bDescriptorType", "B"); + usb_confDescFID_wTotalLength = env->GetFieldID(usb_confDescClazz, "wTotalLength", "S"); + usb_confDescFID_bNumInterfaces = env->GetFieldID(usb_confDescClazz, "bNumInterfaces", "B"); + usb_confDescFID_bConfigurationValue = env->GetFieldID(usb_confDescClazz, "bConfigurationValue", "B"); + usb_confDescFID_iConfiguration = env->GetFieldID(usb_confDescClazz, "iConfiguration", "B"); + usb_confDescFID_bmAttributes = env->GetFieldID(usb_confDescClazz, "bmAttributes", "B"); + usb_confDescFID_MaxPower = env->GetFieldID(usb_confDescClazz, "MaxPower", "B"); + usb_confDescFID_interface_ = env->GetFieldID(usb_confDescClazz, "interface_", "[Lch/ntb/usb/Usb_Interface;"); + usb_confDescFID_extra = env->GetFieldID(usb_confDescClazz, "extra", "Lch/ntb/usb/Usb_Config_Descriptor;"); + usb_confDescFID_extralen = env->GetFieldID(usb_confDescClazz, "extralen", "I"); + + // usb_interface + usb_intClazz = env->FindClass("ch/ntb/usb/Usb_Interface"); + if (usb_intClazz == NULL) { return NULL; /* exception thrown */ } + usb_intMid = env->GetMethodID(usb_intClazz, "","()V"); + if (usb_intMid == NULL) { return NULL; } + + usb_intFID_altsetting = env->GetFieldID(usb_intClazz, "altsetting", "[Lch/ntb/usb/Usb_Interface_Descriptor;"); + usb_intFID_num_altsetting = env->GetFieldID(usb_intClazz, "num_altsetting", "I"); + + // usb_interface_descriptor + usb_intDescClazz = env->FindClass("ch/ntb/usb/Usb_Interface_Descriptor"); + if (usb_intDescClazz == NULL) { return NULL; /* exception thrown */ } + usb_intDescMid = env->GetMethodID(usb_intDescClazz, "","()V"); + if (usb_intDescMid == NULL) { return NULL; } + + usb_intDescFID_bLength = env->GetFieldID(usb_intDescClazz, "bLength", "B"); + usb_intDescFID_bDescriptorType = env->GetFieldID(usb_intDescClazz, "bDescriptorType", "B"); + usb_intDescFID_bInterfaceNumber = env->GetFieldID(usb_intDescClazz, "bInterfaceNumber", "B"); + usb_intDescFID_bAlternateSetting = env->GetFieldID(usb_intDescClazz, "bAlternateSetting", "B"); + usb_intDescFID_bNumEndpoints = env->GetFieldID(usb_intDescClazz, "bNumEndpoints", "B"); + usb_intDescFID_bInterfaceClass = env->GetFieldID(usb_intDescClazz, "bInterfaceClass", "B"); + usb_intDescFID_bInterfaceSubClass = env->GetFieldID(usb_intDescClazz, "bInterfaceSubClass", "B"); + usb_intDescFID_bInterfaceProtocol = env->GetFieldID(usb_intDescClazz, "bInterfaceProtocol", "B"); + usb_intDescFID_iInterface = env->GetFieldID(usb_intDescClazz, "iInterface", "B"); + usb_intDescFID_endpoint = env->GetFieldID(usb_intDescClazz, "endpoint", "[Lch/ntb/usb/Usb_Endpoint_Descriptor;"); + usb_intDescFID_extra = env->GetFieldID(usb_intDescClazz, "extra", "Lch/ntb/usb/Usb_Interface_Descriptor;"); + usb_intDescFID_extralen = env->GetFieldID(usb_intDescClazz, "extralen", "I"); + + // usb_endpoint_descriptor + usb_epDescClazz = env->FindClass("ch/ntb/usb/Usb_Endpoint_Descriptor"); + if (usb_epDescClazz == NULL) { return NULL; /* exception thrown */ } + usb_epDescMid = env->GetMethodID(usb_epDescClazz, "","()V"); + if (usb_epDescMid == NULL) { return NULL; } + + usb_epDescFID_bLength = env->GetFieldID(usb_epDescClazz, "bLength", "B"); + usb_epDescFID_bDescriptorType = env->GetFieldID(usb_epDescClazz, "bDescriptorType", "B"); + usb_epDescFID_bEndpointAddress = env->GetFieldID(usb_epDescClazz, "bEndpointAddress", "B"); + usb_epDescFID_bmAttributes = env->GetFieldID(usb_epDescClazz, "bmAttributes", "B"); + usb_epDescFID_wMaxPacketSize = env->GetFieldID(usb_epDescClazz, "wMaxPacketSize", "S"); + usb_epDescFID_bInterval = env->GetFieldID(usb_epDescClazz, "bInterval", "B"); + usb_epDescFID_bRefresh = env->GetFieldID(usb_epDescClazz, "bRefresh", "B"); + usb_epDescFID_bSynchAddress = env->GetFieldID(usb_epDescClazz, "bSynchAddress", "B"); + usb_epDescFID_extra = env->GetFieldID(usb_epDescClazz, "extra", "Lch/ntb/usb/Usb_Endpoint_Descriptor;"); + usb_epDescFID_extralen = env->GetFieldID(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 (2)\n", bus); +#endif + + // create a new object for every bus + if (!usb_busObj) { + usb_busObj = env->NewObject(usb_busClazz, usb_busMid); + if (!usb_busObj) { printf("Error NewObject 1\n"); 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(usb_busClazz, usb_busMid); + if (!usb_busObj_next) { printf("Error NewObject 2\n"); return NULL; } + } + env->SetObjectField(usb_busObj, usb_busFID_next, usb_busObj_next); + env->SetObjectField(usb_busObj, usb_busFID_prev, usb_busObj_prev); + env->SetObjectField(usb_busObj, usb_busFID_dirname, env->NewStringUTF(bus->dirname)); + env->SetLongField(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 (3)\n", dev); +#endif + // create a new object for every device + if (!usb_devObj){ + usb_devObj = env->NewObject(usb_devClazz, usb_devMid); + if (!usb_devObj) { printf("Error NewObject 3\n"); 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(usb_devClazz, usb_devMid); + if (!usb_devObj_next) { printf("Error NewObject 4\n"); return NULL; } + } + env->SetObjectField(usb_devObj, usb_devFID_next, usb_devObj_next); + env->SetObjectField(usb_devObj, usb_devFID_prev, usb_devObj_prev); + env->SetObjectField(usb_devObj, usb_devFID_bus, usb_busObj); + env->SetObjectField(usb_devObj, usb_devFID_filename, env->NewStringUTF(dev->filename)); + env->SetByteField(usb_devObj, usb_devFID_devnum, dev->devnum); + env->SetByteField(usb_devObj, usb_devFID_num_children, dev->num_children); + + // device descriptor + usb_devDescObj = env->NewObject(usb_devDescClazz, usb_devDescMid); + if (!usb_devDescObj) { printf("Error NewObject 5\n"); return NULL; } + env->SetByteField(usb_devDescObj, usb_devDescFID_bLength, dev->descriptor.bLength); + env->SetByteField(usb_devDescObj, usb_devDescFID_bDescriptorType, dev->descriptor.bDescriptorType); + env->SetShortField(usb_devDescObj, usb_devDescFID_bcdUSB, dev->descriptor.bcdUSB); + env->SetByteField(usb_devDescObj, usb_devDescFID_bDeviceClass, dev->descriptor.bDeviceClass); + env->SetByteField(usb_devDescObj, usb_devDescFID_bDeviceSubClass, dev->descriptor.bDeviceSubClass); + env->SetByteField(usb_devDescObj, usb_devDescFID_bDeviceProtocol, dev->descriptor.bDeviceProtocol); + env->SetByteField(usb_devDescObj, usb_devDescFID_bMaxPacketSize0, dev->descriptor.bMaxPacketSize0); + env->SetShortField(usb_devDescObj, usb_devDescFID_idVendor, dev->descriptor.idVendor); + env->SetShortField(usb_devDescObj, usb_devDescFID_idProduct, dev->descriptor.idProduct); + env->SetShortField(usb_devDescObj, usb_devDescFID_bcdDevice, dev->descriptor.bcdDevice); + env->SetByteField(usb_devDescObj, usb_devDescFID_iManufacturer, dev->descriptor.iManufacturer); + env->SetByteField(usb_devDescObj, usb_devDescFID_iProduct, dev->descriptor.iProduct); + env->SetByteField(usb_devDescObj, usb_devDescFID_iSerialNumber, dev->descriptor.iSerialNumber); + env->SetByteField(usb_devDescObj, usb_devDescFID_bNumConfigurations, dev->descriptor.bNumConfigurations); + + env->SetObjectField(usb_devObj, usb_devFID_descriptor, usb_devDescObj); + // configuration descriptor + // Loop through all of the configurations + usb_confDescObjArray = env->NewObjectArray(dev->descriptor.bNumConfigurations, usb_confDescClazz, NULL); + if (!usb_confDescObjArray) { printf("Error NewObject 6\n"); return NULL; } + for (int c = 0; c < dev->descriptor.bNumConfigurations; c++){ +#ifdef DEBUGON + printf("\t\tusb_get_busses: configuration %x (4)\n", c); +#endif + if (dev->config == NULL) { + printf("dev->config == NULL\n"); + return main_usb_busObj; + } + + usb_confDescObj = env->NewObject(usb_confDescClazz, usb_confDescMid); + if (!usb_confDescObj) { printf("Error NewObject 7\n"); return NULL; } + env->SetObjectArrayElement(usb_confDescObjArray, c, usb_confDescObj); + env->SetByteField(usb_confDescObj, usb_confDescFID_bLength, dev->config[c].bLength); + env->SetByteField(usb_confDescObj, usb_confDescFID_bDescriptorType, dev->config[c].bDescriptorType); + env->SetShortField(usb_confDescObj, usb_confDescFID_wTotalLength, dev->config[c].wTotalLength); + env->SetByteField(usb_confDescObj, usb_confDescFID_bNumInterfaces, dev->config[c].bNumInterfaces); + env->SetByteField(usb_confDescObj, usb_confDescFID_bConfigurationValue, dev->config[c].bConfigurationValue); + env->SetByteField(usb_confDescObj, usb_confDescFID_iConfiguration, dev->config[c].iConfiguration); + env->SetByteField(usb_confDescObj, usb_confDescFID_bmAttributes, dev->config[c].bmAttributes); + env->SetByteField(usb_confDescObj, usb_confDescFID_MaxPower, dev->config[c].MaxPower); + env->SetIntField(usb_confDescObj, usb_confDescFID_extralen, dev->config[c].extralen); + // extra descriptors are not interpreted!!! + env->SetObjectField(usb_confDescObj, usb_confDescFID_extra, NULL); + if (dev->config[c].extra){ + printf("The Device %d contains an extra descriptor!\n", dev->devnum); + printf("Extra descriptors are not passed to java!\n"); + } + // interface + usb_intObjArray = env->NewObjectArray(dev->config[c].bNumInterfaces, usb_intClazz, NULL); + if (!usb_intObjArray) { printf("Error NewObject 8\n"); return NULL; } + for (int i = 0; i < dev->config[c].bNumInterfaces; i++){ +#ifdef DEBUGON + printf("\t\t\tusb_get_busses: interface %x (5)\n", i); +#endif + if (dev->config[c].interface == NULL) { + printf("dev->config[c].interface == NULL\n"); + return main_usb_busObj; + } + + usb_intObj = env->NewObject(usb_intClazz, usb_intMid); + if (!usb_intObj) { printf("Error NewObject 9\n"); return NULL; } + env->SetObjectArrayElement(usb_intObjArray, i, usb_intObj); + env->SetIntField(usb_intObj, usb_intFID_num_altsetting, dev->config[c].interface[i].num_altsetting); + // interface descriptor + usb_intDescObjArray = env->NewObjectArray(dev->config[c].interface[i].num_altsetting, usb_intDescClazz, NULL); + if (!usb_intDescObjArray) { printf("Error NewObject 10\n"); 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 (6)\n", a); +#endif + if (dev->config[c].interface[i].altsetting == NULL) { + printf("dev->config[c].interface[i].altsetting == NULL\n"); + return main_usb_busObj; + } + + usb_intDescObj = env->NewObject(usb_intDescClazz, usb_intDescMid); + if (!usb_intDescObj) { printf("Error NewObject 11\n"); return NULL; } + env->SetObjectArrayElement(usb_intDescObjArray, a, usb_intDescObj); + env->SetByteField(usb_intDescObj, usb_intDescFID_bLength, dev->config[c].interface[i].altsetting[a].bLength); + env->SetByteField(usb_intDescObj, usb_intDescFID_bDescriptorType, dev->config[c].interface[i].altsetting[a].bDescriptorType); + env->SetByteField(usb_intDescObj, usb_intDescFID_bInterfaceNumber, dev->config[c].interface[i].altsetting[a].bInterfaceNumber); + env->SetByteField(usb_intDescObj, usb_intDescFID_bAlternateSetting, dev->config[c].interface[i].altsetting[a].bAlternateSetting); + env->SetByteField(usb_intDescObj, usb_intDescFID_bNumEndpoints, dev->config[c].interface[i].altsetting[a].bNumEndpoints); + env->SetByteField(usb_intDescObj, usb_intDescFID_bInterfaceClass, dev->config[c].interface[i].altsetting[a].bInterfaceClass); + env->SetByteField(usb_intDescObj, usb_intDescFID_bInterfaceSubClass, dev->config[c].interface[i].altsetting[a].bInterfaceSubClass); + env->SetByteField(usb_intDescObj, usb_intDescFID_bInterfaceProtocol, dev->config[c].interface[i].altsetting[a].bInterfaceProtocol); + env->SetByteField(usb_intDescObj, usb_intDescFID_iInterface, dev->config[c].interface[i].altsetting[a].iInterface); + env->SetIntField(usb_intDescObj, usb_intDescFID_extralen, dev->config[c].interface[i].altsetting[a].extralen); + // extra descriptors are not interpreted!!! + if (dev->config[c].interface[i].altsetting[a].extra){ + printf("The Device %d contains an extra interface descriptor!\n", dev->devnum); + printf("Extra descriptors are not passed to java!\n"); + } + env->SetObjectField(usb_intDescObj, usb_intDescFID_extra, NULL); + // endpoint descriptor + usb_epDescObjArray = env->NewObjectArray(dev->config[c].interface[i].altsetting[a].bNumEndpoints, usb_epDescClazz, NULL); + if (!usb_epDescObjArray) { printf("Error NewObject 12\n"); 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 (7)\n", e); +#endif + if (dev->config[c].interface[i].altsetting[a].endpoint == NULL) { + printf("dev->config[c].interface[i].altsetting[a].endpoint == NULL\n"); + return main_usb_busObj; + } + + usb_epDescObj = env->NewObject(usb_epDescClazz, usb_epDescMid); + if (!usb_epDescObj) { printf("Error NewObject 13\n"); return NULL; } + env->SetObjectArrayElement(usb_epDescObjArray, e, usb_epDescObj); + env->SetByteField(usb_epDescObj, usb_epDescFID_bLength, dev->config[c].interface[i].altsetting[a].endpoint[e].bLength); + env->SetByteField(usb_epDescObj, usb_epDescFID_bDescriptorType, dev->config[c].interface[i].altsetting[a].endpoint[e].bDescriptorType); + env->SetByteField(usb_epDescObj, usb_epDescFID_bEndpointAddress, dev->config[c].interface[i].altsetting[a].endpoint[e].bEndpointAddress); + env->SetByteField(usb_epDescObj, usb_epDescFID_bmAttributes, dev->config[c].interface[i].altsetting[a].endpoint[e].bmAttributes); + env->SetShortField(usb_epDescObj, usb_epDescFID_wMaxPacketSize, dev->config[c].interface[i].altsetting[a].endpoint[e].wMaxPacketSize); + env->SetByteField(usb_epDescObj, usb_epDescFID_bInterval, dev->config[c].interface[i].altsetting[a].endpoint[e].bInterval); + env->SetByteField(usb_epDescObj, usb_epDescFID_bRefresh, dev->config[c].interface[i].altsetting[a].endpoint[e].bRefresh); + env->SetByteField(usb_epDescObj, usb_epDescFID_bSynchAddress, dev->config[c].interface[i].altsetting[a].endpoint[e].bSynchAddress); + env->SetIntField(usb_epDescObj, usb_epDescFID_extralen, dev->config[c].interface[i].altsetting[a].endpoint[e].extralen); + // extra descriptors are not interpreted!!! + if (dev->config[c].interface[i].altsetting[a].endpoint[e].extra){ + printf("The Device %d contains an extra endpoint descriptor!\n", dev->devnum); + printf("Extra descriptors are not passed to java!\n"); + } + env->SetObjectField(usb_epDescObj, usb_epDescFID_extra, NULL); + } + env->SetObjectField(usb_intDescObj, usb_intDescFID_endpoint, usb_epDescObjArray); + } + env->SetObjectField(usb_intObj, usb_intFID_altsetting, usb_intDescObjArray); + } + env->SetObjectField(usb_confDescObj, usb_confDescFID_interface_, usb_intObjArray); + } + + + env->SetObjectField(usb_devObj, usb_devFID_config, usb_confDescObjArray); + + usb_devObj_prev = usb_devObj; + usb_devObj = usb_devObj_next; + dev = dev->next; + } + env->SetObjectField(usb_busObj, usb_busFID_devices, main_usb_devObj); + env->SetObjectField(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_LibusbWin + * Method: usb_open + * Signature: (Lch/ntb/usb/Usb_Device;)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1open + (JNIEnv *env, jobject obj, jobject dev) + { + if (busses == NULL) { return -1; } + + unsigned char devnum = env->GetByteField(dev, usb_devFID_devnum); + struct usb_bus *tmpBus; + + for (tmpBus = busses; tmpBus; tmpBus = tmpBus->next) { + struct usb_device *device; + for (device = tmpBus->devices; device; device = device->next) { + if (device->devnum == devnum){ + return (jint) usb_open(device); + } + } + } + return -3; + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_close + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1close + (JNIEnv *env, jobject obj, jint dev_handle) + { + return (jint) usb_close((usb_dev_handle *) dev_handle); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_set_configuration + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1set_1configuration + (JNIEnv *env, jobject obj, jint dev_handle, jint configuration) + { + return usb_set_configuration((usb_dev_handle *) dev_handle, configuration); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_set_altinterface + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1set_1altinterface + (JNIEnv *env, jobject obj, jint dev_handle, jint alternate) + { + return usb_set_altinterface((usb_dev_handle *) dev_handle, alternate); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_clear_halt + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1clear_1halt + (JNIEnv *env, jobject obj, jint dev_handle, jint ep) + { + return usb_clear_halt((usb_dev_handle *) dev_handle, (unsigned) ep); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_reset + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1reset + (JNIEnv *env, jobject obj, jint dev_handle) + { + return usb_reset((usb_dev_handle *) dev_handle); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_claim_interface + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1claim_1interface + (JNIEnv *env, jobject obj, jint dev_handle, jint interface) + { + return usb_claim_interface((usb_dev_handle *) dev_handle, interface); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_release_interface + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1release_1interface + (JNIEnv *env, jobject obj, jint dev_handle, jint interface) + { + return usb_release_interface((usb_dev_handle *) dev_handle, interface); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_control_msg + * Signature: (IIIII[BII)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1control_1msg + (JNIEnv *env, jobject obj, jint dev_handle, jint requesttype, jint request, jint value, jint index, jbyteArray jbytes, jint size, jint timeout) + { + jbyte *bytes = env->GetByteArrayElements(jbytes, NULL); + int retVal = usb_control_msg((usb_dev_handle *) dev_handle, requesttype, request, value, index, (char *) bytes, size, timeout); + if (bytes) { return retVal; } + jbytes = env->NewByteArray(size); + env->SetByteArrayRegion(jbytes, 0, size, bytes); + return retVal; + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_get_string + * Signature: (IIILjava/lang/String;I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1get_1string + (JNIEnv *env, jobject obj, jint dev_handle, jint index, jint langid, jstring jbuf, jint buflen) + { + const jchar *buf = env->GetStringChars(jbuf, NULL); + int retVal = usb_get_string((usb_dev_handle *) dev_handle, index, langid, (char *) buf, buflen); + jbuf = env->NewString(buf, buflen); + return retVal; + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_get_string_simple + * Signature: (IILjava/lang/String;I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1get_1string_1simple + (JNIEnv *env, jobject obj, jint dev_handle, jint index, jstring jbuf, jint buflen) + { + const jchar *buf = env->GetStringChars(jbuf, NULL); + int retVal = usb_get_string_simple((usb_dev_handle *) dev_handle, index, (char *) buf, buflen); + jbuf = env->NewString(buf, buflen); + return retVal; + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_get_descriptor + * Signature: (IBBLjava/lang/String;I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1get_1descriptor + (JNIEnv *env, jobject obj, jint dev_handle, jbyte type, jbyte index, jstring buf, jint size) + { + return usb_get_descriptor((usb_dev_handle *) dev_handle, (unsigned) type, (unsigned) index, buf, size); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_get_descriptor_by_endpoint + * Signature: (IIBBLjava/lang/String;I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1get_1descriptor_1by_1endpoint + (JNIEnv *env, jobject obj, jint dev_handle, jint ep, jbyte type, jbyte index, jstring buf, jint size) + { + return usb_get_descriptor_by_endpoint((usb_dev_handle *) dev_handle, ep, (unsigned) type, (unsigned) index, buf, size); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_bulk_write + * Signature: (II[BII)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1bulk_1write + (JNIEnv *env, jobject obj, jint dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout) + { + jbyte *bytes = env->GetByteArrayElements(jbytes, NULL); + return usb_bulk_write((usb_dev_handle *) dev_handle, ep, (char *) bytes, size, timeout); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_bulk_read + * Signature: (II[BII)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1bulk_1read + (JNIEnv *env, jobject obj, jint dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout) + { + char *bytes = (char *) malloc(size*sizeof(char)); + int retVal = usb_bulk_read((usb_dev_handle *) dev_handle, ep, bytes, size, timeout); + if (!bytes) { return retVal; } + // jbytes = env->NewByteArray(size); + env->SetByteArrayRegion(jbytes, 0, size, (jbyte *) bytes); + free(bytes); + return retVal; + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_interrupt_write + * Signature: (II[BII)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1interrupt_1write + (JNIEnv *env, jobject obj, jint dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout) + { + jbyte *bytes = env->GetByteArrayElements(jbytes, NULL); + return usb_interrupt_write((usb_dev_handle *) dev_handle, ep, (char *) bytes, size, timeout); + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_interrupt_read + * Signature: (II[BII)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1interrupt_1read + (JNIEnv *env, jobject obj, jint dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout) + { + jbyte *bytes; + int retVal = usb_interrupt_write((usb_dev_handle *) dev_handle, ep, (char *) bytes, size, timeout); + if (bytes) { return retVal; } + jbytes = env->NewByteArray(size); + env->SetByteArrayRegion(jbytes, 0, size, bytes); + return retVal; + } + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_strerror + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbWin_usb_1strerror + (JNIEnv *env, jobject obj){ + + char *str = usb_strerror(); + return env->NewStringUTF(str); + } + + diff --git a/libusbdll/LibusbWin.dll b/libusbdll/LibusbWin.dll new file mode 100644 index 0000000..fb5fd4a Binary files /dev/null and b/libusbdll/LibusbWin.dll differ diff --git a/libusbdll/LibusbWin.h b/libusbdll/LibusbWin.h new file mode 100644 index 0000000..97542f9 --- /dev/null +++ b/libusbdll/LibusbWin.h @@ -0,0 +1,189 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class ch_ntb_usb_LibusbWin */ + +#ifndef _Included_ch_ntb_usb_LibusbWin +#define _Included_ch_ntb_usb_LibusbWin +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_init + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_ch_ntb_usb_LibusbWin_usb_1init + (JNIEnv *, jobject); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_find_busses + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1find_1busses + (JNIEnv *, jobject); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_find_devices + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1find_1devices + (JNIEnv *, jobject); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_get_busses + * Signature: ()Lch/ntb/usb/Usb_Bus; + */ +JNIEXPORT jobject JNICALL Java_ch_ntb_usb_LibusbWin_usb_1get_1busses + (JNIEnv *, jobject); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_open + * Signature: (Lch/ntb/usb/Usb_Device;)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1open + (JNIEnv *, jobject, jobject); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_close + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1close + (JNIEnv *, jobject, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_set_configuration + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1set_1configuration + (JNIEnv *, jobject, jint, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_set_altinterface + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1set_1altinterface + (JNIEnv *, jobject, jint, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_clear_halt + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1clear_1halt + (JNIEnv *, jobject, jint, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_reset + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1reset + (JNIEnv *, jobject, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_claim_interface + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1claim_1interface + (JNIEnv *, jobject, jint, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_release_interface + * Signature: (II)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1release_1interface + (JNIEnv *, jobject, jint, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_control_msg + * Signature: (IIIII[BII)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1control_1msg + (JNIEnv *, jobject, jint, jint, jint, jint, jint, jbyteArray, jint, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_get_string + * Signature: (IIILjava/lang/String;I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1get_1string + (JNIEnv *, jobject, jint, jint, jint, jstring, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_get_string_simple + * Signature: (IILjava/lang/String;I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1get_1string_1simple + (JNIEnv *, jobject, jint, jint, jstring, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_get_descriptor + * Signature: (IBBLjava/lang/String;I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1get_1descriptor + (JNIEnv *, jobject, jint, jbyte, jbyte, jstring, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_get_descriptor_by_endpoint + * Signature: (IIBBLjava/lang/String;I)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1get_1descriptor_1by_1endpoint + (JNIEnv *, jobject, jint, jint, jbyte, jbyte, jstring, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_bulk_write + * Signature: (II[BII)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1bulk_1write + (JNIEnv *, jobject, jint, jint, jbyteArray, jint, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_bulk_read + * Signature: (II[BII)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1bulk_1read + (JNIEnv *, jobject, jint, jint, jbyteArray, jint, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_interrupt_write + * Signature: (II[BII)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1interrupt_1write + (JNIEnv *, jobject, jint, jint, jbyteArray, jint, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_interrupt_read + * Signature: (II[BII)I + */ +JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbWin_usb_1interrupt_1read + (JNIEnv *, jobject, jint, jint, jbyteArray, jint, jint); + +/* + * Class: ch_ntb_usb_LibusbWin + * Method: usb_strerror + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbWin_usb_1strerror + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/libusbdll/libusb.lib b/libusbdll/libusb.lib new file mode 100644 index 0000000..b5467c0 Binary files /dev/null and b/libusbdll/libusb.lib differ diff --git a/libusbdll/makefile b/libusbdll/makefile new file mode 100644 index 0000000..2eddc1b --- /dev/null +++ b/libusbdll/makefile @@ -0,0 +1,20 @@ +CC = gcc +SOURCENAME = LibusbWin +DDLNAME = LibusbWin +EXENAME = LibusbTest +JAVAPATH = "C:/Program Files/Java/jdk1.5.0_04" +TARGETDIR = D:/work/USB/libusb/libusbdllMinGW +## -IDLOUT:$(DDLNAME) -MIDL:$(DDLNAME) -TLBOUT:$(DDLNAME) +## $(CC) -LD $(SOURCENAME).c -Fe$(DDLNAME).dll -link libusb.lib + +dll: $(DDLNAME).dll +$(DDLNAME).dll: $(SOURCENAME).cpp + $(CC) -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -I$(JAVAPATH)/include -I$(JAVAPATH)/include/win32 -shared $(SOURCENAME).cpp -o $(DDLNAME).dll libusb.lib + +cleanDll: + rm $(DDLNAME).dll + +testExe: $(EXENAME).exe +$(EXENAME).exe: $(EXENAME).cpp + $(CC) $(EXENAME).cpp -o $(EXENAME).exe $(TARGETDIR)/libusb.a + diff --git a/libusbdll/stdafx.h b/libusbdll/stdafx.h new file mode 100644 index 0000000..1ad3b78 --- /dev/null +++ b/libusbdll/stdafx.h @@ -0,0 +1,13 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + + +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers +// Windows Header Files: +#include + +// TODO: reference additional headers your program requires here diff --git a/libusbdll/usb.h b/libusbdll/usb.h new file mode 100644 index 0000000..04f5afa --- /dev/null +++ b/libusbdll/usb.h @@ -0,0 +1,374 @@ +#ifndef __USB_H__ +#define __USB_H__ + +#include + +/* + * 'interface' is defined somewhere in the Windows header files. This macro + * is deleted here to avoid conflicts and compile errors. + */ + +#ifdef interface +#undef interface +#endif + +/* + * PATH_MAX from limits.h can't be used on Windows if the dll and + * import libraries are build/used by different compilers + */ + +#define LIBUSB_PATH_MAX 512 + + +/* + * USB spec information + * + * This is all stuff grabbed from various USB specs and is pretty much + * not subject to change + */ + +/* + * Device and/or Interface Class codes + */ +#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ +#define USB_CLASS_AUDIO 1 +#define USB_CLASS_COMM 2 +#define USB_CLASS_HID 3 +#define USB_CLASS_PRINTER 7 +#define USB_CLASS_MASS_STORAGE 8 +#define USB_CLASS_HUB 9 +#define USB_CLASS_DATA 10 +#define USB_CLASS_VENDOR_SPEC 0xff + +/* + * Descriptor types + */ +#define USB_DT_DEVICE 0x01 +#define USB_DT_CONFIG 0x02 +#define USB_DT_STRING 0x03 +#define USB_DT_INTERFACE 0x04 +#define USB_DT_ENDPOINT 0x05 + +#define USB_DT_HID 0x21 +#define USB_DT_REPORT 0x22 +#define USB_DT_PHYSICAL 0x23 +#define USB_DT_HUB 0x29 + +/* + * Descriptor sizes per descriptor type + */ +#define USB_DT_DEVICE_SIZE 18 +#define USB_DT_CONFIG_SIZE 9 +#define USB_DT_INTERFACE_SIZE 9 +#define USB_DT_ENDPOINT_SIZE 7 +#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ +#define USB_DT_HUB_NONVAR_SIZE 7 + + +/* ensure byte-packed structures */ +#include + + +/* All standard descriptors have these 2 fields in common */ +struct usb_descriptor_header { + unsigned char bLength; + unsigned char bDescriptorType; +}; + +/* String descriptor */ +struct usb_string_descriptor { + unsigned char bLength; + unsigned char bDescriptorType; + unsigned short wData[1]; +}; + +/* HID descriptor */ +struct usb_hid_descriptor { + unsigned char bLength; + unsigned char bDescriptorType; + unsigned short bcdHID; + unsigned char bCountryCode; + unsigned char bNumDescriptors; +}; + +/* Endpoint descriptor */ +#define USB_MAXENDPOINTS 32 +struct usb_endpoint_descriptor { + unsigned char bLength; + unsigned char bDescriptorType; + unsigned char bEndpointAddress; + unsigned char bmAttributes; + unsigned short wMaxPacketSize; + unsigned char bInterval; + unsigned char bRefresh; + unsigned char bSynchAddress; + + unsigned char *extra; /* Extra descriptors */ + int extralen; +}; + +#define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ +#define USB_ENDPOINT_DIR_MASK 0x80 + +#define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */ +#define USB_ENDPOINT_TYPE_CONTROL 0 +#define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 +#define USB_ENDPOINT_TYPE_BULK 2 +#define USB_ENDPOINT_TYPE_INTERRUPT 3 + +/* Interface descriptor */ +#define USB_MAXINTERFACES 32 +struct usb_interface_descriptor { + unsigned char bLength; + unsigned char bDescriptorType; + unsigned char bInterfaceNumber; + unsigned char bAlternateSetting; + unsigned char bNumEndpoints; + unsigned char bInterfaceClass; + unsigned char bInterfaceSubClass; + unsigned char bInterfaceProtocol; + unsigned char iInterface; + + struct usb_endpoint_descriptor *endpoint; + + unsigned char *extra; /* Extra descriptors */ + int extralen; +}; + +#define USB_MAXALTSETTING 128 /* Hard limit */ + +struct usb_interface { + struct usb_interface_descriptor *altsetting; + + int num_altsetting; +}; + +/* Configuration descriptor information.. */ +#define USB_MAXCONFIG 8 +struct usb_config_descriptor { + unsigned char bLength; + unsigned char bDescriptorType; + unsigned short wTotalLength; + unsigned char bNumInterfaces; + unsigned char bConfigurationValue; + unsigned char iConfiguration; + unsigned char bmAttributes; + unsigned char MaxPower; + + struct usb_interface *interface; + + unsigned char *extra; /* Extra descriptors */ + int extralen; +}; + +/* Device descriptor */ +struct usb_device_descriptor { + unsigned char bLength; + unsigned char bDescriptorType; + unsigned short bcdUSB; + unsigned char bDeviceClass; + unsigned char bDeviceSubClass; + unsigned char bDeviceProtocol; + unsigned char bMaxPacketSize0; + unsigned short idVendor; + unsigned short idProduct; + unsigned short bcdDevice; + unsigned char iManufacturer; + unsigned char iProduct; + unsigned char iSerialNumber; + unsigned char bNumConfigurations; +}; + +struct usb_ctrl_setup { + unsigned char bRequestType; + unsigned char bRequest; + unsigned short wValue; + unsigned short wIndex; + unsigned short wLength; +}; + +/* + * Standard requests + */ +#define USB_REQ_GET_STATUS 0x00 +#define USB_REQ_CLEAR_FEATURE 0x01 +/* 0x02 is reserved */ +#define USB_REQ_SET_FEATURE 0x03 +/* 0x04 is reserved */ +#define USB_REQ_SET_ADDRESS 0x05 +#define USB_REQ_GET_DESCRIPTOR 0x06 +#define USB_REQ_SET_DESCRIPTOR 0x07 +#define USB_REQ_GET_CONFIGURATION 0x08 +#define USB_REQ_SET_CONFIGURATION 0x09 +#define USB_REQ_GET_INTERFACE 0x0A +#define USB_REQ_SET_INTERFACE 0x0B +#define USB_REQ_SYNCH_FRAME 0x0C + +#define USB_TYPE_STANDARD (0x00 << 5) +#define USB_TYPE_CLASS (0x01 << 5) +#define USB_TYPE_VENDOR (0x02 << 5) +#define USB_TYPE_RESERVED (0x03 << 5) + +#define USB_RECIP_DEVICE 0x00 +#define USB_RECIP_INTERFACE 0x01 +#define USB_RECIP_ENDPOINT 0x02 +#define USB_RECIP_OTHER 0x03 + +/* + * Various libusb API related stuff + */ + +#define USB_ENDPOINT_IN 0x80 +#define USB_ENDPOINT_OUT 0x00 + +/* Error codes */ +#define USB_ERROR_BEGIN 500000 + +/* + * This is supposed to look weird. This file is generated from autoconf + * and I didn't want to make this too complicated. + */ +#define USB_LE16_TO_CPU(x) + +/* Data types */ +/* struct usb_device; */ +/* struct usb_bus; */ + +struct usb_device { + struct usb_device *next, *prev; + + char filename[LIBUSB_PATH_MAX]; + + struct usb_bus *bus; + + struct usb_device_descriptor descriptor; + struct usb_config_descriptor *config; + + void *dev; /* Darwin support */ + + unsigned char devnum; + + unsigned char num_children; + struct usb_device **children; +}; + +struct usb_bus { + struct usb_bus *next, *prev; + + char dirname[LIBUSB_PATH_MAX]; + + struct usb_device *devices; + unsigned long location; + + struct usb_device *root_dev; +}; + +/* Version information, Windows specific */ +struct usb_version { + struct { + int major; + int minor; + int micro; + int nano; + } dll; + struct { + int major; + int minor; + int micro; + int nano; + } driver; +}; + + +struct usb_dev_handle; +typedef struct usb_dev_handle usb_dev_handle; + +/* Variables */ +extern struct usb_bus *usb_busses; + + +#include + + +#ifdef __cplusplus +extern "C" { +#endif + + /* Function prototypes */ + + /* usb.c */ + usb_dev_handle *usb_open(struct usb_device *dev); + int usb_close(usb_dev_handle *dev); + int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, + size_t buflen); + int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, + size_t buflen); + + /* descriptors.c */ + int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep, + unsigned char type, unsigned char index, + void *buf, int size); + int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, + unsigned char index, void *buf, int size); + + /* .c */ + int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, + int timeout); + int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, + int timeout); + int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, + int timeout); + int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, + int timeout); + int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, + int value, int index, char *bytes, int size, + int timeout); + int usb_set_configuration(usb_dev_handle *dev, int configuration); + int usb_claim_interface(usb_dev_handle *dev, int interface); + int usb_release_interface(usb_dev_handle *dev, int interface); + int usb_set_altinterface(usb_dev_handle *dev, int alternate); + int usb_resetep(usb_dev_handle *dev, unsigned int ep); + int usb_clear_halt(usb_dev_handle *dev, unsigned int ep); + int usb_reset(usb_dev_handle *dev); + + char *usb_strerror(void); + + void usb_init(void); + void usb_set_debug(int level); + int usb_find_busses(void); + int usb_find_devices(void); + struct usb_device *usb_device(usb_dev_handle *dev); + struct usb_bus *usb_get_busses(void); + + + /* Windows specific functions */ + + #define LIBUSB_HAS_INSTALL_SERVICE_NP 1 + int usb_install_service_np(void); + + #define LIBUSB_HAS_UNINSTALL_SERVICE_NP 1 + int usb_uninstall_service_np(void); + + #define LIBUSB_HAS_INSTALL_DRIVER_NP 1 + int usb_install_driver_np(const char *inf_file); + + const struct usb_version *usb_get_version(void); + + int usb_isochronous_setup_async(usb_dev_handle *dev, void **context, + unsigned char ep, int pktsize); + int usb_bulk_setup_async(usb_dev_handle *dev, void **context, + unsigned char ep); + int usb_interrupt_setup_async(usb_dev_handle *dev, void **context, + unsigned char ep); + + int usb_submit_async(void *context, char *bytes, int size); + int usb_reap_async(void *context, int timeout); + int usb_free_async(void **context); + + +#ifdef __cplusplus +} +#endif + +#endif /* __USB_H__ */ +