-
Notifications
You must be signed in to change notification settings - Fork 65
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
Richo/hotplug events #42
Open
richo
wants to merge
8
commits into
dcuddeback:master
Choose a base branch
from
richo:richo/hotplug-events
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9c41dff
WIP
richo 99df435
Add an example
richo b262b0e
Add the other files I forgot
richo 1651f59
Start cleaning up the implem,entation
richo dc1b197
Continue cleaning
richo efa84f8
Expose the filter api
richo 927ea29
More cleanup
richo 021168d
Turns out heap allocation is enough without needing to pin
richo 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
extern crate libusb; | ||
|
||
use std::error::Error; | ||
use std::slice; | ||
use std::str::FromStr; | ||
use std::time::Duration; | ||
|
||
#[derive(Debug)] | ||
struct Endpoint { | ||
config: u8, | ||
iface: u8, | ||
setting: u8, | ||
address: u8 | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let mut ctx = libusb::Context::new()?; | ||
ctx.register_callback(Default::default(), |device, event| { | ||
eprintln!("invoked"); | ||
println!("{:?} - {:?}", device.device_descriptor(), event); | ||
}); | ||
loop { | ||
ctx.handle_events(); | ||
} | ||
Ok(()) | ||
} | ||
|
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
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,5 @@ | ||
#[derive(Debug, Clone, Copy)] | ||
pub enum HotPlugEvent { | ||
Arrived, | ||
Left, | ||
} |
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,71 @@ | ||
use device::Device; | ||
use event::HotPlugEvent; | ||
|
||
use libusb::*; | ||
|
||
pub struct CallbackWrapper { | ||
pub cb: Box<dyn Fn(&Device, HotPlugEvent)>, | ||
pub handle: i32, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct HotplugFilter { | ||
vendor: Option<i32>, | ||
product: Option<i32>, | ||
class: Option<i32>, | ||
events: Option<i32>, | ||
} | ||
|
||
impl HotplugFilter { | ||
pub fn new() -> Self { | ||
Self { | ||
vendor: None, | ||
product: None, | ||
class: None, | ||
events: None, | ||
} | ||
} | ||
|
||
pub(crate) fn get_vendor(&self) -> i32 { | ||
self.vendor.unwrap_or(LIBUSB_HOTPLUG_MATCH_ANY) | ||
} | ||
|
||
pub(crate) fn get_product(&self) -> i32 { | ||
self.product.unwrap_or(LIBUSB_HOTPLUG_MATCH_ANY) | ||
} | ||
|
||
pub(crate) fn get_class(&self) -> i32 { | ||
self.class.unwrap_or(LIBUSB_HOTPLUG_MATCH_ANY) | ||
} | ||
|
||
pub(crate) fn get_events(&self) -> i32 { | ||
self.events.unwrap_or(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) | ||
} | ||
|
||
pub fn vendor(mut self, vendor: i32) -> Self { | ||
self.vendor = Some(vendor); | ||
self | ||
} | ||
|
||
pub fn product(mut self, product: i32) -> Self { | ||
self.product = Some(product); | ||
self | ||
} | ||
|
||
pub fn class(mut self, class: i32) -> Self { | ||
self.class = Some(class); | ||
self | ||
} | ||
|
||
pub fn arrived_only(mut self) -> Self { | ||
self.events = Some(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED); | ||
self | ||
} | ||
|
||
pub fn left_only(mut self) -> Self { | ||
self.events = Some(LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT); | ||
self | ||
} | ||
} | ||
|
||
|
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
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.
@dcuddeback This is the user facing API, I'd love your thoughts on this part. I create a native &Device for the callback, and created an Event enum to mirror the libusb side but I'm open to other ideas here.
The libusb interface for filters doesn't really map onto rust super well so I made a builder style thing to let you create filters.