Java Code Examples for android.hardware.usb.UsbRequest#initialize()

The following examples show how to use android.hardware.usb.UsbRequest#initialize() . 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: CommonUsbSerialPort.java    From usb-serial-for-android with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void open(UsbDeviceConnection connection) throws IOException {
    if (mConnection != null) {
        throw new IOException("Already open");
    }
    mConnection = connection;
    try {
        openInt(connection);
        if (mReadEndpoint == null || mWriteEndpoint == null) {
            throw new IOException("Could not get read & write endpoints");
        }
        mUsbRequest = new UsbRequest();
        mUsbRequest.initialize(mConnection, mReadEndpoint);
    } catch(Exception e) {
        close();
        throw e;
    }
}
 
Example 2
Source File: CP2102SerialDevice.java    From UsbSerial with MIT License 5 votes vote down vote up
@Override
public boolean open()
{
    boolean ret = openCP2102();

    if(ret)
    {
        // Initialize UsbRequest
        UsbRequest requestIN = new SafeUsbRequest();
        requestIN.initialize(connection, inEndpoint);

        // Restart the working thread if it has been killed before and  get and claim interface
        restartWorkingThread();
        restartWriteThread();

        // Create Flow control thread but it will only be started if necessary
        createFlowControlThread();

        // Pass references to the threads
        setThreadsParams(requestIN, outEndpoint);

        asyncMode = true;
        isOpen = true;

        return true;
    }else
    {
        isOpen = false;
        return false;
    }
}
 
Example 3
Source File: CH34xSerialDevice.java    From UsbSerial with MIT License 5 votes vote down vote up
@Override
public boolean open()
{
    boolean ret = openCH34X();
    if(ret)
    {
        // Initialize UsbRequest
        UsbRequest requestIN = new SafeUsbRequest();
        requestIN.initialize(connection, inEndpoint);

        // Restart the working thread if it has been killed before and  get and claim interface
        restartWorkingThread();
        restartWriteThread();

        // Create Flow control thread but it will only be started if necessary
        createFlowControlThread();

        // Pass references to the threads
        setThreadsParams(requestIN, outEndpoint);

        asyncMode = true;
        isOpen = true;

        return true;
    }else
    {
        isOpen = false;
        return false;
    }
}
 
Example 4
Source File: FTDISerialDevice.java    From UsbSerial with MIT License 5 votes vote down vote up
@Override
public boolean open()
{
    boolean ret = openFTDI();

    if(ret)
    {
        // Initialize UsbRequest
        UsbRequest requestIN = new SafeUsbRequest();
        requestIN.initialize(connection, inEndpoint);

        // Restart the working thread if it has been killed before and  get and claim interface
        restartWorkingThread();
        restartWriteThread();

        // Pass references to the threads
        setThreadsParams(requestIN, outEndpoint);

        asyncMode = true;
        isOpen = true;

        return true;
    }else
    {
        isOpen = false;
        return false;
    }
}
 
Example 5
Source File: BTChipTransportAndroidHID.java    From xmrwallet with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] exchange(byte[] command) {
    ByteArrayOutputStream response = new ByteArrayOutputStream();
    byte[] responseData = null;
    int offset = 0;
    if (debug) {
        Timber.d("=> %s", Dump.dump(command));
    }
    command = LedgerHelper.wrapCommandAPDU(LEDGER_DEFAULT_CHANNEL, command, HID_BUFFER_SIZE);
    UsbRequest requestOut = new UsbRequest();
    requestOut.initialize(connection, out);
    while (offset != command.length) {
        int blockSize = (command.length - offset > HID_BUFFER_SIZE ? HID_BUFFER_SIZE : command.length - offset);
        System.arraycopy(command, offset, transferBuffer, 0, blockSize);
        requestOut.queue(ByteBuffer.wrap(transferBuffer), HID_BUFFER_SIZE);
        connection.requestWait();
        offset += blockSize;
    }
    requestOut.close();
    ByteBuffer responseBuffer = ByteBuffer.allocate(HID_BUFFER_SIZE);
    UsbRequest requestIn = new UsbRequest();
    requestIn.initialize(connection, in);
    while ((responseData = LedgerHelper.unwrapResponseAPDU(LEDGER_DEFAULT_CHANNEL, response.toByteArray(), HID_BUFFER_SIZE)) == null) {
        responseBuffer.clear();
        requestIn.queue(responseBuffer, HID_BUFFER_SIZE);
        connection.requestWait();
        responseBuffer.rewind();
        responseBuffer.get(transferBuffer, 0, HID_BUFFER_SIZE);
        response.write(transferBuffer, 0, HID_BUFFER_SIZE);
    }
    requestIn.close();
    if (debug) {
        Timber.d("<= %s", Dump.dump(responseData));
    }
    return responseData;
}
 
Example 6
Source File: Trezor.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void messageWrite(final Message msg) {
    final int msg_size = msg.getSerializedSize();
    final String msg_name = msg.getClass().getSimpleName();
    final int msg_id = MessageType.valueOf("MessageType_" + msg_name).getNumber();
    Log.d(TAG, String.format("Got message: %s (%d bytes)", msg_name, msg_size));
    final ByteBuffer data = ByteBuffer.allocate(32768);
    data.put((byte)'#');
    data.put((byte)'#');
    data.put((byte)((msg_id >> 8) & 0xFF));
    data.put((byte)(msg_id & 0xFF));
    data.put((byte)((msg_size >> 24) & 0xFF));
    data.put((byte)((msg_size >> 16) & 0xFF));
    data.put((byte)((msg_size >> 8) & 0xFF));
    data.put((byte)(msg_size & 0xFF));
    data.put(msg.toByteArray());
    while (data.position() % 63 > 0)
        data.put((byte)0);
    final UsbRequest request = new UsbRequest();
    request.initialize(mConn, mWriteEndpoint);
    final int chunks = data.position() / 63;
    Log.d(TAG, String.format("Writing %d chunks", chunks));
    data.rewind();
    for (int i = 0; i < chunks; ++i) {
        final byte[] buffer = new byte[64];
        buffer[0] = (byte)'?';
        data.get(buffer, 1, 63);
        //logData("chunk:", buffer);
        request.queue(ByteBuffer.wrap(buffer), 64);
        mConn.requestWait();
    }
    request.close();
}
 
Example 7
Source File: CdcAcmSerialDriver.java    From OkUSB with Apache License 2.0 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis) throws IOException {
    if (mEnableAsyncReads) {
      final UsbRequest request = new UsbRequest();
      try {
        request.initialize(mConnection, mReadEndpoint);
        final ByteBuffer buf = ByteBuffer.wrap(dest);
        if (!request.queue(buf, dest.length)) {
          throw new IOException("Error queueing request.");
        }

        final UsbRequest response = mConnection.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 = mConnection.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 8
Source File: CdcAcmSerialDriver.java    From usb-with-serial-port with Apache License 2.0 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis) throws IOException {
    if (mEnableAsyncReads) {
        final UsbRequest request = new UsbRequest();
        try {
            request.initialize(mConnection, mReadEndpoint);
            final ByteBuffer buf = ByteBuffer.wrap(dest);
            if (!request.queue(buf, dest.length)) {
                throw new IOException("Error queueing request.");
            }

            final UsbRequest response = mConnection.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 = mConnection.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: 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 10
Source File: CdcAcmSerialDriver.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis) throws IOException {
    if (mEnableAsyncReads) {
      final UsbRequest request = new UsbRequest();
      try {
        request.initialize(mConnection, mReadEndpoint);
        final ByteBuffer buf = ByteBuffer.wrap(dest);
        if (!request.queue(buf, dest.length)) {
          throw new IOException("Error queueing request.");
        }

        final UsbRequest response = mConnection.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 = mConnection.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 11
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 12
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 13
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 14
Source File: CdcAcmSerialDriver.java    From Arduino-Serial-Controller with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis) throws IOException {
    if (mEnableAsyncReads) {
      final UsbRequest request = new UsbRequest();
      try {
        request.initialize(mConnection, mReadEndpoint);
        final ByteBuffer buf = ByteBuffer.wrap(dest);
        if (!request.queue(buf, dest.length)) {
          throw new IOException("Error queueing request.");
        }

        final UsbRequest response = mConnection.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 = mConnection.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 15
Source File: CdcAcmSerialDriver.java    From xDrip with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis) throws IOException {
    if (mEnableAsyncReads) {
      final UsbRequest request = new UsbRequest();
      try {
        request.initialize(mConnection, mReadEndpoint);
        final ByteBuffer buf = ByteBuffer.wrap(dest);
        if (!request.queue(buf, dest.length)) {
          throw new IOException("Error queueing request.");
        }

        final UsbRequest response = mConnection.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 = mConnection.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 16
Source File: U2FTransportAndroidHID.java    From android-u2f-bridge with Apache License 2.0 4 votes vote down vote up
public byte[] exchange(byte tag, byte[] command) throws IOException {
   ByteArrayOutputStream response = new ByteArrayOutputStream();
   byte[] responseData = null;
   int offset = 0;
   int responseSize;
   if (debug) {
      Log.d(LOG_TAG, "=> " + Dump.dump(command));
   }
   command = helper.wrapCommandAPDU(tag, command, HID_BUFFER_SIZE);      
   UsbRequest requestWrite = new UsbRequest();
   if (!requestWrite.initialize(connection, out)) {
      throw new IOException();
   }
   while (offset != command.length) {
      int blockSize = (command.length - offset > HID_BUFFER_SIZE ? HID_BUFFER_SIZE : command.length - offset);
      System.arraycopy(command, offset, transferBuffer, 0, blockSize);
      if (debug) {
         Log.d(LOG_TAG, "wire => " + Dump.dump(transferBuffer));
      }
      if (!requestWrite.queue(ByteBuffer.wrap(transferBuffer), HID_BUFFER_SIZE)) {
         requestWrite.close();
         throw new IOException();
      }
      connection.requestWait();
      offset += blockSize;
   }
   ByteBuffer responseBuffer = ByteBuffer.allocate(HID_BUFFER_SIZE);
   UsbRequest requestRead = new UsbRequest();
   if (!requestRead.initialize(connection, in)) {
      requestRead.close();
      requestWrite.close();
      throw new IOException();
   }
   while ((responseData = helper.unwrapResponseAPDU(tag, response.toByteArray(), HID_BUFFER_SIZE)) == null) {
      responseBuffer.clear();
      if (!requestRead.queue(responseBuffer, HID_BUFFER_SIZE)) {
         requestRead.close();
         requestWrite.close();
         throw new IOException();
      }
      connection.requestWait();
      responseBuffer.rewind();
      responseBuffer.get(transferBuffer, 0, HID_BUFFER_SIZE);
      if (debug) {
         Log.d(LOG_TAG, "wire <= " + Dump.dump(transferBuffer));
      }
      response.write(transferBuffer, 0, HID_BUFFER_SIZE);
   }
   if (debug) {
      Log.d(LOG_TAG, "<= " + Dump.dump(responseData));
   }

   requestWrite.close();
   requestRead.close();
   return responseData;
}
 
Example 17
Source File: CdcAcmSerialDriver.java    From PodEmu with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis) throws IOException {
    if (mEnableAsyncReads) {
      final UsbRequest request = new UsbRequest();
      try {
        request.initialize(mConnection, mReadEndpoint);
        final ByteBuffer buf = ByteBuffer.wrap(dest);
        if (!request.queue(buf, dest.length)) {
          throw new IOException("Error queueing request.");
        }

        final UsbRequest response = mConnection.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.duration)));
          return nread;
        } else {
          return 0;
        }
      } finally {
        request.close();
      }
    }

    final int numBytesRead;
    synchronized (mReadBufferLock) {
        int readAmt = Math.min(dest.length, mReadBuffer.length);
        numBytesRead = mConnection.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 18
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 19
Source File: CdcAcmSerialDriver.java    From Chorus-RF-Laptimer with MIT License 4 votes vote down vote up
@Override
public int read(byte[] dest, int timeoutMillis) throws IOException {
    if (mEnableAsyncReads) {
      final UsbRequest request = new UsbRequest();
      try {
        request.initialize(mConnection, mReadEndpoint);
        final ByteBuffer buf = ByteBuffer.wrap(dest);
        if (!request.queue(buf, dest.length)) {
          throw new IOException("Error queueing request.");
        }

        final UsbRequest response = mConnection.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 = mConnection.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 20
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;
}