isBigEndian
This commit is contained in:
parent
626b92fd24
commit
749f8a2858
|
@ -9,15 +9,17 @@ public class DbcField {
|
||||||
private final double mult;
|
private final double mult;
|
||||||
private final double offset;
|
private final double offset;
|
||||||
private String category;
|
private String category;
|
||||||
|
private final boolean isBigEndian;
|
||||||
private boolean isNiceName;
|
private boolean isNiceName;
|
||||||
|
|
||||||
public DbcField(String name, int startOffset, int length, double mult, double offset, String category) {
|
public DbcField(String name, int startOffset, int length, double mult, double offset, String category, boolean isBigEndian) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.startOffset = startOffset;
|
this.startOffset = startOffset;
|
||||||
this.length = length;
|
this.length = length;
|
||||||
this.mult = mult;
|
this.mult = mult;
|
||||||
this.offset = offset;
|
this.offset = offset;
|
||||||
this.category = category;
|
this.category = category;
|
||||||
|
this.isBigEndian = isBigEndian;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DbcField parseField(DbcPacket parent, String line) {
|
public static DbcField parseField(DbcPacket parent, String line) {
|
||||||
|
@ -40,11 +42,15 @@ public class DbcField {
|
||||||
throw new IllegalStateException("While " + line, e);
|
throw new IllegalStateException("While " + line, e);
|
||||||
}
|
}
|
||||||
int length = Integer.parseInt(tokens[index + 1]);
|
int length = Integer.parseInt(tokens[index + 1]);
|
||||||
|
int endiannessCode = Integer.parseInt(tokens[index + 2]);
|
||||||
|
if (endiannessCode != 0 && endiannessCode != 1)
|
||||||
|
throw new IllegalStateException("Unexpected endiannessCode " + endiannessCode);
|
||||||
|
boolean isBigEndian = endiannessCode == 0;
|
||||||
|
|
||||||
double mult = Double.parseDouble(tokens[index + 3]);
|
double mult = Double.parseDouble(tokens[index + 3]);
|
||||||
double offset = Double.parseDouble(tokens[index + 4]);
|
double offset = Double.parseDouble(tokens[index + 4]);
|
||||||
|
|
||||||
return new DbcField(name, startOffset, length, mult, offset, parent.getName());
|
return new DbcField(name, startOffset, length, mult, offset, parent.getName(), isBigEndian);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCategory() {
|
public String getCategory() {
|
||||||
|
@ -75,6 +81,10 @@ public class DbcField {
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isBigEndian() {
|
||||||
|
return isBigEndian;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "DbcField{" +
|
return "DbcField{" +
|
||||||
|
@ -82,10 +92,11 @@ public class DbcField {
|
||||||
", startOffset=" + startOffset +
|
", startOffset=" + startOffset +
|
||||||
", length=" + length +
|
", length=" + length +
|
||||||
", mult=" + mult +
|
", mult=" + mult +
|
||||||
|
", isBigEndian=" + isBigEndian +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getBitIndex(byte[] data, int bitIndex, int bitWidth) {
|
public static int getBitRange(byte[] data, int bitIndex, int bitWidth, boolean isBigEndian) {
|
||||||
if (bitIndex < 0)
|
if (bitIndex < 0)
|
||||||
throw new IllegalArgumentException("Huh? " + bitIndex + " " + bitWidth);
|
throw new IllegalArgumentException("Huh? " + bitIndex + " " + bitWidth);
|
||||||
int byteIndex = bitIndex >> 3;
|
int byteIndex = bitIndex >> 3;
|
||||||
|
@ -94,16 +105,17 @@ public class DbcField {
|
||||||
return 0;
|
return 0;
|
||||||
int value = data[byteIndex] & 0xff;
|
int value = data[byteIndex] & 0xff;
|
||||||
if (shift + bitWidth > 8) {
|
if (shift + bitWidth > 8) {
|
||||||
if (byteIndex + 1 >= data.length)
|
int otherByteIndex = (isBigEndian ? -1 : +1) + byteIndex;
|
||||||
|
if (otherByteIndex < 0 || otherByteIndex >= data.length)
|
||||||
return 0;
|
return 0;
|
||||||
value = value + data[1 + byteIndex] * 256;
|
value = value + data[otherByteIndex] * 256;
|
||||||
}
|
}
|
||||||
int mask = (1 << bitWidth) - 1;
|
int mask = (1 << bitWidth) - 1;
|
||||||
return (value >> shift) & mask;
|
return (value >> shift) & mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getValue(CANPacket packet) {
|
public double getValue(CANPacket packet) {
|
||||||
return getBitIndex(packet.getData(), startOffset, length) * mult + offset;
|
return getBitRange(packet.getData(), startOffset, length, isBigEndian) * mult + offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void rename(String niceName) {
|
public void rename(String niceName) {
|
||||||
|
|
|
@ -12,13 +12,30 @@ import java.io.StringReader;
|
||||||
|
|
||||||
import static com.rusefi.can.reader.impl.ParseDBCTest.VAG_MOTOR_1;
|
import static com.rusefi.can.reader.impl.ParseDBCTest.VAG_MOTOR_1;
|
||||||
import static junit.framework.TestCase.assertNull;
|
import static junit.framework.TestCase.assertNull;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
|
|
||||||
public class GetValueFromTrc {
|
public class GetValueFromTrcTest {
|
||||||
|
|
||||||
public static final double EPS = 0.01;
|
public static final double EPS = 0.01;
|
||||||
|
|
||||||
|
private static int getBitIndex(byte[] data, int bitIndex, int bitWidth) {
|
||||||
|
return DbcField.getBitRange(data, bitIndex, bitWidth, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBigEndian() {
|
||||||
|
byte[] data = {(byte) 0xAB, 0x56};
|
||||||
|
// hmmm
|
||||||
|
assertEquals(0x56AB, DbcField.getBitRange(data, 0, 16, false));
|
||||||
|
assertEquals(0xAB56, DbcField.getBitRange(data, 8, 16, true));
|
||||||
|
|
||||||
|
assertEquals(0xAB, DbcField.getBitRange(data, 0, 8, false));
|
||||||
|
assertEquals(0xAB, DbcField.getBitRange(data, 0, 8, true));
|
||||||
|
|
||||||
|
assertEquals(0x56, DbcField.getBitRange(data, 8, 8, false));
|
||||||
|
assertEquals(0x56, DbcField.getBitRange(data, 8, 8, true));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() throws IOException {
|
public void test() throws IOException {
|
||||||
DbcFile dbc = new DbcFile(LoggingStrategy.LOG_ONLY_TRANSLATED_FIELDS);
|
DbcFile dbc = new DbcFile(LoggingStrategy.LOG_ONLY_TRANSLATED_FIELDS);
|
||||||
|
@ -36,13 +53,13 @@ public class GetValueFromTrc {
|
||||||
assertEquals(8, packet.getData().length);
|
assertEquals(8, packet.getData().length);
|
||||||
assertEquals(640, packet.getId());
|
assertEquals(640, packet.getId());
|
||||||
|
|
||||||
assertEquals(0x12DF, DbcField.getBitIndex(packet.getData(), 16, 16));
|
assertEquals(0x12DF, getBitIndex(packet.getData(), 16, 16));
|
||||||
assertEquals(0xDF1D, DbcField.getBitIndex(packet.getData(), 8, 16));
|
assertEquals(0xDF1D, getBitIndex(packet.getData(), 8, 16));
|
||||||
|
|
||||||
assertEquals(1, DbcField.getBitIndex(packet.getData(), 0, 3));
|
assertEquals(1, getBitIndex(packet.getData(), 0, 3));
|
||||||
|
|
||||||
assertEquals(0x1D, DbcField.getBitIndex(packet.getData(), 8, 8));
|
assertEquals(0x1D, getBitIndex(packet.getData(), 8, 8));
|
||||||
assertEquals(13 , DbcField.getBitIndex(packet.getData(), 8, 4));
|
assertEquals(13, getBitIndex(packet.getData(), 8, 4));
|
||||||
|
|
||||||
|
|
||||||
DbcField bf = dbc.getPacketByIndexSlow(0).find("rpm");
|
DbcField bf = dbc.getPacketByIndexSlow(0).find("rpm");
|
|
@ -11,7 +11,7 @@ import java.io.StringReader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertEquals;
|
import static junit.framework.TestCase.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class ParseDBCTest {
|
public class ParseDBCTest {
|
||||||
public static final String VAG_MOTOR_1 = "BO_ 640 Motor_1: 8 XXX\n" +
|
public static final String VAG_MOTOR_1 = "BO_ 640 Motor_1: 8 XXX\n" +
|
||||||
|
@ -30,7 +30,7 @@ public class ParseDBCTest {
|
||||||
" SG_ Fahrpedalwert_ungenau__Motor_1_ M : 1|1@1+ (1,0) [0|0] \"\" XXX\n" +
|
" SG_ Fahrpedalwert_ungenau__Motor_1_ M : 1|1@1+ (1,0) [0|0] \"\" XXX\n" +
|
||||||
" SG_ Leergasinformation : 0|1@1+ (1,0) [0|0] \"\" XXX" +
|
" SG_ Leergasinformation : 0|1@1+ (1,0) [0|0] \"\" XXX" +
|
||||||
"";
|
"";
|
||||||
public static final String RPM_DBC = "VERSION \"\"\n" +
|
private static final String RPM_DBC = "VERSION \"\"\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
"NS_ :\n" +
|
"NS_ :\n" +
|
||||||
|
@ -62,7 +62,7 @@ public class ParseDBCTest {
|
||||||
"\n" +
|
"\n" +
|
||||||
"BO_ 1394 ZAS_1: 2 XXX\n" +
|
"BO_ 1394 ZAS_1: 2 XXX\n" +
|
||||||
" SG_ Fehlerspeichereintrag__ZAS_ : 15|1@1+ (1,0) [0|0] \"\" XXX\n" +
|
" SG_ Fehlerspeichereintrag__ZAS_ : 15|1@1+ (1,0) [0|0] \"\" XXX\n" +
|
||||||
" SG_ Frei_ZAS_1_3 : 8|7@1+ (1,0) [0|0] \"\" XXX\n" +
|
" SG_ Frei_ZAS_1_3 : 8|7@0+ (1,0) [0|0] \"\" XXX\n" +
|
||||||
" SG_ Frei_ZAS_1_2 : 7|1@1+ (1,0) [0|0] \"\" XXX\n" +
|
" SG_ Frei_ZAS_1_2 : 7|1@1+ (1,0) [0|0] \"\" XXX\n" +
|
||||||
" SG_ Klemme_15_SV : 6|1@1+ (1,0) [0|0] \"\" XXX\n" +
|
" SG_ Klemme_15_SV : 6|1@1+ (1,0) [0|0] \"\" XXX\n" +
|
||||||
" SG_ Frei_ZAS_1_1 : 5|1@1+ (1,0) [0|0] \"\" XXX\n" +
|
" SG_ Frei_ZAS_1_1 : 5|1@1+ (1,0) [0|0] \"\" XXX\n" +
|
||||||
|
@ -102,6 +102,10 @@ public class ParseDBCTest {
|
||||||
|
|
||||||
assertEquals(dbc.packets.size(), 3);
|
assertEquals(dbc.packets.size(), 3);
|
||||||
|
|
||||||
|
DbcPacket zacPacket = dbc.getPacketByIndexSlow(0);
|
||||||
|
assertFalse(zacPacket.getFields().get(0).isBigEndian());
|
||||||
|
assertTrue(zacPacket.getFields().get(1).isBigEndian());
|
||||||
|
|
||||||
DbcPacket motorPacket = dbc.getPacketByIndexSlow(2);
|
DbcPacket motorPacket = dbc.getPacketByIndexSlow(2);
|
||||||
assertNotNull(motorPacket);
|
assertNotNull(motorPacket);
|
||||||
assertEquals(motorPacket.getId(), 640);
|
assertEquals(motorPacket.getId(), 640);
|
||||||
|
|
Loading…
Reference in New Issue