Skip to content

Commit

Permalink
safety: cleanup ints that are bools (#1726)
Browse files Browse the repository at this point in the history
* safety: cleanup ints that are bools

* update tests
  • Loading branch information
adeebshihadeh authored Nov 20, 2023
1 parent 9fed950 commit c6248d6
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 51 deletions.
9 changes: 5 additions & 4 deletions board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_body.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_chrysler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions board/safety/safety_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_elm327.h
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_ford.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_gm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_honda.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_hyundai.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_mazda.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_nissan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_subaru.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_subaru_preglobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_tesla.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_toyota.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_volkswagen_mqb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions board/safety/safety_volkswagen_pq.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
12 changes: 6 additions & 6 deletions board/safety_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions tests/libpanda/libpanda_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -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);
""")
Expand Down
2 changes: 1 addition & 1 deletion tests/safety/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c6248d6

Please sign in to comment.