2023-10-22 20:13:55 -07:00
|
|
|
package com.rusefi.can.analysis;
|
|
|
|
|
|
|
|
import com.rusefi.can.CANPacket;
|
|
|
|
import org.yaml.snakeyaml.Yaml;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
|
|
public class ChecksumScanner {
|
|
|
|
|
|
|
|
public static final String CHECKSUM_YAML = "checksum.yaml";
|
|
|
|
|
2024-02-15 18:21:27 -08:00
|
|
|
public static void scanForChecksums(String reportDestinationFolder, String simpleFileName, List<CANPacket> packets) throws IOException {
|
2023-10-22 20:13:55 -07:00
|
|
|
Map<Integer, AtomicBoolean> isChecksumMap = new HashMap<>();
|
|
|
|
|
|
|
|
J1850_SAE_crc8_Calculator c = new J1850_SAE_crc8_Calculator();
|
|
|
|
|
|
|
|
for (CANPacket packet : packets) {
|
|
|
|
AtomicBoolean isChecksum = isChecksumMap.computeIfAbsent(packet.getId(), integer -> new AtomicBoolean(true));
|
|
|
|
if (!isChecksum.get())
|
|
|
|
continue;
|
|
|
|
byte[] data = packet.getData();
|
|
|
|
if (data.length != 8) {
|
|
|
|
isChecksum.set(false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
byte checksum = c.crc8(data, 7);
|
|
|
|
isChecksum.set(data[7] == checksum);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<Integer> withChecksum = new ArrayList<>();
|
|
|
|
|
|
|
|
for (Map.Entry<Integer, AtomicBoolean> e : isChecksumMap.entrySet()) {
|
|
|
|
if (e.getValue().get()) {
|
|
|
|
Integer sid = e.getKey();
|
2024-04-10 15:35:47 -07:00
|
|
|
System.out.println("ChecksumScanner: Ends with checksum " + sid);
|
2023-10-22 20:13:55 -07:00
|
|
|
withChecksum.add(sid);
|
|
|
|
}
|
|
|
|
}
|
2023-11-08 16:53:33 -08:00
|
|
|
withChecksum.sort(Comparator.naturalOrder());
|
2023-10-22 20:13:55 -07:00
|
|
|
Yaml yaml = new Yaml();
|
2024-02-15 18:21:27 -08:00
|
|
|
// simpleFileName + "_" +
|
2023-10-22 20:13:55 -07:00
|
|
|
String yamlCountersReportFileName = reportDestinationFolder + File.separator + CHECKSUM_YAML;
|
2024-02-09 18:25:39 -08:00
|
|
|
System.out.println(new Date() + " Writing report to " + yamlCountersReportFileName);
|
2023-10-22 20:13:55 -07:00
|
|
|
yaml.dump(withChecksum, new FileWriter(yamlCountersReportFileName));
|
|
|
|
}
|
|
|
|
}
|