| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.usb; |
| 18 |
|
| 19 |
import android.annotation.Nullable; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.os.Build; |
| 22 |
import android.util.Log; |
| 23 |
|
| 24 |
import com.android.internal.util.Preconditions; |
| 25 |
|
| 26 |
import dalvik.system.CloseGuard; |
| 27 |
|
| 28 |
import java.nio.BufferOverflowException; |
| 29 |
import java.nio.ByteBuffer; |
| 30 |
import java.util.Objects; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class UsbRequest { |
| 45 |
|
| 46 |
private static final String TAG = "UsbRequest"; |
| 47 |
|
| 48 |
|
| 49 |
static final int MAX_USBFS_BUFFER_SIZE = 16384; |
| 50 |
|
| 51 |
|
| 52 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 53 |
private long mNativeContext; |
| 54 |
|
| 55 |
private UsbEndpoint mEndpoint; |
| 56 |
|
| 57 |
|
| 58 |
@UnsupportedAppUsage |
| 59 |
private ByteBuffer mBuffer; |
| 60 |
|
| 61 |
|
| 62 |
@UnsupportedAppUsage |
| 63 |
private int mLength; |
| 64 |
|
| 65 |
|
| 66 |
private Object mClientData; |
| 67 |
|
| 68 |
|
| 69 |
private UsbDeviceConnection mConnection; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private boolean mIsUsingNewQueue; |
| 76 |
|
| 77 |
|
| 78 |
private ByteBuffer mTempBuffer; |
| 79 |
|
| 80 |
private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private final Object mLock = new Object(); |
| 87 |
|
| 88 |
public UsbRequest() { |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public boolean initialize(UsbDeviceConnection connection, UsbEndpoint endpoint) { |
| 99 |
mEndpoint = endpoint; |
| 100 |
mConnection = Objects.requireNonNull(connection, "connection"); |
| 101 |
|
| 102 |
boolean wasInitialized = native_init(connection, endpoint.getAddress(), |
| 103 |
endpoint.getAttributes(), endpoint.getMaxPacketSize(), endpoint.getInterval()); |
| 104 |
|
| 105 |
if (wasInitialized) { |
| 106 |
mCloseGuard.open("UsbRequest.close"); |
| 107 |
} |
| 108 |
|
| 109 |
return wasInitialized; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public void close() { |
| 116 |
synchronized (mLock) { |
| 117 |
if (mNativeContext != 0) { |
| 118 |
mEndpoint = null; |
| 119 |
mConnection = null; |
| 120 |
native_close(); |
| 121 |
mCloseGuard.close(); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
protected void finalize() throws Throwable { |
| 128 |
try { |
| 129 |
if (mCloseGuard != null) { |
| 130 |
mCloseGuard.warnIfOpen(); |
| 131 |
} |
| 132 |
|
| 133 |
close(); |
| 134 |
} finally { |
| 135 |
super.finalize(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public UsbEndpoint getEndpoint() { |
| 145 |
return mEndpoint; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public Object getClientData() { |
| 158 |
return mClientData; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public void setClientData(Object data) { |
| 171 |
mClientData = data; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
@Deprecated |
| 195 |
public boolean queue(ByteBuffer buffer, int length) { |
| 196 |
UsbDeviceConnection connection = mConnection; |
| 197 |
if (connection == null) { |
| 198 |
|
| 199 |
throw new NullPointerException("invalid connection"); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
return connection.queueRequest(this, buffer, length); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
boolean queueIfConnectionOpen(ByteBuffer buffer, int length) { |
| 212 |
UsbDeviceConnection connection = mConnection; |
| 213 |
if (connection == null || !connection.isOpen()) { |
| 214 |
|
| 215 |
throw new NullPointerException("invalid connection"); |
| 216 |
} |
| 217 |
|
| 218 |
boolean out = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT); |
| 219 |
boolean result; |
| 220 |
|
| 221 |
if (connection.getContext().getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.P |
| 222 |
&& length > MAX_USBFS_BUFFER_SIZE) { |
| 223 |
length = MAX_USBFS_BUFFER_SIZE; |
| 224 |
} |
| 225 |
|
| 226 |
synchronized (mLock) { |
| 227 |
|
| 228 |
mBuffer = buffer; |
| 229 |
mLength = length; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
if (buffer.isDirect()) { |
| 235 |
result = native_queue_direct(buffer, length, out); |
| 236 |
} else if (buffer.hasArray()) { |
| 237 |
result = native_queue_array(buffer.array(), length, out); |
| 238 |
} else { |
| 239 |
throw new IllegalArgumentException("buffer is not direct and has no array"); |
| 240 |
} |
| 241 |
if (!result) { |
| 242 |
mBuffer = null; |
| 243 |
mLength = 0; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
return result; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public boolean queue(@Nullable ByteBuffer buffer) { |
| 270 |
UsbDeviceConnection connection = mConnection; |
| 271 |
if (connection == null) { |
| 272 |
|
| 273 |
throw new IllegalStateException("invalid connection"); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
return connection.queueRequest(this, buffer); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
boolean queueIfConnectionOpen(@Nullable ByteBuffer buffer) { |
| 286 |
UsbDeviceConnection connection = mConnection; |
| 287 |
if (connection == null || !connection.isOpen()) { |
| 288 |
|
| 289 |
throw new IllegalStateException("invalid connection"); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
Preconditions.checkState(mNativeContext != 0, "request is not initialized"); |
| 294 |
|
| 295 |
|
| 296 |
Preconditions.checkState(!mIsUsingNewQueue, "this request is currently queued"); |
| 297 |
|
| 298 |
boolean isSend = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT); |
| 299 |
boolean wasQueued; |
| 300 |
|
| 301 |
synchronized (mLock) { |
| 302 |
mBuffer = buffer; |
| 303 |
|
| 304 |
if (buffer == null) { |
| 305 |
|
| 306 |
mIsUsingNewQueue = true; |
| 307 |
wasQueued = native_queue(null, 0, 0); |
| 308 |
} else { |
| 309 |
if (connection.getContext().getApplicationInfo().targetSdkVersion |
| 310 |
< Build.VERSION_CODES.P) { |
| 311 |
|
| 312 |
Preconditions.checkArgumentInRange(buffer.remaining(), 0, MAX_USBFS_BUFFER_SIZE, |
| 313 |
"number of remaining bytes"); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
Preconditions.checkArgument(!(buffer.isReadOnly() && !isSend), "buffer can not be " |
| 318 |
+ "read-only when receiving data"); |
| 319 |
|
| 320 |
if (!buffer.isDirect()) { |
| 321 |
mTempBuffer = ByteBuffer.allocateDirect(mBuffer.remaining()); |
| 322 |
|
| 323 |
if (isSend) { |
| 324 |
|
| 325 |
mBuffer.mark(); |
| 326 |
mTempBuffer.put(mBuffer); |
| 327 |
mTempBuffer.flip(); |
| 328 |
mBuffer.reset(); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
buffer = mTempBuffer; |
| 333 |
} |
| 334 |
|
| 335 |
mIsUsingNewQueue = true; |
| 336 |
wasQueued = native_queue(buffer, buffer.position(), buffer.remaining()); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if (!wasQueued) { |
| 341 |
mIsUsingNewQueue = false; |
| 342 |
mTempBuffer = null; |
| 343 |
mBuffer = null; |
| 344 |
} |
| 345 |
|
| 346 |
return wasQueued; |
| 347 |
} |
| 348 |
|
| 349 |
void dequeue(boolean useBufferOverflowInsteadOfIllegalArg) { |
| 350 |
boolean isSend = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT); |
| 351 |
int bytesTransferred; |
| 352 |
|
| 353 |
synchronized (mLock) { |
| 354 |
if (mIsUsingNewQueue) { |
| 355 |
bytesTransferred = native_dequeue_direct(); |
| 356 |
mIsUsingNewQueue = false; |
| 357 |
|
| 358 |
if (mBuffer == null) { |
| 359 |
|
| 360 |
} else if (mTempBuffer == null) { |
| 361 |
mBuffer.position(mBuffer.position() + bytesTransferred); |
| 362 |
} else { |
| 363 |
mTempBuffer.limit(bytesTransferred); |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
try { |
| 369 |
if (isSend) { |
| 370 |
mBuffer.position(mBuffer.position() + bytesTransferred); |
| 371 |
} else { |
| 372 |
|
| 373 |
mBuffer.put(mTempBuffer); |
| 374 |
} |
| 375 |
} finally { |
| 376 |
mTempBuffer = null; |
| 377 |
} |
| 378 |
} |
| 379 |
} else { |
| 380 |
if (mBuffer.isDirect()) { |
| 381 |
bytesTransferred = native_dequeue_direct(); |
| 382 |
} else { |
| 383 |
bytesTransferred = native_dequeue_array(mBuffer.array(), mLength, isSend); |
| 384 |
} |
| 385 |
if (bytesTransferred >= 0) { |
| 386 |
int bytesToStore = Math.min(bytesTransferred, mLength); |
| 387 |
try { |
| 388 |
mBuffer.position(bytesToStore); |
| 389 |
} catch (IllegalArgumentException e) { |
| 390 |
if (useBufferOverflowInsteadOfIllegalArg) { |
| 391 |
Log.e(TAG, "Buffer " + mBuffer + " does not have enough space to read " |
| 392 |
+ bytesToStore + " bytes", e); |
| 393 |
throw new BufferOverflowException(); |
| 394 |
} else { |
| 395 |
throw e; |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
mBuffer = null; |
| 402 |
mLength = 0; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public boolean cancel() { |
| 412 |
UsbDeviceConnection connection = mConnection; |
| 413 |
if (connection == null) { |
| 414 |
return false; |
| 415 |
} |
| 416 |
|
| 417 |
return connection.cancelRequest(this); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
boolean cancelIfOpen() { |
| 432 |
UsbDeviceConnection connection = mConnection; |
| 433 |
if (mNativeContext == 0 || (connection != null && !connection.isOpen())) { |
| 434 |
Log.w(TAG, |
| 435 |
"Detected attempt to cancel a request on a connection which isn't open"); |
| 436 |
return false; |
| 437 |
} |
| 438 |
return native_cancel(); |
| 439 |
} |
| 440 |
|
| 441 |
private native boolean native_init(UsbDeviceConnection connection, int ep_address, |
| 442 |
int ep_attributes, int ep_max_packet_size, int ep_interval); |
| 443 |
private native void native_close(); |
| 444 |
private native boolean native_queue(ByteBuffer buffer, int offset, int length); |
| 445 |
private native boolean native_queue_array(byte[] buffer, int length, boolean out); |
| 446 |
private native int native_dequeue_array(byte[] buffer, int length, boolean out); |
| 447 |
private native boolean native_queue_direct(ByteBuffer buffer, int length, boolean out); |
| 448 |
private native int native_dequeue_direct(); |
| 449 |
private native boolean native_cancel(); |
| 450 |
} |