termios.c 2.6 KB

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