Restore supported Polestar rack counter lag

This commit is contained in:
firestar5683
2026-11-16 17:23:07 -06:00
parent dcba9becc9
commit e2fe14935b
43 changed files with 103 additions and 13 deletions
+3 -2
View File
@@ -163,8 +163,9 @@ def pscm_status_validation_class(data):
# Keep pending stock status in the same validation class as the native cache.
checksum_valid = (data[0] == checksum_1_pscm_related_message(data[1], data[2]) and
data[3] == checksum_2_pscm_related_message(data[2]))
quality_valid = ((data[1] & 8) == 0 and 0x80 <= data[2] <= 0x8E and
data[1] >> 4 == data[2] & 15)
counter, replica = data[1] >> 4, data[2] & 15
quality_valid = ((data[1] & 8) == 0 and 0x80 <= data[2] <= 0x8E and counter < 15 and
(counter - replica) % 15 in (0, 1))
return data[2] >> 4, checksum_valid, quality_valid
@@ -39,7 +39,7 @@ def forwarded(frames):
GOOD = status(0x11, 0x81)
OFFSET = status(0x21, 0x81)
OFFSET = status(0x31, 0x81)
@pytest.mark.parametrize('unsupported', [
@@ -86,3 +86,27 @@ def test_missing_status_cannot_grant_host_lateral():
assert not ci.CC.lat_active
assert pscm_status_validation_class(GOOD) == (8, True, True)
assert pscm_status_validation_class(OFFSET) == (8, True, False)
@pytest.mark.parametrize('counter', range(15))
@pytest.mark.parametrize('lag', [0, 1])
def test_supported_counter_relationship_keeps_host_active(counter, lag):
ci = interface()
step(ci, 40, True)
packet = status((counter << 4) | 1, 0x80 | ((counter - lag) % 15))
assert pscm_status_validation_class(packet) == (8, True, True)
step(ci, 41, True, packet)
assert ci.CC.lat_active and not ci.CS.pscm_rearm_required
incoming, messages = step(ci, 42, False, packet)
assert messages == forwarded(incoming)
@pytest.mark.parametrize('counter,replica', [(15, 14), (15, 0), (0, 1), (0, 13), (1, 14)])
def test_other_counter_relationships_still_withdraw(counter, replica):
ci = interface()
step(ci, 40, True)
packet = status((counter << 4) | 1, 0x80 | replica)
assert pscm_status_validation_class(packet) == (8, True, False)
incoming, messages = step(ci, 41, True, packet)
assert not ci.CC.lat_active and ci.CS.pscm_rearm_required
assert messages == forwarded(incoming)
@@ -277,7 +277,7 @@ static bool volvo_cma_lca2_quality(const CANPacket_t *msg) {
return valid;
}
static bool volvo_cma_receiver_quality(const CANPacket_t *msg) {
return volvo_cma_quality(msg) && volvo_cma_lca2_quality(msg);
return volvo_cma_quality(msg, volvo_cma_lca2_mod15) && volvo_cma_lca2_quality(msg);
}
// Unchanged native status is transport evidence, never valid control evidence.
@@ -117,15 +117,15 @@ static uint32_t volvo_cma_compute_checksum(const CANPacket_t *msg) {
}
return value;
}
static bool volvo_cma_quality(const CANPacket_t *msg) {
static bool volvo_cma_quality(const CANPacket_t *msg, bool allow_counter_lag) {
bool valid = true;
if (msg->addr == 0x17U) {
// Pinned encoder uses modulo-15 replicated counters and the captured
// checksum table only covers byte2 0x80..0x8E. A fallback zero checksum
// cannot turn an unobserved receiver state into valid control evidence.
// Echo bit3 is explicitly unconstrained in the pinned checksum derivation.
// Captured source counters are equal or the replica lags one modulo 15.
// Unknown status/checksum domains and unconstrained echo bit3 stay unsupported.
const uint8_t counter = msg->data[1] >> 4U;
const uint8_t replica = msg->data[2] & 0xFU;
valid = ((msg->data[1] & 0x8U) == 0U) && (msg->data[2] >= 0x80U) && (msg->data[2] <= 0x8EU) &&
((msg->data[1] >> 4U) == (msg->data[2] & 0xFU));
(counter < 15U) && ((counter == replica) || (allow_counter_lag && (counter == ((replica + 1U) % 15U))));
}
return valid;
}
@@ -282,3 +282,68 @@ class TestPolestarCounters(unittest.TestCase):
self.assertEqual(unchanged[1], status[1])
self.assertTrue(self.tx(unchanged))
self.assertFalse(self.safety.get_controls_allowed())
def pscm_counter_status(self, counter, replica):
data = bytearray(8)
data[1] = (counter << 4) | 1
data[2] = 0x80 | replica
return cma.checksummed((0x17, data, 2))
def test_pscm_equal_and_one_count_lag_keep_permission(self):
for counter in range(15):
for lag in (0, 1):
with self.subTest(counter=counter, lag=lag):
self.reset()
self.feed()
status = self.pscm_counter_status(counter, (counter - lag) % 15)
self.assertTrue(self.rx(status))
self.assertTrue(self.safety.get_controls_allowed())
self.assertTrue(self.tx(self.relay(status)))
self.assertTrue(self.tx(self.lca_command()))
def test_pscm_other_counter_relationships_still_withdraw(self):
for counter, replica in ((15, 14), (15, 0), (0, 1), (0, 13), (1, 14)):
with self.subTest(counter=counter, replica=replica):
self.reset()
self.feed()
status = self.pscm_counter_status(counter, replica)
self.assertFalse(self.rx(status))
self.assertFalse(self.safety.get_controls_allowed())
self.safety.safety_tick()
self.assertFalse(self.safety.safety_config_valid())
self.assertTrue(self.tx(self.relay(status)))
self.safety.set_controls_allowed(True)
self.assertFalse(self.tx(self.lca_command()))
def test_pscm_lag_retains_crc_identity_and_freshness_checks(self):
status = self.pscm_counter_status(0, 14)
for pos in (0, 3):
self.reset()
self.feed()
data = bytearray(status[1])
data[pos] ^= 1
self.assertFalse(self.rx((0x17, data, 2)))
self.assertFalse(self.safety.get_controls_allowed())
for bus in (0, 1, 3, 4, 5, 6, 7):
self.reset()
self.feed(cruise=False)
self.rx((0x17, status[1], bus))
self.assertFalse(self.tx(self.relay(status)))
self.reset()
self.feed(cruise=False)
self.assertTrue(self.rx(status))
self.safety.set_timer(110000)
self.assertTrue(self.tx(self.relay(status)))
self.safety.set_timer(110001)
self.assertFalse(self.tx(self.relay(status)))
def test_pscm_lag_does_not_change_other_native_profiles(self):
try:
for param in (0, 1):
self.PARAM = param
self.setUp()
self.feed()
self.assertFalse(self.rx(self.pscm_counter_status(0, 14)))
self.assertFalse(self.safety.get_controls_allowed())
finally:
self.PARAM = 5
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -1,4 +1,4 @@
#pragma once
extern const uint8_t gitversion[29];
const uint8_t gitversion[29] = "DEV-5b5ea7b3-Pf38f504f-DEBUG";
const uint8_t gitversion[29] = "DEV-efa73f99-Pf38f504f-DEBUG";
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+1 -1
View File
@@ -1 +1 @@
DEV-5b5ea7b3-Pf38f504f-DEBUG
DEV-efa73f99-Pf38f504f-DEBUG
Binary file not shown.
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "3e5c60a01a1c91e2c04e025036427855ca11d783e40ac532dd0e6c144364e548", "schema_version": 1}
{"protected_inventory_sha256": "24f6dfb6ce84fb7dd8c559ae929bfa490e8f6181efaa74461ef1134bb593041d", "schema_version": 1}