Java Code Examples for android.hardware.usb.UsbConstants#USB_DIR_OUT

The following examples show how to use android.hardware.usb.UsbConstants#USB_DIR_OUT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UsbHidDevice.java    From UsbHid with MIT License 6 votes vote down vote up
private UsbHidDevice(UsbDevice usbDevice, UsbInterface usbInterface, UsbManager usbManager) {
    mUsbDevice = usbDevice;
    mUsbInterface = usbInterface;
    mUsbManager= usbManager;

    for (int i = 0; i < mUsbInterface.getEndpointCount(); i++) {
        UsbEndpoint endpoint = mUsbInterface.getEndpoint(i);
        int dir = endpoint.getDirection();
        int type = endpoint.getType();
        if (mInUsbEndpoint == null && dir == UsbConstants.USB_DIR_IN && type == UsbConstants.USB_ENDPOINT_XFER_INT) {
            mInUsbEndpoint = endpoint;
        }
        if (mOutUsbEndpoint == null && dir == UsbConstants.USB_DIR_OUT && type == UsbConstants.USB_ENDPOINT_XFER_INT) {
            mOutUsbEndpoint = endpoint;
        }
    }
}
 
Example 2
Source File: CdcAcmSerialDriver.java    From usb-serial-for-android with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void openSingleInterface() throws IOException {
    // the following code is inspired by the cdc-acm driver in the linux kernel

    mControlIndex = 0;
    mControlInterface = mDevice.getInterface(0);
    mDataInterface = mDevice.getInterface(0);
    if (!mConnection.claimInterface(mControlInterface, true)) {
        throw new IOException("Could not claim shared control/data interface");
    }

    for (int i = 0; i < mControlInterface.getEndpointCount(); ++i) {
        UsbEndpoint ep = mControlInterface.getEndpoint(i);
        if ((ep.getDirection() == UsbConstants.USB_DIR_IN) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)) {
            mControlEndpoint = ep;
        } else if ((ep.getDirection() == UsbConstants.USB_DIR_IN) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
            mReadEndpoint = ep;
        } else if ((ep.getDirection() == UsbConstants.USB_DIR_OUT) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
            mWriteEndpoint = ep;
        }
    }
    if (mControlEndpoint == null) {
        throw new IOException("No control endpoint");
    }
}
 
Example 3
Source File: UsbHelper.java    From sample-usbenum with Apache License 2.0 5 votes vote down vote up
public static String nameForDirection(int direction) {
    switch (direction) {
        case UsbConstants.USB_DIR_IN:
            return "IN";
        case UsbConstants.USB_DIR_OUT:
            return "OUT";
        default:
            return "Unknown Direction";
    }
}
 
Example 4
Source File: UsbMidiDeviceAndroid.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a UsbMidiDeviceAndroid.
 * @param manager
 * @param device The USB device which this object is assocated with.
 */
UsbMidiDeviceAndroid(UsbManager manager, UsbDevice device) {
    mConnection = manager.openDevice(device);
    mEndpointMap = new SparseArray<UsbEndpoint>();
    mRequestMap = new HashMap<UsbEndpoint, UsbRequest>();
    mHandler = new Handler();
    mUsbDevice = device;
    mIsClosed = false;
    mHasInputThread = false;
    mNativePointer = 0;

    for (int i = 0; i < device.getInterfaceCount(); ++i) {
        UsbInterface iface = device.getInterface(i);
        if (iface.getInterfaceClass() != UsbConstants.USB_CLASS_AUDIO
                || iface.getInterfaceSubclass() != MIDI_SUBCLASS) {
            continue;
        }
        mConnection.claimInterface(iface, true);
        for (int j = 0; j < iface.getEndpointCount(); ++j) {
            UsbEndpoint endpoint = iface.getEndpoint(j);
            if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
                mEndpointMap.put(endpoint.getEndpointNumber(), endpoint);
            }
        }
    }
    // Start listening for input endpoints.
    // This function will create and run a thread if there is USB-MIDI endpoints in the
    // device. Note that because UsbMidiDevice is shared among all tabs and the thread
    // will be terminated when the device is disconnected, at most one thread can be created
    // for each connected USB-MIDI device.
    startListen(device);
}
 
Example 5
Source File: XferUtils.java    From USBIPServerForAndroid with GNU General Public License v3.0 5 votes vote down vote up
public static int doBulkTransfer(UsbDeviceConnection devConn, UsbEndpoint endpoint, byte[] buff, int timeout) {
	int bytesTransferred = 0;
	while (bytesTransferred < buff.length) {
		byte[] remainingBuffer = new byte[buff.length - bytesTransferred];
		
		if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
			// Copy input data into the new buffer
			System.arraycopy(buff, bytesTransferred, remainingBuffer, 0, remainingBuffer.length);
		}
		
		int res = devConn.bulkTransfer(endpoint, remainingBuffer,
				remainingBuffer.length, timeout);
		if (res < 0) {
			// Failed transfer terminates the bulk transfer
			res = -Errno.getErrno();
			if (res != -110) { 
				// Don't print for ETIMEDOUT
				System.err.println("Bulk Xfer failed: "+res);
			}
			return res;
		}
		
		if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
			// Copy output data into the original buffer
			System.arraycopy(remainingBuffer, 0, buff, bytesTransferred, res);
		}
		
		bytesTransferred += res;
		
		if (res < endpoint.getMaxPacketSize()) {
			// A packet less than the maximum size for this endpoint
			// indicates the transfer has ended
			break;
		}
	}
	
	return bytesTransferred;
}
 
Example 6
Source File: CH34xSerialDevice.java    From UsbSerial with MIT License 5 votes vote down vote up
private boolean openCH34X()
{
    if(connection.claimInterface(mInterface, true))
    {
        Log.i(CLASS_ID, "Interface succesfully claimed");
    }else
    {
        Log.i(CLASS_ID, "Interface could not be claimed");
        return false;
    }

    // Assign endpoints
    int numberEndpoints = mInterface.getEndpointCount();
    for(int i=0;i<=numberEndpoints-1;i++)
    {
        UsbEndpoint endpoint = mInterface.getEndpoint(i);
        if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
                && endpoint.getDirection() == UsbConstants.USB_DIR_IN)
        {
            inEndpoint = endpoint;
        }else if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
                && endpoint.getDirection() == UsbConstants.USB_DIR_OUT)
        {
            outEndpoint = endpoint;
        }
    }

    return init() == 0;
}
 
Example 7
Source File: CDCSerialDevice.java    From UsbSerial with MIT License 5 votes vote down vote up
private boolean openCDC()
{
    if(connection.claimInterface(mInterface, true))
    {
        Log.i(CLASS_ID, "Interface succesfully claimed");
    }else
    {
        Log.i(CLASS_ID, "Interface could not be claimed");
        return false;
    }

    // Assign endpoints
    int numberEndpoints = mInterface.getEndpointCount();
    for(int i=0;i<=numberEndpoints-1;i++)
    {
        UsbEndpoint endpoint = mInterface.getEndpoint(i);
        if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
                && endpoint.getDirection() == UsbConstants.USB_DIR_IN)
        {
            inEndpoint = endpoint;
        }else if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
                && endpoint.getDirection() == UsbConstants.USB_DIR_OUT)
        {
            outEndpoint = endpoint;
        }
    }

    if(outEndpoint == null || inEndpoint == null)
    {
        Log.i(CLASS_ID, "Interface does not have an IN or OUT interface");
        return false;
    }

    // Default Setup
    setControlCommand(CDC_SET_LINE_CODING, 0, getInitialLineCoding());
    setControlCommand(CDC_SET_CONTROL_LINE_STATE, CDC_CONTROL_LINE_ON, null);

    return true;
}
 
Example 8
Source File: CdcAcmSerialDriver.java    From Chorus-RF-Laptimer with MIT License 4 votes vote down vote up
private void openSingleInterface() throws IOException {
    // the following code is inspired by the cdc-acm driver
    // in the linux kernel

    mControlInterface = mDevice.getInterface(0);
    Log.d(TAG, "Control iface=" + mControlInterface);

    mDataInterface = mDevice.getInterface(0);
    Log.d(TAG, "data iface=" + mDataInterface);

    if (!mConnection.claimInterface(mControlInterface, true)) {
        throw new IOException("Could not claim shared control/data interface.");
    }

    int endCount = mControlInterface.getEndpointCount();

    if (endCount < 3) {
        Log.d(TAG,"not enough endpoints - need 3. count=" + mControlInterface.getEndpointCount());
        throw new IOException("Insufficient number of endpoints(" + mControlInterface.getEndpointCount() + ")");
    }

    // Analyse endpoints for their properties
    mControlEndpoint = null;
    mReadEndpoint = null;
    mWriteEndpoint = null;
    for (int i = 0; i < endCount; ++i) {
        UsbEndpoint ep = mControlInterface.getEndpoint(i);
        if ((ep.getDirection() == UsbConstants.USB_DIR_IN) &&
            (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)) {
            Log.d(TAG,"Found controlling endpoint");
            mControlEndpoint = ep;
        } else if ((ep.getDirection() == UsbConstants.USB_DIR_IN) &&
                   (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
            Log.d(TAG,"Found reading endpoint");
            mReadEndpoint = ep;
        } else if ((ep.getDirection() == UsbConstants.USB_DIR_OUT) &&
                (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
            Log.d(TAG,"Found writing endpoint");
            mWriteEndpoint = ep;
        }


        if ((mControlEndpoint != null) &&
            (mReadEndpoint != null) &&
            (mWriteEndpoint != null)) {
            Log.d(TAG,"Found all required endpoints");
            break;
        }
    }

    if ((mControlEndpoint == null) ||
            (mReadEndpoint == null) ||
            (mWriteEndpoint == null)) {
        Log.d(TAG,"Could not establish all endpoints");
        throw new IOException("Could not establish all endpoints");
    }
}
 
Example 9
Source File: CdcAcmSerialDriver.java    From OkUSB with Apache License 2.0 4 votes vote down vote up
private void openSingleInterface() throws IOException {
    // the following code is inspired by the cdc-acm driver
    // in the linux kernel

    mControlInterface = mDevice.getInterface(0);
    Log.d(TAG, "Control iface=" + mControlInterface);

    mDataInterface = mDevice.getInterface(0);
    Log.d(TAG, "data iface=" + mDataInterface);

    if (!mConnection.claimInterface(mControlInterface, true)) {
        throw new IOException("Could not claim shared control/data interface.");
    }

    int endCount = mControlInterface.getEndpointCount();

    if (endCount < 3) {
        Log.d(TAG,"not enough endpoints - need 3. count=" + mControlInterface.getEndpointCount());
        throw new IOException("Insufficient number of endpoints(" + mControlInterface.getEndpointCount() + ")");
    }

    // Analyse endpoints for their properties
    mControlEndpoint = null;
    mReadEndpoint = null;
    mWriteEndpoint = null;
    for (int i = 0; i < endCount; ++i) {
        UsbEndpoint ep = mControlInterface.getEndpoint(i);
        if ((ep.getDirection() == UsbConstants.USB_DIR_IN) &&
            (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)) {
            Log.d(TAG,"Found controlling endpoint");
            mControlEndpoint = ep;
        } else if ((ep.getDirection() == UsbConstants.USB_DIR_IN) &&
                   (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
            Log.d(TAG,"Found reading endpoint");
            mReadEndpoint = ep;
        } else if ((ep.getDirection() == UsbConstants.USB_DIR_OUT) &&
                (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
            Log.d(TAG,"Found writing endpoint");
            mWriteEndpoint = ep;
        }


        if ((mControlEndpoint != null) &&
            (mReadEndpoint != null) &&
            (mWriteEndpoint != null)) {
            Log.d(TAG,"Found all required endpoints");
            break;
        }
    }

    if ((mControlEndpoint == null) ||
            (mReadEndpoint == null) ||
            (mWriteEndpoint == null)) {
        Log.d(TAG,"Could not establish all endpoints");
        throw new IOException("Could not establish all endpoints");
    }
}
 
Example 10
Source File: CdcAcmSerialDriver.java    From usb-with-serial-port with Apache License 2.0 4 votes vote down vote up
private void openSingleInterface() throws IOException {
    // the following code is inspired by the cdc-acm driver
    // in the linux kernel

    mControlInterface = mDevice.getInterface(0);
    Log.d(TAG, "Control iface=" + mControlInterface);

    mDataInterface = mDevice.getInterface(0);
    Log.d(TAG, "data iface=" + mDataInterface);

    if (!mConnection.claimInterface(mControlInterface, true)) {
        throw new IOException("Could not claim shared control/data interface.");
    }

    int endCount = mControlInterface.getEndpointCount();

    if (endCount < 3) {
        Log.d(TAG, "not enough endpoints - need 3. count=" + mControlInterface.getEndpointCount());
        throw new IOException("Insufficient number of endpoints(" + mControlInterface.getEndpointCount() + ")");
    }

    // Analyse endpoints for their properties
    mControlEndpoint = null;
    mReadEndpoint = null;
    mWriteEndpoint = null;
    for (int i = 0; i < endCount; ++i) {
        UsbEndpoint ep = mControlInterface.getEndpoint(i);
        if ((ep.getDirection() == UsbConstants.USB_DIR_IN) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)) {
            Log.d(TAG, "Found controlling endpoint");
            mControlEndpoint = ep;
        } else if ((ep.getDirection() == UsbConstants.USB_DIR_IN) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
            Log.d(TAG, "Found reading endpoint");
            mReadEndpoint = ep;
        } else if ((ep.getDirection() == UsbConstants.USB_DIR_OUT) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
            Log.d(TAG, "Found writing endpoint");
            mWriteEndpoint = ep;
        }


        if ((mControlEndpoint != null) && (mReadEndpoint != null) && (mWriteEndpoint != null)) {
            Log.d(TAG, "Found all required endpoints");
            break;
        }
    }

    if ((mControlEndpoint == null) || (mReadEndpoint == null) || (mWriteEndpoint == null)) {
        Log.d(TAG, "Could not establish all endpoints");
        throw new IOException("Could not establish all endpoints");
    }
}
 
Example 11
Source File: UsbFacade.java    From Pincho-Usb-Mass-Storage-for-Android with MIT License 4 votes vote down vote up
public boolean openDevice()
{
    int index = mDevice.getInterfaceCount();
    for(int i=0;i<=index-1;i++)
    {
        if(massStorageInterface == null) // Silly check only meaningful when testing
            massStorageInterface = mDevice.getInterface(i);

        if(massStorageInterface.getInterfaceClass() == UsbConstants.USB_CLASS_MASS_STORAGE && massStorageInterface.getInterfaceSubclass() == 0x06
                && massStorageInterface.getInterfaceProtocol() == 0x50)
        {
            if(mConnection.claimInterface(massStorageInterface, true))
            {
                int endpointCount = massStorageInterface.getEndpointCount();
                for(int j=0;j<=endpointCount-1;j++)
                {
                    UsbEndpoint endpoint = massStorageInterface.getEndpoint(j);
                    if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
                            && endpoint.getDirection() == UsbConstants.USB_DIR_IN)
                    {
                        inEndpoint = endpoint;
                    }else if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
                            && endpoint.getDirection() == UsbConstants.USB_DIR_OUT)
                    {
                        outEndpoint = endpoint;
                    }
                }

                if(inEndpoint != null && outEndpoint != null)
                {
                    dataOutThread = new DataOutThread();
                    dataOutThread.start();
                    dataInThread = new DataInThread();
                    dataInThread.start();

                    while(outHandler == null)
                    {
                        //Busy waiting to avoid outHandler being null :(
                    }

                    return true;
                }else
                {
                    return false;
                }

            }else
            {
                return false;
            }
        }
    }
    return false;
}
 
Example 12
Source File: PL2303SerialDevice.java    From UsbSerial with MIT License 4 votes vote down vote up
private boolean openPL2303()
{
    if(connection.claimInterface(mInterface, true))
    {
        Log.i(CLASS_ID, "Interface succesfully claimed");
    }else
    {
        Log.i(CLASS_ID, "Interface could not be claimed");
        return false;
    }

    // Assign endpoints
    int numberEndpoints = mInterface.getEndpointCount();
    for(int i=0;i<=numberEndpoints-1;i++)
    {
        UsbEndpoint endpoint = mInterface.getEndpoint(i);
        if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
                && endpoint.getDirection() == UsbConstants.USB_DIR_IN)
            inEndpoint = endpoint;
        else if(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK
                && endpoint.getDirection() == UsbConstants.USB_DIR_OUT)
            outEndpoint = endpoint;
    }

    //Default Setup
    byte[] buf = new byte[1];
    //Specific vendor stuff that I barely understand but It is on linux drivers, So I trust :)
    if(setControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8484, 0, buf) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0404, 0, null) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8484, 0, buf) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8383, 0, buf) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8484, 0, buf) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0404, 1, null) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8484, 0, buf) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_DEVICE2HOST_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x8383, 0, buf) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0000, 1, null) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0001, 0, null) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0002, 0x0044, null) < 0)
        return false;
    // End of specific vendor stuff
    if(setControlCommand(PL2303_REQTYPE_HOST2DEVICE, PL2303_SET_CONTROL_REQUEST, 0x0003, 0,null) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_HOST2DEVICE, PL2303_SET_LINE_CODING, 0x0000, 0, defaultSetLine) < 0)
        return false;
    if(setControlCommand(PL2303_REQTYPE_HOST2DEVICE_VENDOR, PL2303_VENDOR_WRITE_REQUEST, 0x0505, 0x1311, null) < 0)
        return false;

    return true;
}
 
Example 13
Source File: Ch34xSerialDriver.java    From usb-serial-for-android with GNU Lesser General Public License v2.1 4 votes vote down vote up
private int controlOut(int request, int value, int index) {
	final int REQTYPE_HOST_TO_DEVICE = UsbConstants.USB_TYPE_VENDOR | UsbConstants.USB_DIR_OUT;
	return mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, request,
			value, index, null, 0, USB_TIMEOUT_MILLIS);
}
 
Example 14
Source File: CdcAcmSerialDriver.java    From usb-serial-for-android with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void openInterface() throws IOException {
    Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount());

    int controlInterfaceCount = 0;
    int dataInterfaceCount = 0;
    mControlInterface = null;
    mDataInterface = null;
    for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
        UsbInterface usbInterface = mDevice.getInterface(i);
        if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_COMM) {
            if(controlInterfaceCount == mPortNumber) {
                mControlIndex = i;
                mControlInterface = usbInterface;
            }
            controlInterfaceCount++;
        }
        if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
            if(dataInterfaceCount == mPortNumber) {
                mDataInterface = usbInterface;
            }
            dataInterfaceCount++;
        }
    }

    if(mControlInterface == null) {
        throw new IOException("No control interface");
    }
    Log.d(TAG, "Control iface=" + mControlInterface);

    if (!mConnection.claimInterface(mControlInterface, true)) {
        throw new IOException("Could not claim control interface");
    }

    mControlEndpoint = mControlInterface.getEndpoint(0);
    if (mControlEndpoint.getDirection() != UsbConstants.USB_DIR_IN || mControlEndpoint.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
        throw new IOException("Invalid control endpoint");
    }

    if(mDataInterface == null) {
        throw new IOException("No data interface");
    }
    Log.d(TAG, "data iface=" + mDataInterface);

    if (!mConnection.claimInterface(mDataInterface, true)) {
        throw new IOException("Could not claim data interface");
    }

    for (int i = 0; i < mDataInterface.getEndpointCount(); i++) {
        UsbEndpoint ep = mDataInterface.getEndpoint(i);
        if (ep.getDirection() == UsbConstants.USB_DIR_IN && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
            mReadEndpoint = ep;
        if (ep.getDirection() == UsbConstants.USB_DIR_OUT && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
            mWriteEndpoint = ep;
    }
}