unilink  0.4.3
A simple C++ library for unified async communication
serial_config.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 
19 #include <string>
20 
22 
23 namespace unilink {
24 namespace config {
25 
26 struct SerialConfig {
27 #ifdef _WIN32
28  std::string device = "COM1";
29 #else
30  std::string device = "/dev/ttyUSB0";
31 #endif
32  unsigned baud_rate = 115200;
33  unsigned char_size = 8; // 5,6,7,8
34  enum class Parity { None, Even, Odd } parity = Parity::None;
35  unsigned stop_bits = 1; // 1 or 2
36  enum class Flow { None, Software, Hardware } flow = Flow::None;
37 
39  bool reopen_on_error = true; // Attempt to reopen on device disconnection/error
41  bool enable_memory_pool = true;
42  // Controls whether callback exceptions halt the link (true) or trigger the normal retry flow (false)
44 
47 
48  // Validation methods
49  bool is_valid() const {
50  return !device.empty() && baud_rate > 0 && char_size >= 5 && char_size <= 8 && (stop_bits == 1 || stop_bits == 2) &&
56  }
57 
58  // Apply validation and clamp values to valid ranges
60  if (char_size < 5)
61  char_size = 5;
62  else if (char_size > 8)
63  char_size = 8;
64 
65  if (stop_bits != 1 && stop_bits != 2) stop_bits = 1;
66 
71  }
72 
77  }
78 
81  }
82  }
83 };
84 
85 } // namespace config
86 } // namespace unilink