isolating issue with unconsumed data

This commit is contained in:
rusefillc 2021-12-06 12:43:53 -05:00
parent 1935749e27
commit 892c303d9b
4 changed files with 52 additions and 27 deletions

View File

@ -144,7 +144,7 @@ public class BinaryProtocol {
private final Thread hook = new Thread(() -> closeComposites(), "BinaryProtocol::hook"); private final Thread hook = new Thread(() -> closeComposites(), "BinaryProtocol::hook");
public BinaryProtocol(LinkManager linkManager, IoStream stream, IncomingDataBuffer dataBuffer) { public BinaryProtocol(LinkManager linkManager, IoStream stream) {
this.linkManager = linkManager; this.linkManager = linkManager;
this.stream = stream; this.stream = stream;
@ -155,7 +155,7 @@ public class BinaryProtocol {
} }
}; };
incomingData = dataBuffer; incomingData = stream.getDataBuffer();
Runtime.getRuntime().addShutdownHook(hook); Runtime.getRuntime().addShutdownHook(hook);
needCompositeLogger = linkManager.getCompositeLogicEnabled(); needCompositeLogger = linkManager.getCompositeLogicEnabled();
rpmListener = value -> { rpmListener = value -> {
@ -432,25 +432,34 @@ public class BinaryProtocol {
int crcOfLocallyCachedConfiguration = IoHelper.getCrc32(localCached.getContent()); int crcOfLocallyCachedConfiguration = IoHelper.getCrc32(localCached.getContent());
log.info(String.format(CONFIGURATION_RUSEFI_BINARY + " Local cache CRC %x\n", crcOfLocallyCachedConfiguration)); log.info(String.format(CONFIGURATION_RUSEFI_BINARY + " Local cache CRC %x\n", crcOfLocallyCachedConfiguration));
byte[] packet = createCrcCommand(localCached.getSize()); int crcFromController = getCrcFromController(localCached.getSize());
byte[] response = executeCommand(packet, "get CRC32");
if (checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK) && response.length == 5) { if (crcOfLocallyCachedConfiguration == crcFromController) {
ByteBuffer bb = ByteBuffer.wrap(response, 1, 4); return localCached;
// that's unusual - most of the protocol is LITTLE_ENDIAN
bb.order(ByteOrder.BIG_ENDIAN);
int crcFromController = bb.getInt();
log.info(String.format("rusEFI says tune CRC32 0x%x %d\n", crcFromController, crcFromController));
short crc16FromController = (short) crcFromController;
log.info(String.format("rusEFI says tune CRC16 0x%x %d\n", crc16FromController, crc16FromController));
if (crcOfLocallyCachedConfiguration == crcFromController) {
return localCached;
}
} }
} }
return null; return null;
} }
public int getCrcFromController(int configSize) {
byte[] packet = createCrcCommand(configSize);
byte[] response = executeCommand(packet, "get CRC32");
if (checkResponseCode(response, (byte) Fields.TS_RESPONSE_OK) && response.length == 5) {
ByteBuffer bb = ByteBuffer.wrap(response, 1, 4);
// that's unusual - most of the protocol is LITTLE_ENDIAN
bb.order(ByteOrder.BIG_ENDIAN);
int crcFromController = bb.getInt();
log.info(String.format("rusEFI says tune CRC32 0x%x %d\n", crcFromController, crcFromController));
short crc16FromController = (short) crcFromController;
log.info(String.format("rusEFI says tune CRC16 0x%x %d\n", crc16FromController, crc16FromController));
return crcFromController;
} else {
return -1;
}
}
public static byte[] createCrcCommand(int size) { public static byte[] createCrcCommand(int size) {
byte[] packet = new byte[5]; byte[] packet = new byte[5];
packet[0] = Fields.TS_CRC_CHECK_COMMAND; packet[0] = Fields.TS_CRC_CHECK_COMMAND;

View File

@ -10,7 +10,6 @@ import net.jcip.annotations.ThreadSafe;
import java.io.EOFException; import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import static com.devexperts.logging.Logging.getLogging; import static com.devexperts.logging.Logging.getLogging;
@ -151,7 +150,7 @@ public class IncomingDataBuffer {
} }
} }
public void dropPending() { public int dropPending() {
// todo: when exactly do we need this logic? // todo: when exactly do we need this logic?
synchronized (cbb) { synchronized (cbb) {
int pending = cbb.length(); int pending = cbb.length();
@ -161,6 +160,7 @@ public class IncomingDataBuffer {
cbb.get(bytes); cbb.get(bytes);
log.error("DROPPED FROM BUFFER: " + IoStream.printByteArray(bytes)); log.error("DROPPED FROM BUFFER: " + IoStream.printByteArray(bytes));
} }
return pending;
} }
} }

View File

@ -46,7 +46,7 @@ public class PortHolder {
return false; return false;
} }
synchronized (portLock) { synchronized (portLock) {
bp = new BinaryProtocol(linkManager, stream, stream.getDataBuffer()); bp = new BinaryProtocol(linkManager, stream);
portLock.notifyAll(); portLock.notifyAll();
} }

View File

@ -51,12 +51,34 @@ public class PCanSandbox {
System.out.println("****************************************"); System.out.println("****************************************");
*/ */
LinkManager linkManager = new LinkManager(); LinkManager linkManager = new LinkManager();
BinaryProtocol bp = new BinaryProtocol(linkManager, tsStream);
linkManager.COMMUNICATION_EXECUTOR.submit(() -> {
if (tsStream.getDataBuffer().dropPending() != 0)
System.out.println("ERROR Extra data before CRC");
bp.getCrcFromController(Fields.TOTAL_CONFIG_SIZE);
// bp.getCrcFromController(Fields.TOTAL_CONFIG_SIZE);
// bp.getCrcFromController(Fields.TOTAL_CONFIG_SIZE);
if (tsStream.getDataBuffer().dropPending() != 0)
System.out.println("ERROR Extra data after CRC");
});
/*
StreamConnector streamConnector = new StreamConnector(linkManager, () -> tsStream); StreamConnector streamConnector = new StreamConnector(linkManager, () -> tsStream);
linkManager.setConnector(streamConnector); linkManager.setConnector(streamConnector);
streamConnector.connectAndReadConfiguration(new ConnectionStateListener() { streamConnector.connectAndReadConfiguration(new ConnectionStateListener() {
@Override @Override
public void onConnectionEstablished() { public void onConnectionEstablished() {
System.out.println("onConnectionEstablished"); System.out.println("onConnectionEstablished");
BinaryProtocol currentStreamState = linkManager.getCurrentStreamState();
if (currentStreamState == null) {
System.out.println("No BinaryProtocol");
} else {
BinaryProtocolState binaryProtocolState = currentStreamState.getBinaryProtocolState();
ConfigurationImage ci = binaryProtocolState.getControllerConfiguration();
System.out.println("Got ConfigurationImage " + ci);
System.exit(0);
}
} }
@Override @Override
@ -64,14 +86,8 @@ public class PCanSandbox {
System.out.println("onConnectionFailed"); System.out.println("onConnectionFailed");
} }
}); });
BinaryProtocol currentStreamState = linkManager.getCurrentStreamState(); */
if (currentStreamState == null) {
System.out.println("No BinaryProtocol");
} else {
BinaryProtocolState binaryProtocolState = currentStreamState.getBinaryProtocolState();
ConfigurationImage ci = binaryProtocolState.getControllerConfiguration();
System.out.println("Got ConfigurationImage " + ci);
System.exit(0);
}
} }
} }