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.

161 lines
3.9KB

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