serial_port.hpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Copyright (c) 2024 Klemens Morgenstern (klemens.morgenstern@gmx.net)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_COBALT_IO_SERIAL_PORT_HPP
  8. #define BOOST_COBALT_IO_SERIAL_PORT_HPP
  9. #include <boost/cobalt/io/detail/config.hpp>
  10. #include <boost/cobalt/op.hpp>
  11. #include <boost/cobalt/io/ops.hpp>
  12. #include <boost/cobalt/io/buffer.hpp>
  13. #include <boost/cobalt/io/stream.hpp>
  14. #include <boost/asio/basic_serial_port.hpp>
  15. #include <boost/core/detail/string_view.hpp>
  16. #include <boost/system/result.hpp>
  17. namespace boost::cobalt::io
  18. {
  19. struct BOOST_SYMBOL_VISIBLE serial_port final : stream
  20. {
  21. BOOST_COBALT_IO_DECL system::result<void> close();
  22. BOOST_COBALT_IO_DECL system::result<void> cancel();
  23. BOOST_COBALT_IO_DECL bool is_open() const;
  24. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> send_break();
  25. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_baud_rate(unsigned rate);
  26. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<unsigned> get_baud_rate();
  27. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_character_size(unsigned rate);
  28. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<unsigned> get_character_size();
  29. using flow_control = asio::serial_port_base::flow_control::type;
  30. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_flow_control(flow_control rate);
  31. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<flow_control> get_flow_control();
  32. using parity = asio::serial_port_base::parity::type;
  33. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_parity(parity rate);
  34. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<parity> get_parity();
  35. using native_handle_type = typename asio::basic_serial_port<executor>::native_handle_type;
  36. native_handle_type native_handle() {return serial_port_.native_handle();}
  37. BOOST_COBALT_IO_DECL serial_port(const cobalt::executor & executor = this_thread::get_executor());
  38. BOOST_COBALT_IO_DECL serial_port(serial_port && lhs) = default;
  39. BOOST_COBALT_IO_DECL serial_port(std::string_view device, const cobalt::executor & executor = this_thread::get_executor());
  40. BOOST_COBALT_IO_DECL serial_port(native_handle_type native_handle, const cobalt::executor & executor = this_thread::get_executor());
  41. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> assign(native_handle_type native_handle);
  42. [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> open(std::string_view device);
  43. [[nodiscard]] write_op write_some(const_buffer_sequence buffer)
  44. {
  45. return {buffer, this, initiate_write_some_};
  46. }
  47. [[nodiscard]] read_op read_some(mutable_buffer_sequence buffer)
  48. {
  49. return {buffer, this, initiate_read_some_};
  50. }
  51. private:
  52. BOOST_COBALT_IO_DECL static void initiate_read_some_(void *, mutable_buffer_sequence, boost::cobalt::completion_handler<system::error_code, std::size_t>);
  53. BOOST_COBALT_IO_DECL static void initiate_write_some_(void *, const_buffer_sequence, boost::cobalt::completion_handler<system::error_code, std::size_t>);
  54. asio::basic_serial_port<executor> serial_port_;
  55. };
  56. }
  57. #endif //BOOST_COBALT_IO_SERIAL_PORT_HPP