From d0442fd1e45589880c12b27c8a744cd1f540f540 Mon Sep 17 00:00:00 2001 From: Adeeb <8762862+adeebshihadeh@users.noreply.github.com> Date: Fri, 12 Jun 2020 18:03:31 -0700 Subject: [PATCH] add hyundai legacy safety mode (#554) --- board/safety.h | 2 ++ board/safety/safety_hyundai.h | 61 +++++++++++++++++++++++++++++------ python/__init__.py | 1 + tests/safety/test_hyundai.py | 8 +++++ 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/board/safety.h b/board/safety.h index f67c38ad..e426779c 100644 --- a/board/safety.h +++ b/board/safety.h @@ -37,6 +37,7 @@ #define SAFETY_HONDA_BOSCH_HARNESS 20U #define SAFETY_VOLKSWAGEN_PQ 21U #define SAFETY_SUBARU_LEGACY 22U +#define SAFETY_HYUNDAI_LEGACY 23U uint16_t current_safety_mode = SAFETY_SILENT; const safety_hooks *current_hooks = &nooutput_hooks; @@ -227,6 +228,7 @@ const safety_hook_config safety_hook_registry[] = { {SAFETY_VOLKSWAGEN_MQB, &volkswagen_mqb_hooks}, {SAFETY_NISSAN, &nissan_hooks}, {SAFETY_NOOUTPUT, &nooutput_hooks}, + {SAFETY_HYUNDAI_LEGACY, &hyundai_legacy_hooks}, #ifdef ALLOW_DEBUG {SAFETY_MAZDA, &mazda_hooks}, {SAFETY_SUBARU_LEGACY, &subaru_legacy_hooks}, diff --git a/board/safety/safety_hyundai.h b/board/safety/safety_hyundai.h index 278b7250..55792af1 100644 --- a/board/safety/safety_hyundai.h +++ b/board/safety/safety_hyundai.h @@ -19,18 +19,25 @@ const CanMsg HYUNDAI_TX_MSGS[] = { // TODO: missing checksum for wheel speeds message,worst failure case is // wheel speeds stuck at 0 and we don't disengage on brake press -// TODO: refactor addr check to cleanly re-enable commented out checks for cars that have them AddrCheckStruct hyundai_rx_checks[] = { {.msg = {{608, 0, 8, .check_checksum = true, .max_counter = 3U, .expected_timestep = 10000U}}}, - // TODO: older hyundai models don't populate the counter bits in 902 - //{.msg = {{902, 0, 8, .max_counter = 15U, .expected_timestep = 10000U}}}, - {.msg = {{902, 0, 8, .max_counter = 0U, .expected_timestep = 10000U}}}, - //{.msg = {{916, 0, 8, .check_checksum = true, .max_counter = 7U, .expected_timestep = 10000U}}}, - {.msg = {{916, 0, 8, .check_checksum = false, .max_counter = 0U, .expected_timestep = 10000U}}}, + {.msg = {{902, 0, 8, .check_checksum = false, .max_counter = 15U, .expected_timestep = 10000U}}}, + {.msg = {{916, 0, 8, .check_checksum = true, .max_counter = 7U, .expected_timestep = 10000U}}}, {.msg = {{1057, 0, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}}}, }; const int HYUNDAI_RX_CHECK_LEN = sizeof(hyundai_rx_checks) / sizeof(hyundai_rx_checks[0]); +// older hyundai models have less checks due to missing counters and checksums +AddrCheckStruct hyundai_legacy_rx_checks[] = { + {.msg = {{608, 0, 8, .check_checksum = true, .max_counter = 3U, .expected_timestep = 10000U}}}, + {.msg = {{902, 0, 8, .check_checksum = false, .max_counter = 0U, .expected_timestep = 10000U}}}, + {.msg = {{916, 0, 8, .check_checksum = false, .max_counter = 0U, .expected_timestep = 10000U}}}, + {.msg = {{1057, 0, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 20000U}}}, +}; +const int HYUNDAI_LEGACY_RX_CHECK_LEN = sizeof(hyundai_legacy_rx_checks) / sizeof(hyundai_legacy_rx_checks[0]); + +bool hyundai_legacy = false; + static uint8_t hyundai_get_counter(CAN_FIFOMailBox_TypeDef *to_push) { int addr = GET_ADDR(to_push); @@ -82,9 +89,17 @@ static uint8_t hyundai_compute_checksum(CAN_FIFOMailBox_TypeDef *to_push) { static int hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { - bool valid = addr_safety_check(to_push, hyundai_rx_checks, HYUNDAI_RX_CHECK_LEN, - hyundai_get_checksum, hyundai_compute_checksum, - hyundai_get_counter); + bool valid; + if (hyundai_legacy) { + valid = addr_safety_check(to_push, hyundai_legacy_rx_checks, HYUNDAI_LEGACY_RX_CHECK_LEN, + hyundai_get_checksum, hyundai_compute_checksum, + hyundai_get_counter); + + } else { + valid = addr_safety_check(to_push, hyundai_rx_checks, HYUNDAI_RX_CHECK_LEN, + hyundai_get_checksum, hyundai_compute_checksum, + hyundai_get_counter); + } bool unsafe_allow_gas = unsafe_mode & UNSAFE_DISABLE_DISENGAGE_ON_GAS; @@ -234,8 +249,24 @@ static int hyundai_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { return bus_fwd; } +static void hyundai_init(int16_t param) { + UNUSED(param); + controls_allowed = false; + relay_malfunction_reset(); + + hyundai_legacy = false; +} + +static void hyundai_legacy_init(int16_t param) { + UNUSED(param); + controls_allowed = false; + relay_malfunction_reset(); + + hyundai_legacy = true; +} + const safety_hooks hyundai_hooks = { - .init = nooutput_init, + .init = hyundai_init, .rx = hyundai_rx_hook, .tx = hyundai_tx_hook, .tx_lin = nooutput_tx_lin_hook, @@ -243,3 +274,13 @@ const safety_hooks hyundai_hooks = { .addr_check = hyundai_rx_checks, .addr_check_len = sizeof(hyundai_rx_checks) / sizeof(hyundai_rx_checks[0]), }; + +const safety_hooks hyundai_legacy_hooks = { + .init = hyundai_legacy_init, + .rx = hyundai_rx_hook, + .tx = hyundai_tx_hook, + .tx_lin = nooutput_tx_lin_hook, + .fwd = hyundai_fwd_hook, + .addr_check = hyundai_legacy_rx_checks, + .addr_check_len = sizeof(hyundai_legacy_rx_checks) / sizeof(hyundai_legacy_rx_checks[0]), +}; diff --git a/python/__init__.py b/python/__init__.py index 3bceabc2..dc1c892b 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -129,6 +129,7 @@ class Panda(object): SAFETY_HONDA_BOSCH_HARNESS = 20 SAFETY_VOLKSWAGEN_PQ = 21 SAFETY_SUBARU_LEGACY = 22 + SAFETY_HYUNDAI_LEGACY = 23 SERIAL_DEBUG = 0 SERIAL_ESP = 1 diff --git a/tests/safety/test_hyundai.py b/tests/safety/test_hyundai.py index beb8b214..f61bdd40 100644 --- a/tests/safety/test_hyundai.py +++ b/tests/safety/test_hyundai.py @@ -191,5 +191,13 @@ class TestHyundaiSafety(common.PandaSafetyTest): self.assertTrue(self._tx(self._button_msg(RESUME_BTN))) +class TestHyundaiLegacySafety(TestHyundaiSafety): + def setUp(self): + self.packer = CANPackerPanda("hyundai_kia_generic") + self.safety = libpandasafety_py.libpandasafety + self.safety.set_safety_hooks(Panda.SAFETY_HYUNDAI_LEGACY, 0) + self.safety.init_tests() + + if __name__ == "__main__": unittest.main()