]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/impl/reactive_serial_port_service.ipp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
f67539c2 | 5 | // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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_IMPL_REACTIVE_SERIAL_PORT_SERVICE_IPP | |
13 | #define BOOST_ASIO_DETAIL_IMPL_REACTIVE_SERIAL_PORT_SERVICE_IPP | |
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 <cstring> | |
25 | #include <boost/asio/detail/reactive_serial_port_service.hpp> | |
26 | ||
27 | #include <boost/asio/detail/push_options.hpp> | |
28 | ||
29 | namespace boost { | |
30 | namespace asio { | |
31 | namespace detail { | |
32 | ||
33 | reactive_serial_port_service::reactive_serial_port_service( | |
92f5a8d4 TL |
34 | execution_context& context) |
35 | : execution_context_service_base<reactive_serial_port_service>(context), | |
36 | descriptor_service_(context) | |
7c673cae FG |
37 | { |
38 | } | |
39 | ||
b32b8144 | 40 | void reactive_serial_port_service::shutdown() |
7c673cae | 41 | { |
b32b8144 | 42 | descriptor_service_.shutdown(); |
7c673cae FG |
43 | } |
44 | ||
45 | boost::system::error_code reactive_serial_port_service::open( | |
46 | reactive_serial_port_service::implementation_type& impl, | |
47 | const std::string& device, boost::system::error_code& ec) | |
48 | { | |
49 | if (is_open(impl)) | |
50 | { | |
51 | ec = boost::asio::error::already_open; | |
52 | return ec; | |
53 | } | |
54 | ||
55 | descriptor_ops::state_type state = 0; | |
56 | int fd = descriptor_ops::open(device.c_str(), | |
57 | O_RDWR | O_NONBLOCK | O_NOCTTY, ec); | |
58 | if (fd < 0) | |
59 | return ec; | |
60 | ||
61 | int s = descriptor_ops::fcntl(fd, F_GETFL, ec); | |
62 | if (s >= 0) | |
63 | s = descriptor_ops::fcntl(fd, F_SETFL, s | O_NONBLOCK, ec); | |
64 | if (s < 0) | |
65 | { | |
66 | boost::system::error_code ignored_ec; | |
67 | descriptor_ops::close(fd, state, ignored_ec); | |
68 | return ec; | |
69 | } | |
70 | ||
71 | // Set up default serial port options. | |
72 | termios ios; | |
73 | errno = 0; | |
74 | s = descriptor_ops::error_wrapper(::tcgetattr(fd, &ios), ec); | |
75 | if (s >= 0) | |
76 | { | |
b32b8144 | 77 | #if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE) |
7c673cae FG |
78 | ::cfmakeraw(&ios); |
79 | #else | |
80 | ios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | |
81 | | ISTRIP | INLCR | IGNCR | ICRNL | IXON); | |
82 | ios.c_oflag &= ~OPOST; | |
83 | ios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); | |
84 | ios.c_cflag &= ~(CSIZE | PARENB); | |
85 | ios.c_cflag |= CS8; | |
86 | #endif | |
87 | ios.c_iflag |= IGNPAR; | |
88 | ios.c_cflag |= CREAD | CLOCAL; | |
89 | errno = 0; | |
90 | s = descriptor_ops::error_wrapper(::tcsetattr(fd, TCSANOW, &ios), ec); | |
91 | } | |
92 | if (s < 0) | |
93 | { | |
94 | boost::system::error_code ignored_ec; | |
95 | descriptor_ops::close(fd, state, ignored_ec); | |
96 | return ec; | |
97 | } | |
98 | ||
99 | // We're done. Take ownership of the serial port descriptor. | |
100 | if (descriptor_service_.assign(impl, fd, ec)) | |
101 | { | |
102 | boost::system::error_code ignored_ec; | |
103 | descriptor_ops::close(fd, state, ignored_ec); | |
104 | } | |
105 | ||
106 | return ec; | |
107 | } | |
108 | ||
109 | boost::system::error_code reactive_serial_port_service::do_set_option( | |
110 | reactive_serial_port_service::implementation_type& impl, | |
111 | reactive_serial_port_service::store_function_type store, | |
112 | const void* option, boost::system::error_code& ec) | |
113 | { | |
114 | termios ios; | |
115 | errno = 0; | |
116 | descriptor_ops::error_wrapper(::tcgetattr( | |
117 | descriptor_service_.native_handle(impl), &ios), ec); | |
118 | if (ec) | |
119 | return ec; | |
120 | ||
121 | if (store(option, ios, ec)) | |
122 | return ec; | |
123 | ||
124 | errno = 0; | |
125 | descriptor_ops::error_wrapper(::tcsetattr( | |
126 | descriptor_service_.native_handle(impl), TCSANOW, &ios), ec); | |
127 | return ec; | |
128 | } | |
129 | ||
130 | boost::system::error_code reactive_serial_port_service::do_get_option( | |
131 | const reactive_serial_port_service::implementation_type& impl, | |
132 | reactive_serial_port_service::load_function_type load, | |
133 | void* option, boost::system::error_code& ec) const | |
134 | { | |
135 | termios ios; | |
136 | errno = 0; | |
137 | descriptor_ops::error_wrapper(::tcgetattr( | |
138 | descriptor_service_.native_handle(impl), &ios), ec); | |
139 | if (ec) | |
140 | return ec; | |
141 | ||
142 | return load(option, ios, ec); | |
143 | } | |
144 | ||
145 | } // namespace detail | |
146 | } // namespace asio | |
147 | } // namespace boost | |
148 | ||
149 | #include <boost/asio/detail/pop_options.hpp> | |
150 | ||
151 | #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__) | |
152 | #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT) | |
153 | ||
154 | #endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_SERIAL_PORT_SERVICE_IPP |