refactoring
This commit is contained in:
parent
2209b98174
commit
d25840dbef
|
@ -39,7 +39,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
|
||||||
* This properly allows to switch to non-CRC32 mode
|
* This properly allows to switch to non-CRC32 mode
|
||||||
* todo: finish this feature, assuming we even need it.
|
* todo: finish this feature, assuming we even need it.
|
||||||
*/
|
*/
|
||||||
private static boolean PLAIN_PROTOCOL = Boolean.getBoolean(USE_PLAIN_PROTOCOL_PROPERTY);
|
public static boolean PLAIN_PROTOCOL = Boolean.getBoolean(USE_PLAIN_PROTOCOL_PROPERTY);
|
||||||
static {
|
static {
|
||||||
FileLog.MAIN.logLine(USE_PLAIN_PROTOCOL_PROPERTY + ": " + PLAIN_PROTOCOL);
|
FileLog.MAIN.logLine(USE_PLAIN_PROTOCOL_PROPERTY + ": " + PLAIN_PROTOCOL);
|
||||||
}
|
}
|
||||||
|
@ -330,30 +330,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendPacket(byte[] command) throws IOException {
|
private void sendPacket(byte[] command) throws IOException {
|
||||||
sendPacket(command, logger, stream);
|
stream.sendPacket(command, logger);
|
||||||
}
|
|
||||||
|
|
||||||
public static void sendPacket(byte[] plainPacket, Logger logger, IoStream stream) throws IOException {
|
|
||||||
byte[] packet;
|
|
||||||
if (PLAIN_PROTOCOL) {
|
|
||||||
packet = plainPacket;
|
|
||||||
} else {
|
|
||||||
packet = IoHelper.makeCrc32Packet(plainPacket);
|
|
||||||
}
|
|
||||||
logger.info("Sending packet " + printHexBinary(plainPacket));
|
|
||||||
stream.write(packet);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final char[] hexCode = "0123456789ABCDEF".toCharArray();
|
|
||||||
|
|
||||||
private static String printHexBinary(byte[] data) {
|
|
||||||
StringBuilder r = new StringBuilder(data.length * 2);
|
|
||||||
for (byte b : data) {
|
|
||||||
r.append(hexCode[(b >> 4) & 0xF]);
|
|
||||||
r.append(hexCode[(b & 0xF)]);
|
|
||||||
r.append(' ');
|
|
||||||
}
|
|
||||||
return r.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,45 @@
|
||||||
package com.rusefi.io;
|
package com.rusefi.io;
|
||||||
|
|
||||||
|
import com.opensr5.Logger;
|
||||||
import com.opensr5.io.DataListener;
|
import com.opensr5.io.DataListener;
|
||||||
import com.opensr5.io.WriteStream;
|
import com.opensr5.io.WriteStream;
|
||||||
|
import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||||
|
import com.rusefi.binaryprotocol.IoHelper;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Physical bi-directional controller communication level
|
* Physical bi-directional controller communication level
|
||||||
*
|
* <p>
|
||||||
* (c) Andrey Belomutskiy
|
* (c) Andrey Belomutskiy
|
||||||
*
|
* <p>
|
||||||
* 5/11/2015.
|
* 5/11/2015.
|
||||||
*/
|
*/
|
||||||
public interface IoStream extends WriteStream {
|
public interface IoStream extends WriteStream {
|
||||||
|
|
||||||
|
static String printHexBinary(byte[] data) {
|
||||||
|
char[] hexCode = "0123456789ABCDEF".toCharArray();
|
||||||
|
|
||||||
|
StringBuilder r = new StringBuilder(data.length * 2);
|
||||||
|
for (byte b : data) {
|
||||||
|
r.append(hexCode[(b >> 4) & 0xF]);
|
||||||
|
r.append(hexCode[(b & 0xF)]);
|
||||||
|
r.append(' ');
|
||||||
|
}
|
||||||
|
return r.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
default void sendPacket(byte[] plainPacket, Logger logger) throws IOException {
|
||||||
|
byte[] packet;
|
||||||
|
if (BinaryProtocol.PLAIN_PROTOCOL) {
|
||||||
|
packet = plainPacket;
|
||||||
|
} else {
|
||||||
|
packet = IoHelper.makeCrc32Packet(plainPacket);
|
||||||
|
}
|
||||||
|
logger.info("Sending packet " + printHexBinary(plainPacket));
|
||||||
|
write(packet);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param listener would be invoked from unknown implementation-dependent thread
|
* @param listener would be invoked from unknown implementation-dependent thread
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -7,8 +7,6 @@ import com.opensr5.Logger;
|
||||||
import com.opensr5.io.DataListener;
|
import com.opensr5.io.DataListener;
|
||||||
import com.rusefi.io.IoStream;
|
import com.rusefi.io.IoStream;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* https://github.com/Fazecast/jSerialComm looks to be alive as of 2019
|
* https://github.com/Fazecast/jSerialComm looks to be alive as of 2019
|
||||||
* <p>
|
* <p>
|
||||||
|
|
|
@ -102,10 +102,10 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
|
||||||
|
|
||||||
TcpIoStream stream = new TcpIoStream(clientSocket.getInputStream(), clientSocket.getOutputStream());
|
TcpIoStream stream = new TcpIoStream(clientSocket.getInputStream(), clientSocket.getOutputStream());
|
||||||
if (command == COMMAND_HELLO) {
|
if (command == COMMAND_HELLO) {
|
||||||
BinaryProtocol.sendPacket((TS_OK + TS_SIGNATURE).getBytes(), FileLog.LOGGER, stream);
|
stream.sendPacket((TS_OK + TS_SIGNATURE).getBytes(), FileLog.LOGGER);
|
||||||
} else if (command == COMMAND_PROTOCOL) {
|
} else if (command == COMMAND_PROTOCOL) {
|
||||||
// System.out.println("Ignoring crc F command");
|
// System.out.println("Ignoring crc F command");
|
||||||
BinaryProtocol.sendPacket((TS_OK + TS_PROTOCOL).getBytes(), FileLog.LOGGER, stream);
|
stream.sendPacket((TS_OK + TS_PROTOCOL).getBytes(), FileLog.LOGGER);
|
||||||
} else if (command == COMMAND_CRC_CHECK_COMMAND) {
|
} else if (command == COMMAND_CRC_CHECK_COMMAND) {
|
||||||
short page = dis.readShort();
|
short page = dis.readShort();
|
||||||
short offset = dis.readShort();
|
short offset = dis.readShort();
|
||||||
|
@ -116,9 +116,9 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
|
||||||
ByteArrayOutputStream response = new ByteArrayOutputStream();
|
ByteArrayOutputStream response = new ByteArrayOutputStream();
|
||||||
response.write(TS_OK.charAt(0));
|
response.write(TS_OK.charAt(0));
|
||||||
new DataOutputStream(response).write(result);
|
new DataOutputStream(response).write(result);
|
||||||
BinaryProtocol.sendPacket(response.toByteArray(), FileLog.LOGGER, stream);
|
stream.sendPacket(response.toByteArray(), FileLog.LOGGER);
|
||||||
} else if (command == COMMAND_PAGE) {
|
} else if (command == COMMAND_PAGE) {
|
||||||
BinaryProtocol.sendPacket(TS_OK.getBytes(), FileLog.LOGGER, stream);
|
stream.sendPacket(TS_OK.getBytes(), FileLog.LOGGER);
|
||||||
} else if (command == COMMAND_READ) {
|
} else if (command == COMMAND_READ) {
|
||||||
short page = dis.readShort();
|
short page = dis.readShort();
|
||||||
short offset = swap16(dis.readShort());
|
short offset = swap16(dis.readShort());
|
||||||
|
@ -131,7 +131,7 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
|
||||||
byte[] response = new byte[1 + count];
|
byte[] response = new byte[1 + count];
|
||||||
response[0] = (byte) TS_OK.charAt(0);
|
response[0] = (byte) TS_OK.charAt(0);
|
||||||
System.arraycopy(bp.getController().getContent(), offset, response, 1, count);
|
System.arraycopy(bp.getController().getContent(), offset, response, 1, count);
|
||||||
BinaryProtocol.sendPacket(response, FileLog.LOGGER, stream);
|
stream.sendPacket(response, FileLog.LOGGER);
|
||||||
}
|
}
|
||||||
} else if (command == COMMAND_OUTPUTS) {
|
} else if (command == COMMAND_OUTPUTS) {
|
||||||
|
|
||||||
|
@ -145,7 +145,7 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
|
||||||
byte[] currentOutputs = bp.currentOutputs;
|
byte[] currentOutputs = bp.currentOutputs;
|
||||||
if (currentOutputs != null)
|
if (currentOutputs != null)
|
||||||
System.arraycopy(currentOutputs, 1, response, 1, Fields.TS_OUTPUT_SIZE);
|
System.arraycopy(currentOutputs, 1, response, 1, Fields.TS_OUTPUT_SIZE);
|
||||||
BinaryProtocol.sendPacket(response, FileLog.LOGGER, stream);
|
stream.sendPacket(response, FileLog.LOGGER);
|
||||||
} else {
|
} else {
|
||||||
FileLog.MAIN.logLine("Error: unknown command " + command);
|
FileLog.MAIN.logLine("Error: unknown command " + command);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package com.rusefi.autodetect;
|
||||||
|
|
||||||
import com.opensr5.Logger;
|
import com.opensr5.Logger;
|
||||||
import com.rusefi.FileLog;
|
import com.rusefi.FileLog;
|
||||||
import com.rusefi.binaryprotocol.BinaryProtocol;
|
|
||||||
import com.rusefi.binaryprotocol.BinaryProtocolCommands;
|
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;
|
||||||
|
@ -36,7 +35,7 @@ class SerialAutoChecker implements Runnable {
|
||||||
IncomingDataBuffer incomingData = new IncomingDataBuffer(logger);
|
IncomingDataBuffer incomingData = new IncomingDataBuffer(logger);
|
||||||
stream.setInputListener(incomingData::addData);
|
stream.setInputListener(incomingData::addData);
|
||||||
try {
|
try {
|
||||||
BinaryProtocol.sendPacket(new byte[]{BinaryProtocolCommands.COMMAND_HELLO}, logger, stream);
|
stream.sendPacket(new byte[]{BinaryProtocolCommands.COMMAND_HELLO}, logger);
|
||||||
byte[] response = incomingData.getPacket(logger, "", false, System.currentTimeMillis());
|
byte[] response = incomingData.getPacket(logger, "", false, System.currentTimeMillis());
|
||||||
if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK))
|
if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK))
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue