Rust
Rust
PC/SC Wait for Smart Card Status Change (Inserted, Removed from Reader, etc.)
See more SCard Examples
Demonstrates how to synchronously wait for a status change, such as for a smart card to be inserted into a reader, or removed from a reader.Note: This functionality was introduced in Chilkat v9.5.0.87.
Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let scard = chilkat::SCard::new();
// First establish a context to the PC/SC Resource Manager
if scard.establish_context("user").is_err() {
println!("{}", scard.last_error_text());
return;
}
// First we'll examine the state of all connected readers to see which have smartcards already inserted..
// Get JSON containing information about the smartcards currently inserted into readers.
// This also includes information about USB security tokens.
let json = chilkat::JsonObject::new();
if scard.find_smartcards(&json).is_err() {
println!("{}", scard.last_error_text());
return;
}
// When writing this example, I have 3 smart card readers plugged into my system.
// One of them has a smart card inserted.
// Here's the JSON returned by FindSmartcards...
// {
// "reader": [
// {
// "name": "Alcor Micro USB Smart Card Reader 0",
// "state": "empty"
// },
// {
// "name": "Generic Smart Card Reader Interface 0",
// "state": "present",
// "vendorName": "Generic",
// "serialNumber": "3230303730383138303030303030303030",
// "systemName": "Generic Smart Card Reader Interface 0",
// "card": {
// "atr": "3BFC180000813180459067464A00641606F2727E00E0",
// "windows": {
// "miniDriver": "tagliov70px.dll",
// "cryptoProvider": "Microsoft Base Smart Card Crypto Provider",
// "keyStorageProvider": "Microsoft Smart Card Key Storage Provider"
// }
// }
// },
// {
// "name": "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0",
// "state": "empty"
// }
// ]
// }
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
println!(" ");
// We can iterate over the JSON to find the readers with a smart card already inserted..
let mut name = String::new();
let sb_state = chilkat::StringBuilder::new();
let mut i = 0;
let mut count_i = json.size_of_array("reader");
while i < count_i {
json.set_i(i);
name = json.string_of("reader[i].name").unwrap_or_default();
sb_state.clear();
let _ = json.string_of_sb("reader[i].state", &sb_state);
if sb_state.contains("present", true) {
println!("{} has a smart card inserted.", name);
} else {
println!("{} does not have a smart card inserted.", name);
}
i = i + 1;
}
println!(" ");
// Now let's begin the code to wait for a change (where a smart card gets inserted or removed)
// Get the list of all readers.
let st_readers = chilkat::StringTable::new();
if scard.list_readers(&st_readers).is_err() {
println!("{}", scard.last_error_text());
return;
}
// Show the reader names..
let num_readers = st_readers.count();
i = 0;
while i < num_readers {
println!("{}: {}", i, st_readers.string_at(i).unwrap_or_default());
i = i + 1;
}
// Sample output from the above loop.
// 0: Alcor Micro USB Smart Card Reader 0
// 1: Generic Smart Card Reader Interface 0
// 2: SCM Microsystems Inc. SCR33x USB Smart Card Reader 0
//
// Synchronously wait 30 seconds for a card to be inserted or removed from any of the above readers.
let json2 = chilkat::JsonObject::new();
if scard.get_status_change(30000, &st_readers, &json2).is_err() {
println!("{}", scard.last_error_text());
return;
}
println!(" ");
// Let's see what happened...
json2.set_emit_compact(false);
println!("{}", json2.emit().unwrap_or_default());
println!(" ");
// This is what json2 contains.
// A card was inserted into the reader named "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0"
// The "numChanged" indicates that one reader's status changed.
// The "changed":true indicates the reader that changed. The state is now "present".
// {
// "numChanged": 1,
// "reader": [
// {
// "name": "Alcor Micro USB Smart Card Reader 0",
// "changed": false,
// "state": "empty"
// },
// {
// "name": "Generic Smart Card Reader Interface 0",
// "changed": false,
// "state": "present",
// "atr": "3BFC180000813180459067464A00641606F2727E00E0"
// },
// {
// "name": "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0",
// "changed": true,
// "state": "present",
// "atr": "3BDF96FF8131FE455A018048494443313158587300011B09"
// }
// ]
// }
// Find the reader that changed...
let mut changed = false;
let mut state = String::new();
let mut atr = String::new();
let num_changed = json2.int_of("numChanged");
println!("number of readers with a changed state: {}", num_changed);
i = 0;
count_i = json2.size_of_array("reader");
while i < count_i {
json2.set_i(i);
changed = json2.bool_of("reader[i].changed");
if changed {
name = json2.string_of("reader[i].name").unwrap_or_default();
state = json2.string_of("reader[i].state").unwrap_or_default();
println!("Changed: ");
println!(" reader name: {}", name);
println!(" new state: {}", state);
// If a card is now in this reader, we should have the ATR of the card..
if json2.has_member("reader[i].atr") {
atr = json2.string_of("reader[i].atr").unwrap_or_default();
println!(" ATR of card inserted into this reader: {}", atr);
}
}
i = i + 1;
}
// The output from the above loop:
// number of readers with a changed state: 1
// Changed:
// reader name: SCM Microsystems Inc. SCR33x USB Smart Card Reader 0
// new state: present
// ATR of card inserted into this reader: 3BDF96FF8131FE455A018048494443313158587300011B09
// Applications should always release the context when finished.
if scard.release_context().is_err() {
println!("{}", scard.last_error_text());
}