Mirror of CollapseOS
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

109 řádky
2.8KB

  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. void mread(int fd, char *s, int count)
  8. {
  9. while (count) {
  10. while (read(fd, s, 1) == 0) {
  11. usleep(1000);
  12. }
  13. s++;
  14. count--;
  15. }
  16. }
  17. void readprompt(int fd)
  18. {
  19. char junk[3];
  20. mread(fd, junk, 3); // " ok" prompt
  21. }
  22. void sendcmd(int fd, char *cmd)
  23. {
  24. char junk[2];
  25. while (*cmd) {
  26. write(fd, cmd, 1);
  27. read(fd, &junk, 1);
  28. cmd++;
  29. // The other side is sometimes much slower than us and if we don't let
  30. // it breathe, it can choke.
  31. usleep(1000);
  32. }
  33. write(fd, "\r", 1);
  34. mread(fd, junk, 2); // sends back \r\n
  35. usleep(1000);
  36. }
  37. // Send a cmd and also read the " ok" prompt
  38. void sendcmdp(int fd, char *cmd)
  39. {
  40. char junk[2];
  41. sendcmd(fd, cmd);
  42. readprompt(fd);
  43. }
  44. // from https://stackoverflow.com/a/6947758
  45. // discussion from https://stackoverflow.com/a/26006680 is interesting,
  46. // but we don't want POSIX compliance.
  47. int set_interface_attribs(int fd, int speed, int parity)
  48. {
  49. struct termios tty;
  50. if (tcgetattr (fd, &tty) != 0) {
  51. fprintf(stderr, "error %d from tcgetattr", errno);
  52. return -1;
  53. }
  54. if (speed) {
  55. cfsetospeed (&tty, speed);
  56. cfsetispeed (&tty, speed);
  57. }
  58. tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
  59. // disable IGNBRK for mismatched speed tests; otherwise receive break
  60. // as \000 chars
  61. tty.c_iflag &= ~IGNBRK; // disable break processing
  62. tty.c_lflag = 0; // no signaling chars, no echo,
  63. // no canonical processing
  64. tty.c_oflag = 0; // no remapping, no delays
  65. tty.c_cc[VMIN] = 0; // read doesn't block
  66. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  67. tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
  68. tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
  69. // enable reading
  70. tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
  71. tty.c_cflag |= parity;
  72. tty.c_cflag &= ~CSTOPB;
  73. tty.c_cflag &= ~CRTSCTS;
  74. if (tcsetattr (fd, TCSANOW, &tty) != 0) {
  75. fprintf(stderr, "error %d from tcsetattr", errno);
  76. return -1;
  77. }
  78. return 0;
  79. }
  80. void set_blocking(int fd, int should_block)
  81. {
  82. struct termios tty;
  83. memset(&tty, 0, sizeof tty);
  84. if (tcgetattr (fd, &tty) != 0) {
  85. fprintf(stderr, "error %d from tggetattr", errno);
  86. return;
  87. }
  88. tty.c_cc[VMIN] = should_block ? 1 : 0;
  89. tty.c_cc[VTIME] = 1; // 0.1 seconds read timeout
  90. if (tcsetattr (fd, TCSANOW, &tty) != 0) {
  91. fprintf(stderr, "error %d setting term attributes", errno);
  92. }
  93. }