-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
263 lines (202 loc) · 9.37 KB
/
index.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html
import pcsc = require('pcsclite');
import Reader from './reader';
import SmartCard from './cards/smartcard';
import { Sle } from './cards/memorycard';
import { CardEvent } from './cards/smartcard';
export class TsCard {
private static _instance : TsCard;
private _pcsc : any;
private _reader : any;
private readonly _defaultTimeout : number;
constructor() {
this._pcsc = pcsc();
this._defaultTimeout = 15000;
this._reader = null;
}
static get instance () : TsCard {
if (this._instance == null)
this._instance = new TsCard();
return this._instance;
}
//#region Methods
async detectReader(timeout? : number) : Promise<Reader> {
if (timeout == null)
timeout = this._defaultTimeout;
return new Promise<Reader>((resolve,reject) => {
let cmdTimeout = setTimeout(() => {
this._pcsc.close();
reject(`Timeout: no card reader detected after ${timeout}ms`);
}, timeout);
this._pcsc.on('reader', reader => {
this._reader = reader;
clearTimeout(cmdTimeout);
return resolve(new Reader(reader));
});
this._pcsc.on('error', function(err) {
reject(`PCSC error: ${err}`);
});
});
}
async insertCard(timeout? : number) : Promise<[boolean,SmartCard?]> {
if (timeout == null)
timeout = this._defaultTimeout;
return new Promise<[boolean,SmartCard?]>((resolve,reject) => {
let cmdTimeout = setTimeout(() => {
resolve([false]);
}, timeout);
let actualReader = this._reader;
actualReader.on('status', function(status) {
var changes = this.state ^ status.state;
if (changes) {
if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
/* card inserted */
actualReader.connect({ share_mode : this.SCARD_SHARE_SHARED }, function(err, protocol) {
if (err) {
reject(`Connect error: ${err}`);
} else {
clearTimeout(cmdTimeout);
if (Sle.isSupportedMemoryCard(actualReader,[...status.atr]))
return resolve([true, new Sle(new Reader(actualReader), [...status.atr], protocol)]);
else
return resolve([true,new SmartCard([...status.atr], protocol)]);
}
});
}
}
});
this._pcsc.on('error', function(err) {
reject(`PCSC error: ${err}`);
});
});
}
async removeCard(timeout? : number) : Promise<boolean> {
if (timeout == null)
timeout = this._defaultTimeout;
return new Promise<boolean>((resolve,reject) => {
let cmdTimeout = setTimeout(() => {
resolve(false);
}, timeout);
this._reader.on('status', function(status) {
var changes = this.state ^ status.state;
if (changes) {
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
/* card removed */
this._pcsc.reader.disconnect(this._pcsc.reader.SCARD_LEAVE_CARD, function(err) {
if (err) {
reject(`Disconnect error: ${err}`);
} else {
resolve(true);
}
});
};
};
});
});
}
close() : void {
if (this._reader)
this._reader.close();
if (this._pcsc)
this._pcsc.close();
}
//#endregion
//#region Events
onCardEvent(f : (event: CardEvent, card: SmartCard, e : Error) => void) {
let actualReader = this._reader;
actualReader.on('status', function(status) {
var changes = this.state ^ status.state;
if (changes) {
if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
/* card removed */
actualReader.disconnect(actualReader.SCARD_LEAVE_CARD, function(err) {
if (err) {
f(null, null, new Error(err));
} else {
f(CardEvent.Removed, null, null);
}
});
} else
if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
/* card inserted */
actualReader.connect({ share_mode : this.SCARD_SHARE_SHARED }, function(err, protocol) {
if (err) {
f(null, null, new Error(err));
} else {
if (Sle.isSupportedMemoryCard(actualReader,[...status.atr]))
f(CardEvent.Inserted, new Sle(new Reader(actualReader), [...status.atr], protocol), null);
else
f(CardEvent.Inserted, new SmartCard([...status.atr], protocol), null);
}
});
}
}
});
this._pcsc.on('error', function(err) {
f(null, null, new Error(err));
});
}
//#endregion
// static rawDemo() : void {
// let pcsc = new Pcsc();
// pcsc.on('reader', function(reader) {
// console.log('New reader detected', reader.name);
// reader.on('error', function(err) {
// console.log('Error(', this.name, '):', err.message);
// });
// reader.on('status', function(status) {
// console.log('Status(', this.name, '):', status);
// /* check what has changed */
// var changes = this.state ^ status.state;
// if (changes) {
// if ((changes & this.SCARD_STATE_EMPTY) && (status.state & this.SCARD_STATE_EMPTY)) {
// console.log("card removed");/* card removed */
// reader.disconnect(reader.SCARD_LEAVE_CARD, function(err) {
// if (err) {
// console.log(err);
// } else {
// console.log('Disconnected');
// }
// });
// } else if ((changes & this.SCARD_STATE_PRESENT) && (status.state & this.SCARD_STATE_PRESENT)) {
// console.log("card inserted");/* card inserted */
// reader.connect({ share_mode : this.SCARD_SHARE_SHARED }, function(err, protocol) {
// if (err) {
// console.log(err);
// } else {
// console.log('Protocol(', reader.name, '):', protocol);
// console.log('ATR: ', status.atr);
// console.log('Select CardType');
// reader.transmit(new Buffer([0xFF, 0xA4, 0x00, 0x00, 0x01, 0x05]), 2, protocol, function(err, data) {
// if (err) {
// console.log(err);
// } else {
// console.log('Data received', data);
// reader.close();
// pcsc.close();
// }
// console.log('Read Bytes2');
// reader.transmit(new Buffer([0xFF, 0xB0, 0x00, 35, 0x10]), 100, protocol, function(err, data) {
// if (err) {
// console.log(err);
// } else {
// console.log('Data received', data);
// reader.close();
// pcsc.close();
// }
// });
// });
// }
// });
// }
// }
// });
// reader.on('end', function() {
// console.log('Reader', this.name, 'removed');
// });
// });
// pcsc.on('error', function(err) {
// console.log('PCSC error', err.message);
// });
// }
}