-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Line event stream support for Tokio #35
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1e69adc
Fix compiler warnings (requires rustc 1.36.0).
mgottschlag e2ae6cf
Increase MSRV to 1.36.0.
mgottschlag 139be3f
Add asynchronous line event stream for Tokio.
mgottschlag 21d1704
Deduplicate event reading code.
mgottschlag 423a1af
Bump MSRV to 1.38.0
posborne 735f8f7
Disable CI for ppc64 for now (link errors)
posborne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) 2018 The rust-gpio-cdev Project Developers. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use futures::stream::StreamExt; | ||
use gpio_cdev::*; | ||
use quicli::prelude::*; | ||
|
||
#[derive(Debug, StructOpt)] | ||
struct Cli { | ||
/// The gpiochip device (e.g. /dev/gpiochip0) | ||
chip: String, | ||
/// The offset of the GPIO line for the provided chip | ||
line: u32, | ||
} | ||
|
||
async fn do_main(args: Cli) -> std::result::Result<(), errors::Error> { | ||
let mut chip = Chip::new(args.chip)?; | ||
let line = chip.get_line(args.line)?; | ||
let mut events = AsyncLineEventHandle::new(line.events( | ||
LineRequestFlags::INPUT, | ||
EventRequestFlags::BOTH_EDGES, | ||
"gpioevents", | ||
)?)?; | ||
|
||
loop { | ||
match events.next().await { | ||
Some(event) => println!("{:?}", event?), | ||
None => break, | ||
}; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let args = Cli::from_args(); | ||
do_main(args).await.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright (c) 2018 The rust-gpio-cdev Project Developers. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! Wrapper for asynchronous programming using Tokio. | ||
|
||
use futures::ready; | ||
use futures::stream::Stream; | ||
use futures::task::{Context, Poll}; | ||
use mio::event::Evented; | ||
use mio::unix::EventedFd; | ||
use mio::{PollOpt, Ready, Token}; | ||
use tokio::io::PollEvented; | ||
|
||
use std::io; | ||
use std::os::unix::io::AsRawFd; | ||
use std::pin::Pin; | ||
|
||
use super::errors::event_err; | ||
use super::{LineEvent, LineEventHandle, Result}; | ||
|
||
struct PollWrapper { | ||
handle: LineEventHandle, | ||
} | ||
|
||
impl Evented for PollWrapper { | ||
fn register( | ||
&self, | ||
poll: &mio::Poll, | ||
token: Token, | ||
interest: Ready, | ||
opts: PollOpt, | ||
) -> io::Result<()> { | ||
EventedFd(&self.handle.file.as_raw_fd()).register(poll, token, interest, opts) | ||
} | ||
|
||
fn reregister( | ||
&self, | ||
poll: &mio::Poll, | ||
token: Token, | ||
interest: Ready, | ||
opts: PollOpt, | ||
) -> io::Result<()> { | ||
EventedFd(&self.handle.file.as_raw_fd()).reregister(poll, token, interest, opts) | ||
} | ||
|
||
fn deregister(&self, poll: &mio::Poll) -> io::Result<()> { | ||
EventedFd(&self.handle.file.as_raw_fd()).deregister(poll) | ||
} | ||
} | ||
|
||
/// Wrapper around a `LineEventHandle` which implements a `futures::stream::Stream` for interrupts. | ||
/// | ||
/// # Example | ||
/// | ||
/// The following example waits for state changes on an input line. | ||
/// | ||
/// ```no_run | ||
/// # type Result<T> = std::result::Result<T, gpio_cdev::errors::Error>; | ||
/// use futures::stream::StreamExt; | ||
/// use gpio_cdev::{AsyncLineEventHandle, Chip, EventRequestFlags, LineRequestFlags}; | ||
/// | ||
/// async fn print_events(line: u32) -> Result<()> { | ||
/// let mut chip = Chip::new("/dev/gpiochip0")?; | ||
/// let line = chip.get_line(line)?; | ||
/// let mut events = AsyncLineEventHandle::new(line.events( | ||
/// LineRequestFlags::INPUT, | ||
/// EventRequestFlags::BOTH_EDGES, | ||
/// "gpioevents", | ||
/// )?)?; | ||
/// | ||
/// loop { | ||
/// match events.next().await { | ||
/// Some(event) => println!("{:?}", event?), | ||
/// None => break, | ||
/// }; | ||
/// } | ||
/// | ||
/// Ok(()) | ||
/// } | ||
/// | ||
/// # #[tokio::main] | ||
/// # async fn main() { | ||
/// # print_events(42).await.unwrap(); | ||
/// # } | ||
/// ``` | ||
pub struct AsyncLineEventHandle { | ||
evented: PollEvented<PollWrapper>, | ||
} | ||
|
||
impl AsyncLineEventHandle { | ||
/// Wraps the specified `LineEventHandle`. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `handle` - handle to be wrapped. | ||
pub fn new(handle: LineEventHandle) -> Result<AsyncLineEventHandle> { | ||
// The file descriptor needs to be configured for non-blocking I/O for PollEvented to work. | ||
let fd = handle.file.as_raw_fd(); | ||
unsafe { | ||
let flags = libc::fcntl(fd, libc::F_GETFL, 0); | ||
libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK); | ||
} | ||
|
||
Ok(AsyncLineEventHandle { | ||
evented: PollEvented::new(PollWrapper { handle })?, | ||
}) | ||
} | ||
} | ||
|
||
impl Stream for AsyncLineEventHandle { | ||
type Item = Result<LineEvent>; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> { | ||
let ready = Ready::readable(); | ||
if let Err(e) = ready!(self.evented.poll_read_ready(cx, ready)) { | ||
return Poll::Ready(Some(Err(e.into()))); | ||
} | ||
|
||
match self.evented.get_ref().handle.read_event() { | ||
Ok(Some(event)) => Poll::Ready(Some(Ok(event))), | ||
Ok(None) => Poll::Ready(Some(Err(event_err(nix::Error::Sys( | ||
nix::errno::Errno::EIO, | ||
))))), | ||
Err(nix::Error::Sys(nix::errno::Errno::EAGAIN)) => { | ||
self.evented.clear_read_ready(cx, ready)?; | ||
Poll::Pending | ||
} | ||
Err(e) => Poll::Ready(Some(Err(event_err(e)))), | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<LineEventHandle> for AsyncLineEventHandle { | ||
fn as_ref(&self) -> &LineEventHandle { | ||
&self.evented.get_ref().handle | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it's slightly more idiomatic to write this as: