Java Code Examples for android.hardware.usb.UsbEndpoint#getDirection()

The following examples show how to use android.hardware.usb.UsbEndpoint#getDirection() . 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: UsbSession.java    From yubikit-android with Apache License 2.0 6 votes vote down vote up
/**
 * Gets bulkin and bulkout endpoints of specified interface
 * @param usbInterface interface of usb device
 * @return the pair of endpoints: in and out
 */
private Pair<UsbEndpoint, UsbEndpoint> findEndpoints(UsbInterface usbInterface, int type) {
    UsbEndpoint endpointIn = null;
    UsbEndpoint endpointOut = null;

    for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
        UsbEndpoint endpoint = usbInterface.getEndpoint(i);
        if (endpoint.getType() == type) {
            if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
                endpointIn = endpoint;
            } else {
                endpointOut = endpoint;
            }
        }
    }
    return new Pair<>(endpointIn, endpointOut);
}
 
Example 3
Source File: BTChipTransportAndroidHID.java    From xmrwallet with Apache License 2.0 6 votes vote down vote up
public static BTChipTransport open(UsbManager manager, UsbDevice device) throws IOException {
    UsbDeviceConnection connection = manager.openDevice(device);
    if (connection == null) throw new IOException("Device not connected");
    // Must only be called once permission is granted (see http://developer.android.com/reference/android/hardware/usb/UsbManager.html)
    // Important if enumerating, rather than being awaken by the intent notification
    UsbInterface dongleInterface = device.getInterface(0);
    UsbEndpoint in = null;
    UsbEndpoint out = null;
    for (int i = 0; i < dongleInterface.getEndpointCount(); i++) {
        UsbEndpoint tmpEndpoint = dongleInterface.getEndpoint(i);
        if (tmpEndpoint.getDirection() == UsbConstants.USB_DIR_IN) {
            in = tmpEndpoint;
        } else {
            out = tmpEndpoint;
        }
    }
    connection.claimInterface(dongleInterface, true);
    return new BTChipTransportAndroidHID(connection, dongleInterface, in, out);
}
 
Example 4
Source File: BTChipTransportAndroid.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static BTChipTransport open(UsbManager manager, UsbDevice device) {
	// Must only be called once permission is granted (see http://developer.android.com/reference/android/hardware/usb/UsbManager.html)
	// Important if enumerating, rather than being awaken by the intent notification
	UsbInterface dongleInterface = device.getInterface(0);
       UsbEndpoint in = null;
       UsbEndpoint out = null;
       boolean ledger; 
       for (int i=0; i<dongleInterface.getEndpointCount(); i++) {
           UsbEndpoint tmpEndpoint = dongleInterface.getEndpoint(i);
           if (tmpEndpoint.getDirection() == UsbConstants.USB_DIR_IN) {
               in = tmpEndpoint;
           }
           else {
               out = tmpEndpoint;
           }
       }
       UsbDeviceConnection connection = manager.openDevice(device);
       connection.claimInterface(dongleInterface, true);
       ledger = ((device.getProductId() == PID_HID_LEDGER) || (device.getProductId() == PID_HID_LEDGER_PROTON)
	|| (device.getProductId() == PID_NANOS) || (device.getProductId() == PID_BLUE));
       if (device.getProductId() == PID_WINUSB) {
       	return new BTChipTransportAndroidWinUSB(connection, dongleInterface, in, out, TIMEOUT);
       }
       else {
       	return new BTChipTransportAndroidHID(connection, dongleInterface, in, out, TIMEOUT, ledger);
       }
}
 
Example 5
Source File: CP2130SpiDevice.java    From UsbSerial with MIT License 5 votes vote down vote up
private boolean openCP2130()
{
    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
        {
            outEndpoint = endpoint;
        }
    }

    return true;
}
 
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: BTChipTransportAndroid.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public static BTChipTransport open(UsbManager manager, UsbDevice device) {
	// Must only be called once permission is granted (see http://developer.android.com/reference/android/hardware/usb/UsbManager.html)
	// Important if enumerating, rather than being awaken by the intent notification
	UsbInterface dongleInterface = device.getInterface(0);
       UsbEndpoint in = null;
       UsbEndpoint out = null;
       boolean ledger; 
       for (int i=0; i<dongleInterface.getEndpointCount(); i++) {
           UsbEndpoint tmpEndpoint = dongleInterface.getEndpoint(i);
           if (tmpEndpoint.getDirection() == UsbConstants.USB_DIR_IN) {
               in = tmpEndpoint;
           }
           else {
               out = tmpEndpoint;
           }
       }
       UsbDeviceConnection connection = manager.openDevice(device);
       if (connection == null) {
           return null;
       }
       connection.claimInterface(dongleInterface, true);
       ledger = ((device.getProductId() == PID_HID_LEDGER) || (device.getProductId() == PID_HID_LEDGER_PROTON)
	|| (device.getProductId() == PID_NANOS) || (device.getProductId() == PID_BLUE));
       if (device.getProductId() == PID_WINUSB) {
       	return new BTChipTransportAndroidWinUSB(connection, dongleInterface, in, out, TIMEOUT);
       }
       else {
       	return new BTChipTransportAndroidHID(connection, dongleInterface, in, out, TIMEOUT, ledger);
       }
}
 
Example 8
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 9
Source File: FTDISerialDevice.java    From UsbSerial with MIT License 4 votes vote down vote up
private boolean openFTDI()
{
    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
        {
            outEndpoint = endpoint;
        }
    }

    // Default Setup
    firstTime = true;
    if(setControlCommand(FTDI_SIO_RESET, 0x00, 0) < 0)
        return false;
    if(setControlCommand(FTDI_SIO_SET_DATA, FTDI_SET_DATA_DEFAULT, 0) < 0)
        return false;
    currentSioSetData = FTDI_SET_DATA_DEFAULT;
    if(setControlCommand(FTDI_SIO_MODEM_CTRL, FTDI_SET_MODEM_CTRL_DEFAULT1, 0) < 0)
        return false;
    if(setControlCommand(FTDI_SIO_MODEM_CTRL, FTDI_SET_MODEM_CTRL_DEFAULT2, 0) < 0)
        return false;
    if(setControlCommand(FTDI_SIO_SET_FLOW_CTRL, FTDI_SET_FLOW_CTRL_DEFAULT, 0) < 0)
        return false;
    if(setControlCommand(FTDI_SIO_SET_BAUD_RATE, FTDI_BAUDRATE_9600, 0) < 0)
        return false;

    // Flow control disabled by default
    rtsCtsEnabled = false;
    dtrDsrEnabled = false;

    return true;
}
 
Example 10
Source File: Cp21xxSerialDriver.java    From PodEmu with GNU General Public License v3.0 4 votes vote down vote up
@Override
    public void open(UsbDeviceConnection connection) throws IOException {
        if (mConnection != null) {
            throw new IOException("Already opened.");
        }

        mConnection = connection;
        boolean opened = false;
        try {
            for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
                UsbInterface usbIface = mDevice.getInterface(i);
                if (mConnection.claimInterface(usbIface, true)) {
                    Log.d(TAG, "claimInterface " + i + " SUCCESS");
                } else {
                    Log.d(TAG, "claimInterface " + i + " FAIL");
                }
            }

            UsbInterface dataIface = mDevice.getInterface(mDevice.getInterfaceCount() - 1);
            for (int i = 0; i < dataIface.getEndpointCount(); i++) {
                UsbEndpoint ep = dataIface.getEndpoint(i);
                if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                    if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                        mReadEndpoint = ep;
                    } else {
                        mWriteEndpoint = ep;
                    }
                }
            }

            setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
            setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
            setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
//            setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY);
            opened = true;
        } finally {
            if (!opened) {
                try {
                    close();
                } catch (IOException e) {
                    // Ignore IOExceptions during close()
                }
            }
        }
    }
 
Example 11
Source File: BLED112SerialDevice.java    From UsbSerial with MIT License 4 votes vote down vote up
@Override
public boolean open()
{
    // Restart the working thread if it has been killed before and  get and claim interface
    restartWorkingThread();
    restartWriteThread();

    if(connection.claimInterface(mInterface, true))
    {
        Log.i(CLASS_ID, "Interface succesfully claimed");
    }else
    {
        Log.i(CLASS_ID, "Interface could not be claimed");
    }

    // 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
        {
            outEndpoint = endpoint;
        }
    }

    // Default Setup
    setControlCommand(BLED112_SET_LINE_CODING, 0, BLED112_DEFAULT_LINE_CODING);
    setControlCommand(BLED112_SET_CONTROL_LINE_STATE, BLED112_DEFAULT_CONTROL_LINE, null);

    // Initialize UsbRequest
    UsbRequest requestIN = new UsbRequest();
    requestIN.initialize(connection, inEndpoint);

    // Pass references to the threads
    setThreadsParams(requestIN, outEndpoint);

    return true;
}
 
Example 12
Source File: XdcVcpSerialDevice.java    From UsbSerial with MIT License 4 votes vote down vote up
@Override
public boolean open()
{

    // Restart the working thread and writeThread if it has been killed before and  get and claim interface
    restartWorkingThread();
    restartWriteThread();

    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
        {
            outEndpoint = endpoint;
        }
    }


    // Default Setup
    if(setControlCommand(XDCVCP_IFC_ENABLE, XDCVCP_UART_ENABLE, null) < 0)
        return false;
    setBaudRate(DEFAULT_BAUDRATE);
    if(setControlCommand(XDCVCP_SET_LINE_CTL, XDCVCP_LINE_CTL_DEFAULT,null) < 0)
        return false;
    setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
    if(setControlCommand(XDCVCP_SET_MHS, XDCVCP_MHS_DEFAULT, null) < 0)
        return false;

    // Initialize UsbRequest
    UsbRequest requestIN = new UsbRequest();
    requestIN.initialize(connection, inEndpoint);

    // Pass references to the threads
    setThreadsParams(requestIN, outEndpoint);

    return true;
}
 
Example 13
Source File: Cp21xxSerialDriver.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
@Override
    public void open(UsbDeviceConnection connection) throws IOException {
        if (mConnection != null) {
            throw new IOException("Already opened.");
        }

        mConnection = connection;
        boolean opened = false;
        try {
            for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
                UsbInterface usbIface = mDevice.getInterface(i);
                if (mConnection.claimInterface(usbIface, true)) {
                    Log.d(TAG, "claimInterface " + i + " SUCCESS");
                } else {
                    Log.d(TAG, "claimInterface " + i + " FAIL");
                }
            }

            UsbInterface dataIface = mDevice.getInterface(mDevice.getInterfaceCount() - 1);
            for (int i = 0; i < dataIface.getEndpointCount(); i++) {
                UsbEndpoint ep = dataIface.getEndpoint(i);
                if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                    if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                        mReadEndpoint = ep;
                    } else {
                        mWriteEndpoint = ep;
                    }
                }
            }

            setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
            setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
            setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
//            setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY);
            opened = true;
        } finally {
            if (!opened) {
                try {
                    close();
                } catch (IOException e) {
                    // Ignore IOExceptions during close()
                }
            }
        }
    }
 
Example 14
Source File: Cp21xxSerialDriver.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
@Override
    public void open(UsbDeviceConnection connection) throws IOException {
        if (mConnection != null) {
            throw new IOException("Already opened.");
        }

        mConnection = connection;
        boolean opened = false;
        try {
            for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
                UsbInterface usbIface = mDevice.getInterface(i);
                if (mConnection.claimInterface(usbIface, true)) {
                    Log.d(TAG, "claimInterface " + i + " SUCCESS");
                } else {
                    Log.d(TAG, "claimInterface " + i + " FAIL");
                }
            }

            UsbInterface dataIface = mDevice.getInterface(mDevice.getInterfaceCount() - 1);
            for (int i = 0; i < dataIface.getEndpointCount(); i++) {
                UsbEndpoint ep = dataIface.getEndpoint(i);
                if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                    if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                        mReadEndpoint = ep;
                    } else {
                        mWriteEndpoint = ep;
                    }
                }
            }

            setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
            setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
            setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
//            setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY);
            opened = true;
        } finally {
            if (!opened) {
                try {
                    close();
                } catch (IOException e) {
                    // Ignore IOExceptions during close()
                }
            }
        }
    }
 
Example 15
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 16
Source File: Ch34xSerialDriver.java    From usb-with-serial-port with Apache License 2.0 4 votes vote down vote up
@Override
public void open(UsbDeviceConnection connection) throws IOException {
    if (mConnection != null) {
        throw new IOException("Already opened.");
    }

    mConnection = connection;
    boolean opened = false;
    try {
        for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
            UsbInterface usbIface = mDevice.getInterface(i);
            if (mConnection.claimInterface(usbIface, true)) {
                L.INSTANCE.d("claimInterface " + i + " SUCCESS");
            } else {
                L.INSTANCE.d("claimInterface " + i + " FAIL");
            }
        }

        UsbInterface dataIface = mDevice.getInterface(mDevice.getInterfaceCount() - 1);
        for (int i = 0; i < dataIface.getEndpointCount(); i++) {
            UsbEndpoint ep = dataIface.getEndpoint(i);
            if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                    mReadEndpoint = ep;
                } else {
                    mWriteEndpoint = ep;
                }
            } else {
                L.INSTANCE.d("ep.getType():" + ep.getType());
            }
        }

        initialize();
        setBaudRate(DEFAULT_BAUD_RATE);

        opened = true;
    } finally {
        if (!opened) {
            try {
                close();
            } catch (IOException e) {
                // Ignore IOExceptions during close()
            }
        }
    }
}
 
Example 17
Source File: Cp21xxSerialDriver.java    From OkUSB with Apache License 2.0 4 votes vote down vote up
@Override
    public void open(UsbDeviceConnection connection) throws IOException {
        if (mConnection != null) {
            throw new IOException("Already opened.");
        }

        mConnection = connection;
        boolean opened = false;
        try {
            for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
                UsbInterface usbIface = mDevice.getInterface(i);
                if (mConnection.claimInterface(usbIface, true)) {
                    Log.d(TAG, "claimInterface " + i + " SUCCESS");
                } else {
                    Log.d(TAG, "claimInterface " + i + " FAIL");
                }
            }

            UsbInterface dataIface = mDevice.getInterface(mDevice.getInterfaceCount() - 1);
            for (int i = 0; i < dataIface.getEndpointCount(); i++) {
                UsbEndpoint ep = dataIface.getEndpoint(i);
                if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                    if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                        mReadEndpoint = ep;
                    } else {
                        mWriteEndpoint = ep;
                    }
                }
            }

            setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
            setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
            setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
//            setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY);
            opened = true;
        } finally {
            if (!opened) {
                try {
                    close();
                } catch (IOException e) {
                    // Ignore IOExceptions during close()
                }
            }
        }
    }
 
Example 18
Source File: Cp21xxSerialDriver.java    From Chorus-RF-Laptimer with MIT License 4 votes vote down vote up
@Override
    public void open(UsbDeviceConnection connection) throws IOException {
        if (mConnection != null) {
            throw new IOException("Already opened.");
        }

        mConnection = connection;
        boolean opened = false;
        try {
            for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
                UsbInterface usbIface = mDevice.getInterface(i);
                if (mConnection.claimInterface(usbIface, true)) {
                    Log.d(TAG, "claimInterface " + i + " SUCCESS");
                } else {
                    Log.d(TAG, "claimInterface " + i + " FAIL");
                }
            }

            UsbInterface dataIface = mDevice.getInterface(mDevice.getInterfaceCount() - 1);
            for (int i = 0; i < dataIface.getEndpointCount(); i++) {
                UsbEndpoint ep = dataIface.getEndpoint(i);
                if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                    if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
                        mReadEndpoint = ep;
                    } else {
                        mWriteEndpoint = ep;
                    }
                }
            }

            setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
            setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
            setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
//            setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY);
            opened = true;
        } finally {
            if (!opened) {
                try {
                    close();
                } catch (IOException e) {
                    // Ignore IOExceptions during close()
                }
            }
        }
    }
 
Example 19
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 20
Source File: UsbLinkAndroid.java    From crazyflie-android-client with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Initialize the USB device. Determines endpoints and prepares communication.
 *
 * @param vid
 * @param pid
 * @throws IOException if the device cannot be opened
 * @throws SecurityException
 */
public void initDevice(int vid, int pid) throws IOException, SecurityException {
    mUsbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
    if (mUsbManager == null) {
        throw new IllegalArgumentException("UsbManager == null!");
    }

    List<UsbDevice> usbDevices = findUsbDevices(mUsbManager, (short) vid, (short) pid);
    if (usbDevices.isEmpty() || usbDevices.get(0) == null) {
        throw new IOException("USB device not found. (VID: " + vid + ", PID: " + pid + ")");
    }
    // TODO: Only gets the first USB device that is found
    this.mUsbDevice = usbDevices.get(0);

    //request permissions
    if (mUsbDevice != null && !mUsbManager.hasPermission(mUsbDevice)) {
        Log.d(LOG_TAG, "Request permission");
        mPermissionIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(mContext.getPackageName()+".USB_PERMISSION"), 0);
        mUsbManager.requestPermission(mUsbDevice, mPermissionIntent);
    } else if (mUsbDevice != null && mUsbManager.hasPermission(mUsbDevice)) {
        Log.d(LOG_TAG, "Has permission");
    } else {
        Log.d(LOG_TAG, "device == null");
        return;
    }

    Log.d(LOG_TAG, "setDevice " + this.mUsbDevice);
    // find interface
    if (this.mUsbDevice.getInterfaceCount() != 1) {
        Log.e(LOG_TAG, "Could not find interface");
        return;
    }
    mIntf = this.mUsbDevice.getInterface(0);
    // device should have two endpoints
    if (mIntf.getEndpointCount() != 2) {
        Log.e(LOG_TAG, "Could not find endpoints");
        return;
    }
    // endpoints should be of type bulk
    UsbEndpoint ep = mIntf.getEndpoint(0);
    if (ep.getType() != UsbConstants.USB_ENDPOINT_XFER_BULK) {
        Log.e(LOG_TAG, "Endpoint is not of type bulk");
        return;
    }
    // check endpoint direction
    if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
        mEpIn = mIntf.getEndpoint(0);
        mEpOut = mIntf.getEndpoint(1);
    } else {
        mEpIn = mIntf.getEndpoint(1);
        mEpOut = mIntf.getEndpoint(0);
    }

    UsbDeviceConnection connection = mUsbManager.openDevice(mUsbDevice);
    if (connection != null && connection.claimInterface(mIntf, true)) {
        Log.d(LOG_TAG, "open SUCCESS");
        mConnection = connection;
    } else {
        Log.d(LOG_TAG, "open FAIL");
        throw new IOException("could not open usb connection");
    }
}