leodos_libcfs/os/app.rs
1//! Top-level OSAL application lifecycle API.
2//!
3//! # Note
4//!
5//! These functions are typically part of the BSP/runtime and not called by
6//! individual cFS applications, but are provided for completeness and for
7//! special cases like unit testing environments.
8
9use crate::error::Result;
10use crate::ffi;
11use crate::status::check;
12
13/// Initializes the OS Abstraction Layer.
14///
15/// This must be called before any other OSAL routine. It is
16/// typically handled by the cFE Main entry point.
17///
18/// Failure means subsequent OSAL calls have undefined behavior.
19/// The typical response is to abort the application.
20pub fn api_init() -> Result<()> {
21 check(unsafe { ffi::OS_API_Init() })?;
22 Ok(())
23}
24
25/// Tears down and de-initializes the OSAL.
26///
27/// This is best-effort — it may not recover all resources.
28/// Intended for testing or controlled shutdown scenarios.
29pub fn api_teardown() {
30 unsafe { ffi::OS_API_Teardown() };
31}
32
33/// A background thread implementation that waits for events.
34///
35/// This is typically called by the BSP main routine after all
36/// other initialization has taken place. It waits until
37/// [`application_shutdown`] is called.
38pub fn idle_loop() {
39 unsafe { ffi::OS_IdleLoop() };
40}
41
42/// Deletes all resources (tasks, queues, etc.) created in OSAL.
43///
44/// Useful for cleaning up during an orderly shutdown or for testing.
45pub fn delete_all_objects() {
46 unsafe { ffi::OS_DeleteAllObjects() };
47}
48
49/// Initiates or cancels an orderly shutdown of the OSAL application.
50///
51/// Passing `true` initiates shutdown, waking the task currently
52/// blocked in [`idle_loop`]. Passing `false` cancels a
53/// previously-initiated shutdown.
54pub fn application_shutdown(should_shutdown: bool) {
55 unsafe { ffi::OS_ApplicationShutdown(should_shutdown as u8) };
56}
57
58/// Exits/aborts the entire application process immediately.
59///
60/// This function does not return and is typically only used in
61/// non-production scenarios like unit testing.
62pub fn application_exit(status: i32) -> ! {
63 unsafe { ffi::OS_ApplicationExit(status) };
64 loop {}
65}