posix_termios.c 3.4 KB

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