Keep verified Ram startup discovery on normal CAN wiring

This commit is contained in:
firestar5683
2026-11-15 16:30:13 -06:00
parent ab0b4f9f08
commit 5b5ea7b330
5 changed files with 213 additions and 3 deletions
+4 -2
View File
@@ -16,6 +16,7 @@ from opendbc.car.ford.discovery import (normal_bus0_selection as ford_normal_bus
from opendbc.car.volvo.discovery import normal_bus_topology as volvo_normal_bus_topology
from opendbc.car.hyundai.discovery import normal_bus_topology
from opendbc.car.honda.discovery import normal_bus_topology as honda_normal_bus_topology
from opendbc.car.chrysler.discovery import normal_bus_topology as chrysler_normal_bus_topology
from opendbc.car.tesla.discovery import normal_bus0_topology as tesla_normal_bus0_topology
from opendbc.car.values import BRANDS
from opendbc.car.vin import get_vin, is_valid_vin, VIN_UNKNOWN
@@ -180,13 +181,14 @@ def fingerprint(can_recv: CanRecvCallable, can_send: CanSendCallable, set_obd_mu
gm_probe=gm_normal_bus0_topology if _gm_fw_uses_normal_bus0() else None,
volvo_probe=volvo_normal_bus_topology if not fw_versions.FW_QUERY_CONFIGS['volvo'].requests else None,
tesla_probe=tesla_normal_bus0_topology if _tesla_fw_uses_normal_bus0() else None) or
('honda' if honda_normal_bus_topology(normal_bus_packets) else None)) is not None:
('honda' if honda_normal_bus_topology(normal_bus_packets) else None) or
('chrysler' if chrysler_normal_bus_topology(normal_bus_packets) else None)) is not None:
set_obd_multiplexing(False)
warmup_remaining = 0.1 - (time.monotonic() - start_time)
if warmup_remaining > 0:
time.sleep(warmup_remaining)
vin_buses = (0,) if normal_brand in ('ford', 'gm', 'volvo', 'tesla') else (0, 1)
vin_buses = (0,) if normal_brand in ('ford', 'gm', 'volvo', 'tesla', 'chrysler') else (0, 1)
vin_rx_addr, vin_rx_bus, vin = get_vin(can_recv, can_send, vin_buses)
car_fw = get_fw_versions(can_recv, can_send, set_obd_multiplexing,
query_brand=normal_brand, normal_bus_only=True)
@@ -0,0 +1,25 @@
"""Normal-wiring discovery for the observed Ram PT/camera layout."""
from opendbc.car.chrysler.chryslercan import chrysler_checksum
# This chooses diagnostic routing only. Fresh VIN/FW and CAN still own identity.
NORMAL_BUS_LAYOUT = {(0, 0x23): 8, (0, 0x31): 8, (0, 0x83): 8, (2, 0x99): 8}
def normal_bus_topology(packets):
observed = set()
for packet in packets:
for frame in packet:
key = (frame.src, frame.address)
if key in NORMAL_BUS_LAYOUT:
if (len(frame.dat) != NORMAL_BUS_LAYOUT[key] or
chrysler_checksum(frame.address, None, frame.dat) != frame.dat[-1]):
return False
observed.add(key)
if observed != NORMAL_BUS_LAYOUT.keys():
return False
from opendbc.car import fw_versions
configured = fw_versions.FW_QUERY_CONFIGS['chrysler'].requests
effective = [request for brand, _, request in fw_versions.REQUESTS if brand == 'chrysler']
return all(requests and any(request.bus == 0 and not request.logging for request in requests) and
all(request.bus in (0, 4) for request in requests) for requests in (configured, effective))
@@ -0,0 +1,115 @@
import json
from dataclasses import replace
from pathlib import Path
import unittest
from unittest.mock import patch
from opendbc.car import fw_versions
from opendbc.car.can_definitions import CanData
from opendbc.car.car_helpers import fingerprint
from opendbc.car.chrysler.values import CAR
from opendbc.car.structs import CarParams
from opendbc.car.vin import VIN_UNKNOWN
def recorded_frames():
fixture = json.loads((Path(__file__).parent / 'testdata/ram_normal_discovery.json').read_text())
return [CanData(f['address'], bytes.fromhex(f['data']), f['bus']) for f in fixture['frames']]
def recorded_firmware():
fixture = json.loads((Path(__file__).parent / 'testdata/ram_normal_discovery.json').read_text())
result = []
for row in fixture['firmware']:
fw = CarParams.CarFw()
fw.ecu, fw.address, fw.bus, fw.brand = row['ecu'], row['address'], 0, 'chrysler'
fw.fwVersion = bytes.fromhex(row['version'])
result.append(fw)
return result
class TestRamNormalDiscovery(unittest.TestCase):
def test_recorded_ram_never_switches_to_obd_and_preserves_fresh_identity(self):
switches = []
firmware = recorded_firmware()
with patch.dict('os.environ', {}, clear=True), \
patch('opendbc.car.car_helpers.get_vin', return_value=(-1, -1, VIN_UNKNOWN)) as vin, \
patch('opendbc.car.car_helpers.get_fw_versions', return_value=firmware) as query, \
patch('opendbc.car.car_helpers.get_present_ecus') as present, \
patch('opendbc.car.car_helpers.get_fw_versions_ordered') as generic_query, \
patch('opendbc.car.car_helpers.can_fingerprint', return_value=(None, {})):
actual = fingerprint(lambda wait_for_one=False: [recorded_frames()], lambda frames: None, switches.append, None)
self.assertTrue(switches)
self.assertFalse(any(switches))
query.assert_called_once()
self.assertEqual(query.call_args.kwargs, {'query_brand': 'chrysler', 'normal_bus_only': True})
self.assertEqual(vin.call_args.args[2], (0,))
self.assertEqual(actual[0], CAR.RAM_1500_5TH_GEN)
self.assertEqual(len(actual[3]), 8)
self.assertTrue(actual[5])
present.assert_not_called()
generic_query.assert_not_called()
def test_routing_alone_cannot_identify_a_car_and_manual_override_survives(self):
for manual in ('', CAR.RAM_HD_5TH_GEN):
with self.subTest(manual=manual), patch.dict('os.environ', {'FINGERPRINT': str(manual)}, clear=True), \
patch('opendbc.car.car_helpers.get_vin', return_value=(-1, -1, VIN_UNKNOWN)), \
patch('opendbc.car.car_helpers.get_fw_versions', return_value=[]), \
patch('opendbc.car.car_helpers.can_fingerprint', return_value=(None, {})):
actual = fingerprint(lambda wait_for_one=False: [recorded_frames()], lambda frames: None, lambda enabled: self.assertFalse(enabled), None)
self.assertEqual(actual[0], manual or None)
def test_incomplete_bad_crc_length_bus_or_forwarded_frames_cannot_select_chrysler(self):
from opendbc.car.chrysler.discovery import normal_bus_topology
frames = recorded_frames()
self.assertTrue(normal_bus_topology([frames]))
self.assertFalse(normal_bus_topology([]))
for index, frame in enumerate(frames):
others = frames[:index] + frames[index + 1:]
with self.subTest(index=index):
self.assertFalse(normal_bus_topology([others]))
for invalid in (CanData(frame.address, frame.dat[:-1], frame.src),
CanData(frame.address, frame.dat, frame.src + 128),
CanData(frame.address, frame.dat, 1),
CanData(frame.address, frame.dat[:-1] + bytes([frame.dat[-1] ^ 1]), frame.src)):
self.assertFalse(normal_bus_topology([[*others, invalid]]))
self.assertFalse(normal_bus_topology([frames + [CanData(frames[0].address, b'', frames[0].src)]]))
def test_live_chrysler_queries_execute_only_the_declared_normal_buses(self):
switches, buses = [], []
class Query:
def __init__(self, send, recv, bus, *args):
buses.append(bus)
def get_data(self, timeout):
return {}
with patch.object(fw_versions, 'IsoTpParallelQuery', Query):
fw_versions.get_fw_versions(lambda wait_for_one=False: [], lambda frames: None, switches.append,
query_brand='chrysler', normal_bus_only=True)
self.assertIn(0, buses)
self.assertTrue(set(buses) <= {0, 4})
self.assertFalse(any(switches))
def test_changed_declared_or_effective_query_topology_rejects_normal_routing(self):
from opendbc.car.chrysler.discovery import normal_bus_topology
config = fw_versions.FW_QUERY_CONFIGS['chrysler']
bad = replace(config.requests[0], bus=1)
with patch.object(config, 'requests', [*config.requests, bad]):
self.assertFalse(normal_bus_topology([recorded_frames()]))
with patch.object(fw_versions, 'REQUESTS', [*fw_versions.REQUESTS, ('chrysler', config, bad)]):
self.assertFalse(normal_bus_topology([recorded_frames()]))
with patch.object(config, 'requests', []):
self.assertFalse(normal_bus_topology([recorded_frames()]))
def test_unrecognized_or_malformed_layout_retains_generic_discovery(self):
for frames in ([], recorded_frames()[:-1]):
switches = []
with self.subTest(frames=len(frames)), patch.dict('os.environ', {}, clear=True), \
patch('opendbc.car.car_helpers.get_vin', return_value=(-1, -1, VIN_UNKNOWN)), \
patch('opendbc.car.car_helpers.get_fw_versions_ordered', return_value=[]) as query, \
patch('opendbc.car.car_helpers.get_present_ecus', return_value=set()) as present, \
patch('opendbc.car.car_helpers.match_fw_to_car', return_value=(True, set())), \
patch('opendbc.car.car_helpers.can_fingerprint', return_value=(None, {})):
fingerprint(lambda wait_for_one=False, rows=frames: [rows], lambda frames: None, switches.append, None)
self.assertEqual(switches, [True, False])
present.assert_called_once()
query.assert_called_once()
@@ -0,0 +1,68 @@
{
"route": "a5ee50326447ea52/00000001--4ec0f95d9e",
"before_obd_mux": true,
"frames": [
{
"bus": 0,
"address": 35,
"data": "136c10000000c48c"
},
{
"bus": 0,
"address": 49,
"data": "841307d084000055"
},
{
"bus": 0,
"address": 131,
"data": "050400000000b057"
},
{
"bus": 2,
"address": 153,
"data": "07480ffd0000c016"
}
],
"firmware": [
{
"ecu": "combinationMeter",
"address": 1858,
"version": "36383531303238324147"
},
{
"ecu": "fwdRadar",
"address": 1875,
"version": "30343637323933324143"
},
{
"ecu": "eps",
"address": 1882,
"version": "36383538353130364142"
},
{
"ecu": "srs",
"address": 1860,
"version": "36383631353033344141"
},
{
"ecu": "abs",
"address": 1863,
"version": "36383534383930304143"
},
{
"ecu": "transmission",
"address": 2017,
"version": "30353134393533374143"
},
{
"ecu": "hybrid",
"address": 2018,
"version": "30353134393432304142"
},
{
"ecu": "engine",
"address": 2016,
"version": "3035313439363030414520"
}
]
}
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "4d8d6eeebb9006f02722e0b3a29793a4ee6c54cc0413a2a06f0e7480537fc112", "schema_version": 1}
{"protected_inventory_sha256": "c923ff5180d23860a07258821c49f7a63833eea03d09bd8fcfa3f57650e0ae85", "schema_version": 1}