Console¶
See the console guide for an introduction.
console
¶
Interactive serial console with session logging (hwn console).
A small, dependency-free terminal in the spirit of miniterm / picocom, built on any
Tube: a serial port, a tcp: ser2net bridge or a simulated
device. Keys go to the target one by one (so Ctrl-C reaches U-Boot instead of killing the
console) and everything the target prints can be appended to a log file, optionally with a
timestamp at the start of every line, as evidence for the report.
Keys:
Ctrl-]quits.Ctrl-Topens the menu; the next key selects:bsend BREAK,htoggle the hex view,etoggle local echo,Ctrl-T/Ctrl-]send that key literally,?show the keys.
Example
from hwntools import serial
from hwntools.console import Console
with serial("/dev/ttyUSB0", 115200) as io:
Console(io, log_path="session.log", timestamps=True).run()
Console
¶
Console(tube: Tube, log_path: str | PathLike[str] | None = None, timestamps: bool = False, echo: bool = False, eol: str = 'cr', hex_view: bool = False, label: str = '', stdin: IO[bytes] | None = None, stdout: IO[bytes] | None = None, drain: float = 0.5)
Connect tube to the terminal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tube
|
Tube
|
the connection to the target. |
required |
log_path
|
str | PathLike[str] | None
|
append everything received to this file (raw bytes). A header and a footer line mark each session. |
None
|
timestamps
|
bool
|
prefix every line in the log with |
False
|
echo
|
bool
|
print typed characters locally (for targets that do not echo). |
False
|
eol
|
str
|
what Enter sends: |
'cr'
|
hex_view
|
bool
|
show received bytes as hex instead of text (the log stays raw). |
False
|
label
|
str
|
name of the connection used in messages and the log header. |
''
|
stdin
|
IO[bytes] | None
|
binary stream of keys; defaults to the terminal. When it is not a terminal (a pipe or a file), its bytes are sent as keys and the console exits once the input ends and the target has been quiet for drain seconds. |
None
|
stdout
|
IO[bytes] | None
|
binary stream for the target's output; defaults to the terminal. |
None
|
drain
|
float
|
see stdin. |
0.5
|
on_receive
¶
Handle bytes from the target: show them and append them to the log.
send_break
¶
Send a BREAK if the connection supports it (serial ports do; TCP does not).
handle_key
¶
Process one key (one byte, or one UTF-8 character). Returns False to quit.
handle_keys
¶
Process several keys; False once a key asked to quit.
run
¶
Run until the quit key, the end of piped input or the target closing.
Returns 0 (also when the target closes the connection).