Skip to content

Commit

Permalink
revert the runtime dynamic rx check construction
Browse files Browse the repository at this point in the history
  • Loading branch information
ccdunder committed Feb 23, 2025
1 parent f96f0ba commit 854127d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 262 deletions.
77 changes: 0 additions & 77 deletions opendbc/safety/board/libc.h

This file was deleted.

28 changes: 1 addition & 27 deletions opendbc/safety/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
194 changes: 41 additions & 153 deletions opendbc/safety/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 0 additions & 4 deletions opendbc/safety/safety_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 854127d

Please sign in to comment.