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