diff --git a/opendbc_repo/opendbc/car/volvo/cma_carcontroller.py b/opendbc_repo/opendbc/car/volvo/cma_carcontroller.py index 8f2d86f12d..86fc271cb5 100644 --- a/opendbc_repo/opendbc/car/volvo/cma_carcontroller.py +++ b/opendbc_repo/opendbc/car/volvo/cma_carcontroller.py @@ -79,9 +79,13 @@ class CarControllerCMA(CarControllerBase): # A current command cannot authorize steering against actual gear/source # state. B is decoded as drive by this family; no guessed transmission mode. + if self.lca_2_modulus == 15 and not CC.latActive and CS.pscm_status_supported: + CS.pscm_rearm_required = False + pscm_ready = (self.lca_2_modulus != 15 or + (CS.pscm_status_supported and not CS.pscm_rearm_required)) lat_active = bool(CC.latActive and CS.out.canValid and not CS.out.canTimeout and CS.out.gearShifter == structs.CarState.GearShifter.drive and - not CS.out.steerFaultTemporary and not CS.out.steerFaultPermanent) + not CS.out.steerFaultTemporary and not CS.out.steerFaultPermanent and pscm_ready) self.lat_active = lat_active if not lat_active: self.lca_5_primed = False diff --git a/opendbc_repo/opendbc/car/volvo/cma_carstate.py b/opendbc_repo/opendbc/car/volvo/cma_carstate.py index 49c72a5323..88c9aeb9f5 100644 --- a/opendbc_repo/opendbc/car/volvo/cma_carstate.py +++ b/opendbc_repo/opendbc/car/volvo/cma_carstate.py @@ -23,6 +23,8 @@ class CarStateCMA(CarStateBase): self.msg_lca_3 = {} self.msg_gear_position = {} self.pilot_assist_engaged = False + self.pscm_status_supported = False + self.pscm_rearm_required = False self.msg_lca_5 = {} # Formerly msg_speed_1 self.msg_speed = {} self.msg_speed_2 = {} diff --git a/opendbc_repo/opendbc/car/volvo/helpers.py b/opendbc_repo/opendbc/car/volvo/helpers.py index cf242d2c0b..a479456d26 100644 --- a/opendbc_repo/opendbc/car/volvo/helpers.py +++ b/opendbc_repo/opendbc/car/volvo/helpers.py @@ -159,6 +159,15 @@ def checksum_2_pscm_related_message(b2): return lut.get(b2, 0) +def pscm_status_validation_class(data): + # Keep pending stock status in the same validation class as the native cache. + checksum_valid = (data[0] == checksum_1_pscm_related_message(data[1], data[2]) and + data[3] == checksum_2_pscm_related_message(data[2])) + quality_valid = ((data[1] & 8) == 0 and 0x80 <= data[2] <= 0x8E and + data[1] >> 4 == data[2] & 15) + return data[2] >> 4, checksum_valid, quality_valid + + class LCA3CounterSync: """ Best-effort pattern synchronization for LCA_3 COUNTER_1. diff --git a/opendbc_repo/opendbc/car/volvo/interface.py b/opendbc_repo/opendbc/car/volvo/interface.py index 16d0eaec44..d10ce86e28 100644 --- a/opendbc_repo/opendbc/car/volvo/interface.py +++ b/opendbc_repo/opendbc/car/volvo/interface.py @@ -4,6 +4,7 @@ from opendbc.car import structs, get_safety_config from opendbc.car.interfaces import CarInterfaceBase from opendbc.car.volvo.carstate import CarState from opendbc.car.volvo.carcontroller import CarController +from opendbc.car.volvo.helpers import pscm_status_validation_class from opendbc.car.volvo.values import CAR, VolvoC1PlatformConfig, VolvoSPAPlatformConfig, VolvoSafetyFlags @@ -19,6 +20,7 @@ class CarInterface(CarInterfaceBase): self.CarController = CarControllerCMA super().__init__(CP) self.stock_relay = deque(maxlen=128) + self.pscm_status_class = None def update(self, can_packets): state = super().update(can_packets) @@ -27,6 +29,14 @@ class CarInterface(CarInterfaceBase): for address, data, bus in frames: if ((bus == 0 and address in (0x58, 0x57, 0x69, 0x90, 0x67, 0x97, 0x92)) or (bus == 2 and address in (0x16, 0x17))) and len(data) == 8: + if bus == 2 and address == 0x17: + status_class = pscm_status_validation_class(data) + if status_class != self.pscm_status_class: + self.stock_relay = deque((frame for frame in self.stock_relay if frame[0] != 0x17), maxlen=128) + self.pscm_status_class = status_class + self.CS.pscm_status_supported = status_class == (8, True, True) + if not self.CS.pscm_status_supported: + self.CS.pscm_rearm_required = True self.stock_relay.append((address, data, 2 if bus == 0 else 0)) return state diff --git a/opendbc_repo/opendbc/car/volvo/tests/test_cma_pscm_withdrawal.py b/opendbc_repo/opendbc/car/volvo/tests/test_cma_pscm_withdrawal.py new file mode 100644 index 0000000000..f8e7d69973 --- /dev/null +++ b/opendbc_repo/opendbc/car/volvo/tests/test_cma_pscm_withdrawal.py @@ -0,0 +1,88 @@ +"""Unsupported rack status withdraws host commands without starving stock transport.""" +import pytest +from opendbc.car import gen_empty_fingerprint, structs +from opendbc.car.volvo.helpers import (checksum_1_pscm_related_message, checksum_2_pscm_related_message, + pscm_status_validation_class) +from opendbc.car.volvo.interface import CarInterface +from opendbc.car.volvo.tests.test_cma_primary_angle import _sources +from opendbc.car.volvo.values import CAR + + +def status(b1, b2): + return bytes([checksum_1_pscm_related_message(b1, b2), b1, b2, checksum_2_pscm_related_message(b2), 0, 0, 0xFF, 0xFF]) + + +def interface(): + cp = CarInterface.get_params(CAR.POLESTAR_2, gen_empty_fingerprint(), [], False, False, False) + ci = CarInterface(cp) + for frame in range(40): + now = 1_000_000_000 + frame * 10_000_000 + ci.update([(now, _sources('POLESTAR_2', frame=frame))]) + ci.apply(structs.CarControl.new_message().as_reader(), now + 1000) + assert ci.CS.out.canValid and ci.CS.pscm_status_supported + return ci + + +def step(ci, frame, active, rack_status=None): + incoming = _sources('POLESTAR_2', frame=frame) + if rack_status is not None: + incoming = [(a, rack_status if a == 0x17 and b == 2 else d, b) for a, d, b in incoming] + now = 1_000_000_000 + frame * 10_000_000 + ci.update([(now, incoming)]) + _, messages = ci.apply(structs.CarControl.new_message(latActive=active).as_reader(), now + 1000) + return incoming, messages + + +def forwarded(frames): + return [(a, d, 2 if b == 0 else 0) for a, d, b in frames + if (b == 0 and a in (0x58, 0x57, 0x67, 0x69, 0x90, 0x97, 0x92)) or (b == 2 and a in (0x16, 0x17))] + + +GOOD = status(0x11, 0x81) +OFFSET = status(0x21, 0x81) + + +@pytest.mark.parametrize('unsupported', [ + OFFSET, status(0x18, 0x81), status(0xF0, 0x8F), status(0, 0xA0), + bytes([GOOD[0] ^ 1]) + GOOD[1:], GOOD[:3] + bytes([GOOD[3] ^ 1]) + GOOD[4:], +]) +def test_unsupported_status_uses_stock_until_request_cycles(unsupported): + ci = interface() + step(ci, 40, True) + assert ci.CC.lat_active + incoming, messages = step(ci, 41, True, unsupported) + assert not ci.CC.lat_active and ci.CS.pscm_rearm_required + assert messages == forwarded(incoming) + incoming, messages = step(ci, 42, True, GOOD) + assert ci.CS.pscm_status_supported and ci.CS.pscm_rearm_required and not ci.CC.lat_active + assert messages == forwarded(incoming) + step(ci, 43, False, GOOD) + assert not ci.CS.pscm_rearm_required + step(ci, 44, True, GOOD) + assert ci.CC.lat_active + + +@pytest.mark.parametrize('recover_in_batch', [False, True]) +def test_mixed_batch_cannot_hide_bad_status_or_replay_an_earlier_class(recover_in_batch): + ci = interface() + step(ci, 40, True) + received = [(0x17, GOOD, 2), (0x17, OFFSET, 2)] + if recover_in_batch: + received.append((0x17, GOOD, 2)) + ci.update([(1_410_000_000, received)]) + assert ci.CS.pscm_rearm_required + _, messages = ci.apply(structs.CarControl.new_message(latActive=True).as_reader(), 1_410_001_000) + assert not ci.CC.lat_active + expected = GOOD if recover_in_batch else OFFSET + assert [m for m in messages if m[0] == 0x17] == [(0x17, expected, 0)] + + +def test_missing_status_cannot_grant_host_lateral(): + cp = CarInterface.get_params(CAR.POLESTAR_2, gen_empty_fingerprint(), [], False, False, False) + ci = CarInterface(cp) + ci.update([(1_000_000_000, [m for m in _sources('POLESTAR_2') if m[0] != 0x17])]) + ci.CS.out = structs.CarState.new_message(canValid=True, gearShifter=structs.CarState.GearShifter.drive).as_reader() + ci.apply(structs.CarControl.new_message(latActive=True).as_reader(), 1_000_001_000) + assert not ci.CC.lat_active + assert pscm_status_validation_class(GOOD) == (8, True, True) + assert pscm_status_validation_class(OFFSET) == (8, True, False) diff --git a/opendbc_repo/opendbc/car/volvo/tests/test_cma_stock_transport.py b/opendbc_repo/opendbc/car/volvo/tests/test_cma_stock_transport.py index 33835a77ae..94c7b1d805 100644 --- a/opendbc_repo/opendbc/car/volvo/tests/test_cma_stock_transport.py +++ b/opendbc_repo/opendbc/car/volvo/tests/test_cma_stock_transport.py @@ -71,8 +71,14 @@ def test_effective_withdrawal_preserves_only_received_stock(changes): def test_stock_received_while_active_cannot_leak_into_later_withdrawal(): + from opendbc.car.volvo.helpers import checksum_1_pscm_related_message, checksum_2_pscm_related_message ci = interface() - ci.update([(1_000_000_000, packets())]) + status = CANPacker('volvo_mid_1').make_can_msg('PSCM_RELATED', 2, { + 'NEW_SIGNAL_2': 8, 'CHECKSUM_1': checksum_1_pscm_related_message(0, 0x80), + 'CHECKSUM_2': checksum_2_pscm_related_message(0x80), + }) + ci.update([(1_000_000_000, packets() + [status])]) + assert ci.CS.pscm_status_supported ci.CS.out = structs.CarState.new_message(canValid=True, gearShifter=structs.CarState.GearShifter.drive).as_reader() ci.apply(structs.CarControl.new_message(latActive=True).as_reader(), 1_010_000_000) assert ci.CC.lat_active diff --git a/prebuilt.json b/prebuilt.json index f8b9150f0d..68911092e7 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "28d6f4e4bd21a15b72d71bfc3d51420476879934d087647699ca0334794224ea", "schema_version": 1} +{"protected_inventory_sha256": "3c4cce7ebcd73232d20221144072952aeb2ccd2e80e816781e78bfba1b8b73e9", "schema_version": 1}