Support USB Gadget V1.2 HAL

USB Gadget V1.2 HAL include:
1. support NCM function
2. add api to inquiry USB speed
3. add api to inquiry HAL version

Bug: 168282708
Test: build pass and functions are working normally
Change-Id: Idd07c55f4610740c0cf9023391125a86ba39ddee
This commit is contained in:
raychi 2020-09-30 19:04:08 +08:00
parent 5aa16fb5b9
commit 88254a2b56
7 changed files with 390 additions and 1 deletions

View File

@ -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",

View File

@ -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 {

View File

@ -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.
*/

View File

@ -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;
}
}

View File

@ -21,5 +21,6 @@ java_library_static {
"android.hardware.usb-V1.2-java",
"android.hardware.usb.gadget-V1.0-java",
"android.hardware.usb.gadget-V1.1-java",
"android.hardware.usb.gadget-V1.2-java",
],
}

View File

@ -55,8 +55,9 @@ import android.hardware.usb.UsbPort;
import android.hardware.usb.UsbPortStatus;
import android.hardware.usb.gadget.V1_0.GadgetFunction;
import android.hardware.usb.gadget.V1_0.IUsbGadget;
import android.hardware.usb.gadget.V1_0.IUsbGadgetCallback;
import android.hardware.usb.gadget.V1_0.Status;
import android.hardware.usb.gadget.V1_2.IUsbGadgetCallback;
import android.hardware.usb.gadget.V1_2.UsbSpeed;
import android.hidl.manager.V1_0.IServiceManager;
import android.hidl.manager.V1_0.IServiceNotification;
import android.os.BatteryManager;
@ -166,6 +167,8 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
private static final int MSG_RESET_USB_GADGET = 19;
private static final int MSG_ACCESSORY_HANDSHAKE_TIMEOUT = 20;
private static final int MSG_INCREASE_SENDSTRING_COUNT = 21;
private static final int MSG_UPDATE_USB_SPEED = 22;
private static final int MSG_UPDATE_HAL_VERSION = 23;
private static final int AUDIO_MODE_SOURCE = 1;
@ -532,6 +535,8 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
protected SharedPreferences mSettings;
protected int mCurrentUser;
protected boolean mCurrentUsbFunctionsReceived;
protected int mUsbSpeed;
protected int mCurrentGadgetHalVersion;
/**
* The persistent property which stores whether adb is enabled or not.
@ -902,6 +907,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
} else {
mPendingBootBroadcast = true;
}
updateUsbSpeed();
break;
case MSG_UPDATE_PORT_STATE:
SomeArgs args = (SomeArgs) msg.obj;
@ -1117,6 +1123,26 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
return mCurrentAccessory;
}
protected void updateUsbGadgetHalVersion() {
sendMessage(MSG_UPDATE_HAL_VERSION, null);
}
protected void updateUsbSpeed() {
if (mCurrentGadgetHalVersion < UsbManager.GADGET_HAL_V1_0) {
mUsbSpeed = UsbSpeed.UNKNOWN;
return;
}
if (mConnected && mConfigured) {
sendMessage(MSG_UPDATE_USB_SPEED, null);
} else {
// clear USB speed due to disconnected
mUsbSpeed = UsbSpeed.UNKNOWN;
}
return;
}
protected void updateUsbNotification(boolean force) {
if (mNotificationManager == null || !mUseUsbNotification
|| ("0".equals(getSystemProperty("persist.charging.notify", "")))) {
@ -1324,6 +1350,14 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
return mScreenUnlockedFunctions;
}
public int getUsbSpeed() {
return mUsbSpeed;
}
public int getGadgetHalVersion() {
return mCurrentGadgetHalVersion;
}
/**
* Dump a functions mask either as proto-enums (if dumping to proto) or a string (if dumping
* to a print writer)
@ -1450,6 +1484,9 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
mCurrentFunctions = UsbManager.FUNCTION_NONE;
mCurrentUsbFunctionsReceived = true;
mUsbSpeed = UsbSpeed.UNKNOWN;
mCurrentGadgetHalVersion = UsbManager.GADGET_HAL_NOT_SUPPORTED;
String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim();
updateState(state);
} catch (Exception e) {
@ -1826,10 +1863,13 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
USB_GADGET_HAL_DEATH_COOKIE);
mCurrentFunctions = UsbManager.FUNCTION_NONE;
mCurrentUsbFunctionsRequested = true;
mUsbSpeed = UsbSpeed.UNKNOWN;
mCurrentGadgetHalVersion = UsbManager.GADGET_HAL_V1_0;
mGadgetProxy.getCurrentUsbFunctions(new UsbGadgetCallback());
}
String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim();
updateState(state);
updateUsbGadgetHalVersion();
} catch (NoSuchElementException e) {
Slog.e(TAG, "Usb gadget hal not found", e);
} catch (RemoteException e) {
@ -1935,6 +1975,48 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
}
}
break;
case MSG_UPDATE_USB_SPEED:
synchronized (mGadgetProxyLock) {
if (mGadgetProxy == null) {
Slog.e(TAG, "mGadgetProxy is null");
break;
}
try {
android.hardware.usb.gadget.V1_2.IUsbGadget gadgetProxy =
android.hardware.usb.gadget.V1_2.IUsbGadget
.castFrom(mGadgetProxy);
if (gadgetProxy != null) {
gadgetProxy.getUsbSpeed(new UsbGadgetCallback());
}
} catch (RemoteException e) {
Slog.e(TAG, "get UsbSpeed failed", e);
}
}
break;
case MSG_UPDATE_HAL_VERSION:
synchronized (mGadgetProxyLock) {
if (mGadgetProxy == null) {
Slog.e(TAG, "mGadgetProxy is null");
break;
}
android.hardware.usb.gadget.V1_2.IUsbGadget gadgetProxy =
android.hardware.usb.gadget.V1_2.IUsbGadget.castFrom(mGadgetProxy);
if (gadgetProxy == null) {
android.hardware.usb.gadget.V1_1.IUsbGadget gadgetProxyV1By1 =
android.hardware.usb.gadget.V1_1.IUsbGadget
.castFrom(mGadgetProxy);
if (gadgetProxyV1By1 == null) {
mCurrentGadgetHalVersion = UsbManager.GADGET_HAL_V1_0;
break;
}
mCurrentGadgetHalVersion = UsbManager.GADGET_HAL_V1_1;
break;
}
mCurrentGadgetHalVersion = UsbManager.GADGET_HAL_V1_2;
}
break;
default:
super.handleMessage(msg);
}
@ -1982,6 +2064,11 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
sendMessage(MSG_GET_CURRENT_USB_FUNCTIONS, functions,
status == Status.FUNCTIONS_APPLIED);
}
@Override
public void getUsbSpeedCb(int speed) {
mUsbSpeed = speed;
}
}
private void setUsbConfig(long config, boolean chargingFunctions) {
@ -2090,6 +2177,14 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
return mHandler.getEnabledFunctions();
}
public int getCurrentUsbSpeed() {
return mHandler.getUsbSpeed();
}
public int getGadgetHalVersion() {
return mHandler.getGadgetHalVersion();
}
/**
* Returns a dup of the control file descriptor for the given function.
*/

View File

@ -637,6 +637,31 @@ public class UsbService extends IUsbManager.Stub {
return mDeviceManager.getScreenUnlockedFunctions();
}
@Override
public int getCurrentUsbSpeed() {
Preconditions.checkNotNull(mDeviceManager, "DeviceManager must not be null");
final long ident = Binder.clearCallingIdentity();
try {
return mDeviceManager.getCurrentUsbSpeed();
} finally {
Binder.restoreCallingIdentity(ident);
}
}
@Override
public int getGadgetHalVersion() {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
Preconditions.checkNotNull(mDeviceManager, "DeviceManager must not be null");
final long ident = Binder.clearCallingIdentity();
try {
return mDeviceManager.getGadgetHalVersion();
} finally {
Binder.restoreCallingIdentity(ident);
}
}
@Override
public void resetUsbGadget() {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);