use crate::file_util::Nonblocking;
use gpio_cdev::EventType;
use gpio_cdev::LineEvent;
use gpio_cdev::LineEventHandle;
use std::error::Error;
use std::io;
use std::time::{Duration, Instant};
#[allow(unused)]
pub trait EdgeWait {
/// Waits the given edge type. Ignores the opposite edge type.
fn wait_for_edge(&mut self, edge: EventType, deadline: Instant) -> io::Result<()>;
/// Get an event with a timeout.
///
/// Returns Ok(None) on timeout.
fn get_event_timeout(
&mut self,
timeout: Duration,
) -> Result<Option<LineEvent>, gpio_cdev::Error>;
fn drain_events(&mut self) -> io::Result<()>;
}
impl EdgeWait for LineEventHandle {
fn wait_for_edge(&mut self, edge: EventType, deadline: Instant) -> io::Result<()> {
// Wait using blocking read.
loop {
// Compute the timeout until deadline.
let Some(timeout) = deadline.checked_duration_since(Instant::now()) else {
return Err(irq_timeout_err());
};
// Block and wait for an event.
let event = self
.get_event_timeout(timeout)
.map_err(io::Error::other)?
.ok_or_else(irq_timeout_err)?;
if event.event_type() == edge {
break;
}
}
Ok(())
}
fn get_event_timeout(
&mut self,
timeout: Duration,
) -> Result<Option<LineEvent>, gpio_cdev::Error> {
use nix::poll::{poll, PollFd, PollFlags, PollTimeout};
use std::os::fd::AsFd;
// Wait for an event
let poll_timeout =
PollTimeout::try_from(timeout).expect("Failure converting to PollTimeout!");
let mut poll_fds = [PollFd::new(self.file().as_fd(), PollFlags::POLLIN)];
let poll_res = poll(&mut poll_fds, poll_timeout).map_err(poll_to_gpio_err)?;
if poll_res == 0 {
return Ok(None);
}
assert_eq!(poll_res, 1);
assert_eq!(poll_fds[0].any(), Some(true));
// Read the event
Ok(Some(self.get_event()?))
}
fn drain_events(&mut self) -> io::Result<()> {
// Make the file non-blocking.
let orig_nonblocking = self.file().get_nonblocking()?;
if !orig_nonblocking {
self.file().set_nonblocking(true)?;
}
// Consume the line events until the kernel queue is empty.
let result = loop {
match self.get_event() {
Ok(_event) => continue,
Err(err) => {
// This should return EAGAIN/EWOULDBLOCK.
if let Some(io_err) = err.source().and_then(|e| e.downcast_ref::<io::Error>()) {
if io_err.kind() == io::ErrorKind::WouldBlock {
break Ok(());
}
}
break Err(io::Error::new(io::ErrorKind::Other, err));
}
}
};
if !orig_nonblocking {
// Restore original blocking mode.
let _ = self.file().set_nonblocking(false);
}
result
}
}
#[allow(dead_code)]
fn poll_to_gpio_err(err: nix::errno::Errno) -> gpio_cdev::Error {
gpio_cdev::Error::from(std::io::Error::other(err))
}
#[allow(dead_code)]
fn irq_timeout_err() -> io::Error {
io::Error::new(io::ErrorKind::TimedOut, "Timed out waiting for IRQ line")
}
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.103Angebot
¤
*Eine klare Vorstellung vom Zielzustand