From 4b17adf1856973f9d5199e66d7ab1bdb7a4e56bf Mon Sep 17 00:00:00 2001 From: rusefillc Date: Sun, 4 Oct 2020 18:55:23 -0400 Subject: [PATCH] .ini for remote controller --- .../rusefi/autodetect/SerialAutoChecker.java | 3 +-- .../rusefi/binaryprotocol/BinaryProtocol.java | 22 +++++++++---------- .../BinaryProtocolCommands.java | 18 --------------- .../com/rusefi/io/commands/HelloCommand.java | 3 +-- .../rusefi/io/tcp/BinaryProtocolProxy.java | 4 +--- .../rusefi/io/tcp/BinaryProtocolServer.java | 8 +++---- .../rusefi/ui/livedocs/LiveDocsRegistry.java | 7 +++--- .../main/java/com/rusefi/SignatureHelper.java | 8 ++++--- .../com/rusefi/PerformanceTraceHelper.java | 3 +-- .../client/LocalApplicationProxyTest.java | 5 ++--- .../server/ControllerConnectionState.java | 9 +++++++- 11 files changed, 37 insertions(+), 53 deletions(-) delete mode 100644 java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocolCommands.java diff --git a/java_console/io/src/main/java/com/rusefi/autodetect/SerialAutoChecker.java b/java_console/io/src/main/java/com/rusefi/autodetect/SerialAutoChecker.java index e1e4f2acb8..9345881b15 100644 --- a/java_console/io/src/main/java/com/rusefi/autodetect/SerialAutoChecker.java +++ b/java_console/io/src/main/java/com/rusefi/autodetect/SerialAutoChecker.java @@ -1,7 +1,6 @@ package com.rusefi.autodetect; import com.devexperts.logging.Logging; -import com.rusefi.binaryprotocol.BinaryProtocolCommands; import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.config.generated.Fields; import com.rusefi.io.IoStream; @@ -42,7 +41,7 @@ public class SerialAutoChecker implements Runnable { try { HelloCommand.send(stream); byte[] response = incomingData.getPacket(""); - if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK)) + if (!checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK)) return; String signature = new String(response, 1, response.length - 1); SIGNATURE = signature; 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 9be37a33e7..587e490b9a 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 @@ -50,7 +50,7 @@ import static com.rusefi.config.generated.Fields.*; * Andrey Belomutskiy, (c) 2013-2020 * 3/6/2015 */ -public class BinaryProtocol implements BinaryProtocolCommands { +public class BinaryProtocol { private static final Logging log = getLogging(BinaryProtocol.class); private static final ThreadFactory THREAD_FACTORY = new NamedThreadFactory("text pull"); @@ -380,7 +380,7 @@ public class BinaryProtocol implements BinaryProtocolCommands { byte[] response = executeCommand(packet, "load image offset=" + offset); - if (!checkResponseCode(response, RESPONSE_OK) || response.length != requestSize + 1) { + if (!checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK) || response.length != requestSize + 1) { String code = (response == null || response.length == 0) ? "empty" : "code " + getCode(response); String info = response == null ? "NO RESPONSE" : (code + " size " + response.length); log.info("readImage: ERROR UNEXPECTED Something is wrong, retrying... " + info); @@ -434,12 +434,12 @@ public class BinaryProtocol implements BinaryProtocolCommands { log.info(String.format(CONFIGURATION_RUSEFI_BINARY + " Local cache CRC %x\n", crcOfLocallyCachedConfiguration)); byte packet[] = new byte[5]; - packet[0] = COMMAND_CRC_CHECK_COMMAND; + packet[0] = Fields.TS_CRC_CHECK_COMMAND; putShort(packet, 1, swap16(/*offset = */ 0)); putShort(packet, 3, swap16(localCached.getSize())); byte[] response = executeCommand(packet, "get CRC32"); - if (checkResponseCode(response, RESPONSE_OK) && response.length == 5) { + if (checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK) && response.length == 5) { ByteBuffer bb = ByteBuffer.wrap(response, 1, 4); // that's unusual - most of the protocol is LITTLE_ENDIAN bb.order(ByteOrder.BIG_ENDIAN); @@ -494,7 +494,7 @@ public class BinaryProtocol implements BinaryProtocolCommands { isBurnPending = true; byte packet[] = new byte[5 + size]; - packet[0] = COMMAND_CHUNK_WRITE; + packet[0] = Fields.TS_CHUNK_WRITE_COMMAND; putShort(packet, 1, swap16(offset)); putShort(packet, 3, swap16(size)); @@ -503,7 +503,7 @@ public class BinaryProtocol implements BinaryProtocolCommands { long start = System.currentTimeMillis(); while (!isClosed && (System.currentTimeMillis() - start < Timeouts.BINARY_IO_TIMEOUT)) { byte[] response = executeCommand(packet, "writeImage"); - if (!checkResponseCode(response, RESPONSE_OK) || response.length != 1) { + if (!checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK) || response.length != 1) { log.error("writeData: Something is wrong, retrying..."); continue; } @@ -519,8 +519,8 @@ public class BinaryProtocol implements BinaryProtocolCommands { while (true) { if (isClosed) return; - byte[] response = executeCommand(new byte[]{COMMAND_BURN}, "burn"); - if (!checkResponseCode(response, RESPONSE_BURN_OK) || response.length != 1) { + byte[] response = executeCommand(new byte[]{Fields.TS_BURN_COMMAND}, "burn"); + if (!checkResponseCode(response, (byte) Fields.TS_RESPONSE_BURN_OK) || response.length != 1) { continue; } break; @@ -556,7 +556,7 @@ public class BinaryProtocol implements BinaryProtocolCommands { long start = System.currentTimeMillis(); while (!isClosed && (System.currentTimeMillis() - start < Timeouts.BINARY_IO_TIMEOUT)) { byte[] response = executeCommand(command, "execute", false); - if (!checkResponseCode(response, RESPONSE_COMMAND_OK) || response.length != 1) { + if (!checkResponseCode(response, (byte) Fields.TS_RESPONSE_COMMAND_OK) || response.length != 1) { continue; } return false; @@ -597,7 +597,7 @@ public class BinaryProtocol implements BinaryProtocolCommands { isCompositeLoggerEnabled = true; byte[] response = executeCommand(packet, "composite log", true); - if (checkResponseCode(response, RESPONSE_OK)) { + if (checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK)) { List events = CompositeParser.parse(response); createCompositesIfNeeded(); for (StreamFile composite : compositeLogs) @@ -612,7 +612,7 @@ public class BinaryProtocol implements BinaryProtocolCommands { byte[] packet = GetOutputsCommand.createRequest(); byte[] response = executeCommand(packet, "output channels", false); - if (response == null || response.length != (Fields.TS_OUTPUT_SIZE + 1) || response[0] != RESPONSE_OK) + if (response == null || response.length != (Fields.TS_OUTPUT_SIZE + 1) || response[0] != Fields.TS_RESPONSE_OK) return false; state.setCurrentOutputs(response); diff --git a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocolCommands.java b/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocolCommands.java deleted file mode 100644 index 14c6c7cdb5..0000000000 --- a/java_console/io/src/main/java/com/rusefi/binaryprotocol/BinaryProtocolCommands.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.rusefi.binaryprotocol; - -import com.rusefi.config.generated.Fields; - -/** - * Andrey Belomutskiy, (c) 2013-2020 - * 6/21/2017. - */ -public interface BinaryProtocolCommands { - byte RESPONSE_OK = Fields.TS_RESPONSE_OK; - byte RESPONSE_BURN_OK = Fields.TS_RESPONSE_BURN_OK; - byte RESPONSE_COMMAND_OK = Fields.TS_RESPONSE_COMMAND_OK; - char COMMAND_PROTOCOL = Fields.TS_COMMAND_F; - char COMMAND_CRC_CHECK_COMMAND = Fields.TS_CRC_CHECK_COMMAND; - char COMMAND_CHUNK_WRITE = Fields.TS_CHUNK_WRITE_COMMAND; - char COMMAND_BURN = Fields.TS_BURN_COMMAND; - char COMMAND_GET_STRUCT = '9'; // TS_GET_STRUCT -} diff --git a/java_console/io/src/main/java/com/rusefi/io/commands/HelloCommand.java b/java_console/io/src/main/java/com/rusefi/io/commands/HelloCommand.java index e1eb87f527..d73287f88b 100644 --- a/java_console/io/src/main/java/com/rusefi/io/commands/HelloCommand.java +++ b/java_console/io/src/main/java/com/rusefi/io/commands/HelloCommand.java @@ -1,6 +1,5 @@ package com.rusefi.io.commands; -import com.rusefi.binaryprotocol.BinaryProtocolCommands; import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.config.generated.Fields; import com.rusefi.io.IoStream; @@ -26,7 +25,7 @@ public class HelloCommand implements Command { @Nullable public static String getHelloResponse(IncomingDataBuffer incomingData) throws EOFException { byte[] response = incomingData.getPacket("[hello]", true); - if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK)) + if (!checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK)) return null; return new String(response, 1, response.length - 1); } diff --git a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java index 3b0076052f..6d765af04f 100644 --- a/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java +++ b/java_console/io/src/main/java/com/rusefi/io/tcp/BinaryProtocolProxy.java @@ -16,10 +16,8 @@ import java.io.DataInputStream; import java.io.IOException; import java.net.Socket; import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Function; import static com.devexperts.logging.Logging.getLogging; -import static com.rusefi.binaryprotocol.BinaryProtocolCommands.COMMAND_PROTOCOL; import static com.rusefi.config.generated.Fields.TS_PROTOCOL; import static com.rusefi.shared.FileUtil.close; @@ -52,7 +50,7 @@ public class BinaryProtocolProxy { */ while (!targetEcu.isClosed()) { byte firstByte = clientStream.getDataBuffer().readByte(timeoutMs); - if (firstByte == COMMAND_PROTOCOL) { + if (firstByte == Fields.TS_COMMAND_F) { clientStream.write(TS_PROTOCOL.getBytes()); clientStream.flush(); continue; 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 0f64eda675..aba413ce21 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 @@ -40,7 +40,7 @@ import static com.rusefi.config.generated.Fields.*; * 11/24/15 */ -public class BinaryProtocolServer implements BinaryProtocolCommands { +public class BinaryProtocolServer { public static final String TEST_FILE = "test_log.mlg.Z"; private static final Logging log = getLogging(BinaryProtocolServer.class); private static final int DEFAULT_PROXY_PORT = 2390; @@ -169,11 +169,11 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { if (command == Fields.TS_HELLO_COMMAND) { new HelloCommand(Fields.TS_SIGNATURE).handle(stream); - } else if (command == COMMAND_PROTOCOL) { + } else if (command == Fields.TS_COMMAND_F) { stream.sendPacket((TS_OK + TS_PROTOCOL).getBytes()); } else if (command == Fields.TS_GET_FIRMWARE_VERSION) { stream.sendPacket((TS_OK + "rusEFI proxy").getBytes()); - } else if (command == COMMAND_CRC_CHECK_COMMAND) { + } else if (command == Fields.TS_CRC_CHECK_COMMAND) { handleCrc(linkManager, stream); } else if (command == Fields.TS_PAGE_COMMAND) { stream.sendPacket(TS_OK.getBytes()); @@ -363,7 +363,7 @@ public class BinaryProtocolServer implements BinaryProtocolCommands { public static int getPacketLength(IncomingDataBuffer in, Handler protocolCommandHandler, int ioTimeout) throws IOException { byte first = in.readByte(ioTimeout); - if (first == COMMAND_PROTOCOL) { + if (first == Fields.TS_COMMAND_F) { protocolCommandHandler.handle(); return 0; } diff --git a/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocsRegistry.java b/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocsRegistry.java index 50ad4424b6..085c845fdf 100644 --- a/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocsRegistry.java +++ b/java_console/io/src/main/java/com/rusefi/ui/livedocs/LiveDocsRegistry.java @@ -2,13 +2,12 @@ package com.rusefi.ui.livedocs; import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.config.Field; +import com.rusefi.config.generated.Fields; import com.rusefi.ldmp.StateDictionary; import java.util.ArrayList; import java.util.List; -import static com.rusefi.binaryprotocol.BinaryProtocolCommands.COMMAND_GET_STRUCT; -import static com.rusefi.binaryprotocol.BinaryProtocolCommands.RESPONSE_OK; import static com.rusefi.binaryprotocol.IoHelper.putShort; import static com.rusefi.binaryprotocol.IoHelper.swap16; @@ -43,12 +42,12 @@ public enum LiveDocsRegistry { int size = Field.getStructureSize(values); byte[] packet = new byte[5]; - packet[0] = COMMAND_GET_STRUCT; + packet[0] = Fields.TS_GET_STRUCT; putShort(packet, 1, swap16(liveDocRequestId)); // offset putShort(packet, 3, swap16(size)); byte[] responseWithCode = binaryProtocol.executeCommand(packet, "get LiveDoc"); - if (responseWithCode == null || responseWithCode.length != (size + 1) || responseWithCode[0] != RESPONSE_OK) + if (responseWithCode == null || responseWithCode.length != (size + 1) || responseWithCode[0] != Fields.TS_RESPONSE_OK) return; byte[] response = new byte[size]; diff --git a/java_console/shared_io/src/main/java/com/rusefi/SignatureHelper.java b/java_console/shared_io/src/main/java/com/rusefi/SignatureHelper.java index f1f5266481..1e94df1057 100644 --- a/java_console/shared_io/src/main/java/com/rusefi/SignatureHelper.java +++ b/java_console/shared_io/src/main/java/com/rusefi/SignatureHelper.java @@ -34,14 +34,14 @@ public class SignatureHelper { return new Pair("https://rusefi.com/online/ini/rusefi/" + year + SLASH + month + SLASH + day + SLASH + bundle + SLASH + fileName, fileName); } - public static void downloadIfNotAvailable(Pair p) { + public static String downloadIfNotAvailable(Pair p) { if (p == null) - return; + return null; new File(LOCAL_INI).mkdirs(); String localIniFile = LOCAL_INI + File.separator + p.second; File file = new File(localIniFile); if (file.exists() && file.length() > 10000) - return; + return localIniFile; try (BufferedInputStream in = new BufferedInputStream(new URL(p.first).openStream()); FileOutputStream fileOutputStream = new FileOutputStream(localIniFile)) { byte[] dataBuffer = new byte[32 * 1024]; @@ -49,8 +49,10 @@ public class SignatureHelper { while ((bytesRead = in.read(dataBuffer, 0, dataBuffer.length)) != -1) { fileOutputStream.write(dataBuffer, 0, bytesRead); } + return localIniFile; } catch (IOException e) { System.err.println(e.getMessage()); + return null; } } } diff --git a/java_console/ui/src/main/java/com/rusefi/PerformanceTraceHelper.java b/java_console/ui/src/main/java/com/rusefi/PerformanceTraceHelper.java index 73c9bacd0b..e728d86d33 100644 --- a/java_console/ui/src/main/java/com/rusefi/PerformanceTraceHelper.java +++ b/java_console/ui/src/main/java/com/rusefi/PerformanceTraceHelper.java @@ -13,7 +13,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.List; -import static com.rusefi.binaryprotocol.BinaryProtocolCommands.RESPONSE_OK; import static com.rusefi.binaryprotocol.IoHelper.checkResponseCode; import static com.rusefi.tools.ConsoleTools.startAndConnect; @@ -25,7 +24,7 @@ public class PerformanceTraceHelper { Thread.sleep(500); byte[] packet = bp.executeCommand(new byte[]{Fields.TS_PERF_TRACE_GET_BUFFER}, "get trace", true); - if (!checkResponseCode(packet, RESPONSE_OK) || ((packet.length - 1) % 8) != 0) + if (!checkResponseCode(packet, (byte) Fields.TS_RESPONSE_OK) || ((packet.length - 1) % 8) != 0) throw new IllegalStateException("Unexpected packet"); List data = Entry.parseBuffer(packet); diff --git a/java_console/ui/src/test/java/com/rusefi/proxy/client/LocalApplicationProxyTest.java b/java_console/ui/src/test/java/com/rusefi/proxy/client/LocalApplicationProxyTest.java index 6f0da58b02..f61937efed 100644 --- a/java_console/ui/src/test/java/com/rusefi/proxy/client/LocalApplicationProxyTest.java +++ b/java_console/ui/src/test/java/com/rusefi/proxy/client/LocalApplicationProxyTest.java @@ -3,7 +3,6 @@ package com.rusefi.proxy.client; import com.rusefi.BackendTestHelper; import com.rusefi.TestHelper; import com.rusefi.Timeouts; -import com.rusefi.binaryprotocol.BinaryProtocolCommands; import com.rusefi.config.generated.Fields; import com.rusefi.io.IoStream; import com.rusefi.io.commands.GetOutputsCommand; @@ -78,14 +77,14 @@ public class LocalApplicationProxyTest { byte[] protocolResponse = new byte[TS_PROTOCOL.length()]; // request - applicationConnection.write(new byte[] {BinaryProtocolCommands.COMMAND_PROTOCOL}); + applicationConnection.write(new byte[] {Fields.TS_COMMAND_F}); applicationConnection.flush(); // response applicationConnection.getDataBuffer().read(protocolResponse); assertArrayEquals(protocolResponse, TS_PROTOCOL.getBytes()); // request again - applicationConnection.write(new byte[] {BinaryProtocolCommands.COMMAND_PROTOCOL}); + applicationConnection.write(new byte[] {Fields.TS_COMMAND_F}); applicationConnection.flush(); // response again applicationConnection.getDataBuffer().read(protocolResponse); diff --git a/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java b/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java index 8ae9e43e09..353c2a04b9 100644 --- a/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java +++ b/java_tools/proxy_server/src/main/java/com/rusefi/server/ControllerConnectionState.java @@ -1,6 +1,7 @@ package com.rusefi.server; import com.devexperts.logging.Logging; +import com.opensr5.ini.IniFileModel; import com.rusefi.SignatureHelper; import com.rusefi.auth.AuthTokenUtil; import com.rusefi.binaryprotocol.IncomingDataBuffer; @@ -41,6 +42,7 @@ public class ControllerConnectionState { private final SensorsHolder sensorsHolder = new SensorsHolder(); private final Birthday birthday = new Birthday(); private int outputRoundAroundDuration; + private final IniFileModel iniFileModel = new IniFileModel(); public ControllerConnectionState(Socket clientSocket, UserDetailsResolver userDetailsResolver) { this.clientSocket = clientSocket; @@ -104,7 +106,12 @@ public class ControllerConnectionState { throw new IOException("Unable to resolve " + sessionDetails.getAuthToken()); } Pair p = SignatureHelper.getUrl(sessionDetails.getControllerInfo().getSignature()); - SignatureHelper.downloadIfNotAvailable(p); + if (p == null) + throw new IOException("Invalid signature response"); + String localFileName = SignatureHelper.downloadIfNotAvailable(p); + if (localFileName == null) + throw new IOException("Unable to download " + p.second); + iniFileModel.readIniFile(localFileName); controllerKey = new ControllerKey(userDetails.getUserId(), sessionDetails.getControllerInfo()); log.info("User " + userDetails);