1
0
mirror of https://github.com/mik3y/usb-serial-for-android synced 2025-06-07 07:56:20 +00:00

IntDef Parity for better warnings

but no @Intdef for databits, stopbits as these are frequently used with numbers instead of constants
remove redundant modifiers
This commit is contained in:
kai-morich 2021-01-16 22:17:34 +01:00
parent 5519182256
commit fc610a9764
13 changed files with 60 additions and 74 deletions

View File

@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:4.1.1'
}
}

View File

@ -226,7 +226,7 @@ public class TerminalFragment extends Fragment implements SerialInputOutputManag
try {
usbSerialPort.open(usbConnection);
usbSerialPort.setParameters(baudRate, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
usbSerialPort.setParameters(baudRate, 8, 1, UsbSerialPort.PARITY_NONE);
if(withIoManager) {
usbIoManager = new SerialInputOutputManager(usbSerialPort, this);
Executors.newSingleThreadExecutor().submit(usbIoManager);

View File

@ -22,8 +22,8 @@ android {
}
dependencies {
implementation "androidx.annotation:annotation:1.1.0"
testImplementation 'junit:junit:4.13'
androidTestImplementation 'com.android.support:support-annotations:28.0.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'commons-net:commons-net:3.6'
androidTestImplementation 'org.apache.commons:commons-lang3:3.11'

View File

@ -1,5 +1,7 @@
package com.hoho.android.usbserial.util;
import com.hoho.android.usbserial.driver.UsbSerialPort;
import org.apache.commons.net.telnet.InvalidTelnetOptionException;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TelnetCommand;
@ -140,7 +142,7 @@ public class TelnetWrapper {
}
}
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException, InterruptedException, InvalidTelnetOptionException {
public void setParameters(int baudRate, int dataBits, int stopBits, @UsbSerialPort.Parity int parity) throws IOException, InterruptedException, InvalidTelnetOptionException {
doCommand("set-baudrate", new byte[] {RFC2217_COM_PORT_OPTION, RFC2217_SET_BAUDRATE, (byte)(baudRate>>24), (byte)(baudRate>>16), (byte)(baudRate>>8), (byte)baudRate});
doCommand("set-datasize", new byte[] {RFC2217_COM_PORT_OPTION, RFC2217_SET_DATASIZE, (byte)dataBits});
doCommand("set-stopsize", new byte[] {RFC2217_COM_PORT_OPTION, RFC2217_SET_STOPSIZE, (byte)stopBits});

View File

@ -226,7 +226,7 @@ public class UsbWrapper implements SerialInputOutputManager.Listener {
serialPort.write(data, USB_WRITE_WAIT);
}
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException, InterruptedException {
public void setParameters(int baudRate, int dataBits, int stopBits, @UsbSerialPort.Parity int parity) throws IOException, InterruptedException {
serialPort.setParameters(baudRate, dataBits, stopBits, parity);
if(serialDriver instanceof CdcAcmSerialDriver)
Thread.sleep(10); // arduino_leonardeo_bridge.ini needs some time

View File

@ -204,7 +204,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
}
@Override
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
public void setParameters(int baudRate, int dataBits, int stopBits, @Parity int parity) throws IOException {
if(baudRate <= 0) {
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
}

View File

@ -220,7 +220,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
}
@Override
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
public void setParameters(int baudRate, int dataBits, int stopBits, @Parity int parity) throws IOException {
if(baudRate <= 0) {
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
}

View File

@ -229,7 +229,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
}
@Override
public abstract void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException;
public abstract void setParameters(int baudRate, int dataBits, int stopBits, @Parity int parity) throws IOException;
@Override
public boolean getCD() throws IOException { throw new UnsupportedOperationException(); }

View File

@ -175,7 +175,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
}
@Override
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
public void setParameters(int baudRate, int dataBits, int stopBits, @Parity int parity) throws IOException {
if(baudRate <= 0) {
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
}

View File

@ -226,7 +226,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
}
@Override
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
public void setParameters(int baudRate, int dataBits, int stopBits, @Parity int parity) throws IOException {
if(baudRate <= 0) {
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
}

View File

@ -349,7 +349,7 @@ public class ProlificSerialDriver implements UsbSerialDriver {
}
@Override
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
public void setParameters(int baudRate, int dataBits, int stopBits, @Parity int parity) throws IOException {
baudRate = filterBaudRate(baudRate);
if ((mBaudRate == baudRate) && (mDataBits == dataBits)
&& (mStopBits == stopBits) && (mParity == parity)) {

View File

@ -21,7 +21,7 @@ public interface UsbSerialDriver {
*
* @return the device
*/
public UsbDevice getDevice();
UsbDevice getDevice();
/**
* Returns all available ports for this device. This list must have at least
@ -29,5 +29,5 @@ public interface UsbSerialDriver {
*
* @return the ports
*/
public List<UsbSerialPort> getPorts();
List<UsbSerialPort> getPorts();
}

View File

@ -10,8 +10,12 @@ import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import androidx.annotation.IntDef;
import java.io.Closeable;
import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.EnumSet;
/**
@ -22,81 +26,61 @@ import java.util.EnumSet;
public interface UsbSerialPort extends Closeable {
/** 5 data bits. */
public static final int DATABITS_5 = 5;
int DATABITS_5 = 5;
/** 6 data bits. */
public static final int DATABITS_6 = 6;
int DATABITS_6 = 6;
/** 7 data bits. */
public static final int DATABITS_7 = 7;
int DATABITS_7 = 7;
/** 8 data bits. */
public static final int DATABITS_8 = 8;
/** No flow control. */
public static final int FLOWCONTROL_NONE = 0;
/** RTS/CTS input flow control. */
public static final int FLOWCONTROL_RTSCTS_IN = 1;
/** RTS/CTS output flow control. */
public static final int FLOWCONTROL_RTSCTS_OUT = 2;
/** XON/XOFF input flow control. */
public static final int FLOWCONTROL_XONXOFF_IN = 4;
/** XON/XOFF output flow control. */
public static final int FLOWCONTROL_XONXOFF_OUT = 8;
int DATABITS_8 = 8;
/** Values for setParameters(..., parity) */
@Retention(RetentionPolicy.SOURCE)
@IntDef({PARITY_NONE, PARITY_ODD, PARITY_EVEN, PARITY_MARK, PARITY_SPACE})
@interface Parity {}
/** No parity. */
public static final int PARITY_NONE = 0;
int PARITY_NONE = 0;
/** Odd parity. */
public static final int PARITY_ODD = 1;
int PARITY_ODD = 1;
/** Even parity. */
public static final int PARITY_EVEN = 2;
int PARITY_EVEN = 2;
/** Mark parity. */
public static final int PARITY_MARK = 3;
int PARITY_MARK = 3;
/** Space parity. */
public static final int PARITY_SPACE = 4;
int PARITY_SPACE = 4;
/** 1 stop bit. */
public static final int STOPBITS_1 = 1;
int STOPBITS_1 = 1;
/** 1.5 stop bits. */
public static final int STOPBITS_1_5 = 3;
int STOPBITS_1_5 = 3;
/** 2 stop bits. */
public static final int STOPBITS_2 = 2;
int STOPBITS_2 = 2;
/** values for get[Supported]ControlLines() */
public enum ControlLine { RTS, CTS, DTR, DSR, CD, RI };
/** Values for get[Supported]ControlLines() */
enum ControlLine { RTS, CTS, DTR, DSR, CD, RI }
/**
* Returns the driver used by this port.
*/
public UsbSerialDriver getDriver();
UsbSerialDriver getDriver();
/**
* Returns the currently-bound USB device.
*/
public UsbDevice getDevice();
UsbDevice getDevice();
/**
* Port number within driver.
*/
public int getPortNumber();
int getPortNumber();
/**
* The serial number of the underlying UsbDeviceConnection, or {@code null}.
*
* @return value from {@link UsbDeviceConnection#getSerial()}
* @throws SecurityException starting with target SDK 29 (Android 10) if permission for USB device is not granted
*/
public String getSerial();
String getSerial();
/**
* Opens and initializes the port. Upon success, caller must ensure that
@ -106,14 +90,14 @@ public interface UsbSerialPort extends Closeable {
* {@link UsbManager#openDevice(android.hardware.usb.UsbDevice)}
* @throws IOException on error opening or initializing the port.
*/
public void open(UsbDeviceConnection connection) throws IOException;
void open(UsbDeviceConnection connection) throws IOException;
/**
* Closes the port and {@link UsbDeviceConnection}
*
* @throws IOException on error closing the port.
*/
public void close() throws IOException;
void close() throws IOException;
/**
* Reads as many bytes as possible into the destination buffer.
@ -123,7 +107,7 @@ public interface UsbSerialPort extends Closeable {
* @return the actual number of bytes read
* @throws IOException if an error occurred during reading
*/
public int read(final byte[] dest, final int timeout) throws IOException;
int read(final byte[] dest, final int timeout) throws IOException;
/**
* Writes as many bytes as possible from the source buffer.
@ -133,7 +117,7 @@ public interface UsbSerialPort extends Closeable {
* @return the actual number of bytes written
* @throws IOException if an error occurred during writing
*/
public int write(final byte[] src, final int timeout) throws IOException;
int write(final byte[] src, final int timeout) throws IOException;
/**
* Sets various serial port parameters.
@ -147,7 +131,7 @@ public interface UsbSerialPort extends Closeable {
* @throws IOException on error setting the port parameters
* @throws UnsupportedOperationException if values are not supported by a specific device
*/
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException;
void setParameters(int baudRate, int dataBits, int stopBits, @Parity int parity) throws IOException;
/**
* Gets the CD (Carrier Detect) bit from the underlying UART.
@ -156,7 +140,7 @@ public interface UsbSerialPort extends Closeable {
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getCD() throws IOException;
boolean getCD() throws IOException;
/**
* Gets the CTS (Clear To Send) bit from the underlying UART.
@ -165,7 +149,7 @@ public interface UsbSerialPort extends Closeable {
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getCTS() throws IOException;
boolean getCTS() throws IOException;
/**
* Gets the DSR (Data Set Ready) bit from the underlying UART.
@ -174,7 +158,7 @@ public interface UsbSerialPort extends Closeable {
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getDSR() throws IOException;
boolean getDSR() throws IOException;
/**
* Gets the DTR (Data Terminal Ready) bit from the underlying UART.
@ -183,7 +167,7 @@ public interface UsbSerialPort extends Closeable {
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getDTR() throws IOException;
boolean getDTR() throws IOException;
/**
* Sets the DTR (Data Terminal Ready) bit on the underlying UART, if supported.
@ -192,7 +176,7 @@ public interface UsbSerialPort extends Closeable {
* @throws IOException if an error occurred during writing
* @throws UnsupportedOperationException if not supported
*/
public void setDTR(boolean value) throws IOException;
void setDTR(boolean value) throws IOException;
/**
* Gets the RI (Ring Indicator) bit from the underlying UART.
@ -201,7 +185,7 @@ public interface UsbSerialPort extends Closeable {
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getRI() throws IOException;
boolean getRI() throws IOException;
/**
* Gets the RTS (Request To Send) bit from the underlying UART.
@ -210,7 +194,7 @@ public interface UsbSerialPort extends Closeable {
* @throws IOException if an error occurred during reading
* @throws UnsupportedOperationException if not supported
*/
public boolean getRTS() throws IOException;
boolean getRTS() throws IOException;
/**
* Sets the RTS (Request To Send) bit on the underlying UART, if supported.
@ -219,7 +203,7 @@ public interface UsbSerialPort extends Closeable {
* @throws IOException if an error occurred during writing
* @throws UnsupportedOperationException if not supported
*/
public void setRTS(boolean value) throws IOException;
void setRTS(boolean value) throws IOException;
/**
* Gets all control line values from the underlying UART, if supported.
@ -228,7 +212,7 @@ public interface UsbSerialPort extends Closeable {
* @return EnumSet.contains(...) is {@code true} if set, else {@code false}
* @throws IOException if an error occurred during reading
*/
public EnumSet<ControlLine> getControlLines() throws IOException;
EnumSet<ControlLine> getControlLines() throws IOException;
/**
* Gets all control line supported flags.
@ -236,7 +220,7 @@ public interface UsbSerialPort extends Closeable {
* @return EnumSet.contains(...) is {@code true} if supported, else {@code false}
* @throws IOException if an error occurred during reading
*/
public EnumSet<ControlLine> getSupportedControlLines() throws IOException;
EnumSet<ControlLine> getSupportedControlLines() throws IOException;
/**
* Purge non-transmitted output data and / or non-read input data.
@ -246,18 +230,18 @@ public interface UsbSerialPort extends Closeable {
* @throws IOException if an error occurred during flush
* @throws UnsupportedOperationException if not supported
*/
public void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException;
void purgeHwBuffers(boolean purgeWriteBuffers, boolean purgeReadBuffers) throws IOException;
/**
* send BREAK condition.
*
* @param value set/reset
*/
public void setBreak(boolean value) throws IOException;
void setBreak(boolean value) throws IOException;
/**
* Returns the current state of the connection.
*/
public boolean isOpen();
boolean isOpen();
}