com.btchip.comm.LedgerHelper Java Examples

The following examples show how to use com.btchip.comm.LedgerHelper. 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: 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;
}