Skip to content

Commit 97696f3

Browse files
pkral78aescolar
authored andcommitted
uart: native_posix: Add support for pts readiness
Option to pause writing to the pseudo terminal until it is ready to receive data. Useful for pseudo terminal synchronization with other host processes. Signed-off-by: Pavel Král <[email protected]>
1 parent 8910d1c commit 97696f3

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

drivers/serial/Kconfig.native_posix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ config NATIVE_UART_0_ON_STDINOUT
3939

4040
endchoice
4141

42+
config UART_NATIVE_WAIT_PTS_READY_ENABLE
43+
bool "Support waiting for pseudo terminal client readiness"
44+
depends on NATIVE_UART_0_ON_OWN_PTY || UART_NATIVE_POSIX_PORT_1_ENABLE
45+
help
46+
When this option is selected a new command line switch is provided:
47+
`--wait_uart`
48+
When `--wait_uart` is used, writes to the UART will be held until a
49+
client has connected to the slave side of the pseudoterminal.
50+
Otherwise writes are sent irrespectively.
51+
4252
config UART_NATIVE_POSIX_PORT_1_ENABLE
4353
bool "Enable second UART port"
4454
help

drivers/serial/uart_native_posix.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <fcntl.h>
1616
#include <sys/select.h>
1717
#include <unistd.h>
18+
#include <poll.h>
1819

1920
#include <drivers/uart.h>
2021
#include "cmdline.h" /* native_posix command line options header */
@@ -46,6 +47,7 @@ static void np_uart_poll_out(const struct device *dev,
4647
unsigned char out_char);
4748

4849
static bool auto_attach;
50+
static bool wait_pts;
4951
static const char default_cmd[] = CONFIG_NATIVE_UART_AUTOATTACH_DEFAULT_CMD;
5052
static char *auto_attach_cmd;
5153

@@ -184,6 +186,15 @@ static int open_tty(struct native_uart_status *driver_data,
184186
posix_print_trace("%s connected to pseudotty: %s\n",
185187
uart_name, slave_pty_name);
186188

189+
if (wait_pts) {
190+
/*
191+
* This trick sets the HUP flag on the tty master, making it
192+
* possible to detect a client connection using poll.
193+
* The connection of the client would cause the HUP flag to be
194+
* cleared, and in turn set again at disconnect.
195+
*/
196+
close(open(slave_pty_name, O_RDWR | O_NOCTTY));
197+
}
187198
if (do_auto_attach) {
188199
attach_to_tty(slave_pty_name);
189200
}
@@ -261,9 +272,21 @@ static void np_uart_poll_out(const struct device *dev,
261272
unsigned char out_char)
262273
{
263274
int ret;
264-
struct native_uart_status *d;
275+
struct native_uart_status *d = (struct native_uart_status *)dev->data;
276+
277+
if (wait_pts) {
278+
struct pollfd pfd = { .fd = d->out_fd, .events = POLLHUP };
279+
280+
while (1) {
281+
poll(&pfd, 1, 0);
282+
if (!(pfd.revents & POLLHUP)) {
283+
/* There is now a reader on the slave side */
284+
break;
285+
}
286+
k_sleep(K_MSEC(100));
287+
}
288+
}
265289

266-
d = (struct native_uart_status *)dev->data;
267290
ret = write(d->out_fd, &out_char, 1);
268291

269292
if (ret != 1) {
@@ -379,7 +402,13 @@ static void np_add_uart_options(void)
379402
(void *)&auto_attach_cmd, NULL,
380403
"Command used to automatically attach to the terminal, by "
381404
"default: '" CONFIG_NATIVE_UART_AUTOATTACH_DEFAULT_CMD "'"},
382-
405+
IF_ENABLED(CONFIG_UART_NATIVE_WAIT_PTS_READY_ENABLE, (
406+
{false, false, true,
407+
"wait_uart", "", 'b',
408+
(void *)&wait_pts, NULL,
409+
"Hold writes to the uart/pts until a client is "
410+
"connected/ready"},)
411+
)
383412
ARG_TABLE_ENDMARKER
384413
};
385414

0 commit comments

Comments
 (0)