Merge "Revert "Access already-read descriptors on device connect. (Eliminate extra read)""
diff --git a/services/core/jni/com_android_server_UsbHostManager.cpp b/services/core/jni/com_android_server_UsbHostManager.cpp
index 24f2014..11f508b 100644
--- a/services/core/jni/com_android_server_UsbHostManager.cpp
+++ b/services/core/jni/com_android_server_UsbHostManager.cpp
@@ -65,15 +65,28 @@
int subClassID = deviceDesc->bDeviceSubClass;
// get the raw descriptors
- int numBytes = usb_device_get_descriptors_length(device);
+ int fd = usb_device_get_fd(device);
+ if (fd < 0) {
+ ALOGE("usb_device_get_fd failed\n");
+ usb_device_close(device);
+ // TODO return an error code here?
+ return 0;
+ }
+
+ // from android_hardware_UsbDeviceConnection_get_desc()
+ jbyte rawdescriptors[MAX_DESCRIPTORS_LENGTH];
+ lseek(fd, 0, SEEK_SET);
+ int numBytes = read(fd, rawdescriptors, sizeof(rawdescriptors));
+
+ usb_device_close(device);
+
if (numBytes > 0) {
JNIEnv* env = AndroidRuntime::getJNIEnv();
jobject thiz = (jobject)clientData;
jstring deviceAddress = env->NewStringUTF(devAddress);
jbyteArray descriptorsArray = env->NewByteArray(numBytes);
- const jbyte* rawDescriptors = (const jbyte*)usb_device_get_raw_descriptors(device);
- env->SetByteArrayRegion(descriptorsArray, 0, numBytes, rawDescriptors);
+ env->SetByteArrayRegion(descriptorsArray, 0, numBytes, rawdescriptors);
env->CallBooleanMethod(thiz, method_usbDeviceAdded,
deviceAddress, classID, subClassID, descriptorsArray);
@@ -87,8 +100,6 @@
ALOGE("error reading descriptors\n");
}
- usb_device_close(device);
-
return 0;
}