TS SD integration #1653

This commit is contained in:
rusefi 2020-07-31 23:53:56 -04:00
parent afe56a64c8
commit 239ebbadcf
2 changed files with 97 additions and 2 deletions

View File

@ -348,6 +348,7 @@ enable2ndByteCanID = false
; we use this to match logs to tunes
tuneCrc16 = scalar, U16, 244, "crc16", 1, 0
sd_status = scalar, U08, 246, "", 1.0, 0.0
;
; see TunerStudioOutputChannels struct
@ -1043,6 +1044,11 @@ gaugeCategory = Sensors - Raw
indicator = { isIgnitionEnabledIndicator}, "no ignition", "ignition", yellow, black, white, black
indicator = { ind_injection_enabled}, "no injection", "injection", yellow, black, white, black
indicator = { ind_isTriggerError}, "trigger ok", "trigger err", white, black, red, black
indicator = { sd_status & 1}, "No SD", "SD in", white, black, green, black
indicator = { sd_status & 4}, "SD ready", "SD ready", white, black, green, black
indicator = { sd_status & 8}, "SD Log", "SD Log", white, black, green, black
indicator = { sd_status & 16}, "SD Err", "SD Err", white, black, red, black
; this is required so that the "config error" feature works in TS
; don't change this line - TS is looking for an indicator with particular text/styling
@ -1202,6 +1208,9 @@ gaugeCategory = Sensors - Raw
[Menu]
menuDialog = main
menu = "Data Logging"
subMenu = std_ms3SdConsole, "Browse / Import SD Card"
menu = "&Base &Engine"
subMenu = engineChars, "Base engine"
subMenu = triggerConfiguration, "Trigger"

View File

@ -16,6 +16,9 @@ import org.jetbrains.annotations.NotNull;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadFactory;
@ -25,8 +28,7 @@ import java.util.function.Function;
import static com.devexperts.logging.Logging.getLogging;
import static com.rusefi.binaryprotocol.IoHelper.swap16;
import static com.rusefi.config.generated.Fields.TS_PROTOCOL;
import static com.rusefi.config.generated.Fields.TS_RESPONSE_BURN_OK;
import static com.rusefi.config.generated.Fields.*;
/**
* This class makes rusEfi console a proxy for other tuning software, this way we can have two tools connected via same
@ -41,6 +43,9 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
private static final int DEFAULT_PROXY_PORT = 2390;
public static final String TS_OK = "\0";
private final static boolean MOCK_SD_CARD = true;
private static final int SD_STATUS_OFFSET = 246;
public AtomicInteger unknownCommands = new AtomicInteger();
public static final ServerSocketFunction SECURE_SOCKET_FACTORY = rusEFISSLContext::getSSLServerSocket;
@ -173,6 +178,10 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
System.err.println("NOT IMPLEMENTED TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY relay");
// todo: relay command
stream.sendPacket(TS_OK.getBytes());
} else if (command == TS_SD_R_COMMAND) {
handleSD_R_command(stream, packet, payload);
} else if (command == TS_SD_W_COMMAND) {
handleSD_W_command(stream, packet, payload);
} else if (command == Fields.TS_OUTPUT_COMMAND) {
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(payload, 1, payload.length - 1));
int offset = swap16(dis.readShort());
@ -183,6 +192,8 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
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);
stream.sendPacket(response);
@ -198,6 +209,81 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
}
}
private void handleSD_W_command(TcpIoStream stream, Packet packet, byte[] payload) throws IOException {
if (payload[1] == 0 && payload[2] == 0x11) {
if (payload[6] == 1) {
log.info("TS_SD: do command " + payload[payload.length - 1]);
byte[] response = new byte[1];
stream.sendPacket(response);
return;
}
if (payload[6] == 2) {
log.info("TS_SD: read directory command " + payload[payload.length - 1]);
byte[] response = new byte[1];
stream.sendPacket(response);
return;
}
}
log.info("TS_SD: Got unexpected w " + Arrays.toString(packet.packet));
}
private void handleSD_R_command(TcpIoStream stream, Packet packet, byte[] payload) throws IOException {
if (payload[1] == 0 && payload[2] == 7) {
log.info("TS_SD: RTC read command");
byte[] response = new byte[9];
stream.sendPacket(response);
return;
}
if (payload[1] == 0 && payload[2] == 0x11) {
ByteBuffer bb = ByteBuffer.wrap(payload, 5, 2);
bb.order(ByteOrder.BIG_ENDIAN);
int bufferLength = bb.getShort();
log.info("TS_SD: fetch buffer command " + bufferLength);
byte[] response = new byte[1 + bufferLength];
response[0] = TS_RESPONSE_OK;
if (bufferLength == 16) {
response[1] = 1 + 4; // Card present + Ready
response[2] = 0; // Y - error code
response[3] = 2; // higher byte of '512' sector size
response[4] = 0; // lower byte
response[5] = 0;
response[6] = 0x20; // 0x20 00 00 of 512 is 1G virtual card
response[7] = 0;
response[8] = 0;
response[9] = 0;
response[10] = 1; // number of files
} else {
System.arraycopy("hello123mlq".getBytes(), 0, response, 1, 11);
response[1 + 11] = 1; // file
response[1 + 23] = 3; // sector number
response[1 + 24] = (byte) 0; // size
response[1 + 25] = (byte) 0; // size
response[1 + 30] = (byte) 128;
response[1 + 31] = (byte) 4; // size
}
stream.sendPacket(response);
return;
}
log.info("TS_SD: Got unexpected r " + Arrays.toString(packet.packet));
}
public static int getPacketLength(IncomingDataBuffer in, Handler protocolCommandHandler) throws IOException {
return getPacketLength(in, protocolCommandHandler, Timeouts.BINARY_IO_TIMEOUT);
}