.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;
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;

View File

@ -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<CompositeEvent> 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);

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;
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);
}

View File

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

View File

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

View File

@ -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];

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);
}
public static void downloadIfNotAvailable(Pair<String, String> p) {
public static String downloadIfNotAvailable(Pair<String, String> 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;
}
}
}

View File

@ -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<Entry> data = Entry.parseBuffer(packet);

View File

@ -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);

View File

@ -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<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());
log.info("User " + userDetails);