Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.7KB

  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <termios.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #define BREATHE usleep(2000)
  9. //#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
  10. #define DEBUG(...)
  11. void mread(int fd, char *s, int count)
  12. {
  13. while (count) {
  14. while (read(fd, s, 1) == 0) {
  15. BREATHE;
  16. }
  17. s++;
  18. count--;
  19. }
  20. }
  21. // Make sure that nothing is waiting in the pipeline
  22. static void mempty(int fd)
  23. {
  24. char c;
  25. while (read(fd, &c, 1) == 1) {
  26. DEBUG("Emptying %d\n", c);
  27. BREATHE;
  28. }
  29. }
  30. static void mexpect(int fd, char ec)
  31. {
  32. char c;
  33. mread(fd, &c, 1);
  34. if (c != ec) {
  35. fprintf(stderr, "Expected %d but got %d\n", ec, c);
  36. }
  37. }
  38. void readprompt(int fd)
  39. {
  40. mexpect(fd, ' ');
  41. mexpect(fd, 'o');
  42. mexpect(fd, 'k');
  43. mexpect(fd, '\r');
  44. mexpect(fd, '\n');
  45. }
  46. void sendcmd(int fd, char *cmd)
  47. {
  48. DEBUG("Sending %s\n", cmd);
  49. char junk[2];
  50. while (*cmd) {
  51. DEBUG("W: %d\n", *cmd);
  52. write(fd, cmd, 1);
  53. BREATHE;
  54. read(fd, &junk, 1);
  55. DEBUG("R: %d\n", *junk);
  56. cmd++;
  57. // The other side is sometimes much slower than us and if we don't let
  58. // it breathe, it can choke.
  59. BREATHE;
  60. }
  61. write(fd, "\r", 1);
  62. mexpect(fd, '\r');
  63. mexpect(fd, '\n');
  64. BREATHE;
  65. }
  66. // Send a cmd and also read the " ok" prompt
  67. void sendcmdp(int fd, char *cmd)
  68. {
  69. sendcmd(fd, cmd);
  70. readprompt(fd);
  71. }
  72. // from https://stackoverflow.com/a/6947758
  73. // discussion from https://stackoverflow.com/a/26006680 is interesting,
  74. // but we don't want POSIX compliance.
  75. int set_interface_attribs(int fd, int speed, int parity)
  76. {
  77. struct termios tty;
  78. if (tcgetattr (fd, &tty) != 0) {
  79. fprintf(stderr, "error %d from tcgetattr", errno);
  80. return -1;
  81. }
  82. if (speed) {
  83. cfsetospeed (&tty, speed);
  84. cfsetispeed (&tty, speed);
  85. }
  86. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
  87. // disable IGNBRK for mismatched speed tests; otherwise receive break
  88. // as \000 chars
  89. tty.c_iflag &= ~IGNBRK; // disable break processing
  90. tty.c_iflag &= ~ICRNL; // disable CR->NL mapping
  91. tty.c_lflag = 0; // no signaling chars, no echo,
  92. // no canonical processing
  93. tty.c_oflag = 0; // no remapping, no delays
  94. tty.c_cc[VMIN] = 0; // read doesn't block
  95. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  96. tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  97. tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
  98. // enable reading
  99. tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
  100. tty.c_cflag |= parity;
  101. tty.c_cflag &= ~CSTOPB;
  102. tty.c_cflag &= ~CRTSCTS;
  103. if (tcsetattr (fd, TCSANOW, &tty) != 0) {
  104. fprintf(stderr, "error %d from tcsetattr", errno);
  105. return -1;
  106. }
  107. return 0;
  108. }
  109. void set_blocking(int fd, int should_block)
  110. {
  111. struct termios tty;
  112. memset(&tty, 0, sizeof tty);
  113. if (tcgetattr (fd, &tty) != 0) {
  114. fprintf(stderr, "error %d from tggetattr", errno);
  115. return;
  116. }
  117. tty.c_cc[VMIN] = should_block ? 1 : 0;
  118. tty.c_cc[VTIME] = 1; // 0.1 seconds read timeout
  119. if (tcsetattr (fd, TCSANOW, &tty) != 0) {
  120. fprintf(stderr, "error %d setting term attributes", errno);
  121. }
  122. }
  123. int ttyopen(char *devname)
  124. {
  125. int fd = 0;
  126. if (strcmp(devname, "-") != 0) {
  127. fd = open(devname, O_RDWR|O_NOCTTY|O_SYNC);
  128. }
  129. set_interface_attribs(fd, 0, 0);
  130. set_blocking(fd, 0);
  131. mempty(fd);
  132. set_blocking(fd, 1);
  133. return fd;
  134. }