diff --git a/opendbc_repo/opendbc/safety/tests/common.py b/opendbc_repo/opendbc/safety/tests/common.py index d94bdf26c..8197ce872 100644 --- a/opendbc_repo/opendbc/safety/tests/common.py +++ b/opendbc_repo/opendbc/safety/tests/common.py @@ -12,6 +12,7 @@ from opendbc.safety.tests.libsafety import libsafety_py from opendbc.car.lateral import MAX_LATERAL_ACCEL, MAX_LATERAL_JERK from opendbc.safety.tests.mads_common import MadsSafetyTestBase +from opendbc.safety.tests.rx_checks_common import RxChecksSafetyTestBase MAX_WRONG_COUNTERS = 5 MAX_SAMPLE_VALS = 6 @@ -1040,7 +1041,7 @@ class SafetyTest(SafetyTestBase): @add_regen_tests -class CarSafetyTest(SafetyTest, MadsSafetyTestBase): +class CarSafetyTest(SafetyTest, MadsSafetyTestBase, RxChecksSafetyTestBase): STANDSTILL_THRESHOLD: float = 0.0 GAS_PRESSED_THRESHOLD = 0 RELAY_MALFUNCTION_ADDRS: dict[int, tuple[int, ...]] | None = None diff --git a/opendbc_repo/opendbc/safety/tests/libsafety/safety.c b/opendbc_repo/opendbc/safety/tests/libsafety/safety.c index 834eca3c8..429324415 100644 --- a/opendbc_repo/opendbc/safety/tests/libsafety/safety.c +++ b/opendbc_repo/opendbc/safety/tests/libsafety/safety.c @@ -46,6 +46,46 @@ void tick_mads_state(bool vm, bool acc_main, bool op_allowed, bool braking, bool mads_state_update(vm, acc_main, op_allowed, braking, steering_disengage); } +void set_unseen_rx_checks_valid(void){ + for (int i = 0; i < current_safety_config.rx_checks_len; i++) { + RxStatus *status = ¤t_safety_config.rx_checks[i].status; + if (!status->msg_seen) { + status->msg_seen = true; + status->index = 0; + status->valid_checksum = true; + status->wrong_counters = 0; + status->valid_quality_flag = true; + status->last_timestamp = microsecond_timer_get(); + status->lagging = false; + } + } +} + +int get_ignore_frequency_check_count(void){ + int count = 0; + for (int i = 0; i < current_safety_config.rx_checks_len; i++) { + for (uint8_t j = 0U; (j < MAX_ADDR_CHECK_MSGS) && (current_safety_config.rx_checks[i].msg[j].addr != 0); j++) { + if (current_safety_config.rx_checks[i].msg[j].ignore_frequency_check) { + count++; + } + } + } + return count; +} + +bool get_rx_check_ignore_frequency(int addr, int bus){ + bool ignore = false; + for (int i = 0; i < current_safety_config.rx_checks_len; i++) { + for (uint8_t j = 0U; (j < MAX_ADDR_CHECK_MSGS) && (current_safety_config.rx_checks[i].msg[j].addr != 0); j++) { + const CanMsgCheck *msg = ¤t_safety_config.rx_checks[i].msg[j]; + if ((msg->addr == addr) && ((int)msg->bus == bus) && msg->ignore_frequency_check) { + ignore = true; + } + } + } + return ignore; +} + void set_relay_malfunction(bool c){ relay_malfunction = c; } diff --git a/opendbc_repo/opendbc/safety/tests/rx_checks_common.py b/opendbc_repo/opendbc/safety/tests/rx_checks_common.py new file mode 100644 index 000000000..86db0a21a --- /dev/null +++ b/opendbc_repo/opendbc/safety/tests/rx_checks_common.py @@ -0,0 +1,29 @@ +""" +Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors. + +This file is part of sunnypilot and is licensed under the MIT License. +See the LICENSE.md file in the root directory for more details. +""" +import unittest + +from opendbc.safety.tests.libsafety import libsafety_py + + +class RxChecksSafetyTestBase(unittest.TestCase): + safety: libsafety_py.LibSafety + + def _ignore_frequency_check_msgs(self): + return [] + + def test_ignore_frequency_check(self): + msgs = self._ignore_frequency_check_msgs() + self.assertEqual(len(msgs), self.safety.get_ignore_frequency_check_count()) + + for msg in msgs: + self.assertTrue(self._rx(msg)) + self.assertTrue(self.safety.get_rx_check_ignore_frequency(msg[0].addr, msg[0].bus)) + + self.safety.set_unseen_rx_checks_valid() + self.safety.set_controls_allowed(True) + self.safety.safety_tick() + self.assertTrue(self.safety.get_controls_allowed()) diff --git a/opendbc_repo/opendbc/safety/tests/test_chrysler.py b/opendbc_repo/opendbc/safety/tests/test_chrysler.py index 20bcf7ba7..4a9c4761b 100755 --- a/opendbc_repo/opendbc/safety/tests/test_chrysler.py +++ b/opendbc_repo/opendbc/safety/tests/test_chrysler.py @@ -89,6 +89,11 @@ class TestChryslerSafety(common.CarSafetyTest, common.MotorTorqueSteeringSafetyT def _lkas_button_msg(self, enabled): values = {"TOGGLE_LKAS": enabled} return self.packer.make_can_msg_safety("TRACTION_BUTTON", 0, values) + + def _ignore_frequency_check_msgs(self): + return [self._lkas_button_msg(False)] + + class TestChryslerRamDTSafety(TestChryslerSafety): DBC = "chrysler_ram_dt_generated" diff --git a/opendbc_repo/opendbc/safety/tests/test_tesla.py b/opendbc_repo/opendbc/safety/tests/test_tesla.py index c18c4ca28..0a0bbb939 100755 --- a/opendbc_repo/opendbc/safety/tests/test_tesla.py +++ b/opendbc_repo/opendbc/safety/tests/test_tesla.py @@ -489,6 +489,9 @@ class TestTeslaVehicleBusSafety(TestTeslaSafetyBase): values = {"UI_activeTouchPoints": 3 if enabled else 0} return self.packer_adas.make_can_msg_safety("UI_status2", CANBUS.vehicle, values) + def _ignore_frequency_check_msgs(self): + return [self._lkas_button_msg(False)] + def _set_mads_screen_button_config(self, finger_flag): param_sp = TeslaSafetyFlagsSP.HAS_VEHICLE_BUS if finger_flag is not None: diff --git a/opendbc_repo/opendbc/safety/tests/test_toyota.py b/opendbc_repo/opendbc/safety/tests/test_toyota.py index 04a5dcb99..fce27ea57 100755 --- a/opendbc_repo/opendbc/safety/tests/test_toyota.py +++ b/opendbc_repo/opendbc/safety/tests/test_toyota.py @@ -37,6 +37,10 @@ class TestToyotaSafetyBase(common.CarSafetyTest, common.LongitudinalAccelSafetyT SAFETY_PARAM_SP: int = 0 + def setUp(self): + libsafety_py.libsafety.set_current_safety_param_sp(self.SAFETY_PARAM_SP) + super().setUp() + packer: CANPackerSafety safety: libsafety_py.LibSafety @@ -95,6 +99,9 @@ class TestToyotaSafetyBase(common.CarSafetyTest, common.LongitudinalAccelSafetyT values = {"MAIN_ON": enabled} return self.packer.make_can_msg_safety(msg, 0, values) + def _ignore_frequency_check_msgs(self): + return [self._acc_state_msg(False)] if self.SAFETY_PARAM_SP & ToyotaSafetyFlagsSP.UNSUPPORTED_DSU else [] + def test_diagnostics(self, stock_longitudinal: bool = False, ecu_disabled: bool = True): for should_tx, msg in ((False, b"\x6D\x02\x3E\x00\x00\x00\x00\x00"), # fwdCamera tester present (False, b"\x0F\x03\xAA\xAA\x00\x00\x00\x00"), # non-tester present