system/cu: Skip the terminal related stuff if dev isn't a tty

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2023-09-04 02:31:17 +08:00 committed by Alin Jerpelea
parent 3ff51d108b
commit b60cdf4927

View File

@ -131,57 +131,58 @@ static int set_termios(FAR struct cu_globals_s *cu, int rate,
static int set_termios(FAR struct cu_globals_s *cu, int nocrlf) static int set_termios(FAR struct cu_globals_s *cu, int nocrlf)
#endif #endif
{ {
int rc = 0;
int ret; int ret;
struct termios tio; struct termios tio;
tio = cu->devtio; if (isatty(cu->devfd))
{
tio = cu->devtio;
#ifdef CONFIG_SERIAL_TERMIOS #ifdef CONFIG_SERIAL_TERMIOS
tio.c_cflag &= ~(PARENB | PARODD | CRTSCTS); tio.c_cflag &= ~(PARENB | PARODD | CRTSCTS);
switch (parity) switch (parity)
{ {
case PARITY_EVEN: case PARITY_EVEN:
tio.c_cflag |= PARENB; tio.c_cflag |= PARENB;
break; break;
case PARITY_ODD: case PARITY_ODD:
tio.c_cflag |= PARENB | PARODD; tio.c_cflag |= PARENB | PARODD;
break; break;
case PARITY_NONE: case PARITY_NONE:
break; break;
} }
/* set baudrate */ /* Set baudrate */
if (rate != 0) if (rate != 0)
{ {
cfsetspeed(&tio, rate); cfsetspeed(&tio, rate);
} }
if (rtscts) if (rtscts)
{ {
tio.c_cflag |= CRTS_IFLOW | CCTS_OFLOW; tio.c_cflag |= CRTS_IFLOW | CCTS_OFLOW;
} }
#endif #endif
tio.c_oflag = OPOST; tio.c_oflag = OPOST;
/* enable or disable \n -> \r\n conversion during write */ /* Enable or disable \n -> \r\n conversion during write */
if (nocrlf == 0) if (nocrlf == 0)
{ {
tio.c_oflag |= ONLCR; tio.c_oflag |= ONLCR;
} }
ret = tcsetattr(cu->devfd, TCSANOW, &tio); ret = tcsetattr(cu->devfd, TCSANOW, &tio);
if (ret) if (ret)
{ {
cu_error("set_termios: ERROR during tcsetattr(): %d\n", errno); cu_error("set_termios: ERROR during tcsetattr(): %d\n", errno);
rc = -1; return ret;
goto errout; }
} }
/* Let the remote machine to handle all crlf/echo except Ctrl-C */ /* Let the remote machine to handle all crlf/echo except Ctrl-C */
@ -197,19 +198,21 @@ static int set_termios(FAR struct cu_globals_s *cu, int nocrlf)
ret = tcsetattr(cu->stdfd, TCSANOW, &tio); ret = tcsetattr(cu->stdfd, TCSANOW, &tio);
if (ret) if (ret)
{ {
cu_error("set_termios: ERROR during tcsetattr(): %d\n", cu_error("set_termios: ERROR during tcsetattr(): %d\n", errno);
errno); return ret;
rc = -1;
} }
} }
errout: return 0;
return rc;
} }
static void retrieve_termios(FAR struct cu_globals_s *cu) static void retrieve_termios(FAR struct cu_globals_s *cu)
{ {
tcsetattr(cu->devfd, TCSANOW, &cu->devtio); if (isatty(cu->devfd))
{
tcsetattr(cu->devfd, TCSANOW, &cu->devtio);
}
if (cu->stdfd >= 0) if (cu->stdfd >= 0)
{ {
tcsetattr(cu->stdfd, TCSANOW, &cu->stdtio); tcsetattr(cu->stdfd, TCSANOW, &cu->stdtio);
@ -361,17 +364,20 @@ int main(int argc, FAR char *argv[])
if (cu->devfd < 0) if (cu->devfd < 0)
{ {
cu_error("cu_main: ERROR: Failed to open %s for writing: %d\n", cu_error("cu_main: ERROR: Failed to open %s for writing: %d\n",
devname, errno); devname, errno);
goto errout_with_devinit; goto errout_with_devinit;
} }
/* Remember serial device termios attributes */ /* Remember serial device termios attributes */
ret = tcgetattr(cu->devfd, &cu->devtio); if (isatty(cu->devfd))
if (ret)
{ {
cu_error("cu_main: ERROR during tcgetattr(): %d\n", errno); ret = tcgetattr(cu->devfd, &cu->devtio);
goto errout_with_devfd; if (ret)
{
cu_error("cu_main: ERROR during tcgetattr(): %d\n", errno);
goto errout_with_devfd;
}
} }
/* Remember std termios attributes if it is a tty. Try to select /* Remember std termios attributes if it is a tty. Try to select