From b7f7f62fe4d99b39f8083f0c73404f04cc04ea24 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 04:55:29 -0700 Subject: [PATCH 01/20] draft --- board/safety/safety_hyundai_canfd.h | 149 ++++++++++++++++++---------- 1 file changed, 96 insertions(+), 53 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 3ea7a486a5..bb57a71ae9 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -53,66 +53,101 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { {0x1E0, 0, 16}, // LFAHDA_CLUSTER }; +#define HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ + /* ACCELERATOR, ACCELERATOR_BRAKE_ALT, ACCELERATOR_ALT */ \ + {.msg = {{0x35, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, \ + {0x100, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, \ + {0x105, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, \ + {.msg = {{0x175, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ + {.msg = {{0xa0, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ + {.msg = {{0xea, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ + /* CRUISE_BUTTONS, CRUISE_BUTTONS_ALT */ \ + {.msg = {{0x1cf, (pt_bus), 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, \ + {0x1aa, (pt_bus), 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, \ + +#define HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \ + /* SCC_CONTROL (from ADAS unit or camera) */ \ + {.msg = {{0x1a0, (scc_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ + AddrCheckStruct hyundai_canfd_addr_checks[] = { - {.msg = {{0x35, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0x35, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0x105, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, - {.msg = {{0x175, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, - {0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, - {.msg = {{0xa0, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, - {.msg = {{0xea, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, - {.msg = {{0x1a0, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, - {0x1a0, 2, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, - {.msg = {{0x1cf, 1, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, - {0x1cf, 0, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, - {0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}}}, + // Camera sends SCC messages on HDA2 + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) + HYUNDAI_CANFD_SCC_ADDR_CHECK(2) +// {.msg = {{0x35, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0x35, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0x105, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, +// {.msg = {{0x175, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, +// {0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, +// {.msg = {{0xa0, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, +// {.msg = {{0xea, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, +// {.msg = {{0x1a0, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, +// {0x1a0, 2, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, +// {.msg = {{0x1cf, 1, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, +// {0x1cf, 0, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, +// {0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}}}, }; #define HYUNDAI_CANFD_ADDR_CHECK_LEN (sizeof(hyundai_canfd_addr_checks) / sizeof(hyundai_canfd_addr_checks[0])) -// 0x1a0 is on bus 0 +AddrCheckStruct hyundai_canfd_hda2_addr_checks[] = { + // E-CAN is on bus 1, ADAS unit sends SCC messages on HDA2 + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(1) + HYUNDAI_CANFD_SCC_ADDR_CHECK(1) +}; +#define HYUNDAI_CANFD_HDA2_ADDR_CHECK_LEN (sizeof(hyundai_canfd_hda2_addr_checks) / sizeof(hyundai_canfd_hda2_addr_checks[0])) + AddrCheckStruct hyundai_canfd_radar_scc_addr_checks[] = { - {.msg = {{0x35, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0x35, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0x105, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, - {.msg = {{0x175, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, - {0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, - {.msg = {{0xa0, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, - {.msg = {{0xea, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, - {.msg = {{0x1a0, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, - {.msg = {{0x1cf, 1, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, - {0x1cf, 0, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, - {0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}}}, + // Non-HDA2, radar sends SCC messages on these cars + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) + HYUNDAI_CANFD_SCC_ADDR_CHECK(0) +// {.msg = {{0x35, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0x35, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0x105, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, +// {.msg = {{0x175, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, +// {0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, +// {.msg = {{0xa0, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, +// {.msg = {{0xea, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, +// {.msg = {{0x1a0, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, +// {.msg = {{0x1cf, 1, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, +// {0x1cf, 0, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, +// {0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}}}, }; #define HYUNDAI_CANFD_RADAR_SCC_ADDR_CHECK_LEN (sizeof(hyundai_canfd_radar_scc_addr_checks) / sizeof(hyundai_canfd_radar_scc_addr_checks[0])) AddrCheckStruct hyundai_canfd_long_addr_checks[] = { - {.msg = {{0x35, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0x35, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0x105, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, - {.msg = {{0x175, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, - {0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, - {.msg = {{0xa0, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, - {.msg = {{0xea, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, - {0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, - {.msg = {{0x1cf, 1, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, - {0x1cf, 0, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, - {0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}}}, + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) +// {.msg = {{0x35, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0x35, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0x105, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, +// {.msg = {{0x175, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, +// {0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, +// {.msg = {{0xa0, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, +// {.msg = {{0xea, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, +// {0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, +// {.msg = {{0x1cf, 1, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, +// {0x1cf, 0, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, +// {0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}}}, }; #define HYUNDAI_CANFD_LONG_ADDR_CHECK_LEN (sizeof(hyundai_canfd_long_addr_checks) / sizeof(hyundai_canfd_long_addr_checks[0])) -AddrCheckStruct hyundai_canfd_ice_addr_checks[] = { - {.msg = {{0x100, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, - {.msg = {{0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, - {.msg = {{0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, - {.msg = {{0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, - {.msg = {{0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, +AddrCheckStruct hyundai_canfd_hda2_long_addr_checks[] = { + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(1) }; -#define HYUNDAI_CANFD_ICE_ADDR_CHECK_LEN (sizeof(hyundai_canfd_ice_addr_checks) / sizeof(hyundai_canfd_ice_addr_checks[0])) +#define HYUNDAI_CANFD_HDA2_LONG_ADDR_CHECK_LEN (sizeof(hyundai_canfd_hda2_long_addr_checks) / sizeof(hyundai_canfd_hda2_long_addr_checks[0])) + +//AddrCheckStruct hyundai_canfd_ice_addr_checks[] = { +// {.msg = {{0x100, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, +// {.msg = {{0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, +// {.msg = {{0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, +// {.msg = {{0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, +// {.msg = {{0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, +//// {.msg = {{0x1a0, 2, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, +//}; +//#define HYUNDAI_CANFD_ICE_ADDR_CHECK_LEN (sizeof(hyundai_canfd_ice_addr_checks) / sizeof(hyundai_canfd_ice_addr_checks[0])) addr_checks hyundai_canfd_rx_checks = {hyundai_canfd_addr_checks, HYUNDAI_CANFD_ADDR_CHECK_LEN}; @@ -253,7 +288,7 @@ static int hyundai_canfd_rx_hook(CANPacket_t *to_push) { } generic_rx_checks(stock_ecu_detected); - return valid; + return true; } static int hyundai_canfd_tx_hook(CANPacket_t *to_send) { @@ -369,14 +404,22 @@ static const addr_checks* hyundai_canfd_init(uint16_t param) { } if (hyundai_longitudinal) { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_long_addr_checks, HYUNDAI_CANFD_LONG_ADDR_CHECK_LEN}; + if (hyundai_canfd_hda2) { + hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_long_addr_checks, HYUNDAI_CANFD_HDA2_LONG_ADDR_CHECK_LEN}; + } else { + hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_long_addr_checks, HYUNDAI_CANFD_LONG_ADDR_CHECK_LEN}; + } } else { - if (!hyundai_ev_gas_signal && !hyundai_hybrid_gas_signal) { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_ice_addr_checks, HYUNDAI_CANFD_ICE_ADDR_CHECK_LEN}; - } else if (!hyundai_camera_scc && !hyundai_canfd_hda2) { +// if (!hyundai_ev_gas_signal && !hyundai_hybrid_gas_signal) { +// hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_ice_addr_checks, HYUNDAI_CANFD_ICE_ADDR_CHECK_LEN}; + if (!hyundai_camera_scc && !hyundai_canfd_hda2) { hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_radar_scc_addr_checks, HYUNDAI_CANFD_RADAR_SCC_ADDR_CHECK_LEN}; } else { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_addr_checks, HYUNDAI_CANFD_ADDR_CHECK_LEN}; + if (hyundai_canfd_hda2) { + hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_addr_checks, HYUNDAI_CANFD_HDA2_ADDR_CHECK_LEN}; + } else { + hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_addr_checks, HYUNDAI_CANFD_ADDR_CHECK_LEN}; + } } } From 4257abf7e7a54e0461d3fd6ed77e39db4f09e492 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 04:57:40 -0700 Subject: [PATCH 02/20] now clean up old comments --- board/safety/safety_hyundai_canfd.h | 49 ----------------------------- 1 file changed, 49 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index bb57a71ae9..ae03dbc35c 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -73,20 +73,6 @@ AddrCheckStruct hyundai_canfd_addr_checks[] = { // Camera sends SCC messages on HDA2 HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) HYUNDAI_CANFD_SCC_ADDR_CHECK(2) -// {.msg = {{0x35, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0x35, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0x105, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, -// {.msg = {{0x175, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, -// {0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, -// {.msg = {{0xa0, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, -// {.msg = {{0xea, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, -// {.msg = {{0x1a0, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, -// {0x1a0, 2, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, -// {.msg = {{0x1cf, 1, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, -// {0x1cf, 0, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, -// {0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}}}, }; #define HYUNDAI_CANFD_ADDR_CHECK_LEN (sizeof(hyundai_canfd_addr_checks) / sizeof(hyundai_canfd_addr_checks[0])) @@ -101,36 +87,11 @@ AddrCheckStruct hyundai_canfd_radar_scc_addr_checks[] = { // Non-HDA2, radar sends SCC messages on these cars HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) HYUNDAI_CANFD_SCC_ADDR_CHECK(0) -// {.msg = {{0x35, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0x35, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0x105, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, -// {.msg = {{0x175, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, -// {0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, -// {.msg = {{0xa0, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, -// {.msg = {{0xea, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, -// {.msg = {{0x1a0, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, -// {.msg = {{0x1cf, 1, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, -// {0x1cf, 0, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, -// {0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}}}, }; #define HYUNDAI_CANFD_RADAR_SCC_ADDR_CHECK_LEN (sizeof(hyundai_canfd_radar_scc_addr_checks) / sizeof(hyundai_canfd_radar_scc_addr_checks[0])) AddrCheckStruct hyundai_canfd_long_addr_checks[] = { HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) -// {.msg = {{0x35, 1, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0x35, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0x105, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, -// {.msg = {{0x175, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, -// {0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, -// {.msg = {{0xa0, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, -// {.msg = {{0xea, 1, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, -// {0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }}}, -// {.msg = {{0x1cf, 1, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, -// {0x1cf, 0, 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, -// {0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}}}, }; #define HYUNDAI_CANFD_LONG_ADDR_CHECK_LEN (sizeof(hyundai_canfd_long_addr_checks) / sizeof(hyundai_canfd_long_addr_checks[0])) @@ -139,16 +100,6 @@ AddrCheckStruct hyundai_canfd_hda2_long_addr_checks[] = { }; #define HYUNDAI_CANFD_HDA2_LONG_ADDR_CHECK_LEN (sizeof(hyundai_canfd_hda2_long_addr_checks) / sizeof(hyundai_canfd_hda2_long_addr_checks[0])) -//AddrCheckStruct hyundai_canfd_ice_addr_checks[] = { -// {.msg = {{0x100, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, -// {.msg = {{0xa0, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, -// {.msg = {{0xea, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, -// {.msg = {{0x175, 0, 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, -// {.msg = {{0x1aa, 0, 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, -//// {.msg = {{0x1a0, 2, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, -//}; -//#define HYUNDAI_CANFD_ICE_ADDR_CHECK_LEN (sizeof(hyundai_canfd_ice_addr_checks) / sizeof(hyundai_canfd_ice_addr_checks[0])) - addr_checks hyundai_canfd_rx_checks = {hyundai_canfd_addr_checks, HYUNDAI_CANFD_ADDR_CHECK_LEN}; From f4a50cacb489d9ed927cb27ec4c06ccffba94b8e Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 04:59:21 -0700 Subject: [PATCH 03/20] no more special broken ice addr checks --- board/safety/safety_hyundai_canfd.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index ae03dbc35c..abc6b7b193 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -349,11 +349,6 @@ static const addr_checks* hyundai_canfd_init(uint16_t param) { hyundai_canfd_alt_buttons = GET_FLAG(param, HYUNDAI_PARAM_CANFD_ALT_BUTTONS); hyundai_canfd_hda2_alt_steering = GET_FLAG(param, HYUNDAI_PARAM_CANFD_HDA2_ALT_STEERING); - // no long for ICE yet - if (!hyundai_ev_gas_signal && !hyundai_hybrid_gas_signal) { - hyundai_longitudinal = false; - } - if (hyundai_longitudinal) { if (hyundai_canfd_hda2) { hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_long_addr_checks, HYUNDAI_CANFD_HDA2_LONG_ADDR_CHECK_LEN}; @@ -361,8 +356,6 @@ static const addr_checks* hyundai_canfd_init(uint16_t param) { hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_long_addr_checks, HYUNDAI_CANFD_LONG_ADDR_CHECK_LEN}; } } else { -// if (!hyundai_ev_gas_signal && !hyundai_hybrid_gas_signal) { -// hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_ice_addr_checks, HYUNDAI_CANFD_ICE_ADDR_CHECK_LEN}; if (!hyundai_camera_scc && !hyundai_canfd_hda2) { hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_radar_scc_addr_checks, HYUNDAI_CANFD_RADAR_SCC_ADDR_CHECK_LEN}; } else { From 2908e8a49f6db882029a7054aa20fba11c2b4326 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 04:59:49 -0700 Subject: [PATCH 04/20] revert --- board/safety/safety_hyundai_canfd.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index abc6b7b193..e12d5ce3c2 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -239,7 +239,7 @@ static int hyundai_canfd_rx_hook(CANPacket_t *to_push) { } generic_rx_checks(stock_ecu_detected); - return true; + return valid; } static int hyundai_canfd_tx_hook(CANPacket_t *to_send) { From 88039ff03a6c7de801f600748af8b47b20d12897 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 05:15:47 -0700 Subject: [PATCH 05/20] fix --- board/safety/safety_hyundai_canfd.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index e12d5ce3c2..213a6cbcfd 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -53,7 +53,7 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { {0x1E0, 0, 16}, // LFAHDA_CLUSTER }; -#define HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ +#define HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ /* ACCELERATOR, ACCELERATOR_BRAKE_ALT, ACCELERATOR_ALT */ \ {.msg = {{0x35, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, \ {0x100, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, \ From 76d2b0f066cc62eb5c67a22c1ad7f9b1fc5f3838 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 05:24:36 -0700 Subject: [PATCH 06/20] add test for ICE HDA1 --- tests/safety/test_hyundai_canfd.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/safety/test_hyundai_canfd.py b/tests/safety/test_hyundai_canfd.py index a9f349f079..9f61101314 100755 --- a/tests/safety/test_hyundai_canfd.py +++ b/tests/safety/test_hyundai_canfd.py @@ -259,5 +259,35 @@ def test_tester_present_allowed(self): pass +class TestHyundaiCanfdHDALongICE(HyundaiLongitudinalBase, TestHyundaiCanfdHDA1Base): + + FWD_BLACKLISTED_ADDRS = {2: [0x12a, 0x1e0, 0x1a0]} + + DISABLED_ECU_UDS_MSG = (0x730, 1) + DISABLED_ECU_ACTUATION_MSG = (0x1a0, 0) + + STEER_MSG = "LFA" + GAS_MSG = ("ACCELERATOR_BRAKE_ALT", "ACCELERATOR_PEDAL_PRESSED") + STEER_BUS = 0 + SCC_BUS = 2 + + def setUp(self): + self.packer = CANPackerPanda("hyundai_canfd") + self.safety = libpanda_py.libpanda + self.safety.set_safety_hooks(Panda.SAFETY_HYUNDAI_CANFD, Panda.FLAG_HYUNDAI_CAMERA_SCC | Panda.FLAG_HYUNDAI_LONG) + self.safety.init_tests() + + def _accel_msg(self, accel, aeb_req=False, aeb_decel=0): + values = { + "aReqRaw": accel, + "aReqValue": accel, + } + return self.packer.make_can_msg_panda("SCC_CONTROL", 0, values) + + # no knockout + def test_tester_present_allowed(self): + pass + + if __name__ == "__main__": unittest.main() From 97b0235e1fae4e050e01939b5f6f98e941fc030e Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 05:25:47 -0700 Subject: [PATCH 07/20] Revert "add test for ICE HDA1" This reverts commit 76d2b0f066cc62eb5c67a22c1ad7f9b1fc5f3838. --- tests/safety/test_hyundai_canfd.py | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/tests/safety/test_hyundai_canfd.py b/tests/safety/test_hyundai_canfd.py index 9f61101314..a9f349f079 100755 --- a/tests/safety/test_hyundai_canfd.py +++ b/tests/safety/test_hyundai_canfd.py @@ -259,35 +259,5 @@ def test_tester_present_allowed(self): pass -class TestHyundaiCanfdHDALongICE(HyundaiLongitudinalBase, TestHyundaiCanfdHDA1Base): - - FWD_BLACKLISTED_ADDRS = {2: [0x12a, 0x1e0, 0x1a0]} - - DISABLED_ECU_UDS_MSG = (0x730, 1) - DISABLED_ECU_ACTUATION_MSG = (0x1a0, 0) - - STEER_MSG = "LFA" - GAS_MSG = ("ACCELERATOR_BRAKE_ALT", "ACCELERATOR_PEDAL_PRESSED") - STEER_BUS = 0 - SCC_BUS = 2 - - def setUp(self): - self.packer = CANPackerPanda("hyundai_canfd") - self.safety = libpanda_py.libpanda - self.safety.set_safety_hooks(Panda.SAFETY_HYUNDAI_CANFD, Panda.FLAG_HYUNDAI_CAMERA_SCC | Panda.FLAG_HYUNDAI_LONG) - self.safety.init_tests() - - def _accel_msg(self, accel, aeb_req=False, aeb_decel=0): - values = { - "aReqRaw": accel, - "aReqValue": accel, - } - return self.packer.make_can_msg_panda("SCC_CONTROL", 0, values) - - # no knockout - def test_tester_present_allowed(self): - pass - - if __name__ == "__main__": unittest.main() From 36360334eadb56478cacc2ff7eb68a00234521b7 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 05:26:17 -0700 Subject: [PATCH 08/20] can be separate --- board/safety/safety_hyundai_canfd.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 213a6cbcfd..e4d708d1a7 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -349,6 +349,11 @@ static const addr_checks* hyundai_canfd_init(uint16_t param) { hyundai_canfd_alt_buttons = GET_FLAG(param, HYUNDAI_PARAM_CANFD_ALT_BUTTONS); hyundai_canfd_hda2_alt_steering = GET_FLAG(param, HYUNDAI_PARAM_CANFD_HDA2_ALT_STEERING); + // no long for ICE yet + if (!hyundai_ev_gas_signal && !hyundai_hybrid_gas_signal) { + hyundai_longitudinal = false; + } + if (hyundai_longitudinal) { if (hyundai_canfd_hda2) { hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_long_addr_checks, HYUNDAI_CANFD_HDA2_LONG_ADDR_CHECK_LEN}; From 0ad5b82256116daec27f7851b3aeda0947100a79 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 10:32:06 -0700 Subject: [PATCH 09/20] macro for addr check struct --- board/safety/safety_hyundai_canfd.h | 44 ++++++++++++----------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index e4d708d1a7..a91d8af6bb 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -69,38 +69,30 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { /* SCC_CONTROL (from ADAS unit or camera) */ \ {.msg = {{0x1a0, (scc_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ -AddrCheckStruct hyundai_canfd_addr_checks[] = { - // Camera sends SCC messages on HDA2 - HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) - HYUNDAI_CANFD_SCC_ADDR_CHECK(2) -}; -#define HYUNDAI_CANFD_ADDR_CHECK_LEN (sizeof(hyundai_canfd_addr_checks) / sizeof(hyundai_canfd_addr_checks[0])) +#define HYUNDAI_CANFD_CREATE_ADDR_CHECK(name, pt_bus, scc_bus) \ +AddrCheckStruct name[] = { \ + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ + HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \ +}; \ -AddrCheckStruct hyundai_canfd_hda2_addr_checks[] = { - // E-CAN is on bus 1, ADAS unit sends SCC messages on HDA2 - HYUNDAI_CANFD_COMMON_ADDR_CHECKS(1) - HYUNDAI_CANFD_SCC_ADDR_CHECK(1) -}; -#define HYUNDAI_CANFD_HDA2_ADDR_CHECK_LEN (sizeof(hyundai_canfd_hda2_addr_checks) / sizeof(hyundai_canfd_hda2_addr_checks[0])) +// Camera sends SCC messages on HDA2 +HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_addr_checks, 0, 2) -AddrCheckStruct hyundai_canfd_radar_scc_addr_checks[] = { - // Non-HDA2, radar sends SCC messages on these cars - HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) - HYUNDAI_CANFD_SCC_ADDR_CHECK(0) -}; -#define HYUNDAI_CANFD_RADAR_SCC_ADDR_CHECK_LEN (sizeof(hyundai_canfd_radar_scc_addr_checks) / sizeof(hyundai_canfd_radar_scc_addr_checks[0])) +// E-CAN is on bus 1, ADAS unit sends SCC messages on HDA2 +HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_hda2_addr_checks, 1, 1) + +// Non-HDA2, radar sends SCC messages on these cars instead of camera +HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_radar_scc_addr_checks, 0, 0) AddrCheckStruct hyundai_canfd_long_addr_checks[] = { HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) }; -#define HYUNDAI_CANFD_LONG_ADDR_CHECK_LEN (sizeof(hyundai_canfd_long_addr_checks) / sizeof(hyundai_canfd_long_addr_checks[0])) AddrCheckStruct hyundai_canfd_hda2_long_addr_checks[] = { HYUNDAI_CANFD_COMMON_ADDR_CHECKS(1) }; -#define HYUNDAI_CANFD_HDA2_LONG_ADDR_CHECK_LEN (sizeof(hyundai_canfd_hda2_long_addr_checks) / sizeof(hyundai_canfd_hda2_long_addr_checks[0])) -addr_checks hyundai_canfd_rx_checks = {hyundai_canfd_addr_checks, HYUNDAI_CANFD_ADDR_CHECK_LEN}; +addr_checks hyundai_canfd_rx_checks = {hyundai_canfd_addr_checks, ARRAY_LEN(hyundai_canfd_addr_checks)}; uint16_t hyundai_canfd_crc_lut[256]; @@ -356,18 +348,18 @@ static const addr_checks* hyundai_canfd_init(uint16_t param) { if (hyundai_longitudinal) { if (hyundai_canfd_hda2) { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_long_addr_checks, HYUNDAI_CANFD_HDA2_LONG_ADDR_CHECK_LEN}; + hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_long_addr_checks, ARRAY_LEN(hyundai_canfd_hda2_long_addr_checks)}; } else { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_long_addr_checks, HYUNDAI_CANFD_LONG_ADDR_CHECK_LEN}; + hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_long_addr_checks, ARRAY_LEN(hyundai_canfd_long_addr_checks)}; } } else { if (!hyundai_camera_scc && !hyundai_canfd_hda2) { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_radar_scc_addr_checks, HYUNDAI_CANFD_RADAR_SCC_ADDR_CHECK_LEN}; + hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_radar_scc_addr_checks, ARRAY_LEN(hyundai_canfd_radar_scc_addr_checks)}; } else { if (hyundai_canfd_hda2) { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_addr_checks, HYUNDAI_CANFD_HDA2_ADDR_CHECK_LEN}; + hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_addr_checks, ARRAY_LEN(hyundai_canfd_hda2_addr_checks)}; } else { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_addr_checks, HYUNDAI_CANFD_ADDR_CHECK_LEN}; + hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_addr_checks, ARRAY_LEN(hyundai_canfd_addr_checks)}; } } } From c235983f83c17e6e4994610b87e9acabb7ef2ac4 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 12:23:49 -0700 Subject: [PATCH 10/20] add checks for alt buttons (fixes race condition) --- board/safety/safety_hyundai_canfd.h | 79 ++++++++++++++++++----------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index a91d8af6bb..db48f6fb57 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -53,44 +53,61 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { {0x1E0, 0, 16}, // LFAHDA_CLUSTER }; + +// *** Addresses checked in rx hook *** #define HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ - /* ACCELERATOR, ACCELERATOR_BRAKE_ALT, ACCELERATOR_ALT */ \ + /* EV, ICE, HYBRID: ACCELERATOR, ACCELERATOR_BRAKE_ALT, ACCELERATOR_ALT */ \ {.msg = {{0x35, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, \ - {0x100, 0, 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, \ + {0x100, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, \ {0x105, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, \ {.msg = {{0x175, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ {.msg = {{0xa0, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ {.msg = {{0xea, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ - /* CRUISE_BUTTONS, CRUISE_BUTTONS_ALT */ \ - {.msg = {{0x1cf, (pt_bus), 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, \ - {0x1aa, (pt_bus), 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }}}, \ #define HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \ /* SCC_CONTROL (from ADAS unit or camera) */ \ {.msg = {{0x1a0, (scc_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ -#define HYUNDAI_CANFD_CREATE_ADDR_CHECK(name, pt_bus, scc_bus) \ -AddrCheckStruct name[] = { \ - HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ - HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \ -}; \ +#define HYUNDAI_CANFD_BUTTONS_ADDR_CHECK(pt_bus) \ + {.msg = {{0x1cf, (pt_bus), 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ -// Camera sends SCC messages on HDA2 -HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_addr_checks, 0, 2) +#define HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK(pt_bus) \ + {.msg = {{0x1aa, (pt_bus), 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ -// E-CAN is on bus 1, ADAS unit sends SCC messages on HDA2 -HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_hda2_addr_checks, 1, 1) -// Non-HDA2, radar sends SCC messages on these cars instead of camera -HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_radar_scc_addr_checks, 0, 0) +#define HYUNDAI_CANFD_CREATE_ADDR_CHECK(name, pt_bus, scc_bus, button_msg) \ +AddrCheckStruct name[] = { \ + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ + HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \ + button_msg(pt_bus) \ +}; \ -AddrCheckStruct hyundai_canfd_long_addr_checks[] = { - HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) -}; +#define HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(name, pt_bus, button_msg) \ +AddrCheckStruct name[] = { \ + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ + button_msg(pt_bus) \ +}; \ -AddrCheckStruct hyundai_canfd_hda2_long_addr_checks[] = { - HYUNDAI_CANFD_COMMON_ADDR_CHECKS(1) -}; + +// *** Non-HDA2 checks *** +// Camera sends SCC messages on HDA1 +HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_addr_checks, 0, 2, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) +HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_alt_buttons_addr_checks, 0, 2, HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK) + +// Longitudinal checks for HDA1 +HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(hyundai_canfd_long_addr_checks, 0, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) +HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(hyundai_canfd_long_alt_buttons_addr_checks, 0, HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK) + +// Radar sends SCC messages on these cars instead of camera +HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_radar_scc_addr_checks, 0, 0, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) +HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_radar_scc_alt_buttons_addr_checks, 0, 0, HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK) + + +// *** HDA2 checks *** +// E-CAN is on bus 1, ADAS unit sends SCC messages on HDA2. +// Does not use the alt buttons message +HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_hda2_addr_checks, 1, 1, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) +HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(hyundai_canfd_hda2_long_addr_checks, 1, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) addr_checks hyundai_canfd_rx_checks = {hyundai_canfd_addr_checks, ARRAY_LEN(hyundai_canfd_addr_checks)}; @@ -350,17 +367,21 @@ static const addr_checks* hyundai_canfd_init(uint16_t param) { if (hyundai_canfd_hda2) { hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_long_addr_checks, ARRAY_LEN(hyundai_canfd_hda2_long_addr_checks)}; } else { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_long_addr_checks, ARRAY_LEN(hyundai_canfd_long_addr_checks)}; + hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons + ? (addr_checks){hyundai_canfd_long_alt_buttons_addr_checks, ARRAY_LEN(hyundai_canfd_long_alt_buttons_addr_checks)} + : (addr_checks){hyundai_canfd_long_addr_checks, ARRAY_LEN(hyundai_canfd_long_addr_checks)}; } } else { if (!hyundai_camera_scc && !hyundai_canfd_hda2) { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_radar_scc_addr_checks, ARRAY_LEN(hyundai_canfd_radar_scc_addr_checks)}; + hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons + ? (addr_checks){hyundai_canfd_radar_scc_alt_buttons_addr_checks, ARRAY_LEN(hyundai_canfd_radar_scc_alt_buttons_addr_checks)} + : (addr_checks){hyundai_canfd_radar_scc_addr_checks, ARRAY_LEN(hyundai_canfd_radar_scc_addr_checks)}; + } else if (hyundai_canfd_hda2) { + hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_addr_checks, ARRAY_LEN(hyundai_canfd_hda2_addr_checks)}; } else { - if (hyundai_canfd_hda2) { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_addr_checks, ARRAY_LEN(hyundai_canfd_hda2_addr_checks)}; - } else { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_addr_checks, ARRAY_LEN(hyundai_canfd_addr_checks)}; - } + hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons + ? (addr_checks){hyundai_canfd_alt_buttons_addr_checks, ARRAY_LEN(hyundai_canfd_alt_buttons_addr_checks)} + : (addr_checks){hyundai_canfd_addr_checks, ARRAY_LEN(hyundai_canfd_addr_checks)}; } } From a69b4ee870cb8a82d04b15038aa0da4a904a9ad1 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 13:19:38 -0700 Subject: [PATCH 11/20] add macro for array len --- board/utils.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/board/utils.h b/board/utils.h index 3ab6b30bc8..ae3ecf7b71 100644 --- a/board/utils.h +++ b/board/utils.h @@ -22,6 +22,8 @@ (_a > 0) ? _a : (-_a); \ }) +#define ARRAY_LEN(name) (sizeof(name) / sizeof(name[0])) + #ifndef NULL #define NULL ((void*)0) #endif From 42d48df8b982ddb2fa5c27e4e67c5b0ace9e1996 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 13:23:05 -0700 Subject: [PATCH 12/20] add comment --- board/safety/safety_hyundai_canfd.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index db48f6fb57..beee74c9f5 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -90,7 +90,8 @@ AddrCheckStruct name[] = { \ // *** Non-HDA2 checks *** -// Camera sends SCC messages on HDA1 +// Camera sends SCC messages on HDA1. +// Both button messages exist on some platforms, so we ensure we track the correct one HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_addr_checks, 0, 2, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_alt_buttons_addr_checks, 0, 2, HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK) From a136d925d0d855a49ed0743e0f2034a9918e2039 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 13:34:15 -0700 Subject: [PATCH 13/20] misra --- board/safety/safety_hyundai_canfd.h | 14 +++++++------- board/utils.h | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index beee74c9f5..7e9c7dd637 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -76,16 +76,16 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { #define HYUNDAI_CANFD_CREATE_ADDR_CHECK(name, pt_bus, scc_bus, button_msg) \ -AddrCheckStruct name[] = { \ - HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ - HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \ - button_msg(pt_bus) \ +AddrCheckStruct (name)[] = { \ + HYUNDAI_CANFD_COMMON_ADDR_CHECKS((pt_bus)) \ + HYUNDAI_CANFD_SCC_ADDR_CHECK((scc_bus)) \ + (button_msg)((pt_bus)) \ }; \ #define HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(name, pt_bus, button_msg) \ -AddrCheckStruct name[] = { \ - HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ - button_msg(pt_bus) \ +AddrCheckStruct (name)[] = { \ + HYUNDAI_CANFD_COMMON_ADDR_CHECKS((pt_bus)) \ + (button_msg)((pt_bus)) \ }; \ diff --git a/board/utils.h b/board/utils.h index ae3ecf7b71..8de37373e0 100644 --- a/board/utils.h +++ b/board/utils.h @@ -22,7 +22,7 @@ (_a > 0) ? _a : (-_a); \ }) -#define ARRAY_LEN(name) (sizeof(name) / sizeof(name[0])) +#define ARRAY_LEN(name) (sizeof((name)) / sizeof((name)[0])) #ifndef NULL #define NULL ((void*)0) From a83c636b65e879597cddef040fec6abf7bd09201 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 13:39:46 -0700 Subject: [PATCH 14/20] comments to help separate --- board/safety/safety_hyundai_canfd.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 7e9c7dd637..10c759782c 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -75,6 +75,7 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { {.msg = {{0x1aa, (pt_bus), 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ +// *** Macros to create addr check structs *** #define HYUNDAI_CANFD_CREATE_ADDR_CHECK(name, pt_bus, scc_bus, button_msg) \ AddrCheckStruct (name)[] = { \ HYUNDAI_CANFD_COMMON_ADDR_CHECKS((pt_bus)) \ @@ -82,11 +83,12 @@ AddrCheckStruct (name)[] = { \ (button_msg)((pt_bus)) \ }; \ -#define HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(name, pt_bus, button_msg) \ -AddrCheckStruct (name)[] = { \ - HYUNDAI_CANFD_COMMON_ADDR_CHECKS((pt_bus)) \ - (button_msg)((pt_bus)) \ -}; \ +#define HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(name, pt_bus, button_msg) \ +AddrCheckStruct (name)[] = { \ + /* SCC not checked since its ECU is disabled on HDA2 and we use buttons */ \ + HYUNDAI_CANFD_COMMON_ADDR_CHECKS((pt_bus)) \ + (button_msg)((pt_bus)) \ +}; \ // *** Non-HDA2 checks *** From 6f21f85ddb55f0fc576774bf1c7a3e8ea2edd649 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 13:57:58 -0700 Subject: [PATCH 15/20] can't put parenthesis around array item --- board/safety/safety_hyundai_canfd.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 10c759782c..f922eda14d 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -80,14 +80,14 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { AddrCheckStruct (name)[] = { \ HYUNDAI_CANFD_COMMON_ADDR_CHECKS((pt_bus)) \ HYUNDAI_CANFD_SCC_ADDR_CHECK((scc_bus)) \ - (button_msg)((pt_bus)) \ + button_msg((pt_bus)) /* cppcheck-suppress misra-c2012-20.7 */ \ }; \ #define HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(name, pt_bus, button_msg) \ AddrCheckStruct (name)[] = { \ /* SCC not checked since its ECU is disabled on HDA2 and we use buttons */ \ HYUNDAI_CANFD_COMMON_ADDR_CHECKS((pt_bus)) \ - (button_msg)((pt_bus)) \ + button_msg((pt_bus)) /* cppcheck-suppress misra-c2012-20.7 */ \ }; \ From b80c9e563fc085bdcdbd5116eb921acedd8c3615 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 17:45:48 -0700 Subject: [PATCH 16/20] review suggestions --- board/safety/safety_hyundai_canfd.h | 22 +++++++++++----------- board/safety_declarations.h | 2 ++ board/utils.h | 2 -- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index f922eda14d..9d116e1983 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -55,8 +55,8 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { // *** Addresses checked in rx hook *** +// EV, ICE, HYBRID: ACCELERATOR (0x35), ACCELERATOR_BRAKE_ALT (0x100), ACCELERATOR_ALT (0x105) #define HYUNDAI_CANFD_COMMON_ADDR_CHECKS(pt_bus) \ - /* EV, ICE, HYBRID: ACCELERATOR, ACCELERATOR_BRAKE_ALT, ACCELERATOR_ALT */ \ {.msg = {{0x35, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, \ {0x100, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, \ {0x105, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}}}, \ @@ -64,8 +64,8 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { {.msg = {{0xa0, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ {.msg = {{0xea, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ +// SCC_CONTROL (from ADAS unit or camera) #define HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \ - /* SCC_CONTROL (from ADAS unit or camera) */ \ {.msg = {{0x1a0, (scc_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ #define HYUNDAI_CANFD_BUTTONS_ADDR_CHECK(pt_bus) \ @@ -112,7 +112,7 @@ HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_radar_scc_alt_buttons_addr_checks, HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_hda2_addr_checks, 1, 1, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(hyundai_canfd_hda2_long_addr_checks, 1, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) -addr_checks hyundai_canfd_rx_checks = {hyundai_canfd_addr_checks, ARRAY_LEN(hyundai_canfd_addr_checks)}; +addr_checks hyundai_canfd_rx_checks = SET_ADDR_CHECKS(hyundai_canfd_addr_checks); uint16_t hyundai_canfd_crc_lut[256]; @@ -368,23 +368,23 @@ static const addr_checks* hyundai_canfd_init(uint16_t param) { if (hyundai_longitudinal) { if (hyundai_canfd_hda2) { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_long_addr_checks, ARRAY_LEN(hyundai_canfd_hda2_long_addr_checks)}; + hyundai_canfd_rx_checks = SET_ADDR_CHECKS(hyundai_canfd_hda2_long_addr_checks); } else { hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons - ? (addr_checks){hyundai_canfd_long_alt_buttons_addr_checks, ARRAY_LEN(hyundai_canfd_long_alt_buttons_addr_checks)} - : (addr_checks){hyundai_canfd_long_addr_checks, ARRAY_LEN(hyundai_canfd_long_addr_checks)}; + ? SET_ADDR_CHECKS(hyundai_canfd_long_alt_buttons_addr_checks) + : SET_ADDR_CHECKS(hyundai_canfd_long_addr_checks); } } else { if (!hyundai_camera_scc && !hyundai_canfd_hda2) { hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons - ? (addr_checks){hyundai_canfd_radar_scc_alt_buttons_addr_checks, ARRAY_LEN(hyundai_canfd_radar_scc_alt_buttons_addr_checks)} - : (addr_checks){hyundai_canfd_radar_scc_addr_checks, ARRAY_LEN(hyundai_canfd_radar_scc_addr_checks)}; + ? SET_ADDR_CHECKS(hyundai_canfd_radar_scc_alt_buttons_addr_checks) + : SET_ADDR_CHECKS(hyundai_canfd_radar_scc_addr_checks); } else if (hyundai_canfd_hda2) { - hyundai_canfd_rx_checks = (addr_checks){hyundai_canfd_hda2_addr_checks, ARRAY_LEN(hyundai_canfd_hda2_addr_checks)}; + hyundai_canfd_rx_checks = SET_ADDR_CHECKS(hyundai_canfd_hda2_addr_checks); } else { hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons - ? (addr_checks){hyundai_canfd_alt_buttons_addr_checks, ARRAY_LEN(hyundai_canfd_alt_buttons_addr_checks)} - : (addr_checks){hyundai_canfd_addr_checks, ARRAY_LEN(hyundai_canfd_addr_checks)}; + ? SET_ADDR_CHECKS(hyundai_canfd_alt_buttons_addr_checks) + : SET_ADDR_CHECKS(hyundai_canfd_addr_checks); } } diff --git a/board/safety_declarations.h b/board/safety_declarations.h index 9064bb62e0..3050755cb9 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -4,6 +4,8 @@ #define GET_BYTE(msg, b) ((msg)->data[(b)]) #define GET_FLAG(value, mask) (((__typeof__(mask))(value) & (mask)) == (mask)) +#define SET_ADDR_CHECKS(name) ((addr_checks){(name), (sizeof((name)) / sizeof((name)[0]))}) + uint32_t GET_BYTES(const CANPacket_t *msg, int start, int len) { uint32_t ret = 0U; for (int i = 0; i < len; i++) { diff --git a/board/utils.h b/board/utils.h index 8de37373e0..3ab6b30bc8 100644 --- a/board/utils.h +++ b/board/utils.h @@ -22,8 +22,6 @@ (_a > 0) ? _a : (-_a); \ }) -#define ARRAY_LEN(name) (sizeof((name)) / sizeof((name)[0])) - #ifndef NULL #define NULL ((void*)0) #endif From 93955a8f812e3145dd96df5cbe239a7ca95e3c33 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 18:01:37 -0700 Subject: [PATCH 17/20] no intermediary macros for making the structs (remove misra violation) --- board/safety/safety_hyundai_canfd.h | 67 +++++++++++++++++------------ 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 9d116e1983..25f458f730 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -64,53 +64,66 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { {.msg = {{0xa0, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ {.msg = {{0xea, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ -// SCC_CONTROL (from ADAS unit or camera) -#define HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \ - {.msg = {{0x1a0, (scc_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ - #define HYUNDAI_CANFD_BUTTONS_ADDR_CHECK(pt_bus) \ {.msg = {{0x1cf, (pt_bus), 8, .check_checksum = false, .max_counter = 0xfU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ #define HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK(pt_bus) \ {.msg = {{0x1aa, (pt_bus), 16, .check_checksum = false, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ - -// *** Macros to create addr check structs *** -#define HYUNDAI_CANFD_CREATE_ADDR_CHECK(name, pt_bus, scc_bus, button_msg) \ -AddrCheckStruct (name)[] = { \ - HYUNDAI_CANFD_COMMON_ADDR_CHECKS((pt_bus)) \ - HYUNDAI_CANFD_SCC_ADDR_CHECK((scc_bus)) \ - button_msg((pt_bus)) /* cppcheck-suppress misra-c2012-20.7 */ \ -}; \ - -#define HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(name, pt_bus, button_msg) \ -AddrCheckStruct (name)[] = { \ - /* SCC not checked since its ECU is disabled on HDA2 and we use buttons */ \ - HYUNDAI_CANFD_COMMON_ADDR_CHECKS((pt_bus)) \ - button_msg((pt_bus)) /* cppcheck-suppress misra-c2012-20.7 */ \ -}; \ +// SCC_CONTROL (from ADAS unit or camera) +#define HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \ + {.msg = {{0x1a0, (scc_bus), 32, .check_checksum = true, .max_counter = 0xffU, .expected_timestep = 20000U}, { 0 }, { 0 }}}, \ // *** Non-HDA2 checks *** // Camera sends SCC messages on HDA1. // Both button messages exist on some platforms, so we ensure we track the correct one -HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_addr_checks, 0, 2, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) -HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_alt_buttons_addr_checks, 0, 2, HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK) +AddrCheckStruct hyundai_canfd_addr_checks[] = { + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) + HYUNDAI_CANFD_BUTTONS_ADDR_CHECK(0) + HYUNDAI_CANFD_SCC_ADDR_CHECK(2) +}; +AddrCheckStruct hyundai_canfd_alt_buttons_addr_checks[] = { + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) + HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK(0) + HYUNDAI_CANFD_SCC_ADDR_CHECK(2) +}; // Longitudinal checks for HDA1 -HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(hyundai_canfd_long_addr_checks, 0, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) -HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(hyundai_canfd_long_alt_buttons_addr_checks, 0, HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK) +AddrCheckStruct hyundai_canfd_long_addr_checks[] = { + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) + HYUNDAI_CANFD_BUTTONS_ADDR_CHECK(0) +}; +AddrCheckStruct hyundai_canfd_long_alt_buttons_addr_checks[] = { + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) + HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK(0) +}; // Radar sends SCC messages on these cars instead of camera -HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_radar_scc_addr_checks, 0, 0, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) -HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_radar_scc_alt_buttons_addr_checks, 0, 0, HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK) +AddrCheckStruct hyundai_canfd_radar_scc_addr_checks[] = { + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) + HYUNDAI_CANFD_BUTTONS_ADDR_CHECK(0) + HYUNDAI_CANFD_SCC_ADDR_CHECK(0) +}; +AddrCheckStruct hyundai_canfd_radar_scc_alt_buttons_addr_checks[] = { + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) + HYUNDAI_CANFD_ALT_BUTTONS_ADDR_CHECK(0) + HYUNDAI_CANFD_SCC_ADDR_CHECK(0) +}; // *** HDA2 checks *** // E-CAN is on bus 1, ADAS unit sends SCC messages on HDA2. // Does not use the alt buttons message -HYUNDAI_CANFD_CREATE_ADDR_CHECK(hyundai_canfd_hda2_addr_checks, 1, 1, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) -HYUNDAI_CANFD_CREATE_LONG_ADDR_CHECK(hyundai_canfd_hda2_long_addr_checks, 1, HYUNDAI_CANFD_BUTTONS_ADDR_CHECK) +AddrCheckStruct hyundai_canfd_hda2_addr_checks[] = { + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(1) + HYUNDAI_CANFD_BUTTONS_ADDR_CHECK(1) + HYUNDAI_CANFD_SCC_ADDR_CHECK(1) +}; +AddrCheckStruct hyundai_canfd_hda2_long_addr_checks[] = { + HYUNDAI_CANFD_COMMON_ADDR_CHECKS(1) + HYUNDAI_CANFD_BUTTONS_ADDR_CHECK(1) +}; addr_checks hyundai_canfd_rx_checks = SET_ADDR_CHECKS(hyundai_canfd_addr_checks); From d895bbdca5a0a84969909d47728231306c78d85c Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 21:55:56 -0700 Subject: [PATCH 18/20] Update board/safety/safety_hyundai_canfd.h --- board/safety/safety_hyundai_canfd.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 25f458f730..7b3e00d9aa 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -77,7 +77,7 @@ const CanMsg HYUNDAI_CANFD_HDA1_TX_MSGS[] = { // *** Non-HDA2 checks *** // Camera sends SCC messages on HDA1. -// Both button messages exist on some platforms, so we ensure we track the correct one +// Both button messages exist on some platforms, so we ensure we track the correct one using flag AddrCheckStruct hyundai_canfd_addr_checks[] = { HYUNDAI_CANFD_COMMON_ADDR_CHECKS(0) HYUNDAI_CANFD_BUTTONS_ADDR_CHECK(0) From 3c65a82d52c445bb6cdc8550f3ec69fd56053cad Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 23:42:50 -0700 Subject: [PATCH 19/20] single lines --- board/safety/safety_hyundai_canfd.h | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 7b3e00d9aa..1edc8eba91 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -383,21 +383,15 @@ static const addr_checks* hyundai_canfd_init(uint16_t param) { if (hyundai_canfd_hda2) { hyundai_canfd_rx_checks = SET_ADDR_CHECKS(hyundai_canfd_hda2_long_addr_checks); } else { - hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons - ? SET_ADDR_CHECKS(hyundai_canfd_long_alt_buttons_addr_checks) - : SET_ADDR_CHECKS(hyundai_canfd_long_addr_checks); + hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons ? SET_ADDR_CHECKS(hyundai_canfd_long_alt_buttons_addr_checks) : SET_ADDR_CHECKS(hyundai_canfd_long_addr_checks); } } else { if (!hyundai_camera_scc && !hyundai_canfd_hda2) { - hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons - ? SET_ADDR_CHECKS(hyundai_canfd_radar_scc_alt_buttons_addr_checks) - : SET_ADDR_CHECKS(hyundai_canfd_radar_scc_addr_checks); + hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons ? SET_ADDR_CHECKS(hyundai_canfd_radar_scc_alt_buttons_addr_checks) : SET_ADDR_CHECKS(hyundai_canfd_radar_scc_addr_checks); } else if (hyundai_canfd_hda2) { hyundai_canfd_rx_checks = SET_ADDR_CHECKS(hyundai_canfd_hda2_addr_checks); } else { - hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons - ? SET_ADDR_CHECKS(hyundai_canfd_alt_buttons_addr_checks) - : SET_ADDR_CHECKS(hyundai_canfd_addr_checks); + hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons ? SET_ADDR_CHECKS(hyundai_canfd_alt_buttons_addr_checks) : SET_ADDR_CHECKS(hyundai_canfd_addr_checks); } } From d4546d4ccedb0558b1128745911899c663fff1d7 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Thu, 7 Sep 2023 23:48:09 -0700 Subject: [PATCH 20/20] can avoid double checking hda2 since we have addr checks for it now --- board/safety/safety_hyundai_canfd.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 1edc8eba91..55c7de1090 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -386,10 +386,10 @@ static const addr_checks* hyundai_canfd_init(uint16_t param) { hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons ? SET_ADDR_CHECKS(hyundai_canfd_long_alt_buttons_addr_checks) : SET_ADDR_CHECKS(hyundai_canfd_long_addr_checks); } } else { - if (!hyundai_camera_scc && !hyundai_canfd_hda2) { - hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons ? SET_ADDR_CHECKS(hyundai_canfd_radar_scc_alt_buttons_addr_checks) : SET_ADDR_CHECKS(hyundai_canfd_radar_scc_addr_checks); - } else if (hyundai_canfd_hda2) { + if (hyundai_canfd_hda2) { hyundai_canfd_rx_checks = SET_ADDR_CHECKS(hyundai_canfd_hda2_addr_checks); + } else if (!hyundai_camera_scc) { + hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons ? SET_ADDR_CHECKS(hyundai_canfd_radar_scc_alt_buttons_addr_checks) : SET_ADDR_CHECKS(hyundai_canfd_radar_scc_addr_checks); } else { hyundai_canfd_rx_checks = hyundai_canfd_alt_buttons ? SET_ADDR_CHECKS(hyundai_canfd_alt_buttons_addr_checks) : SET_ADDR_CHECKS(hyundai_canfd_addr_checks); }