use core::time::Duration;
use crate::{
services::serial_mux::{PortHandle, WellKnown},
tracing, Kernel,
};
#[derive(Debug, Clone)]
pub struct LoopbackSettings {
pub port: u16,
pub buffer_size: usize,
_priv: (),
}
impl Default for LoopbackSettings {
fn default() -> Self {
Self {
port: WellKnown::Loopback as u16,
buffer_size: 128,
_priv: (),
}
}
}
#[tracing::instrument(skip(kernel))]
pub async fn loopback(kernel: &'static Kernel, settings: LoopbackSettings) {
let LoopbackSettings {
port,
buffer_size,
_priv,
} = settings;
tracing::debug!("initializing SerMux loopback...");
let p0 = PortHandle::open(kernel, port, buffer_size).await.unwrap();
tracing::info!("SerMux Loopback running!");
loop {
let rgr = p0.consumer().read_grant().await;
let len = rgr.len();
tracing::trace!("Loopback read {len}B");
p0.send(&rgr).await;
rgr.release(len);
}
}
#[derive(Debug, Clone)]
pub struct HelloSettings {
pub port: u16,
pub buffer_size: usize,
pub message: &'static [u8],
pub interval: Duration,
_priv: (),
}
impl Default for HelloSettings {
fn default() -> Self {
Self {
port: WellKnown::HelloWorld as u16,
buffer_size: 32,
message: b"hello\r\n",
interval: Duration::from_secs(1),
_priv: (),
}
}
}
#[tracing::instrument(skip(kernel))]
pub async fn hello(kernel: &'static Kernel, settings: HelloSettings) {
let HelloSettings {
port,
buffer_size,
message,
interval,
_priv,
} = settings;
tracing::debug!("Starting SerMux 'hello world'...");
let p1 = PortHandle::open(kernel, port, buffer_size).await.unwrap();
tracing::info!("SerMux 'hello world' running!");
loop {
kernel.sleep(interval).await;
p1.send(message).await;
}
}