termios.cpp 2.9 KB

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