Skip to content

Commit

Permalink
Long allowed (#202)
Browse files Browse the repository at this point in the history
* added long_controls_allowed to cars safety code. long_controls_allowed init to 1 for now, so safety tests pass
  • Loading branch information
rbiasini authored Jun 5, 2019
1 parent 09714e3 commit 380b7c7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
6 changes: 6 additions & 0 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, int hardwired) {
can_init(CAN_NUM_FROM_BUS_NUM(setup->b.wValue.w));
}
break;
// **** 0xdf: set long controls allowed
case 0xdf:
if (hardwired) {
long_controls_allowed = setup->b.wValue.w & 1;
}
break;
// **** 0xe0: uart read
case 0xe0:
ur = get_ring_by_number(setup->b.wValue.w);
Expand Down
3 changes: 3 additions & 0 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ int controls_allowed = 0;
int gas_interceptor_detected = 0;
int gas_interceptor_prev = 0;

// This is set by USB command 0xdf
int long_controls_allowed = 1;

// Include the actual safety policies.
#include "safety/safety_defaults.h"
#include "safety/safety_honda.h"
Expand Down
6 changes: 3 additions & 3 deletions board/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static void gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
// exit controls on rising edge of gas press
if (addr == 417) {
int gas = to_push->RDHR & 0xFF0000;
if (gas && !gm_gas_prev) {
if (gas && !gm_gas_prev && long_controls_allowed) {
controls_allowed = 0;
}
gm_gas_prev = gas;
Expand Down Expand Up @@ -148,7 +148,7 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
int rdlr = to_send->RDLR;
int brake = ((rdlr & 0xF) << 8) + ((rdlr & 0xFF00) >> 8);
brake = (0x1000 - brake) & 0xFFF;
if (current_controls_allowed) {
if (current_controls_allowed && long_controls_allowed) {
if (brake > GM_MAX_BRAKE) return 0;
} else {
if (brake != 0) return 0;
Expand Down Expand Up @@ -212,7 +212,7 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
int rdlr = to_send->RDLR;
int gas_regen = ((rdlr & 0x7F0000) >> 11) + ((rdlr & 0xF8000000) >> 27);
int apply = rdlr & 1;
if (current_controls_allowed) {
if (current_controls_allowed && long_controls_allowed) {
if (gas_regen > GM_MAX_GAS) return 0;
} else {
// Disabled message is !engaed with gas
Expand Down
19 changes: 12 additions & 7 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ static void honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
gas_interceptor_detected = 1;
int gas_interceptor = ((to_push->RDLR & 0xFF) << 8) | ((to_push->RDLR & 0xFF00) >> 8);
if ((gas_interceptor > HONDA_GAS_INTERCEPTOR_THRESHOLD) &&
(gas_interceptor_prev <= HONDA_GAS_INTERCEPTOR_THRESHOLD)) {
(gas_interceptor_prev <= HONDA_GAS_INTERCEPTOR_THRESHOLD) &&
long_controls_allowed) {
controls_allowed = 0;
}
gas_interceptor_prev = gas_interceptor;
Expand All @@ -67,7 +68,7 @@ static void honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
if (!gas_interceptor_detected) {
if ((to_push->RIR>>21) == 0x17C) {
int gas = to_push->RDLR & 0xFF;
if (gas && !(honda_gas_prev)) {
if (gas && !(honda_gas_prev) && long_controls_allowed) {
controls_allowed = 0;
}
honda_gas_prev = gas;
Expand All @@ -91,7 +92,7 @@ static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {

// BRAKE: safety check
if ((to_send->RIR>>21) == 0x1FA) {
if (current_controls_allowed) {
if (current_controls_allowed && long_controls_allowed) {
if ((to_send->RDLR & 0xFFFFFF3F) != to_send->RDLR) return 0;
} else {
if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) return 0;
Expand All @@ -109,7 +110,7 @@ static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {

// GAS: safety check
if ((to_send->RIR>>21) == 0x200) {
if (current_controls_allowed) {
if (current_controls_allowed && long_controls_allowed) {
// all messages are fine here
} else {
if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) return 0;
Expand Down Expand Up @@ -149,11 +150,15 @@ static int honda_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {
int addr = to_fwd->RIR>>21;
if (bus_num == 0) {
return 2;
} else if (bus_num == 2 && addr != 0xE4 && addr != 0x194 && addr != 0x1FA &&
addr != 0x30C && addr != 0x33D && addr != 0x39F) {
} else if (bus_num == 2) {
// block stock lkas messages and stock acc messages (if OP is doing ACC)
int is_lkas_msg = (addr == 0xE4 || addr == 0x194 || addr == 0x33D);
int is_acc_msg = (addr == 0x1FA || addr == 0x30C || addr == 0x39F);
if (is_lkas_msg || (is_acc_msg && long_controls_allowed)) {
return -1;
}
return 0;
}

return -1;
}

Expand Down
30 changes: 19 additions & 11 deletions board/safety/safety_toyota.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ static void toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
int cruise_engaged = to_push->RDLR & 0x20;
// 4th bit is GAS_RELEASED
int gas = !(to_push->RDLR & 0x10);
if (!cruise_engaged || (gas && !toyota_gas_prev && !gas_interceptor_detected)) {
if (!cruise_engaged ||
(gas && !toyota_gas_prev && !gas_interceptor_detected && long_controls_allowed)) {
controls_allowed = 0;
} else if (cruise_engaged && !toyota_cruise_engaged_last) {
controls_allowed = 1;
Expand All @@ -69,7 +70,8 @@ static void toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) {
gas_interceptor_detected = 1;
int gas_interceptor = ((to_push->RDLR & 0xFF) << 8) | ((to_push->RDLR & 0xFF00) >> 8);
if ((gas_interceptor > TOYOTA_GAS_INTERCEPTOR_THRESHOLD) &&
(gas_interceptor_prev <= TOYOTA_GAS_INTERCEPTOR_THRESHOLD)) {
(gas_interceptor_prev <= TOYOTA_GAS_INTERCEPTOR_THRESHOLD) &&
long_controls_allowed) {
controls_allowed = 0;
}
gas_interceptor_prev = gas_interceptor;
Expand Down Expand Up @@ -97,7 +99,7 @@ static int toyota_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {

// GAS PEDAL: safety check
if ((to_send->RIR>>21) == 0x200) {
if (controls_allowed) {
if (controls_allowed && long_controls_allowed) {
// all messages are fine here
} else {
if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) return 0;
Expand All @@ -108,7 +110,7 @@ static int toyota_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) {
if ((to_send->RIR>>21) == 0x343) {
int desired_accel = ((to_send->RDLR & 0xFF) << 8) | ((to_send->RDLR >> 8) & 0xFF);
desired_accel = to_signed(desired_accel, 16);
if (controls_allowed) {
if (controls_allowed && long_controls_allowed) {
int violation = max_limit_check(desired_accel, TOYOTA_MAX_ACCEL, TOYOTA_MIN_ACCEL);
if (violation) return 0;
} else if (!controls_allowed && (desired_accel != 0)) {
Expand Down Expand Up @@ -178,14 +180,20 @@ static void toyota_init(int16_t param) {

static int toyota_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) {

// forward cam to radar and viceversa if car, except lkas cmd and hud
// don't forward when switch 1 is high
if ((bus_num == 0 || bus_num == 2) && toyota_camera_forwarded && !toyota_giraffe_switch_1) {
if (toyota_camera_forwarded && !toyota_giraffe_switch_1) {
int addr = to_fwd->RIR>>21;
bool is_lkas_msg = (addr == 0x2E4 || addr == 0x412) && bus_num == 2;
// in TSSP 2.0 the camera does ACC as well, so filter 0x343
bool is_acc_msg = (addr == 0x343 && bus_num == 2);
return (is_lkas_msg || is_acc_msg)? -1 : (uint8_t)(~bus_num & 0x2);
if (bus_num == 0) {
return 2;
} else if (bus_num == 2) {
// block stock lkas messages and stock acc messages (if OP is doing ACC)
int is_lkas_msg = (addr == 0x2E4 || addr == 0x412);
// in TSSP 2.0 the camera does ACC as well, so filter 0x343
int is_acc_msg = (addr == 0x343);
if (is_lkas_msg || (is_acc_msg && long_controls_allowed)) {
return -1;
}
return 0;
}
}
return -1;
}
Expand Down

0 comments on commit 380b7c7

Please sign in to comment.