Skip to content

Commit

Permalink
Move to safety core and trigger rx_checks_invalid.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccdunder committed Feb 22, 2025
1 parent 3482e71 commit 1acfd9b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
28 changes: 27 additions & 1 deletion opendbc/safety/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ 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 @@ -169,6 +170,30 @@ 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 @@ -309,7 +334,7 @@ void safety_tick(const safety_config *cfg) {
}
}

safety_rx_checks_invalid = rx_checks_invalid;
safety_rx_checks_invalid = rx_checks_invalid || add_check_failed;
}

static void relay_malfunction_set(void) {
Expand Down Expand Up @@ -418,6 +443,7 @@ 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
25 changes: 1 addition & 24 deletions opendbc/safety/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,6 @@
#include "safety_declarations.h"
#include "safety_hyundai_common.h"

#define MAX_RX_CHECKS 16

static safety_config hyundai_canfd_init_safety_config(void) {
static RxCheck hyundai_canfd_rx_checks[MAX_RX_CHECKS] = {0};
safety_config ret = {
.rx_checks = hyundai_canfd_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 {
// TODO: Trigger safety_rx_checks_invalid.
}
}

static bool hyundai_canfd_alt_buttons = false;
static bool hyundai_canfd_lka_steering_alt = false;

Expand Down Expand Up @@ -280,7 +257,7 @@ static safety_config hyundai_canfd_init(uint16_t param) {
hyundai_longitudinal = false;
}

safety_config ret = hyundai_canfd_init_safety_config();
safety_config ret = safety_config_init();

// RX Common checks.
const int pt_bus = hyundai_canfd_lka_steering ? 1 : 0;
Expand Down
4 changes: 4 additions & 0 deletions opendbc/safety/safety_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
(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 @@ -168,6 +169,8 @@ 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 @@ -206,6 +209,7 @@ 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

0 comments on commit 1acfd9b

Please sign in to comment.