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