serial_port_service.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // serial_port_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_SERIAL_PORT_SERVICE_HPP
  11. #define BOOST_ASIO_SERIAL_PORT_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  17. #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <cstddef>
  20. #include <string>
  21. #include <boost/asio/async_result.hpp>
  22. #include <boost/asio/detail/reactive_serial_port_service.hpp>
  23. #include <boost/asio/detail/win_iocp_serial_port_service.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/io_context.hpp>
  26. #include <boost/asio/serial_port_base.hpp>
  27. #include <boost/asio/detail/push_options.hpp>
  28. namespace boost {
  29. namespace asio {
  30. /// Default service implementation for a serial port.
  31. class serial_port_service
  32. #if defined(GENERATING_DOCUMENTATION)
  33. : public boost::asio::io_context::service
  34. #else
  35. : public boost::asio::detail::service_base<serial_port_service>
  36. #endif
  37. {
  38. public:
  39. #if defined(GENERATING_DOCUMENTATION)
  40. /// The unique service identifier.
  41. static boost::asio::io_context::id id;
  42. #endif
  43. private:
  44. // The type of the platform-specific implementation.
  45. #if defined(BOOST_ASIO_HAS_IOCP)
  46. typedef detail::win_iocp_serial_port_service service_impl_type;
  47. #else
  48. typedef detail::reactive_serial_port_service service_impl_type;
  49. #endif
  50. public:
  51. /// The type of a serial port implementation.
  52. #if defined(GENERATING_DOCUMENTATION)
  53. typedef implementation_defined implementation_type;
  54. #else
  55. typedef service_impl_type::implementation_type implementation_type;
  56. #endif
  57. /// The native handle type.
  58. #if defined(GENERATING_DOCUMENTATION)
  59. typedef implementation_defined native_handle_type;
  60. #else
  61. typedef service_impl_type::native_handle_type native_handle_type;
  62. #endif
  63. /// Construct a new serial port service for the specified io_context.
  64. explicit serial_port_service(boost::asio::io_context& io_context)
  65. : boost::asio::detail::service_base<serial_port_service>(io_context),
  66. service_impl_(io_context)
  67. {
  68. }
  69. /// Construct a new serial port implementation.
  70. void construct(implementation_type& impl)
  71. {
  72. service_impl_.construct(impl);
  73. }
  74. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  75. /// Move-construct a new serial port implementation.
  76. void move_construct(implementation_type& impl,
  77. implementation_type& other_impl)
  78. {
  79. service_impl_.move_construct(impl, other_impl);
  80. }
  81. /// Move-assign from another serial port implementation.
  82. void move_assign(implementation_type& impl,
  83. serial_port_service& other_service,
  84. implementation_type& other_impl)
  85. {
  86. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  87. }
  88. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  89. /// Destroy a serial port implementation.
  90. void destroy(implementation_type& impl)
  91. {
  92. service_impl_.destroy(impl);
  93. }
  94. /// Open a serial port.
  95. BOOST_ASIO_SYNC_OP_VOID open(implementation_type& impl,
  96. const std::string& device, boost::system::error_code& ec)
  97. {
  98. service_impl_.open(impl, device, ec);
  99. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  100. }
  101. /// Assign an existing native handle to a serial port.
  102. BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl,
  103. const native_handle_type& handle, boost::system::error_code& ec)
  104. {
  105. service_impl_.assign(impl, handle, ec);
  106. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  107. }
  108. /// Determine whether the handle is open.
  109. bool is_open(const implementation_type& impl) const
  110. {
  111. return service_impl_.is_open(impl);
  112. }
  113. /// Close a serial port implementation.
  114. BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl,
  115. boost::system::error_code& ec)
  116. {
  117. service_impl_.close(impl, ec);
  118. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  119. }
  120. /// Get the native handle implementation.
  121. native_handle_type native_handle(implementation_type& impl)
  122. {
  123. return service_impl_.native_handle(impl);
  124. }
  125. /// Cancel all asynchronous operations associated with the handle.
  126. BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl,
  127. boost::system::error_code& ec)
  128. {
  129. service_impl_.cancel(impl, ec);
  130. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  131. }
  132. /// Set a serial port option.
  133. template <typename SettableSerialPortOption>
  134. BOOST_ASIO_SYNC_OP_VOID set_option(implementation_type& impl,
  135. const SettableSerialPortOption& option, boost::system::error_code& ec)
  136. {
  137. service_impl_.set_option(impl, option, ec);
  138. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  139. }
  140. /// Get a serial port option.
  141. template <typename GettableSerialPortOption>
  142. BOOST_ASIO_SYNC_OP_VOID get_option(const implementation_type& impl,
  143. GettableSerialPortOption& option, boost::system::error_code& ec) const
  144. {
  145. service_impl_.get_option(impl, option, ec);
  146. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  147. }
  148. /// Send a break sequence to the serial port.
  149. BOOST_ASIO_SYNC_OP_VOID send_break(implementation_type& impl,
  150. boost::system::error_code& ec)
  151. {
  152. service_impl_.send_break(impl, ec);
  153. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  154. }
  155. /// Write the given data to the stream.
  156. template <typename ConstBufferSequence>
  157. std::size_t write_some(implementation_type& impl,
  158. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  159. {
  160. return service_impl_.write_some(impl, buffers, ec);
  161. }
  162. /// Start an asynchronous write.
  163. template <typename ConstBufferSequence, typename WriteHandler>
  164. BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
  165. void (boost::system::error_code, std::size_t))
  166. async_write_some(implementation_type& impl,
  167. const ConstBufferSequence& buffers,
  168. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  169. {
  170. async_completion<WriteHandler,
  171. void (boost::system::error_code, std::size_t)> init(handler);
  172. service_impl_.async_write_some(impl, buffers, init.completion_handler);
  173. return init.result.get();
  174. }
  175. /// Read some data from the stream.
  176. template <typename MutableBufferSequence>
  177. std::size_t read_some(implementation_type& impl,
  178. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  179. {
  180. return service_impl_.read_some(impl, buffers, ec);
  181. }
  182. /// Start an asynchronous read.
  183. template <typename MutableBufferSequence, typename ReadHandler>
  184. BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
  185. void (boost::system::error_code, std::size_t))
  186. async_read_some(implementation_type& impl,
  187. const MutableBufferSequence& buffers,
  188. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  189. {
  190. async_completion<ReadHandler,
  191. void (boost::system::error_code, std::size_t)> init(handler);
  192. service_impl_.async_read_some(impl, buffers, init.completion_handler);
  193. return init.result.get();
  194. }
  195. private:
  196. // Destroy all user-defined handler objects owned by the service.
  197. void shutdown()
  198. {
  199. service_impl_.shutdown();
  200. }
  201. // The platform-specific implementation.
  202. service_impl_type service_impl_;
  203. };
  204. } // namespace asio
  205. } // namespace boost
  206. #include <boost/asio/detail/pop_options.hpp>
  207. #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
  208. // || defined(GENERATING_DOCUMENTATION)
  209. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  210. #endif // BOOST_ASIO_SERIAL_PORT_SERVICE_HPP