This commit is contained in:
rusefillc 2021-12-07 00:05:48 -05:00
parent 895142e6f6
commit d1c0a9fba6
5 changed files with 113 additions and 37 deletions

View File

@ -1,5 +1,8 @@
package com.rusefi.binaryprotocol;
import com.devexperts.logging.Logging;
import com.rusefi.io.IoStream;
import java.util.zip.CRC32;
/**
@ -9,6 +12,12 @@ import java.util.zip.CRC32;
* 3/6/2015
*/
public class IoHelper {
private static final Logging log = Logging.getLogging(IoStream.class);
static {
log.configureDebugEnabled(false);
}
public static int getCrc32(byte[] packet) {
return getCrc32(packet, 0, packet.length);
}
@ -23,6 +32,8 @@ public class IoHelper {
* this method adds two bytes for packet size before and four bytes for IoHelper after
*/
public static byte[] makeCrc32Packet(byte[] command) {
if (log.debugEnabled())
log.info("makeCrc32Packet: raw packet " + IoStream.printByteArray(command));
byte[] packet = new byte[command.length + 6];
packet[0] = (byte) (command.length / 256);
@ -31,6 +42,8 @@ public class IoHelper {
System.arraycopy(command, 0, packet, 2, command.length);
int crc = getCrc32(command);
if (log.debugEnabled())
log.info(String.format("makeCrc32Packet: CRC 0x%08X", crc));
putInt(packet, packet.length - 4, crc);
return packet;
}

View File

@ -4,6 +4,7 @@ import com.devexperts.logging.Logging;
import com.opensr5.io.DataListener;
import com.rusefi.config.generated.Fields;
import com.rusefi.io.IoStream;
import org.jetbrains.annotations.NotNull;
import java.io.Closeable;
import java.io.IOException;
@ -213,14 +214,7 @@ public class Elm327Connector implements Closeable {
private void sendCanData(byte [] hdr, byte [] data, int offset, int len) {
//log.info("--------sendData offset="+Integer.toString(offset) + " len=" + Integer.toString(len) + "hdr.len=" + Integer.toString(hdr.length));
len += hdr.length;
byte [] hexData = new byte [len * 2 + 1];
for (int i = 0, j = 0; i < len; i++, j += 2) {
int v = ((i < hdr.length) ? hdr[i] : data[i - hdr.length + offset]) & 0xFF;
hexData[j] = HEX_ARRAY[v >>> 4];
hexData[j + 1] = HEX_ARRAY[v & 0x0F];
}
hexData[len * 2] = '\r';
byte[] hexData = byteToString(hdr, data, offset, len);
//log.info("* Elm327.data: " + (new String(hexData)));
@ -231,6 +225,19 @@ public class Elm327Connector implements Closeable {
}
}
@NotNull
public static byte[] byteToString(byte[] hdr, byte[] data, int offset, int payloadLength) {
int totalLength = hdr.length + payloadLength;
byte[] hexData = new byte[totalLength * 2 + 1];
for (int i = 0, j = 0; i < totalLength; i++, j += 2) {
int v = ((i < hdr.length) ? hdr[i] : data[i - hdr.length + offset]) & 0xFF;
hexData[j] = HEX_ARRAY[v >>> 4];
hexData[j + 1] = HEX_ARRAY[v & 0x0F];
}
hexData[totalLength * 2] = '\r';
return hexData;
}
private byte[] receiveData() {
synchronized (lock) {
try {

View File

@ -3,6 +3,7 @@ package com.rusefi.binaryprotocol.test;
import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.config.generated.Fields;
import com.rusefi.io.IoStream;
import com.rusefi.io.LinkManager;
import com.rusefi.io.can.Elm327Connector;
import com.rusefi.io.serial.BaudRateHolder;
import com.rusefi.io.serial.SerialIoStream;
@ -26,11 +27,21 @@ public class Elm327Sandbox {
IncomingDataBuffer dataBuffer = tsStream.getDataBuffer();
System.out.println("Hello new ELM327 connection, pending=" + dataBuffer.getPendingCount());
/*
runFcommand("First time", tsStream);
Elm327Connector.whyDoWeNeedToSleepBetweenCommands();
runFcommand("Second time", tsStream);
LinkManager linkManager = new LinkManager();
SandboxCommon.verifyCrcNoPending(tsStream, linkManager);
// SandboxCommon.runFcommand("First time", tsStream);
if (1 == 1)
return;
/*
SandboxCommon.runFcommand("First time", tsStream);
Elm327Connector.whyDoWeNeedToSleepBetweenCommands();
SandboxCommon.runFcommand("Second time", tsStream);
Elm327Connector.whyDoWeNeedToSleepBetweenCommands();
*/
@ -56,32 +67,15 @@ public class Elm327Sandbox {
System.out.println("****************************************");
System.out.println("******** ELM327 LOOKS GREAT **********");
System.out.println("****************************************");
SandboxCommon.verifyCrcNoPending(tsStream, linkManager);
SandboxCommon.verifyCrcNoPending(tsStream, linkManager);
SandboxCommon.readImage(tsStream, linkManager);
System.exit(-1);
/*
{
tsStream.sendPacket(BinaryProtocol.createCrcCommand(1000));
byte[] fResponse = new byte[3];
dataBuffer.waitForBytes("CRC", System.currentTimeMillis(), fResponse.length);
dataBuffer.getData(fResponse);
System.out.println(" Got CRC response " + IoStream.printByteArray(fResponse));
}
LinkManager linkManager = new LinkManager();
StreamConnector streamConnector = new StreamConnector(linkManager, () -> tsStream);
linkManager.setConnector(streamConnector);
streamConnector.connectAndReadConfiguration(new ConnectionStateListener() {
@Override
public void onConnectionEstablished() {
System.out.println("onConnectionEstablished");
}
@Override
public void onConnectionFailed() {
System.out.println("onConnectionFailed");
}
});
*/
}
}

View File

@ -0,0 +1,21 @@
package com.rusefi.io.can;
import com.rusefi.io.IoStream;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class Elm327ConnectorTest {
@Test
public void testBytesToString() {
assertEquals("30 46 0D ",
IoStream.printHexBinary(Elm327Connector.byteToString(new byte[]{0xF}, new byte[]{}, 0, 0))
);
assertEquals("31 30 30 42 30 30 30 35 36 42 30 30 30 30 35 30 0D ",
IoStream.printHexBinary(Elm327Connector.byteToString(new byte[]{16, 11}, new byte[]{0, 5, 107, 0, 0, 80, 95, 105, -81, -96, 112}, 0, 6))
);
}
}

View File

@ -0,0 +1,41 @@
package com.rusefi.io.can;
import com.rusefi.io.IoStream;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class IsoTpConnectorTest {
@Test
public void testConnector() {
byte[] crcWrappedCrcRequest = new byte[]{
0, 5, 107, 0, 0, 80, 95, 105, -81, -96, 112};
List<String> packets = new ArrayList<>();
IsoTpConnector testConnector = new IsoTpConnector() {
@Override
public void sendCanData(byte[] hdr, byte[] data, int offset, int payloadLength) {
String packetAsString =
IoStream.printHexBinary(Elm327Connector.byteToString(hdr, data, offset, payloadLength));
packets.add(packetAsString);
}
@Override
public void receiveData() {
}
};
IsoTpConnector.sendStrategy(crcWrappedCrcRequest, testConnector);
assertEquals(2, packets.size());
assertEquals("31 30 30 42 30 30 30 35 36 42 30 30 30 30 35 30 0D ", packets.get(0));
assertEquals("32 31 35 46 36 39 41 46 41 30 37 30 0D ", packets.get(1));
}
}