reactive_serial_port_service.ipp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // detail/impl/reactive_serial_port_service.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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. #ifndef BOOST_ASIO_DETAIL_IMPL_REACTIVE_SERIAL_PORT_SERVICE_IPP
  12. #define BOOST_ASIO_DETAIL_IMPL_REACTIVE_SERIAL_PORT_SERVICE_IPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_SERIAL_PORT)
  18. #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  19. #include <cstring>
  20. #include <boost/asio/detail/reactive_serial_port_service.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. reactive_serial_port_service::reactive_serial_port_service(
  26. boost::asio::io_context& io_context)
  27. : service_base<reactive_serial_port_service>(io_context),
  28. descriptor_service_(io_context)
  29. {
  30. }
  31. void reactive_serial_port_service::shutdown()
  32. {
  33. descriptor_service_.shutdown();
  34. }
  35. boost::system::error_code reactive_serial_port_service::open(
  36. reactive_serial_port_service::implementation_type& impl,
  37. const std::string& device, boost::system::error_code& ec)
  38. {
  39. if (is_open(impl))
  40. {
  41. ec = boost::asio::error::already_open;
  42. return ec;
  43. }
  44. descriptor_ops::state_type state = 0;
  45. int fd = descriptor_ops::open(device.c_str(),
  46. O_RDWR | O_NONBLOCK | O_NOCTTY, ec);
  47. if (fd < 0)
  48. return ec;
  49. int s = descriptor_ops::fcntl(fd, F_GETFL, ec);
  50. if (s >= 0)
  51. s = descriptor_ops::fcntl(fd, F_SETFL, s | O_NONBLOCK, ec);
  52. if (s < 0)
  53. {
  54. boost::system::error_code ignored_ec;
  55. descriptor_ops::close(fd, state, ignored_ec);
  56. return ec;
  57. }
  58. // Set up default serial port options.
  59. termios ios;
  60. errno = 0;
  61. s = descriptor_ops::error_wrapper(::tcgetattr(fd, &ios), ec);
  62. if (s >= 0)
  63. {
  64. #if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
  65. ::cfmakeraw(&ios);
  66. #else
  67. ios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK
  68. | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  69. ios.c_oflag &= ~OPOST;
  70. ios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  71. ios.c_cflag &= ~(CSIZE | PARENB);
  72. ios.c_cflag |= CS8;
  73. #endif
  74. ios.c_iflag |= IGNPAR;
  75. ios.c_cflag |= CREAD | CLOCAL;
  76. errno = 0;
  77. s = descriptor_ops::error_wrapper(::tcsetattr(fd, TCSANOW, &ios), ec);
  78. }
  79. if (s < 0)
  80. {
  81. boost::system::error_code ignored_ec;
  82. descriptor_ops::close(fd, state, ignored_ec);
  83. return ec;
  84. }
  85. // We're done. Take ownership of the serial port descriptor.
  86. if (descriptor_service_.assign(impl, fd, ec))
  87. {
  88. boost::system::error_code ignored_ec;
  89. descriptor_ops::close(fd, state, ignored_ec);
  90. }
  91. return ec;
  92. }
  93. boost::system::error_code reactive_serial_port_service::do_set_option(
  94. reactive_serial_port_service::implementation_type& impl,
  95. reactive_serial_port_service::store_function_type store,
  96. const void* option, boost::system::error_code& ec)
  97. {
  98. termios ios;
  99. errno = 0;
  100. descriptor_ops::error_wrapper(::tcgetattr(
  101. descriptor_service_.native_handle(impl), &ios), ec);
  102. if (ec)
  103. return ec;
  104. if (store(option, ios, ec))
  105. return ec;
  106. errno = 0;
  107. descriptor_ops::error_wrapper(::tcsetattr(
  108. descriptor_service_.native_handle(impl), TCSANOW, &ios), ec);
  109. return ec;
  110. }
  111. boost::system::error_code reactive_serial_port_service::do_get_option(
  112. const reactive_serial_port_service::implementation_type& impl,
  113. reactive_serial_port_service::load_function_type load,
  114. void* option, boost::system::error_code& ec) const
  115. {
  116. termios ios;
  117. errno = 0;
  118. descriptor_ops::error_wrapper(::tcgetattr(
  119. descriptor_service_.native_handle(impl), &ios), ec);
  120. if (ec)
  121. return ec;
  122. return load(option, ios, ec);
  123. }
  124. } // namespace detail
  125. } // namespace asio
  126. } // namespace boost
  127. #include <boost/asio/detail/pop_options.hpp>
  128. #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  129. #endif // defined(BOOST_ASIO_HAS_SERIAL_PORT)
  130. #endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_SERIAL_PORT_SERVICE_IPP