Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 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 | |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | |
Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 22 | #include "jni.h" |
Steven Moreland | 60cc6c0 | 2017-08-25 15:49:25 -0700 | [diff] [blame] | 23 | #include <nativehelper/JNIHelp.h> |
Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 24 | |
| 25 | #include <usbhost/usbhost.h> |
| 26 | |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 27 | #define MAX_DESCRIPTORS_LENGTH 4096 |
Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 28 | |
| 29 | // com.android.server.usb.descriptors |
| 30 | extern "C" { |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 31 | jbyteArray JNICALL Java_com_android_server_usb_descriptors_UsbDescriptorParser_getRawDescriptors_1native( |
Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 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) { |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 44 | usb_device_close(device); |
Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 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)); |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 52 | jbyteArray ret = NULL; |
Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 53 | usb_device_close(device); |
| 54 | |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 55 | if (numBytes > 0) { |
Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 56 | ret = env->NewByteArray(numBytes); |
| 57 | env->SetByteArrayRegion(ret, 0, numBytes, buffer); |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 58 | } else { |
| 59 | ALOGE("error reading descriptors\n"); |
Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 60 | } |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 61 | |
Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 62 | return ret; |
| 63 | } |
| 64 | |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 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 | |
Paul McLean | 6dfa25a | 2017-12-19 12:14:36 -0600 | [diff] [blame] | 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); |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 90 | |
Paul McLean | 6dfa25a | 2017-12-19 12:14:36 -0600 | [diff] [blame] | 91 | jstring j_str = NULL; |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 92 | |
Paul McLean | 6dfa25a | 2017-12-19 12:14:36 -0600 | [diff] [blame] | 93 | if (retVal == 0) { |
| 94 | j_str = env->NewString((jchar*)byteBuffer, numUSC2Bytes/2); |
| 95 | free(byteBuffer); |
| 96 | } |
Paul McLean | 63b4251 | 2018-01-09 12:55:47 -0800 | [diff] [blame] | 97 | |
| 98 | usb_device_close(device); |
| 99 | |
Paul Mclean | 9d84fee | 2017-11-17 13:51:19 +0000 | [diff] [blame] | 100 | return j_str; |
| 101 | } |
| 102 | |
Paul McLean | b5eaa80 | 2017-06-07 16:48:32 -0600 | [diff] [blame] | 103 | } // extern "C" |