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