90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Siri Remote probe: lists Apple HID devices, dumps report descriptors and
|
|
streams raw hidraw reports (hex, with changed bytes marked).
|
|
|
|
usage:
|
|
probe.py list # devices + descriptors
|
|
probe.py watch [hidrawN] [-o log.txt]
|
|
"""
|
|
import glob
|
|
import os
|
|
import select
|
|
import sys
|
|
import time
|
|
|
|
APPLE = "0000004C"
|
|
|
|
|
|
def devices():
|
|
out = []
|
|
for path in sorted(glob.glob("/sys/class/hidraw/hidraw*")):
|
|
dev = os.path.realpath(os.path.join(path, "device"))
|
|
uevent = open(os.path.join(dev, "uevent")).read()
|
|
info = dict(l.split("=", 1) for l in uevent.split() if "=" in l)
|
|
hid_id = info.get("HID_ID", "") # bus:vendor:product
|
|
desc = open(os.path.join(dev, "report_descriptor"), "rb").read()
|
|
out.append({
|
|
"node": "/dev/" + os.path.basename(path),
|
|
"name": info.get("HID_NAME", "?"),
|
|
"id": hid_id,
|
|
"driver": info.get("DRIVER", "?"),
|
|
"apple": APPLE in hid_id.upper(),
|
|
"desc": desc,
|
|
})
|
|
return out
|
|
|
|
|
|
def cmd_list():
|
|
for d in devices():
|
|
mark = " <-- Apple" if d["apple"] else ""
|
|
print(f'{d["node"]} {d["name"]} id={d["id"]} driver={d["driver"]}{mark}')
|
|
if d["apple"]:
|
|
print(" report descriptor (%d bytes):" % len(d["desc"]))
|
|
h = d["desc"].hex(" ")
|
|
for i in range(0, len(h), 96):
|
|
print(" ", h[i:i + 96])
|
|
|
|
|
|
def cmd_watch(args):
|
|
log = None
|
|
if "-o" in args:
|
|
i = args.index("-o")
|
|
log = open(args[i + 1], "a")
|
|
args = args[:i] + args[i + 2:]
|
|
nodes = args or [d["node"] for d in devices() if d["apple"]]
|
|
if not nodes:
|
|
sys.exit("no Apple hidraw device found (is the remote paired and connected?)")
|
|
fds = {os.open(n, os.O_RDONLY | os.O_NONBLOCK): n for n in nodes}
|
|
last = {}
|
|
print("watching", ", ".join(nodes), "- Ctrl+C to stop", flush=True)
|
|
t0 = time.monotonic()
|
|
while True:
|
|
r, _, _ = select.select(list(fds), [], [])
|
|
for fd in r:
|
|
try:
|
|
data = os.read(fd, 256)
|
|
except BlockingIOError:
|
|
continue
|
|
key = (fd, data[0] if data else None)
|
|
prev = last.get(key)
|
|
last[key] = data
|
|
cells = []
|
|
for i, b in enumerate(data):
|
|
changed = prev is not None and (i >= len(prev) or prev[i] != b)
|
|
cells.append(("*" if changed else " ") + f"{b:02x}")
|
|
line = f"{time.monotonic() - t0:9.3f} {fds[fd]} len={len(data):3d} |{''.join(cells)}"
|
|
print(line, flush=True)
|
|
if log:
|
|
log.write(line + "\n")
|
|
log.flush()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
a = sys.argv[1:]
|
|
if not a or a[0] == "list":
|
|
cmd_list()
|
|
elif a[0] == "watch":
|
|
cmd_watch(a[1:])
|
|
else:
|
|
print(__doc__)
|