unilink  0.4.3
A simple C++ library for unified async communication
boost_serial_port.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Jinwoo Sung
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
21 
22 namespace unilink {
23 namespace transport {
24 
25 namespace net = boost::asio;
26 
28  public:
29  explicit BoostSerialPort(net::io_context& ioc) : port_(ioc) {}
30 
31  void open(const std::string& device, boost::system::error_code& ec) override { port_.open(device, ec); }
32  bool is_open() const override { return port_.is_open(); }
33  void close(boost::system::error_code& ec) override { port_.close(ec); }
34 
35  void set_option(const net::serial_port_base::baud_rate& option, boost::system::error_code& ec) override {
36  port_.set_option(option, ec);
37  }
38  void set_option(const net::serial_port_base::character_size& option, boost::system::error_code& ec) override {
39  port_.set_option(option, ec);
40  }
41  void set_option(const net::serial_port_base::stop_bits& option, boost::system::error_code& ec) override {
42  port_.set_option(option, ec);
43  }
44  void set_option(const net::serial_port_base::parity& option, boost::system::error_code& ec) override {
45  port_.set_option(option, ec);
46  }
47  void set_option(const net::serial_port_base::flow_control& option, boost::system::error_code& ec) override {
48  port_.set_option(option, ec);
49  }
50 
51  void async_read_some(const net::mutable_buffer& buffer,
52  std::function<void(const boost::system::error_code&, std::size_t)> handler) override {
53  port_.async_read_some(buffer, std::move(handler));
54  }
55 
56  void async_write(const net::const_buffer& buffer,
57  std::function<void(const boost::system::error_code&, std::size_t)> handler) override {
58  net::async_write(port_, buffer, std::move(handler));
59  }
60 
61  private:
62  net::serial_port port_;
63 };
64 
65 } // namespace transport
66 } // namespace unilink
#define UNILINK_API
Definition: visibility.hpp:37