posix_termios.c 2.6 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 <dfs_posix.h>
  13. #include <termios.h>
  14. int tcgetattr(int fd, struct termios *tio)
  15. {
  16. /* Get the current serial port settings. */
  17. if (ioctl(fd, TCGETA, tio))
  18. return -1;
  19. return 0;
  20. }
  21. int tcsetattr(int fd, int act, const struct termios *tio)
  22. {
  23. switch (act)
  24. {
  25. case TCSANOW:
  26. /* make the change immediately */
  27. return (ioctl(fd, TCSETA, (void*)tio));
  28. case TCSADRAIN:
  29. /*
  30. * Don't make the change until all currently written data
  31. * has been transmitted.
  32. */
  33. return (ioctl(fd, TCSETAW, (void*)tio));
  34. case TCSAFLUSH:
  35. /* Don't make the change until all currently written data
  36. * has been transmitted, at which point any received but
  37. * unread data is also discarded.
  38. */
  39. return (ioctl(fd, TCSETAF, (void*)tio));
  40. default:
  41. errno = EINVAL;
  42. return (-1);
  43. }
  44. }
  45. /**
  46. * this function gets process group ID for session leader for controlling
  47. * terminal
  48. *
  49. * @return always 0
  50. */
  51. pid_t tcgetsid(int fd)
  52. {
  53. return 0;
  54. }
  55. speed_t cfgetospeed(const struct termios *tio)
  56. {
  57. return tio->c_cflag & CBAUD;
  58. }
  59. speed_t cfgetispeed(const struct termios *tio)
  60. {
  61. return cfgetospeed(tio);
  62. }
  63. int cfsetospeed(struct termios *tio, speed_t speed)
  64. {
  65. if (speed & ~CBAUD)
  66. {
  67. errno = EINVAL;
  68. return -1;
  69. }
  70. tio->c_cflag &= ~CBAUD;
  71. tio->c_cflag |= speed;
  72. return 0;
  73. }
  74. int cfsetispeed(struct termios *tio, speed_t speed)
  75. {
  76. return speed ? cfsetospeed(tio, speed) : 0;
  77. }
  78. int tcsendbreak(int fd, int dur)
  79. {
  80. /* nonzero duration is implementation-defined, so ignore it */
  81. return 0;
  82. }
  83. int tcflush(int fd, int queue)
  84. {
  85. return ioctl(fd, TCFLSH, (void*)queue);
  86. }
  87. int tcflow(int fd, int action)
  88. {
  89. return ioctl(fd, TCXONC, (void*)action);
  90. }
  91. /**
  92. * this function waits for transmission of output
  93. */
  94. int tcdrain(int fd)
  95. {
  96. return 0;
  97. }
  98. #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  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. }
  113. #endif