1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <termios.h> 30 #include <unistd.h> 31 32 static speed_t cfgetspeed(const termios* s) { 33 return (s->c_cflag & CBAUD); 34 } 35 36 speed_t cfgetispeed(const termios* s) { 37 return cfgetspeed(s); 38 } 39 40 speed_t cfgetospeed(const termios* s) { 41 return cfgetspeed(s); 42 } 43 44 void cfmakeraw(termios* s) { 45 s->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); 46 s->c_oflag &= ~OPOST; 47 s->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); 48 s->c_cflag &= ~(CSIZE|PARENB); 49 s->c_cflag |= CS8; 50 } 51 52 int cfsetispeed(termios* s, speed_t speed) { 53 return cfsetspeed(s, speed); 54 } 55 56 int cfsetospeed(termios* s, speed_t speed) { 57 return cfsetspeed(s, speed); 58 } 59 60 int cfsetspeed(termios* s, speed_t speed) { 61 // TODO: check 'speed' is valid. 62 s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD); 63 return 0; 64 } 65 66 int tcdrain(int fd) { 67 // A non-zero argument to TCSBRK means "don't send a break". 68 // The drain is a side-effect of the ioctl! 69 return ioctl(fd, TCSBRK, static_cast<unsigned long>(1)); 70 } 71 72 int tcflow(int fd, int action) { 73 return ioctl(fd, TCXONC, static_cast<unsigned long>(action)); 74 } 75 76 int tcflush(int fd, int queue) { 77 return ioctl(fd, TCFLSH, static_cast<unsigned long>(queue)); 78 } 79 80 int tcgetattr(int fd, termios* s) { 81 return ioctl(fd, TCGETS, s); 82 } 83 84 pid_t tcgetsid(int fd) { 85 pid_t sid; 86 if (ioctl(fd, TIOCGSID, &sid) == -1) { 87 return -1; 88 } 89 return sid; 90 } 91 92 int tcsendbreak(int fd, int duration) { 93 return ioctl(fd, TCSBRKP, static_cast<unsigned long>(duration)); 94 } 95 96 int tcsetattr(int fd, int optional_actions, const termios* s) { 97 int cmd; 98 switch (optional_actions) { 99 case TCSANOW: cmd = TCSETS; break; 100 case TCSADRAIN: cmd = TCSETSW; break; 101 case TCSAFLUSH: cmd = TCSETSF; break; 102 default: errno = EINVAL; return -1; 103 } 104 return ioctl(fd, cmd, s); 105 } 106 107 pid_t tcgetpgrp(int fd) { 108 pid_t pid; 109 if (ioctl(fd, TIOCGPGRP, &pid) == -1) { 110 return -1; 111 } 112 return pid; 113 } 114 115 int tcsetpgrp(int fd, pid_t pid) { 116 return ioctl(fd, TIOCSPGRP, &pid); 117 } 118