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 "UsbHostManagerJNI"
18 #include "utils/Log.h"
22 #include "android_runtime/AndroidRuntime.h"
23 #include "android_runtime/Log.h"
24 #include "utils/Vector.h"
26 #include <usbhost/usbhost.h>
29 #include <asm/byteorder.h>
30 #include <sys/types.h>
33 #include <sys/ioctl.h>
38 static struct parcel_file_descriptor_offsets_t
41 jmethodID mConstructor;
42 } gParcelFileDescriptorOffsets;
44 static jmethodID method_usbDeviceAdded;
45 static jmethodID method_usbDeviceRemoved;
47 static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) {
48 if (env->ExceptionCheck()) {
49 ALOGE("An exception was thrown by callback '%s'.", methodName);
51 env->ExceptionClear();
55 static int usb_device_added(const char *devname, void* client_data) {
56 struct usb_descriptor_header* desc;
57 struct usb_descriptor_iter iter;
59 struct usb_device *device = usb_device_open(devname);
61 ALOGE("usb_device_open failed\n");
65 JNIEnv* env = AndroidRuntime::getJNIEnv();
66 jobject thiz = (jobject)client_data;
67 Vector<int> interfaceValues;
68 Vector<int> endpointValues;
69 const usb_device_descriptor* deviceDesc = usb_device_get_device_descriptor(device);
71 uint16_t vendorId = usb_device_get_vendor_id(device);
72 uint16_t productId = usb_device_get_product_id(device);
73 uint8_t deviceClass = deviceDesc->bDeviceClass;
74 uint8_t deviceSubClass = deviceDesc->bDeviceSubClass;
75 uint8_t protocol = deviceDesc->bDeviceProtocol;
77 usb_descriptor_iter_init(device, &iter);
79 while ((desc = usb_descriptor_iter_next(&iter)) != NULL) {
80 if (desc->bDescriptorType == USB_DT_INTERFACE) {
81 struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc;
83 // push class, subclass, protocol and number of endpoints into interfaceValues vector
84 interfaceValues.add(interface->bInterfaceNumber);
85 interfaceValues.add(interface->bInterfaceClass);
86 interfaceValues.add(interface->bInterfaceSubClass);
87 interfaceValues.add(interface->bInterfaceProtocol);
88 interfaceValues.add(interface->bNumEndpoints);
89 } else if (desc->bDescriptorType == USB_DT_ENDPOINT) {
90 struct usb_endpoint_descriptor *endpoint = (struct usb_endpoint_descriptor *)desc;
92 // push address, attributes, max packet size and interval into endpointValues vector
93 endpointValues.add(endpoint->bEndpointAddress);
94 endpointValues.add(endpoint->bmAttributes);
95 endpointValues.add(__le16_to_cpu(endpoint->wMaxPacketSize));
96 endpointValues.add(endpoint->bInterval);
100 usb_device_close(device);
102 // handle generic device notification
103 int length = interfaceValues.size();
104 jintArray interfaceArray = env->NewIntArray(length);
105 env->SetIntArrayRegion(interfaceArray, 0, length, interfaceValues.array());
107 length = endpointValues.size();
108 jintArray endpointArray = env->NewIntArray(length);
109 env->SetIntArrayRegion(endpointArray, 0, length, endpointValues.array());
111 jstring deviceName = env->NewStringUTF(devname);
112 env->CallVoidMethod(thiz, method_usbDeviceAdded,
113 deviceName, vendorId, productId, deviceClass,
114 deviceSubClass, protocol, interfaceArray, endpointArray);
116 env->DeleteLocalRef(interfaceArray);
117 env->DeleteLocalRef(endpointArray);
118 env->DeleteLocalRef(deviceName);
119 checkAndClearExceptionFromCallback(env, __FUNCTION__);
124 static int usb_device_removed(const char *devname, void* client_data) {
125 JNIEnv* env = AndroidRuntime::getJNIEnv();
126 jobject thiz = (jobject)client_data;
128 jstring deviceName = env->NewStringUTF(devname);
129 env->CallVoidMethod(thiz, method_usbDeviceRemoved, deviceName);
130 env->DeleteLocalRef(deviceName);
131 checkAndClearExceptionFromCallback(env, __FUNCTION__);
135 static void android_server_UsbHostManager_monitorUsbHostBus(JNIEnv *env, jobject thiz)
137 struct usb_host_context* context = usb_host_init();
139 ALOGE("usb_host_init failed");
142 // this will never return so it is safe to pass thiz directly
143 usb_host_run(context, usb_device_added, usb_device_removed, NULL, (void *)thiz);
146 static jobject android_server_UsbHostManager_openDevice(JNIEnv *env, jobject thiz, jstring deviceName)
148 const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL);
149 struct usb_device* device = usb_device_open(deviceNameStr);
150 env->ReleaseStringUTFChars(deviceName, deviceNameStr);
155 int fd = usb_device_get_fd(device);
159 usb_device_close(device);
161 jobject fileDescriptor = jniCreateFileDescriptor(env, newFD);
162 if (fileDescriptor == NULL) {
165 return env->NewObject(gParcelFileDescriptorOffsets.mClass,
166 gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
169 static JNINativeMethod method_table[] = {
170 { "monitorUsbHostBus", "()V", (void*)android_server_UsbHostManager_monitorUsbHostBus },
171 { "nativeOpenDevice", "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;",
172 (void*)android_server_UsbHostManager_openDevice },
175 int register_android_server_UsbHostManager(JNIEnv *env)
177 jclass clazz = env->FindClass("com/android/server/usb/UsbHostManager");
179 ALOGE("Can't find com/android/server/usb/UsbHostManager");
182 method_usbDeviceAdded = env->GetMethodID(clazz, "usbDeviceAdded", "(Ljava/lang/String;IIIII[I[I)V");
183 if (method_usbDeviceAdded == NULL) {
184 ALOGE("Can't find usbDeviceAdded");
187 method_usbDeviceRemoved = env->GetMethodID(clazz, "usbDeviceRemoved", "(Ljava/lang/String;)V");
188 if (method_usbDeviceRemoved == NULL) {
189 ALOGE("Can't find usbDeviceRemoved");
193 clazz = env->FindClass("android/os/ParcelFileDescriptor");
194 LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor");
195 gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz);
196 gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V");
197 LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL,
198 "Unable to find constructor for android.os.ParcelFileDescriptor");
200 return jniRegisterNativeMethods(env, "com/android/server/usb/UsbHostManager",
201 method_table, NELEM(method_table));