2017-10-15 22:41:59 +08:00
|
|
|
/*
|
|
|
|
* File : termios.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2017, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2017/08/30 Bernard The first version
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <dfs_posix.h>
|
|
|
|
|
|
|
|
#include <termios.h>
|
|
|
|
|
|
|
|
int tcgetattr(int fd, struct termios *tio)
|
|
|
|
{
|
2017-10-17 22:24:23 +08:00
|
|
|
/* Get the current serial port settings. */
|
2017-10-15 22:41:59 +08:00
|
|
|
if (ioctl(fd, TCGETA, tio))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcsetattr(int fd, int act, const struct termios *tio)
|
|
|
|
{
|
2017-12-07 11:06:10 +08:00
|
|
|
switch (act)
|
2017-10-17 22:24:23 +08:00
|
|
|
{
|
|
|
|
case TCSANOW:
|
|
|
|
/* make the change immediately */
|
|
|
|
return (ioctl(fd, TCSETA, (void*)tio));
|
|
|
|
case TCSADRAIN:
|
|
|
|
/*
|
2017-12-07 11:06:10 +08:00
|
|
|
* Don't make the change until all currently written data
|
2017-10-17 22:24:23 +08:00
|
|
|
* has been transmitted.
|
|
|
|
*/
|
|
|
|
return (ioctl(fd, TCSETAW, (void*)tio));
|
|
|
|
case TCSAFLUSH:
|
2017-12-07 11:06:10 +08:00
|
|
|
/* Don't make the change until all currently written data
|
|
|
|
* has been transmitted, at which point any received but
|
2017-10-17 22:24:23 +08:00
|
|
|
* unread data is also discarded.
|
|
|
|
*/
|
|
|
|
return (ioctl(fd, TCSETAF, (void*)tio));
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return (-1);
|
|
|
|
}
|
2017-10-15 22:41:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-07 11:06:10 +08:00
|
|
|
* this function gets process group ID for session leader for controlling
|
2017-10-15 22:41:59 +08:00
|
|
|
* terminal
|
|
|
|
*
|
|
|
|
* @return always 0
|
|
|
|
*/
|
|
|
|
pid_t tcgetsid(int fd)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
speed_t cfgetospeed(const struct termios *tio)
|
|
|
|
{
|
|
|
|
return tio->c_cflag & CBAUD;
|
|
|
|
}
|
|
|
|
|
|
|
|
speed_t cfgetispeed(const struct termios *tio)
|
|
|
|
{
|
|
|
|
return cfgetospeed(tio);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cfsetospeed(struct termios *tio, speed_t speed)
|
|
|
|
{
|
2017-12-07 11:06:10 +08:00
|
|
|
if (speed & ~CBAUD)
|
2017-10-15 22:41:59 +08:00
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tio->c_cflag &= ~CBAUD;
|
|
|
|
tio->c_cflag |= speed;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cfsetispeed(struct termios *tio, speed_t speed)
|
|
|
|
{
|
|
|
|
return speed ? cfsetospeed(tio, speed) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcsendbreak(int fd, int dur)
|
|
|
|
{
|
|
|
|
/* nonzero duration is implementation-defined, so ignore it */
|
2017-10-17 22:24:23 +08:00
|
|
|
return 0;
|
2017-10-15 22:41:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int tcflush(int fd, int queue)
|
|
|
|
{
|
|
|
|
return ioctl(fd, TCFLSH, (void*)queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
int tcflow(int fd, int action)
|
|
|
|
{
|
|
|
|
return ioctl(fd, TCXONC, (void*)action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function waits for transmission of output
|
|
|
|
*/
|
|
|
|
int tcdrain(int fd)
|
|
|
|
{
|
2017-10-17 22:24:23 +08:00
|
|
|
return 0;
|
2017-10-15 22:41:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int isatty (int fd)
|
|
|
|
{
|
2017-10-17 22:24:23 +08:00
|
|
|
struct termios term;
|
2017-10-15 22:41:59 +08:00
|
|
|
|
2017-10-17 22:24:23 +08:00
|
|
|
return tcgetattr (fd, &term) == 0;
|
2017-10-15 22:41:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
|
|
|
|
void cfmakeraw(struct termios *t)
|
|
|
|
{
|
|
|
|
t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
|
|
|
|
t->c_oflag &= ~OPOST;
|
|
|
|
t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
|
|
|
|
t->c_cflag &= ~(CSIZE|PARENB);
|
|
|
|
t->c_cflag |= CS8;
|
|
|
|
t->c_cc[VMIN] = 1;
|
|
|
|
t->c_cc[VTIME] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cfsetspeed(struct termios *tio, speed_t speed)
|
|
|
|
{
|
|
|
|
return cfsetospeed(tio, speed);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|