mirror of
https://github.com/mik3y/usb-serial-for-android.git
synced 2026-08-18 11:53:09 +00:00
Merge branch 'master' of https://github.com/mik3y/usb-serial-for-android
This commit is contained in:
+368
-445
@@ -1,445 +1,368 @@
|
||||
/* Copyright 2011-2013 Google Inc.
|
||||
* Copyright 2013 mike wakerly <opensource@hoho.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
* USA.
|
||||
*
|
||||
* Project home page: https://github.com/mik3y/usb-serial-for-android
|
||||
*/
|
||||
|
||||
package com.hoho.android.usbserial.driver;
|
||||
|
||||
import android.hardware.usb.UsbConstants;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbEndpoint;
|
||||
import android.hardware.usb.UsbInterface;
|
||||
import android.hardware.usb.UsbRequest;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* USB CDC/ACM serial driver implementation.
|
||||
*
|
||||
* @author mike wakerly (opensource@hoho.com)
|
||||
* @see <a
|
||||
* href="http://www.usb.org/developers/devclass_docs/usbcdc11.pdf">Universal
|
||||
* Serial Bus Class Definitions for Communication Devices, v1.1</a>
|
||||
*/
|
||||
public class CdcAcmSerialDriver implements UsbSerialDriver {
|
||||
|
||||
private final String TAG = CdcAcmSerialDriver.class.getSimpleName();
|
||||
|
||||
private final UsbDevice mDevice;
|
||||
private final UsbSerialPort mPort;
|
||||
private UsbRequest mUsbRequest;
|
||||
|
||||
public CdcAcmSerialDriver(UsbDevice device) {
|
||||
mDevice = device;
|
||||
mPort = new CdcAcmSerialPort(device, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UsbDevice getDevice() {
|
||||
return mDevice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UsbSerialPort> getPorts() {
|
||||
return Collections.singletonList(mPort);
|
||||
}
|
||||
|
||||
class CdcAcmSerialPort extends CommonUsbSerialPort {
|
||||
|
||||
private UsbInterface mControlInterface;
|
||||
private UsbInterface mDataInterface;
|
||||
|
||||
private UsbEndpoint mControlEndpoint;
|
||||
private UsbEndpoint mReadEndpoint;
|
||||
private UsbEndpoint mWriteEndpoint;
|
||||
|
||||
private int mControlIndex;
|
||||
|
||||
private boolean mRts = false;
|
||||
private boolean mDtr = false;
|
||||
|
||||
private static final int USB_RECIP_INTERFACE = 0x01;
|
||||
private static final int USB_RT_ACM = UsbConstants.USB_TYPE_CLASS | USB_RECIP_INTERFACE;
|
||||
|
||||
private static final int SET_LINE_CODING = 0x20; // USB CDC 1.1 section 6.2
|
||||
private static final int GET_LINE_CODING = 0x21;
|
||||
private static final int SET_CONTROL_LINE_STATE = 0x22;
|
||||
private static final int SEND_BREAK = 0x23;
|
||||
|
||||
public CdcAcmSerialPort(UsbDevice device, int portNumber) {
|
||||
super(device, portNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UsbSerialDriver getDriver() {
|
||||
return CdcAcmSerialDriver.this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open(UsbDeviceConnection connection) throws IOException {
|
||||
if (mConnection != null) {
|
||||
throw new IOException("Already open");
|
||||
}
|
||||
|
||||
mConnection = connection;
|
||||
boolean opened = false;
|
||||
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) {
|
||||
mConnection = null;
|
||||
// just to be on the save side
|
||||
mControlEndpoint = null;
|
||||
mReadEndpoint = null;
|
||||
mWriteEndpoint = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void openSingleInterface() throws IOException {
|
||||
// the following code is inspired by the cdc-acm driver
|
||||
// in the linux kernel
|
||||
|
||||
mControlIndex = 0;
|
||||
mControlInterface = mDevice.getInterface(0);
|
||||
Log.d(TAG, "Control iface=" + mControlInterface);
|
||||
|
||||
mDataInterface = mDevice.getInterface(0);
|
||||
Log.d(TAG, "data iface=" + mDataInterface);
|
||||
|
||||
if (!mConnection.claimInterface(mControlInterface, true)) {
|
||||
throw new IOException("Could not claim shared control/data interface.");
|
||||
}
|
||||
|
||||
int endCount = mControlInterface.getEndpointCount();
|
||||
|
||||
if (endCount < 3) {
|
||||
Log.d(TAG,"not enough endpoints - need 3. count=" + mControlInterface.getEndpointCount());
|
||||
throw new IOException("Insufficient number of endpoints(" + mControlInterface.getEndpointCount() + ")");
|
||||
}
|
||||
|
||||
// Analyse endpoints for their properties
|
||||
mControlEndpoint = null;
|
||||
mReadEndpoint = null;
|
||||
mWriteEndpoint = null;
|
||||
for (int i = 0; i < endCount; ++i) {
|
||||
UsbEndpoint ep = mControlInterface.getEndpoint(i);
|
||||
if ((ep.getDirection() == UsbConstants.USB_DIR_IN) &&
|
||||
(ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)) {
|
||||
Log.d(TAG,"Found controlling endpoint");
|
||||
mControlEndpoint = ep;
|
||||
} else if ((ep.getDirection() == UsbConstants.USB_DIR_IN) &&
|
||||
(ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
|
||||
Log.d(TAG,"Found reading endpoint");
|
||||
mReadEndpoint = ep;
|
||||
} else if ((ep.getDirection() == UsbConstants.USB_DIR_OUT) &&
|
||||
(ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
|
||||
Log.d(TAG,"Found writing endpoint");
|
||||
mWriteEndpoint = ep;
|
||||
}
|
||||
|
||||
|
||||
if ((mControlEndpoint != null) &&
|
||||
(mReadEndpoint != null) &&
|
||||
(mWriteEndpoint != null)) {
|
||||
Log.d(TAG,"Found all required endpoints");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((mControlEndpoint == null) ||
|
||||
(mReadEndpoint == null) ||
|
||||
(mWriteEndpoint == null)) {
|
||||
Log.d(TAG,"Could not establish all endpoints");
|
||||
throw new IOException("Could not establish all endpoints");
|
||||
}
|
||||
}
|
||||
|
||||
private void openInterface() throws IOException {
|
||||
Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount());
|
||||
|
||||
mControlInterface = null;
|
||||
mDataInterface = null;
|
||||
for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
|
||||
UsbInterface usbInterface = mDevice.getInterface(i);
|
||||
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_COMM) {
|
||||
mControlIndex = i;
|
||||
mControlInterface = usbInterface;
|
||||
}
|
||||
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
|
||||
mDataInterface = usbInterface;
|
||||
}
|
||||
}
|
||||
|
||||
if(mControlInterface == null) {
|
||||
throw new IOException("no control interface.");
|
||||
}
|
||||
Log.d(TAG, "Control iface=" + mControlInterface);
|
||||
|
||||
if (!mConnection.claimInterface(mControlInterface, true)) {
|
||||
throw new IOException("Could not claim control interface.");
|
||||
}
|
||||
|
||||
mControlEndpoint = mControlInterface.getEndpoint(0);
|
||||
if (mControlEndpoint.getDirection() != UsbConstants.USB_DIR_IN || mControlEndpoint.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
|
||||
throw new IOException("invalid control endpoint");
|
||||
}
|
||||
|
||||
if(mDataInterface == null) {
|
||||
throw new IOException("no data interface.");
|
||||
}
|
||||
Log.d(TAG, "data iface=" + mDataInterface);
|
||||
|
||||
if (!mConnection.claimInterface(mDataInterface, true)) {
|
||||
throw new IOException("Could not claim data interface.");
|
||||
}
|
||||
|
||||
mReadEndpoint = null;
|
||||
mWriteEndpoint = null;
|
||||
for (int i = 0; i < mDataInterface.getEndpointCount(); i++) {
|
||||
UsbEndpoint ep = mDataInterface.getEndpoint(i);
|
||||
if (ep.getDirection() == UsbConstants.USB_DIR_IN && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
|
||||
mReadEndpoint = ep;
|
||||
if (ep.getDirection() == UsbConstants.USB_DIR_OUT && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
|
||||
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 {
|
||||
int len = mConnection.controlTransfer(
|
||||
USB_RT_ACM, request, value, mControlIndex, buf, buf != null ? buf.length : 0, 5000);
|
||||
if(len < 0) {
|
||||
throw new IOException("controlTransfer failed.");
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (mConnection == null) {
|
||||
throw new IOException("Already closed");
|
||||
}
|
||||
synchronized (this) {
|
||||
if (mUsbRequest != null)
|
||||
mUsbRequest.cancel();
|
||||
}
|
||||
mConnection.close();
|
||||
mConnection = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
||||
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;
|
||||
|
||||
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
|
||||
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
|
||||
byte stopBitsByte;
|
||||
switch (stopBits) {
|
||||
case STOPBITS_1: stopBitsByte = 0; break;
|
||||
case STOPBITS_1_5: stopBitsByte = 1; break;
|
||||
case STOPBITS_2: stopBitsByte = 2; break;
|
||||
default: throw new IllegalArgumentException("Bad value for stopBits: " + stopBits);
|
||||
}
|
||||
|
||||
byte parityBitesByte;
|
||||
switch (parity) {
|
||||
case PARITY_NONE: parityBitesByte = 0; break;
|
||||
case PARITY_ODD: parityBitesByte = 1; break;
|
||||
case PARITY_EVEN: parityBitesByte = 2; break;
|
||||
case PARITY_MARK: parityBitesByte = 3; break;
|
||||
case PARITY_SPACE: parityBitesByte = 4; break;
|
||||
default: throw new IllegalArgumentException("Bad value for parity: " + parity);
|
||||
}
|
||||
|
||||
byte[] msg = {
|
||||
(byte) ( baudRate & 0xff),
|
||||
(byte) ((baudRate >> 8 ) & 0xff),
|
||||
(byte) ((baudRate >> 16) & 0xff),
|
||||
(byte) ((baudRate >> 24) & 0xff),
|
||||
stopBitsByte,
|
||||
parityBitesByte,
|
||||
(byte) dataBits};
|
||||
sendAcmControlMessage(SET_LINE_CODING, 0, msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getCD() throws IOException {
|
||||
return false; // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getCTS() throws IOException {
|
||||
return false; // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getDSR() throws IOException {
|
||||
return false; // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getDTR() throws IOException {
|
||||
return mDtr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDTR(boolean value) throws IOException {
|
||||
mDtr = value;
|
||||
setDtrRts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getRI() throws IOException {
|
||||
return false; // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getRTS() throws IOException {
|
||||
return mRts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRTS(boolean value) throws IOException {
|
||||
mRts = value;
|
||||
setDtrRts();
|
||||
}
|
||||
|
||||
private void setDtrRts() throws IOException {
|
||||
int value = (mRts ? 0x2 : 0) | (mDtr ? 0x1 : 0);
|
||||
sendAcmControlMessage(SET_CONTROL_LINE_STATE, value, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Map<Integer, int[]> getSupportedDevices() {
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>();
|
||||
supportedDevices.put(UsbId.VENDOR_ARDUINO,
|
||||
new int[] {
|
||||
UsbId.ARDUINO_UNO,
|
||||
UsbId.ARDUINO_UNO_R3,
|
||||
UsbId.ARDUINO_MEGA_2560,
|
||||
UsbId.ARDUINO_MEGA_2560_R3,
|
||||
UsbId.ARDUINO_SERIAL_ADAPTER,
|
||||
UsbId.ARDUINO_SERIAL_ADAPTER_R3,
|
||||
UsbId.ARDUINO_MEGA_ADK,
|
||||
UsbId.ARDUINO_MEGA_ADK_R3,
|
||||
UsbId.ARDUINO_LEONARDO,
|
||||
UsbId.ARDUINO_MICRO,
|
||||
});
|
||||
supportedDevices.put(UsbId.VENDOR_VAN_OOIJEN_TECH,
|
||||
new int[] {
|
||||
UsbId.VAN_OOIJEN_TECH_TEENSYDUINO_SERIAL,
|
||||
});
|
||||
supportedDevices.put(UsbId.VENDOR_ATMEL,
|
||||
new int[] {
|
||||
UsbId.ATMEL_LUFA_CDC_DEMO_APP,
|
||||
});
|
||||
supportedDevices.put(UsbId.VENDOR_LEAFLABS,
|
||||
new int[] {
|
||||
UsbId.LEAFLABS_MAPLE,
|
||||
});
|
||||
supportedDevices.put(UsbId.VENDOR_ARM,
|
||||
new int[] {
|
||||
UsbId.ARM_MBED,
|
||||
});
|
||||
return supportedDevices;
|
||||
}
|
||||
|
||||
}
|
||||
/* Copyright 2011-2013 Google Inc.
|
||||
* Copyright 2013 mike wakerly <opensource@hoho.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
* USA.
|
||||
*
|
||||
* Project home page: https://github.com/mik3y/usb-serial-for-android
|
||||
*/
|
||||
|
||||
package com.hoho.android.usbserial.driver;
|
||||
|
||||
import android.hardware.usb.UsbConstants;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbEndpoint;
|
||||
import android.hardware.usb.UsbInterface;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* USB CDC/ACM serial driver implementation.
|
||||
*
|
||||
* @author mike wakerly (opensource@hoho.com)
|
||||
* @see <a
|
||||
* href="http://www.usb.org/developers/devclass_docs/usbcdc11.pdf">Universal
|
||||
* Serial Bus Class Definitions for Communication Devices, v1.1</a>
|
||||
*/
|
||||
public class CdcAcmSerialDriver implements UsbSerialDriver {
|
||||
|
||||
private final String TAG = CdcAcmSerialDriver.class.getSimpleName();
|
||||
|
||||
private final UsbDevice mDevice;
|
||||
private final UsbSerialPort mPort;
|
||||
|
||||
public CdcAcmSerialDriver(UsbDevice device) {
|
||||
mDevice = device;
|
||||
mPort = new CdcAcmSerialPort(device, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UsbDevice getDevice() {
|
||||
return mDevice;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UsbSerialPort> getPorts() {
|
||||
return Collections.singletonList(mPort);
|
||||
}
|
||||
|
||||
class CdcAcmSerialPort extends CommonUsbSerialPort {
|
||||
|
||||
private UsbInterface mControlInterface;
|
||||
private UsbInterface mDataInterface;
|
||||
|
||||
private UsbEndpoint mControlEndpoint;
|
||||
|
||||
private int mControlIndex;
|
||||
|
||||
private boolean mRts = false;
|
||||
private boolean mDtr = false;
|
||||
|
||||
private static final int USB_RECIP_INTERFACE = 0x01;
|
||||
private static final int USB_RT_ACM = UsbConstants.USB_TYPE_CLASS | USB_RECIP_INTERFACE;
|
||||
|
||||
private static final int SET_LINE_CODING = 0x20; // USB CDC 1.1 section 6.2
|
||||
private static final int GET_LINE_CODING = 0x21;
|
||||
private static final int SET_CONTROL_LINE_STATE = 0x22;
|
||||
private static final int SEND_BREAK = 0x23;
|
||||
|
||||
public CdcAcmSerialPort(UsbDevice device, int portNumber) {
|
||||
super(device, portNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UsbSerialDriver getDriver() {
|
||||
return CdcAcmSerialDriver.this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open(UsbDeviceConnection connection) throws IOException {
|
||||
if (mConnection != null) {
|
||||
throw new IOException("Already open");
|
||||
}
|
||||
|
||||
mConnection = connection;
|
||||
boolean opened = false;
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void openSingleInterface() throws IOException {
|
||||
// the following code is inspired by the cdc-acm driver
|
||||
// in the linux kernel
|
||||
|
||||
mControlIndex = 0;
|
||||
mControlInterface = mDevice.getInterface(0);
|
||||
Log.d(TAG, "Control iface=" + mControlInterface);
|
||||
|
||||
mDataInterface = mDevice.getInterface(0);
|
||||
Log.d(TAG, "data iface=" + mDataInterface);
|
||||
|
||||
if (!mConnection.claimInterface(mControlInterface, true)) {
|
||||
throw new IOException("Could not claim shared control/data interface");
|
||||
}
|
||||
|
||||
int endCount = mControlInterface.getEndpointCount();
|
||||
|
||||
if (endCount < 3) {
|
||||
Log.d(TAG,"not enough endpoints - need 3. count=" + mControlInterface.getEndpointCount());
|
||||
throw new IOException("Insufficient number of endpoints (" + mControlInterface.getEndpointCount() + ")");
|
||||
}
|
||||
|
||||
// Analyse endpoints for their properties
|
||||
mControlEndpoint = null;
|
||||
mReadEndpoint = null;
|
||||
mWriteEndpoint = null;
|
||||
for (int i = 0; i < endCount; ++i) {
|
||||
UsbEndpoint ep = mControlInterface.getEndpoint(i);
|
||||
if ((ep.getDirection() == UsbConstants.USB_DIR_IN) &&
|
||||
(ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)) {
|
||||
Log.d(TAG,"Found controlling endpoint");
|
||||
mControlEndpoint = ep;
|
||||
} else if ((ep.getDirection() == UsbConstants.USB_DIR_IN) &&
|
||||
(ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
|
||||
Log.d(TAG,"Found reading endpoint");
|
||||
mReadEndpoint = ep;
|
||||
} else if ((ep.getDirection() == UsbConstants.USB_DIR_OUT) &&
|
||||
(ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) {
|
||||
Log.d(TAG,"Found writing endpoint");
|
||||
mWriteEndpoint = ep;
|
||||
}
|
||||
|
||||
|
||||
if ((mControlEndpoint != null) &&
|
||||
(mReadEndpoint != null) &&
|
||||
(mWriteEndpoint != null)) {
|
||||
Log.d(TAG,"Found all required endpoints");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((mControlEndpoint == null) ||
|
||||
(mReadEndpoint == null) ||
|
||||
(mWriteEndpoint == null)) {
|
||||
Log.d(TAG,"Could not establish all endpoints");
|
||||
throw new IOException("Could not establish all endpoints");
|
||||
}
|
||||
}
|
||||
|
||||
private void openInterface() throws IOException {
|
||||
Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount());
|
||||
|
||||
mControlInterface = null;
|
||||
mDataInterface = null;
|
||||
for (int i = 0; i < mDevice.getInterfaceCount(); i++) {
|
||||
UsbInterface usbInterface = mDevice.getInterface(i);
|
||||
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_COMM) {
|
||||
mControlIndex = i;
|
||||
mControlInterface = usbInterface;
|
||||
}
|
||||
if (usbInterface.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) {
|
||||
mDataInterface = usbInterface;
|
||||
}
|
||||
}
|
||||
|
||||
if(mControlInterface == null) {
|
||||
throw new IOException("No control interface");
|
||||
}
|
||||
Log.d(TAG, "Control iface=" + mControlInterface);
|
||||
|
||||
if (!mConnection.claimInterface(mControlInterface, true)) {
|
||||
throw new IOException("Could not claim control interface");
|
||||
}
|
||||
|
||||
mControlEndpoint = mControlInterface.getEndpoint(0);
|
||||
if (mControlEndpoint.getDirection() != UsbConstants.USB_DIR_IN || mControlEndpoint.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
|
||||
throw new IOException("Invalid control endpoint");
|
||||
}
|
||||
|
||||
if(mDataInterface == null) {
|
||||
throw new IOException("No data interface");
|
||||
}
|
||||
Log.d(TAG, "data iface=" + mDataInterface);
|
||||
|
||||
if (!mConnection.claimInterface(mDataInterface, true)) {
|
||||
throw new IOException("Could not claim data interface");
|
||||
}
|
||||
|
||||
mReadEndpoint = null;
|
||||
mWriteEndpoint = null;
|
||||
for (int i = 0; i < mDataInterface.getEndpointCount(); i++) {
|
||||
UsbEndpoint ep = mDataInterface.getEndpoint(i);
|
||||
if (ep.getDirection() == UsbConstants.USB_DIR_IN && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
|
||||
mReadEndpoint = ep;
|
||||
if (ep.getDirection() == UsbConstants.USB_DIR_OUT && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
|
||||
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 {
|
||||
int len = mConnection.controlTransfer(
|
||||
USB_RT_ACM, request, value, mControlIndex, buf, buf != null ? buf.length : 0, 5000);
|
||||
if(len < 0) {
|
||||
throw new IOException("controlTransfer failed");
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void closeInt() {
|
||||
try {
|
||||
mConnection.releaseInterface(mControlInterface);
|
||||
mConnection.releaseInterface(mDataInterface);
|
||||
} catch(Exception ignored) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException {
|
||||
if(baudRate <= 0) {
|
||||
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
|
||||
}
|
||||
if(dataBits < DATABITS_5 || dataBits > DATABITS_8) {
|
||||
throw new IllegalArgumentException("Invalid data bits: " + dataBits);
|
||||
}
|
||||
byte stopBitsByte;
|
||||
switch (stopBits) {
|
||||
case STOPBITS_1: stopBitsByte = 0; break;
|
||||
case STOPBITS_1_5: stopBitsByte = 1; break;
|
||||
case STOPBITS_2: stopBitsByte = 2; break;
|
||||
default: throw new IllegalArgumentException("Invalid stop bits: " + stopBits);
|
||||
}
|
||||
|
||||
byte parityBitesByte;
|
||||
switch (parity) {
|
||||
case PARITY_NONE: parityBitesByte = 0; break;
|
||||
case PARITY_ODD: parityBitesByte = 1; break;
|
||||
case PARITY_EVEN: parityBitesByte = 2; break;
|
||||
case PARITY_MARK: parityBitesByte = 3; break;
|
||||
case PARITY_SPACE: parityBitesByte = 4; break;
|
||||
default: throw new IllegalArgumentException("Invalid parity: " + parity);
|
||||
}
|
||||
byte[] msg = {
|
||||
(byte) ( baudRate & 0xff),
|
||||
(byte) ((baudRate >> 8 ) & 0xff),
|
||||
(byte) ((baudRate >> 16) & 0xff),
|
||||
(byte) ((baudRate >> 24) & 0xff),
|
||||
stopBitsByte,
|
||||
parityBitesByte,
|
||||
(byte) dataBits};
|
||||
sendAcmControlMessage(SET_LINE_CODING, 0, msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getCD() throws IOException {
|
||||
return false; // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getCTS() throws IOException {
|
||||
return false; // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getDSR() throws IOException {
|
||||
return false; // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getDTR() throws IOException {
|
||||
return mDtr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDTR(boolean value) throws IOException {
|
||||
mDtr = value;
|
||||
setDtrRts();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getRI() throws IOException {
|
||||
return false; // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getRTS() throws IOException {
|
||||
return mRts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRTS(boolean value) throws IOException {
|
||||
mRts = value;
|
||||
setDtrRts();
|
||||
}
|
||||
|
||||
private void setDtrRts() throws IOException {
|
||||
int value = (mRts ? 0x2 : 0) | (mDtr ? 0x1 : 0);
|
||||
sendAcmControlMessage(SET_CONTROL_LINE_STATE, value, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Map<Integer, int[]> getSupportedDevices() {
|
||||
final Map<Integer, int[]> supportedDevices = new LinkedHashMap<Integer, int[]>();
|
||||
supportedDevices.put(UsbId.VENDOR_ARDUINO,
|
||||
new int[] {
|
||||
UsbId.ARDUINO_UNO,
|
||||
UsbId.ARDUINO_UNO_R3,
|
||||
UsbId.ARDUINO_MEGA_2560,
|
||||
UsbId.ARDUINO_MEGA_2560_R3,
|
||||
UsbId.ARDUINO_SERIAL_ADAPTER,
|
||||
UsbId.ARDUINO_SERIAL_ADAPTER_R3,
|
||||
UsbId.ARDUINO_MEGA_ADK,
|
||||
UsbId.ARDUINO_MEGA_ADK_R3,
|
||||
UsbId.ARDUINO_LEONARDO,
|
||||
UsbId.ARDUINO_MICRO,
|
||||
});
|
||||
supportedDevices.put(UsbId.VENDOR_VAN_OOIJEN_TECH,
|
||||
new int[] {
|
||||
UsbId.VAN_OOIJEN_TECH_TEENSYDUINO_SERIAL,
|
||||
});
|
||||
supportedDevices.put(UsbId.VENDOR_ATMEL,
|
||||
new int[] {
|
||||
UsbId.ATMEL_LUFA_CDC_DEMO_APP,
|
||||
});
|
||||
supportedDevices.put(UsbId.VENDOR_LEAFLABS,
|
||||
new int[] {
|
||||
UsbId.LEAFLABS_MAPLE,
|
||||
});
|
||||
supportedDevices.put(UsbId.VENDOR_ARM,
|
||||
new int[] {
|
||||
UsbId.ARM_MBED,
|
||||
});
|
||||
return supportedDevices;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+21
-104
@@ -25,11 +25,8 @@ import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbEndpoint;
|
||||
import android.hardware.usb.UsbInterface;
|
||||
import android.hardware.usb.UsbRequest;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -83,10 +80,6 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
private boolean dtr = false;
|
||||
private boolean rts = false;
|
||||
|
||||
private UsbEndpoint mReadEndpoint;
|
||||
private UsbEndpoint mWriteEndpoint;
|
||||
private UsbRequest mUsbRequest;
|
||||
|
||||
public Ch340SerialPort(UsbDevice device, int portNumber) {
|
||||
super(device, portNumber);
|
||||
}
|
||||
@@ -99,7 +92,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
@Override
|
||||
public void open(UsbDeviceConnection connection) throws IOException {
|
||||
if (mConnection != null) {
|
||||
throw new IOException("Already opened.");
|
||||
throw new IOException("Already open");
|
||||
}
|
||||
|
||||
mConnection = connection;
|
||||
@@ -108,7 +101,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
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.");
|
||||
throw new IOException("Could not claim data interface");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,95 +123,17 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
opened = true;
|
||||
} finally {
|
||||
if (!opened) {
|
||||
try {
|
||||
close();
|
||||
} catch (IOException e) {
|
||||
// Ignore IOExceptions during close()
|
||||
}
|
||||
close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (mConnection == null) {
|
||||
throw new IOException("Already closed");
|
||||
}
|
||||
synchronized (this) {
|
||||
if (mUsbRequest != null)
|
||||
mUsbRequest.cancel();
|
||||
}
|
||||
public void closeInt() {
|
||||
try {
|
||||
mConnection.close();
|
||||
} finally {
|
||||
mConnection = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
||||
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;
|
||||
|
||||
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;
|
||||
for (int i = 0; i < mDevice.getInterfaceCount(); i++)
|
||||
mConnection.releaseInterface(mDevice.getInterface(i));
|
||||
} catch(Exception ignored) {}
|
||||
}
|
||||
|
||||
private int controlOut(int request, int value, int index) {
|
||||
@@ -269,7 +184,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
checkState("init #1", 0x5f, 0, new int[]{-1 /* 0x27, 0x30 */, 0x00});
|
||||
|
||||
if (controlOut(0xa1, 0, 0) < 0) {
|
||||
throw new IOException("init failed! #2");
|
||||
throw new IOException("Init failed: #2");
|
||||
}
|
||||
|
||||
setBaudRate(DEFAULT_BAUD_RATE);
|
||||
@@ -277,13 +192,13 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
checkState("init #4", 0x95, 0x2518, new int[]{-1 /* 0x56, c3*/, 0x00});
|
||||
|
||||
if (controlOut(0x9a, 0x2518, LCR_ENABLE_RX | LCR_ENABLE_TX | LCR_CS8) < 0) {
|
||||
throw new IOException("init failed! #5");
|
||||
throw new IOException("Init failed: #5");
|
||||
}
|
||||
|
||||
checkState("init #6", 0x95, 0x0706, new int[]{0xff, 0xee});
|
||||
|
||||
if (controlOut(0xa1, 0x501f, 0xd90a) < 0) {
|
||||
throw new IOException("init failed! #7");
|
||||
throw new IOException("Init failed: #7");
|
||||
}
|
||||
|
||||
setBaudRate(DEFAULT_BAUD_RATE);
|
||||
@@ -307,25 +222,27 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
}
|
||||
|
||||
if (factor > 0xfff0) {
|
||||
throw new IOException("Baudrate " + baudRate + " not supported");
|
||||
throw new UnsupportedOperationException("Unsupported baud rate: " + baudRate);
|
||||
}
|
||||
|
||||
factor = 0x10000 - factor;
|
||||
|
||||
int ret = controlOut(0x9a, 0x1312, (int) ((factor & 0xff00) | divisor));
|
||||
if (ret < 0) {
|
||||
throw new IOException("Error setting baud rate. #1)");
|
||||
throw new IOException("Error setting baud rate: #1)");
|
||||
}
|
||||
|
||||
ret = controlOut(0x9a, 0x0f2c, (int) (factor & 0xff));
|
||||
if (ret < 0) {
|
||||
throw new IOException("Error setting baud rate. #2");
|
||||
throw new IOException("Error setting baud rate: #2");
|
||||
}
|
||||
}
|
||||
|
||||
@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) {
|
||||
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
|
||||
}
|
||||
setBaudRate(baudRate);
|
||||
|
||||
int lcr = LCR_ENABLE_RX | LCR_ENABLE_TX;
|
||||
@@ -344,7 +261,7 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
lcr |= LCR_CS8;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown dataBits value: " + dataBits);
|
||||
throw new IllegalArgumentException("Invalid data bits: " + dataBits);
|
||||
}
|
||||
|
||||
switch (parity) {
|
||||
@@ -363,19 +280,19 @@ public class Ch34xSerialDriver implements UsbSerialDriver {
|
||||
lcr |= LCR_ENABLE_PAR | LCR_MARK_SPACE | LCR_PAR_EVEN;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown parity value: " + parity);
|
||||
throw new IllegalArgumentException("Invalid parity: " + parity);
|
||||
}
|
||||
|
||||
switch (stopBits) {
|
||||
case STOPBITS_1:
|
||||
break;
|
||||
case STOPBITS_1_5:
|
||||
throw new IllegalArgumentException("Unsupported stopBits value: 1.5");
|
||||
throw new UnsupportedOperationException("Unsupported stop bits: 1.5");
|
||||
case STOPBITS_2:
|
||||
lcr |= LCR_STOP_BITS_2;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown stopBits value: " + stopBits);
|
||||
throw new IllegalArgumentException("Invalid stop bits: " + stopBits);
|
||||
}
|
||||
|
||||
int ret = controlOut(0x9a, 0x2518, lcr);
|
||||
|
||||
+100
-30
@@ -23,31 +23,33 @@ package com.hoho.android.usbserial.driver;
|
||||
|
||||
import android.hardware.usb.UsbDevice;
|
||||
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.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* A base class shared by several driver implementations.
|
||||
*
|
||||
* @author mike wakerly (opensource@hoho.com)
|
||||
*/
|
||||
abstract class CommonUsbSerialPort implements UsbSerialPort {
|
||||
public abstract class CommonUsbSerialPort implements UsbSerialPort {
|
||||
|
||||
public static final int DEFAULT_READ_BUFFER_SIZE = 16 * 1024;
|
||||
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 int mPortNumber;
|
||||
|
||||
// non-null when open()
|
||||
protected UsbDeviceConnection mConnection = null;
|
||||
protected UsbEndpoint mReadEndpoint;
|
||||
protected UsbEndpoint mWriteEndpoint;
|
||||
protected UsbRequest mUsbRequest;
|
||||
|
||||
protected final Object mReadBufferLock = new Object();
|
||||
protected final Object mWriteBufferLock = new Object();
|
||||
|
||||
/** Internal read buffer. Guarded by {@link #mReadBufferLock}. */
|
||||
protected byte[] mReadBuffer;
|
||||
|
||||
/** Internal write buffer. Guarded by {@link #mWriteBufferLock}. */
|
||||
protected byte[] mWriteBuffer;
|
||||
|
||||
@@ -55,10 +57,9 @@ abstract class CommonUsbSerialPort implements UsbSerialPort {
|
||||
mDevice = device;
|
||||
mPortNumber = portNumber;
|
||||
|
||||
mReadBuffer = new byte[DEFAULT_READ_BUFFER_SIZE];
|
||||
mWriteBuffer = new byte[DEFAULT_WRITE_BUFFER_SIZE];
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("<%s device_name=%s device_id=%s port_number=%s>",
|
||||
@@ -89,21 +90,6 @@ abstract class CommonUsbSerialPort implements UsbSerialPort {
|
||||
return mConnection.getSerial();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the size of the internal buffer used to exchange data with the USB
|
||||
* stack for read operations. Most users should not need to change this.
|
||||
*
|
||||
* @param bufferSize the size in bytes
|
||||
*/
|
||||
public final void setReadBufferSize(int bufferSize) {
|
||||
synchronized (mReadBufferLock) {
|
||||
if (bufferSize == mReadBuffer.length) {
|
||||
return;
|
||||
}
|
||||
mReadBuffer = new byte[bufferSize];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the size of the internal buffer used to exchange data with the USB
|
||||
* stack for write operations. Most users should not need to change this.
|
||||
@@ -123,17 +109,101 @@ abstract class CommonUsbSerialPort implements UsbSerialPort {
|
||||
public abstract void open(UsbDeviceConnection connection) throws IOException;
|
||||
|
||||
@Override
|
||||
public abstract void close() throws IOException;
|
||||
public void close() throws IOException {
|
||||
if (mConnection == null) {
|
||||
throw new IOException("Already closed");
|
||||
}
|
||||
synchronized (this) {
|
||||
if (mUsbRequest != null)
|
||||
mUsbRequest.cancel();
|
||||
}
|
||||
try {
|
||||
closeInt();
|
||||
} catch(Exception ignored) {}
|
||||
try {
|
||||
mConnection.close();
|
||||
} finally {
|
||||
mConnection = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected abstract void closeInt();
|
||||
|
||||
@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
|
||||
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
|
||||
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;
|
||||
|
||||
@Override
|
||||
public abstract boolean getCD() throws IOException;
|
||||
|
||||
+27
-108
@@ -26,11 +26,8 @@ import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbEndpoint;
|
||||
import android.hardware.usb.UsbInterface;
|
||||
import android.hardware.usb.UsbRequest;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
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_RTS = 0x0200;
|
||||
|
||||
private UsbEndpoint mReadEndpoint;
|
||||
private UsbEndpoint mWriteEndpoint;
|
||||
private UsbRequest mUsbRequest;
|
||||
|
||||
// second port of Cp2105 has limited baudRate, dataBits, stopBits, parity
|
||||
// unsupported baudrate returns error at controlTransfer(), other parameters are silently ignored
|
||||
private boolean mIsRestrictedPort;
|
||||
@@ -123,15 +116,19 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
||||
return Cp21xxSerialDriver.this;
|
||||
}
|
||||
|
||||
private int setConfigSingle(int request, int value) {
|
||||
return mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, request, value,
|
||||
private int setConfigSingle(int request, int value) throws IOException {
|
||||
int result = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, request, value,
|
||||
mPortNumber, null, 0, USB_WRITE_TIMEOUT_MILLIS);
|
||||
if (result != 0) {
|
||||
throw new IOException("Setting baudrate failed: result=" + result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open(UsbDeviceConnection connection) throws IOException {
|
||||
if (mConnection != null) {
|
||||
throw new IOException("Already opened.");
|
||||
throw new IOException("Already open");
|
||||
}
|
||||
|
||||
mConnection = connection;
|
||||
@@ -163,99 +160,19 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
||||
opened = true;
|
||||
} finally {
|
||||
if (!opened) {
|
||||
try {
|
||||
close();
|
||||
} catch (IOException e) {
|
||||
// Ignore IOExceptions during close()
|
||||
}
|
||||
close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (mConnection == null) {
|
||||
throw new IOException("Already closed");
|
||||
}
|
||||
synchronized (this) {
|
||||
if(mUsbRequest != null) {
|
||||
mUsbRequest.cancel();
|
||||
}
|
||||
}
|
||||
public void closeInt() {
|
||||
try {
|
||||
setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_DISABLE);
|
||||
} catch (Exception ignored)
|
||||
{}
|
||||
} catch (Exception ignored) {}
|
||||
try {
|
||||
mConnection.close();
|
||||
} finally {
|
||||
mConnection = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
||||
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;
|
||||
|
||||
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;
|
||||
mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
|
||||
} catch(Exception ignored) {}
|
||||
}
|
||||
|
||||
private void setBaudRate(int baudRate) throws IOException {
|
||||
@@ -268,37 +185,39 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
||||
int ret = mConnection.controlTransfer(REQTYPE_HOST_TO_DEVICE, SILABSER_SET_BAUDRATE,
|
||||
0, mPortNumber, data, 4, USB_WRITE_TIMEOUT_MILLIS);
|
||||
if (ret < 0) {
|
||||
throw new IOException("Error setting baud rate.");
|
||||
throw new IOException("Error setting baud rate");
|
||||
}
|
||||
}
|
||||
|
||||
@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) {
|
||||
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
|
||||
}
|
||||
setBaudRate(baudRate);
|
||||
|
||||
int configDataBits = 0;
|
||||
switch (dataBits) {
|
||||
case DATABITS_5:
|
||||
if(mIsRestrictedPort)
|
||||
throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits);
|
||||
throw new UnsupportedOperationException("Unsupported data bits: " + dataBits);
|
||||
configDataBits |= 0x0500;
|
||||
break;
|
||||
case DATABITS_6:
|
||||
if(mIsRestrictedPort)
|
||||
throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits);
|
||||
throw new UnsupportedOperationException("Unsupported data bits: " + dataBits);
|
||||
configDataBits |= 0x0600;
|
||||
break;
|
||||
case DATABITS_7:
|
||||
if(mIsRestrictedPort)
|
||||
throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits);
|
||||
throw new UnsupportedOperationException("Unsupported data bits: " + dataBits);
|
||||
configDataBits |= 0x0700;
|
||||
break;
|
||||
case DATABITS_8:
|
||||
configDataBits |= 0x0800;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown dataBits value: " + dataBits);
|
||||
throw new IllegalArgumentException("Invalid data bits: " + dataBits);
|
||||
}
|
||||
|
||||
switch (parity) {
|
||||
@@ -312,30 +231,30 @@ public class Cp21xxSerialDriver implements UsbSerialDriver {
|
||||
break;
|
||||
case PARITY_MARK:
|
||||
if(mIsRestrictedPort)
|
||||
throw new IllegalArgumentException("Unsupported parity value: mark");
|
||||
throw new UnsupportedOperationException("Unsupported parity: mark");
|
||||
configDataBits |= 0x0030;
|
||||
break;
|
||||
case PARITY_SPACE:
|
||||
if(mIsRestrictedPort)
|
||||
throw new IllegalArgumentException("Unsupported parity value: space");
|
||||
throw new UnsupportedOperationException("Unsupported parity: space");
|
||||
configDataBits |= 0x0040;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown parity value: " + parity);
|
||||
throw new IllegalArgumentException("Invalid parity: " + parity);
|
||||
}
|
||||
|
||||
switch (stopBits) {
|
||||
case STOPBITS_1:
|
||||
break;
|
||||
case STOPBITS_1_5:
|
||||
throw new IllegalArgumentException("Unsupported stopBits value: 1.5");
|
||||
throw new UnsupportedOperationException("Unsupported stop bits: 1.5");
|
||||
case STOPBITS_2:
|
||||
if(mIsRestrictedPort)
|
||||
throw new IllegalArgumentException("Unsupported stopBits value: 2");
|
||||
throw new UnsupportedOperationException("Unsupported stop bits: 2");
|
||||
configDataBits |= 2;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown stopBits value: " + stopBits);
|
||||
throw new IllegalArgumentException("Invalid stop bits: " + stopBits);
|
||||
}
|
||||
setConfigSingle(SILABSER_SET_LINE_CTL_REQUEST_CODE, configDataBits);
|
||||
}
|
||||
|
||||
+31
-94
@@ -24,12 +24,9 @@ package com.hoho.android.usbserial.driver;
|
||||
import android.hardware.usb.UsbConstants;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
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.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -196,31 +193,32 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
||||
|
||||
/**
|
||||
* Filter FTDI status bytes from buffer
|
||||
* @param src The source buffer (which contains status bytes)
|
||||
* @param dest The destination buffer to write the status bytes into (can be src)
|
||||
* @param buffer The source buffer (which contains status bytes)
|
||||
* buffer The destination buffer to write the status bytes into (can be src)
|
||||
* @param totalBytesRead Number of bytes read to src
|
||||
* @param maxPacketSize The USB endpoint max packet size
|
||||
* @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;
|
||||
for (int packetIdx = 0; packetIdx < packetsCount; ++packetIdx) {
|
||||
final int count = (packetIdx == (packetsCount - 1))
|
||||
? totalBytesRead - packetIdx * maxPacketSize - MODEM_STATUS_HEADER_LENGTH
|
||||
: maxPacketSize - MODEM_STATUS_HEADER_LENGTH;
|
||||
if (count > 0) {
|
||||
System.arraycopy(src,
|
||||
packetIdx * maxPacketSize + MODEM_STATUS_HEADER_LENGTH,
|
||||
dest,
|
||||
packetIdx * (maxPacketSize - MODEM_STATUS_HEADER_LENGTH),
|
||||
count);
|
||||
System.arraycopy(buffer, packetIdx * maxPacketSize + MODEM_STATUS_HEADER_LENGTH,
|
||||
buffer, 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.
|
||||
mType = DeviceType.TYPE_R;
|
||||
if(mDevice.getInterfaceCount() > 1) {
|
||||
@@ -252,89 +250,26 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
||||
} 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();
|
||||
mConnection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (mConnection == null) {
|
||||
throw new IOException("Already closed");
|
||||
}
|
||||
public void closeInt() {
|
||||
try {
|
||||
mConnection.close();
|
||||
} finally {
|
||||
mConnection = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
||||
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 {
|
||||
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;
|
||||
mConnection.releaseInterface(mDevice.getInterface(mPortNumber));
|
||||
} catch(Exception ignored) {}
|
||||
}
|
||||
|
||||
private int setBaudRate(int baudRate) throws IOException {
|
||||
@@ -352,21 +287,23 @@ 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, int parity) throws IOException {
|
||||
if(baudRate <= 0) {
|
||||
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
|
||||
}
|
||||
setBaudRate(baudRate);
|
||||
|
||||
int config = 0;
|
||||
switch (dataBits) {
|
||||
case DATABITS_5:
|
||||
case DATABITS_6:
|
||||
throw new IllegalArgumentException("Unsupported dataBits value: " + dataBits);
|
||||
throw new UnsupportedOperationException("Unsupported data bits: " + dataBits);
|
||||
case DATABITS_7:
|
||||
case DATABITS_8:
|
||||
config |= dataBits;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown dataBits value: " + dataBits);
|
||||
throw new IllegalArgumentException("Invalid data bits: " + dataBits);
|
||||
}
|
||||
|
||||
switch (parity) {
|
||||
@@ -386,7 +323,7 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
||||
config |= (0x04 << 8);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown parity value: " + parity);
|
||||
throw new IllegalArgumentException("Invalid parity: " + parity);
|
||||
}
|
||||
|
||||
switch (stopBits) {
|
||||
@@ -394,12 +331,12 @@ public class FtdiSerialDriver implements UsbSerialDriver {
|
||||
config |= (0x00 << 11);
|
||||
break;
|
||||
case STOPBITS_1_5:
|
||||
throw new IllegalArgumentException("Unsupported stopBits value: 1.5");
|
||||
throw new UnsupportedOperationException("Unsupported stop bits: 1.5");
|
||||
case STOPBITS_2:
|
||||
config |= (0x02 << 11);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown stopBits value: " + stopBits);
|
||||
throw new IllegalArgumentException("Invalid stop bits: " + stopBits);
|
||||
}
|
||||
|
||||
int result = mConnection.controlTransfer(FTDI_DEVICE_OUT_REQTYPE,
|
||||
|
||||
@@ -65,25 +65,19 @@ public class ProbeTable {
|
||||
|
||||
try {
|
||||
method = driverClass.getMethod("getSupportedDevices");
|
||||
} catch (SecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (NoSuchMethodException e) {
|
||||
} catch (SecurityException | NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
final Map<Integer, int[]> devices;
|
||||
try {
|
||||
devices = (Map<Integer, int[]>) method.invoke(null);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
for (Map.Entry<Integer, int[]> entry : devices.entrySet()) {
|
||||
final int vendorId = entry.getKey().intValue();
|
||||
final int vendorId = entry.getKey();
|
||||
for (int productId : entry.getValue()) {
|
||||
addProduct(vendorId, productId, driverClass);
|
||||
}
|
||||
|
||||
+14
-90
@@ -32,12 +32,10 @@ import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbEndpoint;
|
||||
import android.hardware.usb.UsbInterface;
|
||||
import android.hardware.usb.UsbRequest;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -111,8 +109,6 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
|
||||
private int mDeviceType = DEVICE_TYPE_HX;
|
||||
|
||||
private UsbEndpoint mReadEndpoint;
|
||||
private UsbEndpoint mWriteEndpoint;
|
||||
private UsbEndpoint mInterruptEndpoint;
|
||||
|
||||
private int mControlLinesValue = 0;
|
||||
@@ -336,17 +332,13 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
opened = true;
|
||||
} finally {
|
||||
if (!opened) {
|
||||
mConnection = null;
|
||||
connection.releaseInterface(usbInterface);
|
||||
close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (mConnection == null) {
|
||||
throw new IOException("Already closed");
|
||||
}
|
||||
public void closeInt() {
|
||||
try {
|
||||
mStopReadStatusThread = true;
|
||||
synchronized (mReadStatusThreadLock) {
|
||||
@@ -359,80 +351,14 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
}
|
||||
}
|
||||
resetDevice();
|
||||
} finally {
|
||||
try {
|
||||
mConnection.releaseInterface(mDevice.getInterface(0));
|
||||
} finally {
|
||||
mConnection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] dest, int timeoutMillis) throws IOException {
|
||||
final UsbRequest request = new UsbRequest();
|
||||
} catch(Exception ignored) {}
|
||||
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();
|
||||
}
|
||||
mConnection.releaseInterface(mDevice.getInterface(0));
|
||||
} catch(Exception ignored) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int write(byte[] src, int timeoutMillis) throws IOException {
|
||||
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(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
|
||||
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)
|
||||
&& (mStopBits == stopBits) && (mParity == parity)) {
|
||||
// Make sure no action is performed if there is nothing to change
|
||||
@@ -441,6 +367,9 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
|
||||
byte[] lineRequestData = new byte[7];
|
||||
|
||||
if(baudRate <= 0) {
|
||||
throw new IllegalArgumentException("Invalid baud rate: " + baudRate);
|
||||
}
|
||||
lineRequestData[0] = (byte) (baudRate & 0xff);
|
||||
lineRequestData[1] = (byte) ((baudRate >> 8) & 0xff);
|
||||
lineRequestData[2] = (byte) ((baudRate >> 16) & 0xff);
|
||||
@@ -450,44 +379,39 @@ public class ProlificSerialDriver implements UsbSerialDriver {
|
||||
case STOPBITS_1:
|
||||
lineRequestData[4] = 0;
|
||||
break;
|
||||
|
||||
case STOPBITS_1_5:
|
||||
lineRequestData[4] = 1;
|
||||
break;
|
||||
|
||||
case STOPBITS_2:
|
||||
lineRequestData[4] = 2;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown stopBits value: " + stopBits);
|
||||
throw new IllegalArgumentException("Invalid stop bits: " + stopBits);
|
||||
}
|
||||
|
||||
switch (parity) {
|
||||
case PARITY_NONE:
|
||||
lineRequestData[5] = 0;
|
||||
break;
|
||||
|
||||
case PARITY_ODD:
|
||||
lineRequestData[5] = 1;
|
||||
break;
|
||||
|
||||
case PARITY_EVEN:
|
||||
lineRequestData[5] = 2;
|
||||
break;
|
||||
|
||||
case PARITY_MARK:
|
||||
lineRequestData[5] = 3;
|
||||
break;
|
||||
|
||||
case PARITY_SPACE:
|
||||
lineRequestData[5] = 4;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown parity value: " + parity);
|
||||
throw new IllegalArgumentException("Invalid parity: " + parity);
|
||||
}
|
||||
|
||||
if(dataBits < DATABITS_5 || dataBits > DATABITS_8) {
|
||||
throw new IllegalArgumentException("Invalid data bits: " + dataBits);
|
||||
}
|
||||
lineRequestData[6] = (byte) dataBits;
|
||||
|
||||
ctrlOut(SET_LINE_REQUEST, 0, 0, lineRequestData);
|
||||
|
||||
@@ -76,7 +76,7 @@ public final class UsbId {
|
||||
public static final int ARM_MBED = 0x0204;
|
||||
|
||||
private UsbId() {
|
||||
throw new IllegalAccessError("Non-instantiable class.");
|
||||
throw new IllegalAccessError("Non-instantiable class");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -145,9 +145,9 @@ public interface UsbSerialPort {
|
||||
* {@link #PARITY_EVEN}, {@link #PARITY_MARK}, or
|
||||
* {@link #PARITY_SPACE}.
|
||||
* @throws IOException on error setting the port parameters
|
||||
* @throws UnsupportedOperationException if not supported by a specific device
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* Gets the CD (Carrier Detect) bit from the underlying UART.
|
||||
|
||||
+2
-9
@@ -95,15 +95,8 @@ public class UsbSerialProber {
|
||||
final Constructor<? extends UsbSerialDriver> ctor =
|
||||
driverClass.getConstructor(UsbDevice.class);
|
||||
driver = ctor.newInstance(usbDevice);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
} catch (NoSuchMethodException | IllegalArgumentException | InstantiationException |
|
||||
IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return driver;
|
||||
|
||||
+3
-3
@@ -125,12 +125,12 @@ public class SerialInputOutputManager implements Runnable {
|
||||
public void run() {
|
||||
synchronized (this) {
|
||||
if (getState() != State.STOPPED) {
|
||||
throw new IllegalStateException("Already running.");
|
||||
throw new IllegalStateException("Already running");
|
||||
}
|
||||
mState = State.RUNNING;
|
||||
}
|
||||
|
||||
Log.i(TAG, "Running ..");
|
||||
Log.i(TAG, "Running ...");
|
||||
try {
|
||||
while (true) {
|
||||
if (getState() != State.RUNNING) {
|
||||
@@ -148,7 +148,7 @@ public class SerialInputOutputManager implements Runnable {
|
||||
} finally {
|
||||
synchronized (this) {
|
||||
mState = State.STOPPED;
|
||||
Log.i(TAG, "Stopped.");
|
||||
Log.i(TAG, "Stopped");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user