labgrid-client: Add an internal console - #1870
Conversation
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 Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
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>
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>
|
I've switched the terminal handling over to prompt_toolkit. It now uses But...two things worth mentioning.
If you'd rather I went via
Watching stdin from the event loop also removes the busy-poll: the old loop called prompt_toolkit is added to the dependencies in |
|
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. |
|
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. |
The microcom-based console used by
labgrid-clienthas a few limitations:labgrid-client consolestarting 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), orLG_CONSOLE=internalin the environment, so existing scripts can opt in without being rewrittenPress
Ctrl-]twice (quickly) to exit.Refactoring done along the way:
labgrid/util/term.py, keepingclient.pythinner_check_allowed()into a side-effect-freeis_allowed()plus the existing raise-on-failure helper, so the new terminal loop can poll without try/exceptWhat 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.pyandtests/test_client_unit.pycover 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