diff --git a/opendbc_repo/opendbc/car/gm/carstate.py b/opendbc_repo/opendbc/car/gm/carstate.py index 32cca34414..e747e9e6d5 100644 --- a/opendbc_repo/opendbc/car/gm/carstate.py +++ b/opendbc_repo/opendbc/car/gm/carstate.py @@ -683,7 +683,8 @@ class CarState(CarStateBase): healthy = (healthy and self.pedal_sensor_healthy and 0 < self.pedal_sensor_ts_nanos <= now <= self.pedal_sensor_ts_nanos + 100_000_000) _, semantic, edges = self.hybrid_buttons.update( - healthy, enable_ready=healthy and not ret.brakePressed and not ret.gasPressed and not ret.regenBraking) + healthy, enable_ready=healthy and not ret.brakePressed and not ret.regenBraking and + (self.hybrid_profile.pedal or not ret.gasPressed)) from opendbc.car.gm.hybrid_cc import HybridPhysical self.volt_cc_physical = HybridPhysical(now, tuple(stamp for stamp, _ in self.hybrid_sources), self.hybrid_buttons.slot.credit_ns) diff --git a/opendbc_repo/opendbc/car/gm/hybrid_cc.py b/opendbc_repo/opendbc/car/gm/hybrid_cc.py index c7656d49fd..72434f84be 100644 --- a/opendbc_repo/opendbc/car/gm/hybrid_cc.py +++ b/opendbc_repo/opendbc/car/gm/hybrid_cc.py @@ -54,7 +54,7 @@ class PhysicalSlot: return True def observe(self, stamp: int, raw: bytes, *, bus: int): - if stamp <= 0 or stamp <= self.observed_ns: + if bus != 0 or stamp <= 0 or stamp <= self.observed_ns: return if self.observed_ns and stamp - self.observed_ns > TTL_NS: self.needs_requalification = True @@ -62,7 +62,7 @@ class PhysicalSlot: self.clear() if self.needs_requalification: return - if bus != 0 or len(raw) != 7 or raw[:3] != bytes(3) or raw[3] not in (1, 0x41) or raw[4] > 3: + if len(raw) != 7 or raw[:3] != bytes(3) or raw[3] not in (1, 0x41) or raw[4] > 3: return phase = UNIQUE_NEUTRAL.get(raw[5] * 256 + raw[6]) if phase is None or phase == self.last_admitted_phase: @@ -107,7 +107,7 @@ def physical_semantic(raw: bytes, *, bus: int) -> str | None: 0x2ACD: 'main', 0x20EF: 'main', 0x60AF: 'cancel', 0x6A8D: 'cancel'} if word in unique: return unique[word] - if raw == standard_set_bytes(raw[4]): + if raw == standard_set_bytes(raw[4]) or (raw[4] == 0 and word in (0x30DF, 0x35CE, 0x3ABD, 0x3FAC)): return 'set' return None diff --git a/opendbc_repo/opendbc/car/gm/tests/test_hybrid_physical_set.py b/opendbc_repo/opendbc/car/gm/tests/test_hybrid_physical_set.py new file mode 100644 index 0000000000..76fb53bb91 --- /dev/null +++ b/opendbc_repo/opendbc/car/gm/tests/test_hybrid_physical_set.py @@ -0,0 +1,142 @@ +"""Physical Hybrid SET uses a checksum phase independent of the standard counter.""" +import pytest + +from opendbc.car.gm.hybrid_cc import HybridButtons, physical_semantic, standard_set_bytes +from opendbc.safety.tests import test_gm_hybrid_cc as native_fixture + +SET_WORDS = (0x30DF, 0x35CE, 0x3ABD, 0x3FAC) + + +def physical_set(word, prefix=0x41): + return bytes((0, 0, 0, prefix, 0, word >> 8, word & 255)) + + +@pytest.mark.parametrize('prefix', [1, 0x41]) +@pytest.mark.parametrize('word', SET_WORDS) +def test_physical_set_checksum_phases_decode_as_one_press(word, prefix): + assert physical_semantic(physical_set(word, prefix), bus=0) == 'set' + for bus in (2, 128, 130): + assert physical_semantic(physical_set(word, prefix), bus=bus) is None + + +@pytest.mark.parametrize('echo_first', [False, True]) +def test_forwarded_echoes_cannot_consume_physical_button_observation(echo_first): + buttons = HybridButtons() + edges = [] + words = (0x15EE, *SET_WORDS, 0x1FCC) + for tick, word in enumerate(words): + packet = physical_set(word) + frames = [(0x1E1, packet, bus) for bus in ((130, 0) if echo_first else (0, 130))] + buttons.observe_packets([(1_000_000_000 + tick * 30_000_000, frames)]) + edges.extend(buttons.update(True)[2]) + assert edges == [(1, 3), (3, 1)] + + +@pytest.mark.parametrize('counter', range(4)) +def test_existing_standard_set_and_unknown_packet_guards(counter): + raw = standard_set_bytes(counter) + assert physical_semantic(raw, bus=0) == 'set' + for i in range(7): + damaged = bytearray(raw) + damaged[i] ^= 0x80 + assert physical_semantic(bytes(damaged), bus=0) is None + # Custom checksum phase is valid only with its observed zero counter. + assert physical_semantic(bytes.fromhex('000000410130df'), bus=0) is None + + +@pytest.mark.parametrize('word', [0xE800, 0xE801, 0xE802, 0xE803]) +@pytest.mark.parametrize('prefix', [1, 0x41]) +def test_native_physical_set_arms_only_on_release(word, prefix): + case = native_fixture.TestGmHybridCc(methodName='runTest') + case.setUp() + try: + for set_word in SET_WORDS: + case.reset(word) + case.warm() + case.slot(0x15EE) + case.rx(0x1E1, physical_set(set_word, prefix)) + assert not case.safety.get_controls_allowed() + case.slot(0x1FCC) + assert case.safety.get_controls_allowed() + finally: + case.tearDown() + + +@pytest.mark.parametrize('guard', ['brake', 'regen', 'main', 'gear', 'sensor']) +def test_native_physical_set_cannot_bypass_physical_guards(guard): + case = native_fixture.TestGmHybridCc(methodName='runTest') + case.setUp() + try: + case.reset(0xE802) + kwargs = {'brake': {'brake': True}, 'gas': {'gas': True}, 'regen': {'regen': True}, + 'main': {'main': False}, 'gear': {'gear': 2}, 'sensor': {'missing': 0x201}}[guard] + case.warm(**kwargs) + case.rx(0x1E1, physical_set(0x35CE)) + case.slot(0x1FCC) + assert not case.safety.get_controls_allowed() + finally: + case.tearDown() + + +@pytest.mark.parametrize('alternative', [0, 32]) +@pytest.mark.parametrize('sensor_override', [False, True]) +def test_pedal_set_during_accelerator_override_retains_intent_without_actuation(alternative, sensor_override): + case = native_fixture.TestGmHybridCc(methodName='runTest') + case.setUp() + try: + case.reset(0xE802, alternative) + case.warm() + kwargs = {'tracks': (1000, 500)} if sensor_override else {'gas': True} + case.feed(**kwargs) + case.rx(0x1E1, standard_set_bytes(0)) + assert not case.safety.get_controls_allowed() + case.slot(0x1FCC) + assert case.safety.get_controls_allowed() + assert not case.pedal() + if alternative: + assert case.safety.aol_get_permission_mask() == 1 + case.advance(20_000) + case.feed(**kwargs) + assert case.safety.get_controls_allowed() + assert not case.pedal() + case.advance(20_000) + case.feed() + assert case.pedal() + case.slot(0x60AF) + case.advance(20_000) + case.feed() + assert not case.safety.get_controls_allowed() + assert not case.pedal(counter=1) + finally: + case.tearDown() + + +def test_actual_pedal_carstate_emits_set_release_during_override(): + from opendbc.can import CANPacker + from opendbc.car import Bus + from opendbc.car.gm.gmcan import pedal_crc + from opendbc.car.gm.interface import CarInterface + from opendbc.car.gm.tests.test_cc_gateway_stock import pt_frames + from opendbc.car.gm.tests.test_ordinary_cc import malibu_hybrid_params + from opendbc.car.gm.values import DBC + cp = malibu_hybrid_params(pedal=True, removed=True, alternate=True) + ci = CarInterface(cp) + packer = CANPacker(DBC[cp.carFingerprint][Bus.pt]) + for tick in range(45): + now = 1_000_000_000 + tick * 10_000_000 + gas = 30 <= tick < 42 + frames = [f for f in pt_frames(packer, gas=gas) if f[0] != 0x1E1] + button = physical_set(SET_WORDS[tick % 4]) if 34 <= tick < 38 else physical_set(0x15EE if tick % 2 else 0x1FCC) + sensor = bytearray((1000 if gas else 604).to_bytes(2, 'big') + + (500 if gas else 304).to_bytes(2, 'big') + bytes((tick % 16, 0))) + sensor[-1] = pedal_crc(sensor) + frames += [packer.make_can_msg('EBCMRegenPaddle', 0, {}), + packer.make_can_msg('EBCMBrakePedalPosition', 0, {}), (0x201, bytes(sensor), 0), + (0x1E1, button, 130), (0x1E1, button, 0)] + state = ci.update([(now, frames)]) + if tick == 38: + assert state.gasPressed + assert state.buttonEnable + assert any(str(b.type) == 'decelCruise' and not b.pressed for b in state.buttonEvents) + elif tick > 38: + assert not state.buttonEnable diff --git a/opendbc_repo/opendbc/safety/modes/gm_hybrid_cc.h b/opendbc_repo/opendbc/safety/modes/gm_hybrid_cc.h index f0bd0c80fa..a231f9572c 100644 --- a/opendbc_repo/opendbc/safety/modes/gm_hybrid_cc.h +++ b/opendbc_repo/opendbc/safety/modes/gm_hybrid_cc.h @@ -78,9 +78,10 @@ static bool gm_hybrid_current(void) { return current; } -static bool gm_hybrid_ready(void) { +static bool gm_hybrid_ready(const bool allow_gas) { return gm_hybrid_current() && gm_hybrid_main && gm_hybrid_drive && gm_hybrid_eps && - !brake_pressed && !gas_pressed && !gm_hybrid_regen && (!gm_hybrid_pedal || !gm_hybrid_sensor_gas); + !brake_pressed && !gm_hybrid_regen && + (allow_gas || (!gas_pressed && (!gm_hybrid_pedal || !gm_hybrid_sensor_gas))); } static uint8_t gm_hybrid_request(void) { @@ -124,6 +125,8 @@ static uint8_t gm_hybrid_semantic(const CANPacket_t *msg) { else if ((word == 0x55AEU) || (word == 0x5F8CU)) { button = 2U; } else if ((word == 0x2ACDU) || (word == 0x20EFU)) { button = 5U; } else if ((word == 0x60AFU) || (word == 0x6A8DU)) { button = 6U; } + else if ((msg->data[4] == 0U) && ((word == 0x30DFU) || (word == 0x35CEU) || + (word == 0x3ABDU) || (word == 0x3FACU))) { button = 3U; } else { const unsigned int checksum = 0xDFU + ((unsigned int)msg->data[4] * 0x4EFU); if ((msg->data[3] == 1U) && (word == (0x3000U | checksum))) { button = 3U; } @@ -183,7 +186,7 @@ static void gm_hybrid_rx(const CANPacket_t *msg) { if (i == 2U) { gm_hybrid_credit = false; if (gm_hybrid_requalify) { - if (gm_hybrid_ready()) { + if (gm_hybrid_ready(false)) { gm_hybrid_phase_seen = false; gm_hybrid_requalify = false; gm_hybrid_button_seen = false; gm_hybrid_semantic_reset = true; } @@ -199,7 +202,7 @@ static void gm_hybrid_rx(const CANPacket_t *msg) { const uint8_t button = gm_hybrid_semantic(msg); if (button != 0U) { if (!gm_hybrid_button_seen) { gm_hybrid_semantic_reset = button != 1U; } - else if (!gm_hybrid_stock && !gm_hybrid_semantic_reset && gm_hybrid_ready() && + else if (!gm_hybrid_stock && !gm_hybrid_semantic_reset && gm_hybrid_ready(gm_hybrid_pedal) && (((button == 2U) && (gm_hybrid_button != 2U)) || ((button == 1U) && (gm_hybrid_button == 3U)))) { controls_allowed = true; } else { @@ -232,7 +235,7 @@ static void gm_hybrid_rx(const CANPacket_t *msg) { gm_hybrid_clear_credit(); controls_allowed = false; } // Driver intervention withdraws actuation, not the physical cancellation slot. - if (brake_pressed || gm_hybrid_regen || ((gm_hybrid_stock || gm_hybrid_pedal) && gas_pressed) || (gm_hybrid_pedal && gm_hybrid_sensor_gas)) { + if (brake_pressed || gm_hybrid_regen || (gm_hybrid_stock && gas_pressed)) { controls_allowed = false; } } @@ -289,7 +292,7 @@ static bool gm_hybrid_tx(const CANPacket_t *msg) { const uint8_t counter = msg->data[4] & 15U; const int difference = track1 - (2 * track2); const bool inactive = !enabled && (track1 == 0) && (track2 == 0); - const bool active = enabled && gm_hybrid_ready() && !gm_hybrid_stock_active && get_longitudinal_allowed() && + const bool active = enabled && gm_hybrid_ready(false) && !gm_hybrid_stock_active && get_longitudinal_allowed() && (track1 >= 604) && (track1 <= 2633) && (track2 >= 304) && (track2 <= 1316) && (difference >= -16) && (difference <= 16); allowed = (inactive || active) && ((msg->data[4] & 0x70U) == 0U) && diff --git a/panda/board/obj/body_h7.bin.signed b/panda/board/obj/body_h7.bin.signed index 6d2e255b61..660069766c 100644 Binary files a/panda/board/obj/body_h7.bin.signed and b/panda/board/obj/body_h7.bin.signed differ diff --git a/panda/board/obj/panda.bin.signed b/panda/board/obj/panda.bin.signed index 39e9aecf57..de119760b1 100644 Binary files a/panda/board/obj/panda.bin.signed and b/panda/board/obj/panda.bin.signed differ diff --git a/panda/board/obj/panda_can_ignition_only.bin.signed b/panda/board/obj/panda_can_ignition_only.bin.signed index dda42a082b..73b90b0f82 100644 Binary files a/panda/board/obj/panda_can_ignition_only.bin.signed and b/panda/board/obj/panda_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_h7.bin.signed b/panda/board/obj/panda_h7.bin.signed index d516cc17f3..318d910ada 100644 Binary files a/panda/board/obj/panda_h7.bin.signed and b/panda/board/obj/panda_h7.bin.signed differ diff --git a/panda/board/obj/panda_h7_can_ignition_only.bin.signed b/panda/board/obj/panda_h7_can_ignition_only.bin.signed index b55cb45828..59f4d838fb 100644 Binary files a/panda/board/obj/panda_h7_can_ignition_only.bin.signed and b/panda/board/obj/panda_h7_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_h7_hkg_remote.bin.signed b/panda/board/obj/panda_h7_hkg_remote.bin.signed index b4f33633af..13bccd7a4e 100644 Binary files a/panda/board/obj/panda_h7_hkg_remote.bin.signed and b/panda/board/obj/panda_h7_hkg_remote.bin.signed differ diff --git a/panda/board/obj/panda_h7_hkg_remote_can_ignition_only.bin.signed b/panda/board/obj/panda_h7_hkg_remote_can_ignition_only.bin.signed index 5f58acdf6d..6c162ea906 100644 Binary files a/panda/board/obj/panda_h7_hkg_remote_can_ignition_only.bin.signed and b/panda/board/obj/panda_h7_hkg_remote_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_h7_remote.bin.signed b/panda/board/obj/panda_h7_remote.bin.signed index 5b950ee874..1f34de922a 100644 Binary files a/panda/board/obj/panda_h7_remote.bin.signed and b/panda/board/obj/panda_h7_remote.bin.signed differ diff --git a/panda/board/obj/panda_h7_remote_can_ignition_only.bin.signed b/panda/board/obj/panda_h7_remote_can_ignition_only.bin.signed index cf573410e8..27f2cdddcf 100644 Binary files a/panda/board/obj/panda_h7_remote_can_ignition_only.bin.signed and b/panda/board/obj/panda_h7_remote_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_h7_tesla_wake.bin.signed b/panda/board/obj/panda_h7_tesla_wake.bin.signed index 5c90721574..293d8c39f8 100644 Binary files a/panda/board/obj/panda_h7_tesla_wake.bin.signed and b/panda/board/obj/panda_h7_tesla_wake.bin.signed differ diff --git a/panda/board/obj/panda_h7_tesla_wake_can_ignition_only.bin.signed b/panda/board/obj/panda_h7_tesla_wake_can_ignition_only.bin.signed index 4acff8fcad..ac1162a34f 100644 Binary files a/panda/board/obj/panda_h7_tesla_wake_can_ignition_only.bin.signed and b/panda/board/obj/panda_h7_tesla_wake_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_hkg_remote.bin.signed b/panda/board/obj/panda_hkg_remote.bin.signed index 6c46db66e0..4603ce260b 100644 Binary files a/panda/board/obj/panda_hkg_remote.bin.signed and b/panda/board/obj/panda_hkg_remote.bin.signed differ diff --git a/panda/board/obj/panda_hkg_remote_can_ignition_only.bin.signed b/panda/board/obj/panda_hkg_remote_can_ignition_only.bin.signed index 2625122885..21475502e4 100644 Binary files a/panda/board/obj/panda_hkg_remote_can_ignition_only.bin.signed and b/panda/board/obj/panda_hkg_remote_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_remote.bin.signed b/panda/board/obj/panda_remote.bin.signed index 4f01bf6a07..09fa022fd4 100644 Binary files a/panda/board/obj/panda_remote.bin.signed and b/panda/board/obj/panda_remote.bin.signed differ diff --git a/panda/board/obj/panda_remote_can_ignition_only.bin.signed b/panda/board/obj/panda_remote_can_ignition_only.bin.signed index 5d35b78ca0..8f9e671b06 100644 Binary files a/panda/board/obj/panda_remote_can_ignition_only.bin.signed and b/panda/board/obj/panda_remote_can_ignition_only.bin.signed differ diff --git a/panda/board/obj/panda_tesla_wake.bin.signed b/panda/board/obj/panda_tesla_wake.bin.signed index 1918b10da2..f082426ed2 100644 Binary files a/panda/board/obj/panda_tesla_wake.bin.signed and b/panda/board/obj/panda_tesla_wake.bin.signed differ diff --git a/panda/board/obj/panda_tesla_wake_can_ignition_only.bin.signed b/panda/board/obj/panda_tesla_wake_can_ignition_only.bin.signed index 10c176bdd3..f8e53ed8c4 100644 Binary files a/panda/board/obj/panda_tesla_wake_can_ignition_only.bin.signed and b/panda/board/obj/panda_tesla_wake_can_ignition_only.bin.signed differ diff --git a/prebuilt.json b/prebuilt.json index 92e288e7f3..255f8a5f5e 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "856baef0fe09a0676da055fec208181f30db2f6157559422535d69e44f24f578", "schema_version": 1} +{"protected_inventory_sha256": "0117eca71f64448778816c1a20f466a483e6e4e434cb519ebc31580c98ff83a2", "schema_version": 1}