Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit baf630d3 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic "USB-GADGET-HAL-v1.2"

* changes:
  Add methods for Usb Gadget Hal v1.2
  Support USB Gadget V1.2 HAL
parents e9cf1f98 062ddc34
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -515,6 +515,8 @@ java_library {
        "android.hardware.usb-V1.1-java-constants",
        "android.hardware.usb-V1.2-java-constants",
        "android.hardware.usb.gadget-V1.0-java",
        "android.hardware.usb.gadget-V1.1-java",
        "android.hardware.usb.gadget-V1.2-java",
        "android.hardware.vibrator-V1.0-java",
        "android.hardware.vibrator-V1.1-java",
        "android.hardware.vibrator-V1.2-java",
+30 −3
Original line number Diff line number Diff line
@@ -43,9 +43,19 @@ public class UsbCommand extends Svc.Command {
                    + "screen unlock. If function is blank, turn off this feature.\n"
                + "       svc usb getFunctions\n"
                + "         Gets the list of currently enabled functions\n"
                + "         possible values of [function] are any of 'mtp', 'ptp', 'rndis',\n"
                + "         'midi', 'ncm (if supporting gadget hal v1.2)'\n"
                + "       svc usb resetUsbGadget\n"
                + "          Reset usb gadget\n\n"
                + "possible values of [function] are any of 'mtp', 'ptp', 'rndis', 'midi'\n";
                + "         Reset usb gadget\n"
                + "       svc usb getUsbSpeed\n"
                + "         Gets current USB speed\n"
                + "         possible values of USB speed are any of 'low speed', 'full speed',\n"
                + "         'high speed', 'super speed', 'super speed (10G)',\n"
                + "         'super speed (20G)', or higher (future extension)\n"
                + "       svc usb getGadgetHalVersion\n"
                + "         Gets current Gadget Hal Version\n"
                + "         possible values of Hal version are any of 'unknown', 'V1_0', 'V1_1',\n"
                + "         'V1_2'\n";
    }

    @Override
@@ -84,6 +94,23 @@ public class UsbCommand extends Svc.Command {
                    System.err.println("Error communicating with UsbManager: " + e);
                }
                return;
            } else if ("getUsbSpeed".equals(args[1])) {
                try {
                    System.err.println(
                            UsbManager.usbSpeedToBandwidth(usbMgr.getCurrentUsbSpeed()));
                } catch (RemoteException e) {
                    System.err.println("Error communicating with UsbManager: " + e);
                }
                return;
            } else if ("getGadgetHalVersion".equals(args[1])) {
                try {
                    System.err.println(
                            UsbManager.usbGadgetHalVersionToString(
                                    usbMgr.getGadgetHalVersion()));
                } catch (RemoteException e) {
                    System.err.println("Error communicating with UsbManager: " + e);
                }
                return;
            }
        }
        System.err.println(longHelp());
+21 −0
Original line number Diff line number Diff line
@@ -43,6 +43,27 @@ package android.content.rollback {

}

package android.hardware.usb {

  public class UsbManager {
    method @RequiresPermission(android.Manifest.permission.MANAGE_USB) public int getGadgetHalVersion();
    method public int getUsbBandwidth();
    field public static final int GADGET_HAL_NOT_SUPPORTED = -1; // 0xffffffff
    field public static final int GADGET_HAL_V1_0 = 10; // 0xa
    field public static final int GADGET_HAL_V1_1 = 11; // 0xb
    field public static final int GADGET_HAL_V1_2 = 12; // 0xc
    field public static final int USB_DATA_TRANSFER_RATE_10G = 10240; // 0x2800
    field public static final int USB_DATA_TRANSFER_RATE_20G = 20480; // 0x5000
    field public static final int USB_DATA_TRANSFER_RATE_40G = 40960; // 0xa000
    field public static final int USB_DATA_TRANSFER_RATE_5G = 5120; // 0x1400
    field public static final int USB_DATA_TRANSFER_RATE_FULL_SPEED = 12; // 0xc
    field public static final int USB_DATA_TRANSFER_RATE_HIGH_SPEED = 480; // 0x1e0
    field public static final int USB_DATA_TRANSFER_RATE_LOW_SPEED = 2; // 0x2
    field public static final int USB_DATA_TRANSFER_RATE_UNKNOWN = -1; // 0xffffffff
  }

}

package android.media {

  public class AudioManager {
+6 −0
Original line number Diff line number Diff line
@@ -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.
     */
+239 −0
Original line number Diff line number Diff line
@@ -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;
@@ -287,6 +289,34 @@ public class UsbManager {
    @SystemApi
    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.
@@ -389,6 +419,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;

@@ -893,6 +1028,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>
@@ -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;
    }
}
Loading