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

safety: fwd hook config #1954

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
d885b0c
move toyota fwd to tx config
sshane Mar 8, 2025
09b6ecb
don't love this
sshane Mar 8, 2025
bff5121
oh nice
sshane Mar 8, 2025
4803ade
closer to top
sshane Mar 8, 2025
649cdd6
Rivian is nice and clean
sshane Mar 8, 2025
4d82389
Nissan is also nice diff
sshane Mar 8, 2025
9d790a8
do GM
sshane Mar 8, 2025
8a3d636
Chrysler
sshane Mar 8, 2025
c3933e1
Mazda
sshane Mar 8, 2025
9b72aa2
VW PQ
sshane Mar 8, 2025
db7ec1e
sweet wins
sshane Mar 8, 2025
cf5aa8b
small wins
sshane Mar 8, 2025
66d489f
hyundai not too bad
sshane Mar 8, 2025
8f9179e
elm and alloutput
sshane Mar 8, 2025
27695a5
ah an actual ford test bug
sshane Mar 8, 2025
1c2c045
forgot to remove these & body
sshane Mar 8, 2025
5d68c09
do subaru. we can't block what is sent on bus 1, so check that
sshane Mar 8, 2025
2fb7103
misra!
sshane Mar 8, 2025
e3d4039
forgot to remove these
sshane Mar 8, 2025
125dd29
for now pass tests
sshane Mar 8, 2025
e641d0f
use a bool for this
sshane Mar 8, 2025
956071b
Do part of Tesla
sshane Mar 8, 2025
b221a68
Merge remote-tracking branch 'upstream/master' into fwd-config
sshane Mar 10, 2025
00e8cfc
fix
sshane Mar 10, 2025
50ab9fb
fix
sshane Mar 10, 2025
28ded4e
Merge remote-tracking branch 'upstream/master' into fwd-config
sshane Mar 14, 2025
9ae80c3
remove
sshane Mar 14, 2025
e7bbdfe
whoops
sshane Mar 14, 2025
c5bf64f
woo
sshane Mar 14, 2025
c0c0b2d
first
sshane Mar 14, 2025
087a8c4
Merge remote-tracking branch 'upstream/master' into fwd-config
sshane Mar 14, 2025
4da0129
more
sshane Mar 14, 2025
06245ec
more
sshane Mar 14, 2025
7dd8098
more
sshane Mar 14, 2025
9d0ad08
these are the same now!
sshane Mar 14, 2025
ef02a3e
rm addr check
sshane Mar 14, 2025
73167f1
fix rivian
sshane Mar 14, 2025
f91efec
fix gm
sshane Mar 14, 2025
1bd3f89
more
sshane Mar 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion opendbc/safety/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,22 @@ bool safety_tx_hook(CANPacket_t *to_send) {
}

int safety_fwd_hook(int bus_num, int addr) {
return (relay_malfunction ? -1 : current_hooks->fwd(bus_num, addr));
const int destination_bus = bus_num == 0 ? 2 : (bus_num == 2 ? 0 : -1);

bool blocked = false;
if (current_safety_config.block_fwding) {
blocked = true;
} else {
for (int i = 0; i < current_safety_config.tx_msgs_len; i++) {
const CanMsg *m = &current_safety_config.tx_msgs[i];
if ((m->addr == addr) && (m->bus == destination_bus) && m->blocked) {
blocked = true;
break;
}
}
}

return (relay_malfunction || blocked) ? -1 : ((current_hooks->fwd != NULL) ? current_hooks->fwd(bus_num, addr) : destination_bus);
}

bool get_longitudinal_allowed(void) {
Expand Down Expand Up @@ -431,6 +446,7 @@ int set_safety_hooks(uint16_t mode, uint16_t param) {
current_safety_config.rx_checks_len = 0;
current_safety_config.tx_msgs = NULL;
current_safety_config.tx_msgs_len = 0;
current_safety_config.block_fwding = false;

int set_status = -1; // not set
int hook_config_count = sizeof(safety_hook_registry) / sizeof(safety_hook_config);
Expand All @@ -448,6 +464,7 @@ int set_safety_hooks(uint16_t mode, uint16_t param) {
current_safety_config.rx_checks_len = cfg.rx_checks_len;
current_safety_config.tx_msgs = cfg.tx_msgs;
current_safety_config.tx_msgs_len = cfg.tx_msgs_len;
current_safety_config.block_fwding = cfg.block_fwding;
// reset all dynamic fields in addr struct
for (int j = 0; j < current_safety_config.rx_checks_len; j++) {
current_safety_config.rx_checks[j].status = (RxStatus){0};
Expand Down
5 changes: 3 additions & 2 deletions opendbc/safety/safety/safety_body.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ static safety_config body_init(uint16_t param) {
{0x1, 0, 8}}; // CAN flasher

UNUSED(param);
return BUILD_SAFETY_CFG(body_rx_checks, BODY_TX_MSGS);
safety_config ret = BUILD_SAFETY_CFG(body_rx_checks, BODY_TX_MSGS);
ret.block_fwding = true;
return ret;
}

const safety_hooks body_hooks = {
.init = body_init,
.rx = body_rx_hook,
.tx = body_tx_hook,
.fwd = default_fwd_hook,
};
30 changes: 6 additions & 24 deletions opendbc/safety/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,6 @@ static bool chrysler_tx_hook(const CANPacket_t *to_send) {
return tx;
}

static int chrysler_fwd_hook(int bus_num, int addr) {
int bus_fwd = -1;

// forward to camera
if (bus_num == 0) {
bus_fwd = 2;
}

// forward all messages from camera except LKAS messages
const bool is_lkas = ((addr == chrysler_addrs->LKAS_COMMAND) || (addr == chrysler_addrs->DAS_6));
if ((bus_num == 2) && !is_lkas){
bus_fwd = 0;
}

return bus_fwd;
}

static safety_config chrysler_init(uint16_t param) {

const uint32_t CHRYSLER_PARAM_RAM_DT = 1U; // set for Ram DT platform
Expand Down Expand Up @@ -230,14 +213,14 @@ static safety_config chrysler_init(uint16_t param) {

static const CanMsg CHRYSLER_TX_MSGS[] = {
{CHRYSLER_ADDRS.CRUISE_BUTTONS, 0, 3},
{CHRYSLER_ADDRS.LKAS_COMMAND, 0, 6},
{CHRYSLER_ADDRS.DAS_6, 0, 8},
{CHRYSLER_ADDRS.LKAS_COMMAND, 0, 6, .blocked = true},
{CHRYSLER_ADDRS.DAS_6, 0, 8, .blocked = true},
};

static const CanMsg CHRYSLER_RAM_DT_TX_MSGS[] = {
{CHRYSLER_RAM_DT_ADDRS.CRUISE_BUTTONS, 2, 3},
{CHRYSLER_RAM_DT_ADDRS.LKAS_COMMAND, 0, 8},
{CHRYSLER_RAM_DT_ADDRS.DAS_6, 0, 8},
{CHRYSLER_RAM_DT_ADDRS.LKAS_COMMAND, 0, 8, .blocked = true},
{CHRYSLER_RAM_DT_ADDRS.DAS_6, 0, 8, .blocked = true},
};

#ifdef ALLOW_DEBUG
Expand All @@ -263,8 +246,8 @@ static safety_config chrysler_init(uint16_t param) {

static const CanMsg CHRYSLER_RAM_HD_TX_MSGS[] = {
{CHRYSLER_RAM_HD_ADDRS.CRUISE_BUTTONS, 2, 3},
{CHRYSLER_RAM_HD_ADDRS.LKAS_COMMAND, 0, 8},
{CHRYSLER_RAM_HD_ADDRS.DAS_6, 0, 8},
{CHRYSLER_RAM_HD_ADDRS.LKAS_COMMAND, 0, 8, .blocked = true},
{CHRYSLER_RAM_HD_ADDRS.DAS_6, 0, 8, .blocked = true},
};

const uint32_t CHRYSLER_PARAM_RAM_HD = 2U; // set for Ram HD platform
Expand Down Expand Up @@ -297,7 +280,6 @@ const safety_hooks chrysler_hooks = {
.init = chrysler_init,
.rx = chrysler_rx_hook,
.tx = chrysler_tx_hook,
.fwd = chrysler_fwd_hook,
.get_counter = chrysler_get_counter,
.get_checksum = chrysler_get_checksum,
.compute_checksum = chrysler_compute_checksum,
Expand Down
35 changes: 4 additions & 31 deletions opendbc/safety/safety/safety_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void default_rx_hook(const CANPacket_t *to_push) {

static safety_config nooutput_init(uint16_t param) {
UNUSED(param);
return (safety_config){NULL, 0, NULL, 0};
return (safety_config){NULL, 0, NULL, 0, true};
}

// GCOV_EXCL_START
Expand All @@ -21,56 +21,29 @@ static bool nooutput_tx_hook(const CANPacket_t *to_send) {
}
// GCOV_EXCL_STOP

static int default_fwd_hook(int bus_num, int addr) {
UNUSED(bus_num);
UNUSED(addr);
return -1;
}

const safety_hooks nooutput_hooks = {
.init = nooutput_init,
.rx = default_rx_hook,
.tx = nooutput_tx_hook,
.fwd = default_fwd_hook,
};

// *** all output safety mode ***

// Enables passthrough mode where relay is open and bus 0 gets forwarded to bus 2 and vice versa
static bool alloutput_passthrough = false;

static safety_config alloutput_init(uint16_t param) {
// Enables passthrough mode where relay is open and bus 0 gets forwarded to bus 2 and vice versa
const uint16_t ALLOUTPUT_PARAM_PASSTHROUGH = 1;
controls_allowed = true;
alloutput_passthrough = GET_FLAG(param, ALLOUTPUT_PARAM_PASSTHROUGH);
return (safety_config){NULL, 0, NULL, 0};
bool alloutput_passthrough = GET_FLAG(param, ALLOUTPUT_PARAM_PASSTHROUGH);
safety_config ret = {NULL, 0, NULL, 0, !alloutput_passthrough};
return ret;
}

static bool alloutput_tx_hook(const CANPacket_t *to_send) {
UNUSED(to_send);
return true;
}

static int alloutput_fwd_hook(int bus_num, int addr) {
int bus_fwd = -1;
UNUSED(addr);

if (alloutput_passthrough) {
if (bus_num == 0) {
bus_fwd = 2;
}
if (bus_num == 2) {
bus_fwd = 0;
}
}

return bus_fwd;
}

const safety_hooks alloutput_hooks = {
.init = alloutput_init,
.rx = default_rx_hook,
.tx = alloutput_tx_hook,
.fwd = alloutput_fwd_hook,
};
1 change: 0 additions & 1 deletion opendbc/safety/safety/safety_elm327.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ const safety_hooks elm327_hooks = {
.init = nooutput_init,
.rx = default_rx_hook,
.tx = elm327_tx_hook,
.fwd = default_fwd_hook,
};
58 changes: 13 additions & 45 deletions opendbc/safety/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ static void ford_rx_hook(const CANPacket_t *to_push) {

// If steering controls messages are received on the destination bus, it's an indication
// that the relay might be malfunctioning.
// TODO: replace with blocked messages!
bool stock_ecu_detected = ford_lkas_msg_check(addr);
if (ford_longitudinal) {
stock_ecu_detected = stock_ecu_detected || (addr == FORD_ACCDATA);
Expand Down Expand Up @@ -319,38 +320,6 @@ static bool ford_tx_hook(const CANPacket_t *to_send) {
return tx;
}

static int ford_fwd_hook(int bus_num, int addr) {
int bus_fwd = -1;

switch (bus_num) {
case FORD_MAIN_BUS: {
// Forward all traffic from bus 0 onward
bus_fwd = FORD_CAM_BUS;
break;
}
case FORD_CAM_BUS: {
if (ford_lkas_msg_check(addr)) {
// Block stock LKAS and UI messages
bus_fwd = -1;
} else if (ford_longitudinal && (addr == FORD_ACCDATA)) {
// Block stock ACC message
bus_fwd = -1;
} else {
// Forward remaining traffic
bus_fwd = FORD_MAIN_BUS;
}
break;
}
default: {
// No other buses should be in use; fallback to do-not-forward
bus_fwd = -1;
break;
}
}

return bus_fwd;
}

static safety_config ford_init(uint16_t param) {
bool ford_canfd = false;

Expand All @@ -369,33 +338,33 @@ static safety_config ford_init(uint16_t param) {
{.msg = {{FORD_DesiredTorqBrk, 0, 8, .ignore_checksum = true, .ignore_counter = true, .frequency = 50U}, { 0 }, { 0 }}},
};

#define FORD_COMMON_TX_MSGS \
{FORD_Steering_Data_FD1, 0, 8}, \
{FORD_Steering_Data_FD1, 2, 8}, \
{FORD_ACCDATA_3, 0, 8}, \
{FORD_Lane_Assist_Data1, 0, 8}, \
{FORD_IPMA_Data, 0, 8}, \
#define FORD_COMMON_TX_MSGS \
{FORD_Steering_Data_FD1, 0, 8}, \
{FORD_Steering_Data_FD1, 2, 8}, \
{FORD_ACCDATA_3, 0, 8, .blocked = true}, \
{FORD_Lane_Assist_Data1, 0, 8, .blocked = true}, \
{FORD_IPMA_Data, 0, 8, .blocked = true}, \

static const CanMsg FORD_CANFD_LONG_TX_MSGS[] = {
FORD_COMMON_TX_MSGS
{FORD_ACCDATA, 0, 8},
{FORD_LateralMotionControl2, 0, 8},
{FORD_ACCDATA, 0, 8, .blocked = true},
{FORD_LateralMotionControl2, 0, 8, .blocked = true},
};

static const CanMsg FORD_CANFD_STOCK_TX_MSGS[] = {
FORD_COMMON_TX_MSGS
{FORD_LateralMotionControl2, 0, 8},
{FORD_LateralMotionControl2, 0, 8, .blocked = true},
};

static const CanMsg FORD_STOCK_TX_MSGS[] = {
FORD_COMMON_TX_MSGS
{FORD_LateralMotionControl, 0, 8},
{FORD_LateralMotionControl, 0, 8, .blocked = true},
};

static const CanMsg FORD_LONG_TX_MSGS[] = {
FORD_COMMON_TX_MSGS
{FORD_ACCDATA, 0, 8},
{FORD_LateralMotionControl, 0, 8},
{FORD_ACCDATA, 0, 8, .blocked = true},
{FORD_LateralMotionControl, 0, 8, .blocked = true},
};

const uint16_t FORD_PARAM_CANFD = 2;
Expand Down Expand Up @@ -426,7 +395,6 @@ const safety_hooks ford_hooks = {
.init = ford_init,
.rx = ford_rx_hook,
.tx = ford_tx_hook,
.fwd = ford_fwd_hook,
.get_counter = ford_get_counter,
.get_checksum = ford_get_checksum,
.compute_checksum = ford_compute_checksum,
Expand Down
44 changes: 11 additions & 33 deletions opendbc/safety/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ typedef enum {
GM_CAM
} GmHardware;
static GmHardware gm_hw = GM_ASCM;
static bool gm_cam_long = false;
static bool gm_pcm_cruise = false;

static void gm_rx_hook(const CANPacket_t *to_push) {
Expand Down Expand Up @@ -159,32 +158,6 @@ static bool gm_tx_hook(const CANPacket_t *to_send) {
return tx;
}

static int gm_fwd_hook(int bus_num, int addr) {
int bus_fwd = -1;

if (gm_hw == GM_CAM) {
if (bus_num == 0) {
// block PSCMStatus; forwarded through openpilot to hide an alert from the camera
bool is_pscm_msg = (addr == 0x184);
if (!is_pscm_msg) {
bus_fwd = 2;
}
}

if (bus_num == 2) {
// block lkas message and acc messages if gm_cam_long, forward all others
bool is_lkas_msg = (addr == 0x180);
bool is_acc_msg = (addr == 0x315) || (addr == 0x2CB) || (addr == 0x370);
bool block_msg = is_lkas_msg || (is_acc_msg && gm_cam_long);
if (!block_msg) {
bus_fwd = 0;
}
}
}

return bus_fwd;
}

static safety_config gm_init(uint16_t param) {
const uint16_t GM_PARAM_HW_CAM = 1;

Expand All @@ -207,8 +180,9 @@ static safety_config gm_init(uint16_t param) {
.max_brake = 400,
};

static const CanMsg GM_CAM_LONG_TX_MSGS[] = {{0x180, 0, 4}, {0x315, 0, 5}, {0x2CB, 0, 8}, {0x370, 0, 6}, // pt bus
{0x184, 2, 8}}; // camera bus
// block PSCMStatus; forwarded through openpilot to hide an alert from the camera
static const CanMsg GM_CAM_LONG_TX_MSGS[] = {{0x180, 0, 4, .blocked = true}, {0x315, 0, 5, .blocked = true}, {0x2CB, 0, 8, .blocked = true}, {0x370, 0, 6, .blocked = true}, // pt bus
{0x184, 2, 8, .blocked = true}}; // camera bus


// TODO: do checksum and counter checks. Add correct timestep, 0.1s for now.
Expand All @@ -223,8 +197,9 @@ static safety_config gm_init(uint16_t param) {
{.msg = {{0xC9, 0, 8, .ignore_checksum = true, .ignore_counter = true, .frequency = 10U}, { 0 }, { 0 }}},
};

static const CanMsg GM_CAM_TX_MSGS[] = {{0x180, 0, 4}, // pt bus
{0x1E1, 2, 7}, {0x184, 2, 8}}; // camera bus
// block PSCMStatus; forwarded through openpilot to hide an alert from the camera
static const CanMsg GM_CAM_TX_MSGS[] = {{0x180, 0, 4, .blocked = true}, // pt bus
{0x1E1, 2, 7}, {0x184, 2, 8, .blocked = true}}; // camera bus

gm_hw = GET_FLAG(param, GM_PARAM_HW_CAM) ? GM_CAM : GM_ASCM;

Expand All @@ -235,18 +210,22 @@ static safety_config gm_init(uint16_t param) {
} else {
}

bool gm_cam_long = false;
#ifdef ALLOW_DEBUG
const uint16_t GM_PARAM_HW_CAM_LONG = 2;
gm_cam_long = GET_FLAG(param, GM_PARAM_HW_CAM_LONG);
#endif
gm_pcm_cruise = (gm_hw == GM_CAM) && !gm_cam_long;

safety_config ret = BUILD_SAFETY_CFG(gm_rx_checks, GM_ASCM_TX_MSGS);
safety_config ret;
if (gm_hw == GM_CAM) {
// FIXME: cppcheck thinks that gm_cam_long is always false. This is not true
// if ALLOW_DEBUG is defined but cppcheck is run without ALLOW_DEBUG
// cppcheck-suppress knownConditionTrueFalse
ret = gm_cam_long ? BUILD_SAFETY_CFG(gm_rx_checks, GM_CAM_LONG_TX_MSGS) : BUILD_SAFETY_CFG(gm_rx_checks, GM_CAM_TX_MSGS);
} else {
ret = BUILD_SAFETY_CFG(gm_rx_checks, GM_ASCM_TX_MSGS);
ret.block_fwding = true;
}
return ret;
}
Expand All @@ -255,5 +234,4 @@ const safety_hooks gm_hooks = {
.init = gm_init,
.rx = gm_rx_hook,
.tx = gm_tx_hook,
.fwd = gm_fwd_hook,
};
Loading
Loading