Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 1 | /* |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 17 | #define LOG_TAG "UsbHostManagerJNI" |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 18 | #include "utils/Log.h" |
| 19 | |
| 20 | #include "jni.h" |
| 21 | #include "JNIHelp.h" |
| 22 | #include "android_runtime/AndroidRuntime.h" |
Ruben Brunk | 87eac99 | 2013-09-09 17:44:59 -0700 | [diff] [blame^] | 23 | #include "android_runtime/Log.h" |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 24 | #include "utils/Vector.h" |
| 25 | |
| 26 | #include <usbhost/usbhost.h> |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <asm/byteorder.h> |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 30 | #include <sys/types.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <sys/ioctl.h> |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 34 | |
| 35 | namespace android |
| 36 | { |
| 37 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 38 | static struct parcel_file_descriptor_offsets_t |
| 39 | { |
| 40 | jclass mClass; |
| 41 | jmethodID mConstructor; |
| 42 | } gParcelFileDescriptorOffsets; |
| 43 | |
| 44 | static jmethodID method_usbDeviceAdded; |
| 45 | static jmethodID method_usbDeviceRemoved; |
| 46 | |
| 47 | static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 48 | if (env->ExceptionCheck()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 49 | ALOGE("An exception was thrown by callback '%s'.", methodName); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 50 | LOGE_EX(env); |
| 51 | env->ExceptionClear(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | static int usb_device_added(const char *devname, void* client_data) { |
| 56 | struct usb_descriptor_header* desc; |
| 57 | struct usb_descriptor_iter iter; |
| 58 | |
| 59 | struct usb_device *device = usb_device_open(devname); |
| 60 | if (!device) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 61 | ALOGE("usb_device_open failed\n"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
| 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); |
| 70 | |
| 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; |
| 76 | |
| 77 | usb_descriptor_iter_init(device, &iter); |
| 78 | |
| 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; |
| 82 | |
| 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; |
| 91 | |
| 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); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | usb_device_close(device); |
| 101 | |
| 102 | // handle generic device notification |
| 103 | int length = interfaceValues.size(); |
| 104 | jintArray interfaceArray = env->NewIntArray(length); |
| 105 | env->SetIntArrayRegion(interfaceArray, 0, length, interfaceValues.array()); |
| 106 | |
| 107 | length = endpointValues.size(); |
| 108 | jintArray endpointArray = env->NewIntArray(length); |
| 109 | env->SetIntArrayRegion(endpointArray, 0, length, endpointValues.array()); |
| 110 | |
Mike Lockwood | 8f8c602 | 2011-03-27 19:35:43 -0700 | [diff] [blame] | 111 | jstring deviceName = env->NewStringUTF(devname); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 112 | env->CallVoidMethod(thiz, method_usbDeviceAdded, |
Mike Lockwood | 8f8c602 | 2011-03-27 19:35:43 -0700 | [diff] [blame] | 113 | deviceName, vendorId, productId, deviceClass, |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 114 | deviceSubClass, protocol, interfaceArray, endpointArray); |
Mike Lockwood | 8f8c602 | 2011-03-27 19:35:43 -0700 | [diff] [blame] | 115 | |
| 116 | env->DeleteLocalRef(interfaceArray); |
| 117 | env->DeleteLocalRef(endpointArray); |
| 118 | env->DeleteLocalRef(deviceName); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 119 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int usb_device_removed(const char *devname, void* client_data) { |
| 125 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 126 | jobject thiz = (jobject)client_data; |
| 127 | |
Mike Lockwood | 8f8c602 | 2011-03-27 19:35:43 -0700 | [diff] [blame] | 128 | jstring deviceName = env->NewStringUTF(devname); |
Mike Lockwood | b3174c4 | 2011-06-09 14:48:11 -0400 | [diff] [blame] | 129 | env->CallVoidMethod(thiz, method_usbDeviceRemoved, deviceName); |
Mike Lockwood | 8f8c602 | 2011-03-27 19:35:43 -0700 | [diff] [blame] | 130 | env->DeleteLocalRef(deviceName); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 131 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 132 | return 0; |
| 133 | } |
| 134 | |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 135 | static void android_server_UsbHostManager_monitorUsbHostBus(JNIEnv *env, jobject thiz) |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 136 | { |
| 137 | struct usb_host_context* context = usb_host_init(); |
| 138 | if (!context) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 139 | ALOGE("usb_host_init failed"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 140 | return; |
| 141 | } |
| 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); |
| 144 | } |
| 145 | |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 146 | static jobject android_server_UsbHostManager_openDevice(JNIEnv *env, jobject thiz, jstring deviceName) |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 147 | { |
| 148 | const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL); |
| 149 | struct usb_device* device = usb_device_open(deviceNameStr); |
| 150 | env->ReleaseStringUTFChars(deviceName, deviceNameStr); |
| 151 | |
| 152 | if (!device) |
| 153 | return NULL; |
| 154 | |
| 155 | int fd = usb_device_get_fd(device); |
| 156 | if (fd < 0) |
| 157 | return NULL; |
| 158 | int newFD = dup(fd); |
| 159 | usb_device_close(device); |
| 160 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 161 | jobject fileDescriptor = jniCreateFileDescriptor(env, newFD); |
| 162 | if (fileDescriptor == NULL) { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 163 | return NULL; |
| 164 | } |
| 165 | return env->NewObject(gParcelFileDescriptorOffsets.mClass, |
| 166 | gParcelFileDescriptorOffsets.mConstructor, fileDescriptor); |
| 167 | } |
| 168 | |
| 169 | static JNINativeMethod method_table[] = { |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 170 | { "monitorUsbHostBus", "()V", (void*)android_server_UsbHostManager_monitorUsbHostBus }, |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 171 | { "nativeOpenDevice", "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;", |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 172 | (void*)android_server_UsbHostManager_openDevice }, |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 173 | }; |
| 174 | |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 175 | int register_android_server_UsbHostManager(JNIEnv *env) |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 176 | { |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 177 | jclass clazz = env->FindClass("com/android/server/usb/UsbHostManager"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 178 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 179 | ALOGE("Can't find com/android/server/usb/UsbHostManager"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 180 | return -1; |
| 181 | } |
| 182 | method_usbDeviceAdded = env->GetMethodID(clazz, "usbDeviceAdded", "(Ljava/lang/String;IIIII[I[I)V"); |
| 183 | if (method_usbDeviceAdded == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 184 | ALOGE("Can't find usbDeviceAdded"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | method_usbDeviceRemoved = env->GetMethodID(clazz, "usbDeviceRemoved", "(Ljava/lang/String;)V"); |
| 188 | if (method_usbDeviceRemoved == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 189 | ALOGE("Can't find usbDeviceRemoved"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 190 | return -1; |
| 191 | } |
| 192 | |
Brian Carlstrom | 46e18c11 | 2011-04-05 22:44:45 -0700 | [diff] [blame] | 193 | clazz = env->FindClass("android/os/ParcelFileDescriptor"); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 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"); |
| 199 | |
Mike Lockwood | 46d0adf | 2011-05-26 10:27:39 -0400 | [diff] [blame] | 200 | return jniRegisterNativeMethods(env, "com/android/server/usb/UsbHostManager", |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 201 | method_table, NELEM(method_table)); |
| 202 | } |
| 203 | |
| 204 | }; |