android.hardware.usb.UsbEndpoint Java Examples

The following examples show how to use android.hardware.usb.UsbEndpoint. 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: 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 #4
Source File: UsbHidDevice.java    From UsbHid with MIT License 6 votes vote down vote up
private UsbHidDevice(UsbDevice usbDevice, UsbInterface usbInterface, UsbManager usbManager) {
    mUsbDevice = usbDevice;
    mUsbInterface = usbInterface;
    mUsbManager= usbManager;

    for (int i = 0; i < mUsbInterface.getEndpointCount(); i++) {
        UsbEndpoint endpoint = mUsbInterface.getEndpoint(i);
        int dir = endpoint.getDirection();
        int type = endpoint.getType();
        if (mInUsbEndpoint == null && dir == UsbConstants.USB_DIR_IN && type == UsbConstants.USB_ENDPOINT_XFER_INT) {
            mInUsbEndpoint = endpoint;
        }
        if (mOutUsbEndpoint == null && dir == UsbConstants.USB_DIR_OUT && type == UsbConstants.USB_ENDPOINT_XFER_INT) {
            mOutUsbEndpoint = endpoint;
        }
    }
}
 
Example #5
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 #6
Source File: UsbControlHelper.java    From USBIPServerForAndroid with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isEndpointHalted(UsbDeviceConnection devConn, UsbEndpoint endpoint) {
	byte[] statusBuffer = new byte[2];
	
	int res = XferUtils.doControlTransfer(devConn, GET_STATUS_REQUEST_TYPE,
			GET_STATUS_REQUEST,
			0,
			endpoint != null ? endpoint.getAddress() : 0,
			statusBuffer, statusBuffer.length, 0);
	if (res != statusBuffer.length) {
		return false;
	}
	
	return (statusBuffer[0] & 1) != 0;
}
 
Example #7
Source File: BTChipTransportAndroidHID.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public BTChipTransportAndroidHID(UsbDeviceConnection connection, UsbInterface dongleInterface, UsbEndpoint in, UsbEndpoint out, int timeout, boolean ledger) {
	this.connection = connection;
	this.dongleInterface = dongleInterface;
	this.in = in;
	this.out = out;
	this.ledger = ledger;
	// Compatibility with old prototypes, to be removed
	if (!this.ledger) {
		this.ledger = (in.getEndpointNumber() != out.getEndpointNumber());
	}
	this.timeout = timeout;
	transferBuffer = new byte[HID_BUFFER_SIZE];
}
 
Example #8
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 #9
Source File: BTChipTransportAndroidWinUSB.java    From WalletCordova with GNU Lesser General Public License v2.1 5 votes vote down vote up
public BTChipTransportAndroidWinUSB(UsbDeviceConnection connection, UsbInterface dongleInterface, UsbEndpoint in, UsbEndpoint out, int timeout) {
	this.connection = connection;
	this.dongleInterface = dongleInterface;
	this.in = in;
	this.out = out;
	this.timeout = timeout;
	transferBuffer = new byte[260];
}
 
Example #10
Source File: XferUtils.java    From USBIPServerForAndroid with GNU General Public License v3.0 5 votes vote down vote up
public static int doBulkTransfer(UsbDeviceConnection devConn, UsbEndpoint endpoint, byte[] buff, int timeout) {
	int bytesTransferred = 0;
	while (bytesTransferred < buff.length) {
		byte[] remainingBuffer = new byte[buff.length - bytesTransferred];
		
		if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) {
			// Copy input data into the new buffer
			System.arraycopy(buff, bytesTransferred, remainingBuffer, 0, remainingBuffer.length);
		}
		
		int res = devConn.bulkTransfer(endpoint, remainingBuffer,
				remainingBuffer.length, timeout);
		if (res < 0) {
			// Failed transfer terminates the bulk transfer
			res = -Errno.getErrno();
			if (res != -110) { 
				// Don't print for ETIMEDOUT
				System.err.println("Bulk Xfer failed: "+res);
			}
			return res;
		}
		
		if (endpoint.getDirection() == UsbConstants.USB_DIR_IN) {
			// Copy output data into the original buffer
			System.arraycopy(remainingBuffer, 0, buff, bytesTransferred, res);
		}
		
		bytesTransferred += res;
		
		if (res < endpoint.getMaxPacketSize()) {
			// A packet less than the maximum size for this endpoint
			// indicates the transfer has ended
			break;
		}
	}
	
	return bytesTransferred;
}
 
Example #11
Source File: UsbFacadeTest.java    From Pincho-Usb-Mass-Storage-for-Android with MIT License 5 votes vote down vote up
private void initUsb(int bulkResponse)
{
    mConnection = Mockito.mock(UsbDeviceConnection.class);
    mDevice = Mockito.mock(UsbDevice.class);

    // UsbInterface Mass storage device, Must be injected using a setter.
    ifaceMocked = Mockito.mock(UsbInterface.class);
    Mockito.when(ifaceMocked.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_MASS_STORAGE);
    Mockito.when(ifaceMocked.getInterfaceSubclass()).thenReturn(0x06);
    Mockito.when(ifaceMocked.getInterfaceProtocol()).thenReturn(0x50);
    Mockito.when(ifaceMocked.getEndpointCount()).thenReturn(0);

    // UsbEndpoints IN,OUT. Must be injected using a setter
    mockedInEndpoint = Mockito.mock(UsbEndpoint.class);
    mockedOutEndpoint = Mockito.mock(UsbEndpoint.class);

    // UsbDeviceConnection mocked methods
    Mockito.when(mConnection.claimInterface(ifaceMocked, true)).thenReturn(true);
    Mockito.when(mConnection.bulkTransfer(Mockito.any(UsbEndpoint.class), Mockito.any(byte[].class) ,Mockito.anyInt(), Mockito.anyInt())).thenReturn(bulkResponse);

    // UsbDevice mocked methods
    Mockito.when(mDevice.getInterfaceCount()).thenReturn(1);


    // Initialize and inject dependencies
    usbFacade = new UsbFacade(mDevice, mConnection);
    usbFacade.setCallback(mCallback);
    usbFacade.injectInterface(ifaceMocked);
    usbFacade.injectInEndpoint(mockedInEndpoint);
    usbFacade.injectOutEndpoint(mockedOutEndpoint);
}
 
Example #12
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 #13
Source File: UsbSpiDevice.java    From UsbSerial with MIT License 5 votes vote down vote up
protected void setThreadsParams(UsbEndpoint inEndpoint, UsbEndpoint outEndpoint)
{
    if(writeThread != null)
        writeThread.setUsbEndpoint(outEndpoint);

    if(readThread != null)
        readThread.setUsbEndpoint(inEndpoint);
}
 
Example #14
Source File: FtdiSerialDriver.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int write(byte[] src, int timeoutMillis) throws IOException {
    final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1);
    int offset = 0;

    while (offset < src.length) {
        final int writeLength;
        final int amtWritten;

        synchronized (mWriteBufferLock) {
            final byte[] writeBuffer;

            writeLength = Math.min(src.length - offset, mWriteBuffer.length);
            if (offset == 0) {
                writeBuffer = src;
            } else {
                // bulkTransfer does not support offsets, make a copy.
                System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
                writeBuffer = mWriteBuffer;
            }

            amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength,
                    timeoutMillis);
        }

        if (amtWritten <= 0) {
            throw new IOException("Error writing " + writeLength
                    + " bytes at offset " + offset + " length=" + src.length);
        }

        Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength);
        offset += amtWritten;
    }
    return offset;
}
 
Example #15
Source File: UsbSerialDevice.java    From UsbSerial with MIT License 5 votes vote down vote up
protected void setThreadsParams(UsbRequest request, UsbEndpoint endpoint)
{
    writeThread.setUsbEndpoint(endpoint);
    if(mr1Version)
    {
        workerThread.setUsbRequest(request);
    }else
    {
        readThread.setUsbEndpoint(request.getEndpoint());
    }
}
 
Example #16
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 #17
Source File: FtdiSerialDriver.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int write(byte[] src, int timeoutMillis) throws IOException {
    final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1);
    int offset = 0;

    while (offset < src.length) {
        final int writeLength;
        final int amtWritten;

        synchronized (mWriteBufferLock) {
            final byte[] writeBuffer;

            writeLength = Math.min(src.length - offset, mWriteBuffer.length);
            if (offset == 0) {
                writeBuffer = src;
            } else {
                // bulkTransfer does not support offsets, make a copy.
                System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
                writeBuffer = mWriteBuffer;
            }

            amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength,
                    timeoutMillis);
        }

        if (amtWritten <= 0) {
            throw new IOException("Error writing " + writeLength
                    + " bytes at offset " + offset + " length=" + src.length);
        }

        Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength);
        offset += amtWritten;
    }
    return offset;
}
 
Example #18
Source File: UsbControlHelper.java    From USBIPServerForAndroid with GNU General Public License v3.0 5 votes vote down vote up
public static boolean clearHaltCondition(UsbDeviceConnection devConn, UsbEndpoint endpoint) {
	int res = XferUtils.doControlTransfer(devConn, CLEAR_FEATURE_REQUEST_TYPE,
			CLEAR_FEATURE_REQUEST,
			FEATURE_VALUE_HALT,
			endpoint.getAddress(),
			null, 0, 0);
	if (res < 0) {
		return false;
	}
	
	return true;
}
 
Example #19
Source File: FtdiSerialDriver.java    From Arduino-Serial-Controller with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public int write(byte[] src, int timeoutMillis) throws IOException {
    final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1);
    int offset = 0;

    while (offset < src.length) {
        final int writeLength;
        final int amtWritten;

        synchronized (mWriteBufferLock) {
            final byte[] writeBuffer;

            writeLength = Math.min(src.length - offset, mWriteBuffer.length);
            if (offset == 0) {
                writeBuffer = src;
            } else {
                // bulkTransfer does not support offsets, make a copy.
                System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
                writeBuffer = mWriteBuffer;
            }

            amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength,
                    timeoutMillis);
        }

        if (amtWritten <= 0) {
            throw new IOException("Error writing " + writeLength
                    + " bytes at offset " + offset + " length=" + src.length);
        }

        Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength);
        offset += amtWritten;
    }
    return offset;
}
 
Example #20
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 #21
Source File: FtdiSerialDriver.java    From PodEmu with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int write(byte[] src, int timeoutMillis) throws IOException {
    final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1);
    int offset = 0;

    while (offset < src.length) {
        final int writeLength;
        final int amtWritten;

        synchronized (mWriteBufferLock) {
            final byte[] writeBuffer;

            writeLength = Math.min(src.length - offset, mWriteBuffer.length);
            if (offset == 0) {
                writeBuffer = src;
            } else {
                // bulkTransfer does not support offsets, make a copy.
                System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
                writeBuffer = mWriteBuffer;
            }

            amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength,
                    timeoutMillis);
        }

        if (amtWritten <= 0) {
            throw new IOException("Error writing " + writeLength
                    + " bytes at offset " + offset + " duration=" + src.length);
        }

        Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength);
        offset += amtWritten;
    }
    return offset;
}
 
Example #22
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 #23
Source File: ChromeUsbInterface.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@CalledByNative
private UsbEndpoint[] getEndpoints() {
    int count = mInterface.getEndpointCount();
    UsbEndpoint[] endpoints = new UsbEndpoint[count];
    for (int i = 0; i < count; ++i) {
        endpoints[i] = mInterface.getEndpoint(i);
    }
    return endpoints;
}
 
Example #24
Source File: FtdiSerialDriver.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int write(byte[] src, int timeoutMillis) throws IOException {
    final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1);
    int offset = 0;

    while (offset < src.length) {
        final int writeLength;
        final int amtWritten;

        synchronized (mWriteBufferLock) {
            final byte[] writeBuffer;

            writeLength = Math.min(src.length - offset, mWriteBuffer.length);
            if (offset == 0) {
                writeBuffer = src;
            } else {
                // bulkTransfer does not support offsets, make a copy.
                System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
                writeBuffer = mWriteBuffer;
            }

            amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength,
                    timeoutMillis);
        }

        if (amtWritten <= 0) {
            throw new IOException("Error writing " + writeLength
                    + " bytes at offset " + offset + " length=" + src.length);
        }

        Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength);
        offset += amtWritten;
    }
    return offset;
}
 
Example #25
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 #26
Source File: BTChipTransportAndroidWinUSB.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public BTChipTransportAndroidWinUSB(UsbDeviceConnection connection, UsbInterface dongleInterface, UsbEndpoint in, UsbEndpoint out, int timeout) {
	this.connection = connection;
	this.dongleInterface = dongleInterface;
	this.in = in;
	this.out = out;
	this.timeout = timeout;
	transferBuffer = new byte[260];
}
 
Example #27
Source File: BTChipTransportAndroidHID.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
public BTChipTransportAndroidHID(UsbDeviceConnection connection, UsbInterface dongleInterface, UsbEndpoint in, UsbEndpoint out, int timeout, boolean ledger) {
	this.connection = connection;
	this.dongleInterface = dongleInterface;
	this.in = in;
	this.out = out;
	this.ledger = ledger;
	// Compatibility with old prototypes, to be removed
	if (!this.ledger) {
		this.ledger = (in.getEndpointNumber() != out.getEndpointNumber());
	}
	this.timeout = timeout;
	transferBuffer = new byte[HID_BUFFER_SIZE];
}
 
Example #28
Source File: U2FTransportAndroidHID.java    From android-u2f-bridge with Apache License 2.0 5 votes vote down vote up
public U2FTransportAndroidHID(UsbDeviceConnection connection, UsbInterface dongleInterface, UsbEndpoint in, UsbEndpoint out, int timeout) {
   this.connection = connection;
   this.dongleInterface = dongleInterface;
   this.in = in;
   this.out = out;
   this.timeout = timeout;
   transferBuffer = new byte[HID_BUFFER_SIZE];
   helper = new U2FHelper();
   random = new Random();
}
 
Example #29
Source File: FtdiSerialDriver.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int write(byte[] src, int timeoutMillis) throws IOException {
    final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(1);
    int offset = 0;

    while (offset < src.length) {
        final int writeLength;
        final int amtWritten;

        synchronized (mWriteBufferLock) {
            final byte[] writeBuffer;

            writeLength = Math.min(src.length - offset, mWriteBuffer.length);
            if (offset == 0) {
                writeBuffer = src;
            } else {
                // bulkTransfer does not support offsets, make a copy.
                System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
                writeBuffer = mWriteBuffer;
            }

            amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength,
                    timeoutMillis);
        }

        if (amtWritten <= 0) {
            throw new IOException("Error writing " + writeLength
                    + " bytes at offset " + offset + " length=" + src.length);
        }

        Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength);
        offset += amtWritten;
    }
    return offset;
}
 
Example #30
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;
}