1use core::cell::UnsafeCell;
14use core::mem::MaybeUninit;
15
16use crate::bindings;
17use crate::error::{Error, Result};
18
19#[derive(Clone, Copy, Debug, Default)]
35pub struct WaitFlags(u32);
36
37impl WaitFlags {
38 pub const NONE: Self = Self(0);
40 pub const WAIT_ALL: Self = Self(0x01);
42 pub const CLEAR_ON_EXIT: Self = Self(0x02);
44}
45
46impl core::ops::BitOr for WaitFlags {
47 type Output = Self;
48 fn bitor(self, rhs: Self) -> Self {
49 Self(self.0 | rhs.0)
50 }
51}
52
53#[deprecated(note = "use WaitFlags::WAIT_ALL instead")]
55pub const EG_WAIT_ALL: WaitFlags = WaitFlags::WAIT_ALL;
56#[deprecated(note = "use WaitFlags::CLEAR_ON_EXIT instead")]
58pub const EG_CLEAR_ON_EXIT: WaitFlags = WaitFlags::CLEAR_ON_EXIT;
59
60#[allow(dead_code)]
63pub struct EventGroupStorage {
64 storage: UnsafeCell<MaybeUninit<bindings::ove_eventgroup_storage_t>>,
65}
66
67impl EventGroupStorage {
68 #[inline]
70 pub const fn new() -> Self {
71 Self {
72 storage: UnsafeCell::new(MaybeUninit::zeroed()),
73 }
74 }
75}
76
77impl Default for EventGroupStorage {
78 fn default() -> Self {
79 Self::new()
80 }
81}
82
83unsafe impl Sync for EventGroupStorage {}
85
86pub struct EventGroup {
88 handle: bindings::ove_eventgroup_t,
89}
90
91impl EventGroup {
92 #[cfg(not(zero_heap))]
94 pub fn new() -> Result<Self> {
95 let mut handle: bindings::ove_eventgroup_t = core::ptr::null_mut();
96 let rc = unsafe { bindings::ove_eventgroup_create(&mut handle) };
97 Error::from_code(rc)?;
98 Ok(Self { handle })
99 }
100
101 #[cfg(zero_heap)]
106 pub unsafe fn from_static(storage: *mut bindings::ove_eventgroup_storage_t) -> Result<Self> {
107 let mut handle: bindings::ove_eventgroup_t = core::ptr::null_mut();
108 let rc = unsafe { bindings::ove_eventgroup_init(&mut handle, storage) };
109 Error::from_code(rc)?;
110 Ok(Self { handle })
111 }
112
113 pub fn create(storage: &'static EventGroupStorage) -> Result<Self> {
115 #[cfg(not(zero_heap))]
116 {
117 let _ = storage;
118 Self::new()
119 }
120 #[cfg(zero_heap)]
121 {
122 let ptr = UnsafeCell::raw_get(&storage.storage).cast();
123 unsafe { Self::from_static(ptr) }
124 }
125 }
126
127 #[inline]
129 pub fn set_bits(&self, bits: u32) -> u32 {
130 unsafe { bindings::ove_eventgroup_set_bits(self.handle, bits) }
131 }
132
133 #[inline]
135 pub fn clear_bits(&self, bits: u32) -> u32 {
136 unsafe { bindings::ove_eventgroup_clear_bits(self.handle, bits) }
137 }
138
139 #[inline]
145 pub fn wait_bits(&self, bits: u32, flags: WaitFlags) -> Result<u32> {
146 self.wait_bits_with_timeout(bits, flags, u64::MAX)
147 }
148
149 #[inline]
154 pub fn try_wait_bits(&self, bits: u32, flags: WaitFlags) -> Result<u32> {
155 self.wait_bits_with_timeout(bits, flags, 0)
156 }
157
158 #[inline]
160 pub fn wait_bits_for(
161 &self,
162 bits: u32,
163 flags: WaitFlags,
164 d: core::time::Duration,
165 ) -> Result<u32> {
166 self.wait_bits_with_timeout(bits, flags, crate::time::dur_to_ns(d))
167 }
168
169 #[inline]
173 pub fn wait_bits_until(
174 &self,
175 bits: u32,
176 flags: WaitFlags,
177 deadline: crate::time::Instant,
178 ) -> Result<u32> {
179 self.wait_bits_with_timeout(bits, flags, crate::time::deadline_to_timeout_ns(deadline))
180 }
181
182 #[inline]
183 fn wait_bits_with_timeout(&self, bits: u32, flags: WaitFlags, timeout_ns: u64) -> Result<u32> {
184 let mut result: u32 = 0;
185 let rc = unsafe {
186 bindings::ove_eventgroup_wait_bits(self.handle, bits, flags.0, timeout_ns, &mut result)
187 };
188 Error::from_code(rc)?;
189 Ok(result)
190 }
191
192 #[inline]
194 pub fn set_bits_from_isr(&self, bits: u32) -> u32 {
195 unsafe { bindings::ove_eventgroup_set_bits_from_isr(self.handle, bits) }
196 }
197
198 #[inline]
200 pub fn get_bits(&self) -> u32 {
201 unsafe { bindings::ove_eventgroup_get_bits(self.handle) }
202 }
203
204 #[cfg(has_async)]
213 #[inline]
214 pub unsafe fn set_notify(
215 &self,
216 cb: Option<unsafe extern "C" fn(*mut core::ffi::c_void)>,
217 user_data: *mut core::ffi::c_void,
218 ) -> Result<()> {
219 let rc = unsafe { bindings::ove_eventgroup_set_notify(self.handle, cb, user_data) };
220 Error::from_code(rc)
221 }
222}
223
224crate::ove_handle_impl!(EventGroup, ove_eventgroup_destroy, ove_eventgroup_deinit);