blob: b885c285e3803ee88c04e8aff6c2d715b73091e1 [file] [log] [blame]
Mike Lockwoodacc29cc2011-03-11 08:18:08 -05001/*
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 "utils/Log.h"
20
21#include "jni.h"
Steven Moreland2279b252017-07-19 09:50:45 -070022#include <nativehelper/JNIHelp.h>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080023#include "core_jni_helpers.h"
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050024
25#include <usbhost/usbhost.h>
26
Philip P. Moltmann927fefe2016-10-18 12:57:34 -070027#include <chrono>
28
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050029#include <stdio.h>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <fcntl.h>
33
34using namespace android;
Philip P. Moltmann927fefe2016-10-18 12:57:34 -070035using namespace std::chrono;
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050036
Vitalii Tomkivd9804ef2016-10-21 14:03:48 -070037static const int USB_CONTROL_READ_TIMEOUT_MS = 200;
38
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050039static jfieldID field_context;
40
41struct usb_device* get_device_from_object(JNIEnv* env, jobject connection)
42{
Ashok Bhat4838e332014-01-03 14:37:19 +000043 return (struct usb_device*)env->GetLongField(connection, field_context);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050044}
45
46static jboolean
47android_hardware_UsbDeviceConnection_open(JNIEnv *env, jobject thiz, jstring deviceName,
48 jobject fileDescriptor)
49{
Elliott Hughesa3804cf2011-04-11 16:50:19 -070050 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050051 // duplicate the file descriptor, since ParcelFileDescriptor will eventually close its copy
Nick Kralevich4b3a08c2019-01-28 10:39:10 -080052 fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050053 if (fd < 0)
Ashok Bhat4838e332014-01-03 14:37:19 +000054 return JNI_FALSE;
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050055
56 const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
57 struct usb_device* device = usb_device_new(deviceNameStr, fd);
58 if (device) {
Ashok Bhat4838e332014-01-03 14:37:19 +000059 env->SetLongField(thiz, field_context, (jlong)device);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050060 } else {
Steve Block3762c312012-01-06 19:20:56 +000061 ALOGE("usb_device_open failed for %s", deviceNameStr);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050062 close(fd);
63 }
64
65 env->ReleaseStringUTFChars(deviceName, deviceNameStr);
Ashok Bhat4838e332014-01-03 14:37:19 +000066 return (device != NULL) ? JNI_TRUE : JNI_FALSE;
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050067}
68
69static void
70android_hardware_UsbDeviceConnection_close(JNIEnv *env, jobject thiz)
71{
Steve Block5baa3a62011-12-20 16:23:08 +000072 ALOGD("close\n");
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050073 struct usb_device* device = get_device_from_object(env, thiz);
74 if (device) {
75 usb_device_close(device);
Ashok Bhat4838e332014-01-03 14:37:19 +000076 env->SetLongField(thiz, field_context, 0);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050077 }
78}
79
80static jint
81android_hardware_UsbDeviceConnection_get_fd(JNIEnv *env, jobject thiz)
82{
83 struct usb_device* device = get_device_from_object(env, thiz);
84 if (!device) {
Steve Block3762c312012-01-06 19:20:56 +000085 ALOGE("device is closed in native_get_fd");
Mike Lockwoodacc29cc2011-03-11 08:18:08 -050086 return -1;
87 }
88 return usb_device_get_fd(device);
89}
90
Mike Lockwooda88b42d2011-05-19 11:52:40 -040091static jbyteArray
92android_hardware_UsbDeviceConnection_get_desc(JNIEnv *env, jobject thiz)
93{
94 char buffer[16384];
95 int fd = android_hardware_UsbDeviceConnection_get_fd(env, thiz);
96 if (fd < 0) return NULL;
97 lseek(fd, 0, SEEK_SET);
98 int length = read(fd, buffer, sizeof(buffer));
99 if (length < 0) return NULL;
100
101 jbyteArray ret = env->NewByteArray(length);
102 if (ret) {
103 jbyte* bytes = (jbyte*)env->GetPrimitiveArrayCritical(ret, 0);
104 if (bytes) {
105 memcpy(bytes, buffer, length);
106 env->ReleasePrimitiveArrayCritical(ret, bytes, 0);
107 }
108 }
109 return ret;
110}
111
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500112static jboolean
113android_hardware_UsbDeviceConnection_claim_interface(JNIEnv *env, jobject thiz,
Ashok Bhat4838e332014-01-03 14:37:19 +0000114 jint interfaceID, jboolean force)
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500115{
116 struct usb_device* device = get_device_from_object(env, thiz);
117 if (!device) {
Steve Block3762c312012-01-06 19:20:56 +0000118 ALOGE("device is closed in native_claim_interface");
Ashok Bhat4838e332014-01-03 14:37:19 +0000119 return JNI_FALSE;
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500120 }
121
122 int ret = usb_device_claim_interface(device, interfaceID);
123 if (ret && force && errno == EBUSY) {
124 // disconnect kernel driver and try again
125 usb_device_connect_kernel_driver(device, interfaceID, false);
126 ret = usb_device_claim_interface(device, interfaceID);
127 }
Ashok Bhat4838e332014-01-03 14:37:19 +0000128 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500129}
130
Mike Lockwood7531aa22014-01-13 10:31:01 -0800131static jboolean
Ashok Bhat4838e332014-01-03 14:37:19 +0000132android_hardware_UsbDeviceConnection_release_interface(JNIEnv *env, jobject thiz, jint interfaceID)
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500133{
134 struct usb_device* device = get_device_from_object(env, thiz);
135 if (!device) {
Steve Block3762c312012-01-06 19:20:56 +0000136 ALOGE("device is closed in native_release_interface");
Mike Lockwood7531aa22014-01-13 10:31:01 -0800137 return JNI_FALSE;
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500138 }
139 int ret = usb_device_release_interface(device, interfaceID);
140 if (ret == 0) {
141 // allow kernel to reconnect its driver
142 usb_device_connect_kernel_driver(device, interfaceID, true);
143 }
Mike Lockwood7531aa22014-01-13 10:31:01 -0800144 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
145}
146
147static jboolean
148android_hardware_UsbDeviceConnection_set_interface(JNIEnv *env, jobject thiz, jint interfaceID,
149 jint alternateSetting)
150{
151 struct usb_device* device = get_device_from_object(env, thiz);
152 if (!device) {
153 ALOGE("device is closed in native_set_interface");
154 return JNI_FALSE;
155 }
156 int ret = usb_device_set_interface(device, interfaceID, alternateSetting);
157 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
158}
159
160static jboolean
161android_hardware_UsbDeviceConnection_set_configuration(JNIEnv *env, jobject thiz, jint configurationID)
162{
163 struct usb_device* device = get_device_from_object(env, thiz);
164 if (!device) {
165 ALOGE("device is closed in native_set_configuration");
166 return JNI_FALSE;
167 }
168 int ret = usb_device_set_configuration(device, configurationID);
169 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500170}
171
172static jint
173android_hardware_UsbDeviceConnection_control_request(JNIEnv *env, jobject thiz,
174 jint requestType, jint request, jint value, jint index,
Jeff Brown6c81a932013-03-28 03:13:09 -0700175 jbyteArray buffer, jint start, jint length, jint timeout)
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500176{
177 struct usb_device* device = get_device_from_object(env, thiz);
178 if (!device) {
Steve Block3762c312012-01-06 19:20:56 +0000179 ALOGE("device is closed in native_control_request");
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500180 return -1;
181 }
182
183 jbyte* bufferBytes = NULL;
184 if (buffer) {
Jeff Brown6c81a932013-03-28 03:13:09 -0700185 bufferBytes = (jbyte*)env->GetPrimitiveArrayCritical(buffer, NULL);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500186 }
187
188 jint result = usb_device_control_transfer(device, requestType, request,
Jeff Brown6c81a932013-03-28 03:13:09 -0700189 value, index, bufferBytes + start, length, timeout);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500190
Jeff Brown6c81a932013-03-28 03:13:09 -0700191 if (bufferBytes) {
192 env->ReleasePrimitiveArrayCritical(buffer, bufferBytes, 0);
193 }
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500194
195 return result;
196}
197
198static jint
199android_hardware_UsbDeviceConnection_bulk_request(JNIEnv *env, jobject thiz,
Jeff Brown6c81a932013-03-28 03:13:09 -0700200 jint endpoint, jbyteArray buffer, jint start, jint length, jint timeout)
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500201{
202 struct usb_device* device = get_device_from_object(env, thiz);
203 if (!device) {
Steve Block3762c312012-01-06 19:20:56 +0000204 ALOGE("device is closed in native_control_request");
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500205 return -1;
206 }
207
208 jbyte* bufferBytes = NULL;
209 if (buffer) {
Jeff Brown6c81a932013-03-28 03:13:09 -0700210 bufferBytes = (jbyte*)env->GetPrimitiveArrayCritical(buffer, NULL);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500211 }
212
Jeff Brown6c81a932013-03-28 03:13:09 -0700213 jint result = usb_device_bulk_transfer(device, endpoint, bufferBytes + start, length, timeout);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500214
Jeff Brown6c81a932013-03-28 03:13:09 -0700215 if (bufferBytes) {
216 env->ReleasePrimitiveArrayCritical(buffer, bufferBytes, 0);
217 }
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500218
219 return result;
220}
221
222static jobject
Philip P. Moltmann9b6dd2b2017-04-06 15:34:04 -0700223android_hardware_UsbDeviceConnection_request_wait(JNIEnv *env, jobject thiz, jlong timeoutMillis)
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500224{
225 struct usb_device* device = get_device_from_object(env, thiz);
226 if (!device) {
Steve Block3762c312012-01-06 19:20:56 +0000227 ALOGE("device is closed in native_request_wait");
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500228 return NULL;
229 }
230
Philip P. Moltmann927fefe2016-10-18 12:57:34 -0700231 struct usb_request* request;
232 if (timeoutMillis == -1) {
233 request = usb_request_wait(device, -1);
234 } else {
235 steady_clock::time_point currentTime = steady_clock::now();
236 steady_clock::time_point endTime = currentTime + std::chrono::milliseconds(timeoutMillis);
237
238 // Poll the existence of a request via usb_request_wait until we get a result, an unexpected
239 // error or time out. As several threads can listen on the same fd, we might get wakeups
240 // without data.
241 while (1) {
242 request = usb_request_wait(device, duration_cast<std::chrono::milliseconds>(endTime
243 - currentTime).count());
244
245 int error = errno;
Philip P. Moltmann9b6dd2b2017-04-06 15:34:04 -0700246 if (request != NULL) {
247 break;
248 }
249
Philip P. Moltmann927fefe2016-10-18 12:57:34 -0700250 currentTime = steady_clock::now();
Philip P. Moltmann9b6dd2b2017-04-06 15:34:04 -0700251 if (currentTime >= endTime) {
252 jniThrowException(env, "java/util/concurrent/TimeoutException", "");
253 break;
254 }
255
256 if (error != EAGAIN) {
Philip P. Moltmann927fefe2016-10-18 12:57:34 -0700257 break;
258 }
259 };
260 }
261
262 if (request) {
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500263 return (jobject)request->client_data;
Philip P. Moltmann927fefe2016-10-18 12:57:34 -0700264 } else {
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500265 return NULL;
Philip P. Moltmann927fefe2016-10-18 12:57:34 -0700266 }
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500267}
268
269static jstring
270android_hardware_UsbDeviceConnection_get_serial(JNIEnv *env, jobject thiz)
271{
272 struct usb_device* device = get_device_from_object(env, thiz);
273 if (!device) {
Philip P. Moltmann674e8c32016-08-29 12:28:31 -0700274 ALOGE("device is closed in native_get_serial");
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500275 return NULL;
276 }
Vitalii Tomkivd9804ef2016-10-21 14:03:48 -0700277 char* serial = usb_device_get_serial(device,
278 USB_CONTROL_READ_TIMEOUT_MS);
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500279 if (!serial)
280 return NULL;
281 jstring result = env->NewStringUTF(serial);
282 free(serial);
283 return result;
284}
285
Keun-young Parkbadbbae2016-01-05 13:27:29 -0800286static jboolean
287android_hardware_UsbDeviceConnection_reset_device(JNIEnv *env, jobject thiz)
288{
289 struct usb_device* device = get_device_from_object(env, thiz);
290 if (!device) {
291 ALOGE("device is closed in native_reset_device");
292 return JNI_FALSE;
293 }
294 int ret = usb_device_reset(device);
295 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
296}
297
Daniel Micay76f6a862015-09-19 17:31:01 -0400298static const JNINativeMethod method_table[] = {
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500299 {"native_open", "(Ljava/lang/String;Ljava/io/FileDescriptor;)Z",
300 (void *)android_hardware_UsbDeviceConnection_open},
301 {"native_close", "()V", (void *)android_hardware_UsbDeviceConnection_close},
302 {"native_get_fd", "()I", (void *)android_hardware_UsbDeviceConnection_get_fd},
Mike Lockwooda88b42d2011-05-19 11:52:40 -0400303 {"native_get_desc", "()[B", (void *)android_hardware_UsbDeviceConnection_get_desc},
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500304 {"native_claim_interface", "(IZ)Z",(void *)android_hardware_UsbDeviceConnection_claim_interface},
305 {"native_release_interface","(I)Z", (void *)android_hardware_UsbDeviceConnection_release_interface},
Mike Lockwood7531aa22014-01-13 10:31:01 -0800306 {"native_set_interface","(II)Z", (void *)android_hardware_UsbDeviceConnection_set_interface},
307 {"native_set_configuration","(I)Z", (void *)android_hardware_UsbDeviceConnection_set_configuration},
Jeff Brown6c81a932013-03-28 03:13:09 -0700308 {"native_control_request", "(IIII[BIII)I",
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500309 (void *)android_hardware_UsbDeviceConnection_control_request},
Jeff Brown6c81a932013-03-28 03:13:09 -0700310 {"native_bulk_request", "(I[BIII)I",
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500311 (void *)android_hardware_UsbDeviceConnection_bulk_request},
Philip P. Moltmann9b6dd2b2017-04-06 15:34:04 -0700312 {"native_request_wait", "(J)Landroid/hardware/usb/UsbRequest;",
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500313 (void *)android_hardware_UsbDeviceConnection_request_wait},
314 { "native_get_serial", "()Ljava/lang/String;",
315 (void*)android_hardware_UsbDeviceConnection_get_serial },
Keun-young Parkbadbbae2016-01-05 13:27:29 -0800316 {"native_reset_device","()Z", (void *)android_hardware_UsbDeviceConnection_reset_device},
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500317};
318
319int register_android_hardware_UsbDeviceConnection(JNIEnv *env)
320{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800321 jclass clazz = FindClassOrDie(env, "android/hardware/usb/UsbDeviceConnection");
322 field_context = GetFieldIDOrDie(env, clazz, "mNativeContext", "J");
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500323
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800324 return RegisterMethodsOrDie(env, "android/hardware/usb/UsbDeviceConnection",
Mike Lockwoodacc29cc2011-03-11 08:18:08 -0500325 method_table, NELEM(method_table));
326}