use regex, simpler and fixes bug

pull/28386/head
Shane Smiskol 2 years ago
parent 3a647037df
commit e2a467014e
  1. 13
      selfdrive/car/hyundai/values.py

@ -1,3 +1,4 @@
import re
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum, IntFlag from enum import Enum, IntFlag
from typing import Dict, List, Optional, Set, Union from typing import Dict, List, Optional, Set, Union
@ -342,16 +343,16 @@ FINGERPRINTS = {
}], }],
} }
# TODO: use HYUNDAI_VERSION_REQUEST_LONG[1:]?
PLATFORM_CODE_REGEX = b'(?<=\xf1\x00)[A-Z]{2}[A-Za-z0-9]{0,2}'
def get_platform_codes(fw_versions: List[bytes]) -> Set[bytes]: def get_platform_codes(fw_versions: List[bytes]) -> Set[bytes]:
codes = set() codes = set()
platform_code_prefix = HYUNDAI_VERSION_REQUEST_LONG[1:]
for fw in fw_versions: for fw in fw_versions:
if platform_code_prefix not in fw: matches = re.findall(PLATFORM_CODE_REGEX, fw)
continue if len(matches):
start_idx = fw.index(platform_code_prefix) + len(platform_code_prefix) codes.add(matches[0])
code = fw[start_idx:start_idx + 4] # Hyundai platform code is max 4 bytes
codes.add(code.replace(b' ', b'').replace(b'_', b''))
return codes return codes

Loading…
Cancel
Save