auto-sync

This commit is contained in:
rusEfi 2015-11-25 20:01:31 -05:00
parent 7a50f0489b
commit 50449a276f
4 changed files with 27 additions and 14 deletions

View File

@ -1,7 +1,6 @@
package com.rusefi.binaryprotocol;
import com.rusefi.*;
import com.rusefi.config.Field;
import com.rusefi.config.FieldType;
import com.rusefi.core.MessagesCentral;
import com.rusefi.core.Pair;
@ -48,6 +47,8 @@ public class BinaryProtocol {
* See SWITCH_TO_BINARY_COMMAND in firmware source code
*/
private static final String SWITCH_TO_BINARY_COMMAND = "~";
public static final char COMMAND_OUTPUTS = 'O';
public static final char COMMAND_HELLO = 'S';
private final Logger logger;
private final IoStream stream;
@ -404,8 +405,11 @@ public class BinaryProtocol {
}
}
private void sendCrcPacket(byte[] command) throws IOException {
sendCrcPacket(command, logger, stream);
}
public static void sendCrcPacket(byte[] command, Logger logger, IoStream stream) throws IOException {
byte[] packet = IoHelper.makeCrc32Packet(command);
logger.info("Sending " + Arrays.toString(packet));
stream.write(packet);
@ -451,7 +455,7 @@ public class BinaryProtocol {
if (isClosed)
return;
// try {
byte[] response = executeCommand(new byte[]{'O'}, "output channels", false);
byte[] response = executeCommand(new byte[]{COMMAND_OUTPUTS}, "output channels", false);
if (response == null || response.length != (OUTPUT_CHANNELS_SIZE + 1) || response[0] != RESPONSE_OK)
return;

View File

@ -11,7 +11,7 @@ import java.util.zip.CRC32;
public class IoHelper {
public static int crc32(byte[] packet) {
CRC32 c = new CRC32();
c.update(packet, 0, packet.length);
c.update(packet);
return (int) c.getValue();
}

View File

@ -1,10 +1,11 @@
package com.rusefi.io.tcp;
import com.rusefi.FileLog;
import com.rusefi.binaryprotocol.BinaryProtocol;
import com.rusefi.binaryprotocol.IoHelper;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
@ -18,6 +19,7 @@ import java.net.Socket;
public class BinaryProtocolServer {
private static final int PROXY_PORT = 2390;
private static final String TS_SIGNATURE = "\0MShift v0.01";
public static void start() {
FileLog.MAIN.logLine("BinaryProtocolServer on " + PROXY_PORT);
@ -58,17 +60,23 @@ public class BinaryProtocolServer {
System.out.println("Got [" + length + "] length promise");
byte command = (byte) in.read();
System.out.println("Got [" + (char)command + "] command");
byte[] packet = new byte[length - 1];
if (length == 0)
throw new IOException("Zero length not expected");
byte[] packet = new byte[length];
in.read(packet);
int crc = in.readInt();
byte command = packet[0];
System.out.println("Got [" + (char) command + "] command");
int crc = in.readInt();
if (crc != IoHelper.crc32(packet))
throw new IllegalStateException("CRC mismatch");
if (command == BinaryProtocol.COMMAND_HELLO) {
TcpIoStream stream = new TcpIoStream(clientSocket.getOutputStream(), null);
BinaryProtocol.sendCrcPacket(TS_SIGNATURE.getBytes(), FileLog.LOGGER, stream);
}
}

View File

@ -7,6 +7,7 @@ import com.rusefi.io.LinkManager;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
@ -15,9 +16,9 @@ import java.io.OutputStream;
*/
public class TcpIoStream implements IoStream {
private final OutputStream os;
private final BufferedInputStream stream;
private final InputStream stream;
public TcpIoStream(OutputStream os, BufferedInputStream stream) {
public TcpIoStream(OutputStream os, InputStream stream) {
this.os = os;
this.stream = stream;
}