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
21namespace ove {
22
33template <size_t RxBufSize = 0>
34class Uart {
35public:
36 explicit Uart(const struct ove_uart_cfg &cfg) requires (RxBufSize > 0) {
37#ifdef CONFIG_OVE_ZERO_HEAP
38 static_assert(RxBufSize > 0,
39 "RxBufSize must be > 0 in zero-heap mode");
40 int err = ove_uart_init(&handle_, &storage_,
41 rx_buf_, &cfg);
42#else
43 int err = ove_uart_create(&handle_, &cfg);
44#endif
45 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
46 }
47
48 ~Uart() {
49 if (!handle_) return;
50#ifdef CONFIG_OVE_ZERO_HEAP
51 ove_uart_deinit(handle_);
52#else
53 ove_uart_destroy(handle_);
54#endif
55 }
56
57 Uart(const Uart &) = delete;
58 Uart &operator=(const Uart &) = delete;
59
60#ifdef CONFIG_OVE_ZERO_HEAP
61 Uart(Uart &&) = delete;
62 Uart &operator=(Uart &&) = delete;
63#else
64 Uart(Uart &&o) noexcept : handle_(o.handle_) { o.handle_ = nullptr; }
65 Uart &operator=(Uart &&o) noexcept {
66 if (this != &o) {
67 if (handle_) ove_uart_destroy(handle_);
68 handle_ = o.handle_;
69 o.handle_ = nullptr;
70 }
71 return *this;
72 }
73#endif
74
75 [[nodiscard]] int write(const void *data, size_t len,
76 uint32_t timeout_ms = OVE_WAIT_FOREVER,
77 size_t *bytes_written = nullptr) {
78 return ove_uart_write(handle_, data, len,
79 timeout_ms, bytes_written);
80 }
81
82 [[nodiscard]] int read(void *buf, size_t len,
83 uint32_t timeout_ms = OVE_WAIT_FOREVER,
84 size_t *bytes_read = nullptr) {
85 return ove_uart_read(handle_, buf, len,
86 timeout_ms, bytes_read);
87 }
88
89 size_t bytes_available() const {
90 return ove_uart_bytes_available(handle_);
91 }
92
93 [[nodiscard]] int flush() {
94 return ove_uart_flush(handle_);
95 }
96
97 ove_uart_t native_handle() const { return handle_; }
98
99private:
100 ove_uart_t handle_ = nullptr;
101#ifdef CONFIG_OVE_ZERO_HEAP
102 ove_uart_storage_t storage_{};
103 uint8_t rx_buf_[RxBufSize > 0 ? RxBufSize : 1]{};
104#endif
105};
106
107} /* namespace ove */
108
109#endif /* CONFIG_OVE_UART */
void write(const char *data, unsigned int len)
Writes a buffer of bytes to the console output.
Definition console.hpp:71
int read(const char *key, void *buf, size_t len, size_t *out)
Reads the value associated with a key from NVS.
Definition nvs.hpp:55
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.