1
0

posix_termios.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * File : termios.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017/08/30 Bernard The first version
  23. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <rtthread.h>
  27. #include <dfs_posix.h>
  28. #include <termios.h>
  29. int tcgetattr(int fd, struct termios *tio)
  30. {
  31. /* Get the current serial port settings. */
  32. if (ioctl(fd, TCGETA, tio))
  33. return -1;
  34. return 0;
  35. }
  36. int tcsetattr(int fd, int act, const struct termios *tio)
  37. {
  38. switch (act)
  39. {
  40. case TCSANOW:
  41. /* make the change immediately */
  42. return (ioctl(fd, TCSETA, (void*)tio));
  43. case TCSADRAIN:
  44. /*
  45. * Don't make the change until all currently written data
  46. * has been transmitted.
  47. */
  48. return (ioctl(fd, TCSETAW, (void*)tio));
  49. case TCSAFLUSH:
  50. /* Don't make the change until all currently written data
  51. * has been transmitted, at which point any received but
  52. * unread data is also discarded.
  53. */
  54. return (ioctl(fd, TCSETAF, (void*)tio));
  55. default:
  56. errno = EINVAL;
  57. return (-1);
  58. }
  59. }
  60. /**
  61. * this function gets process group ID for session leader for controlling
  62. * terminal
  63. *
  64. * @return always 0
  65. */
  66. pid_t tcgetsid(int fd)
  67. {
  68. return 0;
  69. }
  70. speed_t cfgetospeed(const struct termios *tio)
  71. {
  72. return tio->c_cflag & CBAUD;
  73. }
  74. speed_t cfgetispeed(const struct termios *tio)
  75. {
  76. return cfgetospeed(tio);
  77. }
  78. int cfsetospeed(struct termios *tio, speed_t speed)
  79. {
  80. if (speed & ~CBAUD)
  81. {
  82. errno = EINVAL;
  83. return -1;
  84. }
  85. tio->c_cflag &= ~CBAUD;
  86. tio->c_cflag |= speed;
  87. return 0;
  88. }
  89. int cfsetispeed(struct termios *tio, speed_t speed)
  90. {
  91. return speed ? cfsetospeed(tio, speed) : 0;
  92. }
  93. int tcsendbreak(int fd, int dur)
  94. {
  95. /* nonzero duration is implementation-defined, so ignore it */
  96. return 0;
  97. }
  98. int tcflush(int fd, int queue)
  99. {
  100. return ioctl(fd, TCFLSH, (void*)queue);
  101. }
  102. int tcflow(int fd, int action)
  103. {
  104. return ioctl(fd, TCXONC, (void*)action);
  105. }
  106. /**
  107. * this function waits for transmission of output
  108. */
  109. int tcdrain(int fd)
  110. {
  111. return 0;
  112. }
  113. int isatty (int fd)
  114. {
  115. struct termios term;
  116. return tcgetattr (fd, &term) == 0;
  117. }
  118. #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  119. void cfmakeraw(struct termios *t)
  120. {
  121. t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  122. t->c_oflag &= ~OPOST;
  123. t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  124. t->c_cflag &= ~(CSIZE|PARENB);
  125. t->c_cflag |= CS8;
  126. t->c_cc[VMIN] = 1;
  127. t->c_cc[VTIME] = 0;
  128. }
  129. int cfsetspeed(struct termios *tio, speed_t speed)
  130. {
  131. return cfsetospeed(tio, speed);
  132. }
  133. #endif