Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 "UsbDeviceManagerJNI" |
| 18 | #include "utils/Log.h" |
| 19 | |
| 20 | #include "jni.h" |
| 21 | #include "JNIHelp.h" |
| 22 | #include "android_runtime/AndroidRuntime.h" |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <asm/byteorder.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #include <linux/usb/f_accessory.h> |
| 31 | |
| 32 | #define DRIVER_NAME "/dev/usb_accessory" |
| 33 | |
| 34 | namespace android |
| 35 | { |
| 36 | |
| 37 | static struct parcel_file_descriptor_offsets_t |
| 38 | { |
| 39 | jclass mClass; |
| 40 | jmethodID mConstructor; |
| 41 | } gParcelFileDescriptorOffsets; |
| 42 | |
| 43 | static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 44 | if (env->ExceptionCheck()) { |
| 45 | LOGE("An exception was thrown by callback '%s'.", methodName); |
| 46 | LOGE_EX(env); |
| 47 | env->ExceptionClear(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static void set_accessory_string(JNIEnv *env, int fd, int cmd, jobjectArray strArray, int index) |
| 52 | { |
| 53 | char buffer[256]; |
| 54 | |
| 55 | buffer[0] = 0; |
| 56 | int length = ioctl(fd, cmd, buffer); |
| 57 | if (buffer[0]) { |
| 58 | jstring obj = env->NewStringUTF(buffer); |
| 59 | env->SetObjectArrayElement(strArray, index, obj); |
| 60 | env->DeleteLocalRef(obj); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static jobjectArray android_server_UsbDeviceManager_getAccessoryStrings(JNIEnv *env, jobject thiz) |
| 66 | { |
| 67 | int fd = open(DRIVER_NAME, O_RDWR); |
| 68 | if (fd < 0) { |
| 69 | LOGE("could not open %s", DRIVER_NAME); |
| 70 | return NULL; |
| 71 | } |
| 72 | jclass stringClass = env->FindClass("java/lang/String"); |
| 73 | jobjectArray strArray = env->NewObjectArray(6, stringClass, NULL); |
| 74 | if (!strArray) goto out; |
| 75 | set_accessory_string(env, fd, ACCESSORY_GET_STRING_MANUFACTURER, strArray, 0); |
| 76 | set_accessory_string(env, fd, ACCESSORY_GET_STRING_MODEL, strArray, 1); |
| 77 | set_accessory_string(env, fd, ACCESSORY_GET_STRING_DESCRIPTION, strArray, 2); |
| 78 | set_accessory_string(env, fd, ACCESSORY_GET_STRING_VERSION, strArray, 3); |
| 79 | set_accessory_string(env, fd, ACCESSORY_GET_STRING_URI, strArray, 4); |
| 80 | set_accessory_string(env, fd, ACCESSORY_GET_STRING_SERIAL, strArray, 5); |
| 81 | |
| 82 | out: |
| 83 | close(fd); |
| 84 | return strArray; |
| 85 | } |
| 86 | |
| 87 | static jobject android_server_UsbDeviceManager_openAccessory(JNIEnv *env, jobject thiz) |
| 88 | { |
| 89 | int fd = open(DRIVER_NAME, O_RDWR); |
| 90 | if (fd < 0) { |
| 91 | LOGE("could not open %s", DRIVER_NAME); |
| 92 | return NULL; |
| 93 | } |
| 94 | jobject fileDescriptor = jniCreateFileDescriptor(env, fd); |
| 95 | if (fileDescriptor == NULL) { |
| 96 | return NULL; |
| 97 | } |
| 98 | return env->NewObject(gParcelFileDescriptorOffsets.mClass, |
| 99 | gParcelFileDescriptorOffsets.mConstructor, fileDescriptor); |
| 100 | } |
| 101 | |
Mike Lockwood | d462ecf | 2011-07-21 13:55:16 -0700 | [diff] [blame] | 102 | static jboolean android_server_UsbDeviceManager_isStartRequested(JNIEnv *env, jobject thiz) |
| 103 | { |
| 104 | int fd = open(DRIVER_NAME, O_RDWR); |
| 105 | if (fd < 0) { |
| 106 | LOGE("could not open %s", DRIVER_NAME); |
| 107 | return false; |
| 108 | } |
| 109 | int result = ioctl(fd, ACCESSORY_IS_START_REQUESTED); |
| 110 | close(fd); |
| 111 | return (result == 1); |
| 112 | } |
| 113 | |
| 114 | |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 115 | static JNINativeMethod method_table[] = { |
| 116 | { "nativeGetAccessoryStrings", "()[Ljava/lang/String;", |
| 117 | (void*)android_server_UsbDeviceManager_getAccessoryStrings }, |
| 118 | { "nativeOpenAccessory", "()Landroid/os/ParcelFileDescriptor;", |
| 119 | (void*)android_server_UsbDeviceManager_openAccessory }, |
Mike Lockwood | d462ecf | 2011-07-21 13:55:16 -0700 | [diff] [blame] | 120 | { "nativeIsStartRequested", "()Z", |
| 121 | (void*)android_server_UsbDeviceManager_isStartRequested }, |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | int register_android_server_UsbDeviceManager(JNIEnv *env) |
| 125 | { |
| 126 | jclass clazz = env->FindClass("com/android/server/usb/UsbDeviceManager"); |
| 127 | if (clazz == NULL) { |
| 128 | LOGE("Can't find com/android/server/usb/UsbDeviceManager"); |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | clazz = env->FindClass("android/os/ParcelFileDescriptor"); |
| 133 | LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor"); |
| 134 | gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz); |
| 135 | gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V"); |
| 136 | LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL, |
| 137 | "Unable to find constructor for android.os.ParcelFileDescriptor"); |
| 138 | |
| 139 | return jniRegisterNativeMethods(env, "com/android/server/usb/UsbDeviceManager", |
| 140 | method_table, NELEM(method_table)); |
| 141 | } |
| 142 | |
| 143 | }; |