diff --git a/opendbc/car/volvo/helpers.py b/opendbc/car/volvo/helpers.py index ccdb7655d9c..067fa3e6bcc 100644 --- a/opendbc/car/volvo/helpers.py +++ b/opendbc/car/volvo/helpers.py @@ -136,27 +136,24 @@ def checksum_2_pscm_related_message(b2): """ Computes checksum #2 (goes in byte[3]) for PSCM-related 0x17 message. Depends only on byte[2]. - """ - lut = { - 0x80: 0xBF, - 0x81: 0xF3, - 0x82: 0x27, - 0x83: 0x6B, - 0x84: 0x92, - 0x85: 0xDE, - 0x86: 0x0A, - 0x87: 0x46, - 0x88: 0xE5, - 0x89: 0xA9, - 0x8A: 0x7D, - 0x8B: 0x31, - 0x8C: 0xC8, - 0x8D: 0x84, - 0x8E: 0x50, - } - - return lut.get(b2, 0) + Each set bit of byte[2] flips a fixed pattern of checksum bits, and the + checksum is all of those flips combined (linear over GF(2), same as + checksum_1). Solved from the 30 byte[2] values seen on the bus: 0x80..0x8E + in normal driving and 0xA0..0xAE, which the rack sends after it resets + itself (fault state) until the car is power cycled. + + This replaces a lookup table that covered only 0x80..0x8E and returned 0x00 + otherwise, so every 0x17 frame forwarded during a rack fault had an invalid + checksum. Bits 4 and 6 of byte[2] have never been seen set, so they are + assumed to flip nothing. + """ + FLIPS = {7: 0xBF, 5: 0x3A, 3: 0x5A, 2: 0x2D, 1: 0x98, 0: 0x4C} + c = 0 + for bit, flip in FLIPS.items(): + if (b2 >> bit) & 1: + c ^= flip + return c def checksum_lca_4_message(*args) -> int: diff --git a/opendbc/car/volvo/tests/test_checksums.py b/opendbc/car/volvo/tests/test_checksums.py index df09a46f4b8..83024785cb0 100644 --- a/opendbc/car/volvo/tests/test_checksums.py +++ b/opendbc/car/volvo/tests/test_checksums.py @@ -32,6 +32,14 @@ 0x8A: 0x7D, 0x8B: 0x31, 0x8C: 0xC8, 0x8D: 0x84, 0x8E: 0x50, } +# b2 -> byte[3] while the rack is in its fault state. After the rack resets itself +# mid-drive, it sets byte[2]'s high nibble to 0xA and holds it until power cycle. +PSCM_RELATED_CHECKSUM_2_FAULT = { + 0xA0: 0x85, 0xA1: 0xC9, 0xA2: 0x1D, 0xA3: 0x51, 0xA4: 0xA8, + 0xA5: 0xE4, 0xA6: 0x30, 0xA7: 0x7C, 0xA8: 0xDF, 0xA9: 0x93, + 0xAA: 0x47, 0xAB: 0x0B, 0xAC: 0xF2, 0xAD: 0xBE, 0xAE: 0x6A, +} + class TestPscmRelatedChecksums: def test_checksum_1_matches_the_car(self): @@ -49,3 +57,9 @@ def test_checksum_1_covers_esc_echo(self): def test_checksum_2_matches_the_car(self): for b2, expected in PSCM_RELATED_CHECKSUM_2.items(): assert checksum_2_pscm_related_message(b2) == expected, f"b2=0x{b2:02X}" + + def test_checksum_2_covers_rack_fault_state(self): + # regression: the lookup table only covered byte[2] = 0x80..0x8E and returned 0x00 + # for the fault state, so the car got an invalid checksum while the rack was faulted + for b2, expected in PSCM_RELATED_CHECKSUM_2_FAULT.items(): + assert checksum_2_pscm_related_message(b2) == expected, f"b2=0x{b2:02X}"