Merge "Convert VtsHalUsbGadgetV1_0HostTest to java test" into rvc-dev am: 16920dd784
Change-Id: I8fc782cc7f75861f651c7ccfe5d2ef00b7cb9616
diff --git a/usb/gadget/V1_0/host/Android.bp b/usb/gadget/V1_0/host/Android.bp
index 4d84c3d..db9b24d 100644
--- a/usb/gadget/V1_0/host/Android.bp
+++ b/usb/gadget/V1_0/host/Android.bp
@@ -16,3 +16,19 @@
vts_config {
name: "VtsHalUsbGadgetV1_0Host",
}
+
+java_test_host {
+ name: "HalUsbGadgetV1_0HostTest",
+ libs: [
+ "tradefed",
+ "tradefed-common-util",
+ ],
+ static_libs: [
+ "jna-prebuilt",
+ ],
+ srcs: ["src/**/*.java"],
+ test_suites: [
+ "vts",
+ ],
+ auto_gen_config: true,
+}
diff --git a/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/HalUsbGadgetV1_0HostTest.java b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/HalUsbGadgetV1_0HostTest.java
new file mode 100644
index 0000000..38f0274
--- /dev/null
+++ b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/HalUsbGadgetV1_0HostTest.java
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.tests.usbgadget;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeTrue;
+
+import com.android.tests.usbgadget.libusb.ConfigDescriptor;
+import com.android.tests.usbgadget.libusb.DeviceDescriptor;
+import com.android.tests.usbgadget.libusb.IUsbNative;
+import com.android.tests.usbgadget.libusb.Interface;
+import com.android.tests.usbgadget.libusb.InterfaceDescriptor;
+import com.android.tradefed.invoker.TestInformation;
+import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
+import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
+import com.android.tradefed.testtype.junit4.BeforeClassWithInfo;
+import com.google.common.base.Strings;
+import com.sun.jna.Native;
+import com.sun.jna.Pointer;
+import com.sun.jna.ptr.PointerByReference;
+import java.util.Arrays;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/** A host-side test for USB Gadget HAL */
+@RunWith(DeviceJUnit4ClassRunner.class)
+public class HalUsbGadgetV1_0HostTest extends BaseHostJUnit4Test {
+ private static final int WAIT_TIME = 3000;
+ private static final String HAL_SERVICE = "android.hardware.usb.gadget@1.0::IUsbGadget";
+
+ private static boolean mHasService;
+ private static IUsbNative mUsb;
+ private static Pointer mContext;
+
+ @BeforeClassWithInfo
+ public static void beforeClassWithDevice(TestInformation testInfo) throws Exception {
+ String serviceFound =
+ testInfo.getDevice()
+ .executeShellCommand(String.format("lshal | grep \"%s\"", HAL_SERVICE))
+ .trim();
+ mHasService = !Strings.isNullOrEmpty(serviceFound);
+
+ if (mHasService) {
+ mUsb = (IUsbNative) Native.loadLibrary("usb-1.0", IUsbNative.class);
+ PointerByReference context = new PointerByReference();
+ mUsb.libusb_init(context);
+ mContext = context.getValue();
+ }
+ }
+
+ private static boolean checkProtocol(int usbClass, int usbSubClass, int usbProtocol) {
+ PointerByReference list = new PointerByReference();
+ int count = mUsb.libusb_get_device_list(mContext, list);
+ Pointer[] devices = list.getValue().getPointerArray(0, count);
+ for (Pointer device : devices) {
+ DeviceDescriptor[] devDescriptors = new DeviceDescriptor[1];
+ mUsb.libusb_get_device_descriptor(device, devDescriptors);
+ for (int j = 0; j < devDescriptors[0].bNumConfigurations; j++) {
+ PointerByReference configRef = new PointerByReference();
+ int success = mUsb.libusb_get_config_descriptor(device, j, configRef);
+ ConfigDescriptor config = new ConfigDescriptor(configRef.getValue());
+ List<Interface> interfaces =
+ Arrays.asList(config.interfaces.toArray(config.bNumInterfaces));
+ for (Interface interface_ : interfaces) {
+ List<InterfaceDescriptor> descriptors =
+ Arrays.asList(interface_.altsetting.toArray(interface_.num_altsetting));
+ for (InterfaceDescriptor d : descriptors) {
+ if (Byte.toUnsignedInt(d.bInterfaceClass) == usbClass
+ && Byte.toUnsignedInt(d.bInterfaceSubClass) == usbSubClass
+ && Byte.toUnsignedInt(d.bInterfaceProtocol) == usbProtocol) {
+ return true;
+ }
+ }
+ }
+ }
+ }
+ return false;
+ }
+
+ /** Check for ADB */
+ @Test
+ public void testAndroidUSB() throws Exception {
+ assumeTrue(String.format("The device doesn't have service %s", HAL_SERVICE), mHasService);
+ assertTrue("ADB not present", checkProtocol(255, 66, 1));
+ }
+
+ /**
+ * Check for MTP.
+ *
+ * <p>Enables mtp and checks the host to see if mtp interface is present. MTP:
+ * https://en.wikipedia.org/wiki/Media_Transfer_Protocol.
+ */
+ @Test
+ public void testMtp() throws Exception {
+ assumeTrue(String.format("The device doesn't have service %s", HAL_SERVICE), mHasService);
+ getDevice().executeShellCommand("svc usb setFunctions mtp true");
+ Thread.sleep(WAIT_TIME);
+ assertTrue("MTP not present", checkProtocol(6, 1, 1));
+ }
+
+ /**
+ * Check for PTP.
+ *
+ * <p>Enables ptp and checks the host to see if ptp interface is present. PTP:
+ * https://en.wikipedia.org/wiki/Picture_Transfer_Protocol.
+ */
+ @Test
+ public void testPtp() throws Exception {
+ assumeTrue(String.format("The device doesn't have service %s", HAL_SERVICE), mHasService);
+ getDevice().executeShellCommand("svc usb setFunctions ptp true");
+ Thread.sleep(WAIT_TIME);
+ assertTrue("PTP not present", checkProtocol(6, 1, 1));
+ }
+
+ /**
+ * Check for MIDI.
+ *
+ * <p>Enables midi and checks the host to see if midi interface is present. MIDI:
+ * https://en.wikipedia.org/wiki/MIDI.
+ */
+ @Test
+ public void testMIDI() throws Exception {
+ assumeTrue(String.format("The device doesn't have service %s", HAL_SERVICE), mHasService);
+ getDevice().executeShellCommand("svc usb setFunctions midi true");
+ Thread.sleep(WAIT_TIME);
+ assertTrue("MIDI not present", checkProtocol(1, 3, 0));
+ }
+
+ /**
+ * Check for RNDIS.
+ *
+ * <p>Enables rndis and checks the host to see if rndis interface is present. RNDIS:
+ * https://en.wikipedia.org/wiki/RNDIS.
+ */
+ @Test
+ public void testRndis() throws Exception {
+ assumeTrue(String.format("The device doesn't have service %s", HAL_SERVICE), mHasService);
+ getDevice().executeShellCommand("svc usb setFunctions rndis true");
+ Thread.sleep(WAIT_TIME);
+ assertTrue("RNDIS not present", checkProtocol(10, 0, 0));
+ }
+}
diff --git a/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/ConfigDescriptor.java b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/ConfigDescriptor.java
new file mode 100644
index 0000000..4c0e9a4
--- /dev/null
+++ b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/ConfigDescriptor.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.tests.usbgadget.libusb;
+
+import com.google.common.collect.ImmutableList;
+import com.sun.jna.Pointer;
+import com.sun.jna.Structure;
+import java.util.List;
+
+public class ConfigDescriptor extends Structure {
+ public static class ByReference extends ConfigDescriptor implements Structure.ByReference {}
+
+ public ConfigDescriptor() {}
+
+ public ConfigDescriptor(Pointer p) {
+ super(p);
+ read();
+ }
+
+ @Override
+ protected List<String> getFieldOrder() {
+ return ImmutableList.of("bLength", "bDescriptorType", "wTotalLength", "bNumInterfaces",
+ "bConfigurationValue", "iConfiguration", "bmAttributes", "bMaxPower", "interfaces",
+ "extra", "extra_length");
+ }
+
+ public byte bLength;
+ public byte bDescriptorType;
+ public short wTotalLength;
+ public byte bNumInterfaces;
+ public byte bConfigurationValue;
+ public byte iConfiguration;
+ public byte bmAttributes;
+ public byte bMaxPower;
+ public Interface.ByReference interfaces;
+ public Pointer extra;
+ public int extra_length;
+}
diff --git a/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/DeviceDescriptor.java b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/DeviceDescriptor.java
new file mode 100644
index 0000000..1ac22fd
--- /dev/null
+++ b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/DeviceDescriptor.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.tests.usbgadget.libusb;
+
+import com.google.common.collect.ImmutableList;
+import com.sun.jna.Pointer;
+import com.sun.jna.Structure;
+import java.util.List;
+
+public class DeviceDescriptor extends Structure {
+ public static class ByReference extends DeviceDescriptor implements Structure.ByReference {}
+
+ public DeviceDescriptor() {}
+
+ public DeviceDescriptor(Pointer p) {
+ super(p);
+ read();
+ }
+
+ @Override
+ protected List<String> getFieldOrder() {
+ return ImmutableList.of("bLength", "bDescriptorType", "bcdUSB", "bDeviceClass",
+ "bDeviceSubClass", "bDeviceProtocol", "bMaxPacketSize0", "idVendor", "idProduct",
+ "bcdDevice", "iManufacturer", "iProduct", "iSerialNumber", "bNumConfigurations");
+ }
+
+ public byte bLength;
+ public byte bDescriptorType;
+ public short bcdUSB;
+ public byte bDeviceClass;
+ public byte bDeviceSubClass;
+ public byte bDeviceProtocol;
+ public byte bMaxPacketSize0;
+ public short idVendor;
+ public short idProduct;
+ public short bcdDevice;
+ public byte iManufacturer;
+ public byte iProduct;
+ public byte iSerialNumber;
+ public byte bNumConfigurations;
+}
diff --git a/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/IUsbNative.java b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/IUsbNative.java
new file mode 100644
index 0000000..b8adbb7
--- /dev/null
+++ b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/IUsbNative.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.tests.usbgadget.libusb;
+
+import com.sun.jna.Library;
+import com.sun.jna.Pointer;
+import com.sun.jna.ptr.PointerByReference;
+
+/** JNA adapter for <a href="https://libusb.info">libusb</a>. */
+public interface IUsbNative extends Library {
+ /**
+ * Initialize libusb, must be called before calling any other function.
+ *
+ * @param context output location for context pointer
+ * @return 0 on success, or an error code
+ */
+ int libusb_init(PointerByReference context);
+
+ /**
+ * Deinitialize libusb.
+ *
+ * @param ctx context to deinitialize
+ */
+ void libusb_exit(Pointer ctx);
+
+ /**
+ * Returns a list of USB devices currently attached to the system.
+ *
+ * @param ctx context to operate on
+ * @param list output location for a list of devices
+ * @return number of devices, or an error code
+ */
+ int libusb_get_device_list(Pointer ctx, PointerByReference list);
+
+ /**
+ * Get the USB device descriptor for a given device.
+ *
+ * @param dev device
+ * @param desc output location for the descriptor data
+ * @return 0 on success, or an error code
+ */
+ int libusb_get_device_descriptor(Pointer dev, DeviceDescriptor[] desc);
+
+ /**
+ * Get a USB configuration descriptor based on its index.
+ *
+ * @param dev device
+ * @param config_index config index
+ * @param config a USB configuration descriptor pointer
+ * @return 0 on success, or an error code
+ */
+ int libusb_get_config_descriptor(Pointer dev, int config_index, PointerByReference config);
+}
\ No newline at end of file
diff --git a/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/Interface.java b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/Interface.java
new file mode 100644
index 0000000..3353a2c
--- /dev/null
+++ b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/Interface.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.tests.usbgadget.libusb;
+
+import com.google.common.collect.ImmutableList;
+import com.sun.jna.Pointer;
+import com.sun.jna.Structure;
+import java.util.List;
+
+public class Interface extends Structure {
+ public static class ByReference extends Interface implements Structure.ByReference {}
+
+ public Interface() {}
+
+ public Interface(Pointer p) {
+ super(p);
+ read();
+ }
+
+ public Interface[] toArray(int size) {
+ return (Interface[]) super.toArray(new Interface[size]);
+ }
+
+ @Override
+ protected List<String> getFieldOrder() {
+ return ImmutableList.of("altsetting", "num_altsetting");
+ }
+
+ public InterfaceDescriptor.ByReference altsetting;
+ public int num_altsetting;
+}
diff --git a/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/InterfaceDescriptor.java b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/InterfaceDescriptor.java
new file mode 100644
index 0000000..e6ed1f4
--- /dev/null
+++ b/usb/gadget/V1_0/host/src/com/android/tests/usbgadget/libusb/InterfaceDescriptor.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.tests.usbgadget.libusb;
+
+import com.google.common.collect.ImmutableList;
+import com.sun.jna.Pointer;
+import com.sun.jna.Structure;
+import java.util.List;
+
+public class InterfaceDescriptor extends Structure {
+ public static class ByReference extends InterfaceDescriptor implements Structure.ByReference {}
+
+ public InterfaceDescriptor() {}
+
+ public InterfaceDescriptor(Pointer p) {
+ super(p);
+ read();
+ }
+
+ public InterfaceDescriptor[] toArray(int size) {
+ return (InterfaceDescriptor[]) super.toArray(size);
+ }
+
+ @Override
+ protected List<String> getFieldOrder() {
+ return ImmutableList.of("bLength", "bDescriptorType", "bInterfaceNumber",
+ "bAlternateSetting", "bNumEndpoints", "bInterfaceClass", "bInterfaceSubClass",
+ "bInterfaceProtocol", "iInterface", "endpoint", "extra", "extra_length");
+ }
+
+ public byte bLength;
+ public byte bDescriptorType;
+ public byte bInterfaceNumber;
+ public byte bAlternateSetting;
+ public byte bNumEndpoints;
+ public byte bInterfaceClass;
+ public byte bInterfaceSubClass;
+ public byte bInterfaceProtocol;
+ public byte iInterface;
+ public Pointer endpoint;
+ public Pointer extra;
+ public int extra_length;
+}