1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! # USBWatch
//!
//! A cross-platform USB device monitoring library and command-line tool.
//!
//! USBWatch provides real-time monitoring of USB device connection and disconnection events on Linux, Windows, and macOS. It offers both a library API for integration into other applications and a standalone command-line tool.
//!
//! ## Features
//!
//! - **Cross-platform**: Linux (sysfs), Windows (Win32 APIs), macOS (IOKit)
//! - **Real-time monitoring**: Detect USB events as they happen
//! - **Multiple output formats**: Plain text and JSON
//! - **File logging**: Save events to log files
//! - **Coloured output**: Modern, readable CLI output
//! - **Async/await support**: Built with Tokio for efficient I/O
//! - **Device handle traits**: Access platform-specific device handles for advanced operations
//! - **Install/uninstall commands**: Manage CLI tool from the command line
//!
//! ## Quick Start
//!
//! ### Command Line Usage
//!
//! ```bash
//! # Monitor USB devices with default output
//! usbwatch
//!
//! # Monitor with JSON output
//! usbwatch --json
//!
//! # Monitor and log to file
//! usbwatch --logfile usb-events.log
//!
//! # Monitor with coloured output (default if supported)
//! usbwatch
//!
//! # Install or uninstall the CLI tool
//! usbwatch install
//! usbwatch uninstall
//! ```
//!
//! ### Library Usage
//!
//! ```rust,no_run
//! use usbwatch_rs::{UsbWatcher, UsbDeviceInfo, AsDeviceHandle, DeviceHandle};
//! use tokio::sync::mpsc;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let (tx, mut rx) = mpsc::channel(100);
//! let watcher = UsbWatcher::new(tx)?;
//!
//! // Start monitoring in a background task
//! tokio::spawn(async move {
//! if let Err(e) = watcher.start_monitoring().await {
//! eprintln!("Monitoring error: {}", e);
//! }
//! });
//!
//! // Process device events
//! while let Some(device_info) = rx.recv().await {
//! println!("Device event: {}", device_info);
//! // Access platform-specific device handle
//! match device_info.as_device_handle() {
//! #[cfg(target_os = "linux")]
//! DeviceHandle::Linux { sysfs_path, device_node } => {
//! println!("Linux sysfs path: {}", sysfs_path);
//! if let Some(node) = device_node {
//! println!("Device node: {}", node);
//! }
//! }
//! #[cfg(target_os = "windows")]
//! DeviceHandle::Windows { instance_id, interface_path } => {
//! println!("Windows instance ID: {}", instance_id);
//! if let Some(path) = interface_path {
//! println!("Interface path: {}", path);
//! }
//! }
//! #[cfg(target_os = "macos")]
//! DeviceHandle::Macos { device_id } => {
//! println!("macOS device ID: {}", device_id);
//! }
//! _ => {
//! println!("No platform-specific handle available");
//! }
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## Library API Highlights
//!
//! - [`UsbWatcher`] - Cross-platform watcher for USB device events
//! - [`UsbDeviceInfo`] - Struct containing device metadata and event info
//! - [`DeviceHandle`] - Enum for platform-specific device handles
//! - [`AsDeviceHandle`] - Trait for accessing device handles from device info
//! - [`create_watcher`] - Convenience function for watcher creation
//! - [`monitor_with_callback`] - High-level async monitoring with callback
//! - [`monitor_for_duration`] - Collect events for a fixed duration
//!
//! ## Platform Support
//!
//! - **Linux**: Uses sysfs filesystem (`/sys/bus/usb/devices`)
//! - **Windows**: Uses Win32 Device Installation APIs
//!
//! ## Error Handling
//!
//! All public APIs use `Result` types for proper error handling. Platform-specific
//! errors are wrapped in boxed `std::error::Error` for consistency.
// Re-export commonly used types
pub use ;
pub use ;
pub use UsbWatcher;
/// Library version information
pub const VERSION: &str = env!;
/// Library name
pub const NAME: &str = env!;
/// Library description
pub const DESCRIPTION: &str = env!;
/// A result type for USB monitoring operations
pub type Result<T> = Result;
/// Create a new USB watcher with the given channel sender.
///
/// This is a convenience function that creates a new [`UsbWatcher`] instance.
///
/// # Arguments
///
/// * `sender` - The channel sender to send USB device events to
///
/// # Returns
///
/// Returns a [`Result`] containing the [`UsbWatcher`] or an error if the watcher
/// cannot be created (e.g., on unsupported platforms).
///
/// # Examples
///
/// ```rust,no_run
/// use usbwatch_rs::{create_watcher, UsbDeviceInfo};
/// use tokio::sync::mpsc;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let (tx, mut rx) = mpsc::channel::<UsbDeviceInfo>(100);
/// let watcher = create_watcher(tx)?;
///
/// // Use the watcher...
/// Ok(())
/// }
/// ```
/// Start monitoring USB devices with a callback function.
///
/// This is a high-level convenience function that sets up monitoring and calls
/// the provided callback for each USB device event.
///
/// # Arguments
///
/// * `callback` - A function that will be called for each USB device event
///
/// # Returns
///
/// Returns a [`Result`] that completes when monitoring stops or encounters an error.
///
/// # Examples
///
/// ```rust,no_run
/// use usbwatch_rs::{monitor_with_callback, UsbDeviceInfo};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// monitor_with_callback(|device_info| {
/// println!("USB event: {}", device_info);
/// }).await?;
///
/// Ok(())
/// }
/// ```
pub async
/// Start monitoring USB devices and collect events into a vector.
///
/// This function monitors for the specified duration and returns all collected events.
/// Useful for testing or collecting a snapshot of USB activity.
///
/// # Arguments
///
/// * `duration` - How long to monitor for events
///
/// # Returns
///
/// Returns a [`Result`] containing a vector of [`UsbDeviceInfo`] events collected
/// during the monitoring period.
///
/// # Examples
///
/// ```rust,no_run
/// use usbwatch_rs::monitor_for_duration;
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let events = monitor_for_duration(Duration::from_secs(5)).await?;
/// println!("Collected {} USB events", events.len());
///
/// for event in events {
/// println!("Event: {}", event);
/// }
///
/// Ok(())
/// }
/// ```
pub async
/// Check if USB monitoring is supported on the current platform.
///
/// # Returns
///
/// Returns `true` if USB monitoring is supported on this platform, `false` otherwise.
///
/// # Examples
///
/// ```rust
/// use usbwatch_rs::is_supported;
///
/// if is_supported() {
/// println!("USB monitoring is supported on this platform");
/// } else {
/// println!("USB monitoring is not supported on this platform");
/// }
/// ```
/// Get information about the current platform's USB monitoring implementation.
///
/// # Returns
///
/// Returns a string describing the platform-specific implementation used
/// for USB monitoring.
///
/// # Examples
///
/// ```rust
/// use usbwatch_rs::platform_info;
///
/// println!("USB monitoring implementation: {}", platform_info());
/// ```