Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hyundai Safety: Safety flags in one place #1964

Closed
wants to merge 12 commits into from
13 changes: 4 additions & 9 deletions opendbc/safety/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ static const CanMsg HYUNDAI_TX_MSGS[] = {
HYUNDAI_COMMON_TX_MSGS(0)
};

static bool hyundai_legacy = false;

static uint8_t hyundai_get_counter(const CANPacket_t *to_push) {
int addr = GET_ADDR(to_push);

Expand Down Expand Up @@ -271,6 +269,8 @@ static bool hyundai_fwd_hook(int bus_num, int addr) {
}

static safety_config hyundai_init(uint16_t param) {
hyundai_flags(param);

static const CanMsg HYUNDAI_LONG_TX_MSGS[] = {
HYUNDAI_LONG_COMMON_TX_MSGS(0)
{0x38D, 0, 8, false}, // FCA11 Bus 0
Expand All @@ -286,9 +286,6 @@ static safety_config hyundai_init(uint16_t param) {
HYUNDAI_LONG_COMMON_TX_MSGS(2)
};

hyundai_common_init(param);
hyundai_legacy = false;
Copy link
Contributor

@sshane sshane Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep non-common bools where they are used


safety_config ret;
if (hyundai_longitudinal) {
static RxCheck hyundai_long_rx_checks[] = {
Expand Down Expand Up @@ -318,16 +315,14 @@ static safety_config hyundai_init(uint16_t param) {
}

static safety_config hyundai_legacy_init(uint16_t param) {
hyundai_flags(param);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a much more ambiguous name than hyundai_common_init


// older hyundai models have less checks due to missing counters and checksums
static RxCheck hyundai_legacy_rx_checks[] = {
HYUNDAI_COMMON_RX_CHECKS(true)
HYUNDAI_SCC12_ADDR_CHECK(0)
};

hyundai_common_init(param);
hyundai_legacy = true;
hyundai_longitudinal = false;
hyundai_camera_scc = false;
return BUILD_SAFETY_CFG(hyundai_legacy_rx_checks, HYUNDAI_TX_MSGS);
}

Expand Down
13 changes: 2 additions & 11 deletions opendbc/safety/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
#define HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \
{.msg = {{0x1a0, (scc_bus), 32, .max_counter = 0xffU, .frequency = 50U}, { 0 }, { 0 }}}, \

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

static int hyundai_canfd_get_lka_addr(void) {
return hyundai_canfd_lka_steering_alt ? 0x110 : 0x50;
}
Expand Down Expand Up @@ -235,8 +232,8 @@ static bool hyundai_canfd_fwd_hook(int bus_num, int addr) {
}

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;
hyundai_canfd_flags(param);
gen_crc_lookup_table_16(0x1021, hyundai_canfd_crc_lut);

static const CanMsg HYUNDAI_CANFD_LKA_STEERING_TX_MSGS[] = {
HYUNDAI_CANFD_LKA_STEERING_COMMON_TX_MSGS(0, 1)
Expand Down Expand Up @@ -279,12 +276,6 @@ static safety_config hyundai_canfd_init(uint16_t param) {
HYUNDAI_CANFD_SCC_CONTROL_COMMON_TX_MSGS(0, (longitudinal)) \
{0x160, 0, 16, false}, /* ADRV_0x160 */ \

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);

safety_config ret;
if (hyundai_longitudinal) {
if (hyundai_canfd_lka_steering) {
Expand Down
58 changes: 4 additions & 54 deletions opendbc/safety/safety/safety_hyundai_common.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#pragma once

#include "safety_declarations.h"
#include "safety_hyundai_flags.h"

extern uint16_t hyundai_canfd_crc_lut[256];
uint16_t hyundai_canfd_crc_lut[256];

static const uint8_t HYUNDAI_PREV_BUTTON_SAMPLES = 8; // roughly 160 ms
//

extern const uint32_t HYUNDAI_STANDSTILL_THRSLD;
const uint32_t HYUNDAI_STANDSTILL_THRSLD = 12; // 0.375 kph

Expand All @@ -17,59 +18,8 @@ enum {
HYUNDAI_BTN_CANCEL = 4,
};

// common state
extern bool hyundai_ev_gas_signal;
bool hyundai_ev_gas_signal = false;

extern bool hyundai_hybrid_gas_signal;
bool hyundai_hybrid_gas_signal = false;

extern bool hyundai_longitudinal;
bool hyundai_longitudinal = false;

extern bool hyundai_camera_scc;
bool hyundai_camera_scc = false;

extern bool hyundai_canfd_lka_steering;
bool hyundai_canfd_lka_steering = false;

extern bool hyundai_alt_limits;
bool hyundai_alt_limits = false;

//extern bool hyundai_fcev_gas_signal;
//bool hyundai_fcev_gas_signal = false;

extern bool hyundai_alt_limits_2;
bool hyundai_alt_limits_2 = false;

static uint8_t hyundai_last_button_interaction; // button messages since the user pressed an enable button

void hyundai_common_init(uint16_t param) {
const int HYUNDAI_PARAM_EV_GAS = 1;
const int HYUNDAI_PARAM_HYBRID_GAS = 2;
const int HYUNDAI_PARAM_CAMERA_SCC = 8;
const int HYUNDAI_PARAM_CANFD_LKA_STEERING = 16;
const int HYUNDAI_PARAM_ALT_LIMITS = 64; // TODO: shift this down with the rest of the common flags
// const int HYUNDAI_PARAM_FCEV_GAS = 256;
const int HYUNDAI_PARAM_ALT_LIMITS_2 = 512;

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);
hyundai_camera_scc = GET_FLAG(param, HYUNDAI_PARAM_CAMERA_SCC);
hyundai_canfd_lka_steering = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LKA_STEERING);
hyundai_alt_limits = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS);
// hyundai_fcev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_FCEV_GAS);
hyundai_alt_limits_2 = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS_2);

hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES;

#ifdef ALLOW_DEBUG
const int HYUNDAI_PARAM_LONGITUDINAL = 4;
hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_LONGITUDINAL);
#else
hyundai_longitudinal = false;
#endif
}
extern uint8_t hyundai_last_button_interaction; // button messages since the user pressed an enable button
uint8_t hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES;

void hyundai_common_cruise_state_check(const bool cruise_engaged) {
// some newer HKG models can re-enable after spamming cancel button,
Expand Down
77 changes: 77 additions & 0 deletions opendbc/safety/safety/safety_hyundai_flags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

enum {
HYUNDAI_PARAM_EV_GAS = 1,
HYUNDAI_PARAM_HYBRID_GAS = 2,
#ifdef ALLOW_DEBUG
HYUNDAI_PARAM_LONGITUDINAL = 4,
#endif
HYUNDAI_PARAM_CAMERA_SCC = 8,
HYUNDAI_PARAM_CANFD_LKA_STEERING = 16, // TODO: rename for support with CAN/CAN FD Blended platforms
HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 32,
HYUNDAI_PARAM_ALT_LIMITS = 64,
HYUNDAI_PARAM_CANFD_LKA_STEERING_ALT = 128,
// HYUNDAI_PARAM_FCEV_GAS = 256,
HYUNDAI_PARAM_ALT_LIMITS_2 = 512,
};

// common flags
extern bool hyundai_ev_gas_signal;
bool hyundai_ev_gas_signal = false;

extern bool hyundai_hybrid_gas_signal;
bool hyundai_hybrid_gas_signal = false;

extern bool hyundai_longitudinal;
bool hyundai_longitudinal = false;

extern bool hyundai_camera_scc;
bool hyundai_camera_scc = false;

extern bool hyundai_canfd_lka_steering;
bool hyundai_canfd_lka_steering = false;

//extern bool hyundai_fcev_gas_signal;
//bool hyundai_fcev_gas_signal = false;

// shared flags for non CAN FD cars
extern bool hyundai_alt_limits;
bool hyundai_alt_limits = false;

extern bool hyundai_alt_limits_2;
bool hyundai_alt_limits_2 = false;

// shared flags for CAN FD cars
extern bool hyundai_canfd_alt_buttons;
bool hyundai_canfd_alt_buttons = false;

extern bool hyundai_canfd_lka_steering_alt;
bool hyundai_canfd_lka_steering_alt = false;

void hyundai_common_flags(uint16_t param) {
hyundai_ev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_EV_GAS);
hyundai_hybrid_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_HYBRID_GAS);
hyundai_camera_scc = GET_FLAG(param, HYUNDAI_PARAM_CAMERA_SCC);
hyundai_canfd_lka_steering = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LKA_STEERING);
// hyundai_fcev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_FCEV_GAS);

#ifdef ALLOW_DEBUG
hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_LONGITUDINAL);
#else
hyundai_longitudinal = false;
#endif
}

void hyundai_flags(uint16_t param) {
hyundai_common_flags(param);

hyundai_alt_limits = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS);
hyundai_alt_limits_2 = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS_2);
}

void hyundai_canfd_flags(uint16_t param) {
hyundai_common_flags(param);

hyundai_canfd_lka_steering_alt = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LKA_STEERING_ALT);
hyundai_canfd_alt_buttons = GET_FLAG(param, HYUNDAI_PARAM_CANFD_ALT_BUTTONS);
}