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 <memory>
20 : : #include <optional>
21 : : #include <string>
22 : : #include <vector>
23 : :
24 : : #include "wirestead/base/visibility.hpp"
25 : : #include "wirestead/config/serial_config.hpp"
26 : : #include "wirestead/diagnostics/error_types.hpp"
27 : : #include "wirestead/interface/channel.hpp"
28 : :
29 : : namespace boost {
30 : : namespace asio {
31 : : class io_context;
32 : : }
33 : : } // namespace boost
34 : :
35 : : namespace wirestead {
36 : :
37 : : namespace interface {
38 : : class SerialPortInterface;
39 : : }
40 : :
41 : : namespace transport {
42 : :
43 : : /**
44 : : * @brief Serial Transport implementation
45 : : */
46 : : class WIRESTEAD_API Serial : public interface::Channel, public std::enable_shared_from_this<Serial> {
47 : : public:
48 : : // use_shared_context: opt into the shared IoContextManager singleton
49 : : // instead of the default dedicated io_context + thread. Only meaningful
50 : : // for multi-instance-per-process deployments deliberately trading
51 : : // parallelism for reduced thread/memory overhead (#440); most callers
52 : : // should leave this false.
53 : : static std::shared_ptr<Serial> create(const config::SerialConfig& cfg, bool use_shared_context = false);
54 : : static std::shared_ptr<Serial> create(const config::SerialConfig& cfg, boost::asio::io_context& ioc);
55 : : static std::shared_ptr<Serial> create(const config::SerialConfig& cfg,
56 : : std::unique_ptr<interface::SerialPortInterface> port,
57 : : boost::asio::io_context& ioc);
58 : : ~Serial() override;
59 : :
60 : : // Move semantics
61 : : Serial(Serial&&) noexcept;
62 : : Serial& operator=(Serial&&) noexcept;
63 : :
64 : : // Non-copyable
65 : : Serial(const Serial&) = delete;
66 : : Serial& operator=(const Serial&) = delete;
67 : :
68 : : // Channel implementation
69 : : void start() override;
70 : : void stop() override;
71 : : bool is_connected() const override;
72 : : bool is_backpressure_active() const override;
73 : : wrapper::RuntimeStats stats() const override;
74 : : void reset_stats() override;
75 : : std::optional<diagnostics::ErrorInfo> last_error_info() const override;
76 : : boost::asio::any_io_executor get_executor() override;
77 : :
78 : : bool async_write_copy(memory::ConstByteSpan data) override;
79 : : bool async_write_move(std::vector<uint8_t>&& data) override;
80 : : bool async_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
81 : : bool async_try_write_copy(memory::ConstByteSpan data) override;
82 : : bool async_try_write_move(std::vector<uint8_t>&& data) override;
83 : : bool async_try_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
84 : :
85 : : void on_bytes(OnBytes cb) override;
86 : : void on_state(OnState cb) override;
87 : : void on_backpressure(OnBackpressure cb) override;
88 : :
89 : : void set_backpressure_strategy(base::constants::BackpressureStrategy strategy);
90 : : void set_retry_interval(unsigned interval_ms);
91 : :
92 : : private:
93 : : explicit Serial(const config::SerialConfig& cfg, bool use_shared_context);
94 : : Serial(const config::SerialConfig& cfg, std::unique_ptr<interface::SerialPortInterface> port,
95 : : boost::asio::io_context& ioc);
96 : :
97 : : struct Impl;
98 : 51 : const Impl* get_impl() const { return impl_.get(); }
99 : 285 : Impl* get_impl() { return impl_.get(); }
100 : : std::unique_ptr<Impl> impl_;
101 : : };
102 : : } // namespace transport
103 : : } // namespace wirestead
|