termios.h 12 KB

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