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.

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