From f28a92869fca33cfa64d32184f4092f7aca8d293 Mon Sep 17 00:00:00 2001 From: Alessio Morale Date: Mon, 12 Sep 2016 23:17:03 +0200 Subject: [PATCH] [LP Support] - Add support for complex USB device layouts --- .../usbserial/driver/CdcAcmSerialDriver.java | 72 +++++++++++++++---- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/android/src/com/hoho/android/usbserial/driver/CdcAcmSerialDriver.java b/android/src/com/hoho/android/usbserial/driver/CdcAcmSerialDriver.java index 4a56e4273..b1599474f 100644 --- a/android/src/com/hoho/android/usbserial/driver/CdcAcmSerialDriver.java +++ b/android/src/com/hoho/android/usbserial/driver/CdcAcmSerialDriver.java @@ -47,31 +47,75 @@ public class CdcAcmSerialDriver extends CommonUsbSerialDriver { @Override public void open() throws IOException { - Log.d(TAG, "claiming interfaces, count=" + mDevice.getInterfaceCount()); + Log.d(TAG, "device " + mDevice); + mControlInterface = null; + mDataInterface = null; + mWriteEndpoint = null; + mReadEndpoint = null; + + // locate all needed interfaces + for(int i = 0; i < mDevice.getInterfaceCount();i++){ + UsbInterface iface = mDevice.getInterface(i); + + switch(iface.getInterfaceClass()){ + case UsbConstants.USB_CLASS_COMM: + mControlInterface = iface; + Log.d(TAG, "control iface=" + iface); + break; + case UsbConstants.USB_CLASS_CDC_DATA: + mDataInterface = iface; + Log.d(TAG, "data iface=" + iface); + break; + default: + Log.d(TAG, "skipping iface=" + iface); + break; + } + } - Log.d(TAG, "Claiming control interface."); - mControlInterface = mDevice.getInterface(0); - Log.d(TAG, "Control iface=" + mControlInterface); - // class should be USB_CLASS_COMM + // failback to the old way + if(mControlInterface == null) { + mControlInterface = mDevice.getInterface(0); + Log.d(TAG, "Failback: Control iface=" + mControlInterface); + } if (!mConnection.claimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface."); } + mControlEndpoint = mControlInterface.getEndpoint(0); - Log.d(TAG, "Control endpoint direction: " + mControlEndpoint.getDirection()); + Log.d(TAG, "Control endpoint: " + mControlEndpoint); + - Log.d(TAG, "Claiming data interface."); - mDataInterface = mDevice.getInterface(1); - Log.d(TAG, "data iface=" + mDataInterface); - // class should be USB_CLASS_CDC_DATA + if(mDataInterface == null) { + mDataInterface = mDevice.getInterface(1); + Log.d(TAG, "Failback: data iface=" + mDataInterface); + } if (!mConnection.claimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface."); } - mReadEndpoint = mDataInterface.getEndpoint(1); - Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection()); - mWriteEndpoint = mDataInterface.getEndpoint(0); - Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection()); + + for(int i = 0; i < mDataInterface.getEndpointCount(); i++) { + UsbEndpoint endpoint = mDataInterface.getEndpoint(i); + switch (endpoint.getDirection()) { + case UsbConstants.USB_DIR_OUT: + mWriteEndpoint = endpoint; + Log.d(TAG, "Write endpoint: " + mWriteEndpoint); + break; + case UsbConstants.USB_DIR_IN: + mReadEndpoint = endpoint; + Log.d(TAG, "Read endpoint: " + mReadEndpoint); + break; + } + } + + if(mReadEndpoint == null || mWriteEndpoint == null){ + // failback to the old method + mReadEndpoint = mDataInterface.getEndpoint(0); + Log.d(TAG, "Read endpoint direction: " + mReadEndpoint.getDirection()); + mWriteEndpoint = mDataInterface.getEndpoint(1); + Log.d(TAG, "Write endpoint direction: " + mWriteEndpoint.getDirection()); + } } private int sendAcmControlMessage(int request, int value, byte[] buf) { -- 2.22.0