unilink  0.4.3
A simple C++ library for unified async communication
serial_builder.cc
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 
18 
19 #include <boost/asio/io_context.hpp>
20 
22 
23 namespace unilink {
24 namespace builder {
25 
26 SerialBuilder::SerialBuilder(const std::string& device, uint32_t baud_rate)
27  : device_(device),
28  baud_rate_(baud_rate),
29  auto_manage_(false),
30  use_independent_context_(false),
31  data_bits_(8),
32  stop_bits_(1),
33  parity_("none"),
34  flow_control_("none"),
35  retry_interval_(3000) {
36  // Ensure background IO service is running
38 }
39 
40 std::unique_ptr<wrapper::Serial> SerialBuilder::build() {
41  std::unique_ptr<wrapper::Serial> serial;
42  if (use_independent_context_) {
43  serial = std::make_unique<wrapper::Serial>(device_, baud_rate_, std::make_shared<boost::asio::io_context>());
44  serial->set_manage_external_context(true);
45  } else {
46  serial = std::make_unique<wrapper::Serial>(device_, baud_rate_);
47  }
48 
49  if (on_data_) serial->on_data(on_data_);
50  if (on_connect_) serial->on_connect(on_connect_);
51  if (on_disconnect_) serial->on_disconnect(on_disconnect_);
52  if (on_error_) serial->on_error(on_error_);
53 
54  serial->set_data_bits(data_bits_);
55  serial->set_stop_bits(stop_bits_);
56  serial->set_parity(parity_);
57  serial->set_flow_control(flow_control_);
58  serial->set_retry_interval(retry_interval_);
59 
60  if (auto_manage_) {
61  serial->auto_manage(true);
62  }
63 
64  return serial;
65 }
66 
68  auto_manage_ = auto_manage;
69  return *this;
70 }
71 
72 SerialBuilder& SerialBuilder::on_data(std::function<void(const wrapper::MessageContext&)> handler) {
73  on_data_ = std::move(handler);
74  return *this;
75 }
76 
78  on_connect_ = std::move(handler);
79  return *this;
80 }
81 
83  on_disconnect_ = std::move(handler);
84  return *this;
85 }
86 
87 SerialBuilder& SerialBuilder::on_error(std::function<void(const wrapper::ErrorContext&)> handler) {
88  on_error_ = std::move(handler);
89  return *this;
90 }
91 
93  data_bits_ = bits;
94  return *this;
95 }
96 
98  stop_bits_ = bits;
99  return *this;
100 }
101 
102 SerialBuilder& SerialBuilder::parity(const std::string& p) {
103  parity_ = p;
104  return *this;
105 }
106 
107 SerialBuilder& SerialBuilder::flow_control(const std::string& flow) {
108  flow_control_ = flow;
109  return *this;
110 }
111 
113  retry_interval_ = std::chrono::milliseconds(milliseconds);
114  return *this;
115 }
116 
118  use_independent_context_ = use_independent;
119  return *this;
120 }
121 
122 } // namespace builder
123 } // namespace unilink