refactoring
This commit is contained in:
parent
6484632233
commit
b038ca5987
|
@ -31,7 +31,7 @@ import static com.rusefi.binaryprotocol.IoHelper.*;
|
|||
* (c) Andrey Belomutskiy
|
||||
* 3/6/2015
|
||||
*/
|
||||
public class BinaryProtocol {
|
||||
public class BinaryProtocol implements BinaryProtocolCommands {
|
||||
|
||||
private static final String USE_PLAIN_PROTOCOL_PROPERTY = "protocol.plain";
|
||||
/**
|
||||
|
@ -43,28 +43,6 @@ public class BinaryProtocol {
|
|||
FileLog.MAIN.logLine(USE_PLAIN_PROTOCOL_PROPERTY + ": " + PLAIN_PROTOCOL);
|
||||
}
|
||||
|
||||
// see BLOCKING_FACTOR in firmware code
|
||||
private static final int BLOCKING_FACTOR = Fields.BLOCKING_FACTOR;
|
||||
private static final byte RESPONSE_OK = 0;
|
||||
private static final byte RESPONSE_BURN_OK = 0x04;
|
||||
private static final byte RESPONSE_COMMAND_OK = 0x07;
|
||||
/**
|
||||
* that's hex for "~\n", see BINARY_SWITCH_TAG in firmware source code
|
||||
*/
|
||||
private static final int SWITCH_TO_BINARY_RESPONSE = 0x7e0a;
|
||||
/**
|
||||
* See SWITCH_TO_BINARY_COMMAND in firmware source code
|
||||
*/
|
||||
private static final String SWITCH_TO_BINARY_COMMAND = "~";
|
||||
public static final char COMMAND_OUTPUTS = 'O'; // ochGetCommand
|
||||
public static final char COMMAND_HELLO = 'S'; // queryCommand
|
||||
public static final char COMMAND_PROTOCOL = 'F';
|
||||
public static final char COMMAND_CRC_CHECK_COMMAND = 'k';
|
||||
public static final char COMMAND_PAGE = 'P';
|
||||
public static final char COMMAND_READ = 'R'; // 082 decimal
|
||||
private static final char COMMAND_CHUNK_WRITE = 'C'; // pageChunkWrite
|
||||
private static final char COMMAND_BURN = 'B'; // burnCommand
|
||||
|
||||
private final Logger logger;
|
||||
private final IoStream stream;
|
||||
private final IncomingDataBuffer incomingData;
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.rusefi.binaryprotocol;
|
||||
|
||||
import com.rusefi.config.Fields;
|
||||
|
||||
/**
|
||||
* (c) Andrey Belomutskiy
|
||||
* 6/21/2017.
|
||||
*/
|
||||
public interface BinaryProtocolCommands {
|
||||
// see BLOCKING_FACTOR in firmware code
|
||||
int BLOCKING_FACTOR = Fields.BLOCKING_FACTOR;
|
||||
byte RESPONSE_OK = 0;
|
||||
byte RESPONSE_BURN_OK = 0x04;
|
||||
byte RESPONSE_COMMAND_OK = 0x07;
|
||||
/**
|
||||
* that's hex for "~\n", see BINARY_SWITCH_TAG in firmware source code
|
||||
*/
|
||||
int SWITCH_TO_BINARY_RESPONSE = 0x7e0a;
|
||||
/**
|
||||
* See SWITCH_TO_BINARY_COMMAND in firmware source code
|
||||
*/
|
||||
String SWITCH_TO_BINARY_COMMAND = "~";
|
||||
char COMMAND_OUTPUTS = 'O'; // ochGetCommand
|
||||
char COMMAND_HELLO = 'S'; // queryCommand
|
||||
char COMMAND_PROTOCOL = 'F';
|
||||
char COMMAND_CRC_CHECK_COMMAND = 'k';
|
||||
char COMMAND_PAGE = 'P';
|
||||
char COMMAND_READ = 'R'; // 082 decimal
|
||||
char COMMAND_CHUNK_WRITE = 'C'; // pageChunkWrite
|
||||
char COMMAND_BURN = 'B'; // burnCommand
|
||||
}
|
|
@ -2,6 +2,7 @@ package com.rusefi.io.tcp;
|
|||
|
||||
import com.rusefi.FileLog;
|
||||
import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||
import com.rusefi.binaryprotocol.BinaryProtocolCommands;
|
||||
import com.rusefi.binaryprotocol.BinaryProtocolHolder;
|
||||
import com.rusefi.binaryprotocol.IoHelper;
|
||||
import com.rusefi.config.Fields;
|
||||
|
@ -18,7 +19,7 @@ import java.net.Socket;
|
|||
* 11/24/15
|
||||
*/
|
||||
|
||||
public class BinaryProtocolServer {
|
||||
public class BinaryProtocolServer implements BinaryProtocolCommands {
|
||||
private static final int PROXY_PORT = 2390;
|
||||
private static final String TS_OK = "\0";
|
||||
|
||||
|
@ -28,6 +29,7 @@ public class BinaryProtocolServer {
|
|||
public static void start() {
|
||||
FileLog.MAIN.logLine("BinaryProtocolServer on " + PROXY_PORT);
|
||||
Runnable runnable = new Runnable() {
|
||||
@SuppressWarnings("InfiniteLoopStatement")
|
||||
@Override
|
||||
public void run() {
|
||||
ServerSocket serverSocket;
|
||||
|
@ -50,18 +52,20 @@ public class BinaryProtocolServer {
|
|||
}, "proxy connection").start();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
new Thread(runnable, "BinaryProtocolServer").start();
|
||||
}
|
||||
|
||||
@SuppressWarnings("InfiniteLoopStatement")
|
||||
private static void runProxy(Socket clientSocket) throws IOException {
|
||||
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
|
||||
|
||||
while (true) {
|
||||
byte first = in.readByte();
|
||||
if (first == BinaryProtocol.COMMAND_PROTOCOL) {
|
||||
if (first == COMMAND_PROTOCOL) {
|
||||
//System.out.println("Ignoring plain F command");
|
||||
System.out.println("Got plain F command");
|
||||
OutputStream outputStream = clientSocket.getOutputStream();
|
||||
|
@ -78,7 +82,9 @@ public class BinaryProtocolServer {
|
|||
throw new IOException("Zero length not expected");
|
||||
|
||||
byte[] packet = new byte[length];
|
||||
in.read(packet);
|
||||
int size = in.read(packet);
|
||||
if (size != packet.length)
|
||||
throw new IllegalStateException();
|
||||
|
||||
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(packet));
|
||||
byte command = (byte) dis.read();
|
||||
|
@ -90,12 +96,12 @@ public class BinaryProtocolServer {
|
|||
|
||||
|
||||
TcpIoStream stream = new TcpIoStream(clientSocket.getInputStream(), clientSocket.getOutputStream());
|
||||
if (command == BinaryProtocol.COMMAND_HELLO) {
|
||||
if (command == COMMAND_HELLO) {
|
||||
BinaryProtocol.sendPacket((TS_OK + TS_SIGNATURE).getBytes(), FileLog.LOGGER, stream);
|
||||
} else if (command == BinaryProtocol.COMMAND_PROTOCOL) {
|
||||
} else if (command == COMMAND_PROTOCOL) {
|
||||
// System.out.println("Ignoring crc F command");
|
||||
BinaryProtocol.sendPacket((TS_OK + TS_PROTOCOL).getBytes(), FileLog.LOGGER, stream);
|
||||
} else if (command == BinaryProtocol.COMMAND_CRC_CHECK_COMMAND) {
|
||||
} else if (command == COMMAND_CRC_CHECK_COMMAND) {
|
||||
short page = dis.readShort();
|
||||
short offset = dis.readShort();
|
||||
short count = dis.readShort(); // no swap here? interesting!
|
||||
|
@ -106,9 +112,9 @@ public class BinaryProtocolServer {
|
|||
response.write(TS_OK.charAt(0));
|
||||
new DataOutputStream(response).write(result);
|
||||
BinaryProtocol.sendPacket(response.toByteArray(), FileLog.LOGGER, stream);
|
||||
} else if (command == BinaryProtocol.COMMAND_PAGE) {
|
||||
} else if (command == COMMAND_PAGE) {
|
||||
BinaryProtocol.sendPacket(TS_OK.getBytes(), FileLog.LOGGER, stream);
|
||||
} else if (command == BinaryProtocol.COMMAND_READ) {
|
||||
} else if (command == COMMAND_READ) {
|
||||
short page = dis.readShort();
|
||||
short offset = swap16(dis.readShort());
|
||||
short count = swap16(dis.readShort());
|
||||
|
@ -122,9 +128,9 @@ public class BinaryProtocolServer {
|
|||
System.arraycopy(bp.getController().getContent(), offset, response, 1, count);
|
||||
BinaryProtocol.sendPacket(response, FileLog.LOGGER, stream);
|
||||
}
|
||||
} else if (command == BinaryProtocol.COMMAND_OUTPUTS) {
|
||||
} else if (command == COMMAND_OUTPUTS) {
|
||||
|
||||
if (1==1)
|
||||
if (System.currentTimeMillis() > 0)
|
||||
throw new UnsupportedOperationException("offset and count not supported see #429");
|
||||
// todo: new version with offset and
|
||||
|
||||
|
|
Loading…
Reference in New Issue