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 <memory>
20 #include <string>
21 #include <vector>
22 
26 
27 namespace boost {
28 namespace asio {
29 class io_context;
30 }
31 } // namespace boost
32 
33 namespace unilink {
34 
35 namespace interface {
36 class SerialPortInterface;
37 }
38 
39 namespace transport {
40 
44 class UNILINK_API Serial : public interface::Channel, public std::enable_shared_from_this<Serial> {
45  public:
46  static std::shared_ptr<Serial> create(const config::SerialConfig& cfg);
47  static std::shared_ptr<Serial> create(const config::SerialConfig& cfg, boost::asio::io_context& ioc);
48  static std::shared_ptr<Serial> create(const config::SerialConfig& cfg,
49  std::unique_ptr<interface::SerialPortInterface> port,
50  boost::asio::io_context& ioc);
51  ~Serial() override;
52 
53  // Move semantics
54  Serial(Serial&&) noexcept;
55  Serial& operator=(Serial&&) noexcept;
56 
57  // Non-copyable
58  Serial(const Serial&) = delete;
59  Serial& operator=(const Serial&) = delete;
60 
61  // Channel implementation
62  void start() override;
63  void stop() override;
64  bool is_connected() const override;
65 
66  void async_write_copy(memory::ConstByteSpan data) override;
67  void async_write_move(std::vector<uint8_t>&& data) override;
68  void async_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
69 
70  void on_bytes(OnBytes cb) override;
71  void on_state(OnState cb) override;
72  void on_backpressure(OnBackpressure cb) override;
73 
74  void set_retry_interval(unsigned interval_ms);
75 
76  private:
77  explicit Serial(const config::SerialConfig& cfg);
78  Serial(const config::SerialConfig& cfg, std::unique_ptr<interface::SerialPortInterface> port,
79  boost::asio::io_context& ioc);
80 
81  struct Impl;
82  const Impl* get_impl() const { return impl_.get(); }
83  Impl* get_impl() { return impl_.get(); }
84  std::unique_ptr<Impl> impl_;
85 };
86 } // namespace transport
87 } // namespace unilink
Definition: serial.hpp:27
#define UNILINK_API
Definition: visibility.hpp:37