use core::future::Future;
use super::channel::{Channel, ChannelId, SelectResult};
use super::protocol::features::Feature;
use crate::error::Error;
pub trait Session {
type IoError;
fn advertises(&self, feature: &Feature) -> bool;
fn require_feature(&self, feature: Feature) -> Result<(), Error<Self::IoError>> {
if self.advertises(&feature) {
Ok(())
} else {
Err(Error::MissingFeature(feature))
}
}
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> Session for super::connection::Connection<T>
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) -> bool {
self.negotiated().advertises(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> Session for crate::split::Writer<WH>
where
WH: embedded_io_async::Write,
{
type IoError = <WH as embedded_io::ErrorType>::Error;
fn advertises(&self, feature: &Feature) -> bool {
self.negotiated().advertises(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
}
}
pub trait ServiceChannel {
type IoError;
fn open_service_channel(
&mut self,
dest: &[u8],
) -> impl Future<Output = Result<ChannelId, Error<Self::IoError>>>;
fn read_service_channel(
&mut self,
ch: ChannelId,
buf: &mut [u8],
) -> impl Future<Output = Result<usize, Error<Self::IoError>>>;
fn close_service_channel(
&mut self,
ch: ChannelId,
) -> impl Future<Output = Result<(), Error<Self::IoError>>>;
}
impl<S: Session> ServiceChannel for S {
type IoError = S::IoError;
async fn open_service_channel(
&mut self,
dest: &[u8],
) -> Result<ChannelId, Error<Self::IoError>> {
self.open_channel(dest).await
}
async fn read_service_channel(
&mut self,
ch: ChannelId,
buf: &mut [u8],
) -> Result<usize, Error<Self::IoError>> {
self.read_channel(ch, buf).await
}
async fn close_service_channel(&mut self, ch: ChannelId) -> Result<(), Error<Self::IoError>> {
self.close_channel(ch).await
}
}