serial.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Murray Jensen <Murray.Jensen@csiro.au>
  5. */
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <sys/time.h>
  10. #include "serial.h"
  11. #if defined(__sun__) || \
  12. defined(__OpenBSD__) || \
  13. defined(__FreeBSD__) || \
  14. defined(__NetBSD__) || \
  15. defined(__APPLE__)
  16. static struct termios tios = { BRKINT, 0, B115200|CS8|CREAD, 0, { 0 } };
  17. #else
  18. static struct termios tios = { BRKINT, 0, B115200|CS8|CREAD, 0, 0 };
  19. #endif
  20. static struct speedmap {
  21. char *str;
  22. speed_t val;
  23. } speedmap[] = {
  24. { "50", B50 }, { "75", B75 }, { "110", B110 },
  25. { "134", B134 }, { "150", B150 }, { "200", B200 },
  26. { "300", B300 }, { "600", B600 }, { "1200", B1200 },
  27. { "1800", B1800 }, { "2400", B2400 }, { "4800", B4800 },
  28. { "9600", B9600 }, { "19200", B19200 }, { "38400", B38400 },
  29. { "57600", B57600 },
  30. #ifdef B76800
  31. { "76800", B76800 },
  32. #endif
  33. { "115200", B115200 },
  34. #ifdef B153600
  35. { "153600", B153600 },
  36. #endif
  37. { "230400", B230400 },
  38. #ifdef B307200
  39. { "307200", B307200 },
  40. #endif
  41. #ifdef B460800
  42. { "460800", B460800 }
  43. #endif
  44. };
  45. static int nspeeds = sizeof speedmap / sizeof speedmap[0];
  46. speed_t
  47. cvtspeed(char *str)
  48. {
  49. struct speedmap *smp = speedmap, *esmp = &speedmap[nspeeds];
  50. while (smp < esmp) {
  51. if (strcmp(str, smp->str) == 0)
  52. return (smp->val);
  53. smp++;
  54. }
  55. return B0;
  56. }
  57. int
  58. serialopen(char *device, speed_t speed)
  59. {
  60. int fd;
  61. if (cfsetospeed(&tios, speed) < 0)
  62. return -1;
  63. if ((fd = open(device, O_RDWR)) < 0)
  64. return -1;
  65. if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
  66. (void)close(fd);
  67. return -1;
  68. }
  69. return fd;
  70. }
  71. int
  72. serialreadchar(int fd, int timeout)
  73. {
  74. fd_set fds;
  75. struct timeval tv;
  76. int n;
  77. char ch;
  78. tv.tv_sec = timeout;
  79. tv.tv_usec = 0;
  80. FD_ZERO(&fds);
  81. FD_SET(fd, &fds);
  82. /* this is a fucking horrible quick hack - fix this */
  83. if ((n = select(fd + 1, &fds, 0, 0, &tv)) < 0)
  84. return SERIAL_ERROR;
  85. if (n == 0)
  86. return SERIAL_TIMEOUT;
  87. if ((n = read(fd, &ch, 1)) < 0)
  88. return SERIAL_ERROR;
  89. if (n == 0)
  90. return SERIAL_EOF;
  91. return ch;
  92. }
  93. int
  94. serialwrite(int fd, char *buf, int len)
  95. {
  96. int n;
  97. do {
  98. n = write(fd, buf, len);
  99. if (n < 0)
  100. return 1;
  101. len -= n;
  102. buf += n;
  103. } while (len > 0);
  104. return 0;
  105. }
  106. int
  107. serialclose(int fd)
  108. {
  109. return close(fd);
  110. }