mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-07 16:06:10 +00:00
refactor duplicated read/write methods
This commit is contained in:
parent
e1b62cf675
commit
5767298636
@ -26,11 +26,9 @@ 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.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
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;
|
||||||
@ -50,7 +48,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
private final UsbDevice mDevice;
|
private final UsbDevice mDevice;
|
||||||
private final UsbSerialPort mPort;
|
private final UsbSerialPort mPort;
|
||||||
private UsbRequest mUsbRequest;
|
|
||||||
|
|
||||||
public CdcAcmSerialDriver(UsbDevice device) {
|
public CdcAcmSerialDriver(UsbDevice device) {
|
||||||
mDevice = device;
|
mDevice = device;
|
||||||
@ -73,8 +70,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
private UsbInterface mDataInterface;
|
private UsbInterface mDataInterface;
|
||||||
|
|
||||||
private UsbEndpoint mControlEndpoint;
|
private UsbEndpoint mControlEndpoint;
|
||||||
private UsbEndpoint mReadEndpoint;
|
|
||||||
private UsbEndpoint mWriteEndpoint;
|
|
||||||
|
|
||||||
private int mControlIndex;
|
private int mControlIndex;
|
||||||
|
|
||||||
@ -266,78 +261,6 @@ public class CdcAcmSerialDriver implements UsbSerialDriver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
|
||||||
if(mConnection == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
mUsbRequest = request;
|
|
||||||
final UsbRequest response = mConnection.requestWait();
|
|
||||||
synchronized (this) {
|
|
||||||
mUsbRequest = null;
|
|
||||||
}
|
|
||||||
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 {
|
|
||||||
mUsbRequest = null;
|
|
||||||
request.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int write(byte[] src, int timeoutMillis) throws IOException {
|
|
||||||
// TODO(mikey): Nearly identical to FtdiSerial write. Refactor.
|
|
||||||
int offset = 0;
|
|
||||||
|
|
||||||
if(mConnection == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
while (offset < src.length) {
|
|
||||||
final int writeLength;
|
|
||||||
final int amtWritten;
|
|
||||||
|
|
||||||
synchronized (mWriteBufferLock) {
|
|
||||||
final byte[] writeBuffer;
|
|
||||||
|
|
||||||
writeLength = Math.min(src.length - offset, mWriteBuffer.length);
|
|
||||||
if (offset == 0) {
|
|
||||||
writeBuffer = src;
|
|
||||||
} else {
|
|
||||||
// bulkTransfer does not support offsets, make a copy.
|
|
||||||
System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
|
|
||||||
writeBuffer = mWriteBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
amtWritten = mConnection.bulkTransfer(mWriteEndpoint, writeBuffer, writeLength,
|
|
||||||
timeoutMillis);
|
|
||||||
}
|
|
||||||
if (amtWritten <= 0) {
|
|
||||||
throw new IOException("Error writing " + writeLength
|
|
||||||
+ " bytes at offset " + offset + " length=" + src.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d(TAG, "Wrote amt=" + amtWritten + " attempted=" + writeLength);
|
|
||||||
offset += amtWritten;
|
|
||||||
}
|
|
||||||
return offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
|
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
|
||||||
if(baudRate <= 0) {
|
if(baudRate <= 0) {
|
||||||
|
@ -25,11 +25,8 @@ 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.util.Log;
|
|
||||||
|
|
||||||
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;
|
||||||
@ -83,10 +80,6 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
|||||||
private boolean dtr = false;
|
private boolean dtr = false;
|
||||||
private boolean rts = false;
|
private boolean rts = false;
|
||||||
|
|
||||||
private UsbEndpoint mReadEndpoint;
|
|
||||||
private UsbEndpoint mWriteEndpoint;
|
|
||||||
private UsbRequest mUsbRequest;
|
|
||||||
|
|
||||||
public Ch340SerialPort(UsbDevice device, int portNumber) {
|
public Ch340SerialPort(UsbDevice device, int portNumber) {
|
||||||
super(device, portNumber);
|
super(device, portNumber);
|
||||||
}
|
}
|
||||||
@ -155,78 +148,6 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
|
||||||
if(mConnection == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
mUsbRequest = request;
|
|
||||||
final UsbRequest response = mConnection.requestWait();
|
|
||||||
synchronized (this) {
|
|
||||||
mUsbRequest = null;
|
|
||||||
}
|
|
||||||
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 {
|
|
||||||
mUsbRequest = null;
|
|
||||||
request.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int write(byte[] src, int timeoutMillis) throws IOException {
|
|
||||||
int offset = 0;
|
|
||||||
|
|
||||||
if(mConnection == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
while (offset < src.length) {
|
|
||||||
final int writeLength;
|
|
||||||
final int amtWritten;
|
|
||||||
|
|
||||||
synchronized (mWriteBufferLock) {
|
|
||||||
final byte[] writeBuffer;
|
|
||||||
|
|
||||||
writeLength = Math.min(src.length - offset, mWriteBuffer.length);
|
|
||||||
if (offset == 0) {
|
|
||||||
writeBuffer = src;
|
|
||||||
} else {
|
|
||||||
// bulkTransfer does not support offsets, make a copy.
|
|
||||||
System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
|
|
||||||
writeBuffer = mWriteBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
amtWritten = mConnection.bulkTransfer(mWriteEndpoint, writeBuffer, writeLength,
|
|
||||||
timeoutMillis);
|
|
||||||
}
|
|
||||||
if (amtWritten <= 0) {
|
|
||||||
throw new IOException("Error writing " + writeLength
|
|
||||||
+ " bytes at offset " + offset + " length=" + src.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d(TAG, "Wrote amt=" + amtWritten + " attempted=" + writeLength);
|
|
||||||
offset += amtWritten;
|
|
||||||
}
|
|
||||||
return offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int controlOut(int request, int value, int index) {
|
private int controlOut(int request, int value, int index) {
|
||||||
final int REQTYPE_HOST_TO_DEVICE = 0x41;
|
final int REQTYPE_HOST_TO_DEVICE = 0x41;
|
||||||
return mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, request,
|
return mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, request,
|
||||||
|
@ -23,8 +23,12 @@ package com.hoho.android.usbserial.driver;
|
|||||||
|
|
||||||
import android.hardware.usb.UsbDevice;
|
import android.hardware.usb.UsbDevice;
|
||||||
import android.hardware.usb.UsbDeviceConnection;
|
import android.hardware.usb.UsbDeviceConnection;
|
||||||
|
import android.hardware.usb.UsbEndpoint;
|
||||||
|
import android.hardware.usb.UsbRequest;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A base class shared by several driver implementations.
|
* A base class shared by several driver implementations.
|
||||||
@ -33,16 +37,19 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
||||||
|
|
||||||
public static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
|
private static final String TAG = CommonUsbSerialPort.class.getSimpleName();
|
||||||
|
private static final int DEFAULT_WRITE_BUFFER_SIZE = 16 * 1024;
|
||||||
|
|
||||||
protected final UsbDevice mDevice;
|
protected final UsbDevice mDevice;
|
||||||
protected final int mPortNumber;
|
protected final int mPortNumber;
|
||||||
|
|
||||||
// non-null when open()
|
// non-null when open()
|
||||||
protected UsbDeviceConnection mConnection = null;
|
protected UsbDeviceConnection mConnection = null;
|
||||||
|
protected UsbEndpoint mReadEndpoint;
|
||||||
|
protected UsbEndpoint mWriteEndpoint;
|
||||||
|
protected UsbRequest mUsbRequest;
|
||||||
|
|
||||||
protected final Object mWriteBufferLock = new Object();
|
protected final Object mWriteBufferLock = new Object();
|
||||||
|
|
||||||
/** Internal write buffer. Guarded by {@link #mWriteBufferLock}. */
|
/** Internal write buffer. Guarded by {@link #mWriteBufferLock}. */
|
||||||
protected byte[] mWriteBuffer;
|
protected byte[] mWriteBuffer;
|
||||||
|
|
||||||
@ -52,7 +59,7 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
|
|
||||||
mWriteBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE];
|
mWriteBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("<%s device_name=%s device_id=%s port_number=%s>",
|
return String.format("<%s device_name=%s device_id=%s port_number=%s>",
|
||||||
@ -105,10 +112,76 @@ public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
|||||||
public abstract void close() throws IOException;
|
public abstract void close() throws IOException;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract int read(final byte[] dest, final int timeoutMillis) throws IOException;
|
public int read(final byte[] dest, final int timeoutMillis) throws IOException {
|
||||||
|
if(mConnection == null) {
|
||||||
|
throw new IOException("Connection closed");
|
||||||
|
}
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
mUsbRequest = request;
|
||||||
|
final UsbRequest response = mConnection.requestWait();
|
||||||
|
synchronized (this) {
|
||||||
|
mUsbRequest = null;
|
||||||
|
}
|
||||||
|
if (response == null) {
|
||||||
|
throw new IOException("Null response");
|
||||||
|
}
|
||||||
|
final int nread = buf.position();
|
||||||
|
if (nread > 0) {
|
||||||
|
return readFilter(dest, nread);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
mUsbRequest = null;
|
||||||
|
request.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int readFilter(final byte[] buffer, int len) throws IOException { return len; }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract int write(final byte[] src, final int timeoutMillis) throws IOException;
|
public int write(final byte[] src, final int timeoutMillis) throws IOException {
|
||||||
|
int offset = 0;
|
||||||
|
|
||||||
|
if(mConnection == null) {
|
||||||
|
throw new IOException("Connection closed");
|
||||||
|
}
|
||||||
|
while (offset < src.length) {
|
||||||
|
final int writeLength;
|
||||||
|
final int amtWritten;
|
||||||
|
|
||||||
|
synchronized (mWriteBufferLock) {
|
||||||
|
final byte[] writeBuffer;
|
||||||
|
|
||||||
|
writeLength = Math.min(src.length - offset, mWriteBuffer.length);
|
||||||
|
if (offset == 0) {
|
||||||
|
writeBuffer = src;
|
||||||
|
} else {
|
||||||
|
// bulkTransfer does not support offsets, make a copy.
|
||||||
|
System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
|
||||||
|
writeBuffer = mWriteBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
amtWritten = mConnection.bulkTransfer(mWriteEndpoint, writeBuffer, writeLength,
|
||||||
|
timeoutMillis);
|
||||||
|
}
|
||||||
|
if (amtWritten <= 0) {
|
||||||
|
throw new IOException("Error writing " + writeLength
|
||||||
|
+ " bytes at offset " + offset + " length=" + src.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d(TAG, "Wrote amt=" + amtWritten + " attempted=" + writeLength);
|
||||||
|
offset += amtWritten;
|
||||||
|
}
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@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, int parity) throws IOException;
|
||||||
|
@ -26,11 +26,8 @@ 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.util.Log;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -106,10 +103,6 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||||||
private static final int CONTROL_WRITE_DTR = 0x0100;
|
private static final int CONTROL_WRITE_DTR = 0x0100;
|
||||||
private static final int CONTROL_WRITE_RTS = 0x0200;
|
private static final int CONTROL_WRITE_RTS = 0x0200;
|
||||||
|
|
||||||
private UsbEndpoint mReadEndpoint;
|
|
||||||
private UsbEndpoint mWriteEndpoint;
|
|
||||||
private UsbRequest mUsbRequest;
|
|
||||||
|
|
||||||
// second port of Cp2105 has limited baudRate, dataBits, stopBits, parity
|
// second port of Cp2105 has limited baudRate, dataBits, stopBits, parity
|
||||||
// unsupported baudrate returns error at controlTransfer(), other parameters are silently ignored
|
// unsupported baudrate returns error at controlTransfer(), other parameters are silently ignored
|
||||||
private boolean mIsRestrictedPort;
|
private boolean mIsRestrictedPort;
|
||||||
@ -195,77 +188,6 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
|
||||||
if(mConnection == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
mUsbRequest = request;
|
|
||||||
final UsbRequest response = mConnection.requestWait();
|
|
||||||
synchronized (this) {
|
|
||||||
mUsbRequest = null;
|
|
||||||
}
|
|
||||||
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 {
|
|
||||||
mUsbRequest = null;
|
|
||||||
request.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int write(byte[] src, int timeoutMillis) throws IOException {
|
|
||||||
int offset = 0;
|
|
||||||
|
|
||||||
if(mConnection == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
while (offset < src.length) {
|
|
||||||
final int writeLength;
|
|
||||||
final int amtWritten;
|
|
||||||
|
|
||||||
synchronized (mWriteBufferLock) {
|
|
||||||
final byte[] writeBuffer;
|
|
||||||
|
|
||||||
writeLength = Math.min(src.length - offset, mWriteBuffer.length);
|
|
||||||
if (offset == 0) {
|
|
||||||
writeBuffer = src;
|
|
||||||
} else {
|
|
||||||
// bulkTransfer does not support offsets, make a copy.
|
|
||||||
System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
|
|
||||||
writeBuffer = mWriteBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
amtWritten = mConnection.bulkTransfer(mWriteEndpoint, writeBuffer, writeLength,
|
|
||||||
timeoutMillis);
|
|
||||||
}
|
|
||||||
if (amtWritten <= 0) {
|
|
||||||
throw new IOException("Error writing " + writeLength
|
|
||||||
+ " bytes at offset " + offset + " length=" + src.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d(TAG, "Wrote amt=" + amtWritten + " attempted=" + writeLength);
|
|
||||||
offset += amtWritten;
|
|
||||||
}
|
|
||||||
return offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setBaudRate(int baudRate) throws IOException {
|
private void setBaudRate(int baudRate) throws IOException {
|
||||||
byte[] data = new byte[] {
|
byte[] data = new byte[] {
|
||||||
(byte) ( baudRate & 0xff),
|
(byte) ( baudRate & 0xff),
|
||||||
|
@ -24,12 +24,9 @@ package com.hoho.android.usbserial.driver;
|
|||||||
import android.hardware.usb.UsbConstants;
|
import android.hardware.usb.UsbConstants;
|
||||||
import android.hardware.usb.UsbDevice;
|
import android.hardware.usb.UsbDevice;
|
||||||
import android.hardware.usb.UsbDeviceConnection;
|
import android.hardware.usb.UsbDeviceConnection;
|
||||||
import android.hardware.usb.UsbEndpoint;
|
|
||||||
import android.hardware.usb.UsbRequest;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -196,31 +193,32 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter FTDI status bytes from buffer
|
* Filter FTDI status bytes from buffer
|
||||||
* @param src The source buffer (which contains status bytes)
|
* @param buffer The source buffer (which contains status bytes)
|
||||||
* @param dest The destination buffer to write the status bytes into (can be src)
|
* buffer The destination buffer to write the status bytes into (can be src)
|
||||||
* @param totalBytesRead Number of bytes read to src
|
* @param totalBytesRead Number of bytes read to src
|
||||||
* @param maxPacketSize The USB endpoint max packet size
|
|
||||||
* @return The number of payload bytes
|
* @return The number of payload bytes
|
||||||
*/
|
*/
|
||||||
private final int filterStatusBytes(byte[] src, byte[] dest, int totalBytesRead, int maxPacketSize) {
|
@Override
|
||||||
|
protected int readFilter(byte[] buffer, int totalBytesRead) throws IOException {
|
||||||
|
if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH) {
|
||||||
|
throw new IOException("Expected at least " + MODEM_STATUS_HEADER_LENGTH + " bytes");
|
||||||
|
}
|
||||||
|
int maxPacketSize = mReadEndpoint.getMaxPacketSize();
|
||||||
final int packetsCount = (totalBytesRead + maxPacketSize -1 )/ maxPacketSize;
|
final int packetsCount = (totalBytesRead + maxPacketSize -1 )/ maxPacketSize;
|
||||||
for (int packetIdx = 0; packetIdx < packetsCount; ++packetIdx) {
|
for (int packetIdx = 0; packetIdx < packetsCount; ++packetIdx) {
|
||||||
final int count = (packetIdx == (packetsCount - 1))
|
final int count = (packetIdx == (packetsCount - 1))
|
||||||
? totalBytesRead - packetIdx * maxPacketSize - MODEM_STATUS_HEADER_LENGTH
|
? totalBytesRead - packetIdx * maxPacketSize - MODEM_STATUS_HEADER_LENGTH
|
||||||
: maxPacketSize - MODEM_STATUS_HEADER_LENGTH;
|
: maxPacketSize - MODEM_STATUS_HEADER_LENGTH;
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
System.arraycopy(src,
|
System.arraycopy(buffer, packetIdx * maxPacketSize + MODEM_STATUS_HEADER_LENGTH,
|
||||||
packetIdx * maxPacketSize + MODEM_STATUS_HEADER_LENGTH,
|
buffer, packetIdx * (maxPacketSize - MODEM_STATUS_HEADER_LENGTH),
|
||||||
dest,
|
count);
|
||||||
packetIdx * (maxPacketSize - MODEM_STATUS_HEADER_LENGTH),
|
|
||||||
count);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return totalBytesRead - (packetsCount * 2);
|
||||||
return totalBytesRead - (packetsCount * 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset() throws IOException {
|
void reset() throws IOException {
|
||||||
// TODO(mikey): autodetect.
|
// TODO(mikey): autodetect.
|
||||||
mType = DeviceType.TYPE_R;
|
mType = DeviceType.TYPE_R;
|
||||||
if(mDevice.getInterfaceCount() > 1) {
|
if(mDevice.getInterfaceCount() > 1) {
|
||||||
@ -252,6 +250,12 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||||||
} else {
|
} else {
|
||||||
throw new IOException("Error claiming interface " + mPortNumber);
|
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();
|
reset();
|
||||||
opened = true;
|
opened = true;
|
||||||
} finally {
|
} finally {
|
||||||
@ -276,75 +280,6 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
|
||||||
if(mConnection == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
final UsbEndpoint endpoint = mDevice.getInterface(mPortNumber).getEndpoint(0);
|
|
||||||
final UsbRequest request = new UsbRequest();
|
|
||||||
final ByteBuffer buf = ByteBuffer.wrap(dest);
|
|
||||||
try {
|
|
||||||
request.initialize(mConnection, endpoint);
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
request.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
final int totalBytesRead = buf.position();
|
|
||||||
if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH) {
|
|
||||||
throw new IOException("Expected at least " + MODEM_STATUS_HEADER_LENGTH + " bytes");
|
|
||||||
}
|
|
||||||
|
|
||||||
return filterStatusBytes(dest, dest, totalBytesRead, endpoint.getMaxPacketSize());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int write(byte[] src, int timeoutMillis) throws IOException {
|
|
||||||
if(mConnection == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
final UsbEndpoint endpoint = mDevice.getInterface(mPortNumber).getEndpoint(1);
|
|
||||||
int offset = 0;
|
|
||||||
|
|
||||||
while (offset < src.length) {
|
|
||||||
final int writeLength;
|
|
||||||
final int amtWritten;
|
|
||||||
|
|
||||||
synchronized (mWriteBufferLock) {
|
|
||||||
final byte[] writeBuffer;
|
|
||||||
|
|
||||||
writeLength = Math.min(src.length - offset, mWriteBuffer.length);
|
|
||||||
if (offset == 0) {
|
|
||||||
writeBuffer = src;
|
|
||||||
} else {
|
|
||||||
// bulkTransfer does not support offsets, make a copy.
|
|
||||||
System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
|
|
||||||
writeBuffer = mWriteBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
amtWritten = mConnection.bulkTransfer(endpoint, writeBuffer, writeLength,
|
|
||||||
timeoutMillis);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (amtWritten <= 0) {
|
|
||||||
throw new IOException("Error writing " + writeLength
|
|
||||||
+ " bytes at offset " + offset + " length=" + src.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.d(TAG, "Wrote amtWritten=" + amtWritten + " attempted=" + writeLength);
|
|
||||||
offset += amtWritten;
|
|
||||||
}
|
|
||||||
return offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int setBaudRate(int baudRate) throws IOException {
|
private int setBaudRate(int baudRate) throws IOException {
|
||||||
long[] vals = convertBaudrate(baudRate);
|
long[] vals = convertBaudrate(baudRate);
|
||||||
long actualBaudrate = vals[0];
|
long actualBaudrate = vals[0];
|
||||||
|
@ -32,12 +32,10 @@ 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.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
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;
|
||||||
@ -111,8 +109,6 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||||||
|
|
||||||
private int mDeviceType = DEVICE_TYPE_HX;
|
private int mDeviceType = DEVICE_TYPE_HX;
|
||||||
|
|
||||||
private UsbEndpoint mReadEndpoint;
|
|
||||||
private UsbEndpoint mWriteEndpoint;
|
|
||||||
private UsbEndpoint mInterruptEndpoint;
|
private UsbEndpoint mInterruptEndpoint;
|
||||||
|
|
||||||
private int mControlLinesValue = 0;
|
private int mControlLinesValue = 0;
|
||||||
@ -370,74 +366,6 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
|
||||||
if(mConnection == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int write(byte[] src, int timeoutMillis) throws IOException {
|
|
||||||
int offset = 0;
|
|
||||||
|
|
||||||
if(mConnection == null) {
|
|
||||||
throw new IOException("Connection closed");
|
|
||||||
}
|
|
||||||
while (offset < src.length) {
|
|
||||||
final int writeLength;
|
|
||||||
final int amtWritten;
|
|
||||||
|
|
||||||
synchronized (mWriteBufferLock) {
|
|
||||||
final byte[] writeBuffer;
|
|
||||||
|
|
||||||
writeLength = Math.min(src.length - offset, mWriteBuffer.length);
|
|
||||||
if (offset == 0) {
|
|
||||||
writeBuffer = src;
|
|
||||||
} else {
|
|
||||||
// bulkTransfer does not support offsets, make a copy.
|
|
||||||
System.arraycopy(src, offset, mWriteBuffer, 0, writeLength);
|
|
||||||
writeBuffer = mWriteBuffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
amtWritten = mConnection.bulkTransfer(mWriteEndpoint,
|
|
||||||
writeBuffer, writeLength, timeoutMillis);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (amtWritten <= 0) {
|
|
||||||
throw new IOException("Error writing " + writeLength
|
|
||||||
+ " bytes at offset " + offset + " length="
|
|
||||||
+ src.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
offset += amtWritten;
|
|
||||||
}
|
|
||||||
return offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
|
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
|
||||||
if ((mBaudRate == baudRate) && (mDataBits == dataBits)
|
if ((mBaudRate == baudRate) && (mDataBits == dataBits)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user