From cb6d980aa827e0c1a2c46d9311e222b2a705868f Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 9 Jun 2022 13:02:38 -0400 Subject: [PATCH] first pass at PQ checksums and counters --- can/common.cc | 13 ++++++++++++- can/common.h | 3 ++- can/common.pxd | 2 ++ can/common_dbc.h | 2 ++ can/dbc.cc | 2 ++ can/packer.cc | 9 +++++++-- can/parser.cc | 7 +++++-- vw_golf_mk4.dbc | 28 ++++++++++++++-------------- 8 files changed, 46 insertions(+), 20 deletions(-) diff --git a/can/common.cc b/can/common.cc index e9abbedb32..40d3d9013e 100644 --- a/can/common.cc +++ b/can/common.cc @@ -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 &d) { +unsigned int volkswagen_mqb_crc(uint32_t address, const std::vector &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 @@ -188,6 +188,17 @@ unsigned int volkswagen_crc(uint32_t address, const std::vector &d) { return crc ^ 0xFF; // Return after standard final XOR for CRC8 8H2F/AUTOSAR } +unsigned int volkswagen_pq_checksum(uint32_t address, const std::vector &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 &d) { uint8_t crc = 0xFF; uint8_t poly = 0xD5; // standard crc8 diff --git a/can/common.h b/can/common.h index 7e35e06a3f..c094191906 100644 --- a/can/common.h +++ b/can/common.h @@ -26,7 +26,8 @@ unsigned int honda_checksum(uint32_t address, const std::vector &d); unsigned int toyota_checksum(uint32_t address, const std::vector &d); unsigned int subaru_checksum(uint32_t address, const std::vector &d); unsigned int chrysler_checksum(uint32_t address, const std::vector &d); -unsigned int volkswagen_crc(uint32_t address, const std::vector &d); +unsigned int volkswagen_mqb_crc(uint32_t address, const std::vector &d); +unsigned int volkswagen_pq_checksum(uint32_t address, const std::vector &d); unsigned int hkg_can_fd_checksum(uint32_t address, const std::vector &d); unsigned int pedal_checksum(const std::vector &d); diff --git a/can/common.pxd b/can/common.pxd index 11ec8b115d..80dcdce0e3 100644 --- a/can/common.pxd +++ b/can/common.pxd @@ -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, diff --git a/can/common_dbc.h b/can/common_dbc.h index 567cfeae65..38e6764275 100644 --- a/can/common_dbc.h +++ b/can/common_dbc.h @@ -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, diff --git a/can/dbc.cc b/can/dbc.cc index 280cefcf13..7a0effbdf5 100644 --- a/can/dbc.cc +++ b/can/dbc.cc @@ -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_")) { diff --git a/can/packer.cc b/can/packer.cc index e791160abb..25baa0047f 100644 --- a/can/packer.cc +++ b/can/packer.cc @@ -70,7 +70,9 @@ std::vector CANPacker::pack(uint32_t address, const std::vectorsecond; - 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"); } @@ -88,7 +90,10 @@ std::vector CANPacker::pack(uint32_t address, const std::vector &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; @@ -64,7 +66,8 @@ bool MessageState::parse(uint64_t sec, const std::vector &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); } } diff --git a/vw_golf_mk4.dbc b/vw_golf_mk4.dbc index f99dd2913c..0a151ad489 100644 --- a/vw_golf_mk4.dbc +++ b/vw_golf_mk4.dbc @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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