Skip to content

Commit f2292e4

Browse files
committed
Hyundai: added safety check for button spam
1 parent 1a8c4c4 commit f2292e4

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

board/safety/safety_hyundai.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ const int HYUNDAI_MAX_RATE_DOWN = 7;
66
const int HYUNDAI_DRIVER_TORQUE_ALLOWANCE = 50;
77
const int HYUNDAI_DRIVER_TORQUE_FACTOR = 2;
88

9-
int hyundai_brake_prev = 0;
10-
int hyundai_gas_prev = 0;
11-
int hyundai_speed = 0;
129
int hyundai_camera_detected = 0;
1310
int hyundai_giraffe_switch_2 = 0; // is giraffe switch 2 high?
1411
int hyundai_rt_torque_last = 0;
@@ -67,11 +64,6 @@ static int hyundai_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
6764
return 0;
6865
}
6966

70-
// disallow actuator commands if gas or brake (with vehicle moving) are pressed
71-
// and the the latching controls_allowed flag is True
72-
int pedal_pressed = hyundai_gas_prev || (hyundai_brake_prev && hyundai_speed);
73-
int current_controls_allowed = controls_allowed && !pedal_pressed;
74-
7567
uint32_t addr;
7668
if (to_send->RIR & 4) {
7769
// Extended
@@ -87,7 +79,7 @@ static int hyundai_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
8779
uint32_t ts = TIM2->CNT;
8880
int violation = 0;
8981

90-
if (current_controls_allowed) {
82+
if (controls_allowed) {
9183

9284
// *** global torque limit check ***
9385
violation |= max_limit_check(desired_torque, HYUNDAI_MAX_STEER, -HYUNDAI_MAX_STEER);
@@ -112,12 +104,12 @@ static int hyundai_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
112104
}
113105

114106
// no torque if controls is not allowed
115-
if (!current_controls_allowed && (desired_torque != 0)) {
107+
if (!controls_allowed && (desired_torque != 0)) {
116108
violation = 1;
117109
}
118110

119111
// reset to 0 if either controls is not allowed or there's a violation
120-
if (violation || !current_controls_allowed) {
112+
if (violation || !controls_allowed) {
121113
hyundai_desired_torque_last = 0;
122114
hyundai_rt_torque_last = 0;
123115
hyundai_ts_last = ts;
@@ -128,6 +120,13 @@ static int hyundai_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
128120
}
129121
}
130122

123+
// FORCE CANCEL: safety check only relevant when spamming the cancel button.
124+
// ensuring that only the cancel button press is sent (VAL 4) when controls are off.
125+
// This avoids unintended engagements while still allowing resume spam
126+
if (((to_send->RIR>>21) == 1265) && !controls_allowed && ((to_send->RDTR >> 4) & 0xFF) == 0) {
127+
if ((to_send->RDLR & 0x7) != 4) return 0;
128+
}
129+
131130
// 1 allows the message through
132131
return true;
133132
}

tests/safety/test_honda.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def test_alt_disengage_on_brake(self):
108108
self.safety.set_controls_allowed(1)
109109
self.safety.honda_rx_hook(self._alt_brake_msg(1))
110110
self.assertFalse(self.safety.get_controls_allowed())
111-
111+
112112
self.safety.set_honda_alt_brake_msg(0)
113113
self.safety.set_controls_allowed(1)
114114
self.safety.honda_rx_hook(self._alt_brake_msg(1))

tests/safety/test_hyundai.py

+20
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def setUp(cls):
3232
cls.safety.nooutput_init(0)
3333
cls.safety.init_tests_hyundai()
3434

35+
def _button_msg(self, buttons):
36+
to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *')
37+
to_send[0].RIR = 1265 << 21
38+
to_send[0].RDLR = buttons
39+
return to_send
40+
3541
def _set_prev_torque(self, t):
3642
self.safety.set_hyundai_desired_torque_last(t)
3743
self.safety.set_hyundai_rt_torque_last(t)
@@ -162,5 +168,19 @@ def test_realtime_limits(self):
162168
self.assertTrue(self.safety.hyundai_tx_hook(self._torque_msg(sign * (MAX_RT_DELTA + 1))))
163169

164170

171+
def test_spam_cancel_safety_check(self):
172+
RESUME_BTN = 1
173+
SET_BTN = 2
174+
CANCEL_BTN = 4
175+
BUTTON_MSG = 1265
176+
self.safety.set_controls_allowed(0)
177+
self.assertTrue(self.safety.hyundai_tx_hook(self._button_msg(CANCEL_BTN)))
178+
self.assertFalse(self.safety.hyundai_tx_hook(self._button_msg(RESUME_BTN)))
179+
self.assertFalse(self.safety.hyundai_tx_hook(self._button_msg(SET_BTN)))
180+
# do not block resume if we are engaged already
181+
self.safety.set_controls_allowed(1)
182+
self.assertTrue(self.safety.hyundai_tx_hook(self._button_msg(RESUME_BTN)))
183+
184+
165185
if __name__ == "__main__":
166186
unittest.main()

0 commit comments

Comments
 (0)