1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/*
* Copyright (C) 2022 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;
import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
import android.hardware.usb.IUsbOperationInternal;
import android.hardware.usb.UsbPort;
import android.util.Log;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
/**
* UsbOperationInternal allows UsbPort to support both synchronous and
* asynchronous function irrespective of whether the underlying hal
* method is synchronous or asynchronous.
*
* @hide
*/
public final class UsbOperationInternal extends IUsbOperationInternal.Stub {
private static final String TAG = "UsbPortStatus";
private final int mOperationID;
// Cached portId.
private final String mId;
// True implies operation did not timeout.
private boolean mOperationComplete;
private boolean mAsynchronous = false;
private Executor mExecutor;
private Consumer<Integer> mConsumer;
private int mResult = 0;
private @UsbOperationStatus int mStatus;
final ReentrantLock mLock = new ReentrantLock();
final Condition mOperationWait = mLock.newCondition();
// Maximum time the caller has to wait for onOperationComplete to be called.
private static final int USB_OPERATION_TIMEOUT_MSECS = 5000;
/**
* The requested operation was successfully completed.
* Returned in {@link onOperationComplete} and {@link getStatus}.
*/
public static final int USB_OPERATION_SUCCESS = 0;
/**
* The requested operation failed due to internal error.
* Returned in {@link onOperationComplete} and {@link getStatus}.
*/
public static final int USB_OPERATION_ERROR_INTERNAL = 1;
/**
* The requested operation failed as it's not supported.
* Returned in {@link onOperationComplete} and {@link getStatus}.
*/
public static final int USB_OPERATION_ERROR_NOT_SUPPORTED = 2;
/**
* The requested operation failed as it's not supported.
* Returned in {@link onOperationComplete} and {@link getStatus}.
*/
public static final int USB_OPERATION_ERROR_PORT_MISMATCH = 3;
@IntDef(prefix = { "USB_OPERATION_" }, value = {
USB_OPERATION_SUCCESS,
USB_OPERATION_ERROR_INTERNAL,
USB_OPERATION_ERROR_NOT_SUPPORTED,
USB_OPERATION_ERROR_PORT_MISMATCH
})
@Retention(RetentionPolicy.SOURCE)
@interface UsbOperationStatus{}
UsbOperationInternal(int operationID, String id,
Executor executor, Consumer<Integer> consumer) {
this.mOperationID = operationID;
this.mId = id;
this.mExecutor = executor;
this.mConsumer = consumer;
this.mAsynchronous = true;
}
UsbOperationInternal(int operationID, String id) {
this.mOperationID = operationID;
this.mId = id;
}
/**
* Hal glue layer would directly call this function when the requested
* operation is complete.
*/
@Override
public void onOperationComplete(@UsbOperationStatus int status) {
mLock.lock();
try {
mOperationComplete = true;
mStatus = status;
Log.i(TAG, "Port:" + mId + " opID:" + mOperationID + " status:" + mStatus);
if (mAsynchronous) {
switch (mStatus) {
case USB_OPERATION_SUCCESS:
mResult = UsbPort.RESET_USB_PORT_SUCCESS;
break;
case USB_OPERATION_ERROR_INTERNAL:
mResult = UsbPort.RESET_USB_PORT_ERROR_INTERNAL;
break;
case USB_OPERATION_ERROR_NOT_SUPPORTED:
mResult = UsbPort.RESET_USB_PORT_ERROR_NOT_SUPPORTED;
break;
case USB_OPERATION_ERROR_PORT_MISMATCH:
mResult = UsbPort.RESET_USB_PORT_ERROR_PORT_MISMATCH;
break;
default:
mResult = UsbPort.RESET_USB_PORT_ERROR_OTHER;
}
mExecutor.execute(() -> mConsumer.accept(mResult));
} else {
mOperationWait.signal();
}
} finally {
mLock.unlock();
}
}
/**
* Caller invokes this function to wait for the operation to be complete.
*/
public void waitForOperationComplete() {
mLock.lock();
try {
long now = System.currentTimeMillis();
long deadline = now + USB_OPERATION_TIMEOUT_MSECS;
// Wait in loop to overcome spurious wakeups.
do {
mOperationWait.await(deadline - System.currentTimeMillis(),
TimeUnit.MILLISECONDS);
} while (!mOperationComplete && System.currentTimeMillis() < deadline);
if (!mOperationComplete) {
Log.e(TAG, "Port:" + mId + " opID:" + mOperationID
+ " operationComplete not received in " + USB_OPERATION_TIMEOUT_MSECS
+ "msecs");
}
} catch (InterruptedException e) {
Log.e(TAG, "Port:" + mId + " opID:" + mOperationID + " operationComplete interrupted");
} finally {
mLock.unlock();
}
}
public @UsbOperationStatus int getStatus() {
return mOperationComplete ? mStatus : USB_OPERATION_ERROR_INTERNAL;
}
}
|