ISO-TP does not work on real car #4427

This commit is contained in:
rusefillc 2022-08-09 11:27:42 -04:00
parent 76b19acb5d
commit 4389ffea9d
3 changed files with 20 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import com.rusefi.Timeouts;
import java.util.LinkedList;
public class RateCounter {
private final static int MAGIC_DURATION = Timeouts.SECOND;
private final LinkedList<Pair> timeStamps = new LinkedList<>();
@ -16,7 +17,7 @@ public class RateCounter {
}
public synchronized int getCurrentRate(long now) {
long threshold = now - Timeouts.SECOND;
long threshold = now - MAGIC_DURATION;
while (!timeStamps.isEmpty() && timeStamps.peekFirst().timestamp < threshold)
timeStamps.removeFirst();
@ -27,12 +28,16 @@ public class RateCounter {
return result;
}
public synchronized int getSizeForUnitTest() {
return timeStamps.size();
}
public void add() {
add(System.currentTimeMillis());
}
public synchronized void add(long now) {
timeStamps.add(new Pair(now, 1));
public synchronized void add(long timestamp) {
timeStamps.add(new Pair(timestamp, 1));
}
private static class Pair {

View File

@ -8,6 +8,7 @@ import com.rusefi.io.IoStream;
import com.rusefi.io.can.IsoTpCanDecoder;
import com.rusefi.io.can.IsoTpConnector;
import com.rusefi.io.serial.AbstractIoStream;
import com.rusefi.io.serial.RateCounter;
import com.rusefi.io.tcp.BinaryProtocolServer;
import com.rusefi.ui.StatusConsumer;
import org.jetbrains.annotations.Nullable;
@ -28,6 +29,9 @@ public class PCanIoStream extends AbstractIoStream {
private final IncomingDataBuffer dataBuffer = createDataBuffer("[PCAN] ");
private final PCANBasic can;
private final StatusConsumer statusListener;
private final RateCounter totalCounter = new RateCounter();
private final RateCounter isoTpCounter = new RateCounter();
private final IsoTpCanDecoder canDecoder = new IsoTpCanDecoder() {
@Override
protected void onTpFirstFrame() {
@ -109,13 +113,16 @@ public class PCanIoStream extends AbstractIoStream {
TPCANMsg rx = new TPCANMsg(Byte.MAX_VALUE);
TPCANStatus status = can.Read(CHANNEL, rx, null);
if (status == TPCANStatus.PCAN_ERROR_OK) {
totalCounter.add();
if (log.debugEnabled())
log.debug("Got [" + rx + "] id=" + String.format("%X", rx.getID()) + " len=" + rx.getLength() + ": " + IoStream.printByteArray(rx.getData()));
if (rx.getID() != CAN_ECU_SERIAL_TX_ID) {
// if (log.debugEnabled())
log.info("Skipping non " + String.format("%X", CAN_ECU_SERIAL_TX_ID) + " packet: " + String.format("%X", rx.getID()));
log.info("Total rate " + totalCounter.getCurrentRate() + ", isotp rate " + isoTpCounter.getCurrentRate());
return;
}
isoTpCounter.add();
byte[] decode = canDecoder.decodePacket(rx.getData());
listener.onDataArrived(decode);

View File

@ -13,15 +13,19 @@ public class RateCounterTest {
assertEquals(0, rateCounter.getCurrentRate());
rateCounter.add(1);
rateCounter.add(/*timestamp*/1);
rateCounter.add(1);
rateCounter.add(1);
rateCounter.add(1);
// cute size effect: FUTURE timestamps are also counted :)
assertEquals(4, rateCounter.getCurrentRate(0));
assertEquals(3, rateCounter.getSizeForUnitTest());
assertEquals(0, rateCounter.getCurrentRate(2 * Timeouts.SECOND));
// assert purge of oldest records
assertEquals(0, rateCounter.getSizeForUnitTest());
}
}