2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "UsbDeviceConnectionJNI"
19 #include "utils/Log.h"
23 #include "core_jni_helpers.h"
25 #include <usbhost/usbhost.h>
28 #include <sys/types.h>
32 using namespace android;
34 static jfieldID field_context;
36 struct usb_device* get_device_from_object(JNIEnv* env, jobject connection)
38 return (struct usb_device*)env->GetLongField(connection, field_context);
42 android_hardware_UsbDeviceConnection_open(JNIEnv *env, jobject thiz, jstring deviceName,
43 jobject fileDescriptor)
45 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
46 // duplicate the file descriptor, since ParcelFileDescriptor will eventually close its copy
51 const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
52 struct usb_device* device = usb_device_new(deviceNameStr, fd);
54 env->SetLongField(thiz, field_context, (jlong)device);
56 ALOGE("usb_device_open failed for %s", deviceNameStr);
60 env->ReleaseStringUTFChars(deviceName, deviceNameStr);
61 return (device != NULL) ? JNI_TRUE : JNI_FALSE;
65 android_hardware_UsbDeviceConnection_close(JNIEnv *env, jobject thiz)
68 struct usb_device* device = get_device_from_object(env, thiz);
70 usb_device_close(device);
71 env->SetLongField(thiz, field_context, 0);
76 android_hardware_UsbDeviceConnection_get_fd(JNIEnv *env, jobject thiz)
78 struct usb_device* device = get_device_from_object(env, thiz);
80 ALOGE("device is closed in native_get_fd");
83 return usb_device_get_fd(device);
87 android_hardware_UsbDeviceConnection_get_desc(JNIEnv *env, jobject thiz)
90 int fd = android_hardware_UsbDeviceConnection_get_fd(env, thiz);
91 if (fd < 0) return NULL;
92 lseek(fd, 0, SEEK_SET);
93 int length = read(fd, buffer, sizeof(buffer));
94 if (length < 0) return NULL;
96 jbyteArray ret = env->NewByteArray(length);
98 jbyte* bytes = (jbyte*)env->GetPrimitiveArrayCritical(ret, 0);
100 memcpy(bytes, buffer, length);
101 env->ReleasePrimitiveArrayCritical(ret, bytes, 0);
108 android_hardware_UsbDeviceConnection_claim_interface(JNIEnv *env, jobject thiz,
109 jint interfaceID, jboolean force)
111 struct usb_device* device = get_device_from_object(env, thiz);
113 ALOGE("device is closed in native_claim_interface");
117 int ret = usb_device_claim_interface(device, interfaceID);
118 if (ret && force && errno == EBUSY) {
119 // disconnect kernel driver and try again
120 usb_device_connect_kernel_driver(device, interfaceID, false);
121 ret = usb_device_claim_interface(device, interfaceID);
123 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
127 android_hardware_UsbDeviceConnection_release_interface(JNIEnv *env, jobject thiz, jint interfaceID)
129 struct usb_device* device = get_device_from_object(env, thiz);
131 ALOGE("device is closed in native_release_interface");
134 int ret = usb_device_release_interface(device, interfaceID);
136 // allow kernel to reconnect its driver
137 usb_device_connect_kernel_driver(device, interfaceID, true);
139 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
143 android_hardware_UsbDeviceConnection_set_interface(JNIEnv *env, jobject thiz, jint interfaceID,
144 jint alternateSetting)
146 struct usb_device* device = get_device_from_object(env, thiz);
148 ALOGE("device is closed in native_set_interface");
151 int ret = usb_device_set_interface(device, interfaceID, alternateSetting);
152 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
156 android_hardware_UsbDeviceConnection_set_configuration(JNIEnv *env, jobject thiz, jint configurationID)
158 struct usb_device* device = get_device_from_object(env, thiz);
160 ALOGE("device is closed in native_set_configuration");
163 int ret = usb_device_set_configuration(device, configurationID);
164 return (ret == 0) ? JNI_TRUE : JNI_FALSE;
168 android_hardware_UsbDeviceConnection_control_request(JNIEnv *env, jobject thiz,
169 jint requestType, jint request, jint value, jint index,
170 jbyteArray buffer, jint start, jint length, jint timeout)
172 struct usb_device* device = get_device_from_object(env, thiz);
174 ALOGE("device is closed in native_control_request");
178 jbyte* bufferBytes = NULL;
180 bufferBytes = (jbyte*)env->GetPrimitiveArrayCritical(buffer, NULL);
183 jint result = usb_device_control_transfer(device, requestType, request,
184 value, index, bufferBytes + start, length, timeout);
187 env->ReleasePrimitiveArrayCritical(buffer, bufferBytes, 0);
194 android_hardware_UsbDeviceConnection_bulk_request(JNIEnv *env, jobject thiz,
195 jint endpoint, jbyteArray buffer, jint start, jint length, jint timeout)
197 struct usb_device* device = get_device_from_object(env, thiz);
199 ALOGE("device is closed in native_control_request");
203 jbyte* bufferBytes = NULL;
205 bufferBytes = (jbyte*)env->GetPrimitiveArrayCritical(buffer, NULL);
208 jint result = usb_device_bulk_transfer(device, endpoint, bufferBytes + start, length, timeout);
211 env->ReleasePrimitiveArrayCritical(buffer, bufferBytes, 0);
218 android_hardware_UsbDeviceConnection_request_wait(JNIEnv *env, jobject thiz)
220 struct usb_device* device = get_device_from_object(env, thiz);
222 ALOGE("device is closed in native_request_wait");
226 struct usb_request* request = usb_request_wait(device);
228 return (jobject)request->client_data;
234 android_hardware_UsbDeviceConnection_get_serial(JNIEnv *env, jobject thiz)
236 struct usb_device* device = get_device_from_object(env, thiz);
238 ALOGE("device is closed in native_request_wait");
241 char* serial = usb_device_get_serial(device);
244 jstring result = env->NewStringUTF(serial);
249 static JNINativeMethod method_table[] = {
250 {"native_open", "(Ljava/lang/String;Ljava/io/FileDescriptor;)Z",
251 (void *)android_hardware_UsbDeviceConnection_open},
252 {"native_close", "()V", (void *)android_hardware_UsbDeviceConnection_close},
253 {"native_get_fd", "()I", (void *)android_hardware_UsbDeviceConnection_get_fd},
254 {"native_get_desc", "()[B", (void *)android_hardware_UsbDeviceConnection_get_desc},
255 {"native_claim_interface", "(IZ)Z",(void *)android_hardware_UsbDeviceConnection_claim_interface},
256 {"native_release_interface","(I)Z", (void *)android_hardware_UsbDeviceConnection_release_interface},
257 {"native_set_interface","(II)Z", (void *)android_hardware_UsbDeviceConnection_set_interface},
258 {"native_set_configuration","(I)Z", (void *)android_hardware_UsbDeviceConnection_set_configuration},
259 {"native_control_request", "(IIII[BIII)I",
260 (void *)android_hardware_UsbDeviceConnection_control_request},
261 {"native_bulk_request", "(I[BIII)I",
262 (void *)android_hardware_UsbDeviceConnection_bulk_request},
263 {"native_request_wait", "()Landroid/hardware/usb/UsbRequest;",
264 (void *)android_hardware_UsbDeviceConnection_request_wait},
265 { "native_get_serial", "()Ljava/lang/String;",
266 (void*)android_hardware_UsbDeviceConnection_get_serial },
269 int register_android_hardware_UsbDeviceConnection(JNIEnv *env)
271 jclass clazz = FindClassOrDie(env, "android/hardware/usb/UsbDeviceConnection");
272 field_context = GetFieldIDOrDie(env, clazz, "mNativeContext", "J");
274 return RegisterMethodsOrDie(env, "android/hardware/usb/UsbDeviceConnection",
275 method_table, NELEM(method_table));