1 /* 2 * Copyright 2004-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 * Copyright 2003, Daniel Reinhold, danielre@users.sf.net. All rights reserved. 4 * 5 * Distributed under the terms of the MIT License. 6 */ 7 8 9 #include <termios.h> 10 #include <unistd.h> 11 #include <errno.h> 12 13 14 /** get the attributes of the TTY device at fd */ 15 16 int 17 tcgetattr(int fd, struct termios *termios) 18 { 19 return ioctl(fd, TCGETA, termios); 20 } 21 22 23 /** set the attributes for the TTY device at fd */ 24 25 int 26 tcsetattr(int fd, int opt, const struct termios *termios) 27 { 28 int method; 29 30 switch (opt) { 31 case TCSANOW: 32 // set the attributes immediately 33 method = TCSETA; 34 break; 35 case TCSADRAIN: 36 // wait for ouput to finish before setting the attributes 37 method = TCSETAW; 38 break; 39 case TCSAFLUSH: 40 method = TCSETAF; 41 break; 42 43 default: 44 // no other valid options 45 errno = EINVAL; 46 return -1; 47 } 48 49 return ioctl(fd, method, termios); 50 } 51 52 53 /** wait for all output to be transmitted */ 54 55 int 56 tcdrain(int fd) 57 { 58 /* Some termios implementations have a TIOCDRAIN command 59 * expressly for this purpose (e.g. ioctl(fd, TIOCDRAIN, 0). 60 * However, the BeOS implementation adheres to another 61 * interface which uses a non-zero last parameter to the 62 * TCSBRK ioctl to signify this functionality. 63 */ 64 return ioctl(fd, TCSBRK, 1); 65 } 66 67 68 /** suspend or restart transmission */ 69 70 int 71 tcflow(int fd, int action) 72 { 73 switch (action) { 74 case TCIOFF: 75 case TCION: 76 case TCOOFF: 77 case TCOON: 78 break; 79 80 default: 81 errno = EINVAL; 82 return -1; 83 } 84 85 return ioctl(fd, TCXONC, action); 86 } 87 88 89 /** flush all pending data (input or output) */ 90 91 int 92 tcflush(int fd, int queueSelector) 93 { 94 return ioctl(fd, TCFLSH, queueSelector); 95 } 96 97 98 /** send zero bits for the specified duration */ 99 100 int 101 tcsendbreak(int fd, int duration) 102 { 103 // Posix spec says this should take ~ 0.25 to 0.5 seconds. 104 // As the interpretation of the duration is undefined, we'll just ignore it 105 return ioctl(fd, TCSBRK, 0); 106 } 107 108 109 speed_t 110 cfgetispeed(const struct termios *termios) 111 { 112 return termios->c_ispeed; 113 } 114 115 116 int 117 cfsetispeed(struct termios *termios, speed_t speed) 118 { 119 termios->c_ispeed = speed; 120 return 0; 121 } 122 123 124 speed_t 125 cfgetospeed(const struct termios *termios) 126 { 127 return termios->c_ospeed; 128 } 129 130 131 int 132 cfsetospeed(struct termios *termios, speed_t speed) 133 { 134 termios->c_ospeed = speed; 135 return 0; 136 } 137 138