From c6248d65046e77cf9bb6ddc626fdf86b3e8920cc Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Sun, 19 Nov 2023 16:47:09 -0800 Subject: [PATCH] safety: cleanup ints that are bools (#1726) * safety: cleanup ints that are bools * update tests --- board/safety.h | 9 +++++---- board/safety/safety_body.h | 4 ++-- board/safety/safety_chrysler.h | 4 ++-- board/safety/safety_defaults.h | 10 +++++----- board/safety/safety_elm327.h | 4 ++-- board/safety/safety_ford.h | 4 ++-- board/safety/safety_gm.h | 4 ++-- board/safety/safety_honda.h | 4 ++-- board/safety/safety_hyundai.h | 4 ++-- board/safety/safety_hyundai_canfd.h | 4 ++-- board/safety/safety_mazda.h | 4 ++-- board/safety/safety_nissan.h | 4 ++-- board/safety/safety_subaru.h | 4 ++-- board/safety/safety_subaru_preglobal.h | 4 ++-- board/safety/safety_tesla.h | 4 ++-- board/safety/safety_toyota.h | 4 ++-- board/safety/safety_volkswagen_mqb.h | 4 ++-- board/safety/safety_volkswagen_pq.h | 4 ++-- board/safety_declarations.h | 12 ++++++------ tests/libpanda/libpanda_py.py | 6 +++--- tests/safety/common.py | 2 +- 21 files changed, 52 insertions(+), 51 deletions(-) diff --git a/board/safety.h b/board/safety.h index a2a3e861d8..e3d9a668df 100644 --- a/board/safety.h +++ b/board/safety.h @@ -57,7 +57,7 @@ uint16_t current_safety_param = 0; const safety_hooks *current_hooks = &nooutput_hooks; const addr_checks *current_rx_checks = &default_rx_checks; -int safety_rx_hook(CANPacket_t *to_push) { +bool safety_rx_hook(CANPacket_t *to_push) { bool controls_allowed_prev = controls_allowed; int ret = current_hooks->rx(to_push); @@ -69,11 +69,12 @@ int safety_rx_hook(CANPacket_t *to_push) { return ret; } -int safety_tx_hook(CANPacket_t *to_send) { - return (relay_malfunction ? -1 : current_hooks->tx(to_send)); +bool safety_tx_hook(CANPacket_t *to_send) { + const bool safety_allowed = current_hooks->tx(to_send); + return !relay_malfunction && safety_allowed; } -int safety_tx_lin_hook(int lin_num, uint8_t *data, int len) { +bool safety_tx_lin_hook(int lin_num, uint8_t *data, int len) { return current_hooks->tx_lin(lin_num, data, len); } diff --git a/board/safety/safety_body.h b/board/safety/safety_body.h index df95535975..c6ffdc95be 100644 --- a/board/safety/safety_body.h +++ b/board/safety/safety_body.h @@ -6,7 +6,7 @@ AddrCheckStruct body_addr_checks[] = { }; addr_checks body_rx_checks = SET_ADDR_CHECKS(body_addr_checks); -static int body_rx_hook(CANPacket_t *to_push) { +static bool body_rx_hook(CANPacket_t *to_push) { bool valid = addr_safety_check(to_push, &body_rx_checks, NULL, NULL, NULL, NULL); @@ -20,7 +20,7 @@ static int body_rx_hook(CANPacket_t *to_push) { return valid; } -static int body_tx_hook(CANPacket_t *to_send) { +static bool body_tx_hook(CANPacket_t *to_send) { int tx = 0; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_chrysler.h b/board/safety/safety_chrysler.h index 954a7c0546..194e9aca9f 100644 --- a/board/safety/safety_chrysler.h +++ b/board/safety/safety_chrysler.h @@ -173,7 +173,7 @@ static uint8_t chrysler_get_counter(CANPacket_t *to_push) { return (uint8_t)(GET_BYTE(to_push, 6) >> 4); } -static int chrysler_rx_hook(CANPacket_t *to_push) { +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, @@ -223,7 +223,7 @@ static int chrysler_rx_hook(CANPacket_t *to_push) { return valid; } -static int chrysler_tx_hook(CANPacket_t *to_send) { +static bool chrysler_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_defaults.h b/board/safety/safety_defaults.h index 90c8a0655d..22da55c85d 100644 --- a/board/safety/safety_defaults.h +++ b/board/safety/safety_defaults.h @@ -3,7 +3,7 @@ const addr_checks default_rx_checks = { .len = 0, }; -int default_rx_hook(CANPacket_t *to_push) { +bool default_rx_hook(CANPacket_t *to_push) { UNUSED(to_push); return true; } @@ -15,12 +15,12 @@ static const addr_checks* nooutput_init(uint16_t param) { return &default_rx_checks; } -static int nooutput_tx_hook(CANPacket_t *to_send) { +static bool nooutput_tx_hook(CANPacket_t *to_send) { UNUSED(to_send); return false; } -static int nooutput_tx_lin_hook(int lin_num, uint8_t *data, int len) { +static bool nooutput_tx_lin_hook(int lin_num, uint8_t *data, int len) { UNUSED(lin_num); UNUSED(data); UNUSED(len); @@ -53,12 +53,12 @@ static const addr_checks* alloutput_init(uint16_t param) { return &default_rx_checks; } -static int alloutput_tx_hook(CANPacket_t *to_send) { +static bool alloutput_tx_hook(CANPacket_t *to_send) { UNUSED(to_send); return true; } -static int alloutput_tx_lin_hook(int lin_num, uint8_t *data, int len) { +static bool alloutput_tx_lin_hook(int lin_num, uint8_t *data, int len) { UNUSED(lin_num); UNUSED(data); UNUSED(len); diff --git a/board/safety/safety_elm327.h b/board/safety/safety_elm327.h index 6b1e72eac3..74b7798ba5 100644 --- a/board/safety/safety_elm327.h +++ b/board/safety/safety_elm327.h @@ -1,4 +1,4 @@ -static int elm327_tx_hook(CANPacket_t *to_send) { +static bool elm327_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); @@ -18,7 +18,7 @@ static int elm327_tx_hook(CANPacket_t *to_send) { return tx; } -static int elm327_tx_lin_hook(int lin_num, uint8_t *data, int len) { +static bool elm327_tx_lin_hook(int lin_num, uint8_t *data, int len) { int tx = 1; if (lin_num != 0) { tx = 0; //Only operate on LIN 0, aka serial 2 diff --git a/board/safety/safety_ford.h b/board/safety/safety_ford.h index a199ba7d4e..868321bd65 100644 --- a/board/safety/safety_ford.h +++ b/board/safety/safety_ford.h @@ -206,7 +206,7 @@ const SteeringLimits FORD_STEERING_LIMITS = { .inactive_angle_is_zero = true, }; -static int ford_rx_hook(CANPacket_t *to_push) { +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); @@ -274,7 +274,7 @@ static int ford_rx_hook(CANPacket_t *to_push) { return valid; } -static int ford_tx_hook(CANPacket_t *to_send) { +static bool ford_tx_hook(CANPacket_t *to_send) { int addr = GET_ADDR(to_send); int tx; if (ford_canfd) { diff --git a/board/safety/safety_gm.h b/board/safety/safety_gm.h index 527f539871..86596b43ba 100644 --- a/board/safety/safety_gm.h +++ b/board/safety/safety_gm.h @@ -65,7 +65,7 @@ enum {GM_ASCM, GM_CAM} gm_hw = GM_ASCM; bool gm_cam_long = false; bool gm_pcm_cruise = false; -static int gm_rx_hook(CANPacket_t *to_push) { +static bool gm_rx_hook(CANPacket_t *to_push) { bool valid = addr_safety_check(to_push, &gm_rx_checks, NULL, NULL, NULL, NULL); @@ -146,7 +146,7 @@ static int gm_rx_hook(CANPacket_t *to_push) { // else // block all commands that produce actuation -static int gm_tx_hook(CANPacket_t *to_send) { +static bool gm_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_honda.h b/board/safety/safety_honda.h index e86a34e7cc..633e8da905 100644 --- a/board/safety/safety_honda.h +++ b/board/safety/safety_honda.h @@ -114,7 +114,7 @@ static uint8_t honda_get_counter(CANPacket_t *to_push) { return ((uint8_t)(GET_BYTE(to_push, counter_byte)) >> 4U) & 0x3U; } -static int honda_rx_hook(CANPacket_t *to_push) { +static bool honda_rx_hook(CANPacket_t *to_push) { bool valid = addr_safety_check(to_push, &honda_rx_checks, honda_get_checksum, honda_compute_checksum, honda_get_counter, NULL); @@ -256,7 +256,7 @@ static int honda_rx_hook(CANPacket_t *to_push) { // else // block all commands that produce actuation -static int honda_tx_hook(CANPacket_t *to_send) { +static bool honda_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_hyundai.h b/board/safety/safety_hyundai.h index a357a2ab78..78d7fbae50 100644 --- a/board/safety/safety_hyundai.h +++ b/board/safety/safety_hyundai.h @@ -158,7 +158,7 @@ static uint32_t hyundai_compute_checksum(CANPacket_t *to_push) { return chksum; } -static int hyundai_rx_hook(CANPacket_t *to_push) { +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, @@ -221,7 +221,7 @@ static int hyundai_rx_hook(CANPacket_t *to_push) { return valid; } -static int hyundai_tx_hook(CANPacket_t *to_send) { +static bool hyundai_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_hyundai_canfd.h b/board/safety/safety_hyundai_canfd.h index f0af9060eb..97804709b8 100644 --- a/board/safety/safety_hyundai_canfd.h +++ b/board/safety/safety_hyundai_canfd.h @@ -153,7 +153,7 @@ static uint32_t hyundai_canfd_get_checksum(CANPacket_t *to_push) { return chksum; } -static int hyundai_canfd_rx_hook(CANPacket_t *to_push) { +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); @@ -233,7 +233,7 @@ static int hyundai_canfd_rx_hook(CANPacket_t *to_push) { return valid; } -static int hyundai_canfd_tx_hook(CANPacket_t *to_send) { +static bool hyundai_canfd_tx_hook(CANPacket_t *to_send) { int tx = 0; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_mazda.h b/board/safety/safety_mazda.h index 87feda207f..5854cd98a4 100644 --- a/board/safety/safety_mazda.h +++ b/board/safety/safety_mazda.h @@ -35,7 +35,7 @@ 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 int mazda_rx_hook(CANPacket_t *to_push) { +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)) { int addr = GET_ADDR(to_push); @@ -71,7 +71,7 @@ static int mazda_rx_hook(CANPacket_t *to_push) { return valid; } -static int mazda_tx_hook(CANPacket_t *to_send) { +static bool mazda_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_nissan.h b/board/safety/safety_nissan.h index 458a8c9daf..50d3ff6e55 100644 --- a/board/safety/safety_nissan.h +++ b/board/safety/safety_nissan.h @@ -41,7 +41,7 @@ const int NISSAN_PARAM_ALT_EPS_BUS = 1; bool nissan_alt_eps = false; -static int nissan_rx_hook(CANPacket_t *to_push) { +static bool nissan_rx_hook(CANPacket_t *to_push) { bool valid = addr_safety_check(to_push, &nissan_rx_checks, NULL, NULL, NULL, NULL); @@ -100,7 +100,7 @@ static int nissan_rx_hook(CANPacket_t *to_push) { } -static int nissan_tx_hook(CANPacket_t *to_send) { +static bool nissan_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 1c499a4746..ad6bef21d3 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -139,7 +139,7 @@ static uint32_t subaru_compute_checksum(CANPacket_t *to_push) { return checksum; } -static int subaru_rx_hook(CANPacket_t *to_push) { +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); @@ -193,7 +193,7 @@ static int subaru_rx_hook(CANPacket_t *to_push) { return valid; } -static int subaru_tx_hook(CANPacket_t *to_send) { +static bool subaru_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_subaru_preglobal.h b/board/safety/safety_subaru_preglobal.h index c841b2392d..8e9caf0dd0 100644 --- a/board/safety/safety_subaru_preglobal.h +++ b/board/safety/safety_subaru_preglobal.h @@ -43,7 +43,7 @@ const int SUBARU_PG_PARAM_REVERSED_DRIVER_TORQUE = 1; bool subaru_pg_reversed_driver_torque = false; -static int subaru_preglobal_rx_hook(CANPacket_t *to_push) { +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); @@ -83,7 +83,7 @@ static int subaru_preglobal_rx_hook(CANPacket_t *to_push) { return valid; } -static int subaru_preglobal_tx_hook(CANPacket_t *to_send) { +static bool subaru_preglobal_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_tesla.h b/board/safety/safety_tesla.h index 79ecda7851..99ed67da8e 100644 --- a/board/safety/safety_tesla.h +++ b/board/safety/safety_tesla.h @@ -58,7 +58,7 @@ bool tesla_powertrain = false; // Are we the second panda intercepting the powe bool tesla_stock_aeb = false; -static int tesla_rx_hook(CANPacket_t *to_push) { +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); @@ -126,7 +126,7 @@ static int tesla_rx_hook(CANPacket_t *to_push) { } -static int tesla_tx_hook(CANPacket_t *to_send) { +static bool tesla_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_toyota.h b/board/safety/safety_toyota.h index 53b19bea26..e6756aaa75 100644 --- a/board/safety/safety_toyota.h +++ b/board/safety/safety_toyota.h @@ -69,7 +69,7 @@ static uint32_t toyota_get_checksum(CANPacket_t *to_push) { return (uint8_t)(GET_BYTE(to_push, checksum_byte)); } -static int toyota_rx_hook(CANPacket_t *to_push) { +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); @@ -133,7 +133,7 @@ static int toyota_rx_hook(CANPacket_t *to_push) { return valid; } -static int toyota_tx_hook(CANPacket_t *to_send) { +static bool toyota_tx_hook(CANPacket_t *to_send) { int tx = 1; int addr = GET_ADDR(to_send); diff --git a/board/safety/safety_volkswagen_mqb.h b/board/safety/safety_volkswagen_mqb.h index 3ddc9bd0f6..d1f2b6b21d 100644 --- a/board/safety/safety_volkswagen_mqb.h +++ b/board/safety/safety_volkswagen_mqb.h @@ -105,7 +105,7 @@ static const addr_checks* volkswagen_mqb_init(uint16_t param) { return &volkswagen_mqb_rx_checks; } -static int volkswagen_mqb_rx_hook(CANPacket_t *to_push) { +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); @@ -196,7 +196,7 @@ static int volkswagen_mqb_rx_hook(CANPacket_t *to_push) { return valid; } -static int volkswagen_mqb_tx_hook(CANPacket_t *to_send) { +static bool volkswagen_mqb_tx_hook(CANPacket_t *to_send) { int addr = GET_ADDR(to_send); int tx = 1; diff --git a/board/safety/safety_volkswagen_pq.h b/board/safety/safety_volkswagen_pq.h index a2148dbef6..e45b604cfb 100644 --- a/board/safety/safety_volkswagen_pq.h +++ b/board/safety/safety_volkswagen_pq.h @@ -95,7 +95,7 @@ static const addr_checks* volkswagen_pq_init(uint16_t param) { return &volkswagen_pq_rx_checks; } -static int volkswagen_pq_rx_hook(CANPacket_t *to_push) { +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); @@ -174,7 +174,7 @@ static int volkswagen_pq_rx_hook(CANPacket_t *to_push) { return valid; } -static int volkswagen_pq_tx_hook(CANPacket_t *to_send) { +static bool volkswagen_pq_tx_hook(CANPacket_t *to_send) { int addr = GET_ADDR(to_send); int tx = 1; diff --git a/board/safety_declarations.h b/board/safety_declarations.h index cbef3a25d4..e3fc53a80d 100644 --- a/board/safety_declarations.h +++ b/board/safety_declarations.h @@ -132,9 +132,9 @@ typedef struct { int len; } addr_checks; -int safety_rx_hook(CANPacket_t *to_push); -int safety_tx_hook(CANPacket_t *to_send); -int safety_tx_lin_hook(int lin_num, uint8_t *data, int len); +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); uint32_t get_ts_elapsed(uint32_t ts, uint32_t ts_last); int to_signed(int d, int bits); void update_sample(struct sample_t *sample, int sample_new); @@ -178,9 +178,9 @@ 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 int (*rx_hook)(CANPacket_t *to_push); -typedef int (*tx_hook)(CANPacket_t *to_send); -typedef int (*tx_lin_hook)(int lin_num, uint8_t *data, int len); +typedef bool (*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); typedef struct { diff --git a/tests/libpanda/libpanda_py.py b/tests/libpanda/libpanda_py.py index 994894ac4a..a0fb02b730 100644 --- a/tests/libpanda/libpanda_py.py +++ b/tests/libpanda/libpanda_py.py @@ -25,9 +25,9 @@ """, packed=True) ffi.cdef(""" -int safety_rx_hook(CANPacket_t *to_send); -int safety_tx_hook(CANPacket_t *to_push); -int safety_tx_lin_hook(int lin_num, uint8_t *data, int len); +bool safety_rx_hook(CANPacket_t *to_send); +bool safety_tx_hook(CANPacket_t *to_push); +bool safety_tx_lin_hook(int lin_num, uint8_t *data, int len); int safety_fwd_hook(int bus_num, int addr); int set_safety_hooks(uint16_t mode, uint16_t param); """) diff --git a/tests/safety/common.py b/tests/safety/common.py index 1f3258699d..21049a3472 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -931,7 +931,7 @@ def test_relay_malfunction(self): self.safety.set_relay_malfunction(True) for bus in range(3): for addr in self.SCANNED_ADDRS: - self.assertEqual(-1, self._tx(make_msg(bus, addr, 8))) + self.assertFalse(self._tx(make_msg(bus, addr, 8))) self.assertEqual(-1, self.safety.safety_fwd_hook(bus, addr)) def test_prev_gas(self):