Skip to main content

leodos_libcfs/psp/
mod.rs

1//! CFE Platform Support Package (PSP) interface.
2//!
3//! The PSP provides the lowest-level abstraction layer, interacting directly with
4//! the hardware and board support package (BSP). These wrappers expose some of
5//! the more common and useful PSP functions to applications, but many are `unsafe`
6//! due to their low-level nature.
7
8use core::ffi::CStr;
9
10use heapless::CString;
11
12use crate::error::{CfsError, OsalError, Result};
13use crate::ffi;
14
15pub mod cds;
16pub mod eeprom;
17pub mod exception;
18pub mod mem;
19pub mod restart;
20pub mod time;
21pub mod version;
22pub mod watchdog;
23
24/// Flushes processor data or instruction caches for a given memory range.
25///
26/// # Safety
27///
28/// Flushing caches can have significant system-wide effects. The address and
29/// size must correspond to a valid memory region.
30pub unsafe fn flush_caches(cache_type: u32, address: *mut (), size: u32) {
31    ffi::CFE_PSP_FlushCaches(cache_type, address as *mut _, size);
32}
33
34/// Returns the PSP-defined processor name.
35///
36/// # C-API Mapping
37/// This is a safe wrapper for `CFE_PSP_GetProcessorName`.
38pub fn get_processor_name() -> &'static str {
39    unsafe {
40        CStr::from_ptr(ffi::CFE_PSP_GetProcessorName())
41            .to_str()
42            .unwrap_or("Invalid Processor Name")
43    }
44}
45
46/// Converts a PSP status code to its symbolic name.
47///
48/// # C-API Mapping
49/// This is a safe wrapper for `CFE_PSP_StatusToString`.
50///
51/// # Errors
52/// Returns an error if the resulting string is too long for the internal buffer.
53pub fn status_to_string(
54    status: i32,
55) -> Result<CString<{ ffi::CFE_PSP_STATUS_STRING_LENGTH as usize }>> {
56    let mut buf = [0 as libc::c_char; ffi::CFE_PSP_STATUS_STRING_LENGTH as usize];
57    unsafe { ffi::CFE_PSP_StatusToString(status, &mut buf) };
58    let c_str = unsafe { CStr::from_ptr(buf.as_ptr() as *const libc::c_char) };
59    let mut s = CString::new();
60    s.extend_from_bytes(c_str.to_bytes())
61        .map_err(|_| CfsError::Osal(OsalError::NameTooLong))?;
62    Ok(s)
63}