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

The following examples show how to use android.hardware.usb.UsbEndpoint#getType() . 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: 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 2
Source File: Ch34xSerialDriver.java    From usb-serial-for-android with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
protected void openInt(UsbDeviceConnection connection) throws IOException {
	for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
		UsbInterface usbIface = mDevice.getInterface(i);
		if (!mConnection.claimInterface(usbIface, true)) {
			throw new IOException("Could not claim data interface");
		}
	}

	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;
			}
		}
	}

	initialize();
	setBaudRate(DEFAULT_BAUD_RATE);
}
 
Example 3
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 4
Source File: CP2102SerialDevice.java    From UsbSerial with MIT License 5 votes vote down vote up
private boolean openCP2102()
{
    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(CP210x_IFC_ENABLE, CP210x_UART_ENABLE, null) < 0)
        return false;
    setBaudRate(DEFAULT_BAUDRATE);
    if(setControlCommand(CP210x_SET_LINE_CTL, CP210x_LINE_CTL_DEFAULT,null) < 0)
        return false;
    setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF);
    if(setControlCommand(CP210x_SET_MHS, CP210x_MHS_DEFAULT, null) < 0)
        return false;

    return true;
}
 
Example 5
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 6
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 7
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 8
Source File: UsbIpService.java    From USBIPServerForAndroid with GNU General Public License v3.0 4 votes vote down vote up
private int detectSpeed(UsbDevice dev, UsbDeviceDescriptor devDesc) {
	int possibleSpeeds = FLAG_POSSIBLE_SPEED_LOW |
			FLAG_POSSIBLE_SPEED_FULL |
			FLAG_POSSIBLE_SPEED_HIGH;
	
	for (int i = 0; i < dev.getInterfaceCount(); i++) {
		UsbInterface iface = dev.getInterface(i);
		for (int j = 0; j < iface.getEndpointCount(); j++) {
			UsbEndpoint endpoint = iface.getEndpoint(j);
			if ((endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) ||
				(endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_ISOC)) {
				// Low speed devices can't implement bulk or iso endpoints
				possibleSpeeds &= ~FLAG_POSSIBLE_SPEED_LOW;
			}
			
			if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_CONTROL) {
				if (endpoint.getMaxPacketSize() > 8) {
					// Low speed devices can't use control transfer sizes larger than 8 bytes
					possibleSpeeds &= ~FLAG_POSSIBLE_SPEED_LOW;
				}
				if (endpoint.getMaxPacketSize() < 64) {
					// High speed devices can't use control transfer sizes smaller than 64 bytes
					possibleSpeeds &= ~FLAG_POSSIBLE_SPEED_HIGH;
				}
			}
			else if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_INT) {
				if (endpoint.getMaxPacketSize() > 8) {
					// Low speed devices can't use interrupt transfer sizes larger than 8 bytes
					possibleSpeeds &= ~FLAG_POSSIBLE_SPEED_LOW;
				}
				if (endpoint.getMaxPacketSize() > 64) {
					// Full speed devices can't use interrupt transfer sizes larger than 64 bytes
					possibleSpeeds &= ~FLAG_POSSIBLE_SPEED_FULL;
				}
			}
			else if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
				// A bulk endpoint alone can accurately distiniguish between
				// full and high speed devices
				if (endpoint.getMaxPacketSize() == 512) {
					// High speed devices can only use 512 byte bulk transfers
					possibleSpeeds = FLAG_POSSIBLE_SPEED_HIGH;
				}
				else {
					// Otherwise it must be full speed
					possibleSpeeds = FLAG_POSSIBLE_SPEED_FULL;
				}
			}
			else if (endpoint.getType() == UsbConstants.USB_ENDPOINT_XFER_ISOC) {
				// If the transfer size is 1024, it must be high speed
				if (endpoint.getMaxPacketSize() == 1024) {
					possibleSpeeds = FLAG_POSSIBLE_SPEED_HIGH;
				}
			}
		}
	}
	
	if (devDesc != null) {
		if (devDesc.bcdUSB < 0x200) {
			// High speed only supported on USB 2.0 or higher
			possibleSpeeds &= ~FLAG_POSSIBLE_SPEED_HIGH;
		}
	}
	
	// Return the lowest speed that we're compatible with
	System.out.printf("Speed heuristics for device %d left us with 0x%x\n",
			dev.getDeviceId(), possibleSpeeds);

	if ((possibleSpeeds & FLAG_POSSIBLE_SPEED_LOW) != 0) {
		return UsbIpDevice.USB_SPEED_LOW;
	}
	else if ((possibleSpeeds & FLAG_POSSIBLE_SPEED_FULL) != 0) {
		return UsbIpDevice.USB_SPEED_FULL;
	}
	else if ((possibleSpeeds & FLAG_POSSIBLE_SPEED_HIGH) != 0) {
		return UsbIpDevice.USB_SPEED_HIGH;
	}
	else {
		// Something went very wrong in speed detection
		return UsbIpDevice.USB_SPEED_UNKNOWN;
	}
}
 
Example 9
Source File: Cp21xxSerialDriver.java    From Arduino-Serial-Controller with GNU Lesser General Public License v2.1 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 10
Source File: CH34xSerialDriver.java    From Arduino-Serial-Controller with GNU Lesser General Public License v2.1 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;
				}
			}
		}


		initialize();
		setBaudRate(DEFAULT_BAUD_RATE);

		opened = true;
	} finally {
		if (!opened) {
			try {
				close();
			} catch (IOException e) {
				// Ignore IOExceptions during close()
			}
		}
	}
}
 
Example 11
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 12
Source File: Cp21xxSerialDriver.java    From xDrip-Experimental 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 13
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;
    }
}
 
Example 14
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 15
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 16
Source File: Ch34xSerialDriver.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;
				}
			}
		}


		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 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 18
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 19
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 20
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()
                }
            }
        }
    }