use core::convert::Infallible;
use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::channel::Sender;
use embassy_sync::signal::Signal;
use crate::transport::ClientTransport;
pub struct EmbassyClient<'a, M: RawMutex + 'static, Req, Resp: 'static, const CHANNEL_DEPTH: usize>
{
sender: Sender<'a, M, (Req, &'static Signal<M, Resp>), CHANNEL_DEPTH>,
reply: Signal<M, Resp>,
}
impl<'a, M: RawMutex + 'static, Req, Resp: 'static, const CHANNEL_DEPTH: usize>
EmbassyClient<'a, M, Req, Resp, CHANNEL_DEPTH>
{
pub(crate) fn new(
sender: Sender<'a, M, (Req, &'static Signal<M, Resp>), CHANNEL_DEPTH>,
) -> Self {
Self {
sender,
reply: Signal::new(),
}
}
}
impl<'a, M: RawMutex + 'static, Req, Resp: 'static, const CHANNEL_DEPTH: usize>
ClientTransport<Req, Resp> for EmbassyClient<'a, M, Req, Resp, CHANNEL_DEPTH>
{
type Error = Infallible;
async fn call(&self, req: Req) -> Result<Resp, Self::Error> {
self.reply.reset();
let signal_ref: &'static Signal<M, Resp> = unsafe { &*core::ptr::from_ref(&self.reply) };
self.sender.send((req, signal_ref)).await;
let resp = self.reply.wait().await;
Ok(resp)
}
}