use core::future::Future;
use embedded_io::ErrorType;
use embedded_io_async::{Read, Write};
use crate::base::channel::{Channel, SelectResult};
use crate::base::connection::{
Connection, DEFAULT_MAX_CHANNELS, DEFAULT_MAX_FEATURES, DEFAULT_MAX_PROPERTIES,
};
use crate::base::destination;
use crate::base::error::Error;
fn write_destination(buf: &mut [u8], command: &str) -> Option<usize> {
destination::write_destination(buf, &[b"shell:", command.as_bytes()], &[])
}
pub struct Shell<
'a,
T,
const MAX_CHANNELS: usize = DEFAULT_MAX_CHANNELS,
const MAX_PROPERTIES: usize = DEFAULT_MAX_PROPERTIES,
const MAX_FEATURES: usize = DEFAULT_MAX_FEATURES,
> where
T: Read + Write,
{
channel: Channel<'a, T, MAX_CHANNELS, MAX_PROPERTIES, MAX_FEATURES>,
}
impl<'a, T, const MAX_CHANNELS: usize, const MAX_PROPERTIES: usize, const MAX_FEATURES: usize>
Shell<'a, T, MAX_CHANNELS, MAX_PROPERTIES, MAX_FEATURES>
where
T: Read + Write,
{
pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error<<T as ErrorType>::Error>> {
self.channel.read(buf).await
}
pub async fn write_stdin(&mut self, data: &[u8]) -> Result<(), Error<<T as ErrorType>::Error>> {
self.channel.write(data).await
}
pub async fn select<F: Future>(
&mut self,
buf: &mut [u8],
interrupt: F,
) -> Result<SelectResult<F::Output>, Error<<T as ErrorType>::Error>> {
self.channel.select(buf, interrupt).await
}
pub async fn close(self) -> Result<(), Error<<T as ErrorType>::Error>> {
self.channel.close().await
}
}
pub async fn open<'a, T, const MC: usize, const MP: usize, const MF: usize>(
conn: &'a mut Connection<T, MC, MP, MF>,
command: &str,
dest_buf: &mut [u8],
) -> Result<Shell<'a, T, MC, MP, MF>, Error<<T as ErrorType>::Error>>
where
T: Read + Write,
{
let dest_len = write_destination(dest_buf, command).ok_or(Error::ReceiveBufferFull)?;
let channel = conn.open(&dest_buf[..dest_len]).await?;
Ok(Shell { channel })
}
pub async fn exec<T, const MC: usize, const MP: usize, const MF: usize>(
conn: &mut Connection<T, MC, MP, MF>,
command: &str,
buf: &mut [u8],
) -> Result<usize, Error<<T as ErrorType>::Error>>
where
T: Read + Write,
{
let dest_len = write_destination(buf, command).ok_or(Error::ReceiveBufferFull)?;
let channel = conn.open(&buf[..dest_len]).await?;
let mut shell = Shell { channel };
let mut tail = 0;
loop {
if tail >= buf.len() {
return Err(Error::ReceiveBufferFull);
}
match shell.read(&mut buf[tail..]).await {
Ok(n) => tail += n,
Err(Error::ChannelClosed) => {
let _ = shell.close().await;
return Ok(tail);
}
Err(e) => return Err(e),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn destination_empty_command() {
let mut buf = [0u8; 32];
let n = write_destination(&mut buf, "").unwrap();
assert_eq!(&buf[..n], b"shell:\0");
}
#[test]
fn destination_with_command() {
let mut buf = [0u8; 32];
let n = write_destination(&mut buf, "ls /sdcard").unwrap();
assert_eq!(&buf[..n], b"shell:ls /sdcard\0");
}
#[test]
fn destination_buffer_exact_fit() {
let mut buf = [0u8; 8];
let n = write_destination(&mut buf, "x").unwrap();
assert_eq!(&buf[..n], b"shell:x\0");
}
#[test]
fn destination_buffer_too_small() {
let mut buf = [0u8; 7];
assert_eq!(write_destination(&mut buf, "x"), None);
}
}