termios.c 2.6 KB

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