diff options
author | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2022-05-30 15:00:34 +0300 |
---|---|---|
committer | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2022-05-30 15:00:34 +0300 |
commit | d97f7edf2a2b90b18dd0f78cc700c41f89847570 (patch) | |
tree | a7c9630ef70aab0cf7fdc7296843f64f3d6cc2ca | |
parent | 28ba1675612a1ad9dd87522124b6a901e6697c6c (diff) |
More settings, better comptatibility
8 files changed, 126 insertions, 37 deletions
diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 8f6e4e0..7250d95 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -1,17 +1,17 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="deploymentTargetDropDown"> - <targetSelectedWithDropDown> + <runningDeviceTargetSelectedWithDropDown> <Target> - <type value="QUICK_BOOT_TARGET" /> + <type value="RUNNING_DEVICE_TARGET" /> <deviceKey> <Key> - <type value="VIRTUAL_DEVICE_PATH" /> - <value value="C:\Users\clust\.android\avd\Nexus_5X_API_32.avd" /> + <type value="SERIAL_NUMBER" /> + <value value="10.13.1.10:5555" /> </Key> </deviceKey> </Target> - </targetSelectedWithDropDown> - <timeTargetWasSelectedWithDropDown value="2022-05-27T10:08:07.332831600Z" /> + </runningDeviceTargetSelectedWithDropDown> + <timeTargetWasSelectedWithDropDown value="2022-05-30T11:32:34.655345600Z" /> </component> </project>
\ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 7845b9b..729d686 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "com.clusterrr.usbserialtelnetserver" minSdk 19 targetSdk 32 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "1.1" } buildTypes { diff --git a/app/src/main/java/com/clusterrr/usbserialtelnetserver/MainActivity.java b/app/src/main/java/com/clusterrr/usbserialtelnetserver/MainActivity.java index aba4ea3..9290e6f 100644 --- a/app/src/main/java/com/clusterrr/usbserialtelnetserver/MainActivity.java +++ b/app/src/main/java/com/clusterrr/usbserialtelnetserver/MainActivity.java @@ -23,6 +23,7 @@ import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; +import android.widget.Switch; import android.widget.TextView; import com.hoho.android.usbserial.driver.UsbSerialDriver; @@ -37,6 +38,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe final static String SETTING_DATA_BITS = "data_bits"; final static String SETTING_STOP_BITS = "stop_bits"; final static String SETTING_PARITY = "parity"; + final static String SETTING_NO_LOCAL_ECHO = "no_local_echo"; + final static String SETTING_REMOVE_LF = "remove_lf"; UsbSerialTelnetService.ServiceBinder mServiceBinder = null; Button mStartButton; @@ -47,6 +50,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe Spinner mStopBits; Spinner mParity; TextView mStatus; + Switch mNoLocalEcho; + Switch mRemoveLF; @Override protected void onCreate(Bundle savedInstanceState) { @@ -60,6 +65,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe mStopBits = findViewById(R.id.spinnerStopBits); mParity = findViewById(R.id.spinnerParity); mStatus = findViewById(R.id.textViewStatus); + mNoLocalEcho = findViewById(R.id.switchNoLocalEcho); + mRemoveLF = findViewById(R.id.switchRemoveLf); mStartButton.setOnClickListener(this); mStopButton.setOnClickListener(this); @@ -133,7 +140,13 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe break; } serviceIntent.putExtra(UsbSerialTelnetService.KEY_PARITY, prefs.getInt(SETTING_PARITY, 0)); - startForegroundService(serviceIntent); + serviceIntent.putExtra(UsbSerialTelnetService.KEY_NO_LOCAL_ECHO, prefs.getBoolean(SETTING_NO_LOCAL_ECHO, true)); + serviceIntent.putExtra(UsbSerialTelnetService.KEY_REMOVE_LF, prefs.getBoolean(SETTING_REMOVE_LF, true)); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + startForegroundService(serviceIntent); + } else { + startService(serviceIntent); + } bindService(serviceIntent, serviceConnection, 0); } @@ -187,6 +200,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe .putInt(SETTING_DATA_BITS, mDataBits.getSelectedItemPosition()) .putInt(SETTING_STOP_BITS, mStopBits.getSelectedItemPosition()) .putInt(SETTING_PARITY, mParity.getSelectedItemPosition()) + .putBoolean(SETTING_NO_LOCAL_ECHO, mNoLocalEcho.isChecked()) + .putBoolean(SETTING_REMOVE_LF, mRemoveLF.isChecked()) .commit(); } @@ -200,11 +215,15 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe mDataBits.setEnabled(!started); mStopBits.setEnabled(!started); mParity.setEnabled(!started); + mNoLocalEcho.setEnabled(!started); + mRemoveLF.setEnabled(!started); mTcpPort.setText(String.valueOf(prefs.getInt(SETTING_TCP_PORT, 2323))); mBaudRate.setText(String.valueOf(prefs.getInt(SETTING_BAUD_RATE, 115200))); mDataBits.setSelection(prefs.getInt(SETTING_DATA_BITS, 3)); mStopBits.setSelection(prefs.getInt(SETTING_STOP_BITS, 0)); mParity.setSelection(prefs.getInt(SETTING_PARITY, 0)); + mNoLocalEcho.setChecked(prefs.getBoolean(SETTING_NO_LOCAL_ECHO, true)); + mRemoveLF.setChecked(prefs.getBoolean(SETTING_REMOVE_LF, true)); if (started) mStatus.setText(getString(R.string.started_please_connect) + " telnet://" + UsbSerialTelnetService.getIPAddress() + ":"+ mTcpPort.getText()); else diff --git a/app/src/main/java/com/clusterrr/usbserialtelnetserver/TcpClientThread.java b/app/src/main/java/com/clusterrr/usbserialtelnetserver/TcpClientThread.java index 3e793ab..bcb50a7 100644 --- a/app/src/main/java/com/clusterrr/usbserialtelnetserver/TcpClientThread.java +++ b/app/src/main/java/com/clusterrr/usbserialtelnetserver/TcpClientThread.java @@ -18,6 +18,16 @@ public class TcpClientThread extends Thread { private OutputStream mDataOutputStream; private String mAddress; private List<Byte> mBuffer; + private boolean mNoLocalEcho = true; + private boolean mRemoveLf = true; + private byte mLastChar = 0; + + final static byte CMD_WILL = (byte) 0xFB; + final static byte CMD_WONT = (byte) 0xFC; + final static byte CMD_DO = (byte) 0xFD; + final static byte CMD_DONT = (byte) 0xFE; + final static byte OP_ECHO = (byte) 1; + final static byte OP_SUPPRESS = (byte) 3; public TcpClientThread(UsbSerialTelnetService usbSerialTelnetService, TcpServerThread tcpServerThread, Socket socket) throws IOException { mUsbSerialTelnetService = usbSerialTelnetService; @@ -34,9 +44,13 @@ public class TcpClientThread extends Thread { byte buffer[] = new byte[1024]; try { - mDataOutputStream.write(new byte[]{(byte) 0xFF, (byte) 0xFD, (byte) 0x03}); // Do Suppress Go Ahead - mDataOutputStream.write(new byte[]{(byte) 0xFF, (byte) 0xFB, (byte) 0x03}); // Will Suppress Go Ahead - mDataOutputStream.write(new byte[]{(byte) 0xFF, (byte) 0xFB, (byte) 0x01}); // Will Echo + if (mNoLocalEcho) { + mDataOutputStream.write(new byte[]{(byte) 0xFF, CMD_WILL, OP_ECHO}); // Will Echo + mDataOutputStream.write(new byte[]{(byte) 0xFF, CMD_DONT, OP_ECHO}); // Don't Echo + mDataOutputStream.write(new byte[]{(byte) 0xFF, CMD_DO, OP_SUPPRESS}); // Do Suppress Go Ahead + mDataOutputStream.write(new byte[]{(byte) 0xFF, CMD_WILL, OP_SUPPRESS}); // Will Suppress Go Ahead + } + while (true) { if (mDataInputStream == null) break; int l = mDataInputStream.read(buffer); @@ -74,21 +88,17 @@ public class TcpClientThread extends Thread { for (; i < len; i++) { byte b = mBuffer.get(i); if (b == 0) continue; - if (b == '\n') continue; - /* - if ((b == '\r') && ((i >= len) || (mBuffer.get(i + 1) == '\n'))) { - // skip \r\n - if (true) - continue; + if (mRemoveLf && mLastChar == '\r' && b == '\n') { + // remove LF if need + mLastChar = '\n'; + continue; } - */ if (b == (byte)0xFF) { if (i >= len) break; byte next = mBuffer.get(i + 1); if (next == (byte)0xFF) { // just 0xFF - //mUsbSerialTelnetService.writePort((byte) 0xFF); - output[outputSize++] = (byte)0xFF; + output[outputSize++] = mLastChar = (byte)0xFF; i++; continue; } @@ -96,13 +106,13 @@ public class TcpClientThread extends Thread { if (i + 1 >= len) break; byte cmd = next; byte opt = mBuffer.get(i + 2); - Log.d(UsbSerialTelnetService.TAG, "Telnet command: CMD=" + Integer.toHexString(cmd >= 0 ? cmd : cmd + 256) + " ARG=" + Integer.toHexString(opt >= 0 ? opt : opt + 256)); + Log.d(UsbSerialTelnetService.TAG, "Telnet command: CMD=" + (cmd >= 0 ? cmd : cmd + 256) + " ARG=" + (opt >= 0 ? opt : opt + 256)); i += 2; continue; } // just data - //mUsbSerialTelnetService.writePort(b); - output[outputSize++] = b; + output[outputSize++] = mLastChar = b; + mLastChar = b; } // Remove proceeded @@ -141,4 +151,12 @@ public class TcpClientThread extends Thread { e.printStackTrace(); } } + + public void setNoLocalEcho(boolean noLocalEcho) { + mNoLocalEcho = noLocalEcho; + } + + public void setRemoveLf(boolean removeLf) { + mRemoveLf = removeLf; + } } diff --git a/app/src/main/java/com/clusterrr/usbserialtelnetserver/TcpServerThread.java b/app/src/main/java/com/clusterrr/usbserialtelnetserver/TcpServerThread.java index b18c516..44b4318 100644 --- a/app/src/main/java/com/clusterrr/usbserialtelnetserver/TcpServerThread.java +++ b/app/src/main/java/com/clusterrr/usbserialtelnetserver/TcpServerThread.java @@ -13,9 +13,10 @@ public class TcpServerThread extends Thread { private UsbSerialTelnetService mUsbSerialTelnetService; private ServerSocket mTcpServer; private List<TcpClientThread> mClients; + private boolean mNoLocalEcho = true; + private boolean mRemoveLf = true; - public TcpServerThread(UsbSerialTelnetService usbSerialTelnetService, ServerSocket tcpServer) - { + public TcpServerThread(UsbSerialTelnetService usbSerialTelnetService, ServerSocket tcpServer) { mUsbSerialTelnetService = usbSerialTelnetService; mTcpServer = tcpServer; mClients = new ArrayList<>(); @@ -29,14 +30,14 @@ public class TcpServerThread extends Thread { Socket socket = mTcpServer.accept(); Log.i(UsbSerialTelnetService.TAG, "Connected: " + socket.getRemoteSocketAddress()); TcpClientThread client = new TcpClientThread(mUsbSerialTelnetService, this, socket); + client.setNoLocalEcho(mNoLocalEcho); + client.setRemoveLf(mRemoveLf); client.start(); mClients.add(client); } - } - catch (SocketException e) { + } catch (SocketException e) { Log.i(UsbSerialTelnetService.TAG, "Server: " + e.getMessage()); - } - catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); } close(); @@ -56,8 +57,7 @@ public class TcpServerThread extends Thread { for (TcpClientThread client : mClients) { try { client.write(data, offset, len); - } - catch (Exception ex) { + } catch (Exception ex) { ex.printStackTrace(); toRemove.add(client); } @@ -68,8 +68,7 @@ public class TcpServerThread extends Thread { } } - public void close() - { + public void close() { try { if (mTcpServer != null) { mTcpServer.close(); @@ -81,11 +80,18 @@ public class TcpServerThread extends Thread { for (TcpClientThread client : mClients) { try { client.close(); - } - catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); } } mClients.clear(); } -} + + public void setNoLocalEcho(boolean noLocalEcho) { + mNoLocalEcho = noLocalEcho; + } + + public void setRemoveLf(boolean removeLf) { + mRemoveLf = removeLf; + } +}
\ No newline at end of file diff --git a/app/src/main/java/com/clusterrr/usbserialtelnetserver/UsbSerialTelnetService.java b/app/src/main/java/com/clusterrr/usbserialtelnetserver/UsbSerialTelnetService.java index 2c0061c..9daa7dd 100644 --- a/app/src/main/java/com/clusterrr/usbserialtelnetserver/UsbSerialTelnetService.java +++ b/app/src/main/java/com/clusterrr/usbserialtelnetserver/UsbSerialTelnetService.java @@ -40,6 +40,8 @@ public class UsbSerialTelnetService extends Service { final static String KEY_DATA_BITS = "data_bits"; final static String KEY_STOP_BITS = "stop_bits"; final static String KEY_PARITY = "parity"; + final static String KEY_NO_LOCAL_ECHO = "no_local_echo"; + final static String KEY_REMOVE_LF = "remove_lf"; boolean mStarted = false; //UsbSerialPort mSerialPort = null; @@ -95,6 +97,8 @@ public class UsbSerialTelnetService extends Service { ServerSocket serverSocket = new ServerSocket(intent.getIntExtra(KEY_TCP_PORT,2323)); mUsbSerialThread = new UsbSerialThread(this, serialPort); mTcpServerThread = new TcpServerThread(this, serverSocket); + mTcpServerThread.setNoLocalEcho(intent.getBooleanExtra(KEY_NO_LOCAL_ECHO, true)); + mTcpServerThread.setRemoveLf(intent.getBooleanExtra(KEY_REMOVE_LF, true)); mUsbSerialThread.start(); mTcpServerThread.start(); success = true; diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 51334e1..467c010 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -169,6 +169,46 @@ app:layout_constraintStart_toEndOf="@+id/textViewParity" app:layout_constraintTop_toBottomOf="@+id/spinnerStopBits" /> + <TextView + android:id="@+id/textNoLocalEcho" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginStart="24dp" + android:text="@string/no_local_echo" + app:layout_constraintBottom_toBottomOf="@+id/switchNoLocalEcho" + app:layout_constraintEnd_toStartOf="@+id/switchNoLocalEcho" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="@+id/switchNoLocalEcho" /> + + <Switch + android:id="@+id/switchNoLocalEcho" + android:layout_width="wrap_content" + android:layout_height="48dp" + android:layout_marginEnd="24dp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toEndOf="@+id/textNoLocalEcho" + app:layout_constraintTop_toBottomOf="@+id/spinnerParity" /> + + <TextView + android:id="@+id/textRemoveLf" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginStart="24dp" + android:text="@string/remove_lf" + app:layout_constraintBottom_toBottomOf="@+id/switchRemoveLf" + app:layout_constraintEnd_toStartOf="@+id/switchRemoveLf" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="@+id/switchRemoveLf" /> + + <Switch + android:id="@+id/switchRemoveLf" + android:layout_width="wrap_content" + android:layout_height="48dp" + android:layout_marginEnd="24dp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toEndOf="@+id/textRemoveLf" + app:layout_constraintTop_toBottomOf="@+id/switchNoLocalEcho" /> + </androidx.constraintlayout.widget.ConstraintLayout> </ScrollView> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d01b4c4..cc4d268 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,6 +14,8 @@ <string name="error">Error:</string> <string name="stopped">stopped</string> <string name="already_started">Already started</string> + <string name="no_local_echo">Forced local echo off</string> + <string name="remove_lf">Convert client\'s CR-LF to CR</string> <string-array name="data_bits"> <item>5</item> <item>6</item> |