refactoring
This commit is contained in:
parent
03259557b6
commit
9d178338b5
|
@ -39,7 +39,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
|
|||
* This properly allows to switch to non-CRC32 mode
|
||||
* 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 {
|
||||
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 {
|
||||
sendPacket(command, logger, stream);
|
||||
}
|
||||
|
||||
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();
|
||||
stream.sendPacket(command, logger);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,18 +1,45 @@
|
|||
package com.rusefi.io;
|
||||
|
||||
import com.opensr5.Logger;
|
||||
import com.opensr5.io.DataListener;
|
||||
import com.opensr5.io.WriteStream;
|
||||
import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||
import com.rusefi.binaryprotocol.IoHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Physical bi-directional controller communication level
|
||||
*
|
||||
* <p>
|
||||
* (c) Andrey Belomutskiy
|
||||
*
|
||||
* <p>
|
||||
* 5/11/2015.
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
|
|
@ -7,8 +7,6 @@ import com.opensr5.Logger;
|
|||
import com.opensr5.io.DataListener;
|
||||
import com.rusefi.io.IoStream;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* https://github.com/Fazecast/jSerialComm looks to be alive as of 2019
|
||||
* <p>
|
||||
|
|
|
@ -102,10 +102,10 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
|
|||
|
||||
TcpIoStream stream = new TcpIoStream(clientSocket.getInputStream(), clientSocket.getOutputStream());
|
||||
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) {
|
||||
// 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) {
|
||||
short page = dis.readShort();
|
||||
short offset = dis.readShort();
|
||||
|
@ -116,9 +116,9 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
|
|||
ByteArrayOutputStream response = new ByteArrayOutputStream();
|
||||
response.write(TS_OK.charAt(0));
|
||||
new DataOutputStream(response).write(result);
|
||||
BinaryProtocol.sendPacket(response.toByteArray(), FileLog.LOGGER, stream);
|
||||
stream.sendPacket(response.toByteArray(), FileLog.LOGGER);
|
||||
} 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) {
|
||||
short page = dis.readShort();
|
||||
short offset = swap16(dis.readShort());
|
||||
|
@ -131,7 +131,7 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
|
|||
byte[] response = new byte[1 + count];
|
||||
response[0] = (byte) TS_OK.charAt(0);
|
||||
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) {
|
||||
|
||||
|
@ -145,7 +145,7 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
|
|||
byte[] currentOutputs = bp.currentOutputs;
|
||||
if (currentOutputs != null)
|
||||
System.arraycopy(currentOutputs, 1, response, 1, Fields.TS_OUTPUT_SIZE);
|
||||
BinaryProtocol.sendPacket(response, FileLog.LOGGER, stream);
|
||||
stream.sendPacket(response, FileLog.LOGGER);
|
||||
} else {
|
||||
FileLog.MAIN.logLine("Error: unknown command " + command);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.rusefi.autodetect;
|
|||
|
||||
import com.opensr5.Logger;
|
||||
import com.rusefi.FileLog;
|
||||
import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||
import com.rusefi.binaryprotocol.BinaryProtocolCommands;
|
||||
import com.rusefi.binaryprotocol.IncomingDataBuffer;
|
||||
import com.rusefi.config.generated.Fields;
|
||||
|
@ -36,7 +35,7 @@ class SerialAutoChecker implements Runnable {
|
|||
IncomingDataBuffer incomingData = new IncomingDataBuffer(logger);
|
||||
stream.setInputListener(incomingData::addData);
|
||||
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());
|
||||
if (!checkResponseCode(response, BinaryProtocolCommands.RESPONSE_OK))
|
||||
return;
|
||||
|
|
Loading…
Reference in New Issue