.ini for remote controller

This commit is contained in:
rusefillc 2020-10-04 18:55:23 -04:00
parent aacb5184ad
commit f63ad0001e
11 changed files with 37 additions and 53 deletions

View File

@ -1,7 +1,6 @@
package com.rusefi.autodetect; package com.rusefi.autodetect;
import com.devexperts.logging.Logging; import com.devexperts.logging.Logging;
import com.rusefi.binaryprotocol.BinaryProtocolCommands;
import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.config.generated.Fields; import com.rusefi.config.generated.Fields;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
@ -42,7 +41,7 @@ public class SerialAutoChecker implements Runnable {
try { try {
HelloCommand.send(stream); HelloCommand.send(stream);
byte[] response = incomingData.getPacket(""); byte[] response = incomingData.getPacket("");
if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK)) if (!checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK))
return; return;
String signature = new String(response, 1, response.length - 1); String signature = new String(response, 1, response.length - 1);
SIGNATURE = signature; SIGNATURE = signature;

View File

@ -50,7 +50,7 @@ import static com.rusefi.config.generated.Fields.*;
* Andrey Belomutskiy, (c) 2013-2020 * Andrey Belomutskiy, (c) 2013-2020
* 3/6/2015 * 3/6/2015
*/ */
public class BinaryProtocol implements BinaryProtocolCommands { public class BinaryProtocol {
private static final Logging log = getLogging(BinaryProtocol.class); private static final Logging log = getLogging(BinaryProtocol.class);
private static final ThreadFactory THREAD_FACTORY = new NamedThreadFactory("text pull"); 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); 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 code = (response == null || response.length == 0) ? "empty" : "code " + getCode(response);
String info = response == null ? "NO RESPONSE" : (code + " size " + response.length); String info = response == null ? "NO RESPONSE" : (code + " size " + response.length);
log.info("readImage: ERROR UNEXPECTED Something is wrong, retrying... " + info); 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)); log.info(String.format(CONFIGURATION_RUSEFI_BINARY + " Local cache CRC %x\n", crcOfLocallyCachedConfiguration));
byte packet[] = new byte[5]; 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, 1, swap16(/*offset = */ 0));
putShort(packet, 3, swap16(localCached.getSize())); putShort(packet, 3, swap16(localCached.getSize()));
byte[] response = executeCommand(packet, "get CRC32"); 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); ByteBuffer bb = ByteBuffer.wrap(response, 1, 4);
// that's unusual - most of the protocol is LITTLE_ENDIAN // that's unusual - most of the protocol is LITTLE_ENDIAN
bb.order(ByteOrder.BIG_ENDIAN); bb.order(ByteOrder.BIG_ENDIAN);
@ -494,7 +494,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
isBurnPending = true; isBurnPending = true;
byte packet[] = new byte[5 + size]; 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, 1, swap16(offset));
putShort(packet, 3, swap16(size)); putShort(packet, 3, swap16(size));
@ -503,7 +503,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
while (!isClosed && (System.currentTimeMillis() - start < Timeouts.BINARY_IO_TIMEOUT)) { while (!isClosed && (System.currentTimeMillis() - start < Timeouts.BINARY_IO_TIMEOUT)) {
byte[] response = executeCommand(packet, "writeImage"); 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..."); log.error("writeData: Something is wrong, retrying...");
continue; continue;
} }
@ -519,8 +519,8 @@ public class BinaryProtocol implements BinaryProtocolCommands {
while (true) { while (true) {
if (isClosed) if (isClosed)
return; return;
byte[] response = executeCommand(new byte[]{COMMAND_BURN}, "burn"); byte[] response = executeCommand(new byte[]{Fields.TS_BURN_COMMAND}, "burn");
if (!checkResponseCode(response, RESPONSE_BURN_OK) || response.length != 1) { if (!checkResponseCode(response, (byte) Fields.TS_RESPONSE_BURN_OK) || response.length != 1) {
continue; continue;
} }
break; break;
@ -556,7 +556,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
while (!isClosed && (System.currentTimeMillis() - start < Timeouts.BINARY_IO_TIMEOUT)) { while (!isClosed && (System.currentTimeMillis() - start < Timeouts.BINARY_IO_TIMEOUT)) {
byte[] response = executeCommand(command, "execute", false); 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; continue;
} }
return false; return false;
@ -597,7 +597,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
isCompositeLoggerEnabled = true; isCompositeLoggerEnabled = true;
byte[] response = executeCommand(packet, "composite log", true); byte[] response = executeCommand(packet, "composite log", true);
if (checkResponseCode(response, RESPONSE_OK)) { if (checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK)) {
List<CompositeEvent> events = CompositeParser.parse(response); List<CompositeEvent> events = CompositeParser.parse(response);
createCompositesIfNeeded(); createCompositesIfNeeded();
for (StreamFile composite : compositeLogs) for (StreamFile composite : compositeLogs)
@ -612,7 +612,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
byte[] packet = GetOutputsCommand.createRequest(); byte[] packet = GetOutputsCommand.createRequest();
byte[] response = executeCommand(packet, "output channels", false); 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; return false;
state.setCurrentOutputs(response); state.setCurrentOutputs(response);

View File

@ -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
}

View File

@ -1,6 +1,5 @@
package com.rusefi.io.commands; package com.rusefi.io.commands;
import com.rusefi.binaryprotocol.BinaryProtocolCommands;
import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.config.generated.Fields; import com.rusefi.config.generated.Fields;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
@ -26,7 +25,7 @@ public class HelloCommand implements Command {
@Nullable @Nullable
public static String getHelloResponse(IncomingDataBuffer incomingData) throws EOFException { public static String getHelloResponse(IncomingDataBuffer incomingData) throws EOFException {
byte[] response = incomingData.getPacket("[hello]", true); byte[] response = incomingData.getPacket("[hello]", true);
if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK)) if (!checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK))
return null; return null;
return new String(response, 1, response.length - 1); return new String(response, 1, response.length - 1);
} }

View File

@ -16,10 +16,8 @@ import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import static com.devexperts.logging.Logging.getLogging; 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.config.generated.Fields.TS_PROTOCOL;
import static com.rusefi.shared.FileUtil.close; import static com.rusefi.shared.FileUtil.close;
@ -52,7 +50,7 @@ public class BinaryProtocolProxy {
*/ */
while (!targetEcu.isClosed()) { while (!targetEcu.isClosed()) {
byte firstByte = clientStream.getDataBuffer().readByte(timeoutMs); byte firstByte = clientStream.getDataBuffer().readByte(timeoutMs);
if (firstByte == COMMAND_PROTOCOL) { if (firstByte == Fields.TS_COMMAND_F) {
clientStream.write(TS_PROTOCOL.getBytes()); clientStream.write(TS_PROTOCOL.getBytes());
clientStream.flush(); clientStream.flush();
continue; continue;

View File

@ -40,7 +40,7 @@ import static com.rusefi.config.generated.Fields.*;
* 11/24/15 * 11/24/15
*/ */
public class BinaryProtocolServer implements BinaryProtocolCommands { public class BinaryProtocolServer {
public static final String TEST_FILE = "test_log.mlg.Z"; public static final String TEST_FILE = "test_log.mlg.Z";
private static final Logging log = getLogging(BinaryProtocolServer.class); private static final Logging log = getLogging(BinaryProtocolServer.class);
private static final int DEFAULT_PROXY_PORT = 2390; private static final int DEFAULT_PROXY_PORT = 2390;
@ -169,11 +169,11 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
if (command == Fields.TS_HELLO_COMMAND) { if (command == Fields.TS_HELLO_COMMAND) {
new HelloCommand(Fields.TS_SIGNATURE).handle(stream); 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()); stream.sendPacket((TS_OK + TS_PROTOCOL).getBytes());
} else if (command == Fields.TS_GET_FIRMWARE_VERSION) { } else if (command == Fields.TS_GET_FIRMWARE_VERSION) {
stream.sendPacket((TS_OK + "rusEFI proxy").getBytes()); 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); handleCrc(linkManager, stream);
} else if (command == Fields.TS_PAGE_COMMAND) { } else if (command == Fields.TS_PAGE_COMMAND) {
stream.sendPacket(TS_OK.getBytes()); 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 { public static int getPacketLength(IncomingDataBuffer in, Handler protocolCommandHandler, int ioTimeout) throws IOException {
byte first = in.readByte(ioTimeout); byte first = in.readByte(ioTimeout);
if (first == COMMAND_PROTOCOL) { if (first == Fields.TS_COMMAND_F) {
protocolCommandHandler.handle(); protocolCommandHandler.handle();
return 0; return 0;
} }

View File

@ -2,13 +2,12 @@ package com.rusefi.ui.livedocs;
import com.rusefi.binaryprotocol.BinaryProtocol; import com.rusefi.binaryprotocol.BinaryProtocol;
import com.rusefi.config.Field; import com.rusefi.config.Field;
import com.rusefi.config.generated.Fields;
import com.rusefi.ldmp.StateDictionary; import com.rusefi.ldmp.StateDictionary;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.putShort;
import static com.rusefi.binaryprotocol.IoHelper.swap16; import static com.rusefi.binaryprotocol.IoHelper.swap16;
@ -43,12 +42,12 @@ public enum LiveDocsRegistry {
int size = Field.getStructureSize(values); int size = Field.getStructureSize(values);
byte[] packet = new byte[5]; byte[] packet = new byte[5];
packet[0] = COMMAND_GET_STRUCT; packet[0] = Fields.TS_GET_STRUCT;
putShort(packet, 1, swap16(liveDocRequestId)); // offset putShort(packet, 1, swap16(liveDocRequestId)); // offset
putShort(packet, 3, swap16(size)); putShort(packet, 3, swap16(size));
byte[] responseWithCode = binaryProtocol.executeCommand(packet, "get LiveDoc"); 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; return;
byte[] response = new byte[size]; byte[] response = new byte[size];

View File

@ -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); return new Pair("https://rusefi.com/online/ini/rusefi/" + year + SLASH + month + SLASH + day + SLASH + bundle + SLASH + fileName, fileName);
} }
public static void downloadIfNotAvailable(Pair<String, String> p) { public static String downloadIfNotAvailable(Pair<String, String> p) {
if (p == null) if (p == null)
return; return null;
new File(LOCAL_INI).mkdirs(); new File(LOCAL_INI).mkdirs();
String localIniFile = LOCAL_INI + File.separator + p.second; String localIniFile = LOCAL_INI + File.separator + p.second;
File file = new File(localIniFile); File file = new File(localIniFile);
if (file.exists() && file.length() > 10000) if (file.exists() && file.length() > 10000)
return; return localIniFile;
try (BufferedInputStream in = new BufferedInputStream(new URL(p.first).openStream()); try (BufferedInputStream in = new BufferedInputStream(new URL(p.first).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(localIniFile)) { FileOutputStream fileOutputStream = new FileOutputStream(localIniFile)) {
byte[] dataBuffer = new byte[32 * 1024]; byte[] dataBuffer = new byte[32 * 1024];
@ -49,8 +49,10 @@ public class SignatureHelper {
while ((bytesRead = in.read(dataBuffer, 0, dataBuffer.length)) != -1) { while ((bytesRead = in.read(dataBuffer, 0, dataBuffer.length)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead); fileOutputStream.write(dataBuffer, 0, bytesRead);
} }
return localIniFile;
} catch (IOException e) { } catch (IOException e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
return null;
} }
} }
} }

View File

@ -13,7 +13,6 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import static com.rusefi.binaryprotocol.BinaryProtocolCommands.RESPONSE_OK;
import static com.rusefi.binaryprotocol.IoHelper.checkResponseCode; import static com.rusefi.binaryprotocol.IoHelper.checkResponseCode;
import static com.rusefi.tools.ConsoleTools.startAndConnect; import static com.rusefi.tools.ConsoleTools.startAndConnect;
@ -25,7 +24,7 @@ public class PerformanceTraceHelper {
Thread.sleep(500); Thread.sleep(500);
byte[] packet = bp.executeCommand(new byte[]{Fields.TS_PERF_TRACE_GET_BUFFER}, "get trace", true); 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"); throw new IllegalStateException("Unexpected packet");
List<Entry> data = Entry.parseBuffer(packet); List<Entry> data = Entry.parseBuffer(packet);

View File

@ -3,7 +3,6 @@ package com.rusefi.proxy.client;
import com.rusefi.BackendTestHelper; import com.rusefi.BackendTestHelper;
import com.rusefi.TestHelper; import com.rusefi.TestHelper;
import com.rusefi.Timeouts; import com.rusefi.Timeouts;
import com.rusefi.binaryprotocol.BinaryProtocolCommands;
import com.rusefi.config.generated.Fields; import com.rusefi.config.generated.Fields;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
import com.rusefi.io.commands.GetOutputsCommand; import com.rusefi.io.commands.GetOutputsCommand;
@ -78,14 +77,14 @@ public class LocalApplicationProxyTest {
byte[] protocolResponse = new byte[TS_PROTOCOL.length()]; byte[] protocolResponse = new byte[TS_PROTOCOL.length()];
// request // request
applicationConnection.write(new byte[] {BinaryProtocolCommands.COMMAND_PROTOCOL}); applicationConnection.write(new byte[] {Fields.TS_COMMAND_F});
applicationConnection.flush(); applicationConnection.flush();
// response // response
applicationConnection.getDataBuffer().read(protocolResponse); applicationConnection.getDataBuffer().read(protocolResponse);
assertArrayEquals(protocolResponse, TS_PROTOCOL.getBytes()); assertArrayEquals(protocolResponse, TS_PROTOCOL.getBytes());
// request again // request again
applicationConnection.write(new byte[] {BinaryProtocolCommands.COMMAND_PROTOCOL}); applicationConnection.write(new byte[] {Fields.TS_COMMAND_F});
applicationConnection.flush(); applicationConnection.flush();
// response again // response again
applicationConnection.getDataBuffer().read(protocolResponse); applicationConnection.getDataBuffer().read(protocolResponse);

View File

@ -1,6 +1,7 @@
package com.rusefi.server; package com.rusefi.server;
import com.devexperts.logging.Logging; import com.devexperts.logging.Logging;
import com.opensr5.ini.IniFileModel;
import com.rusefi.SignatureHelper; import com.rusefi.SignatureHelper;
import com.rusefi.auth.AuthTokenUtil; import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.binaryprotocol.IncomingDataBuffer; import com.rusefi.binaryprotocol.IncomingDataBuffer;
@ -41,6 +42,7 @@ public class ControllerConnectionState {
private final SensorsHolder sensorsHolder = new SensorsHolder(); private final SensorsHolder sensorsHolder = new SensorsHolder();
private final Birthday birthday = new Birthday(); private final Birthday birthday = new Birthday();
private int outputRoundAroundDuration; private int outputRoundAroundDuration;
private final IniFileModel iniFileModel = new IniFileModel();
public ControllerConnectionState(Socket clientSocket, UserDetailsResolver userDetailsResolver) { public ControllerConnectionState(Socket clientSocket, UserDetailsResolver userDetailsResolver) {
this.clientSocket = clientSocket; this.clientSocket = clientSocket;
@ -104,7 +106,12 @@ public class ControllerConnectionState {
throw new IOException("Unable to resolve " + sessionDetails.getAuthToken()); throw new IOException("Unable to resolve " + sessionDetails.getAuthToken());
} }
Pair<String, String> p = SignatureHelper.getUrl(sessionDetails.getControllerInfo().getSignature()); Pair<String, String> 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()); controllerKey = new ControllerKey(userDetails.getUserId(), sessionDetails.getControllerInfo());
log.info("User " + userDetails); log.info("User " + userDetails);