use std::time::Duration;
use super::{
Error, OperationType, Result, gpiod,
line::{self, InfoChangeKind},
};
#[derive(Debug, Eq, PartialEq)]
pub struct Event {
pub(crate) event: *mut gpiod::gpiod_info_event,
}
unsafe impl Send for Event {}
impl Event {
pub(crate) unsafe fn from_raw(event: *mut gpiod::gpiod_info_event) -> Self {
Self { event }
}
pub fn event_type(&self) -> Result<InfoChangeKind> {
InfoChangeKind::new(unsafe { gpiod::gpiod_info_event_get_event_type(self.event) })
}
pub fn timestamp(&self) -> Duration {
Duration::from_nanos(unsafe { gpiod::gpiod_info_event_get_timestamp_ns(self.event) })
}
pub fn line_info(&self) -> Result<&line::InfoRef> {
let info = unsafe { gpiod::gpiod_info_event_get_line_info(self.event) };
if info.is_null() {
return Err(Error::OperationFailed(
OperationType::InfoEventGetLineInfo,
errno::errno(),
));
}
Ok(unsafe { line::InfoRef::from_raw(info) })
}
}
impl Drop for Event {
fn drop(&mut self) {
unsafe { gpiod::gpiod_info_event_free(self.event) }
}
}