From b038ca5987110fa386b621810eeb3e7c8a6da125 Mon Sep 17 00:00:00 2001 From: rusefi Date: Wed, 21 Jun 2017 07:33:43 +0300 Subject: [PATCH] refactoring --- .../rusefi/binaryprotocol/BinaryProtocol.java | 24 +------------- .../BinaryProtocolCommands.java | 31 +++++++++++++++++++ .../rusefi/io/tcp/BinaryProtocolServer.java | 26 ++++++++++------ 3 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocolCommands.java diff --git a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java index ea68073acf..ef86d9ecf4 100644 --- a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -31,7 +31,7 @@ import static com.rusefi.binaryprotocol.IoHelper.*; * (c) Andrey Belomutskiy * 3/6/2015 */ -public class BinaryProtocol { +public class BinaryProtocol implements BinaryProtocolCommands { private static final String USE_PLAIN_PROTOCOL_PROPERTY = "protocol.plain"; /** @@ -43,28 +43,6 @@ public class BinaryProtocol { FileLog.MAIN.logLine(USE_PLAIN_PROTOCOL_PROPERTY + ": " + PLAIN_PROTOCOL); } - // see BLOCKING_FACTOR in firmware code - private static final int BLOCKING_FACTOR = Fields.BLOCKING_FACTOR; - private static final byte RESPONSE_OK = 0; - private static final byte RESPONSE_BURN_OK = 0x04; - private static final byte RESPONSE_COMMAND_OK = 0x07; - /** - * that's hex for "~\n", see BINARY_SWITCH_TAG in firmware source code - */ - private static final int SWITCH_TO_BINARY_RESPONSE = 0x7e0a; - /** - * See SWITCH_TO_BINARY_COMMAND in firmware source code - */ - private static final String SWITCH_TO_BINARY_COMMAND = "~"; - public static final char COMMAND_OUTPUTS = 'O'; // ochGetCommand - public static final char COMMAND_HELLO = 'S'; // queryCommand - public static final char COMMAND_PROTOCOL = 'F'; - public static final char COMMAND_CRC_CHECK_COMMAND = 'k'; - public static final char COMMAND_PAGE = 'P'; - public static final char COMMAND_READ = 'R'; // 082 decimal - private static final char COMMAND_CHUNK_WRITE = 'C'; // pageChunkWrite - private static final char COMMAND_BURN = 'B'; // burnCommand - private final Logger logger; private final IoStream stream; private final IncomingDataBuffer incomingData; diff --git a/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocolCommands.java b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocolCommands.java new file mode 100644 index 0000000000..374cea4638 --- /dev/null +++ b/java_console/io/src/com/rusefi/binaryprotocol/BinaryProtocolCommands.java @@ -0,0 +1,31 @@ +package com.rusefi.binaryprotocol; + +import com.rusefi.config.Fields; + +/** + * (c) Andrey Belomutskiy + * 6/21/2017. + */ +public interface BinaryProtocolCommands { + // see BLOCKING_FACTOR in firmware code + int BLOCKING_FACTOR = Fields.BLOCKING_FACTOR; + byte RESPONSE_OK = 0; + byte RESPONSE_BURN_OK = 0x04; + byte RESPONSE_COMMAND_OK = 0x07; + /** + * that's hex for "~\n", see BINARY_SWITCH_TAG in firmware source code + */ + int SWITCH_TO_BINARY_RESPONSE = 0x7e0a; + /** + * See SWITCH_TO_BINARY_COMMAND in firmware source code + */ + String SWITCH_TO_BINARY_COMMAND = "~"; + char COMMAND_OUTPUTS = 'O'; // ochGetCommand + char COMMAND_HELLO = 'S'; // queryCommand + char COMMAND_PROTOCOL = 'F'; + char COMMAND_CRC_CHECK_COMMAND = 'k'; + char COMMAND_PAGE = 'P'; + char COMMAND_READ = 'R'; // 082 decimal + char COMMAND_CHUNK_WRITE = 'C'; // pageChunkWrite + char COMMAND_BURN = 'B'; // burnCommand +} 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 969c81e62e..99aee372ba 100644 --- a/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java +++ b/java_console/io/src/com/rusefi/io/tcp/BinaryProtocolServer.java @@ -2,6 +2,7 @@ package com.rusefi.io.tcp; import com.rusefi.FileLog; import com.rusefi.binaryprotocol.BinaryProtocol; +import com.rusefi.binaryprotocol.BinaryProtocolCommands; import com.rusefi.binaryprotocol.BinaryProtocolHolder; import com.rusefi.binaryprotocol.IoHelper; import com.rusefi.config.Fields; @@ -18,7 +19,7 @@ import java.net.Socket; * 11/24/15 */ -public class BinaryProtocolServer { +public class BinaryProtocolServer implements BinaryProtocolCommands { private static final int PROXY_PORT = 2390; private static final String TS_OK = "\0"; @@ -28,6 +29,7 @@ public class BinaryProtocolServer { public static void start() { FileLog.MAIN.logLine("BinaryProtocolServer on " + PROXY_PORT); Runnable runnable = new Runnable() { + @SuppressWarnings("InfiniteLoopStatement") @Override public void run() { ServerSocket serverSocket; @@ -50,18 +52,20 @@ public class BinaryProtocolServer { }, "proxy connection").start(); } } catch (IOException e) { + throw new IllegalStateException(e); } } }; new Thread(runnable, "BinaryProtocolServer").start(); } + @SuppressWarnings("InfiniteLoopStatement") private static void runProxy(Socket clientSocket) throws IOException { DataInputStream in = new DataInputStream(clientSocket.getInputStream()); while (true) { byte first = in.readByte(); - if (first == BinaryProtocol.COMMAND_PROTOCOL) { + if (first == COMMAND_PROTOCOL) { //System.out.println("Ignoring plain F command"); System.out.println("Got plain F command"); OutputStream outputStream = clientSocket.getOutputStream(); @@ -78,7 +82,9 @@ public class BinaryProtocolServer { throw new IOException("Zero length not expected"); byte[] packet = new byte[length]; - in.read(packet); + int size = in.read(packet); + if (size != packet.length) + throw new IllegalStateException(); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(packet)); byte command = (byte) dis.read(); @@ -90,12 +96,12 @@ public class BinaryProtocolServer { TcpIoStream stream = new TcpIoStream(clientSocket.getInputStream(), clientSocket.getOutputStream()); - if (command == BinaryProtocol.COMMAND_HELLO) { + if (command == COMMAND_HELLO) { BinaryProtocol.sendPacket((TS_OK + TS_SIGNATURE).getBytes(), FileLog.LOGGER, stream); - } else if (command == BinaryProtocol.COMMAND_PROTOCOL) { + } else if (command == COMMAND_PROTOCOL) { // System.out.println("Ignoring crc F command"); BinaryProtocol.sendPacket((TS_OK + TS_PROTOCOL).getBytes(), FileLog.LOGGER, stream); - } else if (command == BinaryProtocol.COMMAND_CRC_CHECK_COMMAND) { + } else if (command == COMMAND_CRC_CHECK_COMMAND) { short page = dis.readShort(); short offset = dis.readShort(); short count = dis.readShort(); // no swap here? interesting! @@ -106,9 +112,9 @@ public class BinaryProtocolServer { response.write(TS_OK.charAt(0)); new DataOutputStream(response).write(result); BinaryProtocol.sendPacket(response.toByteArray(), FileLog.LOGGER, stream); - } else if (command == BinaryProtocol.COMMAND_PAGE) { + } else if (command == COMMAND_PAGE) { BinaryProtocol.sendPacket(TS_OK.getBytes(), FileLog.LOGGER, stream); - } else if (command == BinaryProtocol.COMMAND_READ) { + } else if (command == COMMAND_READ) { short page = dis.readShort(); short offset = swap16(dis.readShort()); short count = swap16(dis.readShort()); @@ -122,9 +128,9 @@ public class BinaryProtocolServer { System.arraycopy(bp.getController().getContent(), offset, response, 1, count); BinaryProtocol.sendPacket(response, FileLog.LOGGER, stream); } - } else if (command == BinaryProtocol.COMMAND_OUTPUTS) { + } else if (command == COMMAND_OUTPUTS) { - if (1==1) + if (System.currentTimeMillis() > 0) throw new UnsupportedOperationException("offset and count not supported see #429"); // todo: new version with offset and