serial.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #include <nall/intrinsics.hpp>
  3. #include <nall/stdint.hpp>
  4. #include <nall/string.hpp>
  5. #if !defined(API_POSIX)
  6. #error "nall/serial: unsupported system"
  7. #endif
  8. #include <sys/ioctl.h>
  9. #include <fcntl.h>
  10. #include <termios.h>
  11. #include <unistd.h>
  12. namespace nall {
  13. struct serial {
  14. ~serial() {
  15. close();
  16. }
  17. auto readable() -> bool {
  18. if(!opened) return false;
  19. fd_set fdset;
  20. FD_ZERO(&fdset);
  21. FD_SET(port, &fdset);
  22. timeval timeout;
  23. timeout.tv_sec = 0;
  24. timeout.tv_usec = 0;
  25. int result = select(FD_SETSIZE, &fdset, nullptr, nullptr, &timeout);
  26. if(result < 1) return false;
  27. return FD_ISSET(port, &fdset);
  28. }
  29. //-1 on error, otherwise return bytes read
  30. auto read(uint8_t* data, uint length) -> int {
  31. if(!opened) return -1;
  32. return ::read(port, (void*)data, length);
  33. }
  34. auto writable() -> bool {
  35. if(!opened) return false;
  36. fd_set fdset;
  37. FD_ZERO(&fdset);
  38. FD_SET(port, &fdset);
  39. timeval timeout;
  40. timeout.tv_sec = 0;
  41. timeout.tv_usec = 0;
  42. int result = select(FD_SETSIZE, nullptr, &fdset, nullptr, &timeout);
  43. if(result < 1) return false;
  44. return FD_ISSET(port, &fdset);
  45. }
  46. //-1 on error, otherwise return bytes written
  47. auto write(const uint8_t* data, uint length) -> int {
  48. if(!opened) return -1;
  49. return ::write(port, (void*)data, length);
  50. }
  51. //rate==0: use flow control (synchronous mode)
  52. //rate!=0: baud-rate (asynchronous mode)
  53. auto open(string device, uint rate = 0) -> bool {
  54. close();
  55. if(!device) device = "/dev/ttyU0"; //note: default device name is for FreeBSD 10+
  56. port = ::open(device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
  57. if(port == -1) return false;
  58. if(ioctl(port, TIOCEXCL) == -1) { close(); return false; }
  59. if(fcntl(port, F_SETFL, 0) == -1) { close(); return false; }
  60. if(tcgetattr(port, &original_attr) == -1) { close(); return false; }
  61. termios attr = original_attr;
  62. cfmakeraw(&attr);
  63. cfsetspeed(&attr, rate ? rate : 57600); //rate value has no effect in synchronous mode
  64. attr.c_lflag &=~ (ECHO | ECHONL | ISIG | ICANON | IEXTEN);
  65. attr.c_iflag &=~ (BRKINT | PARMRK | INPCK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY);
  66. attr.c_iflag |= (IGNBRK | IGNPAR);
  67. attr.c_oflag &=~ (OPOST);
  68. attr.c_cflag &=~ (CSIZE | CSTOPB | PARENB | CLOCAL);
  69. attr.c_cflag |= (CS8 | CREAD);
  70. if(rate) {
  71. attr.c_cflag &= ~CRTSCTS;
  72. } else {
  73. attr.c_cflag |= CRTSCTS;
  74. }
  75. attr.c_cc[VTIME] = attr.c_cc[VMIN] = 0;
  76. if(tcsetattr(port, TCSANOW, &attr) == -1) { close(); return false; }
  77. return opened = true;
  78. }
  79. auto close() -> void {
  80. if(port != -1) {
  81. tcdrain(port);
  82. if(opened) {
  83. tcsetattr(port, TCSANOW, &original_attr);
  84. opened = false;
  85. }
  86. ::close(port);
  87. port = -1;
  88. }
  89. }
  90. private:
  91. int port = -1;
  92. bool opened = false;
  93. termios original_attr;
  94. };
  95. }