UsbSerialPort: add purgeHwBuffers method.

Consolidates following changes from Felix (newest first):
  1123807 Rename flushHwBuffers to purgeHwBuffers
  3eb145d Use UsbSerialPort instead of UsbSerialDriver in SerialInputOutputManager
  f91a974 Return true in flushHwBuffers default implementation if there is nothing to flush
  69c0b59 Implement flushHwBuffers for Cp2102 driver
  4a41bd9 Rename UsbSerialPort.flush function to flushHwBuffers
  c908da4 Refactoring: Make ProlificSerialDriver a subclass of CdcAcmSerialDriver
  39cb480 Refactoring: New UsbSerialPort interface
  d542f64 Refactoring: Do not require permission to USB device when probing
  9a13571 Support flushing non-written / non-read data
pull/39/head
Felix Hädicke 2013-05-26 18:55:49 +02:00 zatwierdzone przez mike wakerly
rodzic 2bdcbfd16e
commit 730ed711e1
5 zmienionych plików z 62 dodań i 5 usunięć

Wyświetl plik

@ -134,4 +134,9 @@ abstract class CommonUsbSerialDriver implements UsbSerialDriver {
@Override @Override
public abstract void setRTS(boolean value) throws IOException; public abstract void setRTS(boolean value) throws IOException;
@Override
public boolean purgeHwBuffers(boolean flushReadBuffers, boolean flushWriteBuffers) throws IOException {
return !flushReadBuffers && !flushWriteBuffers;
}
} }

Wyświetl plik

@ -12,7 +12,6 @@ import android.hardware.usb.UsbInterface;
import android.util.Log; import android.util.Log;
public class Cp2102SerialDriver extends CommonUsbSerialDriver { public class Cp2102SerialDriver extends CommonUsbSerialDriver {
private static final String TAG = Cp2102SerialDriver.class.getSimpleName(); private static final String TAG = Cp2102SerialDriver.class.getSimpleName();
private static final int DEFAULT_BAUD_RATE = 9600; private static final int DEFAULT_BAUD_RATE = 9600;
@ -32,6 +31,10 @@ public class Cp2102SerialDriver extends CommonUsbSerialDriver {
private static final int SILABSER_SET_LINE_CTL_REQUEST_CODE = 0x03; private static final int SILABSER_SET_LINE_CTL_REQUEST_CODE = 0x03;
private static final int SILABSER_SET_MHS_REQUEST_CODE = 0x07; private static final int SILABSER_SET_MHS_REQUEST_CODE = 0x07;
private static final int SILABSER_SET_BAUDRATE = 0x1E; private static final int SILABSER_SET_BAUDRATE = 0x1E;
private static final int SILABSER_FLUSH_REQUEST_CODE = 0x12;
private static final int FLUSH_READ_CODE = 0x0a;
private static final int FLUSH_WRITE_CODE = 0x05;
/* /*
* SILABSER_IFC_ENABLE_REQUEST_CODE * SILABSER_IFC_ENABLE_REQUEST_CODE
@ -258,6 +261,19 @@ public class Cp2102SerialDriver extends CommonUsbSerialDriver {
return true; return true;
} }
@Override
public boolean purgeHwBuffers(boolean purgeReadBuffers,
boolean purgeWriteBuffers) throws IOException {
int value = (purgeReadBuffers ? FLUSH_READ_CODE : 0)
| (purgeWriteBuffers ? FLUSH_WRITE_CODE : 0);
if (value != 0) {
setConfigSingle(SILABSER_FLUSH_REQUEST_CODE, value);
}
return true;
}
@Override @Override
public void setRTS(boolean value) throws IOException { public void setRTS(boolean value) throws IOException {
} }

Wyświetl plik

@ -132,6 +132,8 @@ public class FtdiSerialDriver extends CommonUsbSerialDriver {
private static final int SIO_SET_DATA_REQUEST = 4; private static final int SIO_SET_DATA_REQUEST = 4;
private static final int SIO_RESET_SIO = 0; private static final int SIO_RESET_SIO = 0;
private static final int SIO_RESET_PURGE_RX = 1;
private static final int SIO_RESET_PURGE_TX = 2;
public static final int FTDI_DEVICE_OUT_REQTYPE = public static final int FTDI_DEVICE_OUT_REQTYPE =
UsbConstants.USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT; UsbConstants.USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT;
@ -512,6 +514,27 @@ public class FtdiSerialDriver extends CommonUsbSerialDriver {
public void setRTS(boolean value) throws IOException { public void setRTS(boolean value) throws IOException {
} }
@Override
public boolean purgeHwBuffers(boolean purgeReadBuffers, boolean purgeWriteBuffers) throws IOException {
if (purgeReadBuffers) {
int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE, SIO_RESET_REQUEST,
SIO_RESET_PURGE_RX, 0 /* index */, null, 0, USB_WRITE_TIMEOUT_MILLIS);
if (result != 0) {
throw new IOException("Flushing RX failed: result=" + result);
}
}
if (purgeWriteBuffers) {
int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE, SIO_RESET_REQUEST,
SIO_RESET_PURGE_TX, 0 /* index */, null, 0, USB_WRITE_TIMEOUT_MILLIS);
if (result != 0) {
throw new IOException("Flushing RX failed: result=" + result);
}
}
return true;
}
public static Map<Integer, int[]> getSupportedDevices() { public static Map<Integer, int[]> getSupportedDevices() {
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>(); final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>();
supportedDevices.put(Integer.valueOf(UsbId.VENDOR_FTDI), supportedDevices.put(Integer.valueOf(UsbId.VENDOR_FTDI),

Wyświetl plik

@ -158,7 +158,7 @@ public class ProlificSerialDriver extends CommonUsbSerialDriver {
} }
private void resetDevice() throws IOException { private void resetDevice() throws IOException {
flush(true, true); purgeHwBuffers(true, true);
} }
private void setControlLines(int newControlLinesValue) throws IOException { private void setControlLines(int newControlLinesValue) throws IOException {
@ -494,14 +494,17 @@ public class ProlificSerialDriver extends CommonUsbSerialDriver {
setControlLines(newControlLinesValue); setControlLines(newControlLinesValue);
} }
public void flush(boolean flushRX, boolean flushTX) throws IOException { @Override
if (flushRX) { public boolean purgeHwBuffers(boolean purgeReadBuffers, boolean purgeWriteBuffers) throws IOException {
if (purgeReadBuffers) {
vendorOut(FLUSH_RX_REQUEST, 0, null); vendorOut(FLUSH_RX_REQUEST, 0, null);
} }
if (flushTX) { if (purgeWriteBuffers) {
vendorOut(FLUSH_TX_REQUEST, 0, null); vendorOut(FLUSH_TX_REQUEST, 0, null);
} }
return true;
} }
public static Map<Integer, int[]> getSupportedDevices() { public static Map<Integer, int[]> getSupportedDevices() {

Wyświetl plik

@ -196,5 +196,15 @@ public interface UsbSerialDriver {
* @throws IOException if an error occurred during writing * @throws IOException if an error occurred during writing
*/ */
public void setRTS(boolean value) throws IOException; public void setRTS(boolean value) throws IOException;
/**
* Flush non-transmitted output data and / or non-read input data
* @param flushRX {@code true} to flush non-transmitted output data
* @param flushTX {@code true} to flush non-read input data
* @return {@code true} if the operation was successful, or
* {@code false} if the operation is not supported by the driver or device
* @throws IOException if an error occurred during flush
*/
public boolean flush(boolean flushRX, boolean flushTX) throws IOException;
} }