termios.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/errno.h>
  14. #include "termios.h"
  15. int tcgetattr(int fd, struct termios *tio)
  16. {
  17. /* Get the current serial port settings. */
  18. if (ioctl(fd, TCGETA, tio))
  19. return -1;
  20. return 0;
  21. }
  22. int tcsetattr(int fd, int act, const struct termios *tio)
  23. {
  24. switch (act)
  25. {
  26. case TCSANOW:
  27. /* make the change immediately */
  28. return (ioctl(fd, TCSETA, (void*)tio));
  29. case TCSADRAIN:
  30. /*
  31. * Don't make the change until all currently written data
  32. * has been transmitted.
  33. */
  34. return (ioctl(fd, TCSETAW, (void*)tio));
  35. case TCSAFLUSH:
  36. /* Don't make the change until all currently written data
  37. * has been transmitted, at which point any received but
  38. * unread data is also discarded.
  39. */
  40. return (ioctl(fd, TCSETAF, (void*)tio));
  41. default:
  42. errno = EINVAL;
  43. return (-1);
  44. }
  45. }
  46. /**
  47. * this function gets process group ID for session leader for controlling
  48. * terminal
  49. *
  50. * @return always 0
  51. */
  52. pid_t tcgetsid(int fd)
  53. {
  54. return 0;
  55. }
  56. speed_t cfgetospeed(const struct termios *tio)
  57. {
  58. return tio->c_cflag & CBAUD;
  59. }
  60. speed_t cfgetispeed(const struct termios *tio)
  61. {
  62. return cfgetospeed(tio);
  63. }
  64. int cfsetospeed(struct termios *tio, speed_t speed)
  65. {
  66. if (speed & ~CBAUD)
  67. {
  68. errno = EINVAL;
  69. return -1;
  70. }
  71. tio->c_cflag &= ~CBAUD;
  72. tio->c_cflag |= speed;
  73. return 0;
  74. }
  75. int cfsetispeed(struct termios *tio, speed_t speed)
  76. {
  77. return speed ? cfsetospeed(tio, speed) : 0;
  78. }
  79. int tcsendbreak(int fd, int dur)
  80. {
  81. /* nonzero duration is implementation-defined, so ignore it */
  82. return 0;
  83. }
  84. int tcflush(int fd, int queue)
  85. {
  86. return ioctl(fd, TCFLSH, (void*)queue);
  87. }
  88. int tcflow(int fd, int action)
  89. {
  90. return ioctl(fd, TCXONC, (void*)action);
  91. }
  92. /**
  93. * this function waits for transmission of output
  94. */
  95. int tcdrain(int fd)
  96. {
  97. return 0;
  98. }
  99. void cfmakeraw(struct termios *t)
  100. {
  101. t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  102. t->c_oflag &= ~OPOST;
  103. t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  104. t->c_cflag &= ~(CSIZE|PARENB);
  105. t->c_cflag |= CS8;
  106. t->c_cc[VMIN] = 1;
  107. t->c_cc[VTIME] = 0;
  108. }
  109. int cfsetspeed(struct termios *tio, speed_t speed)
  110. {
  111. return cfsetospeed(tio, speed);
  112. }