android.hardware.usb.UsbRequest#initialize ( )源码实例Demo

下面列出了android.hardware.usb.UsbRequest#initialize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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;
    }
}
 
源代码2 项目: green_android   文件: Trezor.java
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();
}
 
源代码3 项目: UsbSerial   文件: CP2102SerialDevice.java
@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;
    }
}
 
源代码4 项目: xmrwallet   文件: BTChipTransportAndroidHID.java
@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;
}
 
源代码5 项目: UsbSerial   文件: FTDISerialDevice.java
@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;
    }
}
 
源代码6 项目: UsbSerial   文件: CH34xSerialDevice.java
@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;
    }
}
 
源代码7 项目: Chorus-RF-Laptimer   文件: CdcAcmSerialDriver.java
@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;
}
 
源代码8 项目: OkUSB   文件: CdcAcmSerialDriver.java
@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;
}
 
源代码9 项目: usb-with-serial-port   文件: CdcAcmSerialDriver.java
@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;
}
 
源代码10 项目: xDrip   文件: FtdiSerialDriver.java
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;
}
 
源代码11 项目: xDrip   文件: CdcAcmSerialDriver.java
@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;
}
 
源代码12 项目: xDrip   文件: Cp21xxSerialDriver.java
@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;
}
 
源代码13 项目: xDrip   文件: ProlificSerialDriver.java
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;
}
 
源代码14 项目: xDrip-plus   文件: FtdiSerialDriver.java
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;
}
 
@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;
}
 
源代码16 项目: xDrip   文件: CdcAcmSerialDriver.java
@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;
}
 
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;
}
 
源代码18 项目: PodEmu   文件: CdcAcmSerialDriver.java
@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;
}
 
源代码19 项目: xDrip-Experimental   文件: FtdiSerialDriver.java
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;
}
 
源代码20 项目: xDrip   文件: ProlificSerialDriver.java
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;
}