2 * Copyright (C) 2010 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 "UsbDeviceManagerJNI"
18 #include "utils/Log.h"
22 #include "android_runtime/AndroidRuntime.h"
23 #include "android_runtime/Log.h"
26 #include <asm/byteorder.h>
27 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <linux/usb/f_accessory.h>
33 #define DRIVER_NAME "/dev/usb_accessory"
38 static struct parcel_file_descriptor_offsets_t
41 jmethodID mConstructor;
42 } gParcelFileDescriptorOffsets;
44 static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
45 if (env->ExceptionCheck()) {
46 ALOGE("An exception was thrown by callback '%s'.", methodName);
48 env->ExceptionClear();
52 static void set_accessory_string(JNIEnv *env, int fd, int cmd, jobjectArray strArray, int index)
57 int length = ioctl(fd, cmd, buffer);
59 jstring obj = env->NewStringUTF(buffer);
60 env->SetObjectArrayElement(strArray, index, obj);
61 env->DeleteLocalRef(obj);
66 static jobjectArray android_server_UsbDeviceManager_getAccessoryStrings(JNIEnv *env, jobject thiz)
68 int fd = open(DRIVER_NAME, O_RDWR);
70 ALOGE("could not open %s", DRIVER_NAME);
73 jclass stringClass = env->FindClass("java/lang/String");
74 jobjectArray strArray = env->NewObjectArray(6, stringClass, NULL);
75 if (!strArray) goto out;
76 set_accessory_string(env, fd, ACCESSORY_GET_STRING_MANUFACTURER, strArray, 0);
77 set_accessory_string(env, fd, ACCESSORY_GET_STRING_MODEL, strArray, 1);
78 set_accessory_string(env, fd, ACCESSORY_GET_STRING_DESCRIPTION, strArray, 2);
79 set_accessory_string(env, fd, ACCESSORY_GET_STRING_VERSION, strArray, 3);
80 set_accessory_string(env, fd, ACCESSORY_GET_STRING_URI, strArray, 4);
81 set_accessory_string(env, fd, ACCESSORY_GET_STRING_SERIAL, strArray, 5);
88 static jobject android_server_UsbDeviceManager_openAccessory(JNIEnv *env, jobject thiz)
90 int fd = open(DRIVER_NAME, O_RDWR);
92 ALOGE("could not open %s", DRIVER_NAME);
95 jobject fileDescriptor = jniCreateFileDescriptor(env, fd);
96 if (fileDescriptor == NULL) {
99 return env->NewObject(gParcelFileDescriptorOffsets.mClass,
100 gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
103 static jboolean android_server_UsbDeviceManager_isStartRequested(JNIEnv *env, jobject thiz)
105 int fd = open(DRIVER_NAME, O_RDWR);
107 ALOGE("could not open %s", DRIVER_NAME);
110 int result = ioctl(fd, ACCESSORY_IS_START_REQUESTED);
112 return (result == 1);
115 static jint android_server_UsbDeviceManager_getAudioMode(JNIEnv *env, jobject thiz)
117 int fd = open(DRIVER_NAME, O_RDWR);
119 ALOGE("could not open %s", DRIVER_NAME);
122 int result = ioctl(fd, ACCESSORY_GET_AUDIO_MODE);
127 static JNINativeMethod method_table[] = {
128 { "nativeGetAccessoryStrings", "()[Ljava/lang/String;",
129 (void*)android_server_UsbDeviceManager_getAccessoryStrings },
130 { "nativeOpenAccessory", "()Landroid/os/ParcelFileDescriptor;",
131 (void*)android_server_UsbDeviceManager_openAccessory },
132 { "nativeIsStartRequested", "()Z",
133 (void*)android_server_UsbDeviceManager_isStartRequested },
134 { "nativeGetAudioMode", "()I",
135 (void*)android_server_UsbDeviceManager_getAudioMode },
138 int register_android_server_UsbDeviceManager(JNIEnv *env)
140 jclass clazz = env->FindClass("com/android/server/usb/UsbDeviceManager");
142 ALOGE("Can't find com/android/server/usb/UsbDeviceManager");
146 clazz = env->FindClass("android/os/ParcelFileDescriptor");
147 LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
148 gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
149 gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
150 LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
151 "Unable to find constructor for android.os.ParcelFileDescriptor");
153 return jniRegisterNativeMethods(env, "com/android/server/usb/UsbDeviceManager",
154 method_table, NELEM(method_table));