mirror of
https://github.com/D-Modem/app.git
synced 2026-11-28 17:38:05 -08:00
The tests' and shared code's functions in two of eight batches (comment lines only). doc_check.py skips only lines of annotations alone, so a function under an annotated property (@Volatile var) needs its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
114 lines
5.1 KiB
Python
Executable File
114 lines
5.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# Copyright (C) 2026 Neo-Desktop
|
|
"""The USB IDs of serial adapters on the chips usb-serial-for-android drives (FTDI, Silicon
|
|
Labs CP210x, Prolific PL2303, WCH CH34x), as Linux's drivers list them: many adapters carry
|
|
their maker's own IDs, which the library's own table (the chip makers' only) doesn't know.
|
|
|
|
Writes app/src/main/java/com/neodesktop/dmodem/bridge/UsbSerialIds.kt and the generated
|
|
part of app/src/main/res/xml/usb_serial_adapters.xml. Only the ID numbers are taken.
|
|
|
|
tools/usb-serial-ids/generate.py [<linux tag, v6.12 by default> | <a directory with the files>]
|
|
"""
|
|
import os
|
|
import re
|
|
import sys
|
|
import urllib.request
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
KT = os.path.join(ROOT, "app/src/main/java/com/neodesktop/dmodem/bridge/UsbSerialIds.kt")
|
|
XML = os.path.join(ROOT, "app/src/main/res/xml/usb_serial_adapters.xml")
|
|
|
|
# each chip's: the files its IDs and their names are in
|
|
CHIPS = [
|
|
("FTDI", "FTDI's (FtdiSerialDriver)", ["drivers/usb/serial/ftdi_sio_ids.h", "drivers/usb/serial/ftdi_sio.c"]),
|
|
("CP210X", "Silicon Labs CP210x's (Cp21xxSerialDriver)", ["drivers/usb/serial/cp210x.c"]),
|
|
("PL2303", "Prolific PL2303's (ProlificSerialDriver)", ["drivers/usb/serial/pl2303.h", "drivers/usb/serial/pl2303.c"]),
|
|
("CH34X", "WCH CH34x's (Ch34xSerialDriver)", ["drivers/usb/serial/ch341.c"]),
|
|
]
|
|
|
|
|
|
# FTDI's quirks of boards whose first interface isn't a serial port, or that aren't serial adapters
|
|
SKIP = r"jtag|8u2232c|stmclite|TIRA|UIRT"
|
|
|
|
|
|
def fetch(where, path):
|
|
"""the file at path of Linux at the tag where (from GitHub), or by its name in the directory where"""
|
|
if os.path.isdir(where):
|
|
return open(os.path.join(where, os.path.basename(path)), encoding="utf-8", errors="replace").read()
|
|
url = "https://raw.githubusercontent.com/torvalds/linux/%s/%s" % (where, path)
|
|
with urllib.request.urlopen(url, timeout=60) as r:
|
|
return r.read().decode("utf-8", "replace")
|
|
|
|
|
|
def ids(texts):
|
|
"""the (vendor, product) pairs of the USB_DEVICE() entries: not FTDI's JTAG and infrared
|
|
boards (their quirks, SKIP: the first interface isn't a serial port), nor those for one
|
|
interface only (USB_DEVICE_INTERFACE_NUMBER: the same)"""
|
|
text = "\n".join(texts)
|
|
text = re.sub(r"/\*.*?\*/", "", text, flags=re.S)
|
|
defs = dict(re.findall(r"^\s*#define\s+(\w+)\s+\(?\s*(0[xX][0-9a-fA-F]+|\d+|\w+)\s*\)?\s*$", text, flags=re.M))
|
|
|
|
def value(tok):
|
|
"""a number, or a #define's name followed to the number it stands for"""
|
|
seen = 0
|
|
while not re.fullmatch(r"0[xX][0-9a-fA-F]+|\d+", tok):
|
|
tok = defs[tok]
|
|
seen += 1
|
|
if seen > 10:
|
|
raise ValueError(tok)
|
|
return int(tok, 0)
|
|
|
|
out = []
|
|
for m in re.finditer(r"\{\s*USB_DEVICE\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*(,[^}]*)?\}", text):
|
|
if m.group(3) and re.search(SKIP, m.group(3)):
|
|
continue
|
|
out.append((value(m.group(1)), value(m.group(2))))
|
|
return out
|
|
|
|
|
|
def main():
|
|
"""each chip's IDs (each pair kept for the first chip that has it) into UsbSerialIds.kt and the XML's generated part"""
|
|
where = sys.argv[1] if len(sys.argv) > 1 else "v6.12"
|
|
tables, seen = [], {}
|
|
for name, what, files in CHIPS:
|
|
got = []
|
|
for v, p in ids([fetch(where, f) for f in files]):
|
|
if (v, p) in seen:
|
|
if seen[(v, p)] != name:
|
|
print("%04x:%04x: %s's and %s's: kept %s's" % (v, p, seen[(v, p)], name, seen[(v, p)]), file=sys.stderr)
|
|
continue
|
|
seen[(v, p)] = name
|
|
got.append((v, p))
|
|
tables.append((name, what, sorted(got)))
|
|
print("%s: %d" % (name, len(got)), file=sys.stderr)
|
|
|
|
src = "Linux's" if os.path.isdir(where) else "Linux %s's" % where
|
|
with open(KT, "w") as f:
|
|
f.write("// SPDX-License-Identifier: AGPL-3.0-only\n// Copyright (C) 2026 Neo-Desktop\n")
|
|
f.write("// Generated by tools/usb-serial-ids/generate.py from %s USB serial drivers' ID tables: don't edit.\n" % src)
|
|
f.write("package com.neodesktop.dmodem.bridge\n\n")
|
|
f.write("/** USB serial adapters' IDs by their chip, as Linux's drivers list them: each vendor shl 16 or product */\n")
|
|
f.write("internal object UsbSerialIds {\n")
|
|
for name, what, got in tables:
|
|
f.write(" /** %s */\n val %s = intArrayOf(\n" % (what, name))
|
|
for i in range(0, len(got), 6):
|
|
f.write(" " + ", ".join("0x%04x_%04x.toInt()" % vp for vp in got[i:i + 6]) + ",\n")
|
|
f.write(" )\n\n")
|
|
f.seek(f.tell() - 1)
|
|
f.write("}\n")
|
|
|
|
xml = open(XML).read()
|
|
begin, end = " <!-- generated (tools/usb-serial-ids/generate.py): -->\n", " <!-- (generated) -->\n"
|
|
lines = "".join(' <usb-device vendor-id="%d" product-id="%d" />\n' % vp
|
|
for _, _, got in tables for vp in got)
|
|
if begin in xml:
|
|
xml = xml[:xml.index(begin)] + begin + lines + xml[xml.index(end):]
|
|
else:
|
|
xml = xml.replace("</resources>", begin + lines + end + "</resources>")
|
|
open(XML, "w").write(xml)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|