44//! [](https://github.com/timfish/usb-enumeration/actions)
55//!
66//! # Example
7- //! ```
8- //! let devices = usb_enumeration::enumerate();
7+ //! ```no_run
8+ //! let devices = usb_enumeration::enumerate(None, None );
99//!
1010//! println!("{:#?}", devices);
1111//!
3838//! // etc...
3939//! // ]
4040//! ```
41- //! You can also subscribe events using the `Observer`:
41+ //! You can also subscribe to events using the `Observer`:
4242//! ```no_run
4343//! use usb_enumeration::{Observer, Event};
4444//!
45- //! // Set the poll interval to 2 seconds
46- //! let sub = Observer::new (2)
45+ //! let sub = Observer::new()
46+ //! .with_poll_interval (2)
4747//! .with_vendor_id(0x1234)
4848//! .with_product_id(0x5678)
4949//! .subscribe();
@@ -83,39 +83,18 @@ use crate::linux::*;
8383
8484/// # Enumerates connected USB devices
8585///
86- /// * `vendor_id` - USB Vendor ID to filter
87- /// * `product_id` - USB Product ID to filter
86+ /// * `vendor_id` - Optional USB Vendor ID to filter
87+ /// * `product_id` - Optional USB Product ID to filter
8888///
89+ /// ```no_run
90+ /// let devices = usb_enumeration::enumerate(None, None);
8991/// ```
90- /// let devices = usb_enumeration::enumerate();
92+ /// You can also optionally filter by vendor or product ID:
93+ /// ```no_run
94+ /// let devices = usb_enumeration::enumerate(Some(0x1234), None);
9195/// ```
92- /// There are also some handy filters:
93- /// ```
94- /// use usb_enumeration::Filters;
95- ///
96- /// let devices = usb_enumeration::enumerate().with_vendor_id(0x1234);
97- /// ```
98- pub fn enumerate ( ) -> Vec < USBDevice > {
99- enumerate_platform ( )
100- }
101-
102- pub trait Filters {
103- fn with_vendor_id ( self , vendor_id : u16 ) -> Vec < USBDevice > ;
104- fn with_product_id ( self , product_id : u16 ) -> Vec < USBDevice > ;
105- }
106-
107- impl Filters for Vec < USBDevice > {
108- fn with_vendor_id ( self , vendor_id : u16 ) -> Vec < USBDevice > {
109- self . into_iter ( )
110- . filter ( |d| d. vendor_id == vendor_id)
111- . collect ( )
112- }
113-
114- fn with_product_id ( self , product_id : u16 ) -> Vec < USBDevice > {
115- self . into_iter ( )
116- . filter ( |d| d. product_id == product_id)
117- . collect ( )
118- }
96+ pub fn enumerate ( vendor_id : Option < u16 > , product_id : Option < u16 > ) -> Vec < USBDevice > {
97+ enumerate_platform ( vendor_id, product_id)
11998}
12099
121100/// Events send from the Observer
@@ -139,21 +118,32 @@ pub struct Subscription {
139118
140119#[ derive( Debug , Clone ) ]
141120pub struct Observer {
142- poll_interval : u64 ,
121+ poll_interval : u32 ,
143122 vendor_id : Option < u16 > ,
144123 product_id : Option < u16 > ,
145124}
146125
126+ impl Default for Observer {
127+ fn default ( ) -> Self {
128+ Observer :: new ( )
129+ }
130+ }
131+
147132impl Observer {
148133 /// Create a new Observer with the poll interval specified in seconds
149- pub fn new ( poll_interval : u64 ) -> Self {
134+ pub fn new ( ) -> Self {
150135 Observer {
151- poll_interval,
136+ poll_interval : 1 ,
152137 vendor_id : None ,
153138 product_id : None ,
154139 }
155140 }
156141
142+ pub fn with_poll_interval ( mut self , seconds : u32 ) -> Self {
143+ self . poll_interval = seconds;
144+ self
145+ }
146+
157147 /// Filter results by USB Vendor ID
158148 pub fn with_vendor_id ( mut self , vendor_id : u16 ) -> Self {
159149 self . vendor_id = Some ( vendor_id) ;
@@ -166,20 +156,6 @@ impl Observer {
166156 self
167157 }
168158
169- fn enumerate ( & self ) -> Vec < USBDevice > {
170- let mut devices = enumerate ( ) ;
171-
172- if let Some ( vendor_id) = self . vendor_id {
173- devices = devices. with_vendor_id ( vendor_id) ;
174- }
175-
176- if let Some ( product_id) = self . product_id {
177- devices = devices. with_product_id ( product_id) ;
178- }
179-
180- devices
181- }
182-
183159 /// Start the background thread and poll for device changes
184160 pub fn subscribe ( & self ) -> Subscription {
185161 let ( tx_event, rx_event) = unbounded ( ) ;
@@ -190,7 +166,7 @@ impl Observer {
190166 . spawn ( {
191167 let this = self . clone ( ) ;
192168 move || {
193- let device_list = this . enumerate ( ) ;
169+ let device_list = enumerate ( this . vendor_id , this . product_id ) ;
194170
195171 // Send initially connected devices
196172 if tx_event. send ( Event :: Initial ( device_list. clone ( ) ) ) . is_err ( ) {
@@ -215,7 +191,9 @@ impl Observer {
215191 wait_seconds = this. poll_interval ;
216192
217193 let next_devices: HashSet < USBDevice > =
218- this. enumerate ( ) . into_iter ( ) . collect ( ) ;
194+ enumerate ( this. vendor_id , this. product_id )
195+ . into_iter ( )
196+ . collect ( ) ;
219197
220198 // Send Disconnect for missing devices
221199 for device in & device_list {
@@ -251,17 +229,17 @@ mod tests {
251229
252230 #[ test]
253231 fn test_enumerate ( ) {
254- let devices = enumerate ( ) ;
232+ let devices = enumerate ( None , None ) ;
255233 println ! ( "Enumerated devices: {:#?}" , devices) ;
256234 assert ! ( !devices. is_empty( ) ) ;
257235 }
258236
259237 #[ test]
260238 fn test_subscribe ( ) {
261- let subscription = Observer :: new ( 1 ) . subscribe ( ) ;
239+ let subscription = Observer :: new ( ) . subscribe ( ) ;
262240 let mut iter = subscription. rx_event . iter ( ) ;
263241
264- let initial = iter. next ( ) . unwrap ( ) ;
242+ let initial = iter. next ( ) . expect ( "Should get an Event" ) ;
265243 assert ! ( matches!( initial, Event :: Initial ( _) ) ) ;
266244
267245 println ! ( "Connect a USB device" ) ;
0 commit comments