|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Provision a panda jungle v2 SD card for CAN replay. |
| 4 | +
|
| 5 | +Reads CAN messages from an openpilot rlog/qlog file and writes them to an |
| 6 | +SD card (or binary file) in the panda jungle raw replay format. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + provision_sd.py <rlog_or_qlog> <sd_device_or_output_file> |
| 10 | +
|
| 11 | +Examples: |
| 12 | + # Write directly to an SD card block device (requires root/sudo on Linux): |
| 13 | + provision_sd.py /data/media/0/realdata/abc123--0/0/rlog /dev/sdb |
| 14 | +
|
| 15 | + # Write to a binary file, then dd to the SD card manually: |
| 16 | + provision_sd.py route.rlog replay.bin |
| 17 | + dd if=replay.bin of=/dev/sdb bs=512 |
| 18 | +
|
| 19 | +SD card binary format written by this script: |
| 20 | + Sector 0 (512 bytes): header |
| 21 | + bytes 0-7: magic b"PNDREPLY" |
| 22 | + bytes 8-11: uint32 num_records |
| 23 | + bytes 12-15: uint32 record_size (= 20) |
| 24 | + bytes 16-19: uint32 format_version (= 1) |
| 25 | + bytes 20-511: zero-padded |
| 26 | + Sectors 1+: records (20 bytes each) |
| 27 | + uint32 mono_time_us - microseconds since replay start (relative) |
| 28 | + uint32 addr - CAN address |
| 29 | + uint8 bus - CAN bus number (0-2) |
| 30 | + uint8 len - data byte length (0-8) |
| 31 | + uint8 data[8] - CAN payload (zero-padded) |
| 32 | + uint16 pad - reserved (0) |
| 33 | +""" |
| 34 | + |
| 35 | +import argparse |
| 36 | +import struct |
| 37 | +import sys |
| 38 | + |
| 39 | +MAGIC = b"PNDREPLY" |
| 40 | +FORMAT_VERSION = 1 |
| 41 | +RECORD_SIZE = 20 |
| 42 | +SECTOR_SIZE = 512 |
| 43 | +HEADER_SECTOR = 0 |
| 44 | +DATA_START_SECTOR = 1 |
| 45 | + |
| 46 | +HEADER_FMT = "<8sIII" # magic, num_records, record_size, format_version |
| 47 | +RECORD_FMT = "<II BB 8s H" # mono_time_us, addr, bus, len, data[8], pad |
| 48 | + |
| 49 | + |
| 50 | +def parse_args(): |
| 51 | + parser = argparse.ArgumentParser(description="Provision panda jungle v2 SD card for CAN replay") |
| 52 | + parser.add_argument("rlog", help="Path to openpilot rlog or qlog file") |
| 53 | + parser.add_argument("output", help="SD card block device (e.g. /dev/sdb) or output binary file") |
| 54 | + return parser.parse_args() |
| 55 | + |
| 56 | + |
| 57 | +def read_can_messages(rlog_path): |
| 58 | + """Read CAN messages from an openpilot rlog/qlog file. |
| 59 | +
|
| 60 | + Returns list of (mono_time_ns, addr, bus, data) tuples sorted by time. |
| 61 | + """ |
| 62 | + try: |
| 63 | + from openpilot.tools.lib.logreader import LogReader |
| 64 | + except ImportError: |
| 65 | + try: |
| 66 | + from tools.lib.logreader import LogReader |
| 67 | + except ImportError: |
| 68 | + print("Error: could not import LogReader. Run from an openpilot checkout or install openpilot tools.", file=sys.stderr) |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + messages = [] |
| 72 | + lr = LogReader(rlog_path) |
| 73 | + for msg in lr: |
| 74 | + if msg.which() == "sendcan": |
| 75 | + for can_msg in msg.sendcan: |
| 76 | + messages.append((msg.logMonoTime, can_msg.address, can_msg.src, bytes(can_msg.dat))) |
| 77 | + |
| 78 | + if not messages: |
| 79 | + print("Error: no sendcan messages found in log file.", file=sys.stderr) |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | + messages.sort(key=lambda m: m[0]) |
| 83 | + return messages |
| 84 | + |
| 85 | + |
| 86 | +def build_records(messages): |
| 87 | + """Convert (mono_time_ns, addr, bus, data) list to binary record bytes.""" |
| 88 | + t0_ns = messages[0][0] |
| 89 | + records = bytearray() |
| 90 | + for mono_time_ns, addr, bus, data in messages: |
| 91 | + elapsed_us = (mono_time_ns - t0_ns) // 1000 |
| 92 | + if elapsed_us > 0xFFFFFFFF: |
| 93 | + print(f"Warning: timestamp overflow at {elapsed_us} us, clamping to 32-bit max.", file=sys.stderr) |
| 94 | + elapsed_us = 0xFFFFFFFF |
| 95 | + |
| 96 | + data_len = min(len(data), 8) |
| 97 | + padded_data = data[:data_len].ljust(8, b'\x00') |
| 98 | + |
| 99 | + record = struct.pack(RECORD_FMT, elapsed_us, addr, bus & 0xFF, data_len, padded_data, 0) |
| 100 | + assert len(record) == RECORD_SIZE, f"record size mismatch: {len(record)}" |
| 101 | + records.extend(record) |
| 102 | + |
| 103 | + return records |
| 104 | + |
| 105 | + |
| 106 | +def build_header(num_records): |
| 107 | + header_data = struct.pack(HEADER_FMT, MAGIC, num_records, RECORD_SIZE, FORMAT_VERSION) |
| 108 | + # Pad to one full sector |
| 109 | + return header_data + b'\x00' * (SECTOR_SIZE - len(header_data)) |
| 110 | + |
| 111 | + |
| 112 | +def write_image(output_path, header, records): |
| 113 | + with open(output_path, 'wb') as f: |
| 114 | + f.write(header) |
| 115 | + f.write(records) |
| 116 | + # Pad final partial sector with zeros |
| 117 | + remainder = len(records) % SECTOR_SIZE |
| 118 | + if remainder != 0: |
| 119 | + f.write(b'\x00' * (SECTOR_SIZE - remainder)) |
| 120 | + |
| 121 | + |
| 122 | +def main(): |
| 123 | + args = parse_args() |
| 124 | + |
| 125 | + print(f"Reading CAN messages from {args.rlog}...") |
| 126 | + messages = read_can_messages(args.rlog) |
| 127 | + print(f" Found {len(messages)} sendcan messages") |
| 128 | + |
| 129 | + records = build_records(messages) |
| 130 | + num_records = len(messages) |
| 131 | + |
| 132 | + duration_s = (messages[-1][0] - messages[-1][0]) // 1_000_000_000 if len(messages) > 1 else 0 |
| 133 | + elapsed_us = (messages[-1][0] - messages[0][0]) // 1000 |
| 134 | + duration_s = elapsed_us / 1_000_000 |
| 135 | + |
| 136 | + header = build_header(num_records) |
| 137 | + |
| 138 | + total_bytes = len(header) + len(records) |
| 139 | + total_sectors = (total_bytes + SECTOR_SIZE - 1) // SECTOR_SIZE |
| 140 | + |
| 141 | + print(f" Replay duration: {duration_s:.1f} seconds") |
| 142 | + print(f" Total records: {num_records}") |
| 143 | + print(f" SD image size: {total_sectors} sectors ({total_bytes / 1024:.1f} KB)") |
| 144 | + |
| 145 | + print(f"Writing to {args.output}...") |
| 146 | + write_image(args.output, header, records) |
| 147 | + print("Done. Insert SD card into jungle v2 and call sd_replay_start() to begin replay.") |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + main() |
0 commit comments