use core::future::Future;
use super::channel::{Channel, ChannelId, SelectResult};
use super::error::Error;
use super::protocol::features::Feature;
pub trait Session {
type IoError;
fn advertises(&self, feature: &Feature) -> Option<bool>;
fn require_feature(&self, feature: Feature) -> Result<(), Error<Self::IoError>> {
match self.advertises(&feature) {
Some(false) => Err(Error::MissingFeature(feature)),
_ => Ok(()),
}
}
fn open_channel(
&mut self,
destination: &[u8],
) -> impl Future<Output = Result<ChannelId, Error<Self::IoError>>>;
fn read_channel(
&mut self,
ch: ChannelId,
buf: &mut [u8],
) -> impl Future<Output = Result<usize, Error<Self::IoError>>>;
fn write_channel(
&mut self,
ch: ChannelId,
data: &[u8],
) -> impl Future<Output = Result<(), Error<Self::IoError>>>;
fn select_channel<F: Future>(
&mut self,
ch: ChannelId,
buf: &mut [u8],
interrupt: F,
) -> impl Future<Output = Result<SelectResult<F::Output>, Error<Self::IoError>>>;
fn close_channel(
&mut self,
ch: ChannelId,
) -> impl Future<Output = Result<(), Error<Self::IoError>>>;
fn open(
&mut self,
destination: &[u8],
) -> impl Future<Output = Result<Channel<'_, Self>, Error<Self::IoError>>>
where
Self: Sized,
{
async move {
let id = self.open_channel(destination).await?;
Ok(Channel { session: self, id })
}
}
}
impl<T, const MC: usize, const MP: usize, const MF: usize> Session
for super::connection::Connection<T, MC, MP, MF>
where
T: embedded_io_async::Read + embedded_io_async::Write + crate::transport::ReadCancelSafety,
{
type IoError = <T as embedded_io::ErrorType>::Error;
fn advertises(&self, feature: &Feature) -> Option<bool> {
self.device_banner_parsed().map(|b| b.has_feature(feature))
}
async fn open_channel(
&mut self,
destination: &[u8],
) -> Result<ChannelId, Error<Self::IoError>> {
Self::open_channel(self, destination).await
}
async fn read_channel(
&mut self,
ch: ChannelId,
buf: &mut [u8],
) -> Result<usize, Error<Self::IoError>> {
Self::read_channel(self, ch, buf).await
}
async fn write_channel(
&mut self,
ch: ChannelId,
data: &[u8],
) -> Result<(), Error<Self::IoError>> {
Self::write_channel(self, ch, data).await
}
async fn select_channel<F: Future>(
&mut self,
ch: ChannelId,
buf: &mut [u8],
interrupt: F,
) -> Result<SelectResult<F::Output>, Error<Self::IoError>> {
Self::select_channel(self, ch, buf, interrupt).await
}
async fn close_channel(&mut self, ch: ChannelId) -> Result<(), Error<Self::IoError>> {
Self::close_channel(self, ch).await
}
}
#[cfg(feature = "split")]
impl<WH, const MC: usize, const MP: usize, const MF: usize> Session
for crate::split::Writer<WH, MC, MP, MF>
where
WH: embedded_io_async::Write,
{
type IoError = <WH as embedded_io::ErrorType>::Error;
fn advertises(&self, feature: &Feature) -> Option<bool> {
self.device_banner_parsed().map(|b| b.has_feature(feature))
}
async fn open_channel(
&mut self,
destination: &[u8],
) -> Result<ChannelId, Error<Self::IoError>> {
Self::open_channel(self, destination).await
}
async fn read_channel(
&mut self,
ch: ChannelId,
buf: &mut [u8],
) -> Result<usize, Error<Self::IoError>> {
Self::read_channel(self, ch, buf).await
}
async fn write_channel(
&mut self,
ch: ChannelId,
data: &[u8],
) -> Result<(), Error<Self::IoError>> {
Self::write_channel(self, ch, data).await
}
async fn select_channel<F: Future>(
&mut self,
ch: ChannelId,
buf: &mut [u8],
interrupt: F,
) -> Result<SelectResult<F::Output>, Error<Self::IoError>> {
Self::select_channel(self, ch, buf, interrupt).await
}
async fn close_channel(&mut self, ch: ChannelId) -> Result<(), Error<Self::IoError>> {
Self::close_channel(self, ch).await
}
}