Skip to content

Commit

Permalink
Add no-timeout mode to wait_event
Browse files Browse the repository at this point in the history
  • Loading branch information
ChocolateLoverRaj committed Jun 8, 2024
1 parent 6da00c5 commit 80f4101
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
34 changes: 20 additions & 14 deletions crosec/src/wait_event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ pub enum PollData {
SomethingElseHappened(i16),
}

/// If no timeout is specified, this function will wait for an unlimited amount of time
pub fn wait_event(
file: &mut File,
event_type: EcMkbpEventType,
timeout: i32,
timeout: Option<i32>,
) -> Result<PollData, i32> {
let mask = 1 << event_type as u8;
unsafe {
Expand All @@ -33,18 +34,23 @@ pub fn wait_event(
mask,
)
};
let mut fds = pollfd {
fd: file.as_raw_fd(),
events: POLL_IN,
revents: Default::default(),
};
let result = unsafe { poll(&mut fds, 1, timeout) };
match result {
0 => Ok(PollData::Timeout),
1 => match fds.revents {
POLL_IN => Ok(PollData::EventHappened(event_type.read(file).unwrap())),
events => Ok(PollData::SomethingElseHappened(events)),
},
result => Err(result),
match timeout {
Some(timeout) => {
let mut fds = pollfd {
fd: file.as_raw_fd(),
events: POLL_IN,
revents: Default::default(),
};
let result = unsafe { poll(&mut fds, 1, timeout) };
match result {
0 => Ok(PollData::Timeout),
1 => match fds.revents {
POLL_IN => Ok(PollData::EventHappened(event_type.read(file).unwrap())),
events => Ok(PollData::SomethingElseHappened(events)),
},
result => Err(result),
}
}
None => Ok(PollData::EventHappened(event_type.read(file).unwrap())),
}
}
3 changes: 2 additions & 1 deletion ectool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ enum Commands {
WaitEvent {
event_type: String,
/// Timeout in milliseconds
timeout: i32,
timeout: Option<i32>,
#[arg(short, long)]
device: Option<Device>,
},
FpDownload {
Expand Down

0 comments on commit 80f4101

Please sign in to comment.