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
use pcsc::*;
use std::thread::{ self, JoinHandle };
use std::collections::HashMap;
use std::ffi::CStr;

mod badge;
mod ndef;
pub use badge::NFCBadge;

pub fn handle_cards<F, G>(card_handler: F, reader_handler: G) -> JoinHandle<()>
	where F: Fn(&Card, &CStr, usize),
		  F: Send + 'static,
		  G: Fn(&CStr, bool),
		  G: Send + 'static,
{
	thread::spawn(move || {
		let ctx = Context::establish(Scope::User).expect("Failed to establish context");

		let mut readers_buf = [0; 2048];
		let mut reader_states = vec![
			// Listen for reader insertions/removals, if supported
			ReaderState::new(PNP_NOTIFICATION(), State::UNAWARE),
		];
		// Keeps track of which readers have an active card
		let mut readers = HashMap::new();
		loop {
			// Remove dead readers
			fn is_invalid(rs: &ReaderState) -> bool {
				rs.event_state().intersects(State::UNKNOWN | State::IGNORE)
			}
			reader_states.retain(|rs| {
				let shouldKeep = !is_invalid(rs);
				if !shouldKeep {
					// Notify about removal
					reader_handler(rs.name(), false);
				}
				shouldKeep
			});

			// Add new readers
			let names = ctx.list_readers(&mut readers_buf).expect("Failed to list readers");
			for name in names {
				// Ignore the pseudo reader created by Windows Hello
				if !reader_states.iter().any(|rs| rs.name() == name) && !name.to_str().unwrap().contains("Windows Hello") {
					reader_handler(name, true);
					reader_states.push(ReaderState::new(name, State::UNAWARE));
				}
			}

			// Update the view of the state to wait on
			for rs in &mut reader_states {
				rs.sync_current_state();
			}

			// Wait until the state changes
			ctx.get_status_change(None, &mut reader_states).expect("Failed to get status change");
			for (reader_index, rs) in reader_states.iter().enumerate() {
				if rs.name() == PNP_NOTIFICATION() { continue; }

				let name = rs.name().to_owned();
				// Debounce repeated events
				if rs.event_state().intersects(State::PRESENT) {
					if !readers.get(&name).unwrap_or(&false) {
						// Card is tapped
						// Connect to the card.
						match ctx.connect(rs.name(), ShareMode::Shared, Protocols::ANY) {
							Ok(card) => card_handler(&card, rs.name(), reader_index),
							Err(Error::NoSmartcard) => {
								eprintln!("A smartcard is not present in the reader");
							}
							Err(err) => {
								eprintln!("Failed to connect to card: {}", err);
							}
						};
					}
					readers.insert(name, true);
				}
				else if rs.event_state().intersects(State::EMPTY) {
					readers.insert(name, false);
				}
			}
		}
	})
}