From f75753286fa44f6fe90e978f5e2eed4f87663329 Mon Sep 17 00:00:00 2001 From: Martin Endler Date: Thu, 27 Feb 2020 21:16:44 +0100 Subject: [PATCH] Add example of the API addressing the issue #30 --- README.md | 3 +- examples/example.js | 12 ++++-- examples/public.js | 89 +++++++++++++++++++++++++++++++++++++++++++++ lib/constants.js | 15 ++++++++ lib/pcsclite.js | 11 ++++-- 5 files changed, 122 insertions(+), 8 deletions(-) create mode 100755 examples/public.js create mode 100644 lib/constants.js diff --git a/README.md b/README.md index 9cb897ad..300f33f9 100644 --- a/README.md +++ b/README.md @@ -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"); diff --git a/examples/example.js b/examples/example.js index 75f27847..51de5649 100755 --- a/examples/example.js +++ b/examples/example.js @@ -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) => { @@ -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"); diff --git a/examples/public.js b/examples/public.js new file mode 100755 index 00000000..a3bc706b --- /dev/null +++ b/examples/public.js @@ -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); +}); diff --git a/lib/constants.js b/lib/constants.js new file mode 100644 index 00000000..fb1a87d0 --- /dev/null +++ b/lib/constants.js @@ -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, +}; diff --git a/lib/pcsclite.js b/lib/pcsclite.js index 363bd8e9..af797cc9 100644 --- a/lib/pcsclite.js +++ b/lib/pcsclite.js @@ -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); @@ -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;