]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/reactive_serial_port_service.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / detail / reactive_serial_port_service.hpp
1 //
2 // detail/reactive_serial_port_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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
12 #ifndef BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP
13 #define BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP
14
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 # pragma once
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
19 #include <boost/asio/detail/config.hpp>
20
21 #if defined(BOOST_ASIO_HAS_SERIAL_PORT)
22 #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
23
24 #include <string>
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>
30
31 #include <boost/asio/detail/push_options.hpp>
32
33 namespace boost {
34 namespace asio {
35 namespace detail {
36
37 // Extend reactive_descriptor_service to provide serial port support.
38 class reactive_serial_port_service :
39 public service_base<reactive_serial_port_service>
40 {
41 public:
42 // The native type of a serial port.
43 typedef reactive_descriptor_service::native_handle_type native_handle_type;
44
45 // The implementation type of the serial port.
46 typedef reactive_descriptor_service::implementation_type implementation_type;
47
48 BOOST_ASIO_DECL reactive_serial_port_service(
49 boost::asio::io_context& io_context);
50
51 // Destroy all user-defined handler objects owned by the service.
52 BOOST_ASIO_DECL void shutdown();
53
54 // Construct a new serial port implementation.
55 void construct(implementation_type& impl)
56 {
57 descriptor_service_.construct(impl);
58 }
59
60 // Move-construct a new serial port implementation.
61 void move_construct(implementation_type& impl,
62 implementation_type& other_impl)
63 {
64 descriptor_service_.move_construct(impl, other_impl);
65 }
66
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)
71 {
72 descriptor_service_.move_assign(impl,
73 other_service.descriptor_service_, other_impl);
74 }
75
76 // Destroy a serial port implementation.
77 void destroy(implementation_type& impl)
78 {
79 descriptor_service_.destroy(impl);
80 }
81
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);
85
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)
90 {
91 return descriptor_service_.assign(impl, native_descriptor, ec);
92 }
93
94 // Determine whether the serial port is open.
95 bool is_open(const implementation_type& impl) const
96 {
97 return descriptor_service_.is_open(impl);
98 }
99
100 // Destroy a serial port implementation.
101 boost::system::error_code close(implementation_type& impl,
102 boost::system::error_code& ec)
103 {
104 return descriptor_service_.close(impl, ec);
105 }
106
107 // Get the native serial port representation.
108 native_handle_type native_handle(implementation_type& impl)
109 {
110 return descriptor_service_.native_handle(impl);
111 }
112
113 // Cancel all operations associated with the serial port.
114 boost::system::error_code cancel(implementation_type& impl,
115 boost::system::error_code& ec)
116 {
117 return descriptor_service_.cancel(impl, ec);
118 }
119
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)
124 {
125 return do_set_option(impl,
126 &reactive_serial_port_service::store_option<SettableSerialPortOption>,
127 &option, ec);
128 }
129
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
134 {
135 return do_get_option(impl,
136 &reactive_serial_port_service::load_option<GettableSerialPortOption>,
137 &option, ec);
138 }
139
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)
143 {
144 errno = 0;
145 descriptor_ops::error_wrapper(::tcsendbreak(
146 descriptor_service_.native_handle(impl), 0), ec);
147 return ec;
148 }
149
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)
154 {
155 return descriptor_service_.write_some(impl, buffers, ec);
156 }
157
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)
163 {
164 descriptor_service_.async_write_some(impl, buffers, handler);
165 }
166
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)
171 {
172 return descriptor_service_.read_some(impl, buffers, ec);
173 }
174
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)
180 {
181 descriptor_service_.async_read_some(impl, buffers, handler);
182 }
183
184 private:
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&);
188
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)
193 {
194 static_cast<const SettableSerialPortOption*>(option)->store(storage, ec);
195 return ec;
196 }
197
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);
202
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&);
206
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)
211 {
212 static_cast<GettableSerialPortOption*>(option)->load(storage, ec);
213 return ec;
214 }
215
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;
220
221 // The implementation used for initiating asynchronous operations.
222 reactive_descriptor_service descriptor_service_;
223 };
224
225 } // namespace detail
226 } // namespace asio
227 } // namespace boost
228
229 #include <boost/asio/detail/pop_options.hpp>
230
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)
234
235 #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
236 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
237
238 #endif // BOOST_ASIO_DETAIL_REACTIVE_SERIAL_PORT_SERVICE_HPP