Skip to content

Commit cb53e40

Browse files
aescolarhenrikbrixandersen
authored andcommitted
drivers uart_native_posix: rename to native_pty and support N instances
Rename the driver from uart_native_posix to uart_native_pty. Including renaming the DTS compatible, and kconfig options, deprecating the old ones. And refactor the driver, generalizing it, so we can have any number of instances. Note that, unfortunately generalizing to N instances cannot be done without a degree of backwards compatibility breakage: This driver was born with all its configuration and selection of the instances based on kconfig. When the driver was made to use DT, it was done in a way that required both DT and kconfig needing to manually coherently enable the 2nd UART. This has now been fixed, which it means only DT is used to decide how many instances are avaliable, and UART_NATIVE_POSIX_PORT_1_ENABLE is just ignored. Including: * Deprecate UART_NATIVE_WAIT_PTS_READY_ENABLE: the options is always on now as it has no practical drawbacks. * Deprecate UART_NATIVE_POSIX_PORT_1_ENABLE: DTS intanciation defines it being available now. * Rename a few functions and in general shorten pseudo-tty/pseudo- terminal to PTY instead of PTTY. Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
1 parent d811f69 commit cb53e40

File tree

37 files changed

+538
-435
lines changed

37 files changed

+538
-435
lines changed

boards/native/native_sim/doc/index.rst

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,10 @@ The following peripherals are currently provided with this board:
310310
**UART/Serial**
311311
Two optional native UART drivers are available:
312312

313-
**PTTY driver (UART_NATIVE_POSIX)**
314-
With this driver, one or two Zephyr UART devices can be created. These
313+
**PTY driver (UART_NATIVE_PTY)**
314+
With this driver, Zephyr UART devices can be created. These
315315
can be connected to the Linux process stdin/stdout or a newly created
316-
pseudo-tty. For more information refer to the section `PTTY UART`_.
316+
pseudo-tty. For more information refer to the section `PTY UART`_.
317317

318318
**TTY driver (UART_NATIVE_TTY)**
319319
An UART driver for interacting with host-attached serial port devices
@@ -510,44 +510,46 @@ The following peripherals are currently provided with this board:
510510

511511
.. _native_ptty_uart:
512512

513-
PTTY UART
513+
PTY UART
514514
=========
515515

516-
This driver can be configured with :kconfig:option:`CONFIG_UART_NATIVE_POSIX`
517-
to instantiate up to two UARTs. By default only one UART is enabled.
518-
With :kconfig:option:`CONFIG_UART_NATIVE_POSIX_PORT_1_ENABLE`
519-
you can enable the second one.
520-
521-
For the first UART, it can link it to a new
522-
pseudoterminal (i.e. :file:`/dev/pts{<nbr>}`), or map the UART input and
523-
output to the executable's ``stdin`` and ``stdout``.
524-
This is chosen by selecting either
525-
:kconfig:option:`CONFIG_NATIVE_UART_0_ON_OWN_PTY` or
526-
:kconfig:option:`CONFIG_NATIVE_UART_0_ON_STDINOUT`
527-
For interactive use with the :ref:`shell_api`, choose the first (OWN_PTY) option.
528-
The second (STDINOUT) option can be used with the shell for automated
529-
testing, such as when piping other processes' output to control it.
530-
This is because the shell subsystem expects access to a raw terminal,
516+
This driver is automatically enabled when devicetree contains nodes with the
517+
``"zephyr,native-pty-uart"`` compatible property and ``okay`` status and
518+
:kconfig:option:`CONFIG_SERIAL` is set.
519+
By default one ready UART of this type is setup in DTS, but any number can be enabled as desired.
520+
521+
Normally these UARTs are connected to new pseudoterminals PTYs, i.e. :file:`/dev/pts{<nbr>}`,
522+
but it is also possible to map one of them to the executable's ``stdin`` and ``stdout``.
523+
This can be done in two ways, either with the command line option ``--<uart_name>_stdinout``
524+
(where ``<uart_name>`` is the UART DTS node name), or, for the first PTY UART instance by chosing
525+
:kconfig:option:`CONFIG_UART_NATIVE_PTY_0_ON_STDINOUT` instead of the default
526+
:kconfig:option:`CONFIG_UART_NATIVE_PTY_0_ON_OWN_PTY`.
527+
For interactive use with the :ref:`shell_api`, it is recommended to choose the PTY option.
528+
The ``STDINOUT`` option can be used for automated testing, such as when piping other processes'
529+
output to control it. This is because the shell subsystem expects access to a raw terminal,
531530
which (by default) a normal Linux terminal is not.
532531

533-
When :kconfig:option:`CONFIG_NATIVE_UART_0_ON_OWN_PTY` is chosen, the name of the
534-
newly created UART pseudo-terminal will be displayed in the console.
535-
If you want to interact with it manually, you should attach a terminal emulator
536-
to it. This can be done, for example with the command:
532+
When a UART is connected to a new PTY, the name of the newly created UART pseudo-terminal will be
533+
displayed in the console.
534+
If you want to interact with it manually, you should attach a terminal emulator to it.
535+
This can be done, for example with the command:
537536

538537
.. code-block:: console
539538
540-
$ xterm -e screen /dev/<ttyn> &
539+
$ xterm -e screen /dev/<ptyn> &
540+
# Or if you prefer gnome-terminal:
541+
$ gnome-terminal -- screen /dev/<ptyn> &
541542
542-
where :file:`/dev/tty{<n>}` should be replaced with the actual TTY device.
543+
where :file:`/dev/{<ptyn>}` should be replaced with the actual PTY device.
543544

544-
You may also chose to automatically attach a terminal emulator to the first UART
545-
by passing the command line option ``-attach_uart`` to the executable.
546-
The command used for attaching to the new shell can be set with the command line
547-
option ``-attach_uart_cmd=<"cmd">``. Where the default command is given by
548-
:kconfig:option:`CONFIG_NATIVE_UART_AUTOATTACH_DEFAULT_CMD`.
549-
Note that the default command assumes both ``xterm`` and ``screen`` are
550-
installed in the system.
545+
You may also chose to automatically attach a terminal emulator to any of these UARTs.
546+
To automatically attach one to all these UARTs, pass the command line option ``-attach_uart`` to the
547+
executable. To automatically attach one to a single UART use ``-<uart_name>_attach_uart``
548+
The command used for attaching to the new shell can be set for all UARTs with the command line
549+
option ``-attach_uart_cmd=<"cmd">``, or for each individual UART with
550+
``-<uart_name>_attach_uart_cmd``. Where the default command is given by
551+
:kconfig:option:`CONFIG_UART_NATIVE_PTY_AUTOATTACH_DEFAULT_CMD`.
552+
Note that the default command assumes both ``xterm`` and ``screen`` are installed in the system.
551553

552554
This driver only supports poll mode. Interrupt and async mode are not supported.
553555
Neither runtime configuration or line control are supported.
@@ -713,7 +715,7 @@ host libC (:kconfig:option:`CONFIG_EXTERNAL_LIBC`):
713715
Logger backend, :ref:`Native backend <nsim_back_logger>`, :kconfig:option:`CONFIG_LOG_BACKEND_NATIVE_POSIX`, All
714716
Offloaded sockets, :ref:`nsim_per_offloaded_sockets`, :kconfig:option:`CONFIG_NET_NATIVE_OFFLOADED_SOCKETS`, All
715717
RTC, RTC emul, :kconfig:option:`CONFIG_RTC_EMUL`, All
716-
Serial, :ref:`UART native posix/PTTY <native_ptty_uart>`, :kconfig:option:`CONFIG_UART_NATIVE_POSIX`, All
718+
Serial, :ref:`UART native PTY <native_ptty_uart>`, :kconfig:option:`CONFIG_UART_NATIVE_PTY`, All
717719
Serial, :ref:`UART native TTY <native_tty_uart>`, :kconfig:option:`CONFIG_UART_NATIVE_TTY`, All
718720
SPI, SPI emul, :kconfig:option:`CONFIG_SPI_EMUL`, All
719721
System tick, Native_posix timer, :kconfig:option:`CONFIG_NATIVE_POSIX_TIMER`, All

boards/native/native_sim/native_sim.dts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,16 @@
147147

148148
uart0: uart {
149149
status = "okay";
150-
compatible = "zephyr,native-posix-uart";
150+
compatible = "zephyr,native-pty-uart";
151151
/* Dummy current-speed entry to comply with serial
152152
* DTS binding
153153
*/
154154
current-speed = <0>;
155155
};
156156

157157
uart1: uart_1 {
158-
status = "okay";
159-
compatible = "zephyr,native-posix-uart";
158+
status = "disabled";
159+
compatible = "zephyr,native-pty-uart";
160160
/* Dummy current-speed entry to comply with serial
161161
* DTS binding
162162
*/

doc/connectivity/networking/eth_bridge_native_sim_setup.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Then create a sample and enable Ethernet bridging support. In this example we cr
5858
5959
west build -p -b native_sim -d ../build/echo-server \
6060
samples/net/sockets/echo_server -- \
61-
-DCONFIG_NATIVE_UART_AUTOATTACH_DEFAULT_CMD="\"gnome-terminal -- screen %s\"" \
61+
-DCONFIG_UART_NATIVE_PTY_AUTOATTACH_DEFAULT_CMD="\"gnome-terminal -- screen %s\"" \
6262
-DCONFIG_NET_ETHERNET_BRIDGE=y \
6363
-DCONFIG_NET_ETHERNET_BRIDGE_SHELL=y \
6464
-DCONFIG_ETH_NATIVE_TAP_INTERFACE_COUNT=2 \

doc/connectivity/networking/network_monitoring.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ In terminal #3, type:
117117
:zephyr-app: samples/net/capture
118118
:host-os: unix
119119
:board: native_sim
120-
:gen-args: -DCONFIG_NATIVE_UART_AUTOATTACH_DEFAULT_CMD=\""gnome-terminal -- screen %s"\"
120+
:gen-args: -DCONFIG_UART_NATIVE_PTY_AUTOATTACH_DEFAULT_CMD=\""gnome-terminal -- screen %s"\"
121121
:goals: build
122122
:compact:
123123

drivers/serial/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ if (CONFIG_UART_NRFX_UARTE)
103103
endif()
104104
endif()
105105

106-
if(CONFIG_UART_NATIVE_POSIX)
106+
if(CONFIG_UART_NATIVE_PTY)
107107
zephyr_library_compile_definitions(NO_POSIX_CHEATS)
108-
zephyr_library_sources(uart_native_ptty.c)
108+
zephyr_library_sources(uart_native_pty.c)
109109
if (CONFIG_NATIVE_APPLICATION)
110-
zephyr_library_sources(uart_native_ptty_bottom.c)
110+
zephyr_library_sources(uart_native_pty_bottom.c)
111111
else()
112-
target_sources(native_simulator INTERFACE uart_native_ptty_bottom.c)
112+
target_sources(native_simulator INTERFACE uart_native_pty_bottom.c)
113113
endif()
114114
endif()
115115

drivers/serial/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ rsource "Kconfig.mcux_lpuart"
191191
rsource "Kconfig.mec5"
192192
rsource "Kconfig.miv"
193193
rsource "Kconfig.msp432p4xx"
194-
rsource "Kconfig.native_posix"
194+
rsource "Kconfig.native_pty"
195195
rsource "Kconfig.native_tty"
196196
rsource "Kconfig.neorv32"
197197
rsource "Kconfig.npcx"

drivers/serial/Kconfig.native_posix

Lines changed: 0 additions & 74 deletions
This file was deleted.

drivers/serial/Kconfig.native_pty

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Copyright (c) 2025, Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config UART_NATIVE_PTY
5+
bool "PTY based UART driver for native_sim"
6+
default y
7+
depends on (DT_HAS_ZEPHYR_NATIVE_PTY_UART_ENABLED || DT_HAS_ZEPHYR_NATIVE_POSIX_UART_ENABLED)
8+
select SERIAL_HAS_DRIVER
9+
help
10+
This enables a PTY based UART driver for the POSIX ARCH with up to 2 UARTs.
11+
For the first UART port, the driver can be configured
12+
to either connect to the terminal from which the executable was run, or into
13+
one dedicated pseudoterminal for that UART.
14+
15+
config UART_NATIVE_POSIX
16+
bool "UART driver for native_sim (deprecated)"
17+
select SERIAL_HAS_DRIVER
18+
select DEPRECATED
19+
select UART_NATIVE_PTY
20+
help
21+
Deprecated option, use UART_NATIVE_PTY instead
22+
23+
if (UART_NATIVE_PTY || UART_NATIVE_POSIX)
24+
25+
choice UART_NATIVE_PTY_0
26+
prompt "Native PTY Port 0 connection"
27+
default UART_NATIVE_PTY_0_ON_STDINOUT if NATIVE_UART_0_ON_STDINOUT
28+
default UART_NATIVE_PTY_0_ON_OWN_PTY
29+
30+
config UART_NATIVE_PTY_0_ON_OWN_PTY
31+
bool "Connect the first PTY UART to its own pseudo terminal"
32+
help
33+
Connect this UART to its own pseudoterminal. This is the preferred
34+
option for users who want to use Zephyr's shell.
35+
Moreover this option does not conflict with any other native
36+
backend which may use the invoking shell standard input/output.
37+
Note it can be overridden from command line
38+
39+
config UART_NATIVE_PTY_0_ON_STDINOUT
40+
bool "Connect the first PTY UART to the invoking shell stdin/stdout"
41+
help
42+
Connect this UART to the stdin & stdout of the calling shell/terminal
43+
which invoked the native executable. This is good enough for
44+
automated testing, or when feeding from a file/pipe.
45+
Note that other, non UART messages, will also be printed to the
46+
terminal.
47+
It is strongly discouraged to try to use this option with the new
48+
shell interactively, as the default terminal configuration is NOT
49+
appropriate for interactive use.
50+
51+
endchoice
52+
53+
choice NATIVE_UART_0
54+
prompt "Native UART Port 0 connection (deprecated)"
55+
default NATIVE_UART_0_ON_OWN_PTY
56+
57+
config NATIVE_UART_0_ON_OWN_PTY
58+
bool "Connect the UART to its own pseudo terminal (deprecated)"
59+
help
60+
Deprecated, use UART_NATIVE_PTY_0_ON_OWN_PTY instead.
61+
62+
config NATIVE_UART_0_ON_STDINOUT
63+
bool "Connect the UART to the invoking shell stdin/stdout (deprecated)"
64+
select DEPRECATED
65+
help
66+
Deprecated, use UART_NATIVE_PTY_0_ON_STDINOUT instead.
67+
68+
endchoice
69+
70+
config UART_NATIVE_WAIT_PTS_READY_ENABLE
71+
bool "Support waiting for pseudo terminal client readiness (deprecated)"
72+
select DEPRECATED
73+
help
74+
This option is deprecated and does not do anything anymore. (The command line
75+
option --wait_uart is always available).
76+
77+
config UART_NATIVE_POSIX_PORT_1_ENABLE
78+
bool "Second UART port (deprecated)"
79+
select DEPRECATED
80+
help
81+
This option does not do anything anymore. Use DTS to instantiate as many
82+
"zephyr,native-pty-uart" compatible nodes as you want.
83+
84+
config UART_NATIVE_PTY_AUTOATTACH_DEFAULT_CMD
85+
string "Default command to attach a PTY UART to a new terminal"
86+
default NATIVE_UART_AUTOATTACH_DEFAULT_CMD
87+
help
88+
If the native executable is called with the --attach_uart
89+
command line option, this will be the default command which will be
90+
run to attach a new terminal to the 1st UART.
91+
Note that this command must have one, and only one, '%s' as
92+
placeholder for the pseudoterminal device name (e.g. /dev/pts/35)
93+
This is only applicable if the UART_0 is configured to use its own
94+
PTY with NATIVE_UART_0_ON_OWN_PTY.
95+
The 2nd UART will not be affected by this option.
96+
If you are using GNOME, then you can use this command string
97+
'gnome-terminal -- screen %s'
98+
99+
config NATIVE_UART_AUTOATTACH_DEFAULT_CMD
100+
string "Default command to attach the UART to a new terminal (deprecated)"
101+
default "xterm -e screen %s &"
102+
help
103+
Deprecated. Use UART_NATIVE_PTY_AUTOATTACH_DEFAULT_CMD instead
104+
105+
endif # UART_NATIVE_PTY || UART_NATIVE_POSIX

0 commit comments

Comments
 (0)