blob: 62bab073ac1ddfd7f75334ea8d6a00fa8c546dcc [file] [log] [blame]
Paul McLeanb5eaa802017-06-07 16:48:32 -06001/*
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
Paul Mclean9d84fee2017-11-17 13:51:19 +000020#include <stdlib.h>
21
Paul McLeanb5eaa802017-06-07 16:48:32 -060022#include "jni.h"
Steven Moreland60cc6c02017-08-25 15:49:25 -070023#include <nativehelper/JNIHelp.h>
Paul McLeanb5eaa802017-06-07 16:48:32 -060024
25#include <usbhost/usbhost.h>
26
Paul Mclean9d84fee2017-11-17 13:51:19 +000027#define MAX_DESCRIPTORS_LENGTH 4096
Paul McLeanb5eaa802017-06-07 16:48:32 -060028
29// com.android.server.usb.descriptors
30extern "C" {
Paul Mclean9d84fee2017-11-17 13:51:19 +000031jbyteArray JNICALL Java_com_android_server_usb_descriptors_UsbDescriptorParser_getRawDescriptors_1native(
Paul McLeanb5eaa802017-06-07 16:48:32 -060032 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) {
Paul Mclean9d84fee2017-11-17 13:51:19 +000044 usb_device_close(device);
Paul McLeanb5eaa802017-06-07 16:48:32 -060045 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));
Paul Mclean9d84fee2017-11-17 13:51:19 +000052 jbyteArray ret = NULL;
Paul McLeanb5eaa802017-06-07 16:48:32 -060053 usb_device_close(device);
54
Paul Mclean9d84fee2017-11-17 13:51:19 +000055 if (numBytes > 0) {
Paul McLeanb5eaa802017-06-07 16:48:32 -060056 ret = env->NewByteArray(numBytes);
57 env->SetByteArrayRegion(ret, 0, numBytes, buffer);
Paul Mclean9d84fee2017-11-17 13:51:19 +000058 } else {
59 ALOGE("error reading descriptors\n");
Paul McLeanb5eaa802017-06-07 16:48:32 -060060 }
Paul Mclean9d84fee2017-11-17 13:51:19 +000061
Paul McLeanb5eaa802017-06-07 16:48:32 -060062 return ret;
63}
64
Paul Mclean9d84fee2017-11-17 13:51:19 +000065jstring 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
Paul McLean6dfa25a2017-12-19 12:14:36 -060084 // 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);
Paul Mclean9d84fee2017-11-17 13:51:19 +000090
Paul McLean6dfa25a2017-12-19 12:14:36 -060091 jstring j_str = NULL;
Paul Mclean9d84fee2017-11-17 13:51:19 +000092
Paul McLean6dfa25a2017-12-19 12:14:36 -060093 if (retVal == 0) {
94 j_str = env->NewString((jchar*)byteBuffer, numUSC2Bytes/2);
95 free(byteBuffer);
96 }
Paul McLean63b42512018-01-09 12:55:47 -080097
98 usb_device_close(device);
99
Paul Mclean9d84fee2017-11-17 13:51:19 +0000100 return j_str;
101}
102
Paul McLeanb5eaa802017-06-07 16:48:32 -0600103} // extern "C"