1 /* 2 * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 // 7 // This header file is based on the termios header of 8 // "The Single UNIX (r) Specification, Version 2, Copyright (c) 1997 The Open Group". 9 10 #ifndef __ESP_SYS_TERMIOS_H__ 11 #define __ESP_SYS_TERMIOS_H__ 12 13 // ESP-IDF NOTE: This header provides only a compatibility layer for macros and functions defined in sys/termios.h. 14 // Not everything has a defined meaning for ESP-IDF (e.g. process leader IDs) and therefore are likely to be stubbed 15 // in actual implementations. 16 17 18 #include <stdint.h> 19 #include <sys/types.h> 20 #include "sdkconfig.h" 21 22 #ifdef CONFIG_VFS_SUPPORT_TERMIOS 23 24 // subscripts for the array c_cc: 25 #define VEOF 0 /** EOF character */ 26 #define VEOL 1 /** EOL character */ 27 #define VERASE 2 /** ERASE character */ 28 #define VINTR 3 /** INTR character */ 29 #define VKILL 4 /** KILL character */ 30 #define VMIN 5 /** MIN value */ 31 #define VQUIT 6 /** QUIT character */ 32 #define VSTART 7 /** START character */ 33 #define VSTOP 8 /** STOP character */ 34 #define VSUSP 9 /** SUSP character */ 35 #define VTIME 10 /** TIME value */ 36 #define NCCS (VTIME + 1) /** Size of the array c_cc for control characters */ 37 38 // input modes for use as flags in the c_iflag field 39 #define BRKINT (1u << 0) /** Signal interrupt on break. */ 40 #define ICRNL (1u << 1) /** Map CR to NL on input. */ 41 #define IGNBRK (1u << 2) /** Ignore break condition. */ 42 #define IGNCR (1u << 3) /** Ignore CR. */ 43 #define IGNPAR (1u << 4) /** Ignore characters with parity errors. */ 44 #define INLCR (1u << 5) /** Map NL to CR on input. */ 45 #define INPCK (1u << 6) /** Enable input parity check. */ 46 #define ISTRIP (1u << 7) /** Strip character. */ 47 #define IUCLC (1u << 8) /** Map upper-case to lower-case on input (LEGACY). */ 48 #define IXANY (1u << 9) /** Enable any character to restart output. */ 49 #define IXOFF (1u << 10) /** Enable start/stop input control. */ 50 #define IXON (1u << 11) /** Enable start/stop output control. */ 51 #define PARMRK (1u << 12) /** Mark parity errors. */ 52 53 // output Modes for use as flags in the c_oflag field 54 #define OPOST (1u << 0) /** Post-process output */ 55 #define OLCUC (1u << 1) /** Map lower-case to upper-case on output (LEGACY). */ 56 #define ONLCR (1u << 2) /** Map NL to CR-NL on output. */ 57 #define OCRNL (1u << 3) /** Map CR to NL on output. */ 58 #define ONOCR (1u << 4) /** No CR output at column 0. */ 59 #define ONLRET (1u << 5) /** NL performs CR function. */ 60 #define OFILL (1u << 6) /** Use fill characters for delay. */ 61 #define NLDLY (1u << 7) /** Select newline delays: */ 62 #define NL0 (0u << 7) /** Newline character type 0. */ 63 #define NL1 (1u << 7) /** Newline character type 1. */ 64 #define CRDLY (3u << 8) /** Select carriage-return delays: */ 65 #define CR0 (0u << 8) /** Carriage-return delay type 0. */ 66 #define CR1 (1u << 8) /** Carriage-return delay type 1. */ 67 #define CR2 (2u << 8) /** Carriage-return delay type 2. */ 68 #define CR3 (3u << 8) /** Carriage-return delay type 3. */ 69 #define TABDLY (3u << 10) /** Select horizontal-tab delays: */ 70 #define TAB0 (0u << 10) /** Horizontal-tab delay type 0. */ 71 #define TAB1 (1u << 10) /** Horizontal-tab delay type 1. */ 72 #define TAB2 (2u << 10) /** Horizontal-tab delay type 2. */ 73 #define TAB3 (3u << 10) /** Expand tabs to spaces. */ 74 #define BSDLY (1u << 12) /** Select backspace delays: */ 75 #define BS0 (0u << 12) /** Backspace-delay type 0. */ 76 #define BS1 (1u << 12) /** Backspace-delay type 1. */ 77 #define VTDLY (1u << 13) /** Select vertical-tab delays: */ 78 #define VT0 (0u << 13) /** Vertical-tab delay type 0. */ 79 #define VT1 (1u << 13) /** Vertical-tab delay type 1. */ 80 #define FFDLY (1u << 14) /** Select form-feed delays: */ 81 #define FF0 (0u << 14) /** Form-feed delay type 0. */ 82 #define FF1 (1u << 14) /** Form-feed delay type 1. */ 83 84 // Baud Rate Selection. Valid values for objects of type speed_t: 85 // CBAUD range B0 - B38400 86 #define B0 0 /** Hang up */ 87 #define B50 1 88 #define B75 2 89 #define B110 3 90 #define B134 4 91 #define B150 5 92 #define B200 6 93 #define B300 7 94 #define B600 8 95 #define B1200 9 96 #define B1800 10 97 #define B2400 11 98 #define B4800 12 99 #define B9600 13 100 #define B19200 14 101 #define B38400 15 102 // CBAUDEX range B57600 - B4000000 103 #define B57600 16 104 #define B115200 17 105 #define B230400 18 106 #define B460800 19 107 #define B500000 20 108 #define B576000 21 109 #define B921600 22 110 #define B1000000 23 111 #define B1152000 24 112 #define B1500000 25 113 #define B2000000 26 114 #define B2500000 27 115 #define B3000000 28 116 #define B3500000 29 117 #define B4000000 30 118 119 // Control Modes for the c_cflag field: 120 #define CSIZE (3u << 0) /* Character size: */ 121 #define CS5 (0u << 0) /** 5 bits. */ 122 #define CS6 (1u << 0) /** 6 bits. */ 123 #define CS7 (2u << 0) /** 7 bits. */ 124 #define CS8 (3u << 0) /** 8 bits. */ 125 #define CSTOPB (1u << 2) /** Send two stop bits, else one. */ 126 #define CREAD (1u << 3) /** Enable receiver. */ 127 #define PARENB (1u << 4) /** Parity enable. */ 128 #define PARODD (1u << 5) /** Odd parity, else even. */ 129 #define HUPCL (1u << 6) /** Hang up on last close. */ 130 #define CLOCAL (1u << 7) /** Ignore modem status lines. */ 131 #define CBAUD (1u << 8) /** Use baud rates defined by B0-B38400 macros. */ 132 #define CBAUDEX (1u << 9) /** Use baud rates defined by B57600-B4000000 macros. */ 133 #define BOTHER (1u << 10) /** Use custom baud rates */ 134 135 // Local Modes for c_lflag field: 136 #define ECHO (1u << 0) /** Enable echo. */ 137 #define ECHOE (1u << 1) /** Echo erase character as error-correcting backspace. */ 138 #define ECHOK (1u << 2) /** Echo KILL. */ 139 #define ECHONL (1u << 3) /** Echo NL. */ 140 #define ICANON (1u << 4) /** Canonical input (erase and kill processing). */ 141 #define IEXTEN (1u << 5) /** Enable extended input character processing. */ 142 #define ISIG (1u << 6) /** Enable signals. */ 143 #define NOFLSH (1u << 7) /** Disable flush after interrupt or quit. */ 144 #define TOSTOP (1u << 8) /** Send SIGTTOU for background output. */ 145 #define XCASE (1u << 9) /** Canonical upper/lower presentation (LEGACY). */ 146 147 // Attribute Selection constants for use with tcsetattr(): 148 #define TCSANOW 0 /** Change attributes immediately. */ 149 #define TCSADRAIN 1 /** Change attributes when output has drained. */ 150 #define TCSAFLUSH 2 /** Change attributes when output has drained; also flush pending input. */ 151 152 // Line Control constants for use with tcflush(): 153 #define TCIFLUSH 0 /** Flush pending input. Flush untransmitted output. */ 154 #define TCIOFLUSH 1 /** Flush both pending input and untransmitted output. */ 155 #define TCOFLUSH 2 /** Flush untransmitted output. */ 156 157 // constants for use with tcflow(): 158 #define TCIOFF 0 /** Transmit a STOP character, intended to suspend input data. */ 159 #define TCION 1 /** Transmit a START character, intended to restart input data. */ 160 #define TCOOFF 2 /** Suspend output. */ 161 #define TCOON 3 /** Restart output. */ 162 163 typedef uint8_t cc_t; 164 typedef uint32_t speed_t; 165 typedef uint16_t tcflag_t; 166 167 struct termios 168 { 169 tcflag_t c_iflag; /** Input modes */ 170 tcflag_t c_oflag; /** Output modes */ 171 tcflag_t c_cflag; /** Control modes */ 172 tcflag_t c_lflag; /** Local modes */ 173 cc_t c_cc[NCCS]; /** Control characters */ 174 speed_t c_ispeed; /** input baud rate */ 175 speed_t c_ospeed; /** output baud rate */ 176 }; 177 178 #ifdef __cplusplus 179 extern "C" { 180 #endif 181 182 /** 183 * @brief Extracts the input baud rate from the input structure exactly (without interpretation). 184 * 185 * @param p input termios structure 186 * @return input baud rate 187 */ 188 speed_t cfgetispeed(const struct termios *p); 189 190 /** 191 * @brief Extracts the output baud rate from the input structure exactly (without interpretation). 192 * 193 * @param p input termios structure 194 * @return output baud rate 195 */ 196 speed_t cfgetospeed(const struct termios *p); 197 198 /** 199 * @brief Set input baud rate in the termios structure 200 * 201 * There is no effect in hardware until a subsequent call of tcsetattr(). 202 * 203 * @param p input termios structure 204 * @param sp input baud rate 205 * @return 0 when successful, -1 otherwise with errno set 206 */ 207 int cfsetispeed(struct termios *p, speed_t sp); 208 209 /** 210 * @brief Set output baud rate in the termios structure 211 * 212 * There is no effect in hardware until a subsequent call of tcsetattr(). 213 * 214 * @param p input termios structure 215 * @param sp output baud rate 216 * @return 0 when successful, -1 otherwise with errno set 217 */ 218 int cfsetospeed(struct termios *p, speed_t sp); 219 220 /** 221 * @brief Wait for transmission of output 222 * 223 * @param fd file descriptor of the terminal 224 * @return 0 when successful, -1 otherwise with errno set 225 */ 226 int tcdrain(int fd); 227 228 /** 229 * @brief Suspend or restart the transmission or reception of data 230 * 231 * @param fd file descriptor of the terminal 232 * @param action selects actions to do 233 * @return 0 when successful, -1 otherwise with errno set 234 */ 235 int tcflow(int fd, int action); 236 237 /** 238 * @brief Flush non-transmitted output data and non-read input data 239 * 240 * @param fd file descriptor of the terminal 241 * @param select selects what should be flushed 242 * @return 0 when successful, -1 otherwise with errno set 243 */ 244 int tcflush(int fd, int select); 245 246 /** 247 * @brief Gets the parameters of the terminal 248 * 249 * @param fd file descriptor of the terminal 250 * @param p output termios structure 251 * @return 0 when successful, -1 otherwise with errno set 252 */ 253 int tcgetattr(int fd, struct termios *p); 254 255 /** 256 * @brief Get process group ID for session leader for controlling terminal 257 * 258 * @param fd file descriptor of the terminal 259 * @return process group ID when successful, -1 otherwise with errno set 260 */ 261 pid_t tcgetsid(int fd); 262 263 /** 264 * @brief Send a break for a specific duration 265 * 266 * @param fd file descriptor of the terminal 267 * @param duration duration of break 268 * @return 0 when successful, -1 otherwise with errno set 269 */ 270 int tcsendbreak(int fd, int duration); 271 272 /** 273 * @brief Sets the parameters of the terminal 274 * 275 * @param fd file descriptor of the terminal 276 * @param optional_actions optional actions 277 * @param p input termios structure 278 * @return 0 when successful, -1 otherwise with errno set 279 */ 280 int tcsetattr(int fd, int optional_actions, const struct termios *p); 281 282 #ifdef __cplusplus 283 } // extern "C" 284 #endif 285 286 #endif // CONFIG_VFS_SUPPORT_TERMIOS 287 288 #endif //__ESP_SYS_TERMIOS_H__ 289