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

Commit bbfa92e6 authored by Badhri Jagan Sridharan's avatar Badhri Jagan Sridharan Committed by Ricky Niu
Browse files

Support IUsb AIDL interface

UsbPortManager is made to support both AIDL & HIDL interface until
HIDL is deprecated eventually. UsbPortHal abstract class is defined
which the UsbPortManager grabs an instance of from UsbPortHalInstance
method to talk to the Hal implementation process. UsbPortHalInstance
queries the underlying hal implementation and instantiates UsbPortHal
with either UsbPortHidl or UsbPortAidl based on the IUsb interface
implemented by HAL implementation.

go/iusb-aidl-migrate.

Bug: 200993386
Bug: 199357330
Bug: 211677613
Bug: 213312081
Bug: 199358576
Test: Manually tested through UI.
CTS-Coverage-Bug: 215019881
Change-Id: I246fd6e5bee8138d3ad3b6994665a1d4788c8789
parent 4b3f0431
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -113,10 +113,12 @@ package android.hardware.usb {
    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
    field public static final int USB_HAL_NOT_SUPPORTED = -1; // 0xffffffff
    field public static final int USB_HAL_RETRY = -2; // 0xfffffffe
    field public static final int USB_HAL_V1_0 = 10; // 0xa
    field public static final int USB_HAL_V1_1 = 11; // 0xb
    field public static final int USB_HAL_V1_2 = 12; // 0xc
    field public static final int USB_HAL_V1_3 = 13; // 0xd
    field public static final int USB_HAL_V2_0 = 20; // 0x14
  }

}
+6 −0
Original line number Diff line number Diff line
@@ -4686,8 +4686,14 @@ package android.hardware.usb {
  }
  public final class UsbPort {
    method @CheckResult @RequiresPermission(android.Manifest.permission.MANAGE_USB) public int enableUsbData(boolean);
    method @Nullable @RequiresPermission(android.Manifest.permission.MANAGE_USB) public android.hardware.usb.UsbPortStatus getStatus();
    method @RequiresPermission(android.Manifest.permission.MANAGE_USB) public void setRoles(int, int);
    field public static final int ENABLE_USB_DATA_ERROR_INTERNAL = 1; // 0x1
    field public static final int ENABLE_USB_DATA_ERROR_NOT_SUPPORTED = 2; // 0x2
    field public static final int ENABLE_USB_DATA_ERROR_OTHER = 4; // 0x4
    field public static final int ENABLE_USB_DATA_ERROR_PORT_MISMATCH = 3; // 0x3
    field public static final int ENABLE_USB_DATA_SUCCESS = 0; // 0x0
  }
  public final class UsbPortStatus implements android.os.Parcelable {
+4 −3
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.hardware.usb;

import android.app.PendingIntent;
import android.content.ComponentName;
import android.hardware.usb.IUsbOperationInternal;
import android.hardware.usb.UsbAccessory;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.ParcelableUsbPort;
@@ -136,7 +137,7 @@ interface IUsbManager
    void resetUsbGadget();

    /* Set USB data on or off */
    boolean enableUsbDataSignal(boolean enable);
    boolean enableUsbData(in String portId, boolean enable, int operationId, in IUsbOperationInternal callback);

    /* Gets the USB Hal Version. */
    int getUsbHalVersion();
+24 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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 android.hardware.usb;

/**
 * @hide
 */
oneway interface IUsbOperationInternal {
void onOperationComplete(in int status);
}
+61 −5
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ 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.hardware.usb.IUsbOperationInternal;
import android.hardware.usb.UsbPort;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
@@ -48,6 +50,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;

/**
@@ -516,6 +519,14 @@ public class UsbManager {
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int USB_DATA_TRANSFER_RATE_40G = 40 * 1024;

    /**
     * Returned when the client has to retry querying the version.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int USB_HAL_RETRY = -2;

    /**
     * The Value for USB hal is not presented.
     *
@@ -556,6 +567,14 @@ public class UsbManager {
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int USB_HAL_V1_3 = 13;

    /**
     * Value for USB Hal Version v2.0.
     *
     * @hide
     */
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static final int USB_HAL_V2_0 = 20;

    /**
     * Code for the charging usb function. Passed into {@link #setCurrentFunctions(long)}
     * {@hide}
@@ -665,6 +684,7 @@ public class UsbManager {
            USB_HAL_V1_1,
            USB_HAL_V1_2,
            USB_HAL_V1_3,
            USB_HAL_V2_0,
    })
    public @interface UsbHalVersion {}

@@ -1169,8 +1189,9 @@ public class UsbManager {
    /**
     * Enable/Disable the USB data signaling.
     * <p>
     * Enables/Disables USB data path in all the USB ports.
     * Enables/Disables USB data path of the first port..
     * It will force to stop or restore USB data signaling.
     * Call UsbPort API if the device has more than one UsbPort.
     * </p>
     *
     * @param enable enable or disable USB data signaling
@@ -1181,11 +1202,11 @@ public class UsbManager {
     */
    @RequiresPermission(Manifest.permission.MANAGE_USB)
    public boolean enableUsbDataSignal(boolean enable) {
        try {
            return mService.enableUsbDataSignal(enable);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        List<UsbPort> usbPorts = getPorts();
        if (usbPorts.size() == 1) {
            return usbPorts.get(0).enableUsbData(enable) == UsbPort.ENABLE_USB_DATA_SUCCESS;
        }
        return false;
    }

    /**
@@ -1270,6 +1291,41 @@ public class UsbManager {
        }
    }

    /**
     * Should only be called by {@link UsbPort#enableUsbData}.
     * <p>
     * Enables or disables USB data on the specific port.
     *
     * @param port USB port for which USB data needs to be enabled or disabled.
     * @param enable Enable USB data when true.
     *               Disable USB data when false.
     * @param operationId operationId for the request.
     * @param callback callback object to be invoked when the operation is complete.
     * @return True when the operation is asynchronous. The caller must therefore call
     *         {@link UsbOperationInternal#waitForOperationComplete} for processing
     *         the result.
     *         False when the operation is synchronous. Caller can proceed reading the result
     *         through {@link UsbOperationInternal#getStatus}
     * @hide
     */
    @RequiresPermission(Manifest.permission.MANAGE_USB)
    boolean enableUsbData(@NonNull UsbPort port, boolean enable, int operationId,
            IUsbOperationInternal callback) {
        Objects.requireNonNull(port, "enableUsbData: port must not be null. opId:" + operationId);
        try {
            return mService.enableUsbData(port.getId(), enable, operationId, callback);
        } catch (RemoteException e) {
            Log.e(TAG, "enableUsbData: failed. opId:" + operationId, e);
            try {
                callback.onOperationComplete(UsbOperationInternal.USB_OPERATION_ERROR_INTERNAL);
            } catch (RemoteException r) {
                Log.e(TAG, "enableUsbData: failed to call onOperationComplete. opId:"
                        + operationId, r);
            }
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Sets the component that will handle USB device connection.
     * <p>
Loading