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