]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // serial_port_service.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
11fdf7f2 | 5 | // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | #ifndef BOOST_ASIO_SERIAL_PORT_SERVICE_HPP | |
12 | #define BOOST_ASIO_SERIAL_PORT_SERVICE_HPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | ||
b32b8144 FG |
20 | #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) |
21 | ||
7c673cae FG |
22 | #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \ |
23 | || defined(GENERATING_DOCUMENTATION) | |
24 | ||
25 | #include <cstddef> | |
26 | #include <string> | |
27 | #include <boost/asio/async_result.hpp> | |
28 | #include <boost/asio/detail/reactive_serial_port_service.hpp> | |
29 | #include <boost/asio/detail/win_iocp_serial_port_service.hpp> | |
30 | #include <boost/asio/error.hpp> | |
b32b8144 | 31 | #include <boost/asio/io_context.hpp> |
7c673cae FG |
32 | #include <boost/asio/serial_port_base.hpp> |
33 | ||
34 | #include <boost/asio/detail/push_options.hpp> | |
35 | ||
36 | namespace boost { | |
37 | namespace asio { | |
38 | ||
39 | /// Default service implementation for a serial port. | |
40 | class serial_port_service | |
41 | #if defined(GENERATING_DOCUMENTATION) | |
b32b8144 | 42 | : public boost::asio::io_context::service |
7c673cae FG |
43 | #else |
44 | : public boost::asio::detail::service_base<serial_port_service> | |
45 | #endif | |
46 | { | |
47 | public: | |
48 | #if defined(GENERATING_DOCUMENTATION) | |
49 | /// The unique service identifier. | |
b32b8144 | 50 | static boost::asio::io_context::id id; |
7c673cae FG |
51 | #endif |
52 | ||
53 | private: | |
54 | // The type of the platform-specific implementation. | |
55 | #if defined(BOOST_ASIO_HAS_IOCP) | |
56 | typedef detail::win_iocp_serial_port_service service_impl_type; | |
57 | #else | |
58 | typedef detail::reactive_serial_port_service service_impl_type; | |
59 | #endif | |
60 | ||
61 | public: | |
62 | /// The type of a serial port implementation. | |
63 | #if defined(GENERATING_DOCUMENTATION) | |
64 | typedef implementation_defined implementation_type; | |
65 | #else | |
66 | typedef service_impl_type::implementation_type implementation_type; | |
67 | #endif | |
68 | ||
7c673cae FG |
69 | /// The native handle type. |
70 | #if defined(GENERATING_DOCUMENTATION) | |
71 | typedef implementation_defined native_handle_type; | |
72 | #else | |
73 | typedef service_impl_type::native_handle_type native_handle_type; | |
74 | #endif | |
75 | ||
b32b8144 FG |
76 | /// Construct a new serial port service for the specified io_context. |
77 | explicit serial_port_service(boost::asio::io_context& io_context) | |
78 | : boost::asio::detail::service_base<serial_port_service>(io_context), | |
79 | service_impl_(io_context) | |
7c673cae FG |
80 | { |
81 | } | |
82 | ||
83 | /// Construct a new serial port implementation. | |
84 | void construct(implementation_type& impl) | |
85 | { | |
86 | service_impl_.construct(impl); | |
87 | } | |
88 | ||
89 | #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
90 | /// Move-construct a new serial port implementation. | |
91 | void move_construct(implementation_type& impl, | |
92 | implementation_type& other_impl) | |
93 | { | |
94 | service_impl_.move_construct(impl, other_impl); | |
95 | } | |
96 | ||
97 | /// Move-assign from another serial port implementation. | |
98 | void move_assign(implementation_type& impl, | |
99 | serial_port_service& other_service, | |
100 | implementation_type& other_impl) | |
101 | { | |
102 | service_impl_.move_assign(impl, other_service.service_impl_, other_impl); | |
103 | } | |
104 | #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
105 | ||
106 | /// Destroy a serial port implementation. | |
107 | void destroy(implementation_type& impl) | |
108 | { | |
109 | service_impl_.destroy(impl); | |
110 | } | |
111 | ||
112 | /// Open a serial port. | |
b32b8144 | 113 | BOOST_ASIO_SYNC_OP_VOID open(implementation_type& impl, |
7c673cae FG |
114 | const std::string& device, boost::system::error_code& ec) |
115 | { | |
b32b8144 FG |
116 | service_impl_.open(impl, device, ec); |
117 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
118 | } |
119 | ||
120 | /// Assign an existing native handle to a serial port. | |
b32b8144 | 121 | BOOST_ASIO_SYNC_OP_VOID assign(implementation_type& impl, |
7c673cae FG |
122 | const native_handle_type& handle, boost::system::error_code& ec) |
123 | { | |
b32b8144 FG |
124 | service_impl_.assign(impl, handle, ec); |
125 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
126 | } |
127 | ||
128 | /// Determine whether the handle is open. | |
129 | bool is_open(const implementation_type& impl) const | |
130 | { | |
131 | return service_impl_.is_open(impl); | |
132 | } | |
133 | ||
134 | /// Close a serial port implementation. | |
b32b8144 | 135 | BOOST_ASIO_SYNC_OP_VOID close(implementation_type& impl, |
7c673cae FG |
136 | boost::system::error_code& ec) |
137 | { | |
b32b8144 FG |
138 | service_impl_.close(impl, ec); |
139 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
140 | } |
141 | ||
142 | /// Get the native handle implementation. | |
143 | native_handle_type native_handle(implementation_type& impl) | |
144 | { | |
145 | return service_impl_.native_handle(impl); | |
146 | } | |
147 | ||
148 | /// Cancel all asynchronous operations associated with the handle. | |
b32b8144 | 149 | BOOST_ASIO_SYNC_OP_VOID cancel(implementation_type& impl, |
7c673cae FG |
150 | boost::system::error_code& ec) |
151 | { | |
b32b8144 FG |
152 | service_impl_.cancel(impl, ec); |
153 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
154 | } |
155 | ||
156 | /// Set a serial port option. | |
157 | template <typename SettableSerialPortOption> | |
b32b8144 | 158 | BOOST_ASIO_SYNC_OP_VOID set_option(implementation_type& impl, |
7c673cae FG |
159 | const SettableSerialPortOption& option, boost::system::error_code& ec) |
160 | { | |
b32b8144 FG |
161 | service_impl_.set_option(impl, option, ec); |
162 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
163 | } |
164 | ||
165 | /// Get a serial port option. | |
166 | template <typename GettableSerialPortOption> | |
b32b8144 | 167 | BOOST_ASIO_SYNC_OP_VOID get_option(const implementation_type& impl, |
7c673cae FG |
168 | GettableSerialPortOption& option, boost::system::error_code& ec) const |
169 | { | |
b32b8144 FG |
170 | service_impl_.get_option(impl, option, ec); |
171 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
172 | } |
173 | ||
174 | /// Send a break sequence to the serial port. | |
b32b8144 | 175 | BOOST_ASIO_SYNC_OP_VOID send_break(implementation_type& impl, |
7c673cae FG |
176 | boost::system::error_code& ec) |
177 | { | |
b32b8144 FG |
178 | service_impl_.send_break(impl, ec); |
179 | BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); | |
7c673cae FG |
180 | } |
181 | ||
182 | /// Write the given data to the stream. | |
183 | template <typename ConstBufferSequence> | |
184 | std::size_t write_some(implementation_type& impl, | |
185 | const ConstBufferSequence& buffers, boost::system::error_code& ec) | |
186 | { | |
187 | return service_impl_.write_some(impl, buffers, ec); | |
188 | } | |
189 | ||
190 | /// Start an asynchronous write. | |
191 | template <typename ConstBufferSequence, typename WriteHandler> | |
192 | BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler, | |
193 | void (boost::system::error_code, std::size_t)) | |
194 | async_write_some(implementation_type& impl, | |
195 | const ConstBufferSequence& buffers, | |
196 | BOOST_ASIO_MOVE_ARG(WriteHandler) handler) | |
197 | { | |
b32b8144 FG |
198 | async_completion<WriteHandler, |
199 | void (boost::system::error_code, std::size_t)> init(handler); | |
7c673cae | 200 | |
b32b8144 | 201 | service_impl_.async_write_some(impl, buffers, init.completion_handler); |
7c673cae FG |
202 | |
203 | return init.result.get(); | |
204 | } | |
205 | ||
206 | /// Read some data from the stream. | |
207 | template <typename MutableBufferSequence> | |
208 | std::size_t read_some(implementation_type& impl, | |
209 | const MutableBufferSequence& buffers, boost::system::error_code& ec) | |
210 | { | |
211 | return service_impl_.read_some(impl, buffers, ec); | |
212 | } | |
213 | ||
214 | /// Start an asynchronous read. | |
215 | template <typename MutableBufferSequence, typename ReadHandler> | |
216 | BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler, | |
217 | void (boost::system::error_code, std::size_t)) | |
218 | async_read_some(implementation_type& impl, | |
219 | const MutableBufferSequence& buffers, | |
220 | BOOST_ASIO_MOVE_ARG(ReadHandler) handler) | |
221 | { | |
b32b8144 FG |
222 | async_completion<ReadHandler, |
223 | void (boost::system::error_code, std::size_t)> init(handler); | |
7c673cae | 224 | |
b32b8144 | 225 | service_impl_.async_read_some(impl, buffers, init.completion_handler); |
7c673cae FG |
226 | |
227 | return init.result.get(); | |
228 | } | |
229 | ||
230 | private: | |
231 | // Destroy all user-defined handler objects owned by the service. | |
b32b8144 | 232 | void shutdown() |
7c673cae | 233 | { |
b32b8144 | 234 | service_impl_.shutdown(); |
7c673cae FG |
235 | } |
236 | ||
237 | // The platform-specific implementation. | |
238 | service_impl_type service_impl_; | |
239 | }; | |
240 | ||
241 | } // namespace asio | |
242 | } // namespace boost | |
243 | ||
244 | #include <boost/asio/detail/pop_options.hpp> | |
245 | ||
246 | #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT) | |
247 | // || defined(GENERATING_DOCUMENTATION) | |
248 | ||
b32b8144 FG |
249 | #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) |
250 | ||
7c673cae | 251 | #endif // BOOST_ASIO_SERIAL_PORT_SERVICE_HPP |