termios.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. int tcflow([[maybe_unused]] int fd, [[maybe_unused]] int action)
  29. {
  30. errno = EINVAL;
  31. return -1;
  32. }
  33. int tcflush(int fd, int queue_selector)
  34. {
  35. return ioctl(fd, TCFLSH, queue_selector);
  36. }
  37. speed_t cfgetispeed(const struct termios* tp)
  38. {
  39. return tp->c_ispeed;
  40. }
  41. speed_t cfgetospeed(const struct termios* tp)
  42. {
  43. return tp->c_ospeed;
  44. }
  45. static int baud_rate_from_speed(speed_t speed)
  46. {
  47. int rate = -EINVAL;
  48. switch (speed) {
  49. case B0:
  50. rate = 0;
  51. break;
  52. case B50:
  53. rate = 50;
  54. break;
  55. case B75:
  56. rate = 75;
  57. break;
  58. case B110:
  59. rate = 110;
  60. break;
  61. case B134:
  62. rate = 134;
  63. break;
  64. case B150:
  65. rate = 150;
  66. break;
  67. case B200:
  68. rate = 200;
  69. break;
  70. case B300:
  71. rate = 300;
  72. break;
  73. case B600:
  74. rate = 600;
  75. break;
  76. case B1200:
  77. rate = 1200;
  78. break;
  79. case B1800:
  80. rate = 1800;
  81. break;
  82. case B2400:
  83. rate = 2400;
  84. break;
  85. case B4800:
  86. rate = 4800;
  87. break;
  88. case B9600:
  89. rate = 9600;
  90. break;
  91. case B19200:
  92. rate = 19200;
  93. break;
  94. case B38400:
  95. rate = 38400;
  96. break;
  97. }
  98. return rate;
  99. }
  100. int cfsetispeed(struct termios* tp, speed_t speed)
  101. {
  102. auto ispeed = baud_rate_from_speed(speed);
  103. if (ispeed > 0) {
  104. tp->c_ispeed = ispeed;
  105. }
  106. __RETURN_WITH_ERRNO(ispeed, 0, -1);
  107. }
  108. int cfsetospeed(struct termios* tp, speed_t speed)
  109. {
  110. auto ospeed = baud_rate_from_speed(speed);
  111. if (ospeed > 0) {
  112. tp->c_ispeed = ospeed;
  113. }
  114. __RETURN_WITH_ERRNO(ospeed, 0, -1);
  115. }
  116. void cfmakeraw(struct termios* tp)
  117. {
  118. if (!tp)
  119. return;
  120. auto& termios = *tp;
  121. termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  122. termios.c_lflag &= ~OPOST;
  123. termios.c_cflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  124. termios.c_cflag |= CS8;
  125. }
  126. }