reactive_serial_port_service.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // reactive_serial_port_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP
  12. #define BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. #include <cstring>
  19. #include <string>
  20. #include <boost/asio/detail/pop_options.hpp>
  21. #include <boost/asio/serial_port_base.hpp>
  22. #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \
  23. && !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/io_service.hpp>
  26. #include <boost/asio/detail/descriptor_ops.hpp>
  27. #include <boost/asio/detail/reactive_descriptor_service.hpp>
  28. namespace boost {
  29. namespace asio {
  30. namespace detail {
  31. // Extend reactive_descriptor_service to provide serial port support.
  32. template <typename Reactor>
  33. class reactive_serial_port_service
  34. : public boost::asio::detail::service_base<
  35. reactive_serial_port_service<Reactor> >
  36. {
  37. public:
  38. // The native type of a stream handle.
  39. typedef typename reactive_descriptor_service<Reactor>::native_type
  40. native_type;
  41. // The implementation type of the stream handle.
  42. typedef typename reactive_descriptor_service<Reactor>::implementation_type
  43. implementation_type;
  44. reactive_serial_port_service(boost::asio::io_service& io_service)
  45. : boost::asio::detail::service_base<
  46. reactive_serial_port_service>(io_service),
  47. descriptor_service_(boost::asio::use_service<
  48. reactive_descriptor_service<Reactor> >(io_service))
  49. {
  50. }
  51. // Destroy all user-defined handler objects owned by the service.
  52. void shutdown_service()
  53. {
  54. }
  55. // Construct a new handle implementation.
  56. void construct(implementation_type& impl)
  57. {
  58. descriptor_service_.construct(impl);
  59. }
  60. // Destroy a handle implementation.
  61. void destroy(implementation_type& impl)
  62. {
  63. descriptor_service_.destroy(impl);
  64. }
  65. // Open the serial port using the specified device name.
  66. boost::system::error_code open(implementation_type& impl,
  67. const std::string& device, boost::system::error_code& ec)
  68. {
  69. if (is_open(impl))
  70. {
  71. ec = boost::asio::error::already_open;
  72. return ec;
  73. }
  74. int fd = descriptor_ops::open(device.c_str(),
  75. O_RDWR | O_NONBLOCK | O_NOCTTY, ec);
  76. if (fd < 0)
  77. return ec;
  78. int s = descriptor_ops::fcntl(fd, F_GETFL, ec);
  79. if (s >= 0)
  80. s = descriptor_ops::fcntl(fd, F_SETFL, s | O_NONBLOCK, ec);
  81. if (s < 0)
  82. {
  83. boost::system::error_code ignored_ec;
  84. descriptor_ops::close(fd, ignored_ec);
  85. return ec;
  86. }
  87. // Set up default serial port options.
  88. termios ios;
  89. descriptor_ops::clear_error(ec);
  90. s = descriptor_ops::error_wrapper(::tcgetattr(fd, &ios), ec);
  91. if (s >= 0)
  92. {
  93. #if defined(_BSD_SOURCE)
  94. ::cfmakeraw(&ios);
  95. #else
  96. ios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK
  97. | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  98. ios.c_oflag &= ~OPOST;
  99. ios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  100. ios.c_cflag &= ~(CSIZE | PARENB);
  101. ios.c_cflag |= CS8;
  102. #endif
  103. ios.c_iflag |= IGNPAR;
  104. ios.c_cflag |= CREAD | CLOCAL;
  105. descriptor_ops::clear_error(ec);
  106. s = descriptor_ops::error_wrapper(::tcsetattr(fd, TCSANOW, &ios), ec);
  107. }
  108. if (s < 0)
  109. {
  110. boost::system::error_code ignored_ec;
  111. descriptor_ops::close(fd, ignored_ec);
  112. return ec;
  113. }
  114. // We're done. Take ownership of the serial port descriptor.
  115. if (descriptor_service_.assign(impl, fd, ec))
  116. {
  117. boost::system::error_code ignored_ec;
  118. descriptor_ops::close(fd, ignored_ec);
  119. }
  120. return ec;
  121. }
  122. // Assign a native handle to a handle implementation.
  123. boost::system::error_code assign(implementation_type& impl,
  124. const native_type& native_descriptor, boost::system::error_code& ec)
  125. {
  126. return descriptor_service_.assign(impl, native_descriptor, ec);
  127. }
  128. // Determine whether the handle is open.
  129. bool is_open(const implementation_type& impl) const
  130. {
  131. return descriptor_service_.is_open(impl);
  132. }
  133. // Destroy a handle implementation.
  134. boost::system::error_code close(implementation_type& impl,
  135. boost::system::error_code& ec)
  136. {
  137. return descriptor_service_.close(impl, ec);
  138. }
  139. // Get the native handle representation.
  140. native_type native(implementation_type& impl)
  141. {
  142. return descriptor_service_.native(impl);
  143. }
  144. // Cancel all operations associated with the handle.
  145. boost::system::error_code cancel(implementation_type& impl,
  146. boost::system::error_code& ec)
  147. {
  148. return descriptor_service_.cancel(impl, ec);
  149. }
  150. // Set an option on the serial port.
  151. template <typename SettableSerialPortOption>
  152. boost::system::error_code set_option(implementation_type& impl,
  153. const SettableSerialPortOption& option, boost::system::error_code& ec)
  154. {
  155. termios ios;
  156. descriptor_ops::clear_error(ec);
  157. descriptor_ops::error_wrapper(::tcgetattr(
  158. descriptor_service_.native(impl), &ios), ec);
  159. if (ec)
  160. return ec;
  161. if (option.store(ios, ec))
  162. return ec;
  163. descriptor_ops::clear_error(ec);
  164. descriptor_ops::error_wrapper(::tcsetattr(
  165. descriptor_service_.native(impl), TCSANOW, &ios), ec);
  166. return ec;
  167. }
  168. // Get an option from the serial port.
  169. template <typename GettableSerialPortOption>
  170. boost::system::error_code get_option(const implementation_type& impl,
  171. GettableSerialPortOption& option, boost::system::error_code& ec) const
  172. {
  173. termios ios;
  174. descriptor_ops::clear_error(ec);
  175. descriptor_ops::error_wrapper(::tcgetattr(
  176. descriptor_service_.native(impl), &ios), ec);
  177. if (ec)
  178. return ec;
  179. return option.load(ios, ec);
  180. }
  181. // Send a break sequence to the serial port.
  182. boost::system::error_code send_break(implementation_type& impl,
  183. boost::system::error_code& ec)
  184. {
  185. descriptor_ops::clear_error(ec);
  186. descriptor_ops::error_wrapper(::tcsendbreak(
  187. descriptor_service_.native(impl), 0), ec);
  188. return ec;
  189. }
  190. // Write the given data. Returns the number of bytes sent.
  191. template <typename ConstBufferSequence>
  192. size_t write_some(implementation_type& impl,
  193. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  194. {
  195. return descriptor_service_.write_some(impl, buffers, ec);
  196. }
  197. // Start an asynchronous write. The data being written must be valid for the
  198. // lifetime of the asynchronous operation.
  199. template <typename ConstBufferSequence, typename Handler>
  200. void async_write_some(implementation_type& impl,
  201. const ConstBufferSequence& buffers, Handler handler)
  202. {
  203. descriptor_service_.async_write_some(impl, buffers, handler);
  204. }
  205. // Read some data. Returns the number of bytes received.
  206. template <typename MutableBufferSequence>
  207. size_t read_some(implementation_type& impl,
  208. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  209. {
  210. return descriptor_service_.read_some(impl, buffers, ec);
  211. }
  212. // Start an asynchronous read. The buffer for the data being received must be
  213. // valid for the lifetime of the asynchronous operation.
  214. template <typename MutableBufferSequence, typename Handler>
  215. void async_read_some(implementation_type& impl,
  216. const MutableBufferSequence& buffers, Handler handler)
  217. {
  218. descriptor_service_.async_read_some(impl, buffers, handler);
  219. }
  220. private:
  221. // The handle service used for initiating asynchronous operations.
  222. reactive_descriptor_service<Reactor>& descriptor_service_;
  223. };
  224. } // namespace detail
  225. } // namespace asio
  226. } // namespace boost
  227. #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
  228. // && !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
  229. #include <boost/asio/detail/pop_options.hpp>
  230. #endif // BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP