Skip to content

Commit 3f2bd12

Browse files
pokusewpetrzjunior
authored andcommitted
Add example of the API addressing the issue #30
1 parent a97c885 commit 3f2bd12

File tree

5 files changed

+122
-8
lines changed

5 files changed

+122
-8
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ pcsc.on('reader', (reader) => {
136136
137137
});
138138
139-
}
140-
else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
139+
} else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
141140
142141
console.log("card inserted");
143142

examples/example.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
"use strict";
22

33
const pcsclite = require('../lib/pcsclite');
4+
const {
5+
SCARD_SCOPE_USER,
6+
SCARD_SCOPE_TERMINAL,
7+
SCARD_SCOPE_SYSTEM,
8+
SCARD_SCOPE_GLOBAL,
9+
} = require('../lib/constants');
410

511

6-
const pcsc = pcsclite();
12+
// const pcsc = pcsclite(); // without options (scope defaults to SCARD_SCOPE_SYSTEM)
13+
const pcsc = pcsclite({ scope: SCARD_SCOPE_USER }); // overwriting default scope
714

815
pcsc.on('reader', (reader) => {
916

@@ -39,8 +46,7 @@ pcsc.on('reader', (reader) => {
3946

4047
});
4148

42-
}
43-
else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
49+
} else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
4450

4551
console.log("card inserted");
4652

examples/public.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"use strict";
2+
3+
const pcsclite = require('@pokusew/pcsclite');
4+
const {
5+
SCARD_SCOPE_USER,
6+
SCARD_SCOPE_TERMINAL,
7+
SCARD_SCOPE_SYSTEM,
8+
SCARD_SCOPE_GLOBAL,
9+
} = require('@pokusew/pcsclite/lib/constants');
10+
11+
12+
// const pcsc = pcsclite(); // without options (scope defaults to SCARD_SCOPE_SYSTEM)
13+
const pcsc = pcsclite({ scope: SCARD_SCOPE_USER }); // overwriting default scope
14+
15+
pcsc.on('reader', (reader) => {
16+
17+
console.log('New reader detected', reader.name);
18+
19+
reader.on('error', err => {
20+
console.log('Error(', reader.name, '):', err.message);
21+
});
22+
23+
reader.on('status', (status) => {
24+
25+
console.log('Status(', reader.name, '):', status);
26+
27+
// check what has changed
28+
const changes = reader.state ^ status.state;
29+
30+
if (!changes) {
31+
return;
32+
}
33+
34+
if ((changes & reader.SCARD_STATE_EMPTY) && (status.state & reader.SCARD_STATE_EMPTY)) {
35+
36+
console.log("card removed");
37+
38+
reader.disconnect(reader.SCARD_LEAVE_CARD, err => {
39+
40+
if (err) {
41+
console.log(err);
42+
return;
43+
}
44+
45+
console.log('Disconnected');
46+
47+
});
48+
49+
} else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
50+
51+
console.log("card inserted");
52+
53+
reader.connect({ share_mode: reader.SCARD_SHARE_SHARED }, (err, protocol) => {
54+
55+
if (err) {
56+
console.log(err);
57+
return;
58+
}
59+
60+
console.log('Protocol(', reader.name, '):', protocol);
61+
62+
reader.transmit(Buffer.from([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, protocol, (err, data) => {
63+
64+
if (err) {
65+
console.log(err);
66+
return;
67+
}
68+
69+
console.log('Data received', data);
70+
reader.close();
71+
pcsc.close();
72+
73+
});
74+
75+
});
76+
77+
}
78+
79+
});
80+
81+
reader.on('end', () => {
82+
console.log('Reader', reader.name, 'removed');
83+
});
84+
85+
});
86+
87+
pcsc.on('error', err => {
88+
console.log('PCSC error', err.message);
89+
});

lib/constants.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
const {
4+
SCARD_SCOPE_USER,
5+
SCARD_SCOPE_TERMINAL,
6+
SCARD_SCOPE_SYSTEM,
7+
SCARD_SCOPE_GLOBAL,
8+
} = require('../build/Release/pcsclite.node');
9+
10+
module.exports = {
11+
SCARD_SCOPE_USER,
12+
SCARD_SCOPE_TERMINAL,
13+
SCARD_SCOPE_SYSTEM,
14+
SCARD_SCOPE_GLOBAL,
15+
};

lib/pcsclite.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const EventEmitter = require('events');
88
// see https://github.com/nodejs/node-gyp/issues/263, https://github.com/nodejs/node-gyp/issues/631
99
const pcsclite = require('../build/Release/pcsclite.node');
1010

11-
const { PCSCLite, CardReader } = pcsclite;
11+
const { PCSCLite, CardReader, SCARD_SCOPE_SYSTEM } = pcsclite;
1212

1313

1414
inherits(PCSCLite, EventEmitter);
@@ -45,11 +45,16 @@ function diff(a, b) {
4545

4646
}
4747

48-
module.exports = function () {
48+
module.exports = function (options = {}) {
4949

5050
const readers = {};
5151

52-
const p = new PCSCLite();
52+
// const scope = options.scope ?? SCARD_SCOPE_SYSTEM;
53+
// ^ this syntax above (null coalescing operator) would need transpiler as it is not available in the Node.js yet
54+
const scope = options.scope !== undefined && options.scope !== null ? options.scope : SCARD_SCOPE_SYSTEM;
55+
56+
// TODO: consider alternative approach > allow call with undefined and the C++ side will supply the default value
57+
const p = new PCSCLite(scope);
5358

5459
p.readers = readers;
5560

0 commit comments

Comments
 (0)