2 // detail/impl/reactive_serial_port_service.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
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)
12 #ifndef BOOST_ASIO_DETAIL_IMPL_REACTIVE_SERIAL_PORT_SERVICE_IPP
13 #define BOOST_ASIO_DETAIL_IMPL_REACTIVE_SERIAL_PORT_SERVICE_IPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/config.hpp>
21 #if defined(BOOST_ASIO_HAS_SERIAL_PORT)
22 #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
25 #include <boost/asio/detail/reactive_serial_port_service.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 reactive_serial_port_service::reactive_serial_port_service(
34 execution_context& context)
35 : execution_context_service_base<reactive_serial_port_service>(context),
36 descriptor_service_(context)
40 void reactive_serial_port_service::shutdown()
42 descriptor_service_.shutdown();
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)
51 ec = boost::asio::error::already_open;
55 descriptor_ops::state_type state = 0;
56 int fd = descriptor_ops::open(device.c_str(),
57 O_RDWR | O_NONBLOCK | O_NOCTTY, ec);
61 int s = descriptor_ops::fcntl(fd, F_GETFL, ec);
63 s = descriptor_ops::fcntl(fd, F_SETFL, s | O_NONBLOCK, ec);
66 boost::system::error_code ignored_ec;
67 descriptor_ops::close(fd, state, ignored_ec);
71 // Set up default serial port options.
74 s = descriptor_ops::error_wrapper(::tcgetattr(fd, &ios), ec);
77 #if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
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);
87 ios.c_iflag |= IGNPAR;
88 ios.c_cflag |= CREAD | CLOCAL;
90 s = descriptor_ops::error_wrapper(::tcsetattr(fd, TCSANOW, &ios), ec);
94 boost::system::error_code ignored_ec;
95 descriptor_ops::close(fd, state, ignored_ec);
99 // We're done. Take ownership of the serial port descriptor.
100 if (descriptor_service_.assign(impl, fd, ec))
102 boost::system::error_code ignored_ec;
103 descriptor_ops::close(fd, state, ignored_ec);
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)
116 descriptor_ops::error_wrapper(::tcgetattr(
117 descriptor_service_.native_handle(impl), &ios), ec);
121 if (store(option, ios, ec))
125 descriptor_ops::error_wrapper(::tcsetattr(
126 descriptor_service_.native_handle(impl), TCSANOW, &ios), ec);
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
137 descriptor_ops::error_wrapper(::tcgetattr(
138 descriptor_service_.native_handle(impl), &ios), ec);
142 return load(option, ios, ec);
145 } // namespace detail
149 #include <boost/asio/detail/pop_options.hpp>
151 #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
152 #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
154 #endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_SERIAL_PORT_SERVICE_IPP