Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initializes CRC lookup tables at module initialization #1051

Merged
merged 2 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions can/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,15 @@ void gen_crc_lookup_table_16(uint16_t poly, uint16_t crc_lut[]) {
}
}

void init_crc_lookup_tables() {
// At init time, set up static lookup tables for fast CRC computation.
gen_crc_lookup_table_8(0x2F, crc8_lut_8h2f); // CRC-8 8H2F/AUTOSAR for Volkswagen
gen_crc_lookup_table_16(0x1021, crc16_lut_xmodem); // CRC-16 XMODEM for HKG CAN FD
}
// Initializes CRC lookup tables at module initialization
struct CrcInitializer {
CrcInitializer() {
gen_crc_lookup_table_8(0x2F, crc8_lut_8h2f); // CRC-8 8H2F/AUTOSAR for Volkswagen
gen_crc_lookup_table_16(0x1021, crc16_lut_xmodem); // CRC-16 XMODEM for HKG CAN FD
}
};

static CrcInitializer crcInitializer;

unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d) {
// Volkswagen uses standard CRC8 8H2F/AUTOSAR, but they compute it with
Expand Down
2 changes: 0 additions & 2 deletions can/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#define MAX_BAD_COUNTER 5
#define CAN_INVALID_CNT 5

void init_crc_lookup_tables();

// Car specific functions
unsigned int honda_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
unsigned int toyota_checksum(uint32_t address, const Signal &sig, const std::vector<uint8_t> &d);
Expand Down
1 change: 0 additions & 1 deletion can/packer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ CANPacker::CANPacker(const std::string& dbc_name) {
signal_lookup[std::make_pair(msg.address, sig.name)] = sig;
}
}
init_crc_lookup_tables();
}

std::vector<uint8_t> CANPacker::pack(uint32_t address, const std::vector<SignalPackValue> &signals) {
Expand Down
2 changes: 0 additions & 2 deletions can/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ CANParser::CANParser(int abus, const std::string& dbc_name, const std::vector<st
: bus(abus), aligned_buf(kj::heapArray<capnp::word>(1024)) {
dbc = dbc_lookup(dbc_name);
assert(dbc);
init_crc_lookup_tables();

bus_timeout_threshold = std::numeric_limits<uint64_t>::max();

Expand Down Expand Up @@ -149,7 +148,6 @@ CANParser::CANParser(int abus, const std::string& dbc_name, bool ignore_checksum

dbc = dbc_lookup(dbc_name);
assert(dbc);
init_crc_lookup_tables();

for (const auto& msg : dbc->msgs) {
MessageState state = {
Expand Down