oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
uart.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#ifdef CONFIG_OVE_UART
17
18#include <ove/uart.h>
19#include <ove/types.hpp>
20#include <ove/error.hpp>
21
22namespace ove
23{
24
35template <size_t RxBufSize = 0> class Uart
36{
37 public:
42 explicit Uart(const struct ove_uart_cfg &cfg)
43 requires(RxBufSize > 0)
44 {
45#ifdef CONFIG_OVE_ZERO_HEAP
46 static_assert(RxBufSize > 0, "RxBufSize must be > 0 in zero-heap mode");
47 int err = ove_uart_init(&handle_, &storage_, rx_buf_, &cfg);
48#else
49 int err = ove_uart_create(&handle_, &cfg);
50#endif
51 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
52 }
53
54 ~Uart() noexcept
55 {
56 if (!handle_)
57 return;
58#ifdef CONFIG_OVE_ZERO_HEAP
59 ove_uart_deinit(handle_);
60#else
61 ove_uart_destroy(handle_);
62#endif
63 }
64
65 Uart(const Uart &) = delete;
66 Uart &operator=(const Uart &) = delete;
67
68#ifdef CONFIG_OVE_ZERO_HEAP
69 Uart(Uart &&) = delete;
70 Uart &operator=(Uart &&) = delete;
71#else
73 Uart(Uart &&o) noexcept : handle_(o.handle_)
74 {
75 o.handle_ = nullptr;
76 }
78 Uart &operator=(Uart &&o) noexcept
79 {
80 if (this != &o) {
81 if (handle_)
82 ove_uart_destroy(handle_);
83 handle_ = o.handle_;
84 o.handle_ = nullptr;
85 }
86 return *this;
87 }
88#endif
89
95 [[nodiscard]] Result<size_t> write(const void *data, size_t len,
96 std::chrono::nanoseconds timeout = wait_forever) noexcept
97 {
98 size_t bytes_written = 0;
99 const int rc =
100 ove_uart_write(handle_, data, len, to_timeout_ns(timeout), &bytes_written);
101 return from_rc(rc, bytes_written);
102 }
103
109 [[nodiscard]] Result<size_t> read(void *buf, size_t len,
110 std::chrono::nanoseconds timeout = wait_forever) noexcept
111 {
112 size_t bytes_read = 0;
113 const int rc =
114 ove_uart_read(handle_, buf, len, to_timeout_ns(timeout), &bytes_read);
115 return from_rc(rc, bytes_read);
116 }
117
119 size_t bytes_available() const
120 {
121 return ove_uart_bytes_available(handle_);
122 }
123
125 [[nodiscard]] Result<void> flush() noexcept
126 {
127 return from_rc(ove_uart_flush(handle_));
128 }
129
131 ove_uart_t handle() const
132 {
133 return handle_;
134 }
135
136 private:
137 ove_uart_t handle_ = nullptr;
138#ifdef CONFIG_OVE_ZERO_HEAP
139 ove_uart_storage_t storage_{};
140 uint8_t rx_buf_[RxBufSize > 0 ? RxBufSize : 1]{};
141#endif
142};
143
144} /* namespace ove */
145
146#endif /* CONFIG_OVE_UART */
RAII wrapper around an oveRTOS UART peripheral.
Definition uart.hpp:36
ove_uart_t handle() const
Returns the underlying C handle.
Definition uart.hpp:131
Uart(Uart &&o) noexcept
Move constructor — transfers handle; source becomes empty.
Definition uart.hpp:73
Uart & operator=(Uart &&o) noexcept
Move-assignment — destroys current port, then takes o's handle.
Definition uart.hpp:78
Uart(const struct ove_uart_cfg &cfg)
Construct and initialise a UART port from cfg.
Definition uart.hpp:42
Result< size_t > read(void *buf, size_t len, std::chrono::nanoseconds timeout=wait_forever) noexcept
Read bytes from the RX buffer.
Definition uart.hpp:109
Result< void > flush() noexcept
Block until all pending TX bytes have been drained.
Definition uart.hpp:125
size_t bytes_available() const
Bytes currently available in the RX buffer.
Definition uart.hpp:119
Result< size_t > write(const void *data, size_t len, std::chrono::nanoseconds timeout=wait_forever) noexcept
Write bytes to the port.
Definition uart.hpp:95
Strong ove::Error type, Result<T> alias, and std::error_code interop for the oveRTOS C++ binding.
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:20
constexpr uint64_t to_timeout_ns(std::chrono::duration< Rep, Period > d) noexcept
Convert a chrono duration to uint64_t nanoseconds for the C API.
Definition types.hpp:129
constexpr std::chrono::nanoseconds wait_forever
Sentinel duration meaning "block indefinitely".
Definition types.hpp:119
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139
Common type definitions and concepts for the C++ wrapper layer.