unilink  0.4.3
A simple C++ library for unified async communication
serial.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 <chrono>
20 #include <functional>
21 #include <future>
22 #include <memory>
23 #include <string>
24 #include <string_view>
25 
29 
30 namespace boost {
31 namespace asio {
32 class io_context;
33 }
34 } // namespace boost
35 
36 namespace unilink {
37 namespace interface {
38 class Channel;
39 }
40 
41 namespace wrapper {
42 
47  public:
48  Serial(const std::string& device, uint32_t baud_rate);
49  Serial(const std::string& device, uint32_t baud_rate, std::shared_ptr<boost::asio::io_context> external_ioc);
50  explicit Serial(std::shared_ptr<interface::Channel> channel);
51  ~Serial() override;
52 
53  // Move semantics
54  Serial(Serial&&) noexcept;
55  Serial& operator=(Serial&&) noexcept;
56 
57  // Disable copy
58  Serial(const Serial&) = delete;
59  Serial& operator=(const Serial&) = delete;
60 
61  // ChannelInterface implementation
62  std::future<bool> start() override;
63  void stop() override;
64  void send(std::string_view data) override;
65  void send_line(std::string_view line) override;
66  bool is_connected() const override;
67 
68  ChannelInterface& on_data(MessageHandler handler) override;
69  ChannelInterface& on_connect(ConnectionHandler handler) override;
70  ChannelInterface& on_disconnect(ConnectionHandler handler) override;
71  ChannelInterface& on_error(ErrorHandler handler) override;
72 
73  ChannelInterface& auto_manage(bool manage = true) override;
74 
75  // Serial-specific methods
76  void set_baud_rate(uint32_t baud_rate);
77  void set_data_bits(int data_bits);
78  void set_stop_bits(int stop_bits);
79  void set_parity(const std::string& parity);
80  void set_flow_control(const std::string& flow_control);
81  void set_retry_interval(std::chrono::milliseconds interval);
82  void set_manage_external_context(bool manage);
83 
84  // Expose mapped config for testing/inspection
85  config::SerialConfig build_config() const;
86 
87  private:
88  struct Impl;
89  const Impl* get_impl() const { return impl_.get(); }
90  Impl* get_impl() { return impl_.get(); }
91  std::unique_ptr<Impl> impl_;
92 };
93 
94 } // namespace wrapper
95 } // namespace unilink
Definition: serial.hpp:27
#define UNILINK_API
Definition: visibility.hpp:37