Skip to content

safety: fwd hook config #1954

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

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
24 changes: 21 additions & 3 deletions opendbc/safety/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ bool safety_rx_hook(const CANPacket_t *to_push) {
const int addr = GET_ADDR(to_push);
for (int i = 0; i < current_safety_config.tx_msgs_len; i++) {
const CanMsg *m = &current_safety_config.tx_msgs[i];
if (m->check_relay) {
if (m->blocked) {
generic_rx_checks((m->addr == addr) && (m->bus == bus));
}
}
Expand Down Expand Up @@ -278,8 +278,24 @@ static int get_fwd_bus(int bus_num) {
}

int safety_fwd_hook(int bus_num, int addr) {
const bool blocked = relay_malfunction || current_hooks->fwd(bus_num, addr);
return blocked ? -1 : get_fwd_bus(bus_num);
const int destination_bus = get_fwd_bus(bus_num);

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

if (!blocked && (current_hooks->fwd != NULL)) {
blocked = current_hooks->fwd(bus_num, addr);
}

return blocked ? -1 : destination_bus;
}

bool get_longitudinal_allowed(void) {
Expand Down Expand Up @@ -457,6 +473,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.disable_forwarding = false;

int set_status = -1; // not set
int hook_config_count = sizeof(safety_hook_registry) / sizeof(safety_hook_config);
Expand All @@ -474,6 +491,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.disable_forwarding = cfg.disable_forwarding;
// 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, false}}; // 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.disable_forwarding = true;
return ret;
}

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

static bool chrysler_fwd_hook(int bus_num, int addr) {
bool block_msg = false;

// 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){
block_msg = true;
}

return block_msg;
}

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 @@ -224,13 +212,13 @@ static safety_config chrysler_init(uint16_t param) {
static const CanMsg CHRYSLER_TX_MSGS[] = {
{CHRYSLER_ADDRS.CRUISE_BUTTONS, 0, 3, false},
{CHRYSLER_ADDRS.LKAS_COMMAND, 0, 6, true},
{CHRYSLER_ADDRS.DAS_6, 0, 8, false},
{CHRYSLER_ADDRS.DAS_6, 0, 8, true},
};

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

#ifdef ALLOW_DEBUG
Expand All @@ -257,7 +245,7 @@ static safety_config chrysler_init(uint16_t param) {
static const CanMsg CHRYSLER_RAM_HD_TX_MSGS[] = {
{CHRYSLER_RAM_HD_ADDRS.CRUISE_BUTTONS, 2, 3, false},
{CHRYSLER_RAM_HD_ADDRS.LKAS_COMMAND, 0, 8, true},
{CHRYSLER_RAM_HD_ADDRS.DAS_6, 0, 8, false},
{CHRYSLER_RAM_HD_ADDRS.DAS_6, 0, 8, true},
};

const uint32_t CHRYSLER_PARAM_RAM_HD = 2U; // set for Ram HD platform
Expand Down Expand Up @@ -290,7 +278,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
25 changes: 4 additions & 21 deletions opendbc/safety/safety/safety_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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 @@ -24,46 +24,29 @@ static bool nooutput_tx_hook(const CANPacket_t *to_send) {
}
// GCOV_EXCL_STOP

static bool default_fwd_hook(int bus_num, int addr) {
UNUSED(bus_num);
UNUSED(addr);
return true;
}

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 bool alloutput_fwd_hook(int bus_num, int addr) {
UNUSED(bus_num);
UNUSED(addr);
return !alloutput_passthrough;
}

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,
};
32 changes: 0 additions & 32 deletions opendbc/safety/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ static bool ford_longitudinal = false;

#define FORD_MAX_SPEED_DELTA 2.0 // m/s

static bool ford_lkas_msg_check(int addr) {
return (addr == FORD_ACCDATA_3)
|| (addr == FORD_Lane_Assist_Data1)
|| ((addr == FORD_LateralMotionControl) && !ford_canfd)
|| ((addr == FORD_LateralMotionControl2) && ford_canfd)
|| (addr == FORD_IPMA_Data);
}

// Curvature rate limits
#define FORD_LIMITS(limit_lateral_acceleration) { \
.max_angle = 1000, /* 0.02 curvature */ \
Expand Down Expand Up @@ -311,29 +303,6 @@ static bool ford_tx_hook(const CANPacket_t *to_send) {
return tx;
}

static bool ford_fwd_hook(int bus_num, int addr) {
bool block_msg = false;

switch (bus_num) {
case FORD_CAM_BUS: {
if (ford_lkas_msg_check(addr)) {
// Block stock LKAS and UI messages
block_msg = true;
} else if (ford_longitudinal && (addr == FORD_ACCDATA)) {
// Block stock ACC message
block_msg = true;
} else {
}
break;
}
default: {
break;
}
}

return block_msg;
}

static safety_config ford_init(uint16_t param) {
// warning: quality flags are not yet checked in openpilot's CAN parser,
// this may be the cause of blocked messages
Expand Down Expand Up @@ -407,7 +376,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
40 changes: 10 additions & 30 deletions opendbc/safety/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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 @@ -160,31 +159,6 @@ static bool gm_tx_hook(const CANPacket_t *to_send) {
return tx;
}

static bool gm_fwd_hook(int bus_num, int addr) {
bool block_msg = false;

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) {
block_msg = true;
}
}

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);
block_msg = is_lkas_msg || (is_acc_msg && gm_cam_long);
}
} else {
block_msg = true;
}

return block_msg;
}

static safety_config gm_init(uint16_t param) {
const uint16_t GM_PARAM_HW_CAM = 1;
const uint16_t GM_PARAM_EV = 4;
Expand All @@ -208,8 +182,9 @@ static safety_config gm_init(uint16_t param) {
.max_brake = 400,
};

static const CanMsg GM_CAM_LONG_TX_MSGS[] = {{0x180, 0, 4, true}, {0x315, 0, 5, false}, {0x2CB, 0, 8, true}, {0x370, 0, 6, false}, // pt bus
{0x184, 2, 8, false}}; // 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, true}, {0x315, 0, 5, true}, {0x2CB, 0, 8, true}, {0x370, 0, 6, true}, // pt bus
{0x184, 2, 8, true}}; // camera bus


static RxCheck gm_rx_checks[] = {
Expand All @@ -221,8 +196,9 @@ static safety_config gm_init(uint16_t param) {
{.msg = {{0xBD, 0, 7, .ignore_checksum = true, .ignore_counter = true, .frequency = 40U}, { 0 }, { 0 }}},
};

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

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

Expand All @@ -233,6 +209,7 @@ 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);
Expand All @@ -253,12 +230,15 @@ static safety_config gm_init(uint16_t param) {
if (gm_ev) {
SET_RX_CHECKS(gm_ev_rx_checks, ret);
}

if (gm_hw == GM_ASCM) {
ret.disable_forwarding = true;
}
return ret;
}

const safety_hooks gm_hooks = {
.init = gm_init,
.rx = gm_rx_hook,
.tx = gm_tx_hook,
.fwd = gm_fwd_hook,
};
33 changes: 7 additions & 26 deletions opendbc/safety/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ const LongitudinalLimits HYUNDAI_LONG_LIMITS = {
#define HYUNDAI_COMMON_TX_MSGS(scc_bus) \
{0x340, 0, 8, true}, /* LKAS11 Bus 0 */ \
{0x4F1, scc_bus, 4, false}, /* CLU11 Bus 0 (radar-SCC) or 2 (camera-SCC) */ \
{0x485, 0, 4, false}, /* LFAHDA_MFC Bus 0 */ \
{0x485, 0, 4, true}, /* LFAHDA_MFC Bus 0 */ \

#define HYUNDAI_LONG_COMMON_TX_MSGS(scc_bus) \
HYUNDAI_COMMON_TX_MSGS(scc_bus) \
{0x420, 0, 8, false}, /* SCC11 Bus 0 */ \
{0x421, 0, 8, (scc_bus) == 0}, /* SCC12 Bus 0 */ \
{0x50A, 0, 8, false}, /* SCC13 Bus 0 */ \
{0x389, 0, 8, false}, /* SCC14 Bus 0 */ \
{0x4A2, 0, 2, false}, /* FRT_RADAR11 Bus 0 */ \
HYUNDAI_COMMON_TX_MSGS(scc_bus) \
{0x420, 0, 8, (scc_bus) == 2}, /* SCC11 Bus 0 */ \
{0x421, 0, 8, (scc_bus) == 2}, /* SCC12 Bus 0 */ \
{0x50A, 0, 8, (scc_bus) == 2}, /* SCC13 Bus 0 */ \
{0x389, 0, 8, (scc_bus) == 2}, /* SCC14 Bus 0 */ \
{0x4A2, 0, 2, false}, /* FRT_RADAR11 Bus 0 */ \

#define HYUNDAI_COMMON_RX_CHECKS(legacy) \
{.msg = {{0x260, 0, 8, .max_counter = 3U, .frequency = 100U}, \
Expand Down Expand Up @@ -256,23 +256,6 @@ static bool hyundai_tx_hook(const CANPacket_t *to_send) {
return tx;
}

static bool hyundai_fwd_hook(int bus_num, int addr) {
bool block_msg = false;

if (bus_num == 2) {
// Stock LKAS11 messages
bool is_lkas_11 = (addr == 0x340);
// LFA and HDA cluster icons
bool is_lfahda_mfc = (addr == 0x485);
// Stock SCC messages, blocking when doing openpilot longitudinal on camera SCC cars
bool is_scc_msg = (addr == 0x420) || (addr == 0x421) || (addr == 0x50A) || (addr == 0x389);

block_msg = is_lkas_11 || is_lfahda_mfc || (is_scc_msg && hyundai_longitudinal && hyundai_camera_scc);
}

return block_msg;
}

static safety_config hyundai_init(uint16_t param) {
static const CanMsg HYUNDAI_LONG_TX_MSGS[] = {
HYUNDAI_LONG_COMMON_TX_MSGS(0)
Expand Down Expand Up @@ -362,7 +345,6 @@ const safety_hooks hyundai_hooks = {
.init = hyundai_init,
.rx = hyundai_rx_hook,
.tx = hyundai_tx_hook,
.fwd = hyundai_fwd_hook,
.get_counter = hyundai_get_counter,
.get_checksum = hyundai_get_checksum,
.compute_checksum = hyundai_compute_checksum,
Expand All @@ -372,7 +354,6 @@ const safety_hooks hyundai_legacy_hooks = {
.init = hyundai_legacy_init,
.rx = hyundai_rx_hook,
.tx = hyundai_tx_hook,
.fwd = hyundai_fwd_hook,
.get_counter = hyundai_get_counter,
.get_checksum = hyundai_get_checksum,
.compute_checksum = hyundai_compute_checksum,
Expand Down
Loading
Loading