blob: a884b595fe4756c06314788210a2166873423801 [file] [log] [blame]
Elliott Hughes14499742017-07-05 12:00:29 -07001/*
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
32#include <errno.h>
33#include <sys/cdefs.h>
34#include <sys/ioctl.h>
35#include <sys/types.h>
36
37#include <linux/termios.h>
38
39#if !defined(__BIONIC_TERMIOS_INLINE)
Dan Albert2237fcf2024-05-14 17:53:58 +000040#define __BIONIC_TERMIOS_INLINE static __inline
Elliott Hughes14499742017-07-05 12:00:29 -070041#endif
42
43__BEGIN_DECLS
44
Elliott Hughes5da96462017-12-14 09:43:59 -080045// Supporting separate input and output speeds would require an ABI
46// change for `struct termios`.
47
Dan Albert2237fcf2024-05-14 17:53:58 +000048static __inline speed_t cfgetspeed(const struct termios* _Nonnull s) {
Elliott Hughes14499742017-07-05 12:00:29 -070049 return __BIONIC_CAST(static_cast, speed_t, s->c_cflag & CBAUD);
50}
51
zijunzhao9e197852023-06-06 21:57:50 +000052__BIONIC_TERMIOS_INLINE speed_t cfgetispeed(const struct termios* _Nonnull s) {
Elliott Hughes14499742017-07-05 12:00:29 -070053 return cfgetspeed(s);
54}
55
zijunzhao9e197852023-06-06 21:57:50 +000056__BIONIC_TERMIOS_INLINE speed_t cfgetospeed(const struct termios* _Nonnull s) {
Elliott Hughes14499742017-07-05 12:00:29 -070057 return cfgetspeed(s);
58}
59
zijunzhao9e197852023-06-06 21:57:50 +000060__BIONIC_TERMIOS_INLINE void cfmakeraw(struct termios* _Nonnull s) {
Elliott Hughes14499742017-07-05 12:00:29 -070061 s->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
62 s->c_oflag &= ~OPOST;
63 s->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
64 s->c_cflag &= ~(CSIZE|PARENB);
65 s->c_cflag |= CS8;
Elliott Hughes5da96462017-12-14 09:43:59 -080066 s->c_cc[VMIN] = 1;
67 s->c_cc[VTIME] = 0;
Elliott Hughes14499742017-07-05 12:00:29 -070068}
69
zijunzhao9e197852023-06-06 21:57:50 +000070__BIONIC_TERMIOS_INLINE int cfsetspeed(struct termios* _Nonnull s, speed_t speed) {
Elliott Hughes5da96462017-12-14 09:43:59 -080071 // CBAUD is 0x100f, and every matching bit pattern has a Bxxx constant.
72 if ((speed & ~CBAUD) != 0) {
73 errno = EINVAL;
74 return -1;
75 }
Elliott Hughes14499742017-07-05 12:00:29 -070076 s->c_cflag = (s->c_cflag & ~CBAUD) | (speed & CBAUD);
77 return 0;
78}
79
zijunzhao9e197852023-06-06 21:57:50 +000080__BIONIC_TERMIOS_INLINE int cfsetispeed(struct termios* _Nonnull s, speed_t speed) {
Elliott Hughes14499742017-07-05 12:00:29 -070081 return cfsetspeed(s, speed);
82}
83
zijunzhao9e197852023-06-06 21:57:50 +000084__BIONIC_TERMIOS_INLINE int cfsetospeed(struct termios* _Nonnull s, speed_t speed) {
Elliott Hughes14499742017-07-05 12:00:29 -070085 return cfsetspeed(s, speed);
86}
87
88__BIONIC_TERMIOS_INLINE int tcdrain(int fd) {
89 // A non-zero argument to TCSBRK means "don't send a break".
90 // The drain is a side-effect of the ioctl!
91 return ioctl(fd, TCSBRK, __BIONIC_CAST(static_cast, unsigned long, 1));
92}
93
94__BIONIC_TERMIOS_INLINE int tcflow(int fd, int action) {
95 return ioctl(fd, TCXONC, __BIONIC_CAST(static_cast, unsigned long, action));
96}
97
98__BIONIC_TERMIOS_INLINE int tcflush(int fd, int queue) {
99 return ioctl(fd, TCFLSH, __BIONIC_CAST(static_cast, unsigned long, queue));
100}
101
zijunzhao9e197852023-06-06 21:57:50 +0000102__BIONIC_TERMIOS_INLINE int tcgetattr(int fd, struct termios* _Nonnull s) {
Elliott Hughes14499742017-07-05 12:00:29 -0700103 return ioctl(fd, TCGETS, s);
104}
105
106__BIONIC_TERMIOS_INLINE pid_t tcgetsid(int fd) {
107 pid_t sid;
108 return (ioctl(fd, TIOCGSID, &sid) == -1) ? -1 : sid;
109}
110
111__BIONIC_TERMIOS_INLINE int tcsendbreak(int fd, int duration) {
112 return ioctl(fd, TCSBRKP, __BIONIC_CAST(static_cast, unsigned long, duration));
113}
114
zijunzhao9e197852023-06-06 21:57:50 +0000115__BIONIC_TERMIOS_INLINE int tcsetattr(int fd, int optional_actions, const struct termios* _Nonnull s) {
Elliott Hughes14499742017-07-05 12:00:29 -0700116 int cmd;
117 switch (optional_actions) {
118 case TCSANOW: cmd = TCSETS; break;
119 case TCSADRAIN: cmd = TCSETSW; break;
120 case TCSAFLUSH: cmd = TCSETSF; break;
121 default: errno = EINVAL; return -1;
122 }
123 return ioctl(fd, cmd, s);
124}
125
126__END_DECLS
127
128#endif