From 6a22559f6217235df4d5483373c2cab509c79478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Czekan=CC=81ski?= Date: Wed, 11 Sep 2024 23:40:48 +0200 Subject: [PATCH 01/14] VW MQB: Refactor CRC constants Big switch statement in volkswagen_mqb_checksum was refactored with map of arrays. With this approach there's no need for specifying all 16 byte values if constant is the same for all of them. --- opendbc/can/common.cc | 123 +++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 60 deletions(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index bb7b2ee56d..c640bcd8a2 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -1,5 +1,6 @@ #include "opendbc/can/common.h" - +#include +#include unsigned int honda_checksum(uint32_t address, const Signal &sig, const std::vector &d) { int s = 0; @@ -112,6 +113,62 @@ struct CrcInitializer { static CrcInitializer crcInitializer; +static const std::unordered_map> crc_mqb_constants{ + // LWI_01 Steering Angle + {0x86, {0x86}}, + + // LH_EPS_03 Electric Power Steering + {0x9F, {0xF5}}, + + // Getriebe_11 Automatic Gearbox + {0xAD, {0x3F, 0x69, 0x39, 0xDC, 0x94, 0xF9, 0x14, 0x64, 0xD8, 0x6A, 0x34, 0xCE, 0xA2, 0x55, 0xB5, 0x2C}}, + + // ESP_21 Electronic Stability Program + {0xFD, {0xB4, 0xEF, 0xF8, 0x49, 0x1E, 0xE5, 0xC2, 0xC0, 0x97, 0x19, 0x3C, 0xC9, 0xF1, 0x98, 0xD6, 0x61}}, + + // ESP_05 Electronic Stability Program + {0x106, {0x07}}, + + // ACC_10 Automatic Cruise Control + {0x117, {0x16}}, + + // TSK_06 Drivetrain Coordinator + {0x120, {0xC4, 0xE2, 0x4F, 0xE4, 0xF8, 0x2F, 0x56, 0x81, 0x9F, 0xE5, 0x83, 0x44, 0x05, 0x3F, 0x97, 0xDF}}, + + // Motor_20 Driver Throttle Inputs + {0x121, {0xE9, 0x65, 0xAE, 0x6B, 0x7B, 0x35, 0xE5, 0x5F, 0x4E, 0xC7, 0x86, 0xA2, 0xBB, 0xDD, 0xEB, 0xB4}}, + + // ACC_06 Automatic Cruise Control + {0x122, {0x37, 0x7D, 0xF3, 0xA9, 0x18, 0x46, 0x6D, 0x4D, 0x3D, 0x71, 0x92, 0x9C, 0xE5, 0x32, 0x10, 0xB9}}, + + // HCA_01 Heading Control Assist + {0x126, {0xDA}}, + + // GRA_ACC_01 Steering wheel controls for ACC + {0x12B, {0x6A, 0x38, 0xB4, 0x27, 0x22, 0xEF, 0xE1, 0xBB, 0xF8, 0x80, 0x84, 0x49, 0xC7, 0x9E, 0x1E, 0x2B}}, + + // ACC_07 Automatic Cruise Control + {0x12E, {0xF8, 0xE5, 0x97, 0xC9, 0xD6, 0x07, 0x47, 0x21, 0x66, 0xDD, 0xCF, 0x6F, 0xA1, 0x94, 0x74, 0x63}}, + + // EV_Gearshift "Gear" selection data for EVs with no gearbox + {0x187, {0x7F, 0xED, 0x17, 0xC2, 0x7C, 0xEB, 0x44, 0x21, 0x01, 0xFA, 0xDB, 0x15, 0x4A, 0x6B, 0x23, 0x05}}, + + // ACC_02 Automatic Cruise Control + {0x30C, {0x0F}}, + + // SWA_01 Lane Change Assist (SpurWechselAssistent) + {0x30F, {0x0C}}, + + // ACC_04 Automatic Cruise Control + {0x324, {0x27}}, + + // Klemmen_Status_01 ignition and starting status + {0x3C0, {0xC3}}, + + // ESP_20 Electronic Stability Program + {0x65D, {0xAC, 0xB3, 0xAB, 0xEB, 0x7A, 0xE1, 0x3B, 0xF7, 0x73, 0xBA, 0x7C, 0x9E, 0x06, 0x5F, 0x02, 0xD9}}, +}; + unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, 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. @@ -128,68 +185,14 @@ unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, const // Look up and apply the magic final CRC padding byte, which permutes by CAN // address, and additionally (for SOME addresses) by the message counter. uint8_t counter = d[1] & 0x0F; - switch (address) { - case 0x86: // LWI_01 Steering Angle - crc ^= (uint8_t[]){0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86}[counter]; - break; - case 0x9F: // LH_EPS_03 Electric Power Steering - crc ^= (uint8_t[]){0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5}[counter]; - break; - case 0xAD: // Getriebe_11 Automatic Gearbox - crc ^= (uint8_t[]){0x3F, 0x69, 0x39, 0xDC, 0x94, 0xF9, 0x14, 0x64, 0xD8, 0x6A, 0x34, 0xCE, 0xA2, 0x55, 0xB5, 0x2C}[counter]; - break; - case 0xFD: // ESP_21 Electronic Stability Program - crc ^= (uint8_t[]){0xB4, 0xEF, 0xF8, 0x49, 0x1E, 0xE5, 0xC2, 0xC0, 0x97, 0x19, 0x3C, 0xC9, 0xF1, 0x98, 0xD6, 0x61}[counter]; - break; - case 0x106: // ESP_05 Electronic Stability Program - crc ^= (uint8_t[]){0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07}[counter]; - break; - case 0x117: // ACC_10 Automatic Cruise Control - crc ^= (uint8_t[]){0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16}[counter]; - break; - case 0x120: // TSK_06 Drivetrain Coordinator - crc ^= (uint8_t[]){0xC4, 0xE2, 0x4F, 0xE4, 0xF8, 0x2F, 0x56, 0x81, 0x9F, 0xE5, 0x83, 0x44, 0x05, 0x3F, 0x97, 0xDF}[counter]; - break; - case 0x121: // Motor_20 Driver Throttle Inputs - crc ^= (uint8_t[]){0xE9, 0x65, 0xAE, 0x6B, 0x7B, 0x35, 0xE5, 0x5F, 0x4E, 0xC7, 0x86, 0xA2, 0xBB, 0xDD, 0xEB, 0xB4}[counter]; - break; - case 0x122: // ACC_06 Automatic Cruise Control - crc ^= (uint8_t[]){0x37, 0x7D, 0xF3, 0xA9, 0x18, 0x46, 0x6D, 0x4D, 0x3D, 0x71, 0x92, 0x9C, 0xE5, 0x32, 0x10, 0xB9}[counter]; - break; - case 0x126: // HCA_01 Heading Control Assist - crc ^= (uint8_t[]){0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA}[counter]; - break; - case 0x12B: // GRA_ACC_01 Steering wheel controls for ACC - crc ^= (uint8_t[]){0x6A, 0x38, 0xB4, 0x27, 0x22, 0xEF, 0xE1, 0xBB, 0xF8, 0x80, 0x84, 0x49, 0xC7, 0x9E, 0x1E, 0x2B}[counter]; - break; - case 0x12E: // ACC_07 Automatic Cruise Control - crc ^= (uint8_t[]){0xF8, 0xE5, 0x97, 0xC9, 0xD6, 0x07, 0x47, 0x21, 0x66, 0xDD, 0xCF, 0x6F, 0xA1, 0x94, 0x74, 0x63}[counter]; - break; - case 0x187: // EV_Gearshift "Gear" selection data for EVs with no gearbox - crc ^= (uint8_t[]){0x7F, 0xED, 0x17, 0xC2, 0x7C, 0xEB, 0x44, 0x21, 0x01, 0xFA, 0xDB, 0x15, 0x4A, 0x6B, 0x23, 0x05}[counter]; - break; - case 0x30C: // ACC_02 Automatic Cruise Control - crc ^= (uint8_t[]){0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F}[counter]; - break; - case 0x30F: // SWA_01 Lane Change Assist (SpurWechselAssistent) - crc ^= (uint8_t[]){0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C}[counter]; - break; - case 0x324: // ACC_04 Automatic Cruise Control - crc ^= (uint8_t[]){0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27}[counter]; - break; - case 0x3C0: // Klemmen_Status_01 ignition and starting status - crc ^= (uint8_t[]){0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3}[counter]; - break; - case 0x65D: // ESP_20 Electronic Stability Program - crc ^= (uint8_t[]){0xAC, 0xB3, 0xAB, 0xEB, 0x7A, 0xE1, 0x3B, 0xF7, 0x73, 0xBA, 0x7C, 0x9E, 0x06, 0x5F, 0x02, 0xD9}[counter]; - break; - default: // As-yet undefined CAN message, CRC check expected to fail + auto crc_const = crc_mqb_constants.find(address); + if (crc_const != crc_mqb_constants.end()) { + crc ^= crc_const->second[counter]; + } else { // As-yet undefined CAN message, CRC check expected to fail printf("Attempt to CRC check undefined Volkswagen message 0x%02X\n", address); - crc ^= (uint8_t[]){0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}[counter]; - break; } - crc = crc8_lut_8h2f[crc]; + crc = crc8_lut_8h2f[crc]; return crc ^ 0xFF; // Return after standard final XOR for CRC8 8H2F/AUTOSAR } From 06d8f46710612c10600fbda2583ef0e963539b53 Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 15:34:12 -0400 Subject: [PATCH 02/14] temporary, repeat packing step 1M times --- opendbc/can/tests/test_checksums.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/opendbc/can/tests/test_checksums.py b/opendbc/can/tests/test_checksums.py index 52ac79398b..8dc576b6ea 100644 --- a/opendbc/can/tests/test_checksums.py +++ b/opendbc/can/tests/test_checksums.py @@ -2,6 +2,7 @@ from opendbc.can.parser import CANParser from opendbc.can.packer import CANPacker +REPEAT_COUNT = 1000000 class TestCanChecksums: @@ -48,7 +49,9 @@ def verify_vw_mqb_crc(self, subtests, msg_name: str, msg_addr: int, test_message modified = copy.deepcopy(expected) modified.pop(crc_field, None) - modified_msg = packer.make_can_msg(msg_name, 0, modified) + modified_msg = None + for _ in range(REPEAT_COUNT): + modified_msg = packer.make_can_msg(msg_name, 0, modified) parser.update_strings([0, [modified_msg]]) tested = parser.vl[msg_name] From de421f0001b6ec2fd836b4dc7ccf6661f17119b2 Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 15:34:55 -0400 Subject: [PATCH 03/14] follow comma convention for include order --- opendbc/can/common.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index c640bcd8a2..48fbde5c93 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -1,7 +1,8 @@ -#include "opendbc/can/common.h" #include #include +#include "opendbc/can/common.h" + unsigned int honda_checksum(uint32_t address, const Signal &sig, const std::vector &d) { int s = 0; bool extended = address > 0x7FF; From 0656b91f4f28e39efaf8411118f338de5ebf99a1 Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 15:38:49 -0400 Subject: [PATCH 04/14] comma indent convention, tighten message annotations --- opendbc/can/common.cc | 71 +++++++++++-------------------------------- 1 file changed, 18 insertions(+), 53 deletions(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index 48fbde5c93..4fbde8a221 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -115,59 +115,24 @@ struct CrcInitializer { static CrcInitializer crcInitializer; static const std::unordered_map> crc_mqb_constants{ - // LWI_01 Steering Angle - {0x86, {0x86}}, - - // LH_EPS_03 Electric Power Steering - {0x9F, {0xF5}}, - - // Getriebe_11 Automatic Gearbox - {0xAD, {0x3F, 0x69, 0x39, 0xDC, 0x94, 0xF9, 0x14, 0x64, 0xD8, 0x6A, 0x34, 0xCE, 0xA2, 0x55, 0xB5, 0x2C}}, - - // ESP_21 Electronic Stability Program - {0xFD, {0xB4, 0xEF, 0xF8, 0x49, 0x1E, 0xE5, 0xC2, 0xC0, 0x97, 0x19, 0x3C, 0xC9, 0xF1, 0x98, 0xD6, 0x61}}, - - // ESP_05 Electronic Stability Program - {0x106, {0x07}}, - - // ACC_10 Automatic Cruise Control - {0x117, {0x16}}, - - // TSK_06 Drivetrain Coordinator - {0x120, {0xC4, 0xE2, 0x4F, 0xE4, 0xF8, 0x2F, 0x56, 0x81, 0x9F, 0xE5, 0x83, 0x44, 0x05, 0x3F, 0x97, 0xDF}}, - - // Motor_20 Driver Throttle Inputs - {0x121, {0xE9, 0x65, 0xAE, 0x6B, 0x7B, 0x35, 0xE5, 0x5F, 0x4E, 0xC7, 0x86, 0xA2, 0xBB, 0xDD, 0xEB, 0xB4}}, - - // ACC_06 Automatic Cruise Control - {0x122, {0x37, 0x7D, 0xF3, 0xA9, 0x18, 0x46, 0x6D, 0x4D, 0x3D, 0x71, 0x92, 0x9C, 0xE5, 0x32, 0x10, 0xB9}}, - - // HCA_01 Heading Control Assist - {0x126, {0xDA}}, - - // GRA_ACC_01 Steering wheel controls for ACC - {0x12B, {0x6A, 0x38, 0xB4, 0x27, 0x22, 0xEF, 0xE1, 0xBB, 0xF8, 0x80, 0x84, 0x49, 0xC7, 0x9E, 0x1E, 0x2B}}, - - // ACC_07 Automatic Cruise Control - {0x12E, {0xF8, 0xE5, 0x97, 0xC9, 0xD6, 0x07, 0x47, 0x21, 0x66, 0xDD, 0xCF, 0x6F, 0xA1, 0x94, 0x74, 0x63}}, - - // EV_Gearshift "Gear" selection data for EVs with no gearbox - {0x187, {0x7F, 0xED, 0x17, 0xC2, 0x7C, 0xEB, 0x44, 0x21, 0x01, 0xFA, 0xDB, 0x15, 0x4A, 0x6B, 0x23, 0x05}}, - - // ACC_02 Automatic Cruise Control - {0x30C, {0x0F}}, - - // SWA_01 Lane Change Assist (SpurWechselAssistent) - {0x30F, {0x0C}}, - - // ACC_04 Automatic Cruise Control - {0x324, {0x27}}, - - // Klemmen_Status_01 ignition and starting status - {0x3C0, {0xC3}}, - - // ESP_20 Electronic Stability Program - {0x65D, {0xAC, 0xB3, 0xAB, 0xEB, 0x7A, 0xE1, 0x3B, 0xF7, 0x73, 0xBA, 0x7C, 0x9E, 0x06, 0x5F, 0x02, 0xD9}}, + {0x86, {0x86}}, // LWI_01 + {0x9F, {0xF5}}, // LH_EPS_01 + {0xAD, {0x3F, 0x69, 0x39, 0xDC, 0x94, 0xF9, 0x14, 0x64, 0xD8, 0x6A, 0x34, 0xCE, 0xA2, 0x55, 0xB5, 0x2C}}, // Getriebe_11 + {0xFD, {0xB4, 0xEF, 0xF8, 0x49, 0x1E, 0xE5, 0xC2, 0xC0, 0x97, 0x19, 0x3C, 0xC9, 0xF1, 0x98, 0xD6, 0x61}}, // ESP_21 + {0x106, {0x07}}, // ESP_05 + {0x117, {0x16}}, // ACC_10 + {0x120, {0xC4, 0xE2, 0x4F, 0xE4, 0xF8, 0x2F, 0x56, 0x81, 0x9F, 0xE5, 0x83, 0x44, 0x05, 0x3F, 0x97, 0xDF}}, // TSK_06 + {0x121, {0xE9, 0x65, 0xAE, 0x6B, 0x7B, 0x35, 0xE5, 0x5F, 0x4E, 0xC7, 0x86, 0xA2, 0xBB, 0xDD, 0xEB, 0xB4}}, // Motor_20 + {0x122, {0x37, 0x7D, 0xF3, 0xA9, 0x18, 0x46, 0x6D, 0x4D, 0x3D, 0x71, 0x92, 0x9C, 0xE5, 0x32, 0x10, 0xB9}}, // ACC_06 + {0x126, {0xDA}}, // HCA_01 + {0x12B, {0x6A, 0x38, 0xB4, 0x27, 0x22, 0xEF, 0xE1, 0xBB, 0xF8, 0x80, 0x84, 0x49, 0xC7, 0x9E, 0x1E, 0x2B}}, // GRA_ACC_01 + {0x12E, {0xF8, 0xE5, 0x97, 0xC9, 0xD6, 0x07, 0x47, 0x21, 0x66, 0xDD, 0xCF, 0x6F, 0xA1, 0x94, 0x74, 0x63}}, // ACC_07 + {0x187, {0x7F, 0xED, 0x17, 0xC2, 0x7C, 0xEB, 0x44, 0x21, 0x01, 0xFA, 0xDB, 0x15, 0x4A, 0x6B, 0x23, 0x05}}, // Motor_EV_01 + {0x30C, {0x0F}}, // ACC_02 + {0x30F, {0x0C}}, // SWA_01 + {0x324, {0x27}}, // ACC_04 + {0x3C0, {0xC3}}, // Klemmen_Status_01 + {0x65D, {0xAC, 0xB3, 0xAB, 0xEB, 0x7A, 0xE1, 0x3B, 0xF7, 0x73, 0xBA, 0x7C, 0x9E, 0x06, 0x5F, 0x02, 0xD9}}, // ESP_20 }; unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, const std::vector &d) { From 326b088d7c8f44c9a7b157ea34a3c80b622fc29f Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 15:39:27 -0400 Subject: [PATCH 05/14] whitespace --- opendbc/can/common.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index 4fbde8a221..33e9acc52d 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -114,7 +114,7 @@ struct CrcInitializer { static CrcInitializer crcInitializer; -static const std::unordered_map> crc_mqb_constants{ +static const std::unordered_map> crc_mqb_constants { {0x86, {0x86}}, // LWI_01 {0x9F, {0xF5}}, // LH_EPS_01 {0xAD, {0x3F, 0x69, 0x39, 0xDC, 0x94, 0xF9, 0x14, 0x64, 0xD8, 0x6A, 0x34, 0xCE, 0xA2, 0x55, 0xB5, 0x2C}}, // Getriebe_11 From b215a21b2a0f668f1ec57bc0a6105d228c93f75e Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 15:54:20 -0400 Subject: [PATCH 06/14] cleanup --- opendbc/can/common.cc | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index 33e9acc52d..e831107e1d 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -136,30 +136,29 @@ static const std::unordered_map> crc_mqb_const }; unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, 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. + // AUTOSAR E2E Profile 2, CRC-8H2F with a "data ID" appended to the payload, which varies by message and by counter + // https://www.autosar.org/fileadmin/standards/R20-11/FO/AUTOSAR_PRS_E2EProtocol.pdf // https://www.autosar.org/fileadmin/user_upload/standards/classic/4-3/AUTOSAR_SWS_CRCLibrary.pdf - uint8_t crc = 0xFF; // Standard init value for CRC8 8H2F/AUTOSAR + uint8_t crc = 0xFF; // CRC-8H2F initial value - // CRC the payload first, skipping over the first byte where the CRC lives. + // CRC over payload first for (int i = 1; i < d.size(); i++) { crc ^= d[i]; crc = crc8_lut_8h2f[crc]; } - // Look up and apply the magic final CRC padding byte, which permutes by CAN - // address, and additionally (for SOME addresses) by the message counter. + // Continue CRC over "data ID" uint8_t counter = d[1] & 0x0F; auto crc_const = crc_mqb_constants.find(address); if (crc_const != crc_mqb_constants.end()) { crc ^= crc_const->second[counter]; - } else { // As-yet undefined CAN message, CRC check expected to fail + crc = crc8_lut_8h2f[crc]; + } else { printf("Attempt to CRC check undefined Volkswagen message 0x%02X\n", address); } - crc = crc8_lut_8h2f[crc]; - return crc ^ 0xFF; // Return after standard final XOR for CRC8 8H2F/AUTOSAR + return crc ^ 0xFF; // CRC-8H2F final XOR } unsigned int xor_checksum(uint32_t address, const Signal &sig, const std::vector &d) { From 2df63eb81263e4f88ce5ec38f92183bd152fdd73 Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 15:56:06 -0400 Subject: [PATCH 07/14] more comma indent convention --- opendbc/can/common.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index e831107e1d..9fd3c051ec 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -152,10 +152,10 @@ unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, const uint8_t counter = d[1] & 0x0F; auto crc_const = crc_mqb_constants.find(address); if (crc_const != crc_mqb_constants.end()) { - crc ^= crc_const->second[counter]; - crc = crc8_lut_8h2f[crc]; + crc ^= crc_const->second[counter]; + crc = crc8_lut_8h2f[crc]; } else { - printf("Attempt to CRC check undefined Volkswagen message 0x%02X\n", address); + printf("Attempt to CRC check undefined Volkswagen message 0x%02X\n", address); } return crc ^ 0xFF; // CRC-8H2F final XOR From b9fd90993150f3ee86a77f72159723f6d371648f Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 15:56:26 -0400 Subject: [PATCH 08/14] Revert "temporary, repeat packing step 1M times" This reverts commit 06d8f46710612c10600fbda2583ef0e963539b53. --- opendbc/can/tests/test_checksums.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/opendbc/can/tests/test_checksums.py b/opendbc/can/tests/test_checksums.py index 8dc576b6ea..52ac79398b 100644 --- a/opendbc/can/tests/test_checksums.py +++ b/opendbc/can/tests/test_checksums.py @@ -2,7 +2,6 @@ from opendbc.can.parser import CANParser from opendbc.can.packer import CANPacker -REPEAT_COUNT = 1000000 class TestCanChecksums: @@ -49,9 +48,7 @@ def verify_vw_mqb_crc(self, subtests, msg_name: str, msg_addr: int, test_message modified = copy.deepcopy(expected) modified.pop(crc_field, None) - modified_msg = None - for _ in range(REPEAT_COUNT): - modified_msg = packer.make_can_msg(msg_name, 0, modified) + modified_msg = packer.make_can_msg(msg_name, 0, modified) parser.update_strings([0, [modified_msg]]) tested = parser.vl[msg_name] From a957ee4b2b110e805562527785a86367d4bb89a5 Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 15:58:38 -0400 Subject: [PATCH 09/14] comment touch-up --- opendbc/can/common.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index 9fd3c051ec..a1ddac864c 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -142,13 +142,13 @@ unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, const uint8_t crc = 0xFF; // CRC-8H2F initial value - // CRC over payload first + // CRC over payload first, skipping the first byte where the CRC lives for (int i = 1; i < d.size(); i++) { crc ^= d[i]; crc = crc8_lut_8h2f[crc]; } - // Continue CRC over "data ID" + // Continue CRC over the "data ID" uint8_t counter = d[1] & 0x0F; auto crc_const = crc_mqb_constants.find(address); if (crc_const != crc_mqb_constants.end()) { From 561bbd71956ec09403d9c2c5aaebb55b14d7489f Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 15:58:54 -0400 Subject: [PATCH 10/14] beautify spacing --- opendbc/can/common.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index a1ddac864c..f587a23d1e 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -115,10 +115,10 @@ struct CrcInitializer { static CrcInitializer crcInitializer; static const std::unordered_map> crc_mqb_constants { - {0x86, {0x86}}, // LWI_01 - {0x9F, {0xF5}}, // LH_EPS_01 - {0xAD, {0x3F, 0x69, 0x39, 0xDC, 0x94, 0xF9, 0x14, 0x64, 0xD8, 0x6A, 0x34, 0xCE, 0xA2, 0x55, 0xB5, 0x2C}}, // Getriebe_11 - {0xFD, {0xB4, 0xEF, 0xF8, 0x49, 0x1E, 0xE5, 0xC2, 0xC0, 0x97, 0x19, 0x3C, 0xC9, 0xF1, 0x98, 0xD6, 0x61}}, // ESP_21 + {0x86, {0x86}}, // LWI_01 + {0x9F, {0xF5}}, // LH_EPS_01 + {0xAD, {0x3F, 0x69, 0x39, 0xDC, 0x94, 0xF9, 0x14, 0x64, 0xD8, 0x6A, 0x34, 0xCE, 0xA2, 0x55, 0xB5, 0x2C}}, // Getriebe_11 + {0xFD, {0xB4, 0xEF, 0xF8, 0x49, 0x1E, 0xE5, 0xC2, 0xC0, 0x97, 0x19, 0x3C, 0xC9, 0xF1, 0x98, 0xD6, 0x61}}, // ESP_21 {0x106, {0x07}}, // ESP_05 {0x117, {0x16}}, // ACC_10 {0x120, {0xC4, 0xE2, 0x4F, 0xE4, 0xF8, 0x2F, 0x56, 0x81, 0x9F, 0xE5, 0x83, 0x44, 0x05, 0x3F, 0x97, 0xDF}}, // TSK_06 From 00e855267cafaf765422657730346a0058061f28 Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 16:08:05 -0400 Subject: [PATCH 11/14] codespell says you're gonna have to Google it --- opendbc/can/common.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index f587a23d1e..5311a2a402 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -136,9 +136,7 @@ static const std::unordered_map> crc_mqb_const }; unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, const std::vector &d) { - // AUTOSAR E2E Profile 2, CRC-8H2F with a "data ID" appended to the payload, which varies by message and by counter - // https://www.autosar.org/fileadmin/standards/R20-11/FO/AUTOSAR_PRS_E2EProtocol.pdf - // https://www.autosar.org/fileadmin/user_upload/standards/classic/4-3/AUTOSAR_SWS_CRCLibrary.pdf + // This is AUTOSAR E2E Profile 2, CRC-8H2F with a "data ID" (varying by message/counter) appended to the payload uint8_t crc = 0xFF; // CRC-8H2F initial value From 98b3385a77c8963a06e642c8f862a376b2ec8795 Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 16:38:49 -0400 Subject: [PATCH 12/14] fully initialize all data ID arrays --- opendbc/can/common.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index 5311a2a402..e1270fdec6 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -115,23 +115,23 @@ struct CrcInitializer { static CrcInitializer crcInitializer; static const std::unordered_map> crc_mqb_constants { - {0x86, {0x86}}, // LWI_01 - {0x9F, {0xF5}}, // LH_EPS_01 + {0x86, {0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86}}, // LWI_01 + {0x9F, {0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5}}, // LH_EPS_01 {0xAD, {0x3F, 0x69, 0x39, 0xDC, 0x94, 0xF9, 0x14, 0x64, 0xD8, 0x6A, 0x34, 0xCE, 0xA2, 0x55, 0xB5, 0x2C}}, // Getriebe_11 {0xFD, {0xB4, 0xEF, 0xF8, 0x49, 0x1E, 0xE5, 0xC2, 0xC0, 0x97, 0x19, 0x3C, 0xC9, 0xF1, 0x98, 0xD6, 0x61}}, // ESP_21 - {0x106, {0x07}}, // ESP_05 - {0x117, {0x16}}, // ACC_10 + {0x106, {0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07}}, // ESP_05 + {0x117, {0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16}}, // ACC_10 {0x120, {0xC4, 0xE2, 0x4F, 0xE4, 0xF8, 0x2F, 0x56, 0x81, 0x9F, 0xE5, 0x83, 0x44, 0x05, 0x3F, 0x97, 0xDF}}, // TSK_06 {0x121, {0xE9, 0x65, 0xAE, 0x6B, 0x7B, 0x35, 0xE5, 0x5F, 0x4E, 0xC7, 0x86, 0xA2, 0xBB, 0xDD, 0xEB, 0xB4}}, // Motor_20 {0x122, {0x37, 0x7D, 0xF3, 0xA9, 0x18, 0x46, 0x6D, 0x4D, 0x3D, 0x71, 0x92, 0x9C, 0xE5, 0x32, 0x10, 0xB9}}, // ACC_06 - {0x126, {0xDA}}, // HCA_01 + {0x126, {0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA, 0xDA}}, // HCA_01 {0x12B, {0x6A, 0x38, 0xB4, 0x27, 0x22, 0xEF, 0xE1, 0xBB, 0xF8, 0x80, 0x84, 0x49, 0xC7, 0x9E, 0x1E, 0x2B}}, // GRA_ACC_01 {0x12E, {0xF8, 0xE5, 0x97, 0xC9, 0xD6, 0x07, 0x47, 0x21, 0x66, 0xDD, 0xCF, 0x6F, 0xA1, 0x94, 0x74, 0x63}}, // ACC_07 {0x187, {0x7F, 0xED, 0x17, 0xC2, 0x7C, 0xEB, 0x44, 0x21, 0x01, 0xFA, 0xDB, 0x15, 0x4A, 0x6B, 0x23, 0x05}}, // Motor_EV_01 - {0x30C, {0x0F}}, // ACC_02 - {0x30F, {0x0C}}, // SWA_01 - {0x324, {0x27}}, // ACC_04 - {0x3C0, {0xC3}}, // Klemmen_Status_01 + {0x30C, {0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F, 0x0F}}, // ACC_02 + {0x30F, {0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C}}, // SWA_01 + {0x324, {0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27}}, // ACC_04 + {0x3C0, {0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3, 0xC3}}, // Klemmen_Status_01 {0x65D, {0xAC, 0xB3, 0xAB, 0xEB, 0x7A, 0xE1, 0x3B, 0xF7, 0x73, 0xBA, 0x7C, 0x9E, 0x06, 0x5F, 0x02, 0xD9}}, // ESP_20 }; From 844f05155d9a2376cc5eb4985a85841339de858e Mon Sep 17 00:00:00 2001 From: Jason Young Date: Thu, 12 Sep 2024 17:02:35 -0400 Subject: [PATCH 13/14] clarify array name as part of VW support --- opendbc/can/common.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index e1270fdec6..ca4d58f3b1 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -114,7 +114,7 @@ struct CrcInitializer { static CrcInitializer crcInitializer; -static const std::unordered_map> crc_mqb_constants { +static const std::unordered_map> volkswagen_mqb_crc_constants { {0x86, {0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86}}, // LWI_01 {0x9F, {0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5}}, // LH_EPS_01 {0xAD, {0x3F, 0x69, 0x39, 0xDC, 0x94, 0xF9, 0x14, 0x64, 0xD8, 0x6A, 0x34, 0xCE, 0xA2, 0x55, 0xB5, 0x2C}}, // Getriebe_11 @@ -148,8 +148,8 @@ unsigned int volkswagen_mqb_checksum(uint32_t address, const Signal &sig, const // Continue CRC over the "data ID" uint8_t counter = d[1] & 0x0F; - auto crc_const = crc_mqb_constants.find(address); - if (crc_const != crc_mqb_constants.end()) { + auto crc_const = volkswagen_mqb_crc_constants.find(address); + if (crc_const != volkswagen_mqb_crc_constants.end()) { crc ^= crc_const->second[counter]; crc = crc8_lut_8h2f[crc]; } else { From 34d2a3a3716fbed22b92e0262892b496c4bed98e Mon Sep 17 00:00:00 2001 From: Jason Young Date: Fri, 13 Sep 2024 10:23:51 -0400 Subject: [PATCH 14/14] fix comment typo --- opendbc/can/common.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opendbc/can/common.cc b/opendbc/can/common.cc index ca4d58f3b1..ca5e7351e2 100644 --- a/opendbc/can/common.cc +++ b/opendbc/can/common.cc @@ -116,7 +116,7 @@ static CrcInitializer crcInitializer; static const std::unordered_map> volkswagen_mqb_crc_constants { {0x86, {0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86, 0x86}}, // LWI_01 - {0x9F, {0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5}}, // LH_EPS_01 + {0x9F, {0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5, 0xF5}}, // LH_EPS_03 {0xAD, {0x3F, 0x69, 0x39, 0xDC, 0x94, 0xF9, 0x14, 0x64, 0xD8, 0x6A, 0x34, 0xCE, 0xA2, 0x55, 0xB5, 0x2C}}, // Getriebe_11 {0xFD, {0xB4, 0xEF, 0xF8, 0x49, 0x1E, 0xE5, 0xC2, 0xC0, 0x97, 0x19, 0x3C, 0xC9, 0xF1, 0x98, 0xD6, 0x61}}, // ESP_21 {0x106, {0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07}}, // ESP_05