use crate::protocol::START_BYTE;
use downcast_rs::{impl_downcast, Downcast};
use std::io;
use std::time::{Duration, Instant};
/// Represents the transport by which a target device can be communicated with,
/// e.g. SPI, I2C, UART.
pub trait Transport: Downcast {
/// Reads data from the target device.
fn read(&mut self, buf: &mut [u8], deadline: Instant) -> io::Result<()>;
/// Read the start of a packet.
///
/// Performs any polling as necessary.
///
/// The default implementation defers to read().
///
/// # Arguments
///
/// * `buf` - The packet buffer. Must be 2 as it includes
/// the start byte and packet type.
fn read_start(&mut self, buf: &mut [u8], deadline: Instant) -> io::Result<()> {
assert_eq!(buf.len(), 2);
self.read(buf, deadline)?;
let start_byte = buf[0];
if start_byte != START_BYTE {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Invalid start byte: 0x{start_byte:X}"),
));
}
Ok(())
}
/// Writes data to the target device.
fn write(&mut self, buf: &[u8], deadline: Instant) -> io::Result<()>;
fn set_irq_line(&mut self, line: &gpio_cdev::Line) -> io::Result<()> {
let _ = line;
Err(io::Error::new(
io::ErrorKind::Other,
"irq line not supported",
))
}
}
impl_downcast!(Transport);
pub fn to_timeout(deadline: Instant) -> io::Result<Duration> {
deadline
.checked_duration_since(Instant::now())
.ok_or_else(|| io::Error::new(io::ErrorKind::TimedOut, "Timed out"))
}
¤ Dauer der Verarbeitung: 0.19 Sekunden
¤
*© Formatika GbR, Deutschland