termios.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-08-30 Bernard The first version
  9. * 2024-04-26 TroyMitchell Add comments for all functions
  10. */
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/errno.h>
  15. #include "termios.h"
  16. #include <rtthread.h>
  17. /**
  18. * @brief Gets the current attributes of a terminal device.
  19. * @param fd File descriptor of the terminal device.
  20. * @param tio Pointer to a struct termios where the attributes will be stored.
  21. * @return Upon successful completion, returns 0; otherwise, returns -1.
  22. *
  23. * @note This function retrieves the current attributes of a terminal device specified by the file descriptor fd.
  24. * It uses the ioctl system call with the TCGETA command to obtain the attributes and stores them in the
  25. * struct termios pointed to by tio.
  26. * If the ioctl operation fails, the function returns -1 to indicate an error.
  27. */
  28. int tcgetattr(int fd, struct termios *tio)
  29. {
  30. /* Get the current serial port settings. */
  31. if (ioctl(fd, TCGETA, tio))
  32. return -1;
  33. return 0;
  34. }
  35. /**
  36. * @brief Sets the attributes of a terminal device.
  37. * @param fd File descriptor of the terminal device.
  38. * @param act Action to be taken for the attribute change (TCSANOW, TCSADRAIN, or TCSAFLUSH).
  39. * @param tio Pointer to a struct termios containing the new attributes.
  40. * @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
  41. *
  42. * @note This function sets the attributes of a terminal device specified by the file descriptor fd.
  43. * The act parameter determines when the attribute change takes effect:
  44. * - TCSANOW: Make the change immediately.
  45. * - TCSADRAIN: Make the change after all currently written data has been transmitted.
  46. * - TCSAFLUSH: Make the change after all currently written data has been transmitted, and discard
  47. * any received but unread data.
  48. * The new attributes are specified in the struct termios pointed to by tio.
  49. * The ioctl system call is used to set the attributes based on the value of act.
  50. * If the ioctl operation fails or an invalid action is specified, errno is set to indicate the error,
  51. * and the function returns -1.
  52. */
  53. int tcsetattr(int fd, int act, const struct termios *tio)
  54. {
  55. switch (act)
  56. {
  57. case TCSANOW:
  58. /* make the change immediately */
  59. return (ioctl(fd, TCSETA, (void*)tio));
  60. case TCSADRAIN:
  61. /*
  62. * Don't make the change until all currently written data
  63. * has been transmitted.
  64. */
  65. return (ioctl(fd, TCSETAW, (void*)tio));
  66. case TCSAFLUSH:
  67. /* Don't make the change until all currently written data
  68. * has been transmitted, at which point any received but
  69. * unread data is also discarded.
  70. */
  71. return (ioctl(fd, TCSETAF, (void*)tio));
  72. default:
  73. errno = EINVAL;
  74. return (-1);
  75. }
  76. }
  77. /**
  78. * @brief Gets the session ID of a terminal.
  79. * @param fd File descriptor of the terminal device.
  80. * @return Always returns 0.
  81. *
  82. * @note This function is a stub and always returns 0.
  83. * In POSIX systems, tcgetsid() is used to get the session ID of the terminal associated with the file descriptor fd.
  84. * However, this function does not provide this functionality and simply returns 0 as a placeholder.
  85. */
  86. pid_t tcgetsid(int fd)
  87. {
  88. return 0;
  89. }
  90. /**
  91. * @brief Gets the output baud rate from a termios structure.
  92. * @param tio Pointer to a struct termios containing the terminal attributes.
  93. * @return Output baud rate extracted from the terminal attributes.
  94. *
  95. * @note This function extracts the output baud rate from the termios structure pointed to by tio.
  96. * It retrieves the baud rate from the c_cflag member of the termios structure using the CBAUD mask.
  97. * The output baud rate is returned as a speed_t type.
  98. */
  99. speed_t cfgetospeed(const struct termios *tio)
  100. {
  101. return tio->c_cflag & CBAUD;
  102. }
  103. /**
  104. * @brief Gets the input baud rate from a termios structure.
  105. * @param tio Pointer to a struct termios containing the terminal attributes.
  106. * @return Input baud rate extracted from the terminal attributes.
  107. *
  108. * @note This function is a wrapper for the cfgetospeed() function.
  109. * It returns the input baud rate by calling cfgetospeed() with the termios structure pointer tio.
  110. */
  111. speed_t cfgetispeed(const struct termios *tio)
  112. {
  113. return cfgetospeed(tio);
  114. }
  115. /**
  116. * @brief Sets the output baud rate in a termios structure.
  117. * @param tio Pointer to a struct termios where the output baud rate will be set.
  118. * @param speed Output baud rate to be set.
  119. * @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
  120. *
  121. * @note This function sets the output baud rate in the termios structure pointed to by tio.
  122. * The speed parameter specifies the baud rate to be set.
  123. * If the specified speed exceeds the CBAUD mask, indicating an invalid baud rate value,
  124. * errno is set to EINVAL, and the function returns -1.
  125. * Otherwise, the function clears the CBAUD bits in the c_cflag member of the termios structure
  126. * and sets them to the specified speed value. It then returns 0 to indicate success.
  127. */
  128. int cfsetospeed(struct termios *tio, speed_t speed)
  129. {
  130. if (speed & ~CBAUD)
  131. {
  132. errno = EINVAL;
  133. return -1;
  134. }
  135. tio->c_cflag &= ~CBAUD;
  136. tio->c_cflag |= speed;
  137. return 0;
  138. }
  139. /**
  140. * @brief Sets the input baud rate in a termios structure.
  141. * @param tio Pointer to a struct termios where the input baud rate will be set.
  142. * @param speed Input baud rate to be set.
  143. * @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
  144. *
  145. * @note This function sets the input baud rate in the termios structure pointed to by tio.
  146. * The speed parameter specifies the input baud rate to be set.
  147. * If the speed parameter is non-zero, indicating a valid baud rate, the function calls
  148. * cfsetospeed() to set both the input and output baud rates to the specified value.
  149. * If speed is zero, indicating that no baud rate needs to be set for input, the function returns 0
  150. * without making any changes to the termios structure.
  151. */
  152. int cfsetispeed(struct termios *tio, speed_t speed)
  153. {
  154. return speed ? cfsetospeed(tio, speed) : 0;
  155. }
  156. /**
  157. * @brief Sends a break signal on a terminal.
  158. * @param fd File descriptor of the terminal device.
  159. * @param dur Duration of the break signal (ignored).
  160. * @return Always returns 0.
  161. *
  162. * @note This function is a stub and does not send an actual break signal.
  163. * Sending a break signal with a nonzero duration is implementation-defined,
  164. * so this function ignores the duration parameter and always returns 0.
  165. */
  166. int tcsendbreak(int fd, int dur)
  167. {
  168. /* nonzero duration is implementation-defined, so ignore it */
  169. return 0;
  170. }
  171. /**
  172. * @brief Flushes data from a terminal input or output queue.
  173. * @param fd File descriptor of the terminal device.
  174. * @param queue Queue to be flushed (TCIFLUSH, TCOFLUSH, or TCIOFLUSH).
  175. * @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
  176. *
  177. * @note This function flushes data from the input or output queue of the terminal device specified by the file descriptor fd.
  178. * The queue parameter specifies which queue to flush:
  179. * - TCIFLUSH: Flushes data from the input queue.
  180. * - TCOFLUSH: Flushes data from the output queue.
  181. * - TCIOFLUSH: Flushes data from both the input and output queues.
  182. * The ioctl system call is used with the TCFLSH command to perform the flush operation.
  183. * If the ioctl operation fails, errno is set to indicate the error, and the function returns -1.
  184. */
  185. int tcflush(int fd, int queue)
  186. {
  187. return ioctl(fd, TCFLSH, (void*)(rt_ubase_t)queue);
  188. }
  189. /**
  190. * @brief Controls the terminal flow control.
  191. * @param fd File descriptor of the terminal device.
  192. * @param action Action to be taken for flow control (TCOOFF, TCOON, TCIOFF, or TCION).
  193. * @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
  194. *
  195. * @note This function controls the flow of data on the terminal device specified by the file descriptor fd.
  196. * The action parameter determines the flow control action to be taken:
  197. * - TCOOFF: Suspends output transmission.
  198. * - TCOON: Restarts output transmission.
  199. * - TCIOFF: Suspends input transmission.
  200. * - TCION: Restarts input transmission.
  201. * The ioctl system call is used with the TCXONC command to perform the flow control operation.
  202. * If the ioctl operation fails, errno is set to indicate the error, and the function returns -1.
  203. */
  204. int tcflow(int fd, int action)
  205. {
  206. return ioctl(fd, TCXONC, (void*)(rt_ubase_t)action);
  207. }
  208. /**
  209. * @brief Waits until all output written to the terminal is transmitted.
  210. * @param fd File descriptor of the terminal device.
  211. * @return Always returns 0.
  212. *
  213. * @note This function is a stub and does not perform any action.
  214. * In POSIX systems, tcdrain() is used to wait until all output written to the terminal
  215. * is transmitted, but this function does not provide this functionality and simply returns 0 as a placeholder.
  216. */
  217. int tcdrain(int fd)
  218. {
  219. return 0;
  220. }
  221. /**
  222. * @brief Sets the terminal attributes to raw mode.
  223. * @param t Pointer to a struct termios where the terminal attributes will be set.
  224. *
  225. * @note This function sets the terminal attributes pointed to by t to raw mode.
  226. * Raw mode disables special input and output processing features, making the terminal behave more like a data stream.
  227. * The following modifications are made to the termios structure:
  228. * - Input flags (c_iflag) are cleared of various processing flags.
  229. * - Output flags (c_oflag) are cleared to disable output processing.
  230. * - Local flags (c_lflag) are cleared to disable canonical mode, echoing, and signal handling.
  231. * - Control flags (c_cflag) are modified to set character size to 8 bits and disable parity.
  232. * - The VMIN and VTIME control characters are set to 1 and 0, respectively, for non-blocking read behavior.
  233. */
  234. void cfmakeraw(struct termios *t)
  235. {
  236. t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  237. t->c_oflag &= ~OPOST;
  238. t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  239. t->c_cflag &= ~(CSIZE|PARENB);
  240. t->c_cflag |= CS8;
  241. t->c_cc[VMIN] = 1;
  242. t->c_cc[VTIME] = 0;
  243. }
  244. /**
  245. * @brief Sets the input and output baud rates in a termios structure.
  246. * @param tio Pointer to a struct termios where the input and output baud rates will be set.
  247. * @param speed Baud rate to be set for both input and output.
  248. * @return Upon successful completion, returns 0; otherwise, returns -1 and sets errno to indicate the error.
  249. *
  250. * @note This function is a wrapper for the cfsetospeed() function.
  251. * It sets both the input and output baud rates in the termios structure pointed to by tio to the specified speed.
  252. * The function returns the result of cfsetospeed(), which sets the baud rate for both input and output.
  253. */
  254. int cfsetspeed(struct termios *tio, speed_t speed)
  255. {
  256. return cfsetospeed(tio, speed);
  257. }