Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
felHR85 committed Mar 9, 2019
2 parents aad56b7 + da87575 commit 566c20f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
41 changes: 41 additions & 0 deletions usbserial/src/main/java/com/felhr/usbserial/FTDISerialDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;

import android.annotation.SuppressLint;
import android.hardware.usb.UsbConstants;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
Expand Down Expand Up @@ -626,6 +627,10 @@ public int syncRead(byte[] buffer, int timeout)
return 0;
}

if (mr1Version) {
return readSyncJelly(buffer, timeout, stopTime);
}

int n = buffer.length / 62;
if(buffer.length % 62 != 0)
{
Expand Down Expand Up @@ -666,4 +671,40 @@ public int syncRead(byte[] buffer, int timeout)

return readen;
}

private static final byte[] skip = new byte[2];

/**
* This method avoids creation of garbage by reusing the same
* array instance for skipping header bytes and running
* {@link UsbDeviceConnection#bulkTransfer(UsbEndpoint, byte[], int, int, int)}
* directly.
*/
@SuppressLint("NewApi")
private int readSyncJelly(byte[] buffer, int timeout, long stopTime) {
int read = 0;
do
{
int timeLeft = 0;
if(timeout > 0)
{
timeLeft = (int) (stopTime - System.currentTimeMillis());
if (timeLeft <= 0)
{
break;
}
}


int numberBytes = connection.bulkTransfer(inEndpoint, skip, skip.length, timeLeft);

if(numberBytes > 2) // Data received
{
numberBytes = connection.bulkTransfer(inEndpoint, buffer, read, 62, timeLeft);
read += numberBytes;
}
} while(read <= 0);

return read;
}
}
16 changes: 4 additions & 12 deletions usbserial/src/main/java/com/felhr/usbserial/UsbSerialDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public abstract class UsbSerialDevice implements UsbSerialInterface

protected static final String COM_PORT = "COM ";

private static final boolean mr1Version;
// Android version < 4.3 It is not going to be asynchronous read operations
static final boolean mr1Version =
android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
protected final UsbDevice device;
protected final UsbDeviceConnection connection;

Expand All @@ -47,15 +49,6 @@ public abstract class UsbSerialDevice implements UsbSerialInterface
private String portName = "";
protected boolean isOpen;

// Get Android version if version < 4.3 It is not going to be asynchronous read operations
static
{
if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
mr1Version = true;
else
mr1Version = false;
}

public UsbSerialDevice(UsbDevice device, UsbDeviceConnection connection)
{
this.device = device;
Expand Down Expand Up @@ -455,14 +448,13 @@ protected void setSyncParams(UsbEndpoint inEndpoint, UsbEndpoint outEndpoint)

protected void setThreadsParams(UsbRequest request, UsbEndpoint endpoint)
{
writeThread.setUsbEndpoint(endpoint);
if(mr1Version)
{
workerThread.setUsbRequest(request);
writeThread.setUsbEndpoint(endpoint);
}else
{
readThread.setUsbEndpoint(request.getEndpoint());
writeThread.setUsbEndpoint(endpoint);
}
}

Expand Down

0 comments on commit 566c20f

Please sign in to comment.