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

source code of boost/boost/asio/detail/reactive_serial_port_service.hpp