Skip to main content

leodos_libcfs/cfe/es/
resource.rs

1//! Safe wrappers for generic CFE Resource ID functions.
2
3use crate::cfe::es::app::AppId;
4use crate::cfe::es::app::AppInfo;
5use crate::cfe::es::lib::LibId;
6use crate::error::Result;
7use crate::ffi;
8use crate::status::check;
9use core::mem::MaybeUninit;
10
11/// A generic, type-safe wrapper for a CFE Resource ID.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13#[repr(transparent)]
14pub struct ResourceId(pub ffi::CFE_ResourceId_t);
15
16impl ResourceId {
17    /// Gets the base value (type/category) from this resource ID.
18    pub fn base(&self) -> u32 {
19        unsafe { ffi::CFE_ResourceId_GetBase(self.0) }
20    }
21
22    /// Gets the serial number from this resource ID.
23    pub fn serial(&self) -> u32 {
24        unsafe { ffi::CFE_ResourceId_GetSerial(self.0) }
25    }
26
27    /// Retrieves information about an Application or Library given this Resource ID.
28    ///
29    /// This is a generic wrapper that can be used for either an `AppId` or a `LibId`.
30    pub fn module_info(&self) -> Result<AppInfo> {
31        let mut module_info_uninit = MaybeUninit::uninit();
32        check(unsafe { ffi::CFE_ES_GetModuleInfo(module_info_uninit.as_mut_ptr(), self.0) })?;
33        Ok(AppInfo {
34            inner: unsafe { module_info_uninit.assume_init() },
35        })
36    }
37}
38
39// ADD these From implementations
40impl From<AppId> for ResourceId {
41    fn from(app_id: AppId) -> Self {
42        ResourceId(app_id.0)
43    }
44}
45
46impl From<LibId> for ResourceId {
47    fn from(lib_id: LibId) -> Self {
48        ResourceId(lib_id.0)
49    }
50}