From 4f9c87969670f7e66e0982db8d6842dc89822374 Mon Sep 17 00:00:00 2001 From: Riccardo Date: Tue, 19 Nov 2019 11:29:04 -0800 Subject: [PATCH] remove abunch of lines from safety regression tests by using common make_msg function --- tests/safety/common.py | 4 ++-- tests/safety/test_cadillac.py | 20 ++++-------------- tests/safety/test_chrysler.py | 15 +++++-------- tests/safety/test_gm.py | 27 +++++++----------------- tests/safety/test_honda.py | 36 ++++++++------------------------ tests/safety/test_subaru.py | 16 ++++---------- tests/safety/test_toyota.py | 25 ++++++---------------- tests/safety/test_toyota_ipas.py | 20 ++++++------------ tests/safety/test_volkswagen.py | 26 ++++++----------------- 9 files changed, 50 insertions(+), 139 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 07f90faa21..99f7237921 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -1,13 +1,13 @@ from panda.tests.safety import libpandasafety_py -def make_msg(bus, addr, length): +def make_msg(bus, addr, length=8): to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') if addr >= 0x800: to_send[0].RIR = (addr << 3) | 5 else: to_send[0].RIR = (addr << 21) | 1 to_send[0].RDTR = length - to_send[0].RDTR = bus << 4 + to_send[0].RDTR |= bus << 4 return to_send diff --git a/tests/safety/test_cadillac.py b/tests/safety/test_cadillac.py index 8245305bd8..7806fb7b28 100644 --- a/tests/safety/test_cadillac.py +++ b/tests/safety/test_cadillac.py @@ -44,18 +44,13 @@ def _set_prev_torque(self, t): self.safety.set_cadillac_rt_torque_last(t) def _torque_driver_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x164 << 21 - t = twos_comp(torque, 11) + to_send = make_msg(0, 0x164) to_send[0].RDLR = ((t >> 8) & 0x7) | ((t & 0xFF) << 8) return to_send def _torque_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x151 << 21 - to_send[0].RDTR = 2 << 4 - + to_send = make_msg(2, 0x151) t = twos_comp(torque, 14) to_send[0].RDLR = ((t >> 8) & 0x3F) | ((t & 0xFF) << 8) return to_send @@ -70,20 +65,13 @@ def test_manually_enable_controls_allowed(self): test_manually_enable_controls_allowed(self) def test_enable_control_allowed_from_cruise(self): - to_push = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_push[0].RIR = 0x370 << 21 + to_push = make_msg(0, 0x370) to_push[0].RDLR = 0x800000 - to_push[0].RDTR = 0 - self.safety.safety_rx_hook(to_push) self.assertTrue(self.safety.get_controls_allowed()) def test_disable_control_allowed_from_cruise(self): - to_push = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_push[0].RIR = 0x370 << 21 - to_push[0].RDLR = 0 - to_push[0].RDTR = 0 - + to_push = make_msg(0, 0x370) self.safety.set_controls_allowed(1) self.safety.safety_rx_hook(to_push) self.assertFalse(self.safety.get_controls_allowed()) diff --git a/tests/safety/test_chrysler.py b/tests/safety/test_chrysler.py index 2ddd3aff8a..2c472706fe 100755 --- a/tests/safety/test_chrysler.py +++ b/tests/safety/test_chrysler.py @@ -24,8 +24,7 @@ def setUp(cls): cls.safety.init_tests_chrysler() def _button_msg(self, buttons): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 571 << 21 + to_send = make_msg(0, 571) to_send[0].RDLR = buttons return to_send @@ -35,14 +34,12 @@ def _set_prev_torque(self, t): self.safety.set_chrysler_torque_meas(t, t) def _torque_meas_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 544 << 21 + to_send = make_msg(0, 544) to_send[0].RDHR = ((torque + 1024) >> 8) + (((torque + 1024) & 0xff) << 8) return to_send def _torque_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x292 << 21 + to_send = make_msg(0, 0x292) to_send[0].RDLR = ((torque + 1024) >> 8) + (((torque + 1024) & 0xff) << 8) return to_send @@ -69,16 +66,14 @@ def test_manually_enable_controls_allowed(self): test_manually_enable_controls_allowed(self) def test_enable_control_allowed_from_cruise(self): - to_push = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_push[0].RIR = 0x1f4 << 21 + to_push = make_msg(0, 0x1F4) to_push[0].RDLR = 0x380000 self.safety.safety_rx_hook(to_push) self.assertTrue(self.safety.get_controls_allowed()) def test_disable_control_allowed_from_cruise(self): - to_push = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_push[0].RIR = 0x1f4 << 21 + to_push = make_msg(0, 0x1F4) to_push[0].RDLR = 0 self.safety.set_controls_allowed(1) diff --git a/tests/safety/test_gm.py b/tests/safety/test_gm.py index b169e5c14d..99c15750c4 100644 --- a/tests/safety/test_gm.py +++ b/tests/safety/test_gm.py @@ -43,40 +43,33 @@ def setUp(cls): cls.safety.init_tests_gm() def _speed_msg(self, speed): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 842 << 21 + to_send = make_msg(0, 842) to_send[0].RDLR = speed return to_send def _button_msg(self, buttons): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 481 << 21 + to_send = make_msg(0, 481) to_send[0].RDHR = buttons << 12 return to_send def _brake_msg(self, brake): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 241 << 21 + to_send = make_msg(0, 241) to_send[0].RDLR = 0xa00 if brake else 0x900 return to_send def _gas_msg(self, gas): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 417 << 21 + to_send = make_msg(0, 417) to_send[0].RDHR = (1 << 16) if gas else 0 return to_send def _send_brake_msg(self, brake): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 789 << 21 - to_send[0].RDTR = 2 << 4 + to_send = make_msg(2, 789) brake = (-brake) & 0xfff to_send[0].RDLR = (brake >> 8) | ((brake &0xff) << 8) return to_send def _send_gas_msg(self, gas): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 715 << 21 + to_send = make_msg(0, 715) to_send[0].RDLR = ((gas & 0x1f) << 27) | ((gas & 0xfe0) << 11) return to_send @@ -85,18 +78,14 @@ def _set_prev_torque(self, t): self.safety.set_gm_rt_torque_last(t) def _torque_driver_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 388 << 21 - t = twos_comp(torque, 11) + to_send = make_msg(0, 388) to_send[0].RDHR = (((t >> 8) & 0x7) << 16) | ((t & 0xFF) << 24) return to_send def _torque_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 384 << 21 - t = twos_comp(torque, 11) + to_send = make_msg(0, 384) to_send[0].RDLR = ((t >> 8) & 0x7) | ((t & 0xFF) << 8) return to_send diff --git a/tests/safety/test_honda.py b/tests/safety/test_honda.py index c8c55a7fea..6ab247835a 100755 --- a/tests/safety/test_honda.py +++ b/tests/safety/test_honda.py @@ -18,66 +18,48 @@ def setUp(cls): cls.safety.init_tests_honda() def _speed_msg(self, speed): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x158 << 21 + to_send = make_msg(0, 0x158) to_send[0].RDLR = speed - return to_send def _button_msg(self, buttons, msg): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = msg << 21 - to_send[0].RDLR = buttons << 5 has_relay = self.safety.board_has_relay() honda_bosch_hardware = self.safety.get_honda_bosch_hardware() bus = 1 if has_relay and honda_bosch_hardware else 0 - to_send[0].RDTR = bus << 4 - + to_send = make_msg(bus, msg) + to_send[0].RDLR = buttons << 5 return to_send def _brake_msg(self, brake): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x17C << 21 + to_send = make_msg(0, 0x17C) to_send[0].RDHR = 0x200000 if brake else 0 - return to_send def _alt_brake_msg(self, brake): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x1BE << 21 + to_send = make_msg(0, 0x1BE) to_send[0].RDLR = 0x10 if brake else 0 - return to_send def _gas_msg(self, gas): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x17C << 21 + to_send = make_msg(0, 0x17C) to_send[0].RDLR = 1 if gas else 0 - return to_send def _send_brake_msg(self, brake): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x1FA << 21 + to_send = make_msg(0, 0x1FA) to_send[0].RDLR = ((brake & 0x3) << 14) | ((brake & 0x3FF) >> 2) - return to_send def _send_interceptor_msg(self, gas, addr): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = addr << 21 - to_send[0].RDTR = 6 + to_send = make_msg(0, addr, 6) gas2 = gas * 2 to_send[0].RDLR = ((gas & 0xff) << 8) | ((gas & 0xff00) >> 8) | \ ((gas2 & 0xff) << 24) | ((gas2 & 0xff00) << 8) - return to_send def _send_steer_msg(self, steer): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0xE4 << 21 + to_send = make_msg(0, 0xE4, 6) to_send[0].RDLR = steer - return to_send def test_spam_can_buses(self): diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 9616d3cfb4..e18d7515cb 100644 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -41,17 +41,13 @@ def _set_prev_torque(self, t): self.safety.set_subaru_rt_torque_last(t) def _torque_driver_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x119 << 21 - t = twos_comp(torque, 11) + to_send = make_msg(0, 0x119) to_send[0].RDLR = ((t & 0x7FF) << 16) return to_send def _torque_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x122 << 21 - + to_send = make_msg(0, 0x122) t = twos_comp(torque, 13) to_send[0].RDLR = (t << 16) return to_send @@ -66,18 +62,14 @@ def test_default_controls_not_allowed(self): self.assertFalse(self.safety.get_controls_allowed()) def test_enable_control_allowed_from_cruise(self): - to_push = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_push[0].RIR = 0x240 << 21 + to_push = make_msg(0, 0x240) to_push[0].RDHR = 1 << 9 - self.safety.safety_rx_hook(to_push) self.assertTrue(self.safety.get_controls_allowed()) def test_disable_control_allowed_from_cruise(self): - to_push = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_push[0].RIR = 0x240 << 21 + to_push = make_msg(0, 0x240) to_push[0].RDHR = 0 - self.safety.set_controls_allowed(1) self.safety.safety_rx_hook(to_push) self.assertFalse(self.safety.get_controls_allowed()) diff --git a/tests/safety/test_toyota.py b/tests/safety/test_toyota.py index 334d51f0fc..58e32f036e 100644 --- a/tests/safety/test_toyota.py +++ b/tests/safety/test_toyota.py @@ -49,51 +49,38 @@ def _set_prev_torque(self, t): self.safety.set_toyota_torque_meas(t, t) def _torque_meas_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x260 << 21 - t = twos_comp(torque, 16) + to_send = make_msg(0, 0x260) to_send[0].RDHR = t | ((t & 0xFF) << 16) return to_send def _torque_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x2E4 << 21 - t = twos_comp(torque, 16) + to_send = make_msg(0, 0x2E4) to_send[0].RDLR = t | ((t & 0xFF) << 16) return to_send def _accel_msg(self, accel): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x343 << 21 - + to_send = make_msg(0, 0x343) a = twos_comp(accel, 16) to_send[0].RDLR = (a & 0xFF) << 8 | (a >> 8) return to_send def _send_gas_msg(self, gas): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x2C1 << 21 + to_send = make_msg(0, 0x2C1) to_send[0].RDHR = (gas & 0xFF) << 16 - return to_send def _send_interceptor_msg(self, gas, addr): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = addr << 21 - to_send[0].RDTR = 6 gas2 = gas * 2 + to_send = make_msg(0, addr, 6) to_send[0].RDLR = ((gas & 0xff) << 8) | ((gas & 0xff00) >> 8) | \ ((gas2 & 0xff) << 24) | ((gas2 & 0xff00) << 8) - return to_send def _pcm_cruise_msg(self, cruise_on): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x1D2 << 21 + to_send = make_msg(0, 0x1D2) to_send[0].RDLR = cruise_on << 5 - return to_send def test_spam_can_buses(self): diff --git a/tests/safety/test_toyota_ipas.py b/tests/safety/test_toyota_ipas.py index ac1a62cbe5..680fc149b7 100644 --- a/tests/safety/test_toyota_ipas.py +++ b/tests/safety/test_toyota_ipas.py @@ -3,6 +3,7 @@ import numpy as np from panda import Panda from panda.tests.safety import libpandasafety_py +from panda.tests. safety.common import make_msg IPAS_OVERRIDE_THRESHOLD = 200 @@ -30,9 +31,7 @@ def setUp(cls): cls.safety.init_tests_toyota() def _torque_driver_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x260 << 21 - + to_send = make_msg(0, 0x260) t = twos_comp(torque, 16) to_send[0].RDLR = t | ((t & 0xFF) << 16) return to_send @@ -42,9 +41,7 @@ def _torque_driver_msg_array(self, torque): self.safety.safety_rx_hook(self._torque_driver_msg(torque)) def _angle_meas_msg(self, angle): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x25 << 21 - + to_send = make_msg(0, 0x25) t = twos_comp(angle, 12) to_send[0].RDLR = ((t & 0xF00) >> 8) | ((t & 0xFF) << 8) return to_send @@ -54,17 +51,13 @@ def _angle_meas_msg_array(self, angle): self.safety.safety_rx_hook(self._angle_meas_msg(angle)) def _ipas_state_msg(self, state): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x262 << 21 - + to_send = make_msg(0, 0x262) to_send[0].RDLR = state & 0xF return to_send def _ipas_control_msg(self, angle, state): # note: we command 2/3 of the angle due to CAN conversion - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x266 << 21 - + to_send = make_msg(0, 0x266) t = twos_comp(angle, 12) to_send[0].RDLR = ((t & 0xF00) >> 8) | ((t & 0xFF) << 8) to_send[0].RDLR |= ((state & 0xf) << 4) @@ -72,8 +65,7 @@ def _ipas_control_msg(self, angle, state): return to_send def _speed_msg(self, speed): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0xb4 << 21 + to_send = make_msg(0, 0xB4) speed = int(speed * 100 * 3.6) to_send[0].RDHR = ((speed & 0xFF) << 16) | (speed & 0xFF00) diff --git a/tests/safety/test_volkswagen.py b/tests/safety/test_volkswagen.py index 9b3531c81d..4c7f9d937e 100644 --- a/tests/safety/test_volkswagen.py +++ b/tests/safety/test_volkswagen.py @@ -35,9 +35,7 @@ def _set_prev_torque(self, t): self.safety.set_volkswagen_rt_torque_last(t) def _torque_driver_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x9F << 21 - + to_send = make_msg(0, 0x9F) t = abs(torque) to_send[0].RDHR = ((t & 0x1FFF) << 8) if torque < 0: @@ -45,9 +43,7 @@ def _torque_driver_msg(self, torque): return to_send def _torque_msg(self, torque): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x126 << 21 - + to_send = make_msg(0, 0x126) t = abs(torque) to_send[0].RDLR = (t & 0xFFF) << 16 if torque < 0: @@ -55,18 +51,13 @@ def _torque_msg(self, torque): return to_send def _gas_msg(self, gas): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x121 << 21 + to_send = make_msg(0, 0x121) to_send[0].RDLR = (gas & 0xFF) << 12 - return to_send def _button_msg(self, bit): - to_send = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_send[0].RIR = 0x12B << 21 + to_send = make_msg(2, 0x12B) to_send[0].RDLR = 1 << bit - to_send[0].RDTR = 2 << 4 - return to_send def test_spam_can_buses(self): @@ -84,18 +75,13 @@ def test_default_controls_not_allowed(self): self.assertFalse(self.safety.get_controls_allowed()) def test_enable_control_allowed_from_cruise(self): - to_push = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_push[0].RIR = 0x122 << 21 + to_push = make_msg(0, 0x122) to_push[0].RDHR = 0x30000000 - self.safety.safety_rx_hook(to_push) self.assertTrue(self.safety.get_controls_allowed()) def test_disable_control_allowed_from_cruise(self): - to_push = libpandasafety_py.ffi.new('CAN_FIFOMailBox_TypeDef *') - to_push[0].RIR = 0x122 << 21 - to_push[0].RDHR = 0 - + to_push = make_msg(0, 0x122) self.safety.set_controls_allowed(1) self.safety.safety_rx_hook(to_push) self.assertFalse(self.safety.get_controls_allowed())