{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "facb8edc-9924-491a-a4dd-fe6135b0c6c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import all cars from opendbc\n",
    "\n",
    "from opendbc.car.values import PLATFORMS as TEST_PLATFORMS\n",
    "\n",
    "# Example: add additional platforms/segments to test outside of commaCarSegments\n",
    "\n",
    "EXTRA_SEGMENTS = {\n",
    "  # \"81dd9e9fe256c397/0000001f--97c42cf98d\",  # Volkswagen ID.4 test route, new car port, not in public dataset\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "ed1c8aec-c274-4c61-b83d-711ea194bf86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Searching 221 platforms\n",
      "No segments available for DODGE_DURANGO\n",
      "No segments available for FORD_RANGER_MK2\n",
      "No segments available for HOLDEN_ASTRA\n",
      "No segments available for CADILLAC_ATS\n",
      "No segments available for CHEVROLET_MALIBU\n",
      "No segments available for CADILLAC_XT4\n",
      "No segments available for CHEVROLET_VOLT_2019\n",
      "No segments available for CHEVROLET_TRAVERSE\n",
      "No segments available for GMC_YUKON\n",
      "No segments available for HONDA_ODYSSEY_CHN\n",
      "No segments available for HYUNDAI_KONA_2022\n",
      "No segments available for HYUNDAI_NEXO_1ST_GEN\n",
      "No segments available for GENESIS_GV70_ELECTRIFIED_1ST_GEN\n",
      "No segments available for GENESIS_G80_2ND_GEN_FL\n",
      "No segments available for RIVIAN_R1_GEN1\n",
      "No segments available for SUBARU_FORESTER_HYBRID\n",
      "No segments available for TESLA_MODEL_3\n",
      "No segments available for TESLA_MODEL_Y\n",
      "No segments available for TOYOTA_RAV4_PRIME\n",
      "No segments available for TOYOTA_SIENNA_4TH_GEN\n",
      "No segments available for LEXUS_LC_TSS2\n",
      "No segments available for VOLKSWAGEN_CADDY_MK3\n",
      "No segments available for VOLKSWAGEN_CRAFTER_MK2\n",
      "No segments available for VOLKSWAGEN_JETTA_MK6\n",
      "Searching 577 segments\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "from openpilot.tools.lib.logreader import LogReader\n",
    "from openpilot.tools.lib.comma_car_segments import get_comma_car_segments_database\n",
    "\n",
    "\n",
    "MAX_SEGS_PER_PLATFORM = 3  # Increase this to search more segments\n",
    "\n",
    "database = get_comma_car_segments_database()\n",
    "TEST_SEGMENTS = []\n",
    "\n",
    "print(f\"Searching {len(TEST_PLATFORMS)} platforms\")\n",
    "\n",
    "for platform in TEST_PLATFORMS:\n",
    "  if platform not in database:\n",
    "    print(f\"No segments available for {platform}\")\n",
    "    continue\n",
    "\n",
    "  all_segments = database[platform]\n",
    "  NUM_SEGMENTS = min(len(all_segments), MAX_SEGS_PER_PLATFORM)\n",
    "  TEST_SEGMENTS.extend(random.sample(all_segments, NUM_SEGMENTS))\n",
    "\n",
    "TEST_SEGMENTS.extend(EXTRA_SEGMENTS)\n",
    "\n",
    "print(f\"Searching {len(TEST_SEGMENTS)} segments\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c75e8f2-4f5f-4f89-b8db-5223a6534a9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "27a243c33de44498b2b946190df44b23",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "segments searched:   0%|          | 0/577 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Match found: 0f53b336851e1384/2023-11-20--09-44-03/12/s    CHRYSLER PACIFICA HYBRID 2018          ['VW CAN Ign']\n",
      "Match found: 7620ad20d3cefc64/2023-10-28--08-14-40/3/s     CHRYSLER PACIFICA HYBRID 2018          ['VW CAN Ign']\n",
      "Match found: 00d247a9bb1f9196/2023-11-06--13-33-17/9/s     CHRYSLER PACIFICA HYBRID 2018          ['VW CAN Ign']\n",
      "Match found: 120a432f63cb0de2/2023-10-30--20-01-34/1/s     CHRYSLER PACIFICA HYBRID 2019          ['VW CAN Ign']\n",
      "Match found: b70b56b76a6217f2/2023-12-19--08-30-22/35/s    CHRYSLER PACIFICA HYBRID 2019          ['VW CAN Ign']\n",
      "Match found: 97e388680a6716ed/2024-01-17--10-15-13/9/s     CHRYSLER PACIFICA HYBRID 2019          ['VW CAN Ign']\n",
      "Match found: 2137b01aa0ca63f9/2024-01-06--22-06-14/70/s    CHRYSLER PACIFICA 2018                 ['VW CAN Ign']\n",
      "Match found: 8fc6a1b72c8b1357/2023-11-06--07-50-05/8/s     CHRYSLER PACIFICA 2018                 ['VW CAN Ign']\n",
      "Match found: 7e705eb5c27a49cc/2024-01-18--16-51-20/3/s     CHRYSLER PACIFICA 2018                 ['VW CAN Ign']\n",
      "Match found: 12208e5acdc97eb3/2024-01-20--14-46-24/12/s    CHRYSLER PACIFICA 2020                 ['VW CAN Ign']\n",
      "Match found: 12208e5acdc97eb3/2023-11-30--12-01-09/2/s     CHRYSLER PACIFICA 2020                 ['VW CAN Ign']\n",
      "Match found: 9cad19e0efce3650/2024-01-26--10-24-52/27/s    CHRYSLER PACIFICA 2020                 ['VW CAN Ign']\n",
      "Match found: 9db428338427dec2/2023-11-05--18-40-09/21/s    JEEP GRAND CHEROKEE V6 2018            ['VW CAN Ign']\n",
      "Match found: d50ada8ee55a5e74/2023-12-11--13-38-09/0/s     JEEP GRAND CHEROKEE V6 2018            ['VW CAN Ign']\n",
      "Match found: 900dfa83b4addfe6/2023-12-30--19-20-08/28/s    JEEP GRAND CHEROKEE V6 2018            ['VW CAN Ign']\n",
      "Match found: 20acda0eb23d7f23/2024-01-19--17-33-26/41/s    JEEP GRAND CHEROKEE 2019               ['VW CAN Ign']\n",
      "Match found: 1cc3b46843cad2ca/2024-01-10--20-20-54/24/s    JEEP GRAND CHEROKEE 2019               ['VW CAN Ign']\n",
      "Match found: 2d9b6425552c52c1/2023-12-07--10-31-46/22/s    JEEP GRAND CHEROKEE 2019               ['VW CAN Ign']\n",
      "Match found: ae679616266f4096/2023-12-04--13-13-56/16/s    RAM HD 5TH GEN                         ['Tesla 3/Y CAN Ign']\n",
      "Match found: ae679616266f4096/2024-01-08--07-58-12/65/s    RAM HD 5TH GEN                         ['Tesla 3/Y CAN Ign']\n",
      "Match found: ae679616266f4096/2023-12-05--15-43-46/25/s    RAM HD 5TH GEN                         ['Tesla 3/Y CAN Ign']\n",
      "Match found: 6dae2984cc53cd7f/2024-01-09--21-41-11/4/s     FORD BRONCO SPORT 1ST GEN              ['Rivian CAN Ign']\n",
      "Match found: 440a155809ba2b6d/2023-12-30--08-51-53/2/s     FORD BRONCO SPORT 1ST GEN              ['Rivian CAN Ign']\n",
      "Match found: 6dae2984cc53cd7f/2024-01-06--10-11-07/1/s     FORD BRONCO SPORT 1ST GEN              ['Rivian CAN Ign']\n",
      "Match found: a4218e6416dfd978/2023-11-27--13-48-46/19/s    FORD ESCAPE 4TH GEN                    ['Rivian CAN Ign']\n",
      "Match found: a4218e6416dfd978/2023-11-10--14-13-14/0/s     FORD ESCAPE 4TH GEN                    ['Rivian CAN Ign']\n",
      "Match found: a4218e6416dfd978/2023-11-27--13-48-46/4/s     FORD ESCAPE 4TH GEN                    ['Rivian CAN Ign']\n",
      "Match found: 8a732841c3a8d5ef/2023-12-10--19-02-33/3/s     FORD EXPLORER 6TH GEN                  ['Rivian CAN Ign']\n",
      "Match found: 0b91b433b9332780/2023-12-28--14-02-49/4/s     FORD EXPLORER 6TH GEN                  ['Rivian CAN Ign']\n",
      "Match found: 8a732841c3a8d5ef/2023-11-09--07-28-12/1/s     FORD EXPLORER 6TH GEN                  ['Rivian CAN Ign']\n",
      "Match found: e886087f430e7fe7/2023-11-05--19-59-40/59/s    FORD FOCUS 4TH GEN                     ['Rivian CAN Ign']\n",
      "Match found: e886087f430e7fe7/2023-11-05--19-59-40/82/s    FORD FOCUS 4TH GEN                     ['Rivian CAN Ign']\n",
      "Match found: e886087f430e7fe7/2023-11-05--19-59-40/106/s   FORD FOCUS 4TH GEN                     ['Rivian CAN Ign']\n"
     ]
    }
   ],
   "source": [
    "from openpilot.tools.lib.logreader import comma_car_segments_source\n",
    "from tqdm.notebook import tnrange\n",
    "\n",
    "# Example search for CAN ignition messages\n",
    "# Be careful when filtering by bus, account for odd harness arrangements on Honda/HKG\n",
    "\n",
    "BUSES_TO_SEARCH = [0, 1, 2]\n",
    "\n",
    "# Support for external Red Panda\n",
    "EXTERNAL_PANDA_BUSES = [bus + 4 for bus in BUSES_TO_SEARCH]\n",
    "\n",
    "MESSAGES_TO_FIND = {\n",
    "  0x1F1: \"GM CAN Ign\",\n",
    "  0x152: \"Rivian CAN Ign\",\n",
    "  0x221: \"Tesla 3/Y CAN Ign\",\n",
    "  0x9E:  \"Mazda CAN Ign\",\n",
    "  0x3C0: \"VW CAN Ign\",\n",
    "}\n",
    "\n",
    "progress_bar = tnrange(len(TEST_SEGMENTS), desc=\"segments searched\")\n",
    "\n",
    "for segment in TEST_SEGMENTS:\n",
    "  lr = LogReader(segment, sources=[comma_car_segments_source])\n",
    "  CP = lr.first(\"carParams\")\n",
    "  if CP is None:\n",
    "    progress_bar.update()\n",
    "    continue\n",
    "\n",
    "  can_packets = [msg for msg in lr if msg.which() == \"can\"]\n",
    "  matched_messages = set()\n",
    "\n",
    "  for packet in can_packets:\n",
    "    for msg in packet.can:\n",
    "      if msg.address in MESSAGES_TO_FIND and msg.src in (BUSES_TO_SEARCH + EXTERNAL_PANDA_BUSES):\n",
    "        # print(msg)\n",
    "        matched_messages.add(msg.address)\n",
    "\n",
    "  if len(matched_messages) > 0:\n",
    "    message_names = [MESSAGES_TO_FIND[message] for message in matched_messages]\n",
    "    print(f\"Match found: {segment:<45} {CP.carFingerprint:<38} {message_names}\")\n",
    "\n",
    "  progress_bar.update()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7724dd97-f62e-4fd3-9f64-63d49be669d2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f393e00-8efd-40fb-a41e-d312531a83e8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
