unix.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*!
  2. * \file serial/impl/unix.h
  3. * \author William Woodall <wjwwood@gmail.com>
  4. * \author John Harrison <ash@greaterthaninfinity.com>
  5. * \version 0.1
  6. *
  7. * \section LICENSE
  8. *
  9. * The MIT License
  10. *
  11. * Copyright (c) 2012 William Woodall, John Harrison
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a
  14. * copy of this software and associated documentation files (the "Software"),
  15. * to deal in the Software without restriction, including without limitation
  16. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17. * and/or sell copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. * DEALINGS IN THE SOFTWARE.
  30. *
  31. * \section DESCRIPTION
  32. *
  33. * This provides a unix based pimpl for the Serial class. This implementation is
  34. * based off termios.h and uses select for multiplexing the IO ports.
  35. *
  36. */
  37. #if !defined(_WIN32)
  38. #ifndef SERIAL_IMPL_UNIX_H
  39. #define SERIAL_IMPL_UNIX_H
  40. #include "serial/serial.h"
  41. #include <pthread.h>
  42. namespace serial {
  43. using std::size_t;
  44. using std::string;
  45. using std::invalid_argument;
  46. using serial::SerialException;
  47. using serial::IOException;
  48. class MillisecondTimer {
  49. public:
  50. MillisecondTimer(const uint32_t millis);
  51. int64_t remaining();
  52. private:
  53. static timespec timespec_now();
  54. timespec expiry;
  55. };
  56. class serial::Serial::SerialImpl {
  57. public:
  58. SerialImpl (const string &port,
  59. unsigned long baudrate,
  60. bytesize_t bytesize,
  61. parity_t parity,
  62. stopbits_t stopbits,
  63. flowcontrol_t flowcontrol);
  64. virtual ~SerialImpl ();
  65. void
  66. open ();
  67. void
  68. close ();
  69. bool
  70. isOpen () const;
  71. size_t
  72. available ();
  73. bool
  74. waitReadable (uint32_t timeout);
  75. void
  76. waitByteTimes (size_t count);
  77. size_t
  78. read (uint8_t *buf, size_t size = 1);
  79. size_t
  80. write (const uint8_t *data, size_t length);
  81. void
  82. flush ();
  83. void
  84. flushInput ();
  85. void
  86. flushOutput ();
  87. void
  88. sendBreak (int duration);
  89. void
  90. setBreak (bool level);
  91. void
  92. setRTS (bool level);
  93. void
  94. setDTR (bool level);
  95. bool
  96. waitForChange ();
  97. bool
  98. getCTS ();
  99. bool
  100. getDSR ();
  101. bool
  102. getRI ();
  103. bool
  104. getCD ();
  105. void
  106. setPort (const string &port);
  107. string
  108. getPort () const;
  109. void
  110. setTimeout (Timeout &timeout);
  111. Timeout
  112. getTimeout () const;
  113. void
  114. setBaudrate (unsigned long baudrate);
  115. unsigned long
  116. getBaudrate () const;
  117. void
  118. setBytesize (bytesize_t bytesize);
  119. bytesize_t
  120. getBytesize () const;
  121. void
  122. setParity (parity_t parity);
  123. parity_t
  124. getParity () const;
  125. void
  126. setStopbits (stopbits_t stopbits);
  127. stopbits_t
  128. getStopbits () const;
  129. void
  130. setFlowcontrol (flowcontrol_t flowcontrol);
  131. flowcontrol_t
  132. getFlowcontrol () const;
  133. void
  134. readLock ();
  135. void
  136. readUnlock ();
  137. void
  138. writeLock ();
  139. void
  140. writeUnlock ();
  141. protected:
  142. void reconfigurePort ();
  143. private:
  144. string port_; // Path to the file descriptor
  145. int fd_; // The current file descriptor
  146. bool is_open_;
  147. bool xonxoff_;
  148. bool rtscts_;
  149. Timeout timeout_; // Timeout for read operations
  150. unsigned long baudrate_; // Baudrate
  151. uint32_t byte_time_ns_; // Nanoseconds to transmit/receive a single byte
  152. parity_t parity_; // Parity
  153. bytesize_t bytesize_; // Size of the bytes
  154. stopbits_t stopbits_; // Stop Bits
  155. flowcontrol_t flowcontrol_; // Flow Control
  156. // Mutex used to lock the read functions
  157. pthread_mutex_t read_mutex;
  158. // Mutex used to lock the write functions
  159. pthread_mutex_t write_mutex;
  160. };
  161. }
  162. #endif // SERIAL_IMPL_UNIX_H
  163. #endif // !defined(_WIN32)