forked from techniq/node-pn532
-
Notifications
You must be signed in to change notification settings - Fork 1
/
frame.js
executable file
·296 lines (248 loc) · 7.34 KB
/
frame.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
'use strict';
var util = require('util');
var c = require('./constants');
var PREAMBLE = 0x00;
var START_CODE_1 = 0x00;
var START_CODE_2 = 0xFF;
var POSTAMBLE = 0x00;
/*
Represents a single communication frame for communication with the PN532 NFC Chip.
*/
class Frame {
// Gets the frame's data length
getFrameLength() {
throw new Error('Implement in subclass');
}
// Convert Frame instance to a Buffer instance
toBuffer() {
throw new Error('Implement in subclass');
}
inspect() {
return util.format('<Frame>');
}
static fromBuffer(buffer) {
if (AckFrame.isFrame(buffer)) {
return new AckFrame();
}
if (NackFrame.isFrame(buffer)) {
return new NackFrame();
}
if (ErrorFrame.isFrame(buffer)) {
return new ErrorFrame();
}
if (DataFrame.isFrame(buffer)) {
return new DataFrame(buffer);
}
throw new Error('Invalid Response');
}
static isFrame(buffer) {
return DataFrame.isFrame(buffer) ||
AckFrame.isFrame(buffer) ||
NackFrame.isFrame(buffer) ||
ErrorFrame.isFrame(buffer);
}
}
class DataFrame extends Frame {
/*
Constructor
@param {(bufer|array}} data - Complete packet data, or just the data portion of the package data
@param {number} [direction=DIRECTION_HOST_TO_PN532] - Way of message (PN532 to HOST, or HOST to PN532. See DIRECTION_* constants)
*/
constructor(data, direction) {
super();
if (data instanceof Buffer) {
var buffer = data;
var dataLength = buffer[3];
var dataStart = 6;
var dataEnd = dataStart + dataLength;
var d = new Buffer(dataLength);
buffer.copy(d, 0, dataStart, dataEnd);
this._data = d;
this.direction = buffer[5];
} else if (data instanceof Array) {
this._data = data;
this._direction = direction || c.DIRECTION_HOST_TO_PN532;
} else {
throw new Error('data must be an instanceof a Buffer or Array');
}
}
toJSON() {
return {
direction: this.getDirection(),
data: {
command: this.getDataCommand(),
body: this.getDataBody(),
// checksum: this.getDataChecksum(),
// length: this.getDataLength(),
// lengthChecksum: this.getDataLengthChecksum()
},
};
}
inspect() {
return util.format('<DataFrame %j>', this.toJSON());
}
// Gets the frame's direction
getDirection() {
return this._direction;
}
// Gets the frame's data
getData() {
return this._data;
}
getDataCommand() {
return this._data[0];
}
getDataBody() {
return this._data.slice(1);
}
// Gets the frame's data length
getDataLength() {
return this._data.length + 1;
}
// Gets the checksum of getDataLength().
getDataLengthChecksum() {
return (~this.getDataLength() & 0xFF) + 0x01;
}
// Gets a checksum for the frame's data.
getDataChecksum() {
var dataCopy = this._data.slice();
dataCopy.push(this._direction);
var sum = dataCopy.reduce((prev,current) => prev + current);
var inverse = (~sum & 0xFF) + 0x01;
if (inverse > 255) {
inverse = inverse - 255;
}
return inverse;
}
getFrameLength() {
var frameLengthMinusData = 7;
return this._data.length + frameLengthMinusData;
}
static isFrame(buffer) {
// Checks if a buffer from the PN532 is valid.
var frameLengthMinusData = 7;
if (buffer.length <= frameLengthMinusData) {
return false;
}
var dataLength = buffer[3];
var validFrameLength = frameLengthMinusData + dataLength;
if (buffer.length < validFrameLength) {
return false;
}
// TODO: Check LCS and DCS checksums
return (buffer[0] === PREAMBLE &&
buffer[1] === START_CODE_1 &&
buffer[2] === START_CODE_2 &&
buffer[validFrameLength - 1] === 0x00);
}
toBuffer() {
var array = [].concat([
PREAMBLE,
START_CODE_1,
START_CODE_2,
this.getDataLength(),
this.getDataLengthChecksum(),
this.getDirection()
], this._data, [
this.getDataChecksum(),
POSTAMBLE
]);
return new Buffer(array);
}
}
class AckFrame extends Frame {
constructor() {
super();
}
getFrameLength() {
return 6;
}
static isFrame(buffer) {
// Checks if the buffer is an ACK frame. [00 00 FF 00 FF 00]
return (buffer.length >= 6 &&
buffer[0] === PREAMBLE &&
buffer[1] === START_CODE_1 &&
buffer[2] === START_CODE_2 &&
buffer[3] === 0x00 &&
buffer[4] === 0xFF &&
buffer[5] === POSTAMBLE);
}
toBuffer() {
return new Buffer([
PREAMBLE,
START_CODE_1,
START_CODE_2,
0x00,
0xFF,
POSTAMBLE
]);
}
inspect() {
return util.format('<AckFrame %j>', this.toBuffer());
}
}
class NackFrame extends Frame {
constructor() {
super();
}
getFrameLength() {
return 6;
}
static isFrame(buffer) {
// Checks if the buffer is an NACK frame. [00 00 FF FF 00 00]
return (buffer.length >= 6 &&
buffer[0] === PREAMBLE &&
buffer[1] === START_CODE_1 &&
buffer[2] === START_CODE_2 &&
buffer[3] === 0xFF &&
buffer[4] === 0x00 &&
buffer[5] === POSTAMBLE);
}
toBuffer() {
return new Buffer([
PREAMBLE,
START_CODE_1,
START_CODE_2,
0xFF,
0x00,
POSTAMBLE
]);
}
inspect() {
return util.format('<NackFrame %j>', this.toBuffer());
}
}
class ErrorFrame extends DataFrame {
constructor() {
super([0x7F]);
}
static isFrame(buffer) {
// Checks if the buffer is an error frame. [00 00 FF 01 FF 7F 81 00]
return (buffer.length >= 8 &&
buffer[0] === PREAMBLE &&
buffer[1] === START_CODE_1 &&
buffer[2] === START_CODE_2 &&
buffer[3] === 0x01 && // Packet length
buffer[4] === 0xFF && // Packet length checksum
buffer[5] === 0x7F && // Specific application level error code
buffer[6] === 0x81 && // Packet data checksum
buffer[7] === POSTAMBLE);
}
toBuffer() {
return new Buffer([
PREAMBLE,
START_CODE_1,
START_CODE_2,
0x01, // Packet length
0xFF, // Packet length checksum
0x7F, // Specific application level error code
0x81, // Packet data checksum
POSTAMBLE
]);
}
}
exports.Frame = Frame;
exports.DataFrame = DataFrame;
exports.AckFrame = AckFrame;
exports.NackFrame = NackFrame;
exports.ErrorFrame = ErrorFrame;