reactive_serial_port_service.hpp 8.1 KB

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