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 committed Feb 27, 2020
1 parent fc540b5 commit deaaa72
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 7 deletions.
9 changes: 8 additions & 1 deletion 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
90 changes: 90 additions & 0 deletions examples/public.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"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,
};
20 changes: 14 additions & 6 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 Expand Up @@ -128,7 +133,8 @@ CardReader.prototype.connect = function (options, cb) {

if (!this.connected) {
this._connect(options.share_mode, options.protocol, cb);
} else {
}
else {
cb();
}

Expand All @@ -147,7 +153,8 @@ CardReader.prototype.disconnect = function (disposition, cb) {

if (this.connected) {
this._disconnect(disposition, cb);
} else {
}
else {
cb();
}

Expand Down Expand Up @@ -187,7 +194,8 @@ CardReader.prototype.SCARD_CTL_CODE = function (code) {

if (isWin) {
return (0x31 << 16 | (code) << 2);
} else {
}
else {
return 0x42000000 + (code);
}

Expand Down

0 comments on commit deaaa72

Please sign in to comment.