diff --git a/openpilot/starpilot/conditional_mode/manual.py b/openpilot/starpilot/conditional_mode/manual.py index 957ebb1553..772c0748ae 100644 --- a/openpilot/starpilot/conditional_mode/manual.py +++ b/openpilot/starpilot/conditional_mode/manual.py @@ -28,6 +28,9 @@ MEDIA_LONG_NS = CRUISE_LONG_PRESS * 10_000_000 # Convert 100 Hz ticks to source MEDIA_VERY_LONG_NS = VERY_LONG_PRESS * 10_000_000 ButtonType = car.CarState.ButtonEvent.Type +_EV6_MEDIA_REQUIRED = int(HyundaiFlags.CANFD | HyundaiFlags.EV) +_EV6_MEDIA_EXCLUDED = int(HyundaiFlags.CCNC | HyundaiFlags.CANFD_ANGLE_STEERING) + def ioniq6_media_eligible(CP: car.CarParams) -> bool: """Legacy name shared by the Ioniq 6 and restored first-generation EV6 path. @@ -37,9 +40,8 @@ def ioniq6_media_eligible(CP: car.CarParams) -> bool: """ if CP.carFingerprint == HYUNDAI_CAR.HYUNDAI_IONIQ_6: return config_index(CP) is not None and CanBus(CP).ECAN in (1, 5) - required = HyundaiFlags.CANFD | HyundaiFlags.EV return (CP.brand == 'hyundai' and CP.carFingerprint == HYUNDAI_CAR.KIA_EV6 and - CP.flags & required == required and not CP.flags & (HyundaiFlags.CCNC | HyundaiFlags.CANFD_ANGLE_STEERING) and + CP.flags & _EV6_MEDIA_REQUIRED == _EV6_MEDIA_REQUIRED and not CP.flags & _EV6_MEDIA_EXCLUDED and config_index(CP) is not None and CanBus(CP).ECAN in (0, 1, 4, 5)) diff --git a/openpilot/starpilot/conditional_mode/tests/test_media_integer_masks.py b/openpilot/starpilot/conditional_mode/tests/test_media_integer_masks.py new file mode 100644 index 0000000000..73c1620dac --- /dev/null +++ b/openpilot/starpilot/conditional_mode/tests/test_media_integer_masks.py @@ -0,0 +1,53 @@ +from types import SimpleNamespace +from unittest.mock import patch + +import pytest + +from opendbc.car import gen_empty_fingerprint +from opendbc.car.hyundai.interface import CarInterface +from opendbc.car.hyundai.values import CAR, HyundaiFlags +from openpilot.starpilot.conditional_mode.manual import ioniq6_media_eligible + + +def params(car=CAR.KIA_EV6, hda2=True, offset=0, alpha=False): + fp = gen_empty_fingerprint() + bus = offset + int(hda2) + fp[bus].update({0x1CF: 8, 0x35: 32, 0x130: 16, 0x175: 24, 0xA0: 24, 0xEA: 24}) + if hda2: + fp[offset + 2][0x50] = 16 + return CarInterface.get_params(car, fp, [], alpha, False, False) + + +@pytest.mark.parametrize('car', (CAR.KIA_EV6, CAR.HYUNDAI_IONIQ_6)) +@pytest.mark.parametrize('hda2', (False, True)) +@pytest.mark.parametrize('offset', (0, 4)) +@pytest.mark.parametrize('alpha', (False, True)) +def test_live_factory_topologies_do_not_rebuild_flag_masks(car, hda2, offset, alpha): + cp = params(car, hda2, offset, alpha) + expected = car == CAR.KIA_EV6 or hda2 + with patch.object(HyundaiFlags, '__or__', side_effect=AssertionError('live mask allocation')): + for _ in range(20): + assert ioniq6_media_eligible(cp) is expected + + +@pytest.mark.parametrize('change', ('brand', 'car', 'canfd', 'ev', 'angle', 'ccnc', 'owner', 'second_owner')) +def test_mutated_profile_is_rechecked_on_every_call(change): + cp = params() + assert ioniq6_media_eligible(cp) + if change == 'brand': + cp.brand = 'other' + elif change == 'car': + cp.carFingerprint = CAR.KIA_NIRO_EV_2ND_GEN + elif change in ('canfd', 'ev'): + cp.flags &= ~int(HyundaiFlags.CANFD if change == 'canfd' else HyundaiFlags.EV) + elif change in ('angle', 'ccnc'): + cp.flags |= int(HyundaiFlags.CANFD_ANGLE_STEERING if change == 'angle' else HyundaiFlags.CCNC) + elif change == 'owner': + cp.safetyConfigs = [] + else: + cp.safetyConfigs = [cp.safetyConfigs[0].to_dict()] * 2 + assert ioniq6_media_eligible(cp) is False + + +def test_unrelated_car_preserves_short_circuit_without_reading_flags(): + assert ioniq6_media_eligible(SimpleNamespace(brand='other', carFingerprint='unrelated')) is False diff --git a/prebuilt.json b/prebuilt.json index 58b282d95d..8c11fd4719 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "91daddf550f6f4f4375a736632563e5fba766c5de6098c9974e0789c4158a08b", "schema_version": 1} +{"protected_inventory_sha256": "2c20a124096fb2dafc5707f59fccdbf24846dbd5972a8236b87fd6c71b1bf1c9", "schema_version": 1}