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,10 +131,11 @@ 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;
if (isatty(cu->devfd))
{
tio = cu->devtio; tio = cu->devtio;
#ifdef CONFIG_SERIAL_TERMIOS #ifdef CONFIG_SERIAL_TERMIOS
@ -154,7 +155,7 @@ static int set_termios(FAR struct cu_globals_s *cu, int nocrlf)
break; break;
} }
/* set baudrate */ /* Set baudrate */
if (rate != 0) if (rate != 0)
{ {
@ -169,7 +170,7 @@ static int set_termios(FAR struct cu_globals_s *cu, int nocrlf)
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)
{ {
@ -180,8 +181,8 @@ static int set_termios(FAR struct cu_globals_s *cu, int nocrlf)
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)
{ {
if (isatty(cu->devfd))
{
tcsetattr(cu->devfd, TCSANOW, &cu->devtio); 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);
@ -367,12 +370,15 @@ int main(int argc, FAR char *argv[])
/* Remember serial device termios attributes */ /* Remember serial device termios attributes */
if (isatty(cu->devfd))
{
ret = tcgetattr(cu->devfd, &cu->devtio); ret = tcgetattr(cu->devfd, &cu->devtio);
if (ret) if (ret)
{ {
cu_error("cu_main: ERROR during tcgetattr(): %d\n", errno); cu_error("cu_main: ERROR during tcgetattr(): %d\n", errno);
goto errout_with_devfd; 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
* right descriptor that is used to refer to tty * right descriptor that is used to refer to tty