Home | History | Annotate | Download | only in jni
      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 #include "utils/Vector.h"
     25 
     26 #include <usbhost/usbhost.h>
     27 
     28 #include <stdio.h>
     29 #include <asm/byteorder.h>
     30 #include <sys/types.h>
     31 #include <sys/stat.h>
     32 #include <fcntl.h>
     33 #include <sys/ioctl.h>
     34 
     35 namespace android
     36 {
     37 
     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()) {
     49         ALOGE("An exception was thrown by callback '%s'.", methodName);
     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) {
     61         ALOGE("usb_device_open failed\n");
     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 
    111     jstring deviceName = env->NewStringUTF(devname);
    112     env->CallVoidMethod(thiz, method_usbDeviceAdded,
    113             deviceName, vendorId, productId, deviceClass,
    114             deviceSubClass, protocol, interfaceArray, endpointArray);
    115 
    116     env->DeleteLocalRef(interfaceArray);
    117     env->DeleteLocalRef(endpointArray);
    118     env->DeleteLocalRef(deviceName);
    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 
    128     jstring deviceName = env->NewStringUTF(devname);
    129     env->CallVoidMethod(thiz, method_usbDeviceRemoved, deviceName);
    130     env->DeleteLocalRef(deviceName);
    131     checkAndClearExceptionFromCallback(env, __FUNCTION__);
    132     return 0;
    133 }
    134 
    135 static void android_server_UsbHostManager_monitorUsbHostBus(JNIEnv *env, jobject thiz)
    136 {
    137     struct usb_host_context* context = usb_host_init();
    138     if (!context) {
    139         ALOGE("usb_host_init failed");
    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 
    146 static jobject android_server_UsbHostManager_openDevice(JNIEnv *env, jobject thiz, jstring deviceName)
    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 
    161     jobject fileDescriptor = jniCreateFileDescriptor(env, newFD);
    162     if (fileDescriptor == NULL) {
    163         return NULL;
    164     }
    165     return env->NewObject(gParcelFileDescriptorOffsets.mClass,
    166         gParcelFileDescriptorOffsets.mConstructor, fileDescriptor);
    167 }
    168 
    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 },
    173 };
    174 
    175 int register_android_server_UsbHostManager(JNIEnv *env)
    176 {
    177     jclass clazz = env->FindClass("com/android/server/usb/UsbHostManager");
    178     if (clazz == NULL) {
    179         ALOGE("Can't find com/android/server/usb/UsbHostManager");
    180         return -1;
    181     }
    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");
    185         return -1;
    186     }
    187     method_usbDeviceRemoved = env->GetMethodID(clazz, "usbDeviceRemoved", "(Ljava/lang/String;)V");
    188     if (method_usbDeviceRemoved == NULL) {
    189         ALOGE("Can't find usbDeviceRemoved");
    190         return -1;
    191     }
    192 
    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");
    199 
    200     return jniRegisterNativeMethods(env, "com/android/server/usb/UsbHostManager",
    201             method_table, NELEM(method_table));
    202 }
    203 
    204 };
    205