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

The following examples show how to use android.hardware.usb.UsbConstants#USB_ENDPOINT_XFER_BULK . 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: 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 3
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 4
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 5
Source File: UsbHelper.java    From sample-usbenum with Apache License 2.0 5 votes vote down vote up
public static String nameForEndpointType(int type) {
    switch (type) {
        case UsbConstants.USB_ENDPOINT_XFER_BULK:
            return "Bulk";
        case UsbConstants.USB_ENDPOINT_XFER_CONTROL:
            return "Control";
        case UsbConstants.USB_ENDPOINT_XFER_INT:
            return "Interrupt";
        case UsbConstants.USB_ENDPOINT_XFER_ISOC:
            return "Isochronous";
        default:
            return "Unknown Type";
    }
}
 
Example 6
Source File: Ch34xSerialDriver.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;
				}
			}
		}


		initialize();
		setBaudRate(DEFAULT_BAUD_RATE);

		opened = true;
	} finally {
		if (!opened) {
			try {
				close();
			} catch (IOException e) {
				// Ignore IOExceptions during close()
			}
		}
	}
}
 
Example 7
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 8
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 9
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 10
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");
    }
}
 
Example 11
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 12
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 13
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 14
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 15
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 16
Source File: FTDI_USB_Handler.java    From gsn with GNU General Public License v3.0 4 votes vote down vote up
private boolean init_USB(UsbDevice dev) {
	showLog("Init_USB");
	
	try {
		if (dev == null)
			return false;
		UsbManager usbm = (UsbManager) activity
				.getSystemService(Context.USB_SERVICE);
		conn = usbm.openDevice(dev);
		l("Interface Count: " + dev.getInterfaceCount());
		l("Using "
				+ String.format("%04X:%04X", dev.getVendorId(), dev.getProductId()));

		if (!conn.claimInterface(dev.getInterface(0), true))
			return false;

		conn.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
		conn.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
		conn.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
		conn.controlTransfer(0x40, 0x02, 0x1311, 0x40, null, 0, 0);// control flow
																																// XOFF/XON
		// conn.controlTransfer(0x40, 0x04, 0x0008, 0, null, 0, 0); //data bit 8,
		// parity none, stop bit 1, tx off
		// conn.controlTransfer(0x40, 0x03, 0x809C, 0, null, 0, 0);//baudrate
		// 19200
		conn.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);// baudrate 9600

		usbIf = dev.getInterface(0);
		for (int i = 0; i < usbIf.getEndpointCount(); i++) {
			l("EP: " + String.format("0x%02X", usbIf.getEndpoint(i).getAddress()));
			if (usbIf.getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
				l("Bulk Endpoint");
				if (usbIf.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN)
					epIN = usbIf.getEndpoint(i);
				else
					epOUT = usbIf.getEndpoint(i);
			}
			else {
				l("Not Bulk");
			}
		}

		activity.registerReceiver(mUsbReceiver, new IntentFilter(
				UsbManager.ACTION_USB_DEVICE_DETACHED));
		l("init_USB: epIN " + epIN + ", epOUT: " + epOUT);

		readThread.start();
	}
	catch (final Exception e) {
		 e.printStackTrace();
	}

	showLog("init_USB finish");
	
	return (epIN != null && epOUT != null);
}
 
Example 17
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 18
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 19
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 20
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");
    }
}