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

reuse UsbRequest

less LogCat output
This commit is contained in:
kai-morich 2019-12-09 22:42:51 +01:00
parent 7b578918b0
commit ce97a3408b
9 changed files with 141 additions and 209 deletions

View File

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

View File

@ -5,7 +5,7 @@ publishing {
maven(MavenPublication) { maven(MavenPublication) {
groupId 'com.github.mik3y' groupId 'com.github.mik3y'
artifactId 'usb-serial-for-android' artifactId 'usb-serial-for-android'
version '2.1.0a' version '2.2.0a'
afterEvaluate { afterEvaluate {
artifact androidSourcesJar artifact androidSourcesJar
artifact bundleReleaseAar artifact bundleReleaseAar

View File

@ -1091,13 +1091,13 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
// //
int diffLen = readSpeedInt(5, 0); int diffLen = readSpeedInt(5, 0);
if(usbSerialDriver instanceof Ch34xSerialDriver && diffLen == -1) if(usbSerialDriver instanceof Ch34xSerialDriver && diffLen == -1)
diffLen = 0; // todo: investigate last packet loss diffLen = 0; // todo: investigate last packet loss
assertEquals(0, diffLen); assertEquals(0, diffLen);
} }
private int readSpeedInt(int writeSeconds, int readTimeout) throws Exception { private int readSpeedInt(int writeSeconds, int readTimeout) throws Exception {
int baudrate = 115200; int baudrate = 115200;
if(usbSerialDriver instanceof Ch34xSerialDriver && readTimeout != 0) if(usbSerialDriver instanceof Ch34xSerialDriver)
baudrate = 38400; baudrate = 38400;
int writeAhead = 5*baudrate/10; // write ahead for another 5 second read int writeAhead = 5*baudrate/10; // write ahead for another 5 second read
if(usbSerialDriver instanceof CdcAcmSerialDriver) if(usbSerialDriver instanceof CdcAcmSerialDriver)
@ -1355,8 +1355,6 @@ public class DeviceTest implements SerialInputOutputManager.Listener {
int longTimeout = 1000; int longTimeout = 1000;
int shortTimeout = 10; int shortTimeout = 10;
if(usbSerialDriver instanceof Ch34xSerialDriver)
shortTimeout = 20; // too short timeout causes mysterious effects like lost telnet data
time = System.currentTimeMillis(); time = System.currentTimeMillis();
len = usbSerialPort.read(buf, shortTimeout); len = usbSerialPort.read(buf, shortTimeout);
assertEquals(0, len); assertEquals(0, len);

View File

@ -94,26 +94,13 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
} }
@Override @Override
public void open(UsbDeviceConnection connection) throws IOException { public void openInt(UsbDeviceConnection connection) throws IOException {
if (mConnection != null) { if (1 == mDevice.getInterfaceCount()) {
throw new IOException("Already open"); Log.d(TAG,"device might be castrated ACM device, trying single interface logic");
} openSingleInterface();
} else {
mConnection = connection; Log.d(TAG,"trying default interface logic");
boolean opened = false; openInterface();
try {
if (1 == mDevice.getInterfaceCount()) {
Log.d(TAG,"device might be castrated ACM device, trying single interface logic");
openSingleInterface();
} else {
Log.d(TAG,"trying default interface logic");
openInterface();
}
opened = true;
} finally {
if (!opened) {
close();
}
} }
} }
@ -224,9 +211,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
if (ep.getDirection() == UsbConstants.USB_DIR_OUT && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) if (ep.getDirection() == UsbConstants.USB_DIR_OUT && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
mWriteEndpoint = ep; mWriteEndpoint = ep;
} }
if (mReadEndpoint == null || mWriteEndpoint == null) {
throw new IOException("Could not get read&write endpoints");
}
} }
private int sendAcmControlMessage(int request, int value, byte[] buf) throws IOException { private int sendAcmControlMessage(int request, int value, byte[] buf) throws IOException {

View File

@ -90,42 +90,28 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
} }
@Override @Override
public void open(UsbDeviceConnection connection) throws IOException { public void openInt(UsbDeviceConnection connection) throws IOException {
if (mConnection != null) { for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
throw new IOException("Already open"); UsbInterface usbIface = mDevice.getInterface(i);
} if (!mConnection.claimInterface(usbIface, true)) {
throw new IOException("Could not claim data interface");
mConnection = connection;
boolean opened = false;
try {
for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
UsbInterface usbIface = mDevice.getInterface(i);
if (!mConnection.claimInterface(usbIface, true)) {
throw new IOException("Could not claim data interface");
}
}
UsbInterface dataIface = mDevice.getInterface(mDevice.getInterfaceCount() - 1);
for (int i = 0; i < dataIface.getEndpointCount(); i++) {
UsbEndpoint ep = dataIface.getEndpoint(i);
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
mReadEndpoint = ep;
} else {
mWriteEndpoint = ep;
}
}
}
initialize();
setBaudRate(DEFAULT_BAUD_RATE);
opened = true;
} finally {
if (!opened) {
close();
} }
} }
UsbInterface dataIface = mDevice.getInterface(mDevice.getInterfaceCount() - 1);
for (int i = 0; i < dataIface.getEndpointCount(); i++) {
UsbEndpoint ep = dataIface.getEndpoint(i);
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
mReadEndpoint = ep;
} else {
mWriteEndpoint = ep;
}
}
}
initialize();
setBaudRate(DEFAULT_BAUD_RATE);
} }
@Override @Override

View File

@ -107,26 +107,42 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
} }
@Override @Override
public abstract void open(UsbDeviceConnection connection) throws IOException; public void open(UsbDeviceConnection connection) throws IOException {
if (mConnection != null) {
throw new IOException("Already open");
}
mConnection = connection;
try {
openInt(connection);
if (mReadEndpoint == null || mWriteEndpoint == null) {
throw new IOException("Could not get read & write endpoints");
}
mUsbRequest = new UsbRequest();
mUsbRequest.initialize(mConnection, mReadEndpoint);
} catch(Exception e) {
close();
throw e;
}
}
protected abstract void openInt(UsbDeviceConnection connection) throws IOException;
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (mConnection == null) { if (mConnection == null) {
throw new IOException("Already closed"); throw new IOException("Already closed");
} }
synchronized (this) { try {
if (mUsbRequest != null) mUsbRequest.cancel();
mUsbRequest.cancel(); } catch(Exception ignored) {}
} mUsbRequest = null;
try { try {
closeInt(); closeInt();
} catch(Exception ignored) {} } catch(Exception ignored) {}
try { try {
mConnection.close(); mConnection.close();
} finally { } catch(Exception ignored) {}
mConnection = null; mConnection = null;
}
} }
protected abstract void closeInt(); protected abstract void closeInt();
@ -150,26 +166,15 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
nread = mConnection.bulkTransfer(mReadEndpoint, dest, readMax, timeout); nread = mConnection.bulkTransfer(mReadEndpoint, dest, readMax, timeout);
} else { } else {
final UsbRequest request = new UsbRequest(); final ByteBuffer buf = ByteBuffer.wrap(dest);
try { if (!mUsbRequest.queue(buf, dest.length)) {
request.initialize(mConnection, mReadEndpoint); throw new IOException("Queueing USB request failed");
final ByteBuffer buf = ByteBuffer.wrap(dest);
if (!request.queue(buf, dest.length)) {
throw new IOException("Error queueing request");
}
mUsbRequest = request;
final UsbRequest response = mConnection.requestWait();
synchronized (this) {
mUsbRequest = null;
}
if (response == null) {
throw new IOException("Null response");
}
nread = buf.position();
} finally {
mUsbRequest = null;
request.close();
} }
final UsbRequest response = mConnection.requestWait();
if (response == null) {
throw new IOException("Waiting for USB request failed");
}
nread = buf.position();
} }
if (nread > 0) { if (nread > 0) {
return readFilter(dest, nread); return readFilter(dest, nread);

View File

@ -138,43 +138,30 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
} }
@Override @Override
public void open(UsbDeviceConnection connection) throws IOException { public void openInt(UsbDeviceConnection connection) throws IOException {
if (mConnection != null) {
throw new IOException("Already open");
}
mConnection = connection;
boolean opened = false;
mIsRestrictedPort = mDevice.getInterfaceCount() == 2 && mPortNumber == 1; mIsRestrictedPort = mDevice.getInterfaceCount() == 2 && mPortNumber == 1;
try { if(mPortNumber >= mDevice.getInterfaceCount()) {
if(mPortNumber >= mDevice.getInterfaceCount()) { throw new IOException("Unknown port number");
throw new IOException("Unknown port number"); }
} UsbInterface dataIface = mDevice.getInterface(mPortNumber);
UsbInterface dataIface = mDevice.getInterface(mPortNumber); if (!mConnection.claimInterface(dataIface, true)) {
if (!mConnection.claimInterface(dataIface, true)) { throw new IOException("Could not claim interface " + mPortNumber);
throw new IOException("Could not claim interface " + mPortNumber); }
} for (int i = 0; i < dataIface.getEndpointCount(); i++) {
for (int i = 0; i < dataIface.getEndpointCount(); i++) { UsbEndpoint ep = dataIface.getEndpoint(i);
UsbEndpoint ep = dataIface.getEndpoint(i); if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { if (ep.getDirection() == UsbConstants.USB_DIR_IN) {
if (ep.getDirection() == UsbConstants.USB_DIR_IN) { mReadEndpoint = ep;
mReadEndpoint = ep; } else {
} else { mWriteEndpoint = ep;
mWriteEndpoint = ep;
}
} }
} }
setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
// setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY);
opened = true;
} finally {
if (!opened) {
close();
}
} }
setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
// setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY);
} }
@Override @Override

View File

@ -295,32 +295,19 @@ public class FtdiSerialDriver implements UsbSerialDriver {
} }
@Override @Override
public void open(UsbDeviceConnection connection) throws IOException { public void openInt(UsbDeviceConnection connection) throws IOException {
if (mConnection != null) { if (connection.claimInterface(mDevice.getInterface(mPortNumber), true)) {
throw new IOException("Already open"); Log.d(TAG, "claimInterface " + mPortNumber + " SUCCESS");
} else {
throw new IOException("Error claiming interface " + mPortNumber);
} }
mConnection = connection; if (mDevice.getInterface(mPortNumber).getEndpointCount() < 2) {
throw new IOException("Insufficient number of endpoints (" +
boolean opened = false; mDevice.getInterface(mPortNumber).getEndpointCount() + ")");
try {
if (connection.claimInterface(mDevice.getInterface(mPortNumber), true)) {
Log.d(TAG, "claimInterface " + mPortNumber + " SUCCESS");
} else {
throw new IOException("Error claiming interface " + mPortNumber);
}
if (mDevice.getInterface(mPortNumber).getEndpointCount() < 2) {
throw new IOException("Insufficient number of endpoints (" +
mDevice.getInterface(mPortNumber).getEndpointCount() + ")");
}
mReadEndpoint = mDevice.getInterface(mPortNumber).getEndpoint(0);
mWriteEndpoint = mDevice.getInterface(mPortNumber).getEndpoint(1);
reset();
opened = true;
} finally {
if (!opened) {
close();
}
} }
mReadEndpoint = mDevice.getInterface(mPortNumber).getEndpoint(0);
mWriteEndpoint = mDevice.getInterface(mPortNumber).getEndpoint(1);
reset();
} }
@Override @Override

View File

@ -263,78 +263,63 @@ public class ProlificSerialDriver implements UsbSerialDriver {
} }
@Override @Override
public void open(UsbDeviceConnection connection) throws IOException { public void openInt(UsbDeviceConnection connection) throws IOException {
if (mConnection != null) {
throw new IOException("Already open");
}
UsbInterface usbInterface = mDevice.getInterface(0); UsbInterface usbInterface = mDevice.getInterface(0);
if (!connection.claimInterface(usbInterface, true)) { if (!connection.claimInterface(usbInterface, true)) {
throw new IOException("Error claiming Prolific interface 0"); throw new IOException("Error claiming Prolific interface 0");
} }
mConnection = connection; for (int i = 0; i < usbInterface.getEndpointCount(); ++i) {
boolean opened = false; UsbEndpoint currentEndpoint = usbInterface.getEndpoint(i);
try {
for (int i = 0; i < usbInterface.getEndpointCount(); ++i) {
UsbEndpoint currentEndpoint = usbInterface.getEndpoint(i);
switch (currentEndpoint.getAddress()) { switch (currentEndpoint.getAddress()) {
case READ_ENDPOINT: case READ_ENDPOINT:
mReadEndpoint = currentEndpoint; mReadEndpoint = currentEndpoint;
break; break;
case WRITE_ENDPOINT: case WRITE_ENDPOINT:
mWriteEndpoint = currentEndpoint; mWriteEndpoint = currentEndpoint;
break; break;
case INTERRUPT_ENDPOINT: case INTERRUPT_ENDPOINT:
mInterruptEndpoint = currentEndpoint; mInterruptEndpoint = currentEndpoint;
break; break;
}
}
if (mDevice.getDeviceClass() == 0x02) {
mDeviceType = DEVICE_TYPE_0;
} else {
try {
Method getRawDescriptorsMethod
= mConnection.getClass().getMethod("getRawDescriptors");
byte[] rawDescriptors
= (byte[]) getRawDescriptorsMethod.invoke(mConnection);
byte maxPacketSize0 = rawDescriptors[7];
if (maxPacketSize0 == 64) {
mDeviceType = DEVICE_TYPE_HX;
} else if ((mDevice.getDeviceClass() == 0x00)
|| (mDevice.getDeviceClass() == 0xff)) {
mDeviceType = DEVICE_TYPE_1;
} else {
Log.w(TAG, "Could not detect PL2303 subtype, "
+ "Assuming that it is a HX device");
mDeviceType = DEVICE_TYPE_HX;
}
} catch (NoSuchMethodException e) {
Log.w(TAG, "Method UsbDeviceConnection.getRawDescriptors, "
+ "required for PL2303 subtype detection, not "
+ "available! Assuming that it is a HX device");
mDeviceType = DEVICE_TYPE_HX;
} catch (Exception e) {
Log.e(TAG, "An unexpected exception occured while trying "
+ "to detect PL2303 subtype", e);
}
}
setControlLines(mControlLinesValue);
resetDevice();
doBlackMagic();
opened = true;
} finally {
if (!opened) {
close();
} }
} }
if (mDevice.getDeviceClass() == 0x02) {
mDeviceType = DEVICE_TYPE_0;
} else {
try {
Method getRawDescriptorsMethod
= mConnection.getClass().getMethod("getRawDescriptors");
byte[] rawDescriptors
= (byte[]) getRawDescriptorsMethod.invoke(mConnection);
byte maxPacketSize0 = rawDescriptors[7];
if (maxPacketSize0 == 64) {
mDeviceType = DEVICE_TYPE_HX;
} else if ((mDevice.getDeviceClass() == 0x00)
|| (mDevice.getDeviceClass() == 0xff)) {
mDeviceType = DEVICE_TYPE_1;
} else {
Log.w(TAG, "Could not detect PL2303 subtype, "
+ "Assuming that it is a HX device");
mDeviceType = DEVICE_TYPE_HX;
}
} catch (NoSuchMethodException e) {
Log.w(TAG, "Method UsbDeviceConnection.getRawDescriptors, "
+ "required for PL2303 subtype detection, not "
+ "available! Assuming that it is a HX device");
mDeviceType = DEVICE_TYPE_HX;
} catch (Exception e) {
Log.e(TAG, "An unexpected exception occured while trying "
+ "to detect PL2303 subtype", e);
}
}
setControlLines(mControlLinesValue);
resetDevice();
doBlackMagic();
} }
@Override @Override