Skip to content

Commit

Permalink
Add example of the API addressing the issue #30
Browse files Browse the repository at this point in the history
  • Loading branch information
pokusew authored and petrzjunior committed Feb 28, 2020
1 parent 6bb524a commit f757532
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 8 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ pcsc.on('reader', (reader) => {
});
}
else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
} else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
console.log("card inserted");
Expand Down
12 changes: 9 additions & 3 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
"use strict";

const pcsclite = require('../lib/pcsclite');
const {
SCARD_SCOPE_USER,
SCARD_SCOPE_TERMINAL,
SCARD_SCOPE_SYSTEM,
SCARD_SCOPE_GLOBAL,
} = require('../lib/constants');


const pcsc = pcsclite();
// const pcsc = pcsclite(); // without options (scope defaults to SCARD_SCOPE_SYSTEM)
const pcsc = pcsclite({ scope: SCARD_SCOPE_USER }); // overwriting default scope

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

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

});

}
else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {
} else if ((changes & reader.SCARD_STATE_PRESENT) && (status.state & reader.SCARD_STATE_PRESENT)) {

console.log("card inserted");

Expand Down
89 changes: 89 additions & 0 deletions examples/public.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"use strict";

const pcsclite = require('@pokusew/pcsclite');
const {
SCARD_SCOPE_USER,
SCARD_SCOPE_TERMINAL,
SCARD_SCOPE_SYSTEM,
SCARD_SCOPE_GLOBAL,
} = require('@pokusew/pcsclite/lib/constants');


// const pcsc = pcsclite(); // without options (scope defaults to SCARD_SCOPE_SYSTEM)
const pcsc = pcsclite({ scope: SCARD_SCOPE_USER }); // overwriting default scope

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);
});
15 changes: 15 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

const {
SCARD_SCOPE_USER,
SCARD_SCOPE_TERMINAL,
SCARD_SCOPE_SYSTEM,
SCARD_SCOPE_GLOBAL,
} = require('../build/Release/pcsclite.node');

module.exports = {
SCARD_SCOPE_USER,
SCARD_SCOPE_TERMINAL,
SCARD_SCOPE_SYSTEM,
SCARD_SCOPE_GLOBAL,
};
11 changes: 8 additions & 3 deletions lib/pcsclite.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const EventEmitter = require('events');
// see https://github.com/nodejs/node-gyp/issues/263, https://github.com/nodejs/node-gyp/issues/631
const pcsclite = require('../build/Release/pcsclite.node');

const { PCSCLite, CardReader } = pcsclite;
const { PCSCLite, CardReader, SCARD_SCOPE_SYSTEM } = pcsclite;


inherits(PCSCLite, EventEmitter);
Expand Down Expand Up @@ -45,11 +45,16 @@ function diff(a, b) {

}

module.exports = function () {
module.exports = function (options = {}) {

const readers = {};

const p = new PCSCLite();
// const scope = options.scope ?? SCARD_SCOPE_SYSTEM;
// ^ this syntax above (null coalescing operator) would need transpiler as it is not available in the Node.js yet
const scope = options.scope !== undefined && options.scope !== null ? options.scope : SCARD_SCOPE_SYSTEM;

// TODO: consider alternative approach > allow call with undefined and the C++ side will supply the default value
const p = new PCSCLite(scope);

p.readers = readers;

Expand Down

0 comments on commit f757532

Please sign in to comment.