diff --git a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java index 6d916f0c6a..ace365317d 100644 --- a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java +++ b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocol.java @@ -65,7 +65,7 @@ public class BinaryProtocol { private final Object ioLock = new Object(); BinaryProtocolLogger binaryProtocolLogger; - public static boolean DISABLE_LOCAL_CACHE; + public static boolean DISABLE_LOCAL_CONFIGURATION_CACHE; public static String findCommand(byte command) { switch (command) { @@ -377,7 +377,7 @@ public class BinaryProtocol { } private ConfigurationImage getAndValidateLocallyCached() { - if (DISABLE_LOCAL_CACHE) + if (DISABLE_LOCAL_CONFIGURATION_CACHE) return null; ConfigurationImage localCached; try { diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java index 1cf027cd66..454964ddb5 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolServer.java @@ -182,19 +182,10 @@ public class BinaryProtocolServer { // todo: relay command stream.sendPacket(TS_OK.getBytes()); } else if (command == Fields.TS_OUTPUT_COMMAND) { - DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1)); - int offset = swap16(dis.readShort()); - int count = swap16(dis.readShort()); - log.info("TS_OUTPUT_COMMAND offset=" + offset + "/count=" + count); - - byte[] response = new byte[1 + count]; - response[0] = (byte) TS_OK.charAt(0); BinaryProtocolState binaryProtocolState = linkManager.getBinaryProtocolState(); byte[] currentOutputs = binaryProtocolState.getCurrentOutputs(); - if (MOCK_SD_CARD) - currentOutputs[SD_STATUS_OFFSET] = 1 + 4; - if (currentOutputs != null) - System.arraycopy(currentOutputs, offset, response, 1, count); + + byte[] response = getOutputCommandResponse(payload, currentOutputs); stream.sendPacket(response); } else if (command == Fields.TS_GET_TEXT) { // todo: relay command @@ -208,6 +199,23 @@ public class BinaryProtocolServer { } } + @NotNull + public static byte[] getOutputCommandResponse(byte[] payload, byte[] currentOutputs) throws IOException { + DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1)); + int offset = swap16(dis.readShort()); + int count = swap16(dis.readShort()); + if (log.debugEnabled()) + log.debug("TS_OUTPUT_COMMAND offset=" + offset + "/count=" + count); + + byte[] response = new byte[1 + count]; + response[0] = (byte) TS_OK.charAt(0); + if (MOCK_SD_CARD) + currentOutputs[SD_STATUS_OFFSET] = 1 + 4; + if (currentOutputs != null) + System.arraycopy(currentOutputs, offset, response, 1, count); + return response; + } + @NotNull public static byte[] getPacketContent(IncomingDataBuffer in, Integer length) throws IOException { if (log.debugEnabled()) @@ -329,11 +337,19 @@ public class BinaryProtocolServer { log.info("CRC check"); BinaryProtocolState bp = linkManager.getBinaryProtocolState(); byte[] content = bp.getControllerConfiguration().getContent(); - int result = IoHelper.getCrc32(content); + byte[] packet = createCrcResponse(content); + stream.sendPacket(packet); + } + + @NotNull + public static byte[] createCrcResponse(byte[] content) throws IOException { + int crc32value = IoHelper.getCrc32(content); ByteArrayOutputStream response = new ByteArrayOutputStream(); + // header response.write(TS_OK.charAt(0)); - new DataOutputStream(response).writeInt(result); - stream.sendPacket(response.toByteArray()); + // payload + new DataOutputStream(response).writeInt(crc32value); + return response.toByteArray(); } public static class Packet { diff --git a/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/SimulatorTcpSandbox.java b/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/TcpClientSandbox.java similarity index 87% rename from java_console/io/src/test/java/com/rusefi/binaryprotocol/test/SimulatorTcpSandbox.java rename to java_console/io/src/test/java/com/rusefi/binaryprotocol/test/TcpClientSandbox.java index 60419f2920..96715e5784 100644 --- a/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/SimulatorTcpSandbox.java +++ b/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/TcpClientSandbox.java @@ -10,9 +10,12 @@ import java.net.Socket; import static com.rusefi.io.tcp.TcpConnector.DEFAULT_PORT; import static com.rusefi.io.tcp.TcpConnector.LOCALHOST; -public class SimulatorTcpSandbox { - public static void main(String[] args) throws IOException, InterruptedException { - BinaryProtocol.DISABLE_LOCAL_CACHE = true; +/** + * @see TcpServerSandbox + */ +public class TcpClientSandbox { + public static void main(String[] args) throws IOException { + BinaryProtocol.DISABLE_LOCAL_CONFIGURATION_CACHE = true; Socket s = new Socket(LOCALHOST, DEFAULT_PORT); TcpIoStream tsStream = new TcpIoStream("sandbox", s); @@ -36,9 +39,6 @@ public class SimulatorTcpSandbox { long time = System.currentTimeMillis() - startMs; double timePerCommand = 1.0 * time / count; System.out.println("Executed " + count + " getSignature in " + time + "ms\n" + "Per-signature: " + timePerCommand + "ms"); - -// linkManager.submit(() -> { - } { @@ -57,8 +57,6 @@ public class SimulatorTcpSandbox { } - // ConfigurationImage ci = SandboxCommon.readImage(tsStream, linkManager); - System.exit(0); } diff --git a/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/TcpServerSandbox.java b/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/TcpServerSandbox.java index 97f1fa73df..e290116206 100644 --- a/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/TcpServerSandbox.java +++ b/java_console/io/src/test/java/com/rusefi/binaryprotocol/test/TcpServerSandbox.java @@ -2,6 +2,7 @@ package com.rusefi.binaryprotocol.test; import com.rusefi.CompatibleFunction; import com.rusefi.Listener; +import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.config.generated.Fields; import com.rusefi.io.IoStream; @@ -18,12 +19,18 @@ import java.net.Socket; import static com.rusefi.binaryprotocol.IoHelper.swap16; import static com.rusefi.config.generated.Fields.TS_PROTOCOL; import static com.rusefi.io.tcp.BinaryProtocolServer.TS_OK; +import static com.rusefi.io.tcp.BinaryProtocolServer.getOutputCommandResponse; /** * Fully self-contained fake TCP-IP 'ECU' side of TS protocol * does not have checkCrc32 command implementation so you have to remove it from .ini if you want to connect to this ECU + * + * @see TcpClientSandbox */ public class TcpServerSandbox { + private final static byte [] TOTALLY_EMPTY_CONFIGURATION = new byte[Fields.TOTAL_CONFIG_SIZE]; + private final static byte [] TOTALLY_EMPTY_OUTPUTS = new byte[Fields.TS_TOTAL_OUTPUT_SIZE]; + public static void main(String[] args) throws IOException { Listener serverSocketCreationCallback = parameter -> System.out.println("serverSocketCreationCallback"); CompatibleFunction socketRunnableFactory = new CompatibleFunction() { @@ -71,7 +78,10 @@ public class TcpServerSandbox { } else if (command == Fields.TS_PAGE_COMMAND) { stream.sendPacket(TS_OK.getBytes()); } else if (command == Fields.TS_CRC_CHECK_COMMAND) { - throw new IllegalStateException("Need CRC"); + stream.sendPacket(BinaryProtocolServer.createCrcResponse(TOTALLY_EMPTY_CONFIGURATION)); + } else if (command == Fields.TS_OUTPUT_COMMAND) { + byte[] response = getOutputCommandResponse(payload, TOTALLY_EMPTY_OUTPUTS); + stream.sendPacket(response); } else if (command == Fields.TS_READ_COMMAND) { DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1)); int offset = swap16(dis.readShort()); @@ -83,6 +93,6 @@ public class TcpServerSandbox { } else if (command == Fields.TS_GET_FIRMWARE_VERSION) { stream.sendPacket((TS_OK + "rusEFI proxy").getBytes()); } else - throw new UnsupportedOperationException("Unsupported command " + command); + throw new UnsupportedOperationException("Unsupported command " + BinaryProtocol.findCommand(command)); } } diff --git a/java_tools/proxy_server/src/test/java/com/rusefi/BackendTestHelper.java b/java_tools/proxy_server/src/test/java/com/rusefi/BackendTestHelper.java index 9da3197c51..497d1e2f96 100644 --- a/java_tools/proxy_server/src/test/java/com/rusefi/BackendTestHelper.java +++ b/java_tools/proxy_server/src/test/java/com/rusefi/BackendTestHelper.java @@ -43,7 +43,7 @@ public class BackendTestHelper { public static void commonServerTest() throws MalformedURLException { HttpUtil.RUSEFI_PROXY_HOSTNAME = TcpConnector.LOCALHOST; - BinaryProtocol.DISABLE_LOCAL_CACHE = true; + BinaryProtocol.DISABLE_LOCAL_CONFIGURATION_CACHE = true; rusEFISSLContext.init("certificate/test_pkcs12.jks", "password"); }