Branch data Line data Source code
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 <chrono>
20 : : #include <cstdint>
21 : : #include <string>
22 : :
23 : : #include "wirestead/base/visibility.hpp"
24 : : #include "wirestead/builder/ibuilder.hpp"
25 : : #include "wirestead/wrapper/serial/serial.hpp"
26 : :
27 : : namespace wirestead {
28 : : namespace builder {
29 : :
30 : : #ifdef _MSC_VER
31 : : #pragma warning(push)
32 : : #pragma warning(disable : 4251)
33 : : #endif
34 : :
35 : : /**
36 : : * @brief Modernized Builder for Serial
37 : : */
38 : : class WIRESTEAD_API SerialBuilder : public BuilderInterface<wrapper::Serial, SerialBuilder> {
39 : : public:
40 : : SerialBuilder(const std::string& device, uint32_t baud_rate);
41 : :
42 : : // Delete copy
43 : : SerialBuilder(const SerialBuilder&) = delete;
44 : : SerialBuilder& operator=(const SerialBuilder&) = delete;
45 : :
46 : : std::unique_ptr<wrapper::Serial> build() override;
47 : :
48 : : SerialBuilder& auto_start(bool auto_start = true) override;
49 : : SerialBuilder& char_size(unsigned int size);
50 : 2 : SerialBuilder& data_bits(unsigned int size) { return char_size(size); }
51 : : SerialBuilder& stop_bits(unsigned int bits);
52 : : SerialBuilder& parity(config::SerialConfig::Parity p);
53 : : SerialBuilder& parity(const std::string& p);
54 : : SerialBuilder& flow_control(config::SerialConfig::Flow f);
55 : : SerialBuilder& flow_control(const std::string& f);
56 : : // Bytes each read fills. The TCP and UDS builders call the same knob
57 : : // read_buffer_size(); serial named it read_chunk before those existed.
58 : : SerialBuilder& read_chunk(size_t bytes);
59 : : // Ask the driver to deliver received bytes without waiting out its own
60 : : // buffering timer (Linux ASYNC_LOW_LATENCY). On by default; see
61 : : // SerialConfig::low_latency.
62 : : SerialBuilder& low_latency(bool enable = true);
63 : : // Half-duplex RS-485. Delays are milliseconds; see SerialConfig::Rs485.
64 : : SerialBuilder& rs485(bool rts_on_send = true, bool rx_during_tx = false, unsigned delay_before_ms = 0,
65 : : unsigned delay_after_ms = 0);
66 : : // Assert or clear DTR / RTS at open. Not calling these leaves the driver's
67 : : // default alone, which differs from passing false.
68 : : SerialBuilder& dtr(bool assert_line);
69 : : SerialBuilder& rts(bool assert_line);
70 : : SerialBuilder& reopen_on_error(bool enable = true);
71 : : // Reopen the port when no data has been received for this long. Off by
72 : : // default; see SerialConfig::rx_idle_timeout_ms.
73 : : SerialBuilder& rx_idle_timeout(std::chrono::milliseconds timeout);
74 : : SerialBuilder& retry_interval(std::chrono::milliseconds interval);
75 : : SerialBuilder& independent_context(bool use_independent = true);
76 : : // Opt into the shared IoContextManager singleton instead of the default
77 : : // dedicated io_context + thread (#440). Only meaningful for deliberately
78 : : // trading per-instance parallelism for reduced thread/memory overhead
79 : : // across many instances in one process; most callers should not need this.
80 : : SerialBuilder& shared_context(bool use_shared = true);
81 : :
82 : : private:
83 : : std::string device_;
84 : : uint32_t baud_rate_;
85 : : bool auto_start_;
86 : : bool independent_context_;
87 : : bool shared_context_{false};
88 : :
89 : : uint32_t retry_interval_ms_;
90 : : bool retry_interval_set_{false};
91 : : unsigned int char_size_;
92 : : bool char_size_set_{false};
93 : : unsigned int stop_bits_;
94 : : bool stop_bits_set_{false};
95 : : config::SerialConfig::Parity parity_;
96 : : bool parity_set_{false};
97 : : config::SerialConfig::Flow flow_;
98 : : bool flow_set_{false};
99 : : bool reopen_on_error_;
100 : : bool reopen_on_error_set_{false};
101 : : size_t read_chunk_{base::constants::DEFAULT_READ_BUFFER_SIZE};
102 : : bool read_chunk_set_{false};
103 : : bool low_latency_{true};
104 : : bool low_latency_set_{false};
105 : : config::SerialConfig::Rs485 rs485_{};
106 : : std::optional<bool> dtr_;
107 : : std::optional<bool> rts_;
108 : 22 : std::chrono::milliseconds rx_idle_timeout_{0};
109 : : bool rx_idle_timeout_set_{false};
110 : : };
111 : :
112 : : using SerialBuilderDefault = SerialBuilder;
113 : :
114 : : #ifdef _MSC_VER
115 : : #pragma warning(pop)
116 : : #endif
117 : :
118 : : } // namespace builder
119 : : } // namespace wirestead
|