fixed parse readers string function
added .editorconfig formatted code updated dependencies improved package.json fixed version number
This commit is contained in:
255
lib/pcsclite.js
255
lib/pcsclite.js
@@ -1,155 +1,174 @@
|
||||
"use strict";
|
||||
|
||||
var events = require('events');
|
||||
/****************************************************************************** var bt = require('buffertools'); */
|
||||
const EventEmitter = require('events');
|
||||
const pcsclite = require('bindings')('pcsclite');
|
||||
const {PCSCLite, CardReader} = pcsclite;
|
||||
|
||||
/* Make sure we choose the correct build directory */
|
||||
var bindings = require('bindings')('pcsclite');
|
||||
var PCSCLite = bindings.PCSCLite;
|
||||
var CardReader = bindings.CardReader;
|
||||
inherits(PCSCLite, events.EventEmitter);
|
||||
inherits(CardReader, events.EventEmitter);
|
||||
/**
|
||||
var parse_readers_string = function(readers_str) {
|
||||
var pos;
|
||||
var readers = [];
|
||||
var ini = 0;
|
||||
while ((pos = bt.indexOf(readers_str.slice(ini), '\0')) > 0) {
|
||||
readers.push(readers_str.slice(ini, ini + pos).toString());
|
||||
ini += pos + 1;
|
||||
}
|
||||
|
||||
return readers;
|
||||
};
|
||||
*/
|
||||
inherits(PCSCLite, EventEmitter);
|
||||
inherits(CardReader, EventEmitter);
|
||||
|
||||
var parse_readers_string_by_hapet = function(readers_str) {
|
||||
function parseReadersString(buffer) {
|
||||
|
||||
// without buffertool module ;)
|
||||
const string = buffer.toString().slice(0, -1);
|
||||
|
||||
// it looks like
|
||||
// ACS ACR122U PICC Interface\u0000ACS ACR122U PICC Interface 01\u0000\u0000
|
||||
// [reader_name]\u0000[reader_name]\u0000\u0000
|
||||
// ^separator ^separator^end_separator
|
||||
|
||||
// returns readers in array
|
||||
// like [ 'ACS ACR122U PICC Interface', 'ACS ACR122U PICC Interface 01' ]
|
||||
|
||||
return string.split('\u0000').slice(0, -1);
|
||||
|
||||
return [readers_str];
|
||||
}
|
||||
|
||||
/*
|
||||
* It returns an array with the elements contained in a that aren't contained in b
|
||||
*/
|
||||
function diff(a, b) {
|
||||
return a.filter(function(i) {
|
||||
return b.indexOf(i) === -1;
|
||||
});
|
||||
|
||||
return a.filter(i => b.indexOf(i) === -1);
|
||||
|
||||
}
|
||||
|
||||
module.exports = function () {
|
||||
|
||||
const readers = {};
|
||||
|
||||
const p = new PCSCLite();
|
||||
|
||||
process.nextTick(function () {
|
||||
|
||||
p.start(function (err, data) {
|
||||
|
||||
if (err) {
|
||||
return p.emit('error', err);
|
||||
}
|
||||
|
||||
const names = parseReadersString(data);
|
||||
|
||||
const currentNames = Object.keys(readers);
|
||||
const newNames = diff(names, currentNames);
|
||||
const removedNames = diff(currentNames, names);
|
||||
|
||||
newNames.forEach(function (name) {
|
||||
|
||||
const r = new CardReader(name);
|
||||
|
||||
r.on('_end', function () {
|
||||
r.removeAllListeners('status');
|
||||
r.emit('end');
|
||||
delete readers[name];
|
||||
});
|
||||
|
||||
readers[name] = r;
|
||||
|
||||
r.get_status(function (err, state, atr) {
|
||||
|
||||
if (err) {
|
||||
return r.emit('error', err);
|
||||
}
|
||||
|
||||
const status = {state: state};
|
||||
|
||||
if (atr) {
|
||||
status.atr = atr;
|
||||
}
|
||||
|
||||
r.emit('status', status);
|
||||
|
||||
r.state = state;
|
||||
|
||||
});
|
||||
|
||||
p.emit('reader', r);
|
||||
|
||||
});
|
||||
|
||||
removedNames.forEach(function (name) {
|
||||
readers[name].close();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return p;
|
||||
};
|
||||
|
||||
module.exports = function() {
|
||||
CardReader.prototype.connect = function (options, cb) {
|
||||
|
||||
var readers = {};
|
||||
var p = new PCSCLite();
|
||||
process.nextTick(function() {
|
||||
p.start(function(err, data) {
|
||||
if (err) {
|
||||
return p.emit('error', err);
|
||||
}
|
||||
if (typeof options === 'function') {
|
||||
cb = options;
|
||||
options = undefined;
|
||||
}
|
||||
|
||||
/************************************************************* var names = parse_readers_string(data); */
|
||||
var names = parse_readers_string_by_hapet(data);
|
||||
options = options || {};
|
||||
options.share_mode = options.share_mode || this.SCARD_SHARE_EXCLUSIVE;
|
||||
options.protocol = options.protocol || this.SCARD_PROTOCOL_T0 | this.SCARD_PROTOCOL_T1;
|
||||
|
||||
var current_names = Object.keys(readers);
|
||||
var new_names = diff(names, current_names);
|
||||
var removed_names = diff(current_names, names);
|
||||
if (!this.connected) {
|
||||
this._connect(options.share_mode, options.protocol, cb);
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
|
||||
new_names.forEach(function(name) {
|
||||
var r = new CardReader(name);
|
||||
r.on('_end', function() {
|
||||
r.removeAllListeners('status');
|
||||
r.emit('end');
|
||||
delete readers[name];
|
||||
});
|
||||
|
||||
readers[name] = r;
|
||||
r.get_status(function(err, state, atr) {
|
||||
if (err) {
|
||||
return r.emit('error', err);
|
||||
}
|
||||
|
||||
var status = { state : state };
|
||||
if (atr) {
|
||||
status.atr = atr;
|
||||
}
|
||||
|
||||
r.emit('status', status);
|
||||
r.state = state;
|
||||
});
|
||||
|
||||
p.emit('reader', r);
|
||||
});
|
||||
|
||||
removed_names.forEach(function(name) {
|
||||
readers[name].close();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return p;
|
||||
};
|
||||
|
||||
CardReader.prototype.connect = function(options, cb) {
|
||||
if (typeof options === 'function') {
|
||||
cb = options;
|
||||
options = undefined;
|
||||
}
|
||||
CardReader.prototype.disconnect = function (disposition, cb) {
|
||||
|
||||
options = options || {};
|
||||
options.share_mode = options.share_mode || this.SCARD_SHARE_EXCLUSIVE;
|
||||
options.protocol = options.protocol || this.SCARD_PROTOCOL_T0 | this.SCARD_PROTOCOL_T1;
|
||||
if (typeof disposition === 'function') {
|
||||
cb = disposition;
|
||||
disposition = undefined;
|
||||
}
|
||||
|
||||
if (typeof disposition !== 'number') {
|
||||
disposition = this.SCARD_UNPOWER_CARD;
|
||||
}
|
||||
|
||||
if (this.connected) {
|
||||
this._disconnect(disposition, cb);
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
|
||||
if (!this.connected) {
|
||||
this._connect(options.share_mode, options.protocol, cb);
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
};
|
||||
|
||||
CardReader.prototype.disconnect = function(disposition, cb) {
|
||||
if (typeof disposition === 'function') {
|
||||
cb = disposition;
|
||||
disposition = undefined;
|
||||
}
|
||||
CardReader.prototype.transmit = function (data, res_len, protocol, cb) {
|
||||
|
||||
if (typeof disposition !== 'number') {
|
||||
disposition = this.SCARD_UNPOWER_CARD;
|
||||
}
|
||||
if (!this.connected) {
|
||||
return cb(new Error('Card Reader not connected'));
|
||||
}
|
||||
|
||||
this._transmit(data, res_len, protocol, cb);
|
||||
|
||||
if (this.connected) {
|
||||
this._disconnect(disposition, cb);
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
};
|
||||
|
||||
CardReader.prototype.transmit = function(data, res_len, protocol, cb) {
|
||||
if (!this.connected) {
|
||||
return cb(new Error("Card Reader not connected"));
|
||||
}
|
||||
CardReader.prototype.control = function (data, control_code, res_len, cb) {
|
||||
|
||||
this._transmit(data, res_len, protocol, cb);
|
||||
};
|
||||
if (!this.connected) {
|
||||
return cb(new Error('Card Reader not connected'));
|
||||
}
|
||||
|
||||
CardReader.prototype.control = function(data, control_code, res_len, cb) {
|
||||
if (!this.connected) {
|
||||
return cb(new Error("Card Reader not connected"));
|
||||
}
|
||||
const output = new Buffer(res_len);
|
||||
|
||||
var output = new Buffer(res_len);
|
||||
this._control(data, control_code, output, function(err, len) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
this._control(data, control_code, output, function (err, len) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
cb(err, output.slice(0, len));
|
||||
});
|
||||
|
||||
cb(err, output.slice(0, len));
|
||||
});
|
||||
};
|
||||
|
||||
// extend prototype
|
||||
function inherits(target, source) {
|
||||
for (var k in source.prototype) {
|
||||
target.prototype[k] = source.prototype[k];
|
||||
}
|
||||
|
||||
for (const k in source.prototype) {
|
||||
target.prototype[k] = source.prototype[k];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user