posix_serial_port_service.ipp 4.3 KB

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