2023-06-09 03:36:48 +02:00
|
|
|
use core::cell::{Cell, RefCell};
|
|
|
|
use core::future::poll_fn;
|
|
|
|
use core::task::{Poll, Waker};
|
|
|
|
|
|
|
|
use embassy_sync::waitqueue::WakerRegistration;
|
|
|
|
|
|
|
|
use crate::fmt::Bytes;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct PendingIoctl {
|
|
|
|
pub buf: *mut [u8],
|
|
|
|
pub req_len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
2023-06-21 18:55:29 +02:00
|
|
|
enum IoctlState {
|
2023-06-09 03:36:48 +02:00
|
|
|
Pending(PendingIoctl),
|
|
|
|
Sent { buf: *mut [u8] },
|
|
|
|
Done { resp_len: usize },
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:55:29 +02:00
|
|
|
pub struct Shared(RefCell<SharedInner>);
|
2023-06-09 03:36:48 +02:00
|
|
|
|
2023-06-21 18:55:29 +02:00
|
|
|
struct SharedInner {
|
|
|
|
ioctl: IoctlState,
|
2023-06-21 19:05:20 +02:00
|
|
|
is_init: bool,
|
2023-06-21 18:55:29 +02:00
|
|
|
control_waker: WakerRegistration,
|
|
|
|
runner_waker: WakerRegistration,
|
2023-06-09 03:36:48 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 18:55:29 +02:00
|
|
|
impl Shared {
|
2023-06-09 03:36:48 +02:00
|
|
|
pub fn new() -> Self {
|
2023-06-21 18:55:29 +02:00
|
|
|
Self(RefCell::new(SharedInner {
|
|
|
|
ioctl: IoctlState::Done { resp_len: 0 },
|
2023-06-21 19:05:20 +02:00
|
|
|
is_init: false,
|
2023-06-21 18:55:29 +02:00
|
|
|
control_waker: WakerRegistration::new(),
|
|
|
|
runner_waker: WakerRegistration::new(),
|
|
|
|
}))
|
2023-06-09 03:36:48 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 18:55:29 +02:00
|
|
|
pub async fn ioctl_wait_complete(&self) -> usize {
|
2023-06-09 03:36:48 +02:00
|
|
|
poll_fn(|cx| {
|
2023-06-21 18:55:29 +02:00
|
|
|
let mut this = self.0.borrow_mut();
|
|
|
|
if let IoctlState::Done { resp_len } = this.ioctl {
|
2023-06-09 03:36:48 +02:00
|
|
|
Poll::Ready(resp_len)
|
|
|
|
} else {
|
2023-06-21 18:55:29 +02:00
|
|
|
this.control_waker.register(cx.waker());
|
2023-06-09 03:36:48 +02:00
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:55:29 +02:00
|
|
|
pub async fn ioctl_wait_pending(&self) -> PendingIoctl {
|
2023-06-09 03:36:48 +02:00
|
|
|
let pending = poll_fn(|cx| {
|
2023-06-21 18:55:29 +02:00
|
|
|
let mut this = self.0.borrow_mut();
|
|
|
|
if let IoctlState::Pending(pending) = this.ioctl {
|
2023-06-09 03:36:48 +02:00
|
|
|
Poll::Ready(pending)
|
|
|
|
} else {
|
2023-06-21 18:55:29 +02:00
|
|
|
this.runner_waker.register(cx.waker());
|
2023-06-09 03:36:48 +02:00
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
2023-06-21 18:55:29 +02:00
|
|
|
self.0.borrow_mut().ioctl = IoctlState::Sent { buf: pending.buf };
|
2023-06-09 03:36:48 +02:00
|
|
|
pending
|
|
|
|
}
|
|
|
|
|
2023-06-21 18:55:29 +02:00
|
|
|
pub fn ioctl_cancel(&self) {
|
|
|
|
self.0.borrow_mut().ioctl = IoctlState::Done { resp_len: 0 };
|
2023-06-09 03:36:48 +02:00
|
|
|
}
|
|
|
|
|
2023-06-21 18:55:29 +02:00
|
|
|
pub async fn ioctl(&self, buf: &mut [u8], req_len: usize) -> usize {
|
2023-06-21 18:13:19 +02:00
|
|
|
trace!("ioctl req bytes: {:02x}", Bytes(&buf[..req_len]));
|
2023-06-09 03:36:48 +02:00
|
|
|
|
2023-06-21 18:55:29 +02:00
|
|
|
{
|
|
|
|
let mut this = self.0.borrow_mut();
|
|
|
|
this.ioctl = IoctlState::Pending(PendingIoctl { buf, req_len });
|
|
|
|
this.runner_waker.wake();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ioctl_wait_complete().await
|
2023-06-09 03:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ioctl_done(&self, response: &[u8]) {
|
2023-06-21 18:55:29 +02:00
|
|
|
let mut this = self.0.borrow_mut();
|
|
|
|
if let IoctlState::Sent { buf } = this.ioctl {
|
2023-06-21 18:13:19 +02:00
|
|
|
trace!("ioctl resp bytes: {:02x}", Bytes(response));
|
2023-06-09 03:36:48 +02:00
|
|
|
|
|
|
|
// TODO fix this
|
|
|
|
(unsafe { &mut *buf }[..response.len()]).copy_from_slice(response);
|
|
|
|
|
2023-06-21 18:55:29 +02:00
|
|
|
this.ioctl = IoctlState::Done {
|
2023-06-09 03:36:48 +02:00
|
|
|
resp_len: response.len(),
|
2023-06-21 18:55:29 +02:00
|
|
|
};
|
|
|
|
this.control_waker.wake();
|
2023-06-09 03:36:48 +02:00
|
|
|
} else {
|
|
|
|
warn!("IOCTL Response but no pending Ioctl");
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 19:05:20 +02:00
|
|
|
|
|
|
|
// // // // // // // // // // // // // // // // // // // //
|
|
|
|
|
|
|
|
pub fn init_done(&self) {
|
|
|
|
let mut this = self.0.borrow_mut();
|
|
|
|
this.is_init = true;
|
|
|
|
this.control_waker.wake();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn init_wait(&self) {
|
|
|
|
poll_fn(|cx| {
|
|
|
|
let mut this = self.0.borrow_mut();
|
|
|
|
if this.is_init {
|
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
|
|
|
this.control_waker.register(cx.waker());
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
2023-06-09 03:36:48 +02:00
|
|
|
}
|