Skip to content

Commit

Permalink
Further fixes from Kai Morich's fork. Many thanks for your work Kai!
Browse files Browse the repository at this point in the history
  • Loading branch information
greblus committed Dec 8, 2018
1 parent cacd8d8 commit 9903248
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 36 deletions.
Expand Up @@ -36,6 +36,7 @@
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import android.os.Build;


/** /**
* A {@link CommonUsbSerialPort} implementation for a variety of FTDI devices * A {@link CommonUsbSerialPort} implementation for a variety of FTDI devices
Expand Down Expand Up @@ -152,8 +153,6 @@ public class FtdiSerialPort extends CommonUsbSerialPort {
* Set flow control register. * Set flow control register.
*/ */
private static final int SIO_SET_FLOW_CTRL_REQUEST = 2; 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. * Set baud rate.
Expand Down Expand Up @@ -194,10 +193,11 @@ public class FtdiSerialPort extends CommonUsbSerialPort {
* since it gives no indication of number of bytes read. Set this to * since it gives no indication of number of bytes read. Set this to
* {@code true} on platforms where it is fixed. * {@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) { public FtdiSerialPort(UsbDevice device, int portNumber) {
super(device, portNumber); super(device, portNumber);
mEnableAsyncReads = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1);
} }


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


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

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

final ByteBuffer buf = ByteBuffer.wrap(dest); final ByteBuffer buf = ByteBuffer.wrap(dest);
if (!request.queue(buf, readAmt)) { try {
throw new IOException("Error queueing request."); request.initialize(mConnection, endpoint);
} if (!request.queue(buf, dest.length)) {
throw new IOException("Error queueing request.");
}


final UsbRequest response = mConnection.requestWait(); final UsbRequest response = mConnection.requestWait();
if (response == null) { if (response == null) {
throw new IOException("Null response"); throw new IOException("Null response");
}
} finally {
request.close();
} }


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

} else { } else {
final int totalBytesRead; final int totalBytesRead;


Expand Down Expand Up @@ -452,14 +448,6 @@ public void setParameters(int baudRate, int dataBits, int stopBits, int parity)
if (result != 0) { if (result != 0) {
throw new IOException("Setting parameters failed: result=" + result); 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) { private long[] convertBaudrate(int baudrate) {
Expand Down
Expand Up @@ -63,9 +63,6 @@ public final class UsbId {
public static final int SILABS_CP2108 = 0xea71; public static final int SILABS_CP2108 = 0xea71;
public static final int SILABS_CP2110 = 0xea80; public static final int SILABS_CP2110 = 0xea80;


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

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


Expand Down

0 comments on commit 9903248

Please sign in to comment.