termios.h 11 KB

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