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 | |
| 17 | #define LOG_TAG "UsbService" |
| 18 | #include "utils/Log.h" |
| 19 | |
| 20 | #include "jni.h" |
| 21 | #include "JNIHelp.h" |
| 22 | #include "android_runtime/AndroidRuntime.h" |
| 23 | #include "utils/Vector.h" |
| 24 | |
| 25 | #include <usbhost/usbhost.h> |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <asm/byteorder.h> |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame^] | 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <linux/usb/f_accessory.h> |
| 34 | |
| 35 | #define DRIVER_NAME "/dev/usb_accessory" |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 36 | |
| 37 | namespace android |
| 38 | { |
| 39 | |
| 40 | static struct file_descriptor_offsets_t |
| 41 | { |
| 42 | jclass mClass; |
| 43 | jmethodID mConstructor; |
| 44 | jfieldID mDescriptor; |
| 45 | } gFileDescriptorOffsets; |
| 46 | |
| 47 | static struct parcel_file_descriptor_offsets_t |
| 48 | { |
| 49 | jclass mClass; |
| 50 | jmethodID mConstructor; |
| 51 | } gParcelFileDescriptorOffsets; |
| 52 | |
| 53 | static jmethodID method_usbDeviceAdded; |
| 54 | static jmethodID method_usbDeviceRemoved; |
| 55 | |
| 56 | static void checkAndClearExceptionFromCallback(JNIEnv* env, const char* methodName) { |
| 57 | if (env->ExceptionCheck()) { |
| 58 | LOGE("An exception was thrown by callback '%s'.", methodName); |
| 59 | LOGE_EX(env); |
| 60 | env->ExceptionClear(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static int usb_device_added(const char *devname, void* client_data) { |
| 65 | struct usb_descriptor_header* desc; |
| 66 | struct usb_descriptor_iter iter; |
| 67 | |
| 68 | struct usb_device *device = usb_device_open(devname); |
| 69 | if (!device) { |
| 70 | LOGE("usb_device_open failed\n"); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 75 | jobject thiz = (jobject)client_data; |
| 76 | Vector<int> interfaceValues; |
| 77 | Vector<int> endpointValues; |
| 78 | const usb_device_descriptor* deviceDesc = usb_device_get_device_descriptor(device); |
| 79 | |
| 80 | uint16_t vendorId = usb_device_get_vendor_id(device); |
| 81 | uint16_t productId = usb_device_get_product_id(device); |
| 82 | uint8_t deviceClass = deviceDesc->bDeviceClass; |
| 83 | uint8_t deviceSubClass = deviceDesc->bDeviceSubClass; |
| 84 | uint8_t protocol = deviceDesc->bDeviceProtocol; |
| 85 | |
| 86 | usb_descriptor_iter_init(device, &iter); |
| 87 | |
| 88 | while ((desc = usb_descriptor_iter_next(&iter)) != NULL) { |
| 89 | if (desc->bDescriptorType == USB_DT_INTERFACE) { |
| 90 | struct usb_interface_descriptor *interface = (struct usb_interface_descriptor *)desc; |
| 91 | |
| 92 | // push class, subclass, protocol and number of endpoints into interfaceValues vector |
| 93 | interfaceValues.add(interface->bInterfaceNumber); |
| 94 | interfaceValues.add(interface->bInterfaceClass); |
| 95 | interfaceValues.add(interface->bInterfaceSubClass); |
| 96 | interfaceValues.add(interface->bInterfaceProtocol); |
| 97 | interfaceValues.add(interface->bNumEndpoints); |
| 98 | } else if (desc->bDescriptorType == USB_DT_ENDPOINT) { |
| 99 | struct usb_endpoint_descriptor *endpoint = (struct usb_endpoint_descriptor *)desc; |
| 100 | |
| 101 | // push address, attributes, max packet size and interval into endpointValues vector |
| 102 | endpointValues.add(endpoint->bEndpointAddress); |
| 103 | endpointValues.add(endpoint->bmAttributes); |
| 104 | endpointValues.add(__le16_to_cpu(endpoint->wMaxPacketSize)); |
| 105 | endpointValues.add(endpoint->bInterval); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | usb_device_close(device); |
| 110 | |
| 111 | // handle generic device notification |
| 112 | int length = interfaceValues.size(); |
| 113 | jintArray interfaceArray = env->NewIntArray(length); |
| 114 | env->SetIntArrayRegion(interfaceArray, 0, length, interfaceValues.array()); |
| 115 | |
| 116 | length = endpointValues.size(); |
| 117 | jintArray endpointArray = env->NewIntArray(length); |
| 118 | env->SetIntArrayRegion(endpointArray, 0, length, endpointValues.array()); |
| 119 | |
| 120 | env->CallVoidMethod(thiz, method_usbDeviceAdded, |
| 121 | env->NewStringUTF(devname), vendorId, productId, deviceClass, |
| 122 | deviceSubClass, protocol, interfaceArray, endpointArray); |
| 123 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static int usb_device_removed(const char *devname, void* client_data) { |
| 129 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 130 | jobject thiz = (jobject)client_data; |
| 131 | |
| 132 | env->CallVoidMethod(thiz, method_usbDeviceRemoved, env->NewStringUTF(devname)); |
| 133 | checkAndClearExceptionFromCallback(env, __FUNCTION__); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static void android_server_UsbService_monitorUsbHostBus(JNIEnv *env, jobject thiz) |
| 138 | { |
| 139 | struct usb_host_context* context = usb_host_init(); |
| 140 | if (!context) { |
| 141 | LOGE("usb_host_init failed"); |
| 142 | return; |
| 143 | } |
| 144 | // this will never return so it is safe to pass thiz directly |
| 145 | usb_host_run(context, usb_device_added, usb_device_removed, NULL, (void *)thiz); |
| 146 | } |
| 147 | |
| 148 | static jobject android_server_UsbService_openDevice(JNIEnv *env, jobject thiz, jstring deviceName) |
| 149 | { |
| 150 | const char *deviceNameStr = env->GetStringUTFChars(deviceName, NULL); |
| 151 | struct usb_device* device = usb_device_open(deviceNameStr); |
| 152 | env->ReleaseStringUTFChars(deviceName, deviceNameStr); |
| 153 | |
| 154 | if (!device) |
| 155 | return NULL; |
| 156 | |
| 157 | int fd = usb_device_get_fd(device); |
| 158 | if (fd < 0) |
| 159 | return NULL; |
| 160 | int newFD = dup(fd); |
| 161 | usb_device_close(device); |
| 162 | |
| 163 | jobject fileDescriptor = env->NewObject(gFileDescriptorOffsets.mClass, |
| 164 | gFileDescriptorOffsets.mConstructor); |
| 165 | if (fileDescriptor != NULL) { |
| 166 | env->SetIntField(fileDescriptor, gFileDescriptorOffsets.mDescriptor, newFD); |
| 167 | } else { |
| 168 | return NULL; |
| 169 | } |
| 170 | return env->NewObject(gParcelFileDescriptorOffsets.mClass, |
| 171 | gParcelFileDescriptorOffsets.mConstructor, fileDescriptor); |
| 172 | } |
| 173 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame^] | 174 | static void set_accessory_string(JNIEnv *env, int fd, int cmd, jobjectArray strArray, int index) |
| 175 | { |
| 176 | char buffer[256]; |
| 177 | |
| 178 | buffer[0] = 0; |
| 179 | int length = ioctl(fd, cmd, buffer); |
| 180 | LOGD("ioctl returned %d", length); |
| 181 | if (buffer[0]) { |
| 182 | jstring obj = env->NewStringUTF(buffer); |
| 183 | env->SetObjectArrayElement(strArray, index, obj); |
| 184 | env->DeleteLocalRef(obj); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | |
| 189 | static jobjectArray android_server_UsbService_getAccessoryStrings(JNIEnv *env, jobject thiz) |
| 190 | { |
| 191 | int fd = open(DRIVER_NAME, O_RDWR); |
| 192 | if (fd < 0) { |
| 193 | LOGE("could not open %s", DRIVER_NAME); |
| 194 | return NULL; |
| 195 | } |
| 196 | jclass stringClass = env->FindClass("java/lang/String"); |
| 197 | jobjectArray strArray = env->NewObjectArray(4, stringClass, NULL); |
| 198 | if (!strArray) goto out; |
| 199 | set_accessory_string(env, fd, ACCESSORY_GET_STRING_MANUFACTURER, strArray, 0); |
| 200 | set_accessory_string(env, fd, ACCESSORY_GET_STRING_MODEL, strArray, 1); |
| 201 | set_accessory_string(env, fd, ACCESSORY_GET_STRING_TYPE, strArray, 2); |
| 202 | set_accessory_string(env, fd, ACCESSORY_GET_STRING_VERSION, strArray, 3); |
| 203 | |
| 204 | out: |
| 205 | close(fd); |
| 206 | return strArray; |
| 207 | } |
| 208 | |
| 209 | static jobject android_server_UsbService_openAccessory(JNIEnv *env, jobject thiz) |
| 210 | { |
| 211 | int fd = open(DRIVER_NAME, O_RDWR); |
| 212 | if (fd < 0) { |
| 213 | LOGE("could not open %s", DRIVER_NAME); |
| 214 | return NULL; |
| 215 | } |
| 216 | jobject fileDescriptor = env->NewObject(gFileDescriptorOffsets.mClass, |
| 217 | gFileDescriptorOffsets.mConstructor); |
| 218 | if (fileDescriptor != NULL) { |
| 219 | env->SetIntField(fileDescriptor, gFileDescriptorOffsets.mDescriptor, fd); |
| 220 | } else { |
| 221 | return NULL; |
| 222 | } |
| 223 | return env->NewObject(gParcelFileDescriptorOffsets.mClass, |
| 224 | gParcelFileDescriptorOffsets.mConstructor, fileDescriptor); |
| 225 | } |
| 226 | |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 227 | static JNINativeMethod method_table[] = { |
| 228 | { "monitorUsbHostBus", "()V", (void*)android_server_UsbService_monitorUsbHostBus }, |
| 229 | { "nativeOpenDevice", "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;", |
| 230 | (void*)android_server_UsbService_openDevice }, |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame^] | 231 | { "nativeGetAccessoryStrings", "()[Ljava/lang/String;", |
| 232 | (void*)android_server_UsbService_getAccessoryStrings }, |
| 233 | { "nativeOpenAccessory","()Landroid/os/ParcelFileDescriptor;", |
| 234 | (void*)android_server_UsbService_openAccessory }, |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | int register_android_server_UsbService(JNIEnv *env) |
| 238 | { |
| 239 | jclass clazz = env->FindClass("com/android/server/UsbService"); |
| 240 | if (clazz == NULL) { |
| 241 | LOGE("Can't find com/android/server/UsbService"); |
| 242 | return -1; |
| 243 | } |
| 244 | method_usbDeviceAdded = env->GetMethodID(clazz, "usbDeviceAdded", "(Ljava/lang/String;IIIII[I[I)V"); |
| 245 | if (method_usbDeviceAdded == NULL) { |
| 246 | LOGE("Can't find usbDeviceAdded"); |
| 247 | return -1; |
| 248 | } |
| 249 | method_usbDeviceRemoved = env->GetMethodID(clazz, "usbDeviceRemoved", "(Ljava/lang/String;)V"); |
| 250 | if (method_usbDeviceRemoved == NULL) { |
| 251 | LOGE("Can't find usbDeviceRemoved"); |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | clazz = env->FindClass("java/io/FileDescriptor"); |
| 256 | LOG_FATAL_IF(clazz == NULL, "Unable to find class java.io.FileDescriptor"); |
| 257 | gFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz); |
| 258 | gFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "()V"); |
| 259 | gFileDescriptorOffsets.mDescriptor = env->GetFieldID(clazz, "descriptor", "I"); |
| 260 | LOG_FATAL_IF(gFileDescriptorOffsets.mDescriptor == NULL, |
| 261 | "Unable to find descriptor field in java.io.FileDescriptor"); |
| 262 | |
| 263 | clazz = env->FindClass("android/os/ParcelFileDescriptor"); |
| 264 | LOG_FATAL_IF(clazz == NULL, "Unable to find class android.os.ParcelFileDescriptor"); |
| 265 | gParcelFileDescriptorOffsets.mClass = (jclass) env->NewGlobalRef(clazz); |
| 266 | gParcelFileDescriptorOffsets.mConstructor = env->GetMethodID(clazz, "<init>", "(Ljava/io/FileDescriptor;)V"); |
| 267 | LOG_FATAL_IF(gParcelFileDescriptorOffsets.mConstructor == NULL, |
| 268 | "Unable to find constructor for android.os.ParcelFileDescriptor"); |
| 269 | |
| 270 | return jniRegisterNativeMethods(env, "com/android/server/UsbService", |
| 271 | method_table, NELEM(method_table)); |
| 272 | } |
| 273 | |
| 274 | }; |