forked from khkmzynv/node-pcsclite
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of the API addressing the issue #30
- Loading branch information
1 parent
6bb524a
commit f757532
Showing
5 changed files
with
122 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters