#pcsc #monitor #api-bindings #smartcard-reader #hotplug

bin+lib pcsc-mon

Monitor PC/SC smart card readers with hotplug and card event support

1 unstable release

0.1.0 Jun 13, 2025

#573 in Operating systems

MIT license

15KB
175 lines

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. 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

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.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 for details.

Dependencies

~240KB