Skip to content

Commit 9ddab33

Browse files
committed
screw it, we can split vin.py and vin_query.py later. this is nicer
1 parent 320d96d commit 9ddab33

File tree

6 files changed

+24
-25
lines changed

6 files changed

+24
-25
lines changed

opendbc/car/fw_query_definitions.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ def p16(val):
2828
return struct.pack("!H", val)
2929

3030

31+
@dataclass
32+
class Vin:
33+
vin: str
34+
wmi: str = field(init=False)
35+
vds: str = field(init=False)
36+
vis: str = field(init=False)
37+
38+
def __post_init__(self):
39+
# parses VIN in accordance with North America standard >2000 vehicles:
40+
# https://en.wikipedia.org/wiki/Vehicle_identification_number#Components
41+
self.wmi = self.vin[:3] # World Manufacturer Identifier
42+
self.vds = self.vin[3:9] # Vehicle Descriptor Section
43+
self.vis = self.vin[9:17] # Vehicle Identifier Section
44+
45+
3146
class StdQueries:
3247
# FW queries
3348
TESTER_PRESENT_REQUEST = bytes([uds.SERVICE_TYPE.TESTER_PRESENT, 0x0])
@@ -103,7 +118,7 @@ class FwQueryConfig:
103118
extra_ecus: list[tuple[Ecu, int, int | None]] = field(default_factory=list)
104119
# Function a brand can implement to provide better fuzzy matching. Takes in FW versions and VIN,
105120
# returns set of candidates. Only will match if one candidate is returned
106-
match_fw_to_car_fuzzy: Callable[[LiveFwVersions, str, OfflineFwVersions], set[str]] | None = None
121+
match_fw_to_car_fuzzy: Callable[[LiveFwVersions, Vin, OfflineFwVersions], set[str]] | None = None
107122

108123
def __post_init__(self):
109124
# Asserts that a request exists if extra ecus are used

opendbc/car/fw_versions.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from opendbc.car.structs import CarParams
1111
from opendbc.car.ecu_addrs import get_ecu_addrs
1212
from opendbc.car.fingerprints import FW_VERSIONS
13-
from opendbc.car.fw_query_definitions import ESSENTIAL_ECUS, AddrType, EcuAddrBusType, FwQueryConfig, LiveFwVersions, OfflineFwVersions
13+
from opendbc.car.fw_query_definitions import ESSENTIAL_ECUS, AddrType, EcuAddrBusType, FwQueryConfig, LiveFwVersions, OfflineFwVersions, Vin
1414
from opendbc.car.interfaces import get_interface_attr
1515
from opendbc.car.isotp_parallel_query import IsoTpParallelQuery
1616

@@ -161,7 +161,7 @@ def match_fw_to_car(fw_versions: list[CarParams.CarFw], vin: str, allow_exact: b
161161
# If specified and no matches so far, fall back to brand's fuzzy fingerprinting function
162162
config = FW_QUERY_CONFIGS[brand]
163163
if not exact_match and not len(matches) and config.match_fw_to_car_fuzzy is not None:
164-
matches |= config.match_fw_to_car_fuzzy(fw_versions_dict, vin, VERSIONS[brand])
164+
matches |= config.match_fw_to_car_fuzzy(fw_versions_dict, Vin(vin), VERSIONS[brand])
165165

166166
if len(matches):
167167
return exact_match, matches

opendbc/car/tests/test_fw_fingerprint.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from opendbc.car.car_helpers import interfaces
88
from opendbc.car.structs import CarParams
99
from opendbc.car.fingerprints import FW_VERSIONS
10+
from opendbc.car.fw_query_definitions import Vin
1011
from opendbc.car.fw_versions import FW_QUERY_CONFIGS, FUZZY_EXCLUDE_ECUS, VERSIONS, build_fw_dict, \
1112
match_fw_to_car, get_brand_ecu_matches, get_fw_versions, get_present_ecus
1213
from opendbc.car.vin import get_vin
@@ -64,7 +65,7 @@ def test_custom_fuzzy_match(self, brand, car_model, ecus):
6465
address=addr, subAddress=0 if sub_addr is None else sub_addr))
6566
CP.carFw = fw
6667
_, matches = match_fw_to_car(CP.carFw, CP.carVin, allow_exact=False, log=False)
67-
brand_matches = config.match_fw_to_car_fuzzy(build_fw_dict(CP.carFw), CP.carVin, VERSIONS[brand])
68+
brand_matches = config.match_fw_to_car_fuzzy(build_fw_dict(CP.carFw), Vin(CP.carVin), VERSIONS[brand])
6869

6970
# If both have matches, they must agree
7071
if len(matches) == 1 and len(brand_matches) == 1:

opendbc/car/vin.py

-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import re
2-
from dataclasses import dataclass, field
32

43
from opendbc.car import uds
54
from opendbc.car.carlog import carlog
@@ -10,21 +9,6 @@
109
VIN_RE = "[A-HJ-NPR-Z0-9]{17}"
1110

1211

13-
@dataclass
14-
class Vin:
15-
vin: str
16-
wmi: str = field(init=False)
17-
vds: str = field(init=False)
18-
vis: str = field(init=False)
19-
20-
def __post_init__(self):
21-
# parses VIN in accordance with North America standard >2000 vehicles:
22-
# https://en.wikipedia.org/wiki/Vehicle_identification_number#Components
23-
self.wmi = self.vin[:3] # World Manufacturer Identifier
24-
self.vds = self.vin[3:9] # Vehicle Descriptor Section
25-
self.vis = self.vin[9:17] # Vehicle Identifier Section
26-
27-
2812
def is_valid_vin(vin: str):
2913
return re.fullmatch(VIN_RE, vin) is not None
3014

opendbc/car/volkswagen/tests/test_volkswagen.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33

44
from opendbc.car.structs import CarParams
5+
from opendbc.car.fw_query_definitions import Vin
56
from opendbc.car.volkswagen.values import CAR, FW_QUERY_CONFIG, WMI
67
from opendbc.car.volkswagen.fingerprints import FW_VERSIONS
78

@@ -46,7 +47,7 @@ def test_custom_fuzzy_fingerprinting(self, subtests):
4647
vin = ["0"] * 17
4748
vin[0:3] = wmi
4849
vin[6:8] = chassis_code
49-
vin = "".join(vin)
50+
vin = Vin("".join(vin))
5051

5152
# Check a few FW cases - expected, unexpected
5253
for radar_fw in random.sample(all_radar_fw, 5) + [b'\xf1\x875Q0907572G \xf1\x890571', b'\xf1\x877H9907572AA\xf1\x890396']:

opendbc/car/volkswagen/values.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from opendbc.car.docs_definitions import CarFootnote, CarHarness, CarDocs, CarParts, Column, \
99
Device
1010
from opendbc.car.fw_query_definitions import EcuAddrSubAddr, FwQueryConfig, Request, p16
11-
from opendbc.car.vin import Vin
1211

1312
Ecu = structs.CarParams.Ecu
1413
NetworkLocation = structs.CarParams.NetworkLocation
@@ -454,8 +453,7 @@ def match_fw_to_car_fuzzy(live_fw_versions, vin, offline_fw_versions) -> set[str
454453

455454
# Check the WMI and chassis code to determine the platform
456455
# https://www.clubvw.org.au/vwreference/vwvin
457-
vin_obj = Vin(vin)
458-
chassis_code = vin_obj.vds[3:5]
456+
chassis_code = vin.vds[3:5]
459457

460458
for platform in CAR:
461459
valid_ecus = set()
@@ -475,7 +473,7 @@ def match_fw_to_car_fuzzy(live_fw_versions, vin, offline_fw_versions) -> set[str
475473
if valid_ecus != CHECK_FUZZY_ECUS:
476474
continue
477475

478-
if vin_obj.wmi in platform.config.wmis and chassis_code in platform.config.chassis_codes:
476+
if vin.wmi in platform.config.wmis and chassis_code in platform.config.chassis_codes:
479477
candidates.add(platform)
480478

481479
return {str(c) for c in candidates}

0 commit comments

Comments
 (0)