diff --git a/board/safety.h b/board/safety.h index e3d9a668df..f78a84036e 100644 --- a/board/safety.h +++ b/board/safety.h @@ -59,14 +59,20 @@ const addr_checks *current_rx_checks = &default_rx_checks; bool safety_rx_hook(CANPacket_t *to_push) { bool controls_allowed_prev = controls_allowed; - int ret = current_hooks->rx(to_push); + + bool valid = addr_safety_check(to_push, current_rx_checks, current_hooks->get_checksum, + current_hooks->compute_checksum, current_hooks->get_counter, + current_hooks->get_quality_flag_valid); + if (valid) { + current_hooks->rx(to_push); + } // reset mismatches on rising edge of controls_allowed to avoid rare race condition if (controls_allowed && !controls_allowed_prev) { heartbeat_engaged_mismatches = 0; } - return ret; + return valid; } bool safety_tx_hook(CANPacket_t *to_send) { @@ -216,10 +222,10 @@ void update_addr_timestamp(AddrCheckStruct addr_list[], int index) { bool addr_safety_check(CANPacket_t *to_push, const addr_checks *rx_checks, - uint32_t (*get_checksum)(CANPacket_t *to_push), - uint32_t (*compute_checksum)(CANPacket_t *to_push), - uint8_t (*get_counter)(CANPacket_t *to_push), - bool (*get_quality_flag_valid)(CANPacket_t *to_push)) { + const get_checksum_t get_checksum, + const compute_checksum_t compute_checksum, + const get_counter_t get_counter, + const get_quality_flag_valid_t get_quality_flag_valid) { int index = get_addr_check_index(to_push, rx_checks->check, rx_checks->len); update_addr_timestamp(rx_checks->check, index); diff --git a/board/safety/safety_body.h b/board/safety/safety_body.h index c6ffdc95be..3b9c738909 100644 --- a/board/safety/safety_body.h +++ b/board/safety/safety_body.h @@ -6,18 +6,14 @@ AddrCheckStruct body_addr_checks[] = { }; addr_checks body_rx_checks = SET_ADDR_CHECKS(body_addr_checks); -static bool body_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &body_rx_checks, NULL, NULL, NULL, NULL); - +static void body_rx_hook(CANPacket_t *to_push) { // body is never at standstill vehicle_moving = true; - if (valid && (GET_ADDR(to_push) == 0x201U)) { + if (GET_ADDR(to_push) == 0x201U) { controls_allowed = true; } - return valid; } static bool body_tx_hook(CANPacket_t *to_send) { diff --git a/board/safety/safety_chrysler.h b/board/safety/safety_chrysler.h index 194e9aca9f..f22f479d27 100644 --- a/board/safety/safety_chrysler.h +++ b/board/safety/safety_chrysler.h @@ -173,54 +173,45 @@ static uint8_t chrysler_get_counter(CANPacket_t *to_push) { return (uint8_t)(GET_BYTE(to_push, 6) >> 4); } -static bool chrysler_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &chrysler_rx_checks, - chrysler_get_checksum, chrysler_compute_checksum, - chrysler_get_counter, NULL); - +static void chrysler_rx_hook(CANPacket_t *to_push) { const int bus = GET_BUS(to_push); const int addr = GET_ADDR(to_push); - if (valid) { - - // Measured EPS torque - if ((bus == 0) && (addr == chrysler_addrs->EPS_2)) { - int torque_meas_new = ((GET_BYTE(to_push, 4) & 0x7U) << 8) + GET_BYTE(to_push, 5) - 1024U; - update_sample(&torque_meas, torque_meas_new); - } - - // enter controls on rising edge of ACC, exit controls on ACC off - const int das_3_bus = (chrysler_platform == CHRYSLER_PACIFICA) ? 0 : 2; - if ((bus == das_3_bus) && (addr == chrysler_addrs->DAS_3)) { - bool cruise_engaged = GET_BIT(to_push, 21U) == 1U; - pcm_cruise_check(cruise_engaged); - } + // Measured EPS torque + if ((bus == 0) && (addr == chrysler_addrs->EPS_2)) { + int torque_meas_new = ((GET_BYTE(to_push, 4) & 0x7U) << 8) + GET_BYTE(to_push, 5) - 1024U; + update_sample(&torque_meas, torque_meas_new); + } - // TODO: use the same message for both - // update vehicle moving - if ((chrysler_platform != CHRYSLER_PACIFICA) && (bus == 0) && (addr == chrysler_addrs->ESP_8)) { - vehicle_moving = ((GET_BYTE(to_push, 4) << 8) + GET_BYTE(to_push, 5)) != 0U; - } - if ((chrysler_platform == CHRYSLER_PACIFICA) && (bus == 0) && (addr == 514)) { - int speed_l = (GET_BYTE(to_push, 0) << 4) + (GET_BYTE(to_push, 1) >> 4); - int speed_r = (GET_BYTE(to_push, 2) << 4) + (GET_BYTE(to_push, 3) >> 4); - vehicle_moving = (speed_l != 0) || (speed_r != 0); - } + // enter controls on rising edge of ACC, exit controls on ACC off + const int das_3_bus = (chrysler_platform == CHRYSLER_PACIFICA) ? 0 : 2; + if ((bus == das_3_bus) && (addr == chrysler_addrs->DAS_3)) { + bool cruise_engaged = GET_BIT(to_push, 21U) == 1U; + pcm_cruise_check(cruise_engaged); + } - // exit controls on rising edge of gas press - if ((bus == 0) && (addr == chrysler_addrs->ECM_5)) { - gas_pressed = GET_BYTE(to_push, 0U) != 0U; - } + // TODO: use the same message for both + // update vehicle moving + if ((chrysler_platform != CHRYSLER_PACIFICA) && (bus == 0) && (addr == chrysler_addrs->ESP_8)) { + vehicle_moving = ((GET_BYTE(to_push, 4) << 8) + GET_BYTE(to_push, 5)) != 0U; + } + if ((chrysler_platform == CHRYSLER_PACIFICA) && (bus == 0) && (addr == 514)) { + int speed_l = (GET_BYTE(to_push, 0) << 4) + (GET_BYTE(to_push, 1) >> 4); + int speed_r = (GET_BYTE(to_push, 2) << 4) + (GET_BYTE(to_push, 3) >> 4); + vehicle_moving = (speed_l != 0) || (speed_r != 0); + } - // exit controls on rising edge of brake press - if ((bus == 0) && (addr == chrysler_addrs->ESP_1)) { - brake_pressed = ((GET_BYTE(to_push, 0U) & 0xFU) >> 2U) == 1U; - } + // exit controls on rising edge of gas press + if ((bus == 0) && (addr == chrysler_addrs->ECM_5)) { + gas_pressed = GET_BYTE(to_push, 0U) != 0U; + } - generic_rx_checks((bus == 0) && (addr == chrysler_addrs->LKAS_COMMAND)); + // exit controls on rising edge of brake press + if ((bus == 0) && (addr == chrysler_addrs->ESP_1)) { + brake_pressed = ((GET_BYTE(to_push, 0U) & 0xFU) >> 2U) == 1U; } - return valid; + + generic_rx_checks((bus == 0) && (addr == chrysler_addrs->LKAS_COMMAND)); } static bool chrysler_tx_hook(CANPacket_t *to_send) { @@ -307,4 +298,7 @@ const safety_hooks chrysler_hooks = { .tx = chrysler_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = chrysler_fwd_hook, + .get_counter = chrysler_get_counter, + .get_checksum = chrysler_get_checksum, + .compute_checksum = chrysler_compute_checksum, }; diff --git a/board/safety/safety_defaults.h b/board/safety/safety_defaults.h index 22da55c85d..94c9e6cbf5 100644 --- a/board/safety/safety_defaults.h +++ b/board/safety/safety_defaults.h @@ -3,9 +3,8 @@ const addr_checks default_rx_checks = { .len = 0, }; -bool default_rx_hook(CANPacket_t *to_push) { +void default_rx_hook(CANPacket_t *to_push) { UNUSED(to_push); - return true; } // *** no output safety mode *** diff --git a/board/safety/safety_ford.h b/board/safety/safety_ford.h index 868321bd65..6f34dfbe5d 100644 --- a/board/safety/safety_ford.h +++ b/board/safety/safety_ford.h @@ -206,11 +206,8 @@ const SteeringLimits FORD_STEERING_LIMITS = { .inactive_angle_is_zero = true, }; -static bool ford_rx_hook(CANPacket_t *to_push) { - bool valid = addr_safety_check(to_push, &ford_rx_checks, - ford_get_checksum, ford_compute_checksum, ford_get_counter, ford_get_quality_flag_valid); - - if (valid && (GET_BUS(to_push) == FORD_MAIN_BUS)) { +static void ford_rx_hook(CANPacket_t *to_push) { + if (GET_BUS(to_push) == FORD_MAIN_BUS) { int addr = GET_ADDR(to_push); // Update in motion state from standstill signal @@ -271,7 +268,6 @@ static bool ford_rx_hook(CANPacket_t *to_push) { generic_rx_checks(stock_ecu_detected); } - return valid; } static bool ford_tx_hook(CANPacket_t *to_send) { @@ -432,4 +428,8 @@ const safety_hooks ford_hooks = { .tx = ford_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = ford_fwd_hook, + .get_counter = ford_get_counter, + .get_checksum = ford_get_checksum, + .compute_checksum = ford_compute_checksum, + .get_quality_flag_valid = ford_get_quality_flag_valid, }; diff --git a/board/safety/safety_gm.h b/board/safety/safety_gm.h index 86596b43ba..cc1f1caf5c 100644 --- a/board/safety/safety_gm.h +++ b/board/safety/safety_gm.h @@ -65,11 +65,8 @@ enum {GM_ASCM, GM_CAM} gm_hw = GM_ASCM; bool gm_cam_long = false; bool gm_pcm_cruise = false; -static bool gm_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &gm_rx_checks, NULL, NULL, NULL, NULL); - - if (valid && (GET_BUS(to_push) == 0U)) { +static void gm_rx_hook(CANPacket_t *to_push) { + if (GET_BUS(to_push) == 0U) { int addr = GET_ADDR(to_push); if (addr == 0x184) { @@ -137,7 +134,6 @@ static bool gm_rx_hook(CANPacket_t *to_push) { } generic_rx_checks(stock_ecu_detected); } - return valid; } // all commands: gas/regen, friction brake and steering diff --git a/board/safety/safety_honda.h b/board/safety/safety_honda.h index 633e8da905..25156c3870 100644 --- a/board/safety/safety_honda.h +++ b/board/safety/safety_honda.h @@ -114,140 +114,134 @@ static uint8_t honda_get_counter(CANPacket_t *to_push) { return ((uint8_t)(GET_BYTE(to_push, counter_byte)) >> 4U) & 0x3U; } -static bool honda_rx_hook(CANPacket_t *to_push) { +static void honda_rx_hook(CANPacket_t *to_push) { + const bool pcm_cruise = ((honda_hw == HONDA_BOSCH) && !honda_bosch_long) || \ + ((honda_hw == HONDA_NIDEC) && !gas_interceptor_detected); + int pt_bus = honda_get_pt_bus(); - bool valid = addr_safety_check(to_push, &honda_rx_checks, - honda_get_checksum, honda_compute_checksum, honda_get_counter, NULL); - - if (valid) { - const bool pcm_cruise = ((honda_hw == HONDA_BOSCH) && !honda_bosch_long) || \ - ((honda_hw == HONDA_NIDEC) && !gas_interceptor_detected); - int pt_bus = honda_get_pt_bus(); + int addr = GET_ADDR(to_push); + int len = GET_LEN(to_push); + int bus = GET_BUS(to_push); - int addr = GET_ADDR(to_push); - int len = GET_LEN(to_push); - int bus = GET_BUS(to_push); + // sample speed + if (addr == 0x158) { + // first 2 bytes + vehicle_moving = GET_BYTE(to_push, 0) | GET_BYTE(to_push, 1); + } - // sample speed - if (addr == 0x158) { - // first 2 bytes - vehicle_moving = GET_BYTE(to_push, 0) | GET_BYTE(to_push, 1); + // check ACC main state + // 0x326 for all Bosch and some Nidec, 0x1A6 for some Nidec + if ((addr == 0x326) || (addr == 0x1A6)) { + acc_main_on = GET_BIT(to_push, ((addr == 0x326) ? 28U : 47U)); + if (!acc_main_on) { + controls_allowed = false; } + } - // check ACC main state - // 0x326 for all Bosch and some Nidec, 0x1A6 for some Nidec - if ((addr == 0x326) || (addr == 0x1A6)) { - acc_main_on = GET_BIT(to_push, ((addr == 0x326) ? 28U : 47U)); - if (!acc_main_on) { - controls_allowed = false; - } + // enter controls when PCM enters cruise state + if (pcm_cruise && (addr == 0x17C)) { + const bool cruise_engaged = GET_BIT(to_push, 38U) != 0U; + // engage on rising edge + if (cruise_engaged && !cruise_engaged_prev) { + controls_allowed = true; } - // enter controls when PCM enters cruise state - if (pcm_cruise && (addr == 0x17C)) { - const bool cruise_engaged = GET_BIT(to_push, 38U) != 0U; - // engage on rising edge - if (cruise_engaged && !cruise_engaged_prev) { - controls_allowed = true; - } - - // Since some Nidec cars can brake down to 0 after the PCM disengages, - // we don't disengage when the PCM does. - if (!cruise_engaged && (honda_hw != HONDA_NIDEC)) { - controls_allowed = false; - } - cruise_engaged_prev = cruise_engaged; + // Since some Nidec cars can brake down to 0 after the PCM disengages, + // we don't disengage when the PCM does. + if (!cruise_engaged && (honda_hw != HONDA_NIDEC)) { + controls_allowed = false; } + cruise_engaged_prev = cruise_engaged; + } - // state machine to enter and exit controls for button enabling - // 0x1A6 for the ILX, 0x296 for the Civic Touring - if (((addr == 0x1A6) || (addr == 0x296)) && (bus == pt_bus)) { - int button = (GET_BYTE(to_push, 0) & 0xE0U) >> 5; - - // enter controls on the falling edge of set or resume - bool set = (button != HONDA_BTN_SET) && (cruise_button_prev == HONDA_BTN_SET); - bool res = (button != HONDA_BTN_RESUME) && (cruise_button_prev == HONDA_BTN_RESUME); - if (acc_main_on && !pcm_cruise && (set || res)) { - controls_allowed = true; - } + // state machine to enter and exit controls for button enabling + // 0x1A6 for the ILX, 0x296 for the Civic Touring + if (((addr == 0x1A6) || (addr == 0x296)) && (bus == pt_bus)) { + int button = (GET_BYTE(to_push, 0) & 0xE0U) >> 5; - // exit controls once main or cancel are pressed - if ((button == HONDA_BTN_MAIN) || (button == HONDA_BTN_CANCEL)) { - controls_allowed = false; - } - cruise_button_prev = button; + // enter controls on the falling edge of set or resume + bool set = (button != HONDA_BTN_SET) && (cruise_button_prev == HONDA_BTN_SET); + bool res = (button != HONDA_BTN_RESUME) && (cruise_button_prev == HONDA_BTN_RESUME); + if (acc_main_on && !pcm_cruise && (set || res)) { + controls_allowed = true; } - // user brake signal on 0x17C reports applied brake from computer brake on accord - // and crv, which prevents the usual brake safety from working correctly. these - // cars have a signal on 0x1BE which only detects user's brake being applied so - // in these cases, this is used instead. - // most hondas: 0x17C - // accord, crv: 0x1BE - if (honda_alt_brake_msg) { - if (addr == 0x1BE) { - brake_pressed = GET_BIT(to_push, 4U) != 0U; - } - } else { - if (addr == 0x17C) { - // also if brake switch is 1 for two CAN frames, as brake pressed is delayed - const bool brake_switch = GET_BIT(to_push, 32U) != 0U; - brake_pressed = (GET_BIT(to_push, 53U) != 0U) || (brake_switch && honda_brake_switch_prev); - honda_brake_switch_prev = brake_switch; - } + // exit controls once main or cancel are pressed + if ((button == HONDA_BTN_MAIN) || (button == HONDA_BTN_CANCEL)) { + controls_allowed = false; } + cruise_button_prev = button; + } - // length check because bosch hardware also uses this id (0x201 w/ len = 8) - if ((addr == 0x201) && (len == 6)) { - gas_interceptor_detected = 1; - int gas_interceptor = HONDA_GET_INTERCEPTOR(to_push); - gas_pressed = gas_interceptor > HONDA_GAS_INTERCEPTOR_THRESHOLD; - gas_interceptor_prev = gas_interceptor; + // user brake signal on 0x17C reports applied brake from computer brake on accord + // and crv, which prevents the usual brake safety from working correctly. these + // cars have a signal on 0x1BE which only detects user's brake being applied so + // in these cases, this is used instead. + // most hondas: 0x17C + // accord, crv: 0x1BE + if (honda_alt_brake_msg) { + if (addr == 0x1BE) { + brake_pressed = GET_BIT(to_push, 4U) != 0U; } + } else { + if (addr == 0x17C) { + // also if brake switch is 1 for two CAN frames, as brake pressed is delayed + const bool brake_switch = GET_BIT(to_push, 32U) != 0U; + brake_pressed = (GET_BIT(to_push, 53U) != 0U) || (brake_switch && honda_brake_switch_prev); + honda_brake_switch_prev = brake_switch; + } + } - if (!gas_interceptor_detected) { - if (addr == 0x17C) { - gas_pressed = GET_BYTE(to_push, 0) != 0U; - } + // length check because bosch hardware also uses this id (0x201 w/ len = 8) + if ((addr == 0x201) && (len == 6)) { + gas_interceptor_detected = 1; + int gas_interceptor = HONDA_GET_INTERCEPTOR(to_push); + gas_pressed = gas_interceptor > HONDA_GAS_INTERCEPTOR_THRESHOLD; + gas_interceptor_prev = gas_interceptor; + } + + if (!gas_interceptor_detected) { + if (addr == 0x17C) { + gas_pressed = GET_BYTE(to_push, 0) != 0U; } + } - // disable stock Honda AEB in alternative experience - if (!(alternative_experience & ALT_EXP_DISABLE_STOCK_AEB)) { - if ((bus == 2) && (addr == 0x1FA)) { - bool honda_stock_aeb = GET_BIT(to_push, 29U) != 0U; - int honda_stock_brake = (GET_BYTE(to_push, 0) << 2) | (GET_BYTE(to_push, 1) >> 6); - - // Forward AEB when stock braking is higher than openpilot braking - // only stop forwarding when AEB event is over - if (!honda_stock_aeb) { - honda_fwd_brake = false; - } else if (honda_stock_brake >= honda_brake) { - honda_fwd_brake = true; - } else { - // Leave Honda forward brake as is - } + // disable stock Honda AEB in alternative experience + if (!(alternative_experience & ALT_EXP_DISABLE_STOCK_AEB)) { + if ((bus == 2) && (addr == 0x1FA)) { + bool honda_stock_aeb = GET_BIT(to_push, 29U) != 0U; + int honda_stock_brake = (GET_BYTE(to_push, 0) << 2) | (GET_BYTE(to_push, 1) >> 6); + + // Forward AEB when stock braking is higher than openpilot braking + // only stop forwarding when AEB event is over + if (!honda_stock_aeb) { + honda_fwd_brake = false; + } else if (honda_stock_brake >= honda_brake) { + honda_fwd_brake = true; + } else { + // Leave Honda forward brake as is } } + } - int bus_rdr_car = (honda_hw == HONDA_BOSCH) ? 0 : 2; // radar bus, car side - bool stock_ecu_detected = false; + int bus_rdr_car = (honda_hw == HONDA_BOSCH) ? 0 : 2; // radar bus, car side + bool stock_ecu_detected = false; - // If steering controls messages are received on the destination bus, it's an indication - // that the relay might be malfunctioning - if ((addr == 0xE4) || (addr == 0x194)) { - if (((honda_hw != HONDA_NIDEC) && (bus == bus_rdr_car)) || ((honda_hw == HONDA_NIDEC) && (bus == 0))) { - stock_ecu_detected = true; - } - } - // If Honda Bosch longitudinal mode is selected we need to ensure the radar is turned off - // Verify this by ensuring ACC_CONTROL (0x1DF) is not received on the PT bus - if (honda_bosch_long && !honda_bosch_radarless && (bus == pt_bus) && (addr == 0x1DF)) { + // If steering controls messages are received on the destination bus, it's an indication + // that the relay might be malfunctioning + if ((addr == 0xE4) || (addr == 0x194)) { + if (((honda_hw != HONDA_NIDEC) && (bus == bus_rdr_car)) || ((honda_hw == HONDA_NIDEC) && (bus == 0))) { stock_ecu_detected = true; } - - generic_rx_checks(stock_ecu_detected); } - return valid; + // If Honda Bosch longitudinal mode is selected we need to ensure the radar is turned off + // Verify this by ensuring ACC_CONTROL (0x1DF) is not received on the PT bus + if (honda_bosch_long && !honda_bosch_radarless && (bus == pt_bus) && (addr == 0x1DF)) { + stock_ecu_detected = true; + } + + generic_rx_checks(stock_ecu_detected); + } // all commands: gas, brake and steering @@ -456,6 +450,9 @@ const safety_hooks honda_nidec_hooks = { .tx = honda_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = honda_nidec_fwd_hook, + .get_counter = honda_get_counter, + .get_checksum = honda_get_checksum, + .compute_checksum = honda_compute_checksum, }; const safety_hooks honda_bosch_hooks = { @@ -464,4 +461,7 @@ const safety_hooks honda_bosch_hooks = { .tx = honda_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = honda_bosch_fwd_hook, + .get_counter = honda_get_counter, + .get_checksum = honda_get_checksum, + .compute_checksum = honda_compute_checksum, }; diff --git a/board/safety/safety_hyundai.h b/board/safety/safety_hyundai.h index 78d7fbae50..3ef9043289 100644 --- a/board/safety/safety_hyundai.h +++ b/board/safety/safety_hyundai.h @@ -158,23 +158,18 @@ static uint32_t hyundai_compute_checksum(CANPacket_t *to_push) { return chksum; } -static bool hyundai_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &hyundai_rx_checks, - hyundai_get_checksum, hyundai_compute_checksum, - hyundai_get_counter, NULL); - +static void hyundai_rx_hook(CANPacket_t *to_push) { int bus = GET_BUS(to_push); int addr = GET_ADDR(to_push); // SCC12 is on bus 2 for camera-based SCC cars, bus 0 on all others - if (valid && (addr == 0x421) && (((bus == 0) && !hyundai_camera_scc) || ((bus == 2) && hyundai_camera_scc))) { + if ((addr == 0x421) && (((bus == 0) && !hyundai_camera_scc) || ((bus == 2) && hyundai_camera_scc))) { // 2 bits: 13-14 int cruise_engaged = (GET_BYTES(to_push, 0, 4) >> 13) & 0x3U; hyundai_common_cruise_state_check(cruise_engaged); } - if (valid && (bus == 0)) { + if (bus == 0) { if (addr == 0x251) { int torque_driver_new = (GET_BYTES(to_push, 0, 2) & 0x7ffU) - 1024U; // update array of samples @@ -218,7 +213,6 @@ static bool hyundai_rx_hook(CANPacket_t *to_push) { } generic_rx_checks(stock_ecu_detected); } - return valid; } static bool hyundai_tx_hook(CANPacket_t *to_send) { @@ -346,6 +340,9 @@ const safety_hooks hyundai_hooks = { .tx = hyundai_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = hyundai_fwd_hook, + .get_counter = hyundai_get_counter, + .get_checksum = hyundai_get_checksum, + .compute_checksum = hyundai_compute_checksum, }; const safety_hooks hyundai_legacy_hooks = { @@ -354,4 +351,7 @@ const safety_hooks hyundai_legacy_hooks = { .tx = hyundai_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = hyundai_fwd_hook, + .get_counter = hyundai_get_counter, + .get_checksum = hyundai_get_checksum, + .compute_checksum = hyundai_compute_checksum, }; diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index 97804709b8..82f9d33365 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -153,18 +153,14 @@ static uint32_t hyundai_canfd_get_checksum(CANPacket_t *to_push) { return chksum; } -static bool hyundai_canfd_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &hyundai_canfd_rx_checks, - hyundai_canfd_get_checksum, hyundai_common_canfd_compute_checksum, hyundai_canfd_get_counter, NULL); - +static void hyundai_canfd_rx_hook(CANPacket_t *to_push) { int bus = GET_BUS(to_push); int addr = GET_ADDR(to_push); const int pt_bus = hyundai_canfd_hda2 ? 1 : 0; const int scc_bus = hyundai_camera_scc ? 2 : pt_bus; - if (valid && (bus == pt_bus)) { + if (bus == pt_bus) { // driver torque if (addr == 0xea) { int torque_driver_new = ((GET_BYTE(to_push, 11) & 0x1fU) << 8U) | GET_BYTE(to_push, 10); @@ -210,7 +206,7 @@ static bool hyundai_canfd_rx_hook(CANPacket_t *to_push) { } } - if (valid && (bus == scc_bus)) { + if (bus == scc_bus) { // cruise state if ((addr == 0x1a0) && !hyundai_longitudinal) { // 1=enabled, 2=driver override @@ -230,7 +226,6 @@ static bool hyundai_canfd_rx_hook(CANPacket_t *to_push) { } generic_rx_checks(stock_ecu_detected); - return valid; } static bool hyundai_canfd_tx_hook(CANPacket_t *to_send) { @@ -369,4 +364,7 @@ const safety_hooks hyundai_canfd_hooks = { .tx = hyundai_canfd_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = hyundai_canfd_fwd_hook, + .get_counter = hyundai_canfd_get_counter, + .get_checksum = hyundai_canfd_get_checksum, + .compute_checksum = hyundai_common_canfd_compute_checksum, }; diff --git a/board/safety/safety_mazda.h b/board/safety/safety_mazda.h index 5854cd98a4..2e5016937d 100644 --- a/board/safety/safety_mazda.h +++ b/board/safety/safety_mazda.h @@ -35,9 +35,8 @@ AddrCheckStruct mazda_addr_checks[] = { addr_checks mazda_rx_checks = SET_ADDR_CHECKS(mazda_addr_checks); // track msgs coming from OP so that we know what CAM msgs to drop and what to forward -static bool mazda_rx_hook(CANPacket_t *to_push) { - bool valid = addr_safety_check(to_push, &mazda_rx_checks, NULL, NULL, NULL, NULL); - if (valid && ((int)GET_BUS(to_push) == MAZDA_MAIN)) { +static void mazda_rx_hook(CANPacket_t *to_push) { + if ((int)GET_BUS(to_push) == MAZDA_MAIN) { int addr = GET_ADDR(to_push); if (addr == MAZDA_ENGINE_DATA) { @@ -68,7 +67,6 @@ static bool mazda_rx_hook(CANPacket_t *to_push) { generic_rx_checks((addr == MAZDA_LKAS)); } - return valid; } static bool mazda_tx_hook(CANPacket_t *to_send) { diff --git a/board/safety/safety_nissan.h b/board/safety/safety_nissan.h index 50d3ff6e55..ad5a2730c3 100644 --- a/board/safety/safety_nissan.h +++ b/board/safety/safety_nissan.h @@ -41,62 +41,56 @@ const int NISSAN_PARAM_ALT_EPS_BUS = 1; bool nissan_alt_eps = false; -static bool nissan_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &nissan_rx_checks, NULL, NULL, NULL, NULL); - - if (valid) { - int bus = GET_BUS(to_push); - int addr = GET_ADDR(to_push); - - if (bus == (nissan_alt_eps ? 1 : 0)) { - if (addr == 0x2) { - // Current steering angle - // Factor -0.1, little endian - int angle_meas_new = (GET_BYTES(to_push, 0, 4) & 0xFFFFU); - // Multiply by -10 to match scale of LKAS angle - angle_meas_new = to_signed(angle_meas_new, 16) * -10; - - // update array of samples - update_sample(&angle_meas, angle_meas_new); - } - - if (addr == 0x285) { - // Get current speed and standstill - uint16_t right_rear = (GET_BYTE(to_push, 0) << 8) | (GET_BYTE(to_push, 1)); - uint16_t left_rear = (GET_BYTE(to_push, 2) << 8) | (GET_BYTE(to_push, 3)); - vehicle_moving = (right_rear | left_rear) != 0U; - update_sample(&vehicle_speed, ROUND((right_rear + left_rear) / 2.0 * 0.005 / 3.6 * VEHICLE_SPEED_FACTOR)); - } +static void nissan_rx_hook(CANPacket_t *to_push) { + int bus = GET_BUS(to_push); + int addr = GET_ADDR(to_push); + + if (bus == (nissan_alt_eps ? 1 : 0)) { + if (addr == 0x2) { + // Current steering angle + // Factor -0.1, little endian + int angle_meas_new = (GET_BYTES(to_push, 0, 4) & 0xFFFFU); + // Multiply by -10 to match scale of LKAS angle + angle_meas_new = to_signed(angle_meas_new, 16) * -10; + + // update array of samples + update_sample(&angle_meas, angle_meas_new); + } - // X-Trail 0x15c, Leaf 0x239 - if ((addr == 0x15c) || (addr == 0x239)) { - if (addr == 0x15c){ - gas_pressed = ((GET_BYTE(to_push, 5) << 2) | ((GET_BYTE(to_push, 6) >> 6) & 0x3U)) > 3U; - } else { - gas_pressed = GET_BYTE(to_push, 0) > 3U; - } - } + if (addr == 0x285) { + // Get current speed and standstill + uint16_t right_rear = (GET_BYTE(to_push, 0) << 8) | (GET_BYTE(to_push, 1)); + uint16_t left_rear = (GET_BYTE(to_push, 2) << 8) | (GET_BYTE(to_push, 3)); + vehicle_moving = (right_rear | left_rear) != 0U; + update_sample(&vehicle_speed, ROUND((right_rear + left_rear) / 2.0 * 0.005 / 3.6 * VEHICLE_SPEED_FACTOR)); } - // X-trail 0x454, Leaf 0x239 - if ((addr == 0x454) || (addr == 0x239)) { - if (addr == 0x454){ - brake_pressed = (GET_BYTE(to_push, 2) & 0x80U) != 0U; + // X-Trail 0x15c, Leaf 0x239 + if ((addr == 0x15c) || (addr == 0x239)) { + if (addr == 0x15c){ + gas_pressed = ((GET_BYTE(to_push, 5) << 2) | ((GET_BYTE(to_push, 6) >> 6) & 0x3U)) > 3U; } else { - brake_pressed = ((GET_BYTE(to_push, 4) >> 5) & 1U) != 0U; + gas_pressed = GET_BYTE(to_push, 0) > 3U; } } + } - // Handle cruise enabled - if ((addr == 0x30f) && (bus == (nissan_alt_eps ? 1 : 2))) { - bool cruise_engaged = (GET_BYTE(to_push, 0) >> 3) & 1U; - pcm_cruise_check(cruise_engaged); + // X-trail 0x454, Leaf 0x239 + if ((addr == 0x454) || (addr == 0x239)) { + if (addr == 0x454){ + brake_pressed = (GET_BYTE(to_push, 2) & 0x80U) != 0U; + } else { + brake_pressed = ((GET_BYTE(to_push, 4) >> 5) & 1U) != 0U; } + } - generic_rx_checks((addr == 0x169) && (bus == 0)); + // Handle cruise enabled + if ((addr == 0x30f) && (bus == (nissan_alt_eps ? 1 : 2))) { + bool cruise_engaged = (GET_BYTE(to_push, 0) >> 3) & 1U; + pcm_cruise_check(cruise_engaged); } - return valid; + + generic_rx_checks((addr == 0x169) && (bus == 0)); } diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index ad6bef21d3..18cfb1e36a 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -139,58 +139,51 @@ static uint32_t subaru_compute_checksum(CANPacket_t *to_push) { return checksum; } -static bool subaru_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &subaru_rx_checks, - subaru_get_checksum, subaru_compute_checksum, subaru_get_counter, NULL); - - if (valid) { - const int bus = GET_BUS(to_push); - const int alt_main_bus = subaru_gen2 ? SUBARU_ALT_BUS : SUBARU_MAIN_BUS; - - int addr = GET_ADDR(to_push); - if ((addr == MSG_SUBARU_Steering_Torque) && (bus == SUBARU_MAIN_BUS)) { - int torque_driver_new; - torque_driver_new = ((GET_BYTES(to_push, 0, 4) >> 16) & 0x7FFU); - torque_driver_new = -1 * to_signed(torque_driver_new, 11); - update_sample(&torque_driver, torque_driver_new); - - int angle_meas_new = (GET_BYTES(to_push, 4, 2) & 0xFFFFU); - // convert Steering_Torque -> Steering_Angle to centidegrees, to match the ES_LKAS_ANGLE angle request units - angle_meas_new = ROUND(to_signed(angle_meas_new, 16) * -2.17); - update_sample(&angle_meas, angle_meas_new); - } +static void subaru_rx_hook(CANPacket_t *to_push) { + const int bus = GET_BUS(to_push); + const int alt_main_bus = subaru_gen2 ? SUBARU_ALT_BUS : SUBARU_MAIN_BUS; - // enter controls on rising edge of ACC, exit controls on ACC off - if ((addr == MSG_SUBARU_CruiseControl) && (bus == alt_main_bus)) { - bool cruise_engaged = GET_BIT(to_push, 41U) != 0U; - pcm_cruise_check(cruise_engaged); - } + int addr = GET_ADDR(to_push); + if ((addr == MSG_SUBARU_Steering_Torque) && (bus == SUBARU_MAIN_BUS)) { + int torque_driver_new; + torque_driver_new = ((GET_BYTES(to_push, 0, 4) >> 16) & 0x7FFU); + torque_driver_new = -1 * to_signed(torque_driver_new, 11); + update_sample(&torque_driver, torque_driver_new); + + int angle_meas_new = (GET_BYTES(to_push, 4, 2) & 0xFFFFU); + // convert Steering_Torque -> Steering_Angle to centidegrees, to match the ES_LKAS_ANGLE angle request units + angle_meas_new = ROUND(to_signed(angle_meas_new, 16) * -2.17); + update_sample(&angle_meas, angle_meas_new); + } - // update vehicle moving with any non-zero wheel speed - if ((addr == MSG_SUBARU_Wheel_Speeds) && (bus == alt_main_bus)) { - uint32_t fr = (GET_BYTES(to_push, 1, 3) >> 4) & 0x1FFFU; - uint32_t rr = (GET_BYTES(to_push, 3, 3) >> 1) & 0x1FFFU; - uint32_t rl = (GET_BYTES(to_push, 4, 3) >> 6) & 0x1FFFU; - uint32_t fl = (GET_BYTES(to_push, 6, 2) >> 3) & 0x1FFFU; + // enter controls on rising edge of ACC, exit controls on ACC off + if ((addr == MSG_SUBARU_CruiseControl) && (bus == alt_main_bus)) { + bool cruise_engaged = GET_BIT(to_push, 41U) != 0U; + pcm_cruise_check(cruise_engaged); + } - vehicle_moving = (fr > 0U) || (rr > 0U) || (rl > 0U) || (fl > 0U); + // update vehicle moving with any non-zero wheel speed + if ((addr == MSG_SUBARU_Wheel_Speeds) && (bus == alt_main_bus)) { + uint32_t fr = (GET_BYTES(to_push, 1, 3) >> 4) & 0x1FFFU; + uint32_t rr = (GET_BYTES(to_push, 3, 3) >> 1) & 0x1FFFU; + uint32_t rl = (GET_BYTES(to_push, 4, 3) >> 6) & 0x1FFFU; + uint32_t fl = (GET_BYTES(to_push, 6, 2) >> 3) & 0x1FFFU; - float speed = (fr + rr + rl + fl) / 4U * 0.057; - update_sample(&vehicle_speed, ROUND(speed * VEHICLE_SPEED_FACTOR)); - } + vehicle_moving = (fr > 0U) || (rr > 0U) || (rl > 0U) || (fl > 0U); - if ((addr == MSG_SUBARU_Brake_Status) && (bus == alt_main_bus)) { - brake_pressed = GET_BIT(to_push, 62U) != 0U; - } + float speed = (fr + rr + rl + fl) / 4U * 0.057; + update_sample(&vehicle_speed, ROUND(speed * VEHICLE_SPEED_FACTOR)); + } - if ((addr == MSG_SUBARU_Throttle) && (bus == SUBARU_MAIN_BUS)) { - gas_pressed = GET_BYTE(to_push, 4) != 0U; - } + if ((addr == MSG_SUBARU_Brake_Status) && (bus == alt_main_bus)) { + brake_pressed = GET_BIT(to_push, 62U) != 0U; + } - generic_rx_checks((addr == MSG_SUBARU_ES_LKAS) && (bus == SUBARU_MAIN_BUS)); + if ((addr == MSG_SUBARU_Throttle) && (bus == SUBARU_MAIN_BUS)) { + gas_pressed = GET_BYTE(to_push, 4) != 0U; } - return valid; + + generic_rx_checks((addr == MSG_SUBARU_ES_LKAS) && (bus == SUBARU_MAIN_BUS)); } static bool subaru_tx_hook(CANPacket_t *to_send) { @@ -312,4 +305,7 @@ const safety_hooks subaru_hooks = { .tx = subaru_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = subaru_fwd_hook, + .get_counter = subaru_get_counter, + .get_checksum = subaru_get_checksum, + .compute_checksum = subaru_compute_checksum, }; diff --git a/board/safety/safety_subaru_preglobal.h b/board/safety/safety_subaru_preglobal.h index 8e9caf0dd0..e014f71f75 100644 --- a/board/safety/safety_subaru_preglobal.h +++ b/board/safety/safety_subaru_preglobal.h @@ -43,13 +43,10 @@ const int SUBARU_PG_PARAM_REVERSED_DRIVER_TORQUE = 1; bool subaru_pg_reversed_driver_torque = false; -static bool subaru_preglobal_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &subaru_preglobal_rx_checks, NULL, NULL, NULL, NULL); - +static void subaru_preglobal_rx_hook(CANPacket_t *to_push) { const int bus = GET_BUS(to_push); - if (valid && (bus == SUBARU_PG_MAIN_BUS)) { + if (bus == SUBARU_PG_MAIN_BUS) { int addr = GET_ADDR(to_push); if (addr == MSG_SUBARU_PG_Steering_Torque) { int torque_driver_new; @@ -80,7 +77,6 @@ static bool subaru_preglobal_rx_hook(CANPacket_t *to_push) { generic_rx_checks((addr == MSG_SUBARU_PG_ES_LKAS)); } - return valid; } static bool subaru_preglobal_tx_hook(CANPacket_t *to_send) { diff --git a/board/safety/safety_tesla.h b/board/safety/safety_tesla.h index 99ed67da8e..69b3e264d0 100644 --- a/board/safety/safety_tesla.h +++ b/board/safety/safety_tesla.h @@ -58,71 +58,65 @@ bool tesla_powertrain = false; // Are we the second panda intercepting the powe bool tesla_stock_aeb = false; -static bool tesla_rx_hook(CANPacket_t *to_push) { - bool valid = addr_safety_check(to_push, tesla_powertrain ? (&tesla_pt_rx_checks) : (&tesla_rx_checks), - NULL, NULL, NULL, NULL); - - if(valid) { - int bus = GET_BUS(to_push); - int addr = GET_ADDR(to_push); - - if(bus == 0) { - if (!tesla_powertrain) { - if(addr == 0x370) { - // Steering angle: (0.1 * val) - 819.2 in deg. - // Store it 1/10 deg to match steering request - int angle_meas_new = (((GET_BYTE(to_push, 4) & 0x3FU) << 8) | GET_BYTE(to_push, 5)) - 8192U; - update_sample(&angle_meas, angle_meas_new); - } - } - - if(addr == (tesla_powertrain ? 0x116 : 0x118)) { - // Vehicle speed: ((0.05 * val) - 25) * MPH_TO_MPS - float speed = (((((GET_BYTE(to_push, 3) & 0x0FU) << 8) | (GET_BYTE(to_push, 2))) * 0.05) - 25) * 0.447; - vehicle_moving = ABS(speed) > 0.1; - update_sample(&vehicle_speed, ROUND(speed * VEHICLE_SPEED_FACTOR)); +static void tesla_rx_hook(CANPacket_t *to_push) { + int bus = GET_BUS(to_push); + int addr = GET_ADDR(to_push); + + if(bus == 0) { + if (!tesla_powertrain) { + if(addr == 0x370) { + // Steering angle: (0.1 * val) - 819.2 in deg. + // Store it 1/10 deg to match steering request + int angle_meas_new = (((GET_BYTE(to_push, 4) & 0x3FU) << 8) | GET_BYTE(to_push, 5)) - 8192U; + update_sample(&angle_meas, angle_meas_new); } + } - if(addr == (tesla_powertrain ? 0x106 : 0x108)) { - // Gas pressed - gas_pressed = (GET_BYTE(to_push, 6) != 0U); - } + if(addr == (tesla_powertrain ? 0x116 : 0x118)) { + // Vehicle speed: ((0.05 * val) - 25) * MPH_TO_MPS + float speed = (((((GET_BYTE(to_push, 3) & 0x0FU) << 8) | (GET_BYTE(to_push, 2))) * 0.05) - 25) * 0.447; + vehicle_moving = ABS(speed) > 0.1; + update_sample(&vehicle_speed, ROUND(speed * VEHICLE_SPEED_FACTOR)); + } - if(addr == (tesla_powertrain ? 0x1f8 : 0x20a)) { - // Brake pressed - brake_pressed = (((GET_BYTE(to_push, 0) & 0x0CU) >> 2) != 1U); - } + if(addr == (tesla_powertrain ? 0x106 : 0x108)) { + // Gas pressed + gas_pressed = (GET_BYTE(to_push, 6) != 0U); + } - if(addr == (tesla_powertrain ? 0x256 : 0x368)) { - // Cruise state - int cruise_state = (GET_BYTE(to_push, 1) >> 4); - bool cruise_engaged = (cruise_state == 2) || // ENABLED - (cruise_state == 3) || // STANDSTILL - (cruise_state == 4) || // OVERRIDE - (cruise_state == 6) || // PRE_FAULT - (cruise_state == 7); // PRE_CANCEL - pcm_cruise_check(cruise_engaged); - } + if(addr == (tesla_powertrain ? 0x1f8 : 0x20a)) { + // Brake pressed + brake_pressed = (((GET_BYTE(to_push, 0) & 0x0CU) >> 2) != 1U); } - if (bus == 2) { - int das_control_addr = (tesla_powertrain ? 0x2bf : 0x2b9); - if (tesla_longitudinal && (addr == das_control_addr)) { - // "AEB_ACTIVE" - tesla_stock_aeb = ((GET_BYTE(to_push, 2) & 0x03U) == 1U); - } + if(addr == (tesla_powertrain ? 0x256 : 0x368)) { + // Cruise state + int cruise_state = (GET_BYTE(to_push, 1) >> 4); + bool cruise_engaged = (cruise_state == 2) || // ENABLED + (cruise_state == 3) || // STANDSTILL + (cruise_state == 4) || // OVERRIDE + (cruise_state == 6) || // PRE_FAULT + (cruise_state == 7); // PRE_CANCEL + pcm_cruise_check(cruise_engaged); } + } - if (tesla_powertrain) { - // 0x2bf: DAS_control should not be received on bus 0 - generic_rx_checks((addr == 0x2bf) && (bus == 0)); - } else { - // 0x488: DAS_steeringControl should not be received on bus 0 - generic_rx_checks((addr == 0x488) && (bus == 0)); + if (bus == 2) { + int das_control_addr = (tesla_powertrain ? 0x2bf : 0x2b9); + if (tesla_longitudinal && (addr == das_control_addr)) { + // "AEB_ACTIVE" + tesla_stock_aeb = ((GET_BYTE(to_push, 2) & 0x03U) == 1U); } } - return valid; + if (tesla_powertrain) { + // 0x2bf: DAS_control should not be received on bus 0 + generic_rx_checks((addr == 0x2bf) && (bus == 0)); + } else { + // 0x488: DAS_steeringControl should not be received on bus 0 + generic_rx_checks((addr == 0x488) && (bus == 0)); + } + } diff --git a/board/safety/safety_toyota.h b/board/safety/safety_toyota.h index e6756aaa75..1c4312ae99 100644 --- a/board/safety/safety_toyota.h +++ b/board/safety/safety_toyota.h @@ -69,12 +69,8 @@ static uint32_t toyota_get_checksum(CANPacket_t *to_push) { return (uint8_t)(GET_BYTE(to_push, checksum_byte)); } -static bool toyota_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &toyota_rx_checks, - toyota_get_checksum, toyota_compute_checksum, NULL, NULL); - - if (valid && (GET_BUS(to_push) == 0U)) { +static void toyota_rx_hook(CANPacket_t *to_push) { + if (GET_BUS(to_push) == 0U) { int addr = GET_ADDR(to_push); // get eps motor torque (0.66 factor in dbc) @@ -130,7 +126,6 @@ static bool toyota_rx_hook(CANPacket_t *to_push) { generic_rx_checks((addr == 0x2E4)); } - return valid; } static bool toyota_tx_hook(CANPacket_t *to_send) { @@ -262,4 +257,6 @@ const safety_hooks toyota_hooks = { .tx = toyota_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = toyota_fwd_hook, + .get_checksum = toyota_get_checksum, + .compute_checksum = toyota_compute_checksum, }; diff --git a/board/safety/safety_volkswagen_mqb.h b/board/safety/safety_volkswagen_mqb.h index d1f2b6b21d..4cd1b63170 100644 --- a/board/safety/safety_volkswagen_mqb.h +++ b/board/safety/safety_volkswagen_mqb.h @@ -105,12 +105,8 @@ static const addr_checks* volkswagen_mqb_init(uint16_t param) { return &volkswagen_mqb_rx_checks; } -static bool volkswagen_mqb_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &volkswagen_mqb_rx_checks, - volkswagen_mqb_get_checksum, volkswagen_mqb_compute_crc, volkswagen_mqb_get_counter, NULL); - - if (valid && (GET_BUS(to_push) == 0U)) { +static void volkswagen_mqb_rx_hook(CANPacket_t *to_push) { + if (GET_BUS(to_push) == 0U) { int addr = GET_ADDR(to_push); // Update in-motion state by sampling wheel speeds @@ -193,7 +189,6 @@ static bool volkswagen_mqb_rx_hook(CANPacket_t *to_push) { generic_rx_checks((addr == MSG_HCA_01)); } - return valid; } static bool volkswagen_mqb_tx_hook(CANPacket_t *to_send) { @@ -295,4 +290,7 @@ const safety_hooks volkswagen_mqb_hooks = { .tx = volkswagen_mqb_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = volkswagen_mqb_fwd_hook, + .get_counter = volkswagen_mqb_get_counter, + .get_checksum = volkswagen_mqb_get_checksum, + .compute_checksum = volkswagen_mqb_compute_crc, }; diff --git a/board/safety/safety_volkswagen_pq.h b/board/safety/safety_volkswagen_pq.h index e45b604cfb..6de21622a7 100644 --- a/board/safety/safety_volkswagen_pq.h +++ b/board/safety/safety_volkswagen_pq.h @@ -95,12 +95,8 @@ static const addr_checks* volkswagen_pq_init(uint16_t param) { return &volkswagen_pq_rx_checks; } -static bool volkswagen_pq_rx_hook(CANPacket_t *to_push) { - - bool valid = addr_safety_check(to_push, &volkswagen_pq_rx_checks, - volkswagen_pq_get_checksum, volkswagen_pq_compute_checksum, volkswagen_pq_get_counter, NULL); - - if (valid && (GET_BUS(to_push) == 0U)) { +static void volkswagen_pq_rx_hook(CANPacket_t *to_push) { + if (GET_BUS(to_push) == 0U) { int addr = GET_ADDR(to_push); // Update in-motion state from speed value. @@ -171,7 +167,6 @@ static bool volkswagen_pq_rx_hook(CANPacket_t *to_push) { generic_rx_checks((addr == MSG_HCA_1)); } - return valid; } static bool volkswagen_pq_tx_hook(CANPacket_t *to_send) { @@ -262,4 +257,7 @@ const safety_hooks volkswagen_pq_hooks = { .tx = volkswagen_pq_tx_hook, .tx_lin = nooutput_tx_lin_hook, .fwd = volkswagen_pq_fwd_hook, + .get_counter = volkswagen_pq_get_counter, + .get_checksum = volkswagen_pq_get_checksum, + .compute_checksum = volkswagen_pq_compute_checksum, }; diff --git a/board/safety_declarations.h b/board/safety_declarations.h index e3fc53a80d..902e64c478 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -132,6 +132,11 @@ typedef struct { int len; } addr_checks; +typedef uint32_t (*get_checksum_t)(CANPacket_t *to_push); +typedef uint32_t (*compute_checksum_t)(CANPacket_t *to_push); +typedef uint8_t (*get_counter_t)(CANPacket_t *to_push); +typedef bool (*get_quality_flag_valid_t)(CANPacket_t *to_push); + bool safety_rx_hook(CANPacket_t *to_push); bool safety_tx_hook(CANPacket_t *to_send); bool safety_tx_lin_hook(int lin_num, uint8_t *data, int len); @@ -160,10 +165,10 @@ void update_addr_timestamp(AddrCheckStruct addr_list[], int index); bool is_msg_valid(AddrCheckStruct addr_list[], int index); bool addr_safety_check(CANPacket_t *to_push, const addr_checks *rx_checks, - uint32_t (*get_checksum)(CANPacket_t *to_push), - uint32_t (*compute_checksum)(CANPacket_t *to_push), - uint8_t (*get_counter)(CANPacket_t *to_push), - bool (*get_quality_flag_valid)(CANPacket_t *to_push)); + const get_checksum_t get_checksum, + const compute_checksum_t compute_checksum, + const get_counter_t get_counter, + const get_quality_flag_valid_t get_quality_flag); void generic_rx_checks(bool stock_ecu_detected); void relay_malfunction_set(void); void relay_malfunction_reset(void); @@ -178,7 +183,7 @@ bool longitudinal_interceptor_checks(CANPacket_t *to_send); void pcm_cruise_check(bool cruise_engaged); typedef const addr_checks* (*safety_hook_init)(uint16_t param); -typedef bool (*rx_hook)(CANPacket_t *to_push); +typedef void (*rx_hook)(CANPacket_t *to_push); typedef bool (*tx_hook)(CANPacket_t *to_send); typedef bool (*tx_lin_hook)(int lin_num, uint8_t *data, int len); typedef int (*fwd_hook)(int bus_num, int addr); @@ -189,6 +194,10 @@ typedef struct { tx_hook tx; tx_lin_hook tx_lin; fwd_hook fwd; + get_checksum_t get_checksum; + compute_checksum_t compute_checksum; + get_counter_t get_counter; + get_quality_flag_valid_t get_quality_flag_valid; } safety_hooks; void safety_tick(const addr_checks *addr_checks);