mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-12-01 05:32:58 +08:00
ci(usb-ids): add a checker for board USB IDs against the Dronecode registry
Boards that ship the Dronecode USB vendor ID 0x3643 must use a product ID allocated in the Dronecode/usb-ids registry, and nothing in the tree enforces that. A board reusing another board's PID, or picking an unallocated one, becomes ambiguous to host tools that identify boards by VID/PID. The contract is the registry mapping: a defconfig under boards/<vendor>/<board>/ using VID 0x3643 must use a registered PID whose px4_board is exactly <vendor>/<board>. The USB vendor string is deliberately not checked, since the registry does not govern it. Boards on other vendor IDs are ignored. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Ramon Roche <mrpollo@gmail.com>
This commit is contained in:
1 parent
3fe7e7af35
commit
89549434e8
2 files changed
+361
No files matched your search
@@ -0,0 +1,213 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Check board USB IDs against the Dronecode USB ID registry.
|
||||
|
||||
Every NuttX defconfig under boards/<vendor>/<board>/ that declares the
|
||||
Dronecode USB vendor ID (0x3643) must use a product ID registered in
|
||||
https://github.com/Dronecode/usb-ids whose px4_board is exactly
|
||||
<vendor>/<board>. Boards using other vendor IDs are ignored. The USB
|
||||
vendor string is not checked; the registry does not govern it.
|
||||
|
||||
Usage:
|
||||
check_usb_ids.py check <defconfig> [<defconfig> ...]
|
||||
check_usb_ids.py lookup <0xNNNN | boards/<vendor>[/...]>
|
||||
|
||||
The registry is fetched from the usb-ids repo main branch by default;
|
||||
use --registry <file> to check against a local copy.
|
||||
|
||||
Only dependency: PyYAML.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import re
|
||||
import sys
|
||||
import urllib.request
|
||||
from typing import Dict, List, Optional, TypedDict
|
||||
|
||||
import yaml
|
||||
|
||||
REGISTRY_URL = (
|
||||
"https://raw.githubusercontent.com/Dronecode/usb-ids/main/usb-ids.yaml"
|
||||
)
|
||||
|
||||
CONFIG_RE = re.compile(
|
||||
r'^CONFIG_CDCACM_(VENDORID|PRODUCTID)=("?)(.*?)\2\s*$'
|
||||
)
|
||||
|
||||
|
||||
class PidEntry(TypedDict):
|
||||
manufacturer: str
|
||||
board: str
|
||||
px4_board: Optional[str]
|
||||
|
||||
|
||||
class Registry(TypedDict):
|
||||
vid: int
|
||||
pids: Dict[int, PidEntry] # pid -> registry entry
|
||||
vendors: Dict[str, str] # px4_vendor slug -> manufacturer name
|
||||
|
||||
|
||||
def load_registry(path: Optional[str] = None,
|
||||
url: str = REGISTRY_URL) -> Registry:
|
||||
if path:
|
||||
with open(path, encoding="utf-8") as f:
|
||||
doc = yaml.safe_load(f)
|
||||
else:
|
||||
with urllib.request.urlopen(url, timeout=30) as r:
|
||||
doc = yaml.safe_load(r.read())
|
||||
|
||||
registry: Registry = {
|
||||
"vid": int(doc["vid"], 16),
|
||||
"pids": {},
|
||||
"vendors": {},
|
||||
}
|
||||
for mfr in doc["manufacturers"]:
|
||||
slug = mfr.get("px4_vendor")
|
||||
if slug:
|
||||
registry["vendors"][slug] = mfr["name"]
|
||||
for entry in mfr["pids"]:
|
||||
registry["pids"][int(entry["pid"], 16)] = {
|
||||
"manufacturer": mfr["name"],
|
||||
"board": entry["board"],
|
||||
"px4_board": entry.get("px4_board"),
|
||||
}
|
||||
return registry
|
||||
|
||||
|
||||
def parse_defconfig(path: str) -> Dict[str, str]:
|
||||
values: Dict[str, str] = {}
|
||||
with open(path, encoding="utf-8") as f:
|
||||
for line in f:
|
||||
m = CONFIG_RE.match(line)
|
||||
if m:
|
||||
values[m.group(1)] = m.group(3)
|
||||
return values
|
||||
|
||||
|
||||
def parse_hex(value: str) -> Optional[int]:
|
||||
try:
|
||||
return int(value, 16)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def board_path(path: str) -> Optional[str]:
|
||||
"""'<vendor>/<board>' from a path like boards/<vendor>/<board>/..."""
|
||||
parts = path.replace("\\", "/").split("/")
|
||||
try:
|
||||
i = parts.index("boards")
|
||||
except ValueError:
|
||||
return None
|
||||
return "/".join(parts[i + 1:i + 3]) or None
|
||||
|
||||
|
||||
def check_defconfig(registry: Registry, path: str,
|
||||
values: Dict[str, str]) -> Optional[str]:
|
||||
"""Return the violation for a defconfig using the Dronecode VID.
|
||||
|
||||
None means the defconfig passes.
|
||||
"""
|
||||
pid_raw = values.get("PRODUCTID")
|
||||
if pid_raw is None:
|
||||
return "VID 0x3643 set but no CONFIG_CDCACM_PRODUCTID"
|
||||
pid = parse_hex(pid_raw)
|
||||
if pid is None:
|
||||
return f"CONFIG_CDCACM_PRODUCTID \"{pid_raw}\" is not a hex value"
|
||||
|
||||
entry = registry["pids"].get(pid)
|
||||
if entry is None:
|
||||
return (
|
||||
f"PID {pid_raw} is not registered in the Dronecode USB ID "
|
||||
"registry (https://github.com/Dronecode/usb-ids)"
|
||||
)
|
||||
|
||||
board = board_path(path)
|
||||
if entry["px4_board"] is None:
|
||||
return (
|
||||
f"PID {pid_raw} ({entry['manufacturer']}, "
|
||||
f"\"{entry['board']}\") has no px4_board in the registry; "
|
||||
f"set it to \"{board}\" in usb-ids.yaml"
|
||||
)
|
||||
if entry["px4_board"] != board:
|
||||
return (
|
||||
f"PID {pid_raw} is mapped to boards/{entry['px4_board']}/ in the "
|
||||
f"registry, not boards/{board}/"
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def cmd_check(registry: Registry, paths: List[str]) -> int:
|
||||
violations: List[str] = []
|
||||
checked = 0
|
||||
for path in paths:
|
||||
values = parse_defconfig(path)
|
||||
vid_raw = values.get("VENDORID")
|
||||
if vid_raw is None:
|
||||
continue
|
||||
vid = parse_hex(vid_raw)
|
||||
if vid is None:
|
||||
violations.append(
|
||||
f"{path}: CONFIG_CDCACM_VENDORID \"{vid_raw}\" "
|
||||
"is not a hex value"
|
||||
)
|
||||
continue
|
||||
if vid != registry["vid"]:
|
||||
continue
|
||||
checked += 1
|
||||
|
||||
violation = check_defconfig(registry, path, values)
|
||||
if violation:
|
||||
violations.append(f"{path}: {violation}")
|
||||
|
||||
for v in violations:
|
||||
print(f"error: {v}", file=sys.stderr)
|
||||
print(
|
||||
f"checked {checked} defconfig(s) with VID 0x3643, "
|
||||
f"{len(violations)} violation(s)"
|
||||
)
|
||||
return 1 if violations else 0
|
||||
|
||||
|
||||
def cmd_lookup(registry: Registry, query: str) -> int:
|
||||
if query.lower().startswith("0x"):
|
||||
entry = registry["pids"].get(int(query, 16))
|
||||
if entry is None:
|
||||
print(f"{query}: not registered")
|
||||
return 1
|
||||
print(f"{query}: {entry['manufacturer']}, board: {entry['board']}")
|
||||
return 0
|
||||
|
||||
vendor = (board_path(query) or query).split("/")[0]
|
||||
name = registry["vendors"].get(vendor)
|
||||
if name is None:
|
||||
print(
|
||||
f"boards/{vendor}/: no manufacturer registered "
|
||||
"for this vendor directory"
|
||||
)
|
||||
return 1
|
||||
pids = sorted(
|
||||
p for p, e in registry["pids"].items() if e["manufacturer"] == name
|
||||
)
|
||||
pid_list = ", ".join(f"0x{p:04X}" for p in pids)
|
||||
print(f"boards/{vendor}/: {name}, PIDs: {pid_list}")
|
||||
return 0
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--registry", help="path to a local usb-ids.yaml")
|
||||
parser.add_argument("--registry-url", default=REGISTRY_URL)
|
||||
sub = parser.add_subparsers(dest="command", required=True)
|
||||
p_check = sub.add_parser("check", help="check defconfig files")
|
||||
p_check.add_argument("paths", nargs="+")
|
||||
p_lookup = sub.add_parser("lookup", help="look up a PID or board path")
|
||||
p_lookup.add_argument("query")
|
||||
args = parser.parse_args()
|
||||
|
||||
registry = load_registry(args.registry, args.registry_url)
|
||||
if args.command == "check":
|
||||
return cmd_check(registry, args.paths)
|
||||
return cmd_lookup(registry, args.query)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,148 @@
|
||||
"""Check check_usb_ids.py against a fixture registry and fake board trees.
|
||||
|
||||
The checker gates every PR that touches a board defconfig, so a false
|
||||
pass lets a board ship a PID the registry never gave it, and a false
|
||||
failure or traceback blocks unrelated board work.
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
from typing import List, Tuple
|
||||
import unittest
|
||||
|
||||
from check_usb_ids import cmd_check, cmd_lookup, load_registry
|
||||
|
||||
REGISTRY = """
|
||||
vid: "0x3643"
|
||||
vendor_string: "Dronecode Project, Inc."
|
||||
manufacturers:
|
||||
- name: PX4/Dronecode
|
||||
px4_vendor: px4
|
||||
pids:
|
||||
- pid: "0x001D"
|
||||
board: FMU-v6XRT
|
||||
px4_board: px4/fmu-v6xrt
|
||||
- pid: "0x001E"
|
||||
board: Unmapped
|
||||
- name: SIYI
|
||||
px4_vendor: siyi
|
||||
pids:
|
||||
- pid: "0x04B0"
|
||||
board: SIYI N7
|
||||
px4_board: siyi/n7
|
||||
"""
|
||||
|
||||
|
||||
class CheckUsbIdsTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
directory = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(directory.cleanup)
|
||||
self.root = Path(directory.name)
|
||||
registry = self.root / "usb-ids.yaml"
|
||||
registry.write_text(REGISTRY)
|
||||
self.registry = load_registry(str(registry))
|
||||
|
||||
def defconfig(self, board: str, *lines: str) -> str:
|
||||
path = self.root.joinpath(
|
||||
"boards", board, "nuttx-config", "nsh", "defconfig")
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text("".join(f"{line}\n" for line in lines))
|
||||
return str(path)
|
||||
|
||||
def check(self, *paths: str) -> Tuple[int, List[str]]:
|
||||
out, err = io.StringIO(), io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
|
||||
rc = cmd_check(self.registry, list(paths))
|
||||
return rc, err.getvalue().splitlines()
|
||||
|
||||
def assert_one_error(self, path: str, expected: str) -> None:
|
||||
rc, errors = self.check(path)
|
||||
self.assertEqual(rc, 1)
|
||||
self.assertEqual(len(errors), 1, errors)
|
||||
self.assertIn(expected, errors[0])
|
||||
|
||||
def test_registered_and_mapped_passes(self) -> None:
|
||||
path = self.defconfig("px4/fmu-v6xrt",
|
||||
"CONFIG_CDCACM_VENDORID=0x3643",
|
||||
"CONFIG_CDCACM_PRODUCTID=0x001D")
|
||||
self.assertEqual(self.check(path), (0, []))
|
||||
|
||||
def test_pid_compare_is_case_insensitive(self) -> None:
|
||||
# the registry spells PIDs uppercase, defconfigs often do not
|
||||
path = self.defconfig("px4/fmu-v6xrt",
|
||||
"CONFIG_CDCACM_VENDORID=0x3643",
|
||||
"CONFIG_CDCACM_PRODUCTID=0x001d")
|
||||
self.assertEqual(self.check(path), (0, []))
|
||||
|
||||
def test_custom_vendor_string_is_ignored(self) -> None:
|
||||
# the registry does not govern the vendor string
|
||||
path = self.defconfig("siyi/n7",
|
||||
"CONFIG_CDCACM_VENDORID=0x3643",
|
||||
"CONFIG_CDCACM_PRODUCTID=0x04B0",
|
||||
'CONFIG_CDCACM_VENDORSTR='
|
||||
'"Totally Weird Widgets GmbH"')
|
||||
self.assertEqual(self.check(path), (0, []))
|
||||
|
||||
def test_unregistered_pid_fails(self) -> None:
|
||||
path = self.defconfig("px4/fmu-v6xrt",
|
||||
"CONFIG_CDCACM_VENDORID=0x3643",
|
||||
"CONFIG_CDCACM_PRODUCTID=0x0999")
|
||||
self.assert_one_error(path, "PID 0x0999 is not registered")
|
||||
|
||||
def test_pid_mapped_to_another_board_fails(self) -> None:
|
||||
# a vendor reusing its PID for a second board
|
||||
path = self.defconfig("siyi/n8",
|
||||
"CONFIG_CDCACM_VENDORID=0x3643",
|
||||
"CONFIG_CDCACM_PRODUCTID=0x04B0")
|
||||
self.assert_one_error(
|
||||
path,
|
||||
"mapped to boards/siyi/n7/ in the registry, not boards/siyi/n8/")
|
||||
|
||||
def test_missing_px4_board_fails(self) -> None:
|
||||
path = self.defconfig("px4/other",
|
||||
"CONFIG_CDCACM_VENDORID=0x3643",
|
||||
"CONFIG_CDCACM_PRODUCTID=0x001E")
|
||||
self.assert_one_error(
|
||||
path,
|
||||
'has no px4_board in the registry; set it to "px4/other"')
|
||||
|
||||
def test_malformed_hex_is_a_one_line_error(self) -> None:
|
||||
cases = {
|
||||
"vid": ("CONFIG_CDCACM_VENDORID=0x36G3",
|
||||
"CONFIG_CDCACM_PRODUCTID=0x001D"),
|
||||
"pid": ("CONFIG_CDCACM_VENDORID=0x3643",
|
||||
"CONFIG_CDCACM_PRODUCTID=0xZZ01"),
|
||||
}
|
||||
for name, lines in cases.items():
|
||||
with self.subTest(name):
|
||||
path = self.defconfig(f"bad/{name}", *lines)
|
||||
self.assert_one_error(path, "is not a hex value")
|
||||
|
||||
def test_other_vendor_id_is_ignored(self) -> None:
|
||||
path = self.defconfig("acme/fc",
|
||||
"CONFIG_CDCACM_VENDORID=0x1209",
|
||||
"CONFIG_CDCACM_PRODUCTID=0x0999")
|
||||
self.assertEqual(self.check(path), (0, []))
|
||||
|
||||
def lookup(self, query: str) -> Tuple[int, str]:
|
||||
out = io.StringIO()
|
||||
with contextlib.redirect_stdout(out):
|
||||
rc = cmd_lookup(self.registry, query)
|
||||
return rc, out.getvalue().strip()
|
||||
|
||||
def test_lookup_by_pid(self) -> None:
|
||||
self.assertEqual(self.lookup("0x04b0"),
|
||||
(0, "0x04b0: SIYI, board: SIYI N7"))
|
||||
self.assertEqual(self.lookup("0x0999"), (1, "0x0999: not registered"))
|
||||
|
||||
def test_lookup_by_board_path(self) -> None:
|
||||
self.assertEqual(
|
||||
self.lookup("boards/px4/fmu-v6xrt"),
|
||||
(0, "boards/px4/: PX4/Dronecode, PIDs: 0x001D, 0x001E"))
|
||||
self.assertEqual(self.lookup("boards/acme/fc")[0], 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user