FileDocCategorySizeDatePackage
UsbRequest.javaAPI DocAndroid 5.1 API6534Thu Mar 12 22:22:10 GMT 2015android.hardware.usb

UsbRequest

public class UsbRequest extends Object
A class representing USB request packet. This can be used for both reading and writing data to or from a {@link android.hardware.usb.UsbDeviceConnection}. UsbRequests can be used to transfer data on bulk and interrupt endpoints. Requests on bulk endpoints can be sent synchronously via {@link UsbDeviceConnection#bulkTransfer} or asynchronously via {@link #queue} and {@link UsbDeviceConnection#requestWait}. Requests on interrupt endpoints are only send and received asynchronously.

Requests on endpoint zero are not supported by this class; use {@link UsbDeviceConnection#controlTransfer} for endpoint zero requests instead.

Fields Summary
private static final String
TAG
private long
mNativeContext
private UsbEndpoint
mEndpoint
private ByteBuffer
mBuffer
private int
mLength
private Object
mClientData
Constructors Summary
public UsbRequest()


      
    
Methods Summary
public booleancancel()
Cancels a pending queue operation.

return
true if cancelling succeeded

        return native_cancel();
    
public voidclose()
Releases all resources related to this request.

        mEndpoint = null;
        native_close();
    
voiddequeue()

        boolean out = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT);
        int bytesRead;
        if (mBuffer.isDirect()) {
            bytesRead = native_dequeue_direct();
        } else {
            bytesRead = native_dequeue_array(mBuffer.array(), mLength, out);
        }
        if (bytesRead >= 0) {
            mBuffer.position(Math.min(bytesRead, mLength));
        }
        mBuffer = null;
        mLength = 0;
    
protected voidfinalize()

        try {
            if (mEndpoint != null) {
                Log.v(TAG, "endpoint still open in finalize(): " + this);
                close();
            }
        } finally {
            super.finalize();
        }
    
public java.lang.ObjectgetClientData()
Returns the client data for the request. This can be used in conjunction with {@link #setClientData} to associate another object with this request, which can be useful for maintaining state between calls to {@link #queue} and {@link android.hardware.usb.UsbDeviceConnection#requestWait}

return
the client data for the request

        return mClientData;
    
public UsbEndpointgetEndpoint()
Returns the endpoint for the request, or null if the request is not opened.

return
the request's endpoint

        return mEndpoint;
    
public booleaninitialize(UsbDeviceConnection connection, UsbEndpoint endpoint)
Initializes the request so it can read or write data on the given endpoint. Whether the request allows reading or writing depends on the direction of the endpoint.

param
endpoint the endpoint to be used for this request.
return
true if the request was successfully opened.

        mEndpoint = endpoint;
        return native_init(connection, endpoint.getAddress(), endpoint.getAttributes(),
                endpoint.getMaxPacketSize(), endpoint.getInterval());
    
private native booleannative_cancel()

private native voidnative_close()

private native intnative_dequeue_array(byte[] buffer, int length, boolean out)

private native intnative_dequeue_direct()

private native booleannative_init(UsbDeviceConnection connection, int ep_address, int ep_attributes, int ep_max_packet_size, int ep_interval)

private native booleannative_queue_array(byte[] buffer, int length, boolean out)

private native booleannative_queue_direct(java.nio.ByteBuffer buffer, int length, boolean out)

public booleanqueue(java.nio.ByteBuffer buffer, int length)
Queues the request to send or receive data on its endpoint. For OUT endpoints, the given buffer data will be sent on the endpoint. For IN endpoints, the endpoint will attempt to read the given number of bytes into the specified buffer. If the queueing operation is successful, we return true and the result will be returned via {@link android.hardware.usb.UsbDeviceConnection#requestWait}

param
buffer the buffer containing the bytes to write, or location to store the results of a read
param
length number of bytes to read or write
return
true if the queueing operation succeeded

        boolean out = (mEndpoint.getDirection() == UsbConstants.USB_DIR_OUT);
        boolean result;
        if (buffer.isDirect()) {
            result = native_queue_direct(buffer, length, out);
        } else if (buffer.hasArray()) {
            result = native_queue_array(buffer.array(), length, out);
        } else {
            throw new IllegalArgumentException("buffer is not direct and has no array");
        }
        if (result) {
            // save our buffer for when the request has completed
            mBuffer = buffer;
            mLength = length;
        }
        return result;
    
public voidsetClientData(java.lang.Object data)
Sets the client data for the request. This can be used in conjunction with {@link #getClientData} to associate another object with this request, which can be useful for maintaining state between calls to {@link #queue} and {@link android.hardware.usb.UsbDeviceConnection#requestWait}

param
data the client data for the request

        mClientData = data;