1 |
2 |
ZTEX |
/*
|
2 |
|
|
* Java libusb wrapper
|
3 |
|
|
* Copyright (c) 2005-2006 Andreas Schläpfer <spandi at users.sourceforge.net>
|
4 |
|
|
*
|
5 |
|
|
* http://libusbjava.sourceforge.net
|
6 |
|
|
* This library is covered by the LGPL, read LGPL.txt for details.
|
7 |
|
|
*/
|
8 |
|
|
|
9 |
|
|
#include <jni.h>
|
10 |
|
|
#include <stddef.h>
|
11 |
|
|
#include <string.h>
|
12 |
|
|
#include <locale.h>
|
13 |
|
|
#include <errno.h>
|
14 |
|
|
#include <usb.h>
|
15 |
|
|
#include "LibusbJava.h"
|
16 |
|
|
|
17 |
|
|
// Windows specific stuff
|
18 |
|
|
#ifdef WIN32
|
19 |
|
|
#include <error.h>
|
20 |
|
|
#endif
|
21 |
|
|
|
22 |
|
|
//#define DEBUGON
|
23 |
|
|
|
24 |
|
|
// global bus (updated when usb_get_busses() is called)
|
25 |
|
|
struct usb_bus *busses;
|
26 |
|
|
|
27 |
|
|
// global flag for loading all class, method and field ID references
|
28 |
|
|
int java_references_loaded = 0;
|
29 |
|
|
|
30 |
|
|
// if > 0 an LibusbJava specific error string is set
|
31 |
|
|
char *libusbJavaError = NULL;
|
32 |
|
|
|
33 |
|
|
// macros to set and clear LibusbJava specific errors
|
34 |
|
|
#define setLibusbJavaError(error) libusbJavaError = error
|
35 |
|
|
#define clearLibusbJavaError() libusbJavaError = NULL
|
36 |
|
|
|
37 |
|
|
// class references
|
38 |
|
|
jclass usb_busClazz, usb_devClazz, usb_devDescClazz, usb_confDescClazz, \
|
39 |
|
|
usb_intClazz, usb_intDescClazz, usb_epDescClazz;
|
40 |
|
|
|
41 |
|
|
// method ID references
|
42 |
|
|
jmethodID usb_busMid, usb_devMid, usb_devDescMid, usb_confDescMid, \
|
43 |
|
|
usb_intMid, usb_intDescMid, usb_epDescMid;
|
44 |
|
|
|
45 |
|
|
// field ID references
|
46 |
|
|
// usb_bus
|
47 |
|
|
jfieldID usb_busFID_next, usb_busFID_prev, usb_busFID_dirname, \
|
48 |
|
|
usb_busFID_devices, usb_busFID_location, usb_busFID_root_dev;
|
49 |
|
|
// usb_device
|
50 |
|
|
jfieldID usb_devFID_next, usb_devFID_prev, usb_devFID_filename, \
|
51 |
|
|
usb_devFID_bus, usb_devFID_descriptor, usb_devFID_config, \
|
52 |
|
|
usb_devFID_devnum, usb_devFID_num_children, usb_devFID_children, \
|
53 |
|
|
usb_devFID_devStructAddr;
|
54 |
|
|
// usb_deviceDescriptor
|
55 |
|
|
jfieldID usb_devDescFID_bLength, usb_devDescFID_bDescriptorType, \
|
56 |
|
|
usb_devDescFID_bcdUSB, usb_devDescFID_bDeviceClass, \
|
57 |
|
|
usb_devDescFID_bDeviceSubClass, usb_devDescFID_bDeviceProtocol, \
|
58 |
|
|
usb_devDescFID_bMaxPacketSize0, usb_devDescFID_idVendor, \
|
59 |
|
|
usb_devDescFID_idProduct, usb_devDescFID_bcdDevice, \
|
60 |
|
|
usb_devDescFID_iManufacturer, usb_devDescFID_iProduct, \
|
61 |
|
|
usb_devDescFID_iSerialNumber, usb_devDescFID_bNumConfigurations;
|
62 |
|
|
// usb_configurationDescriptor
|
63 |
|
|
jfieldID usb_confDescFID_bLength, usb_confDescFID_bDescriptorType, usb_confDescFID_wTotalLength, \
|
64 |
|
|
usb_confDescFID_bNumInterfaces, usb_confDescFID_bConfigurationValue, \
|
65 |
|
|
usb_confDescFID_iConfiguration, usb_confDescFID_bmAttributes, usb_confDescFID_MaxPower, \
|
66 |
|
|
usb_confDescFID_interface_, usb_confDescFID_extra, usb_confDescFID_extralen;
|
67 |
|
|
// usb_interface
|
68 |
|
|
jfieldID usb_intFID_altsetting, usb_intFID_num_altsetting;
|
69 |
|
|
// usb_intDesc
|
70 |
|
|
jfieldID usb_intDescFID_bLength, usb_intDescFID_bDescriptorType, \
|
71 |
|
|
usb_intDescFID_bInterfaceNumber, usb_intDescFID_bAlternateSetting, \
|
72 |
|
|
usb_intDescFID_bNumEndpoints, usb_intDescFID_bInterfaceClass, \
|
73 |
|
|
usb_intDescFID_bInterfaceSubClass, usb_intDescFID_bInterfaceProtocol, \
|
74 |
|
|
usb_intDescFID_iInterface, usb_intDescFID_endpoint, usb_intDescFID_extra, \
|
75 |
|
|
usb_intDescFID_extralen;
|
76 |
|
|
// usb_endpointDescriptor
|
77 |
|
|
jfieldID usb_epDescFID_bLength, usb_epDescFID_bDescriptorType, \
|
78 |
|
|
usb_epDescFID_bEndpointAddress, usb_epDescFID_bmAttributes, \
|
79 |
|
|
usb_epDescFID_wMaxPacketSize, usb_epDescFID_bInterval, \
|
80 |
|
|
usb_epDescFID_bRefresh, usb_epDescFID_bSynchAddress, usb_epDescFID_extra, \
|
81 |
|
|
usb_epDescFID_extralen;
|
82 |
|
|
|
83 |
|
|
/*
|
84 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
85 |
|
|
* Method: usb_set_debug
|
86 |
|
|
* Signature: (B)V
|
87 |
|
|
*/
|
88 |
|
|
JNIEXPORT void JNICALL Java_ch_ntb_usb_LibusbJava_usb_1set_1debug
|
89 |
|
|
(JNIEnv *env, jclass obj, jint level)
|
90 |
|
|
{
|
91 |
|
|
clearLibusbJavaError();
|
92 |
|
|
usb_set_debug(level);
|
93 |
|
|
}
|
94 |
|
|
/*
|
95 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
96 |
|
|
* Method: usb_init
|
97 |
|
|
* Signature: ()V
|
98 |
|
|
*/
|
99 |
|
|
JNIEXPORT void JNICALL Java_ch_ntb_usb_LibusbJava_usb_1init
|
100 |
|
|
(JNIEnv *env, jclass obj)
|
101 |
|
|
{
|
102 |
|
|
clearLibusbJavaError();
|
103 |
|
|
usb_init();
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
/*
|
107 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
108 |
|
|
* Method: usb_find_busses
|
109 |
|
|
* Signature: ()I
|
110 |
|
|
*/
|
111 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1find_1busses
|
112 |
|
|
(JNIEnv *env, jclass obj)
|
113 |
|
|
{
|
114 |
|
|
clearLibusbJavaError();
|
115 |
|
|
return usb_find_busses();
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
/*
|
119 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
120 |
|
|
* Method: usb_find_devices
|
121 |
|
|
* Signature: ()I
|
122 |
|
|
*/
|
123 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1find_1devices
|
124 |
|
|
(JNIEnv *env, jclass obj)
|
125 |
|
|
{
|
126 |
|
|
clearLibusbJavaError();
|
127 |
|
|
return usb_find_devices();
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
/*
|
131 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
132 |
|
|
* Method: usb_get_busses
|
133 |
|
|
* Signature: ()Lch/ntb/usb/Usb_Bus;
|
134 |
|
|
*/
|
135 |
|
|
JNIEXPORT jobject JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1busses
|
136 |
|
|
(JNIEnv *env, jclass obj)
|
137 |
|
|
{
|
138 |
|
|
|
139 |
|
|
clearLibusbJavaError();
|
140 |
|
|
|
141 |
|
|
// only load class, method and field ID references once
|
142 |
|
|
if (!java_references_loaded) {
|
143 |
|
|
// find classes and field ids
|
144 |
|
|
// usb_bus
|
145 |
|
|
usb_busClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Bus");
|
146 |
|
|
if (usb_busClazz == NULL) { return NULL; /* exception thrown */ }
|
147 |
|
|
usb_busMid = (*env)->GetMethodID(env, usb_busClazz, "<init>","()V");
|
148 |
|
|
if (usb_busMid == NULL) { return NULL; }
|
149 |
|
|
|
150 |
|
|
usb_busFID_next = (*env)->GetFieldID(env, usb_busClazz, "next", "Lch/ntb/usb/Usb_Bus;");
|
151 |
|
|
usb_busFID_prev = (*env)->GetFieldID(env, usb_busClazz, "prev", "Lch/ntb/usb/Usb_Bus;");
|
152 |
|
|
usb_busFID_dirname = (*env)->GetFieldID(env, usb_busClazz, "dirname", "Ljava/lang/String;");
|
153 |
|
|
usb_busFID_devices = (*env)->GetFieldID(env, usb_busClazz, "devices", "Lch/ntb/usb/Usb_Device;");
|
154 |
|
|
usb_busFID_location = (*env)->GetFieldID(env, usb_busClazz, "location", "J");
|
155 |
|
|
usb_busFID_root_dev = (*env)->GetFieldID(env, usb_busClazz, "root_dev", "Lch/ntb/usb/Usb_Device;");
|
156 |
|
|
|
157 |
|
|
// usb_device
|
158 |
|
|
usb_devClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Device");
|
159 |
|
|
if (usb_devClazz == NULL) { return NULL; /* exception thrown */ }
|
160 |
|
|
usb_devMid = (*env)->GetMethodID(env, usb_devClazz, "<init>","()V");
|
161 |
|
|
if (usb_devMid == NULL) { return NULL; }
|
162 |
|
|
|
163 |
|
|
usb_devFID_next = (*env)->GetFieldID(env, usb_devClazz, "next", "Lch/ntb/usb/Usb_Device;");
|
164 |
|
|
usb_devFID_prev = (*env)->GetFieldID(env, usb_devClazz, "prev", "Lch/ntb/usb/Usb_Device;");
|
165 |
|
|
usb_devFID_filename = (*env)->GetFieldID(env, usb_devClazz, "filename", "Ljava/lang/String;");
|
166 |
|
|
usb_devFID_bus = (*env)->GetFieldID(env, usb_devClazz, "bus", "Lch/ntb/usb/Usb_Bus;");
|
167 |
|
|
usb_devFID_descriptor = (*env)->GetFieldID(env, usb_devClazz, "descriptor", "Lch/ntb/usb/Usb_Device_Descriptor;");
|
168 |
|
|
usb_devFID_config = (*env)->GetFieldID(env, usb_devClazz, "config", "[Lch/ntb/usb/Usb_Config_Descriptor;");
|
169 |
|
|
usb_devFID_devnum = (*env)->GetFieldID(env, usb_devClazz, "devnum", "B");
|
170 |
|
|
usb_devFID_num_children = (*env)->GetFieldID(env, usb_devClazz, "num_children", "B");
|
171 |
|
|
usb_devFID_children = (*env)->GetFieldID(env, usb_devClazz, "children", "Lch/ntb/usb/Usb_Device;");
|
172 |
|
|
usb_devFID_devStructAddr = (*env)->GetFieldID(env, usb_devClazz, "devStructAddr", "J");
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
// usb_device_descriptor
|
176 |
|
|
usb_devDescClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Device_Descriptor");
|
177 |
|
|
if (usb_devDescClazz == NULL) { return NULL; /* exception thrown */ }
|
178 |
|
|
usb_devDescMid = (*env)->GetMethodID(env, usb_devDescClazz, "<init>","()V");
|
179 |
|
|
if (usb_devDescMid == NULL) { return NULL; }
|
180 |
|
|
|
181 |
|
|
usb_devDescFID_bLength = (*env)->GetFieldID(env, usb_devDescClazz, "bLength", "B");
|
182 |
|
|
usb_devDescFID_bDescriptorType = (*env)->GetFieldID(env, usb_devDescClazz, "bDescriptorType", "B");
|
183 |
|
|
usb_devDescFID_bcdUSB = (*env)->GetFieldID(env, usb_devDescClazz, "bcdUSB", "S");
|
184 |
|
|
usb_devDescFID_bDeviceClass = (*env)->GetFieldID(env, usb_devDescClazz, "bDeviceClass", "B");
|
185 |
|
|
usb_devDescFID_bDeviceSubClass = (*env)->GetFieldID(env, usb_devDescClazz, "bDeviceSubClass", "B");
|
186 |
|
|
usb_devDescFID_bDeviceProtocol = (*env)->GetFieldID(env, usb_devDescClazz, "bDeviceProtocol", "B");
|
187 |
|
|
usb_devDescFID_bMaxPacketSize0 = (*env)->GetFieldID(env, usb_devDescClazz, "bMaxPacketSize0", "B");
|
188 |
|
|
usb_devDescFID_idVendor = (*env)->GetFieldID(env, usb_devDescClazz, "idVendor", "S");
|
189 |
|
|
usb_devDescFID_idProduct = (*env)->GetFieldID(env, usb_devDescClazz, "idProduct", "S");
|
190 |
|
|
usb_devDescFID_bcdDevice = (*env)->GetFieldID(env, usb_devDescClazz, "bcdDevice", "S");
|
191 |
|
|
usb_devDescFID_iManufacturer = (*env)->GetFieldID(env, usb_devDescClazz, "iManufacturer", "B");
|
192 |
|
|
usb_devDescFID_iProduct = (*env)->GetFieldID(env, usb_devDescClazz, "iProduct", "B");
|
193 |
|
|
usb_devDescFID_iSerialNumber = (*env)->GetFieldID(env, usb_devDescClazz, "iSerialNumber", "B");
|
194 |
|
|
usb_devDescFID_bNumConfigurations = (*env)->GetFieldID(env, usb_devDescClazz, "bNumConfigurations", "B");
|
195 |
|
|
|
196 |
|
|
// usb_configuration_descriptor
|
197 |
|
|
usb_confDescClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Config_Descriptor");
|
198 |
|
|
if (usb_confDescClazz == NULL) { return NULL; /* exception thrown */ }
|
199 |
|
|
usb_confDescMid = (*env)->GetMethodID(env, usb_confDescClazz, "<init>","()V");
|
200 |
|
|
if (usb_confDescMid == NULL) { return NULL; }
|
201 |
|
|
|
202 |
|
|
usb_confDescFID_bLength = (*env)->GetFieldID(env, usb_confDescClazz, "bLength", "B");
|
203 |
|
|
usb_confDescFID_bDescriptorType = (*env)->GetFieldID(env, usb_confDescClazz, "bDescriptorType", "B");
|
204 |
|
|
usb_confDescFID_wTotalLength = (*env)->GetFieldID(env, usb_confDescClazz, "wTotalLength", "S");
|
205 |
|
|
usb_confDescFID_bNumInterfaces = (*env)->GetFieldID(env, usb_confDescClazz, "bNumInterfaces", "B");
|
206 |
|
|
usb_confDescFID_bConfigurationValue = (*env)->GetFieldID(env, usb_confDescClazz, "bConfigurationValue", "B");
|
207 |
|
|
usb_confDescFID_iConfiguration = (*env)->GetFieldID(env, usb_confDescClazz, "iConfiguration", "B");
|
208 |
|
|
usb_confDescFID_bmAttributes = (*env)->GetFieldID(env, usb_confDescClazz, "bmAttributes", "B");
|
209 |
|
|
usb_confDescFID_MaxPower = (*env)->GetFieldID(env, usb_confDescClazz, "MaxPower", "B");
|
210 |
|
|
usb_confDescFID_interface_ = (*env)->GetFieldID(env, usb_confDescClazz, "interface_", "[Lch/ntb/usb/Usb_Interface;");
|
211 |
|
|
usb_confDescFID_extra = (*env)->GetFieldID(env, usb_confDescClazz, "extra", "[B");
|
212 |
|
|
usb_confDescFID_extralen = (*env)->GetFieldID(env, usb_confDescClazz, "extralen", "I");
|
213 |
|
|
|
214 |
|
|
// usb_interface
|
215 |
|
|
usb_intClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Interface");
|
216 |
|
|
if (usb_intClazz == NULL) { return NULL; /* exception thrown */ }
|
217 |
|
|
usb_intMid = (*env)->GetMethodID(env, usb_intClazz, "<init>","()V");
|
218 |
|
|
if (usb_intMid == NULL) { return NULL; }
|
219 |
|
|
|
220 |
|
|
usb_intFID_altsetting = (*env)->GetFieldID(env, usb_intClazz, "altsetting", "[Lch/ntb/usb/Usb_Interface_Descriptor;");
|
221 |
|
|
usb_intFID_num_altsetting = (*env)->GetFieldID(env, usb_intClazz, "num_altsetting", "I");
|
222 |
|
|
|
223 |
|
|
// usb_interface_descriptor
|
224 |
|
|
usb_intDescClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Interface_Descriptor");
|
225 |
|
|
if (usb_intDescClazz == NULL) { return NULL; /* exception thrown */ }
|
226 |
|
|
usb_intDescMid = (*env)->GetMethodID(env, usb_intDescClazz, "<init>","()V");
|
227 |
|
|
if (usb_intDescMid == NULL) { return NULL; }
|
228 |
|
|
|
229 |
|
|
usb_intDescFID_bLength = (*env)->GetFieldID(env, usb_intDescClazz, "bLength", "B");
|
230 |
|
|
usb_intDescFID_bDescriptorType = (*env)->GetFieldID(env, usb_intDescClazz, "bDescriptorType", "B");
|
231 |
|
|
usb_intDescFID_bInterfaceNumber = (*env)->GetFieldID(env, usb_intDescClazz, "bInterfaceNumber", "B");
|
232 |
|
|
usb_intDescFID_bAlternateSetting = (*env)->GetFieldID(env, usb_intDescClazz, "bAlternateSetting", "B");
|
233 |
|
|
usb_intDescFID_bNumEndpoints = (*env)->GetFieldID(env, usb_intDescClazz, "bNumEndpoints", "B");
|
234 |
|
|
usb_intDescFID_bInterfaceClass = (*env)->GetFieldID(env, usb_intDescClazz, "bInterfaceClass", "B");
|
235 |
|
|
usb_intDescFID_bInterfaceSubClass = (*env)->GetFieldID(env, usb_intDescClazz, "bInterfaceSubClass", "B");
|
236 |
|
|
usb_intDescFID_bInterfaceProtocol = (*env)->GetFieldID(env, usb_intDescClazz, "bInterfaceProtocol", "B");
|
237 |
|
|
usb_intDescFID_iInterface = (*env)->GetFieldID(env, usb_intDescClazz, "iInterface", "B");
|
238 |
|
|
usb_intDescFID_endpoint = (*env)->GetFieldID(env, usb_intDescClazz, "endpoint", "[Lch/ntb/usb/Usb_Endpoint_Descriptor;");
|
239 |
|
|
usb_intDescFID_extra = (*env)->GetFieldID(env, usb_intDescClazz, "extra", "[B");
|
240 |
|
|
usb_intDescFID_extralen = (*env)->GetFieldID(env, usb_intDescClazz, "extralen", "I");
|
241 |
|
|
|
242 |
|
|
// usb_endpoint_descriptor
|
243 |
|
|
usb_epDescClazz = (*env)->FindClass(env, "ch/ntb/usb/Usb_Endpoint_Descriptor");
|
244 |
|
|
if (usb_epDescClazz == NULL) { return NULL; /* exception thrown */ }
|
245 |
|
|
usb_epDescMid = (*env)->GetMethodID(env, usb_epDescClazz, "<init>","()V");
|
246 |
|
|
if (usb_epDescMid == NULL) { return NULL; }
|
247 |
|
|
|
248 |
|
|
usb_epDescFID_bLength = (*env)->GetFieldID(env, usb_epDescClazz, "bLength", "B");
|
249 |
|
|
usb_epDescFID_bDescriptorType = (*env)->GetFieldID(env, usb_epDescClazz, "bDescriptorType", "B");
|
250 |
|
|
usb_epDescFID_bEndpointAddress = (*env)->GetFieldID(env, usb_epDescClazz, "bEndpointAddress", "B");
|
251 |
|
|
usb_epDescFID_bmAttributes = (*env)->GetFieldID(env, usb_epDescClazz, "bmAttributes", "B");
|
252 |
|
|
usb_epDescFID_wMaxPacketSize = (*env)->GetFieldID(env, usb_epDescClazz, "wMaxPacketSize", "S");
|
253 |
|
|
usb_epDescFID_bInterval = (*env)->GetFieldID(env, usb_epDescClazz, "bInterval", "B");
|
254 |
|
|
usb_epDescFID_bRefresh = (*env)->GetFieldID(env, usb_epDescClazz, "bRefresh", "B");
|
255 |
|
|
usb_epDescFID_bSynchAddress = (*env)->GetFieldID(env, usb_epDescClazz, "bSynchAddress", "B");
|
256 |
|
|
usb_epDescFID_extra = (*env)->GetFieldID(env, usb_epDescClazz, "extra", "[B");
|
257 |
|
|
usb_epDescFID_extralen = (*env)->GetFieldID(env, usb_epDescClazz, "extralen", "I");
|
258 |
|
|
#ifdef DEBUGON
|
259 |
|
|
printf("usb_get_busses: Field initialization done (1)\n");
|
260 |
|
|
#endif
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
//************************************************************************//
|
264 |
|
|
|
265 |
|
|
struct usb_device *dev;
|
266 |
|
|
struct usb_bus *bus;
|
267 |
|
|
|
268 |
|
|
busses = usb_get_busses();
|
269 |
|
|
bus = busses;
|
270 |
|
|
if (!bus){
|
271 |
|
|
return NULL;
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
// objects
|
275 |
|
|
jobject main_usb_busObj, usb_busObj, usb_busObj_next, usb_busObj_prev, \
|
276 |
|
|
main_usb_devObj, usb_devObj, usb_devObj_next, usb_devObj_prev, \
|
277 |
|
|
usb_devDescObj, usb_confDescObj, usb_intObj, usb_intDescObj, \
|
278 |
|
|
usb_epDescObj;
|
279 |
|
|
|
280 |
|
|
jobjectArray usb_confDescObjArray, usb_intObjArray, usb_intDescObjArray, usb_epDescObjArray;
|
281 |
|
|
|
282 |
|
|
usb_busObj = NULL;
|
283 |
|
|
usb_busObj_prev = NULL;
|
284 |
|
|
main_usb_busObj = NULL;
|
285 |
|
|
|
286 |
|
|
#ifdef DEBUGON
|
287 |
|
|
printf("usb_get_busses: usb_get_busses done (2)\n");
|
288 |
|
|
#endif
|
289 |
|
|
|
290 |
|
|
while (bus){
|
291 |
|
|
#ifdef DEBUGON
|
292 |
|
|
printf("\tusb_get_busses: bus %x (3)\n", bus);
|
293 |
|
|
#endif
|
294 |
|
|
|
295 |
|
|
// create a new object for every bus
|
296 |
|
|
if (!usb_busObj) {
|
297 |
|
|
usb_busObj = (*env)->NewObject(env, usb_busClazz, usb_busMid);
|
298 |
|
|
if (!usb_busObj) {
|
299 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_busObj)");
|
300 |
|
|
return NULL;
|
301 |
|
|
}
|
302 |
|
|
main_usb_busObj = usb_busObj;
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
// fill the fields of the object
|
306 |
|
|
usb_busObj_next = NULL;
|
307 |
|
|
if (bus->next){
|
308 |
|
|
usb_busObj_next = (*env)->NewObject(env, usb_busClazz, usb_busMid);
|
309 |
|
|
if (!usb_busObj_next) {
|
310 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_busObj_next)");
|
311 |
|
|
return NULL;
|
312 |
|
|
}
|
313 |
|
|
}
|
314 |
|
|
(*env)->SetObjectField(env, usb_busObj, usb_busFID_next, usb_busObj_next);
|
315 |
|
|
(*env)->SetObjectField(env, usb_busObj, usb_busFID_prev, usb_busObj_prev);
|
316 |
|
|
(*env)->SetObjectField(env, usb_busObj, usb_busFID_dirname, (*env)->NewStringUTF(env, bus->dirname));
|
317 |
|
|
(*env)->SetLongField(env, usb_busObj, usb_busFID_location, bus->location);
|
318 |
|
|
|
319 |
|
|
dev = bus->devices;
|
320 |
|
|
usb_devObj = NULL;
|
321 |
|
|
usb_devObj_prev = NULL;
|
322 |
|
|
main_usb_devObj = NULL;
|
323 |
|
|
|
324 |
|
|
while (dev){
|
325 |
|
|
#ifdef DEBUGON
|
326 |
|
|
printf("\tusb_get_busses: dev %x (4)\n", dev);
|
327 |
|
|
#endif
|
328 |
|
|
// create a new object for every device
|
329 |
|
|
if (!usb_devObj){
|
330 |
|
|
usb_devObj = (*env)->NewObject(env, usb_devClazz, usb_devMid);
|
331 |
|
|
if (!usb_devObj) {
|
332 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_devObj)");
|
333 |
|
|
return NULL;
|
334 |
|
|
}
|
335 |
|
|
main_usb_devObj = usb_devObj;
|
336 |
|
|
}
|
337 |
|
|
// fill the fields of the object
|
338 |
|
|
usb_devObj_next = NULL;
|
339 |
|
|
if (dev->next){
|
340 |
|
|
usb_devObj_next = (*env)->NewObject(env, usb_devClazz, usb_devMid);
|
341 |
|
|
if (!usb_devObj_next) {
|
342 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_devObj_next)");
|
343 |
|
|
return NULL;
|
344 |
|
|
}
|
345 |
|
|
}
|
346 |
|
|
(*env)->SetObjectField(env, usb_devObj, usb_devFID_next, usb_devObj_next);
|
347 |
|
|
(*env)->SetObjectField(env, usb_devObj, usb_devFID_prev, usb_devObj_prev);
|
348 |
|
|
(*env)->SetObjectField(env, usb_devObj, usb_devFID_bus, usb_busObj);
|
349 |
|
|
(*env)->SetObjectField(env, usb_devObj, usb_devFID_filename, (*env)->NewStringUTF(env, dev->filename));
|
350 |
|
|
(*env)->SetByteField(env, usb_devObj, usb_devFID_devnum, dev->devnum);
|
351 |
|
|
(*env)->SetByteField(env, usb_devObj, usb_devFID_num_children, dev->num_children);
|
352 |
|
|
(*env)->SetLongField(env, usb_devObj, usb_devFID_devStructAddr, (jlong) (int) dev);
|
353 |
|
|
|
354 |
|
|
// device descriptor
|
355 |
|
|
usb_devDescObj = (*env)->NewObject(env, usb_devDescClazz, usb_devDescMid);
|
356 |
|
|
if (!usb_devDescObj) {
|
357 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_devDescObj)");
|
358 |
|
|
return NULL;
|
359 |
|
|
}
|
360 |
|
|
(*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bLength, dev->descriptor.bLength);
|
361 |
|
|
(*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bDescriptorType, dev->descriptor.bDescriptorType);
|
362 |
|
|
(*env)->SetShortField(env, usb_devDescObj, usb_devDescFID_bcdUSB, dev->descriptor.bcdUSB);
|
363 |
|
|
(*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bDeviceClass, dev->descriptor.bDeviceClass);
|
364 |
|
|
(*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bDeviceSubClass, dev->descriptor.bDeviceSubClass);
|
365 |
|
|
(*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bDeviceProtocol, dev->descriptor.bDeviceProtocol);
|
366 |
|
|
(*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bMaxPacketSize0, dev->descriptor.bMaxPacketSize0);
|
367 |
|
|
(*env)->SetShortField(env, usb_devDescObj, usb_devDescFID_idVendor, dev->descriptor.idVendor);
|
368 |
|
|
(*env)->SetShortField(env, usb_devDescObj, usb_devDescFID_idProduct, dev->descriptor.idProduct);
|
369 |
|
|
(*env)->SetShortField(env, usb_devDescObj, usb_devDescFID_bcdDevice, dev->descriptor.bcdDevice);
|
370 |
|
|
(*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_iManufacturer, dev->descriptor.iManufacturer);
|
371 |
|
|
(*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_iProduct, dev->descriptor.iProduct);
|
372 |
|
|
(*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_iSerialNumber, dev->descriptor.iSerialNumber);
|
373 |
|
|
(*env)->SetByteField(env, usb_devDescObj, usb_devDescFID_bNumConfigurations, dev->descriptor.bNumConfigurations);
|
374 |
|
|
|
375 |
|
|
(*env)->SetObjectField(env, usb_devObj, usb_devFID_descriptor, usb_devDescObj);
|
376 |
|
|
// configuration descriptor
|
377 |
|
|
// Loop through all of the configurations
|
378 |
|
|
usb_confDescObjArray = (jobjectArray) (*env)->NewObjectArray(env, dev->descriptor.bNumConfigurations, usb_confDescClazz, NULL);
|
379 |
|
|
if (!usb_confDescObjArray) {
|
380 |
|
|
setLibusbJavaError("shared library error: Error NewObject 6");
|
381 |
|
|
return NULL;
|
382 |
|
|
}
|
383 |
|
|
for (int c = 0; c < dev->descriptor.bNumConfigurations; c++){
|
384 |
|
|
#ifdef DEBUGON
|
385 |
|
|
printf("\t\tusb_get_busses: configuration %x (5)\n", c);
|
386 |
|
|
#endif
|
387 |
|
|
if (dev->config == NULL) {
|
388 |
|
|
// this shouldn't happen, but it did occasionally (maybe this is (or probably was) a libusb bug)
|
389 |
|
|
setLibusbJavaError("shared library error: dev->config == NULL");
|
390 |
|
|
return main_usb_busObj;
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
usb_confDescObj = (*env)->NewObject(env, usb_confDescClazz, usb_confDescMid);
|
394 |
|
|
if (!usb_confDescObj) {
|
395 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_confDescObj)");
|
396 |
|
|
return NULL;
|
397 |
|
|
}
|
398 |
|
|
(*env)->SetObjectArrayElement(env, usb_confDescObjArray, c, usb_confDescObj);
|
399 |
|
|
(*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_bLength, dev->config[c].bLength);
|
400 |
|
|
(*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_bDescriptorType, dev->config[c].bDescriptorType);
|
401 |
|
|
(*env)->SetShortField(env, usb_confDescObj, usb_confDescFID_wTotalLength, dev->config[c].wTotalLength);
|
402 |
|
|
(*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_bNumInterfaces, dev->config[c].bNumInterfaces);
|
403 |
|
|
(*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_bConfigurationValue, dev->config[c].bConfigurationValue);
|
404 |
|
|
(*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_iConfiguration, dev->config[c].iConfiguration);
|
405 |
|
|
(*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_bmAttributes, dev->config[c].bmAttributes);
|
406 |
|
|
(*env)->SetByteField(env, usb_confDescObj, usb_confDescFID_MaxPower, dev->config[c].MaxPower);
|
407 |
|
|
(*env)->SetIntField(env, usb_confDescObj, usb_confDescFID_extralen, dev->config[c].extralen);
|
408 |
|
|
if (dev->config[c].extra){
|
409 |
|
|
jbyteArray jbExtraDesc = (*env)->NewByteArray(env, dev->config[c].extralen);
|
410 |
|
|
(*env)->SetByteArrayRegion(env, jbExtraDesc, 0, dev->config[c].extralen, (jbyte *) dev->config[c].extra);
|
411 |
|
|
(*env)->SetObjectField(env, usb_confDescObj, usb_confDescFID_extra, jbExtraDesc);
|
412 |
|
|
} else {
|
413 |
|
|
(*env)->SetObjectField(env, usb_confDescObj, usb_confDescFID_extra, NULL);
|
414 |
|
|
}
|
415 |
|
|
// interface
|
416 |
|
|
usb_intObjArray = (jobjectArray) (*env)->NewObjectArray(env, dev->config[c].bNumInterfaces, usb_intClazz, NULL);
|
417 |
|
|
if (!usb_intObjArray) {
|
418 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_intObjArray)");
|
419 |
|
|
return NULL;
|
420 |
|
|
}
|
421 |
|
|
for (int i = 0; i < dev->config[c].bNumInterfaces; i++){
|
422 |
|
|
#ifdef DEBUGON
|
423 |
|
|
printf("\t\t\tusb_get_busses: interface %x (6)\n", i);
|
424 |
|
|
#endif
|
425 |
|
|
if (dev->config[c].interface == NULL) {
|
426 |
|
|
// this shouldn't happen
|
427 |
|
|
printf("dev->config[c].interface == NULL");
|
428 |
|
|
return main_usb_busObj;
|
429 |
|
|
}
|
430 |
|
|
|
431 |
|
|
usb_intObj = (*env)->NewObject(env, usb_intClazz, usb_intMid);
|
432 |
|
|
if (!usb_intObj) {
|
433 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_intObj)");
|
434 |
|
|
return NULL;
|
435 |
|
|
}
|
436 |
|
|
(*env)->SetObjectArrayElement(env, usb_intObjArray, i, usb_intObj);
|
437 |
|
|
(*env)->SetIntField(env, usb_intObj, usb_intFID_num_altsetting, dev->config[c].interface[i].num_altsetting);
|
438 |
|
|
// interface descriptor
|
439 |
|
|
usb_intDescObjArray = (jobjectArray) (*env)->NewObjectArray(env, dev->config[c].interface[i].num_altsetting, usb_intDescClazz, NULL);
|
440 |
|
|
if (!usb_intDescObjArray) {
|
441 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_intDescObjArray)");
|
442 |
|
|
return NULL;
|
443 |
|
|
}
|
444 |
|
|
for (int a = 0; a < dev->config[c].interface[i].num_altsetting; a++){
|
445 |
|
|
#ifdef DEBUGON
|
446 |
|
|
printf("\t\t\t\tusb_get_busses: interface descriptor %x (7)\n", a);
|
447 |
|
|
#endif
|
448 |
|
|
if (dev->config[c].interface[i].altsetting == NULL) {
|
449 |
|
|
// this shouldn't happen
|
450 |
|
|
printf("LibusbJava: usb_get_busses: dev->config[c].interface[i].altsetting == NULL\n");
|
451 |
|
|
return main_usb_busObj;
|
452 |
|
|
}
|
453 |
|
|
|
454 |
|
|
usb_intDescObj = (*env)->NewObject(env, usb_intDescClazz, usb_intDescMid);
|
455 |
|
|
if (!usb_intDescObj) {
|
456 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_intDescObj)");
|
457 |
|
|
return NULL;
|
458 |
|
|
}
|
459 |
|
|
(*env)->SetObjectArrayElement(env, usb_intDescObjArray, a, usb_intDescObj);
|
460 |
|
|
(*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bLength, dev->config[c].interface[i].altsetting[a].bLength);
|
461 |
|
|
(*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bDescriptorType, dev->config[c].interface[i].altsetting[a].bDescriptorType);
|
462 |
|
|
(*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bInterfaceNumber, dev->config[c].interface[i].altsetting[a].bInterfaceNumber);
|
463 |
|
|
(*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bAlternateSetting, dev->config[c].interface[i].altsetting[a].bAlternateSetting);
|
464 |
|
|
(*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bNumEndpoints, dev->config[c].interface[i].altsetting[a].bNumEndpoints);
|
465 |
|
|
(*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bInterfaceClass, dev->config[c].interface[i].altsetting[a].bInterfaceClass);
|
466 |
|
|
(*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bInterfaceSubClass, dev->config[c].interface[i].altsetting[a].bInterfaceSubClass);
|
467 |
|
|
(*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_bInterfaceProtocol, dev->config[c].interface[i].altsetting[a].bInterfaceProtocol);
|
468 |
|
|
(*env)->SetByteField(env, usb_intDescObj, usb_intDescFID_iInterface, dev->config[c].interface[i].altsetting[a].iInterface);
|
469 |
|
|
(*env)->SetIntField(env, usb_intDescObj, usb_intDescFID_extralen, dev->config[c].interface[i].altsetting[a].extralen);
|
470 |
|
|
if (dev->config[c].interface[i].altsetting[a].extra){
|
471 |
|
|
jbyteArray jbExtraDesc = (*env)->NewByteArray(env, dev->config[c].interface[i].altsetting[a].extralen);
|
472 |
|
|
(*env)->SetByteArrayRegion(env, jbExtraDesc, 0, dev->config[c].interface[i].altsetting[a].extralen, (jbyte *) dev->config[c].interface[i].altsetting[a].extra);
|
473 |
|
|
(*env)->SetObjectField(env, usb_intDescObj, usb_intDescFID_extra, jbExtraDesc);
|
474 |
|
|
} else {
|
475 |
|
|
(*env)->SetObjectField(env, usb_intDescObj, usb_intDescFID_extra, NULL);
|
476 |
|
|
}
|
477 |
|
|
// endpoint descriptor
|
478 |
|
|
usb_epDescObjArray = (jobjectArray) (*env)->NewObjectArray(env, dev->config[c].interface[i].altsetting[a].bNumEndpoints, usb_epDescClazz, NULL);
|
479 |
|
|
if (!usb_epDescObjArray) {
|
480 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_epDescObjArray)");
|
481 |
|
|
return NULL;
|
482 |
|
|
}
|
483 |
|
|
for (int e = 0; e < dev->config[c].interface[i].altsetting[a].bNumEndpoints; e++){
|
484 |
|
|
#ifdef DEBUGON
|
485 |
|
|
printf("\t\t\t\t\tusb_get_busses: endpoint descriptor %x (8)\n", e);
|
486 |
|
|
#endif
|
487 |
|
|
if (dev->config[c].interface[i].altsetting[a].endpoint == NULL) {
|
488 |
|
|
printf("LibusbJava: usb_get_busses: dev->config[c].interface[i].altsetting[a].endpoint == NULL\n");
|
489 |
|
|
return main_usb_busObj;
|
490 |
|
|
}
|
491 |
|
|
|
492 |
|
|
usb_epDescObj = (*env)->NewObject(env, usb_epDescClazz, usb_epDescMid);
|
493 |
|
|
if (!usb_epDescObj) {
|
494 |
|
|
setLibusbJavaError("shared library error: Error NewObject (usb_epDescObj)");
|
495 |
|
|
return NULL;
|
496 |
|
|
}
|
497 |
|
|
(*env)->SetObjectArrayElement(env, usb_epDescObjArray, e, usb_epDescObj);
|
498 |
|
|
(*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bLength, dev->config[c].interface[i].altsetting[a].endpoint[e].bLength);
|
499 |
|
|
(*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bDescriptorType, dev->config[c].interface[i].altsetting[a].endpoint[e].bDescriptorType);
|
500 |
|
|
(*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bEndpointAddress, dev->config[c].interface[i].altsetting[a].endpoint[e].bEndpointAddress);
|
501 |
|
|
(*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bmAttributes, dev->config[c].interface[i].altsetting[a].endpoint[e].bmAttributes);
|
502 |
|
|
(*env)->SetShortField(env, usb_epDescObj, usb_epDescFID_wMaxPacketSize, dev->config[c].interface[i].altsetting[a].endpoint[e].wMaxPacketSize);
|
503 |
|
|
(*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bInterval, dev->config[c].interface[i].altsetting[a].endpoint[e].bInterval);
|
504 |
|
|
(*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bRefresh, dev->config[c].interface[i].altsetting[a].endpoint[e].bRefresh);
|
505 |
|
|
(*env)->SetByteField(env, usb_epDescObj, usb_epDescFID_bSynchAddress, dev->config[c].interface[i].altsetting[a].endpoint[e].bSynchAddress);
|
506 |
|
|
(*env)->SetIntField(env, usb_epDescObj, usb_epDescFID_extralen, dev->config[c].interface[i].altsetting[a].endpoint[e].extralen);
|
507 |
|
|
if (dev->config[c].interface[i].altsetting[a].endpoint[e].extra){
|
508 |
|
|
jbyteArray jbExtraDesc = (*env)->NewByteArray(env, dev->config[c].interface[i].altsetting[a].endpoint[e].extralen);
|
509 |
|
|
(*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);
|
510 |
|
|
(*env)->SetObjectField(env, usb_epDescObj, usb_epDescFID_extra, jbExtraDesc);
|
511 |
|
|
} else {
|
512 |
|
|
(*env)->SetObjectField(env, usb_epDescObj, usb_epDescFID_extra, NULL);
|
513 |
|
|
}
|
514 |
|
|
}
|
515 |
|
|
(*env)->SetObjectField(env, usb_intDescObj, usb_intDescFID_endpoint, usb_epDescObjArray);
|
516 |
|
|
}
|
517 |
|
|
(*env)->SetObjectField(env, usb_intObj, usb_intFID_altsetting, usb_intDescObjArray);
|
518 |
|
|
}
|
519 |
|
|
(*env)->SetObjectField(env, usb_confDescObj, usb_confDescFID_interface_, usb_intObjArray);
|
520 |
|
|
}
|
521 |
|
|
|
522 |
|
|
|
523 |
|
|
(*env)->SetObjectField(env, usb_devObj, usb_devFID_config, usb_confDescObjArray);
|
524 |
|
|
|
525 |
|
|
usb_devObj_prev = usb_devObj;
|
526 |
|
|
usb_devObj = usb_devObj_next;
|
527 |
|
|
dev = dev->next;
|
528 |
|
|
}
|
529 |
|
|
(*env)->SetObjectField(env, usb_busObj, usb_busFID_devices, main_usb_devObj);
|
530 |
|
|
(*env)->SetObjectField(env, usb_busObj, usb_busFID_root_dev, main_usb_devObj);
|
531 |
|
|
|
532 |
|
|
usb_busObj_prev = usb_busObj;
|
533 |
|
|
usb_busObj = usb_busObj_next;
|
534 |
|
|
bus = bus->next;
|
535 |
|
|
}
|
536 |
|
|
|
537 |
|
|
#ifdef DEBUGON
|
538 |
|
|
printf("usb_get_busses: done\n");
|
539 |
|
|
#endif
|
540 |
|
|
return main_usb_busObj;
|
541 |
|
|
}
|
542 |
|
|
|
543 |
|
|
/*
|
544 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
545 |
|
|
* Method: usb_open
|
546 |
|
|
* Signature: (Lch/ntb/usb/Usb_Device;)I
|
547 |
|
|
*/
|
548 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1open
|
549 |
|
|
(JNIEnv *env, jclass obj, jobject dev)
|
550 |
|
|
{
|
551 |
|
|
clearLibusbJavaError();
|
552 |
|
|
if (busses == NULL) {
|
553 |
|
|
setLibusbJavaError("shared library error: busses is null");
|
554 |
|
|
return 0;
|
555 |
|
|
}
|
556 |
|
|
|
557 |
|
|
jlong usb_device_cmp_addr = (*env)->GetLongField(env, dev, usb_devFID_devStructAddr);
|
558 |
|
|
struct usb_bus *tmpBus;
|
559 |
|
|
|
560 |
|
|
for (tmpBus = busses; tmpBus; tmpBus = tmpBus->next) {
|
561 |
|
|
struct usb_device *device;
|
562 |
|
|
for (device = tmpBus->devices; device; device = device->next) {
|
563 |
|
|
if ((jlong) (int) device == usb_device_cmp_addr){
|
564 |
|
|
return (jint) usb_open(device);
|
565 |
|
|
}
|
566 |
|
|
}
|
567 |
|
|
}
|
568 |
|
|
setLibusbJavaError("shared library error: no device with dev.devnum found on busses");
|
569 |
|
|
return 0;
|
570 |
|
|
}
|
571 |
|
|
|
572 |
|
|
/*
|
573 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
574 |
|
|
* Method: usb_close
|
575 |
|
|
* Signature: (I)I
|
576 |
|
|
*/
|
577 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1close
|
578 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle)
|
579 |
|
|
{
|
580 |
|
|
clearLibusbJavaError();
|
581 |
|
|
return (jint) usb_close((usb_dev_handle *) dev_handle);
|
582 |
|
|
}
|
583 |
|
|
|
584 |
|
|
/*
|
585 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
586 |
|
|
* Method: usb_set_configuration
|
587 |
|
|
* Signature: (II)I
|
588 |
|
|
*/
|
589 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1set_1configuration
|
590 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint configuration)
|
591 |
|
|
{
|
592 |
|
|
clearLibusbJavaError();
|
593 |
|
|
return usb_set_configuration((usb_dev_handle *) dev_handle, configuration);
|
594 |
|
|
}
|
595 |
|
|
|
596 |
|
|
/*
|
597 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
598 |
|
|
* Method: usb_set_altinterface
|
599 |
|
|
* Signature: (II)I
|
600 |
|
|
*/
|
601 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1set_1altinterface
|
602 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint alternate)
|
603 |
|
|
{
|
604 |
|
|
clearLibusbJavaError();
|
605 |
|
|
return usb_set_altinterface((usb_dev_handle *) dev_handle, alternate);
|
606 |
|
|
}
|
607 |
|
|
|
608 |
|
|
/*
|
609 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
610 |
|
|
* Method: usb_clear_halt
|
611 |
|
|
* Signature: (II)I
|
612 |
|
|
*/
|
613 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1clear_1halt
|
614 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint ep)
|
615 |
|
|
{
|
616 |
|
|
clearLibusbJavaError();
|
617 |
|
|
return usb_clear_halt((usb_dev_handle *) dev_handle, (unsigned) ep);
|
618 |
|
|
}
|
619 |
|
|
|
620 |
|
|
/*
|
621 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
622 |
|
|
* Method: usb_reset
|
623 |
|
|
* Signature: (I)I
|
624 |
|
|
*/
|
625 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1reset
|
626 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle)
|
627 |
|
|
{
|
628 |
|
|
clearLibusbJavaError();
|
629 |
|
|
return usb_reset((usb_dev_handle *) dev_handle);
|
630 |
|
|
}
|
631 |
|
|
|
632 |
|
|
/*
|
633 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
634 |
|
|
* Method: usb_claim_interface
|
635 |
|
|
* Signature: (II)I
|
636 |
|
|
*/
|
637 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1claim_1interface
|
638 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint interface)
|
639 |
|
|
{
|
640 |
|
|
clearLibusbJavaError();
|
641 |
|
|
return usb_claim_interface((usb_dev_handle *) dev_handle, interface);
|
642 |
|
|
}
|
643 |
|
|
|
644 |
|
|
/*
|
645 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
646 |
|
|
* Method: usb_release_interface
|
647 |
|
|
* Signature: (II)I
|
648 |
|
|
*/
|
649 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1release_1interface
|
650 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint interface)
|
651 |
|
|
{
|
652 |
|
|
clearLibusbJavaError();
|
653 |
|
|
return usb_release_interface((usb_dev_handle *) dev_handle, interface);
|
654 |
|
|
}
|
655 |
|
|
|
656 |
|
|
/*
|
657 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
658 |
|
|
* Method: usb_control_msg
|
659 |
|
|
* Signature: (IIIII[BII)I
|
660 |
|
|
*/
|
661 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1control_1msg
|
662 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint requesttype, jint request, jint value, jint index, jbyteArray jbytes, jint size, jint timeout)
|
663 |
|
|
{
|
664 |
|
|
clearLibusbJavaError();
|
665 |
|
|
jbyte *bytes = (*env)->GetByteArrayElements(env, jbytes, NULL);
|
666 |
|
|
int num_bytes = usb_control_msg((usb_dev_handle *) dev_handle, requesttype, request, value, index, (char *) bytes, size, timeout);
|
667 |
|
|
(*env)->SetByteArrayRegion(env, jbytes, 0, size, bytes);
|
668 |
|
|
(*env)->ReleaseByteArrayElements(env, jbytes, bytes, 0);
|
669 |
|
|
return num_bytes;
|
670 |
|
|
}
|
671 |
|
|
|
672 |
|
|
/*
|
673 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
674 |
|
|
* Method: usb_get_string
|
675 |
|
|
* Signature: (III)Ljava/lang/String;
|
676 |
|
|
*/
|
677 |
|
|
JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1string
|
678 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint index, jint langid)
|
679 |
|
|
{
|
680 |
|
|
clearLibusbJavaError();
|
681 |
|
|
char string[256];
|
682 |
|
|
int retVal = usb_get_string((usb_dev_handle *) dev_handle, index, langid, string, 256);
|
683 |
|
|
if (retVal > 0)
|
684 |
|
|
return (*env)->NewStringUTF(env, string);
|
685 |
|
|
return 0;
|
686 |
|
|
}
|
687 |
|
|
|
688 |
|
|
/*
|
689 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
690 |
|
|
* Method: usb_get_string_simple
|
691 |
|
|
* Signature: (II)Ljava/lang/String;
|
692 |
|
|
*/
|
693 |
|
|
JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1string_1simple
|
694 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint index)
|
695 |
|
|
{
|
696 |
|
|
clearLibusbJavaError();
|
697 |
|
|
char string[256];
|
698 |
|
|
int retVal = usb_get_string_simple((usb_dev_handle *) dev_handle, index, string, 256);
|
699 |
|
|
if (retVal > 0)
|
700 |
|
|
return (*env)->NewStringUTF(env, string);
|
701 |
|
|
return 0;
|
702 |
|
|
}
|
703 |
|
|
|
704 |
|
|
/*
|
705 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
706 |
|
|
* Method: usb_get_descriptor
|
707 |
|
|
* Signature: (IBBI)Ljava/lang/String;
|
708 |
|
|
*/
|
709 |
|
|
JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1descriptor
|
710 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jbyte type, jbyte index, jint size)
|
711 |
|
|
{
|
712 |
|
|
clearLibusbJavaError();
|
713 |
|
|
char *string = (char *) malloc(size * sizeof(char));
|
714 |
|
|
int retVal = usb_get_descriptor((usb_dev_handle *) dev_handle, (unsigned) type,
|
715 |
|
|
(unsigned) index, string, size);
|
716 |
|
|
if (retVal > 0)
|
717 |
|
|
return (*env)->NewStringUTF(env, string);
|
718 |
|
|
return 0;
|
719 |
|
|
}
|
720 |
|
|
|
721 |
|
|
/*
|
722 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
723 |
|
|
* Method: usb_get_descriptor_by_endpoint
|
724 |
|
|
* Signature: (IIBBI)Ljava/lang/String;
|
725 |
|
|
*/
|
726 |
|
|
JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1get_1descriptor_1by_1endpoint
|
727 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint ep, jbyte type, jbyte index, jint size)
|
728 |
|
|
{
|
729 |
|
|
clearLibusbJavaError();
|
730 |
|
|
char *string = (char *) malloc(size * sizeof(char));
|
731 |
|
|
int retVal = usb_get_descriptor_by_endpoint((usb_dev_handle *) dev_handle, ep, (unsigned) type,
|
732 |
|
|
(unsigned) index, string, size);
|
733 |
|
|
if (retVal > 0)
|
734 |
|
|
return (*env)->NewStringUTF(env, string);
|
735 |
|
|
return 0;
|
736 |
|
|
}
|
737 |
|
|
|
738 |
|
|
/*
|
739 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
740 |
|
|
* Method: usb_bulk_write
|
741 |
|
|
* Signature: (II[BII)I
|
742 |
|
|
*/
|
743 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1bulk_1write
|
744 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout)
|
745 |
|
|
{
|
746 |
|
|
clearLibusbJavaError();
|
747 |
|
|
jbyte *bytes = (*env)->GetByteArrayElements(env, jbytes, NULL);
|
748 |
|
|
int num_bytes = usb_bulk_write((usb_dev_handle *) dev_handle, ep, (char *) bytes, size, timeout);
|
749 |
|
|
(*env)->ReleaseByteArrayElements(env, jbytes, bytes, 0);
|
750 |
|
|
return num_bytes;
|
751 |
|
|
}
|
752 |
|
|
|
753 |
|
|
/*
|
754 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
755 |
|
|
* Method: usb_bulk_read
|
756 |
|
|
* Signature: (II[BII)I
|
757 |
|
|
*/
|
758 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1bulk_1read
|
759 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout)
|
760 |
|
|
{
|
761 |
|
|
clearLibusbJavaError();
|
762 |
|
|
char *bytes = (char *) malloc(size * sizeof(char));
|
763 |
|
|
int num_bytes = usb_bulk_read((usb_dev_handle *) dev_handle, ep, bytes, size, timeout);
|
764 |
|
|
if (!bytes) { return num_bytes; }
|
765 |
|
|
(*env)->SetByteArrayRegion(env, jbytes, 0, size, (jbyte *) bytes);
|
766 |
|
|
free(bytes);
|
767 |
|
|
return num_bytes;
|
768 |
|
|
}
|
769 |
|
|
|
770 |
|
|
/*
|
771 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
772 |
|
|
* Method: usb_interrupt_write
|
773 |
|
|
* Signature: (II[BII)I
|
774 |
|
|
*/
|
775 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1interrupt_1write
|
776 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout)
|
777 |
|
|
{
|
778 |
|
|
clearLibusbJavaError();
|
779 |
|
|
jbyte *bytes = (*env)->GetByteArrayElements(env, jbytes, NULL);
|
780 |
|
|
int num_bytes = usb_interrupt_write( (usb_dev_handle *) dev_handle, ep, (char *) bytes, size, timeout);
|
781 |
|
|
(*env)->ReleaseByteArrayElements(env, jbytes, bytes, 0);
|
782 |
|
|
return num_bytes;
|
783 |
|
|
}
|
784 |
|
|
|
785 |
|
|
/*
|
786 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
787 |
|
|
* Method: usb_interrupt_read
|
788 |
|
|
* Signature: (II[BII)I
|
789 |
|
|
*/
|
790 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1interrupt_1read
|
791 |
|
|
(JNIEnv *env, jclass obj, jint dev_handle, jint ep, jbyteArray jbytes, jint size, jint timeout)
|
792 |
|
|
{
|
793 |
|
|
clearLibusbJavaError();
|
794 |
|
|
char *bytes = (char *) malloc(size * sizeof(char));
|
795 |
|
|
int num_bytes = usb_interrupt_read((usb_dev_handle *) dev_handle, ep, bytes, size, timeout);
|
796 |
|
|
if (!bytes) { return num_bytes; }
|
797 |
|
|
(*env)->SetByteArrayRegion(env, jbytes, 0, size, (jbyte *) bytes);
|
798 |
|
|
free(bytes);
|
799 |
|
|
return num_bytes;
|
800 |
|
|
}
|
801 |
|
|
|
802 |
|
|
/*
|
803 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
804 |
|
|
* Method: usb_strerror
|
805 |
|
|
* Signature: ()Ljava/lang/String;
|
806 |
|
|
*/
|
807 |
|
|
JNIEXPORT jstring JNICALL Java_ch_ntb_usb_LibusbJava_usb_1strerror
|
808 |
|
|
(JNIEnv *env, jclass obj){
|
809 |
|
|
|
810 |
|
|
char *str;
|
811 |
|
|
// check for LibusbJava specific errors first
|
812 |
|
|
if (libusbJavaError != NULL) {
|
813 |
|
|
str = libusbJavaError;
|
814 |
|
|
clearLibusbJavaError();
|
815 |
|
|
} else {
|
816 |
|
|
str = usb_strerror();
|
817 |
|
|
}
|
818 |
|
|
|
819 |
|
|
return (*env)->NewStringUTF(env, str);
|
820 |
|
|
}
|
821 |
|
|
|
822 |
|
|
/*
|
823 |
|
|
* Class: ch_ntb_usb_LibusbJava
|
824 |
|
|
* Method: usb_error_no
|
825 |
|
|
* Signature: (I)I
|
826 |
|
|
*/
|
827 |
|
|
JNIEXPORT jint JNICALL Java_ch_ntb_usb_LibusbJava_usb_1error_1no
|
828 |
|
|
(JNIEnv *env, jclass obj, jint java_error_no){
|
829 |
|
|
|
830 |
|
|
switch (java_error_no) {
|
831 |
|
|
case 0:
|
832 |
|
|
return 0;
|
833 |
|
|
case 1:
|
834 |
|
|
return EBADF;
|
835 |
|
|
case 2:
|
836 |
|
|
return ENXIO;
|
837 |
|
|
case 3:
|
838 |
|
|
return EBUSY;
|
839 |
|
|
case 4:
|
840 |
|
|
return EINVAL;
|
841 |
|
|
case 5:
|
842 |
|
|
return ETIMEDOUT;
|
843 |
|
|
case 6:
|
844 |
|
|
return EIO;
|
845 |
|
|
case 7:
|
846 |
|
|
return ENOMEM;
|
847 |
|
|
default:
|
848 |
|
|
return 100000;
|
849 |
|
|
}
|
850 |
|
|
}
|