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