Java Code Examples for android.hardware.usb.UsbDeviceConnection#bulkTransfer()

The following examples show how to use android.hardware.usb.UsbDeviceConnection#bulkTransfer() . 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: XferUtils.java    From USBIPServerForAndroid with GNU General Public License v3.0 5 votes vote down vote up
public static int doInterruptTransfer(UsbDeviceConnection devConn, UsbEndpoint endpoint, byte[] buff, int timeout) {
	// Interrupt transfers are implemented as one-shot bulk transfers
	int res = devConn.bulkTransfer(endpoint, buff,
			buff.length, timeout);
	if (res < 0) {
		res = -Errno.getErrno();
		if (res != -110) { 
			// Don't print for ETIMEDOUT
			System.err.println("Interrupt Xfer failed: "+res);
		}
	}
	
	return res;
}
 
Example 2
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 3
Source File: FtdiSerialDriver.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, null);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(null, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 4
Source File: FtdiSerialDriver.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, null);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(null, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 5
Source File: Cp21xxSerialDriver.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, mReadEndpoint);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(mReadEndpoint, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 6
Source File: ProlificSerialDriver.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, mReadEndpoint);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(mReadEndpoint, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 7
Source File: FtdiSerialDriver.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, null);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(null, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 8
Source File: Cp21xxSerialDriver.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, mReadEndpoint);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(mReadEndpoint, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 9
Source File: ProlificSerialDriver.java    From xDrip-plus with GNU General Public License v3.0 4 votes vote down vote up
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, mReadEndpoint);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(mReadEndpoint, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 10
Source File: FtdiSerialDriver.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, null);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(null, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 11
Source File: Cp21xxSerialDriver.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, mReadEndpoint);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(mReadEndpoint, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 12
Source File: ProlificSerialDriver.java    From xDrip-Experimental with GNU General Public License v3.0 4 votes vote down vote up
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, mReadEndpoint);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(mReadEndpoint, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 13
Source File: Cp21xxSerialDriver.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, mReadEndpoint);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(mReadEndpoint, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}
 
Example 14
Source File: ProlificSerialDriver.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
public int read(byte[] dest, int timeoutMillis, UsbDeviceConnection connection) throws IOException {
    if (false) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(connection, mReadEndpoint);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = connection.requestWait();
            if (response == null) {
                throw new IOException("Null response");
            }

            final int nread = buf.position();
            if (nread > 0) {
                //Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
                return nread;
            } else {
                return 0;
            }
        } finally {
            request.close();
        }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = connection.bulkTransfer(mReadEndpoint, mReadBuffer, readAmt,
                timeoutMillis);
        if (numBytesRead < 0) {
            // This sucks: we get -1 on timeout, not 0 as preferred.
            // We *should* use UsbRequest, except it has a bug/api oversight
            // where there is no way to determine the number of bytes read
            // in response :\ -- http://b.android.com/28023
            if (timeoutMillis == Integer.MAX_VALUE) {
                // Hack: Special case "~infinite timeout" as an error.
                return -1;
            }
            return 0;
        }
        System.arraycopy(mReadBuffer, 0, dest, 0, numBytesRead);
    }
    return numBytesRead;
}