OBLink host daemonDraft
oblinkd is the host side of OBLink. It lives in
tools/oblink, needs only Python 3 and pyserial, and runs on Linux,
macOS and Windows. One daemon owns the serial port of the board; every
other tool (the hardware-run launcher, scripts, a human at a shell)
talks to the daemon, never to the port.
The daemon:
- finds the device by vendor and product id and follows it across unplug, replug and reboot;
- performs the handshake and keeps a console ring (the text the device printed, up to 4 MiB) that clients read by offset;
- runs monitor commands and image pushes on behalf of clients, with every wait ending on an event (arrival, handshake, reply) and a timeout as the failure bound. The only polling is the port-list scan every 200 ms.
Install#
python3 -m venv ~/oblink-venv
~/oblink-venv/bin/pip install pyserial
On Debian-based Linux, apt install python3-serial works too. The user
needs access to the serial device node (on Debian, membership of the
dialout group). uhubctl is needed only for the vbus-cycle
operation.
Run#
cd tools/oblink
python3 oblinkd.py -v
The daemon listens on TCP port 7766 on the loopback interface only. To use it from another machine, forward the port over SSH. To keep it running, start it from a service manager with automatic restart.
Options:
| Option | Meaning |
|---|---|
--vidpid 1209:316d |
The device id to look for. |
--which first|last|N, --iface N |
Which of the two CDC ports to use; the default is the lower one, which is the console. |
--listen host:port |
Control address; the default is the loopback address and port 7766. |
--port PATH |
Use this serial device instead of discovery (for the fake device). |
--vbus-location, --vbus-port |
Hub location and port for uhubctl. |
-v |
Verbose log. |
On macOS the port appears as a cu.usbmodem node. On Windows it is a COM
port provided by the class driver; port numbers change per device
instance, which discovery by id does not care about.
Control protocol#
Clients send one JSON object per line and read JSON lines back. A request
is {"op": ..., "id": <any>, ...}; a reply is {"id": ..., "ok": bool, ...}, and a failing reply carries why. Streaming operations first send
lines with an event key and finish with a line that has none.
The command line wrapper takes the operation and key=value pairs:
python3 -m oblink ctl status
python3 -m oblink ctl wait-arrival since=-1 timeout=120
python3 -m oblink ctl info
python3 -m oblink ctl send-monitor cmd=help
python3 -m oblink ctl push-image path=/tmp/image.bin
Operations#
| Operation | Arguments | Reply |
|---|---|---|
status |
none | Whether a device is connected, the port, the arrival and removal counters, the last error, the last HELLO reply, frame and CRC counters, the console offset. |
wait-arrival |
since (the arrival counter; -1 returns at once if attached), timeout |
Port, counter, HELLO reply. |
wait-removal |
since, timeout |
The removal counter. |
console-tail |
since, timeout, until (a substring), follow |
Stream of console events, then the reason it ended. |
console-read |
since, timeout |
One read of the ring. |
replay |
quiet, total |
Asks the device to resend its log from the start and returns the text. |
ping |
wait |
Round-trip time of a PING and PONG. |
info |
wait |
The text of the monitor's info command and its key: value lines as fields. |
send-monitor |
cmd, idle, total, wait |
The output text and the status: 0 OK, 1 error, 0x10 busy. A timeout is ok: false with why. |
push-image |
path, addr, chunk, timeout, reconnects, rewait |
Progress events, then the address, bytes, CRC, the offset resumed from and the retransmit count. |
vbus-cycle |
action (cycle, on, off), location, port, delay |
Replugs the board's USB port through uhubctl; a clear error if that tool is missing. |
push-image re-runs itself after a port drop: it waits for the next
arrival event and sends PUSH_BEGIN again, and the device answers with the
prefix it still holds.
The path in push-image is a file on the daemon's own machine.
Without hardware#
python3 -m oblink fake
python3 oblinkd.py --port /dev/pts/7 -v
fake starts a simulated device on a pseudo-terminal and prints its path;
the second command points the daemon at it (use the path that was
printed). The host tests use the same fake device.
Library#
The pieces are importable: oblink.framing (the codec and decoder),
oblink.discovery, oblink.link, oblink.push, oblink.daemon,
oblink.server and oblink.fakedev.
Not verified#
- Running as a service, on Windows, and
uhubctlbehaviour on specific hubs were not exercised in a documentation pass.