osal_rs/traits/queue.rs
1/***************************************************************************
2 *
3 * osal-rs
4 * Copyright (C) 2023/2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***************************************************************************/
19
20//! Queue traits for inter-task communication.
21//!
22//! Provides both raw byte-based queues and type-safe streamed queues
23//! for message passing between tasks.
24#[cfg(not(feature = "serde"))]
25use crate::os::Deserialize;
26
27#[cfg(feature = "serde")]
28use osal_rs_serde::Deserialize;
29
30use crate::os::types::TickType;
31use crate::utils::Result;
32
33/// Raw byte-oriented queue for inter-task message passing.
34///
35/// This trait defines a FIFO queue that works with raw byte arrays,
36/// suitable for variable-sized messages or when type safety is not required.
37///
38/// # Examples
39///
40/// ```ignore
41/// use osal_rs::os::{Queue, QueueFn};
42///
43/// let queue = Queue::new(10, 32).unwrap(); // 10 messages, 32 bytes each
44///
45/// let data = [1, 2, 3, 4];
46/// queue.post(&data, 100).unwrap();
47///
48/// let mut buffer = [0u8; 32];
49/// queue.fetch(&mut buffer, 100).unwrap();
50/// ```
51pub trait Queue {
52 /// Fetches a message from the queue (blocking).
53 ///
54 /// Removes and retrieves the oldest message from the queue.
55 ///
56 /// # Parameters
57 ///
58 /// * `buffer` - Buffer to receive the message data
59 /// * `time` - Maximum ticks to wait for a message
60 ///
61 /// # Returns
62 ///
63 /// * `Ok(())` - Message received successfully
64 /// * `Err(Error)` - Timeout or other error occurred
65 ///
66 /// # Examples
67 ///
68 /// ```ignore
69 /// let mut buffer = [0u8; 16];
70 /// queue.fetch(&mut buffer, 1000).unwrap();
71 /// ```
72 fn fetch(&self, buffer: &mut [u8], time: TickType) -> Result<()>;
73
74 /// Fetches a message from ISR context (non-blocking).
75 ///
76 /// ISR-safe version of `fetch()`. Does not block.
77 ///
78 /// # Parameters
79 ///
80 /// * `buffer` - Buffer to receive the message data
81 ///
82 /// # Returns
83 ///
84 /// * `Ok(())` - Message received
85 /// * `Err(Error)` - Queue empty or error
86 fn fetch_from_isr(&self, buffer: &mut [u8]) -> Result<()>;
87
88 /// Posts a message to the queue (blocking).
89 ///
90 /// Adds a new message to the end of the queue.
91 ///
92 /// # Parameters
93 ///
94 /// * `item` - The message data to send
95 /// * `time` - Maximum ticks to wait if queue is full
96 ///
97 /// # Returns
98 ///
99 /// * `Ok(())` - Message sent successfully
100 /// * `Err(Error)` - Timeout or error occurred
101 ///
102 /// # Examples
103 ///
104 /// ```ignore
105 /// let data = [1, 2, 3, 4];
106 /// queue.post(&data, 1000).unwrap();
107 /// ```
108 fn post(&self, item: &[u8], time: TickType) -> Result<()>;
109
110 /// Posts a message from ISR context (non-blocking).
111 ///
112 /// ISR-safe version of `post()`. Does not block.
113 ///
114 /// # Parameters
115 ///
116 /// * `item` - The message data to send
117 ///
118 /// # Returns
119 ///
120 /// * `Ok(())` - Message sent
121 /// * `Err(Error)` - Queue full or error
122 fn post_from_isr(&self, item: &[u8]) -> Result<()>;
123
124 /// Deletes the queue and frees its resources.
125 ///
126 /// # Safety
127 ///
128 /// Ensure no tasks are blocked on this queue before deletion.
129 fn delete(&mut self);
130}
131
132/// Type-safe queue for structured message passing.
133///
134/// This trait provides a queue that works with specific types,
135/// offering compile-time type safety for queue operations.
136///
137/// # Type Parameters
138///
139/// * `T` - The message type (must implement `ToBytes`)
140///
141/// # Examples
142///
143/// ```ignore
144/// use osal_rs::os::{QueueStreamed, QueueStreamedFn, ToBytes};
145///
146/// struct Message {
147/// id: u32,
148/// value: i16,
149/// }
150///
151/// let queue = QueueStreamed::<Message>::new(10, size_of::<Message>()).unwrap();
152///
153/// let msg = Message { id: 1, value: 42 };
154/// queue.post(&msg, 100).unwrap();
155///
156/// let mut received = Message { id: 0, value: 0 };
157/// queue.fetch(&mut received, 100).unwrap();
158/// ```
159
160pub trait QueueStreamed<T>
161where
162 T: Deserialize + Sized {
163
164 /// Fetches a typed message from the queue (blocking).
165 ///
166 /// # Parameters
167 ///
168 /// * `buffer` - Mutable reference to receive the message
169 /// * `time` - Maximum ticks to wait
170 ///
171 /// # Returns
172 ///
173 /// * `Ok(())` - Message received
174 /// * `Err(Error)` - Timeout or error
175 fn fetch(&self, buffer: &mut T, time: TickType) -> Result<()>;
176
177 /// Fetches a typed message from ISR context (non-blocking).
178 ///
179 /// # Parameters
180 ///
181 /// * `buffer` - Mutable reference to receive the message
182 ///
183 /// # Returns
184 ///
185 /// * `Ok(())` - Message received
186 /// * `Err(Error)` - Queue empty or error
187 fn fetch_from_isr(&self, buffer: &mut T) -> Result<()>;
188
189 /// Posts a typed message to the queue (blocking).
190 ///
191 /// # Parameters
192 ///
193 /// * `item` - Reference to the message to send
194 /// * `time` - Maximum ticks to wait if full
195 ///
196 /// # Returns
197 ///
198 /// * `Ok(())` - Message sent
199 /// * `Err(Error)` - Timeout or error
200 fn post(&self, item: &T, time: TickType) -> Result<()>;
201
202 /// Posts a typed message from ISR context (non-blocking).
203 ///
204 /// # Parameters
205 ///
206 /// * `item` - Reference to the message to send
207 ///
208 /// # Returns
209 ///
210 /// * `Ok(())` - Message sent
211 /// * `Err(Error)` - Queue full or error
212 fn post_from_isr(&self, item: &T) -> Result<()>;
213
214 /// Deletes the queue and frees its resources.
215 ///
216 /// # Safety
217 ///
218 /// Ensure no tasks are blocked on this queue before deletion.
219 fn delete(&mut self);
220}