From 7af336244c272066d609efd8586c47467a74c609 Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Tue, 3 Oct 2023 00:32:13 -0400 Subject: [PATCH] Hyundai CAN-FD: Move CRC compute function to common Hyundai safety (#1634) * HKG CAN-FD: Move CRC lookup function to common Hyundai safety * cleanup * declare in common * Subaru: gen2 long safety (#1594) gen2 long safety * VW MQB: Check steer req bit (#1631) vq mqb * cleanup * more cleanup --------- Co-authored-by: Justin Newberry Co-authored-by: Adeeb Shihadeh --- board/safety/safety_hyundai_canfd.h | 34 +--------------------------- board/safety/safety_hyundai_common.h | 31 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 4e172b3470..f0af9060eb 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -128,9 +128,6 @@ AddrCheckStruct hyundai_canfd_hda2_long_addr_checks[] = { addr_checks hyundai_canfd_rx_checks = SET_ADDR_CHECKS(hyundai_canfd_addr_checks); -uint16_t hyundai_canfd_crc_lut[256]; - - const int HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 32; const int HYUNDAI_PARAM_CANFD_HDA2_ALT_STEERING = 128; bool hyundai_canfd_alt_buttons = false; @@ -156,39 +153,10 @@ static uint32_t hyundai_canfd_get_checksum(CANPacket_t *to_push) { return chksum; } -static uint32_t hyundai_canfd_compute_checksum(CANPacket_t *to_push) { - int len = GET_LEN(to_push); - uint32_t address = GET_ADDR(to_push); - - uint16_t crc = 0; - - for (int i = 2; i < len; i++) { - crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ GET_BYTE(to_push, i)]; - } - - // Add address to crc - crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 0U) & 0xFFU)]; - crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 8U) & 0xFFU)]; - - if (len == 8) { - crc ^= 0x5f29U; - } else if (len == 16) { - crc ^= 0x041dU; - } else if (len == 24) { - crc ^= 0x819dU; - } else if (len == 32) { - crc ^= 0x9f5bU; - } else { - - } - - return crc; -} - static int hyundai_canfd_rx_hook(CANPacket_t *to_push) { bool valid = addr_safety_check(to_push, &hyundai_canfd_rx_checks, - hyundai_canfd_get_checksum, hyundai_canfd_compute_checksum, hyundai_canfd_get_counter, NULL); + hyundai_canfd_get_checksum, hyundai_common_canfd_compute_checksum, hyundai_canfd_get_counter, NULL); int bus = GET_BUS(to_push); int addr = GET_ADDR(to_push); diff --git a/board/safety/safety_hyundai_common.h b/board/safety/safety_hyundai_common.h index 34974a4a64..8fec9b06cd 100644 --- a/board/safety/safety_hyundai_common.h +++ b/board/safety/safety_hyundai_common.h @@ -27,6 +27,8 @@ bool hyundai_canfd_hda2 = false; bool hyundai_alt_limits = false; uint8_t hyundai_last_button_interaction; // button messages since the user pressed an enable button +uint16_t hyundai_canfd_crc_lut[256]; + 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); @@ -85,4 +87,33 @@ void hyundai_common_cruise_buttons_check(const int cruise_button, const int main } } +uint32_t hyundai_common_canfd_compute_checksum(CANPacket_t *to_push) { + int len = GET_LEN(to_push); + uint32_t address = GET_ADDR(to_push); + + uint16_t crc = 0; + + for (int i = 2; i < len; i++) { + crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ GET_BYTE(to_push, i)]; + } + + // Add address to crc + crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 0U) & 0xFFU)]; + crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 8U) & 0xFFU)]; + + if (len == 8) { + crc ^= 0x5f29U; + } else if (len == 16) { + crc ^= 0x041dU; + } else if (len == 24) { + crc ^= 0x819dU; + } else if (len == 32) { + crc ^= 0x9f5bU; + } else { + + } + + return crc; +} + #endif