posix_serial_port_service.hpp 8.6 KB

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