2 // detail/reactive_serial_port_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2014 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_service.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
41 // The native type of a serial port.
42 typedef reactive_descriptor_service::native_handle_type native_handle_type;
44 // The implementation type of the serial port.
45 typedef reactive_descriptor_service::implementation_type implementation_type;
47 BOOST_ASIO_DECL reactive_serial_port_service(
48 boost::asio::io_service& io_service);
50 // Destroy all user-defined handler objects owned by the service.
51 BOOST_ASIO_DECL void shutdown_service();
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>
160 void async_write_some(implementation_type& impl,
161 const ConstBufferSequence& buffers, Handler& handler)
163 descriptor_service_.async_write_some(impl, buffers, handler);
166 // Read some data. Returns the number of bytes received.
167 template <typename MutableBufferSequence>
168 size_t read_some(implementation_type& impl,
169 const MutableBufferSequence& buffers, boost::system::error_code& ec)
171 return descriptor_service_.read_some(impl, buffers, ec);
174 // Start an asynchronous read. The buffer for the data being received must be
175 // valid for the lifetime of the asynchronous operation.
176 template <typename MutableBufferSequence, typename Handler>
177 void async_read_some(implementation_type& impl,
178 const MutableBufferSequence& buffers, Handler& handler)
180 descriptor_service_.async_read_some(impl, buffers, handler);
184 // Function pointer type for storing a serial port option.
185 typedef boost::system::error_code (*store_function_type)(
186 const void*, termios&, boost::system::error_code&);
188 // Helper function template to store a serial port option.
189 template <typename SettableSerialPortOption>
190 static boost::system::error_code store_option(const void* option,
191 termios& storage, boost::system::error_code& ec)
193 return static_cast<const SettableSerialPortOption*>(option)->store(
197 // Helper function to set a serial port option.
198 BOOST_ASIO_DECL boost::system::error_code do_set_option(
199 implementation_type& impl, store_function_type store,
200 const void* option, boost::system::error_code& ec);
202 // Function pointer type for loading a serial port option.
203 typedef boost::system::error_code (*load_function_type)(
204 void*, const termios&, boost::system::error_code&);
206 // Helper function template to load a serial port option.
207 template <typename GettableSerialPortOption>
208 static boost::system::error_code load_option(void* option,
209 const termios& storage, boost::system::error_code& ec)
211 return static_cast<GettableSerialPortOption*>(option)->load(storage, ec);
214 // Helper function to get a serial port option.
215 BOOST_ASIO_DECL boost::system::error_code do_get_option(
216 const implementation_type& impl, load_function_type load,
217 void* option, boost::system::error_code& ec) const;
219 // The implementation used for initiating asynchronous operations.
220 reactive_descriptor_service descriptor_service_;
223 } // namespace detail
227 #include <boost/asio/detail/pop_options.hpp>
229 #if defined(BOOST_ASIO_HEADER_ONLY)
230 # include <boost/asio/detail/impl/reactive_serial_port_service.ipp>
231 #endif // defined(BOOST_ASIO_HEADER_ONLY)
233 #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
234 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
236 #endif // BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP