-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (38 loc) · 1.45 KB
/
index.js
File metadata and controls
41 lines (38 loc) · 1.45 KB
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
'use strict';
const checkChange = (reader, status, constant) => ((reader.state ^ status.state) & reader[constant]) && (status.state & reader[constant]);
const cardRemoved = (reader, status) => checkChange(reader, status, 'SCARD_STATE_EMPTY');
const cardPresented = (reader, status) => checkChange(reader, status, 'SCARD_STATE_PRESENT');
const pcsclite = require('pcsclite');
const pcsc = pcsclite();
pcsc
.on('reader', reader => {
console.log('new reader detected', reader.name);
reader.on('error', console.error);
reader.on('status', status => {
const changes = reader.state ^ status.state;
console.log('change detected on', reader.name);
console.log(`changes: ${Number(changes).toString(16)}`);
if (changes) {
console.log('changes detected');
if (cardRemoved(reader, status)) {
console.log('card removed');
}
if (cardPresented(reader, status)) {
console.log('card presented');
reader.connect((err, proto) => {
if (err) {
console.error('reader.connect error', err);
return;
}
console.log('proto', proto);
});
}
console.dir(status.atr);
console.log(status.atr.toString('hex'));
} else {
console.log('no changes detected');
}
});
reader.on('end', () => console.log(`${reader.name} disconnected`));
})
.on('error', err => console.error('pcsc', err));