mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-08-21 16:23:46 +08:00
add script to automatically format fingerprints file (#30792)
* Add script * fix script * fix script * fix for subaddr * run the script * old style can fingerprints * cleanup * sort imports, make executable, fix path * newline w/o newline * match og can formatting * match og formatting * generate template once * standard name * less nested * can fingerprints comments * fix spacing * no need for PLATFORM_TO_ENUM_NAME! * prep for PRs * comments for all, add honda comments * Auto-generated fingerprint PR from fuzzy fingerprinting cars * Revert "Auto-generated fingerprint PR from fuzzy fingerprinting cars" This reverts commit 97bc9e3bdb9d819dcbe684ceba92ea702d40eaf0. * even closer to original * readd this comment * and run script * add to precommit * add comments * add comments * add to release * use for auto fingerprint * disable precommit for now --------- Co-authored-by: Shane Smiskol <shane@smiskol.com> old-commit-hash: 3713e4d5eaed783738ea8ecdbd3b7e6abaa974c0
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
from openpilot.common.basedir import BASEDIR
|
||||
from collections import defaultdict
|
||||
from typing import Optional
|
||||
from openpilot.selfdrive.debug.format_fingerprints import format_brand_fw_versions
|
||||
|
||||
from openpilot.tools.lib.logreader import MultiLogIterator
|
||||
from openpilot.tools.lib.route import Route
|
||||
@@ -16,31 +18,6 @@ PLATFORM_TO_PYTHON_CAR_NAME = {brand: {car.value: car.name for car in ALL_CARS[b
|
||||
BRAND_TO_PLATFORMS = {brand: [car.value for car in ALL_CARS[brand]] for brand in ALL_CARS}
|
||||
PLATFORM_TO_BRAND = dict(sum([[(platform, brand) for platform in BRAND_TO_PLATFORMS[brand]] for brand in BRAND_TO_PLATFORMS], []))
|
||||
|
||||
def add_fw_versions(brand, platform, new_fw_versions):
|
||||
filename = f"{BASEDIR}/selfdrive/car/{brand}/fingerprints.py"
|
||||
with open(filename, "r") as f:
|
||||
values_py = f.read()
|
||||
|
||||
for key in new_fw_versions.keys():
|
||||
ecu, addr, subAddr = key
|
||||
fw_version = new_fw_versions[key]
|
||||
|
||||
platform_start = values_py.index(f"CAR.{PLATFORM_TO_PYTHON_CAR_NAME[brand][platform]}: {{")
|
||||
|
||||
start = values_py.index(f"(Ecu.{ecu}, {hex(addr)}, {subAddr}): [", platform_start)
|
||||
|
||||
try:
|
||||
end_str = "],\n"
|
||||
end = values_py.index(end_str, start)
|
||||
except ValueError:
|
||||
end_str = "]\n"
|
||||
end = values_py.index(end_str, start)
|
||||
|
||||
values_py = values_py[:end] + f" {fw_version},\n " + values_py[end:]
|
||||
|
||||
with open(filename, "w") as f:
|
||||
f.write(values_py)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Auto fingerprint from a route")
|
||||
@@ -55,6 +32,8 @@ if __name__ == "__main__":
|
||||
carVin = None
|
||||
carPlatform = None
|
||||
|
||||
platform: Optional[str] = None
|
||||
|
||||
for msg in lr:
|
||||
if msg.which() == "carParams":
|
||||
carFw = msg.carParams.carFw
|
||||
@@ -75,31 +54,26 @@ if __name__ == "__main__":
|
||||
print("Using platform from route")
|
||||
platform = carPlatform
|
||||
else:
|
||||
raise Exception("unable to determine platform, try manually specifying the fingerprint.")
|
||||
platform = None
|
||||
else:
|
||||
platform = list(possible_platforms)[0]
|
||||
|
||||
else:
|
||||
platform = args.platform
|
||||
|
||||
if platform is None:
|
||||
raise Exception("unable to determine platform, try manually specifying the fingerprint.")
|
||||
|
||||
print("Attempting to add fw version for: ", platform)
|
||||
|
||||
fw_versions: dict[str, dict[tuple, list[bytes]]] = defaultdict(lambda: defaultdict(list))
|
||||
brand = PLATFORM_TO_BRAND[platform]
|
||||
|
||||
new_fw_versions = {}
|
||||
|
||||
for fw in carFw:
|
||||
if fw.brand == brand:
|
||||
if fw.brand == brand and not fw.logging:
|
||||
addr = fw.address
|
||||
subAddr = None if fw.subAddress == 0 else fw.subAddress
|
||||
key = (fw.ecu.raw, addr, subAddr)
|
||||
|
||||
if key in ALL_FW_VERSIONS[brand][platform]:
|
||||
fw_versions = set(ALL_FW_VERSIONS[brand][platform][key])
|
||||
if fw.fwVersion not in fw_versions:
|
||||
new_fw_versions[(fw.ecu, addr, subAddr)] = fw.fwVersion
|
||||
fw_versions[platform][key].append(fw.fwVersion)
|
||||
|
||||
if not new_fw_versions:
|
||||
print("No new fw versions found...")
|
||||
|
||||
add_fw_versions(brand, platform, new_fw_versions)
|
||||
format_brand_fw_versions(brand, fw_versions)
|
||||
|
||||
Executable
+86
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env python3
|
||||
import jinja2
|
||||
import os
|
||||
|
||||
from cereal import car
|
||||
from openpilot.common.basedir import BASEDIR
|
||||
from openpilot.selfdrive.car.interfaces import get_interface_attr
|
||||
|
||||
Ecu = car.CarParams.Ecu
|
||||
|
||||
CARS = get_interface_attr('CAR')
|
||||
FW_VERSIONS = get_interface_attr('FW_VERSIONS')
|
||||
FINGERPRINTS = get_interface_attr('FINGERPRINTS')
|
||||
ECU_NAME = {v: k for k, v in Ecu.schema.enumerants.items()}
|
||||
|
||||
FINGERPRINTS_PY_TEMPLATE = jinja2.Template("""
|
||||
{%- if FINGERPRINTS[brand] %}
|
||||
# ruff: noqa: E501
|
||||
{% endif %}
|
||||
{% if FW_VERSIONS[brand] %}
|
||||
from cereal import car
|
||||
{% endif %}
|
||||
from openpilot.selfdrive.car.{{brand}}.values import CAR
|
||||
{% if FW_VERSIONS[brand] %}
|
||||
|
||||
Ecu = car.CarParams.Ecu
|
||||
{% endif %}
|
||||
{% if comments +%}
|
||||
{{ comments | join() }}
|
||||
{% endif %}
|
||||
{% if FINGERPRINTS[brand] %}
|
||||
|
||||
FINGERPRINTS = {
|
||||
{% for car, fingerprints in FINGERPRINTS[brand].items() %}
|
||||
CAR.{{car.name}}: [{
|
||||
{% for fingerprint in fingerprints %}
|
||||
{% if not loop.first %}
|
||||
{{ "{" }}
|
||||
{% endif %}
|
||||
{% for key, value in fingerprint.items() %}{{key}}: {{value}}{% if not loop.last %}, {% endif %}{% endfor %}
|
||||
|
||||
}{% if loop.last %}]{% endif %},
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
}
|
||||
{% endif %}
|
||||
{% if FW_VERSIONS[brand] %}
|
||||
|
||||
FW_VERSIONS = {
|
||||
{% for car, _ in FW_VERSIONS[brand].items() %}
|
||||
CAR.{{car.name}}: {
|
||||
{% for key, fw_versions in FW_VERSIONS[brand][car].items() %}
|
||||
(Ecu.{{ECU_NAME[key[0]]}}, 0x{{"%0x" | format(key[1] | int)}}, \
|
||||
{% if key[2] %}0x{{"%0x" | format(key[2] | int)}}{% else %}{{key[2]}}{% endif %}): [
|
||||
{% for fw_version in fw_versions %}
|
||||
{{fw_version}},
|
||||
{% endfor %}
|
||||
],
|
||||
{% endfor %}
|
||||
},
|
||||
{% endfor %}
|
||||
}
|
||||
{% endif %}
|
||||
""", trim_blocks=True)
|
||||
|
||||
|
||||
def format_brand_fw_versions(brand, extra_fw_versions: None | dict[str, dict[tuple, list[bytes]]] = None):
|
||||
fw_versions = FW_VERSIONS.copy()
|
||||
# TODO: this modifies FW_VERSIONS in place
|
||||
if extra_fw_versions is not None:
|
||||
for platform, ecus in extra_fw_versions.items():
|
||||
for ecu, fws in ecus.items():
|
||||
fw_versions[brand][platform][ecu] += set(fws) - set(fw_versions[brand][platform][ecu])
|
||||
|
||||
fingerprints_file = os.path.join(BASEDIR, f"selfdrive/car/{brand}/fingerprints.py")
|
||||
with open(fingerprints_file, "r") as f:
|
||||
comments = [line for line in f.readlines() if line.startswith("#") and "noqa" not in line]
|
||||
|
||||
with open(fingerprints_file, "w") as f:
|
||||
f.write(FINGERPRINTS_PY_TEMPLATE.render(brand=brand, comments=comments, ECU_NAME=ECU_NAME,
|
||||
FINGERPRINTS=FINGERPRINTS, FW_VERSIONS=fw_versions))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for brand in FW_VERSIONS.keys():
|
||||
format_brand_fw_versions(brand)
|
||||
Reference in New Issue
Block a user