Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2017 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 <stdlib.h>
     21 
     22 #include "jni.h"
     23 #include <nativehelper/JNIHelp.h>
     24 
     25 #include <usbhost/usbhost.h>
     26 
     27 #define MAX_DESCRIPTORS_LENGTH 4096
     28 
     29 // com.android.server.usb.descriptors
     30 extern "C" {
     31 jbyteArray JNICALL Java_com_android_server_usb_descriptors_UsbDescriptorParser_getRawDescriptors_1native(
     32         JNIEnv* env, jobject thiz, jstring deviceAddr) {
     33     const char *deviceAddrStr = env->GetStringUTFChars(deviceAddr, NULL);
     34     struct usb_device* device = usb_device_open(deviceAddrStr);
     35     env->ReleaseStringUTFChars(deviceAddr, deviceAddrStr);
     36 
     37     if (!device) {
     38         ALOGE("usb_device_open failed");
     39         return NULL;
     40     }
     41 
     42     int fd = usb_device_get_fd(device);
     43     if (fd < 0) {
     44         usb_device_close(device);
     45         return NULL;
     46     }
     47 
     48     // from android_hardware_UsbDeviceConnection_get_desc()
     49     jbyte buffer[MAX_DESCRIPTORS_LENGTH];
     50     lseek(fd, 0, SEEK_SET);
     51     int numBytes = read(fd, buffer, sizeof(buffer));
     52     jbyteArray ret = NULL;
     53     usb_device_close(device);
     54 
     55     if (numBytes > 0) {
     56         ret = env->NewByteArray(numBytes);
     57         env->SetByteArrayRegion(ret, 0, numBytes, buffer);
     58     } else {
     59         ALOGE("error reading descriptors\n");
     60     }
     61 
     62     return ret;
     63 }
     64 
     65 jstring JNICALL Java_com_android_server_usb_descriptors_UsbDescriptorParser_getDescriptorString_1native(
     66         JNIEnv* env, jobject thiz, jstring deviceAddr, jint stringId) {
     67 
     68     const char *deviceAddrStr = env->GetStringUTFChars(deviceAddr, NULL);
     69     struct usb_device* device = usb_device_open(deviceAddrStr);
     70     env->ReleaseStringUTFChars(deviceAddr, deviceAddrStr);
     71 
     72     if (!device) {
     73         ALOGE("usb_device_open failed");
     74         return NULL;
     75     }
     76 
     77     int fd = usb_device_get_fd(device);
     78     if (fd < 0) {
     79         ALOGE("usb_device_get_fd failed");
     80         usb_device_close(device);
     81         return NULL;
     82     }
     83 
     84     // Get Raw UCS2 Bytes
     85     jbyte* byteBuffer = NULL;
     86     size_t numUSC2Bytes = 0;
     87     int retVal =
     88             usb_device_get_string_ucs2(device, stringId, 0 /*timeout*/,
     89                                      (void**)&byteBuffer, &numUSC2Bytes);
     90 
     91     jstring j_str = NULL;
     92 
     93     if (retVal == 0) {
     94         j_str = env->NewString((jchar*)byteBuffer, numUSC2Bytes/2);
     95         free(byteBuffer);
     96     }
     97 
     98     usb_device_close(device);
     99 
    100     return j_str;
    101 }
    102 
    103 } // extern "C"
    104