2023-07-21 03:51:49 +02:00
|
|
|
use core::future::Future;
|
|
|
|
use core::task;
|
2023-07-21 23:10:34 +02:00
|
|
|
use core::task::Poll;
|
2023-07-16 02:15:01 +02:00
|
|
|
|
2023-07-21 03:51:49 +02:00
|
|
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
|
|
|
use embassy_sync::mutex::MutexGuard;
|
|
|
|
use embassy_sync::signal::Signal;
|
|
|
|
use futures::FutureExt;
|
|
|
|
|
|
|
|
use super::commands::MacCommand;
|
2023-07-21 23:10:34 +02:00
|
|
|
use super::event::MacEvent;
|
2023-07-21 03:51:49 +02:00
|
|
|
use super::typedefs::MacError;
|
|
|
|
use crate::mac::runner::Runner;
|
2023-07-16 02:15:01 +02:00
|
|
|
|
|
|
|
pub struct Control<'a> {
|
2023-07-19 01:28:12 +02:00
|
|
|
runner: &'a Runner<'a>,
|
2023-07-16 02:15:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Control<'a> {
|
2023-07-19 01:28:12 +02:00
|
|
|
pub(crate) fn new(runner: &'a Runner<'a>) -> Self {
|
|
|
|
Self { runner: runner }
|
|
|
|
}
|
2023-07-16 02:15:01 +02:00
|
|
|
|
2023-07-21 03:51:49 +02:00
|
|
|
pub async fn send_command<T>(&self, cmd: &T) -> Result<(), MacError>
|
|
|
|
where
|
|
|
|
T: MacCommand,
|
|
|
|
{
|
|
|
|
let _wm = self.runner.write_mutex.lock().await;
|
|
|
|
|
|
|
|
self.runner.mac_subsystem.send_command(cmd).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_command_and_get_response<T>(&self, cmd: &T) -> Result<EventToken<'a>, MacError>
|
|
|
|
where
|
|
|
|
T: MacCommand,
|
|
|
|
{
|
|
|
|
let rm = self.runner.read_mutex.lock().await;
|
2023-07-22 00:02:36 +02:00
|
|
|
let _wm = self.runner.write_mutex.lock().await;
|
2023-07-21 03:51:49 +02:00
|
|
|
let token = EventToken::new(self.runner, rm);
|
|
|
|
|
|
|
|
self.runner.mac_subsystem.send_command(cmd).await?;
|
|
|
|
|
|
|
|
Ok(token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EventToken<'a> {
|
|
|
|
runner: &'a Runner<'a>,
|
|
|
|
_mutex_guard: MutexGuard<'a, CriticalSectionRawMutex, ()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> EventToken<'a> {
|
|
|
|
pub(crate) fn new(runner: &'a Runner<'a>, mutex_guard: MutexGuard<'a, CriticalSectionRawMutex, ()>) -> Self {
|
|
|
|
// Enable event receiving
|
|
|
|
runner.rx_event_channel.lock(|s| {
|
|
|
|
*s.borrow_mut() = Some(Signal::new());
|
|
|
|
});
|
|
|
|
|
|
|
|
Self {
|
|
|
|
runner: runner,
|
|
|
|
_mutex_guard: mutex_guard,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Future for EventToken<'a> {
|
2023-07-21 23:10:34 +02:00
|
|
|
type Output = MacEvent<'a>;
|
2023-07-21 03:51:49 +02:00
|
|
|
|
2023-07-21 23:10:34 +02:00
|
|
|
fn poll(self: core::pin::Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
|
2023-07-21 03:51:49 +02:00
|
|
|
self.get_mut().runner.rx_event_channel.lock(|s| {
|
|
|
|
let signal = s.borrow_mut();
|
|
|
|
let signal = match &*signal {
|
|
|
|
Some(s) => s,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2023-07-21 23:10:34 +02:00
|
|
|
let result = match signal.wait().poll_unpin(cx) {
|
|
|
|
Poll::Ready(mac_event) => Poll::Ready(mac_event),
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
};
|
2023-07-21 03:51:49 +02:00
|
|
|
|
2023-07-21 23:10:34 +02:00
|
|
|
result
|
|
|
|
})
|
2023-07-21 03:51:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Drop for EventToken<'a> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// Disable event receiving
|
|
|
|
// This will also drop the contained event, if it exists, and will free up receiving the next event
|
|
|
|
self.runner.rx_event_channel.lock(|s| {
|
|
|
|
*s.borrow_mut() = None;
|
|
|
|
});
|
2023-07-16 02:15:01 +02:00
|
|
|
}
|
|
|
|
}
|