Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #ifndef _BITS_TERMIOS_INLINES_H_ |
| 30 | #define _BITS_TERMIOS_INLINES_H_ |
| 31 | |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 32 | #include <sys/cdefs.h> |
Elliott Hughes | 414dd2d | 2024-10-16 14:48:30 +0000 | [diff] [blame] | 33 | |
| 34 | #include <errno.h> |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 35 | #include <sys/ioctl.h> |
| 36 | #include <sys/types.h> |
| 37 | |
| 38 | #include <linux/termios.h> |
| 39 | |
| 40 | #if !defined(__BIONIC_TERMIOS_INLINE) |
Dan Albert | 2237fcf | 2024-05-14 17:53:58 +0000 | [diff] [blame] | 41 | #define __BIONIC_TERMIOS_INLINE static __inline |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | __BEGIN_DECLS |
| 45 | |
Elliott Hughes | 5da9646 | 2017-12-14 09:43:59 -0800 | [diff] [blame] | 46 | // Supporting separate input and output speeds would require an ABI |
| 47 | // change for `struct termios`. |
| 48 | |
Dan Albert | 2237fcf | 2024-05-14 17:53:58 +0000 | [diff] [blame] | 49 | static __inline speed_t cfgetspeed(const struct termios* _Nonnull s) { |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 50 | return __BIONIC_CAST(static_cast, speed_t, s->c_cflag & CBAUD); |
| 51 | } |
| 52 | |
zijunzhao | 9e19785 | 2023-06-06 21:57:50 +0000 | [diff] [blame] | 53 | __BIONIC_TERMIOS_INLINE speed_t cfgetispeed(const struct termios* _Nonnull s) { |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 54 | return cfgetspeed(s); |
| 55 | } |
| 56 | |
zijunzhao | 9e19785 | 2023-06-06 21:57:50 +0000 | [diff] [blame] | 57 | __BIONIC_TERMIOS_INLINE speed_t cfgetospeed(const struct termios* _Nonnull s) { |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 58 | return cfgetspeed(s); |
| 59 | } |
| 60 | |
zijunzhao | 9e19785 | 2023-06-06 21:57:50 +0000 | [diff] [blame] | 61 | __BIONIC_TERMIOS_INLINE void cfmakeraw(struct termios* _Nonnull s) { |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 62 | s->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
| 63 | s->c_oflag &= ~OPOST; |
| 64 | s->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
| 65 | s->c_cflag &= ~(CSIZE|PARENB); |
| 66 | s->c_cflag |= CS8; |
Elliott Hughes | 5da9646 | 2017-12-14 09:43:59 -0800 | [diff] [blame] | 67 | s->c_cc[VMIN] = 1; |
| 68 | s->c_cc[VTIME] = 0; |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 69 | } |
| 70 | |
zijunzhao | 9e19785 | 2023-06-06 21:57:50 +0000 | [diff] [blame] | 71 | __BIONIC_TERMIOS_INLINE int cfsetspeed(struct termios* _Nonnull s, speed_t speed) { |
Elliott Hughes | 5da9646 | 2017-12-14 09:43:59 -0800 | [diff] [blame] | 72 | // CBAUD is 0x100f, and every matching bit pattern has a Bxxx constant. |
| 73 | if ((speed & ~CBAUD) != 0) { |
| 74 | errno = EINVAL; |
| 75 | return -1; |
| 76 | } |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 77 | s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD); |
| 78 | return 0; |
| 79 | } |
| 80 | |
zijunzhao | 9e19785 | 2023-06-06 21:57:50 +0000 | [diff] [blame] | 81 | __BIONIC_TERMIOS_INLINE int cfsetispeed(struct termios* _Nonnull s, speed_t speed) { |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 82 | return cfsetspeed(s, speed); |
| 83 | } |
| 84 | |
zijunzhao | 9e19785 | 2023-06-06 21:57:50 +0000 | [diff] [blame] | 85 | __BIONIC_TERMIOS_INLINE int cfsetospeed(struct termios* _Nonnull s, speed_t speed) { |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 86 | return cfsetspeed(s, speed); |
| 87 | } |
| 88 | |
| 89 | __BIONIC_TERMIOS_INLINE int tcdrain(int fd) { |
| 90 | // A non-zero argument to TCSBRK means "don't send a break". |
| 91 | // The drain is a side-effect of the ioctl! |
| 92 | return ioctl(fd, TCSBRK, __BIONIC_CAST(static_cast, unsigned long, 1)); |
| 93 | } |
| 94 | |
| 95 | __BIONIC_TERMIOS_INLINE int tcflow(int fd, int action) { |
| 96 | return ioctl(fd, TCXONC, __BIONIC_CAST(static_cast, unsigned long, action)); |
| 97 | } |
| 98 | |
| 99 | __BIONIC_TERMIOS_INLINE int tcflush(int fd, int queue) { |
| 100 | return ioctl(fd, TCFLSH, __BIONIC_CAST(static_cast, unsigned long, queue)); |
| 101 | } |
| 102 | |
zijunzhao | 9e19785 | 2023-06-06 21:57:50 +0000 | [diff] [blame] | 103 | __BIONIC_TERMIOS_INLINE int tcgetattr(int fd, struct termios* _Nonnull s) { |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 104 | return ioctl(fd, TCGETS, s); |
| 105 | } |
| 106 | |
| 107 | __BIONIC_TERMIOS_INLINE pid_t tcgetsid(int fd) { |
| 108 | pid_t sid; |
| 109 | return (ioctl(fd, TIOCGSID, &sid) == -1) ? -1 : sid; |
| 110 | } |
| 111 | |
| 112 | __BIONIC_TERMIOS_INLINE int tcsendbreak(int fd, int duration) { |
| 113 | return ioctl(fd, TCSBRKP, __BIONIC_CAST(static_cast, unsigned long, duration)); |
| 114 | } |
| 115 | |
zijunzhao | 9e19785 | 2023-06-06 21:57:50 +0000 | [diff] [blame] | 116 | __BIONIC_TERMIOS_INLINE int tcsetattr(int fd, int optional_actions, const struct termios* _Nonnull s) { |
Elliott Hughes | 1449974 | 2017-07-05 12:00:29 -0700 | [diff] [blame] | 117 | int cmd; |
| 118 | switch (optional_actions) { |
| 119 | case TCSANOW: cmd = TCSETS; break; |
| 120 | case TCSADRAIN: cmd = TCSETSW; break; |
| 121 | case TCSAFLUSH: cmd = TCSETSF; break; |
| 122 | default: errno = EINVAL; return -1; |
| 123 | } |
| 124 | return ioctl(fd, cmd, s); |
| 125 | } |
| 126 | |
| 127 | __END_DECLS |
| 128 | |
| 129 | #endif |