can-log-tools/reader/src/main/java/com/rusefi/can/reader/dbc/DbcField.java

114 lines
3.0 KiB
Java
Raw Normal View History

2022-06-20 14:29:02 -07:00
package com.rusefi.can.reader.dbc;
2022-06-20 18:49:31 -07:00
import com.rusefi.can.CANPacket;
2022-06-20 14:29:02 -07:00
public class DbcField {
2022-08-19 20:16:59 -07:00
private String name;
2022-06-20 14:29:02 -07:00
private final int startOffset;
private final int length;
private final double mult;
2022-08-27 16:57:29 -07:00
private final double offset;
2022-08-16 22:01:48 -07:00
private String category;
2022-08-19 20:16:59 -07:00
private boolean isNiceName;
2022-06-20 14:29:02 -07:00
2022-08-27 16:57:29 -07:00
public DbcField(String name, int startOffset, int length, double mult, double offset, String category) {
2022-06-20 14:29:02 -07:00
this.name = name;
this.startOffset = startOffset;
this.length = length;
this.mult = mult;
2022-08-27 16:57:29 -07:00
this.offset = offset;
2022-08-16 22:01:48 -07:00
this.category = category;
}
2022-08-27 16:57:29 -07:00
public static DbcField parseField(DbcPacket parent, String line) {
line = DbcFile.replaceSpecialWithSpaces(line);
String[] tokens = line.split(" ");
2022-09-12 23:26:02 -07:00
if (tokens.length < 2)
return null;
2022-08-27 16:57:29 -07:00
String name = tokens[1];
int index = 1;
while (!tokens[index - 1].equals(":"))
index++;
if (DbcFile.debugEnabled)
System.out.println(line);
int startOffset;
try {
startOffset = Integer.parseInt(tokens[index]);
} catch (NumberFormatException e) {
throw new IllegalStateException("While " + line, e);
}
int length = Integer.parseInt(tokens[index + 1]);
double mult = Double.parseDouble(tokens[index + 3]);
double offset = Double.parseDouble(tokens[index + 4]);
return new DbcField(name, startOffset, length, mult, offset, parent.getName());
}
2022-08-16 22:01:48 -07:00
public String getCategory() {
return category;
2022-06-20 14:29:02 -07:00
}
2022-08-19 20:16:59 -07:00
public boolean isNiceName() {
return isNiceName;
}
2022-06-20 14:29:02 -07:00
public String getName() {
return name;
}
public int getStartOffset() {
return startOffset;
}
public int getLength() {
return length;
}
public double getMult() {
return mult;
}
2022-08-27 16:57:29 -07:00
public double getOffset() {
return offset;
}
2022-06-20 14:29:02 -07:00
@Override
public String toString() {
return "DbcField{" +
"name='" + name + '\'' +
", startOffset=" + startOffset +
", length=" + length +
", mult=" + mult +
'}';
}
2022-06-20 18:29:27 -07:00
public static int getBitIndex(byte[] data, int bitIndex, int bitWidth) {
2022-06-26 09:54:11 -07:00
if (bitIndex < 0)
throw new IllegalArgumentException("Huh? " + bitIndex + " " + bitWidth);
2022-06-20 18:29:27 -07:00
int byteIndex = bitIndex >> 3;
int shift = bitIndex - byteIndex * 8;
2022-06-26 09:54:11 -07:00
if (byteIndex >= data.length)
return 0;
2022-06-20 18:49:31 -07:00
int value = data[byteIndex] & 0xff;
2022-06-20 18:29:27 -07:00
if (shift + bitWidth > 8) {
2022-06-26 09:54:11 -07:00
if (byteIndex + 1 >= data.length)
return 0;
2022-06-20 18:29:27 -07:00
value = value + data[1 + byteIndex] * 256;
}
int mask = (1 << bitWidth) - 1;
return (value >> shift) & mask;
}
2022-06-20 18:49:31 -07:00
public double getValue(CANPacket packet) {
2022-08-27 16:57:29 -07:00
return getBitIndex(packet.getData(), startOffset, length) * mult + offset;
2022-06-20 18:49:31 -07:00
}
2022-08-19 20:16:59 -07:00
public void rename(String niceName) {
name = niceName;
isNiceName = true;
}
2022-06-20 14:29:02 -07:00
}