win_iocp_serial_port_service.hpp 7.8 KB

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