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 "jni.h"
     21 #include "JNIHelp.h"
     22 
     23 #include <usbhost/usbhost.h>
     24 
     25 #define MAX_DESCRIPTORS_LENGTH 16384
     26 
     27 // com.android.server.usb.descriptors
     28 extern "C" {
     29 jbyteArray JNICALL Java_com_android_server_usb_descriptors_UsbDescriptorParser_getRawDescriptors(
     30         JNIEnv* env, jobject thiz, jstring deviceAddr) {
     31     const char *deviceAddrStr = env->GetStringUTFChars(deviceAddr, NULL);
     32     struct usb_device* device = usb_device_open(deviceAddrStr);
     33     env->ReleaseStringUTFChars(deviceAddr, deviceAddrStr);
     34 
     35     if (!device) {
     36         ALOGE("usb_device_open failed");
     37         return NULL;
     38     }
     39 
     40     int fd = usb_device_get_fd(device);
     41     if (fd < 0) {
     42         return NULL;
     43     }
     44 
     45     // from android_hardware_UsbDeviceConnection_get_desc()
     46     jbyte buffer[MAX_DESCRIPTORS_LENGTH];
     47     lseek(fd, 0, SEEK_SET);
     48     int numBytes = read(fd, buffer, sizeof(buffer));
     49 
     50     usb_device_close(device);
     51 
     52     jbyteArray ret = NULL;
     53     if (numBytes != 0) {
     54         ret = env->NewByteArray(numBytes);
     55         env->SetByteArrayRegion(ret, 0, numBytes, buffer);
     56     }
     57     return ret;
     58 }
     59 
     60 } // extern "C"
     61 
     62 
     63