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