Skip to main content

flipperzero_sys/furi/
status.rs

1//! Low-level wrappers around Furi Status API.
2
3use core::fmt::Display;
4
5use crate::FuriStatus;
6
7/// The error type for Furi kernel operations.
8#[derive(Clone, Copy, Debug, ufmt::derive::uDebug, Eq, PartialEq)]
9pub enum Error {
10    Unspecified,
11    TimedOut,
12    ResourceBusy,
13    InvalidParameter,
14    OutOfMemory,
15    ForbiddenInISR,
16    Other(i32),
17}
18
19impl Error {
20    /// Describe the kind of error.
21    pub fn description(&self) -> &str {
22        match self {
23            Self::Unspecified => "Unspecified RTOS error",
24            Self::TimedOut => "Operation not completed within the timeout period",
25            Self::ResourceBusy => "Resource not available",
26            Self::InvalidParameter => "Parameter error",
27            Self::OutOfMemory => "System is out of memory",
28            Self::ForbiddenInISR => "Not allowed in ISR context",
29            _ => "Unknown",
30        }
31    }
32}
33
34/// Create [`Error`] from [`FuriStatus`].
35impl TryFrom<FuriStatus> for Error {
36    type Error = i32;
37
38    fn try_from(status: crate::FuriStatus) -> core::result::Result<Self, Self::Error> {
39        match status {
40            crate::FuriStatusError => Ok(Self::Unspecified),
41            crate::FuriStatusErrorTimeout => Ok(Self::TimedOut),
42            crate::FuriStatusErrorResource => Ok(Self::ResourceBusy),
43            crate::FuriStatusErrorParameter => Ok(Self::InvalidParameter),
44            crate::FuriStatusErrorNoMemory => Ok(Self::OutOfMemory),
45            crate::FuriStatusErrorISR => Ok(Self::ForbiddenInISR),
46            crate::FuriStatus(x) => {
47                if x < 0 {
48                    Ok(Self::Other(x))
49                } else {
50                    Err(x)
51                }
52            }
53        }
54    }
55}
56
57/// Create [`FuriStatus`] from [`Error`].
58impl From<Error> for FuriStatus {
59    fn from(error: Error) -> Self {
60        match error {
61            Error::Unspecified => crate::FuriStatusError,
62            Error::TimedOut => crate::FuriStatusErrorTimeout,
63            Error::ResourceBusy => crate::FuriStatusErrorResource,
64            Error::InvalidParameter => crate::FuriStatusErrorParameter,
65            Error::OutOfMemory => crate::FuriStatusErrorNoMemory,
66            Error::ForbiddenInISR => crate::FuriStatusErrorISR,
67            Error::Other(x) => crate::FuriStatus(x),
68        }
69    }
70}
71
72impl Display for Error {
73    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
74        write!(f, "{} ({})", self.description(), FuriStatus::from(*self).0)
75    }
76}
77
78impl ufmt::uDisplay for Error {
79    fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
80    where
81        W: ufmt::uWrite + ?Sized,
82    {
83        ufmt::uwrite!(f, "{} ({})", self.description(), FuriStatus::from(*self).0)
84    }
85}
86
87/// Operation status.
88/// The Furi API switches between using `enum FuriStatus`, `int32_t` and `uint32_t`.
89/// Since these all use the same bit representation, we can just "cast" the returns to this type.
90#[repr(transparent)]
91#[derive(Clone, Copy, Debug, ufmt::derive::uDebug, Eq, PartialEq)]
92#[must_use]
93pub struct Status(pub i32);
94
95impl Status {
96    /// Operation completed successfully.
97    pub const OK: Status = Status(0);
98    /// Unspecified RTOS error: run-time error but no other error message fits.
99    pub const ERR: Status = Status(-1);
100    /// Operation not completed within the timeout period.
101    pub const ERR_TIMEOUT: Status = Status(-2);
102    /// Resource not available.
103    pub const ERR_RESOURCE: Status = Status(-3);
104    /// Parameter error.
105    pub const ERR_PARAMETER: Status = Status(-4);
106    /// System is out of memory: it was impossible to allocate or reserve memory for the operation.
107    pub const ERR_NO_MEMORY: Status = Status(-5);
108    /// Not allowed in ISR context: the function cannot be called from interrupt service routines.
109    pub const ERR_ISR: Status = Status(-6);
110
111    /// Describes the status result of the operation.
112    pub fn description(self) -> &'static str {
113        match self {
114            Self::OK => "Operation completed successfully",
115            Self::ERR => "Unspecified RTOS error",
116            Self::ERR_TIMEOUT => "Operation not completed within the timeout period",
117            Self::ERR_RESOURCE => "Resource not available",
118            Self::ERR_PARAMETER => "Parameter error",
119            Self::ERR_NO_MEMORY => "System is out of memory",
120            Self::ERR_ISR => "Not allowed in ISR context",
121            _ => "Unknown",
122        }
123    }
124
125    /// Check if this is an error status.
126    pub fn is_err(self) -> bool {
127        self != Self::OK
128    }
129
130    /// Convert into [`Result`] type.
131    pub fn into_result(self) -> Result<i32, Error> {
132        match Error::try_from(crate::FuriStatus(self.0)) {
133            Err(x) => Ok(x),
134            Ok(err) => Err(err),
135        }
136    }
137}
138
139impl Display for Status {
140    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
141        write!(f, "{:?}: {}", self, self.description())
142    }
143}
144
145impl ufmt::uDisplay for Status {
146    fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
147    where
148        W: ufmt::uWrite + ?Sized,
149    {
150        ufmt::uwrite!(f, "{:?}: {}", self, self.description())
151    }
152}
153
154impl From<crate::FuriStatus> for Status {
155    fn from(code: FuriStatus) -> Self {
156        Status(code.0)
157    }
158}
159
160impl From<i32> for Status {
161    fn from(value: i32) -> Self {
162        Status(value)
163    }
164}
165
166impl From<u32> for Status {
167    fn from(value: u32) -> Self {
168        Status(value as i32)
169    }
170}
171
172impl From<Status> for Result<i32, Error> {
173    fn from(status: Status) -> Self {
174        status.into_result()
175    }
176}