diff --git a/serial-android/src/main/java/com/smartbotkit/serial/android/CDCSerialPort.java b/serial-android/src/main/java/com/smartbotkit/serial/android/CDCSerialPort.java index b0fbbec..76e78e5 100644 --- a/serial-android/src/main/java/com/smartbotkit/serial/android/CDCSerialPort.java +++ b/serial-android/src/main/java/com/smartbotkit/serial/android/CDCSerialPort.java @@ -16,7 +16,7 @@ import java.io.IOException; * A serial port implementation for chips following the CDC ACM (communication device class, abstract control model). * The documentation can be found here. * - * This chipset is used by the OpenCR board of the turtlebot 3. + * This chipset is used by the OpenCR board of the TurtleBot 3. * * @author Marcus */ diff --git a/serial-android/src/main/java/com/smartbotkit/serial/android/CP210XSerialPort.java b/serial-android/src/main/java/com/smartbotkit/serial/android/CP210XSerialPort.java index 39c2d90..2a19b20 100644 --- a/serial-android/src/main/java/com/smartbotkit/serial/android/CP210XSerialPort.java +++ b/serial-android/src/main/java/com/smartbotkit/serial/android/CP210XSerialPort.java @@ -76,7 +76,6 @@ public class CP210XSerialPort extends AbstractSerialPort { */ private int readLength = 0; - /** * Creates a new serial port with the specified config, manager and device. * @@ -197,11 +196,7 @@ public class CP210XSerialPort extends AbstractSerialPort { (byte) 0x00, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x00 }; - // work-around for RPLidar, don't set the flow control if it is off - // TODO: check Linux driver to figure out how this should be done properly - // see https://github.com/torvalds/linux/blob/master/drivers/usb/serial/cp210x.c - //return sendControlTransfer(SET_FLOW, 0, dataOff); - return 0; + return sendControlTransfer(SET_FLOW, 0, dataOff); case DSR_DTR: byte[] dataDSRDTR = new byte[]{ (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x00, @@ -401,7 +396,7 @@ public class CP210XSerialPort extends AbstractSerialPort { public int read(byte[] buffer, int offset, int length) throws IOException { // perform an indirect read using the read buffer to avoid read attempts // by the application with small length values that will result in read errors - if (readLength <= 0) { + if (readLength <= 0) { int read = connection.bulkTransfer(in, readBuffer, 0, readBuffer.length, 0); readLength = read; readOffset = 0; diff --git a/serial-android/src/main/java/com/smartbotkit/serial/android/FTDISerialPort.java b/serial-android/src/main/java/com/smartbotkit/serial/android/FTDISerialPort.java index 051ed6d..c79a47d 100644 --- a/serial-android/src/main/java/com/smartbotkit/serial/android/FTDISerialPort.java +++ b/serial-android/src/main/java/com/smartbotkit/serial/android/FTDISerialPort.java @@ -61,12 +61,25 @@ public class FTDISerialPort extends AbstractSerialPort { private static final int INTERFACE_OFFSET_DSR_DTR = 3; private static final int INTERFACE_OFFSET_XON_XOFF = 5; - /** - * The buffer to read data with the length initialized - * to maximum + 2 bytes for the status. + * The size of packets including the 2-byte prefix. */ - protected byte[] readBuffer = new byte[4096 + 2]; + private static final int PACKET_SIZE = 64; + + /** + * The payload per packet. + */ + private static final int PACKET_PAYLOAD = PACKET_SIZE - 2; + + /** + * The maximum number of packets. + */ + private static final int PACKET_MAX = 50; + + /** + * The buffer to read data in 64 byte packets. + */ + protected byte[] readBuffer = new byte[PACKET_SIZE * PACKET_MAX]; /** * Creates a new serial port with the specified config, manager and device. @@ -354,12 +367,25 @@ public class FTDISerialPort extends AbstractSerialPort { @Override public int read(byte[] buffer, int offset, int length) throws IOException { - int maximum = Math.min(length + 2, readBuffer.length); + if (length <= 0) return 0; // check that user wants to read something + int maximum = Math.min(length, PACKET_MAX * PACKET_PAYLOAD); // at most read buffer size - 2 per packet + int packets = maximum / PACKET_PAYLOAD; // number of full packets + int remainder = maximum - (packets * PACKET_PAYLOAD); // check for half full packet + if (remainder > 0) packets += 1; // there is one + maximum += (PACKET_SIZE - PACKET_PAYLOAD) * packets; // add 2 byte header for each packet int read = connection.bulkTransfer(in, readBuffer, maximum, 0); - if (read < 2) throw new IOException("Failed to read modem data."); - interpretModemData(readBuffer[0], readBuffer[1]); - if (read > 2) System.arraycopy(readBuffer, 2, buffer, offset, read - 2); - return read - 2; + int processed = 0; // number of bytes payload processed + int packetStart = 0; // start byte of next packet + while (read > 2) { + int dataLength = Math.min(read, PACKET_SIZE) - 2; // determine available payload bytes in packet + interpretModemData(readBuffer[packetStart], readBuffer[packetStart + 1]); // process modem data in packet + System.arraycopy(readBuffer, packetStart + 2, buffer, offset + processed, dataLength); // copy remainder of packet + processed += dataLength; // add bytes to number of processed + packetStart += PACKET_SIZE; // move to next packet + read -= dataLength + 2; // reduce available bytes by packet + } + if (read != 0) throw new IOException("Failed to read modem state (" + read + " < 2)."); + return processed; } /** diff --git a/serial-android/src/main/java/com/smartbotkit/serial/android/SlamtecSerialPort.java b/serial-android/src/main/java/com/smartbotkit/serial/android/SlamtecSerialPort.java new file mode 100644 index 0000000..a2732d6 --- /dev/null +++ b/serial-android/src/main/java/com/smartbotkit/serial/android/SlamtecSerialPort.java @@ -0,0 +1,98 @@ +package com.smartbotkit.serial.android; + +import java.io.IOException; + +import com.smartbotkit.serial.FlowControl; +import com.smartbotkit.serial.RetrySerialPort; +import com.smartbotkit.serial.SerialConfig; + +import android.hardware.usb.UsbDevice; +import android.hardware.usb.UsbManager; + +/** + * This serial port works around the quirks of the cp2102 + * chipset on the slamtec usb board shipped with the rplidar. + * Currently, this class fixes two issues. First, + * + * @author Marcus + */ +public class SlamtecSerialPort extends RetrySerialPort { + + /** + * A helper class that ensures that the flow control + * settings are not transfered as this will cause the + * chip to hang. + */ + private static class SlamtecCP210XSerialPort extends CP210XSerialPort { + + /** + * Creates a new serial port with the specified config, manager and device. + * + * @param config The port configuration. + * @param manager The usb manager. + * @param device The device. + * @throws IOException Thrown if the port cannot be opened. + */ + public SlamtecCP210XSerialPort(UsbManager manager, UsbDevice device, SerialConfig config) throws IOException { + super(manager, device, config); + } + + @Override + protected int initFlowControl() throws IOException { + // Do not set the flow control for this chip. + // If this is configured, the board will turn on + // a red light and stop working. Here we just + // check if flow control is turned off as it should + // be for this device. + if (getConfig().getFlowControl() != FlowControl.NONE) + throw new IOException("Unsupported flow control configuration."); + return 0; + } + + } + + /** + * Creates a new serial port with the specified config, manager and device that + * uses the default exponential back-off values of the underlying retry port. + * + * @param config The port configuration. + * @param manager The usb manager. + * @param device The device. + * @throws IOException Thrown if the port cannot be opened. + */ + public SlamtecSerialPort(UsbManager manager, UsbDevice device, SerialConfig config) throws IOException { + super(new SlamtecCP210XSerialPort(manager, device, config)); + } + + /** + * Creates a new serial port with the specified config, manager and device that + * uses the specified time as a fixed back-off for retries. + * + * @param config The port configuration. + * @param manager The usb manager. + * @param device The device. + * @param backoff The back-off time in milliseconds. + * @throws IllegalArgumentException Thrown if the back-off is not larger than 0. + * @throws IOException Thrown if the port cannot be opened. + */ + public SlamtecSerialPort(UsbManager manager, UsbDevice device, SerialConfig config, long backoff) throws IOException { + super(new SlamtecCP210XSerialPort(manager, device, config), backoff); + } + + /** + * Creates a new serial port with the specified config, manager and device that uses + * an exponential back-off with the specified minimum and maximum delay for retries. + * + * @param config The port configuration. + * @param manager The usb manager. + * @param device The device. + * @param minBackoff The minimum back-off time in milliseconds. + * @param maxBackoff The maximum back-off time in milliseconds. + * @throws IllegalArgumentException Thrown if the minimum or maximum back-off is not larger than 0. + * @throws IOException Thrown if the port cannot be opened. + */ + public SlamtecSerialPort(UsbManager manager, UsbDevice device, SerialConfig config, long minBackoff, long maxBackoff) throws IOException { + super(new SlamtecCP210XSerialPort(manager, device, config), minBackoff, maxBackoff); + } + +} diff --git a/serial-core/src/main/java/com/smartbotkit/serial/FilterSerialPort.java b/serial-core/src/main/java/com/smartbotkit/serial/FilterSerialPort.java new file mode 100644 index 0000000..0f9fe7b --- /dev/null +++ b/serial-core/src/main/java/com/smartbotkit/serial/FilterSerialPort.java @@ -0,0 +1,62 @@ +package com.smartbotkit.serial; + +import java.io.IOException; + +/** + * A base class for implementing wrappers for ports. + * This implementation simply forwards all calls to + * the wrapped port. Subclasses may use this to + * overwrite individual methods. + * + * @author Marcus + */ +public class FilterSerialPort implements ISerialPort { + + /** + * The port that we are operating on. + */ + protected ISerialPort port; + + /** + * Creates a wrapper for the port. + * + * @param port The port to be wrapped. + */ + public FilterSerialPort(ISerialPort port) { + this.port = port; + } + + @Override + public SerialConfig getConfig() { + return port.getConfig(); + } + @Override + public int read(byte[] buffer, int offset, int length) throws IOException { + return port.read(buffer, offset, length); + } + @Override + public void setFlowControlListener(IFlowControlListener flowControlListener) { + port.setFlowControlListener(flowControlListener); + } + + @Override + public void setLine(OutputControlLine line, boolean value) throws IOException { + port.setLine(line, value); + } + @Override + public void setLineErrorListener(ILineErrorListener lineErrorListener) { + port.setLineErrorListener(lineErrorListener); + } + @Override + public void write(byte[] buffer, int offset, int length) throws IOException { + port.write(buffer, offset, length); + } + + @Override + public void close() throws IOException { + port.close(); + } + + +} + diff --git a/serial-core/src/main/java/com/smartbotkit/serial/RetrySerialPort.java b/serial-core/src/main/java/com/smartbotkit/serial/RetrySerialPort.java new file mode 100644 index 0000000..2724c27 --- /dev/null +++ b/serial-core/src/main/java/com/smartbotkit/serial/RetrySerialPort.java @@ -0,0 +1,136 @@ +package com.smartbotkit.serial; + +import java.io.IOException; + +/** + * A port that retries read operations that are failing with + * a negative return value until the port is manually closed. + * To avoid high processor loads, the retry logic performs an + * exponential back-off starting from configurable a minimum up + * to a configurable maximum delay. + * + * @author Marcus + */ +public class RetrySerialPort extends FilterSerialPort { + + /** + * The default minimum back-off time. + */ + public static final long BACKOFF_MINIMUM_DEFAULT = 15; + + /** + * The default maximum back-off time. + */ + public static final long BACKOFF_MAXIMUM_DEFAULT = 120; + + /** + * A flag to indicate whether the port is closed. + */ + protected boolean closed = false; + + /** + * The port that we are operating on. + */ + protected ISerialPort port; + + /** + * The minimum back-off time. + */ + protected long minBackoff; + + /** + * The maximum back-off time. + */ + protected long maxBackoff; + + /** + * Creates a blocking port with an exponential back-off + * with default back-off values. + * + * @param port The port. + */ + public RetrySerialPort(ISerialPort port) { + this(port, BACKOFF_MINIMUM_DEFAULT, BACKOFF_MAXIMUM_DEFAULT); + } + + /** + * Creates a port with a fixed back-off time in milliseconds. + * + * @param port The port. + * @param backoff The back-off time in milliseconds. + * @throws IllegalArgumentException Thrown if the back-off is not larger than 0. + */ + public RetrySerialPort(ISerialPort port, long backoff) { + this(port, backoff, backoff); + } + + /** + * Creates a port with an exponential back-off with the + * specified minimum and maximum values in milliseconds. + * + * @param port The port. + * @param minBackoff The minimum back-off time in milliseconds. + * @param maxBackoff The maximum back-off time in milliseconds. + * @throws IllegalArgumentException Thrown if the minimum or maximum back-off is not larger than 0. + */ + public RetrySerialPort(ISerialPort port, long minBackoff, long maxBackoff) { + super(port); + if (minBackoff <= 0) throw new IllegalArgumentException("Minimum back-off time must be positive (" + minBackoff + " <= 0)."); + if (maxBackoff <= 0) throw new IllegalArgumentException("Maximum back-off time must be positive (" + maxBackoff + " <= 0)."); + this.minBackoff = minBackoff; + this.maxBackoff = maxBackoff; + } + + + @Override + public int read(byte[] buffer, int offset, int length) throws IOException { + long backoff = Math.min(minBackoff, maxBackoff); + while (true) { + if (closed) throw new IOException("Port is closed."); + int result = super.read(buffer, offset, length); + if (result >= 0 || (result < 0 && closed)) return result; + sleep(backoff); + backoff = Math.min(backoff * 2, maxBackoff); + } + } + + /** + * Sleeps for for the specified time back-off time by + * waiting on the this monitor. The logic ensures + * that the desired time is met unless the clock + * is not monotonic or the port is closed. In both + * cases, the sleep time can be shorter than expected. + * + * @param backoff The time to wait. + */ + protected void sleep(long backoff) { + long now = System.currentTimeMillis(); + long end = now + backoff; + synchronized (this) { + while (now < end && !closed) { + try { + this.wait(end - now); + } catch (InterruptedException e) { + // nothing to be done + } + long time = System.currentTimeMillis(); + // handle non-monotonic wall-time clock changes + if (time < now) break; + now = time; + } + } + } + + + @Override + public void close() throws IOException { + // close the port an unblock waiting read operations + synchronized (this) { + this.closed = true; + this.notifyAll(); + } + super.close(); + } + + +}