posix_serial_port_service.hpp 8.1 KB

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