# pcsc_mon
A lightweight, thread-safe PC/SC monitor for detecting smart card reader and card events in Rust.
This crate provides a singleton-style interface for monitoring reader addition/removal and card insertion/removal events using [pcsc](https://crates.io/crates/pcsc). It supports hotplug detection, background monitoring, and callback registration.
## Features
- Detect reader plug/unplug events.
- Detect card insert/removal events.
- Automatically manages `pcsc::Context` and `pcsc::Card` in callbacks.
- Thread-safe, event-driven API.
- Singleton pattern ensures safe concurrent access.
## Example
```rust
use pcsc_mon::PcscMonitor;
fn main() {
let mut monitor = PcscMonitor::instance();
monitor.on_reader_added(|reader| {
println!("Reader added: {}", reader);
});
monitor.on_reader_removed(|reader| {
println!("Reader removed: {}", reader);
});
monitor.on_card_inserted(|_ctx, card| {
match card.get_attribute(pcsc::Attribute::AtrString) {
Ok(atr) => println!("Card ATR: {:02X?}", atr),
Err(err) => eprintln!("Failed to read ATR: {:?}", err),
}
});
monitor.on_card_removed(|reader| {
println!("Card removed from reader: {}", reader);
});
monitor.on_error(|err|{
eprintln!("An error ocurred: {:?}", err)
});
monitor.start();
// Keep the main thread alive
loop {
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
```
## Known Quirks
- When a reader is unplugged, it is marked internally with State::IGNORE. If the same reader is re-plugged, a card must be inserted and removed again to re-trigger card events. This is a peculiarity of pcsc crate
## License
MIT License © 2025 Thernamyte Cloud & Medien UG (haftungsbeschränkt)
See [LICENSE](./LICENSE) for details.