mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-07 16:06:10 +00:00
cdc: Add async read capability.
This commit is contained in:
parent
95592f984a
commit
8e8ded4a9c
@ -26,9 +26,14 @@ import android.hardware.usb.UsbDevice;
|
|||||||
import android.hardware.usb.UsbDeviceConnection;
|
import android.hardware.usb.UsbDeviceConnection;
|
||||||
import android.hardware.usb.UsbEndpoint;
|
import android.hardware.usb.UsbEndpoint;
|
||||||
import android.hardware.usb.UsbInterface;
|
import android.hardware.usb.UsbInterface;
|
||||||
|
import android.hardware.usb.UsbRequest;
|
||||||
|
import android.os.Build;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.hoho.android.usbserial.util.HexDump;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -66,6 +71,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
class CdcAcmSerialPort extends CommonUsbSerialPort {
|
class CdcAcmSerialPort extends CommonUsbSerialPort {
|
||||||
|
|
||||||
|
private final boolean mEnableAsyncReads;
|
||||||
private UsbInterface mControlInterface;
|
private UsbInterface mControlInterface;
|
||||||
private UsbInterface mDataInterface;
|
private UsbInterface mDataInterface;
|
||||||
|
|
||||||
@ -86,6 +92,7 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
public CdcAcmSerialPort(UsbDevice device, int portNumber) {
|
public CdcAcmSerialPort(UsbDevice device, int portNumber) {
|
||||||
super(device, portNumber);
|
super(device, portNumber);
|
||||||
|
mEnableAsyncReads = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -125,6 +132,11 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection());
|
Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection());
|
||||||
mWriteEndpoint = mDataInterface.getEndpoint(0);
|
mWriteEndpoint = mDataInterface.getEndpoint(0);
|
||||||
Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection());
|
Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection());
|
||||||
|
if (mEnableAsyncReads) {
|
||||||
|
Log.d(TAG, "Async reads enabled");
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "Async reads disabled.");
|
||||||
|
}
|
||||||
opened = true;
|
opened = true;
|
||||||
} finally {
|
} finally {
|
||||||
if (!opened) {
|
if (!opened) {
|
||||||
@ -149,6 +161,32 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
||||||
|
if (mEnableAsyncReads) {
|
||||||
|
final UsbRequest request = new UsbRequest();
|
||||||
|
try {
|
||||||
|
request.initialize(mConnection, mReadEndpoint);
|
||||||
|
final ByteBuffer buf = ByteBuffer.wrap(dest);
|
||||||
|
if (!request.queue(buf, dest.length)) {
|
||||||
|
throw new IOException("Error queueing request.");
|
||||||
|
}
|
||||||
|
|
||||||
|
final UsbRequest response = mConnection.requestWait();
|
||||||
|
if (response == null) {
|
||||||
|
throw new IOException("Null response");
|
||||||
|
}
|
||||||
|
|
||||||
|
final int nread = buf.position();
|
||||||
|
if (nread > 0) {
|
||||||
|
//Log.d(TAG, HexDump.dumpHexString(dest, 0, Math.min(32, dest.length)));
|
||||||
|
return nread;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
request.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final int numBytesRead;
|
final int numBytesRead;
|
||||||
synchronized (mReadBufferLock) {
|
synchronized (mReadBufferLock) {
|
||||||
int readAmt = Math.min(dest.length, mReadBuffer.length);
|
int readAmt = Math.min(dest.length, mReadBuffer.length);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user