doc: Update README

- Improve format.
- Document API.
This commit is contained in:
Santiago Gimeno
2014-07-28 13:05:16 +02:00
parent c3d947860c
commit bb87f0b09c

178
README.md
View File

@@ -1,10 +1,8 @@
node-pcsclite # node-pcsclite
=============
Bindings over pcsclite to access Smart Cards Bindings over pcsclite to access Smart Cards
Installation ## Installation
============
In order to install the package you need to have installed in the system the In order to install the package you need to have installed in the system the
pcsclite libraries. In Debian/Ubuntu: pcsclite libraries. In Debian/Ubuntu:
@@ -19,65 +17,135 @@ To run any code you will also need to have installed the pcsc daemon:
apt-get install pcscd apt-get install pcscd
Example ## Example
=======
var pcsc = require('pcsclite'); ```
var pcsc = require('pcsclite');
var pcsc = pcsc(); var pcsc = pcsc();
/* Check for new card reader detection */ /* Check for new card reader detection */
pcsc.on('reader', function(reader) { pcsc.on('reader', function(reader) {
console.log('New reader detected', reader.name); console.log('New reader detected', reader.name);
/* Check for reader status changes such a new card insertion */ /* Check for reader status changes such a new card insertion */
reader.on('status', function(status) { reader.on('status', function(status) {
console.log('Status(', this.name, '):', status); console.log('Status(', this.name, '):', status);
/* check what has changed */ /* check what has changed */
var changes = this.state ^ status.state; var changes = this.state ^ status.state;
if (changes) { if (changes) {
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) { if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
console.log("card removed");/* card removed */ console.log("card removed");/* card removed */
reader.disconnect(function(err) { reader.disconnect(function(err) {
if (err) { if (err) {
console.log(err); console.log(err);
} else { } else {
console.log('Disconnected'); console.log('Disconnected');
} }
}); });
} else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) { } else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
console.log("card inserted");/* card inserted */ console.log("card inserted");/* card inserted */
reader.connect(function(err, protocol) { reader.connect(function(err, protocol) {
if (err) { if (err) {
console.log(err); console.log(err);
} else { } else {
console.log('Protocol(', this.name, '):', protocol); console.log('Protocol(', this.name, '):', protocol);
reader.transmit(new Buffer([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, protocol, function(err, data) { reader.transmit(new Buffer([0x00, 0xB0, 0x00, 0x00, 0x20]), 40, protocol, function(err, data) {
if (err) { if (err) {
console.log(err); console.log(err);
} else { } else {
console.log('Data received', data); console.log('Data received', data);
} }
}); });
} }
}); });
}
} }
}); }
reader.on('end', function() {
console.log('Reader', this.name, 'removed');
});
reader.on('error', function(err) {
console.log('Error(', this.name, '):', err.message);
});
}); });
pcsc.on('error', function(err) { reader.on('end', function() {
console.log('PCSC error', err.message); console.log('Reader', this.name, 'removed');
}); });
reader.on('error', function(err) {
console.log('Error(', this.name, '):', err.message);
});
});
pcsc.on('error', function(err) {
console.log('PCSC error', err.message);
});
```
## API
### Class: ChildProcess
The PCSCLite object is an EventEmitter that notifies the existence of Card Readers.
#### Event: 'error'
* *err* `Error Object`. The error.
#### Event: 'reader'
* *reader* `CardReader`. A CardReader object associated to the card reader detected
Emitted whenever a new card reader is detected.
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/santigimeno/node-pcsclite/trend.png)](https://bitdeli.com/free "Bitdeli Badge") ### Class: CardReader
The CardReader object is an EventEmitter that allows to manipulate a card reader.
#### Event: 'error'
* *err* `Error Object`. The error.
#### Event: 'end'
Emitted when the card reader has been removed.
#### Event: 'status'
* *status* `Object`.
* *state* The current status of the card reader as returned by `SCardGetStatusChange`
* *atr* ATR of the card inserted (if any)
Emitted whenever the status of the reader changes.
#### reader.connect(callback)
* *callback* `Function` called when connection operation ends
* *error* `Error`
* *protocol* `Number` Established protocol to this connection.
Wrapper around `SCardConnect`. Establishes a connection to the reader.
#### reader.disconnect(callback)
* *callback* `Function` called when disconnection operation ends
* *error* `Error`
Wrapper around `SCardDisconnect`. Terminates a connection to the reader.
#### reader.transmit(input, res_len, protocol, callback)
* *input* `Buffer` input data to be transmitted
* *res_len* `Number`. Max. expected length of the response
* *protocol* `Number`. Protocol to be used in the transmission
* *callback* `Function` called when disconnection operation ends
* *error* `Error`
* *output* `Buffer`
Wrapper around `SCardTransmit`. Sends an APDU to the smart card contained in the reader connected to.
#### reader.control(input, control_code, res_len, callback)
* *input* `Buffer` input data to be transmitted
* *control_code* `Number`. Control code for the operation
* *res_len* `Number`. Max. expected length of the response
* *callback* `Function` called when disconnection operation ends
* *error* `Error`
* *output* `Buffer`
Wrapper around `SCardControl`. Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.