1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2017/08/30 Bernard The first version 9 */ 10 #include <stdlib.h> 11 #include <string.h> 12 #include <rtthread.h> 13 #include <dfs_posix.h> 14 15 #include <termios.h> 16 17 int tcgetattr(int fd, struct termios *tio) 18 { 19 /* Get the current serial port settings. */ 20 if (ioctl(fd, TCGETA, tio)) 21 return -1; 22 23 return 0; 24 } 25 26 int tcsetattr(int fd, int act, const struct termios *tio) 27 { 28 switch (act) 29 { 30 case TCSANOW: 31 /* make the change immediately */ 32 return (ioctl(fd, TCSETA, (void*)tio)); 33 case TCSADRAIN: 34 /* 35 * Don't make the change until all currently written data 36 * has been transmitted. 37 */ 38 return (ioctl(fd, TCSETAW, (void*)tio)); 39 case TCSAFLUSH: 40 /* Don't make the change until all currently written data 41 * has been transmitted, at which point any received but 42 * unread data is also discarded. 43 */ 44 return (ioctl(fd, TCSETAF, (void*)tio)); 45 default: 46 errno = EINVAL; 47 return (-1); 48 } 49 } 50 51 /** 52 * this function gets process group ID for session leader for controlling 53 * terminal 54 * 55 * @return always 0 56 */ 57 pid_t tcgetsid(int fd) 58 { 59 return 0; 60 } 61 62 speed_t cfgetospeed(const struct termios *tio) 63 { 64 return tio->c_cflag & CBAUD; 65 } 66 67 speed_t cfgetispeed(const struct termios *tio) 68 { 69 return cfgetospeed(tio); 70 } 71 72 int cfsetospeed(struct termios *tio, speed_t speed) 73 { 74 if (speed & ~CBAUD) 75 { 76 errno = EINVAL; 77 return -1; 78 } 79 80 tio->c_cflag &= ~CBAUD; 81 tio->c_cflag |= speed; 82 return 0; 83 } 84 85 int cfsetispeed(struct termios *tio, speed_t speed) 86 { 87 return speed ? cfsetospeed(tio, speed) : 0; 88 } 89 90 int tcsendbreak(int fd, int dur) 91 { 92 /* nonzero duration is implementation-defined, so ignore it */ 93 return 0; 94 } 95 96 int tcflush(int fd, int queue) 97 { 98 return ioctl(fd, TCFLSH, (void*)queue); 99 } 100 101 int tcflow(int fd, int action) 102 { 103 return ioctl(fd, TCXONC, (void*)action); 104 } 105 106 /** 107 * this function waits for transmission of output 108 */ 109 int tcdrain(int fd) 110 { 111 return 0; 112 } 113 114 int isatty (int fd) 115 { 116 struct termios term; 117 118 return tcgetattr (fd, &term) == 0; 119 } 120 121 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) 122 void cfmakeraw(struct termios *t) 123 { 124 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); 125 t->c_oflag &= ~OPOST; 126 t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); 127 t->c_cflag &= ~(CSIZE|PARENB); 128 t->c_cflag |= CS8; 129 t->c_cc[VMIN] = 1; 130 t->c_cc[VTIME] = 0; 131 } 132 133 int cfsetspeed(struct termios *tio, speed_t speed) 134 { 135 return cfsetospeed(tio, speed); 136 } 137 #endif 138 139