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