Skip to content

Commit 5a24ffe

Browse files
authored
Move relay malfunction check to fwd hook (commaai#2087)
* ohhh wait a minute * fix from merge * todo * we need to test this now * we got it * fix rest * rm
1 parent b733481 commit 5a24ffe

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

opendbc/safety/safety.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,6 @@ bool safety_rx_hook(const CANPacket_t *to_push) {
219219
// Handles gas, brake, and regen paddle
220220
generic_rx_checks();
221221

222-
// the relay malfunction hook runs on all incoming rx messages.
223-
// check all tx msgs for liveness on sending bus if specified.
224-
// used to detect a relay malfunction or control messages from disabled ECUs like the radar
225-
const int bus = GET_BUS(to_push);
226-
const int addr = GET_ADDR(to_push);
227-
for (int i = 0; i < current_safety_config.tx_msgs_len; i++) {
228-
const CanMsg *m = &current_safety_config.tx_msgs[i];
229-
if (m->check_relay) {
230-
stock_ecu_check((m->addr == addr) && (m->bus == bus));
231-
}
232-
}
233-
234222
// reset mismatches on rising edge of controls_allowed to avoid rare race condition
235223
if (controls_allowed && !controls_allowed_prev) {
236224
heartbeat_engaged_mismatches = 0;
@@ -281,6 +269,16 @@ static int get_fwd_bus(int bus_num) {
281269
}
282270

283271
int safety_fwd_hook(int bus_num, int addr) {
272+
// the relay malfunction hook runs on all incoming rx messages.
273+
// check all tx msgs for liveness on sending bus if specified.
274+
// used to detect a relay malfunction or control messages from disabled ECUs like the radar
275+
for (int i = 0; i < current_safety_config.tx_msgs_len; i++) {
276+
const CanMsg *m = &current_safety_config.tx_msgs[i];
277+
if (m->check_relay) {
278+
stock_ecu_check((m->addr == addr) && (m->bus == bus_num));
279+
}
280+
}
281+
284282
bool blocked = relay_malfunction || current_safety_config.disable_forwarding;
285283

286284
if (!blocked && (current_hooks->fwd != NULL)) {

opendbc/safety/tests/common.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ class PandaSafetyTest(PandaSafetyTestBase):
751751
*range(0x18DA00F1, 0x18DB00F1, 0x100), # 29-bit UDS physical addressing
752752
*range(0x18DB00F1, 0x18DC00F1, 0x100), # 29-bit UDS functional addressing
753753
*range(0x3300, 0x3400)] # Honda
754+
RELAY_MALFUNCTION_ADDRS: dict[int, tuple[int, ...]] | None = {}
754755
FWD_BLACKLISTED_ADDRS: dict[int, list[int]] = {} # {bus: [addr]}
755756
FWD_BUS_LOOKUP: dict[int, int] = {0: 2, 2: 0}
756757

@@ -774,10 +775,13 @@ def test_fwd_hook(self):
774775
# some safety modes don't forward anything, while others blacklist msgs
775776
for bus in range(3):
776777
for addr in self.SCANNED_ADDRS:
778+
self.safety.set_relay_malfunction(False)
777779
# assume len 8
778780
fwd_bus = self.FWD_BUS_LOOKUP.get(bus, -1)
779781
if bus in self.FWD_BLACKLISTED_ADDRS and addr in self.FWD_BLACKLISTED_ADDRS[bus]:
780782
fwd_bus = -1
783+
if bus in self.RELAY_MALFUNCTION_ADDRS and addr in self.RELAY_MALFUNCTION_ADDRS[bus]:
784+
fwd_bus = -1
781785
self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(bus, addr), f"{addr=:#x} from {bus=} to {fwd_bus=}")
782786

783787
def test_spam_can_buses(self):
@@ -878,7 +882,6 @@ def test_tx_hook_on_wrong_safety_mode(self):
878882
class PandaCarSafetyTest(PandaSafetyTest):
879883
STANDSTILL_THRESHOLD: float = 0.0
880884
GAS_PRESSED_THRESHOLD = 0
881-
RELAY_MALFUNCTION_ADDRS: dict[int, tuple[int, ...]] | None = None
882885

883886
@classmethod
884887
def setUpClass(cls):
@@ -919,7 +922,7 @@ def test_relay_malfunction(self):
919922
for bus in range(3):
920923
for addr in self.SCANNED_ADDRS:
921924
self.safety.set_relay_malfunction(False)
922-
self._rx(make_msg(bus, addr, 8))
925+
self.safety.safety_fwd_hook(bus, addr)
923926
should_relay_malfunction = addr in self.RELAY_MALFUNCTION_ADDRS.get(bus, ())
924927
self.assertEqual(should_relay_malfunction, self.safety.get_relay_malfunction(), (bus, hex(addr)))
925928

opendbc/safety/tests/hyundai_common.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import opendbc.safety.tests.common as common
44
from opendbc.safety.tests.libsafety import libsafety_py
5-
from opendbc.safety.tests.common import make_msg
65

76

87
class Buttons:
@@ -151,5 +150,5 @@ def test_disabled_ecu_alive(self):
151150

152151
addr, bus = self.DISABLED_ECU_ACTUATION_MSG
153152
self.assertFalse(self.safety.get_relay_malfunction())
154-
self._rx(make_msg(bus, addr, 8))
153+
self.safety.safety_fwd_hook(bus, addr)
155154
self.assertTrue(self.safety.get_relay_malfunction())

0 commit comments

Comments
 (0)