Skip to content

Commit

Permalink
first pass at PQ checksums and counters
Browse files Browse the repository at this point in the history
  • Loading branch information
jyoung8607 committed Jun 9, 2022
1 parent fd82dcb commit cb6d980
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 20 deletions.
13 changes: 12 additions & 1 deletion can/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void init_crc_lookup_tables() {
gen_crc_lookup_table_16(0x1021, crc16_lut_xmodem); // CRC-16 XMODEM for HKG CAN FD
}

unsigned int volkswagen_crc(uint32_t address, const std::vector<uint8_t> &d) {
unsigned int volkswagen_mqb_crc(uint32_t address, const std::vector<uint8_t> &d) {
// Volkswagen uses standard CRC8 8H2F/AUTOSAR, but they compute it with
// a magic variable padding byte tacked onto the end of the payload.
// https://www.autosar.org/fileadmin/user_upload/standards/classic/4-3/AUTOSAR_SWS_CRCLibrary.pdf
Expand Down Expand Up @@ -188,6 +188,17 @@ unsigned int volkswagen_crc(uint32_t address, const std::vector<uint8_t> &d) {
return crc ^ 0xFF; // Return after standard final XOR for CRC8 8H2F/AUTOSAR
}

unsigned int volkswagen_pq_checksum(uint32_t address, const std::vector<uint8_t> &d) {
uint8_t checksum = 0;

// Simple checksum over the payload, skipping over the first byte where the checksum lives.
for (int i = 1; i < d.size(); i++) {
checksum ^= d[i];
}

return checksum;
}

unsigned int pedal_checksum(const std::vector<uint8_t> &d) {
uint8_t crc = 0xFF;
uint8_t poly = 0xD5; // standard crc8
Expand Down
3 changes: 2 additions & 1 deletion can/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ unsigned int honda_checksum(uint32_t address, const std::vector<uint8_t> &d);
unsigned int toyota_checksum(uint32_t address, const std::vector<uint8_t> &d);
unsigned int subaru_checksum(uint32_t address, const std::vector<uint8_t> &d);
unsigned int chrysler_checksum(uint32_t address, const std::vector<uint8_t> &d);
unsigned int volkswagen_crc(uint32_t address, const std::vector<uint8_t> &d);
unsigned int volkswagen_mqb_crc(uint32_t address, const std::vector<uint8_t> &d);
unsigned int volkswagen_pq_checksum(uint32_t address, const std::vector<uint8_t> &d);
unsigned int hkg_can_fd_checksum(uint32_t address, const std::vector<uint8_t> &d);
unsigned int pedal_checksum(const std::vector<uint8_t> &d);

Expand Down
2 changes: 2 additions & 0 deletions can/common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ cdef extern from "common_dbc.h":
PEDAL_COUNTER,
VOLKSWAGEN_MQB_CHECKSUM,
VOLKSWAGEN_MQB_COUNTER,
VOLKSWAGEN_PQ_CHECKSUM,
VOLKSWAGEN_PQ_COUNTER,
SUBARU_CHECKSUM,
CHRYSLER_CHECKSUM
HKG_CAN_FD_CHECKSUM,
Expand Down
2 changes: 2 additions & 0 deletions can/common_dbc.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ enum SignalType {
PEDAL_COUNTER,
VOLKSWAGEN_MQB_CHECKSUM,
VOLKSWAGEN_MQB_COUNTER,
VOLKSWAGEN_PQ_CHECKSUM,
VOLKSWAGEN_PQ_COUNTER,
SUBARU_CHECKSUM,
CHRYSLER_CHECKSUM,
HKG_CAN_FD_CHECKSUM,
Expand Down
2 changes: 2 additions & 0 deletions can/dbc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ ChecksumState* get_checksum(const std::string& dbc_name) {
s = new ChecksumState({16, 8, 0, 0, true, HKG_CAN_FD_CHECKSUM, HKG_CAN_FD_COUNTER});
} else if (startswith(dbc_name, "vw_mqb")) {
s = new ChecksumState({8, 4, 0, 0, true, VOLKSWAGEN_MQB_CHECKSUM, VOLKSWAGEN_MQB_COUNTER});
} else if (startswith(dbc_name, "vw_golf")) {
s = new ChecksumState({8, 4, 0, 0, true, VOLKSWAGEN_PQ_CHECKSUM, VOLKSWAGEN_PQ_COUNTER});
} else if (startswith(dbc_name, "subaru_global_")) {
s = new ChecksumState({8, -1, 0, -1, true, SUBARU_CHECKSUM});
} else if (startswith(dbc_name, "chrysler_")) {
Expand Down
9 changes: 7 additions & 2 deletions can/packer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ std::vector<uint8_t> CANPacker::pack(uint32_t address, const std::vector<SignalP
}
const auto& sig = sig_it->second;

if ((sig.type != SignalType::HONDA_COUNTER) && (sig.type != SignalType::VOLKSWAGEN_MQB_COUNTER)) {
if ((sig.type != SignalType::HONDA_COUNTER) &&
(sig.type != SignalType::VOLKSWAGEN_MQB_COUNTER) &&
(sig.type != SignalType::VOLKSWAGEN_PQ_COUNTER)) {
//WARN("COUNTER signal type not valid\n");
}

Expand All @@ -88,7 +90,10 @@ std::vector<uint8_t> CANPacker::pack(uint32_t address, const std::vector<SignalP
unsigned int chksm = toyota_checksum(address, ret);
set_value(ret, sig, chksm);
} else if (sig.type == SignalType::VOLKSWAGEN_MQB_CHECKSUM) {
unsigned int chksm = volkswagen_crc(address, ret);
unsigned int chksm = volkswagen_mqb_crc(address, ret);
set_value(ret, sig, chksm);
} else if (sig.type == SignalType::VOLKSWAGEN_PQ_CHECKSUM) {
unsigned int chksm = volkswagen_pq_checksum(address, ret);
set_value(ret, sig, chksm);
} else if (sig.type == SignalType::SUBARU_CHECKSUM) {
unsigned int chksm = subaru_checksum(address, ret);
Expand Down
7 changes: 5 additions & 2 deletions can/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ bool MessageState::parse(uint64_t sec, const std::vector<uint8_t> &dat) {
checksum_failed = true;
} else if (sig.type == SignalType::TOYOTA_CHECKSUM && toyota_checksum(address, dat) != tmp) {
checksum_failed = true;
} else if (sig.type == SignalType::VOLKSWAGEN_MQB_CHECKSUM && volkswagen_crc(address, dat) != tmp) {
} else if (sig.type == SignalType::VOLKSWAGEN_MQB_CHECKSUM && volkswagen_mqb_crc(address, dat) != tmp) {
checksum_failed = true;
} else if (sig.type == SignalType::VOLKSWAGEN_PQ_CHECKSUM && volkswagen_pq_checksum(address, dat) != tmp) {
checksum_failed = true;
} else if (sig.type == SignalType::SUBARU_CHECKSUM && subaru_checksum(address, dat) != tmp) {
checksum_failed = true;
Expand All @@ -64,7 +66,8 @@ bool MessageState::parse(uint64_t sec, const std::vector<uint8_t> &dat) {

bool counter_failed = false;
if (!ignore_counter) {
if (sig.type == SignalType::HONDA_COUNTER || sig.type == SignalType::VOLKSWAGEN_MQB_COUNTER || sig.type == SignalType::PEDAL_COUNTER) {
if (sig.type == SignalType::HONDA_COUNTER || sig.type == SignalType::VOLKSWAGEN_MQB_COUNTER ||
sig.type == SignalType::VOLKSWAGEN_PQ_COUNTER || sig.type == SignalType::PEDAL_COUNTER) {
counter_failed = !update_counter_generic(tmp, sig.size);
}
}
Expand Down
28 changes: 14 additions & 14 deletions vw_golf_mk4.dbc
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ BO_ 1504 Klima_1: 8 XXX
SG_ Drehzahlanhebung : 0|1@1+ (1,0) [0|0] "" XXX

BO_ 906 GRA_Neu: 4 XXX
SG_ GRA_Checksum : 0|8@1+ (1,0) [0|255] "" XXX
SG_ CHECKSUM : 0|8@1+ (1,0) [0|255] "" XXX
SG_ GRA_Hauptschalt : 8|1@1+ (1,0) [0|1] "" XXX
SG_ GRA_Abbrechen : 9|1@1+ (1,0) [0|1] "" XXX
SG_ GRA_Down_kurz : 10|1@1+ (1,0) [0|1] "" XXX
Expand All @@ -628,7 +628,7 @@ BO_ 906 GRA_Neu: 4 XXX
SG_ GRA_Neu_Setzen : 16|1@1+ (1,0) [0|1] "" XXX
SG_ GRA_Recall : 17|1@1+ (1,0) [0|1] "" XXX
SG_ GRA_Sender : 18|2@1+ (1,0) [0|3] "" XXX
SG_ GRA_Neu_Zaehler : 20|4@1+ (1,0) [0|15] "" XXX
SG_ COUNTER : 20|4@1+ (1,0) [0|15] "" XXX
SG_ GRA_Tip_Down : 24|1@1+ (1,0) [0|1] "" XXX
SG_ GRA_Tip_Up : 25|1@1+ (1,0) [0|1] "" XXX
SG_ GRA_Zeitluecke : 26|2@1+ (1,0) [0|3] "" XXX
Expand Down Expand Up @@ -683,7 +683,7 @@ BO_ 1344 Getriebe_2: 8 XXX
BO_ 1088 Getriebe_1: 8 XXX
SG_ Wandlerverlustmoment : 56|8@1+ (0.39,0) [0|99.06] "MDI" XXX
SG_ Fehlerspeichereintrag__Getriebe : 55|1@1+ (1,0) [0|0] "" XXX
SG_ Zaehler_Getriebe_1 : 51|4@1+ (1,0) [0|15] "" XXX
SG_ COUNTER : 51|4@1+ (1,0) [0|15] "" XXX
SG_ Gang_eingelegt : 50|1@1+ (1,0) [0|0] "" XXX
SG_ Schaltabsicht : 49|1@1+ (1,0) [0|0] "" XXX
SG_ Motor_aus : 48|1@1+ (1,0) [0|0] "" XXX
Expand Down Expand Up @@ -857,8 +857,8 @@ BO_ 424 Bremse_6: 3 XXX
SG_ Bremsdruck__Bremse_6_ : 0|10@1+ (0.3255,-40) [-40|293] "bar" XXX

BO_ 1192 Bremse_5: 8 XXX
SG_ Checksumme_Bremse_5 : 56|8@1+ (1,0) [0|0] "" XXX
SG_ Zaehler_Bremse_5 : 52|4@1+ (1,0) [0|15] "" XXX
SG_ CHECKSUM : 56|8@1+ (1,0) [0|0] "" XXX
SG_ COUNTER : 52|4@1+ (1,0) [0|15] "" XXX
SG_ Bremslicht_ECD : 51|1@1+ (1,0) [0|0] "" XXX
SG_ Bremsentemperatur_vorn : 48|3@1+ (125,125) [125|1000] "C" XXX
SG_ Frei_Bremse_5_5 : 40|8@1+ (1,0) [0|0] "" XXX
Expand Down Expand Up @@ -909,7 +909,7 @@ BO_ 416 Bremse_1: 8 XXX
SG_ ESP_Systemstatus_4_1 : 62|1@1+ (1,0) [0|0] "" XXX
SG_ ESP_Passiv_getastet : 61|1@1+ (1,0) [0|0] "" XXX
SG_ ASR_Steuerger_t : 60|1@1+ (1,0) [0|0] "" XXX
SG_ Zaehler_Bremse_1 : 56|4@1+ (1,0) [0|15] "" XXX
SG_ COUNTER : 56|4@1+ (1,0) [0|15] "" XXX
SG_ MSR_Eingriffsmoment : 48|8@1+ (0.39,0) [0|99.06] "MDI" XXX
SG_ ASR_Eingriffsmoment_schnell : 40|8@1+ (0.39,0) [0|99.06] "MDI" XXX
SG_ ASR_Eingriffsmoment_langsam : 32|8@1+ (0.39,0) [0|99.06] "MDI" XXX
Expand Down Expand Up @@ -981,8 +981,8 @@ BO_ 1360 Airbag_2: 2 XXX
SG_ Checksumme_Airbag_2__reserviert : 0|8@1+ (1,0) [0|0] "" XXX

BO_ 80 Airbag_1: 4 XXX
SG_ Checksumme_Airbag_1 : 24|8@1+ (1,0) [0|0] "" XXX
SG_ Zaehler_Airbag_1 : 20|4@1+ (1,0) [0|15] "" XXX
SG_ CHECKSUM : 24|8@1+ (1,0) [0|0] "" XXX
SG_ COUNTER : 20|4@1+ (1,0) [0|15] "" XXX
SG_ Fehlerspeichereintrag : 19|1@1+ (1,0) [0|0] "" XXX
SG_ Frei_Airbag_1_2 : 18|1@1+ (1,0) [0|0] "" XXX
SG_ Airbag_im_Stellgliedtest : 17|1@1+ (1,0) [0|0] "" XXX
Expand Down Expand Up @@ -1084,7 +1084,7 @@ BO_ 872 ACC_System: 8 XXX
SG_ ACS_max_AendGrad : 48|8@1+ (1,0.02) [0.02|5.06] "Unit_MeterPerSeconSquar" XXX

BO_ 1386 ACC_GRA_Anziege: 8 XXX
SG_ ACA_Checksum : 0|8@1+ (1,0) [0|255] "" XXX
SG_ CHECKSUM : 0|8@1+ (1,0) [0|255] "" XXX
SG_ ACA_StaACC : 8|3@1+ (1,0) [0|7] "" XXX
SG_ ACA_ID_StaACC : 11|5@1+ (1,0) [0|31] "" XXX
SG_ ACA_Fahrerhinw : 16|1@1+ (1,0) [0|1] "" XXX
Expand All @@ -1102,12 +1102,12 @@ BO_ 1386 ACC_GRA_Anziege: 8 XXX
SG_ ACA_Codierung : 56|1@1+ (1,0) [0|1] "" XXX
SG_ ACA_Tachokranz : 57|1@1+ (1,0) [0|1] "" XXX
SG_ ACA_Aend_Zeitluecke : 58|1@1+ (1,0) [0|1] "" XXX
SG_ ACA_Zaehler : 60|4@1+ (1,0) [0|15] "" XXX
SG_ COUNTER : 60|4@1+ (1,0) [0|15] "" XXX

BO_ 208 Lenkhilfe_3: 6 XXX
SG_ LH3_Checksumme : 0|8@1+ (1,0) [0|255] "" XXX
SG_ CHECKSUM : 0|8@1+ (1,0) [0|255] "" XXX
SG_ LH3_BS_Spiegel : 8|4@1+ (1,0) [0|15] "" XXX
SG_ LH3_Zaehler : 12|4@1+ (1,0) [0|15] "" XXX
SG_ COUNTER : 12|4@1+ (1,0) [0|15] "" XXX
SG_ LH3_LM : 16|10@1+ (1,0) [0|1023] "" XXX
SG_ LH3_LMSign : 26|1@1+ (1,0) [0|1] "" XXX
SG_ LH3_LMValid : 27|1@1+ (1,0) [0|1] "" XXX
Expand All @@ -1118,8 +1118,8 @@ BO_ 208 Lenkhilfe_3: 6 XXX
SG_ LH3_Lenkungstyp : 46|2@1+ (1,0) [0|3] "" XXX

BO_ 978 Lenkhilfe_2: 8 XXX
SG_ LH2_Checksumme : 0|8@1+ (1,0) [0|255] "" XXX
SG_ LH2_Zaehler : 8|4@1+ (1,0) [0|15] "" XXX
SG_ CHECKSUM : 0|8@1+ (1,0) [0|255] "" XXX
SG_ COUNTER : 8|4@1+ (1,0) [0|15] "" XXX
SG_ LH2_Geradeaus : 12|1@1+ (1,0) [0|1] "" XXX
SG_ LH2_Sta_Charisma : 13|3@1+ (1,0) [0|7] "" XXX
SG_ LH2_Sta_HCA : 16|4@1+ (1,0) [0|15] "" XXX
Expand Down

0 comments on commit cb6d980

Please sign in to comment.