summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/hardware/usb/IUsbManager.aidl6
-rw-r--r--core/java/android/hardware/usb/UsbManager.java239
2 files changed, 245 insertions, 0 deletions
diff --git a/core/java/android/hardware/usb/IUsbManager.aidl b/core/java/android/hardware/usb/IUsbManager.aidl
index e32865ebeced..ca79901c2bbe 100644
--- a/core/java/android/hardware/usb/IUsbManager.aidl
+++ b/core/java/android/hardware/usb/IUsbManager.aidl
@@ -118,6 +118,12 @@ interface IUsbManager
/* Gets the current USB functions. */
long getCurrentFunctions();
+ /* Gets the current USB Speed. */
+ int getCurrentUsbSpeed();
+
+ /* Gets the Gadget Hal Version. */
+ int getGadgetHalVersion();
+
/* Sets the screen unlocked USB function(s), which will be set automatically
* when the screen is unlocked.
*/
diff --git a/core/java/android/hardware/usb/UsbManager.java b/core/java/android/hardware/usb/UsbManager.java
index 62a5782cb55d..aac57fe88b17 100644
--- a/core/java/android/hardware/usb/UsbManager.java
+++ b/core/java/android/hardware/usb/UsbManager.java
@@ -18,6 +18,7 @@
package android.hardware.usb;
import android.Manifest;
+import android.annotation.IntDef;
import android.annotation.LongDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -34,6 +35,7 @@ import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.hardware.usb.gadget.V1_0.GadgetFunction;
+import android.hardware.usb.gadget.V1_2.UsbSpeed;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
@@ -288,6 +290,34 @@ public class UsbManager {
public static final String USB_FUNCTION_NCM = "ncm";
/**
+ * Name of Gadget Hal Not Present;
+ *
+ * {@hide}
+ */
+ public static final String GADGET_HAL_UNKNOWN = "unknown";
+
+ /**
+ * Name of the USB Gadget Hal Version v1.0;
+ *
+ * {@hide}
+ */
+ public static final String GADGET_HAL_VERSION_1_0 = "V1_0";
+
+ /**
+ * Name of the USB Gadget Hal Version v1.1;
+ *
+ * {@hide}
+ */
+ public static final String GADGET_HAL_VERSION_1_1 = "V1_1";
+
+ /**
+ * Name of the USB Gadget Hal Version v1.2;
+ *
+ * {@hide}
+ */
+ public static final String GADGET_HAL_VERSION_1_2 = "V1_2";
+
+ /**
* Name of extra for {@link #ACTION_USB_PORT_CHANGED}
* containing the {@link UsbPort} object for the port.
*
@@ -390,6 +420,102 @@ public class UsbManager {
public static final String EXTRA_CAN_BE_DEFAULT = "android.hardware.usb.extra.CAN_BE_DEFAULT";
/**
+ * The Value for USB gadget hal is not presented.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int GADGET_HAL_NOT_SUPPORTED = -1;
+
+ /**
+ * Value for Gadget Hal Version v1.0.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int GADGET_HAL_V1_0 = 10;
+
+ /**
+ * Value for Gadget Hal Version v1.1.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int GADGET_HAL_V1_1 = 11;
+
+ /**
+ * Value for Gadget Hal Version v1.2.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int GADGET_HAL_V1_2 = 12;
+
+ /**
+ * Value for USB_STATE is not configured.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int USB_DATA_TRANSFER_RATE_UNKNOWN = -1;
+
+ /**
+ * Value for USB Transfer Rate of Low Speed in Mbps (real value is 1.5Mbps).
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int USB_DATA_TRANSFER_RATE_LOW_SPEED = 2;
+
+ /**
+ * Value for USB Transfer Rate of Full Speed in Mbps.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int USB_DATA_TRANSFER_RATE_FULL_SPEED = 12;
+
+ /**
+ * Value for USB Transfer Rate of High Speed in Mbps.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int USB_DATA_TRANSFER_RATE_HIGH_SPEED = 480;
+
+ /**
+ * Value for USB Transfer Rate of Super Speed in Mbps.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int USB_DATA_TRANSFER_RATE_5G = 5 * 1024;
+
+ /**
+ * Value for USB Transfer Rate of 10G.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int USB_DATA_TRANSFER_RATE_10G = 10 * 1024;
+
+ /**
+ * Value for USB Transfer Rate of 20G.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int USB_DATA_TRANSFER_RATE_20G = 20 * 1024;
+
+ /**
+ * Value for USB Transfer Rate of 40G.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int USB_DATA_TRANSFER_RATE_40G = 40 * 1024;
+
+ /**
* Code for the charging usb function. Passed into {@link #setCurrentFunctions(long)}
* {@hide}
*/
@@ -482,6 +608,15 @@ public class UsbManager {
})
public @interface UsbFunctionMode {}
+ /** @hide */
+ @IntDef(flag = true, prefix = { "GADGET_HAL_" }, value = {
+ GADGET_HAL_NOT_SUPPORTED,
+ GADGET_HAL_V1_0,
+ GADGET_HAL_V1_1,
+ GADGET_HAL_V1_2,
+ })
+ public @interface UsbGadgetHalVersion {}
+
private final Context mContext;
private final IUsbManager mService;
@@ -894,6 +1029,53 @@ public class UsbManager {
}
/**
+ * Get the Current USB Bandwidth.
+ * <p>
+ * This function returns the current USB bandwidth through USB Gadget HAL.
+ * It should be used when Android device is in USB peripheral mode and
+ * connects to a USB host. If USB state is not configued, API will return
+ * {@value #USB_DATA_TRANSFER_RATE_UNKNOWN}. In addition, the unit of the
+ * return value is Mbps.
+ * </p>
+ *
+ * @return The value of currently USB Bandwidth.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public int getUsbBandwidth() {
+ int usbSpeed;
+
+ try {
+ usbSpeed = mService.getCurrentUsbSpeed();
+ } catch (RemoteException e) {
+ throw e.rethrowFromSystemServer();
+ }
+
+ return usbSpeedToBandwidth(usbSpeed);
+ }
+
+ /**
+ * Get the Current Gadget Hal Version.
+ * <p>
+ * This function returns the current Gadget Hal Version.
+ * </p>
+ *
+ * @return a integer {@code GADGET_HAL_*} represent hal version.
+ *
+ * {@hide}
+ */
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ @RequiresPermission(Manifest.permission.MANAGE_USB)
+ public @UsbGadgetHalVersion int getGadgetHalVersion() {
+ try {
+ return mService.getGadgetHalVersion();
+ } catch (RemoteException e) {
+ throw e.rethrowFromSystemServer();
+ }
+ }
+
+ /**
* Resets the USB Gadget.
* <p>
* Performs USB data stack reset through USB Gadget HAL.
@@ -1084,4 +1266,61 @@ public class UsbManager {
}
return ret;
}
+
+ /**
+ * Converts the given integer of USB speed to corresponding bandwidth.
+ *
+ * @return a value of USB bandwidth
+ *
+ * {@hide}
+ */
+ public static int usbSpeedToBandwidth(int speed) {
+ switch (speed) {
+ case UsbSpeed.USB4_GEN3_40Gb:
+ return USB_DATA_TRANSFER_RATE_40G;
+ case UsbSpeed.USB4_GEN3_20Gb:
+ return USB_DATA_TRANSFER_RATE_20G;
+ case UsbSpeed.USB4_GEN2_20Gb:
+ return USB_DATA_TRANSFER_RATE_20G;
+ case UsbSpeed.USB4_GEN2_10Gb:
+ return USB_DATA_TRANSFER_RATE_10G;
+ case UsbSpeed.SUPERSPEED_20Gb:
+ return USB_DATA_TRANSFER_RATE_20G;
+ case UsbSpeed.SUPERSPEED_10Gb:
+ return USB_DATA_TRANSFER_RATE_10G;
+ case UsbSpeed.SUPERSPEED:
+ return USB_DATA_TRANSFER_RATE_5G;
+ case UsbSpeed.HIGHSPEED:
+ return USB_DATA_TRANSFER_RATE_HIGH_SPEED;
+ case UsbSpeed.FULLSPEED:
+ return USB_DATA_TRANSFER_RATE_FULL_SPEED;
+ case UsbSpeed.LOWSPEED:
+ return USB_DATA_TRANSFER_RATE_LOW_SPEED;
+ default:
+ return USB_DATA_TRANSFER_RATE_UNKNOWN;
+ }
+ }
+
+ /**
+ * Converts the given usb gadgdet hal version to String
+ *
+ * @return String representation of Usb Gadget Hal Version
+ *
+ * {@hide}
+ */
+ public static @NonNull String usbGadgetHalVersionToString(int version) {
+ String halVersion;
+
+ if (version == GADGET_HAL_V1_2) {
+ halVersion = GADGET_HAL_VERSION_1_2;
+ } else if (version == GADGET_HAL_V1_1) {
+ halVersion = GADGET_HAL_VERSION_1_1;
+ } else if (version == GADGET_HAL_V1_0) {
+ halVersion = GADGET_HAL_VERSION_1_0;
+ } else {
+ halVersion = GADGET_HAL_UNKNOWN;
+ }
+
+ return halVersion;
+ }
}