diff options
| author | Raul Alimbekov <[email protected]> | 2025-12-16 09:05:22 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-16 09:05:22 +0300 |
| commit | c9a04b4b732b7a3b696eb8223664c1a7942b1875 (patch) | |
| tree | 6dbe5c02e66eed8d8762f13f95afd24f8db2b38c /embassy-stm32-wpan/src/mac/control.rs | |
| parent | cde24a3ef1117653ba5ed4184102b33f745782fb (diff) | |
| parent | 5ae6e060ec1c90561719aabdc29d5b6e7b8b0a82 (diff) | |
Merge branch 'main' into main
Diffstat (limited to 'embassy-stm32-wpan/src/mac/control.rs')
| -rw-r--r-- | embassy-stm32-wpan/src/mac/control.rs | 95 |
1 files changed, 0 insertions, 95 deletions
diff --git a/embassy-stm32-wpan/src/mac/control.rs b/embassy-stm32-wpan/src/mac/control.rs deleted file mode 100644 index e8d2f9f7b..000000000 --- a/embassy-stm32-wpan/src/mac/control.rs +++ /dev/null | |||
| @@ -1,95 +0,0 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | use core::task; | ||
| 3 | use core::task::Poll; | ||
| 4 | |||
| 5 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 6 | use embassy_sync::mutex::MutexGuard; | ||
| 7 | use embassy_sync::signal::Signal; | ||
| 8 | use futures_util::FutureExt; | ||
| 9 | |||
| 10 | use super::commands::MacCommand; | ||
| 11 | use super::event::MacEvent; | ||
| 12 | use super::typedefs::MacError; | ||
| 13 | use crate::mac::runner::Runner; | ||
| 14 | |||
| 15 | pub struct Control<'a> { | ||
| 16 | runner: &'a Runner<'a>, | ||
| 17 | } | ||
| 18 | |||
| 19 | impl<'a> Control<'a> { | ||
| 20 | pub(crate) fn new(runner: &'a Runner<'a>) -> Self { | ||
| 21 | Self { runner: runner } | ||
| 22 | } | ||
| 23 | |||
| 24 | pub async fn send_command<T>(&self, cmd: &T) -> Result<(), MacError> | ||
| 25 | where | ||
| 26 | T: MacCommand, | ||
| 27 | { | ||
| 28 | let _wm = self.runner.write_mutex.lock().await; | ||
| 29 | |||
| 30 | self.runner.mac_subsystem.send_command(cmd).await | ||
| 31 | } | ||
| 32 | |||
| 33 | pub async fn send_command_and_get_response<T>(&self, cmd: &T) -> Result<EventToken<'a>, MacError> | ||
| 34 | where | ||
| 35 | T: MacCommand, | ||
| 36 | { | ||
| 37 | let rm = self.runner.read_mutex.lock().await; | ||
| 38 | let _wm = self.runner.write_mutex.lock().await; | ||
| 39 | let token = EventToken::new(self.runner, rm); | ||
| 40 | |||
| 41 | self.runner.mac_subsystem.send_command(cmd).await?; | ||
| 42 | |||
| 43 | Ok(token) | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | pub struct EventToken<'a> { | ||
| 48 | runner: &'a Runner<'a>, | ||
| 49 | _mutex_guard: MutexGuard<'a, CriticalSectionRawMutex, ()>, | ||
| 50 | } | ||
| 51 | |||
| 52 | impl<'a> EventToken<'a> { | ||
| 53 | pub(crate) fn new(runner: &'a Runner<'a>, mutex_guard: MutexGuard<'a, CriticalSectionRawMutex, ()>) -> Self { | ||
| 54 | // Enable event receiving | ||
| 55 | runner.rx_event_channel.lock(|s| { | ||
| 56 | *s.borrow_mut() = Some(Signal::new()); | ||
| 57 | }); | ||
| 58 | |||
| 59 | Self { | ||
| 60 | runner: runner, | ||
| 61 | _mutex_guard: mutex_guard, | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | impl<'a> Future for EventToken<'a> { | ||
| 67 | type Output = MacEvent<'a>; | ||
| 68 | |||
| 69 | fn poll(self: core::pin::Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> { | ||
| 70 | self.get_mut().runner.rx_event_channel.lock(|s| { | ||
| 71 | let signal = s.borrow_mut(); | ||
| 72 | let signal = match &*signal { | ||
| 73 | Some(s) => s, | ||
| 74 | _ => unreachable!(), | ||
| 75 | }; | ||
| 76 | |||
| 77 | let result = match signal.wait().poll_unpin(cx) { | ||
| 78 | Poll::Ready(mac_event) => Poll::Ready(mac_event), | ||
| 79 | Poll::Pending => Poll::Pending, | ||
| 80 | }; | ||
| 81 | |||
| 82 | result | ||
| 83 | }) | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | impl<'a> Drop for EventToken<'a> { | ||
| 88 | fn drop(&mut self) { | ||
| 89 | // Disable event receiving | ||
| 90 | // This will also drop the contained event, if it exists, and will free up receiving the next event | ||
| 91 | self.runner.rx_event_channel.lock(|s| { | ||
| 92 | *s.borrow_mut() = None; | ||
| 93 | }); | ||
| 94 | } | ||
| 95 | } | ||
