Skip to content

Commit

Permalink
don't allow transmit/forward for 0x131 unless SecOC
Browse files Browse the repository at this point in the history
  • Loading branch information
jyoung8607 committed Oct 1, 2024
1 parent 5750eae commit 8da8161
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
27 changes: 20 additions & 7 deletions board/safety/safety_toyota.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
#include "safety_declarations.h"

// Stock longitudinal
// TODO: don't allow 0x2E4, 0, 5 for SecOC?
#define TOYOTA_COMMON_TX_MSGS \
{0x2E4, 0, 5}, {0x2E4, 0, 8}, {0x131, 0, 8}, {0x191, 0, 8}, {0x412, 0, 8}, {0x343, 0, 8}, {0x1D2, 0, 8}, /* LKAS + LTA + ACC & PCM cancel cmds */ \
{0x2E4, 0, 5}, {0x191, 0, 8}, {0x412, 0, 8}, {0x343, 0, 8}, {0x1D2, 0, 8}, /* LKAS + LTA + ACC & PCM cancel cmds */ \

#define TOYOTA_COMMON_SECOC_TX_MSGS \
TOYOTA_COMMON_TX_MSGS \
{0x2E4, 0, 8}, {0x131, 0, 8}, \

#define TOYOTA_COMMON_LONG_TX_MSGS \
TOYOTA_COMMON_TX_MSGS \
Expand Down Expand Up @@ -219,7 +224,7 @@ static bool toyota_tx_hook(const CANPacket_t *to_send) {
}

// LTA angle steering check for SecOC cars
if (addr == 0x131) {
if (toyota_secoc_car && (addr == 0x131)) {
// Block any form of actuation for now
if (GET_BYTE(to_send, 0) != 0U) {
tx = false;
Expand Down Expand Up @@ -309,6 +314,10 @@ static safety_config toyota_init(uint16_t param) {
TOYOTA_COMMON_TX_MSGS
};

static const CanMsg TOYOTA_SECOC_TX_MSGS[] = {
TOYOTA_COMMON_SECOC_TX_MSGS
};

static const CanMsg TOYOTA_LONG_TX_MSGS[] = {
TOYOTA_COMMON_LONG_TX_MSGS
};
Expand All @@ -322,17 +331,20 @@ static safety_config toyota_init(uint16_t param) {
const uint32_t TOYOTA_PARAM_LTA = 4UL << TOYOTA_PARAM_OFFSET;
const uint32_t TOYOTA_PARAM_SECOC_CAR = 8UL << TOYOTA_PARAM_OFFSET;

toyota_secoc_car = GET_FLAG(param, TOYOTA_PARAM_SECOC_CAR);
toyota_alt_brake = GET_FLAG(param, TOYOTA_PARAM_ALT_BRAKE);
toyota_stock_longitudinal = GET_FLAG(param, TOYOTA_PARAM_STOCK_LONGITUDINAL);

toyota_secoc_car = GET_FLAG(param, TOYOTA_PARAM_SECOC_CAR);

toyota_lta = GET_FLAG(param, TOYOTA_PARAM_LTA);
toyota_dbc_eps_torque_factor = param & TOYOTA_EPS_FACTOR;

safety_config ret;

if (toyota_stock_longitudinal) {
SET_TX_MSGS(TOYOTA_TX_MSGS, ret);
if (toyota_secoc_car) {
SET_TX_MSGS(TOYOTA_SECOC_TX_MSGS, ret);
} else {
SET_TX_MSGS(TOYOTA_TX_MSGS, ret);
}
} else {
SET_TX_MSGS(TOYOTA_LONG_TX_MSGS, ret);
}
Expand Down Expand Up @@ -366,8 +378,9 @@ static int toyota_fwd_hook(int bus_num, int addr) {
if (bus_num == 2) {
// block stock lkas messages and stock acc messages (if OP is doing ACC)
// in TSS2, 0x191 is LTA which we need to block to avoid controls collision
bool is_lkas_msg = ((addr == 0x2E4) || (addr == 0x412) || (addr == 0x191));
// on SecOC cars 0x131 is also LTA
bool is_lkas_msg = ((addr == 0x2E4) || (addr == 0x412) || (addr == 0x191) || (addr == 0x131));
is_lkas_msg |= toyota_secoc_car && (addr == 0x131);
// in TSS2 the camera does ACC as well, so filter 0x343
bool is_acc_msg = (addr == 0x343);
bool block_msg = is_lkas_msg || (is_acc_msg && !toyota_stock_longitudinal);
Expand Down
49 changes: 27 additions & 22 deletions tests/safety/test_toyota.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import panda.tests.safety.common as common
from panda.tests.safety.common import CANPackerPanda

TOYOTA_COMMON_TX_MSGS = [[0x2E4, 0], [0x131, 0], [0x191, 0], [0x412, 0], [0x343, 0], [0x1D2, 0]] # LKAS + LTA + ACC & PCM cancel cmds
TOYOTA_COMMON_TX_MSGS = [[0x2E4, 0], [0x191, 0], [0x412, 0], [0x343, 0], [0x1D2, 0]] # LKAS + LTA + ACC & PCM cancel cmds
TOYOTA_SECOC_TX_MSGS = [[0x131, 0]] + TOYOTA_COMMON_TX_MSGS
TOYOTA_COMMON_LONG_TX_MSGS = [[0x283, 0], [0x2E6, 0], [0x2E7, 0], [0x33E, 0], [0x344, 0], [0x365, 0], [0x366, 0], [0x4CB, 0], # DSU bus 0
[0x128, 1], [0x141, 1], [0x160, 1], [0x161, 1], [0x470, 1], # DSU bus 1
[0x411, 0], # PCS_HUD
Expand All @@ -21,7 +22,7 @@ class TestToyotaSafetyBase(common.PandaCarSafetyTest, common.LongitudinalAccelSa
TX_MSGS = TOYOTA_COMMON_TX_MSGS + TOYOTA_COMMON_LONG_TX_MSGS
STANDSTILL_THRESHOLD = 0 # kph
RELAY_MALFUNCTION_ADDRS = {0: (0x2E4, 0x343)}
FWD_BLACKLISTED_ADDRS = {2: [0x2E4, 0x412, 0x131, 0x191, 0x343]}
FWD_BLACKLISTED_ADDRS = {2: [0x2E4, 0x412, 0x191, 0x343]}
FWD_BUS_LOOKUP = {0: 2, 2: 0}
EPS_SCALE = 73

Expand Down Expand Up @@ -278,31 +279,12 @@ def test_lta_steer_cmd(self):
pass


class TestToyotaSecOcSafety(TestToyotaSafetyTorque):

def setUp(self):
self.packer = CANPackerPanda("toyota_rav4_prime_generated")
self.safety = libpanda_py.libpanda
self.safety.set_safety_hooks(Panda.SAFETY_TOYOTA, self.EPS_SCALE | Panda.FLAG_TOYOTA_SECOC_CAR)
self.safety.init_tests()

# This platform also has an alternate brake message, but same naming in the DBC, so same packer works

def _user_gas_msg(self, gas):
values = {"GAS_PEDAL_USER": gas}
return self.packer.make_can_msg_panda("GAS_PEDAL", 0, values)

# No LTA message in the DBC
def test_lta_steer_cmd(self):
pass


class TestToyotaStockLongitudinalBase(TestToyotaSafetyBase):

TX_MSGS = TOYOTA_COMMON_TX_MSGS
# Base addresses minus ACC_CONTROL (0x343)
RELAY_MALFUNCTION_ADDRS = {0: (0x2E4,)}
FWD_BLACKLISTED_ADDRS = {2: [0x2E4, 0x412, 0x191, 0x131]}
FWD_BLACKLISTED_ADDRS = {2: [0x2E4, 0x412, 0x191]}

def test_diagnostics(self, stock_longitudinal: bool = True):
super().test_diagnostics(stock_longitudinal=stock_longitudinal)
Expand Down Expand Up @@ -343,5 +325,28 @@ def setUp(self):
self.safety.init_tests()


class TestToyotaSecOcSafety(TestToyotaStockLongitudinalBase):

TX_MSGS = TOYOTA_SECOC_TX_MSGS
RELAY_MALFUNCTION_ADDRS = {0: (0x2E4,)}
FWD_BLACKLISTED_ADDRS = {2: [0x2E4, 0x412, 0x191, 0x131]}

def setUp(self):
self.packer = CANPackerPanda("toyota_rav4_prime_generated")
self.safety = libpanda_py.libpanda
self.safety.set_safety_hooks(Panda.SAFETY_TOYOTA, self.EPS_SCALE | Panda.FLAG_TOYOTA_STOCK_LONGITUDINAL | Panda.FLAG_TOYOTA_SECOC_CAR)
self.safety.init_tests()

# This platform also has an alternate brake message, but same naming in the DBC, so same packer works

def _user_gas_msg(self, gas):
values = {"GAS_PEDAL_USER": gas}
return self.packer.make_can_msg_panda("GAS_PEDAL", 0, values)

# No LTA message in the DBC
def test_lta_steer_cmd(self):
pass


if __name__ == "__main__":
unittest.main()

0 comments on commit 8da8161

Please sign in to comment.