2019-09-25 08:50:53 +08:00
|
|
|
#!/usr/bin/env python3
|
2023-09-02 04:08:04 +08:00
|
|
|
import enum
|
2019-03-14 02:52:50 +08:00
|
|
|
import unittest
|
2019-10-03 09:20:32 +08:00
|
|
|
from panda import Panda
|
2022-12-01 09:41:24 +08:00
|
|
|
from panda.tests.libpanda import libpanda_py
|
2020-04-16 08:00:21 +08:00
|
|
|
import panda.tests.safety.common as common
|
2023-10-24 11:04:05 +08:00
|
|
|
from panda.tests.safety.common import CANPackerPanda
|
2023-08-13 08:16:28 +08:00
|
|
|
from functools import partial
|
2019-03-14 02:52:50 +08:00
|
|
|
|
2023-09-02 04:08:04 +08:00
|
|
|
class SubaruMsg(enum.IntEnum):
|
|
|
|
Brake_Status = 0x13c
|
|
|
|
CruiseControl = 0x240
|
|
|
|
Throttle = 0x40
|
|
|
|
Steering_Torque = 0x119
|
|
|
|
Wheel_Speeds = 0x13a
|
|
|
|
ES_LKAS = 0x122
|
|
|
|
ES_LKAS_ANGLE = 0x124
|
|
|
|
ES_Brake = 0x220
|
|
|
|
ES_Distance = 0x221
|
|
|
|
ES_Status = 0x222
|
|
|
|
ES_DashStatus = 0x321
|
|
|
|
ES_LKAS_State = 0x322
|
|
|
|
ES_Infotainment = 0x323
|
|
|
|
ES_UDS_Request = 0x787
|
|
|
|
ES_HighBeamAssist = 0x121
|
|
|
|
ES_STATIC_1 = 0x22a
|
|
|
|
ES_STATIC_2 = 0x325
|
2019-03-14 02:52:50 +08:00
|
|
|
|
2023-07-15 03:43:49 +08:00
|
|
|
|
|
|
|
SUBARU_MAIN_BUS = 0
|
|
|
|
SUBARU_ALT_BUS = 1
|
|
|
|
SUBARU_CAM_BUS = 2
|
|
|
|
|
|
|
|
|
2023-09-02 04:08:04 +08:00
|
|
|
def lkas_tx_msgs(alt_bus, lkas_msg=SubaruMsg.ES_LKAS):
|
2023-08-24 04:17:36 +08:00
|
|
|
return [[lkas_msg, SUBARU_MAIN_BUS],
|
2023-09-02 04:08:04 +08:00
|
|
|
[SubaruMsg.ES_Distance, alt_bus],
|
|
|
|
[SubaruMsg.ES_DashStatus, SUBARU_MAIN_BUS],
|
|
|
|
[SubaruMsg.ES_LKAS_State, SUBARU_MAIN_BUS],
|
|
|
|
[SubaruMsg.ES_Infotainment, SUBARU_MAIN_BUS]]
|
2023-07-15 03:43:49 +08:00
|
|
|
|
2023-09-02 04:08:04 +08:00
|
|
|
def long_tx_msgs(alt_bus):
|
|
|
|
return [[SubaruMsg.ES_Brake, alt_bus],
|
|
|
|
[SubaruMsg.ES_Status, alt_bus]]
|
2023-07-15 03:43:49 +08:00
|
|
|
|
2023-09-02 04:08:04 +08:00
|
|
|
def gen2_long_additional_tx_msgs():
|
|
|
|
return [[SubaruMsg.ES_UDS_Request, SUBARU_CAM_BUS],
|
|
|
|
[SubaruMsg.ES_HighBeamAssist, SUBARU_MAIN_BUS],
|
|
|
|
[SubaruMsg.ES_STATIC_1, SUBARU_MAIN_BUS],
|
|
|
|
[SubaruMsg.ES_STATIC_2, SUBARU_MAIN_BUS]]
|
|
|
|
|
|
|
|
def fwd_blacklisted_addr(lkas_msg=SubaruMsg.ES_LKAS):
|
|
|
|
return {SUBARU_CAM_BUS: [lkas_msg, SubaruMsg.ES_DashStatus, SubaruMsg.ES_LKAS_State, SubaruMsg.ES_Infotainment]}
|
2023-08-17 03:40:29 +08:00
|
|
|
|
2023-11-07 08:19:27 +08:00
|
|
|
class TestSubaruSafetyBase(common.PandaCarSafetyTest):
|
2023-07-15 03:43:49 +08:00
|
|
|
FLAGS = 0
|
|
|
|
STANDSTILL_THRESHOLD = 0 # kph
|
2023-11-08 11:03:08 +08:00
|
|
|
RELAY_MALFUNCTION_ADDRS = {SUBARU_MAIN_BUS: (SubaruMsg.ES_LKAS,)}
|
2023-07-15 03:43:49 +08:00
|
|
|
FWD_BUS_LOOKUP = {SUBARU_MAIN_BUS: SUBARU_CAM_BUS, SUBARU_CAM_BUS: SUBARU_MAIN_BUS}
|
2023-08-17 03:40:29 +08:00
|
|
|
FWD_BLACKLISTED_ADDRS = fwd_blacklisted_addr()
|
2020-04-16 08:00:21 +08:00
|
|
|
|
2022-05-26 13:51:33 +08:00
|
|
|
MAX_RT_DELTA = 940
|
|
|
|
RT_INTERVAL = 250000
|
|
|
|
|
|
|
|
DRIVER_TORQUE_ALLOWANCE = 60
|
2022-07-30 11:44:41 +08:00
|
|
|
DRIVER_TORQUE_FACTOR = 50
|
|
|
|
|
2023-08-17 03:40:29 +08:00
|
|
|
ALT_MAIN_BUS = SUBARU_MAIN_BUS
|
|
|
|
ALT_CAM_BUS = SUBARU_CAM_BUS
|
2022-05-26 13:51:33 +08:00
|
|
|
|
2023-10-03 17:18:10 +08:00
|
|
|
DEG_TO_CAN = 100
|
2023-08-05 08:16:35 +08:00
|
|
|
|
2023-08-13 08:16:28 +08:00
|
|
|
INACTIVE_GAS = 1818
|
|
|
|
|
2020-04-16 08:00:21 +08:00
|
|
|
def setUp(self):
|
2020-07-24 04:06:11 +08:00
|
|
|
self.packer = CANPackerPanda("subaru_global_2017_generated")
|
2022-12-01 09:41:24 +08:00
|
|
|
self.safety = libpanda_py.libpanda
|
2023-07-15 03:43:49 +08:00
|
|
|
self.safety.set_safety_hooks(Panda.SAFETY_SUBARU, self.FLAGS)
|
2020-04-29 01:33:20 +08:00
|
|
|
self.safety.init_tests()
|
2019-03-14 02:52:50 +08:00
|
|
|
|
|
|
|
def _set_prev_torque(self, t):
|
2020-04-29 01:33:20 +08:00
|
|
|
self.safety.set_desired_torque_last(t)
|
|
|
|
self.safety.set_rt_torque_last(t)
|
2019-03-14 02:52:50 +08:00
|
|
|
|
|
|
|
def _torque_driver_msg(self, torque):
|
2022-07-20 08:18:42 +08:00
|
|
|
values = {"Steer_Torque_Sensor": torque}
|
2022-07-22 09:50:42 +08:00
|
|
|
return self.packer.make_can_msg_panda("Steering_Torque", 0, values)
|
2019-03-14 02:52:50 +08:00
|
|
|
|
2020-03-07 14:29:46 +08:00
|
|
|
def _speed_msg(self, speed):
|
2023-08-05 08:16:35 +08:00
|
|
|
values = {s: speed for s in ["FR", "FL", "RR", "RL"]}
|
2023-08-17 03:40:29 +08:00
|
|
|
return self.packer.make_can_msg_panda("Wheel_Speeds", self.ALT_MAIN_BUS, values)
|
2023-08-05 08:16:35 +08:00
|
|
|
|
|
|
|
def _angle_meas_msg(self, angle):
|
|
|
|
values = {"Steering_Angle": angle}
|
|
|
|
return self.packer.make_can_msg_panda("Steering_Torque", 0, values)
|
2020-03-07 14:29:46 +08:00
|
|
|
|
2022-03-29 04:13:27 +08:00
|
|
|
def _user_brake_msg(self, brake):
|
2022-07-20 08:18:42 +08:00
|
|
|
values = {"Brake": brake}
|
2023-08-17 03:40:29 +08:00
|
|
|
return self.packer.make_can_msg_panda("Brake_Status", self.ALT_MAIN_BUS, values)
|
2020-03-07 14:29:46 +08:00
|
|
|
|
2022-03-29 04:13:27 +08:00
|
|
|
def _user_gas_msg(self, gas):
|
2022-07-20 08:18:42 +08:00
|
|
|
values = {"Throttle_Pedal": gas}
|
2022-07-22 09:50:42 +08:00
|
|
|
return self.packer.make_can_msg_panda("Throttle", 0, values)
|
2020-04-16 08:00:21 +08:00
|
|
|
|
2020-06-03 07:27:07 +08:00
|
|
|
def _pcm_status_msg(self, enable):
|
2022-07-20 08:18:42 +08:00
|
|
|
values = {"Cruise_Activated": enable}
|
2023-08-17 03:40:29 +08:00
|
|
|
return self.packer.make_can_msg_panda("CruiseControl", self.ALT_MAIN_BUS, values)
|
2022-07-30 11:44:41 +08:00
|
|
|
|
2023-08-15 10:26:00 +08:00
|
|
|
|
|
|
|
class TestSubaruStockLongitudinalSafetyBase(TestSubaruSafetyBase):
|
2023-08-13 08:16:28 +08:00
|
|
|
def _cancel_msg(self, cancel, cruise_throttle=0):
|
|
|
|
values = {"Cruise_Cancel": cancel, "Cruise_Throttle": cruise_throttle}
|
2023-08-17 03:40:29 +08:00
|
|
|
return self.packer.make_can_msg_panda("ES_Distance", self.ALT_MAIN_BUS, values)
|
2023-08-13 08:16:28 +08:00
|
|
|
|
|
|
|
def test_cancel_message(self):
|
|
|
|
# test that we can only send the cancel message (ES_Distance) with inactive throttle (1818) and Cruise_Cancel=1
|
|
|
|
for cancel in [True, False]:
|
|
|
|
self._generic_limit_safety_check(partial(self._cancel_msg, cancel), self.INACTIVE_GAS, self.INACTIVE_GAS, 0, 2**12, 1, self.INACTIVE_GAS, cancel)
|
|
|
|
|
2022-07-30 11:44:41 +08:00
|
|
|
|
2023-08-16 09:51:13 +08:00
|
|
|
class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.LongitudinalGasBrakeSafetyTest):
|
|
|
|
MIN_GAS = 808
|
|
|
|
MAX_GAS = 3400
|
|
|
|
INACTIVE_GAS = 1818
|
2023-12-30 08:25:02 +08:00
|
|
|
MAX_POSSIBLE_GAS = 2**13
|
2023-08-16 09:51:13 +08:00
|
|
|
|
|
|
|
MIN_BRAKE = 0
|
|
|
|
MAX_BRAKE = 600
|
|
|
|
MAX_POSSIBLE_BRAKE = 2**16
|
|
|
|
|
|
|
|
MIN_RPM = 0
|
2024-04-26 04:56:25 +08:00
|
|
|
MAX_RPM = 3600
|
2023-12-30 08:25:02 +08:00
|
|
|
MAX_POSSIBLE_RPM = 2**13
|
2023-08-16 09:51:13 +08:00
|
|
|
|
2023-09-02 04:08:04 +08:00
|
|
|
FWD_BLACKLISTED_ADDRS = {2: [SubaruMsg.ES_LKAS, SubaruMsg.ES_Brake, SubaruMsg.ES_Distance,
|
|
|
|
SubaruMsg.ES_Status, SubaruMsg.ES_DashStatus,
|
|
|
|
SubaruMsg.ES_LKAS_State, SubaruMsg.ES_Infotainment]}
|
2023-08-16 09:51:13 +08:00
|
|
|
|
|
|
|
def test_rpm_safety_check(self):
|
|
|
|
self._generic_limit_safety_check(self._send_rpm_msg, self.MIN_RPM, self.MAX_RPM, 0, self.MAX_POSSIBLE_RPM, 1)
|
|
|
|
|
|
|
|
def _send_brake_msg(self, brake):
|
|
|
|
values = {"Brake_Pressure": brake}
|
2023-08-17 03:40:29 +08:00
|
|
|
return self.packer.make_can_msg_panda("ES_Brake", self.ALT_MAIN_BUS, values)
|
2023-08-16 09:51:13 +08:00
|
|
|
|
|
|
|
def _send_gas_msg(self, gas):
|
|
|
|
values = {"Cruise_Throttle": gas}
|
2023-08-17 03:40:29 +08:00
|
|
|
return self.packer.make_can_msg_panda("ES_Distance", self.ALT_MAIN_BUS, values)
|
2023-08-16 09:51:13 +08:00
|
|
|
|
|
|
|
def _send_rpm_msg(self, rpm):
|
|
|
|
values = {"Cruise_RPM": rpm}
|
2023-08-17 03:40:29 +08:00
|
|
|
return self.packer.make_can_msg_panda("ES_Status", self.ALT_MAIN_BUS, values)
|
2023-08-16 09:51:13 +08:00
|
|
|
|
|
|
|
|
2023-09-07 11:34:24 +08:00
|
|
|
class TestSubaruTorqueSafetyBase(TestSubaruSafetyBase, common.DriverTorqueSteeringSafetyTest, common.SteerRequestCutSafetyTest):
|
2023-08-15 10:26:00 +08:00
|
|
|
MAX_RATE_UP = 50
|
|
|
|
MAX_RATE_DOWN = 70
|
|
|
|
MAX_TORQUE = 2047
|
|
|
|
|
2023-09-07 11:34:24 +08:00
|
|
|
# Safety around steering req bit
|
|
|
|
MIN_VALID_STEERING_FRAMES = 7
|
|
|
|
MAX_INVALID_STEERING_FRAMES = 1
|
|
|
|
MIN_VALID_STEERING_RT_INTERVAL = 144000
|
|
|
|
|
2023-08-15 10:26:00 +08:00
|
|
|
def _torque_cmd_msg(self, torque, steer_req=1):
|
2023-08-30 16:29:27 +08:00
|
|
|
values = {"LKAS_Output": torque, "LKAS_Request": steer_req}
|
2023-08-17 03:40:29 +08:00
|
|
|
return self.packer.make_can_msg_panda("ES_LKAS", SUBARU_MAIN_BUS, values)
|
2022-07-30 11:44:41 +08:00
|
|
|
|
|
|
|
|
2023-08-15 10:26:00 +08:00
|
|
|
class TestSubaruGen1TorqueStockLongitudinalSafety(TestSubaruStockLongitudinalSafetyBase, TestSubaruTorqueSafetyBase):
|
2023-07-15 03:43:49 +08:00
|
|
|
FLAGS = 0
|
|
|
|
TX_MSGS = lkas_tx_msgs(SUBARU_MAIN_BUS)
|
|
|
|
|
|
|
|
|
2023-09-02 04:08:04 +08:00
|
|
|
class TestSubaruGen2TorqueSafetyBase(TestSubaruTorqueSafetyBase):
|
2023-08-17 03:40:29 +08:00
|
|
|
ALT_MAIN_BUS = SUBARU_ALT_BUS
|
|
|
|
ALT_CAM_BUS = SUBARU_ALT_BUS
|
2023-08-15 10:26:00 +08:00
|
|
|
|
|
|
|
MAX_RATE_UP = 40
|
|
|
|
MAX_RATE_DOWN = 40
|
|
|
|
MAX_TORQUE = 1000
|
|
|
|
|
2023-09-02 04:08:04 +08:00
|
|
|
|
|
|
|
class TestSubaruGen2TorqueStockLongitudinalSafety(TestSubaruStockLongitudinalSafetyBase, TestSubaruGen2TorqueSafetyBase):
|
2023-07-15 03:43:49 +08:00
|
|
|
FLAGS = Panda.FLAG_SUBARU_GEN2
|
|
|
|
TX_MSGS = lkas_tx_msgs(SUBARU_ALT_BUS)
|
2020-02-20 11:55:05 +08:00
|
|
|
|
2019-03-14 02:52:50 +08:00
|
|
|
|
2023-08-16 09:51:13 +08:00
|
|
|
class TestSubaruGen1LongitudinalSafety(TestSubaruLongitudinalSafetyBase, TestSubaruTorqueSafetyBase):
|
|
|
|
FLAGS = Panda.FLAG_SUBARU_LONG
|
2023-09-02 04:08:04 +08:00
|
|
|
TX_MSGS = lkas_tx_msgs(SUBARU_MAIN_BUS) + long_tx_msgs(SUBARU_MAIN_BUS)
|
|
|
|
|
|
|
|
|
|
|
|
class TestSubaruGen2LongitudinalSafety(TestSubaruLongitudinalSafetyBase, TestSubaruGen2TorqueSafetyBase):
|
|
|
|
FLAGS = Panda.FLAG_SUBARU_LONG | Panda.FLAG_SUBARU_GEN2
|
|
|
|
TX_MSGS = lkas_tx_msgs(SUBARU_ALT_BUS) + long_tx_msgs(SUBARU_ALT_BUS) + gen2_long_additional_tx_msgs()
|
|
|
|
|
|
|
|
def _rdbi_msg(self, did: int):
|
|
|
|
return b'\x03\x22' + did.to_bytes(2) + b'\x00\x00\x00\x00'
|
|
|
|
|
|
|
|
def _es_uds_msg(self, msg: bytes):
|
|
|
|
return libpanda_py.make_CANPacket(SubaruMsg.ES_UDS_Request, 2, msg)
|
|
|
|
|
|
|
|
def test_es_uds_message(self):
|
|
|
|
tester_present = b'\x02\x3E\x80\x00\x00\x00\x00\x00'
|
|
|
|
not_tester_present = b"\x03\xAA\xAA\x00\x00\x00\x00\x00"
|
|
|
|
|
|
|
|
button_did = 0x1130
|
|
|
|
|
|
|
|
# Tester present is allowed for gen2 long to keep eyesight disabled
|
|
|
|
self.assertTrue(self._tx(self._es_uds_msg(tester_present)))
|
|
|
|
|
|
|
|
# Non-Tester present is not allowed
|
|
|
|
self.assertFalse(self._tx(self._es_uds_msg(not_tester_present)))
|
|
|
|
|
|
|
|
# Only button_did is allowed to be read via UDS
|
|
|
|
for did in range(0xFFFF):
|
|
|
|
should_tx = (did == button_did)
|
|
|
|
self.assertEqual(self._tx(self._es_uds_msg(self._rdbi_msg(did))), should_tx)
|
|
|
|
|
|
|
|
# any other msg is not allowed
|
|
|
|
for sid in range(0xFF):
|
|
|
|
msg = b'\x03' + sid.to_bytes(1) + b'\x00' * 6
|
|
|
|
self.assertFalse(self._tx(self._es_uds_msg(msg)))
|
2023-08-16 09:51:13 +08:00
|
|
|
|
|
|
|
|
2019-03-14 02:52:50 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|