2 // serial_port_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_SERIAL_PORT_SERVICE_HPP
12 #define BOOST_ASIO_SERIAL_PORT_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
22 #include <boost/config.hpp>
24 #include <boost/asio/detail/pop_options.hpp>
26 #include <boost/asio/error.hpp>
27 #include <boost/asio/io_service.hpp>
28 #include <boost/asio/serial_port_base.hpp>
29 #include <boost/asio/detail/service_base.hpp>
30 #include <boost/asio/detail/reactive_serial_port_service.hpp>
31 #include <boost/asio/detail/win_iocp_serial_port_service.hpp>
33 #if defined(BOOST_ASIO_HAS_SERIAL_PORT) \
34 || defined(GENERATING_DOCUMENTATION)
39 /// Default service implementation for a serial port.
40 class serial_port_service
41 #if defined(GENERATING_DOCUMENTATION)
42 : public boost::asio::io_service::service
44 : public boost::asio::detail::service_base
<serial_port_service
>
48 #if defined(GENERATING_DOCUMENTATION)
49 /// The unique service identifier.
50 static boost::asio::io_service::id id
;
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 #elif defined(BOOST_ASIO_HAS_EPOLL)
58 typedef detail::reactive_serial_port_service
<
59 detail::epoll_reactor
<false> > service_impl_type
;
60 #elif defined(BOOST_ASIO_HAS_KQUEUE)
61 typedef detail::reactive_serial_port_service
<
62 detail::kqueue_reactor
<false> > service_impl_type
;
63 #elif defined(BOOST_ASIO_HAS_DEV_POLL)
64 typedef detail::reactive_serial_port_service
<
65 detail::dev_poll_reactor
<false> > service_impl_type
;
67 typedef detail::reactive_serial_port_service
<
68 detail::select_reactor
<false> > service_impl_type
;
72 /// The type of a serial port implementation.
73 #if defined(GENERATING_DOCUMENTATION)
74 typedef implementation_defined implementation_type
;
76 typedef service_impl_type::implementation_type implementation_type
;
79 /// The native handle type.
80 #if defined(GENERATING_DOCUMENTATION)
81 typedef implementation_defined native_type
;
83 typedef service_impl_type::native_type native_type
;
86 /// Construct a new serial port service for the specified io_service.
87 explicit serial_port_service(boost::asio::io_service
& io_service
)
88 : boost::asio::detail::service_base
<serial_port_service
>(io_service
),
89 service_impl_(boost::asio::use_service
<service_impl_type
>(io_service
))
93 /// Destroy all user-defined handler objects owned by the service.
94 void shutdown_service()
98 /// Construct a new serial port implementation.
99 void construct(implementation_type
& impl
)
101 service_impl_
.construct(impl
);
104 /// Destroy a serial port implementation.
105 void destroy(implementation_type
& impl
)
107 service_impl_
.destroy(impl
);
110 /// Open a serial port.
111 boost::system::error_code
open(implementation_type
& impl
,
112 const std::string
& device
, boost::system::error_code
& ec
)
114 return service_impl_
.open(impl
, device
, ec
);
117 /// Assign an existing native handle to a serial port.
118 boost::system::error_code
assign(implementation_type
& impl
,
119 const native_type
& native_handle
, boost::system::error_code
& ec
)
121 return service_impl_
.assign(impl
, native_handle
, ec
);
124 /// Determine whether the handle is open.
125 bool is_open(const implementation_type
& impl
) const
127 return service_impl_
.is_open(impl
);
130 /// Close a serial port implementation.
131 boost::system::error_code
close(implementation_type
& impl
,
132 boost::system::error_code
& ec
)
134 return service_impl_
.close(impl
, ec
);
137 /// Get the native handle implementation.
138 native_type
native(implementation_type
& impl
)
140 return service_impl_
.native(impl
);
143 /// Cancel all asynchronous operations associated with the handle.
144 boost::system::error_code
cancel(implementation_type
& impl
,
145 boost::system::error_code
& ec
)
147 return service_impl_
.cancel(impl
, ec
);
150 /// Set a serial port option.
151 template <typename SettableSerialPortOption
>
152 boost::system::error_code
set_option(implementation_type
& impl
,
153 const SettableSerialPortOption
& option
, boost::system::error_code
& ec
)
155 return service_impl_
.set_option(impl
, option
, ec
);
158 /// Get a serial port option.
159 template <typename GettableSerialPortOption
>
160 boost::system::error_code
get_option(const implementation_type
& impl
,
161 GettableSerialPortOption
& option
, boost::system::error_code
& ec
) const
163 return service_impl_
.get_option(impl
, option
, ec
);
166 /// Send a break sequence to the serial port.
167 boost::system::error_code
send_break(implementation_type
& impl
,
168 boost::system::error_code
& ec
)
170 return service_impl_
.send_break(impl
, ec
);
173 /// Write the given data to the stream.
174 template <typename ConstBufferSequence
>
175 std::size_t write_some(implementation_type
& impl
,
176 const ConstBufferSequence
& buffers
, boost::system::error_code
& ec
)
178 return service_impl_
.write_some(impl
, buffers
, ec
);
181 /// Start an asynchronous write.
182 template <typename ConstBufferSequence
, typename WriteHandler
>
183 void async_write_some(implementation_type
& impl
,
184 const ConstBufferSequence
& buffers
, WriteHandler handler
)
186 service_impl_
.async_write_some(impl
, buffers
, handler
);
189 /// Read some data from the stream.
190 template <typename MutableBufferSequence
>
191 std::size_t read_some(implementation_type
& impl
,
192 const MutableBufferSequence
& buffers
, boost::system::error_code
& ec
)
194 return service_impl_
.read_some(impl
, buffers
, ec
);
197 /// Start an asynchronous read.
198 template <typename MutableBufferSequence
, typename ReadHandler
>
199 void async_read_some(implementation_type
& impl
,
200 const MutableBufferSequence
& buffers
, ReadHandler handler
)
202 service_impl_
.async_read_some(impl
, buffers
, handler
);
206 // The service that provides the platform-specific implementation.
207 service_impl_type
& service_impl_
;
213 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
214 // || defined(GENERATING_DOCUMENTATION)
216 #include <boost/asio/detail/pop_options.hpp>
218 #endif // BOOST_ASIO_SERIAL_PORT_SERVICE_HPP