Skip to content

Commit c325de2

Browse files
committed
1 parent 75d4e0e commit c325de2

File tree

5 files changed

+87
-76
lines changed

5 files changed

+87
-76
lines changed

opendbc/safety/safety/safety_hyundai.h

+4-9
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ static const CanMsg HYUNDAI_TX_MSGS[] = {
5353
HYUNDAI_COMMON_TX_MSGS(0)
5454
};
5555

56-
static bool hyundai_legacy = false;
57-
5856
static uint8_t hyundai_get_counter(const CANPacket_t *to_push) {
5957
int addr = GET_ADDR(to_push);
6058

@@ -288,6 +286,8 @@ static int hyundai_fwd_hook(int bus_num, int addr) {
288286
}
289287

290288
static safety_config hyundai_init(uint16_t param) {
289+
hyundai_flags(param);
290+
291291
static const CanMsg HYUNDAI_LONG_TX_MSGS[] = {
292292
HYUNDAI_LONG_COMMON_TX_MSGS(0)
293293
{0x38D, 0, 8}, // FCA11 Bus 0
@@ -303,9 +303,6 @@ static safety_config hyundai_init(uint16_t param) {
303303
HYUNDAI_LONG_COMMON_TX_MSGS(2)
304304
};
305305

306-
hyundai_common_init(param);
307-
hyundai_legacy = false;
308-
309306
safety_config ret;
310307
if (hyundai_longitudinal) {
311308
static RxCheck hyundai_long_rx_checks[] = {
@@ -335,16 +332,14 @@ static safety_config hyundai_init(uint16_t param) {
335332
}
336333

337334
static safety_config hyundai_legacy_init(uint16_t param) {
335+
hyundai_flags(param);
336+
338337
// older hyundai models have less checks due to missing counters and checksums
339338
static RxCheck hyundai_legacy_rx_checks[] = {
340339
HYUNDAI_COMMON_RX_CHECKS(true)
341340
HYUNDAI_SCC12_ADDR_CHECK(0)
342341
};
343342

344-
hyundai_common_init(param);
345-
hyundai_legacy = true;
346-
hyundai_longitudinal = false;
347-
hyundai_camera_scc = false;
348343
return BUILD_SAFETY_CFG(hyundai_legacy_rx_checks, HYUNDAI_TX_MSGS);
349344
}
350345

opendbc/safety/safety/safety_hyundai_canfd.h

+2-11
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
#define HYUNDAI_CANFD_SCC_ADDR_CHECK(scc_bus) \
4040
{.msg = {{0x1a0, (scc_bus), 32, .max_counter = 0xffU, .frequency = 50U}, { 0 }, { 0 }}}, \
4141

42-
static bool hyundai_canfd_alt_buttons = false;
43-
static bool hyundai_canfd_lka_steering_alt = false;
44-
4542
static int hyundai_canfd_get_lka_addr(void) {
4643
return hyundai_canfd_lka_steering_alt ? 0x110 : 0x50;
4744
}
@@ -250,8 +247,8 @@ static int hyundai_canfd_fwd_hook(int bus_num, int addr) {
250247
}
251248

252249
static safety_config hyundai_canfd_init(uint16_t param) {
253-
const int HYUNDAI_PARAM_CANFD_LKA_STEERING_ALT = 128;
254-
const int HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 32;
250+
hyundai_canfd_flags(param);
251+
gen_crc_lookup_table_16(0x1021, hyundai_canfd_crc_lut);
255252

256253
static const CanMsg HYUNDAI_CANFD_LKA_STEERING_TX_MSGS[] = {
257254
HYUNDAI_CANFD_LKA_STEERING_COMMON_TX_MSGS(0, 1)
@@ -295,12 +292,6 @@ static safety_config hyundai_canfd_init(uint16_t param) {
295292
{0x160, 0, 16}, // ADRV_0x160
296293
};
297294

298-
hyundai_common_init(param);
299-
300-
gen_crc_lookup_table_16(0x1021, hyundai_canfd_crc_lut);
301-
hyundai_canfd_alt_buttons = GET_FLAG(param, HYUNDAI_PARAM_CANFD_ALT_BUTTONS);
302-
hyundai_canfd_lka_steering_alt = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LKA_STEERING_ALT);
303-
304295
safety_config ret;
305296
if (hyundai_longitudinal) {
306297
if (hyundai_canfd_lka_steering) {

opendbc/safety/safety/safety_hyundai_common.h

+4-54
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#pragma once
22

33
#include "safety_declarations.h"
4+
#include "safety_hyundai_flags.h"
45

56
extern uint16_t hyundai_canfd_crc_lut[256];
67
uint16_t hyundai_canfd_crc_lut[256];
78

89
static const uint8_t HYUNDAI_PREV_BUTTON_SAMPLES = 8; // roughly 160 ms
9-
//
10+
1011
extern const uint32_t HYUNDAI_STANDSTILL_THRSLD;
1112
const uint32_t HYUNDAI_STANDSTILL_THRSLD = 12; // 0.375 kph
1213

@@ -17,59 +18,8 @@ enum {
1718
HYUNDAI_BTN_CANCEL = 4,
1819
};
1920

20-
// common state
21-
extern bool hyundai_ev_gas_signal;
22-
bool hyundai_ev_gas_signal = false;
23-
24-
extern bool hyundai_hybrid_gas_signal;
25-
bool hyundai_hybrid_gas_signal = false;
26-
27-
extern bool hyundai_longitudinal;
28-
bool hyundai_longitudinal = false;
29-
30-
extern bool hyundai_camera_scc;
31-
bool hyundai_camera_scc = false;
32-
33-
extern bool hyundai_canfd_lka_steering;
34-
bool hyundai_canfd_lka_steering = false;
35-
36-
extern bool hyundai_alt_limits;
37-
bool hyundai_alt_limits = false;
38-
39-
extern bool hyundai_fcev_gas_signal;
40-
bool hyundai_fcev_gas_signal = false;
41-
42-
extern bool hyundai_alt_limits_2;
43-
bool hyundai_alt_limits_2 = false;
44-
45-
static uint8_t hyundai_last_button_interaction; // button messages since the user pressed an enable button
46-
47-
void hyundai_common_init(uint16_t param) {
48-
const int HYUNDAI_PARAM_EV_GAS = 1;
49-
const int HYUNDAI_PARAM_HYBRID_GAS = 2;
50-
const int HYUNDAI_PARAM_CAMERA_SCC = 8;
51-
const int HYUNDAI_PARAM_CANFD_LKA_STEERING = 16;
52-
const int HYUNDAI_PARAM_ALT_LIMITS = 64; // TODO: shift this down with the rest of the common flags
53-
const int HYUNDAI_PARAM_FCEV_GAS = 256;
54-
const int HYUNDAI_PARAM_ALT_LIMITS_2 = 512;
55-
56-
hyundai_ev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_EV_GAS);
57-
hyundai_hybrid_gas_signal = !hyundai_ev_gas_signal && GET_FLAG(param, HYUNDAI_PARAM_HYBRID_GAS);
58-
hyundai_camera_scc = GET_FLAG(param, HYUNDAI_PARAM_CAMERA_SCC);
59-
hyundai_canfd_lka_steering = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LKA_STEERING);
60-
hyundai_alt_limits = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS);
61-
hyundai_fcev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_FCEV_GAS);
62-
hyundai_alt_limits_2 = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS_2);
63-
64-
hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES;
65-
66-
#ifdef ALLOW_DEBUG
67-
const int HYUNDAI_PARAM_LONGITUDINAL = 4;
68-
hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_LONGITUDINAL);
69-
#else
70-
hyundai_longitudinal = false;
71-
#endif
72-
}
21+
extern uint8_t hyundai_last_button_interaction; // button messages since the user pressed an enable button
22+
uint8_t hyundai_last_button_interaction = HYUNDAI_PREV_BUTTON_SAMPLES;
7323

7424
void hyundai_common_cruise_state_check(const bool cruise_engaged) {
7525
// some newer HKG models can re-enable after spamming cancel button,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#pragma once
2+
3+
// common flags
4+
extern bool hyundai_ev_gas_signal;
5+
bool hyundai_ev_gas_signal = false;
6+
7+
extern bool hyundai_hybrid_gas_signal;
8+
bool hyundai_hybrid_gas_signal = false;
9+
10+
extern bool hyundai_longitudinal;
11+
bool hyundai_longitudinal = false;
12+
13+
extern bool hyundai_camera_scc;
14+
bool hyundai_camera_scc = false;
15+
16+
extern bool hyundai_canfd_lka_steering;
17+
bool hyundai_canfd_lka_steering = false;
18+
19+
extern bool hyundai_fcev_gas_signal;
20+
bool hyundai_fcev_gas_signal = false;
21+
22+
// shared flags for non CAN FD cars
23+
extern bool hyundai_alt_limits;
24+
bool hyundai_alt_limits = false;
25+
26+
extern bool hyundai_alt_limits_2;
27+
bool hyundai_alt_limits_2 = false;
28+
29+
// shared flags for CAN FD cars
30+
extern bool hyundai_canfd_alt_buttons;
31+
bool hyundai_canfd_alt_buttons = false;
32+
33+
extern bool hyundai_canfd_lka_steering_alt;
34+
bool hyundai_canfd_lka_steering_alt = false;
35+
36+
void hyundai_common_flags(uint16_t param) {
37+
const int HYUNDAI_PARAM_EV_GAS = 1;
38+
const int HYUNDAI_PARAM_HYBRID_GAS = 2;
39+
const int HYUNDAI_PARAM_CAMERA_SCC = 8;
40+
const int HYUNDAI_PARAM_CANFD_LKA_STEERING = 16; // TODO: rename for support with CAN/CAN FD Blended platforms
41+
const int HYUNDAI_PARAM_FCEV_GAS = 256;
42+
43+
hyundai_ev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_EV_GAS);
44+
hyundai_hybrid_gas_signal = !hyundai_ev_gas_signal && GET_FLAG(param, HYUNDAI_PARAM_HYBRID_GAS);
45+
hyundai_camera_scc = GET_FLAG(param, HYUNDAI_PARAM_CAMERA_SCC);
46+
hyundai_canfd_lka_steering = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LKA_STEERING);
47+
hyundai_fcev_gas_signal = GET_FLAG(param, HYUNDAI_PARAM_FCEV_GAS);
48+
49+
#ifdef ALLOW_DEBUG
50+
const int HYUNDAI_PARAM_LONGITUDINAL = 4;
51+
hyundai_longitudinal = GET_FLAG(param, HYUNDAI_PARAM_LONGITUDINAL);
52+
#else
53+
hyundai_longitudinal = false;
54+
#endif
55+
}
56+
57+
void hyundai_flags(uint16_t param) {
58+
hyundai_common_flags(param);
59+
60+
const int HYUNDAI_PARAM_ALT_LIMITS = 64;
61+
const int HYUNDAI_PARAM_ALT_LIMITS_2 = 512;
62+
63+
hyundai_alt_limits = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS);
64+
hyundai_alt_limits_2 = GET_FLAG(param, HYUNDAI_PARAM_ALT_LIMITS_2);
65+
}
66+
67+
void hyundai_canfd_flags(uint16_t param) {
68+
hyundai_common_flags(param);
69+
70+
const int HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 32;
71+
const int HYUNDAI_PARAM_CANFD_LKA_STEERING_ALT = 128;
72+
73+
hyundai_canfd_lka_steering_alt = GET_FLAG(param, HYUNDAI_PARAM_CANFD_LKA_STEERING_ALT);
74+
hyundai_canfd_alt_buttons = GET_FLAG(param, HYUNDAI_PARAM_CANFD_ALT_BUTTONS);
75+
}

opendbc/safety/tests/test_hyundai.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class TestHyundaiLegacySafetyEV(TestHyundaiSafety):
173173
def setUp(self):
174174
self.packer = CANPackerPanda("hyundai_kia_generic")
175175
self.safety = libsafety_py.libsafety
176-
self.safety.set_safety_hooks(CarParams.SafetyModel.hyundaiLegacy, 1)
176+
self.safety.set_safety_hooks(CarParams.SafetyModel.hyundaiLegacy, HyundaiSafetyFlags.EV_GAS)
177177
self.safety.init_tests()
178178

179179
def _user_gas_msg(self, gas):
@@ -185,7 +185,7 @@ class TestHyundaiLegacySafetyHEV(TestHyundaiSafety):
185185
def setUp(self):
186186
self.packer = CANPackerPanda("hyundai_kia_generic")
187187
self.safety = libsafety_py.libsafety
188-
self.safety.set_safety_hooks(CarParams.SafetyModel.hyundaiLegacy, 2)
188+
self.safety.set_safety_hooks(CarParams.SafetyModel.hyundaiLegacy, HyundaiSafetyFlags.HYBRID_GAS)
189189
self.safety.init_tests()
190190

191191
def _user_gas_msg(self, gas):

0 commit comments

Comments
 (0)