From 622ce923e901c634aab4c29be68638e38b0fcc16 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Wed, 12 Oct 2022 20:29:15 -0700 Subject: [PATCH] Hyundai: share flags with CAN-FD platform (#1099) * Hyundai: share flags with CAN-FD platform * update flags there --- board/safety/safety_hyundai.h | 23 ++++++----------------- board/safety/safety_hyundai_canfd.h | 16 +++++++--------- board/safety/safety_hyundai_common.h | 18 ++++++++++++++++++ python/__init__.py | 6 ++---- tests/safety/test_hyundai_canfd.py | 2 +- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/board/safety/safety_hyundai.h b/board/safety/safety_hyundai.h index d64345ac57..3fa1fdea5c 100644 --- a/board/safety/safety_hyundai.h +++ b/board/safety/safety_hyundai.h @@ -84,14 +84,9 @@ AddrCheckStruct hyundai_legacy_addr_checks[] = { }; #define HYUNDAI_LEGACY_ADDR_CHECK_LEN (sizeof(hyundai_legacy_addr_checks) / sizeof(hyundai_legacy_addr_checks[0])) -const int HYUNDAI_PARAM_EV_GAS = 1; -const int HYUNDAI_PARAM_HYBRID_GAS = 2; -const int HYUNDAI_PARAM_LONGITUDINAL = 4; const int HYUNDAI_PARAM_CAMERA_SCC = 8; bool hyundai_legacy = false; -bool hyundai_ev_gas_signal = false; -bool hyundai_hybrid_gas_signal = false; bool hyundai_camera_scc = false; addr_checks hyundai_rx_checks = {hyundai_addr_checks, HYUNDAI_ADDR_CHECK_LEN}; @@ -331,18 +326,13 @@ static int hyundai_fwd_hook(int bus_num, CANPacket_t *to_fwd) { } static const addr_checks* hyundai_init(uint16_t param) { + hyundai_common_init(param); hyundai_legacy = false; - hyundai_ev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_EV_GAS); - hyundai_hybrid_gas_signal = !hyundai_ev_gas_signal && GET_FLAG(param, HYUNDAI_PARAM_HYBRID_GAS); hyundai_camera_scc = GET_FLAG(param, HYUNDAI_PARAM_CAMERA_SCC); - hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES; -#ifdef ALLOW_DEBUG - // TODO: add longitudinal support for camera-based SCC platform - hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_LONGITUDINAL) && !hyundai_camera_scc; -#else - hyundai_longitudinal = false; -#endif + if (hyundai_camera_scc) { + hyundai_longitudinal = false; + } if (hyundai_longitudinal) { hyundai_rx_checks = (addr_checks){hyundai_long_addr_checks, HYUNDAI_LONG_ADDR_CHECK_LEN}; @@ -355,12 +345,11 @@ static const addr_checks* hyundai_init(uint16_t param) { } static const addr_checks* hyundai_legacy_init(uint16_t param) { + hyundai_common_init(param); hyundai_legacy = true; hyundai_longitudinal = false; hyundai_camera_scc = false; - hyundai_ev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_EV_GAS); - hyundai_hybrid_gas_signal = !hyundai_ev_gas_signal && GET_FLAG(param, HYUNDAI_PARAM_HYBRID_GAS); - hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES; + hyundai_rx_checks = (addr_checks){hyundai_legacy_addr_checks, HYUNDAI_LEGACY_ADDR_CHECK_LEN}; return &hyundai_rx_checks; } diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 8e5d70fad8..ba209b5367 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -63,9 +63,8 @@ addr_checks hyundai_canfd_rx_checks = {hyundai_canfd_addr_checks, HYUNDAI_CANFD_ uint16_t hyundai_canfd_crc_lut[256]; -const int HYUNDAI_PARAM_CANFD_HDA2 = 1; -const int HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 2; -const int HYUNDAI_PARAM_CANFD_LONG = 4; +const int HYUNDAI_PARAM_CANFD_HDA2 = 8; +const int HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 16; bool hyundai_canfd_hda2 = false; bool hyundai_canfd_alt_buttons = false; @@ -287,16 +286,15 @@ static int hyundai_canfd_fwd_hook(int bus_num, CANPacket_t *to_fwd) { } static const addr_checks* hyundai_canfd_init(uint16_t param) { + hyundai_common_init(param); + gen_crc_lookup_table_16(0x1021, hyundai_canfd_crc_lut); - hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES; hyundai_canfd_hda2 = GET_FLAG(param, HYUNDAI_PARAM_CANFD_HDA2); hyundai_canfd_alt_buttons = GET_FLAG(param, HYUNDAI_PARAM_CANFD_ALT_BUTTONS); -#ifdef ALLOW_DEBUG - hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LONG) && hyundai_canfd_hda2; -#else - hyundai_longitudinal = false; -#endif + if (!hyundai_canfd_hda2) { + hyundai_longitudinal = false; + } return &hyundai_canfd_rx_checks; } diff --git a/board/safety/safety_hyundai_common.h b/board/safety/safety_hyundai_common.h index 256c5df8ee..e56f6f465b 100644 --- a/board/safety/safety_hyundai_common.h +++ b/board/safety/safety_hyundai_common.h @@ -1,6 +1,10 @@ #ifndef SAFETY_HYUNDAI_COMMON_H #define SAFETY_HYUNDAI_COMMON_H +const int HYUNDAI_PARAM_EV_GAS = 1; +const int HYUNDAI_PARAM_HYBRID_GAS = 2; +const int HYUNDAI_PARAM_LONGITUDINAL = 4; + const uint8_t HYUNDAI_PREV_BUTTON_SAMPLES = 8; // roughly 160 ms const uint32_t HYUNDAI_STANDSTILL_THRSLD = 30; // ~1kph @@ -12,9 +16,23 @@ enum { }; // common state +bool hyundai_ev_gas_signal = false; +bool hyundai_hybrid_gas_signal = false; bool hyundai_longitudinal = false; uint8_t hyundai_last_button_interaction; // button messages since the user pressed an enable button +void hyundai_common_init(uint16_t param) { + hyundai_ev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_EV_GAS); + hyundai_hybrid_gas_signal = !hyundai_ev_gas_signal && GET_FLAG(param, HYUNDAI_PARAM_HYBRID_GAS); + + hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES; + +#ifdef ALLOW_DEBUG + hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_LONGITUDINAL); +#else + hyundai_longitudinal = false; +#endif +} void hyundai_common_cruise_state_check(const int cruise_engaged) { // some newer HKG models can re-enable after spamming cancel button, diff --git a/python/__init__.py b/python/__init__.py index 2df84b975d..0024efdbaf 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -213,10 +213,8 @@ class Panda: FLAG_HYUNDAI_HYBRID_GAS = 2 FLAG_HYUNDAI_LONG = 4 FLAG_HYUNDAI_CAMERA_SCC = 8 - - FLAG_HYUNDAI_CANFD_HDA2 = 1 - FLAG_HYUNDAI_CANFD_ALT_BUTTONS = 2 - FLAG_HYUNDAI_CANFD_LONG = 4 + FLAG_HYUNDAI_CANFD_HDA2 = 8 + FLAG_HYUNDAI_CANFD_ALT_BUTTONS = 16 FLAG_TESLA_POWERTRAIN = 1 FLAG_TESLA_LONG_CONTROL = 2 diff --git a/tests/safety/test_hyundai_canfd.py b/tests/safety/test_hyundai_canfd.py index d153bc717f..fa03af429f 100755 --- a/tests/safety/test_hyundai_canfd.py +++ b/tests/safety/test_hyundai_canfd.py @@ -145,7 +145,7 @@ class TestHyundaiCanfdHDA2Long(HyundaiLongitudinalBase, TestHyundaiCanfdHDA2): def setUp(self): self.packer = CANPackerPanda("hyundai_canfd") self.safety = libpandasafety_py.libpandasafety - self.safety.set_safety_hooks(Panda.SAFETY_HYUNDAI_CANFD, Panda.FLAG_HYUNDAI_CANFD_HDA2 | Panda.FLAG_HYUNDAI_CANFD_LONG) + self.safety.set_safety_hooks(Panda.SAFETY_HYUNDAI_CANFD, Panda.FLAG_HYUNDAI_CANFD_HDA2 | Panda.FLAG_HYUNDAI_LONG) self.safety.init_tests() def _accel_msg(self, accel, aeb_req=False, aeb_decel=0):