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 "UsbHostManagerJNI"
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 <usbhost/usbhost.h>
26 
27 #include <stdio.h>
28 #include <asm/byteorder.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <sys/ioctl.h>
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 jmethodID method_beginUsbDeviceAdded;
44 static jmethodID method_addUsbConfiguration;
45 static jmethodID method_addUsbInterface;
46 static jmethodID method_addUsbEndpoint;
47 static jmethodID method_endUsbDeviceAdded;
48 static jmethodID method_usbDeviceRemoved;
49 
checkAndClearExceptionFromCallback(JNIEnv * env,const char * methodName)50 static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
51     if (env->ExceptionCheck()) {
52         ALOGE("An exception was thrown by callback '%s'.", methodName);
53         LOGE_EX(env);
54         env->ExceptionClear();
55     }
56 }
57 
usb_device_added(const char * devname,void * client_data)58 static int usb_device_added(const char *devname, void* client_data) {
59     struct usb_descriptor_header* desc;
60     struct usb_descriptor_iter iter;
61 
62     struct usb_device *device = usb_device_open(devname);
63     if (!device) {
64         ALOGE("usb_device_open failed\n");
65         return 0;
66     }
67 
68     JNIEnv* env = AndroidRuntime::getJNIEnv();
69     jobject thiz = (jobject)client_data;
70     const usb_device_descriptor* deviceDesc = usb_device_get_device_descriptor(device);
71 
72     char *manufacturer = usb_device_get_manufacturer_name(device);
73     char *product = usb_device_get_product_name(device);
74     char *serial = usb_device_get_serial(device);
75 
76     jstring deviceName = env->NewStringUTF(devname);
77     jstring manufacturerName = AndroidRuntime::NewStringLatin1(env, manufacturer);
78     jstring productName = AndroidRuntime::NewStringLatin1(env, product);
79     jstring serialNumber = AndroidRuntime::NewStringLatin1(env, serial);
80 
81     jboolean result = env->CallBooleanMethod(thiz, method_beginUsbDeviceAdded,
82             deviceName, usb_device_get_vendor_id(device), usb_device_get_product_id(device),
83             deviceDesc->bDeviceClass, deviceDesc->bDeviceSubClass, deviceDesc->bDeviceProtocol,
84             manufacturerName, productName, serialNumber);
85 
86     env->DeleteLocalRef(serialNumber);
87     env->DeleteLocalRef(productName);
88     env->DeleteLocalRef(manufacturerName);
89     env->DeleteLocalRef(deviceName);
90     free(manufacturer);
91     free(product);
92     free(serial);
93 
94     if (!result) goto fail;
95 
96     usb_descriptor_iter_init(device, &iter);
97 
98     while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
99         if (desc->bDescriptorType == USB_DT_CONFIG) {
100             struct usb_config_descriptor *config = (struct usb_config_descriptor *)desc;
101             char *name = usb_device_get_string(device, config->iConfiguration);
102             jstring configName = AndroidRuntime::NewStringLatin1(env, name);
103 
104             env->CallVoidMethod(thiz, method_addUsbConfiguration,
105                     config->bConfigurationValue, configName, config->bmAttributes,
106                     config->bMaxPower);
107 
108             env->DeleteLocalRef(configName);
109             free(name);
110         } else if (desc->bDescriptorType == USB_DT_INTERFACE) {
111             struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
112             char *name = usb_device_get_string(device, interface->iInterface);
113             jstring interfaceName = AndroidRuntime::NewStringLatin1(env, name);
114 
115             env->CallVoidMethod(thiz, method_addUsbInterface,
116                     interface->bInterfaceNumber, interfaceName, interface->bAlternateSetting,
117                     interface->bInterfaceClass, interface->bInterfaceSubClass,
118                     interface->bInterfaceProtocol);
119 
120             env->DeleteLocalRef(interfaceName);
121             free(name);
122         } else if (desc->bDescriptorType == USB_DT_ENDPOINT) {
123             struct usb_endpoint_descriptor *endpoint = (struct usb_endpoint_descriptor *)desc;
124 
125             env->CallVoidMethod(thiz, method_addUsbEndpoint,
126                     endpoint->bEndpointAddress, endpoint->bmAttributes,
127                     __le16_to_cpu(endpoint->wMaxPacketSize), endpoint->bInterval);
128         }
129     }
130 
131     env->CallVoidMethod(thiz, method_endUsbDeviceAdded);
132 
133 fail:
134     usb_device_close(device);
135     checkAndClearExceptionFromCallback(env, __FUNCTION__);
136 
137     return 0;
138 }
139 
usb_device_removed(const char * devname,void * client_data)140 static int usb_device_removed(const char *devname, void* client_data) {
141     JNIEnv* env = AndroidRuntime::getJNIEnv();
142     jobject thiz = (jobject)client_data;
143 
144     jstring deviceName = env->NewStringUTF(devname);
145     env->CallVoidMethod(thiz, method_usbDeviceRemoved, deviceName);
146     env->DeleteLocalRef(deviceName);
147     checkAndClearExceptionFromCallback(env, __FUNCTION__);
148     return 0;
149 }
150 
android_server_UsbHostManager_monitorUsbHostBus(JNIEnv * env,jobject thiz)151 static void android_server_UsbHostManager_monitorUsbHostBus(JNIEnv *env, jobject thiz)
152 {
153     struct usb_host_context* context = usb_host_init();
154     if (!context) {
155         ALOGE("usb_host_init failed");
156         return;
157     }
158     // this will never return so it is safe to pass thiz directly
159     usb_host_run(context, usb_device_added, usb_device_removed, NULL, (void *)thiz);
160 }
161 
android_server_UsbHostManager_openDevice(JNIEnv * env,jobject thiz,jstring deviceName)162 static jobject android_server_UsbHostManager_openDevice(JNIEnv *env, jobject thiz, jstring deviceName)
163 {
164     const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
165     struct usb_device* device = usb_device_open(deviceNameStr);
166     env->ReleaseStringUTFChars(deviceName, deviceNameStr);
167 
168     if (!device)
169         return NULL;
170 
171     int fd = usb_device_get_fd(device);
172     if (fd < 0) {
173         usb_device_close(device);
174         return NULL;
175     }
176     int newFD = dup(fd);
177     usb_device_close(device);
178 
179     jobject fileDescriptor = jniCreateFileDescriptor(env, newFD);
180     if (fileDescriptor == NULL) {
181         return NULL;
182     }
183     return env->NewObject(gParcelFileDescriptorOffsets.mClass,
184         gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
185 }
186 
187 static JNINativeMethod method_table[] = {
188     { "monitorUsbHostBus", "()V", (void*)android_server_UsbHostManager_monitorUsbHostBus },
189     { "nativeOpenDevice",  "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;",
190                                   (void*)android_server_UsbHostManager_openDevice },
191 };
192 
register_android_server_UsbHostManager(JNIEnv * env)193 int register_android_server_UsbHostManager(JNIEnv *env)
194 {
195     jclass clazz = env->FindClass("com/android/server/usb/UsbHostManager");
196     if (clazz == NULL) {
197         ALOGE("Can't find com/android/server/usb/UsbHostManager");
198         return -1;
199     }
200     method_beginUsbDeviceAdded = env->GetMethodID(clazz, "beginUsbDeviceAdded",
201             "(Ljava/lang/String;IIIIILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z");
202     if (method_beginUsbDeviceAdded == NULL) {
203         ALOGE("Can't find beginUsbDeviceAdded");
204         return -1;
205     }
206     method_addUsbConfiguration = env->GetMethodID(clazz, "addUsbConfiguration",
207             "(ILjava/lang/String;II)V");
208     if (method_addUsbConfiguration == NULL) {
209         ALOGE("Can't find addUsbConfiguration");
210         return -1;
211     }
212     method_addUsbInterface = env->GetMethodID(clazz, "addUsbInterface",
213             "(ILjava/lang/String;IIII)V");
214     if (method_addUsbInterface == NULL) {
215         ALOGE("Can't find addUsbInterface");
216         return -1;
217     }
218     method_addUsbEndpoint = env->GetMethodID(clazz, "addUsbEndpoint", "(IIII)V");
219     if (method_addUsbEndpoint == NULL) {
220         ALOGE("Can't find addUsbEndpoint");
221         return -1;
222     }
223     method_endUsbDeviceAdded = env->GetMethodID(clazz, "endUsbDeviceAdded", "()V");
224     if (method_endUsbDeviceAdded == NULL) {
225         ALOGE("Can't find endUsbDeviceAdded");
226         return -1;
227     }
228     method_usbDeviceRemoved = env->GetMethodID(clazz, "usbDeviceRemoved",
229             "(Ljava/lang/String;)V");
230     if (method_usbDeviceRemoved == NULL) {
231         ALOGE("Can't find usbDeviceRemoved");
232         return -1;
233     }
234 
235     clazz = env->FindClass("android/os/ParcelFileDescriptor");
236     LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
237     gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
238     gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>",
239             "(Ljava/io/FileDescriptor;)V");
240     LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
241                  "Unable to find constructor for android.os.ParcelFileDescriptor");
242 
243     return jniRegisterNativeMethods(env, "com/android/server/usb/UsbHostManager",
244             method_table, NELEM(method_table));
245 }
246 
247 };
248