1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-08 08:26:18 +00:00

UsbSerialDriver is an interface once again.

Code shared among the existing drivers is moved into CommonUsbSerialDriver.
This commit is contained in:
mike wakerly 2013-02-19 13:22:02 -08:00
parent 2b3528b425
commit fe2eb8278b
5 changed files with 190 additions and 87 deletions

View File

@ -19,7 +19,7 @@ import java.util.Map;
* href="http://www.usb.org/developers/devclass_docs/usbcdc11.pdf">Universal * href="http://www.usb.org/developers/devclass_docs/usbcdc11.pdf">Universal
* Serial Bus Class Definitions for Communication Devices, v1.1</a> * Serial Bus Class Definitions for Communication Devices, v1.1</a>
*/ */
public class CdcAcmSerialDriver extends UsbSerialDriver { public class CdcAcmSerialDriver extends CommonUsbSerialDriver {
private final String TAG = CdcAcmSerialDriver.class.getSimpleName(); private final String TAG = CdcAcmSerialDriver.class.getSimpleName();

View File

@ -0,0 +1,141 @@
/* Copyright 2013 Google Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* Project home page: http://code.google.com/p/usb-serial-for-android/
*/
package com.hoho.android.usbserial.driver;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import java.io.IOException;
/**
* A base class shared by several driver implementations.
*
* @author mike wakerly (opensource@hoho.com)
*/
abstract class CommonUsbSerialDriver implements UsbSerialDriver {
public static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
public static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
protected final UsbDevice mDevice;
protected final UsbDeviceConnection mConnection;
protected final Object mReadBufferLock = new Object();
protected final Object mWriteBufferLock = new Object();
/** Internal read buffer. Guarded by {@link #mReadBufferLock}. */
protected byte[] mReadBuffer;
/** Internal write buffer. Guarded by {@link #mWriteBufferLock}. */
protected byte[] mWriteBuffer;
public CommonUsbSerialDriver(UsbDevice device, UsbDeviceConnection connection) {
mDevice = device;
mConnection = connection;
mReadBuffer = new byte[DEFAULT_READ_BUFFER_SIZE];
mWriteBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE];
}
/**
* Returns the currently-bound USB device.
*
* @return the device
*/
public final UsbDevice getDevice() {
return mDevice;
}
/**
* Sets the size of the internal buffer used to exchange data with the USB
* stack for read operations. Most users should not need to change this.
*
* @param bufferSize the size in bytes
*/
public final void setReadBufferSize(int bufferSize) {
synchronized (mReadBufferLock) {
if (bufferSize == mReadBuffer.length) {
return;
}
mReadBuffer = new byte[bufferSize];
}
}
/**
* Sets the size of the internal buffer used to exchange data with the USB
* stack for write operations. Most users should not need to change this.
*
* @param bufferSize the size in bytes
*/
public final void setWriteBufferSize(int bufferSize) {
synchronized (mWriteBufferLock) {
if (bufferSize == mWriteBuffer.length) {
return;
}
mWriteBuffer = new byte[bufferSize];
}
}
@Override
public abstract void open() throws IOException;
@Override
public abstract void close() throws IOException;
@Override
public abstract int read(final byte[] dest, final int timeoutMillis) throws IOException;
@Override
public abstract int write(final byte[] src, final int timeoutMillis) throws IOException;
@Override
@Deprecated
public abstract int setBaudRate(final int baudRate) throws IOException;
@Override
public abstract void setParameters(
int baudRate, int dataBits, int stopBits, int parity) throws IOException;
@Override
public abstract boolean getCD() throws IOException;
@Override
public abstract boolean getCTS() throws IOException;
@Override
public abstract boolean getDSR() throws IOException;
@Override
public abstract boolean getDTR() throws IOException;
@Override
public abstract void setDTR(boolean value) throws IOException;
@Override
public abstract boolean getRI() throws IOException;
@Override
public abstract boolean getRTS() throws IOException;
@Override
public abstract void setRTS(boolean value) throws IOException;
}

View File

@ -11,7 +11,7 @@ import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface; import android.hardware.usb.UsbInterface;
import android.util.Log; import android.util.Log;
public class Cp2102SerialDriver extends UsbSerialDriver { public class Cp2102SerialDriver extends CommonUsbSerialDriver {
private static final String TAG = Cp2102SerialDriver.class.getSimpleName(); private static final String TAG = Cp2102SerialDriver.class.getSimpleName();

View File

@ -35,7 +35,7 @@ import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
/** /**
* A {@link UsbSerialDriver} implementation for a variety of FTDI devices * A {@link CommonUsbSerialDriver} implementation for a variety of FTDI devices
* <p> * <p>
* This driver is based on * This driver is based on
* <a href="http://www.intra2net.com/en/developer/libftdi">libftdi</a>, and is * <a href="http://www.intra2net.com/en/developer/libftdi">libftdi</a>, and is
@ -87,7 +87,7 @@ import java.util.Map;
* @see <a href="http://www.ftdichip.com/">FTDI Homepage</a> * @see <a href="http://www.ftdichip.com/">FTDI Homepage</a>
* @see <a href="http://www.intra2net.com/en/developer/libftdi">libftdi</a> * @see <a href="http://www.intra2net.com/en/developer/libftdi">libftdi</a>
*/ */
public class FtdiSerialDriver extends UsbSerialDriver { public class FtdiSerialDriver extends CommonUsbSerialDriver {
private static final int DEFAULT_BAUD_RATE = 115200; private static final int DEFAULT_BAUD_RATE = 115200;
private static final int DEFAULT_DATA_BITS = DATABITS_8; private static final int DEFAULT_DATA_BITS = DATABITS_8;

View File

@ -20,79 +20,80 @@
package com.hoho.android.usbserial.driver; package com.hoho.android.usbserial.driver;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import java.io.IOException; import java.io.IOException;
/** /**
* Driver interface for a supported USB serial device. * Driver interface for a USB serial device.
* *
* @author mike wakerly (opensource@hoho.com) * @author mike wakerly (opensource@hoho.com)
*/ */
public abstract class UsbSerialDriver { public interface UsbSerialDriver {
public static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
public static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
protected final UsbDevice mDevice;
protected final UsbDeviceConnection mConnection;
protected final Object mReadBufferLock = new Object();
protected final Object mWriteBufferLock = new Object();
/** Internal read buffer. Guarded by {@link #mReadBufferLock}. */
protected byte[] mReadBuffer;
/** Internal write buffer. Guarded by {@link #mWriteBufferLock}. */
protected byte[] mWriteBuffer;
/** 5 data bits. */
public static final int DATABITS_5 = 5; public static final int DATABITS_5 = 5;
/** 6 data bits. */
public static final int DATABITS_6 = 6; public static final int DATABITS_6 = 6;
/** 7 data bits. */
public static final int DATABITS_7 = 7; public static final int DATABITS_7 = 7;
/** 8 data bits. */
public static final int DATABITS_8 = 8; public static final int DATABITS_8 = 8;
/** No flow control. */
public static final int FLOWCONTROL_NONE = 0; public static final int FLOWCONTROL_NONE = 0;
/** RTS/CTS input flow control. */
public static final int FLOWCONTROL_RTSCTS_IN = 1; public static final int FLOWCONTROL_RTSCTS_IN = 1;
/** RTS/CTS output flow control. */
public static final int FLOWCONTROL_RTSCTS_OUT = 2; public static final int FLOWCONTROL_RTSCTS_OUT = 2;
/** XON/XOFF input flow control. */
public static final int FLOWCONTROL_XONXOFF_IN = 4; public static final int FLOWCONTROL_XONXOFF_IN = 4;
/** XON/XOFF output flow control. */
public static final int FLOWCONTROL_XONXOFF_OUT = 8; public static final int FLOWCONTROL_XONXOFF_OUT = 8;
public static final int PARITY_EVEN = 2; /** No parity. */
public static final int PARITY_MARK = 3;
public static final int PARITY_NONE = 0; public static final int PARITY_NONE = 0;
/** Odd parity. */
public static final int PARITY_ODD = 1; public static final int PARITY_ODD = 1;
/** Even parity. */
public static final int PARITY_EVEN = 2;
/** Mark parity. */
public static final int PARITY_MARK = 3;
/** Space parity. */
public static final int PARITY_SPACE = 4; public static final int PARITY_SPACE = 4;
/** 1 stop bit. */ /** 1 stop bit. */
public static final int STOPBITS_1 = 1; public static final int STOPBITS_1 = 1;
/** 1.5 stop bits. */ /** 1.5 stop bits. */
public static final int STOPBITS_1_5 = 3; public static final int STOPBITS_1_5 = 3;
/** 2 stop bits. */ /** 2 stop bits. */
public static final int STOPBITS_2 = 2; public static final int STOPBITS_2 = 2;
public UsbSerialDriver(UsbDevice device, UsbDeviceConnection connection) {
mDevice = device;
mConnection = connection;
mReadBuffer = new byte[DEFAULT_READ_BUFFER_SIZE];
mWriteBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE];
}
/** /**
* Opens and initializes the device as a USB serial device. Upon success, * Opens and initializes the device as a USB serial device. Upon success,
* caller must ensure that {@link #close()} is eventually called. * caller must ensure that {@link #close()} is eventually called.
* *
* @throws IOException on error opening or initializing the device. * @throws IOException on error opening or initializing the device.
*/ */
public abstract void open() throws IOException; public void open() throws IOException;
/** /**
* Closes the serial device. * Closes the serial device.
* *
* @throws IOException on error closing the device. * @throws IOException on error closing the device.
*/ */
public abstract void close() throws IOException; public void close() throws IOException;
/** /**
* Reads as many bytes as possible into the destination buffer. * Reads as many bytes as possible into the destination buffer.
@ -102,7 +103,7 @@ public abstract class UsbSerialDriver {
* @return the actual number of bytes read * @return the actual number of bytes read
* @throws IOException if an error occurred during reading * @throws IOException if an error occurred during reading
*/ */
public abstract int read(final byte[] dest, final int timeoutMillis) throws IOException; public int read(final byte[] dest, final int timeoutMillis) throws IOException;
/** /**
* Writes as many bytes as possible from the source buffer. * Writes as many bytes as possible from the source buffer.
@ -112,7 +113,7 @@ public abstract class UsbSerialDriver {
* @return the actual number of bytes written * @return the actual number of bytes written
* @throws IOException if an error occurred during writing * @throws IOException if an error occurred during writing
*/ */
public abstract int write(final byte[] src, final int timeoutMillis) throws IOException; public int write(final byte[] src, final int timeoutMillis) throws IOException;
/** /**
* Sets the baud rate of the serial device. * Sets the baud rate of the serial device.
@ -123,7 +124,7 @@ public abstract class UsbSerialDriver {
* @deprecated Use {@link #setParameters(int, int, int, int)} instead of this method. * @deprecated Use {@link #setParameters(int, int, int, int)} instead of this method.
*/ */
@Deprecated @Deprecated
public abstract int setBaudRate(final int baudRate) throws IOException; public int setBaudRate(final int baudRate) throws IOException;
/** /**
* Sets various serial port parameters. * Sets various serial port parameters.
@ -138,7 +139,7 @@ public abstract class UsbSerialDriver {
* {@link #PARITY_SPACE}. * {@link #PARITY_SPACE}.
* @throws IOException on error setting the port parameters * @throws IOException on error setting the port parameters
*/ */
public abstract void setParameters( public void setParameters(
int baudRate, int dataBits, int stopBits, int parity) throws IOException; int baudRate, int dataBits, int stopBits, int parity) throws IOException;
/** /**
@ -147,7 +148,7 @@ public abstract class UsbSerialDriver {
* @return the current state, or {@code false} if not supported. * @return the current state, or {@code false} if not supported.
* @throws IOException if an error occurred during reading * @throws IOException if an error occurred during reading
*/ */
public abstract boolean getCD() throws IOException; public boolean getCD() throws IOException;
/** /**
* Gets the CTS (Clear To Send) bit from the underlying UART. * Gets the CTS (Clear To Send) bit from the underlying UART.
@ -155,7 +156,7 @@ public abstract class UsbSerialDriver {
* @return the current state, or {@code false} if not supported. * @return the current state, or {@code false} if not supported.
* @throws IOException if an error occurred during reading * @throws IOException if an error occurred during reading
*/ */
public abstract boolean getCTS() throws IOException; public boolean getCTS() throws IOException;
/** /**
* Gets the DSR (Data Set Ready) bit from the underlying UART. * Gets the DSR (Data Set Ready) bit from the underlying UART.
@ -163,7 +164,7 @@ public abstract class UsbSerialDriver {
* @return the current state, or {@code false} if not supported. * @return the current state, or {@code false} if not supported.
* @throws IOException if an error occurred during reading * @throws IOException if an error occurred during reading
*/ */
public abstract boolean getDSR() throws IOException; public boolean getDSR() throws IOException;
/** /**
* Gets the DTR (Data Terminal Ready) bit from the underlying UART. * Gets the DTR (Data Terminal Ready) bit from the underlying UART.
@ -171,7 +172,7 @@ public abstract class UsbSerialDriver {
* @return the current state, or {@code false} if not supported. * @return the current state, or {@code false} if not supported.
* @throws IOException if an error occurred during reading * @throws IOException if an error occurred during reading
*/ */
public abstract boolean getDTR() throws IOException; public boolean getDTR() throws IOException;
/** /**
* Sets the DTR (Data Terminal Ready) bit on the underlying UART, if * Sets the DTR (Data Terminal Ready) bit on the underlying UART, if
@ -180,7 +181,7 @@ public abstract class UsbSerialDriver {
* @param value the value to set * @param value the value to set
* @throws IOException if an error occurred during writing * @throws IOException if an error occurred during writing
*/ */
public abstract void setDTR(boolean value) throws IOException; public void setDTR(boolean value) throws IOException;
/** /**
* Gets the RI (Ring Indicator) bit from the underlying UART. * Gets the RI (Ring Indicator) bit from the underlying UART.
@ -188,7 +189,7 @@ public abstract class UsbSerialDriver {
* @return the current state, or {@code false} if not supported. * @return the current state, or {@code false} if not supported.
* @throws IOException if an error occurred during reading * @throws IOException if an error occurred during reading
*/ */
public abstract boolean getRI() throws IOException; public boolean getRI() throws IOException;
/** /**
* Gets the RTS (Request To Send) bit from the underlying UART. * Gets the RTS (Request To Send) bit from the underlying UART.
@ -196,7 +197,7 @@ public abstract class UsbSerialDriver {
* @return the current state, or {@code false} if not supported. * @return the current state, or {@code false} if not supported.
* @throws IOException if an error occurred during reading * @throws IOException if an error occurred during reading
*/ */
public abstract boolean getRTS() throws IOException; public boolean getRTS() throws IOException;
/** /**
* Sets the RTS (Request To Send) bit on the underlying UART, if * Sets the RTS (Request To Send) bit on the underlying UART, if
@ -205,45 +206,6 @@ public abstract class UsbSerialDriver {
* @param value the value to set * @param value the value to set
* @throws IOException if an error occurred during writing * @throws IOException if an error occurred during writing
*/ */
public abstract void setRTS(boolean value) throws IOException; public void setRTS(boolean value) throws IOException;
/**
* Returns the currently-bound USB device.
*
* @return the device
*/
public final UsbDevice getDevice() {
return mDevice;
}
/**
* Sets the size of the internal buffer used to exchange data with the USB
* stack for read operations. Most users should not need to change this.
*
* @param bufferSize the size in bytes
*/
public final void setReadBufferSize(int bufferSize) {
synchronized (mReadBufferLock) {
if (bufferSize == mReadBuffer.length) {
return;
}
mReadBuffer = new byte[bufferSize];
}
}
/**
* Sets the size of the internal buffer used to exchange data with the USB
* stack for write operations. Most users should not need to change this.
*
* @param bufferSize the size in bytes
*/
public final void setWriteBufferSize(int bufferSize) {
synchronized (mWriteBufferLock) {
if (bufferSize == mWriteBuffer.length) {
return;
}
mWriteBuffer = new byte[bufferSize];
}
}
} }