reactive_serial_port_service.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // detail/reactive_serial_port_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_SERIAL_PORT)
  18. #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  19. #include <string>
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/io_context.hpp>
  22. #include <boost/asio/serial_port_base.hpp>
  23. #include <boost/asio/detail/descriptor_ops.hpp>
  24. #include <boost/asio/detail/reactive_descriptor_service.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace detail {
  29. // Extend reactive_descriptor_service to provide serial port support.
  30. class reactive_serial_port_service :
  31. public service_base<reactive_serial_port_service>
  32. {
  33. public:
  34. // The native type of a serial port.
  35. typedef reactive_descriptor_service::native_handle_type native_handle_type;
  36. // The implementation type of the serial port.
  37. typedef reactive_descriptor_service::implementation_type implementation_type;
  38. BOOST_ASIO_DECL reactive_serial_port_service(
  39. boost::asio::io_context& io_context);
  40. // Destroy all user-defined handler objects owned by the service.
  41. BOOST_ASIO_DECL void shutdown();
  42. // Construct a new serial port implementation.
  43. void construct(implementation_type& impl)
  44. {
  45. descriptor_service_.construct(impl);
  46. }
  47. // Move-construct a new serial port implementation.
  48. void move_construct(implementation_type& impl,
  49. implementation_type& other_impl)
  50. {
  51. descriptor_service_.move_construct(impl, other_impl);
  52. }
  53. // Move-assign from another serial port implementation.
  54. void move_assign(implementation_type& impl,
  55. reactive_serial_port_service& other_service,
  56. implementation_type& other_impl)
  57. {
  58. descriptor_service_.move_assign(impl,
  59. other_service.descriptor_service_, other_impl);
  60. }
  61. // Destroy a serial port implementation.
  62. void destroy(implementation_type& impl)
  63. {
  64. descriptor_service_.destroy(impl);
  65. }
  66. // Open the serial port using the specified device name.
  67. BOOST_ASIO_DECL boost::system::error_code open(implementation_type& impl,
  68. const std::string& device, boost::system::error_code& ec);
  69. // Assign a native descriptor to a serial port implementation.
  70. boost::system::error_code assign(implementation_type& impl,
  71. const native_handle_type& native_descriptor,
  72. boost::system::error_code& ec)
  73. {
  74. return descriptor_service_.assign(impl, native_descriptor, ec);
  75. }
  76. // Determine whether the serial port is open.
  77. bool is_open(const implementation_type& impl) const
  78. {
  79. return descriptor_service_.is_open(impl);
  80. }
  81. // Destroy a serial port implementation.
  82. boost::system::error_code close(implementation_type& impl,
  83. boost::system::error_code& ec)
  84. {
  85. return descriptor_service_.close(impl, ec);
  86. }
  87. // Get the native serial port representation.
  88. native_handle_type native_handle(implementation_type& impl)
  89. {
  90. return descriptor_service_.native_handle(impl);
  91. }
  92. // Cancel all operations associated with the serial port.
  93. boost::system::error_code cancel(implementation_type& impl,
  94. boost::system::error_code& ec)
  95. {
  96. return descriptor_service_.cancel(impl, ec);
  97. }
  98. // Set an option on the serial port.
  99. template <typename SettableSerialPortOption>
  100. boost::system::error_code set_option(implementation_type& impl,
  101. const SettableSerialPortOption& option, boost::system::error_code& ec)
  102. {
  103. return do_set_option(impl,
  104. &reactive_serial_port_service::store_option<SettableSerialPortOption>,
  105. &option, ec);
  106. }
  107. // Get an option from the serial port.
  108. template <typename GettableSerialPortOption>
  109. boost::system::error_code get_option(const implementation_type& impl,
  110. GettableSerialPortOption& option, boost::system::error_code& ec) const
  111. {
  112. return do_get_option(impl,
  113. &reactive_serial_port_service::load_option<GettableSerialPortOption>,
  114. &option, ec);
  115. }
  116. // Send a break sequence to the serial port.
  117. boost::system::error_code send_break(implementation_type& impl,
  118. boost::system::error_code& ec)
  119. {
  120. errno = 0;
  121. descriptor_ops::error_wrapper(::tcsendbreak(
  122. descriptor_service_.native_handle(impl), 0), ec);
  123. return ec;
  124. }
  125. // Write the given data. Returns the number of bytes sent.
  126. template <typename ConstBufferSequence>
  127. size_t write_some(implementation_type& impl,
  128. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  129. {
  130. return descriptor_service_.write_some(impl, buffers, ec);
  131. }
  132. // Start an asynchronous write. The data being written must be valid for the
  133. // lifetime of the asynchronous operation.
  134. template <typename ConstBufferSequence, typename Handler>
  135. void async_write_some(implementation_type& impl,
  136. const ConstBufferSequence& buffers, Handler& handler)
  137. {
  138. descriptor_service_.async_write_some(impl, buffers, handler);
  139. }
  140. // Read some data. Returns the number of bytes received.
  141. template <typename MutableBufferSequence>
  142. size_t read_some(implementation_type& impl,
  143. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  144. {
  145. return descriptor_service_.read_some(impl, buffers, ec);
  146. }
  147. // Start an asynchronous read. The buffer for the data being received must be
  148. // valid for the lifetime of the asynchronous operation.
  149. template <typename MutableBufferSequence, typename Handler>
  150. void async_read_some(implementation_type& impl,
  151. const MutableBufferSequence& buffers, Handler& handler)
  152. {
  153. descriptor_service_.async_read_some(impl, buffers, handler);
  154. }
  155. private:
  156. // Function pointer type for storing a serial port option.
  157. typedef boost::system::error_code (*store_function_type)(
  158. const void*, termios&, boost::system::error_code&);
  159. // Helper function template to store a serial port option.
  160. template <typename SettableSerialPortOption>
  161. static boost::system::error_code store_option(const void* option,
  162. termios& storage, boost::system::error_code& ec)
  163. {
  164. static_cast<const SettableSerialPortOption*>(option)->store(storage, ec);
  165. return ec;
  166. }
  167. // Helper function to set a serial port option.
  168. BOOST_ASIO_DECL boost::system::error_code do_set_option(
  169. implementation_type& impl, store_function_type store,
  170. const void* option, boost::system::error_code& ec);
  171. // Function pointer type for loading a serial port option.
  172. typedef boost::system::error_code (*load_function_type)(
  173. void*, const termios&, boost::system::error_code&);
  174. // Helper function template to load a serial port option.
  175. template <typename GettableSerialPortOption>
  176. static boost::system::error_code load_option(void* option,
  177. const termios& storage, boost::system::error_code& ec)
  178. {
  179. static_cast<GettableSerialPortOption*>(option)->load(storage, ec);
  180. return ec;
  181. }
  182. // Helper function to get a serial port option.
  183. BOOST_ASIO_DECL boost::system::error_code do_get_option(
  184. const implementation_type& impl, load_function_type load,
  185. void* option, boost::system::error_code& ec) const;
  186. // The implementation used for initiating asynchronous operations.
  187. reactive_descriptor_service descriptor_service_;
  188. };
  189. } // namespace detail
  190. } // namespace asio
  191. } // namespace boost
  192. #include <boost/asio/detail/pop_options.hpp>
  193. #if defined(BOOST_ASIO_HEADER_ONLY)
  194. # include <boost/asio/detail/impl/reactive_serial_port_service.ipp>
  195. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  196. #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  197. #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
  198. #endif // BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP