oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
stream.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_STREAM
17
18#include <ove/stream.h>
19#include <ove/types.hpp>
20
21namespace ove {
22
37template <size_t BufSize = 0>
38class Stream {
39public:
50 explicit Stream(size_t trigger) requires (BufSize > 0) {
51#ifdef CONFIG_OVE_ZERO_HEAP
52 static_assert(BufSize > 0,
53 "BufSize must be > 0 in zero-heap mode");
54 int err = ove_stream_init(&handle_, &storage_,
55 buffer_, BufSize, trigger);
56#else
57 int err = ove_stream_create(&handle_, BufSize, trigger);
58#endif
59 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
60 }
61
66 if (!handle_) return;
67#ifdef CONFIG_OVE_ZERO_HEAP
68 ove_stream_deinit(handle_);
69#else
70 ove_stream_destroy(handle_);
71#endif
72 }
73
74 Stream(const Stream &) = delete;
75 Stream &operator=(const Stream &) = delete;
76
77#ifdef CONFIG_OVE_ZERO_HEAP
78 Stream(Stream &&) = delete;
79 Stream &operator=(Stream &&) = delete;
80#else
85 Stream(Stream &&other) noexcept : handle_(other.handle_) {
86 other.handle_ = nullptr;
87 }
88
94 Stream &operator=(Stream &&other) noexcept {
95 if (this != &other) {
96 if (handle_) ove_stream_destroy(handle_);
97 handle_ = other.handle_;
98 other.handle_ = nullptr;
99 }
100 return *this;
101 }
102#endif
103
112 [[nodiscard]] int send(const void *data, size_t len,
113 uint32_t timeout_ms, size_t *bytes_sent) {
114 return ove_stream_send(handle_, data, len, timeout_ms,
115 bytes_sent);
116 }
117
126 [[nodiscard]] int receive(void *buf, size_t len,
127 uint32_t timeout_ms,
128 size_t *bytes_received) {
129 return ove_stream_receive(handle_, buf, len, timeout_ms,
130 bytes_received);
131 }
132
140 [[nodiscard]] int send_from_isr(const void *data, size_t len,
141 size_t *bytes_sent) {
142 return ove_stream_send_from_isr(handle_, data, len,
143 bytes_sent);
144 }
145
153 [[nodiscard]] int receive_from_isr(void *buf, size_t len,
154 size_t *bytes_received) {
155 return ove_stream_receive_from_isr(handle_, buf, len,
156 bytes_received);
157 }
158
163 [[nodiscard]] int reset() {
164 return ove_stream_reset(handle_);
165 }
166
171 size_t bytes_available() const {
172 return ove_stream_bytes_available(handle_);
173 }
174
179 bool valid() const { return handle_ != nullptr; }
180
185 ove_stream_t handle() const { return handle_; }
186
187private:
188 ove_stream_t handle_ = nullptr;
189#ifdef CONFIG_OVE_ZERO_HEAP
190 ove_stream_storage_t storage_ = {};
191 uint8_t buffer_[(BufSize > 0 ? BufSize : 1) + 1];
192#endif
193};
194
195} /* namespace ove */
196
197#endif /* CONFIG_OVE_STREAM */
RAII wrapper around an oveRTOS byte-stream (ring-buffer) object.
Definition stream.hpp:38
ove_stream_t handle() const
Returns the raw oveRTOS stream handle.
Definition stream.hpp:185
Stream & operator=(Stream &&other) noexcept
Move-assignment operator — transfers ownership of the kernel handle.
Definition stream.hpp:94
bool valid() const
Returns true if the underlying kernel handle is non-null.
Definition stream.hpp:179
int reset()
Resets the stream, discarding any buffered data.
Definition stream.hpp:163
size_t bytes_available() const
Returns the number of bytes currently available to read.
Definition stream.hpp:171
int send(const void *data, size_t len, uint32_t timeout_ms, size_t *bytes_sent)
Sends bytes into the stream from task context.
Definition stream.hpp:112
int receive(void *buf, size_t len, uint32_t timeout_ms, size_t *bytes_received)
Receives bytes from the stream from task context.
Definition stream.hpp:126
Stream(size_t trigger)
Constructs and initialises the stream with the given receive trigger.
Definition stream.hpp:50
~Stream()
Destroys the stream, releasing the underlying kernel resource.
Definition stream.hpp:65
int send_from_isr(const void *data, size_t len, size_t *bytes_sent)
Sends bytes into the stream from an ISR context (non-blocking).
Definition stream.hpp:140
Stream(Stream &&other) noexcept
Move constructor — transfers ownership of the kernel handle.
Definition stream.hpp:85
int receive_from_isr(void *buf, size_t len, size_t *bytes_received)
Receives bytes from the stream from an ISR context (non-blocking).
Definition stream.hpp:153
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.