From 3acd049a0b05c29c3094950f88253bf108bad547 Mon Sep 17 00:00:00 2001 From: Jason Young Date: Wed, 22 Jun 2022 15:19:15 -0400 Subject: [PATCH] if-spaghetti reduction --- can/common_dbc.h | 1 + can/dbc.cc | 19 ++++++++++++------- can/packer.cc | 26 +++----------------------- can/parser.cc | 14 +------------- 4 files changed, 17 insertions(+), 43 deletions(-) diff --git a/can/common_dbc.h b/can/common_dbc.h index 567cfeae65..9962c6a4d6 100644 --- a/can/common_dbc.h +++ b/can/common_dbc.h @@ -51,6 +51,7 @@ struct Signal { double factor, offset; bool is_little_endian; SignalType type; + unsigned int (*calc_checksum)(uint32_t address, const Signal &sig, const std::vector &d); }; struct Msg { diff --git a/can/dbc.cc b/can/dbc.cc index c69e907e3d..3b4996c20c 100644 --- a/can/dbc.cc +++ b/can/dbc.cc @@ -9,6 +9,7 @@ #include #include +#include "common.h" #include "common_dbc.h" std::regex bo_regexp(R"(^BO_ (\w+) (\w+) *: (\w+) (\w+))"); @@ -54,35 +55,39 @@ typedef struct ChecksumState { bool little_endian; SignalType checksum_type; SignalType counter_type; + unsigned int (*calc_checksum)(uint32_t address, const Signal &sig, const std::vector &d); } ChecksumState; ChecksumState* get_checksum(const std::string& dbc_name) { ChecksumState* s = nullptr; if (startswith(dbc_name, {"honda_", "acura_"})) { - s = new ChecksumState({4, 2, 3, 5, false, HONDA_CHECKSUM, HONDA_COUNTER}); + s = new ChecksumState({4, 2, 3, 5, false, HONDA_CHECKSUM, HONDA_COUNTER, &honda_checksum}); } else if (startswith(dbc_name, {"toyota_", "lexus_"})) { - s = new ChecksumState({8, -1, 7, -1, false, TOYOTA_CHECKSUM}); + s = new ChecksumState({8, -1, 7, -1, false, TOYOTA_CHECKSUM, DEFAULT, &toyota_checksum}); } else if (startswith(dbc_name, "kia_ev6")) { - s = new ChecksumState({16, 8, 0, 0, true, HKG_CAN_FD_CHECKSUM, HKG_CAN_FD_COUNTER}); + s = new ChecksumState({16, 8, 0, 0, true, HKG_CAN_FD_CHECKSUM, HKG_CAN_FD_COUNTER, &hkg_can_fd_checksum}); } else if (startswith(dbc_name, {"vw_mqb_2010"})) { - s = new ChecksumState({8, 4, 0, 0, true, VOLKSWAGEN_MQB_CHECKSUM, VOLKSWAGEN_MQB_COUNTER}); + s = new ChecksumState({8, 4, 0, 0, true, VOLKSWAGEN_MQB_CHECKSUM, VOLKSWAGEN_MQB_COUNTER, &volkswagen_mqb_checksum}); } else if (startswith(dbc_name, "subaru_global_")) { - s = new ChecksumState({8, -1, 0, -1, true, SUBARU_CHECKSUM}); + s = new ChecksumState({8, -1, 0, -1, true, SUBARU_CHECKSUM, DEFAULT, &subaru_checksum}); } else if (startswith(dbc_name, "chrysler_")) { - s = new ChecksumState({8, -1, 7, -1, false, CHRYSLER_CHECKSUM}); + s = new ChecksumState({8, -1, 7, -1, false, CHRYSLER_CHECKSUM, DEFAULT, &chrysler_checksum}); } else if (startswith(dbc_name, "comma_body")) { - s = new ChecksumState({8, 4, 7, 3, false, PEDAL_CHECKSUM, PEDAL_COUNTER}); + s = new ChecksumState({8, 4, 7, 3, false, PEDAL_CHECKSUM, PEDAL_COUNTER, &pedal_checksum}); } return s; } void set_signal_type(Signal& s, ChecksumState* chk, const std::string& dbc_name, int line_num) { + s.calc_checksum = nullptr; if (chk) { if (s.name == "CHECKSUM") { DBC_ASSERT(chk->checksum_size == -1 || s.size == chk->checksum_size, "CHECKSUM is not " << chk->checksum_size << " bits long"); DBC_ASSERT(chk->checksum_start_bit == -1 || (s.start_bit % 8) == chk->checksum_start_bit, " CHECKSUM starts at wrong bit"); DBC_ASSERT(s.is_little_endian == chk->little_endian, "CHECKSUM has wrong endianness"); + DBC_ASSERT(chk->calc_checksum != nullptr, "CHECKSUM calculate function not supplied"); s.type = chk->checksum_type; + s.calc_checksum = chk->calc_checksum; } else if (s.name == "COUNTER") { DBC_ASSERT(chk->counter_size == -1 || s.size == chk->counter_size, "COUNTER is not " << chk->counter_size << " bits long"); DBC_ASSERT(chk->counter_start_bit == -1 || (s.start_bit % 8) == chk->counter_start_bit, "COUNTER starts at wrong bit"); diff --git a/can/packer.cc b/can/packer.cc index 6d2e8c9f0a..c8d29ab42d 100644 --- a/can/packer.cc +++ b/can/packer.cc @@ -81,29 +81,9 @@ std::vector CANPacker::pack(uint32_t address, const std::vectorsecond; - if (sig.type == SignalType::HONDA_CHECKSUM) { - unsigned int chksm = honda_checksum(address, sig, ret); - set_value(ret, sig, chksm); - } else if (sig.type == SignalType::TOYOTA_CHECKSUM) { - unsigned int chksm = toyota_checksum(address, sig, ret); - set_value(ret, sig, chksm); - } else if (sig.type == SignalType::VOLKSWAGEN_MQB_CHECKSUM) { - unsigned int chksm = volkswagen_mqb_checksum(address, sig, ret); - set_value(ret, sig, chksm); - } else if (sig.type == SignalType::SUBARU_CHECKSUM) { - unsigned int chksm = subaru_checksum(address, sig, ret); - set_value(ret, sig, chksm); - } else if (sig.type == SignalType::CHRYSLER_CHECKSUM) { - unsigned int chksm = chrysler_checksum(address, sig, ret); - set_value(ret, sig, chksm); - } else if (sig.type == SignalType::PEDAL_CHECKSUM) { - unsigned int chksm = pedal_checksum(address, sig, ret); - set_value(ret, sig, chksm); - } else if (sig.type == SignalType::HKG_CAN_FD_CHECKSUM) { - unsigned int chksm = hkg_can_fd_checksum(address, sig, ret); - set_value(ret, sig, chksm); - } else { - //WARN("CHECKSUM signal type not valid\n"); + if (sig.calc_checksum != nullptr) { + unsigned int checksum = sig.calc_checksum(address, sig, ret); + set_value(ret, sig, checksum); } } diff --git a/can/parser.cc b/can/parser.cc index 7efdcf8c9c..903625f545 100644 --- a/can/parser.cc +++ b/can/parser.cc @@ -45,19 +45,7 @@ bool MessageState::parse(uint64_t sec, const std::vector &dat) { bool checksum_failed = false; if (!ignore_checksum) { - if (sig.type == SignalType::HONDA_CHECKSUM && honda_checksum(address, sig, dat) != tmp) { - checksum_failed = true; - } else if (sig.type == SignalType::TOYOTA_CHECKSUM && toyota_checksum(address, sig, dat) != tmp) { - checksum_failed = true; - } else if (sig.type == SignalType::VOLKSWAGEN_MQB_CHECKSUM && volkswagen_mqb_checksum(address, sig, dat) != tmp) { - checksum_failed = true; - } else if (sig.type == SignalType::SUBARU_CHECKSUM && subaru_checksum(address, sig, dat) != tmp) { - checksum_failed = true; - } else if (sig.type == SignalType::CHRYSLER_CHECKSUM && chrysler_checksum(address, sig, dat) != tmp) { - checksum_failed = true; - } else if (sig.type == SignalType::HKG_CAN_FD_CHECKSUM && hkg_can_fd_checksum(address, sig, dat) != tmp) { - checksum_failed = true; - } else if (sig.type == SignalType::PEDAL_CHECKSUM && pedal_checksum(address, sig, dat) != tmp) { + if (sig.calc_checksum != nullptr && sig.calc_checksum(address, sig, dat) != tmp) { checksum_failed = true; } }