diff --git a/opendbc/safety/board/libc.h b/opendbc/safety/board/libc.h deleted file mode 100644 index c5f0219350..0000000000 --- a/opendbc/safety/board/libc.h +++ /dev/null @@ -1,77 +0,0 @@ -// **** libc **** - -void delay(uint32_t a) { - volatile uint32_t i; - for (i = 0; i < a; i++); -} - -void assert_fatal(bool condition, const char *msg) { - if (!condition) { - print("ASSERT FAILED\n"); - print(msg); - while (1) { - // hang - } - } -} - -// cppcheck-suppress misra-c2012-21.2 -void *memset(void *str, int c, unsigned int n) { - uint8_t *s = str; - for (unsigned int i = 0; i < n; i++) { - *s = c; - s++; - } - return str; -} - -#define UNALIGNED(X, Y) \ - (((uint32_t)(X) & (sizeof(uint32_t) - 1U)) | ((uint32_t)(Y) & (sizeof(uint32_t) - 1U))) - -// cppcheck-suppress misra-c2012-21.2 -void *memcpy(void *dest, const void *src, unsigned int len) { - unsigned int n = len; - uint8_t *d8 = dest; - const uint8_t *s8 = src; - - if ((n >= 4U) && !UNALIGNED(s8, d8)) { - uint32_t *d32 = (uint32_t *)d8; // cppcheck-suppress misra-c2012-11.3 ; already checked that it's properly aligned - const uint32_t *s32 = (const uint32_t *)s8; // cppcheck-suppress misra-c2012-11.3 ; already checked that it's properly aligned - - while(n >= 16U) { - *d32 = *s32; d32++; s32++; - *d32 = *s32; d32++; s32++; - *d32 = *s32; d32++; s32++; - *d32 = *s32; d32++; s32++; - n -= 16U; - } - - while(n >= 4U) { - *d32 = *s32; d32++; s32++; - n -= 4U; - } - - d8 = (uint8_t *)d32; - s8 = (const uint8_t *)s32; - } - while (n-- > 0U) { - *d8 = *s8; d8++; s8++; - } - return dest; -} - -// cppcheck-suppress misra-c2012-21.2 -int memcmp(const void * ptr1, const void * ptr2, unsigned int num) { - int ret = 0; - const uint8_t *p1 = ptr1; - const uint8_t *p2 = ptr2; - for (unsigned int i = 0; i < num; i++) { - if (*p1 != *p2) { - ret = -1; - break; - } - p1++; - p2++; - } - return ret; -} diff --git a/opendbc/safety/safety.h b/opendbc/safety/safety.h index 49081a52a1..913109957c 100644 --- a/opendbc/safety/safety.h +++ b/opendbc/safety/safety.h @@ -80,7 +80,6 @@ bool vehicle_moving = false; bool acc_main_on = false; // referred to as "ACC off" in ISO 15622:2018 int cruise_button_prev = 0; bool safety_rx_checks_invalid = false; -bool add_check_failed = false; // for safety modes with torque steering control int desired_torque_last = 0; // last desired steer torque @@ -170,30 +169,6 @@ static void update_counter(RxCheck addr_list[], int index, uint8_t counter) { } } -// This is pretty arbirary. It's more than enough for now. -#define MAX_RX_CHECKS 16 - -static safety_config safety_config_init(void) { - static RxCheck rx_checks[MAX_RX_CHECKS] = {0}; - safety_config ret = { - .rx_checks = rx_checks, - .rx_checks_len = 0, - .tx_msgs = NULL, - .tx_msgs_len = 0 - }; - return ret; -} - -static void add_rx_check(safety_config *safetyConfig, RxCheck config) { - const uint8_t index = safetyConfig->rx_checks_len; - if (index < (uint8_t)MAX_RX_CHECKS) { - safetyConfig->rx_checks_len++; - (void)memcpy(&safetyConfig->rx_checks[index], &config, sizeof(RxCheck)); - } else { - add_check_failed = true; - } -} - static bool rx_msg_safety_check(const CANPacket_t *to_push, const safety_config *cfg, const safety_hooks *safety_hooks) { @@ -334,7 +309,7 @@ void safety_tick(const safety_config *cfg) { } } - safety_rx_checks_invalid = rx_checks_invalid || add_check_failed; + safety_rx_checks_invalid = rx_checks_invalid; } static void relay_malfunction_set(void) { @@ -443,7 +418,6 @@ int set_safety_hooks(uint16_t mode, uint16_t param) { controls_allowed = false; relay_malfunction_reset(); safety_rx_checks_invalid = false; - add_check_failed = false; current_safety_config.rx_checks = NULL; current_safety_config.rx_checks_len = 0; diff --git a/opendbc/safety/safety/safety_hyundai_canfd.h b/opendbc/safety/safety/safety_hyundai_canfd.h index 76f7089156..427a23797a 100644 --- a/opendbc/safety/safety/safety_hyundai_canfd.h +++ b/opendbc/safety/safety/safety_hyundai_canfd.h @@ -32,9 +32,6 @@ static void hyundai_canfd_rx_hook(const CANPacket_t *to_push) { const int pt_bus = hyundai_canfd_lka_steering ? 1 : 0; const int scc_bus = hyundai_camera_scc ? 2 : pt_bus; - // TODO: this a bug? this what checks was using. - // const int8_t scc_bus = hyundai_canfd_lka_steering ? 1 : hyundai_camera_scc ? 2 : 0; - if (bus == pt_bus) { // driver torque if (addr == 0xea) { @@ -208,111 +205,10 @@ static int hyundai_canfd_fwd_hook(int bus_num, int addr) { return bus_fwd; } -/* - - -*/ - -/***************** -RX_CHECKS_CONFIG = [ - # Common. - { - DEFAULT: - """ - {.msg = {{0x175, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .frequency = 50U}, { 0 }, { 0 }}}, - {.msg = {{0xa0, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}, - {.msg = {{0xea, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}, - """ - } - # Accel. - { - "hyundai_ev_gas_signal": - """ - {.msg = {{0x35, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}, - """ - "hyundai_hybrid_gas_signal": - """ - {.msg = {{0x105, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}, - """ - DEFAULT: - """ - {.msg = {{0x100, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}, - """ - }, - - # Cruise. - { - "hyundai_canfd_alt_buttons": - """ - {.msg = {{0x1aa, (pt_bus), 16, .check_checksum = false, .max_counter = 0xffU, .frequency = 50U}, { 0 }, { 0 }}}, - """ - DEFAULT: - """ - {.msg = {{0x1cf, (pt_bus), 8, .check_checksum = false, .max_counter = 0xfU, .frequency = 50U}, { 0 }, { 0 }}}, - """ - }, - - # SCC. - { - "hyundai_longitudinal": - # SCC_CONTROL sent, not read. - """" - {0}, - """ - DEFAULT: - # SCC_CONTROL read. - """ - {.msg = {{0x1a0, (scc_bus), 32, .check_checksum = true, .max_counter = 0xffU, .frequency = 50U}, { 0 }, { 0 }}}, - """ - }, -] - -def gen_checks(): - config_strs = [] - for key_val_pairs in itertools.product(*[list(conf.items()) for conf in RX_CHECKS_CONFIG]): - checks = {} - for key, val in key_val_pairs: - checks[key] = val - config_str = "" - for check in checks: - if check is None: - continue - config_str += check - config_strs.append(config_str) - return "\n".join(config_strs) - - -def get_rx_checks(hyundai_ev_gas_signal, hyundai_hybrid_gas_signal, hyundai_canfd_alt_buttons, hyundai_longitudinal): - - -*****************/ - -static RxCheck* get_rx_checks() { - int pt_bus = 1; - int scc_bus = 2; - static RxCheck foo[] = { - {.msg = {{0x175, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .frequency = 50U}, { 0 }, { 0 }}}, - {.msg = {{0xa0, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}, - {.msg = {{0xea, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}, - - - {.msg = {{0x100, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}, - - - {.msg = {{0x1cf, (pt_bus), 8, .check_checksum = false, .max_counter = 0xfU, .frequency = 50U}, { 0 }, { 0 }}}, - - - {.msg = {{0x1a0, (scc_bus), 32, .check_checksum = true, .max_counter = 0xffU, .frequency = 50U}, { 0 }, { 0 }}}, - - }; - return foo; -} - static safety_config hyundai_canfd_init(uint16_t param) { const int HYUNDAI_PARAM_CANFD_LKA_STEERING_ALT = 128; const int HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 32; - // TODO: Build TX checks more precisely (like RX checks). static const CanMsg HYUNDAI_CANFD_LKA_STEERING_TX_MSGS[] = { {0x50, 0, 16}, // LKAS {0x1CF, 1, 8}, // CRUISE_BUTTON @@ -348,72 +244,64 @@ static safety_config hyundai_canfd_init(uint16_t param) { {0x1E0, 0, 16}, // LFAHDA_CLUSTER }; + hyundai_common_init(param); gen_crc_lookup_table_16(0x1021, hyundai_canfd_crc_lut); hyundai_canfd_alt_buttons = GET_FLAG(param, HYUNDAI_PARAM_CANFD_ALT_BUTTONS); hyundai_canfd_lka_steering_alt = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LKA_STEERING_ALT); - // No long for radar-SCC with LFA steering yet. + // no long for radar-SCC with LFA steering yet if (!hyundai_canfd_lka_steering && !hyundai_camera_scc) { hyundai_longitudinal = false; } - safety_config ret = safety_config_init(); - // RX Common checks. - const int pt_bus = hyundai_canfd_lka_steering ? 1 : 0; - add_rx_check(&ret, (RxCheck){.msg = {{0x175, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .frequency = 50U}, { 0 }, { 0 }}}); - add_rx_check(&ret, (RxCheck){.msg = {{0xa0, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}); - add_rx_check(&ret, (RxCheck){.msg = {{0xea, (pt_bus), 24, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}); - - // Accel signals. - if (hyundai_ev_gas_signal) { - add_rx_check(&ret, (RxCheck){.msg = {{0x35, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}); - } else if (hyundai_hybrid_gas_signal) { - add_rx_check(&ret, (RxCheck){.msg = {{0x105, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}); - } else { - add_rx_check(&ret, (RxCheck){.msg = {{0x100, (pt_bus), 32, .check_checksum = true, .max_counter = 0xffU, .frequency = 100U}, { 0 }, { 0 }}}); - } - - // Cruise buttons. - if (hyundai_canfd_alt_buttons) { - add_rx_check(&ret, (RxCheck){.msg = {{0x1aa, (pt_bus), 16, .check_checksum = false, .max_counter = 0xffU, .frequency = 50U}, { 0 }, { 0 }}}); - } else { - add_rx_check(&ret, (RxCheck){.msg = {{0x1cf, (pt_bus), 8, .check_checksum = false, .max_counter = 0xfU, .frequency = 50U}, { 0 }, { 0 }}}); - } - - if (hyundai_longitudinal) { - // SCC_CONTROL sent, not read. - } else { - // SCC_CONTROL read. - const int8_t scc_bus = hyundai_canfd_lka_steering ? 1 : hyundai_camera_scc ? 2 : 0; - add_rx_check(&ret, (RxCheck){.msg = {{0x1a0, (scc_bus), 32, .check_checksum = true, .max_counter = 0xffU, .frequency = 50U}, { 0 }, { 0 }}}); - } - - //static const RxCheck RX_CHECKS[] = rx_checks(); - //RX_CHECKS - //rx_checks(); - - // TX checks. + safety_config ret; if (hyundai_longitudinal) { if (hyundai_canfd_lka_steering) { - if (hyundai_canfd_lka_steering_alt) { - // TODO: Support this. - } else { - SET_TX_MSGS(HYUNDAI_CANFD_LKA_STEERING_LONG_TX_MSGS, ret); - } + static RxCheck hyundai_canfd_lka_steering_long_rx_checks[] = { + HYUNDAI_CANFD_COMMON_RX_CHECKS(1) + }; + + ret = BUILD_SAFETY_CFG(hyundai_canfd_lka_steering_long_rx_checks, HYUNDAI_CANFD_LKA_STEERING_LONG_TX_MSGS); } else { - SET_TX_MSGS(HYUNDAI_CANFD_LFA_STEERING_TX_MSGS, ret); + // Longitudinal checks for LFA steering + static RxCheck hyundai_canfd_long_rx_checks[] = { + HYUNDAI_CANFD_COMMON_RX_CHECKS(0) + }; + + ret = BUILD_SAFETY_CFG(hyundai_canfd_long_rx_checks, HYUNDAI_CANFD_LFA_STEERING_TX_MSGS); } } else { if (hyundai_canfd_lka_steering) { - if (hyundai_canfd_lka_steering_alt) { - SET_TX_MSGS(HYUNDAI_CANFD_LKA_STEERING_ALT_TX_MSGS, ret); - } else { - SET_TX_MSGS(HYUNDAI_CANFD_LKA_STEERING_TX_MSGS, ret); - } + // *** LKA steering checks *** + // E-CAN is on bus 1, SCC messages are sent on cars with ADRV ECU. + // Does not use the alt buttons message + static RxCheck hyundai_canfd_lka_steering_rx_checks[] = { + HYUNDAI_CANFD_COMMON_RX_CHECKS(1) + HYUNDAI_CANFD_SCC_ADDR_CHECK(1) + }; + + ret = hyundai_canfd_lka_steering_alt ? BUILD_SAFETY_CFG(hyundai_canfd_lka_steering_rx_checks, HYUNDAI_CANFD_LKA_STEERING_ALT_TX_MSGS) : \ + BUILD_SAFETY_CFG(hyundai_canfd_lka_steering_rx_checks, HYUNDAI_CANFD_LKA_STEERING_TX_MSGS); + } else if (!hyundai_camera_scc) { + // Radar sends SCC messages on these cars instead of camera + static RxCheck hyundai_canfd_radar_scc_rx_checks[] = { + HYUNDAI_CANFD_COMMON_RX_CHECKS(0) + HYUNDAI_CANFD_SCC_ADDR_CHECK(0) + }; + + ret = BUILD_SAFETY_CFG(hyundai_canfd_radar_scc_rx_checks, HYUNDAI_CANFD_LFA_STEERING_TX_MSGS); } else { - SET_TX_MSGS(HYUNDAI_CANFD_LFA_STEERING_TX_MSGS, ret); + // *** LFA steering checks *** + // Camera sends SCC messages on LFA steering cars. + // Both button messages exist on some platforms, so we ensure we track the correct one using flag + static RxCheck hyundai_canfd_rx_checks[] = { + HYUNDAI_CANFD_COMMON_RX_CHECKS(0) + HYUNDAI_CANFD_SCC_ADDR_CHECK(2) + }; + + ret = BUILD_SAFETY_CFG(hyundai_canfd_rx_checks, HYUNDAI_CANFD_LFA_STEERING_TX_MSGS); } } diff --git a/opendbc/safety/safety_declarations.h b/opendbc/safety/safety_declarations.h index 780b057292..b0e9588e95 100644 --- a/opendbc/safety/safety_declarations.h +++ b/opendbc/safety/safety_declarations.h @@ -21,7 +21,6 @@ (config).tx_msgs_len = sizeof((tx)) / sizeof((tx)[0]); \ } while(0); - #define UPDATE_VEHICLE_SPEED(val_ms) (update_sample(&vehicle_speed, ROUND((val_ms) * VEHICLE_SPEED_FACTOR))) uint32_t GET_BYTES(const CANPacket_t *msg, int start, int len); @@ -169,8 +168,6 @@ typedef struct { get_quality_flag_valid_t get_quality_flag_valid; } safety_hooks; -static safety_config safety_config_init(void); -static void add_rx_check(safety_config *safetyConfig, RxCheck config); bool safety_rx_hook(const CANPacket_t *to_push); bool safety_tx_hook(CANPacket_t *to_send); uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last); @@ -209,7 +206,6 @@ extern bool vehicle_moving; extern bool acc_main_on; // referred to as "ACC off" in ISO 15622:2018 extern int cruise_button_prev; extern bool safety_rx_checks_invalid; -extern bool add_check_failed; // for safety modes with torque steering control extern int desired_torque_last; // last desired steer torque diff --git a/opendbc/safety/tests/libsafety/safety.c b/opendbc/safety/tests/libsafety/safety.c index 4035c4cc60..bdb63596b1 100644 --- a/opendbc/safety/tests/libsafety/safety.c +++ b/opendbc/safety/tests/libsafety/safety.c @@ -6,7 +6,6 @@ //int safety_tx_hook(CANPacket_t *to_send) { return 1; } #include "faults.h" -#include "libc.h" #include "safety.h" #include "drivers/can_common.h"