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()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 45 | ALOGE("An exception was thrown by callback '%s'.", methodName); |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 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) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 69 | ALOGE("could not open %s", DRIVER_NAME); |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 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) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 91 | ALOGE("could not open %s", DRIVER_NAME); |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 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) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 106 | ALOGE("could not open %s", DRIVER_NAME); |
Mike Lockwood | d462ecf | 2011-07-21 13:55:16 -0700 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | int result = ioctl(fd, ACCESSORY_IS_START_REQUESTED); |
| 110 | close(fd); |
| 111 | return (result == 1); |
| 112 | } |
| 113 | |
Mike Lockwood | 166b05e | 2012-04-24 10:45:18 -0700 | [diff] [blame] | 114 | static jint android_server_UsbDeviceManager_getAudioMode(JNIEnv *env, jobject thiz) |
| 115 | { |
| 116 | int fd = open(DRIVER_NAME, O_RDWR); |
| 117 | if (fd < 0) { |
| 118 | ALOGE("could not open %s", DRIVER_NAME); |
| 119 | return false; |
| 120 | } |
| 121 | int result = ioctl(fd, ACCESSORY_GET_AUDIO_MODE); |
| 122 | close(fd); |
| 123 | return result; |
| 124 | } |
Mike Lockwood | d462ecf | 2011-07-21 13:55:16 -0700 | [diff] [blame] | 125 | |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 126 | static JNINativeMethod method_table[] = { |
| 127 | { "nativeGetAccessoryStrings", "()[Ljava/lang/String;", |
| 128 | (void*)android_server_UsbDeviceManager_getAccessoryStrings }, |
| 129 | { "nativeOpenAccessory", "()Landroid/os/ParcelFileDescriptor;", |
| 130 | (void*)android_server_UsbDeviceManager_openAccessory }, |
Mike Lockwood | d462ecf | 2011-07-21 13:55:16 -0700 | [diff] [blame] | 131 | { "nativeIsStartRequested", "()Z", |
| 132 | (void*)android_server_UsbDeviceManager_isStartRequested }, |
Mike Lockwood | 166b05e | 2012-04-24 10:45:18 -0700 | [diff] [blame] | 133 | { "nativeGetAudioMode", "()I", |
| 134 | (void*)android_server_UsbDeviceManager_getAudioMode }, |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | int register_android_server_UsbDeviceManager(JNIEnv *env) |
| 138 | { |
| 139 | jclass clazz = env->FindClass("com/android/server/usb/UsbDeviceManager"); |
| 140 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 141 | ALOGE("Can't find com/android/server/usb/UsbDeviceManager"); |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | clazz = env->FindClass("android/os/ParcelFileDescriptor"); |
| 146 | LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor"); |
| 147 | gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz); |
| 148 | gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V"); |
| 149 | LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL, |
| 150 | "Unable to find constructor for android.os.ParcelFileDescriptor"); |
| 151 | |
| 152 | return jniRegisterNativeMethods(env, "com/android/server/usb/UsbDeviceManager", |
| 153 | method_table, NELEM(method_table)); |
| 154 | } |
| 155 | |
| 156 | }; |