make all SerialPort classes public
and test FtdiSerialPort methods
This commit is contained in:
parent
33149b66fd
commit
735fa3d70f
|
@ -33,6 +33,7 @@ build/
|
|||
# Gradle:
|
||||
.idea/gradle.xml
|
||||
.idea/libraries
|
||||
.idea/jarRepositories.xml
|
||||
|
||||
# Eclipse/Android/Misc
|
||||
.metadata/
|
||||
|
|
|
@ -6,7 +6,7 @@ buildscript {
|
|||
google()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.6.2'
|
||||
classpath 'com.android.tools.build:gradle:4.0.0'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#Sat Feb 29 19:14:16 CET 2020
|
||||
#Wed Jun 10 08:41:47 CEST 2020
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
apply plugin: 'com.android.application'
|
||||
|
||||
android {
|
||||
compileSdkVersion 28
|
||||
buildToolsVersion '28.0.3'
|
||||
compileSdkVersion 29
|
||||
buildToolsVersion '29.0.3'
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
apply plugin: 'com.android.library'
|
||||
|
||||
android {
|
||||
compileSdkVersion 28
|
||||
buildToolsVersion '28.0.3'
|
||||
compileSdkVersion 29
|
||||
buildToolsVersion '29.0.3'
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 17
|
||||
|
|
|
@ -65,6 +65,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
@ -1675,4 +1676,57 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
|
|||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void commonMethods() throws Exception {
|
||||
String s;
|
||||
assertNotNull(usbSerialPort.getDriver());
|
||||
assertNotNull(usbSerialPort.getDevice());
|
||||
assertEquals(test_device_port, usbSerialPort.getPortNumber());
|
||||
s = usbSerialDriver.toString();
|
||||
assertNotEquals(0, s.length());
|
||||
|
||||
assertFalse(usbSerialPort.isOpen());
|
||||
usbOpen();
|
||||
assertTrue(usbSerialPort.isOpen());
|
||||
|
||||
s = usbSerialPort.getSerial();
|
||||
// with target sdk 29 can throw SecurityException before USB permission dialog is confirmed
|
||||
// not all devices implement serial numbers. some observed values are:
|
||||
// FT232 00000000, FTGH4NTX, ...
|
||||
// FT2232 <null>
|
||||
// CP2102 0001
|
||||
// CP2105 0035E46E
|
||||
// CH340 <null>
|
||||
// PL2303 <null>
|
||||
// CDC:Microbit 9900000037024e450034200b0000004a0000000097969901
|
||||
// CDC:Digispark <null>
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ftdiMethods() throws Exception {
|
||||
if(!(usbSerialDriver instanceof FtdiSerialDriver))
|
||||
return;
|
||||
byte[] b;
|
||||
usbOpen();
|
||||
usbParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
telnetParameters(115200, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
|
||||
FtdiSerialDriver.FtdiSerialPort ftdiSerialPort = (FtdiSerialDriver.FtdiSerialPort) usbSerialPort;
|
||||
int lt = ftdiSerialPort.getLatencyTimer();
|
||||
ftdiSerialPort.setLatencyTimer(1);
|
||||
telnetWrite("x".getBytes());
|
||||
b = usbRead(1);
|
||||
long t1 = System.currentTimeMillis();
|
||||
telnetWrite("x".getBytes());
|
||||
b = usbRead(1);
|
||||
ftdiSerialPort.setLatencyTimer(100);
|
||||
long t2 = System.currentTimeMillis();
|
||||
telnetWrite("x".getBytes());
|
||||
b = usbRead(1);
|
||||
long t3 = System.currentTimeMillis();
|
||||
ftdiSerialPort.setLatencyTimer(lt);
|
||||
assertEquals("latency 1", 99, Math.max(t2-t1, 99)); // looks strange, but shows actual value
|
||||
assertEquals("latency 100", 99, Math.min(t3-t2, 99));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||
return mPorts;
|
||||
}
|
||||
|
||||
class CdcAcmSerialPort extends CommonUsbSerialPort {
|
||||
public class CdcAcmSerialPort extends CommonUsbSerialPort {
|
||||
|
||||
private UsbInterface mControlInterface;
|
||||
private UsbInterface mDataInterface;
|
||||
|
@ -109,7 +109,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void openInt(UsbDeviceConnection connection) throws IOException {
|
||||
protected void openInt(UsbDeviceConnection connection) throws IOException {
|
||||
if (mPortNumber == -1) {
|
||||
Log.d(TAG,"device might be castrated ACM device, trying single interface logic");
|
||||
openSingleInterface();
|
||||
|
@ -210,7 +210,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void closeInt() {
|
||||
protected void closeInt() {
|
||||
try {
|
||||
mConnection.releaseInterface(mControlInterface);
|
||||
mConnection.releaseInterface(mDataInterface);
|
||||
|
|
|
@ -90,7 +90,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void openInt(UsbDeviceConnection connection) throws IOException {
|
||||
protected void openInt(UsbDeviceConnection connection) throws IOException {
|
||||
for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
|
||||
UsbInterface usbIface = mDevice.getInterface(i);
|
||||
if (!mConnection.claimInterface(usbIface, true)) {
|
||||
|
@ -115,7 +115,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void closeInt() {
|
||||
protected void closeInt() {
|
||||
try {
|
||||
for (int i = 0; i < mDevice.getInterfaceCount(); i++)
|
||||
mConnection.releaseInterface(mDevice.getInterface(i));
|
||||
|
|
|
@ -68,12 +68,8 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||
mDevice.getDeviceId(), mPortNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the currently-bound USB device.
|
||||
*
|
||||
* @return the device
|
||||
*/
|
||||
public final UsbDevice getDevice() {
|
||||
@Override
|
||||
public UsbDevice getDevice() {
|
||||
return mDevice;
|
||||
}
|
||||
|
||||
|
|
|
@ -137,7 +137,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void openInt(UsbDeviceConnection connection) throws IOException {
|
||||
protected void openInt(UsbDeviceConnection connection) throws IOException {
|
||||
mIsRestrictedPort = mDevice.getInterfaceCount() == 2 && mPortNumber == 1;
|
||||
if(mPortNumber >= mDevice.getInterfaceCount()) {
|
||||
throw new IOException("Unknown port number");
|
||||
|
@ -164,7 +164,7 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void closeInt() {
|
||||
protected void closeInt() {
|
||||
try {
|
||||
setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_DISABLE);
|
||||
} catch (Exception ignored) {}
|
||||
|
|
|
@ -117,23 +117,23 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||
return mPorts;
|
||||
}
|
||||
|
||||
private class FtdiSerialPort extends CommonUsbSerialPort {
|
||||
public class FtdiSerialPort extends CommonUsbSerialPort {
|
||||
|
||||
public static final int USB_TYPE_STANDARD = 0x00 << 5;
|
||||
public static final int USB_TYPE_CLASS = 0x00 << 5;
|
||||
public static final int USB_TYPE_VENDOR = 0x00 << 5;
|
||||
public static final int USB_TYPE_RESERVED = 0x00 << 5;
|
||||
private static final int USB_TYPE_STANDARD = 0x00 << 5;
|
||||
private static final int USB_TYPE_CLASS = 0x00 << 5;
|
||||
private static final int USB_TYPE_VENDOR = 0x00 << 5;
|
||||
private static final int USB_TYPE_RESERVED = 0x00 << 5;
|
||||
|
||||
public static final int USB_RECIP_DEVICE = 0x00;
|
||||
public static final int USB_RECIP_INTERFACE = 0x01;
|
||||
public static final int USB_RECIP_ENDPOINT = 0x02;
|
||||
public static final int USB_RECIP_OTHER = 0x03;
|
||||
private static final int USB_RECIP_DEVICE = 0x00;
|
||||
private static final int USB_RECIP_INTERFACE = 0x01;
|
||||
private static final int USB_RECIP_ENDPOINT = 0x02;
|
||||
private static final int USB_RECIP_OTHER = 0x03;
|
||||
|
||||
public static final int USB_ENDPOINT_IN = 0x80;
|
||||
public static final int USB_ENDPOINT_OUT = 0x00;
|
||||
private static final int USB_ENDPOINT_IN = 0x80;
|
||||
private static final int USB_ENDPOINT_OUT = 0x00;
|
||||
|
||||
public static final int USB_WRITE_TIMEOUT_MILLIS = 5000;
|
||||
public static final int USB_READ_TIMEOUT_MILLIS = 5000;
|
||||
private static final int USB_WRITE_TIMEOUT_MILLIS = 5000;
|
||||
private static final int USB_READ_TIMEOUT_MILLIS = 5000;
|
||||
|
||||
// From ftdi.h
|
||||
/**
|
||||
|
@ -175,10 +175,10 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||
*/
|
||||
private static final int SIO_GET_MODEM_STATUS_REQUEST = 5;
|
||||
|
||||
public static final int SIO_MODEM_STATUS_CTS = 0x10;
|
||||
public static final int SIO_MODEM_STATUS_DSR = 0x20;
|
||||
public static final int SIO_MODEM_STATUS_RI = 0x40;
|
||||
public static final int SIO_MODEM_STATUS_RLSD = 0x80;
|
||||
private static final int SIO_MODEM_STATUS_CTS = 0x10;
|
||||
private static final int SIO_MODEM_STATUS_DSR = 0x20;
|
||||
private static final int SIO_MODEM_STATUS_RI = 0x40;
|
||||
private static final int SIO_MODEM_STATUS_RLSD = 0x80;
|
||||
|
||||
/**
|
||||
* Set the latency timer.
|
||||
|
@ -190,10 +190,10 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||
*/
|
||||
private static final int SIO_GET_LATENCY_TIMER_REQUEST = 10;
|
||||
|
||||
public static final int FTDI_DEVICE_OUT_REQTYPE =
|
||||
private static final int FTDI_DEVICE_OUT_REQTYPE =
|
||||
UsbConstants.USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT;
|
||||
|
||||
public static final int FTDI_DEVICE_IN_REQTYPE =
|
||||
private static final int FTDI_DEVICE_IN_REQTYPE =
|
||||
UsbConstants.USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN;
|
||||
|
||||
/**
|
||||
|
@ -246,7 +246,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||
return totalBytesRead - (packetsCount * 2);
|
||||
}
|
||||
|
||||
void reset() throws IOException {
|
||||
private void reset() throws IOException {
|
||||
// TODO(mikey): autodetect.
|
||||
mType = DeviceType.TYPE_R;
|
||||
if(mDevice.getInterfaceCount() > 1) {
|
||||
|
@ -284,7 +284,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||
return data[0];
|
||||
}
|
||||
|
||||
public int getModemStatus() throws IOException {
|
||||
private int getModemStatus() throws IOException {
|
||||
byte[] data = new byte[2];
|
||||
int result = mConnection.controlTransfer(FTDI_DEVICE_IN_REQTYPE, SIO_GET_MODEM_STATUS_REQUEST,
|
||||
0, mIndex, data, data.length, USB_WRITE_TIMEOUT_MILLIS);
|
||||
|
@ -295,7 +295,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void openInt(UsbDeviceConnection connection) throws IOException {
|
||||
protected void openInt(UsbDeviceConnection connection) throws IOException {
|
||||
if (connection.claimInterface(mDevice.getInterface(mPortNumber), true)) {
|
||||
Log.d(TAG, "claimInterface " + mPortNumber + " SUCCESS");
|
||||
} else {
|
||||
|
@ -311,7 +311,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void closeInt() {
|
||||
protected void closeInt() {
|
||||
try {
|
||||
mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
|
||||
} catch(Exception ignored) {}
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
package com.hoho.android.usbserial.driver;
|
||||
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbManager;
|
||||
|
||||
|
@ -86,7 +87,12 @@ public interface UsbSerialPort extends Closeable {
|
|||
public static final int STOPBITS_2 = 2;
|
||||
|
||||
public UsbSerialDriver getDriver();
|
||||
|
||||
|
||||
/**
|
||||
* Returns the currently-bound USB device.
|
||||
*/
|
||||
public UsbDevice getDevice();
|
||||
|
||||
/**
|
||||
* Port number within driver.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue