Permalink
Browse files

Further fixes from Kai Morich's fork. Many thanks for your work Kai!

  • Loading branch information...
greblus committed Dec 8, 2018
1 parent cacd8d8 commit 990324830fd7fba69c3bd94447053f3d6e21b74d
@@ -36,6 +36,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import android.os.Build;

/**
* A {@link CommonUsbSerialPort} implementation for a variety of FTDI devices
@@ -152,8 +153,6 @@ public UsbDevice getDevice() {
* Set flow control register.
*/
private static final int SIO_SET_FLOW_CTRL_REQUEST = 2;
private static final int SIO_SET_FLOW_CTRL_REQUEST_TYPE = 0x40;
private static final int SIO_DISABLE_FLOW_CTRL = 0;

/**
* Set baud rate.
@@ -194,10 +193,11 @@ public UsbDevice getDevice() {
* since it gives no indication of number of bytes read. Set this to
* {@code true} on platforms where it is fixed.
*/
private static final boolean ENABLE_ASYNC_READS = false;
private final boolean mEnableAsyncReads;

public FtdiSerialPort(UsbDevice device, int portNumber) {
super(device, portNumber);
mEnableAsyncReads = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1);
}

@Override
@@ -296,33 +296,29 @@ public void close() throws IOException {
public int sread(byte[] dest, int size, int timeoutMillis) throws IOException {
final UsbEndpoint endpoint = mDevice.getInterface(0).getEndpoint(0);

if (ENABLE_ASYNC_READS) {
final int readAmt;
synchronized (mReadBufferLock) {
// mReadBuffer is only used for maximum read size.
readAmt = Math.min(size, mReadBuffer.length);
}

if (mEnableAsyncReads) {
final UsbRequest request = new UsbRequest();
request.initialize(mConnection, endpoint);

final ByteBuffer buf = ByteBuffer.wrap(dest);
if (!request.queue(buf, readAmt)) {
throw new IOException("Error queueing request.");
}
try {
request.initialize(mConnection, endpoint);
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 UsbRequest response = mConnection.requestWait();
if (response == null) {
throw new IOException("Null response");
}
} finally {
request.close();
}

final int payloadBytesRead = buf.position() - MODEM_STATUS_HEADER_LENGTH;
if (payloadBytesRead > 0) {
Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, size)));
return payloadBytesRead;
} else {
return 0;
final int totalBytesRead = buf.position();
if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH) {
throw new IOException("Expected at least " + MODEM_STATUS_HEADER_LENGTH + " bytes");
}
return filterStatusBytes(dest, dest, totalBytesRead, endpoint.getMaxPacketSize());

} else {
final int totalBytesRead;

@@ -452,14 +448,6 @@ public void setParameters(int baudRate, int dataBits, int stopBits, int parity)
if (result != 0) {
throw new IOException("Setting parameters failed: result=" + result);
}
//disable flow control, needed for FT231X
result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE,
SIO_SET_FLOW_CTRL_REQUEST, SIO_DISABLE_FLOW_CTRL, 0 /* index */,
null, 0, USB_WRITE_TIMEOUT_MILLIS);
if (result != 0) {
throw new IOException("Cannot disable flow control: result=" + result);
}

}

private long[] convertBaudrate(int baudrate) {
@@ -63,9 +63,6 @@
public static final int SILABS_CP2108 = 0xea71;
public static final int SILABS_CP2110 = 0xea80;

public static final int VENDOR_PROLIFIC = 0x067b;
public static final int PROLIFIC_PL2303 = 0x2303;

private UsbId() {
throw new IllegalAccessError("Non-instantiable class.");
}
@@ -48,7 +48,6 @@ public static UsbSerialProber getDefaultProber() {
public static ProbeTable getDefaultProbeTable() {
final ProbeTable probeTable = new ProbeTable();
probeTable.addDriver(FtdiSerialDriver.class);
probeTable.addDriver(ProlificSerialDriver.class);
return probeTable;
}

0 comments on commit 9903248

Please sign in to comment.