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