From d25840dbef1bf13dd6dae6f86121bf602e0d117c Mon Sep 17 00:00:00 2001 From: rusefi Date: Wed, 14 Aug 2019 23:12:00 -0400 Subject: [PATCH] refactoring --- .../rusefi/binaryprotocol/BinaryProtocol.java | 27 ++-------------- .../io/src/com/rusefi/io/IoStream.java | 31 +++++++++++++++++-- .../io/serial/SerialIoStreamJSerialComm.java | 2 -- .../rusefi/io/tcp/BinaryProtocolServer.java | 12 +++---- .../rusefi/autodetect/SerialAutoChecker.java | 3 +- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java index 3fc979962e..c237a99c3a 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -39,7 +39,7 @@ public class BinaryProtocol implements BinaryProtocolCommands { * This properly allows to switch to non-CRC32 mode * todo: finish this feature, assuming we even need it. */ - private static boolean PLAIN_PROTOCOL = Boolean.getBoolean(USE_PLAIN_PROTOCOL_PROPERTY); + public static boolean PLAIN_PROTOCOL = Boolean.getBoolean(USE_PLAIN_PROTOCOL_PROPERTY); static { FileLog.MAIN.logLine(USE_PLAIN_PROTOCOL_PROPERTY + ": " + PLAIN_PROTOCOL); } @@ -330,30 +330,7 @@ public class BinaryProtocol implements BinaryProtocolCommands { } private void sendPacket(byte[] command) throws IOException { - sendPacket(command, logger, stream); - } - - public static void sendPacket(byte[] plainPacket, Logger logger, IoStream stream) throws IOException { - byte[] packet; - if (PLAIN_PROTOCOL) { - packet = plainPacket; - } else { - packet = IoHelper.makeCrc32Packet(plainPacket); - } - logger.info("Sending packet " + printHexBinary(plainPacket)); - stream.write(packet); - } - - private static final char[] hexCode = "0123456789ABCDEF".toCharArray(); - - private static String printHexBinary(byte[] data) { - StringBuilder r = new StringBuilder(data.length * 2); - for (byte b : data) { - r.append(hexCode[(b >> 4) & 0xF]); - r.append(hexCode[(b & 0xF)]); - r.append(' '); - } - return r.toString(); + stream.sendPacket(command, logger); } diff --git a/java_console/io/src/com/rusefi/io/IoStream.java b/java_console/io/src/com/rusefi/io/IoStream.java index 292317c60e..bda6171aa9 100644 --- a/java_console/io/src/com/rusefi/io/IoStream.java +++ b/java_console/io/src/com/rusefi/io/IoStream.java @@ -1,18 +1,45 @@ package com.rusefi.io; +import com.opensr5.Logger; import com.opensr5.io.DataListener; import com.opensr5.io.WriteStream; +import com.rusefi.binaryprotocol.BinaryProtocol; +import com.rusefi.binaryprotocol.IoHelper; import java.io.IOException; /** * Physical bi-directional controller communication level - * + *

* (c) Andrey Belomutskiy - * + *

* 5/11/2015. */ public interface IoStream extends WriteStream { + + static String printHexBinary(byte[] data) { + char[] hexCode = "0123456789ABCDEF".toCharArray(); + + StringBuilder r = new StringBuilder(data.length * 2); + for (byte b : data) { + r.append(hexCode[(b >> 4) & 0xF]); + r.append(hexCode[(b & 0xF)]); + r.append(' '); + } + return r.toString(); + } + + default void sendPacket(byte[] plainPacket, Logger logger) throws IOException { + byte[] packet; + if (BinaryProtocol.PLAIN_PROTOCOL) { + packet = plainPacket; + } else { + packet = IoHelper.makeCrc32Packet(plainPacket); + } + logger.info("Sending packet " + printHexBinary(plainPacket)); + write(packet); + } + /** * @param listener would be invoked from unknown implementation-dependent thread */ diff --git a/java_console/io/src/com/rusefi/io/serial/SerialIoStreamJSerialComm.java b/java_console/io/src/com/rusefi/io/serial/SerialIoStreamJSerialComm.java index bdb3537e7b..ea3a98ed97 100644 --- a/java_console/io/src/com/rusefi/io/serial/SerialIoStreamJSerialComm.java +++ b/java_console/io/src/com/rusefi/io/serial/SerialIoStreamJSerialComm.java @@ -7,8 +7,6 @@ import com.opensr5.Logger; import com.opensr5.io.DataListener; import com.rusefi.io.IoStream; -import java.io.IOException; - /** * https://github.com/Fazecast/jSerialComm looks to be alive as of 2019 *

diff --git a/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java b/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java index 81495f1666..708bae9713 100644 --- a/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java +++ b/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java @@ -102,10 +102,10 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { TcpIoStream stream = new TcpIoStream(clientSocket.getInputStream(), clientSocket.getOutputStream()); if (command == COMMAND_HELLO) { - BinaryProtocol.sendPacket((TS_OK + TS_SIGNATURE).getBytes(), FileLog.LOGGER, stream); + stream.sendPacket((TS_OK + TS_SIGNATURE).getBytes(), FileLog.LOGGER); } else if (command == COMMAND_PROTOCOL) { // System.out.println("Ignoring crc F command"); - BinaryProtocol.sendPacket((TS_OK + TS_PROTOCOL).getBytes(), FileLog.LOGGER, stream); + stream.sendPacket((TS_OK + TS_PROTOCOL).getBytes(), FileLog.LOGGER); } else if (command == COMMAND_CRC_CHECK_COMMAND) { short page = dis.readShort(); short offset = dis.readShort(); @@ -116,9 +116,9 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { ByteArrayOutputStream response = new ByteArrayOutputStream(); response.write(TS_OK.charAt(0)); new DataOutputStream(response).write(result); - BinaryProtocol.sendPacket(response.toByteArray(), FileLog.LOGGER, stream); + stream.sendPacket(response.toByteArray(), FileLog.LOGGER); } else if (command == COMMAND_PAGE) { - BinaryProtocol.sendPacket(TS_OK.getBytes(), FileLog.LOGGER, stream); + stream.sendPacket(TS_OK.getBytes(), FileLog.LOGGER); } else if (command == COMMAND_READ) { short page = dis.readShort(); short offset = swap16(dis.readShort()); @@ -131,7 +131,7 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { byte[] response = new byte[1 + count]; response[0] = (byte) TS_OK.charAt(0); System.arraycopy(bp.getController().getContent(), offset, response, 1, count); - BinaryProtocol.sendPacket(response, FileLog.LOGGER, stream); + stream.sendPacket(response, FileLog.LOGGER); } } else if (command == COMMAND_OUTPUTS) { @@ -145,7 +145,7 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { byte[] currentOutputs = bp.currentOutputs; if (currentOutputs != null) System.arraycopy(currentOutputs, 1, response, 1, Fields.TS_OUTPUT_SIZE); - BinaryProtocol.sendPacket(response, FileLog.LOGGER, stream); + stream.sendPacket(response, FileLog.LOGGER); } else { FileLog.MAIN.logLine("Error: unknown command " + command); } diff --git a/java_console/ui/src/com/rusefi/autodetect/SerialAutoChecker.java b/java_console/ui/src/com/rusefi/autodetect/SerialAutoChecker.java index 36e71fd148..d62c9370b0 100644 --- a/java_console/ui/src/com/rusefi/autodetect/SerialAutoChecker.java +++ b/java_console/ui/src/com/rusefi/autodetect/SerialAutoChecker.java @@ -2,7 +2,6 @@ package com.rusefi.autodetect; import com.opensr5.Logger; import com.rusefi.FileLog; -import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.binaryprotocol.BinaryProtocolCommands; import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.config.generated.Fields; @@ -36,7 +35,7 @@ class SerialAutoChecker implements Runnable { IncomingDataBuffer incomingData = new IncomingDataBuffer(logger); stream.setInputListener(incomingData::addData); try { - BinaryProtocol.sendPacket(new byte[]{BinaryProtocolCommands.COMMAND_HELLO}, logger, stream); + stream.sendPacket(new byte[]{BinaryProtocolCommands.COMMAND_HELLO}, logger); byte[] response = incomingData.getPacket(logger, "", false, System.currentTimeMillis()); if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK)) return;