Skip to content

Commit

Permalink
if-spaghetti reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
jyoung8607 committed Jun 22, 2022
1 parent 5abd736 commit 3acd049
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 43 deletions.
1 change: 1 addition & 0 deletions can/common_dbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> &d);
};

struct Msg {
Expand Down
19 changes: 12 additions & 7 deletions can/dbc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <mutex>
#include <cstring>

#include "common.h"
#include "common_dbc.h"

std::regex bo_regexp(R"(^BO_ (\w+) (\w+) *: (\w+) (\w+))");
Expand Down Expand Up @@ -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<uint8_t> &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");
Expand Down
26 changes: 3 additions & 23 deletions can/packer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,9 @@ std::vector<uint8_t> CANPacker::pack(uint32_t address, const std::vector<SignalP
auto sig_it_checksum = signal_lookup.find(std::make_pair(address, "CHECKSUM"));
if (sig_it_checksum != signal_lookup.end()) {
const auto &sig = sig_it_checksum->second;
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);
}
}

Expand Down
14 changes: 1 addition & 13 deletions can/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,7 @@ bool MessageState::parse(uint64_t sec, const std::vector<uint8_t> &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;
}
}
Expand Down

0 comments on commit 3acd049

Please sign in to comment.