diff --git a/usbSerialExamples/src/main/java/com/hoho/android/usbserial/examples/TerminalFragment.java b/usbSerialExamples/src/main/java/com/hoho/android/usbserial/examples/TerminalFragment.java index 2f4cfb33..5850debf 100644 --- a/usbSerialExamples/src/main/java/com/hoho/android/usbserial/examples/TerminalFragment.java +++ b/usbSerialExamples/src/main/java/com/hoho/android/usbserial/examples/TerminalFragment.java @@ -351,7 +351,7 @@ private void run() { cdBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.CD)); riBtn.setChecked(controlLines.contains(UsbSerialPort.ControlLine.RI)); mainLooper.postDelayed(runnable, refreshInterval); - } catch (IOException e) { + } catch (Exception e) { status("getControlLines() failed: " + e.getMessage() + " -> stopped control line refresh"); } } @@ -368,8 +368,15 @@ void start() { if (!controlLines.contains(UsbSerialPort.ControlLine.CD)) cdBtn.setVisibility(View.INVISIBLE); if (!controlLines.contains(UsbSerialPort.ControlLine.RI)) riBtn.setVisibility(View.INVISIBLE); run(); - } catch (IOException e) { + } catch (Exception e) { Toast.makeText(getActivity(), "getSupportedControlLines() failed: " + e.getMessage(), Toast.LENGTH_SHORT).show(); + rtsBtn.setVisibility(View.INVISIBLE); + ctsBtn.setVisibility(View.INVISIBLE); + dtrBtn.setVisibility(View.INVISIBLE); + dsrBtn.setVisibility(View.INVISIBLE); + cdBtn.setVisibility(View.INVISIBLE); + cdBtn.setVisibility(View.INVISIBLE); + riBtn.setVisibility(View.INVISIBLE); } } diff --git a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/GsmSerialDriver.java b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/GsmSerialDriver.java new file mode 100644 index 00000000..06b1996f --- /dev/null +++ b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/GsmSerialDriver.java @@ -0,0 +1,121 @@ +package com.hoho.android.usbserial.driver; + +import android.hardware.usb.UsbConstants; +import android.hardware.usb.UsbDevice; +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.EnumSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class GsmSerialDriver implements UsbSerialDriver{ + + private static final String TAG = "GsmSerialDriver"; + private final UsbDevice mDevice; + + private final UsbSerialPort mPort; + + private UsbInterface mControlInterface; + private UsbInterface mDataInterface; + private UsbEndpoint mControlEndpoint; + + private int mControlIndex; + private static final int SET_CONTROL_LINE_STATE = 0x22; + + @Override + public UsbDevice getDevice() { + return mDevice; + } + + @Override + public List getPorts() { + return Collections.singletonList(mPort); + } + + public GsmSerialDriver(UsbDevice mDevice) { + this.mDevice = mDevice; + mPort = new GsmSerialDriver.GsmSerialPort(mDevice, 0); + } + + public class GsmSerialPort extends CommonUsbSerialPort { + + public GsmSerialPort(UsbDevice device, int portNumber) { + super(device, portNumber); + } + + @Override + protected void openInt() throws IOException { + mControlIndex = 0; + mControlInterface = mDevice.getInterface(0); + mDataInterface = mDevice.getInterface(0); + if (!mConnection.claimInterface(mControlInterface, true)) { + throw new IOException("Could not claim shared control/data interface"); + } + Log.i(TAG, "endpointcount "+ mControlInterface.getEndpointCount()); + for (int i = 0; i < mControlInterface.getEndpointCount(); ++i) { + UsbEndpoint ep = mControlInterface.getEndpoint(i); + Log.i(TAG, ep.toString() + "dir "+ ep.getDirection() + " type " + ep.getType()); + if ((ep.getDirection() == UsbConstants.USB_DIR_IN) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_INT)) { + mControlEndpoint = ep; + } else if ((ep.getDirection() == UsbConstants.USB_DIR_IN) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) { + mReadEndpoint = ep; + } else if ((ep.getDirection() == UsbConstants.USB_DIR_OUT) && (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)) { + mWriteEndpoint = ep; + } + } + sendGsmControlMessage(SET_CONTROL_LINE_STATE, 0x01, null); + + } + + @Override + protected void closeInt() { + try { + mConnection.releaseInterface(mControlInterface); + mConnection.releaseInterface(mDataInterface); + } catch(Exception ignored) {} + + } + + private int sendGsmControlMessage(int request, int value, byte[] buf) throws IOException { + int len = mConnection.controlTransfer( + 0x21, request, value, mControlIndex, buf, buf != null ? buf.length : 0, 5000); + if(len < 0) { + throw new IOException("controlTransfer failed"); + } + return len; + } + + @Override + public UsbSerialDriver getDriver() { + return GsmSerialDriver.this; + } + + @Override + public void setParameters(int baudRate, int dataBits, int stopBits, int parity) throws IOException { + } + + @Override + public EnumSet getControlLines() throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public EnumSet getSupportedControlLines() throws IOException { + throw new UnsupportedOperationException(); + } + } + + public static Map getSupportedDevices() { + final Map supportedDevices = new LinkedHashMap<>(); + supportedDevices.put(UsbId.VENDOR_UNISOC, new int[]{ + UsbId.FIBO_L610, + UsbId.FIBO_L612, + }); + return supportedDevices; + } +} diff --git a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/UsbId.java b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/UsbId.java index bed2ffe4..2a68238e 100644 --- a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/UsbId.java +++ b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/UsbId.java @@ -41,6 +41,10 @@ public final class UsbId { public static final int QINHENG_CH340 = 0x7523; public static final int QINHENG_CH341A = 0x5523; + public static final int VENDOR_UNISOC = 0x1782; + public static final int FIBO_L610 = 0x4D10; + public static final int FIBO_L612 = 0x4D12; + private UsbId() { throw new IllegalAccessError("Non-instantiable class"); diff --git a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/UsbSerialProber.java b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/UsbSerialProber.java index fd94a187..172f0eee 100644 --- a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/UsbSerialProber.java +++ b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/driver/UsbSerialProber.java @@ -37,6 +37,7 @@ public static ProbeTable getDefaultProbeTable() { probeTable.addDriver(FtdiSerialDriver.class); probeTable.addDriver(ProlificSerialDriver.class); probeTable.addDriver(Ch34xSerialDriver.class); + probeTable.addDriver(GsmSerialDriver.class); return probeTable; }