summaryrefslogtreecommitdiffstats
path: root/core/jni/android_hardware_UsbDevice.cpp
diff options
context:
space:
mode:
authorMike Lockwood <lockwood@android.com>2011-03-11 08:18:08 -0500
committerMike Lockwood <lockwood@android.com>2011-03-11 10:24:21 -0500
commitacc29cc91be634070c92a807df412ced97b9b375 (patch)
tree0d4f25642cd68ac9d15966129d04ea127665d00d /core/jni/android_hardware_UsbDevice.cpp
parent364903bac6b9bfde694f1c0c5c40b6a2af628408 (diff)
downloadframeworks_base-acc29cc91be634070c92a807df412ced97b9b375.zip
frameworks_base-acc29cc91be634070c92a807df412ced97b9b375.tar.gz
frameworks_base-acc29cc91be634070c92a807df412ced97b9b375.tar.bz2
UsbDevice: Move IO related methods to new UsbDeviceConnection class
UsbDevice is now just an immutable parcelable object like UsbInterface and UsbEndpoint. All IO related functionality is now contained in UsbDeviceConnection and UsbRequest. Bug: 4067029 Change-Id: Ia84da0b512a697acc940eee0c3566711c62e1a68 Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'core/jni/android_hardware_UsbDevice.cpp')
-rw-r--r--core/jni/android_hardware_UsbDevice.cpp202
1 files changed, 0 insertions, 202 deletions
diff --git a/core/jni/android_hardware_UsbDevice.cpp b/core/jni/android_hardware_UsbDevice.cpp
index b01820c..c2950ea 100644
--- a/core/jni/android_hardware_UsbDevice.cpp
+++ b/core/jni/android_hardware_UsbDevice.cpp
@@ -24,190 +24,8 @@
#include <usbhost/usbhost.h>
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
using namespace android;
-static jfieldID field_context;
-
-struct usb_device* get_device_from_object(JNIEnv* env, jobject javaDevice)
-{
- return (struct usb_device*)env->GetIntField(javaDevice, field_context);
-}
-
-// in android_hardware_UsbEndpoint.cpp
-extern struct usb_endpoint* get_endpoint_from_object(JNIEnv* env, jobject javaEndpoint);
-
-static jboolean
-android_hardware_UsbDevice_open(JNIEnv *env, jobject thiz, jstring deviceName,
- jobject fileDescriptor)
-{
- int fd = getParcelFileDescriptorFD(env, fileDescriptor);
- // duplicate the file descriptor, since ParcelFileDescriptor will eventually close its copy
- fd = dup(fd);
- if (fd < 0)
- return false;
-
- const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
- struct usb_device* device = usb_device_new(deviceNameStr, fd);
- if (device) {
- env->SetIntField(thiz, field_context, (int)device);
- } else {
- LOGE("usb_device_open failed for %s", deviceNameStr);
- close(fd);
- }
-
- env->ReleaseStringUTFChars(deviceName, deviceNameStr);
- return (device != NULL);
-}
-
-static void
-android_hardware_UsbDevice_close(JNIEnv *env, jobject thiz)
-{
- LOGD("close\n");
- struct usb_device* device = get_device_from_object(env, thiz);
- if (device) {
- usb_device_close(device);
- env->SetIntField(thiz, field_context, 0);
- }
-}
-
-static jint
-android_hardware_UsbDevice_get_fd(JNIEnv *env, jobject thiz)
-{
- struct usb_device* device = get_device_from_object(env, thiz);
- if (!device) {
- LOGE("device is closed in native_get_fd");
- return -1;
- }
- return usb_device_get_fd(device);
-}
-
-static jboolean
-android_hardware_UsbDevice_claim_interface(JNIEnv *env, jobject thiz, int interfaceID, jboolean force)
-{
- struct usb_device* device = get_device_from_object(env, thiz);
- if (!device) {
- LOGE("device is closed in native_claim_interface");
- return -1;
- }
-
- int ret = usb_device_claim_interface(device, interfaceID);
- if (ret && force && errno == EBUSY) {
- // disconnect kernel driver and try again
- usb_device_connect_kernel_driver(device, interfaceID, false);
- ret = usb_device_claim_interface(device, interfaceID);
- }
- return ret == 0;
-}
-
-static jint
-android_hardware_UsbDevice_release_interface(JNIEnv *env, jobject thiz, int interfaceID)
-{
- struct usb_device* device = get_device_from_object(env, thiz);
- if (!device) {
- LOGE("device is closed in native_release_interface");
- return -1;
- }
- int ret = usb_device_release_interface(device, interfaceID);
- if (ret == 0) {
- // allow kernel to reconnect its driver
- usb_device_connect_kernel_driver(device, interfaceID, true);
- }
- return ret;
-}
-
-static jint
-android_hardware_UsbDevice_control_request(JNIEnv *env, jobject thiz,
- jint requestType, jint request, jint value, jint index,
- jbyteArray buffer, jint length, jint timeout)
-{
- struct usb_device* device = get_device_from_object(env, thiz);
- if (!device) {
- LOGE("device is closed in native_control_request");
- return -1;
- }
-
- jbyte* bufferBytes = NULL;
- if (buffer) {
- if (env->GetArrayLength(buffer) < length) {
- env->ThrowNew(env->FindClass("java/lang/ArrayIndexOutOfBoundsException"), NULL);
- return -1;
- }
- bufferBytes = env->GetByteArrayElements(buffer, 0);
- }
-
- jint result = usb_device_control_transfer(device, requestType, request,
- value, index, bufferBytes, length, timeout);
-
- if (bufferBytes)
- env->ReleaseByteArrayElements(buffer, bufferBytes, 0);
-
- return result;
-}
-
-static jint
-android_hardware_UsbDevice_bulk_request(JNIEnv *env, jobject thiz,
- jint endpoint, jbyteArray buffer, jint length, jint timeout)
-{
- struct usb_device* device = get_device_from_object(env, thiz);
- if (!device) {
- LOGE("device is closed in native_control_request");
- return -1;
- }
-
- jbyte* bufferBytes = NULL;
- if (buffer) {
- if (env->GetArrayLength(buffer) < length) {
- env->ThrowNew(env->FindClass("java/lang/ArrayIndexOutOfBoundsException"), NULL);
- return -1;
- }
- bufferBytes = env->GetByteArrayElements(buffer, 0);
- }
-
- jint result = usb_device_bulk_transfer(device, endpoint, bufferBytes, length, timeout);
-
- if (bufferBytes)
- env->ReleaseByteArrayElements(buffer, bufferBytes, 0);
-
- return result;
-}
-
-static jobject
-android_hardware_UsbDevice_request_wait(JNIEnv *env, jobject thiz)
-{
- struct usb_device* device = get_device_from_object(env, thiz);
- if (!device) {
- LOGE("device is closed in native_request_wait");
- return NULL;
- }
-
- struct usb_request* request = usb_request_wait(device);
- if (request)
- return (jobject)request->client_data;
- else
- return NULL;
-}
-
-static jstring
-android_hardware_UsbDevice_get_serial(JNIEnv *env, jobject thiz)
-{
- struct usb_device* device = get_device_from_object(env, thiz);
- if (!device) {
- LOGE("device is closed in native_request_wait");
- return NULL;
- }
- char* serial = usb_device_get_serial(device);
- if (!serial)
- return NULL;
- jstring result = env->NewStringUTF(serial);
- free(serial);
- return result;
-}
-
static jint
android_hardware_UsbDevice_get_device_id(JNIEnv *env, jobject clazz, jstring name)
{
@@ -227,21 +45,6 @@ android_hardware_UsbDevice_get_device_name(JNIEnv *env, jobject clazz, jint id)
}
static JNINativeMethod method_table[] = {
- {"native_open", "(Ljava/lang/String;Ljava/io/FileDescriptor;)Z",
- (void *)android_hardware_UsbDevice_open},
- {"native_close", "()V", (void *)android_hardware_UsbDevice_close},
- {"native_get_fd", "()I", (void *)android_hardware_UsbDevice_get_fd},
- {"native_claim_interface", "(IZ)Z",(void *)android_hardware_UsbDevice_claim_interface},
- {"native_release_interface","(I)Z", (void *)android_hardware_UsbDevice_release_interface},
- {"native_control_request", "(IIII[BII)I",
- (void *)android_hardware_UsbDevice_control_request},
- {"native_bulk_request", "(I[BII)I",
- (void *)android_hardware_UsbDevice_bulk_request},
- {"native_request_wait", "()Landroid/hardware/usb/UsbRequest;",
- (void *)android_hardware_UsbDevice_request_wait},
- { "native_get_serial", "()Ljava/lang/String;",
- (void*)android_hardware_UsbDevice_get_serial },
-
// static methods
{ "native_get_device_id", "(Ljava/lang/String;)I",
(void*)android_hardware_UsbDevice_get_device_id },
@@ -256,11 +59,6 @@ int register_android_hardware_UsbDevice(JNIEnv *env)
LOGE("Can't find android/hardware/usb/UsbDevice");
return -1;
}
- field_context = env->GetFieldID(clazz, "mNativeContext", "I");
- if (field_context == NULL) {
- LOGE("Can't find UsbDevice.mNativeContext");
- return -1;
- }
return AndroidRuntime::registerNativeMethods(env, "android/hardware/usb/UsbDevice",
method_table, NELEM(method_table));