posix_serial_port_service.ipp 4.5 KB

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