2016-07-15 21:28:38 +02:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/pty_test/pty_test.c
|
2016-07-15 21:28:38 +02:00
|
|
|
*
|
2021-06-08 10:21:48 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-07-15 21:28:38 +02:00
|
|
|
*
|
2021-06-08 10:21:48 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-07-15 21:28:38 +02:00
|
|
|
*
|
2021-06-08 10:21:48 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-07-15 21:28:38 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
#include <nuttx/config.h>
|
2016-07-15 21:28:38 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2016-07-16 19:24:54 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2016-07-16 20:38:44 +02:00
|
|
|
#include <termios.h>
|
2016-07-16 19:24:54 +02:00
|
|
|
#include <poll.h>
|
2016-07-15 21:28:38 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2016-07-16 19:24:54 +02:00
|
|
|
#include <sys/ioctl.h>
|
2016-07-15 21:28:38 +02:00
|
|
|
|
|
|
|
#include <nuttx/serial/pty.h>
|
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
#include "nshlib/nshlib.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_PTYTEST_SERIALDEV
|
|
|
|
# define CONFIG_EXAMPLES_PTYTEST_SERIALDEV "/dev/ttyS1"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_PTYTEST_DAEMONPRIO
|
|
|
|
# define CONFIG_EXAMPLES_PTYTEST_DAEMONPRIO SCHED_PRIORITY_DEFAULT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_PTYTEST_STACKSIZE
|
|
|
|
# define CONFIG_EXAMPLES_PTYTEST_STACKSIZE 2048
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define POLL_TIMEOUT 200
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct term_pair_s
|
|
|
|
{
|
|
|
|
int fd_uart;
|
|
|
|
int fd_pty;
|
|
|
|
};
|
|
|
|
|
2016-07-15 21:28:38 +02:00
|
|
|
/****************************************************************************
|
2016-07-16 19:24:54 +02:00
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: serial_in
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read data from serial and write to pts
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void serial_in(struct term_pair_s *tp)
|
|
|
|
{
|
2016-07-16 21:46:52 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_PTYTEST_POLL
|
2016-07-16 19:24:54 +02:00
|
|
|
struct pollfd fdp;
|
2016-07-16 21:46:52 +02:00
|
|
|
#endif
|
2016-07-16 19:24:54 +02:00
|
|
|
char buffer[16];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!tp)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: terminal pair struct is NULL!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-16 21:46:52 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_PTYTEST_POLL
|
2016-07-16 19:24:54 +02:00
|
|
|
fdp.fd = tp->fd_uart;
|
|
|
|
fdp.events = POLLIN;
|
2016-07-16 21:46:52 +02:00
|
|
|
#endif
|
2016-07-16 19:24:54 +02:00
|
|
|
|
|
|
|
/* Run forever */
|
|
|
|
|
2020-06-15 20:20:29 +02:00
|
|
|
while (true)
|
2016-07-16 19:24:54 +02:00
|
|
|
{
|
2016-07-16 21:46:52 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_PTYTEST_POLL
|
2016-07-16 19:24:54 +02:00
|
|
|
ret = poll((struct pollfd *)&fdp, 1, POLL_TIMEOUT);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
if ((fdp.revents & POLLIN) != 0)
|
2016-07-16 21:46:52 +02:00
|
|
|
#endif
|
2016-07-16 19:24:54 +02:00
|
|
|
{
|
2016-07-16 21:46:52 +02:00
|
|
|
ssize_t len;
|
2016-07-16 19:24:54 +02:00
|
|
|
|
|
|
|
len = read(tp->fd_uart, buffer, 16);
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR Failed to read from serial: %d\n",
|
|
|
|
errno);
|
|
|
|
}
|
|
|
|
else if (len > 0)
|
|
|
|
{
|
|
|
|
ret = write(tp->fd_pty, buffer, len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Failed to write to serial: %d\n",
|
|
|
|
errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-16 21:46:52 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_PTYTEST_POLL
|
2016-07-16 19:24:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Timeout */
|
|
|
|
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Poll error */
|
|
|
|
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: poll failed: %d\n", errno);
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-16 21:46:52 +02:00
|
|
|
#endif
|
2016-07-16 19:24:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: serial_out
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read data from pts and write to serial
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void serial_out(struct term_pair_s *tp)
|
|
|
|
{
|
2016-07-16 21:46:52 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_PTYTEST_POLL
|
2016-07-16 19:24:54 +02:00
|
|
|
struct pollfd fdp;
|
2016-07-16 21:46:52 +02:00
|
|
|
#endif
|
2016-07-16 19:24:54 +02:00
|
|
|
char buffer[16];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!tp)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: terminal pair struct is NULL!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-16 21:46:52 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_PTYTEST_POLL
|
2016-07-16 19:24:54 +02:00
|
|
|
fdp.fd = tp->fd_pty;
|
|
|
|
fdp.events = POLLIN;
|
2016-07-16 21:46:52 +02:00
|
|
|
#endif
|
2016-07-16 19:24:54 +02:00
|
|
|
|
|
|
|
/* Run forever */
|
|
|
|
|
2020-06-15 20:20:29 +02:00
|
|
|
while (true)
|
2016-07-16 19:24:54 +02:00
|
|
|
{
|
2016-07-16 21:46:52 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_PTYTEST_POLL
|
2016-07-16 19:24:54 +02:00
|
|
|
ret = poll((struct pollfd *)&fdp, 1, POLL_TIMEOUT);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
2016-07-18 19:26:45 +02:00
|
|
|
if ((fdp.revents & POLLIN) != 0)
|
2016-07-16 21:46:52 +02:00
|
|
|
#endif
|
2016-07-16 19:24:54 +02:00
|
|
|
{
|
2016-07-16 21:46:52 +02:00
|
|
|
ssize_t len;
|
2016-07-16 19:24:54 +02:00
|
|
|
|
|
|
|
len = read(tp->fd_pty, buffer, 16);
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Failed to read from pseudo-terminal: %d\n",
|
|
|
|
errno);
|
|
|
|
}
|
|
|
|
else if (len > 0)
|
|
|
|
{
|
|
|
|
ret = write(tp->fd_uart, buffer, len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Failed to write to serial: %d\n",
|
|
|
|
errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-16 21:46:52 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_PTYTEST_POLL
|
2016-07-16 19:24:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Timeout */
|
|
|
|
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* poll error */
|
|
|
|
|
|
|
|
else if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: poll failed: %d\n", errno);
|
|
|
|
continue;
|
|
|
|
}
|
2016-07-16 21:46:52 +02:00
|
|
|
#endif
|
2016-07-16 19:24:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pty_test_main
|
2016-07-15 21:28:38 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
2016-07-16 19:24:54 +02:00
|
|
|
struct term_pair_s termpair;
|
2016-07-16 20:38:44 +02:00
|
|
|
struct termios tio;
|
2016-07-16 19:24:54 +02:00
|
|
|
pthread_t si_thread;
|
|
|
|
pthread_t so_thread;
|
2016-07-15 21:28:38 +02:00
|
|
|
char buffer[16];
|
2016-07-16 19:24:54 +02:00
|
|
|
pid_t pid;
|
|
|
|
int fd_pts;
|
2016-07-15 21:28:38 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
printf("Create pseudo-terminal\n");
|
|
|
|
|
|
|
|
/* Open pseudo-terminal master (pts factory) */
|
2016-07-15 21:28:38 +02:00
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
termpair.fd_pty = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
|
|
|
if (termpair.fd_pty < 0)
|
2016-07-15 21:28:38 +02:00
|
|
|
{
|
2016-07-16 19:24:54 +02:00
|
|
|
fprintf(stderr, "ERROR: Failed to opening /dev/ptmx: %d\n",
|
|
|
|
errno);
|
|
|
|
return EXIT_FAILURE;
|
2016-07-15 21:28:38 +02:00
|
|
|
}
|
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
/* Grant access and unlock the slave PTY */
|
2016-07-15 21:28:38 +02:00
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
ret = grantpt(termpair.fd_pty);
|
2016-07-15 22:09:14 +02:00
|
|
|
if (ret < 0)
|
2016-07-15 21:28:38 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: grantpt() failed: %d\n", errno);
|
2016-07-16 19:24:54 +02:00
|
|
|
goto error_pts;
|
2016-07-15 21:28:38 +02:00
|
|
|
}
|
|
|
|
|
2016-07-16 19:56:17 +02:00
|
|
|
ret = unlockpt(termpair.fd_pty);
|
2016-07-15 22:09:14 +02:00
|
|
|
if (ret < 0)
|
2016-07-15 21:28:38 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: unlockpt() failed: %d\n", errno);
|
2016-07-16 19:24:54 +02:00
|
|
|
goto error_pts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the create pts file-name */
|
|
|
|
|
|
|
|
ret = ptsname_r(termpair.fd_pty, buffer, 16);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: ptsname_r() failed: %d\n", errno);
|
|
|
|
goto error_pts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the created pts */
|
|
|
|
|
2021-01-04 08:50:50 +01:00
|
|
|
fd_pts = open(buffer, O_RDWR);
|
2016-07-16 19:24:54 +02:00
|
|
|
if (fd_pts < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to open %s: %d\n", buffer, errno);
|
|
|
|
goto error_pts;
|
2016-07-15 21:28:38 +02:00
|
|
|
}
|
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
/* Open the second serial port to create a new console there */
|
|
|
|
|
2021-01-13 10:03:57 +01:00
|
|
|
termpair.fd_uart = open(CONFIG_EXAMPLES_PTYTEST_SERIALDEV, O_RDWR);
|
2016-07-16 19:24:54 +02:00
|
|
|
if (termpair.fd_uart < 0)
|
|
|
|
{
|
2020-06-15 17:27:13 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_PTYTEST_WAIT_CONNECTED
|
|
|
|
/* if ENOTCONN is received, re-attempt to open periodically */
|
|
|
|
|
|
|
|
if (errno == ENOTCONN)
|
|
|
|
{
|
2020-06-15 20:20:29 +02:00
|
|
|
fprintf(stderr, "ERROR: device not connected, will continue"
|
|
|
|
" trying\n");
|
2020-06-15 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (termpair.fd_uart < 0 && errno == ENOTCONN)
|
|
|
|
{
|
|
|
|
sleep(1);
|
|
|
|
|
2021-01-04 08:50:50 +01:00
|
|
|
termpair.fd_uart = open(CONFIG_EXAMPLES_PTYTEST_SERIALDEV, O_RDWR);
|
2020-06-15 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if we exited due to an error different than ENOTCONN */
|
|
|
|
|
|
|
|
if (termpair.fd_uart < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to open %s: %i\n",
|
|
|
|
CONFIG_EXAMPLES_PTYTEST_SERIALDEV, errno);
|
|
|
|
goto error_serial;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
fprintf(stderr, "Failed to open %s: %i\n",
|
2016-07-16 19:24:54 +02:00
|
|
|
CONFIG_EXAMPLES_PTYTEST_SERIALDEV, errno);
|
|
|
|
goto error_serial;
|
2020-06-15 17:27:13 +02:00
|
|
|
#endif
|
2016-07-16 19:24:54 +02:00
|
|
|
}
|
|
|
|
|
2016-07-19 22:28:07 +02:00
|
|
|
#ifdef CONFIG_SERIAL_TERMIOS
|
2016-07-16 20:38:44 +02:00
|
|
|
/* Enable \n -> \r\n conversion during write */
|
|
|
|
|
|
|
|
ret = tcgetattr(termpair.fd_uart, &tio);
|
|
|
|
if (ret)
|
|
|
|
{
|
2016-07-17 17:39:26 +02:00
|
|
|
fprintf(stderr, "ERROR: tcgetattr() failed: %d\n", errno);
|
2016-07-16 20:38:44 +02:00
|
|
|
goto error_serial;
|
|
|
|
}
|
|
|
|
|
|
|
|
tio.c_oflag = OPOST | ONLCR;
|
|
|
|
ret = tcsetattr(termpair.fd_uart, TCSANOW, &tio);
|
|
|
|
if (ret)
|
|
|
|
{
|
2016-07-17 17:39:26 +02:00
|
|
|
fprintf(stderr, "ERROR: tcsetattr() failed: %d\n", errno);
|
2016-07-16 20:38:44 +02:00
|
|
|
goto error_serial;
|
|
|
|
}
|
2016-07-19 22:28:07 +02:00
|
|
|
#endif
|
2016-07-16 20:38:44 +02:00
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
printf("Starting a new NSH Session using %s\n", buffer);
|
|
|
|
|
|
|
|
/* Close default stdin, stdout and stderr */
|
2016-07-15 21:28:38 +02:00
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
close(0);
|
|
|
|
close(1);
|
|
|
|
close(2);
|
2016-07-15 21:28:38 +02:00
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
/* Use this pts file as stdin, stdout, and stderr */
|
2016-07-15 21:28:38 +02:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
dup2(fd_pts, 0);
|
|
|
|
dup2(fd_pts, 1);
|
|
|
|
dup2(fd_pts, 2);
|
2016-07-16 19:24:54 +02:00
|
|
|
|
|
|
|
/* Create a new console using this /dev/pts/N */
|
|
|
|
|
|
|
|
pid = task_create("NSH Console", CONFIG_EXAMPLES_PTYTEST_DAEMONPRIO,
|
2020-06-15 20:20:29 +02:00
|
|
|
CONFIG_EXAMPLES_PTYTEST_STACKSIZE, nsh_consolemain,
|
2021-01-04 08:55:39 +01:00
|
|
|
&argv[1]);
|
2016-07-16 19:24:54 +02:00
|
|
|
if (pid < 0)
|
2016-07-15 21:28:38 +02:00
|
|
|
{
|
2016-07-16 19:24:54 +02:00
|
|
|
/* Can't do output because stdout and stderr are redirected */
|
|
|
|
|
|
|
|
goto error_console;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start a thread to read from serial */
|
2016-07-15 22:09:14 +02:00
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
ret = pthread_create(&si_thread, NULL,
|
|
|
|
(pthread_startroutine_t)serial_in,
|
|
|
|
(pthread_addr_t)&termpair);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
/* Can't do output because stdout and stderr are redirected */
|
2016-07-15 21:28:38 +02:00
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
goto error_thread1;
|
2016-07-15 21:28:38 +02:00
|
|
|
}
|
|
|
|
|
2016-07-16 19:24:54 +02:00
|
|
|
/* Start a thread to write to serial */
|
|
|
|
|
|
|
|
ret = pthread_create(&so_thread, NULL,
|
|
|
|
(pthread_startroutine_t)serial_out,
|
|
|
|
(pthread_addr_t)&termpair);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
/* Can't do output because stdout and stderr are redirected */
|
|
|
|
|
|
|
|
goto error_thread2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stay here to keep the threads running */
|
|
|
|
|
2020-06-15 20:20:29 +02:00
|
|
|
while (true)
|
2016-07-16 19:24:54 +02:00
|
|
|
{
|
|
|
|
/* Nothing to do, then sleep to avoid eating all cpu time */
|
|
|
|
|
2020-06-15 17:26:21 +02:00
|
|
|
pause();
|
2016-07-16 19:24:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
error_thread2:
|
|
|
|
error_thread1:
|
|
|
|
error_console:
|
|
|
|
close(termpair.fd_uart);
|
|
|
|
error_serial:
|
|
|
|
close(fd_pts);
|
|
|
|
error_pts:
|
|
|
|
close(termpair.fd_pty);
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
2016-07-15 21:28:38 +02:00
|
|
|
}
|