2 // detail/reactive_serial_port_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
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)
12 #ifndef BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP
13 #define BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if defined(BOOST_ASIO_HAS_SERIAL_PORT)
22 #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
25 #include <boost/asio/error.hpp>
26 #include <boost/asio/io_context.hpp>
27 #include <boost/asio/serial_port_base.hpp>
28 #include <boost/asio/detail/descriptor_ops.hpp>
29 #include <boost/asio/detail/reactive_descriptor_service.hpp>
31 #include <boost/asio/detail/push_options.hpp>
37 // Extend reactive_descriptor_service to provide serial port support.
38 class reactive_serial_port_service :
39 public service_base<reactive_serial_port_service>
42 // The native type of a serial port.
43 typedef reactive_descriptor_service::native_handle_type native_handle_type;
45 // The implementation type of the serial port.
46 typedef reactive_descriptor_service::implementation_type implementation_type;
48 BOOST_ASIO_DECL reactive_serial_port_service(
49 boost::asio::io_context& io_context);
51 // Destroy all user-defined handler objects owned by the service.
52 BOOST_ASIO_DECL void shutdown();
54 // Construct a new serial port implementation.
55 void construct(implementation_type& impl)
57 descriptor_service_.construct(impl);
60 // Move-construct a new serial port implementation.
61 void move_construct(implementation_type& impl,
62 implementation_type& other_impl)
64 descriptor_service_.move_construct(impl, other_impl);
67 // Move-assign from another serial port implementation.
68 void move_assign(implementation_type& impl,
69 reactive_serial_port_service& other_service,
70 implementation_type& other_impl)
72 descriptor_service_.move_assign(impl,
73 other_service.descriptor_service_, other_impl);
76 // Destroy a serial port implementation.
77 void destroy(implementation_type& impl)
79 descriptor_service_.destroy(impl);
82 // Open the serial port using the specified device name.
83 BOOST_ASIO_DECL boost::system::error_code open(implementation_type& impl,
84 const std::string& device, boost::system::error_code& ec);
86 // Assign a native descriptor to a serial port implementation.
87 boost::system::error_code assign(implementation_type& impl,
88 const native_handle_type& native_descriptor,
89 boost::system::error_code& ec)
91 return descriptor_service_.assign(impl, native_descriptor, ec);
94 // Determine whether the serial port is open.
95 bool is_open(const implementation_type& impl) const
97 return descriptor_service_.is_open(impl);
100 // Destroy a serial port implementation.
101 boost::system::error_code close(implementation_type& impl,
102 boost::system::error_code& ec)
104 return descriptor_service_.close(impl, ec);
107 // Get the native serial port representation.
108 native_handle_type native_handle(implementation_type& impl)
110 return descriptor_service_.native_handle(impl);
113 // Cancel all operations associated with the serial port.
114 boost::system::error_code cancel(implementation_type& impl,
115 boost::system::error_code& ec)
117 return descriptor_service_.cancel(impl, ec);
120 // Set an option on the serial port.
121 template <typename SettableSerialPortOption>
122 boost::system::error_code set_option(implementation_type& impl,
123 const SettableSerialPortOption& option, boost::system::error_code& ec)
125 return do_set_option(impl,
126 &reactive_serial_port_service::store_option<SettableSerialPortOption>,
130 // Get an option from the serial port.
131 template <typename GettableSerialPortOption>
132 boost::system::error_code get_option(const implementation_type& impl,
133 GettableSerialPortOption& option, boost::system::error_code& ec) const
135 return do_get_option(impl,
136 &reactive_serial_port_service::load_option<GettableSerialPortOption>,
140 // Send a break sequence to the serial port.
141 boost::system::error_code send_break(implementation_type& impl,
142 boost::system::error_code& ec)
145 descriptor_ops::error_wrapper(::tcsendbreak(
146 descriptor_service_.native_handle(impl), 0), ec);
150 // Write the given data. Returns the number of bytes sent.
151 template <typename ConstBufferSequence>
152 size_t write_some(implementation_type& impl,
153 const ConstBufferSequence& buffers, boost::system::error_code& ec)
155 return descriptor_service_.write_some(impl, buffers, ec);
158 // Start an asynchronous write. The data being written must be valid for the
159 // lifetime of the asynchronous operation.
160 template <typename ConstBufferSequence, typename Handler>
161 void async_write_some(implementation_type& impl,
162 const ConstBufferSequence& buffers, Handler& handler)
164 descriptor_service_.async_write_some(impl, buffers, handler);
167 // Read some data. Returns the number of bytes received.
168 template <typename MutableBufferSequence>
169 size_t read_some(implementation_type& impl,
170 const MutableBufferSequence& buffers, boost::system::error_code& ec)
172 return descriptor_service_.read_some(impl, buffers, ec);
175 // Start an asynchronous read. The buffer for the data being received must be
176 // valid for the lifetime of the asynchronous operation.
177 template <typename MutableBufferSequence, typename Handler>
178 void async_read_some(implementation_type& impl,
179 const MutableBufferSequence& buffers, Handler& handler)
181 descriptor_service_.async_read_some(impl, buffers, handler);
185 // Function pointer type for storing a serial port option.
186 typedef boost::system::error_code (*store_function_type)(
187 const void*, termios&, boost::system::error_code&);
189 // Helper function template to store a serial port option.
190 template <typename SettableSerialPortOption>
191 static boost::system::error_code store_option(const void* option,
192 termios& storage, boost::system::error_code& ec)
194 static_cast<const SettableSerialPortOption*>(option)->store(storage, ec);
198 // Helper function to set a serial port option.
199 BOOST_ASIO_DECL boost::system::error_code do_set_option(
200 implementation_type& impl, store_function_type store,
201 const void* option, boost::system::error_code& ec);
203 // Function pointer type for loading a serial port option.
204 typedef boost::system::error_code (*load_function_type)(
205 void*, const termios&, boost::system::error_code&);
207 // Helper function template to load a serial port option.
208 template <typename GettableSerialPortOption>
209 static boost::system::error_code load_option(void* option,
210 const termios& storage, boost::system::error_code& ec)
212 static_cast<GettableSerialPortOption*>(option)->load(storage, ec);
216 // Helper function to get a serial port option.
217 BOOST_ASIO_DECL boost::system::error_code do_get_option(
218 const implementation_type& impl, load_function_type load,
219 void* option, boost::system::error_code& ec) const;
221 // The implementation used for initiating asynchronous operations.
222 reactive_descriptor_service descriptor_service_;
225 } // namespace detail
229 #include <boost/asio/detail/pop_options.hpp>
231 #if defined(BOOST_ASIO_HEADER_ONLY)
232 # include <boost/asio/detail/impl/reactive_serial_port_service.ipp>
233 #endif // defined(BOOST_ASIO_HEADER_ONLY)
235 #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
236 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
238 #endif // BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP