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