2 // detail/reactive_serial_port_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 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/execution_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 execution_context_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(execution_context& context);
50 // Destroy all user-defined handler objects owned by the service.
51 BOOST_ASIO_DECL void shutdown();
53 // Construct a new serial port implementation.
54 void construct(implementation_type& impl)
56 descriptor_service_.construct(impl);
59 // Move-construct a new serial port implementation.
60 void move_construct(implementation_type& impl,
61 implementation_type& other_impl)
63 descriptor_service_.move_construct(impl, other_impl);
66 // Move-assign from another serial port implementation.
67 void move_assign(implementation_type& impl,
68 reactive_serial_port_service& other_service,
69 implementation_type& other_impl)
71 descriptor_service_.move_assign(impl,
72 other_service.descriptor_service_, other_impl);
75 // Destroy a serial port implementation.
76 void destroy(implementation_type& impl)
78 descriptor_service_.destroy(impl);
81 // Open the serial port using the specified device name.
82 BOOST_ASIO_DECL boost::system::error_code open(implementation_type& impl,
83 const std::string& device, boost::system::error_code& ec);
85 // Assign a native descriptor to a serial port implementation.
86 boost::system::error_code assign(implementation_type& impl,
87 const native_handle_type& native_descriptor,
88 boost::system::error_code& ec)
90 return descriptor_service_.assign(impl, native_descriptor, ec);
93 // Determine whether the serial port is open.
94 bool is_open(const implementation_type& impl) const
96 return descriptor_service_.is_open(impl);
99 // Destroy a serial port implementation.
100 boost::system::error_code close(implementation_type& impl,
101 boost::system::error_code& ec)
103 return descriptor_service_.close(impl, ec);
106 // Get the native serial port representation.
107 native_handle_type native_handle(implementation_type& impl)
109 return descriptor_service_.native_handle(impl);
112 // Cancel all operations associated with the serial port.
113 boost::system::error_code cancel(implementation_type& impl,
114 boost::system::error_code& ec)
116 return descriptor_service_.cancel(impl, ec);
119 // Set an option on the serial port.
120 template <typename SettableSerialPortOption>
121 boost::system::error_code set_option(implementation_type& impl,
122 const SettableSerialPortOption& option, boost::system::error_code& ec)
124 return do_set_option(impl,
125 &reactive_serial_port_service::store_option<SettableSerialPortOption>,
129 // Get an option from the serial port.
130 template <typename GettableSerialPortOption>
131 boost::system::error_code get_option(const implementation_type& impl,
132 GettableSerialPortOption& option, boost::system::error_code& ec) const
134 return do_get_option(impl,
135 &reactive_serial_port_service::load_option<GettableSerialPortOption>,
139 // Send a break sequence to the serial port.
140 boost::system::error_code send_break(implementation_type& impl,
141 boost::system::error_code& ec)
144 descriptor_ops::error_wrapper(::tcsendbreak(
145 descriptor_service_.native_handle(impl), 0), ec);
149 // Write the given data. Returns the number of bytes sent.
150 template <typename ConstBufferSequence>
151 size_t write_some(implementation_type& impl,
152 const ConstBufferSequence& buffers, boost::system::error_code& ec)
154 return descriptor_service_.write_some(impl, buffers, ec);
157 // Start an asynchronous write. The data being written must be valid for the
158 // lifetime of the asynchronous operation.
159 template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
160 void async_write_some(implementation_type& impl,
161 const ConstBufferSequence& buffers,
162 Handler& handler, const IoExecutor& io_ex)
164 descriptor_service_.async_write_some(impl, buffers, handler, io_ex);
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,
178 typename Handler, typename IoExecutor>
179 void async_read_some(implementation_type& impl,
180 const MutableBufferSequence& buffers,
181 Handler& handler, const IoExecutor& io_ex)
183 descriptor_service_.async_read_some(impl, buffers, handler, io_ex);
187 // Function pointer type for storing a serial port option.
188 typedef boost::system::error_code (*store_function_type)(
189 const void*, termios&, boost::system::error_code&);
191 // Helper function template to store a serial port option.
192 template <typename SettableSerialPortOption>
193 static boost::system::error_code store_option(const void* option,
194 termios& storage, boost::system::error_code& ec)
196 static_cast<const SettableSerialPortOption*>(option)->store(storage, ec);
200 // Helper function to set a serial port option.
201 BOOST_ASIO_DECL boost::system::error_code do_set_option(
202 implementation_type& impl, store_function_type store,
203 const void* option, boost::system::error_code& ec);
205 // Function pointer type for loading a serial port option.
206 typedef boost::system::error_code (*load_function_type)(
207 void*, const termios&, boost::system::error_code&);
209 // Helper function template to load a serial port option.
210 template <typename GettableSerialPortOption>
211 static boost::system::error_code load_option(void* option,
212 const termios& storage, boost::system::error_code& ec)
214 static_cast<GettableSerialPortOption*>(option)->load(storage, ec);
218 // Helper function to get a serial port option.
219 BOOST_ASIO_DECL boost::system::error_code do_get_option(
220 const implementation_type& impl, load_function_type load,
221 void* option, boost::system::error_code& ec) const;
223 // The implementation used for initiating asynchronous operations.
224 reactive_descriptor_service descriptor_service_;
227 } // namespace detail
231 #include <boost/asio/detail/pop_options.hpp>
233 #if defined(BOOST_ASIO_HEADER_ONLY)
234 # include <boost/asio/detail/impl/reactive_serial_port_service.ipp>
235 #endif // defined(BOOST_ASIO_HEADER_ONLY)
237 #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
238 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
240 #endif // BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP