1use core::ffi::CStr;
2use core::{ffi::c_void, str::from_utf8_mut};
3use core::fmt::{Debug, Display};
4use core::ops::Deref;
5use core::time::Duration;
6use alloc::string::{String, ToString};
7
8
9pub enum Error {
10 OutOfMemory,
11 QueueSendTimeout,
12 QueueReceiveTimeout,
13 MutexTimeout,
14 MutexLockFailed,
15 Timeout,
16 QueueFull,
17 StringConversionError,
18 TaskNotFound,
19 InvalidQueueSize,
20 NullPtr,
21 NotFound,
22 OutOfIndex,
23 InvalidType,
24 Unhandled(&'static str)
25}
26
27impl Debug for Error {
28 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
29
30 use Error::*;
31
32 match self {
33 OutOfMemory => write!(f, "OutOfMemory"),
34 QueueSendTimeout => write!(f, "QueueSendTimeout"),
35 QueueReceiveTimeout => write!(f, "QueueReceiveTimeout"),
36 MutexTimeout => write!(f, "MutexTimeout"),
37 MutexLockFailed => write!(f, "MutexLockFailed"),
38 Timeout => write!(f, "Timeout"),
39 QueueFull => write!(f, "QueueFull"),
40 StringConversionError => write!(f, "StringConversionError"),
41 TaskNotFound => write!(f, "TaskNotFound"),
42 InvalidQueueSize => write!(f, "InvalidQueueSize"),
43 NullPtr => write!(f, "NullPtr"),
44 NotFound => write!(f, "NotFound"),
45 OutOfIndex => write!(f, "OutOfIndex"),
46 InvalidType => write!(f, "InvalidType"),
47 Unhandled(msg) => write!(f, "Unhandled error: {}", msg),
48 }
49 }
50}
51
52#[derive(PartialEq, Eq, Clone, Copy, Debug)]
53pub enum CpuRegisterSize {
54 Bit64,
55 Bit32
56}
57
58#[derive(PartialEq, Eq, Clone, Copy, Debug)]
59#[repr(u8)]
60pub enum OsalRsBool {
61 False = 1,
62 True = 0
63}
64
65pub const MAX_DELAY: Duration = Duration::from_millis(usize::MAX as u64);
66
67pub type Result<T, E = Error> = core::result::Result<T, E>;
68
69pub type DoublePtr = *mut *mut c_void;
70pub type Ptr = *mut c_void;
71pub type ConstPtr = *const c_void;
72
73
74pub const fn register_bit_size() -> CpuRegisterSize {
75 if size_of::<usize>() == 8 {
76 CpuRegisterSize::Bit64
77 } else {
78 CpuRegisterSize::Bit32
79 }
80}
81
82#[macro_export]
83macro_rules! from_c_str {
84 ($str:expr) => {
85 unsafe {
86 let c_str = core::ffi::CStr::from_ptr($str);
87 alloc::string::String::from_utf8_lossy(c_str.to_bytes()).to_string()
88 }
89 };
90}
91
92#[macro_export]
93macro_rules! to_cstring {
94 ($s:expr) => {
95 alloc::ffi::CString::new($s.as_str())
96 .map_err(|_| $crate::utils::Error::Unhandled("Failed to convert string to CString"))
97 };
98}
99
100#[macro_export]
101macro_rules! to_c_str {
102 ($s:expr) => {
103 alloc::ffi::CString::new($s.as_ref() as &str).unwrap().as_ptr()
104 };
105}
106
107#[macro_export]
108macro_rules! from_str_to_array {
109 ($str:expr, $buff_name:ident, $buff_size:expr) => {
110 let mut $buff_name = [b' '; $buff_size];
111 let _bytes = $str.as_bytes();
112 let _len = core::cmp::min(_bytes.len(), $buff_size);
113 $buff_name[.._len].copy_from_slice(&_bytes[.._len]);
114 };
115}
116
117#[macro_export]
118macro_rules! thread_extract_param {
119 ($param:expr, $t:ty) => {
120 match $param.as_ref() {
121 Some(p) => {
122 match p.downcast_ref::<$t>() {
123 Some(value) => value,
124 None => return Err($crate::utils::Error::InvalidType),
125 }
126 }
127 None => return Err($crate::utils::Error::NullPtr),
128 }
129 };
130}
131
132#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
133pub struct Bytes<const SIZE: usize = 0> (pub [u8; SIZE]);
134
135impl<const SIZE: usize> Deref for Bytes<SIZE> {
136 type Target = [u8; SIZE];
137
138 fn deref(&self) -> &Self::Target {
139 &self.0
140 }
141}
142
143impl<const SIZE: usize> Display for Bytes<SIZE> {
144 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
145 let str = unsafe {
146 CStr::from_ptr(self.0.as_ptr() as *const i8)
147 .to_str()
148 .unwrap_or("Conversion error")
149 };
150
151 write!(f, "{}", str.to_string())
152 }
153}
154
155
156impl<const SIZE: usize> Bytes<SIZE> {
157 pub const fn new() -> Self {
158 Self( [0u8; SIZE] )
159 }
160
161 pub fn new_by_str(str: &str) -> Self {
162
163 let mut array = [0u8; SIZE];
164
165 let mut i = 0usize ;
166 for byte in str.as_bytes() {
167 if i > SIZE - 1{
168 break;
169 }
170 array[i] = *byte;
171 i += 1;
172 }
173
174 Self( array )
175 }
176
177 pub fn new_by_string(str: &impl ToString) -> Self {
178 Self::new_by_str(&str.to_string())
179 }
180
181 pub fn fill_str(&mut self, dest: &mut str) {
182 match from_utf8_mut(&mut self.0) {
183 Ok(str) => {
184 let len = core::cmp::min(str.len(), dest.len());
185 unsafe {
186 dest.as_bytes_mut()[..len].copy_from_slice(&str.as_bytes()[..len]);
187 }
188 }
189 Err(_) => todo!(),
190 }
191 }
192}