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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! The low-level kernel static configuration interface to be implemented by a
//! kernel implementor.
//!
//! # General Structure
//!
//! This module includes the traits for a kernel-specific low-level configurator
//! type, which is used by [the kernel static configuration process][1] to
//! receive the specificiations of defined kernel objects and assign their IDs.
//! [`CfgBase`][] is the supertrait of all these traits and must be implemented
//! by the configurator type. It can optionally can implement other traits as
//! well if they can be supported.
//!
//! The `Cfg${Ty}` traits extend [`CfgBase`] by providing a
//! method named `${ty}_define` to define a kernel object of the corresponding
//! type (`${Ty}`). The method takes two parameters: `${Ty}Descriptor`
//! containing mandatory properties and `impl `[`Bag`] containing additional,
//! implementation-specific properties.
//!
//! The `${Ty}Descriptor` types contain mandatory (both for the consumers and
//! the implementors) properties of a kernel object to be created. They all
//! contain a `phantom: `[`PhantomInvariant`]`<System>` field to ensure they are
//! always parameterized and invariant over `System`.
//!
//! # Safety
//!
//! Most traits in this method are `unsafe trait` because they have to be
//! trustworthy to be able to build sound memory-safe abstractions on top of
//! them.
//!
//! # Stability
//!
//! This module is covered by [the kernel-side API stability guarantee][2].
//!
//! The trait paths in this module are covered by the application-side API
//! stability guarantee. Application code should only use these traits in trait
//! bounds and, to access the provided functionalities, should use the the
//! stable wrapper [outside this module](../index.html) instead.
//!
//! [1]: crate::kernel::cfg::KernelStatic
//! [2]: crate#stability
use crate::{bag::Bag, closure::Closure, kernel::raw, time::Duration, utils::PhantomInvariant};

/// The trait for all kernel-specific low-level configurator types, used by
/// [the kernel static configuration process][2].
///
/// # Safety
///
/// See [the module documentation][4].
///
/// # Stability
///
/// See [the module documentation][3].
///
/// [2]: crate::kernel::cfg::KernelStatic
/// [3]: self#stability
/// [4]: self#safety
#[const_trait]
pub unsafe trait CfgBase {
    type System: raw::KernelBase;
    fn num_task_priority_levels(&mut self, new_value: usize);

    /// Register a combined [startup hook][1].
    ///
    /// The configuration system calls this exactly once for each built system.
    ///
    /// [1]: crate::kernel::hook::StartupHook
    fn startup_hook_define(&mut self, func: fn());
}

/// A low-level configurator trait providing a method to define a
/// [task][1] in [the kernel static configuration process][2].
///
/// # Safety
///
/// See [the module documentation][4].
///
/// # Stability
///
/// See [the module documentation][3].
///
/// [1]: crate::kernel::StaticTask
/// [2]: crate::kernel::cfg::KernelStatic
/// [3]: self#stability
/// [4]: self#safety
#[const_trait]
pub unsafe trait CfgTask: ~const CfgBase {
    fn task_define<Properties: ~const Bag>(
        &mut self,
        descriptor: TaskDescriptor<Self::System>,
        properties: Properties,
    ) -> <Self::System as raw::KernelBase>::RawTaskId;
}

/// The basic properties of a task.
#[derive(Debug)]
pub struct TaskDescriptor<System> {
    pub phantom: PhantomInvariant<System>,
    pub start: Closure,
    pub active: bool,
    pub priority: usize,
    pub stack_size: Option<usize>,
}

/// A low-level configurator trait providing a method to define an
/// [event group][2] in [the kernel static configuration process][1].
///
/// # Safety
///
/// See [the module documentation][4].
///
/// # Stability
///
/// See [the module documentation][3].
///
/// [1]: crate::kernel::StaticEventGroup
/// [2]: crate::kernel::cfg::KernelStatic
/// [3]: self#stability
/// [4]: self#safety
#[const_trait]
pub unsafe trait CfgEventGroup: ~const CfgBase<System: raw::KernelEventGroup> {
    fn event_group_define<Properties: ~const Bag>(
        &mut self,
        descriptor: EventGroupDescriptor<Self::System>,
        properties: Properties,
    ) -> <Self::System as raw::KernelEventGroup>::RawEventGroupId;
}

/// The basic properties of an event group.
#[derive(Debug)]
pub struct EventGroupDescriptor<System> {
    pub phantom: PhantomInvariant<System>,
    pub initial_bits: raw::EventGroupBits,
    pub queue_order: raw::QueueOrder,
}

/// A low-level configurator trait providing a method to define a
/// [mutex][2] in [the kernel static configuration process][1].
///
/// # Safety
///
/// See [the module documentation][4].
///
/// # Stability
///
/// See [the module documentation][3].
///
/// [1]: crate::kernel::StaticMutex
/// [2]: crate::kernel::cfg::KernelStatic
/// [3]: self#stability
/// [4]: self#safety
#[const_trait]
pub unsafe trait CfgMutex: ~const CfgBase<System: raw::KernelMutex> {
    fn mutex_define<Properties: ~const Bag>(
        &mut self,
        descriptor: MutexDescriptor<Self::System>,
        properties: Properties,
    ) -> <Self::System as raw::KernelMutex>::RawMutexId;
}

/// The basic properties of a mutex.
#[derive(Debug)]
pub struct MutexDescriptor<System> {
    pub phantom: PhantomInvariant<System>,
    pub protocol: raw::MutexProtocol,
}

/// A low-level configurator trait providing a method to define a
/// [semaphore][2] in [the kernel static configuration process][1].
///
/// # Safety
///
/// See [the module documentation][4].
///
/// # Stability
///
/// See [the module documentation][3].
///
/// [1]: crate::kernel::StaticSemaphore
/// [2]: crate::kernel::cfg::KernelStatic
/// [3]: self#stability
/// [4]: self#safety
#[const_trait]
pub unsafe trait CfgSemaphore: ~const CfgBase<System: raw::KernelSemaphore> {
    fn semaphore_define<Properties: ~const Bag>(
        &mut self,
        descriptor: SemaphoreDescriptor<Self::System>,
        properties: Properties,
    ) -> <Self::System as raw::KernelSemaphore>::RawSemaphoreId;
}

/// The basic properties of a semaphore.
#[derive(Debug)]
pub struct SemaphoreDescriptor<System> {
    pub phantom: PhantomInvariant<System>,
    pub initial: raw::SemaphoreValue,
    pub maximum: raw::SemaphoreValue,
    pub queue_order: raw::QueueOrder,
}

/// A low-level configurator trait providing a method to define a
/// [timwer][2] in [the kernel static configuration process][1].
///
/// # Safety
///
/// See [the module documentation][4].
///
/// # Stability
///
/// See [the module documentation][3].
///
/// [1]: crate::kernel::StaticTimer
/// [2]: crate::kernel::cfg::KernelStatic
/// [3]: self#stability
/// [4]: self#safety
#[const_trait]
pub unsafe trait CfgTimer: ~const CfgBase<System: raw::KernelTimer> {
    fn timer_define<Properties: ~const Bag>(
        &mut self,
        descriptor: TimerDescriptor<Self::System>,
        properties: Properties,
    ) -> <Self::System as raw::KernelTimer>::RawTimerId;
}

/// The basic properties of a timer.
#[derive(Debug)]
pub struct TimerDescriptor<System> {
    pub phantom: PhantomInvariant<System>,
    pub start: Closure,
    pub active: bool,
    pub delay: Option<Duration>,
    pub period: Option<Duration>,
}

/// A low-level configurator trait providing a method to define an
/// [interrupt line][2] in [the kernel static configuration process][1].
///
/// # Safety
///
/// See [the module documentation][4].
///
/// # Stability
///
/// See [the module documentation][3].
///
/// [1]: crate::kernel::InterruptLine
/// [2]: crate::kernel::cfg::KernelStatic
/// [3]: self#stability
/// [4]: self#safety
#[const_trait]
pub unsafe trait CfgInterruptLine: ~const CfgBase<System: raw::KernelInterruptLine> {
    fn interrupt_line_define<Properties: ~const Bag>(
        &mut self,
        descriptor: InterruptLineDescriptor<Self::System>,
        properties: Properties,
    );
}

/// The basic properties of an interrupt line.
#[derive(Debug)]
pub struct InterruptLineDescriptor<System> {
    pub phantom: PhantomInvariant<System>,
    pub line: raw::InterruptNum,
    pub priority: Option<raw::InterruptPriority>,
    pub start: Option<raw::InterruptHandlerFn>,
    pub enabled: bool,
}