diff --git a/board/safety/safety_cadillac.h b/board/safety/safety_cadillac.h index cf6de2b..822e0e7 100644 --- a/board/safety/safety_cadillac.h +++ b/board/safety/safety_cadillac.h @@ -44,7 +44,8 @@ static void cadillac_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { int cruise_engaged = to_push->RDLR & 0x800000; // bit 23 if (cruise_engaged && !cadillac_cruise_engaged_last) { controls_allowed = 1; - } else if (!cruise_engaged) { + } + if (!cruise_engaged) { controls_allowed = 0; } cadillac_cruise_engaged_last = cruise_engaged; diff --git a/board/safety/safety_chrysler.h b/board/safety/safety_chrysler.h index 5807596..dda50d5 100644 --- a/board/safety/safety_chrysler.h +++ b/board/safety/safety_chrysler.h @@ -30,7 +30,8 @@ static void chrysler_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { int cruise_engaged = ((to_push->RDLR & 0x380000) >> 19) == 7; if (cruise_engaged && !chrysler_cruise_engaged_last) { controls_allowed = 1; - } else if (!cruise_engaged) { + } + if (!cruise_engaged) { controls_allowed = 0; } chrysler_cruise_engaged_last = cruise_engaged; diff --git a/board/safety/safety_ford.h b/board/safety/safety_ford.h index 00ab6b3..21c9c54 100644 --- a/board/safety/safety_ford.h +++ b/board/safety/safety_ford.h @@ -27,7 +27,8 @@ static void ford_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { bool set_or_resume = (to_push->RDLR >> 28) & 0x3; if (cancel) { controls_allowed = 0; - } else if (set_or_resume) { + } + if (set_or_resume) { controls_allowed = 1; } } @@ -69,9 +70,7 @@ static int ford_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) { // STEER: safety check if (addr == 0x3CA) { - if (current_controls_allowed) { - // all messages are fine here - } else { + if (!current_controls_allowed) { // bits 7-4 need to be 0xF to disallow lkas commands if (((to_send->RDLR >> 4) & 0xF) != 0xF) { tx = 0; diff --git a/board/safety/safety_gm.h b/board/safety/safety_gm.h index 00f01b4..7156750 100644 --- a/board/safety/safety_gm.h +++ b/board/safety/safety_gm.h @@ -65,12 +65,17 @@ static void gm_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { // ACC steering wheel buttons if (addr == 481) { - int buttons = (to_push->RDHR >> 12) & 0x7; - // res/set - enable, cancel button - disable - if ((buttons == 2) || (buttons == 3)) { - controls_allowed = 1; - } else if (buttons == 6) { - controls_allowed = 0; + int button = (to_push->RDHR >> 12) & 0x7; + switch (button) { + case 2: // resume + case 3: // set + controls_allowed = 1; + break; + case 6: // cancel + controls_allowed = 0; + break; + default: + break; // any other button is irrelevant } } @@ -134,15 +139,14 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) { uint32_t rdlr = to_send->RDLR; int brake = ((rdlr & 0xFU) << 8) + ((rdlr & 0xFF00U) >> 8); brake = (0x1000 - brake) & 0xFFF; - if (current_controls_allowed && long_controls_allowed) { - if (brake > GM_MAX_BRAKE) { - tx = 0; - } - } else { + if (!current_controls_allowed || !long_controls_allowed) { if (brake != 0) { tx = 0; } } + if (brake > GM_MAX_BRAKE) { + tx = 0; + } } // LKA STEER: safety check @@ -203,18 +207,17 @@ static int gm_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) { if (addr == 715) { uint32_t rdlr = to_send->RDLR; int gas_regen = ((rdlr & 0x7F0000U) >> 11) + ((rdlr & 0xF8000000U) >> 27); - bool apply = (rdlr & 1U) != 0U; - if (current_controls_allowed && long_controls_allowed) { - if (gas_regen > GM_MAX_GAS) { - tx = 0; - } - } else { - // Disabled message is !engaed with gas - // value that corresponds to max regen. + // Disabled message is !engaed with gas + // value that corresponds to max regen. + if (!current_controls_allowed || !long_controls_allowed) { + bool apply = (rdlr & 1U) != 0U; if (apply || (gas_regen != GM_MAX_REGEN)) { tx = 0; } } + if (gas_regen > GM_MAX_GAS) { + tx = 0; + } } // 1 allows the message through diff --git a/board/safety/safety_gm_ascm.h b/board/safety/safety_gm_ascm.h index 4b436e1..d452818 100644 --- a/board/safety/safety_gm_ascm.h +++ b/board/safety/safety_gm_ascm.h @@ -17,7 +17,8 @@ static int gm_ascm_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { if (!supercruise_on) { bus_fwd = -1; } - } else if ((addr == 0x151) || (addr == 0x153) || (addr == 0x314)) { + } + if ((addr == 0x151) || (addr == 0x153) || (addr == 0x314)) { // on the chassis bus, the OBDII port is on the module side, so we need to read // the lkas messages sent by openpilot (put on unused 0x151 ane 0x153 addrs) and send it to // the actuator as 0x152 and 0x154 diff --git a/board/safety/safety_honda.h b/board/safety/safety_honda.h index 988b381..5120488 100644 --- a/board/safety/safety_honda.h +++ b/board/safety/safety_honda.h @@ -28,11 +28,17 @@ static void honda_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { // state machine to enter and exit controls // 0x1A6 for the ILX, 0x296 for the Civic Touring if ((addr == 0x1A6) || (addr == 0x296)) { - int buttons = (to_push->RDLR & 0xE0) >> 5; - if ((buttons == 4) || (buttons == 3)) { - controls_allowed = 1; - } else if (buttons == 2) { - controls_allowed = 0; + int button = (to_push->RDLR & 0xE0) >> 5; + switch (button) { + case 2: // cancel + controls_allowed = 0; + break; + case 3: // set + case 4: // resume + controls_allowed = 1; + break; + default: + break; // any other button is irrelevant } } @@ -100,22 +106,19 @@ static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) { // BRAKE: safety check if (addr == 0x1FA) { - if (current_controls_allowed && long_controls_allowed) { - if ((to_send->RDLR & 0xFFFFFF3F) != to_send->RDLR) { - tx = 0; - } - } else { + if (!current_controls_allowed || !long_controls_allowed) { if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) { tx = 0; } } + if ((to_send->RDLR & 0xFFFFFF3F) != to_send->RDLR) { + tx = 0; + } } // STEER: safety check if ((addr == 0xE4) || (addr == 0x194)) { - if (current_controls_allowed) { - // all messages are fine here - } else { + if (!current_controls_allowed) { if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) { tx = 0; } @@ -124,9 +127,7 @@ static int honda_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) { // GAS: safety check if (addr == 0x200) { - if (current_controls_allowed && long_controls_allowed) { - // all messages are fine here - } else { + if (!current_controls_allowed || !long_controls_allowed) { if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) { tx = 0; } @@ -169,7 +170,8 @@ static int honda_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { if (bus_num == 0) { bus_fwd = 2; - } else if (bus_num == 2) { + } + if (bus_num == 2) { // block stock lkas messages and stock acc messages (if OP is doing ACC) int addr = GET_ADDR(to_fwd); int is_lkas_msg = (addr == 0xE4) || (addr == 0x194) || (addr == 0x33D); @@ -187,7 +189,8 @@ static int honda_bosch_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { if (bus_num == 2) { bus_fwd = 1; - } else if (bus_num == 1) { + } + if (bus_num == 1) { int addr = GET_ADDR(to_fwd); int is_lkas_msg = (addr == 0xE4) || (addr == 0x33D); if (!is_lkas_msg) { diff --git a/board/safety/safety_hyundai.h b/board/safety/safety_hyundai.h index f98fe07..6a56f11 100644 --- a/board/safety/safety_hyundai.h +++ b/board/safety/safety_hyundai.h @@ -42,7 +42,8 @@ static void hyundai_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { int cruise_engaged = (to_push->RDLR >> 13) & 0x3; if (cruise_engaged && !hyundai_cruise_engaged_last) { controls_allowed = 1; - } else if (!cruise_engaged) { + } + if (!cruise_engaged) { controls_allowed = 0; } hyundai_cruise_engaged_last = cruise_engaged; @@ -132,7 +133,8 @@ static int hyundai_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { if (hyundai_giraffe_switch_2) { if (bus_num == 0) { bus_fwd = hyundai_camera_bus; - } else if (bus_num == hyundai_camera_bus) { + } + if (bus_num == hyundai_camera_bus) { int addr = GET_ADDR(to_fwd); if (addr != 832) { bus_fwd = 0; diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index ba05178..c7a8c20 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -31,7 +31,8 @@ static void subaru_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { int cruise_engaged = (to_push->RDHR >> 9) & 1; if (cruise_engaged && !subaru_cruise_engaged_last) { controls_allowed = 1; - } else if (!cruise_engaged) { + } + if (!cruise_engaged) { controls_allowed = 0; } subaru_cruise_engaged_last = cruise_engaged; @@ -99,7 +100,8 @@ static int subaru_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { int bus_fwd = -1; if (bus_num == 0) { bus_fwd = 2; // Camera CAN - } else if (bus_num == 2) { + } + if (bus_num == 2) { // 356 is LKAS for outback 2015 // 356 is LKAS for Global Platform // 545 is ES_Distance diff --git a/board/safety/safety_tesla.h b/board/safety/safety_tesla.h index 7fe359e..2368026 100644 --- a/board/safety/safety_tesla.h +++ b/board/safety/safety_tesla.h @@ -59,7 +59,8 @@ static void tesla_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { if (lever_position == 2) { // pull forward // activate openpilot controls_allowed = 1; - } else if (lever_position == 1) { // push towards the back + } + if (lever_position == 1) { // push towards the back // deactivate openpilot controls_allowed = 0; } diff --git a/board/safety/safety_toyota.h b/board/safety/safety_toyota.h index 21e05ee..d9e1327 100644 --- a/board/safety/safety_toyota.h +++ b/board/safety/safety_toyota.h @@ -59,7 +59,8 @@ static void toyota_rx_hook(CAN_FIFOMailBox_TypeDef *to_push) { int cruise_engaged = to_push->RDLR & 0x20; if (!cruise_engaged) { controls_allowed = 0; - } else if (cruise_engaged && !toyota_cruise_engaged_last) { + } + if (cruise_engaged && !toyota_cruise_engaged_last) { controls_allowed = 1; } toyota_cruise_engaged_last = cruise_engaged; @@ -113,9 +114,7 @@ static int toyota_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) { // GAS PEDAL: safety check if (addr == 0x200) { - if (controls_allowed && long_controls_allowed) { - // all messages are fine here - } else { + if (!controls_allowed || !long_controls_allowed) { if ((to_send->RDLR & 0xFFFF0000) != to_send->RDLR) { tx = 0; } @@ -126,12 +125,13 @@ static int toyota_tx_hook(CAN_FIFOMailBox_TypeDef *to_send) { if (addr == 0x343) { int desired_accel = ((to_send->RDLR & 0xFF) << 8) | ((to_send->RDLR >> 8) & 0xFF); desired_accel = to_signed(desired_accel, 16); - if (controls_allowed && long_controls_allowed) { - bool violation = max_limit_check(desired_accel, TOYOTA_MAX_ACCEL, TOYOTA_MIN_ACCEL); - if (violation) { + if (!controls_allowed || !long_controls_allowed) { + if (desired_accel != 0) { tx = 0; } - } else if (desired_accel != 0) { + } + bool violation = max_limit_check(desired_accel, TOYOTA_MAX_ACCEL, TOYOTA_MIN_ACCEL); + if (violation) { tx = 0; } } @@ -202,7 +202,8 @@ static int toyota_fwd_hook(int bus_num, CAN_FIFOMailBox_TypeDef *to_fwd) { if (toyota_camera_forwarded && !toyota_giraffe_switch_1) { if (bus_num == 0) { bus_fwd = 2; - } else if (bus_num == 2) { + } + if (bus_num == 2) { int addr = GET_ADDR(to_fwd); // block stock lkas messages and stock acc messages (if OP is doing ACC) int is_lkas_msg = ((addr == 0x2E4) || (addr == 0x412));