Skip to content

Commit e3f727c

Browse files
aescolarAnas Nashif
authored andcommitted
native: added stdin handling for shell
Now the native console driver also handles stdin so we can drive the shell from the command line, a pipe or a file Signed-off-by: Alberto Escolar Piedras <[email protected]>
1 parent a7e55b8 commit e3f727c

File tree

6 files changed

+399
-16
lines changed

6 files changed

+399
-16
lines changed

boards/posix/native_posix/Kconfig.board

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ config BOARD_NATIVE_POSIX
33
bool "Native POSIX"
44
depends on SOC_POSIX
55
select NATIVE_POSIX_TIMER
6+
select NATIVE_POSIX_CONSOLE
67
help
78
Will produce a console Linux process which can be executed natively.
89
It provides some minimal needed models:

boards/posix/native_posix/doc/board.rst

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,23 @@ Peripherals
342342

343343
The following peripherals are currently provided with this board:
344344

345-
**Console/printk driver**:
346-
A driver is provided that redirects any printk write to the native
347-
host application's stdout.
345+
**Console driver**:
346+
A console driver is provided which by default is configured to:
347+
348+
- Redirect any :c:func:`printk` write to the native host application's
349+
`stdout`.
350+
351+
- Feed any input from the native application `stdin` to a possible
352+
running :ref:`Shell`. For more information refer to the section
353+
`Shell support`_.
348354

349355
**Simple timer**:
350356
A simple timer provides the kernel with a 10ms tick.
351357
This peripheral driver also provides the needed functionality for this
352358
architecture-specific :c:func:`k_busy_wait`.
353359

354-
This timer, is configured by default with NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME_
360+
This timer, is configured by default with
361+
:option:`CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME`
355362
to slow down the execution to real host time.
356363
This will provide the illusion that the simulated time is running at the same
357364
speed as the real host time.
@@ -361,11 +368,66 @@ The following peripherals are currently provided with this board:
361368
Normally the Zephyr application and HW models run in very little time
362369
on the host CPU, so this is a good enough approach.
363370

364-
.. _NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME:
365-
../../../../reference/kconfig/CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME.html
366-
367371
**Interrupt controller**:
368372
A simple yet generic interrupt controller is provided. It can nest interrupts
369373
and provides interrupt priorities. Interrupts can be individually masked or
370374
unmasked. SW interrupts are also supported.
371375

376+
Shell support
377+
*************
378+
379+
When the :ref:`Shell` subsystem is compiled with your application, the native
380+
standard input (`stdin`) will be redirected to the shell.
381+
You may use the shell interactively through the console,
382+
by piping another process output to it, or by feeding it a file.
383+
384+
When using it interactively you may want to select the option
385+
:option:`CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME`.
386+
387+
When feeding `stdin` from a pipe or file, the console driver will ensure
388+
reproducibility between runs of the process:
389+
390+
- The execution of the process will be stalled while waiting for new `stdin`
391+
data to be ready.
392+
393+
- Commands will be fed to the shell as fast as the shell can process them.
394+
To allow controlling the flow of commands to the shell, you may use the
395+
driver directive ``!wait <ms>``.
396+
397+
- When the file ends, or the pipe is closed the driver will stop attempting to
398+
read it.
399+
400+
Driver directives
401+
=================
402+
403+
The console driver understands a set of special commands: driver directives.
404+
These directives are captured by the console driver itself and are not
405+
forwarded to the shell.
406+
These directives are:
407+
408+
- ``!wait <ms>``: When received, the driver will pause feeding commands to the
409+
shell for `<ms>` milliseconds.
410+
411+
- ``!quit``: When received the driver will cause the application to gracefully
412+
exit by calling :c:func:`posix_exit`.
413+
414+
415+
Use example
416+
===========
417+
418+
For example, you can build the shell sample app:
419+
420+
.. zephyr-app-commands::
421+
:zephyr-app: samples/subsys/shell/shell_module
422+
:host-os: unix
423+
:board: native_posix
424+
:goals: build
425+
:compact:
426+
427+
And feed it the following set of commands through a pipe:
428+
429+
.. code-block:: console
430+
431+
echo -e \
432+
'select kernel\nuptime\n!wait 500\nuptime\n!wait 1000\nuptime\n!quit' \
433+
| zephyr/zephyr.exe

drivers/console/Kconfig

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,44 @@ config XTENSA_SIM_CONSOLE
200200

201201
config NATIVE_POSIX_CONSOLE
202202
bool
203-
prompt "Print to console"
204-
depends on BOARD_NATIVE_POSIX
203+
prompt "Use native stdin/stdout for console"
204+
depends on ARCH_POSIX
205205
select CONSOLE_HAS_DRIVER
206206
default y
207207
help
208-
Use native console to print messages.
208+
Use native stdin and stdout to print messages and get input
209+
210+
config NATIVE_POSIX_STDIN_CONSOLE
211+
bool "Use native stdin for console"
212+
prompt "Read from native stdin for console"
213+
depends on NATIVE_POSIX_CONSOLE
214+
default y
215+
help
216+
Use native console to get input.
217+
218+
config NATIVE_STDIN_POLL_PERIOD
219+
int
220+
prompt "Polling period for stdin"
221+
depends on NATIVE_POSIX_STDIN_CONSOLE
222+
default 20
223+
help
224+
In ms, polling period for stdin
225+
226+
config NATIVE_STDIN_PRIO
227+
int
228+
prompt "Priority of the stdin polling thread"
229+
depends on NATIVE_POSIX_STDIN_CONSOLE
230+
default 4
231+
help
232+
Prioriry of the native stdin polling thread
233+
234+
config NATIVE_POSIX_STDOUT_CONSOLE
235+
bool "Use native stdout for console"
236+
prompt "Print to native stdout"
237+
depends on NATIVE_POSIX_CONSOLE
238+
default y
239+
help
240+
Use native console (stout) to print messages.
209241

210242
config XTENSA_CONSOLE_INIT_PRIORITY
211243
int

0 commit comments

Comments
 (0)