1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/***************************************************************************
*
* osal-rs
* Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
*
***************************************************************************/
//! Trait definitions for OSAL abstractions.
//!
//! This module defines the trait interfaces that concrete RTOS implementations
//! must satisfy. These traits provide a portable API across different operating systems.
//!
//! # Architecture
//!
//! The OSAL-RS library uses a trait-based design pattern:
//! 1. **Traits** (this module) - Define the interface contracts
//! 2. **Implementations** (freertos, posix) - Provide concrete implementations
//! 3. **Re-exports** (`os` module) - Expose implementations through a unified API
//!
//! This design allows:
//! - Platform independence: Switch RTOS by changing feature flags
//! - Type safety: Compile-time verification of correct usage
//! - Zero-cost abstraction: Traits compile to direct function calls
//! - Extensibility: Add new RTOS backends by implementing the traits
//!
//! # Available Traits
//!
//! ## Synchronization Primitives
//!
//! - [`MutexFn`] - Mutual exclusion with RAII guards
//! - [`RawMutexFn`] - Low-level mutex without guards
//! - [`SemaphoreFn`] - Counting and binary semaphores
//! - [`EventGroupFn`] - Multi-bit synchronization flags
//!
//! ## Communication
//!
//! - [`QueueFn`] - FIFO queue for raw byte messages
//! - [`QueueStreamedFn`] - Type-safe queue using serialization
//! - [`Serialize`] / [`Deserialize`] - Serialization traits for queues
//!
//! ## Threading
//!
//! - [`ThreadFn`] - Thread/task creation and management
//! - [`ThreadNotification`] - Thread notification mechanisms
//! - [`ToPriority`] - Priority conversion trait
//!
//! ## Timers
//!
//! - [`TimerFn`] - Software timer callbacks
//!
//! ## System
//!
//! - [`SystemFn`] - System-level operations (scheduler, timing, critical sections)
//! - [`ToTick`] / [`FromTick`] - Time conversion to/from RTOS ticks
//!
//! ## Utilities
//!
//! - [`BytesHasLen`] - Length queries for serializable types
//!
//! # Naming Convention
//!
//! Traits are re-exported with a `Fn` suffix to avoid naming conflicts with
//! concrete implementation types:
//!
//! ```ignore
//! // Trait definition (in this module)
//! pub trait Thread { ... }
//!
//! // Re-exported as ThreadFn to avoid conflict
//! pub use Thread as ThreadFn;
//!
//! // Concrete type in freertos module
//! pub struct Thread { ... }
//! impl ThreadFn for Thread { ... }
//! ```
//!
//! This allows both the trait and the concrete type to coexist in the same namespace.
//!
//! # Usage
//!
//! Most users should use the `os` module instead of importing traits directly:
//!
//! ```ignore
//! use osal_rs::os::*; // Gets concrete types
//!
//! let mutex = Mutex::new(0); // Uses concrete freertos::Mutex
//! ```
//!
//! Advanced users can import traits for generic programming:
//!
//! ```ignore
//! use osal_rs::traits::MutexFn;
//!
//! fn use_mutex<M: MutexFn<i32>>(mutex: &M) {
//! let guard = mutex.lock();
//! // ...
//! }
//! ```
//!
//! # Implementation Requirements
//!
//! When implementing these traits for a new RTOS backend:
//! 1. Implement all trait methods faithfully to the documented behavior
//! 2. Ensure thread safety as documented
//! 3. Handle ISR context appropriately (provide `_from_isr` variants where needed)
//! 4. Use appropriate error types from `utils::Error`
//! 5. Follow RAII patterns where applicable (e.g., mutex guards)
//!
//! # See Also
//!
//! - Individual trait modules for detailed documentation
//! - `freertos` module for the FreeRTOS implementation
//! - `os` module for the unified public API
/// Byte serialization and deserialization traits.
/// Event group trait for multi-bit synchronization.
/// Mutex traits for mutual exclusion with RAII.
/// Queue traits for inter-task communication.
/// Semaphore trait for counting and binary semaphores.
/// System-level RTOS control trait.
/// Thread/task management trait.
/// Tick conversion traits for time handling.
/// Software timer trait for callbacks.
// Re-export serialization traits for queue usage
pub use crate*;
// Re-export event group trait with Fn suffix to avoid naming conflicts
pub use crateEventGroup as EventGroupFn;
// Re-export mutex traits with Fn suffix (RawMutex, Mutex, MutexGuard)
pub use crate;
// Re-export queue traits with Fn suffix (Queue for raw bytes, QueueStreamed for typed messages)
pub use crate;
// Re-export semaphore trait with Fn suffix
pub use crateSemaphore as SemaphoreFn;
// Re-export system trait with Fn suffix for scheduler, timing, and critical section control
pub use crateSystem as SystemFn;
// Re-export thread trait and related types (ThreadParam, function pointers, notifications, priority conversion)
pub use crate;
// Re-export tick conversion traits (ToTick, FromTick)
pub use crate*;
// Re-export timer trait and related types (TimerParam, function pointer)
pub use crate;