use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, DuplexStream, ReadBuf};
use super::*;
pub struct LoopbackIo(DuplexStream);
impl AsyncRead for LoopbackIo {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_read(cx, buf)
}
}
impl AsyncWrite for LoopbackIo {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
Pin::new(&mut self.0).poll_write(cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Pin::new(&mut self.0).poll_shutdown(cx)
}
}
impl SerialIo for LoopbackIo {
fn clear_os_buffers(&self, _target: FlushTarget) -> std::io::Result<()> {
Ok(())
}
fn set_dtr_rts(&mut self, _dtr: bool, _rts: bool) -> std::io::Result<()> {
Ok(())
}
fn set_flow_control(&mut self, _flow_control: FlowControl) -> std::io::Result<()> {
Ok(())
}
fn set_break_state(&self, _asserted: bool) -> std::io::Result<()> {
Ok(())
}
}
pub fn loopback_connection(port: &str) -> (SerialConnection, DuplexStream) {
let (a, b) = tokio::io::duplex(4096);
let conn = SerialConnection::from_io(port.to_string(), Box::new(LoopbackIo(a)));
(conn, b)
}
pub fn loopback_connection_with_config(
config: ConnectionConfig,
) -> (SerialConnection, DuplexStream) {
let (a, b) = tokio::io::duplex(4096);
let conn = SerialConnection::from_io_with_config(config, Box::new(LoopbackIo(a)));
(conn, b)
}
struct QueuedTxState {
tx_queue: std::collections::VecDeque<u8>,
rx_queue: std::collections::VecDeque<u8>,
rx_waker: Option<std::task::Waker>,
}
impl QueuedTxState {
fn new() -> Self {
Self {
tx_queue: std::collections::VecDeque::new(),
rx_queue: std::collections::VecDeque::new(),
rx_waker: None,
}
}
}
pub struct QueuedTxIo {
state: std::sync::Arc<std::sync::Mutex<QueuedTxState>>,
}
pub struct QueuedTxHandle {
state: std::sync::Arc<std::sync::Mutex<QueuedTxState>>,
}
pub fn queued_tx_connection(port: &str) -> (SerialConnection, QueuedTxHandle) {
let state = std::sync::Arc::new(std::sync::Mutex::new(QueuedTxState::new()));
let conn = SerialConnection::from_io(
port.to_string(),
Box::new(QueuedTxIo {
state: state.clone(),
}),
);
(conn, QueuedTxHandle { state })
}
impl QueuedTxHandle {
pub fn tx_queue_len(&self) -> usize {
self.state.lock().expect("tx state poisoned").tx_queue.len()
}
pub fn drain_tx(&self, max: usize) -> Vec<u8> {
let mut state = self.state.lock().expect("tx state poisoned");
let take = max.min(state.tx_queue.len());
let mut out = Vec::with_capacity(take);
for _ in 0..take {
out.push(state.tx_queue.pop_front().expect("len checked"));
}
out
}
pub fn inject_rx(&self, bytes: &[u8]) {
let mut state = self.state.lock().expect("tx state poisoned");
state.rx_queue.extend(bytes);
if let Some(w) = state.rx_waker.take() {
w.wake();
}
}
pub fn rx_queue_len(&self) -> usize {
self.state.lock().expect("tx state poisoned").rx_queue.len()
}
}
impl AsyncRead for QueuedTxIo {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<std::io::Result<()>> {
let mut state = self.state.lock().expect("tx state poisoned");
let n = buf.initialize_unfilled().len().min(state.rx_queue.len());
if n == 0 {
state.rx_waker = Some(_cx.waker().clone());
return Poll::Pending;
}
let filled = buf.initialize_unfilled();
for slot in filled.iter_mut().take(n) {
*slot = state.rx_queue.pop_front().expect("len checked");
}
buf.advance(n);
Poll::Ready(Ok(()))
}
}
impl AsyncWrite for QueuedTxIo {
fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
let mut state = self.state.lock().expect("tx state poisoned");
state.tx_queue.extend(buf);
Poll::Ready(Ok(buf.len()))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Poll::Ready(Ok(()))
}
}
impl SerialIo for QueuedTxIo {
fn clear_os_buffers(&self, target: FlushTarget) -> std::io::Result<()> {
let mut state = self.state.lock().expect("tx state poisoned");
match target {
FlushTarget::Input => {
state.rx_queue.clear();
}
FlushTarget::Output => {
state.tx_queue.clear();
}
FlushTarget::Both => {
state.tx_queue.clear();
state.rx_queue.clear();
}
}
Ok(())
}
fn set_dtr_rts(&mut self, _dtr: bool, _rts: bool) -> std::io::Result<()> {
Ok(())
}
fn set_flow_control(&mut self, _flow_control: FlowControl) -> std::io::Result<()> {
Ok(())
}
fn set_break_state(&self, _asserted: bool) -> std::io::Result<()> {
Ok(())
}
}