-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
123 lines (93 loc) · 4.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
var express = require('express'),
cors = require('cors')
pcsc = require('pcsclite');
var app = express(),
pcsc = pcsc(),
lastRead = false;
// Enabling CORS for all routes.
app.use(cors());
// API
app.get('/', function (req, res) {
res.send(lastRead);
// Preventing multiple reads of the same read.
lastRead = false;
});
// Start web server.
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Remote RFID server API available at http://%s:%s', host, port);
});
// PC/SC interface.
pcsc.on('reader', function(reader) {
console.log('Reader detected:', reader);
reader.on('error', function(err) {
console.log('Error(', reader.name, '):', err.message);
});
reader.on('status', function(status) {
console.log('Status(', reader.name, '):', status);
// Check changes.
var changes = this.state ^ status.state;
if (changes) {
// Card removed.
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
console.log('Status(', reader.name, '): Card removed');
reader.disconnect(reader.SCARD_LEAVE_CARD, function(err) {
if (err) {
console.log('Error(', reader.name, '):', err);
}
else {
console.log('Status(', reader.name, '): Disconnected');
}
});
}
// Card inserted.
else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
console.log('Status(', reader.name, '): Card inserted');
reader.connect({ share_mode : this.SCARD_SHARE_SHARED }, function(err, protocol) {
if (err) {
console.log('Error(', reader.name, '):', err);
}
else {
console.log('Protocol(', reader.name, '):', protocol);
/*
Read card UID: [0xFF, 0xCA, 0x00, 0x00, 0x00]
UID is specified in the ISO 14443 T=CL transport protocol while APDU's are specified in the ISO 7816 application layer protocol.
"Get Data Command" is defined in PCSC 3 v2. If your driver is PCSC v2 compliant, you can get UID using it:
Class = 0xFF
INS = 0xCA
P1 = 0x00
P2 = 0x00
Le = 0x00 (return full length: ISO14443A single 4 bytes, double 7 bytes, triple 10 bytes, for ISO14443B 4 bytes PUPI, for 15693 8 bytes UID)
Expected response: Data+SW1SW2
*/
var message = new Buffer([0xFF, 0xCA, 0x00, 0x00, 0x00]);
reader.transmit(message, 40, protocol, function(err, data) {
if (err) {
console.log('Error(', reader.name, '):', err);
}
else {
/*
buf.readUIntLE(offset, byteLength[, noAssert])
Set noAssert to true to skip validation of value and offset. Defaults to false.
*/
lastRead = data.readUIntBE(0, 6, true).toString(16);
console.log('Status(', reader.name, '): Read:', data, ' toString:', lastRead);
}
});
}
});
}
}
});
reader.on('end', function() {
console.log('Status(', reader.name, '): Removed');
// Release resources.
reader.close();
pcsc.close();
});
});
pcsc.on('error', function(err) {
console.log('Error( PCSC ): ', err);
});