1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "UsbDeviceConnectionJNI"
18
19 #include <fcntl.h>
20 #include <nativehelper/JNIPlatformHelp.h>
21 #include <stdio.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <usbhost/usbhost.h>
25 #include <usbhost/usbhost_jni.h>
26
27 #include <chrono>
28
29 #include "core_jni_helpers.h"
30 #include "jni.h"
31 #include "utils/Log.h"
32
33 using namespace android;
34 using namespace std::chrono;
35
36 static const int USB_CONTROL_READ_TIMEOUT_MS = 200;
37
38 static jfieldID field_context;
39
get_device_from_object(JNIEnv * env,jobject connection)40 struct usb_device* get_device_from_object(JNIEnv* env, jobject connection)
41 {
42 return (struct usb_device*)env->GetLongField(connection, field_context);
43 }
44
45 static jboolean
android_hardware_UsbDeviceConnection_open(JNIEnv * env,jobject thiz,jstring deviceName,jobject fileDescriptor)46 android_hardware_UsbDeviceConnection_open(JNIEnv *env, jobject thiz, jstring deviceName,
47 jobject fileDescriptor)
48 {
49 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
50 // duplicate the file descriptor, since ParcelFileDescriptor will eventually close its copy
51 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
52 if (fd < 0)
53 return JNI_FALSE;
54
55 const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
56 struct usb_device* device = usb_device_new(deviceNameStr, fd);
57 if (device) {
58 env->SetLongField(thiz, field_context, (jlong)device);
59 } else {
60 ALOGE("usb_device_open failed for %s", deviceNameStr);
61 close(fd);
62 }
63
64 env->ReleaseStringUTFChars(deviceName, deviceNameStr);
65 return (device != NULL) ? JNI_TRUE : JNI_FALSE;
66 }
67
68 static void
android_hardware_UsbDeviceConnection_close(JNIEnv * env,jobject thiz)69 android_hardware_UsbDeviceConnection_close(JNIEnv *env, jobject thiz)
70 {
71 ALOGD("close\n");
72 struct usb_device* device = get_device_from_object(env, thiz);
73 if (device) {
74 usb_device_close(device);
75 env->SetLongField(thiz, field_context, 0);
76 }
77 }
78
79 static jint
android_hardware_UsbDeviceConnection_get_fd(JNIEnv * env,jobject thiz)80 android_hardware_UsbDeviceConnection_get_fd(JNIEnv *env, jobject thiz)
81 {
82 struct usb_device* device = get_device_from_object(env, thiz);
83 if (!device) {
84 ALOGE("device is closed in native_get_fd");
85 return -1;
86 }
87 return usb_device_get_fd(device);
88 }
89
90 static jbyteArray
android_hardware_UsbDeviceConnection_get_desc(JNIEnv * env,jobject thiz)91 android_hardware_UsbDeviceConnection_get_desc(JNIEnv *env, jobject thiz)
92 {
93 int fd = android_hardware_UsbDeviceConnection_get_fd(env, thiz);
94 return usb_jni_read_descriptors(env, fd);
95 }
96
97 static jboolean
android_hardware_UsbDeviceConnection_claim_interface(JNIEnv * env,jobject thiz,jint interfaceID,jboolean force)98 android_hardware_UsbDeviceConnection_claim_interface(JNIEnv *env, jobject thiz,
99 jint interfaceID, jboolean force)
100 {
101 struct usb_device* device = get_device_from_object(env, thiz);
102 if (!device) {
103 ALOGE("device is closed in native_claim_interface");
104 return JNI_FALSE;
105 }
106
107 int ret = usb_device_claim_interface(device, interfaceID);
108 if (ret && force && errno == EBUSY) {
109 // disconnect kernel driver and try again
110 usb_device_connect_kernel_driver(device, interfaceID, false);
111 ret = usb_device_claim_interface(device, interfaceID);
112 }
113 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
114 }
115
116 static jboolean
android_hardware_UsbDeviceConnection_release_interface(JNIEnv * env,jobject thiz,jint interfaceID)117 android_hardware_UsbDeviceConnection_release_interface(JNIEnv *env, jobject thiz, jint interfaceID)
118 {
119 struct usb_device* device = get_device_from_object(env, thiz);
120 if (!device) {
121 ALOGE("device is closed in native_release_interface");
122 return JNI_FALSE;
123 }
124 int ret = usb_device_release_interface(device, interfaceID);
125 if (ret == 0) {
126 // allow kernel to reconnect its driver
127 usb_device_connect_kernel_driver(device, interfaceID, true);
128 }
129 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
130 }
131
132 static jboolean
android_hardware_UsbDeviceConnection_set_interface(JNIEnv * env,jobject thiz,jint interfaceID,jint alternateSetting)133 android_hardware_UsbDeviceConnection_set_interface(JNIEnv *env, jobject thiz, jint interfaceID,
134 jint alternateSetting)
135 {
136 struct usb_device* device = get_device_from_object(env, thiz);
137 if (!device) {
138 ALOGE("device is closed in native_set_interface");
139 return JNI_FALSE;
140 }
141 int ret = usb_device_set_interface(device, interfaceID, alternateSetting);
142 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
143 }
144
145 static jboolean
android_hardware_UsbDeviceConnection_set_configuration(JNIEnv * env,jobject thiz,jint configurationID)146 android_hardware_UsbDeviceConnection_set_configuration(JNIEnv *env, jobject thiz, jint configurationID)
147 {
148 struct usb_device* device = get_device_from_object(env, thiz);
149 if (!device) {
150 ALOGE("device is closed in native_set_configuration");
151 return JNI_FALSE;
152 }
153 int ret = usb_device_set_configuration(device, configurationID);
154 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
155 }
156
157 static jint
android_hardware_UsbDeviceConnection_control_request(JNIEnv * env,jobject thiz,jint requestType,jint request,jint value,jint index,jbyteArray buffer,jint start,jint length,jint timeout)158 android_hardware_UsbDeviceConnection_control_request(JNIEnv *env, jobject thiz,
159 jint requestType, jint request, jint value, jint index,
160 jbyteArray buffer, jint start, jint length, jint timeout)
161 {
162 struct usb_device* device = get_device_from_object(env, thiz);
163 if (!device) {
164 ALOGE("device is closed in native_control_request");
165 return -1;
166 }
167
168 jbyte* bufferBytes = NULL;
169 if (buffer) {
170 bufferBytes = (jbyte*)env->GetPrimitiveArrayCritical(buffer, NULL);
171 }
172
173 jint result = usb_device_control_transfer(device, requestType, request,
174 value, index, bufferBytes + start, length, timeout);
175
176 if (bufferBytes) {
177 env->ReleasePrimitiveArrayCritical(buffer, bufferBytes, 0);
178 }
179
180 return result;
181 }
182
183 static jint
android_hardware_UsbDeviceConnection_bulk_request(JNIEnv * env,jobject thiz,jint endpoint,jbyteArray buffer,jint start,jint length,jint timeout)184 android_hardware_UsbDeviceConnection_bulk_request(JNIEnv *env, jobject thiz,
185 jint endpoint, jbyteArray buffer, jint start, jint length, jint timeout)
186 {
187 struct usb_device* device = get_device_from_object(env, thiz);
188 if (!device) {
189 ALOGE("device is closed in native_control_request");
190 return -1;
191 }
192
193 jbyte* bufferBytes = NULL;
194 if (buffer) {
195 bufferBytes = (jbyte*)env->GetPrimitiveArrayCritical(buffer, NULL);
196 }
197
198 jint result = usb_device_bulk_transfer(device, endpoint, bufferBytes + start, length, timeout);
199
200 if (bufferBytes) {
201 env->ReleasePrimitiveArrayCritical(buffer, bufferBytes, 0);
202 }
203
204 return result;
205 }
206
207 static jobject
android_hardware_UsbDeviceConnection_request_wait(JNIEnv * env,jobject thiz,jlong timeoutMillis)208 android_hardware_UsbDeviceConnection_request_wait(JNIEnv *env, jobject thiz, jlong timeoutMillis)
209 {
210 struct usb_device* device = get_device_from_object(env, thiz);
211 if (!device) {
212 ALOGE("device is closed in native_request_wait");
213 return NULL;
214 }
215
216 struct usb_request* request;
217 if (timeoutMillis == -1) {
218 request = usb_request_wait(device, -1);
219 } else {
220 steady_clock::time_point currentTime = steady_clock::now();
221 steady_clock::time_point endTime = currentTime + std::chrono::milliseconds(timeoutMillis);
222
223 // Poll the existence of a request via usb_request_wait until we get a result, an unexpected
224 // error or time out. As several threads can listen on the same fd, we might get wakeups
225 // without data.
226 while (1) {
227 request = usb_request_wait(device, duration_cast<std::chrono::milliseconds>(endTime
228 - currentTime).count());
229
230 int error = errno;
231 if (request != NULL) {
232 break;
233 }
234
235 currentTime = steady_clock::now();
236 if (currentTime >= endTime) {
237 jniThrowException(env, "java/util/concurrent/TimeoutException", "");
238 break;
239 }
240
241 if (error != EAGAIN) {
242 break;
243 }
244 };
245 }
246
247 if (request) {
248 return (jobject)request->client_data;
249 } else {
250 return NULL;
251 }
252 }
253
254 static jstring
android_hardware_UsbDeviceConnection_get_serial(JNIEnv * env,jobject thiz)255 android_hardware_UsbDeviceConnection_get_serial(JNIEnv *env, jobject thiz)
256 {
257 struct usb_device* device = get_device_from_object(env, thiz);
258 if (!device) {
259 ALOGE("device is closed in native_get_serial");
260 return NULL;
261 }
262 char* serial = usb_device_get_serial(device,
263 USB_CONTROL_READ_TIMEOUT_MS);
264 if (!serial)
265 return NULL;
266 jstring result = env->NewStringUTF(serial);
267 free(serial);
268 return result;
269 }
270
271 static jboolean
android_hardware_UsbDeviceConnection_reset_device(JNIEnv * env,jobject thiz)272 android_hardware_UsbDeviceConnection_reset_device(JNIEnv *env, jobject thiz)
273 {
274 struct usb_device* device = get_device_from_object(env, thiz);
275 if (!device) {
276 ALOGE("device is closed in native_reset_device");
277 return JNI_FALSE;
278 }
279 int ret = usb_device_reset(device);
280 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
281 }
282
283 static const JNINativeMethod method_table[] = {
284 {"native_open", "(Ljava/lang/String;Ljava/io/FileDescriptor;)Z",
285 (void *)android_hardware_UsbDeviceConnection_open},
286 {"native_close", "()V", (void *)android_hardware_UsbDeviceConnection_close},
287 {"native_get_fd", "()I", (void *)android_hardware_UsbDeviceConnection_get_fd},
288 {"native_get_desc", "()[B", (void *)android_hardware_UsbDeviceConnection_get_desc},
289 {"native_claim_interface", "(IZ)Z",(void *)android_hardware_UsbDeviceConnection_claim_interface},
290 {"native_release_interface","(I)Z", (void *)android_hardware_UsbDeviceConnection_release_interface},
291 {"native_set_interface","(II)Z", (void *)android_hardware_UsbDeviceConnection_set_interface},
292 {"native_set_configuration","(I)Z", (void *)android_hardware_UsbDeviceConnection_set_configuration},
293 {"native_control_request", "(IIII[BIII)I",
294 (void *)android_hardware_UsbDeviceConnection_control_request},
295 {"native_bulk_request", "(I[BIII)I",
296 (void *)android_hardware_UsbDeviceConnection_bulk_request},
297 {"native_request_wait", "(J)Landroid/hardware/usb/UsbRequest;",
298 (void *)android_hardware_UsbDeviceConnection_request_wait},
299 { "native_get_serial", "()Ljava/lang/String;",
300 (void*)android_hardware_UsbDeviceConnection_get_serial },
301 {"native_reset_device","()Z", (void *)android_hardware_UsbDeviceConnection_reset_device},
302 };
303
register_android_hardware_UsbDeviceConnection(JNIEnv * env)304 int register_android_hardware_UsbDeviceConnection(JNIEnv *env)
305 {
306 jclass clazz = FindClassOrDie(env, "android/hardware/usb/UsbDeviceConnection");
307 field_context = GetFieldIDOrDie(env, clazz, "mNativeContext", "J");
308
309 return RegisterMethodsOrDie(env, "android/hardware/usb/UsbDeviceConnection",
310 method_table, NELEM(method_table));
311 }
312