IsoTp copy-paste from rusEFI

This commit is contained in:
rusefillc 2023-06-03 01:50:55 -04:00
parent 412d214a99
commit 965d574ac6
4 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package com.rusefi.io.can;
import org.jetbrains.annotations.NotNull;
/**
* @see IsoTpCanDecoder
*/
public abstract class IsoTpConnector {
public static void sendStrategy(byte[] bytes, IsoTpConnector connector) {
// log.info("-------sendBytesToCan " + bytes.length + " byte(s):");
// log.info(HexBinary.printHexBinary(bytes));
// 1 frame
if (bytes.length <= 7) {
connector.sendCanFrame((IsoTpConstants.ISO_TP_FRAME_SINGLE << 4) | bytes.length, bytes, 0, bytes.length);
return;
}
// multiple frames
// send the first header frame
connector.sendCanFrame((IsoTpConstants.ISO_TP_FRAME_FIRST << 4) | ((bytes.length >> 8) & 0x0f), bytes.length & 0xff, bytes, 0, 6);
// get a flow control frame
connector.receiveData();
// send the rest of the data
int idx = 1, offset = 6;
int remaining = bytes.length - 6;
while (remaining > 0) {
int len = Math.min(remaining, 7);
// send the consecutive frames
connector.sendCanFrame((IsoTpConstants.ISO_TP_FRAME_CONSECUTIVE << 4) | ((idx++) & 0x0f), bytes, offset, len);
offset += len;
remaining -= len;
}
}
@NotNull
public static byte[] combineArrays(byte[] hdr, byte[] data, int dataOffset, int dataLength) {
byte[] total = new byte[hdr.length + dataLength];
System.arraycopy(hdr, 0, total, 0, hdr.length);
System.arraycopy(data, dataOffset, total, hdr.length, dataLength);
return total;
}
public void sendCanFrame(int hdr0, byte[] data, int offset, int dataLength) {
sendCanData(new byte[]{(byte) hdr0}, data, offset, dataLength);
}
public void sendCanFrame(int hdr0, int hdr1, byte[] data, int dataOffset, int dataLength) {
sendCanData(new byte[]{(byte) hdr0, (byte) hdr1}, data, dataOffset, dataLength);
}
public abstract void sendCanData(byte[] hdr, byte[] data, int dataOffset, int dataLength);
public abstract void receiveData();
}

View File

@ -0,0 +1,7 @@
package com.rusefi.io.can;
public class IsoTpConstants {
final static int ISO_TP_FRAME_SINGLE = 0;
final static int ISO_TP_FRAME_FIRST = 1;
final static int ISO_TP_FRAME_CONSECUTIVE = 2;
}

View File

@ -0,0 +1,39 @@
package com.rusefi.io.can;
import com.rusefi.util.HexBinary;
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 dataOffset, int dataLength) {
byte[] total = combineArrays(hdr, data, dataOffset, dataLength);
String packetAsString = HexBinary.printHexBinary(total);
packets.add(packetAsString);
}
@Override
public void receiveData() {
}
};
IsoTpConnector.sendStrategy(crcWrappedCrcRequest, testConnector);
assertEquals(2, packets.size());
assertEquals("10 0B 00 05 6B 00 00 50 ", packets.get(0));
assertEquals("21 5F 69 AF A0 70 ", packets.get(1));
}
}

View File

@ -0,0 +1,29 @@
package com.rusefi.util;
public class HexBinary {
public static String printHexBinary(byte[] data) {
if (data == null)
return "(null)";
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();
}
public static String printByteArray(byte[] data) {
StringBuilder sb = new StringBuilder();
for (byte b : data) {
if (Character.isJavaIdentifierPart(b)) {
sb.append((char) b);
} else {
sb.append(' ');
}
}
return printHexBinary(data) + sb;
}
}