Skip to content

labgrid-client: Add an internal console - #1870

Open
sjg20 wants to merge 5 commits into
labgrid-project:masterfrom
sjg20:push9-cons-small
Open

labgrid-client: Add an internal console#1870
sjg20 wants to merge 5 commits into
labgrid-project:masterfrom
sjg20:push9-cons-small

Conversation

@sjg20

@sjg20 sjg20 commented May 13, 2026

Copy link
Copy Markdown
Contributor

The microcom-based console used by labgrid-client has a few limitations:

  • console output is lost between when the board is reset and microcom connects;
  • microcom has no txdelay handling, so boards can miss characters when the client is sending input quickly (e.g. pasted test commands);
  • between labgrid-client console starting and microcom attaching, a few characters can echo back to the caller, which can break test systems which rely on output.

For many use cases, microcom is more than is needed. This PR adds a small internal terminal that solves the above. Enable it with:

  • labgrid-client console -i (or --internal), or
  • LG_CONSOLE=internal in the environment, so existing scripts can opt in without being rewritten

Press Ctrl-] twice (quickly) to exit.

Refactoring done along the way:

  • Move all terminal handling into a new file labgrid/util/term.py, keeping client.py thinner
  • Split _check_allowed() into a side-effect-free is_allowed() plus the existing raise-on-failure helper, so the new terminal loop can poll without try/except
  • Flush the console device only when needed, so console data is preserved across strategy-driver → interactive transitions when the internal terminal is in use

What is the feature used for? Console access in environments where microcom is unavailable or its limitations bite: U-Boot tests, post-reset capture, and any flow that needs txdelay.

How does labgrid benefit? No external dependency for a basic console, plus clean handoff between strategy-driver use and interactive use without losing data.

How was it verified? Used the internal console against a QEMU target end-to-end. The unit tests under tests/test_term.py and tests/test_client_unit.py cover both the external (microcom/telnet) and internal paths: keystroke handling, txdelay, exit-char deadline, logfile output, listen-only mode and check-allowed termination.

No hardware-specific changes.

Checklist

  • Documentation for the feature
  • Tests for the feature
  • Add a section on how to use the feature to doc/usage.rst
  • PR has been tested
  • Man pages have been regenerated

sjg20 added 3 commits May 12, 2026 13:33
At present if a console device is used by a strategy driver it is then
removed by the client immediately afterwards. If the 'console' command
is used, this means that any console data is lost.

This is done so that the console device is release for microcom, so
move the logic in with the microcom logic. With the forthcoming internal
terminal, it will not be needed.

Signed-off-by: Simon Glass <sjg@chromium.org>
It is useful to be able to see if a place is allowed without raising an
error and needing a try...except block. Add a function to handle this
and update _check_allowed() to use it.

This will be used by the updated terminal suport.

Signed-off-by: Simon Glass <sjg@chromium.org>
There is quite a lot of code here, so move the terminal function into
its own file. This will make it easier to extend it later.

Signed-off-by: Simon Glass <sjg@chromium.org>
@codecov

codecov Bot commented May 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.63473% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.5%. Comparing base (db1cba3) to head (7571145).
⚠️ Report is 78 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
labgrid/remote/client.py 27.0% 27 Missing ⚠️
labgrid/util/term.py 98.4% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##           master   #1870      +/-   ##
=========================================
+ Coverage    46.0%   61.5%   +15.4%     
=========================================
  Files         180     183       +3     
  Lines       14439   14989     +550     
=========================================
+ Hits         6654    9224    +2570     
+ Misses       7785    5765    -2020     
Flag Coverage Δ
3.10 61.4% <82.6%> (+15.4%) ⬆️
3.11 61.4% <82.6%> (+15.4%) ⬆️
3.12 61.4% <82.6%> (+15.3%) ⬆️
3.13 61.4% <82.6%> (+15.4%) ⬆️
3.14 61.4% <82.6%> (+15.3%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@Emantor

Emantor commented May 30, 2026

Copy link
Copy Markdown
Member

While I do agree with the concept of an internal console for labgrid-client, my expectation is more in line to use existing modules like Python Prompt Toolkit instead of writing our own terminal handling from scratch.

@sjg20

sjg20 commented May 30, 2026

Copy link
Copy Markdown
Contributor Author

While I do agree with the concept of an internal console for labgrid-client, my expectation is more in line to use existing modules like Python Prompt Toolkit instead of writing our own terminal handling from scratch.

OK I will take a look

At present Labgrid uses microcom as its console. This has some
limitations:

- console output is lost between when the board is reset and microcom
  connects
- txdelay cannot be handled in microcom, meaning that boards may fail
  to receive expected output
- the console may echo a few characters back to the caller in the time
  between when 'labgrid-client console' is executed and when microcom
  starts (which causes failures with U-Boot test system)

For many use cases, microcom is more than is needed, so provide a simple
internal terminal which resolves the above problems.

It is enabled by a '-i' option to the 'console' command, as well as an
environment variable, so that it can be adjusted without updating a lot
of scripts.

To exit, press Ctrl-] twice, quickly.

Use prompt_toolkit to look after the terminal, since it already knows
how to put it into raw mode and how to watch it from the event loop.
The keystrokes themselves are read with os.read() rather than
prompt_toolkit's read_keys(), since the board needs a transparent byte
stream: read_keys() decodes the input and parses escape sequences into
key objects, so control characters and escape sequences would no longer
reach the board as they were typed.

Series-changes: 4
- Get internal console working with qemu
- Show a prompt when starting, to indicate it is waiting for the board

Series-changes: 5
- Use prompt_toolkit to handle the terminal, rather than driving termios
  directly (Rouven)
- Watch the keyboard from the event loop, instead of polling stdin

Signed-off-by: Simon Glass <sjg@chromium.org>
@sjg20
sjg20 force-pushed the push9-cons-small branch from a575230 to dcca2a4 Compare July 12, 2026 21:15
Add unit tests for the terminal handling in term.py, covering the
external console launch (microcom and telnet fallback), the internal
console read/write loop, and terminal setup and teardown. Also add
tests for the new is_allowed() helper and the new parser argument for
the internal console.

Use real pipes for stdin rather than mocking os.read(), giving more
realistic coverage of the keystroke and txdelay paths. The exit-char
deadline test uses a threading.Event to synchronise with the run
loop, avoiding brittle fixed sleeps.

Also fix a duplicate --logfile append in term.external() when using
microcom.

Series-changes: 5
- Feed keystrokes through a prompt_toolkit Input, so that raw mode
  and the event-loop hookup are covered too
- Add a test that escape sequences and control characters reach the
  board untouched
- Drop the note about an UnboundLocalError fix, which is not in this
  commit

Signed-off-by: Simon Glass <sjg@chromium.org>
Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
@sjg20
sjg20 force-pushed the push9-cons-small branch from dcca2a4 to 7571145 Compare July 12, 2026 21:24
@sjg20

sjg20 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

I've switched the terminal handling over to prompt_toolkit.

It now uses create_input() for the terminal, inp.raw_mode() to put it into raw mode and inp.attach() to watch the keyboard from the event loop, so labgrid no longer touches termios itself and no longer polls stdin.

But...two things worth mentioning.

  1. The keystrokes are still read with os.read(), not read_keys(). A console has to be a transparent byte stream: whatever the user types must reach the board exactly as typed. read_keys() runs stdin through PosixStdinReader (which decodes it to str) and then Vt100Parser, which turns escape sequences into KeyPress objects and buffers incomplete ones. That is exactly right for a REPL, but it would mean arrow keys, function keys, Ctrl-C and any 8-bit data no longer arrive at the board unchanged. So I use prompt_toolkit for the terminal (raw mode, event-loop hookup) and read the raw bytes myself.

If you'd rather I went via read_keys() and reconstructed the bytes from KeyPress.data, I can, but it seemed like a lot of work to undo something we don't want in the first place.

  1. Doing it this way fixed two bugs - prompt_toolkit's raw_mode() clears more than my hand-coded version did: as well as ECHO/ICANON/ISIG, it clears IXON/IXOFF and ICRNL/INLCR/IGNCR. So previously Ctrl-S and Ctrl-Q were being eaten by the tty as flow control instead of reaching the board, and CR was being translated to NL on the way in. Both are now correct.

Watching stdin from the event loop also removes the busy-poll: the old loop called time.sleep(0.001) from inside an async function, which blocked the event loop.

prompt_toolkit is added to the dependencies in pyproject.toml.

@Emantor

Emantor commented Jul 31, 2026

Copy link
Copy Markdown
Member

Looking at this PR I expected the console abstraction to also use windowing from prompt-toolkit, with the future goal of adding an inline command prompt to run client commands directly from the console as well. I don't know if you are willing to look into that, thanks for reworking this so far.

@sjg20

sjg20 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look.

I did consider prompt-toolkit's full-screen/windowing support, but went the other way deliberately: the internal console aims to be a transparent byte pipe, so that escape sequences, control characters and interactive programs on the board (editors, menuconfig, etc.) work exactly as they do with microcom. A prompt-toolkit Application takes over the terminal and re-renders it, which makes that transparency hard to keep - every byte from the board would need to be interpreted or routed around the renderer.

That said, I like the idea of an inline command prompt and I think it can be layered on top of this PR rather than replacing it. The run() loop already owns both streams, so a follow-up could watch for a hotkey (say Ctrl-] once, since exit is Ctrl-] twice) and switch into a prompt-toolkit session to run a client command, then drop back to the raw console. That keeps the transparent path as the default and only brings in windowing when the user asks for it.

So yes, I am happy to look into that as a follow-up, if it is OK to land the basic console first? I can file an issue to track the inline-prompt idea so it does not get lost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants