mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-07-18 08:22:05 +08:00
e1e9efb965
* usb logging * fix test * discover by PID * hardwared, add counters * edit msg * add manufacturer * reorder * rm portli * rm this
64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
from pathlib import Path
|
|
|
|
CHESTNUT_VENDOR_ID = 0xADD1
|
|
CHESTNUT_PRODUCT_ID = 0x0001
|
|
USB_DEVICES_PATH = Path("/sys/bus/usb/devices")
|
|
|
|
|
|
def read(path: Path) -> str | None:
|
|
try:
|
|
return path.read_text().strip()
|
|
except OSError:
|
|
return None
|
|
|
|
|
|
def read_int(path: Path, base: int = 10) -> int:
|
|
try:
|
|
return int(path.read_text(), base)
|
|
except (OSError, ValueError):
|
|
return 0
|
|
|
|
|
|
def usb_devices() -> list[Path]:
|
|
try:
|
|
devices = (d for d in USB_DEVICES_PATH.glob("*") if (d / "idVendor").exists())
|
|
return sorted(devices, key=lambda p: p.name)
|
|
except OSError:
|
|
return []
|
|
|
|
|
|
def get_usb_state() -> list[dict]:
|
|
devices = []
|
|
for device in usb_devices():
|
|
vendor_id = read_int(device / "idVendor", 16)
|
|
product_id = read_int(device / "idProduct", 16)
|
|
devices.append({
|
|
"busnum": read_int(device / "busnum"),
|
|
"devnum": read_int(device / "devnum"),
|
|
"vendorId": vendor_id,
|
|
"productId": product_id,
|
|
"speedMbps": read_int(device / "speed"),
|
|
"manufacturer": read(device / "manufacturer") or "",
|
|
"product": read(device / "product") or "",
|
|
})
|
|
return devices
|
|
|
|
|
|
def set_usb_state(device_state, devices: list[dict]) -> None:
|
|
entries = device_state.usbState.init('devices', len(devices))
|
|
|
|
chestnut_present = False
|
|
for entry, device in zip(entries, devices, strict=True):
|
|
entry.busnum = device["busnum"]
|
|
entry.devnum = device["devnum"]
|
|
entry.vendorId = device["vendorId"]
|
|
entry.productId = device["productId"]
|
|
entry.speedMbps = device["speedMbps"]
|
|
entry.manufacturer = device["manufacturer"]
|
|
entry.product = device["product"]
|
|
|
|
if (entry.vendorId, entry.productId) == (CHESTNUT_VENDOR_ID, CHESTNUT_PRODUCT_ID):
|
|
chestnut_present = True
|
|
|
|
device_state.chestnutPresent = chestnut_present
|