Java Code Examples for android.hardware.usb.UsbInterface#getEndpoint()

The following examples show how to use android.hardware.usb.UsbInterface#getEndpoint() . 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: 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 2
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 3
Source File: Cp21xxSerialDriver.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 {
            mIsRestrictedPort = mDevice.getInterfaceCount() == 2 && mPortNumber == 1;
            if(mPortNumber >= mDevice.getInterfaceCount()) {
                throw new IOException("Unknown port number");
            }
            UsbInterface dataIface = mDevice.getInterface(mPortNumber);
            if (!mConnection.claimInterface(dataIface, true)) {
                throw new IOException("Could not claim interface " + mPortNumber);
            }
            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);
        }
 
Example 4
Source File: Usb.java    From android-stm32-dfu-programmer with Apache License 2.0 5 votes vote down vote up
public String getDeviceInfo(UsbDevice device) {
    if (device == null)
        return "No device found.";

    StringBuilder sb = new StringBuilder();
    sb.append("Model: " + device.getDeviceName() + "\n");
    sb.append("ID: " + device.getDeviceId() + " (0x" + Integer.toHexString(device.getDeviceId()) + ")" + "\n");
    sb.append("Class: " + device.getDeviceClass() + "\n");
    sb.append("Subclass: " + device.getDeviceSubclass() + "\n");
    sb.append("Protocol: " + device.getDeviceProtocol() + "\n");
    sb.append("Vendor ID " + device.getVendorId() + " (0x" + Integer.toHexString(device.getVendorId()) + ")" + "\n");
    sb.append("Product ID: " + device.getProductId() + " (0x" + Integer.toHexString(device.getProductId()) + ")" + "\n");
    sb.append("Device Ver: 0x" + Integer.toHexString(mDeviceVersion) + "\n");
    sb.append("Interface count: " + device.getInterfaceCount() + "\n");

    for (int i = 0; i < device.getInterfaceCount(); i++) {

        UsbInterface usbInterface = device.getInterface(i);

        sb.append("Interface: " + usbInterface.toString() + "\n");
        sb.append("Endpoint Count: " + usbInterface.getEndpointCount() + "\n");

        for (int j = 0; j < usbInterface.getEndpointCount(); j++) {

            UsbEndpoint ep = usbInterface.getEndpoint(j);

            sb.append("Endpoint: " + ep.toString() + "\n");
        }
    }

    return sb.toString();
}
 
Example 5
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 6
Source File: UsbMidiDeviceAndroid.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a UsbMidiDeviceAndroid.
 * @param manager
 * @param device The USB device which this object is assocated with.
 */
UsbMidiDeviceAndroid(UsbManager manager, UsbDevice device) {
    mConnection = manager.openDevice(device);
    mEndpointMap = new SparseArray<UsbEndpoint>();
    mRequestMap = new HashMap<UsbEndpoint, UsbRequest>();
    mHandler = new Handler();
    mUsbDevice = device;
    mIsClosed = false;
    mHasInputThread = false;
    mNativePointer = 0;

    for (int i = 0; i < device.getInterfaceCount(); ++i) {
        UsbInterface iface = device.getInterface(i);
        if (iface.getInterfaceClass() != UsbConstants.USB_CLASS_AUDIO
                || iface.getInterfaceSubclass() != MIDI_SUBCLASS) {
            continue;
        }
        mConnection.claimInterface(iface, true);
        for (int j = 0; j < iface.getEndpointCount(); ++j) {
            UsbEndpoint endpoint = iface.getEndpoint(j);
            if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
                mEndpointMap.put(endpoint.getEndpointNumber(), endpoint);
            }
        }
    }
    // Start listening for input endpoints.
    // This function will create and run a thread if there is USB-MIDI endpoints in the
    // device. Note that because UsbMidiDevice is shared among all tabs and the thread
    // will be terminated when the device is disconnected, at most one thread can be created
    // for each connected USB-MIDI device.
    startListen(device);
}
 
Example 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: BTChipTransportAndroid.java    From green_android 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) || (device.getProductId() == PID_NANOX));
       if (device.getProductId() == PID_WINUSB) {
       	return new BTChipTransportAndroidWinUSB(connection, dongleInterface, in, out, TIMEOUT);
       }
       else {
       	return new BTChipTransportAndroidHID(connection, dongleInterface, in, out, TIMEOUT, ledger);
       }
}
 
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 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 11
Source File: Cp21xxSerialDriver.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;
    boolean claim = false;
    try {
        for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
            UsbInterface usbIface = mDevice.getInterface(i);
            if (mConnection.claimInterface(usbIface, true)) {
                claim = true;
            } else {
                claim = false;
            }
        }
        if (!claim)
            throw new IOException("claim interface failed.");
        //                UsbInterface dataIface = mDevice.getInterface(mDevice.getInterfaceCount() - 1);
        for (int index = 0; index < mDevice.getInterfaceCount(); index++) {
            UsbInterface dataIface = mDevice.getInterface(index);
            for (int i = 0; i < dataIface.getEndpointCount(); i++) {
                UsbEndpoint ep = dataIface.getEndpoint(i);
                L.INSTANCE.d("断点类型:" + ep.getType());
                //按硬件属性判断
                //USB_ENDPOINT_XFER_BULK
                //#define USB_ENDPOINT_XFER_CONTROL 0 -- 控制传输
                //#define USB_ENDPOINT_XFER_ISOC 1 -- 等时传输
                //#define USB_ENDPOINT_XFER_BULK 2 -- 块传输
                //#define USB_ENDPOINT_XFER_INT 3 -- 中断传输
                //                        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);
        opened = true;
    } finally {
        if (!opened) {
            try {
                close();
            } catch (IOException e) {
                // Ignore IOExceptions during close()
            }
        }
    }
}
 
Example 12
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 13
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 14
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 15
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 16
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 17
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 18
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 19
Source File: Trezor.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
public static Trezor getDevice(final Context context) {
    final UsbManager manager = (UsbManager)context.getSystemService(Context.USB_SERVICE);

    for (final UsbDevice device: manager.getDeviceList().values()) {
        // Check if the device is TREZOR (or AvalonWallet or BWALLET)

        final int vendorId = device.getVendorId();
        final int productId = device.getProductId();

        if ((vendorId != 0x534c || productId != 0x0001) &&
            (vendorId != 0x1209 || productId != 0x53c0) &&
            (vendorId != 0x1209 || productId != 0x53c1) &&
            (vendorId != 0x10c4 || productId != 0xea80)) {
            continue;
        }

        Log.d(TAG, "Hardware Wallet device found");
        if (device.getInterfaceCount() < 1) {
            Log.e(TAG, "Wrong interface count");
            continue;
        }

        // Use first interface
        final UsbInterface iface = device.getInterface(0);
        // Try to find read/write endpoints
        UsbEndpoint readEndpoint = null, writeEndpoint = null;

        for (int i = 0; i < iface.getEndpointCount(); ++i) {
            final UsbEndpoint ep = iface.getEndpoint(i);

            if (readEndpoint == null &&
                    ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT &&
                    ep.getAddress() == 0x81) {
                // number = 1 ; dir = USB_DIR_IN
                readEndpoint = ep;
                continue;
            }
            if (writeEndpoint == null &&
                    ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT &&
                    (ep.getAddress() == 0x01 || ep.getAddress() == 0x02)) {
                // number = 1 ; dir = USB_DIR_OUT
                writeEndpoint = ep;
                continue;
            }
            Log.d(TAG, String.format("ep %d", ep.getAddress()));
        }

        if (isEndpointBad(readEndpoint, "read") || isEndpointBad(writeEndpoint, "write"))
            continue;

        // Try to open the device
        final UsbDeviceConnection conn = manager.openDevice(device);
        if (conn == null || !conn.claimInterface(iface,  true)) {
            Log.e(TAG, conn == null ? "Could not open connection" : "Could not claim interface");
            continue;
        }

        // All OK - return the class
        return new Trezor(device, conn, readEndpoint, writeEndpoint);
    }
    return null;
}
 
Example 20
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;
	}
}