From 62bb0c2e339101c041990162c03174017a4b2fe6 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 1 Sep 2023 13:08:04 -0700 Subject: [PATCH 01/12] Subaru: gen2 long safety (#1594) gen2 long safety --- board/safety/safety_subaru.h | 59 +++++++++++++++----- tests/safety/common.py | 2 +- tests/safety/test_subaru.py | 105 +++++++++++++++++++++++++---------- 3 files changed, 122 insertions(+), 44 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index da282e90d6..0b43ebd8df 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -39,20 +39,32 @@ const LongitudinalLimits SUBARU_LONG_LIMITS = { #define MSG_SUBARU_ES_LKAS_State 0x322 #define MSG_SUBARU_ES_Infotainment 0x323 +#define MSG_SUBARU_ES_UDS_Request 0x787 + +#define MSG_SUBARU_ES_HighBeamAssist 0x121 +#define MSG_SUBARU_ES_STATIC_1 0x22a +#define MSG_SUBARU_ES_STATIC_2 0x325 + #define SUBARU_MAIN_BUS 0 #define SUBARU_ALT_BUS 1 #define SUBARU_CAM_BUS 2 -#define SUBARU_COMMON_TX_MSGS(alt_bus, lkas_msg) \ - {MSG_SUBARU_ES_LKAS, SUBARU_MAIN_BUS, 8}, \ - {MSG_SUBARU_ES_Distance, alt_bus, 8}, \ - {MSG_SUBARU_ES_DashStatus, SUBARU_MAIN_BUS, 8}, \ - {MSG_SUBARU_ES_LKAS_State, SUBARU_MAIN_BUS, 8}, \ - {MSG_SUBARU_ES_Infotainment, SUBARU_MAIN_BUS, 8}, \ +#define SUBARU_COMMON_TX_MSGS(alt_bus, lkas_msg) \ + {lkas_msg, SUBARU_MAIN_BUS, 8}, \ + {MSG_SUBARU_ES_Distance, alt_bus, 8}, \ + {MSG_SUBARU_ES_DashStatus, SUBARU_MAIN_BUS, 8}, \ + {MSG_SUBARU_ES_LKAS_State, SUBARU_MAIN_BUS, 8}, \ + {MSG_SUBARU_ES_Infotainment, SUBARU_MAIN_BUS, 8}, \ + +#define SUBARU_COMMON_LONG_TX_MSGS(alt_bus) \ + {MSG_SUBARU_ES_Brake, alt_bus, 8}, \ + {MSG_SUBARU_ES_Status, alt_bus, 8}, \ -#define SUBARU_COMMON_LONG_TX_MSGS(alt_bus) \ - {MSG_SUBARU_ES_Brake, SUBARU_MAIN_BUS, 8}, \ - {MSG_SUBARU_ES_Status, SUBARU_MAIN_BUS, 8}, \ +#define SUBARU_GEN2_LONG_ADDITIONAL_TX_MSGS() \ + {MSG_SUBARU_ES_UDS_Request, SUBARU_CAM_BUS, 8}, \ + {MSG_SUBARU_ES_HighBeamAssist, SUBARU_MAIN_BUS, 8}, \ + {MSG_SUBARU_ES_STATIC_1, SUBARU_MAIN_BUS, 8}, \ + {MSG_SUBARU_ES_STATIC_2, SUBARU_MAIN_BUS, 8}, \ #define SUBARU_COMMON_ADDR_CHECKS(alt_bus) \ {.msg = {{MSG_SUBARU_Throttle, SUBARU_MAIN_BUS, 8, .check_checksum = true, .max_counter = 15U, .expected_timestep = 10000U}, { 0 }, { 0 }}}, \ @@ -77,6 +89,13 @@ const CanMsg SUBARU_GEN2_TX_MSGS[] = { }; #define SUBARU_GEN2_TX_MSGS_LEN (sizeof(SUBARU_GEN2_TX_MSGS) / sizeof(SUBARU_GEN2_TX_MSGS[0])) +const CanMsg SUBARU_GEN2_LONG_TX_MSGS[] = { + SUBARU_COMMON_TX_MSGS(SUBARU_ALT_BUS, MSG_SUBARU_ES_LKAS) + SUBARU_COMMON_LONG_TX_MSGS(SUBARU_ALT_BUS) + SUBARU_GEN2_LONG_ADDITIONAL_TX_MSGS() +}; +#define SUBARU_GEN2_LONG_TX_MSGS_LEN (sizeof(SUBARU_GEN2_LONG_TX_MSGS) / sizeof(SUBARU_GEN2_LONG_TX_MSGS[0])) + AddrCheckStruct subaru_addr_checks[] = { SUBARU_COMMON_ADDR_CHECKS(SUBARU_MAIN_BUS) }; @@ -175,12 +194,14 @@ static int subaru_tx_hook(CANPacket_t *to_send) { int addr = GET_ADDR(to_send); bool violation = false; - if (subaru_gen2) { - tx = msg_allowed(to_send, SUBARU_GEN2_TX_MSGS, SUBARU_GEN2_TX_MSGS_LEN); + if (subaru_gen2 && subaru_longitudinal) { + tx = msg_allowed(to_send, SUBARU_GEN2_LONG_TX_MSGS, SUBARU_GEN2_LONG_TX_MSGS_LEN); + } else if (subaru_gen2) { + tx = msg_allowed(to_send, SUBARU_GEN2_TX_MSGS, SUBARU_GEN2_TX_MSGS_LEN); } else if (subaru_longitudinal) { - tx = msg_allowed(to_send, SUBARU_LONG_TX_MSGS, SUBARU_LONG_TX_MSGS_LEN); + tx = msg_allowed(to_send, SUBARU_LONG_TX_MSGS, SUBARU_LONG_TX_MSGS_LEN); } else { - tx = msg_allowed(to_send, SUBARU_TX_MSGS, SUBARU_TX_MSGS_LEN); + tx = msg_allowed(to_send, SUBARU_TX_MSGS, SUBARU_TX_MSGS_LEN); } // steer cmd checks @@ -221,6 +242,16 @@ static int subaru_tx_hook(CANPacket_t *to_send) { violation |= longitudinal_transmission_rpm_checks(transmission_rpm, SUBARU_LONG_LIMITS); } + if (addr == MSG_SUBARU_ES_UDS_Request) { + // tester present ('\x02\x3E\x80\x00\x00\x00\x00\x00') is allowed for gen2 longitudinal to keep eyesight disabled + bool is_tester_present = (GET_BYTES(to_send, 0, 4) == 0x00803E02U) && (GET_BYTES(to_send, 4, 4) == 0x0U); + + // reading ES button data by identifier (b'\x03\x22\x11\x30\x00\x00\x00\x00') is also allowed (DID 0x1130) + bool is_button_rdbi = (GET_BYTES(to_send, 0, 4) == 0x30112203U) && (GET_BYTES(to_send, 4, 4) == 0x0U); + + violation |= !(is_tester_present || is_button_rdbi); + } + if (violation){ tx = 0; } @@ -258,7 +289,7 @@ static const addr_checks* subaru_init(uint16_t param) { subaru_gen2 = GET_FLAG(param, SUBARU_PARAM_GEN2); #ifdef ALLOW_DEBUG - subaru_longitudinal = GET_FLAG(param, SUBARU_PARAM_LONGITUDINAL) && !subaru_gen2; + subaru_longitudinal = GET_FLAG(param, SUBARU_PARAM_LONGITUDINAL); #endif if (subaru_gen2) { diff --git a/tests/safety/common.py b/tests/safety/common.py index 36df296eac..3bead37982 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -955,7 +955,7 @@ def test_tx_hook_on_wrong_safety_mode(self): if attr.startswith('TestToyota') and current_test.startswith('TestToyota'): continue if {attr, current_test}.issubset({'TestSubaruGen1TorqueStockLongitudinalSafety', 'TestSubaruGen2TorqueStockLongitudinalSafety', - 'TestSubaruGen1LongitudinalSafety'}): + 'TestSubaruGen1LongitudinalSafety', 'TestSubaruGen2LongitudinalSafety'}): continue if {attr, current_test}.issubset({'TestVolkswagenPqSafety', 'TestVolkswagenPqStockSafety', 'TestVolkswagenPqLongSafety'}): continue diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index e89cda80dc..5c54cc1e07 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import enum import unittest from panda import Panda from panda.tests.libpanda import libpanda_py @@ -6,44 +7,55 @@ from panda.tests.safety.common import CANPackerPanda, MeasurementSafetyTest from functools import partial +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 -MSG_SUBARU_Brake_Status = 0x13c -MSG_SUBARU_CruiseControl = 0x240 -MSG_SUBARU_Throttle = 0x40 -MSG_SUBARU_Steering_Torque = 0x119 -MSG_SUBARU_Wheel_Speeds = 0x13a -MSG_SUBARU_ES_LKAS = 0x122 -MSG_SUBARU_ES_LKAS_ANGLE = 0x124 -MSG_SUBARU_ES_Brake = 0x220 -MSG_SUBARU_ES_Distance = 0x221 -MSG_SUBARU_ES_Status = 0x222 -MSG_SUBARU_ES_DashStatus = 0x321 -MSG_SUBARU_ES_LKAS_State = 0x322 -MSG_SUBARU_ES_Infotainment = 0x323 SUBARU_MAIN_BUS = 0 SUBARU_ALT_BUS = 1 SUBARU_CAM_BUS = 2 -def lkas_tx_msgs(alt_bus, lkas_msg=MSG_SUBARU_ES_LKAS): +def lkas_tx_msgs(alt_bus, lkas_msg=SubaruMsg.ES_LKAS): return [[lkas_msg, SUBARU_MAIN_BUS], - [MSG_SUBARU_ES_Distance, alt_bus], - [MSG_SUBARU_ES_DashStatus, SUBARU_MAIN_BUS], - [MSG_SUBARU_ES_LKAS_State, SUBARU_MAIN_BUS], - [MSG_SUBARU_ES_Infotainment, SUBARU_MAIN_BUS]] + [SubaruMsg.ES_Distance, alt_bus], + [SubaruMsg.ES_DashStatus, SUBARU_MAIN_BUS], + [SubaruMsg.ES_LKAS_State, SUBARU_MAIN_BUS], + [SubaruMsg.ES_Infotainment, SUBARU_MAIN_BUS]] -def long_tx_msgs(): - return [[MSG_SUBARU_ES_Brake, SUBARU_MAIN_BUS], - [MSG_SUBARU_ES_Status, SUBARU_MAIN_BUS]] +def long_tx_msgs(alt_bus): + return [[SubaruMsg.ES_Brake, alt_bus], + [SubaruMsg.ES_Status, alt_bus]] -def fwd_blacklisted_addr(lkas_msg=MSG_SUBARU_ES_LKAS): - return {SUBARU_CAM_BUS: [lkas_msg, MSG_SUBARU_ES_DashStatus, MSG_SUBARU_ES_LKAS_State, MSG_SUBARU_ES_Infotainment]} +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]} class TestSubaruSafetyBase(common.PandaSafetyTest, MeasurementSafetyTest): FLAGS = 0 STANDSTILL_THRESHOLD = 0 # kph - RELAY_MALFUNCTION_ADDR = MSG_SUBARU_ES_LKAS + RELAY_MALFUNCTION_ADDR = SubaruMsg.ES_LKAS RELAY_MALFUNCTION_BUS = SUBARU_MAIN_BUS FWD_BUS_LOOKUP = {SUBARU_MAIN_BUS: SUBARU_CAM_BUS, SUBARU_CAM_BUS: SUBARU_MAIN_BUS} FWD_BLACKLISTED_ADDRS = fwd_blacklisted_addr() @@ -122,9 +134,9 @@ class TestSubaruLongitudinalSafetyBase(TestSubaruSafetyBase, common.Longitudinal MAX_RPM = 2400 MAX_POSSIBLE_RPM = 2**12 - FWD_BLACKLISTED_ADDRS = {2: [MSG_SUBARU_ES_LKAS, MSG_SUBARU_ES_Brake, MSG_SUBARU_ES_Distance, - MSG_SUBARU_ES_Status, MSG_SUBARU_ES_DashStatus, - MSG_SUBARU_ES_LKAS_State, MSG_SUBARU_ES_Infotainment]} + 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]} 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) @@ -157,7 +169,7 @@ class TestSubaruGen1TorqueStockLongitudinalSafety(TestSubaruStockLongitudinalSaf TX_MSGS = lkas_tx_msgs(SUBARU_MAIN_BUS) -class TestSubaruGen2TorqueStockLongitudinalSafety(TestSubaruStockLongitudinalSafetyBase, TestSubaruTorqueSafetyBase): +class TestSubaruGen2TorqueSafetyBase(TestSubaruTorqueSafetyBase): ALT_MAIN_BUS = SUBARU_ALT_BUS ALT_CAM_BUS = SUBARU_ALT_BUS @@ -165,13 +177,48 @@ class TestSubaruGen2TorqueStockLongitudinalSafety(TestSubaruStockLongitudinalSaf MAX_RATE_DOWN = 40 MAX_TORQUE = 1000 + +class TestSubaruGen2TorqueStockLongitudinalSafety(TestSubaruStockLongitudinalSafetyBase, TestSubaruGen2TorqueSafetyBase): FLAGS = Panda.FLAG_SUBARU_GEN2 TX_MSGS = lkas_tx_msgs(SUBARU_ALT_BUS) class TestSubaruGen1LongitudinalSafety(TestSubaruLongitudinalSafetyBase, TestSubaruTorqueSafetyBase): FLAGS = Panda.FLAG_SUBARU_LONG - TX_MSGS = lkas_tx_msgs(SUBARU_MAIN_BUS) + long_tx_msgs() + 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))) if __name__ == "__main__": From 72f1603a89c7fecb6746d0154232f2d8187ebe70 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Fri, 1 Sep 2023 16:32:08 -0700 Subject: [PATCH 02/12] VW MQB: Check steer req bit (#1631) vq mqb --- board/safety/safety_volkswagen_mqb.h | 4 +++- tests/safety/test_volkswagen_mqb.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_volkswagen_mqb.h b/board/safety/safety_volkswagen_mqb.h index 4cd9c2ec31..0716cdf8fb 100644 --- a/board/safety/safety_volkswagen_mqb.h +++ b/board/safety/safety_volkswagen_mqb.h @@ -223,7 +223,9 @@ static int volkswagen_mqb_tx_hook(CANPacket_t *to_send) { desired_torque *= -1; } - if (steer_torque_cmd_checks(desired_torque, -1, VOLKSWAGEN_MQB_STEERING_LIMITS)) { + bool steer_req = GET_BIT(to_send, 30U) != 0U; + + if (steer_torque_cmd_checks(desired_torque, steer_req, VOLKSWAGEN_MQB_STEERING_LIMITS)) { tx = 0; } } diff --git a/tests/safety/test_volkswagen_mqb.py b/tests/safety/test_volkswagen_mqb.py index 0a49d477b0..8eeb4dadc2 100755 --- a/tests/safety/test_volkswagen_mqb.py +++ b/tests/safety/test_volkswagen_mqb.py @@ -85,7 +85,7 @@ def _torque_driver_msg(self, torque): # openpilot steering output torque def _torque_cmd_msg(self, torque, steer_req=1): - values = {"HCA_01_LM_Offset": abs(torque), "HCA_01_LM_OffSign": torque < 0} + values = {"HCA_01_LM_Offset": abs(torque), "HCA_01_LM_OffSign": torque < 0, "HCA_01_Sendestatus": steer_req} return self.packer.make_can_msg_panda("HCA_01", 0, values) # Cruise control buttons From 3ab4f43de06d7abcc4d594ee2a4efc0466e42c94 Mon Sep 17 00:00:00 2001 From: Igor Biletskyy Date: Tue, 5 Sep 2023 13:36:13 -0700 Subject: [PATCH 03/12] enable red and green leds until USB/SPI init (#1647) * init * add green --- board/jungle/main.c | 6 ++++++ board/main.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/board/jungle/main.c b/board/jungle/main.c index 43193a51aa..a2af733a7d 100644 --- a/board/jungle/main.c +++ b/board/jungle/main.c @@ -150,6 +150,9 @@ int main(void) { clock_init(); peripherals_init(); detect_board_type(); + // red+green leds enabled until succesful USB init, as a debug indicator + current_board->set_led(LED_RED, true); + current_board->set_led(LED_GREEN, true); // print hello print("\n\n\n************************ MAIN START ************************\n"); @@ -181,6 +184,9 @@ int main(void) { // enable USB (right before interrupts or enum can fail!) usb_init(); + current_board->set_led(LED_RED, false); + current_board->set_led(LED_GREEN, false); + print("**** INTERRUPTS ON ****\n"); enable_interrupts(); diff --git a/board/main.c b/board/main.c index 5fccaa7ac0..5f0457dfcb 100644 --- a/board/main.c +++ b/board/main.c @@ -348,6 +348,9 @@ int main(void) { clock_init(); peripherals_init(); detect_board_type(); + // red+green leds enabled until succesful USB/SPI init, as a debug indicator + current_board->set_led(LED_RED, true); + current_board->set_led(LED_GREEN, true); adc_init(); logging_init(); @@ -410,6 +413,9 @@ int main(void) { } #endif + current_board->set_led(LED_RED, false); + current_board->set_led(LED_GREEN, false); + print("**** INTERRUPTS ON ****\n"); enable_interrupts(); From 104d70854717fa7ea7db16ee4db66445d925bd65 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Tue, 5 Sep 2023 15:32:44 -0700 Subject: [PATCH 04/12] Precommit: ensure executable bit is set (#1650) * precommit ensure executable * fix the violations * the correct shebang --- .pre-commit-config.yaml | 2 ++ examples/tesla_tester.py | 0 python/ccp.py | 1 - python/uds.py | 1 - tests/read_winusb_descriptors.py | 0 tests/safety_replay/helpers.py | 1 - tests/usbprotocol/test_comms.py | 1 + 7 files changed, 3 insertions(+), 3 deletions(-) mode change 100644 => 100755 examples/tesla_tester.py mode change 100644 => 100755 tests/read_winusb_descriptors.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d37c5d1581..a44c089ce4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,6 +6,8 @@ repos: - id: check-yaml - id: check-merge-conflict - id: check-symlinks + - id: check-executables-have-shebangs + - id: check-shebang-scripts-are-executable - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.5.1 hooks: diff --git a/examples/tesla_tester.py b/examples/tesla_tester.py old mode 100644 new mode 100755 diff --git a/python/ccp.py b/python/ccp.py index 9183120021..50a5f87a3d 100644 --- a/python/ccp.py +++ b/python/ccp.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 import sys import time import struct diff --git a/python/uds.py b/python/uds.py index 9b25dffe66..aaa0697f90 100644 --- a/python/uds.py +++ b/python/uds.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 import time import struct from collections import deque diff --git a/tests/read_winusb_descriptors.py b/tests/read_winusb_descriptors.py old mode 100644 new mode 100755 diff --git a/tests/safety_replay/helpers.py b/tests/safety_replay/helpers.py index 4877b2e5e4..4c84e53206 100644 --- a/tests/safety_replay/helpers.py +++ b/tests/safety_replay/helpers.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 import panda.tests.libpanda.libpanda_py as libpanda_py from panda import Panda diff --git a/tests/usbprotocol/test_comms.py b/tests/usbprotocol/test_comms.py index 9b020f2347..c08551b709 100755 --- a/tests/usbprotocol/test_comms.py +++ b/tests/usbprotocol/test_comms.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import random import unittest From 0b6adad9c80f4a3565c5851f8e314eb70b0402f3 Mon Sep 17 00:00:00 2001 From: Shane Smiskol Date: Wed, 6 Sep 2023 00:36:25 -0700 Subject: [PATCH 05/12] Nissan: test alternate EPS bus param (#1651) * test this safety mode * test this safety mode * use int * clean up * clean up --- board/safety/safety_nissan.h | 4 ++-- tests/safety/common.py | 2 +- tests/safety/test_nissan.py | 24 ++++++++++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/board/safety/safety_nissan.h b/board/safety/safety_nissan.h index 1f44238e4e..3810c375fb 100644 --- a/board/safety/safety_nissan.h +++ b/board/safety/safety_nissan.h @@ -48,7 +48,7 @@ static int nissan_rx_hook(CANPacket_t *to_push) { int bus = GET_BUS(to_push); int addr = GET_ADDR(to_push); - if (((bus == 0) && (!nissan_alt_eps)) || ((bus == 1) && (nissan_alt_eps))) { + if (bus == (nissan_alt_eps ? 1 : 0)) { if (addr == 0x2) { // Current steering angle // Factor -0.1, little endian @@ -88,7 +88,7 @@ static int nissan_rx_hook(CANPacket_t *to_push) { } // Handle cruise enabled - if ((addr == 0x30f) && (((bus == 2) && (!nissan_alt_eps)) || ((bus == 1) && (nissan_alt_eps)))) { + if ((addr == 0x30f) && (bus == (nissan_alt_eps ? 1 : 2))) { bool cruise_engaged = (GET_BYTE(to_push, 0) >> 3) & 1U; pcm_cruise_check(cruise_engaged); } diff --git a/tests/safety/common.py b/tests/safety/common.py index 3bead37982..7eb14da38a 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -998,6 +998,6 @@ def test_tx_hook_on_wrong_safety_mode(self): msg = make_msg(bus, addr) self.safety.set_controls_allowed(1) # TODO: this should be blocked - if current_test in ["TestNissanSafety", "TestNissanLeafSafety"] and [addr, bus] in self.TX_MSGS: + if current_test in ["TestNissanSafety", "TestNissanSafetyAltEpsBus", "TestNissanLeafSafety"] and [addr, bus] in self.TX_MSGS: continue self.assertFalse(self._tx(msg), f"transmit of {addr=:#x} {bus=} from {test_name} during {current_test} was allowed") diff --git a/tests/safety/test_nissan.py b/tests/safety/test_nissan.py index 80d97f50f6..5ab519faf6 100755 --- a/tests/safety/test_nissan.py +++ b/tests/safety/test_nissan.py @@ -16,6 +16,9 @@ class TestNissanSafety(common.PandaSafetyTest, common.AngleSteeringSafetyTest): FWD_BLACKLISTED_ADDRS = {0: [0x280], 2: [0x169, 0x2b1, 0x4cc]} FWD_BUS_LOOKUP = {0: 2, 2: 0} + EPS_BUS = 0 + CRUISE_BUS = 2 + # Angle control limits DEG_TO_CAN = -100 @@ -35,16 +38,16 @@ def _angle_cmd_msg(self, angle: float, enabled: bool): def _angle_meas_msg(self, angle: float): values = {"STEER_ANGLE": angle} - return self.packer.make_can_msg_panda("STEER_ANGLE_SENSOR", 0, values) + return self.packer.make_can_msg_panda("STEER_ANGLE_SENSOR", self.EPS_BUS, values) def _pcm_status_msg(self, enable): values = {"CRUISE_ENABLED": enable} - return self.packer.make_can_msg_panda("CRUISE_STATE", 2, values) + return self.packer.make_can_msg_panda("CRUISE_STATE", self.CRUISE_BUS, values) def _speed_msg(self, speed): # TODO: why the 3.6? m/s to kph? not in dbc values = {"WHEEL_SPEED_%s" % s: speed * 3.6 for s in ["RR", "RL"]} - return self.packer.make_can_msg_panda("WHEEL_SPEEDS_REAR", 0, values) + return self.packer.make_can_msg_panda("WHEEL_SPEEDS_REAR", self.EPS_BUS, values) def _user_brake_msg(self, brake): values = {"USER_BRAKE_PRESSED": brake} @@ -52,7 +55,7 @@ def _user_brake_msg(self, brake): def _user_gas_msg(self, gas): values = {"GAS_PEDAL": gas} - return self.packer.make_can_msg_panda("GAS_PEDAL", 0, values) + return self.packer.make_can_msg_panda("GAS_PEDAL", self.EPS_BUS, values) def _acc_button_cmd(self, cancel=0, propilot=0, flw_dist=0, _set=0, res=0): no_button = not any([cancel, propilot, flw_dist, _set, res]) @@ -78,6 +81,19 @@ def test_acc_buttons(self): self.assertEqual(tx, should_tx) +class TestNissanSafetyAltEpsBus(TestNissanSafety): + """Altima uses different buses""" + + EPS_BUS = 1 + CRUISE_BUS = 1 + + def setUp(self): + self.packer = CANPackerPanda("nissan_x_trail_2017_generated") + self.safety = libpanda_py.libpanda + self.safety.set_safety_hooks(Panda.SAFETY_NISSAN, 1) + self.safety.init_tests() + + class TestNissanLeafSafety(TestNissanSafety): def setUp(self): From 39bc5a488686dc28b6bd7b53f555fa22ae9e5860 Mon Sep 17 00:00:00 2001 From: Jason Wen Date: Wed, 6 Sep 2023 03:46:33 -0400 Subject: [PATCH 06/12] Nissan Altima: add a param for alternate EPS bus (#1649) * Nissan Altima: Specify panda flag for alt bus safety config * test this safety mode * test this safety mode * rm * nl --------- Co-authored-by: Shane Smiskol --- board/safety/safety_nissan.h | 4 +++- python/__init__.py | 2 ++ tests/safety/test_nissan.py | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/board/safety/safety_nissan.h b/board/safety/safety_nissan.h index 3810c375fb..ccb2b9fb2c 100644 --- a/board/safety/safety_nissan.h +++ b/board/safety/safety_nissan.h @@ -38,6 +38,8 @@ AddrCheckStruct nissan_addr_checks[] = { addr_checks nissan_rx_checks = {nissan_addr_checks, NISSAN_ADDR_CHECK_LEN}; // EPS Location. false = V-CAN, true = C-CAN +const int NISSAN_PARAM_ALT_EPS_BUS = 1; + bool nissan_alt_eps = false; static int nissan_rx_hook(CANPacket_t *to_push) { @@ -158,7 +160,7 @@ static int nissan_fwd_hook(int bus_num, int addr) { } static const addr_checks* nissan_init(uint16_t param) { - nissan_alt_eps = param ? 1 : 0; + nissan_alt_eps = GET_FLAG(param, NISSAN_PARAM_ALT_EPS_BUS); return &nissan_rx_checks; } diff --git a/python/__init__.py b/python/__init__.py index 828108d776..e0c2007b93 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -237,6 +237,8 @@ class Panda: FLAG_SUBARU_GEN2 = 1 FLAG_SUBARU_LONG = 2 + FLAG_NISSAN_ALT_EPS_BUS = 1 + FLAG_GM_HW_CAM = 1 FLAG_GM_HW_CAM_LONG = 2 diff --git a/tests/safety/test_nissan.py b/tests/safety/test_nissan.py index 5ab519faf6..03f88b0d64 100755 --- a/tests/safety/test_nissan.py +++ b/tests/safety/test_nissan.py @@ -90,7 +90,7 @@ class TestNissanSafetyAltEpsBus(TestNissanSafety): def setUp(self): self.packer = CANPackerPanda("nissan_x_trail_2017_generated") self.safety = libpanda_py.libpanda - self.safety.set_safety_hooks(Panda.SAFETY_NISSAN, 1) + self.safety.set_safety_hooks(Panda.SAFETY_NISSAN, Panda.FLAG_NISSAN_ALT_EPS_BUS) self.safety.init_tests() From 394b61cd1651b53c8d9b51470e8cc3b9617e9272 Mon Sep 17 00:00:00 2001 From: Igor Biletskyy Date: Wed, 6 Sep 2023 09:50:13 -0700 Subject: [PATCH 07/12] H7: reset CAN core on bus off state (#1615) * init * misra * merge --- board/drivers/fdcan.h | 8 +++++--- board/jungle/main.c | 9 --------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/board/drivers/fdcan.h b/board/drivers/fdcan.h index 34367cb3a9..5104f3fec9 100644 --- a/board/drivers/fdcan.h +++ b/board/drivers/fdcan.h @@ -75,9 +75,11 @@ void update_can_health_pkt(uint8_t can_number, uint32_t ir_reg) { if ((ir_reg & (FDCAN_IR_RF0L)) != 0) { can_health[can_number].total_rx_lost_cnt += 1U; } - // While multiplexing between buses 1 and 3 we are getting ACK errors that overwhelm CAN core - // By resseting CAN core when no ACK is detected for a while(until TEC counter reaches 127) it can recover faster - if (((can_health[can_number].last_error == CAN_ACK_ERROR) || (can_health[can_number].last_data_error == CAN_ACK_ERROR)) && (can_health[can_number].transmit_error_cnt > 127U)) { + // Cases: + // 1. while multiplexing between buses 1 and 3 we are getting ACK errors that overwhelm CAN core, by resetting it recovers faster + // 2. H7 gets stuck in bus off recovery state indefinitely + if ((((can_health[can_number].last_error == CAN_ACK_ERROR) || (can_health[can_number].last_data_error == CAN_ACK_ERROR)) && (can_health[can_number].transmit_error_cnt > 127U)) || + ((ir_reg & FDCAN_IR_BO) != 0)) { can_health[can_number].can_core_reset_cnt += 1U; can_health[can_number].total_tx_lost_cnt += (FDCAN_TX_FIFO_EL_CNT - (FDCANx->TXFQS & FDCAN_TXFQS_TFFL)); // TX FIFO msgs will be lost after reset llcan_clear_send(FDCANx); diff --git a/board/jungle/main.c b/board/jungle/main.c index a2af733a7d..3ab39ba1d6 100644 --- a/board/jungle/main.c +++ b/board/jungle/main.c @@ -93,15 +93,6 @@ void tick_handler(void) { } #ifdef FINAL_PROVISIONING - // Reset CAN core if it got stuck in bus off state after shorted CANH/L - // Looks like an issue with H7 that it doesn't want to recover from bus off - // FIXME: this must be fixed on the driver level, temporary workaround - for (uint8_t i = 0U; i < 3U; i++) { - update_can_health_pkt(i, 0); - if (can_health[i].bus_off == 1U) { - can_init(i); - } - } // Ignition blinking uint8_t ignition_bitmask = 0U; for (uint8_t i = 0U; i < 6U; i++) { From c12cbc1b94c2809685a697b761feae31d895a88d Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 6 Sep 2023 19:35:54 -0700 Subject: [PATCH 08/12] Safety: separate test for steer request bit cutting (#1653) * common test for that * canfd too * we set this everywhere * never none * subclass * Update tests/safety/common.py --------- Co-authored-by: Shane Smiskol --- tests/safety/common.py | 28 ++++++++++++++-------------- tests/safety/test_hyundai.py | 2 +- tests/safety/test_hyundai_canfd.py | 2 +- tests/safety/test_toyota.py | 2 +- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/safety/common.py b/tests/safety/common.py index 7eb14da38a..b5a343e918 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -245,11 +245,6 @@ class TorqueSteeringSafetyTestBase(PandaSafetyTestBase, abc.ABC): MAX_RT_DELTA = 0 RT_INTERVAL = 0 - # Safety around steering req bit - MIN_VALID_STEERING_FRAMES = 0 - MAX_INVALID_STEERING_FRAMES = 0 - MIN_VALID_STEERING_RT_INTERVAL = 0 - @classmethod def setUpClass(cls): if cls.__name__ == "TorqueSteeringSafetyTestBase": @@ -288,6 +283,20 @@ def test_non_realtime_limit_up(self): self._set_prev_torque(0) self.assertFalse(self._tx(self._torque_cmd_msg(-self.MAX_RATE_UP - 1))) + +class SteerRequestCutSafetyTest(TorqueSteeringSafetyTestBase, abc.ABC): + + @classmethod + def setUpClass(cls): + if cls.__name__ == "SteerRequestCutSafetyTest": + cls.safety = None + raise unittest.SkipTest + + # Safety around steering request bit + MIN_VALID_STEERING_FRAMES: int + MAX_INVALID_STEERING_FRAMES: int + MIN_VALID_STEERING_RT_INTERVAL: int + def test_steer_req_bit_frames(self): """ Certain safety modes implement some tolerance on their steer request bits matching the @@ -329,10 +338,6 @@ def test_steer_req_bit_multi_invalid(self): is sent after an invalid frame (even without sending the max number of allowed invalid frames), all counters are reset. """ - # TODO: Add safety around steer request bits for all safety modes and remove exception - if self.MIN_VALID_STEERING_FRAMES == 0: - raise unittest.SkipTest("Safety mode does not implement tolerance for steer request bit safety") - for max_invalid_steer_frames in range(1, self.MAX_INVALID_STEERING_FRAMES * 2): self.safety.init_tests() self.safety.set_timer(self.MIN_VALID_STEERING_RT_INTERVAL) @@ -360,10 +365,6 @@ def test_steer_req_bit_realtime(self): - That we allow messages with mismatching steer request bit if time from last is >= MIN_VALID_STEERING_RT_INTERVAL - That frame mismatch safety does not interfere with this test """ - # TODO: Add safety around steer request bits for all safety modes and remove exception - if self.MIN_VALID_STEERING_RT_INTERVAL == 0: - raise unittest.SkipTest("Safety mode does not implement tolerance for steer request bit safety") - for rt_us in np.arange(self.MIN_VALID_STEERING_RT_INTERVAL - 50000, self.MIN_VALID_STEERING_RT_INTERVAL + 50000, 10000): # Reset match count and rt timer (valid_steer_req_count, ts_steer_req_mismatch_last) self.safety.init_tests() @@ -397,7 +398,6 @@ class DriverTorqueSteeringSafetyTest(TorqueSteeringSafetyTestBase, abc.ABC): @classmethod def setUpClass(cls): - super().setUpClass() if cls.__name__ == "DriverTorqueSteeringSafetyTest": cls.safety = None raise unittest.SkipTest diff --git a/tests/safety/test_hyundai.py b/tests/safety/test_hyundai.py index 17aee771e2..079584c6a5 100755 --- a/tests/safety/test_hyundai.py +++ b/tests/safety/test_hyundai.py @@ -42,7 +42,7 @@ def checksum(msg): return addr, t, ret, bus -class TestHyundaiSafety(HyundaiButtonBase, common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): +class TestHyundaiSafety(HyundaiButtonBase, common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest, common.SteerRequestCutSafetyTest): TX_MSGS = [[0x340, 0], [0x4F1, 0], [0x485, 0]] STANDSTILL_THRESHOLD = 12 # 0.375 kph RELAY_MALFUNCTION_ADDR = 0x340 diff --git a/tests/safety/test_hyundai_canfd.py b/tests/safety/test_hyundai_canfd.py index b1f4f895cb..a9f349f079 100755 --- a/tests/safety/test_hyundai_canfd.py +++ b/tests/safety/test_hyundai_canfd.py @@ -9,7 +9,7 @@ -class TestHyundaiCanfdBase(HyundaiButtonBase, common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest): +class TestHyundaiCanfdBase(HyundaiButtonBase, common.PandaSafetyTest, common.DriverTorqueSteeringSafetyTest, common.SteerRequestCutSafetyTest): TX_MSGS = [[0x50, 0], [0x1CF, 1], [0x2A4, 0]] STANDSTILL_THRESHOLD = 12 # 0.375 kph diff --git a/tests/safety/test_toyota.py b/tests/safety/test_toyota.py index a0ba9c1130..abe724e840 100755 --- a/tests/safety/test_toyota.py +++ b/tests/safety/test_toyota.py @@ -120,7 +120,7 @@ def test_rx_hook(self): self.assertFalse(self.safety.get_controls_allowed()) -class TestToyotaSafetyTorque(TestToyotaSafetyBase, common.MotorTorqueSteeringSafetyTest): +class TestToyotaSafetyTorque(TestToyotaSafetyBase, common.MotorTorqueSteeringSafetyTest, common.SteerRequestCutSafetyTest): MAX_RATE_UP = 15 MAX_RATE_DOWN = 25 From cb0cd1bfaabc6673b4f21b6c8361778c7b3e0014 Mon Sep 17 00:00:00 2001 From: Justin Newberry Date: Wed, 6 Sep 2023 20:34:24 -0700 Subject: [PATCH 09/12] Subaru: add safety around cutting steer req bit (#1632) * wip * steer bit * add tolerance * increase a bit more * test too * Had that backwards * Here too * review suggestions * minimal diff * minimal diff * add type hints * spacing Co-authored-by: Shane Smiskol * update test name * add tolerance comment Co-authored-by: Shane Smiskol --------- Co-authored-by: Shane Smiskol --- board/safety/safety_subaru.h | 29 ++++++++++++++++++----------- tests/safety/test_subaru.py | 7 ++++++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/board/safety/safety_subaru.h b/board/safety/safety_subaru.h index 0b43ebd8df..bac8a76509 100644 --- a/board/safety/safety_subaru.h +++ b/board/safety/safety_subaru.h @@ -1,14 +1,21 @@ -#define SUBARU_STEERING_LIMITS_GENERATOR(steer_max, rate_up, rate_down) \ - { \ - .max_steer = (steer_max), \ - .max_rt_delta = 940, \ - .max_rt_interval = 250000, \ - .max_rate_up = (rate_up), \ - .max_rate_down = (rate_down), \ - .driver_torque_factor = 50, \ - .driver_torque_allowance = 60, \ - .type = TorqueDriverLimited, \ - } \ +#define SUBARU_STEERING_LIMITS_GENERATOR(steer_max, rate_up, rate_down) \ + { \ + .max_steer = (steer_max), \ + .max_rt_delta = 940, \ + .max_rt_interval = 250000, \ + .max_rate_up = (rate_up), \ + .max_rate_down = (rate_down), \ + .driver_torque_factor = 50, \ + .driver_torque_allowance = 60, \ + .type = TorqueDriverLimited, \ + /* the EPS will temporary fault if the steering rate is too high, so we cut the \ + the steering torque every 7 frames for 1 frame if the steering rate is high */ \ + .min_valid_request_frames = 7, \ + .max_invalid_request_frames = 1, \ + .min_valid_request_rt_interval = 144000, /* 10% tolerance */ \ + .has_steer_req_tolerance = true, \ + } + const SteeringLimits SUBARU_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(2047, 50, 70); const SteeringLimits SUBARU_GEN2_STEERING_LIMITS = SUBARU_STEERING_LIMITS_GENERATOR(1000, 40, 40); diff --git a/tests/safety/test_subaru.py b/tests/safety/test_subaru.py index 5c54cc1e07..219cd278e9 100755 --- a/tests/safety/test_subaru.py +++ b/tests/safety/test_subaru.py @@ -154,11 +154,16 @@ def _send_rpm_msg(self, rpm): return self.packer.make_can_msg_panda("ES_Status", self.ALT_MAIN_BUS, values) -class TestSubaruTorqueSafetyBase(TestSubaruSafetyBase, common.DriverTorqueSteeringSafetyTest): +class TestSubaruTorqueSafetyBase(TestSubaruSafetyBase, common.DriverTorqueSteeringSafetyTest, common.SteerRequestCutSafetyTest): MAX_RATE_UP = 50 MAX_RATE_DOWN = 70 MAX_TORQUE = 2047 + # Safety around steering req bit + MIN_VALID_STEERING_FRAMES = 7 + MAX_INVALID_STEERING_FRAMES = 1 + MIN_VALID_STEERING_RT_INTERVAL = 144000 + def _torque_cmd_msg(self, torque, steer_req=1): values = {"LKAS_Output": torque, "LKAS_Request": steer_req} return self.packer.make_can_msg_panda("ES_LKAS", SUBARU_MAIN_BUS, values) From e98639b752267b66bd8ae2fdd8b32ef1dd685910 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Thu, 7 Sep 2023 16:33:36 -0700 Subject: [PATCH 10/12] increase not booted log timeout --- board/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/board/main.c b/board/main.c index 5f0457dfcb..8b6706e9d4 100644 --- a/board/main.c +++ b/board/main.c @@ -203,8 +203,8 @@ void tick_handler(void) { if (som_running) { log("device booted"); waiting_to_boot = false; - } else if (waiting_to_boot_count == 10U) { - log("not booted after 10s"); + } else if (waiting_to_boot_count == 45U) { + log("not booted after 45s"); } else { } From ee21517c7713c6577679a21dd1632c2ab1830600 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Thu, 7 Sep 2023 19:10:05 -0700 Subject: [PATCH 11/12] CI: run each job only once (#1659) --- .github/workflows/test.yaml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index c1be2c832c..72c329bc76 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,5 +1,10 @@ name: tests -on: [push, pull_request] + +on: + push: + branches: + - master + pull_request: concurrency: group: ${{ github.workflow }}-${{ github.ref != 'refs/heads/master' && github.ref || github.run_id }}-${{ github.event_name }} From d87d33cb514d934bd4d33923a99d9be00bb657a0 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Thu, 7 Sep 2023 19:10:30 -0700 Subject: [PATCH 12/12] pre-commit: autoupdate hooks (#1639) * Update pre-commit hook versions * fix --------- Co-authored-by: adeebshihadeh --- .pre-commit-config.yaml | 2 +- python/__init__.py | 2 +- python/spi.py | 2 +- python/usb.py | 2 +- tests/safety/common.py | 16 ++++++++-------- tests/safety/test_honda.py | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a44c089ce4..af36331107 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,6 +15,6 @@ repos: additional_dependencies: ['git+https://github.com/numpy/numpy-stubs', 'types-requests', 'types-atomicwrites', 'types-pycurl'] - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.0.285 + rev: v0.0.287 hooks: - id: ruff diff --git a/python/__init__.py b/python/__init__.py index e0c2007b93..91523e8fee 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -458,7 +458,7 @@ def reconnect(self): success = False # wait up to 15 seconds - for _ in range(0, 15*10): + for _ in range(15*10): try: self.connect() success = True diff --git a/python/spi.py b/python/spi.py index 0be28f49c1..faec497c6f 100644 --- a/python/spi.py +++ b/python/spi.py @@ -421,7 +421,7 @@ def close(self): def program(self, address, dat): bs = 256 # max block size for writing to flash over SPI dat += b"\xFF" * ((bs - len(dat)) % bs) - for i in range(0, len(dat) // bs): + for i in range(len(dat) // bs): block = dat[i * bs:(i + 1) * bs] self._cmd(0x31, data=[ struct.pack('>I', address + i*bs), diff --git a/python/usb.py b/python/usb.py index 0f1bff7c16..e6b807ca6f 100644 --- a/python/usb.py +++ b/python/usb.py @@ -78,7 +78,7 @@ def program(self, address, dat): # Program bs = min(len(dat), self._mcu_type.config.block_size) dat += b"\xFF" * ((bs - len(dat)) % bs) - for i in range(0, len(dat) // bs): + for i in range(len(dat) // bs): ldat = dat[i * bs:(i + 1) * bs] print("programming %d with length %d" % (i, len(ldat))) self._libusb_handle.controlWrite(0x21, self.DFU_DNLOAD, 2 + i, 0, ldat) diff --git a/tests/safety/common.py b/tests/safety/common.py index b5a343e918..f0d11b482e 100644 --- a/tests/safety/common.py +++ b/tests/safety/common.py @@ -126,7 +126,7 @@ def test_prev_gas_interceptor(self): self.safety.set_gas_interceptor_detected(False) def test_disengage_on_gas_interceptor(self): - for g in range(0, 0x1000): + for g in range(0x1000): self._rx(self._interceptor_user_gas(0)) self.safety.set_controls_allowed(True) self._rx(self._interceptor_user_gas(g)) @@ -138,7 +138,7 @@ def test_disengage_on_gas_interceptor(self): def test_alternative_experience_no_disengage_on_gas_interceptor(self): self.safety.set_controls_allowed(True) self.safety.set_alternative_experience(ALTERNATIVE_EXPERIENCE.DISABLE_DISENGAGE_ON_GAS) - for g in range(0, 0x1000): + for g in range(0x1000): self._rx(self._interceptor_user_gas(g)) # Test we allow lateral, but not longitudinal self.assertTrue(self.safety.get_controls_allowed()) @@ -576,13 +576,13 @@ def test_torque_measurements(self): self.assertTrue(self.safety.get_torque_meas_min() in min_range) self.assertTrue(self.safety.get_torque_meas_max() in max_range) - max_range = range(0, self.TORQUE_MEAS_TOLERANCE + 1) + max_range = range(self.TORQUE_MEAS_TOLERANCE + 1) min_range = range(-(trq + self.TORQUE_MEAS_TOLERANCE), -trq + 1) self._rx(self._torque_meas_msg(0)) self.assertTrue(self.safety.get_torque_meas_min() in min_range) self.assertTrue(self.safety.get_torque_meas_max() in max_range) - max_range = range(0, self.TORQUE_MEAS_TOLERANCE + 1) + max_range = range(self.TORQUE_MEAS_TOLERANCE + 1) min_range = range(-self.TORQUE_MEAS_TOLERANCE, 0 + 1) self._rx(self._torque_meas_msg(0)) self.assertTrue(self.safety.get_torque_meas_min() in min_range) @@ -733,7 +733,7 @@ def test_angle_cmd_when_disabled(self): @add_regen_tests class PandaSafetyTest(PandaSafetyTestBase): TX_MSGS: Optional[List[List[int]]] = None - SCANNED_ADDRS = [*range(0x0, 0x800), # Entire 11-bit CAN address space + SCANNED_ADDRS = [*range(0x800), # Entire 11-bit CAN address space *range(0x18DA00F1, 0x18DB00F1, 0x100), # 29-bit UDS physical addressing *range(0x18DB00F1, 0x18DC00F1, 0x100), # 29-bit UDS functional addressing *range(0x3300, 0x3400), # Honda @@ -791,14 +791,14 @@ def test_relay_malfunction(self): self.assertFalse(self.safety.get_relay_malfunction()) self._rx(make_msg(self.RELAY_MALFUNCTION_BUS, self.RELAY_MALFUNCTION_ADDR, 8)) self.assertTrue(self.safety.get_relay_malfunction()) - for bus in range(0, 3): + for bus in range(3): for addr in self.SCANNED_ADDRS: self.assertEqual(-1, self._tx(make_msg(bus, addr, 8))) self.assertEqual(-1, self.safety.safety_fwd_hook(bus, addr)) def test_fwd_hook(self): # some safety modes don't forward anything, while others blacklist msgs - for bus in range(0, 3): + for bus in range(3): for addr in self.SCANNED_ADDRS: # assume len 8 fwd_bus = self.FWD_BUS_LOOKUP.get(bus, -1) @@ -807,7 +807,7 @@ def test_fwd_hook(self): self.assertEqual(fwd_bus, self.safety.safety_fwd_hook(bus, addr), f"{addr=:#x} from {bus=} to {fwd_bus=}") def test_spam_can_buses(self): - for bus in range(0, 4): + for bus in range(4): for addr in self.SCANNED_ADDRS: if all(addr != m[0] or bus != m[1] for m in self.TX_MSGS): self.assertFalse(self._tx(make_msg(bus, addr, 8)), f"allowed TX {addr=} {bus=}") diff --git a/tests/safety/test_honda.py b/tests/safety/test_honda.py index 0be536cee8..6164189ccc 100755 --- a/tests/safety/test_honda.py +++ b/tests/safety/test_honda.py @@ -300,8 +300,8 @@ def _send_acc_hud_msg(self, pcm_gas, pcm_speed): def test_acc_hud_safety_check(self): for controls_allowed in [True, False]: self.safety.set_controls_allowed(controls_allowed) - for pcm_gas in range(0, 255): - for pcm_speed in range(0, 100): + for pcm_gas in range(255): + for pcm_speed in range(100): send = (controls_allowed and pcm_gas <= self.MAX_GAS) or (pcm_gas == 0 and pcm_speed == 0) self.assertEqual(send, self._tx(self._send_acc_hud_msg(pcm_gas, pcm_speed)))