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
35namespace android
36{
37
38static struct parcel_file_descriptor_offsets_t
39{
40    jclass mClass;
41    jmethodID mConstructor;
42} gParcelFileDescriptorOffsets;
43
44static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
45    if (env->ExceptionCheck()) {
46        ALOGE("An exception was thrown by callback '%s'.", methodName);
47        LOGE_EX(env);
48        env->ExceptionClear();
49    }
50}
51
52static void set_accessory_string(JNIEnv *env, int fd, int cmd, jobjectArray strArray, int index)
53{
54    char buffer[256];
55
56    buffer[0] = 0;
57    int length = ioctl(fd, cmd, buffer);
58    if (buffer[0]) {
59        jstring obj = env->NewStringUTF(buffer);
60        env->SetObjectArrayElement(strArray, index, obj);
61        env->DeleteLocalRef(obj);
62    }
63}
64
65
66static jobjectArray android_server_UsbDeviceManager_getAccessoryStrings(JNIEnv *env, jobject thiz)
67{
68    int fd = open(DRIVER_NAME, O_RDWR);
69    if (fd < 0) {
70        ALOGE("could not open %s", DRIVER_NAME);
71        return NULL;
72    }
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);
82
83out:
84    close(fd);
85    return strArray;
86}
87
88static jobject android_server_UsbDeviceManager_openAccessory(JNIEnv *env, jobject thiz)
89{
90    int fd = open(DRIVER_NAME, O_RDWR);
91    if (fd < 0) {
92        ALOGE("could not open %s", DRIVER_NAME);
93        return NULL;
94    }
95    jobject fileDescriptor = jniCreateFileDescriptor(env, fd);
96    if (fileDescriptor == NULL) {
97        return NULL;
98    }
99    return env->NewObject(gParcelFileDescriptorOffsets.mClass,
100        gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
101}
102
103static jboolean android_server_UsbDeviceManager_isStartRequested(JNIEnv *env, jobject thiz)
104{
105    int fd = open(DRIVER_NAME, O_RDWR);
106    if (fd < 0) {
107        ALOGE("could not open %s", DRIVER_NAME);
108        return false;
109    }
110    int result = ioctl(fd, ACCESSORY_IS_START_REQUESTED);
111    close(fd);
112    return (result == 1);
113}
114
115static jint android_server_UsbDeviceManager_getAudioMode(JNIEnv *env, jobject thiz)
116{
117    int fd = open(DRIVER_NAME, O_RDWR);
118    if (fd < 0) {
119        ALOGE("could not open %s", DRIVER_NAME);
120        return false;
121    }
122    int result = ioctl(fd, ACCESSORY_GET_AUDIO_MODE);
123    close(fd);
124    return result;
125}
126
127static 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 },
136};
137
138int register_android_server_UsbDeviceManager(JNIEnv *env)
139{
140    jclass clazz = env->FindClass("com/android/server/usb/UsbDeviceManager");
141    if (clazz == NULL) {
142        ALOGE("Can't find com/android/server/usb/UsbDeviceManager");
143        return -1;
144    }
145
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");
152
153    return jniRegisterNativeMethods(env, "com/android/server/usb/UsbDeviceManager",
154            method_table, NELEM(method_table));
155}
156
157};
158