{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "kj/filesystem-disk-unix.c++:1703: warning: PWD environment variable doesn't match current directory; pwd = /home/batman\n"
     ]
    }
   ],
   "source": [
    "from cereal import car\n",
    "from openpilot.selfdrive.car.subaru.values import CAR, SubaruFlags\n",
    "from openpilot.selfdrive.car.subaru.fingerprints import FW_VERSIONS\n",
    "\n",
    "TEST_PLATFORMS = set(CAR) - CAR.with_flags(SubaruFlags.PREGLOBAL)\n",
    "\n",
    "Ecu = car.CarParams.Ecu\n",
    "\n",
    "FW_BY_ECU = {platform: {ecu: versions for (ecu, addr, sub_addr), versions in fw_versions.items()} for platform, fw_versions in FW_VERSIONS.items()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PLATFORM_CODES = {\n",
    "    Ecu.abs: {\n",
    "        0: {\n",
    "            b'\\xa5': [CAR.SUBARU_ASCENT, CAR.SUBARU_ASCENT_2023],\n",
    "            b'\\xa2': [CAR.SUBARU_IMPREZA, CAR.SUBARU_IMPREZA_2020, CAR.SUBARU_CROSSTREK_HYBRID],\n",
    "            b'\\xa1': [CAR.SUBARU_OUTBACK, CAR.SUBARU_LEGACY, CAR.SUBARU_OUTBACK_2023],\n",
    "            b'\\xa3': [CAR.SUBARU_FORESTER, CAR.SUBARU_FORESTER_HYBRID, CAR.SUBARU_FORESTER_2022],\n",
    "            b'z': [CAR.SUBARU_IMPREZA],\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "YEAR_CODES = {\n",
    "    Ecu.abs: {\n",
    "        2: {\n",
    "            b'\\x18': 2018,\n",
    "            b'\\x19': 2019,\n",
    "            b'\\x20': 2020,\n",
    "            b'\\x21': 2021,\n",
    "            b'\\x22': 2022,\n",
    "            b'\\x23': 2023,\n",
    "        }\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_codes(platforms, codes):\n",
    "  results = []\n",
    "  for platform in platforms:\n",
    "    for ecu in codes:\n",
    "      for i in codes[ecu]:\n",
    "        if isinstance(i, tuple):\n",
    "          j = slice(i[0], i[1])\n",
    "        else:\n",
    "          j = slice(i, i+1)\n",
    "        for version in FW_BY_ECU[platform][ecu]:\n",
    "          code = version[j]\n",
    "          if code not in codes[ecu][i]:\n",
    "            print(f\"{platform} {code.hex()} not in {codes[ecu][i].keys()}\")\n",
    "          else:\n",
    "            results.append((platform, codes[ecu][i][code]))\n",
    "  return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SUBARU IMPREZA LIMITED 2019 08 not in dict_keys([b'\\x18', b'\\x19', b' ', b'!', b'\"', b'#'])\n",
      "SUBARU IMPREZA LIMITED 2019 08 not in dict_keys([b'\\x18', b'\\x19', b' ', b'!', b'\"', b'#'])\n",
      "SUBARU IMPREZA LIMITED 2019 0c not in dict_keys([b'\\x18', b'\\x19', b' ', b'!', b'\"', b'#'])\n",
      "SUBARU IMPREZA LIMITED 2019 0c not in dict_keys([b'\\x18', b'\\x19', b' ', b'!', b'\"', b'#'])\n",
      "SUBARU IMPREZA LIMITED 2019 2e not in dict_keys([b'\\x18', b'\\x19', b' ', b'!', b'\"', b'#'])\n",
      "SUBARU IMPREZA LIMITED 2019 3f not in dict_keys([b'\\x18', b'\\x19', b' ', b'!', b'\"', b'#'])\n",
      "correct_year=True   platform=SUBARU OUTBACK 7TH GEN           year=2023  years=[2023]\n",
      "correct_year=True   platform=SUBARU OUTBACK 7TH GEN           year=2023  years=[2023]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2022  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU OUTBACK 6TH GEN           year=2022  years=[2020, 2021, 2022]\n",
      "correct_year=False  platform=SUBARU CROSSTREK HYBRID 2020     year=2019  years=[2020]\n",
      "correct_year=False  platform=SUBARU CROSSTREK HYBRID 2020     year=2021  years=[2020]\n",
      "correct_year=False  platform=SUBARU FORESTER HYBRID 2020      year=2019  years=[2020]\n",
      "correct_year=True   platform=SUBARU LEGACY 7TH GEN            year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU LEGACY 7TH GEN            year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU LEGACY 7TH GEN            year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU LEGACY 7TH GEN            year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU IMPREZA LIMITED 2019      year=2019  years=[2017, 2018, 2019]\n",
      "correct_year=True   platform=SUBARU IMPREZA LIMITED 2019      year=2019  years=[2017, 2018, 2019]\n",
      "correct_year=True   platform=SUBARU IMPREZA LIMITED 2019      year=2018  years=[2017, 2018, 2019]\n",
      "correct_year=True   platform=SUBARU IMPREZA LIMITED 2019      year=2019  years=[2017, 2018, 2019]\n",
      "correct_year=True   platform=SUBARU IMPREZA LIMITED 2019      year=2019  years=[2017, 2018, 2019]\n",
      "correct_year=True   platform=SUBARU IMPREZA LIMITED 2019      year=2019  years=[2017, 2018, 2019]\n",
      "correct_year=False  platform=SUBARU FORESTER 2022             year=2021  years=[2022, 2023, 2024]\n",
      "correct_year=False  platform=SUBARU FORESTER 2022             year=2021  years=[2022, 2023, 2024]\n",
      "correct_year=True   platform=SUBARU FORESTER 2022             year=2022  years=[2022, 2023, 2024]\n",
      "correct_year=True   platform=SUBARU FORESTER 2022             year=2022  years=[2022, 2023, 2024]\n",
      "correct_year=False  platform=SUBARU IMPREZA SPORT 2020        year=2019  years=[2020, 2021, 2022]\n",
      "correct_year=False  platform=SUBARU IMPREZA SPORT 2020        year=2019  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU IMPREZA SPORT 2020        year=2020  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU IMPREZA SPORT 2020        year=2021  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU IMPREZA SPORT 2020        year=2021  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU IMPREZA SPORT 2020        year=2021  years=[2020, 2021, 2022]\n",
      "correct_year=True   platform=SUBARU ASCENT 2023               year=2023  years=[2023]\n",
      "correct_year=True   platform=SUBARU ASCENT LIMITED 2019       year=2019  years=[2019, 2020, 2021]\n",
      "correct_year=True   platform=SUBARU ASCENT LIMITED 2019       year=2021  years=[2019, 2020, 2021]\n",
      "correct_year=False  platform=SUBARU FORESTER 2019             year=2018  years=[2019, 2020, 2021]\n",
      "correct_year=False  platform=SUBARU FORESTER 2019             year=2018  years=[2019, 2020, 2021]\n",
      "correct_year=True   platform=SUBARU FORESTER 2019             year=2019  years=[2019, 2020, 2021]\n",
      "correct_year=True   platform=SUBARU FORESTER 2019             year=2019  years=[2019, 2020, 2021]\n",
      "correct_year=True   platform=SUBARU FORESTER 2019             year=2020  years=[2019, 2020, 2021]\n",
      "correct_year=True   platform=SUBARU FORESTER 2019             year=2020  years=[2019, 2020, 2021]\n"
     ]
    }
   ],
   "source": [
    "def test_year_code(platform, year):\n",
    "  car_docs = CAR(platform).config.car_docs\n",
    "  if isinstance(car_docs, list):\n",
    "    car_docs = car_docs[0]\n",
    "  years = [int(y) for y in car_docs.year_list]\n",
    "  correct_year = year in years\n",
    "  print(f\"{correct_year=!s: <6} {platform=: <32} {year=: <5} {years=}\")\n",
    "\n",
    "codes = get_codes(TEST_PLATFORMS, YEAR_CODES)\n",
    "for platform, year in codes:\n",
    "  test_year_code(platform, year)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "in_possible_platforms=True   platform=SUBARU OUTBACK 7TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 7TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU OUTBACK 6TH GEN           platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU CROSSTREK HYBRID 2020     platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU CROSSTREK HYBRID 2020     platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER HYBRID 2020      platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n",
      "in_possible_platforms=True   platform=SUBARU LEGACY 7TH GEN            platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU LEGACY 7TH GEN            platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU LEGACY 7TH GEN            platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU LEGACY 7TH GEN            platforms=['SUBARU OUTBACK 6TH GEN', 'SUBARU LEGACY 7TH GEN', 'SUBARU OUTBACK 7TH GEN']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA LIMITED 2019      platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER 2022             platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER 2022             platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER 2022             platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER 2022             platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA SPORT 2020        platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA SPORT 2020        platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA SPORT 2020        platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA SPORT 2020        platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA SPORT 2020        platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU IMPREZA SPORT 2020        platforms=['SUBARU IMPREZA LIMITED 2019', 'SUBARU IMPREZA SPORT 2020', 'SUBARU CROSSTREK HYBRID 2020']\n",
      "in_possible_platforms=True   platform=SUBARU ASCENT 2023               platforms=['SUBARU ASCENT LIMITED 2019', 'SUBARU ASCENT 2023']\n",
      "in_possible_platforms=True   platform=SUBARU ASCENT LIMITED 2019       platforms=['SUBARU ASCENT LIMITED 2019', 'SUBARU ASCENT 2023']\n",
      "in_possible_platforms=True   platform=SUBARU ASCENT LIMITED 2019       platforms=['SUBARU ASCENT LIMITED 2019', 'SUBARU ASCENT 2023']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER 2019             platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER 2019             platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER 2019             platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER 2019             platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER 2019             platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n",
      "in_possible_platforms=True   platform=SUBARU FORESTER 2019             platforms=['SUBARU FORESTER 2019', 'SUBARU FORESTER HYBRID 2020', 'SUBARU FORESTER 2022']\n"
     ]
    }
   ],
   "source": [
    "def test_platform_code(platform, platforms):\n",
    "  platforms = [str(p) for p in platforms]\n",
    "  in_possible_platforms = platform in platforms\n",
    "  print(f\"{in_possible_platforms=!s: <6} {platform=: <32} {platforms=}\")\n",
    "\n",
    "codes = get_codes(TEST_PLATFORMS, PLATFORM_CODES)\n",
    "for platform, possible_platforms in codes:\n",
    "  test_platform_code(platform, possible_platforms)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
