forked from khkmzynv/node-pcsclite
-
Notifications
You must be signed in to change notification settings - Fork 55
/
example.js
executable file
·83 lines (52 loc) · 1.48 KB
/
example.js
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
"use strict";
const pcsclite = require('../lib/pcsclite');
const pcsc = pcsclite();
pcsc.on('reader', (reader) => {
console.log('New reader detected', reader.name);
reader.on('error', err => {
console.log('Error(', reader.name, '):', err.message);
});
reader.on('status', (status) => {
console.log('Status(', reader.name, '):', status);
// check what has changed
const changes = reader.state ^ status.state;
if (!changes) {
return;
}
if ((changes & reader.SCARD_STATE_EMPTY) && (status.state & reader.SCARD_STATE_EMPTY)) {
console.log("card removed");
reader.disconnect(reader.SCARD_LEAVE_CARD, err => {
if (err) {
console.log(err);
return;
}
console.log('Disconnected');
});
}
else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
console.log("card inserted");
reader.connect({ share_mode: reader.SCARD_SHARE_SHARED }, (err, protocol) => {
if (err) {
console.log(err);
return;
}
console.log('Protocol(', reader.name, '):', protocol);
reader.transmit(Buffer.from([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, protocol, (err, data) => {
if (err) {
console.log(err);
return;
}
console.log('Data received', data);
reader.close();
pcsc.close();
});
});
}
});
reader.on('end', () => {
console.log('Reader', reader.name, 'removed');
});
});
pcsc.on('error', err => {
console.log('PCSC error', err.message);
});