termios.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <assert.h>
  7. #include <bits/pthread_cancel.h>
  8. #include <errno.h>
  9. #include <sys/ioctl.h>
  10. #include <termios.h>
  11. extern "C" {
  12. int tcgetattr(int fd, struct termios* t)
  13. {
  14. return ioctl(fd, TCGETS, t);
  15. }
  16. int tcsetattr(int fd, int optional_actions, const struct termios* t)
  17. {
  18. switch (optional_actions) {
  19. case TCSANOW:
  20. return ioctl(fd, TCSETS, t);
  21. case TCSADRAIN:
  22. return ioctl(fd, TCSETSW, t);
  23. case TCSAFLUSH:
  24. return ioctl(fd, TCSETSF, t);
  25. }
  26. errno = EINVAL;
  27. return -1;
  28. }
  29. // https://pubs.opengroup.org/onlinepubs/009695399/functions/tcsendbreak.html
  30. int tcsendbreak([[maybe_unused]] int fd, [[maybe_unused]] int duration)
  31. {
  32. // FIXME: Implement this for real.
  33. return 0;
  34. }
  35. int tcflow([[maybe_unused]] int fd, [[maybe_unused]] int action)
  36. {
  37. errno = EINVAL;
  38. return -1;
  39. }
  40. int tcflush(int fd, int queue_selector)
  41. {
  42. return ioctl(fd, TCFLSH, queue_selector);
  43. }
  44. // https://pubs.opengroup.org/onlinepubs/009695399/functions/tcdrain.html
  45. int tcdrain([[maybe_unused]] int fd)
  46. {
  47. __pthread_maybe_cancel();
  48. // FIXME: Implement this for real.
  49. return 0;
  50. }
  51. speed_t cfgetispeed(const struct termios* tp)
  52. {
  53. return tp->c_ispeed;
  54. }
  55. speed_t cfgetospeed(const struct termios* tp)
  56. {
  57. return tp->c_ospeed;
  58. }
  59. static int baud_rate_from_speed(speed_t speed)
  60. {
  61. int rate = -EINVAL;
  62. switch (speed) {
  63. case B0:
  64. rate = 0;
  65. break;
  66. case B50:
  67. rate = 50;
  68. break;
  69. case B75:
  70. rate = 75;
  71. break;
  72. case B110:
  73. rate = 110;
  74. break;
  75. case B134:
  76. rate = 134;
  77. break;
  78. case B150:
  79. rate = 150;
  80. break;
  81. case B200:
  82. rate = 200;
  83. break;
  84. case B300:
  85. rate = 300;
  86. break;
  87. case B600:
  88. rate = 600;
  89. break;
  90. case B1200:
  91. rate = 1200;
  92. break;
  93. case B1800:
  94. rate = 1800;
  95. break;
  96. case B2400:
  97. rate = 2400;
  98. break;
  99. case B4800:
  100. rate = 4800;
  101. break;
  102. case B9600:
  103. rate = 9600;
  104. break;
  105. case B19200:
  106. rate = 19200;
  107. break;
  108. case B38400:
  109. rate = 38400;
  110. break;
  111. }
  112. return rate;
  113. }
  114. int cfsetispeed(struct termios* tp, speed_t speed)
  115. {
  116. auto ispeed = baud_rate_from_speed(speed);
  117. if (ispeed > 0) {
  118. tp->c_ispeed = ispeed;
  119. }
  120. __RETURN_WITH_ERRNO(ispeed, 0, -1);
  121. }
  122. int cfsetospeed(struct termios* tp, speed_t speed)
  123. {
  124. auto ospeed = baud_rate_from_speed(speed);
  125. if (ospeed > 0) {
  126. tp->c_ispeed = ospeed;
  127. }
  128. __RETURN_WITH_ERRNO(ospeed, 0, -1);
  129. }
  130. void cfmakeraw(struct termios* tp)
  131. {
  132. if (!tp)
  133. return;
  134. auto& termios = *tp;
  135. termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  136. termios.c_lflag &= ~OPOST;
  137. termios.c_cflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  138. termios.c_cflag |= CS8;
  139. }
  140. }